daily update
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
64d03ab5 2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
24f58f47 3 2005, 2006, 2007, 2008, 2009, 2010, 2011
9dbe8890 4 Free Software Foundation, Inc.
252b5132 5
8fdd7217 6 This file is part of BFD, the Binary File Descriptor library.
252b5132 7
8fdd7217
NC
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
cd123cb7 10 the Free Software Foundation; either version 3 of the License, or
8fdd7217 11 (at your option) any later version.
252b5132 12
8fdd7217
NC
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
252b5132 17
8fdd7217
NC
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
cd123cb7
NC
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
252b5132 22
252b5132 23#include "sysdep.h"
3db64b00 24#include "bfd.h"
252b5132
RH
25#include "bfdlink.h"
26#include "libbfd.h"
27#define ARCH_SIZE 0
28#include "elf-bfd.h"
4ad4eba5 29#include "safe-ctype.h"
ccf2f652 30#include "libiberty.h"
66eb6687 31#include "objalloc.h"
252b5132 32
28caa186
AM
33/* This struct is used to pass information to routines called via
34 elf_link_hash_traverse which must return failure. */
35
36struct elf_info_failed
37{
38 struct bfd_link_info *info;
39 struct bfd_elf_version_tree *verdefs;
40 bfd_boolean failed;
41};
42
43/* This structure is used to pass information to
44 _bfd_elf_link_find_version_dependencies. */
45
46struct elf_find_verdep_info
47{
48 /* General link information. */
49 struct bfd_link_info *info;
50 /* The number of dependencies. */
51 unsigned int vers;
52 /* Whether we had a failure. */
53 bfd_boolean failed;
54};
55
56static bfd_boolean _bfd_elf_fix_symbol_flags
57 (struct elf_link_hash_entry *, struct elf_info_failed *);
58
d98685ac
AM
59/* Define a symbol in a dynamic linkage section. */
60
61struct elf_link_hash_entry *
62_bfd_elf_define_linkage_sym (bfd *abfd,
63 struct bfd_link_info *info,
64 asection *sec,
65 const char *name)
66{
67 struct elf_link_hash_entry *h;
68 struct bfd_link_hash_entry *bh;
ccabcbe5 69 const struct elf_backend_data *bed;
d98685ac
AM
70
71 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
72 if (h != NULL)
73 {
74 /* Zap symbol defined in an as-needed lib that wasn't linked.
75 This is a symptom of a larger problem: Absolute symbols
76 defined in shared libraries can't be overridden, because we
77 lose the link to the bfd which is via the symbol section. */
78 h->root.type = bfd_link_hash_new;
79 }
80
81 bh = &h->root;
82 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
83 sec, 0, NULL, FALSE,
84 get_elf_backend_data (abfd)->collect,
85 &bh))
86 return NULL;
87 h = (struct elf_link_hash_entry *) bh;
88 h->def_regular = 1;
e28df02b 89 h->non_elf = 0;
d98685ac
AM
90 h->type = STT_OBJECT;
91 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
92
ccabcbe5
AM
93 bed = get_elf_backend_data (abfd);
94 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
95 return h;
96}
97
b34976b6 98bfd_boolean
268b6b39 99_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
100{
101 flagword flags;
aad5d350 102 asection *s;
252b5132 103 struct elf_link_hash_entry *h;
9c5bfbb7 104 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 105 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
106
107 /* This function may be called more than once. */
aad5d350
AM
108 s = bfd_get_section_by_name (abfd, ".got");
109 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
b34976b6 110 return TRUE;
252b5132 111
e5a52504 112 flags = bed->dynamic_sec_flags;
252b5132 113
6de2ae4a
L
114 s = bfd_make_section_with_flags (abfd,
115 (bed->rela_plts_and_copies_p
116 ? ".rela.got" : ".rel.got"),
117 (bed->dynamic_sec_flags
118 | SEC_READONLY));
119 if (s == NULL
120 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
121 return FALSE;
122 htab->srelgot = s;
252b5132 123
64e77c6d
L
124 s = bfd_make_section_with_flags (abfd, ".got", flags);
125 if (s == NULL
126 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
127 return FALSE;
128 htab->sgot = s;
129
252b5132
RH
130 if (bed->want_got_plt)
131 {
3496cb2a 132 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
252b5132 133 if (s == NULL
6de2ae4a
L
134 || !bfd_set_section_alignment (abfd, s,
135 bed->s->log_file_align))
b34976b6 136 return FALSE;
6de2ae4a 137 htab->sgotplt = s;
252b5132
RH
138 }
139
64e77c6d
L
140 /* The first bit of the global offset table is the header. */
141 s->size += bed->got_header_size;
142
2517a57f
AM
143 if (bed->want_got_sym)
144 {
145 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
146 (or .got.plt) section. We don't do this in the linker script
147 because we don't want to define the symbol if we are not creating
148 a global offset table. */
6de2ae4a
L
149 h = _bfd_elf_define_linkage_sym (abfd, info, s,
150 "_GLOBAL_OFFSET_TABLE_");
2517a57f 151 elf_hash_table (info)->hgot = h;
d98685ac
AM
152 if (h == NULL)
153 return FALSE;
2517a57f 154 }
252b5132 155
b34976b6 156 return TRUE;
252b5132
RH
157}
158\f
7e9f0867
AM
159/* Create a strtab to hold the dynamic symbol names. */
160static bfd_boolean
161_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
162{
163 struct elf_link_hash_table *hash_table;
164
165 hash_table = elf_hash_table (info);
166 if (hash_table->dynobj == NULL)
167 hash_table->dynobj = abfd;
168
169 if (hash_table->dynstr == NULL)
170 {
171 hash_table->dynstr = _bfd_elf_strtab_init ();
172 if (hash_table->dynstr == NULL)
173 return FALSE;
174 }
175 return TRUE;
176}
177
45d6a902
AM
178/* Create some sections which will be filled in with dynamic linking
179 information. ABFD is an input file which requires dynamic sections
180 to be created. The dynamic sections take up virtual memory space
181 when the final executable is run, so we need to create them before
182 addresses are assigned to the output sections. We work out the
183 actual contents and size of these sections later. */
252b5132 184
b34976b6 185bfd_boolean
268b6b39 186_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 187{
45d6a902 188 flagword flags;
91d6fa6a 189 asection *s;
9c5bfbb7 190 const struct elf_backend_data *bed;
252b5132 191
0eddce27 192 if (! is_elf_hash_table (info->hash))
45d6a902
AM
193 return FALSE;
194
195 if (elf_hash_table (info)->dynamic_sections_created)
196 return TRUE;
197
7e9f0867
AM
198 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
199 return FALSE;
45d6a902 200
7e9f0867 201 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
202 bed = get_elf_backend_data (abfd);
203
204 flags = bed->dynamic_sec_flags;
45d6a902
AM
205
206 /* A dynamically linked executable has a .interp section, but a
207 shared library does not. */
36af4a4e 208 if (info->executable)
252b5132 209 {
3496cb2a
L
210 s = bfd_make_section_with_flags (abfd, ".interp",
211 flags | SEC_READONLY);
212 if (s == NULL)
45d6a902
AM
213 return FALSE;
214 }
bb0deeff 215
45d6a902
AM
216 /* Create sections to hold version informations. These are removed
217 if they are not needed. */
3496cb2a
L
218 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
219 flags | SEC_READONLY);
45d6a902 220 if (s == NULL
45d6a902
AM
221 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
222 return FALSE;
223
3496cb2a
L
224 s = bfd_make_section_with_flags (abfd, ".gnu.version",
225 flags | SEC_READONLY);
45d6a902 226 if (s == NULL
45d6a902
AM
227 || ! bfd_set_section_alignment (abfd, s, 1))
228 return FALSE;
229
3496cb2a
L
230 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
231 flags | SEC_READONLY);
45d6a902 232 if (s == NULL
45d6a902
AM
233 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
234 return FALSE;
235
3496cb2a
L
236 s = bfd_make_section_with_flags (abfd, ".dynsym",
237 flags | SEC_READONLY);
45d6a902 238 if (s == NULL
45d6a902
AM
239 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
240 return FALSE;
241
3496cb2a
L
242 s = bfd_make_section_with_flags (abfd, ".dynstr",
243 flags | SEC_READONLY);
244 if (s == NULL)
45d6a902
AM
245 return FALSE;
246
3496cb2a 247 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
45d6a902 248 if (s == NULL
45d6a902
AM
249 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
250 return FALSE;
251
252 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
253 .dynamic section. We could set _DYNAMIC in a linker script, but we
254 only want to define it if we are, in fact, creating a .dynamic
255 section. We don't want to define it if there is no .dynamic
256 section, since on some ELF platforms the start up code examines it
257 to decide how to initialize the process. */
d98685ac 258 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
45d6a902
AM
259 return FALSE;
260
fdc90cb4
JJ
261 if (info->emit_hash)
262 {
263 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
264 if (s == NULL
265 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
266 return FALSE;
267 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
268 }
269
270 if (info->emit_gnu_hash)
271 {
272 s = bfd_make_section_with_flags (abfd, ".gnu.hash",
273 flags | SEC_READONLY);
274 if (s == NULL
275 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
276 return FALSE;
277 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
278 4 32-bit words followed by variable count of 64-bit words, then
279 variable count of 32-bit words. */
280 if (bed->s->arch_size == 64)
281 elf_section_data (s)->this_hdr.sh_entsize = 0;
282 else
283 elf_section_data (s)->this_hdr.sh_entsize = 4;
284 }
45d6a902
AM
285
286 /* Let the backend create the rest of the sections. This lets the
287 backend set the right flags. The backend will normally create
288 the .got and .plt sections. */
289 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
290 return FALSE;
291
292 elf_hash_table (info)->dynamic_sections_created = TRUE;
293
294 return TRUE;
295}
296
297/* Create dynamic sections when linking against a dynamic object. */
298
299bfd_boolean
268b6b39 300_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
301{
302 flagword flags, pltflags;
7325306f 303 struct elf_link_hash_entry *h;
45d6a902 304 asection *s;
9c5bfbb7 305 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 306 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 307
252b5132
RH
308 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
309 .rel[a].bss sections. */
e5a52504 310 flags = bed->dynamic_sec_flags;
252b5132
RH
311
312 pltflags = flags;
252b5132 313 if (bed->plt_not_loaded)
6df4d94c
MM
314 /* We do not clear SEC_ALLOC here because we still want the OS to
315 allocate space for the section; it's just that there's nothing
316 to read in from the object file. */
5d1634d7 317 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
318 else
319 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
320 if (bed->plt_readonly)
321 pltflags |= SEC_READONLY;
322
3496cb2a 323 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
252b5132 324 if (s == NULL
252b5132 325 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 326 return FALSE;
6de2ae4a 327 htab->splt = s;
252b5132 328
d98685ac
AM
329 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
330 .plt section. */
7325306f
RS
331 if (bed->want_plt_sym)
332 {
333 h = _bfd_elf_define_linkage_sym (abfd, info, s,
334 "_PROCEDURE_LINKAGE_TABLE_");
335 elf_hash_table (info)->hplt = h;
336 if (h == NULL)
337 return FALSE;
338 }
252b5132 339
3496cb2a 340 s = bfd_make_section_with_flags (abfd,
d35fd659 341 (bed->rela_plts_and_copies_p
3496cb2a
L
342 ? ".rela.plt" : ".rel.plt"),
343 flags | SEC_READONLY);
252b5132 344 if (s == NULL
45d6a902 345 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 346 return FALSE;
6de2ae4a 347 htab->srelplt = s;
252b5132
RH
348
349 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 350 return FALSE;
252b5132 351
3018b441
RH
352 if (bed->want_dynbss)
353 {
354 /* The .dynbss section is a place to put symbols which are defined
355 by dynamic objects, are referenced by regular objects, and are
356 not functions. We must allocate space for them in the process
357 image and use a R_*_COPY reloc to tell the dynamic linker to
358 initialize them at run time. The linker script puts the .dynbss
359 section into the .bss section of the final image. */
3496cb2a
L
360 s = bfd_make_section_with_flags (abfd, ".dynbss",
361 (SEC_ALLOC
362 | SEC_LINKER_CREATED));
363 if (s == NULL)
b34976b6 364 return FALSE;
252b5132 365
3018b441 366 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
367 normally needed. We need to create it here, though, so that the
368 linker will map it to an output section. We can't just create it
369 only if we need it, because we will not know whether we need it
370 until we have seen all the input files, and the first time the
371 main linker code calls BFD after examining all the input files
372 (size_dynamic_sections) the input sections have already been
373 mapped to the output sections. If the section turns out not to
374 be needed, we can discard it later. We will never need this
375 section when generating a shared object, since they do not use
376 copy relocs. */
3018b441
RH
377 if (! info->shared)
378 {
3496cb2a 379 s = bfd_make_section_with_flags (abfd,
d35fd659 380 (bed->rela_plts_and_copies_p
3496cb2a
L
381 ? ".rela.bss" : ".rel.bss"),
382 flags | SEC_READONLY);
3018b441 383 if (s == NULL
45d6a902 384 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 385 return FALSE;
3018b441 386 }
252b5132
RH
387 }
388
b34976b6 389 return TRUE;
252b5132
RH
390}
391\f
252b5132
RH
392/* Record a new dynamic symbol. We record the dynamic symbols as we
393 read the input files, since we need to have a list of all of them
394 before we can determine the final sizes of the output sections.
395 Note that we may actually call this function even though we are not
396 going to output any dynamic symbols; in some cases we know that a
397 symbol should be in the dynamic symbol table, but only if there is
398 one. */
399
b34976b6 400bfd_boolean
c152c796
AM
401bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
402 struct elf_link_hash_entry *h)
252b5132
RH
403{
404 if (h->dynindx == -1)
405 {
2b0f7ef9 406 struct elf_strtab_hash *dynstr;
68b6ddd0 407 char *p;
252b5132 408 const char *name;
252b5132
RH
409 bfd_size_type indx;
410
7a13edea
NC
411 /* XXX: The ABI draft says the linker must turn hidden and
412 internal symbols into STB_LOCAL symbols when producing the
413 DSO. However, if ld.so honors st_other in the dynamic table,
414 this would not be necessary. */
415 switch (ELF_ST_VISIBILITY (h->other))
416 {
417 case STV_INTERNAL:
418 case STV_HIDDEN:
9d6eee78
L
419 if (h->root.type != bfd_link_hash_undefined
420 && h->root.type != bfd_link_hash_undefweak)
38048eb9 421 {
f5385ebf 422 h->forced_local = 1;
67687978
PB
423 if (!elf_hash_table (info)->is_relocatable_executable)
424 return TRUE;
7a13edea 425 }
0444bdd4 426
7a13edea
NC
427 default:
428 break;
429 }
430
252b5132
RH
431 h->dynindx = elf_hash_table (info)->dynsymcount;
432 ++elf_hash_table (info)->dynsymcount;
433
434 dynstr = elf_hash_table (info)->dynstr;
435 if (dynstr == NULL)
436 {
437 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 438 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 439 if (dynstr == NULL)
b34976b6 440 return FALSE;
252b5132
RH
441 }
442
443 /* We don't put any version information in the dynamic string
aad5d350 444 table. */
252b5132
RH
445 name = h->root.root.string;
446 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
447 if (p != NULL)
448 /* We know that the p points into writable memory. In fact,
449 there are only a few symbols that have read-only names, being
450 those like _GLOBAL_OFFSET_TABLE_ that are created specially
451 by the backends. Most symbols will have names pointing into
452 an ELF string table read from a file, or to objalloc memory. */
453 *p = 0;
454
455 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
456
457 if (p != NULL)
458 *p = ELF_VER_CHR;
252b5132
RH
459
460 if (indx == (bfd_size_type) -1)
b34976b6 461 return FALSE;
252b5132
RH
462 h->dynstr_index = indx;
463 }
464
b34976b6 465 return TRUE;
252b5132 466}
45d6a902 467\f
55255dae
L
468/* Mark a symbol dynamic. */
469
28caa186 470static void
55255dae 471bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
472 struct elf_link_hash_entry *h,
473 Elf_Internal_Sym *sym)
55255dae 474{
40b36307 475 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 476
40b36307
L
477 /* It may be called more than once on the same H. */
478 if(h->dynamic || info->relocatable)
55255dae
L
479 return;
480
40b36307
L
481 if ((info->dynamic_data
482 && (h->type == STT_OBJECT
483 || (sym != NULL
484 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
a0c8462f 485 || (d != NULL
40b36307
L
486 && h->root.type == bfd_link_hash_new
487 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
488 h->dynamic = 1;
489}
490
45d6a902
AM
491/* Record an assignment to a symbol made by a linker script. We need
492 this in case some dynamic object refers to this symbol. */
493
494bfd_boolean
fe21a8fc
L
495bfd_elf_record_link_assignment (bfd *output_bfd,
496 struct bfd_link_info *info,
268b6b39 497 const char *name,
fe21a8fc
L
498 bfd_boolean provide,
499 bfd_boolean hidden)
45d6a902 500{
00cbee0a 501 struct elf_link_hash_entry *h, *hv;
4ea42fb7 502 struct elf_link_hash_table *htab;
00cbee0a 503 const struct elf_backend_data *bed;
45d6a902 504
0eddce27 505 if (!is_elf_hash_table (info->hash))
45d6a902
AM
506 return TRUE;
507
4ea42fb7
AM
508 htab = elf_hash_table (info);
509 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 510 if (h == NULL)
4ea42fb7 511 return provide;
45d6a902 512
00cbee0a 513 switch (h->root.type)
77cfaee6 514 {
00cbee0a
L
515 case bfd_link_hash_defined:
516 case bfd_link_hash_defweak:
517 case bfd_link_hash_common:
518 break;
519 case bfd_link_hash_undefweak:
520 case bfd_link_hash_undefined:
521 /* Since we're defining the symbol, don't let it seem to have not
522 been defined. record_dynamic_symbol and size_dynamic_sections
523 may depend on this. */
4ea42fb7 524 h->root.type = bfd_link_hash_new;
77cfaee6
AM
525 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
526 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
527 break;
528 case bfd_link_hash_new:
40b36307 529 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 530 h->non_elf = 0;
00cbee0a
L
531 break;
532 case bfd_link_hash_indirect:
533 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 534 the versioned symbol point to this one. */
00cbee0a
L
535 bed = get_elf_backend_data (output_bfd);
536 hv = h;
537 while (hv->root.type == bfd_link_hash_indirect
538 || hv->root.type == bfd_link_hash_warning)
539 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
540 /* We don't need to update h->root.u since linker will set them
541 later. */
542 h->root.type = bfd_link_hash_undefined;
543 hv->root.type = bfd_link_hash_indirect;
544 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
545 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
546 break;
547 case bfd_link_hash_warning:
548 abort ();
549 break;
55255dae 550 }
45d6a902
AM
551
552 /* If this symbol is being provided by the linker script, and it is
553 currently defined by a dynamic object, but not by a regular
554 object, then mark it as undefined so that the generic linker will
555 force the correct value. */
556 if (provide
f5385ebf
AM
557 && h->def_dynamic
558 && !h->def_regular)
45d6a902
AM
559 h->root.type = bfd_link_hash_undefined;
560
561 /* If this symbol is not being provided by the linker script, and it is
562 currently defined by a dynamic object, but not by a regular object,
563 then clear out any version information because the symbol will not be
564 associated with the dynamic object any more. */
565 if (!provide
f5385ebf
AM
566 && h->def_dynamic
567 && !h->def_regular)
45d6a902
AM
568 h->verinfo.verdef = NULL;
569
f5385ebf 570 h->def_regular = 1;
45d6a902 571
fe21a8fc
L
572 if (provide && hidden)
573 {
91d6fa6a 574 bed = get_elf_backend_data (output_bfd);
fe21a8fc
L
575 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
576 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
577 }
578
6fa3860b
PB
579 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
580 and executables. */
581 if (!info->relocatable
582 && h->dynindx != -1
583 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
584 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
585 h->forced_local = 1;
586
f5385ebf
AM
587 if ((h->def_dynamic
588 || h->ref_dynamic
67687978
PB
589 || info->shared
590 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
45d6a902
AM
591 && h->dynindx == -1)
592 {
c152c796 593 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
594 return FALSE;
595
596 /* If this is a weak defined symbol, and we know a corresponding
597 real symbol from the same dynamic object, make sure the real
598 symbol is also made into a dynamic symbol. */
f6e332e6
AM
599 if (h->u.weakdef != NULL
600 && h->u.weakdef->dynindx == -1)
45d6a902 601 {
f6e332e6 602 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
603 return FALSE;
604 }
605 }
606
607 return TRUE;
608}
42751cf3 609
8c58d23b
AM
610/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
611 success, and 2 on a failure caused by attempting to record a symbol
612 in a discarded section, eg. a discarded link-once section symbol. */
613
614int
c152c796
AM
615bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
616 bfd *input_bfd,
617 long input_indx)
8c58d23b
AM
618{
619 bfd_size_type amt;
620 struct elf_link_local_dynamic_entry *entry;
621 struct elf_link_hash_table *eht;
622 struct elf_strtab_hash *dynstr;
623 unsigned long dynstr_index;
624 char *name;
625 Elf_External_Sym_Shndx eshndx;
626 char esym[sizeof (Elf64_External_Sym)];
627
0eddce27 628 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
629 return 0;
630
631 /* See if the entry exists already. */
632 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
633 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
634 return 1;
635
636 amt = sizeof (*entry);
a50b1753 637 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
638 if (entry == NULL)
639 return 0;
640
641 /* Go find the symbol, so that we can find it's name. */
642 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 643 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
644 {
645 bfd_release (input_bfd, entry);
646 return 0;
647 }
648
649 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 650 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
651 {
652 asection *s;
653
654 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
655 if (s == NULL || bfd_is_abs_section (s->output_section))
656 {
657 /* We can still bfd_release here as nothing has done another
658 bfd_alloc. We can't do this later in this function. */
659 bfd_release (input_bfd, entry);
660 return 2;
661 }
662 }
663
664 name = (bfd_elf_string_from_elf_section
665 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
666 entry->isym.st_name));
667
668 dynstr = elf_hash_table (info)->dynstr;
669 if (dynstr == NULL)
670 {
671 /* Create a strtab to hold the dynamic symbol names. */
672 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
673 if (dynstr == NULL)
674 return 0;
675 }
676
b34976b6 677 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
8c58d23b
AM
678 if (dynstr_index == (unsigned long) -1)
679 return 0;
680 entry->isym.st_name = dynstr_index;
681
682 eht = elf_hash_table (info);
683
684 entry->next = eht->dynlocal;
685 eht->dynlocal = entry;
686 entry->input_bfd = input_bfd;
687 entry->input_indx = input_indx;
688 eht->dynsymcount++;
689
690 /* Whatever binding the symbol had before, it's now local. */
691 entry->isym.st_info
692 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
693
694 /* The dynindx will be set at the end of size_dynamic_sections. */
695
696 return 1;
697}
698
30b30c21 699/* Return the dynindex of a local dynamic symbol. */
42751cf3 700
30b30c21 701long
268b6b39
AM
702_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
703 bfd *input_bfd,
704 long input_indx)
30b30c21
RH
705{
706 struct elf_link_local_dynamic_entry *e;
707
708 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
709 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
710 return e->dynindx;
711 return -1;
712}
713
714/* This function is used to renumber the dynamic symbols, if some of
715 them are removed because they are marked as local. This is called
716 via elf_link_hash_traverse. */
717
b34976b6 718static bfd_boolean
268b6b39
AM
719elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
720 void *data)
42751cf3 721{
a50b1753 722 size_t *count = (size_t *) data;
30b30c21 723
e92d460e
AM
724 if (h->root.type == bfd_link_hash_warning)
725 h = (struct elf_link_hash_entry *) h->root.u.i.link;
726
6fa3860b
PB
727 if (h->forced_local)
728 return TRUE;
729
730 if (h->dynindx != -1)
731 h->dynindx = ++(*count);
732
733 return TRUE;
734}
735
736
737/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
738 STB_LOCAL binding. */
739
740static bfd_boolean
741elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
742 void *data)
743{
a50b1753 744 size_t *count = (size_t *) data;
6fa3860b
PB
745
746 if (h->root.type == bfd_link_hash_warning)
747 h = (struct elf_link_hash_entry *) h->root.u.i.link;
748
749 if (!h->forced_local)
750 return TRUE;
751
42751cf3 752 if (h->dynindx != -1)
30b30c21
RH
753 h->dynindx = ++(*count);
754
b34976b6 755 return TRUE;
42751cf3 756}
30b30c21 757
aee6f5b4
AO
758/* Return true if the dynamic symbol for a given section should be
759 omitted when creating a shared library. */
760bfd_boolean
761_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
762 struct bfd_link_info *info,
763 asection *p)
764{
74541ad4
AM
765 struct elf_link_hash_table *htab;
766
aee6f5b4
AO
767 switch (elf_section_data (p)->this_hdr.sh_type)
768 {
769 case SHT_PROGBITS:
770 case SHT_NOBITS:
771 /* If sh_type is yet undecided, assume it could be
772 SHT_PROGBITS/SHT_NOBITS. */
773 case SHT_NULL:
74541ad4
AM
774 htab = elf_hash_table (info);
775 if (p == htab->tls_sec)
776 return FALSE;
777
778 if (htab->text_index_section != NULL)
779 return p != htab->text_index_section && p != htab->data_index_section;
780
aee6f5b4
AO
781 if (strcmp (p->name, ".got") == 0
782 || strcmp (p->name, ".got.plt") == 0
783 || strcmp (p->name, ".plt") == 0)
784 {
785 asection *ip;
aee6f5b4 786
74541ad4
AM
787 if (htab->dynobj != NULL
788 && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
aee6f5b4
AO
789 && (ip->flags & SEC_LINKER_CREATED)
790 && ip->output_section == p)
791 return TRUE;
792 }
793 return FALSE;
794
795 /* There shouldn't be section relative relocations
796 against any other section. */
797 default:
798 return TRUE;
799 }
800}
801
062e2358 802/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
803 symbol for each output section, which come first. Next come symbols
804 which have been forced to local binding. Then all of the back-end
805 allocated local dynamic syms, followed by the rest of the global
806 symbols. */
30b30c21 807
554220db
AM
808static unsigned long
809_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
810 struct bfd_link_info *info,
811 unsigned long *section_sym_count)
30b30c21
RH
812{
813 unsigned long dynsymcount = 0;
814
67687978 815 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
30b30c21 816 {
aee6f5b4 817 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
818 asection *p;
819 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 820 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
821 && (p->flags & SEC_ALLOC) != 0
822 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
823 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
824 else
825 elf_section_data (p)->dynindx = 0;
30b30c21 826 }
554220db 827 *section_sym_count = dynsymcount;
30b30c21 828
6fa3860b
PB
829 elf_link_hash_traverse (elf_hash_table (info),
830 elf_link_renumber_local_hash_table_dynsyms,
831 &dynsymcount);
832
30b30c21
RH
833 if (elf_hash_table (info)->dynlocal)
834 {
835 struct elf_link_local_dynamic_entry *p;
836 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
837 p->dynindx = ++dynsymcount;
838 }
839
840 elf_link_hash_traverse (elf_hash_table (info),
841 elf_link_renumber_hash_table_dynsyms,
842 &dynsymcount);
843
844 /* There is an unused NULL entry at the head of the table which
845 we must account for in our count. Unless there weren't any
846 symbols, which means we'll have no table at all. */
847 if (dynsymcount != 0)
848 ++dynsymcount;
849
ccabcbe5
AM
850 elf_hash_table (info)->dynsymcount = dynsymcount;
851 return dynsymcount;
30b30c21 852}
252b5132 853
54ac0771
L
854/* Merge st_other field. */
855
856static void
857elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
858 Elf_Internal_Sym *isym, bfd_boolean definition,
859 bfd_boolean dynamic)
860{
861 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
862
863 /* If st_other has a processor-specific meaning, specific
864 code might be needed here. We never merge the visibility
865 attribute with the one from a dynamic object. */
866 if (bed->elf_backend_merge_symbol_attribute)
867 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
868 dynamic);
869
870 /* If this symbol has default visibility and the user has requested
871 we not re-export it, then mark it as hidden. */
872 if (definition
873 && !dynamic
874 && (abfd->no_export
875 || (abfd->my_archive && abfd->my_archive->no_export))
876 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
877 isym->st_other = (STV_HIDDEN
878 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
879
880 if (!dynamic && ELF_ST_VISIBILITY (isym->st_other) != 0)
881 {
882 unsigned char hvis, symvis, other, nvis;
883
884 /* Only merge the visibility. Leave the remainder of the
885 st_other field to elf_backend_merge_symbol_attribute. */
886 other = h->other & ~ELF_ST_VISIBILITY (-1);
887
888 /* Combine visibilities, using the most constraining one. */
889 hvis = ELF_ST_VISIBILITY (h->other);
890 symvis = ELF_ST_VISIBILITY (isym->st_other);
891 if (! hvis)
892 nvis = symvis;
893 else if (! symvis)
894 nvis = hvis;
895 else
896 nvis = hvis < symvis ? hvis : symvis;
897
898 h->other = other | nvis;
899 }
900}
901
45d6a902
AM
902/* This function is called when we want to define a new symbol. It
903 handles the various cases which arise when we find a definition in
904 a dynamic object, or when there is already a definition in a
905 dynamic object. The new symbol is described by NAME, SYM, PSEC,
906 and PVALUE. We set SYM_HASH to the hash table entry. We set
907 OVERRIDE if the old symbol is overriding a new definition. We set
908 TYPE_CHANGE_OK if it is OK for the type to change. We set
909 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
910 change, we mean that we shouldn't warn if the type or size does
af44c138
L
911 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
912 object is overridden by a regular object. */
45d6a902
AM
913
914bfd_boolean
268b6b39
AM
915_bfd_elf_merge_symbol (bfd *abfd,
916 struct bfd_link_info *info,
917 const char *name,
918 Elf_Internal_Sym *sym,
919 asection **psec,
920 bfd_vma *pvalue,
af44c138 921 unsigned int *pold_alignment,
268b6b39
AM
922 struct elf_link_hash_entry **sym_hash,
923 bfd_boolean *skip,
924 bfd_boolean *override,
925 bfd_boolean *type_change_ok,
0f8a2703 926 bfd_boolean *size_change_ok)
252b5132 927{
7479dfd4 928 asection *sec, *oldsec;
45d6a902
AM
929 struct elf_link_hash_entry *h;
930 struct elf_link_hash_entry *flip;
931 int bind;
932 bfd *oldbfd;
933 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 934 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 935 const struct elf_backend_data *bed;
45d6a902
AM
936
937 *skip = FALSE;
938 *override = FALSE;
939
940 sec = *psec;
941 bind = ELF_ST_BIND (sym->st_info);
942
cd7be95b
KH
943 /* Silently discard TLS symbols from --just-syms. There's no way to
944 combine a static TLS block with a new TLS block for this executable. */
945 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
946 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
947 {
948 *skip = TRUE;
949 return TRUE;
950 }
951
45d6a902
AM
952 if (! bfd_is_und_section (sec))
953 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
954 else
955 h = ((struct elf_link_hash_entry *)
956 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
957 if (h == NULL)
958 return FALSE;
959 *sym_hash = h;
252b5132 960
88ba32a0
L
961 bed = get_elf_backend_data (abfd);
962
45d6a902
AM
963 /* This code is for coping with dynamic objects, and is only useful
964 if we are doing an ELF link. */
88ba32a0 965 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
45d6a902 966 return TRUE;
252b5132 967
45d6a902
AM
968 /* For merging, we only care about real symbols. */
969
970 while (h->root.type == bfd_link_hash_indirect
971 || h->root.type == bfd_link_hash_warning)
972 h = (struct elf_link_hash_entry *) h->root.u.i.link;
973
40b36307
L
974 /* We have to check it for every instance since the first few may be
975 refereences and not all compilers emit symbol type for undefined
976 symbols. */
977 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
978
45d6a902
AM
979 /* If we just created the symbol, mark it as being an ELF symbol.
980 Other than that, there is nothing to do--there is no merge issue
981 with a newly defined symbol--so we just return. */
982
983 if (h->root.type == bfd_link_hash_new)
252b5132 984 {
f5385ebf 985 h->non_elf = 0;
45d6a902
AM
986 return TRUE;
987 }
252b5132 988
7479dfd4
L
989 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
990 existing symbol. */
252b5132 991
45d6a902
AM
992 switch (h->root.type)
993 {
994 default:
995 oldbfd = NULL;
7479dfd4 996 oldsec = NULL;
45d6a902 997 break;
252b5132 998
45d6a902
AM
999 case bfd_link_hash_undefined:
1000 case bfd_link_hash_undefweak:
1001 oldbfd = h->root.u.undef.abfd;
7479dfd4 1002 oldsec = NULL;
45d6a902
AM
1003 break;
1004
1005 case bfd_link_hash_defined:
1006 case bfd_link_hash_defweak:
1007 oldbfd = h->root.u.def.section->owner;
7479dfd4 1008 oldsec = h->root.u.def.section;
45d6a902
AM
1009 break;
1010
1011 case bfd_link_hash_common:
1012 oldbfd = h->root.u.c.p->section->owner;
7479dfd4 1013 oldsec = h->root.u.c.p->section;
45d6a902
AM
1014 break;
1015 }
1016
895fa45f
MGD
1017 /* Differentiate strong and weak symbols. */
1018 newweak = bind == STB_WEAK;
1019 oldweak = (h->root.type == bfd_link_hash_defweak
1020 || h->root.type == bfd_link_hash_undefweak);
1021
45d6a902
AM
1022 /* In cases involving weak versioned symbols, we may wind up trying
1023 to merge a symbol with itself. Catch that here, to avoid the
1024 confusion that results if we try to override a symbol with
1025 itself. The additional tests catch cases like
1026 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1027 dynamic object, which we do want to handle here. */
1028 if (abfd == oldbfd
895fa45f 1029 && (newweak || oldweak)
45d6a902 1030 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1031 || !h->def_regular))
45d6a902
AM
1032 return TRUE;
1033
1034 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1035 respectively, is from a dynamic object. */
1036
707bba77 1037 newdyn = (abfd->flags & DYNAMIC) != 0;
45d6a902 1038
707bba77 1039 olddyn = FALSE;
45d6a902
AM
1040 if (oldbfd != NULL)
1041 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1042 else if (oldsec != NULL)
45d6a902 1043 {
707bba77 1044 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1045 indices used by MIPS ELF. */
707bba77 1046 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1047 }
252b5132 1048
45d6a902
AM
1049 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1050 respectively, appear to be a definition rather than reference. */
1051
707bba77 1052 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1053
707bba77
AM
1054 olddef = (h->root.type != bfd_link_hash_undefined
1055 && h->root.type != bfd_link_hash_undefweak
1056 && h->root.type != bfd_link_hash_common);
45d6a902 1057
0a36a439
L
1058 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1059 respectively, appear to be a function. */
1060
1061 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1062 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1063
1064 oldfunc = (h->type != STT_NOTYPE
1065 && bed->is_function_type (h->type));
1066
580a2b6e
L
1067 /* When we try to create a default indirect symbol from the dynamic
1068 definition with the default version, we skip it if its type and
1069 the type of existing regular definition mismatch. We only do it
1070 if the existing regular definition won't be dynamic. */
1071 if (pold_alignment == NULL
1072 && !info->shared
1073 && !info->export_dynamic
1074 && !h->ref_dynamic
1075 && newdyn
1076 && newdef
1077 && !olddyn
1078 && (olddef || h->root.type == bfd_link_hash_common)
1079 && ELF_ST_TYPE (sym->st_info) != h->type
1080 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
fcb93ecf 1081 && h->type != STT_NOTYPE
0a36a439 1082 && !(newfunc && oldfunc))
580a2b6e
L
1083 {
1084 *skip = TRUE;
1085 return TRUE;
1086 }
1087
68f49ba3
L
1088 /* Check TLS symbol. We don't check undefined symbol introduced by
1089 "ld -u". */
7479dfd4 1090 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
68f49ba3
L
1091 && ELF_ST_TYPE (sym->st_info) != h->type
1092 && oldbfd != NULL)
7479dfd4
L
1093 {
1094 bfd *ntbfd, *tbfd;
1095 bfd_boolean ntdef, tdef;
1096 asection *ntsec, *tsec;
1097
1098 if (h->type == STT_TLS)
1099 {
3b36f7e6 1100 ntbfd = abfd;
7479dfd4
L
1101 ntsec = sec;
1102 ntdef = newdef;
1103 tbfd = oldbfd;
1104 tsec = oldsec;
1105 tdef = olddef;
1106 }
1107 else
1108 {
1109 ntbfd = oldbfd;
1110 ntsec = oldsec;
1111 ntdef = olddef;
1112 tbfd = abfd;
1113 tsec = sec;
1114 tdef = newdef;
1115 }
1116
1117 if (tdef && ntdef)
1118 (*_bfd_error_handler)
fc3e1e3c 1119 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1120 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1121 else if (!tdef && !ntdef)
1122 (*_bfd_error_handler)
fc3e1e3c 1123 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
7479dfd4
L
1124 tbfd, ntbfd, h->root.root.string);
1125 else if (tdef)
1126 (*_bfd_error_handler)
fc3e1e3c 1127 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
7479dfd4
L
1128 tbfd, tsec, ntbfd, h->root.root.string);
1129 else
1130 (*_bfd_error_handler)
fc3e1e3c 1131 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1132 tbfd, ntbfd, ntsec, h->root.root.string);
1133
1134 bfd_set_error (bfd_error_bad_value);
1135 return FALSE;
1136 }
1137
4cc11e76 1138 /* We need to remember if a symbol has a definition in a dynamic
45d6a902
AM
1139 object or is weak in all dynamic objects. Internal and hidden
1140 visibility will make it unavailable to dynamic objects. */
f5385ebf 1141 if (newdyn && !h->dynamic_def)
45d6a902
AM
1142 {
1143 if (!bfd_is_und_section (sec))
f5385ebf 1144 h->dynamic_def = 1;
45d6a902 1145 else
252b5132 1146 {
45d6a902
AM
1147 /* Check if this symbol is weak in all dynamic objects. If it
1148 is the first time we see it in a dynamic object, we mark
1149 if it is weak. Otherwise, we clear it. */
f5385ebf 1150 if (!h->ref_dynamic)
79349b09 1151 {
45d6a902 1152 if (bind == STB_WEAK)
f5385ebf 1153 h->dynamic_weak = 1;
252b5132 1154 }
45d6a902 1155 else if (bind != STB_WEAK)
f5385ebf 1156 h->dynamic_weak = 0;
252b5132 1157 }
45d6a902 1158 }
252b5132 1159
45d6a902
AM
1160 /* If the old symbol has non-default visibility, we ignore the new
1161 definition from a dynamic object. */
1162 if (newdyn
9c7a29a3 1163 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1164 && !bfd_is_und_section (sec))
1165 {
1166 *skip = TRUE;
1167 /* Make sure this symbol is dynamic. */
f5385ebf 1168 h->ref_dynamic = 1;
45d6a902
AM
1169 /* A protected symbol has external availability. Make sure it is
1170 recorded as dynamic.
1171
1172 FIXME: Should we check type and size for protected symbol? */
1173 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1174 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1175 else
1176 return TRUE;
1177 }
1178 else if (!newdyn
9c7a29a3 1179 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1180 && h->def_dynamic)
45d6a902
AM
1181 {
1182 /* If the new symbol with non-default visibility comes from a
1183 relocatable file and the old definition comes from a dynamic
1184 object, we remove the old definition. */
1185 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1186 {
1187 /* Handle the case where the old dynamic definition is
1188 default versioned. We need to copy the symbol info from
1189 the symbol with default version to the normal one if it
1190 was referenced before. */
1191 if (h->ref_regular)
1192 {
d2dee3b2 1193 struct elf_link_hash_entry *vh = *sym_hash;
91d6fa6a 1194
d2dee3b2
L
1195 vh->root.type = h->root.type;
1196 h->root.type = bfd_link_hash_indirect;
1197 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1198 /* Protected symbols will override the dynamic definition
1199 with default version. */
1200 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1201 {
1202 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1203 vh->dynamic_def = 1;
1204 vh->ref_dynamic = 1;
1205 }
1206 else
1207 {
1208 h->root.type = vh->root.type;
1209 vh->ref_dynamic = 0;
1210 /* We have to hide it here since it was made dynamic
1211 global with extra bits when the symbol info was
1212 copied from the old dynamic definition. */
1213 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1214 }
1215 h = vh;
1216 }
1217 else
1218 h = *sym_hash;
1219 }
1de1a317 1220
f6e332e6 1221 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317
L
1222 && bfd_is_und_section (sec))
1223 {
1224 /* If the new symbol is undefined and the old symbol was
1225 also undefined before, we need to make sure
1226 _bfd_generic_link_add_one_symbol doesn't mess
f6e332e6 1227 up the linker hash table undefs list. Since the old
1de1a317
L
1228 definition came from a dynamic object, it is still on the
1229 undefs list. */
1230 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1231 h->root.u.undef.abfd = abfd;
1232 }
1233 else
1234 {
1235 h->root.type = bfd_link_hash_new;
1236 h->root.u.undef.abfd = NULL;
1237 }
1238
f5385ebf 1239 if (h->def_dynamic)
252b5132 1240 {
f5385ebf
AM
1241 h->def_dynamic = 0;
1242 h->ref_dynamic = 1;
1243 h->dynamic_def = 1;
45d6a902
AM
1244 }
1245 /* FIXME: Should we check type and size for protected symbol? */
1246 h->size = 0;
1247 h->type = 0;
1248 return TRUE;
1249 }
14a793b2 1250
3e7a7d11
NC
1251 if (bind == STB_GNU_UNIQUE)
1252 h->unique_global = 1;
1253
15b43f48
AM
1254 /* If a new weak symbol definition comes from a regular file and the
1255 old symbol comes from a dynamic library, we treat the new one as
1256 strong. Similarly, an old weak symbol definition from a regular
1257 file is treated as strong when the new symbol comes from a dynamic
1258 library. Further, an old weak symbol from a dynamic library is
1259 treated as strong if the new symbol is from a dynamic library.
1260 This reflects the way glibc's ld.so works.
1261
1262 Do this before setting *type_change_ok or *size_change_ok so that
1263 we warn properly when dynamic library symbols are overridden. */
1264
1265 if (newdef && !newdyn && olddyn)
0f8a2703 1266 newweak = FALSE;
15b43f48 1267 if (olddef && newdyn)
0f8a2703
AM
1268 oldweak = FALSE;
1269
d334575b 1270 /* Allow changes between different types of function symbol. */
0a36a439 1271 if (newfunc && oldfunc)
fcb93ecf
PB
1272 *type_change_ok = TRUE;
1273
79349b09
AM
1274 /* It's OK to change the type if either the existing symbol or the
1275 new symbol is weak. A type change is also OK if the old symbol
1276 is undefined and the new symbol is defined. */
252b5132 1277
79349b09
AM
1278 if (oldweak
1279 || newweak
1280 || (newdef
1281 && h->root.type == bfd_link_hash_undefined))
1282 *type_change_ok = TRUE;
1283
1284 /* It's OK to change the size if either the existing symbol or the
1285 new symbol is weak, or if the old symbol is undefined. */
1286
1287 if (*type_change_ok
1288 || h->root.type == bfd_link_hash_undefined)
1289 *size_change_ok = TRUE;
45d6a902 1290
45d6a902
AM
1291 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1292 symbol, respectively, appears to be a common symbol in a dynamic
1293 object. If a symbol appears in an uninitialized section, and is
1294 not weak, and is not a function, then it may be a common symbol
1295 which was resolved when the dynamic object was created. We want
1296 to treat such symbols specially, because they raise special
1297 considerations when setting the symbol size: if the symbol
1298 appears as a common symbol in a regular object, and the size in
1299 the regular object is larger, we must make sure that we use the
1300 larger size. This problematic case can always be avoided in C,
1301 but it must be handled correctly when using Fortran shared
1302 libraries.
1303
1304 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1305 likewise for OLDDYNCOMMON and OLDDEF.
1306
1307 Note that this test is just a heuristic, and that it is quite
1308 possible to have an uninitialized symbol in a shared object which
1309 is really a definition, rather than a common symbol. This could
1310 lead to some minor confusion when the symbol really is a common
1311 symbol in some regular object. However, I think it will be
1312 harmless. */
1313
1314 if (newdyn
1315 && newdef
79349b09 1316 && !newweak
45d6a902
AM
1317 && (sec->flags & SEC_ALLOC) != 0
1318 && (sec->flags & SEC_LOAD) == 0
1319 && sym->st_size > 0
0a36a439 1320 && !newfunc)
45d6a902
AM
1321 newdyncommon = TRUE;
1322 else
1323 newdyncommon = FALSE;
1324
1325 if (olddyn
1326 && olddef
1327 && h->root.type == bfd_link_hash_defined
f5385ebf 1328 && h->def_dynamic
45d6a902
AM
1329 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1330 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1331 && h->size > 0
0a36a439 1332 && !oldfunc)
45d6a902
AM
1333 olddyncommon = TRUE;
1334 else
1335 olddyncommon = FALSE;
1336
a4d8e49b
L
1337 /* We now know everything about the old and new symbols. We ask the
1338 backend to check if we can merge them. */
a4d8e49b
L
1339 if (bed->merge_symbol
1340 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1341 pold_alignment, skip, override,
1342 type_change_ok, size_change_ok,
1343 &newdyn, &newdef, &newdyncommon, &newweak,
1344 abfd, &sec,
1345 &olddyn, &olddef, &olddyncommon, &oldweak,
1346 oldbfd, &oldsec))
1347 return FALSE;
1348
45d6a902
AM
1349 /* If both the old and the new symbols look like common symbols in a
1350 dynamic object, set the size of the symbol to the larger of the
1351 two. */
1352
1353 if (olddyncommon
1354 && newdyncommon
1355 && sym->st_size != h->size)
1356 {
1357 /* Since we think we have two common symbols, issue a multiple
1358 common warning if desired. Note that we only warn if the
1359 size is different. If the size is the same, we simply let
1360 the old symbol override the new one as normally happens with
1361 symbols defined in dynamic objects. */
1362
1363 if (! ((*info->callbacks->multiple_common)
24f58f47 1364 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902 1365 return FALSE;
252b5132 1366
45d6a902
AM
1367 if (sym->st_size > h->size)
1368 h->size = sym->st_size;
252b5132 1369
45d6a902 1370 *size_change_ok = TRUE;
252b5132
RH
1371 }
1372
45d6a902
AM
1373 /* If we are looking at a dynamic object, and we have found a
1374 definition, we need to see if the symbol was already defined by
1375 some other object. If so, we want to use the existing
1376 definition, and we do not want to report a multiple symbol
1377 definition error; we do this by clobbering *PSEC to be
1378 bfd_und_section_ptr.
1379
1380 We treat a common symbol as a definition if the symbol in the
1381 shared library is a function, since common symbols always
1382 represent variables; this can cause confusion in principle, but
1383 any such confusion would seem to indicate an erroneous program or
1384 shared library. We also permit a common symbol in a regular
79349b09 1385 object to override a weak symbol in a shared object. */
45d6a902
AM
1386
1387 if (newdyn
1388 && newdef
77cfaee6 1389 && (olddef
45d6a902 1390 || (h->root.type == bfd_link_hash_common
0a36a439 1391 && (newweak || newfunc))))
45d6a902
AM
1392 {
1393 *override = TRUE;
1394 newdef = FALSE;
1395 newdyncommon = FALSE;
252b5132 1396
45d6a902
AM
1397 *psec = sec = bfd_und_section_ptr;
1398 *size_change_ok = TRUE;
252b5132 1399
45d6a902
AM
1400 /* If we get here when the old symbol is a common symbol, then
1401 we are explicitly letting it override a weak symbol or
1402 function in a dynamic object, and we don't want to warn about
1403 a type change. If the old symbol is a defined symbol, a type
1404 change warning may still be appropriate. */
252b5132 1405
45d6a902
AM
1406 if (h->root.type == bfd_link_hash_common)
1407 *type_change_ok = TRUE;
1408 }
1409
1410 /* Handle the special case of an old common symbol merging with a
1411 new symbol which looks like a common symbol in a shared object.
1412 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1413 common symbol, and let _bfd_generic_link_add_one_symbol do the
1414 right thing. */
45d6a902
AM
1415
1416 if (newdyncommon
1417 && h->root.type == bfd_link_hash_common)
1418 {
1419 *override = TRUE;
1420 newdef = FALSE;
1421 newdyncommon = FALSE;
1422 *pvalue = sym->st_size;
a4d8e49b 1423 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1424 *size_change_ok = TRUE;
1425 }
1426
c5e2cead 1427 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1428 if (newdef && olddef && newweak)
54ac0771 1429 {
35ed3f94
AM
1430 /* Don't skip new non-IR weak syms. */
1431 if (!((oldbfd->flags & BFD_PLUGIN) != 0
1432 && (abfd->flags & BFD_PLUGIN) == 0))
1433 *skip = TRUE;
54ac0771
L
1434
1435 /* Merge st_other. If the symbol already has a dynamic index,
1436 but visibility says it should not be visible, turn it into a
1437 local symbol. */
1438 elf_merge_st_other (abfd, h, sym, newdef, newdyn);
1439 if (h->dynindx != -1)
1440 switch (ELF_ST_VISIBILITY (h->other))
1441 {
1442 case STV_INTERNAL:
1443 case STV_HIDDEN:
1444 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1445 break;
1446 }
1447 }
c5e2cead 1448
45d6a902
AM
1449 /* If the old symbol is from a dynamic object, and the new symbol is
1450 a definition which is not from a dynamic object, then the new
1451 symbol overrides the old symbol. Symbols from regular files
1452 always take precedence over symbols from dynamic objects, even if
1453 they are defined after the dynamic object in the link.
1454
1455 As above, we again permit a common symbol in a regular object to
1456 override a definition in a shared object if the shared object
0f8a2703 1457 symbol is a function or is weak. */
45d6a902
AM
1458
1459 flip = NULL;
77cfaee6 1460 if (!newdyn
45d6a902
AM
1461 && (newdef
1462 || (bfd_is_com_section (sec)
0a36a439 1463 && (oldweak || oldfunc)))
45d6a902
AM
1464 && olddyn
1465 && olddef
f5385ebf 1466 && h->def_dynamic)
45d6a902
AM
1467 {
1468 /* Change the hash table entry to undefined, and let
1469 _bfd_generic_link_add_one_symbol do the right thing with the
1470 new definition. */
1471
1472 h->root.type = bfd_link_hash_undefined;
1473 h->root.u.undef.abfd = h->root.u.def.section->owner;
1474 *size_change_ok = TRUE;
1475
1476 olddef = FALSE;
1477 olddyncommon = FALSE;
1478
1479 /* We again permit a type change when a common symbol may be
1480 overriding a function. */
1481
1482 if (bfd_is_com_section (sec))
0a36a439
L
1483 {
1484 if (oldfunc)
1485 {
1486 /* If a common symbol overrides a function, make sure
1487 that it isn't defined dynamically nor has type
1488 function. */
1489 h->def_dynamic = 0;
1490 h->type = STT_NOTYPE;
1491 }
1492 *type_change_ok = TRUE;
1493 }
45d6a902
AM
1494
1495 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1496 flip = *sym_hash;
1497 else
1498 /* This union may have been set to be non-NULL when this symbol
1499 was seen in a dynamic object. We must force the union to be
1500 NULL, so that it is correct for a regular symbol. */
1501 h->verinfo.vertree = NULL;
1502 }
1503
1504 /* Handle the special case of a new common symbol merging with an
1505 old symbol that looks like it might be a common symbol defined in
1506 a shared object. Note that we have already handled the case in
1507 which a new common symbol should simply override the definition
1508 in the shared library. */
1509
1510 if (! newdyn
1511 && bfd_is_com_section (sec)
1512 && olddyncommon)
1513 {
1514 /* It would be best if we could set the hash table entry to a
1515 common symbol, but we don't know what to use for the section
1516 or the alignment. */
1517 if (! ((*info->callbacks->multiple_common)
24f58f47 1518 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902
AM
1519 return FALSE;
1520
4cc11e76 1521 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1522 larger, pretend that the new symbol has its size. */
1523
1524 if (h->size > *pvalue)
1525 *pvalue = h->size;
1526
af44c138
L
1527 /* We need to remember the alignment required by the symbol
1528 in the dynamic object. */
1529 BFD_ASSERT (pold_alignment);
1530 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1531
1532 olddef = FALSE;
1533 olddyncommon = FALSE;
1534
1535 h->root.type = bfd_link_hash_undefined;
1536 h->root.u.undef.abfd = h->root.u.def.section->owner;
1537
1538 *size_change_ok = TRUE;
1539 *type_change_ok = TRUE;
1540
1541 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1542 flip = *sym_hash;
1543 else
1544 h->verinfo.vertree = NULL;
1545 }
1546
1547 if (flip != NULL)
1548 {
1549 /* Handle the case where we had a versioned symbol in a dynamic
1550 library and now find a definition in a normal object. In this
1551 case, we make the versioned symbol point to the normal one. */
45d6a902 1552 flip->root.type = h->root.type;
00cbee0a 1553 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1554 h->root.type = bfd_link_hash_indirect;
1555 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1556 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1557 if (h->def_dynamic)
45d6a902 1558 {
f5385ebf
AM
1559 h->def_dynamic = 0;
1560 flip->ref_dynamic = 1;
45d6a902
AM
1561 }
1562 }
1563
45d6a902
AM
1564 return TRUE;
1565}
1566
1567/* This function is called to create an indirect symbol from the
1568 default for the symbol with the default version if needed. The
1569 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
0f8a2703 1570 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1571
28caa186 1572static bfd_boolean
268b6b39
AM
1573_bfd_elf_add_default_symbol (bfd *abfd,
1574 struct bfd_link_info *info,
1575 struct elf_link_hash_entry *h,
1576 const char *name,
1577 Elf_Internal_Sym *sym,
1578 asection **psec,
1579 bfd_vma *value,
1580 bfd_boolean *dynsym,
0f8a2703 1581 bfd_boolean override)
45d6a902
AM
1582{
1583 bfd_boolean type_change_ok;
1584 bfd_boolean size_change_ok;
1585 bfd_boolean skip;
1586 char *shortname;
1587 struct elf_link_hash_entry *hi;
1588 struct bfd_link_hash_entry *bh;
9c5bfbb7 1589 const struct elf_backend_data *bed;
45d6a902
AM
1590 bfd_boolean collect;
1591 bfd_boolean dynamic;
1592 char *p;
1593 size_t len, shortlen;
1594 asection *sec;
1595
1596 /* If this symbol has a version, and it is the default version, we
1597 create an indirect symbol from the default name to the fully
1598 decorated name. This will cause external references which do not
1599 specify a version to be bound to this version of the symbol. */
1600 p = strchr (name, ELF_VER_CHR);
1601 if (p == NULL || p[1] != ELF_VER_CHR)
1602 return TRUE;
1603
1604 if (override)
1605 {
4cc11e76 1606 /* We are overridden by an old definition. We need to check if we
45d6a902
AM
1607 need to create the indirect symbol from the default name. */
1608 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1609 FALSE, FALSE);
1610 BFD_ASSERT (hi != NULL);
1611 if (hi == h)
1612 return TRUE;
1613 while (hi->root.type == bfd_link_hash_indirect
1614 || hi->root.type == bfd_link_hash_warning)
1615 {
1616 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1617 if (hi == h)
1618 return TRUE;
1619 }
1620 }
1621
1622 bed = get_elf_backend_data (abfd);
1623 collect = bed->collect;
1624 dynamic = (abfd->flags & DYNAMIC) != 0;
1625
1626 shortlen = p - name;
a50b1753 1627 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1628 if (shortname == NULL)
1629 return FALSE;
1630 memcpy (shortname, name, shortlen);
1631 shortname[shortlen] = '\0';
1632
1633 /* We are going to create a new symbol. Merge it with any existing
1634 symbol with this name. For the purposes of the merge, act as
1635 though we were defining the symbol we just defined, although we
1636 actually going to define an indirect symbol. */
1637 type_change_ok = FALSE;
1638 size_change_ok = FALSE;
1639 sec = *psec;
1640 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
af44c138
L
1641 NULL, &hi, &skip, &override,
1642 &type_change_ok, &size_change_ok))
45d6a902
AM
1643 return FALSE;
1644
1645 if (skip)
1646 goto nondefault;
1647
1648 if (! override)
1649 {
1650 bh = &hi->root;
1651 if (! (_bfd_generic_link_add_one_symbol
1652 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
268b6b39 1653 0, name, FALSE, collect, &bh)))
45d6a902
AM
1654 return FALSE;
1655 hi = (struct elf_link_hash_entry *) bh;
1656 }
1657 else
1658 {
1659 /* In this case the symbol named SHORTNAME is overriding the
1660 indirect symbol we want to add. We were planning on making
1661 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1662 is the name without a version. NAME is the fully versioned
1663 name, and it is the default version.
1664
1665 Overriding means that we already saw a definition for the
1666 symbol SHORTNAME in a regular object, and it is overriding
1667 the symbol defined in the dynamic object.
1668
1669 When this happens, we actually want to change NAME, the
1670 symbol we just added, to refer to SHORTNAME. This will cause
1671 references to NAME in the shared object to become references
1672 to SHORTNAME in the regular object. This is what we expect
1673 when we override a function in a shared object: that the
1674 references in the shared object will be mapped to the
1675 definition in the regular object. */
1676
1677 while (hi->root.type == bfd_link_hash_indirect
1678 || hi->root.type == bfd_link_hash_warning)
1679 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1680
1681 h->root.type = bfd_link_hash_indirect;
1682 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1683 if (h->def_dynamic)
45d6a902 1684 {
f5385ebf
AM
1685 h->def_dynamic = 0;
1686 hi->ref_dynamic = 1;
1687 if (hi->ref_regular
1688 || hi->def_regular)
45d6a902 1689 {
c152c796 1690 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1691 return FALSE;
1692 }
1693 }
1694
1695 /* Now set HI to H, so that the following code will set the
1696 other fields correctly. */
1697 hi = h;
1698 }
1699
fab4a87f
L
1700 /* Check if HI is a warning symbol. */
1701 if (hi->root.type == bfd_link_hash_warning)
1702 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1703
45d6a902
AM
1704 /* If there is a duplicate definition somewhere, then HI may not
1705 point to an indirect symbol. We will have reported an error to
1706 the user in that case. */
1707
1708 if (hi->root.type == bfd_link_hash_indirect)
1709 {
1710 struct elf_link_hash_entry *ht;
1711
45d6a902 1712 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1713 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902
AM
1714
1715 /* See if the new flags lead us to realize that the symbol must
1716 be dynamic. */
1717 if (! *dynsym)
1718 {
1719 if (! dynamic)
1720 {
ca4a656b 1721 if (! info->executable
f5385ebf 1722 || hi->ref_dynamic)
45d6a902
AM
1723 *dynsym = TRUE;
1724 }
1725 else
1726 {
f5385ebf 1727 if (hi->ref_regular)
45d6a902
AM
1728 *dynsym = TRUE;
1729 }
1730 }
1731 }
1732
1733 /* We also need to define an indirection from the nondefault version
1734 of the symbol. */
1735
1736nondefault:
1737 len = strlen (name);
a50b1753 1738 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1739 if (shortname == NULL)
1740 return FALSE;
1741 memcpy (shortname, name, shortlen);
1742 memcpy (shortname + shortlen, p + 1, len - shortlen);
1743
1744 /* Once again, merge with any existing symbol. */
1745 type_change_ok = FALSE;
1746 size_change_ok = FALSE;
1747 sec = *psec;
1748 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
af44c138
L
1749 NULL, &hi, &skip, &override,
1750 &type_change_ok, &size_change_ok))
45d6a902
AM
1751 return FALSE;
1752
1753 if (skip)
1754 return TRUE;
1755
1756 if (override)
1757 {
1758 /* Here SHORTNAME is a versioned name, so we don't expect to see
1759 the type of override we do in the case above unless it is
4cc11e76 1760 overridden by a versioned definition. */
45d6a902
AM
1761 if (hi->root.type != bfd_link_hash_defined
1762 && hi->root.type != bfd_link_hash_defweak)
1763 (*_bfd_error_handler)
d003868e
AM
1764 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1765 abfd, shortname);
45d6a902
AM
1766 }
1767 else
1768 {
1769 bh = &hi->root;
1770 if (! (_bfd_generic_link_add_one_symbol
1771 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1772 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1773 return FALSE;
1774 hi = (struct elf_link_hash_entry *) bh;
1775
1776 /* If there is a duplicate definition somewhere, then HI may not
1777 point to an indirect symbol. We will have reported an error
1778 to the user in that case. */
1779
1780 if (hi->root.type == bfd_link_hash_indirect)
1781 {
fcfa13d2 1782 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
45d6a902
AM
1783
1784 /* See if the new flags lead us to realize that the symbol
1785 must be dynamic. */
1786 if (! *dynsym)
1787 {
1788 if (! dynamic)
1789 {
ca4a656b 1790 if (! info->executable
f5385ebf 1791 || hi->ref_dynamic)
45d6a902
AM
1792 *dynsym = TRUE;
1793 }
1794 else
1795 {
f5385ebf 1796 if (hi->ref_regular)
45d6a902
AM
1797 *dynsym = TRUE;
1798 }
1799 }
1800 }
1801 }
1802
1803 return TRUE;
1804}
1805\f
1806/* This routine is used to export all defined symbols into the dynamic
1807 symbol table. It is called via elf_link_hash_traverse. */
1808
28caa186 1809static bfd_boolean
268b6b39 1810_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1811{
a50b1753 1812 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 1813
55255dae
L
1814 /* Ignore this if we won't export it. */
1815 if (!eif->info->export_dynamic && !h->dynamic)
1816 return TRUE;
1817
45d6a902
AM
1818 /* Ignore indirect symbols. These are added by the versioning code. */
1819 if (h->root.type == bfd_link_hash_indirect)
1820 return TRUE;
1821
1822 if (h->root.type == bfd_link_hash_warning)
1823 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1824
1825 if (h->dynindx == -1
f5385ebf
AM
1826 && (h->def_regular
1827 || h->ref_regular))
45d6a902 1828 {
1e8fa21e 1829 bfd_boolean hide;
45d6a902 1830
1e8fa21e 1831 if (eif->verdefs == NULL
09e2aba4 1832 || (bfd_find_version_for_sym (eif->verdefs, h->root.root.string, &hide)
1e8fa21e 1833 && !hide))
45d6a902 1834 {
c152c796 1835 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
1836 {
1837 eif->failed = TRUE;
1838 return FALSE;
1839 }
1840 }
1841 }
1842
1843 return TRUE;
1844}
1845\f
1846/* Look through the symbols which are defined in other shared
1847 libraries and referenced here. Update the list of version
1848 dependencies. This will be put into the .gnu.version_r section.
1849 This function is called via elf_link_hash_traverse. */
1850
28caa186 1851static bfd_boolean
268b6b39
AM
1852_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1853 void *data)
45d6a902 1854{
a50b1753 1855 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
1856 Elf_Internal_Verneed *t;
1857 Elf_Internal_Vernaux *a;
1858 bfd_size_type amt;
1859
1860 if (h->root.type == bfd_link_hash_warning)
1861 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1862
1863 /* We only care about symbols defined in shared objects with version
1864 information. */
f5385ebf
AM
1865 if (!h->def_dynamic
1866 || h->def_regular
45d6a902
AM
1867 || h->dynindx == -1
1868 || h->verinfo.verdef == NULL)
1869 return TRUE;
1870
1871 /* See if we already know about this version. */
28caa186
AM
1872 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1873 t != NULL;
1874 t = t->vn_nextref)
45d6a902
AM
1875 {
1876 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1877 continue;
1878
1879 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1880 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1881 return TRUE;
1882
1883 break;
1884 }
1885
1886 /* This is a new version. Add it to tree we are building. */
1887
1888 if (t == NULL)
1889 {
1890 amt = sizeof *t;
a50b1753 1891 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
1892 if (t == NULL)
1893 {
1894 rinfo->failed = TRUE;
1895 return FALSE;
1896 }
1897
1898 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
1899 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
1900 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
1901 }
1902
1903 amt = sizeof *a;
a50b1753 1904 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
1905 if (a == NULL)
1906 {
1907 rinfo->failed = TRUE;
1908 return FALSE;
1909 }
45d6a902
AM
1910
1911 /* Note that we are copying a string pointer here, and testing it
1912 above. If bfd_elf_string_from_elf_section is ever changed to
1913 discard the string data when low in memory, this will have to be
1914 fixed. */
1915 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1916
1917 a->vna_flags = h->verinfo.verdef->vd_flags;
1918 a->vna_nextptr = t->vn_auxptr;
1919
1920 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1921 ++rinfo->vers;
1922
1923 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1924
1925 t->vn_auxptr = a;
1926
1927 return TRUE;
1928}
1929
1930/* Figure out appropriate versions for all the symbols. We may not
1931 have the version number script until we have read all of the input
1932 files, so until that point we don't know which symbols should be
1933 local. This function is called via elf_link_hash_traverse. */
1934
28caa186 1935static bfd_boolean
268b6b39 1936_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 1937{
28caa186 1938 struct elf_info_failed *sinfo;
45d6a902 1939 struct bfd_link_info *info;
9c5bfbb7 1940 const struct elf_backend_data *bed;
45d6a902
AM
1941 struct elf_info_failed eif;
1942 char *p;
1943 bfd_size_type amt;
1944
a50b1753 1945 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
1946 info = sinfo->info;
1947
1948 if (h->root.type == bfd_link_hash_warning)
1949 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1950
1951 /* Fix the symbol flags. */
1952 eif.failed = FALSE;
1953 eif.info = info;
1954 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1955 {
1956 if (eif.failed)
1957 sinfo->failed = TRUE;
1958 return FALSE;
1959 }
1960
1961 /* We only need version numbers for symbols defined in regular
1962 objects. */
f5385ebf 1963 if (!h->def_regular)
45d6a902
AM
1964 return TRUE;
1965
28caa186 1966 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
1967 p = strchr (h->root.root.string, ELF_VER_CHR);
1968 if (p != NULL && h->verinfo.vertree == NULL)
1969 {
1970 struct bfd_elf_version_tree *t;
1971 bfd_boolean hidden;
1972
1973 hidden = TRUE;
1974
1975 /* There are two consecutive ELF_VER_CHR characters if this is
1976 not a hidden symbol. */
1977 ++p;
1978 if (*p == ELF_VER_CHR)
1979 {
1980 hidden = FALSE;
1981 ++p;
1982 }
1983
1984 /* If there is no version string, we can just return out. */
1985 if (*p == '\0')
1986 {
1987 if (hidden)
f5385ebf 1988 h->hidden = 1;
45d6a902
AM
1989 return TRUE;
1990 }
1991
1992 /* Look for the version. If we find it, it is no longer weak. */
1993 for (t = sinfo->verdefs; t != NULL; t = t->next)
1994 {
1995 if (strcmp (t->name, p) == 0)
1996 {
1997 size_t len;
1998 char *alc;
1999 struct bfd_elf_version_expr *d;
2000
2001 len = p - h->root.root.string;
a50b1753 2002 alc = (char *) bfd_malloc (len);
45d6a902 2003 if (alc == NULL)
14b1c01e
AM
2004 {
2005 sinfo->failed = TRUE;
2006 return FALSE;
2007 }
45d6a902
AM
2008 memcpy (alc, h->root.root.string, len - 1);
2009 alc[len - 1] = '\0';
2010 if (alc[len - 2] == ELF_VER_CHR)
2011 alc[len - 2] = '\0';
2012
2013 h->verinfo.vertree = t;
2014 t->used = TRUE;
2015 d = NULL;
2016
108ba305
JJ
2017 if (t->globals.list != NULL)
2018 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2019
2020 /* See if there is anything to force this symbol to
2021 local scope. */
108ba305 2022 if (d == NULL && t->locals.list != NULL)
45d6a902 2023 {
108ba305
JJ
2024 d = (*t->match) (&t->locals, NULL, alc);
2025 if (d != NULL
2026 && h->dynindx != -1
108ba305
JJ
2027 && ! info->export_dynamic)
2028 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2029 }
2030
2031 free (alc);
2032 break;
2033 }
2034 }
2035
2036 /* If we are building an application, we need to create a
2037 version node for this version. */
36af4a4e 2038 if (t == NULL && info->executable)
45d6a902
AM
2039 {
2040 struct bfd_elf_version_tree **pp;
2041 int version_index;
2042
2043 /* If we aren't going to export this symbol, we don't need
2044 to worry about it. */
2045 if (h->dynindx == -1)
2046 return TRUE;
2047
2048 amt = sizeof *t;
a50b1753 2049 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
45d6a902
AM
2050 if (t == NULL)
2051 {
2052 sinfo->failed = TRUE;
2053 return FALSE;
2054 }
2055
45d6a902 2056 t->name = p;
45d6a902
AM
2057 t->name_indx = (unsigned int) -1;
2058 t->used = TRUE;
2059
2060 version_index = 1;
2061 /* Don't count anonymous version tag. */
2062 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
2063 version_index = 0;
2064 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
2065 ++version_index;
2066 t->vernum = version_index;
2067
2068 *pp = t;
2069
2070 h->verinfo.vertree = t;
2071 }
2072 else if (t == NULL)
2073 {
2074 /* We could not find the version for a symbol when
2075 generating a shared archive. Return an error. */
2076 (*_bfd_error_handler)
c55fe096 2077 (_("%B: version node not found for symbol %s"),
28caa186 2078 info->output_bfd, h->root.root.string);
45d6a902
AM
2079 bfd_set_error (bfd_error_bad_value);
2080 sinfo->failed = TRUE;
2081 return FALSE;
2082 }
2083
2084 if (hidden)
f5385ebf 2085 h->hidden = 1;
45d6a902
AM
2086 }
2087
2088 /* If we don't have a version for this symbol, see if we can find
2089 something. */
2090 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
2091 {
1e8fa21e 2092 bfd_boolean hide;
ae5a3597 2093
09e2aba4 2094 h->verinfo.vertree = bfd_find_version_for_sym (sinfo->verdefs,
1e8fa21e
AM
2095 h->root.root.string, &hide);
2096 if (h->verinfo.vertree != NULL && hide)
2097 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2098 }
2099
2100 return TRUE;
2101}
2102\f
45d6a902
AM
2103/* Read and swap the relocs from the section indicated by SHDR. This
2104 may be either a REL or a RELA section. The relocations are
2105 translated into RELA relocations and stored in INTERNAL_RELOCS,
2106 which should have already been allocated to contain enough space.
2107 The EXTERNAL_RELOCS are a buffer where the external form of the
2108 relocations should be stored.
2109
2110 Returns FALSE if something goes wrong. */
2111
2112static bfd_boolean
268b6b39 2113elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2114 asection *sec,
268b6b39
AM
2115 Elf_Internal_Shdr *shdr,
2116 void *external_relocs,
2117 Elf_Internal_Rela *internal_relocs)
45d6a902 2118{
9c5bfbb7 2119 const struct elf_backend_data *bed;
268b6b39 2120 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2121 const bfd_byte *erela;
2122 const bfd_byte *erelaend;
2123 Elf_Internal_Rela *irela;
243ef1e0
L
2124 Elf_Internal_Shdr *symtab_hdr;
2125 size_t nsyms;
45d6a902 2126
45d6a902
AM
2127 /* Position ourselves at the start of the section. */
2128 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2129 return FALSE;
2130
2131 /* Read the relocations. */
2132 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2133 return FALSE;
2134
243ef1e0 2135 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2136 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2137
45d6a902
AM
2138 bed = get_elf_backend_data (abfd);
2139
2140 /* Convert the external relocations to the internal format. */
2141 if (shdr->sh_entsize == bed->s->sizeof_rel)
2142 swap_in = bed->s->swap_reloc_in;
2143 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2144 swap_in = bed->s->swap_reloca_in;
2145 else
2146 {
2147 bfd_set_error (bfd_error_wrong_format);
2148 return FALSE;
2149 }
2150
a50b1753 2151 erela = (const bfd_byte *) external_relocs;
51992aec 2152 erelaend = erela + shdr->sh_size;
45d6a902
AM
2153 irela = internal_relocs;
2154 while (erela < erelaend)
2155 {
243ef1e0
L
2156 bfd_vma r_symndx;
2157
45d6a902 2158 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2159 r_symndx = ELF32_R_SYM (irela->r_info);
2160 if (bed->s->arch_size == 64)
2161 r_symndx >>= 24;
ce98a316
NC
2162 if (nsyms > 0)
2163 {
2164 if ((size_t) r_symndx >= nsyms)
2165 {
2166 (*_bfd_error_handler)
2167 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2168 " for offset 0x%lx in section `%A'"),
2169 abfd, sec,
2170 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2171 bfd_set_error (bfd_error_bad_value);
2172 return FALSE;
2173 }
2174 }
cf35638d 2175 else if (r_symndx != STN_UNDEF)
243ef1e0
L
2176 {
2177 (*_bfd_error_handler)
ce98a316
NC
2178 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2179 " when the object file has no symbol table"),
d003868e
AM
2180 abfd, sec,
2181 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2182 bfd_set_error (bfd_error_bad_value);
2183 return FALSE;
2184 }
45d6a902
AM
2185 irela += bed->s->int_rels_per_ext_rel;
2186 erela += shdr->sh_entsize;
2187 }
2188
2189 return TRUE;
2190}
2191
2192/* Read and swap the relocs for a section O. They may have been
2193 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2194 not NULL, they are used as buffers to read into. They are known to
2195 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2196 the return value is allocated using either malloc or bfd_alloc,
2197 according to the KEEP_MEMORY argument. If O has two relocation
2198 sections (both REL and RELA relocations), then the REL_HDR
2199 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2200 RELA_HDR relocations. */
45d6a902
AM
2201
2202Elf_Internal_Rela *
268b6b39
AM
2203_bfd_elf_link_read_relocs (bfd *abfd,
2204 asection *o,
2205 void *external_relocs,
2206 Elf_Internal_Rela *internal_relocs,
2207 bfd_boolean keep_memory)
45d6a902 2208{
268b6b39 2209 void *alloc1 = NULL;
45d6a902 2210 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2211 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2212 struct bfd_elf_section_data *esdo = elf_section_data (o);
2213 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2214
d4730f92
BS
2215 if (esdo->relocs != NULL)
2216 return esdo->relocs;
45d6a902
AM
2217
2218 if (o->reloc_count == 0)
2219 return NULL;
2220
45d6a902
AM
2221 if (internal_relocs == NULL)
2222 {
2223 bfd_size_type size;
2224
2225 size = o->reloc_count;
2226 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2227 if (keep_memory)
a50b1753 2228 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2229 else
a50b1753 2230 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2231 if (internal_relocs == NULL)
2232 goto error_return;
2233 }
2234
2235 if (external_relocs == NULL)
2236 {
d4730f92
BS
2237 bfd_size_type size = 0;
2238
2239 if (esdo->rel.hdr)
2240 size += esdo->rel.hdr->sh_size;
2241 if (esdo->rela.hdr)
2242 size += esdo->rela.hdr->sh_size;
45d6a902 2243
268b6b39 2244 alloc1 = bfd_malloc (size);
45d6a902
AM
2245 if (alloc1 == NULL)
2246 goto error_return;
2247 external_relocs = alloc1;
2248 }
2249
d4730f92
BS
2250 internal_rela_relocs = internal_relocs;
2251 if (esdo->rel.hdr)
2252 {
2253 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2254 external_relocs,
2255 internal_relocs))
2256 goto error_return;
2257 external_relocs = (((bfd_byte *) external_relocs)
2258 + esdo->rel.hdr->sh_size);
2259 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2260 * bed->s->int_rels_per_ext_rel);
2261 }
2262
2263 if (esdo->rela.hdr
2264 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2265 external_relocs,
2266 internal_rela_relocs)))
45d6a902
AM
2267 goto error_return;
2268
2269 /* Cache the results for next time, if we can. */
2270 if (keep_memory)
d4730f92 2271 esdo->relocs = internal_relocs;
45d6a902
AM
2272
2273 if (alloc1 != NULL)
2274 free (alloc1);
2275
2276 /* Don't free alloc2, since if it was allocated we are passing it
2277 back (under the name of internal_relocs). */
2278
2279 return internal_relocs;
2280
2281 error_return:
2282 if (alloc1 != NULL)
2283 free (alloc1);
2284 if (alloc2 != NULL)
4dd07732
AM
2285 {
2286 if (keep_memory)
2287 bfd_release (abfd, alloc2);
2288 else
2289 free (alloc2);
2290 }
45d6a902
AM
2291 return NULL;
2292}
2293
2294/* Compute the size of, and allocate space for, REL_HDR which is the
2295 section header for a section containing relocations for O. */
2296
28caa186 2297static bfd_boolean
268b6b39 2298_bfd_elf_link_size_reloc_section (bfd *abfd,
d4730f92 2299 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2300{
d4730f92 2301 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2302
2303 /* That allows us to calculate the size of the section. */
d4730f92 2304 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2305
2306 /* The contents field must last into write_object_contents, so we
2307 allocate it with bfd_alloc rather than malloc. Also since we
2308 cannot be sure that the contents will actually be filled in,
2309 we zero the allocated space. */
a50b1753 2310 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2311 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2312 return FALSE;
2313
d4730f92 2314 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2315 {
2316 struct elf_link_hash_entry **p;
2317
a50b1753 2318 p = (struct elf_link_hash_entry **)
d4730f92 2319 bfd_zmalloc (reldata->count * sizeof (struct elf_link_hash_entry *));
45d6a902
AM
2320 if (p == NULL)
2321 return FALSE;
2322
d4730f92 2323 reldata->hashes = p;
45d6a902
AM
2324 }
2325
2326 return TRUE;
2327}
2328
2329/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2330 originated from the section given by INPUT_REL_HDR) to the
2331 OUTPUT_BFD. */
2332
2333bfd_boolean
268b6b39
AM
2334_bfd_elf_link_output_relocs (bfd *output_bfd,
2335 asection *input_section,
2336 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2337 Elf_Internal_Rela *internal_relocs,
2338 struct elf_link_hash_entry **rel_hash
2339 ATTRIBUTE_UNUSED)
45d6a902
AM
2340{
2341 Elf_Internal_Rela *irela;
2342 Elf_Internal_Rela *irelaend;
2343 bfd_byte *erel;
d4730f92 2344 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2345 asection *output_section;
9c5bfbb7 2346 const struct elf_backend_data *bed;
268b6b39 2347 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2348 struct bfd_elf_section_data *esdo;
45d6a902
AM
2349
2350 output_section = input_section->output_section;
45d6a902 2351
d4730f92
BS
2352 bed = get_elf_backend_data (output_bfd);
2353 esdo = elf_section_data (output_section);
2354 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2355 {
d4730f92
BS
2356 output_reldata = &esdo->rel;
2357 swap_out = bed->s->swap_reloc_out;
45d6a902 2358 }
d4730f92
BS
2359 else if (esdo->rela.hdr
2360 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2361 {
d4730f92
BS
2362 output_reldata = &esdo->rela;
2363 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2364 }
2365 else
2366 {
2367 (*_bfd_error_handler)
d003868e
AM
2368 (_("%B: relocation size mismatch in %B section %A"),
2369 output_bfd, input_section->owner, input_section);
297d8443 2370 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2371 return FALSE;
2372 }
2373
d4730f92
BS
2374 erel = output_reldata->hdr->contents;
2375 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2376 irela = internal_relocs;
2377 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2378 * bed->s->int_rels_per_ext_rel);
2379 while (irela < irelaend)
2380 {
2381 (*swap_out) (output_bfd, irela, erel);
2382 irela += bed->s->int_rels_per_ext_rel;
2383 erel += input_rel_hdr->sh_entsize;
2384 }
2385
2386 /* Bump the counter, so that we know where to add the next set of
2387 relocations. */
d4730f92 2388 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2389
2390 return TRUE;
2391}
2392\f
508c3946
L
2393/* Make weak undefined symbols in PIE dynamic. */
2394
2395bfd_boolean
2396_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2397 struct elf_link_hash_entry *h)
2398{
2399 if (info->pie
2400 && h->dynindx == -1
2401 && h->root.type == bfd_link_hash_undefweak)
2402 return bfd_elf_link_record_dynamic_symbol (info, h);
2403
2404 return TRUE;
2405}
2406
45d6a902
AM
2407/* Fix up the flags for a symbol. This handles various cases which
2408 can only be fixed after all the input files are seen. This is
2409 currently called by both adjust_dynamic_symbol and
2410 assign_sym_version, which is unnecessary but perhaps more robust in
2411 the face of future changes. */
2412
28caa186 2413static bfd_boolean
268b6b39
AM
2414_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2415 struct elf_info_failed *eif)
45d6a902 2416{
33774f08 2417 const struct elf_backend_data *bed;
508c3946 2418
45d6a902
AM
2419 /* If this symbol was mentioned in a non-ELF file, try to set
2420 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2421 permit a non-ELF file to correctly refer to a symbol defined in
2422 an ELF dynamic object. */
f5385ebf 2423 if (h->non_elf)
45d6a902
AM
2424 {
2425 while (h->root.type == bfd_link_hash_indirect)
2426 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2427
2428 if (h->root.type != bfd_link_hash_defined
2429 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2430 {
2431 h->ref_regular = 1;
2432 h->ref_regular_nonweak = 1;
2433 }
45d6a902
AM
2434 else
2435 {
2436 if (h->root.u.def.section->owner != NULL
2437 && (bfd_get_flavour (h->root.u.def.section->owner)
2438 == bfd_target_elf_flavour))
f5385ebf
AM
2439 {
2440 h->ref_regular = 1;
2441 h->ref_regular_nonweak = 1;
2442 }
45d6a902 2443 else
f5385ebf 2444 h->def_regular = 1;
45d6a902
AM
2445 }
2446
2447 if (h->dynindx == -1
f5385ebf
AM
2448 && (h->def_dynamic
2449 || h->ref_dynamic))
45d6a902 2450 {
c152c796 2451 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2452 {
2453 eif->failed = TRUE;
2454 return FALSE;
2455 }
2456 }
2457 }
2458 else
2459 {
f5385ebf 2460 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2461 was first seen in a non-ELF file. Fortunately, if the symbol
2462 was first seen in an ELF file, we're probably OK unless the
2463 symbol was defined in a non-ELF file. Catch that case here.
2464 FIXME: We're still in trouble if the symbol was first seen in
2465 a dynamic object, and then later in a non-ELF regular object. */
2466 if ((h->root.type == bfd_link_hash_defined
2467 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2468 && !h->def_regular
45d6a902
AM
2469 && (h->root.u.def.section->owner != NULL
2470 ? (bfd_get_flavour (h->root.u.def.section->owner)
2471 != bfd_target_elf_flavour)
2472 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2473 && !h->def_dynamic)))
2474 h->def_regular = 1;
45d6a902
AM
2475 }
2476
508c3946 2477 /* Backend specific symbol fixup. */
33774f08
AM
2478 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2479 if (bed->elf_backend_fixup_symbol
2480 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2481 return FALSE;
508c3946 2482
45d6a902
AM
2483 /* If this is a final link, and the symbol was defined as a common
2484 symbol in a regular object file, and there was no definition in
2485 any dynamic object, then the linker will have allocated space for
f5385ebf 2486 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2487 flag will not have been set. */
2488 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2489 && !h->def_regular
2490 && h->ref_regular
2491 && !h->def_dynamic
45d6a902 2492 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
f5385ebf 2493 h->def_regular = 1;
45d6a902
AM
2494
2495 /* If -Bsymbolic was used (which means to bind references to global
2496 symbols to the definition within the shared object), and this
2497 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2498 need a PLT entry. Likewise, if the symbol has non-default
2499 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2500 will force it local. */
f5385ebf 2501 if (h->needs_plt
45d6a902 2502 && eif->info->shared
0eddce27 2503 && is_elf_hash_table (eif->info->hash)
55255dae 2504 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2505 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2506 && h->def_regular)
45d6a902 2507 {
45d6a902
AM
2508 bfd_boolean force_local;
2509
45d6a902
AM
2510 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2511 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2512 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2513 }
2514
2515 /* If a weak undefined symbol has non-default visibility, we also
2516 hide it from the dynamic linker. */
9c7a29a3 2517 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2518 && h->root.type == bfd_link_hash_undefweak)
33774f08 2519 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2520
2521 /* If this is a weak defined symbol in a dynamic object, and we know
2522 the real definition in the dynamic object, copy interesting flags
2523 over to the real definition. */
f6e332e6 2524 if (h->u.weakdef != NULL)
45d6a902
AM
2525 {
2526 struct elf_link_hash_entry *weakdef;
2527
f6e332e6 2528 weakdef = h->u.weakdef;
45d6a902
AM
2529 if (h->root.type == bfd_link_hash_indirect)
2530 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2531
2532 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2533 || h->root.type == bfd_link_hash_defweak);
f5385ebf 2534 BFD_ASSERT (weakdef->def_dynamic);
45d6a902
AM
2535
2536 /* If the real definition is defined by a regular object file,
2537 don't do anything special. See the longer description in
2538 _bfd_elf_adjust_dynamic_symbol, below. */
f5385ebf 2539 if (weakdef->def_regular)
f6e332e6 2540 h->u.weakdef = NULL;
45d6a902 2541 else
a26587ba
RS
2542 {
2543 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2544 || weakdef->root.type == bfd_link_hash_defweak);
2545 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2546 }
45d6a902
AM
2547 }
2548
2549 return TRUE;
2550}
2551
2552/* Make the backend pick a good value for a dynamic symbol. This is
2553 called via elf_link_hash_traverse, and also calls itself
2554 recursively. */
2555
28caa186 2556static bfd_boolean
268b6b39 2557_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2558{
a50b1753 2559 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2560 bfd *dynobj;
9c5bfbb7 2561 const struct elf_backend_data *bed;
45d6a902 2562
0eddce27 2563 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2564 return FALSE;
2565
2566 if (h->root.type == bfd_link_hash_warning)
2567 {
a6aa5195
AM
2568 h->got = elf_hash_table (eif->info)->init_got_offset;
2569 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2570
2571 /* When warning symbols are created, they **replace** the "real"
2572 entry in the hash table, thus we never get to see the real
2573 symbol in a hash traversal. So look at it now. */
2574 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2575 }
2576
2577 /* Ignore indirect symbols. These are added by the versioning code. */
2578 if (h->root.type == bfd_link_hash_indirect)
2579 return TRUE;
2580
2581 /* Fix the symbol flags. */
2582 if (! _bfd_elf_fix_symbol_flags (h, eif))
2583 return FALSE;
2584
2585 /* If this symbol does not require a PLT entry, and it is not
2586 defined by a dynamic object, or is not referenced by a regular
2587 object, ignore it. We do have to handle a weak defined symbol,
2588 even if no regular object refers to it, if we decided to add it
2589 to the dynamic symbol table. FIXME: Do we normally need to worry
2590 about symbols which are defined by one dynamic object and
2591 referenced by another one? */
f5385ebf 2592 if (!h->needs_plt
91e21fb7 2593 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2594 && (h->def_regular
2595 || !h->def_dynamic
2596 || (!h->ref_regular
f6e332e6 2597 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2598 {
a6aa5195 2599 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2600 return TRUE;
2601 }
2602
2603 /* If we've already adjusted this symbol, don't do it again. This
2604 can happen via a recursive call. */
f5385ebf 2605 if (h->dynamic_adjusted)
45d6a902
AM
2606 return TRUE;
2607
2608 /* Don't look at this symbol again. Note that we must set this
2609 after checking the above conditions, because we may look at a
2610 symbol once, decide not to do anything, and then get called
2611 recursively later after REF_REGULAR is set below. */
f5385ebf 2612 h->dynamic_adjusted = 1;
45d6a902
AM
2613
2614 /* If this is a weak definition, and we know a real definition, and
2615 the real symbol is not itself defined by a regular object file,
2616 then get a good value for the real definition. We handle the
2617 real symbol first, for the convenience of the backend routine.
2618
2619 Note that there is a confusing case here. If the real definition
2620 is defined by a regular object file, we don't get the real symbol
2621 from the dynamic object, but we do get the weak symbol. If the
2622 processor backend uses a COPY reloc, then if some routine in the
2623 dynamic object changes the real symbol, we will not see that
2624 change in the corresponding weak symbol. This is the way other
2625 ELF linkers work as well, and seems to be a result of the shared
2626 library model.
2627
2628 I will clarify this issue. Most SVR4 shared libraries define the
2629 variable _timezone and define timezone as a weak synonym. The
2630 tzset call changes _timezone. If you write
2631 extern int timezone;
2632 int _timezone = 5;
2633 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2634 you might expect that, since timezone is a synonym for _timezone,
2635 the same number will print both times. However, if the processor
2636 backend uses a COPY reloc, then actually timezone will be copied
2637 into your process image, and, since you define _timezone
2638 yourself, _timezone will not. Thus timezone and _timezone will
2639 wind up at different memory locations. The tzset call will set
2640 _timezone, leaving timezone unchanged. */
2641
f6e332e6 2642 if (h->u.weakdef != NULL)
45d6a902
AM
2643 {
2644 /* If we get to this point, we know there is an implicit
2645 reference by a regular object file via the weak symbol H.
2646 FIXME: Is this really true? What if the traversal finds
f6e332e6
AM
2647 H->U.WEAKDEF before it finds H? */
2648 h->u.weakdef->ref_regular = 1;
45d6a902 2649
f6e332e6 2650 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2651 return FALSE;
2652 }
2653
2654 /* If a symbol has no type and no size and does not require a PLT
2655 entry, then we are probably about to do the wrong thing here: we
2656 are probably going to create a COPY reloc for an empty object.
2657 This case can arise when a shared object is built with assembly
2658 code, and the assembly code fails to set the symbol type. */
2659 if (h->size == 0
2660 && h->type == STT_NOTYPE
f5385ebf 2661 && !h->needs_plt)
45d6a902
AM
2662 (*_bfd_error_handler)
2663 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2664 h->root.root.string);
2665
2666 dynobj = elf_hash_table (eif->info)->dynobj;
2667 bed = get_elf_backend_data (dynobj);
e7c33416 2668
45d6a902
AM
2669 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2670 {
2671 eif->failed = TRUE;
2672 return FALSE;
2673 }
2674
2675 return TRUE;
2676}
2677
027297b7
L
2678/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2679 DYNBSS. */
2680
2681bfd_boolean
2682_bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2683 asection *dynbss)
2684{
91ac5911 2685 unsigned int power_of_two;
027297b7
L
2686 bfd_vma mask;
2687 asection *sec = h->root.u.def.section;
2688
2689 /* The section aligment of definition is the maximum alignment
91ac5911
L
2690 requirement of symbols defined in the section. Since we don't
2691 know the symbol alignment requirement, we start with the
2692 maximum alignment and check low bits of the symbol address
2693 for the minimum alignment. */
2694 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2695 mask = ((bfd_vma) 1 << power_of_two) - 1;
2696 while ((h->root.u.def.value & mask) != 0)
2697 {
2698 mask >>= 1;
2699 --power_of_two;
2700 }
027297b7 2701
91ac5911
L
2702 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2703 dynbss))
027297b7
L
2704 {
2705 /* Adjust the section alignment if needed. */
2706 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2707 power_of_two))
027297b7
L
2708 return FALSE;
2709 }
2710
91ac5911 2711 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2712 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2713
2714 /* Define the symbol as being at this point in DYNBSS. */
2715 h->root.u.def.section = dynbss;
2716 h->root.u.def.value = dynbss->size;
2717
2718 /* Increment the size of DYNBSS to make room for the symbol. */
2719 dynbss->size += h->size;
2720
2721 return TRUE;
2722}
2723
45d6a902
AM
2724/* Adjust all external symbols pointing into SEC_MERGE sections
2725 to reflect the object merging within the sections. */
2726
28caa186 2727static bfd_boolean
268b6b39 2728_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2729{
2730 asection *sec;
2731
2732 if (h->root.type == bfd_link_hash_warning)
2733 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2734
2735 if ((h->root.type == bfd_link_hash_defined
2736 || h->root.type == bfd_link_hash_defweak)
2737 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2738 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2739 {
a50b1753 2740 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2741
2742 h->root.u.def.value =
2743 _bfd_merged_section_offset (output_bfd,
2744 &h->root.u.def.section,
2745 elf_section_data (sec)->sec_info,
753731ee 2746 h->root.u.def.value);
45d6a902
AM
2747 }
2748
2749 return TRUE;
2750}
986a241f
RH
2751
2752/* Returns false if the symbol referred to by H should be considered
2753 to resolve local to the current module, and true if it should be
2754 considered to bind dynamically. */
2755
2756bfd_boolean
268b6b39
AM
2757_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2758 struct bfd_link_info *info,
89a2ee5a 2759 bfd_boolean not_local_protected)
986a241f
RH
2760{
2761 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2762 const struct elf_backend_data *bed;
2763 struct elf_link_hash_table *hash_table;
986a241f
RH
2764
2765 if (h == NULL)
2766 return FALSE;
2767
2768 while (h->root.type == bfd_link_hash_indirect
2769 || h->root.type == bfd_link_hash_warning)
2770 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2771
2772 /* If it was forced local, then clearly it's not dynamic. */
2773 if (h->dynindx == -1)
2774 return FALSE;
f5385ebf 2775 if (h->forced_local)
986a241f
RH
2776 return FALSE;
2777
2778 /* Identify the cases where name binding rules say that a
2779 visible symbol resolves locally. */
55255dae 2780 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
986a241f
RH
2781
2782 switch (ELF_ST_VISIBILITY (h->other))
2783 {
2784 case STV_INTERNAL:
2785 case STV_HIDDEN:
2786 return FALSE;
2787
2788 case STV_PROTECTED:
fcb93ecf
PB
2789 hash_table = elf_hash_table (info);
2790 if (!is_elf_hash_table (hash_table))
2791 return FALSE;
2792
2793 bed = get_elf_backend_data (hash_table->dynobj);
2794
986a241f
RH
2795 /* Proper resolution for function pointer equality may require
2796 that these symbols perhaps be resolved dynamically, even though
2797 we should be resolving them to the current module. */
89a2ee5a 2798 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2799 binding_stays_local_p = TRUE;
2800 break;
2801
2802 default:
986a241f
RH
2803 break;
2804 }
2805
aa37626c 2806 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2807 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2808 return TRUE;
2809
986a241f
RH
2810 /* Otherwise, the symbol is dynamic if binding rules don't tell
2811 us that it remains local. */
2812 return !binding_stays_local_p;
2813}
f6c52c13
AM
2814
2815/* Return true if the symbol referred to by H should be considered
2816 to resolve local to the current module, and false otherwise. Differs
2817 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2818 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2819 for the place where forced_local and dynindx == -1 are tested. If
2820 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2821 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2822 the symbol is local only for defined symbols.
2823 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2824 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2825 treatment of undefined weak symbols. For those that do not make
2826 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2827
2828bfd_boolean
268b6b39
AM
2829_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2830 struct bfd_link_info *info,
2831 bfd_boolean local_protected)
f6c52c13 2832{
fcb93ecf
PB
2833 const struct elf_backend_data *bed;
2834 struct elf_link_hash_table *hash_table;
2835
f6c52c13
AM
2836 /* If it's a local sym, of course we resolve locally. */
2837 if (h == NULL)
2838 return TRUE;
2839
d95edcac
L
2840 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2841 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2842 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2843 return TRUE;
2844
7e2294f9
AO
2845 /* Common symbols that become definitions don't get the DEF_REGULAR
2846 flag set, so test it first, and don't bail out. */
2847 if (ELF_COMMON_DEF_P (h))
2848 /* Do nothing. */;
f6c52c13 2849 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2850 resolve locally. The sym is either undefined or dynamic. */
2851 else if (!h->def_regular)
f6c52c13
AM
2852 return FALSE;
2853
2854 /* Forced local symbols resolve locally. */
f5385ebf 2855 if (h->forced_local)
f6c52c13
AM
2856 return TRUE;
2857
2858 /* As do non-dynamic symbols. */
2859 if (h->dynindx == -1)
2860 return TRUE;
2861
2862 /* At this point, we know the symbol is defined and dynamic. In an
2863 executable it must resolve locally, likewise when building symbolic
2864 shared libraries. */
55255dae 2865 if (info->executable || SYMBOLIC_BIND (info, h))
f6c52c13
AM
2866 return TRUE;
2867
2868 /* Now deal with defined dynamic symbols in shared libraries. Ones
2869 with default visibility might not resolve locally. */
2870 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2871 return FALSE;
2872
fcb93ecf
PB
2873 hash_table = elf_hash_table (info);
2874 if (!is_elf_hash_table (hash_table))
2875 return TRUE;
2876
2877 bed = get_elf_backend_data (hash_table->dynobj);
2878
1c16dfa5 2879 /* STV_PROTECTED non-function symbols are local. */
fcb93ecf 2880 if (!bed->is_function_type (h->type))
1c16dfa5
L
2881 return TRUE;
2882
f6c52c13 2883 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
2884 symbols be treated as dynamic symbols. If the address of a
2885 function not defined in an executable is set to that function's
2886 plt entry in the executable, then the address of the function in
2887 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
2888 return local_protected;
2889}
e1918d23
AM
2890
2891/* Caches some TLS segment info, and ensures that the TLS segment vma is
2892 aligned. Returns the first TLS output section. */
2893
2894struct bfd_section *
2895_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2896{
2897 struct bfd_section *sec, *tls;
2898 unsigned int align = 0;
2899
2900 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2901 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2902 break;
2903 tls = sec;
2904
2905 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2906 if (sec->alignment_power > align)
2907 align = sec->alignment_power;
2908
2909 elf_hash_table (info)->tls_sec = tls;
2910
2911 /* Ensure the alignment of the first section is the largest alignment,
2912 so that the tls segment starts aligned. */
2913 if (tls != NULL)
2914 tls->alignment_power = align;
2915
2916 return tls;
2917}
0ad989f9
L
2918
2919/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2920static bfd_boolean
2921is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2922 Elf_Internal_Sym *sym)
2923{
a4d8e49b
L
2924 const struct elf_backend_data *bed;
2925
0ad989f9
L
2926 /* Local symbols do not count, but target specific ones might. */
2927 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2928 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2929 return FALSE;
2930
fcb93ecf 2931 bed = get_elf_backend_data (abfd);
0ad989f9 2932 /* Function symbols do not count. */
fcb93ecf 2933 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
2934 return FALSE;
2935
2936 /* If the section is undefined, then so is the symbol. */
2937 if (sym->st_shndx == SHN_UNDEF)
2938 return FALSE;
2939
2940 /* If the symbol is defined in the common section, then
2941 it is a common definition and so does not count. */
a4d8e49b 2942 if (bed->common_definition (sym))
0ad989f9
L
2943 return FALSE;
2944
2945 /* If the symbol is in a target specific section then we
2946 must rely upon the backend to tell us what it is. */
2947 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2948 /* FIXME - this function is not coded yet:
2949
2950 return _bfd_is_global_symbol_definition (abfd, sym);
2951
2952 Instead for now assume that the definition is not global,
2953 Even if this is wrong, at least the linker will behave
2954 in the same way that it used to do. */
2955 return FALSE;
2956
2957 return TRUE;
2958}
2959
2960/* Search the symbol table of the archive element of the archive ABFD
2961 whose archive map contains a mention of SYMDEF, and determine if
2962 the symbol is defined in this element. */
2963static bfd_boolean
2964elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2965{
2966 Elf_Internal_Shdr * hdr;
2967 bfd_size_type symcount;
2968 bfd_size_type extsymcount;
2969 bfd_size_type extsymoff;
2970 Elf_Internal_Sym *isymbuf;
2971 Elf_Internal_Sym *isym;
2972 Elf_Internal_Sym *isymend;
2973 bfd_boolean result;
2974
2975 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2976 if (abfd == NULL)
2977 return FALSE;
2978
2979 if (! bfd_check_format (abfd, bfd_object))
2980 return FALSE;
2981
2982 /* If we have already included the element containing this symbol in the
2983 link then we do not need to include it again. Just claim that any symbol
2984 it contains is not a definition, so that our caller will not decide to
2985 (re)include this element. */
2986 if (abfd->archive_pass)
2987 return FALSE;
2988
2989 /* Select the appropriate symbol table. */
2990 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2991 hdr = &elf_tdata (abfd)->symtab_hdr;
2992 else
2993 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2994
2995 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2996
2997 /* The sh_info field of the symtab header tells us where the
2998 external symbols start. We don't care about the local symbols. */
2999 if (elf_bad_symtab (abfd))
3000 {
3001 extsymcount = symcount;
3002 extsymoff = 0;
3003 }
3004 else
3005 {
3006 extsymcount = symcount - hdr->sh_info;
3007 extsymoff = hdr->sh_info;
3008 }
3009
3010 if (extsymcount == 0)
3011 return FALSE;
3012
3013 /* Read in the symbol table. */
3014 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3015 NULL, NULL, NULL);
3016 if (isymbuf == NULL)
3017 return FALSE;
3018
3019 /* Scan the symbol table looking for SYMDEF. */
3020 result = FALSE;
3021 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3022 {
3023 const char *name;
3024
3025 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3026 isym->st_name);
3027 if (name == NULL)
3028 break;
3029
3030 if (strcmp (name, symdef->name) == 0)
3031 {
3032 result = is_global_data_symbol_definition (abfd, isym);
3033 break;
3034 }
3035 }
3036
3037 free (isymbuf);
3038
3039 return result;
3040}
3041\f
5a580b3a
AM
3042/* Add an entry to the .dynamic table. */
3043
3044bfd_boolean
3045_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3046 bfd_vma tag,
3047 bfd_vma val)
3048{
3049 struct elf_link_hash_table *hash_table;
3050 const struct elf_backend_data *bed;
3051 asection *s;
3052 bfd_size_type newsize;
3053 bfd_byte *newcontents;
3054 Elf_Internal_Dyn dyn;
3055
3056 hash_table = elf_hash_table (info);
3057 if (! is_elf_hash_table (hash_table))
3058 return FALSE;
3059
3060 bed = get_elf_backend_data (hash_table->dynobj);
3061 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3062 BFD_ASSERT (s != NULL);
3063
eea6121a 3064 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3065 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3066 if (newcontents == NULL)
3067 return FALSE;
3068
3069 dyn.d_tag = tag;
3070 dyn.d_un.d_val = val;
eea6121a 3071 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3072
eea6121a 3073 s->size = newsize;
5a580b3a
AM
3074 s->contents = newcontents;
3075
3076 return TRUE;
3077}
3078
3079/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3080 otherwise just check whether one already exists. Returns -1 on error,
3081 1 if a DT_NEEDED tag already exists, and 0 on success. */
3082
4ad4eba5 3083static int
7e9f0867
AM
3084elf_add_dt_needed_tag (bfd *abfd,
3085 struct bfd_link_info *info,
4ad4eba5
AM
3086 const char *soname,
3087 bfd_boolean do_it)
5a580b3a
AM
3088{
3089 struct elf_link_hash_table *hash_table;
3090 bfd_size_type oldsize;
3091 bfd_size_type strindex;
3092
7e9f0867
AM
3093 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3094 return -1;
3095
5a580b3a
AM
3096 hash_table = elf_hash_table (info);
3097 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3098 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3099 if (strindex == (bfd_size_type) -1)
3100 return -1;
3101
3102 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3103 {
3104 asection *sdyn;
3105 const struct elf_backend_data *bed;
3106 bfd_byte *extdyn;
3107
3108 bed = get_elf_backend_data (hash_table->dynobj);
3109 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3110 if (sdyn != NULL)
3111 for (extdyn = sdyn->contents;
3112 extdyn < sdyn->contents + sdyn->size;
3113 extdyn += bed->s->sizeof_dyn)
3114 {
3115 Elf_Internal_Dyn dyn;
5a580b3a 3116
7e9f0867
AM
3117 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3118 if (dyn.d_tag == DT_NEEDED
3119 && dyn.d_un.d_val == strindex)
3120 {
3121 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3122 return 1;
3123 }
3124 }
5a580b3a
AM
3125 }
3126
3127 if (do_it)
3128 {
7e9f0867
AM
3129 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3130 return -1;
3131
5a580b3a
AM
3132 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3133 return -1;
3134 }
3135 else
3136 /* We were just checking for existence of the tag. */
3137 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3138
3139 return 0;
3140}
3141
010e5ae2
AM
3142static bfd_boolean
3143on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3144{
3145 for (; needed != NULL; needed = needed->next)
3146 if (strcmp (soname, needed->name) == 0)
3147 return TRUE;
3148
3149 return FALSE;
3150}
3151
5a580b3a 3152/* Sort symbol by value and section. */
4ad4eba5
AM
3153static int
3154elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3155{
3156 const struct elf_link_hash_entry *h1;
3157 const struct elf_link_hash_entry *h2;
10b7e05b 3158 bfd_signed_vma vdiff;
5a580b3a
AM
3159
3160 h1 = *(const struct elf_link_hash_entry **) arg1;
3161 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3162 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3163 if (vdiff != 0)
3164 return vdiff > 0 ? 1 : -1;
3165 else
3166 {
3167 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3168 if (sdiff != 0)
3169 return sdiff > 0 ? 1 : -1;
3170 }
5a580b3a
AM
3171 return 0;
3172}
4ad4eba5 3173
5a580b3a
AM
3174/* This function is used to adjust offsets into .dynstr for
3175 dynamic symbols. This is called via elf_link_hash_traverse. */
3176
3177static bfd_boolean
3178elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3179{
a50b1753 3180 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a
AM
3181
3182 if (h->root.type == bfd_link_hash_warning)
3183 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3184
3185 if (h->dynindx != -1)
3186 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3187 return TRUE;
3188}
3189
3190/* Assign string offsets in .dynstr, update all structures referencing
3191 them. */
3192
4ad4eba5
AM
3193static bfd_boolean
3194elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3195{
3196 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3197 struct elf_link_local_dynamic_entry *entry;
3198 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3199 bfd *dynobj = hash_table->dynobj;
3200 asection *sdyn;
3201 bfd_size_type size;
3202 const struct elf_backend_data *bed;
3203 bfd_byte *extdyn;
3204
3205 _bfd_elf_strtab_finalize (dynstr);
3206 size = _bfd_elf_strtab_size (dynstr);
3207
3208 bed = get_elf_backend_data (dynobj);
3209 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3210 BFD_ASSERT (sdyn != NULL);
3211
3212 /* Update all .dynamic entries referencing .dynstr strings. */
3213 for (extdyn = sdyn->contents;
eea6121a 3214 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3215 extdyn += bed->s->sizeof_dyn)
3216 {
3217 Elf_Internal_Dyn dyn;
3218
3219 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3220 switch (dyn.d_tag)
3221 {
3222 case DT_STRSZ:
3223 dyn.d_un.d_val = size;
3224 break;
3225 case DT_NEEDED:
3226 case DT_SONAME:
3227 case DT_RPATH:
3228 case DT_RUNPATH:
3229 case DT_FILTER:
3230 case DT_AUXILIARY:
7ee314fa
AM
3231 case DT_AUDIT:
3232 case DT_DEPAUDIT:
5a580b3a
AM
3233 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3234 break;
3235 default:
3236 continue;
3237 }
3238 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3239 }
3240
3241 /* Now update local dynamic symbols. */
3242 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3243 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3244 entry->isym.st_name);
3245
3246 /* And the rest of dynamic symbols. */
3247 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3248
3249 /* Adjust version definitions. */
3250 if (elf_tdata (output_bfd)->cverdefs)
3251 {
3252 asection *s;
3253 bfd_byte *p;
3254 bfd_size_type i;
3255 Elf_Internal_Verdef def;
3256 Elf_Internal_Verdaux defaux;
3257
3258 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3259 p = s->contents;
3260 do
3261 {
3262 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3263 &def);
3264 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3265 if (def.vd_aux != sizeof (Elf_External_Verdef))
3266 continue;
5a580b3a
AM
3267 for (i = 0; i < def.vd_cnt; ++i)
3268 {
3269 _bfd_elf_swap_verdaux_in (output_bfd,
3270 (Elf_External_Verdaux *) p, &defaux);
3271 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3272 defaux.vda_name);
3273 _bfd_elf_swap_verdaux_out (output_bfd,
3274 &defaux, (Elf_External_Verdaux *) p);
3275 p += sizeof (Elf_External_Verdaux);
3276 }
3277 }
3278 while (def.vd_next);
3279 }
3280
3281 /* Adjust version references. */
3282 if (elf_tdata (output_bfd)->verref)
3283 {
3284 asection *s;
3285 bfd_byte *p;
3286 bfd_size_type i;
3287 Elf_Internal_Verneed need;
3288 Elf_Internal_Vernaux needaux;
3289
3290 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3291 p = s->contents;
3292 do
3293 {
3294 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3295 &need);
3296 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3297 _bfd_elf_swap_verneed_out (output_bfd, &need,
3298 (Elf_External_Verneed *) p);
3299 p += sizeof (Elf_External_Verneed);
3300 for (i = 0; i < need.vn_cnt; ++i)
3301 {
3302 _bfd_elf_swap_vernaux_in (output_bfd,
3303 (Elf_External_Vernaux *) p, &needaux);
3304 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3305 needaux.vna_name);
3306 _bfd_elf_swap_vernaux_out (output_bfd,
3307 &needaux,
3308 (Elf_External_Vernaux *) p);
3309 p += sizeof (Elf_External_Vernaux);
3310 }
3311 }
3312 while (need.vn_next);
3313 }
3314
3315 return TRUE;
3316}
3317\f
13285a1b
AM
3318/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3319 The default is to only match when the INPUT and OUTPUT are exactly
3320 the same target. */
3321
3322bfd_boolean
3323_bfd_elf_default_relocs_compatible (const bfd_target *input,
3324 const bfd_target *output)
3325{
3326 return input == output;
3327}
3328
3329/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3330 This version is used when different targets for the same architecture
3331 are virtually identical. */
3332
3333bfd_boolean
3334_bfd_elf_relocs_compatible (const bfd_target *input,
3335 const bfd_target *output)
3336{
3337 const struct elf_backend_data *obed, *ibed;
3338
3339 if (input == output)
3340 return TRUE;
3341
3342 ibed = xvec_get_elf_backend_data (input);
3343 obed = xvec_get_elf_backend_data (output);
3344
3345 if (ibed->arch != obed->arch)
3346 return FALSE;
3347
3348 /* If both backends are using this function, deem them compatible. */
3349 return ibed->relocs_compatible == obed->relocs_compatible;
3350}
3351
4ad4eba5
AM
3352/* Add symbols from an ELF object file to the linker hash table. */
3353
3354static bfd_boolean
3355elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3356{
a0c402a5 3357 Elf_Internal_Ehdr *ehdr;
4ad4eba5
AM
3358 Elf_Internal_Shdr *hdr;
3359 bfd_size_type symcount;
3360 bfd_size_type extsymcount;
3361 bfd_size_type extsymoff;
3362 struct elf_link_hash_entry **sym_hash;
3363 bfd_boolean dynamic;
3364 Elf_External_Versym *extversym = NULL;
3365 Elf_External_Versym *ever;
3366 struct elf_link_hash_entry *weaks;
3367 struct elf_link_hash_entry **nondeflt_vers = NULL;
3368 bfd_size_type nondeflt_vers_cnt = 0;
3369 Elf_Internal_Sym *isymbuf = NULL;
3370 Elf_Internal_Sym *isym;
3371 Elf_Internal_Sym *isymend;
3372 const struct elf_backend_data *bed;
3373 bfd_boolean add_needed;
66eb6687 3374 struct elf_link_hash_table *htab;
4ad4eba5 3375 bfd_size_type amt;
66eb6687 3376 void *alloc_mark = NULL;
4f87808c
AM
3377 struct bfd_hash_entry **old_table = NULL;
3378 unsigned int old_size = 0;
3379 unsigned int old_count = 0;
66eb6687
AM
3380 void *old_tab = NULL;
3381 void *old_hash;
3382 void *old_ent;
3383 struct bfd_link_hash_entry *old_undefs = NULL;
3384 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3385 long old_dynsymcount = 0;
3386 size_t tabsize = 0;
3387 size_t hashsize = 0;
4ad4eba5 3388
66eb6687 3389 htab = elf_hash_table (info);
4ad4eba5 3390 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3391
3392 if ((abfd->flags & DYNAMIC) == 0)
3393 dynamic = FALSE;
3394 else
3395 {
3396 dynamic = TRUE;
3397
3398 /* You can't use -r against a dynamic object. Also, there's no
3399 hope of using a dynamic object which does not exactly match
3400 the format of the output file. */
3401 if (info->relocatable
66eb6687 3402 || !is_elf_hash_table (htab)
f13a99db 3403 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3404 {
9a0789ec
NC
3405 if (info->relocatable)
3406 bfd_set_error (bfd_error_invalid_operation);
3407 else
3408 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3409 goto error_return;
3410 }
3411 }
3412
a0c402a5
L
3413 ehdr = elf_elfheader (abfd);
3414 if (info->warn_alternate_em
3415 && bed->elf_machine_code != ehdr->e_machine
3416 && ((bed->elf_machine_alt1 != 0
3417 && ehdr->e_machine == bed->elf_machine_alt1)
3418 || (bed->elf_machine_alt2 != 0
3419 && ehdr->e_machine == bed->elf_machine_alt2)))
3420 info->callbacks->einfo
3421 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3422 ehdr->e_machine, abfd, bed->elf_machine_code);
3423
4ad4eba5
AM
3424 /* As a GNU extension, any input sections which are named
3425 .gnu.warning.SYMBOL are treated as warning symbols for the given
3426 symbol. This differs from .gnu.warning sections, which generate
3427 warnings when they are included in an output file. */
dd98f8d2
NC
3428 /* PR 12761: Also generate this warning when building shared libraries. */
3429 if (info->executable || info->shared)
4ad4eba5
AM
3430 {
3431 asection *s;
3432
3433 for (s = abfd->sections; s != NULL; s = s->next)
3434 {
3435 const char *name;
3436
3437 name = bfd_get_section_name (abfd, s);
0112cd26 3438 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5
AM
3439 {
3440 char *msg;
3441 bfd_size_type sz;
4ad4eba5
AM
3442
3443 name += sizeof ".gnu.warning." - 1;
3444
3445 /* If this is a shared object, then look up the symbol
3446 in the hash table. If it is there, and it is already
3447 been defined, then we will not be using the entry
3448 from this shared object, so we don't need to warn.
3449 FIXME: If we see the definition in a regular object
3450 later on, we will warn, but we shouldn't. The only
3451 fix is to keep track of what warnings we are supposed
3452 to emit, and then handle them all at the end of the
3453 link. */
3454 if (dynamic)
3455 {
3456 struct elf_link_hash_entry *h;
3457
66eb6687 3458 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4ad4eba5
AM
3459
3460 /* FIXME: What about bfd_link_hash_common? */
3461 if (h != NULL
3462 && (h->root.type == bfd_link_hash_defined
3463 || h->root.type == bfd_link_hash_defweak))
3464 {
3465 /* We don't want to issue this warning. Clobber
3466 the section size so that the warning does not
3467 get copied into the output file. */
eea6121a 3468 s->size = 0;
4ad4eba5
AM
3469 continue;
3470 }
3471 }
3472
eea6121a 3473 sz = s->size;
a50b1753 3474 msg = (char *) bfd_alloc (abfd, sz + 1);
4ad4eba5
AM
3475 if (msg == NULL)
3476 goto error_return;
3477
370a0e1b 3478 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4ad4eba5
AM
3479 goto error_return;
3480
370a0e1b 3481 msg[sz] = '\0';
4ad4eba5
AM
3482
3483 if (! (_bfd_generic_link_add_one_symbol
3484 (info, abfd, name, BSF_WARNING, s, 0, msg,
66eb6687 3485 FALSE, bed->collect, NULL)))
4ad4eba5
AM
3486 goto error_return;
3487
3488 if (! info->relocatable)
3489 {
3490 /* Clobber the section size so that the warning does
3491 not get copied into the output file. */
eea6121a 3492 s->size = 0;
11d2f718
AM
3493
3494 /* Also set SEC_EXCLUDE, so that symbols defined in
3495 the warning section don't get copied to the output. */
3496 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3497 }
3498 }
3499 }
3500 }
3501
3502 add_needed = TRUE;
3503 if (! dynamic)
3504 {
3505 /* If we are creating a shared library, create all the dynamic
3506 sections immediately. We need to attach them to something,
3507 so we attach them to this BFD, provided it is the right
3508 format. FIXME: If there are no input BFD's of the same
3509 format as the output, we can't make a shared library. */
3510 if (info->shared
66eb6687 3511 && is_elf_hash_table (htab)
f13a99db 3512 && info->output_bfd->xvec == abfd->xvec
66eb6687 3513 && !htab->dynamic_sections_created)
4ad4eba5
AM
3514 {
3515 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3516 goto error_return;
3517 }
3518 }
66eb6687 3519 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3520 goto error_return;
3521 else
3522 {
3523 asection *s;
3524 const char *soname = NULL;
7ee314fa 3525 char *audit = NULL;
4ad4eba5
AM
3526 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3527 int ret;
3528
3529 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3530 ld shouldn't allow it. */
4ad4eba5
AM
3531 if ((s = abfd->sections) != NULL
3532 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
92fd189d 3533 abort ();
4ad4eba5
AM
3534
3535 /* If this dynamic lib was specified on the command line with
3536 --as-needed in effect, then we don't want to add a DT_NEEDED
3537 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3538 in by another lib's DT_NEEDED. When --no-add-needed is used
3539 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3540 any dynamic library in DT_NEEDED tags in the dynamic lib at
3541 all. */
3542 add_needed = (elf_dyn_lib_class (abfd)
3543 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3544 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3545
3546 s = bfd_get_section_by_name (abfd, ".dynamic");
3547 if (s != NULL)
3548 {
3549 bfd_byte *dynbuf;
3550 bfd_byte *extdyn;
cb33740c 3551 unsigned int elfsec;
4ad4eba5
AM
3552 unsigned long shlink;
3553
eea6121a 3554 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3555 {
3556error_free_dyn:
3557 free (dynbuf);
3558 goto error_return;
3559 }
4ad4eba5
AM
3560
3561 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3562 if (elfsec == SHN_BAD)
4ad4eba5
AM
3563 goto error_free_dyn;
3564 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3565
3566 for (extdyn = dynbuf;
eea6121a 3567 extdyn < dynbuf + s->size;
4ad4eba5
AM
3568 extdyn += bed->s->sizeof_dyn)
3569 {
3570 Elf_Internal_Dyn dyn;
3571
3572 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3573 if (dyn.d_tag == DT_SONAME)
3574 {
3575 unsigned int tagv = dyn.d_un.d_val;
3576 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3577 if (soname == NULL)
3578 goto error_free_dyn;
3579 }
3580 if (dyn.d_tag == DT_NEEDED)
3581 {
3582 struct bfd_link_needed_list *n, **pn;
3583 char *fnm, *anm;
3584 unsigned int tagv = dyn.d_un.d_val;
3585
3586 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3587 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3588 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3589 if (n == NULL || fnm == NULL)
3590 goto error_free_dyn;
3591 amt = strlen (fnm) + 1;
a50b1753 3592 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3593 if (anm == NULL)
3594 goto error_free_dyn;
3595 memcpy (anm, fnm, amt);
3596 n->name = anm;
3597 n->by = abfd;
3598 n->next = NULL;
66eb6687 3599 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3600 ;
3601 *pn = n;
3602 }
3603 if (dyn.d_tag == DT_RUNPATH)
3604 {
3605 struct bfd_link_needed_list *n, **pn;
3606 char *fnm, *anm;
3607 unsigned int tagv = dyn.d_un.d_val;
3608
3609 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3610 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3611 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3612 if (n == NULL || fnm == NULL)
3613 goto error_free_dyn;
3614 amt = strlen (fnm) + 1;
a50b1753 3615 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3616 if (anm == NULL)
3617 goto error_free_dyn;
3618 memcpy (anm, fnm, amt);
3619 n->name = anm;
3620 n->by = abfd;
3621 n->next = NULL;
3622 for (pn = & runpath;
3623 *pn != NULL;
3624 pn = &(*pn)->next)
3625 ;
3626 *pn = n;
3627 }
3628 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3629 if (!runpath && dyn.d_tag == DT_RPATH)
3630 {
3631 struct bfd_link_needed_list *n, **pn;
3632 char *fnm, *anm;
3633 unsigned int tagv = dyn.d_un.d_val;
3634
3635 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3636 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3637 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3638 if (n == NULL || fnm == NULL)
3639 goto error_free_dyn;
3640 amt = strlen (fnm) + 1;
a50b1753 3641 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3642 if (anm == NULL)
f8703194 3643 goto error_free_dyn;
4ad4eba5
AM
3644 memcpy (anm, fnm, amt);
3645 n->name = anm;
3646 n->by = abfd;
3647 n->next = NULL;
3648 for (pn = & rpath;
3649 *pn != NULL;
3650 pn = &(*pn)->next)
3651 ;
3652 *pn = n;
3653 }
7ee314fa
AM
3654 if (dyn.d_tag == DT_AUDIT)
3655 {
3656 unsigned int tagv = dyn.d_un.d_val;
3657 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3658 }
4ad4eba5
AM
3659 }
3660
3661 free (dynbuf);
3662 }
3663
3664 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3665 frees all more recently bfd_alloc'd blocks as well. */
3666 if (runpath)
3667 rpath = runpath;
3668
3669 if (rpath)
3670 {
3671 struct bfd_link_needed_list **pn;
66eb6687 3672 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3673 ;
3674 *pn = rpath;
3675 }
3676
3677 /* We do not want to include any of the sections in a dynamic
3678 object in the output file. We hack by simply clobbering the
3679 list of sections in the BFD. This could be handled more
3680 cleanly by, say, a new section flag; the existing
3681 SEC_NEVER_LOAD flag is not the one we want, because that one
3682 still implies that the section takes up space in the output
3683 file. */
3684 bfd_section_list_clear (abfd);
3685
4ad4eba5
AM
3686 /* Find the name to use in a DT_NEEDED entry that refers to this
3687 object. If the object has a DT_SONAME entry, we use it.
3688 Otherwise, if the generic linker stuck something in
3689 elf_dt_name, we use that. Otherwise, we just use the file
3690 name. */
3691 if (soname == NULL || *soname == '\0')
3692 {
3693 soname = elf_dt_name (abfd);
3694 if (soname == NULL || *soname == '\0')
3695 soname = bfd_get_filename (abfd);
3696 }
3697
3698 /* Save the SONAME because sometimes the linker emulation code
3699 will need to know it. */
3700 elf_dt_name (abfd) = soname;
3701
7e9f0867 3702 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3703 if (ret < 0)
3704 goto error_return;
3705
3706 /* If we have already included this dynamic object in the
3707 link, just ignore it. There is no reason to include a
3708 particular dynamic object more than once. */
3709 if (ret > 0)
3710 return TRUE;
7ee314fa
AM
3711
3712 /* Save the DT_AUDIT entry for the linker emulation code. */
3713 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3714 }
3715
3716 /* If this is a dynamic object, we always link against the .dynsym
3717 symbol table, not the .symtab symbol table. The dynamic linker
3718 will only see the .dynsym symbol table, so there is no reason to
3719 look at .symtab for a dynamic object. */
3720
3721 if (! dynamic || elf_dynsymtab (abfd) == 0)
3722 hdr = &elf_tdata (abfd)->symtab_hdr;
3723 else
3724 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3725
3726 symcount = hdr->sh_size / bed->s->sizeof_sym;
3727
3728 /* The sh_info field of the symtab header tells us where the
3729 external symbols start. We don't care about the local symbols at
3730 this point. */
3731 if (elf_bad_symtab (abfd))
3732 {
3733 extsymcount = symcount;
3734 extsymoff = 0;
3735 }
3736 else
3737 {
3738 extsymcount = symcount - hdr->sh_info;
3739 extsymoff = hdr->sh_info;
3740 }
3741
3742 sym_hash = NULL;
3743 if (extsymcount != 0)
3744 {
3745 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3746 NULL, NULL, NULL);
3747 if (isymbuf == NULL)
3748 goto error_return;
3749
3750 /* We store a pointer to the hash table entry for each external
3751 symbol. */
3752 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 3753 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
4ad4eba5
AM
3754 if (sym_hash == NULL)
3755 goto error_free_sym;
3756 elf_sym_hashes (abfd) = sym_hash;
3757 }
3758
3759 if (dynamic)
3760 {
3761 /* Read in any version definitions. */
fc0e6df6
PB
3762 if (!_bfd_elf_slurp_version_tables (abfd,
3763 info->default_imported_symver))
4ad4eba5
AM
3764 goto error_free_sym;
3765
3766 /* Read in the symbol versions, but don't bother to convert them
3767 to internal format. */
3768 if (elf_dynversym (abfd) != 0)
3769 {
3770 Elf_Internal_Shdr *versymhdr;
3771
3772 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 3773 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
3774 if (extversym == NULL)
3775 goto error_free_sym;
3776 amt = versymhdr->sh_size;
3777 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3778 || bfd_bread (extversym, amt, abfd) != amt)
3779 goto error_free_vers;
3780 }
3781 }
3782
66eb6687
AM
3783 /* If we are loading an as-needed shared lib, save the symbol table
3784 state before we start adding symbols. If the lib turns out
3785 to be unneeded, restore the state. */
3786 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3787 {
3788 unsigned int i;
3789 size_t entsize;
3790
3791 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3792 {
3793 struct bfd_hash_entry *p;
2de92251 3794 struct elf_link_hash_entry *h;
66eb6687
AM
3795
3796 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
3797 {
3798 h = (struct elf_link_hash_entry *) p;
3799 entsize += htab->root.table.entsize;
3800 if (h->root.type == bfd_link_hash_warning)
3801 entsize += htab->root.table.entsize;
3802 }
66eb6687
AM
3803 }
3804
3805 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3806 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3807 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3808 if (old_tab == NULL)
3809 goto error_free_vers;
3810
3811 /* Remember the current objalloc pointer, so that all mem for
3812 symbols added can later be reclaimed. */
3813 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3814 if (alloc_mark == NULL)
3815 goto error_free_vers;
3816
5061a885
AM
3817 /* Make a special call to the linker "notice" function to
3818 tell it that we are about to handle an as-needed lib. */
3819 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
16d96b5b 3820 notice_as_needed, 0, NULL))
9af2a943 3821 goto error_free_vers;
5061a885 3822
66eb6687
AM
3823 /* Clone the symbol table and sym hashes. Remember some
3824 pointers into the symbol table, and dynamic symbol count. */
3825 old_hash = (char *) old_tab + tabsize;
3826 old_ent = (char *) old_hash + hashsize;
3827 memcpy (old_tab, htab->root.table.table, tabsize);
3828 memcpy (old_hash, sym_hash, hashsize);
3829 old_undefs = htab->root.undefs;
3830 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
3831 old_table = htab->root.table.table;
3832 old_size = htab->root.table.size;
3833 old_count = htab->root.table.count;
66eb6687
AM
3834 old_dynsymcount = htab->dynsymcount;
3835
3836 for (i = 0; i < htab->root.table.size; i++)
3837 {
3838 struct bfd_hash_entry *p;
2de92251 3839 struct elf_link_hash_entry *h;
66eb6687
AM
3840
3841 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3842 {
3843 memcpy (old_ent, p, htab->root.table.entsize);
3844 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
3845 h = (struct elf_link_hash_entry *) p;
3846 if (h->root.type == bfd_link_hash_warning)
3847 {
3848 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3849 old_ent = (char *) old_ent + htab->root.table.entsize;
3850 }
66eb6687
AM
3851 }
3852 }
3853 }
4ad4eba5 3854
66eb6687 3855 weaks = NULL;
4ad4eba5
AM
3856 ever = extversym != NULL ? extversym + extsymoff : NULL;
3857 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3858 isym < isymend;
3859 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3860 {
3861 int bind;
3862 bfd_vma value;
af44c138 3863 asection *sec, *new_sec;
4ad4eba5
AM
3864 flagword flags;
3865 const char *name;
3866 struct elf_link_hash_entry *h;
3867 bfd_boolean definition;
3868 bfd_boolean size_change_ok;
3869 bfd_boolean type_change_ok;
3870 bfd_boolean new_weakdef;
3871 bfd_boolean override;
a4d8e49b 3872 bfd_boolean common;
4ad4eba5
AM
3873 unsigned int old_alignment;
3874 bfd *old_bfd;
3cbc5de0 3875 bfd * undef_bfd = NULL;
4ad4eba5
AM
3876
3877 override = FALSE;
3878
3879 flags = BSF_NO_FLAGS;
3880 sec = NULL;
3881 value = isym->st_value;
3882 *sym_hash = NULL;
a4d8e49b 3883 common = bed->common_definition (isym);
4ad4eba5
AM
3884
3885 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 3886 switch (bind)
4ad4eba5 3887 {
3e7a7d11 3888 case STB_LOCAL:
4ad4eba5
AM
3889 /* This should be impossible, since ELF requires that all
3890 global symbols follow all local symbols, and that sh_info
3891 point to the first global symbol. Unfortunately, Irix 5
3892 screws this up. */
3893 continue;
3e7a7d11
NC
3894
3895 case STB_GLOBAL:
a4d8e49b 3896 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 3897 flags = BSF_GLOBAL;
3e7a7d11
NC
3898 break;
3899
3900 case STB_WEAK:
3901 flags = BSF_WEAK;
3902 break;
3903
3904 case STB_GNU_UNIQUE:
3905 flags = BSF_GNU_UNIQUE;
3906 break;
3907
3908 default:
4ad4eba5 3909 /* Leave it up to the processor backend. */
3e7a7d11 3910 break;
4ad4eba5
AM
3911 }
3912
3913 if (isym->st_shndx == SHN_UNDEF)
3914 sec = bfd_und_section_ptr;
cb33740c
AM
3915 else if (isym->st_shndx == SHN_ABS)
3916 sec = bfd_abs_section_ptr;
3917 else if (isym->st_shndx == SHN_COMMON)
3918 {
3919 sec = bfd_com_section_ptr;
3920 /* What ELF calls the size we call the value. What ELF
3921 calls the value we call the alignment. */
3922 value = isym->st_size;
3923 }
3924 else
4ad4eba5
AM
3925 {
3926 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3927 if (sec == NULL)
3928 sec = bfd_abs_section_ptr;
529fcb95
PB
3929 else if (sec->kept_section)
3930 {
e5d08002
L
3931 /* Symbols from discarded section are undefined. We keep
3932 its visibility. */
529fcb95
PB
3933 sec = bfd_und_section_ptr;
3934 isym->st_shndx = SHN_UNDEF;
3935 }
4ad4eba5
AM
3936 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3937 value -= sec->vma;
3938 }
4ad4eba5
AM
3939
3940 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3941 isym->st_name);
3942 if (name == NULL)
3943 goto error_free_vers;
3944
3945 if (isym->st_shndx == SHN_COMMON
02d00247
AM
3946 && (abfd->flags & BFD_PLUGIN) != 0)
3947 {
3948 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
3949
3950 if (xc == NULL)
3951 {
3952 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
3953 | SEC_EXCLUDE);
3954 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
3955 if (xc == NULL)
3956 goto error_free_vers;
3957 }
3958 sec = xc;
3959 }
3960 else if (isym->st_shndx == SHN_COMMON
3961 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3962 && !info->relocatable)
4ad4eba5
AM
3963 {
3964 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3965
3966 if (tcomm == NULL)
3967 {
02d00247
AM
3968 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
3969 | SEC_LINKER_CREATED);
3970 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 3971 if (tcomm == NULL)
4ad4eba5
AM
3972 goto error_free_vers;
3973 }
3974 sec = tcomm;
3975 }
66eb6687 3976 else if (bed->elf_add_symbol_hook)
4ad4eba5 3977 {
66eb6687
AM
3978 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3979 &sec, &value))
4ad4eba5
AM
3980 goto error_free_vers;
3981
3982 /* The hook function sets the name to NULL if this symbol
3983 should be skipped for some reason. */
3984 if (name == NULL)
3985 continue;
3986 }
3987
3988 /* Sanity check that all possibilities were handled. */
3989 if (sec == NULL)
3990 {
3991 bfd_set_error (bfd_error_bad_value);
3992 goto error_free_vers;
3993 }
3994
3995 if (bfd_is_und_section (sec)
3996 || bfd_is_com_section (sec))
3997 definition = FALSE;
3998 else
3999 definition = TRUE;
4000
4001 size_change_ok = FALSE;
66eb6687 4002 type_change_ok = bed->type_change_ok;
4ad4eba5
AM
4003 old_alignment = 0;
4004 old_bfd = NULL;
af44c138 4005 new_sec = sec;
4ad4eba5 4006
66eb6687 4007 if (is_elf_hash_table (htab))
4ad4eba5
AM
4008 {
4009 Elf_Internal_Versym iver;
4010 unsigned int vernum = 0;
4011 bfd_boolean skip;
4012
b918acf9
NC
4013 /* If this is a definition of a symbol which was previously
4014 referenced in a non-weak manner then make a note of the bfd
4015 that contained the reference. This is used if we need to
4016 refer to the source of the reference later on. */
4017 if (! bfd_is_und_section (sec))
4018 {
4019 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4020
4021 if (h != NULL
4022 && h->root.type == bfd_link_hash_undefined
4023 && h->root.u.undef.abfd)
4024 undef_bfd = h->root.u.undef.abfd;
4025 }
4026
fc0e6df6 4027 if (ever == NULL)
4ad4eba5 4028 {
fc0e6df6
PB
4029 if (info->default_imported_symver)
4030 /* Use the default symbol version created earlier. */
4031 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4032 else
4033 iver.vs_vers = 0;
4034 }
4035 else
4036 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4037
4038 vernum = iver.vs_vers & VERSYM_VERSION;
4039
4040 /* If this is a hidden symbol, or if it is not version
4041 1, we append the version name to the symbol name.
cc86ff91
EB
4042 However, we do not modify a non-hidden absolute symbol
4043 if it is not a function, because it might be the version
4044 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4045 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4046 || (vernum > 1
4047 && (!bfd_is_abs_section (sec)
4048 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4049 {
4050 const char *verstr;
4051 size_t namelen, verlen, newlen;
4052 char *newname, *p;
4053
4054 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4055 {
fc0e6df6
PB
4056 if (vernum > elf_tdata (abfd)->cverdefs)
4057 verstr = NULL;
4058 else if (vernum > 1)
4059 verstr =
4060 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4061 else
4062 verstr = "";
4ad4eba5 4063
fc0e6df6 4064 if (verstr == NULL)
4ad4eba5 4065 {
fc0e6df6
PB
4066 (*_bfd_error_handler)
4067 (_("%B: %s: invalid version %u (max %d)"),
4068 abfd, name, vernum,
4069 elf_tdata (abfd)->cverdefs);
4070 bfd_set_error (bfd_error_bad_value);
4071 goto error_free_vers;
4ad4eba5 4072 }
fc0e6df6
PB
4073 }
4074 else
4075 {
4076 /* We cannot simply test for the number of
4077 entries in the VERNEED section since the
4078 numbers for the needed versions do not start
4079 at 0. */
4080 Elf_Internal_Verneed *t;
4081
4082 verstr = NULL;
4083 for (t = elf_tdata (abfd)->verref;
4084 t != NULL;
4085 t = t->vn_nextref)
4ad4eba5 4086 {
fc0e6df6 4087 Elf_Internal_Vernaux *a;
4ad4eba5 4088
fc0e6df6
PB
4089 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4090 {
4091 if (a->vna_other == vernum)
4ad4eba5 4092 {
fc0e6df6
PB
4093 verstr = a->vna_nodename;
4094 break;
4ad4eba5 4095 }
4ad4eba5 4096 }
fc0e6df6
PB
4097 if (a != NULL)
4098 break;
4099 }
4100 if (verstr == NULL)
4101 {
4102 (*_bfd_error_handler)
4103 (_("%B: %s: invalid needed version %d"),
4104 abfd, name, vernum);
4105 bfd_set_error (bfd_error_bad_value);
4106 goto error_free_vers;
4ad4eba5 4107 }
4ad4eba5 4108 }
fc0e6df6
PB
4109
4110 namelen = strlen (name);
4111 verlen = strlen (verstr);
4112 newlen = namelen + verlen + 2;
4113 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4114 && isym->st_shndx != SHN_UNDEF)
4115 ++newlen;
4116
a50b1753 4117 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4118 if (newname == NULL)
4119 goto error_free_vers;
4120 memcpy (newname, name, namelen);
4121 p = newname + namelen;
4122 *p++ = ELF_VER_CHR;
4123 /* If this is a defined non-hidden version symbol,
4124 we add another @ to the name. This indicates the
4125 default version of the symbol. */
4126 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4127 && isym->st_shndx != SHN_UNDEF)
4128 *p++ = ELF_VER_CHR;
4129 memcpy (p, verstr, verlen + 1);
4130
4131 name = newname;
4ad4eba5
AM
4132 }
4133
b918acf9
NC
4134 /* If necessary, make a second attempt to locate the bfd
4135 containing an unresolved, non-weak reference to the
4136 current symbol. */
4137 if (! bfd_is_und_section (sec) && undef_bfd == NULL)
3cbc5de0
NC
4138 {
4139 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4140
4141 if (h != NULL
b918acf9 4142 && h->root.type == bfd_link_hash_undefined
3cbc5de0
NC
4143 && h->root.u.undef.abfd)
4144 undef_bfd = h->root.u.undef.abfd;
4145 }
4146
af44c138
L
4147 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4148 &value, &old_alignment,
4ad4eba5
AM
4149 sym_hash, &skip, &override,
4150 &type_change_ok, &size_change_ok))
4151 goto error_free_vers;
4152
4153 if (skip)
4154 continue;
4155
4156 if (override)
4157 definition = FALSE;
4158
4159 h = *sym_hash;
4160 while (h->root.type == bfd_link_hash_indirect
4161 || h->root.type == bfd_link_hash_warning)
4162 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4163
4164 /* Remember the old alignment if this is a common symbol, so
4165 that we don't reduce the alignment later on. We can't
4166 check later, because _bfd_generic_link_add_one_symbol
4167 will set a default for the alignment which we want to
4168 override. We also remember the old bfd where the existing
4169 definition comes from. */
4170 switch (h->root.type)
4171 {
4172 default:
4173 break;
4174
4175 case bfd_link_hash_defined:
4176 case bfd_link_hash_defweak:
4177 old_bfd = h->root.u.def.section->owner;
4178 break;
4179
4180 case bfd_link_hash_common:
4181 old_bfd = h->root.u.c.p->section->owner;
4182 old_alignment = h->root.u.c.p->alignment_power;
4183 break;
4184 }
4185
4186 if (elf_tdata (abfd)->verdef != NULL
4187 && ! override
4188 && vernum > 1
4189 && definition)
4190 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4191 }
4192
4193 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4194 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4195 (struct bfd_link_hash_entry **) sym_hash)))
4196 goto error_free_vers;
4197
4198 h = *sym_hash;
4199 while (h->root.type == bfd_link_hash_indirect
4200 || h->root.type == bfd_link_hash_warning)
4201 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4202
4ad4eba5 4203 *sym_hash = h;
d64284fe
L
4204 if (is_elf_hash_table (htab))
4205 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4ad4eba5
AM
4206
4207 new_weakdef = FALSE;
4208 if (dynamic
4209 && definition
4210 && (flags & BSF_WEAK) != 0
fcb93ecf 4211 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4212 && is_elf_hash_table (htab)
f6e332e6 4213 && h->u.weakdef == NULL)
4ad4eba5
AM
4214 {
4215 /* Keep a list of all weak defined non function symbols from
4216 a dynamic object, using the weakdef field. Later in this
4217 function we will set the weakdef field to the correct
4218 value. We only put non-function symbols from dynamic
4219 objects on this list, because that happens to be the only
4220 time we need to know the normal symbol corresponding to a
4221 weak symbol, and the information is time consuming to
4222 figure out. If the weakdef field is not already NULL,
4223 then this symbol was already defined by some previous
4224 dynamic object, and we will be using that previous
4225 definition anyhow. */
4226
f6e332e6 4227 h->u.weakdef = weaks;
4ad4eba5
AM
4228 weaks = h;
4229 new_weakdef = TRUE;
4230 }
4231
4232 /* Set the alignment of a common symbol. */
a4d8e49b 4233 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4234 && h->root.type == bfd_link_hash_common)
4235 {
4236 unsigned int align;
4237
a4d8e49b 4238 if (common)
af44c138
L
4239 align = bfd_log2 (isym->st_value);
4240 else
4241 {
4242 /* The new symbol is a common symbol in a shared object.
4243 We need to get the alignment from the section. */
4244 align = new_sec->alignment_power;
4245 }
595213d4 4246 if (align > old_alignment)
4ad4eba5
AM
4247 h->root.u.c.p->alignment_power = align;
4248 else
4249 h->root.u.c.p->alignment_power = old_alignment;
4250 }
4251
66eb6687 4252 if (is_elf_hash_table (htab))
4ad4eba5 4253 {
4ad4eba5 4254 bfd_boolean dynsym;
4ad4eba5
AM
4255
4256 /* Check the alignment when a common symbol is involved. This
4257 can change when a common symbol is overridden by a normal
4258 definition or a common symbol is ignored due to the old
4259 normal definition. We need to make sure the maximum
4260 alignment is maintained. */
a4d8e49b 4261 if ((old_alignment || common)
4ad4eba5
AM
4262 && h->root.type != bfd_link_hash_common)
4263 {
4264 unsigned int common_align;
4265 unsigned int normal_align;
4266 unsigned int symbol_align;
4267 bfd *normal_bfd;
4268 bfd *common_bfd;
4269
4270 symbol_align = ffs (h->root.u.def.value) - 1;
4271 if (h->root.u.def.section->owner != NULL
4272 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4273 {
4274 normal_align = h->root.u.def.section->alignment_power;
4275 if (normal_align > symbol_align)
4276 normal_align = symbol_align;
4277 }
4278 else
4279 normal_align = symbol_align;
4280
4281 if (old_alignment)
4282 {
4283 common_align = old_alignment;
4284 common_bfd = old_bfd;
4285 normal_bfd = abfd;
4286 }
4287 else
4288 {
4289 common_align = bfd_log2 (isym->st_value);
4290 common_bfd = abfd;
4291 normal_bfd = old_bfd;
4292 }
4293
4294 if (normal_align < common_align)
d07676f8
NC
4295 {
4296 /* PR binutils/2735 */
4297 if (normal_bfd == NULL)
4298 (*_bfd_error_handler)
4299 (_("Warning: alignment %u of common symbol `%s' in %B"
4300 " is greater than the alignment (%u) of its section %A"),
4301 common_bfd, h->root.u.def.section,
4302 1 << common_align, name, 1 << normal_align);
4303 else
4304 (*_bfd_error_handler)
4305 (_("Warning: alignment %u of symbol `%s' in %B"
4306 " is smaller than %u in %B"),
4307 normal_bfd, common_bfd,
4308 1 << normal_align, name, 1 << common_align);
4309 }
4ad4eba5
AM
4310 }
4311
83ad0046
L
4312 /* Remember the symbol size if it isn't undefined. */
4313 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4ad4eba5
AM
4314 && (definition || h->size == 0))
4315 {
83ad0046
L
4316 if (h->size != 0
4317 && h->size != isym->st_size
4318 && ! size_change_ok)
4ad4eba5 4319 (*_bfd_error_handler)
d003868e
AM
4320 (_("Warning: size of symbol `%s' changed"
4321 " from %lu in %B to %lu in %B"),
4322 old_bfd, abfd,
4ad4eba5 4323 name, (unsigned long) h->size,
d003868e 4324 (unsigned long) isym->st_size);
4ad4eba5
AM
4325
4326 h->size = isym->st_size;
4327 }
4328
4329 /* If this is a common symbol, then we always want H->SIZE
4330 to be the size of the common symbol. The code just above
4331 won't fix the size if a common symbol becomes larger. We
4332 don't warn about a size change here, because that is
fcb93ecf
PB
4333 covered by --warn-common. Allow changed between different
4334 function types. */
4ad4eba5
AM
4335 if (h->root.type == bfd_link_hash_common)
4336 h->size = h->root.u.c.size;
4337
4338 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4339 && (definition || h->type == STT_NOTYPE))
4340 {
2955ec4c
L
4341 unsigned int type = ELF_ST_TYPE (isym->st_info);
4342
4343 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4344 symbol. */
4345 if (type == STT_GNU_IFUNC
4346 && (abfd->flags & DYNAMIC) != 0)
4347 type = STT_FUNC;
4ad4eba5 4348
2955ec4c
L
4349 if (h->type != type)
4350 {
4351 if (h->type != STT_NOTYPE && ! type_change_ok)
4352 (*_bfd_error_handler)
4353 (_("Warning: type of symbol `%s' changed"
4354 " from %d to %d in %B"),
4355 abfd, name, h->type, type);
4356
4357 h->type = type;
4358 }
4ad4eba5
AM
4359 }
4360
54ac0771
L
4361 /* Merge st_other field. */
4362 elf_merge_st_other (abfd, h, isym, definition, dynamic);
4ad4eba5
AM
4363
4364 /* Set a flag in the hash table entry indicating the type of
4365 reference or definition we just found. Keep a count of
4366 the number of dynamic symbols we find. A dynamic symbol
4367 is one which is referenced or defined by both a regular
4368 object and a shared object. */
4ad4eba5
AM
4369 dynsym = FALSE;
4370 if (! dynamic)
4371 {
4372 if (! definition)
4373 {
f5385ebf 4374 h->ref_regular = 1;
4ad4eba5 4375 if (bind != STB_WEAK)
f5385ebf 4376 h->ref_regular_nonweak = 1;
4ad4eba5
AM
4377 }
4378 else
d8880531
L
4379 {
4380 h->def_regular = 1;
4381 if (h->def_dynamic)
4382 {
4383 h->def_dynamic = 0;
4384 h->ref_dynamic = 1;
4385 h->dynamic_def = 1;
4386 }
4387 }
4ad4eba5 4388 if (! info->executable
f5385ebf
AM
4389 || h->def_dynamic
4390 || h->ref_dynamic)
4ad4eba5
AM
4391 dynsym = TRUE;
4392 }
4393 else
4394 {
4395 if (! definition)
f5385ebf 4396 h->ref_dynamic = 1;
4ad4eba5 4397 else
f5385ebf
AM
4398 h->def_dynamic = 1;
4399 if (h->def_regular
4400 || h->ref_regular
f6e332e6 4401 || (h->u.weakdef != NULL
4ad4eba5 4402 && ! new_weakdef
f6e332e6 4403 && h->u.weakdef->dynindx != -1))
4ad4eba5
AM
4404 dynsym = TRUE;
4405 }
4406
b2064611 4407 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
92b7c7b6
L
4408 {
4409 /* We don't want to make debug symbol dynamic. */
92b7c7b6
L
4410 dynsym = FALSE;
4411 }
4412
35fc36a8
RS
4413 if (definition)
4414 h->target_internal = isym->st_target_internal;
4415
4ad4eba5
AM
4416 /* Check to see if we need to add an indirect symbol for
4417 the default name. */
4418 if (definition || h->root.type == bfd_link_hash_common)
4419 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4420 &sec, &value, &dynsym,
4421 override))
4422 goto error_free_vers;
4423
4424 if (definition && !dynamic)
4425 {
4426 char *p = strchr (name, ELF_VER_CHR);
4427 if (p != NULL && p[1] != ELF_VER_CHR)
4428 {
4429 /* Queue non-default versions so that .symver x, x@FOO
4430 aliases can be checked. */
66eb6687 4431 if (!nondeflt_vers)
4ad4eba5 4432 {
66eb6687
AM
4433 amt = ((isymend - isym + 1)
4434 * sizeof (struct elf_link_hash_entry *));
a50b1753
NC
4435 nondeflt_vers =
4436 (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4437 if (!nondeflt_vers)
4438 goto error_free_vers;
4ad4eba5 4439 }
66eb6687 4440 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4441 }
4442 }
4443
4444 if (dynsym && h->dynindx == -1)
4445 {
c152c796 4446 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4447 goto error_free_vers;
f6e332e6 4448 if (h->u.weakdef != NULL
4ad4eba5 4449 && ! new_weakdef
f6e332e6 4450 && h->u.weakdef->dynindx == -1)
4ad4eba5 4451 {
66eb6687 4452 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4453 goto error_free_vers;
4454 }
4455 }
4456 else if (dynsym && h->dynindx != -1)
4457 /* If the symbol already has a dynamic index, but
4458 visibility says it should not be visible, turn it into
4459 a local symbol. */
4460 switch (ELF_ST_VISIBILITY (h->other))
4461 {
4462 case STV_INTERNAL:
4463 case STV_HIDDEN:
4464 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4465 dynsym = FALSE;
4466 break;
4467 }
4468
4469 if (!add_needed
4470 && definition
010e5ae2
AM
4471 && ((dynsym
4472 && h->ref_regular)
4473 || (h->ref_dynamic
4474 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4475 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4ad4eba5
AM
4476 {
4477 int ret;
4478 const char *soname = elf_dt_name (abfd);
4479
4480 /* A symbol from a library loaded via DT_NEEDED of some
4481 other library is referenced by a regular object.
e56f61be 4482 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4483 --no-add-needed is used and the reference was not
4484 a weak one. */
4485 if (undef_bfd != NULL
4486 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4487 {
4488 (*_bfd_error_handler)
3cbc5de0 4489 (_("%B: undefined reference to symbol '%s'"),
b918acf9 4490 undef_bfd, name);
3cbc5de0
NC
4491 (*_bfd_error_handler)
4492 (_("note: '%s' is defined in DSO %B so try adding it to the linker command line"),
d003868e 4493 abfd, name);
3cbc5de0 4494 bfd_set_error (bfd_error_invalid_operation);
e56f61be
L
4495 goto error_free_vers;
4496 }
4497
a50b1753
NC
4498 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4499 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4500
4ad4eba5 4501 add_needed = TRUE;
7e9f0867 4502 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4503 if (ret < 0)
4504 goto error_free_vers;
4505
4506 BFD_ASSERT (ret == 0);
4507 }
4508 }
4509 }
4510
66eb6687
AM
4511 if (extversym != NULL)
4512 {
4513 free (extversym);
4514 extversym = NULL;
4515 }
4516
4517 if (isymbuf != NULL)
4518 {
4519 free (isymbuf);
4520 isymbuf = NULL;
4521 }
4522
4523 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4524 {
4525 unsigned int i;
4526
4527 /* Restore the symbol table. */
97fed1c9
JJ
4528 if (bed->as_needed_cleanup)
4529 (*bed->as_needed_cleanup) (abfd, info);
66eb6687
AM
4530 old_hash = (char *) old_tab + tabsize;
4531 old_ent = (char *) old_hash + hashsize;
4532 sym_hash = elf_sym_hashes (abfd);
4f87808c
AM
4533 htab->root.table.table = old_table;
4534 htab->root.table.size = old_size;
4535 htab->root.table.count = old_count;
66eb6687
AM
4536 memcpy (htab->root.table.table, old_tab, tabsize);
4537 memcpy (sym_hash, old_hash, hashsize);
4538 htab->root.undefs = old_undefs;
4539 htab->root.undefs_tail = old_undefs_tail;
4540 for (i = 0; i < htab->root.table.size; i++)
4541 {
4542 struct bfd_hash_entry *p;
4543 struct elf_link_hash_entry *h;
4544
4545 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4546 {
4547 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4548 if (h->root.type == bfd_link_hash_warning)
4549 h = (struct elf_link_hash_entry *) h->root.u.i.link;
66eb6687
AM
4550 if (h->dynindx >= old_dynsymcount)
4551 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
2de92251 4552
66eb6687
AM
4553 memcpy (p, old_ent, htab->root.table.entsize);
4554 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4555 h = (struct elf_link_hash_entry *) p;
4556 if (h->root.type == bfd_link_hash_warning)
4557 {
4558 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4559 old_ent = (char *) old_ent + htab->root.table.entsize;
4560 }
66eb6687
AM
4561 }
4562 }
4563
5061a885
AM
4564 /* Make a special call to the linker "notice" function to
4565 tell it that symbols added for crefs may need to be removed. */
4566 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
16d96b5b 4567 notice_not_needed, 0, NULL))
9af2a943 4568 goto error_free_vers;
5061a885 4569
66eb6687
AM
4570 free (old_tab);
4571 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4572 alloc_mark);
4573 if (nondeflt_vers != NULL)
4574 free (nondeflt_vers);
4575 return TRUE;
4576 }
2de92251 4577
66eb6687
AM
4578 if (old_tab != NULL)
4579 {
5061a885 4580 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
16d96b5b 4581 notice_needed, 0, NULL))
9af2a943 4582 goto error_free_vers;
66eb6687
AM
4583 free (old_tab);
4584 old_tab = NULL;
4585 }
4586
4ad4eba5
AM
4587 /* Now that all the symbols from this input file are created, handle
4588 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4589 if (nondeflt_vers != NULL)
4590 {
4591 bfd_size_type cnt, symidx;
4592
4593 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4594 {
4595 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4596 char *shortname, *p;
4597
4598 p = strchr (h->root.root.string, ELF_VER_CHR);
4599 if (p == NULL
4600 || (h->root.type != bfd_link_hash_defined
4601 && h->root.type != bfd_link_hash_defweak))
4602 continue;
4603
4604 amt = p - h->root.root.string;
a50b1753 4605 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4606 if (!shortname)
4607 goto error_free_vers;
4ad4eba5
AM
4608 memcpy (shortname, h->root.root.string, amt);
4609 shortname[amt] = '\0';
4610
4611 hi = (struct elf_link_hash_entry *)
66eb6687 4612 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4613 FALSE, FALSE, FALSE);
4614 if (hi != NULL
4615 && hi->root.type == h->root.type
4616 && hi->root.u.def.value == h->root.u.def.value
4617 && hi->root.u.def.section == h->root.u.def.section)
4618 {
4619 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4620 hi->root.type = bfd_link_hash_indirect;
4621 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4622 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4623 sym_hash = elf_sym_hashes (abfd);
4624 if (sym_hash)
4625 for (symidx = 0; symidx < extsymcount; ++symidx)
4626 if (sym_hash[symidx] == hi)
4627 {
4628 sym_hash[symidx] = h;
4629 break;
4630 }
4631 }
4632 free (shortname);
4633 }
4634 free (nondeflt_vers);
4635 nondeflt_vers = NULL;
4636 }
4637
4ad4eba5
AM
4638 /* Now set the weakdefs field correctly for all the weak defined
4639 symbols we found. The only way to do this is to search all the
4640 symbols. Since we only need the information for non functions in
4641 dynamic objects, that's the only time we actually put anything on
4642 the list WEAKS. We need this information so that if a regular
4643 object refers to a symbol defined weakly in a dynamic object, the
4644 real symbol in the dynamic object is also put in the dynamic
4645 symbols; we also must arrange for both symbols to point to the
4646 same memory location. We could handle the general case of symbol
4647 aliasing, but a general symbol alias can only be generated in
4648 assembler code, handling it correctly would be very time
4649 consuming, and other ELF linkers don't handle general aliasing
4650 either. */
4651 if (weaks != NULL)
4652 {
4653 struct elf_link_hash_entry **hpp;
4654 struct elf_link_hash_entry **hppend;
4655 struct elf_link_hash_entry **sorted_sym_hash;
4656 struct elf_link_hash_entry *h;
4657 size_t sym_count;
4658
4659 /* Since we have to search the whole symbol list for each weak
4660 defined symbol, search time for N weak defined symbols will be
4661 O(N^2). Binary search will cut it down to O(NlogN). */
4662 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 4663 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4664 if (sorted_sym_hash == NULL)
4665 goto error_return;
4666 sym_hash = sorted_sym_hash;
4667 hpp = elf_sym_hashes (abfd);
4668 hppend = hpp + extsymcount;
4669 sym_count = 0;
4670 for (; hpp < hppend; hpp++)
4671 {
4672 h = *hpp;
4673 if (h != NULL
4674 && h->root.type == bfd_link_hash_defined
fcb93ecf 4675 && !bed->is_function_type (h->type))
4ad4eba5
AM
4676 {
4677 *sym_hash = h;
4678 sym_hash++;
4679 sym_count++;
4680 }
4681 }
4682
4683 qsort (sorted_sym_hash, sym_count,
4684 sizeof (struct elf_link_hash_entry *),
4685 elf_sort_symbol);
4686
4687 while (weaks != NULL)
4688 {
4689 struct elf_link_hash_entry *hlook;
4690 asection *slook;
4691 bfd_vma vlook;
4692 long ilook;
4693 size_t i, j, idx;
4694
4695 hlook = weaks;
f6e332e6
AM
4696 weaks = hlook->u.weakdef;
4697 hlook->u.weakdef = NULL;
4ad4eba5
AM
4698
4699 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4700 || hlook->root.type == bfd_link_hash_defweak
4701 || hlook->root.type == bfd_link_hash_common
4702 || hlook->root.type == bfd_link_hash_indirect);
4703 slook = hlook->root.u.def.section;
4704 vlook = hlook->root.u.def.value;
4705
4706 ilook = -1;
4707 i = 0;
4708 j = sym_count;
4709 while (i < j)
4710 {
4711 bfd_signed_vma vdiff;
4712 idx = (i + j) / 2;
4713 h = sorted_sym_hash [idx];
4714 vdiff = vlook - h->root.u.def.value;
4715 if (vdiff < 0)
4716 j = idx;
4717 else if (vdiff > 0)
4718 i = idx + 1;
4719 else
4720 {
a9b881be 4721 long sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4722 if (sdiff < 0)
4723 j = idx;
4724 else if (sdiff > 0)
4725 i = idx + 1;
4726 else
4727 {
4728 ilook = idx;
4729 break;
4730 }
4731 }
4732 }
4733
4734 /* We didn't find a value/section match. */
4735 if (ilook == -1)
4736 continue;
4737
4738 for (i = ilook; i < sym_count; i++)
4739 {
4740 h = sorted_sym_hash [i];
4741
4742 /* Stop if value or section doesn't match. */
4743 if (h->root.u.def.value != vlook
4744 || h->root.u.def.section != slook)
4745 break;
4746 else if (h != hlook)
4747 {
f6e332e6 4748 hlook->u.weakdef = h;
4ad4eba5
AM
4749
4750 /* If the weak definition is in the list of dynamic
4751 symbols, make sure the real definition is put
4752 there as well. */
4753 if (hlook->dynindx != -1 && h->dynindx == -1)
4754 {
c152c796 4755 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
4756 {
4757 err_free_sym_hash:
4758 free (sorted_sym_hash);
4759 goto error_return;
4760 }
4ad4eba5
AM
4761 }
4762
4763 /* If the real definition is in the list of dynamic
4764 symbols, make sure the weak definition is put
4765 there as well. If we don't do this, then the
4766 dynamic loader might not merge the entries for the
4767 real definition and the weak definition. */
4768 if (h->dynindx != -1 && hlook->dynindx == -1)
4769 {
c152c796 4770 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 4771 goto err_free_sym_hash;
4ad4eba5
AM
4772 }
4773 break;
4774 }
4775 }
4776 }
4777
4778 free (sorted_sym_hash);
4779 }
4780
33177bb1
AM
4781 if (bed->check_directives
4782 && !(*bed->check_directives) (abfd, info))
4783 return FALSE;
85fbca6a 4784
4ad4eba5
AM
4785 /* If this object is the same format as the output object, and it is
4786 not a shared library, then let the backend look through the
4787 relocs.
4788
4789 This is required to build global offset table entries and to
4790 arrange for dynamic relocs. It is not required for the
4791 particular common case of linking non PIC code, even when linking
4792 against shared libraries, but unfortunately there is no way of
4793 knowing whether an object file has been compiled PIC or not.
4794 Looking through the relocs is not particularly time consuming.
4795 The problem is that we must either (1) keep the relocs in memory,
4796 which causes the linker to require additional runtime memory or
4797 (2) read the relocs twice from the input file, which wastes time.
4798 This would be a good case for using mmap.
4799
4800 I have no idea how to handle linking PIC code into a file of a
4801 different format. It probably can't be done. */
4ad4eba5 4802 if (! dynamic
66eb6687 4803 && is_elf_hash_table (htab)
13285a1b 4804 && bed->check_relocs != NULL
39334f3a 4805 && elf_object_id (abfd) == elf_hash_table_id (htab)
f13a99db 4806 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4ad4eba5
AM
4807 {
4808 asection *o;
4809
4810 for (o = abfd->sections; o != NULL; o = o->next)
4811 {
4812 Elf_Internal_Rela *internal_relocs;
4813 bfd_boolean ok;
4814
4815 if ((o->flags & SEC_RELOC) == 0
4816 || o->reloc_count == 0
4817 || ((info->strip == strip_all || info->strip == strip_debugger)
4818 && (o->flags & SEC_DEBUGGING) != 0)
4819 || bfd_is_abs_section (o->output_section))
4820 continue;
4821
4822 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4823 info->keep_memory);
4824 if (internal_relocs == NULL)
4825 goto error_return;
4826
66eb6687 4827 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4ad4eba5
AM
4828
4829 if (elf_section_data (o)->relocs != internal_relocs)
4830 free (internal_relocs);
4831
4832 if (! ok)
4833 goto error_return;
4834 }
4835 }
4836
4837 /* If this is a non-traditional link, try to optimize the handling
4838 of the .stab/.stabstr sections. */
4839 if (! dynamic
4840 && ! info->traditional_format
66eb6687 4841 && is_elf_hash_table (htab)
4ad4eba5
AM
4842 && (info->strip != strip_all && info->strip != strip_debugger))
4843 {
4844 asection *stabstr;
4845
4846 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4847 if (stabstr != NULL)
4848 {
4849 bfd_size_type string_offset = 0;
4850 asection *stab;
4851
4852 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 4853 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
4854 && (!stab->name[5] ||
4855 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4856 && (stab->flags & SEC_MERGE) == 0
4857 && !bfd_is_abs_section (stab->output_section))
4858 {
4859 struct bfd_elf_section_data *secdata;
4860
4861 secdata = elf_section_data (stab);
66eb6687
AM
4862 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4863 stabstr, &secdata->sec_info,
4ad4eba5
AM
4864 &string_offset))
4865 goto error_return;
4866 if (secdata->sec_info)
4867 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4868 }
4869 }
4870 }
4871
66eb6687 4872 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
4873 {
4874 /* Add this bfd to the loaded list. */
4875 struct elf_link_loaded_list *n;
4876
a50b1753
NC
4877 n = (struct elf_link_loaded_list *)
4878 bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4ad4eba5
AM
4879 if (n == NULL)
4880 goto error_return;
4881 n->abfd = abfd;
66eb6687
AM
4882 n->next = htab->loaded;
4883 htab->loaded = n;
4ad4eba5
AM
4884 }
4885
4886 return TRUE;
4887
4888 error_free_vers:
66eb6687
AM
4889 if (old_tab != NULL)
4890 free (old_tab);
4ad4eba5
AM
4891 if (nondeflt_vers != NULL)
4892 free (nondeflt_vers);
4893 if (extversym != NULL)
4894 free (extversym);
4895 error_free_sym:
4896 if (isymbuf != NULL)
4897 free (isymbuf);
4898 error_return:
4899 return FALSE;
4900}
4901
8387904d
AM
4902/* Return the linker hash table entry of a symbol that might be
4903 satisfied by an archive symbol. Return -1 on error. */
4904
4905struct elf_link_hash_entry *
4906_bfd_elf_archive_symbol_lookup (bfd *abfd,
4907 struct bfd_link_info *info,
4908 const char *name)
4909{
4910 struct elf_link_hash_entry *h;
4911 char *p, *copy;
4912 size_t len, first;
4913
2a41f396 4914 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
4915 if (h != NULL)
4916 return h;
4917
4918 /* If this is a default version (the name contains @@), look up the
4919 symbol again with only one `@' as well as without the version.
4920 The effect is that references to the symbol with and without the
4921 version will be matched by the default symbol in the archive. */
4922
4923 p = strchr (name, ELF_VER_CHR);
4924 if (p == NULL || p[1] != ELF_VER_CHR)
4925 return h;
4926
4927 /* First check with only one `@'. */
4928 len = strlen (name);
a50b1753 4929 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
4930 if (copy == NULL)
4931 return (struct elf_link_hash_entry *) 0 - 1;
4932
4933 first = p - name + 1;
4934 memcpy (copy, name, first);
4935 memcpy (copy + first, name + first + 1, len - first);
4936
2a41f396 4937 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
4938 if (h == NULL)
4939 {
4940 /* We also need to check references to the symbol without the
4941 version. */
4942 copy[first - 1] = '\0';
4943 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 4944 FALSE, FALSE, TRUE);
8387904d
AM
4945 }
4946
4947 bfd_release (abfd, copy);
4948 return h;
4949}
4950
0ad989f9
L
4951/* Add symbols from an ELF archive file to the linker hash table. We
4952 don't use _bfd_generic_link_add_archive_symbols because of a
4953 problem which arises on UnixWare. The UnixWare libc.so is an
4954 archive which includes an entry libc.so.1 which defines a bunch of
4955 symbols. The libc.so archive also includes a number of other
4956 object files, which also define symbols, some of which are the same
4957 as those defined in libc.so.1. Correct linking requires that we
4958 consider each object file in turn, and include it if it defines any
4959 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4960 this; it looks through the list of undefined symbols, and includes
4961 any object file which defines them. When this algorithm is used on
4962 UnixWare, it winds up pulling in libc.so.1 early and defining a
4963 bunch of symbols. This means that some of the other objects in the
4964 archive are not included in the link, which is incorrect since they
4965 precede libc.so.1 in the archive.
4966
4967 Fortunately, ELF archive handling is simpler than that done by
4968 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4969 oddities. In ELF, if we find a symbol in the archive map, and the
4970 symbol is currently undefined, we know that we must pull in that
4971 object file.
4972
4973 Unfortunately, we do have to make multiple passes over the symbol
4974 table until nothing further is resolved. */
4975
4ad4eba5
AM
4976static bfd_boolean
4977elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
4978{
4979 symindex c;
4980 bfd_boolean *defined = NULL;
4981 bfd_boolean *included = NULL;
4982 carsym *symdefs;
4983 bfd_boolean loop;
4984 bfd_size_type amt;
8387904d
AM
4985 const struct elf_backend_data *bed;
4986 struct elf_link_hash_entry * (*archive_symbol_lookup)
4987 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
4988
4989 if (! bfd_has_map (abfd))
4990 {
4991 /* An empty archive is a special case. */
4992 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4993 return TRUE;
4994 bfd_set_error (bfd_error_no_armap);
4995 return FALSE;
4996 }
4997
4998 /* Keep track of all symbols we know to be already defined, and all
4999 files we know to be already included. This is to speed up the
5000 second and subsequent passes. */
5001 c = bfd_ardata (abfd)->symdef_count;
5002 if (c == 0)
5003 return TRUE;
5004 amt = c;
5005 amt *= sizeof (bfd_boolean);
a50b1753
NC
5006 defined = (bfd_boolean *) bfd_zmalloc (amt);
5007 included = (bfd_boolean *) bfd_zmalloc (amt);
0ad989f9
L
5008 if (defined == NULL || included == NULL)
5009 goto error_return;
5010
5011 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5012 bed = get_elf_backend_data (abfd);
5013 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5014
5015 do
5016 {
5017 file_ptr last;
5018 symindex i;
5019 carsym *symdef;
5020 carsym *symdefend;
5021
5022 loop = FALSE;
5023 last = -1;
5024
5025 symdef = symdefs;
5026 symdefend = symdef + c;
5027 for (i = 0; symdef < symdefend; symdef++, i++)
5028 {
5029 struct elf_link_hash_entry *h;
5030 bfd *element;
5031 struct bfd_link_hash_entry *undefs_tail;
5032 symindex mark;
5033
5034 if (defined[i] || included[i])
5035 continue;
5036 if (symdef->file_offset == last)
5037 {
5038 included[i] = TRUE;
5039 continue;
5040 }
5041
8387904d
AM
5042 h = archive_symbol_lookup (abfd, info, symdef->name);
5043 if (h == (struct elf_link_hash_entry *) 0 - 1)
5044 goto error_return;
0ad989f9
L
5045
5046 if (h == NULL)
5047 continue;
5048
5049 if (h->root.type == bfd_link_hash_common)
5050 {
5051 /* We currently have a common symbol. The archive map contains
5052 a reference to this symbol, so we may want to include it. We
5053 only want to include it however, if this archive element
5054 contains a definition of the symbol, not just another common
5055 declaration of it.
5056
5057 Unfortunately some archivers (including GNU ar) will put
5058 declarations of common symbols into their archive maps, as
5059 well as real definitions, so we cannot just go by the archive
5060 map alone. Instead we must read in the element's symbol
5061 table and check that to see what kind of symbol definition
5062 this is. */
5063 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5064 continue;
5065 }
5066 else if (h->root.type != bfd_link_hash_undefined)
5067 {
5068 if (h->root.type != bfd_link_hash_undefweak)
5069 defined[i] = TRUE;
5070 continue;
5071 }
5072
5073 /* We need to include this archive member. */
5074 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5075 if (element == NULL)
5076 goto error_return;
5077
5078 if (! bfd_check_format (element, bfd_object))
5079 goto error_return;
5080
5081 /* Doublecheck that we have not included this object
5082 already--it should be impossible, but there may be
5083 something wrong with the archive. */
5084 if (element->archive_pass != 0)
5085 {
5086 bfd_set_error (bfd_error_bad_value);
5087 goto error_return;
5088 }
5089 element->archive_pass = 1;
5090
5091 undefs_tail = info->hash->undefs_tail;
5092
0e144ba7
AM
5093 if (!(*info->callbacks
5094 ->add_archive_element) (info, element, symdef->name, &element))
0ad989f9 5095 goto error_return;
0e144ba7 5096 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5097 goto error_return;
5098
5099 /* If there are any new undefined symbols, we need to make
5100 another pass through the archive in order to see whether
5101 they can be defined. FIXME: This isn't perfect, because
5102 common symbols wind up on undefs_tail and because an
5103 undefined symbol which is defined later on in this pass
5104 does not require another pass. This isn't a bug, but it
5105 does make the code less efficient than it could be. */
5106 if (undefs_tail != info->hash->undefs_tail)
5107 loop = TRUE;
5108
5109 /* Look backward to mark all symbols from this object file
5110 which we have already seen in this pass. */
5111 mark = i;
5112 do
5113 {
5114 included[mark] = TRUE;
5115 if (mark == 0)
5116 break;
5117 --mark;
5118 }
5119 while (symdefs[mark].file_offset == symdef->file_offset);
5120
5121 /* We mark subsequent symbols from this object file as we go
5122 on through the loop. */
5123 last = symdef->file_offset;
5124 }
5125 }
5126 while (loop);
5127
5128 free (defined);
5129 free (included);
5130
5131 return TRUE;
5132
5133 error_return:
5134 if (defined != NULL)
5135 free (defined);
5136 if (included != NULL)
5137 free (included);
5138 return FALSE;
5139}
4ad4eba5
AM
5140
5141/* Given an ELF BFD, add symbols to the global hash table as
5142 appropriate. */
5143
5144bfd_boolean
5145bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5146{
5147 switch (bfd_get_format (abfd))
5148 {
5149 case bfd_object:
5150 return elf_link_add_object_symbols (abfd, info);
5151 case bfd_archive:
5152 return elf_link_add_archive_symbols (abfd, info);
5153 default:
5154 bfd_set_error (bfd_error_wrong_format);
5155 return FALSE;
5156 }
5157}
5a580b3a 5158\f
14b1c01e
AM
5159struct hash_codes_info
5160{
5161 unsigned long *hashcodes;
5162 bfd_boolean error;
5163};
a0c8462f 5164
5a580b3a
AM
5165/* This function will be called though elf_link_hash_traverse to store
5166 all hash value of the exported symbols in an array. */
5167
5168static bfd_boolean
5169elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5170{
a50b1753 5171 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a
AM
5172 const char *name;
5173 char *p;
5174 unsigned long ha;
5175 char *alc = NULL;
5176
5177 if (h->root.type == bfd_link_hash_warning)
5178 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5179
5180 /* Ignore indirect symbols. These are added by the versioning code. */
5181 if (h->dynindx == -1)
5182 return TRUE;
5183
5184 name = h->root.root.string;
5185 p = strchr (name, ELF_VER_CHR);
5186 if (p != NULL)
5187 {
a50b1753 5188 alc = (char *) bfd_malloc (p - name + 1);
14b1c01e
AM
5189 if (alc == NULL)
5190 {
5191 inf->error = TRUE;
5192 return FALSE;
5193 }
5a580b3a
AM
5194 memcpy (alc, name, p - name);
5195 alc[p - name] = '\0';
5196 name = alc;
5197 }
5198
5199 /* Compute the hash value. */
5200 ha = bfd_elf_hash (name);
5201
5202 /* Store the found hash value in the array given as the argument. */
14b1c01e 5203 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5204
5205 /* And store it in the struct so that we can put it in the hash table
5206 later. */
f6e332e6 5207 h->u.elf_hash_value = ha;
5a580b3a
AM
5208
5209 if (alc != NULL)
5210 free (alc);
5211
5212 return TRUE;
5213}
5214
fdc90cb4
JJ
5215struct collect_gnu_hash_codes
5216{
5217 bfd *output_bfd;
5218 const struct elf_backend_data *bed;
5219 unsigned long int nsyms;
5220 unsigned long int maskbits;
5221 unsigned long int *hashcodes;
5222 unsigned long int *hashval;
5223 unsigned long int *indx;
5224 unsigned long int *counts;
5225 bfd_vma *bitmask;
5226 bfd_byte *contents;
5227 long int min_dynindx;
5228 unsigned long int bucketcount;
5229 unsigned long int symindx;
5230 long int local_indx;
5231 long int shift1, shift2;
5232 unsigned long int mask;
14b1c01e 5233 bfd_boolean error;
fdc90cb4
JJ
5234};
5235
5236/* This function will be called though elf_link_hash_traverse to store
5237 all hash value of the exported symbols in an array. */
5238
5239static bfd_boolean
5240elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5241{
a50b1753 5242 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5243 const char *name;
5244 char *p;
5245 unsigned long ha;
5246 char *alc = NULL;
5247
5248 if (h->root.type == bfd_link_hash_warning)
5249 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5250
5251 /* Ignore indirect symbols. These are added by the versioning code. */
5252 if (h->dynindx == -1)
5253 return TRUE;
5254
5255 /* Ignore also local symbols and undefined symbols. */
5256 if (! (*s->bed->elf_hash_symbol) (h))
5257 return TRUE;
5258
5259 name = h->root.root.string;
5260 p = strchr (name, ELF_VER_CHR);
5261 if (p != NULL)
5262 {
a50b1753 5263 alc = (char *) bfd_malloc (p - name + 1);
14b1c01e
AM
5264 if (alc == NULL)
5265 {
5266 s->error = TRUE;
5267 return FALSE;
5268 }
fdc90cb4
JJ
5269 memcpy (alc, name, p - name);
5270 alc[p - name] = '\0';
5271 name = alc;
5272 }
5273
5274 /* Compute the hash value. */
5275 ha = bfd_elf_gnu_hash (name);
5276
5277 /* Store the found hash value in the array for compute_bucket_count,
5278 and also for .dynsym reordering purposes. */
5279 s->hashcodes[s->nsyms] = ha;
5280 s->hashval[h->dynindx] = ha;
5281 ++s->nsyms;
5282 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5283 s->min_dynindx = h->dynindx;
5284
5285 if (alc != NULL)
5286 free (alc);
5287
5288 return TRUE;
5289}
5290
5291/* This function will be called though elf_link_hash_traverse to do
5292 final dynaminc symbol renumbering. */
5293
5294static bfd_boolean
5295elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5296{
a50b1753 5297 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5298 unsigned long int bucket;
5299 unsigned long int val;
5300
5301 if (h->root.type == bfd_link_hash_warning)
5302 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5303
5304 /* Ignore indirect symbols. */
5305 if (h->dynindx == -1)
5306 return TRUE;
5307
5308 /* Ignore also local symbols and undefined symbols. */
5309 if (! (*s->bed->elf_hash_symbol) (h))
5310 {
5311 if (h->dynindx >= s->min_dynindx)
5312 h->dynindx = s->local_indx++;
5313 return TRUE;
5314 }
5315
5316 bucket = s->hashval[h->dynindx] % s->bucketcount;
5317 val = (s->hashval[h->dynindx] >> s->shift1)
5318 & ((s->maskbits >> s->shift1) - 1);
5319 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5320 s->bitmask[val]
5321 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5322 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5323 if (s->counts[bucket] == 1)
5324 /* Last element terminates the chain. */
5325 val |= 1;
5326 bfd_put_32 (s->output_bfd, val,
5327 s->contents + (s->indx[bucket] - s->symindx) * 4);
5328 --s->counts[bucket];
5329 h->dynindx = s->indx[bucket]++;
5330 return TRUE;
5331}
5332
5333/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5334
5335bfd_boolean
5336_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5337{
5338 return !(h->forced_local
5339 || h->root.type == bfd_link_hash_undefined
5340 || h->root.type == bfd_link_hash_undefweak
5341 || ((h->root.type == bfd_link_hash_defined
5342 || h->root.type == bfd_link_hash_defweak)
5343 && h->root.u.def.section->output_section == NULL));
5344}
5345
5a580b3a
AM
5346/* Array used to determine the number of hash table buckets to use
5347 based on the number of symbols there are. If there are fewer than
5348 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5349 fewer than 37 we use 17 buckets, and so forth. We never use more
5350 than 32771 buckets. */
5351
5352static const size_t elf_buckets[] =
5353{
5354 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5355 16411, 32771, 0
5356};
5357
5358/* Compute bucket count for hashing table. We do not use a static set
5359 of possible tables sizes anymore. Instead we determine for all
5360 possible reasonable sizes of the table the outcome (i.e., the
5361 number of collisions etc) and choose the best solution. The
5362 weighting functions are not too simple to allow the table to grow
5363 without bounds. Instead one of the weighting factors is the size.
5364 Therefore the result is always a good payoff between few collisions
5365 (= short chain lengths) and table size. */
5366static size_t
b20dd2ce 5367compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5368 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5369 unsigned long int nsyms,
5370 int gnu_hash)
5a580b3a 5371{
5a580b3a 5372 size_t best_size = 0;
5a580b3a 5373 unsigned long int i;
5a580b3a 5374
5a580b3a
AM
5375 /* We have a problem here. The following code to optimize the table
5376 size requires an integer type with more the 32 bits. If
5377 BFD_HOST_U_64_BIT is set we know about such a type. */
5378#ifdef BFD_HOST_U_64_BIT
5379 if (info->optimize)
5380 {
5a580b3a
AM
5381 size_t minsize;
5382 size_t maxsize;
5383 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5384 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5385 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5386 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5387 unsigned long int *counts;
d40f3da9 5388 bfd_size_type amt;
0883b6e0 5389 unsigned int no_improvement_count = 0;
5a580b3a
AM
5390
5391 /* Possible optimization parameters: if we have NSYMS symbols we say
5392 that the hashing table must at least have NSYMS/4 and at most
5393 2*NSYMS buckets. */
5394 minsize = nsyms / 4;
5395 if (minsize == 0)
5396 minsize = 1;
5397 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5398 if (gnu_hash)
5399 {
5400 if (minsize < 2)
5401 minsize = 2;
5402 if ((best_size & 31) == 0)
5403 ++best_size;
5404 }
5a580b3a
AM
5405
5406 /* Create array where we count the collisions in. We must use bfd_malloc
5407 since the size could be large. */
5408 amt = maxsize;
5409 amt *= sizeof (unsigned long int);
a50b1753 5410 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5411 if (counts == NULL)
fdc90cb4 5412 return 0;
5a580b3a
AM
5413
5414 /* Compute the "optimal" size for the hash table. The criteria is a
5415 minimal chain length. The minor criteria is (of course) the size
5416 of the table. */
5417 for (i = minsize; i < maxsize; ++i)
5418 {
5419 /* Walk through the array of hashcodes and count the collisions. */
5420 BFD_HOST_U_64_BIT max;
5421 unsigned long int j;
5422 unsigned long int fact;
5423
fdc90cb4
JJ
5424 if (gnu_hash && (i & 31) == 0)
5425 continue;
5426
5a580b3a
AM
5427 memset (counts, '\0', i * sizeof (unsigned long int));
5428
5429 /* Determine how often each hash bucket is used. */
5430 for (j = 0; j < nsyms; ++j)
5431 ++counts[hashcodes[j] % i];
5432
5433 /* For the weight function we need some information about the
5434 pagesize on the target. This is information need not be 100%
5435 accurate. Since this information is not available (so far) we
5436 define it here to a reasonable default value. If it is crucial
5437 to have a better value some day simply define this value. */
5438# ifndef BFD_TARGET_PAGESIZE
5439# define BFD_TARGET_PAGESIZE (4096)
5440# endif
5441
fdc90cb4
JJ
5442 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5443 and the chains. */
5444 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5445
5446# if 1
5447 /* Variant 1: optimize for short chains. We add the squares
5448 of all the chain lengths (which favors many small chain
5449 over a few long chains). */
5450 for (j = 0; j < i; ++j)
5451 max += counts[j] * counts[j];
5452
5453 /* This adds penalties for the overall size of the table. */
fdc90cb4 5454 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5455 max *= fact * fact;
5456# else
5457 /* Variant 2: Optimize a lot more for small table. Here we
5458 also add squares of the size but we also add penalties for
5459 empty slots (the +1 term). */
5460 for (j = 0; j < i; ++j)
5461 max += (1 + counts[j]) * (1 + counts[j]);
5462
5463 /* The overall size of the table is considered, but not as
5464 strong as in variant 1, where it is squared. */
fdc90cb4 5465 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5466 max *= fact;
5467# endif
5468
5469 /* Compare with current best results. */
5470 if (max < best_chlen)
5471 {
5472 best_chlen = max;
5473 best_size = i;
0883b6e0 5474 no_improvement_count = 0;
5a580b3a 5475 }
0883b6e0
NC
5476 /* PR 11843: Avoid futile long searches for the best bucket size
5477 when there are a large number of symbols. */
5478 else if (++no_improvement_count == 100)
5479 break;
5a580b3a
AM
5480 }
5481
5482 free (counts);
5483 }
5484 else
5485#endif /* defined (BFD_HOST_U_64_BIT) */
5486 {
5487 /* This is the fallback solution if no 64bit type is available or if we
5488 are not supposed to spend much time on optimizations. We select the
5489 bucket count using a fixed set of numbers. */
5490 for (i = 0; elf_buckets[i] != 0; i++)
5491 {
5492 best_size = elf_buckets[i];
fdc90cb4 5493 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5494 break;
5495 }
fdc90cb4
JJ
5496 if (gnu_hash && best_size < 2)
5497 best_size = 2;
5a580b3a
AM
5498 }
5499
5a580b3a
AM
5500 return best_size;
5501}
5502
d0bf826b
AM
5503/* Size any SHT_GROUP section for ld -r. */
5504
5505bfd_boolean
5506_bfd_elf_size_group_sections (struct bfd_link_info *info)
5507{
5508 bfd *ibfd;
5509
5510 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5511 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5512 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5513 return FALSE;
5514 return TRUE;
5515}
5516
5a580b3a
AM
5517/* Set up the sizes and contents of the ELF dynamic sections. This is
5518 called by the ELF linker emulation before_allocation routine. We
5519 must set the sizes of the sections before the linker sets the
5520 addresses of the various sections. */
5521
5522bfd_boolean
5523bfd_elf_size_dynamic_sections (bfd *output_bfd,
5524 const char *soname,
5525 const char *rpath,
5526 const char *filter_shlib,
7ee314fa
AM
5527 const char *audit,
5528 const char *depaudit,
5a580b3a
AM
5529 const char * const *auxiliary_filters,
5530 struct bfd_link_info *info,
5531 asection **sinterpptr,
5532 struct bfd_elf_version_tree *verdefs)
5533{
5534 bfd_size_type soname_indx;
5535 bfd *dynobj;
5536 const struct elf_backend_data *bed;
28caa186 5537 struct elf_info_failed asvinfo;
5a580b3a
AM
5538
5539 *sinterpptr = NULL;
5540
5541 soname_indx = (bfd_size_type) -1;
5542
5543 if (!is_elf_hash_table (info->hash))
5544 return TRUE;
5545
6bfdb61b 5546 bed = get_elf_backend_data (output_bfd);
5a580b3a
AM
5547 if (info->execstack)
5548 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5549 else if (info->noexecstack)
5550 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5551 else
5552 {
5553 bfd *inputobj;
5554 asection *notesec = NULL;
5555 int exec = 0;
5556
5557 for (inputobj = info->input_bfds;
5558 inputobj;
5559 inputobj = inputobj->link_next)
5560 {
5561 asection *s;
5562
a94b9d2d 5563 if (inputobj->flags & (DYNAMIC | EXEC_P | BFD_LINKER_CREATED))
5a580b3a
AM
5564 continue;
5565 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5566 if (s)
5567 {
5568 if (s->flags & SEC_CODE)
5569 exec = PF_X;
5570 notesec = s;
5571 }
6bfdb61b 5572 else if (bed->default_execstack)
5a580b3a
AM
5573 exec = PF_X;
5574 }
5575 if (notesec)
5576 {
5577 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5578 if (exec && info->relocatable
5579 && notesec->output_section != bfd_abs_section_ptr)
5580 notesec->output_section->flags |= SEC_CODE;
5581 }
5582 }
5583
5584 /* Any syms created from now on start with -1 in
5585 got.refcount/offset and plt.refcount/offset. */
a6aa5195
AM
5586 elf_hash_table (info)->init_got_refcount
5587 = elf_hash_table (info)->init_got_offset;
5588 elf_hash_table (info)->init_plt_refcount
5589 = elf_hash_table (info)->init_plt_offset;
5a580b3a 5590
d0bf826b
AM
5591 if (info->relocatable
5592 && !_bfd_elf_size_group_sections (info))
5593 return FALSE;
5594
5a580b3a
AM
5595 /* The backend may have to create some sections regardless of whether
5596 we're dynamic or not. */
5a580b3a
AM
5597 if (bed->elf_backend_always_size_sections
5598 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5599 return FALSE;
5600
eb3d5f3b
JB
5601 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5602 return FALSE;
5603
5a580b3a
AM
5604 dynobj = elf_hash_table (info)->dynobj;
5605
5606 /* If there were no dynamic objects in the link, there is nothing to
5607 do here. */
5608 if (dynobj == NULL)
5609 return TRUE;
5610
5a580b3a
AM
5611 if (elf_hash_table (info)->dynamic_sections_created)
5612 {
5613 struct elf_info_failed eif;
5614 struct elf_link_hash_entry *h;
5615 asection *dynstr;
5616 struct bfd_elf_version_tree *t;
5617 struct bfd_elf_version_expr *d;
046183de 5618 asection *s;
5a580b3a
AM
5619 bfd_boolean all_defined;
5620
5621 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5622 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5623
5624 if (soname != NULL)
5625 {
5626 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5627 soname, TRUE);
5628 if (soname_indx == (bfd_size_type) -1
5629 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5630 return FALSE;
5631 }
5632
5633 if (info->symbolic)
5634 {
5635 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5636 return FALSE;
5637 info->flags |= DF_SYMBOLIC;
5638 }
5639
5640 if (rpath != NULL)
5641 {
5642 bfd_size_type indx;
5643
5644 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5645 TRUE);
5646 if (indx == (bfd_size_type) -1
5647 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5648 return FALSE;
5649
5650 if (info->new_dtags)
5651 {
5652 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5653 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5654 return FALSE;
5655 }
5656 }
5657
5658 if (filter_shlib != NULL)
5659 {
5660 bfd_size_type indx;
5661
5662 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5663 filter_shlib, TRUE);
5664 if (indx == (bfd_size_type) -1
5665 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5666 return FALSE;
5667 }
5668
5669 if (auxiliary_filters != NULL)
5670 {
5671 const char * const *p;
5672
5673 for (p = auxiliary_filters; *p != NULL; p++)
5674 {
5675 bfd_size_type indx;
5676
5677 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5678 *p, TRUE);
5679 if (indx == (bfd_size_type) -1
5680 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5681 return FALSE;
5682 }
5683 }
5684
7ee314fa
AM
5685 if (audit != NULL)
5686 {
5687 bfd_size_type indx;
5688
5689 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5690 TRUE);
5691 if (indx == (bfd_size_type) -1
5692 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5693 return FALSE;
5694 }
5695
5696 if (depaudit != NULL)
5697 {
5698 bfd_size_type indx;
5699
5700 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5701 TRUE);
5702 if (indx == (bfd_size_type) -1
5703 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5704 return FALSE;
5705 }
5706
5a580b3a
AM
5707 eif.info = info;
5708 eif.verdefs = verdefs;
5709 eif.failed = FALSE;
5710
5711 /* If we are supposed to export all symbols into the dynamic symbol
5712 table (this is not the normal case), then do so. */
55255dae
L
5713 if (info->export_dynamic
5714 || (info->executable && info->dynamic))
5a580b3a
AM
5715 {
5716 elf_link_hash_traverse (elf_hash_table (info),
5717 _bfd_elf_export_symbol,
5718 &eif);
5719 if (eif.failed)
5720 return FALSE;
5721 }
5722
5723 /* Make all global versions with definition. */
5724 for (t = verdefs; t != NULL; t = t->next)
5725 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5726 if (!d->symver && d->literal)
5a580b3a
AM
5727 {
5728 const char *verstr, *name;
5729 size_t namelen, verlen, newlen;
93252b1c 5730 char *newname, *p, leading_char;
5a580b3a
AM
5731 struct elf_link_hash_entry *newh;
5732
93252b1c 5733 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 5734 name = d->pattern;
93252b1c 5735 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
5736 verstr = t->name;
5737 verlen = strlen (verstr);
5738 newlen = namelen + verlen + 3;
5739
a50b1753 5740 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
5741 if (newname == NULL)
5742 return FALSE;
93252b1c
MF
5743 newname[0] = leading_char;
5744 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
5745
5746 /* Check the hidden versioned definition. */
5747 p = newname + namelen;
5748 *p++ = ELF_VER_CHR;
5749 memcpy (p, verstr, verlen + 1);
5750 newh = elf_link_hash_lookup (elf_hash_table (info),
5751 newname, FALSE, FALSE,
5752 FALSE);
5753 if (newh == NULL
5754 || (newh->root.type != bfd_link_hash_defined
5755 && newh->root.type != bfd_link_hash_defweak))
5756 {
5757 /* Check the default versioned definition. */
5758 *p++ = ELF_VER_CHR;
5759 memcpy (p, verstr, verlen + 1);
5760 newh = elf_link_hash_lookup (elf_hash_table (info),
5761 newname, FALSE, FALSE,
5762 FALSE);
5763 }
5764 free (newname);
5765
5766 /* Mark this version if there is a definition and it is
5767 not defined in a shared object. */
5768 if (newh != NULL
f5385ebf 5769 && !newh->def_dynamic
5a580b3a
AM
5770 && (newh->root.type == bfd_link_hash_defined
5771 || newh->root.type == bfd_link_hash_defweak))
5772 d->symver = 1;
5773 }
5774
5775 /* Attach all the symbols to their version information. */
5a580b3a
AM
5776 asvinfo.info = info;
5777 asvinfo.verdefs = verdefs;
5778 asvinfo.failed = FALSE;
5779
5780 elf_link_hash_traverse (elf_hash_table (info),
5781 _bfd_elf_link_assign_sym_version,
5782 &asvinfo);
5783 if (asvinfo.failed)
5784 return FALSE;
5785
5786 if (!info->allow_undefined_version)
5787 {
5788 /* Check if all global versions have a definition. */
5789 all_defined = TRUE;
5790 for (t = verdefs; t != NULL; t = t->next)
5791 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5792 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
5793 {
5794 (*_bfd_error_handler)
5795 (_("%s: undefined version: %s"),
5796 d->pattern, t->name);
5797 all_defined = FALSE;
5798 }
5799
5800 if (!all_defined)
5801 {
5802 bfd_set_error (bfd_error_bad_value);
5803 return FALSE;
5804 }
5805 }
5806
5807 /* Find all symbols which were defined in a dynamic object and make
5808 the backend pick a reasonable value for them. */
5809 elf_link_hash_traverse (elf_hash_table (info),
5810 _bfd_elf_adjust_dynamic_symbol,
5811 &eif);
5812 if (eif.failed)
5813 return FALSE;
5814
5815 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 5816 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
5817 now so that we know the final size of the .dynamic section. */
5818
5819 /* If there are initialization and/or finalization functions to
5820 call then add the corresponding DT_INIT/DT_FINI entries. */
5821 h = (info->init_function
5822 ? elf_link_hash_lookup (elf_hash_table (info),
5823 info->init_function, FALSE,
5824 FALSE, FALSE)
5825 : NULL);
5826 if (h != NULL
f5385ebf
AM
5827 && (h->ref_regular
5828 || h->def_regular))
5a580b3a
AM
5829 {
5830 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5831 return FALSE;
5832 }
5833 h = (info->fini_function
5834 ? elf_link_hash_lookup (elf_hash_table (info),
5835 info->fini_function, FALSE,
5836 FALSE, FALSE)
5837 : NULL);
5838 if (h != NULL
f5385ebf
AM
5839 && (h->ref_regular
5840 || h->def_regular))
5a580b3a
AM
5841 {
5842 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5843 return FALSE;
5844 }
5845
046183de
AM
5846 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5847 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5848 {
5849 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5850 if (! info->executable)
5851 {
5852 bfd *sub;
5853 asection *o;
5854
5855 for (sub = info->input_bfds; sub != NULL;
5856 sub = sub->link_next)
3fcd97f1
JJ
5857 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5858 for (o = sub->sections; o != NULL; o = o->next)
5859 if (elf_section_data (o)->this_hdr.sh_type
5860 == SHT_PREINIT_ARRAY)
5861 {
5862 (*_bfd_error_handler)
5863 (_("%B: .preinit_array section is not allowed in DSO"),
5864 sub);
5865 break;
5866 }
5a580b3a
AM
5867
5868 bfd_set_error (bfd_error_nonrepresentable_section);
5869 return FALSE;
5870 }
5871
5872 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5873 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5874 return FALSE;
5875 }
046183de
AM
5876 s = bfd_get_section_by_name (output_bfd, ".init_array");
5877 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5878 {
5879 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5880 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5881 return FALSE;
5882 }
046183de
AM
5883 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5884 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5885 {
5886 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5887 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5888 return FALSE;
5889 }
5890
5891 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5892 /* If .dynstr is excluded from the link, we don't want any of
5893 these tags. Strictly, we should be checking each section
5894 individually; This quick check covers for the case where
5895 someone does a /DISCARD/ : { *(*) }. */
5896 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5897 {
5898 bfd_size_type strsize;
5899
5900 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
5901 if ((info->emit_hash
5902 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5903 || (info->emit_gnu_hash
5904 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
5905 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5906 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5907 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5908 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5909 bed->s->sizeof_sym))
5910 return FALSE;
5911 }
5912 }
5913
5914 /* The backend must work out the sizes of all the other dynamic
5915 sections. */
5916 if (bed->elf_backend_size_dynamic_sections
5917 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5918 return FALSE;
5919
5920 if (elf_hash_table (info)->dynamic_sections_created)
5921 {
554220db 5922 unsigned long section_sym_count;
5a580b3a 5923 asection *s;
5a580b3a
AM
5924
5925 /* Set up the version definition section. */
5926 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5927 BFD_ASSERT (s != NULL);
5928
5929 /* We may have created additional version definitions if we are
5930 just linking a regular application. */
5931 verdefs = asvinfo.verdefs;
5932
5933 /* Skip anonymous version tag. */
5934 if (verdefs != NULL && verdefs->vernum == 0)
5935 verdefs = verdefs->next;
5936
3e3b46e5 5937 if (verdefs == NULL && !info->create_default_symver)
8423293d 5938 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
5939 else
5940 {
5941 unsigned int cdefs;
5942 bfd_size_type size;
5943 struct bfd_elf_version_tree *t;
5944 bfd_byte *p;
5945 Elf_Internal_Verdef def;
5946 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
5947 struct bfd_link_hash_entry *bh;
5948 struct elf_link_hash_entry *h;
5949 const char *name;
5a580b3a
AM
5950
5951 cdefs = 0;
5952 size = 0;
5953
5954 /* Make space for the base version. */
5955 size += sizeof (Elf_External_Verdef);
5956 size += sizeof (Elf_External_Verdaux);
5957 ++cdefs;
5958
3e3b46e5
PB
5959 /* Make space for the default version. */
5960 if (info->create_default_symver)
5961 {
5962 size += sizeof (Elf_External_Verdef);
5963 ++cdefs;
5964 }
5965
5a580b3a
AM
5966 for (t = verdefs; t != NULL; t = t->next)
5967 {
5968 struct bfd_elf_version_deps *n;
5969
a6cc6b3b
RO
5970 /* Don't emit base version twice. */
5971 if (t->vernum == 0)
5972 continue;
5973
5a580b3a
AM
5974 size += sizeof (Elf_External_Verdef);
5975 size += sizeof (Elf_External_Verdaux);
5976 ++cdefs;
5977
5978 for (n = t->deps; n != NULL; n = n->next)
5979 size += sizeof (Elf_External_Verdaux);
5980 }
5981
eea6121a 5982 s->size = size;
a50b1753 5983 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 5984 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
5985 return FALSE;
5986
5987 /* Fill in the version definition section. */
5988
5989 p = s->contents;
5990
5991 def.vd_version = VER_DEF_CURRENT;
5992 def.vd_flags = VER_FLG_BASE;
5993 def.vd_ndx = 1;
5994 def.vd_cnt = 1;
3e3b46e5
PB
5995 if (info->create_default_symver)
5996 {
5997 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5998 def.vd_next = sizeof (Elf_External_Verdef);
5999 }
6000 else
6001 {
6002 def.vd_aux = sizeof (Elf_External_Verdef);
6003 def.vd_next = (sizeof (Elf_External_Verdef)
6004 + sizeof (Elf_External_Verdaux));
6005 }
5a580b3a
AM
6006
6007 if (soname_indx != (bfd_size_type) -1)
6008 {
6009 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6010 soname_indx);
6011 def.vd_hash = bfd_elf_hash (soname);
6012 defaux.vda_name = soname_indx;
3e3b46e5 6013 name = soname;
5a580b3a
AM
6014 }
6015 else
6016 {
5a580b3a
AM
6017 bfd_size_type indx;
6018
06084812 6019 name = lbasename (output_bfd->filename);
5a580b3a
AM
6020 def.vd_hash = bfd_elf_hash (name);
6021 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6022 name, FALSE);
6023 if (indx == (bfd_size_type) -1)
6024 return FALSE;
6025 defaux.vda_name = indx;
6026 }
6027 defaux.vda_next = 0;
6028
6029 _bfd_elf_swap_verdef_out (output_bfd, &def,
6030 (Elf_External_Verdef *) p);
6031 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6032 if (info->create_default_symver)
6033 {
6034 /* Add a symbol representing this version. */
6035 bh = NULL;
6036 if (! (_bfd_generic_link_add_one_symbol
6037 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6038 0, NULL, FALSE,
6039 get_elf_backend_data (dynobj)->collect, &bh)))
6040 return FALSE;
6041 h = (struct elf_link_hash_entry *) bh;
6042 h->non_elf = 0;
6043 h->def_regular = 1;
6044 h->type = STT_OBJECT;
6045 h->verinfo.vertree = NULL;
6046
6047 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6048 return FALSE;
6049
6050 /* Create a duplicate of the base version with the same
6051 aux block, but different flags. */
6052 def.vd_flags = 0;
6053 def.vd_ndx = 2;
6054 def.vd_aux = sizeof (Elf_External_Verdef);
6055 if (verdefs)
6056 def.vd_next = (sizeof (Elf_External_Verdef)
6057 + sizeof (Elf_External_Verdaux));
6058 else
6059 def.vd_next = 0;
6060 _bfd_elf_swap_verdef_out (output_bfd, &def,
6061 (Elf_External_Verdef *) p);
6062 p += sizeof (Elf_External_Verdef);
6063 }
5a580b3a
AM
6064 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6065 (Elf_External_Verdaux *) p);
6066 p += sizeof (Elf_External_Verdaux);
6067
6068 for (t = verdefs; t != NULL; t = t->next)
6069 {
6070 unsigned int cdeps;
6071 struct bfd_elf_version_deps *n;
5a580b3a 6072
a6cc6b3b
RO
6073 /* Don't emit the base version twice. */
6074 if (t->vernum == 0)
6075 continue;
6076
5a580b3a
AM
6077 cdeps = 0;
6078 for (n = t->deps; n != NULL; n = n->next)
6079 ++cdeps;
6080
6081 /* Add a symbol representing this version. */
6082 bh = NULL;
6083 if (! (_bfd_generic_link_add_one_symbol
6084 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6085 0, NULL, FALSE,
6086 get_elf_backend_data (dynobj)->collect, &bh)))
6087 return FALSE;
6088 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6089 h->non_elf = 0;
6090 h->def_regular = 1;
5a580b3a
AM
6091 h->type = STT_OBJECT;
6092 h->verinfo.vertree = t;
6093
c152c796 6094 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6095 return FALSE;
6096
6097 def.vd_version = VER_DEF_CURRENT;
6098 def.vd_flags = 0;
6099 if (t->globals.list == NULL
6100 && t->locals.list == NULL
6101 && ! t->used)
6102 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6103 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6104 def.vd_cnt = cdeps + 1;
6105 def.vd_hash = bfd_elf_hash (t->name);
6106 def.vd_aux = sizeof (Elf_External_Verdef);
6107 def.vd_next = 0;
a6cc6b3b
RO
6108
6109 /* If a basever node is next, it *must* be the last node in
6110 the chain, otherwise Verdef construction breaks. */
6111 if (t->next != NULL && t->next->vernum == 0)
6112 BFD_ASSERT (t->next->next == NULL);
6113
6114 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6115 def.vd_next = (sizeof (Elf_External_Verdef)
6116 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6117
6118 _bfd_elf_swap_verdef_out (output_bfd, &def,
6119 (Elf_External_Verdef *) p);
6120 p += sizeof (Elf_External_Verdef);
6121
6122 defaux.vda_name = h->dynstr_index;
6123 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6124 h->dynstr_index);
6125 defaux.vda_next = 0;
6126 if (t->deps != NULL)
6127 defaux.vda_next = sizeof (Elf_External_Verdaux);
6128 t->name_indx = defaux.vda_name;
6129
6130 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6131 (Elf_External_Verdaux *) p);
6132 p += sizeof (Elf_External_Verdaux);
6133
6134 for (n = t->deps; n != NULL; n = n->next)
6135 {
6136 if (n->version_needed == NULL)
6137 {
6138 /* This can happen if there was an error in the
6139 version script. */
6140 defaux.vda_name = 0;
6141 }
6142 else
6143 {
6144 defaux.vda_name = n->version_needed->name_indx;
6145 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6146 defaux.vda_name);
6147 }
6148 if (n->next == NULL)
6149 defaux.vda_next = 0;
6150 else
6151 defaux.vda_next = sizeof (Elf_External_Verdaux);
6152
6153 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6154 (Elf_External_Verdaux *) p);
6155 p += sizeof (Elf_External_Verdaux);
6156 }
6157 }
6158
6159 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6160 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6161 return FALSE;
6162
6163 elf_tdata (output_bfd)->cverdefs = cdefs;
6164 }
6165
6166 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6167 {
6168 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6169 return FALSE;
6170 }
6171 else if (info->flags & DF_BIND_NOW)
6172 {
6173 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6174 return FALSE;
6175 }
6176
6177 if (info->flags_1)
6178 {
6179 if (info->executable)
6180 info->flags_1 &= ~ (DF_1_INITFIRST
6181 | DF_1_NODELETE
6182 | DF_1_NOOPEN);
6183 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6184 return FALSE;
6185 }
6186
6187 /* Work out the size of the version reference section. */
6188
6189 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
6190 BFD_ASSERT (s != NULL);
6191 {
6192 struct elf_find_verdep_info sinfo;
6193
5a580b3a
AM
6194 sinfo.info = info;
6195 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6196 if (sinfo.vers == 0)
6197 sinfo.vers = 1;
6198 sinfo.failed = FALSE;
6199
6200 elf_link_hash_traverse (elf_hash_table (info),
6201 _bfd_elf_link_find_version_dependencies,
6202 &sinfo);
14b1c01e
AM
6203 if (sinfo.failed)
6204 return FALSE;
5a580b3a
AM
6205
6206 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6207 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6208 else
6209 {
6210 Elf_Internal_Verneed *t;
6211 unsigned int size;
6212 unsigned int crefs;
6213 bfd_byte *p;
6214
a6cc6b3b 6215 /* Build the version dependency section. */
5a580b3a
AM
6216 size = 0;
6217 crefs = 0;
6218 for (t = elf_tdata (output_bfd)->verref;
6219 t != NULL;
6220 t = t->vn_nextref)
6221 {
6222 Elf_Internal_Vernaux *a;
6223
6224 size += sizeof (Elf_External_Verneed);
6225 ++crefs;
6226 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6227 size += sizeof (Elf_External_Vernaux);
6228 }
6229
eea6121a 6230 s->size = size;
a50b1753 6231 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6232 if (s->contents == NULL)
6233 return FALSE;
6234
6235 p = s->contents;
6236 for (t = elf_tdata (output_bfd)->verref;
6237 t != NULL;
6238 t = t->vn_nextref)
6239 {
6240 unsigned int caux;
6241 Elf_Internal_Vernaux *a;
6242 bfd_size_type indx;
6243
6244 caux = 0;
6245 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6246 ++caux;
6247
6248 t->vn_version = VER_NEED_CURRENT;
6249 t->vn_cnt = caux;
6250 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6251 elf_dt_name (t->vn_bfd) != NULL
6252 ? elf_dt_name (t->vn_bfd)
06084812 6253 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6254 FALSE);
6255 if (indx == (bfd_size_type) -1)
6256 return FALSE;
6257 t->vn_file = indx;
6258 t->vn_aux = sizeof (Elf_External_Verneed);
6259 if (t->vn_nextref == NULL)
6260 t->vn_next = 0;
6261 else
6262 t->vn_next = (sizeof (Elf_External_Verneed)
6263 + caux * sizeof (Elf_External_Vernaux));
6264
6265 _bfd_elf_swap_verneed_out (output_bfd, t,
6266 (Elf_External_Verneed *) p);
6267 p += sizeof (Elf_External_Verneed);
6268
6269 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6270 {
6271 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6272 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6273 a->vna_nodename, FALSE);
6274 if (indx == (bfd_size_type) -1)
6275 return FALSE;
6276 a->vna_name = indx;
6277 if (a->vna_nextptr == NULL)
6278 a->vna_next = 0;
6279 else
6280 a->vna_next = sizeof (Elf_External_Vernaux);
6281
6282 _bfd_elf_swap_vernaux_out (output_bfd, a,
6283 (Elf_External_Vernaux *) p);
6284 p += sizeof (Elf_External_Vernaux);
6285 }
6286 }
6287
6288 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6289 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6290 return FALSE;
6291
6292 elf_tdata (output_bfd)->cverrefs = crefs;
6293 }
6294 }
6295
8423293d
AM
6296 if ((elf_tdata (output_bfd)->cverrefs == 0
6297 && elf_tdata (output_bfd)->cverdefs == 0)
6298 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6299 &section_sym_count) == 0)
6300 {
6301 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6302 s->flags |= SEC_EXCLUDE;
6303 }
6304 }
6305 return TRUE;
6306}
6307
74541ad4
AM
6308/* Find the first non-excluded output section. We'll use its
6309 section symbol for some emitted relocs. */
6310void
6311_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6312{
6313 asection *s;
6314
6315 for (s = output_bfd->sections; s != NULL; s = s->next)
6316 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6317 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6318 {
6319 elf_hash_table (info)->text_index_section = s;
6320 break;
6321 }
6322}
6323
6324/* Find two non-excluded output sections, one for code, one for data.
6325 We'll use their section symbols for some emitted relocs. */
6326void
6327_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6328{
6329 asection *s;
6330
266b05cf
DJ
6331 /* Data first, since setting text_index_section changes
6332 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6333 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6334 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6335 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6336 {
266b05cf 6337 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6338 break;
6339 }
6340
6341 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6342 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6343 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6344 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6345 {
266b05cf 6346 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6347 break;
6348 }
6349
6350 if (elf_hash_table (info)->text_index_section == NULL)
6351 elf_hash_table (info)->text_index_section
6352 = elf_hash_table (info)->data_index_section;
6353}
6354
8423293d
AM
6355bfd_boolean
6356bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6357{
74541ad4
AM
6358 const struct elf_backend_data *bed;
6359
8423293d
AM
6360 if (!is_elf_hash_table (info->hash))
6361 return TRUE;
6362
74541ad4
AM
6363 bed = get_elf_backend_data (output_bfd);
6364 (*bed->elf_backend_init_index_section) (output_bfd, info);
6365
8423293d
AM
6366 if (elf_hash_table (info)->dynamic_sections_created)
6367 {
6368 bfd *dynobj;
8423293d
AM
6369 asection *s;
6370 bfd_size_type dynsymcount;
6371 unsigned long section_sym_count;
8423293d
AM
6372 unsigned int dtagcount;
6373
6374 dynobj = elf_hash_table (info)->dynobj;
6375
5a580b3a
AM
6376 /* Assign dynsym indicies. In a shared library we generate a
6377 section symbol for each output section, which come first.
6378 Next come all of the back-end allocated local dynamic syms,
6379 followed by the rest of the global symbols. */
6380
554220db
AM
6381 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6382 &section_sym_count);
5a580b3a
AM
6383
6384 /* Work out the size of the symbol version section. */
6385 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6386 BFD_ASSERT (s != NULL);
8423293d
AM
6387 if (dynsymcount != 0
6388 && (s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6389 {
eea6121a 6390 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6391 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6392 if (s->contents == NULL)
6393 return FALSE;
6394
6395 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6396 return FALSE;
6397 }
6398
6399 /* Set the size of the .dynsym and .hash sections. We counted
6400 the number of dynamic symbols in elf_link_add_object_symbols.
6401 We will build the contents of .dynsym and .hash when we build
6402 the final symbol table, because until then we do not know the
6403 correct value to give the symbols. We built the .dynstr
6404 section as we went along in elf_link_add_object_symbols. */
6405 s = bfd_get_section_by_name (dynobj, ".dynsym");
6406 BFD_ASSERT (s != NULL);
eea6121a 6407 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a
AM
6408
6409 if (dynsymcount != 0)
6410 {
a50b1753 6411 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
554220db
AM
6412 if (s->contents == NULL)
6413 return FALSE;
5a580b3a 6414
554220db
AM
6415 /* The first entry in .dynsym is a dummy symbol.
6416 Clear all the section syms, in case we don't output them all. */
6417 ++section_sym_count;
6418 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a
AM
6419 }
6420
fdc90cb4
JJ
6421 elf_hash_table (info)->bucketcount = 0;
6422
5a580b3a
AM
6423 /* Compute the size of the hashing table. As a side effect this
6424 computes the hash values for all the names we export. */
fdc90cb4
JJ
6425 if (info->emit_hash)
6426 {
6427 unsigned long int *hashcodes;
14b1c01e 6428 struct hash_codes_info hashinf;
fdc90cb4
JJ
6429 bfd_size_type amt;
6430 unsigned long int nsyms;
6431 size_t bucketcount;
6432 size_t hash_entry_size;
6433
6434 /* Compute the hash values for all exported symbols. At the same
6435 time store the values in an array so that we could use them for
6436 optimizations. */
6437 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6438 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6439 if (hashcodes == NULL)
6440 return FALSE;
14b1c01e
AM
6441 hashinf.hashcodes = hashcodes;
6442 hashinf.error = FALSE;
5a580b3a 6443
fdc90cb4
JJ
6444 /* Put all hash values in HASHCODES. */
6445 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6446 elf_collect_hash_codes, &hashinf);
6447 if (hashinf.error)
4dd07732
AM
6448 {
6449 free (hashcodes);
6450 return FALSE;
6451 }
5a580b3a 6452
14b1c01e 6453 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6454 bucketcount
6455 = compute_bucket_count (info, hashcodes, nsyms, 0);
6456 free (hashcodes);
6457
6458 if (bucketcount == 0)
6459 return FALSE;
5a580b3a 6460
fdc90cb4
JJ
6461 elf_hash_table (info)->bucketcount = bucketcount;
6462
6463 s = bfd_get_section_by_name (dynobj, ".hash");
6464 BFD_ASSERT (s != NULL);
6465 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6466 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6467 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6468 if (s->contents == NULL)
6469 return FALSE;
6470
6471 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6472 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6473 s->contents + hash_entry_size);
6474 }
6475
6476 if (info->emit_gnu_hash)
6477 {
6478 size_t i, cnt;
6479 unsigned char *contents;
6480 struct collect_gnu_hash_codes cinfo;
6481 bfd_size_type amt;
6482 size_t bucketcount;
6483
6484 memset (&cinfo, 0, sizeof (cinfo));
6485
6486 /* Compute the hash values for all exported symbols. At the same
6487 time store the values in an array so that we could use them for
6488 optimizations. */
6489 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6490 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6491 if (cinfo.hashcodes == NULL)
6492 return FALSE;
6493
6494 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6495 cinfo.min_dynindx = -1;
6496 cinfo.output_bfd = output_bfd;
6497 cinfo.bed = bed;
6498
6499 /* Put all hash values in HASHCODES. */
6500 elf_link_hash_traverse (elf_hash_table (info),
6501 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6502 if (cinfo.error)
4dd07732
AM
6503 {
6504 free (cinfo.hashcodes);
6505 return FALSE;
6506 }
fdc90cb4
JJ
6507
6508 bucketcount
6509 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6510
6511 if (bucketcount == 0)
6512 {
6513 free (cinfo.hashcodes);
6514 return FALSE;
6515 }
6516
6517 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6518 BFD_ASSERT (s != NULL);
6519
6520 if (cinfo.nsyms == 0)
6521 {
6522 /* Empty .gnu.hash section is special. */
6523 BFD_ASSERT (cinfo.min_dynindx == -1);
6524 free (cinfo.hashcodes);
6525 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6526 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6527 if (contents == NULL)
6528 return FALSE;
6529 s->contents = contents;
6530 /* 1 empty bucket. */
6531 bfd_put_32 (output_bfd, 1, contents);
6532 /* SYMIDX above the special symbol 0. */
6533 bfd_put_32 (output_bfd, 1, contents + 4);
6534 /* Just one word for bitmask. */
6535 bfd_put_32 (output_bfd, 1, contents + 8);
6536 /* Only hash fn bloom filter. */
6537 bfd_put_32 (output_bfd, 0, contents + 12);
6538 /* No hashes are valid - empty bitmask. */
6539 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6540 /* No hashes in the only bucket. */
6541 bfd_put_32 (output_bfd, 0,
6542 contents + 16 + bed->s->arch_size / 8);
6543 }
6544 else
6545 {
9e6619e2 6546 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6547 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6548
9e6619e2
AM
6549 x = cinfo.nsyms;
6550 maskbitslog2 = 1;
6551 while ((x >>= 1) != 0)
6552 ++maskbitslog2;
fdc90cb4
JJ
6553 if (maskbitslog2 < 3)
6554 maskbitslog2 = 5;
6555 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6556 maskbitslog2 = maskbitslog2 + 3;
6557 else
6558 maskbitslog2 = maskbitslog2 + 2;
6559 if (bed->s->arch_size == 64)
6560 {
6561 if (maskbitslog2 == 5)
6562 maskbitslog2 = 6;
6563 cinfo.shift1 = 6;
6564 }
6565 else
6566 cinfo.shift1 = 5;
6567 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6568 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6569 cinfo.maskbits = 1 << maskbitslog2;
6570 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6571 amt = bucketcount * sizeof (unsigned long int) * 2;
6572 amt += maskwords * sizeof (bfd_vma);
a50b1753 6573 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6574 if (cinfo.bitmask == NULL)
6575 {
6576 free (cinfo.hashcodes);
6577 return FALSE;
6578 }
6579
a50b1753 6580 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6581 cinfo.indx = cinfo.counts + bucketcount;
6582 cinfo.symindx = dynsymcount - cinfo.nsyms;
6583 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6584
6585 /* Determine how often each hash bucket is used. */
6586 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6587 for (i = 0; i < cinfo.nsyms; ++i)
6588 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6589
6590 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6591 if (cinfo.counts[i] != 0)
6592 {
6593 cinfo.indx[i] = cnt;
6594 cnt += cinfo.counts[i];
6595 }
6596 BFD_ASSERT (cnt == dynsymcount);
6597 cinfo.bucketcount = bucketcount;
6598 cinfo.local_indx = cinfo.min_dynindx;
6599
6600 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6601 s->size += cinfo.maskbits / 8;
a50b1753 6602 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6603 if (contents == NULL)
6604 {
6605 free (cinfo.bitmask);
6606 free (cinfo.hashcodes);
6607 return FALSE;
6608 }
6609
6610 s->contents = contents;
6611 bfd_put_32 (output_bfd, bucketcount, contents);
6612 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6613 bfd_put_32 (output_bfd, maskwords, contents + 8);
6614 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6615 contents += 16 + cinfo.maskbits / 8;
6616
6617 for (i = 0; i < bucketcount; ++i)
6618 {
6619 if (cinfo.counts[i] == 0)
6620 bfd_put_32 (output_bfd, 0, contents);
6621 else
6622 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6623 contents += 4;
6624 }
6625
6626 cinfo.contents = contents;
6627
6628 /* Renumber dynamic symbols, populate .gnu.hash section. */
6629 elf_link_hash_traverse (elf_hash_table (info),
6630 elf_renumber_gnu_hash_syms, &cinfo);
6631
6632 contents = s->contents + 16;
6633 for (i = 0; i < maskwords; ++i)
6634 {
6635 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6636 contents);
6637 contents += bed->s->arch_size / 8;
6638 }
6639
6640 free (cinfo.bitmask);
6641 free (cinfo.hashcodes);
6642 }
6643 }
5a580b3a
AM
6644
6645 s = bfd_get_section_by_name (dynobj, ".dynstr");
6646 BFD_ASSERT (s != NULL);
6647
4ad4eba5 6648 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6649
eea6121a 6650 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6651
6652 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6653 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6654 return FALSE;
6655 }
6656
6657 return TRUE;
6658}
4d269e42
AM
6659\f
6660/* Indicate that we are only retrieving symbol values from this
6661 section. */
6662
6663void
6664_bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6665{
6666 if (is_elf_hash_table (info->hash))
6667 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6668 _bfd_generic_link_just_syms (sec, info);
6669}
6670
6671/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6672
6673static void
6674merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6675 asection *sec)
6676{
6677 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6678 sec->sec_info_type = ELF_INFO_TYPE_NONE;
6679}
6680
6681/* Finish SHF_MERGE section merging. */
6682
6683bfd_boolean
6684_bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6685{
6686 bfd *ibfd;
6687 asection *sec;
6688
6689 if (!is_elf_hash_table (info->hash))
6690 return FALSE;
6691
6692 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6693 if ((ibfd->flags & DYNAMIC) == 0)
6694 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6695 if ((sec->flags & SEC_MERGE) != 0
6696 && !bfd_is_abs_section (sec->output_section))
6697 {
6698 struct bfd_elf_section_data *secdata;
6699
6700 secdata = elf_section_data (sec);
6701 if (! _bfd_add_merge_section (abfd,
6702 &elf_hash_table (info)->merge_info,
6703 sec, &secdata->sec_info))
6704 return FALSE;
6705 else if (secdata->sec_info)
6706 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6707 }
6708
6709 if (elf_hash_table (info)->merge_info != NULL)
6710 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6711 merge_sections_remove_hook);
6712 return TRUE;
6713}
6714
6715/* Create an entry in an ELF linker hash table. */
6716
6717struct bfd_hash_entry *
6718_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6719 struct bfd_hash_table *table,
6720 const char *string)
6721{
6722 /* Allocate the structure if it has not already been allocated by a
6723 subclass. */
6724 if (entry == NULL)
6725 {
a50b1753
NC
6726 entry = (struct bfd_hash_entry *)
6727 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6728 if (entry == NULL)
6729 return entry;
6730 }
6731
6732 /* Call the allocation method of the superclass. */
6733 entry = _bfd_link_hash_newfunc (entry, table, string);
6734 if (entry != NULL)
6735 {
6736 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6737 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6738
6739 /* Set local fields. */
6740 ret->indx = -1;
6741 ret->dynindx = -1;
6742 ret->got = htab->init_got_refcount;
6743 ret->plt = htab->init_plt_refcount;
6744 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6745 - offsetof (struct elf_link_hash_entry, size)));
6746 /* Assume that we have been called by a non-ELF symbol reader.
6747 This flag is then reset by the code which reads an ELF input
6748 file. This ensures that a symbol created by a non-ELF symbol
6749 reader will have the flag set correctly. */
6750 ret->non_elf = 1;
6751 }
6752
6753 return entry;
6754}
6755
6756/* Copy data from an indirect symbol to its direct symbol, hiding the
6757 old indirect symbol. Also used for copying flags to a weakdef. */
6758
6759void
6760_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6761 struct elf_link_hash_entry *dir,
6762 struct elf_link_hash_entry *ind)
6763{
6764 struct elf_link_hash_table *htab;
6765
6766 /* Copy down any references that we may have already seen to the
6767 symbol which just became indirect. */
6768
6769 dir->ref_dynamic |= ind->ref_dynamic;
6770 dir->ref_regular |= ind->ref_regular;
6771 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6772 dir->non_got_ref |= ind->non_got_ref;
6773 dir->needs_plt |= ind->needs_plt;
6774 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6775
6776 if (ind->root.type != bfd_link_hash_indirect)
6777 return;
6778
6779 /* Copy over the global and procedure linkage table refcount entries.
6780 These may have been already set up by a check_relocs routine. */
6781 htab = elf_hash_table (info);
6782 if (ind->got.refcount > htab->init_got_refcount.refcount)
6783 {
6784 if (dir->got.refcount < 0)
6785 dir->got.refcount = 0;
6786 dir->got.refcount += ind->got.refcount;
6787 ind->got.refcount = htab->init_got_refcount.refcount;
6788 }
6789
6790 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6791 {
6792 if (dir->plt.refcount < 0)
6793 dir->plt.refcount = 0;
6794 dir->plt.refcount += ind->plt.refcount;
6795 ind->plt.refcount = htab->init_plt_refcount.refcount;
6796 }
6797
6798 if (ind->dynindx != -1)
6799 {
6800 if (dir->dynindx != -1)
6801 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6802 dir->dynindx = ind->dynindx;
6803 dir->dynstr_index = ind->dynstr_index;
6804 ind->dynindx = -1;
6805 ind->dynstr_index = 0;
6806 }
6807}
6808
6809void
6810_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6811 struct elf_link_hash_entry *h,
6812 bfd_boolean force_local)
6813{
3aa14d16
L
6814 /* STT_GNU_IFUNC symbol must go through PLT. */
6815 if (h->type != STT_GNU_IFUNC)
6816 {
6817 h->plt = elf_hash_table (info)->init_plt_offset;
6818 h->needs_plt = 0;
6819 }
4d269e42
AM
6820 if (force_local)
6821 {
6822 h->forced_local = 1;
6823 if (h->dynindx != -1)
6824 {
6825 h->dynindx = -1;
6826 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6827 h->dynstr_index);
6828 }
6829 }
6830}
6831
6832/* Initialize an ELF linker hash table. */
6833
6834bfd_boolean
6835_bfd_elf_link_hash_table_init
6836 (struct elf_link_hash_table *table,
6837 bfd *abfd,
6838 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6839 struct bfd_hash_table *,
6840 const char *),
4dfe6ac6
NC
6841 unsigned int entsize,
6842 enum elf_target_id target_id)
4d269e42
AM
6843{
6844 bfd_boolean ret;
6845 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6846
6847 memset (table, 0, sizeof * table);
6848 table->init_got_refcount.refcount = can_refcount - 1;
6849 table->init_plt_refcount.refcount = can_refcount - 1;
6850 table->init_got_offset.offset = -(bfd_vma) 1;
6851 table->init_plt_offset.offset = -(bfd_vma) 1;
6852 /* The first dynamic symbol is a dummy. */
6853 table->dynsymcount = 1;
6854
6855 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 6856
4d269e42 6857 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 6858 table->hash_table_id = target_id;
4d269e42
AM
6859
6860 return ret;
6861}
6862
6863/* Create an ELF linker hash table. */
6864
6865struct bfd_link_hash_table *
6866_bfd_elf_link_hash_table_create (bfd *abfd)
6867{
6868 struct elf_link_hash_table *ret;
6869 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6870
a50b1753 6871 ret = (struct elf_link_hash_table *) bfd_malloc (amt);
4d269e42
AM
6872 if (ret == NULL)
6873 return NULL;
6874
6875 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
6876 sizeof (struct elf_link_hash_entry),
6877 GENERIC_ELF_DATA))
4d269e42
AM
6878 {
6879 free (ret);
6880 return NULL;
6881 }
6882
6883 return &ret->root;
6884}
6885
6886/* This is a hook for the ELF emulation code in the generic linker to
6887 tell the backend linker what file name to use for the DT_NEEDED
6888 entry for a dynamic object. */
6889
6890void
6891bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6892{
6893 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6894 && bfd_get_format (abfd) == bfd_object)
6895 elf_dt_name (abfd) = name;
6896}
6897
6898int
6899bfd_elf_get_dyn_lib_class (bfd *abfd)
6900{
6901 int lib_class;
6902 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6903 && bfd_get_format (abfd) == bfd_object)
6904 lib_class = elf_dyn_lib_class (abfd);
6905 else
6906 lib_class = 0;
6907 return lib_class;
6908}
6909
6910void
6911bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6912{
6913 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6914 && bfd_get_format (abfd) == bfd_object)
6915 elf_dyn_lib_class (abfd) = lib_class;
6916}
6917
6918/* Get the list of DT_NEEDED entries for a link. This is a hook for
6919 the linker ELF emulation code. */
6920
6921struct bfd_link_needed_list *
6922bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6923 struct bfd_link_info *info)
6924{
6925 if (! is_elf_hash_table (info->hash))
6926 return NULL;
6927 return elf_hash_table (info)->needed;
6928}
6929
6930/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6931 hook for the linker ELF emulation code. */
6932
6933struct bfd_link_needed_list *
6934bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6935 struct bfd_link_info *info)
6936{
6937 if (! is_elf_hash_table (info->hash))
6938 return NULL;
6939 return elf_hash_table (info)->runpath;
6940}
6941
6942/* Get the name actually used for a dynamic object for a link. This
6943 is the SONAME entry if there is one. Otherwise, it is the string
6944 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6945
6946const char *
6947bfd_elf_get_dt_soname (bfd *abfd)
6948{
6949 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6950 && bfd_get_format (abfd) == bfd_object)
6951 return elf_dt_name (abfd);
6952 return NULL;
6953}
6954
6955/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6956 the ELF linker emulation code. */
6957
6958bfd_boolean
6959bfd_elf_get_bfd_needed_list (bfd *abfd,
6960 struct bfd_link_needed_list **pneeded)
6961{
6962 asection *s;
6963 bfd_byte *dynbuf = NULL;
cb33740c 6964 unsigned int elfsec;
4d269e42
AM
6965 unsigned long shlink;
6966 bfd_byte *extdyn, *extdynend;
6967 size_t extdynsize;
6968 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6969
6970 *pneeded = NULL;
6971
6972 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6973 || bfd_get_format (abfd) != bfd_object)
6974 return TRUE;
6975
6976 s = bfd_get_section_by_name (abfd, ".dynamic");
6977 if (s == NULL || s->size == 0)
6978 return TRUE;
6979
6980 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6981 goto error_return;
6982
6983 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 6984 if (elfsec == SHN_BAD)
4d269e42
AM
6985 goto error_return;
6986
6987 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 6988
4d269e42
AM
6989 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6990 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6991
6992 extdyn = dynbuf;
6993 extdynend = extdyn + s->size;
6994 for (; extdyn < extdynend; extdyn += extdynsize)
6995 {
6996 Elf_Internal_Dyn dyn;
6997
6998 (*swap_dyn_in) (abfd, extdyn, &dyn);
6999
7000 if (dyn.d_tag == DT_NULL)
7001 break;
7002
7003 if (dyn.d_tag == DT_NEEDED)
7004 {
7005 const char *string;
7006 struct bfd_link_needed_list *l;
7007 unsigned int tagv = dyn.d_un.d_val;
7008 bfd_size_type amt;
7009
7010 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7011 if (string == NULL)
7012 goto error_return;
7013
7014 amt = sizeof *l;
a50b1753 7015 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7016 if (l == NULL)
7017 goto error_return;
7018
7019 l->by = abfd;
7020 l->name = string;
7021 l->next = *pneeded;
7022 *pneeded = l;
7023 }
7024 }
7025
7026 free (dynbuf);
7027
7028 return TRUE;
7029
7030 error_return:
7031 if (dynbuf != NULL)
7032 free (dynbuf);
7033 return FALSE;
7034}
7035
7036struct elf_symbuf_symbol
7037{
7038 unsigned long st_name; /* Symbol name, index in string tbl */
7039 unsigned char st_info; /* Type and binding attributes */
7040 unsigned char st_other; /* Visibilty, and target specific */
7041};
7042
7043struct elf_symbuf_head
7044{
7045 struct elf_symbuf_symbol *ssym;
7046 bfd_size_type count;
7047 unsigned int st_shndx;
7048};
7049
7050struct elf_symbol
7051{
7052 union
7053 {
7054 Elf_Internal_Sym *isym;
7055 struct elf_symbuf_symbol *ssym;
7056 } u;
7057 const char *name;
7058};
7059
7060/* Sort references to symbols by ascending section number. */
7061
7062static int
7063elf_sort_elf_symbol (const void *arg1, const void *arg2)
7064{
7065 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7066 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7067
7068 return s1->st_shndx - s2->st_shndx;
7069}
7070
7071static int
7072elf_sym_name_compare (const void *arg1, const void *arg2)
7073{
7074 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7075 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7076 return strcmp (s1->name, s2->name);
7077}
7078
7079static struct elf_symbuf_head *
7080elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7081{
14b1c01e 7082 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7083 struct elf_symbuf_symbol *ssym;
7084 struct elf_symbuf_head *ssymbuf, *ssymhead;
3ae181ee 7085 bfd_size_type i, shndx_count, total_size;
4d269e42 7086
a50b1753 7087 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7088 if (indbuf == NULL)
7089 return NULL;
7090
7091 for (ind = indbuf, i = 0; i < symcount; i++)
7092 if (isymbuf[i].st_shndx != SHN_UNDEF)
7093 *ind++ = &isymbuf[i];
7094 indbufend = ind;
7095
7096 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7097 elf_sort_elf_symbol);
7098
7099 shndx_count = 0;
7100 if (indbufend > indbuf)
7101 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7102 if (ind[0]->st_shndx != ind[1]->st_shndx)
7103 shndx_count++;
7104
3ae181ee
L
7105 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7106 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7107 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7108 if (ssymbuf == NULL)
7109 {
7110 free (indbuf);
7111 return NULL;
7112 }
7113
3ae181ee 7114 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7115 ssymbuf->ssym = NULL;
7116 ssymbuf->count = shndx_count;
7117 ssymbuf->st_shndx = 0;
7118 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7119 {
7120 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7121 {
7122 ssymhead++;
7123 ssymhead->ssym = ssym;
7124 ssymhead->count = 0;
7125 ssymhead->st_shndx = (*ind)->st_shndx;
7126 }
7127 ssym->st_name = (*ind)->st_name;
7128 ssym->st_info = (*ind)->st_info;
7129 ssym->st_other = (*ind)->st_other;
7130 ssymhead->count++;
7131 }
3ae181ee
L
7132 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7133 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7134 == total_size));
4d269e42
AM
7135
7136 free (indbuf);
7137 return ssymbuf;
7138}
7139
7140/* Check if 2 sections define the same set of local and global
7141 symbols. */
7142
8f317e31 7143static bfd_boolean
4d269e42
AM
7144bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7145 struct bfd_link_info *info)
7146{
7147 bfd *bfd1, *bfd2;
7148 const struct elf_backend_data *bed1, *bed2;
7149 Elf_Internal_Shdr *hdr1, *hdr2;
7150 bfd_size_type symcount1, symcount2;
7151 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7152 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7153 Elf_Internal_Sym *isym, *isymend;
7154 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7155 bfd_size_type count1, count2, i;
cb33740c 7156 unsigned int shndx1, shndx2;
4d269e42
AM
7157 bfd_boolean result;
7158
7159 bfd1 = sec1->owner;
7160 bfd2 = sec2->owner;
7161
4d269e42
AM
7162 /* Both sections have to be in ELF. */
7163 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7164 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7165 return FALSE;
7166
7167 if (elf_section_type (sec1) != elf_section_type (sec2))
7168 return FALSE;
7169
4d269e42
AM
7170 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7171 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7172 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7173 return FALSE;
7174
7175 bed1 = get_elf_backend_data (bfd1);
7176 bed2 = get_elf_backend_data (bfd2);
7177 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7178 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7179 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7180 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7181
7182 if (symcount1 == 0 || symcount2 == 0)
7183 return FALSE;
7184
7185 result = FALSE;
7186 isymbuf1 = NULL;
7187 isymbuf2 = NULL;
a50b1753
NC
7188 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7189 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7190
7191 if (ssymbuf1 == NULL)
7192 {
7193 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7194 NULL, NULL, NULL);
7195 if (isymbuf1 == NULL)
7196 goto done;
7197
7198 if (!info->reduce_memory_overheads)
7199 elf_tdata (bfd1)->symbuf = ssymbuf1
7200 = elf_create_symbuf (symcount1, isymbuf1);
7201 }
7202
7203 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7204 {
7205 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7206 NULL, NULL, NULL);
7207 if (isymbuf2 == NULL)
7208 goto done;
7209
7210 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7211 elf_tdata (bfd2)->symbuf = ssymbuf2
7212 = elf_create_symbuf (symcount2, isymbuf2);
7213 }
7214
7215 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7216 {
7217 /* Optimized faster version. */
7218 bfd_size_type lo, hi, mid;
7219 struct elf_symbol *symp;
7220 struct elf_symbuf_symbol *ssym, *ssymend;
7221
7222 lo = 0;
7223 hi = ssymbuf1->count;
7224 ssymbuf1++;
7225 count1 = 0;
7226 while (lo < hi)
7227 {
7228 mid = (lo + hi) / 2;
cb33740c 7229 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7230 hi = mid;
cb33740c 7231 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7232 lo = mid + 1;
7233 else
7234 {
7235 count1 = ssymbuf1[mid].count;
7236 ssymbuf1 += mid;
7237 break;
7238 }
7239 }
7240
7241 lo = 0;
7242 hi = ssymbuf2->count;
7243 ssymbuf2++;
7244 count2 = 0;
7245 while (lo < hi)
7246 {
7247 mid = (lo + hi) / 2;
cb33740c 7248 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7249 hi = mid;
cb33740c 7250 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7251 lo = mid + 1;
7252 else
7253 {
7254 count2 = ssymbuf2[mid].count;
7255 ssymbuf2 += mid;
7256 break;
7257 }
7258 }
7259
7260 if (count1 == 0 || count2 == 0 || count1 != count2)
7261 goto done;
7262
a50b1753
NC
7263 symtable1 = (struct elf_symbol *)
7264 bfd_malloc (count1 * sizeof (struct elf_symbol));
7265 symtable2 = (struct elf_symbol *)
7266 bfd_malloc (count2 * sizeof (struct elf_symbol));
4d269e42
AM
7267 if (symtable1 == NULL || symtable2 == NULL)
7268 goto done;
7269
7270 symp = symtable1;
7271 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7272 ssym < ssymend; ssym++, symp++)
7273 {
7274 symp->u.ssym = ssym;
7275 symp->name = bfd_elf_string_from_elf_section (bfd1,
7276 hdr1->sh_link,
7277 ssym->st_name);
7278 }
7279
7280 symp = symtable2;
7281 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7282 ssym < ssymend; ssym++, symp++)
7283 {
7284 symp->u.ssym = ssym;
7285 symp->name = bfd_elf_string_from_elf_section (bfd2,
7286 hdr2->sh_link,
7287 ssym->st_name);
7288 }
7289
7290 /* Sort symbol by name. */
7291 qsort (symtable1, count1, sizeof (struct elf_symbol),
7292 elf_sym_name_compare);
7293 qsort (symtable2, count1, sizeof (struct elf_symbol),
7294 elf_sym_name_compare);
7295
7296 for (i = 0; i < count1; i++)
7297 /* Two symbols must have the same binding, type and name. */
7298 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7299 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7300 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7301 goto done;
7302
7303 result = TRUE;
7304 goto done;
7305 }
7306
a50b1753
NC
7307 symtable1 = (struct elf_symbol *)
7308 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7309 symtable2 = (struct elf_symbol *)
7310 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7311 if (symtable1 == NULL || symtable2 == NULL)
7312 goto done;
7313
7314 /* Count definitions in the section. */
7315 count1 = 0;
7316 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7317 if (isym->st_shndx == shndx1)
4d269e42
AM
7318 symtable1[count1++].u.isym = isym;
7319
7320 count2 = 0;
7321 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7322 if (isym->st_shndx == shndx2)
4d269e42
AM
7323 symtable2[count2++].u.isym = isym;
7324
7325 if (count1 == 0 || count2 == 0 || count1 != count2)
7326 goto done;
7327
7328 for (i = 0; i < count1; i++)
7329 symtable1[i].name
7330 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7331 symtable1[i].u.isym->st_name);
7332
7333 for (i = 0; i < count2; i++)
7334 symtable2[i].name
7335 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7336 symtable2[i].u.isym->st_name);
7337
7338 /* Sort symbol by name. */
7339 qsort (symtable1, count1, sizeof (struct elf_symbol),
7340 elf_sym_name_compare);
7341 qsort (symtable2, count1, sizeof (struct elf_symbol),
7342 elf_sym_name_compare);
7343
7344 for (i = 0; i < count1; i++)
7345 /* Two symbols must have the same binding, type and name. */
7346 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7347 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7348 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7349 goto done;
7350
7351 result = TRUE;
7352
7353done:
7354 if (symtable1)
7355 free (symtable1);
7356 if (symtable2)
7357 free (symtable2);
7358 if (isymbuf1)
7359 free (isymbuf1);
7360 if (isymbuf2)
7361 free (isymbuf2);
7362
7363 return result;
7364}
7365
7366/* Return TRUE if 2 section types are compatible. */
7367
7368bfd_boolean
7369_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7370 bfd *bbfd, const asection *bsec)
7371{
7372 if (asec == NULL
7373 || bsec == NULL
7374 || abfd->xvec->flavour != bfd_target_elf_flavour
7375 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7376 return TRUE;
7377
7378 return elf_section_type (asec) == elf_section_type (bsec);
7379}
7380\f
c152c796
AM
7381/* Final phase of ELF linker. */
7382
7383/* A structure we use to avoid passing large numbers of arguments. */
7384
7385struct elf_final_link_info
7386{
7387 /* General link information. */
7388 struct bfd_link_info *info;
7389 /* Output BFD. */
7390 bfd *output_bfd;
7391 /* Symbol string table. */
7392 struct bfd_strtab_hash *symstrtab;
7393 /* .dynsym section. */
7394 asection *dynsym_sec;
7395 /* .hash section. */
7396 asection *hash_sec;
7397 /* symbol version section (.gnu.version). */
7398 asection *symver_sec;
7399 /* Buffer large enough to hold contents of any section. */
7400 bfd_byte *contents;
7401 /* Buffer large enough to hold external relocs of any section. */
7402 void *external_relocs;
7403 /* Buffer large enough to hold internal relocs of any section. */
7404 Elf_Internal_Rela *internal_relocs;
7405 /* Buffer large enough to hold external local symbols of any input
7406 BFD. */
7407 bfd_byte *external_syms;
7408 /* And a buffer for symbol section indices. */
7409 Elf_External_Sym_Shndx *locsym_shndx;
7410 /* Buffer large enough to hold internal local symbols of any input
7411 BFD. */
7412 Elf_Internal_Sym *internal_syms;
7413 /* Array large enough to hold a symbol index for each local symbol
7414 of any input BFD. */
7415 long *indices;
7416 /* Array large enough to hold a section pointer for each local
7417 symbol of any input BFD. */
7418 asection **sections;
7419 /* Buffer to hold swapped out symbols. */
7420 bfd_byte *symbuf;
7421 /* And one for symbol section indices. */
7422 Elf_External_Sym_Shndx *symshndxbuf;
7423 /* Number of swapped out symbols in buffer. */
7424 size_t symbuf_count;
7425 /* Number of symbols which fit in symbuf. */
7426 size_t symbuf_size;
7427 /* And same for symshndxbuf. */
7428 size_t shndxbuf_size;
7429};
7430
7431/* This struct is used to pass information to elf_link_output_extsym. */
7432
7433struct elf_outext_info
7434{
7435 bfd_boolean failed;
7436 bfd_boolean localsyms;
7437 struct elf_final_link_info *finfo;
7438};
7439
d9352518
DB
7440
7441/* Support for evaluating a complex relocation.
7442
7443 Complex relocations are generalized, self-describing relocations. The
7444 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7445 relocations themselves.
d9352518
DB
7446
7447 The relocations are use a reserved elf-wide relocation type code (R_RELC
7448 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7449 information (start bit, end bit, word width, etc) into the addend. This
7450 information is extracted from CGEN-generated operand tables within gas.
7451
7452 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7453 internal) representing prefix-notation expressions, including but not
7454 limited to those sorts of expressions normally encoded as addends in the
7455 addend field. The symbol mangling format is:
7456
7457 <node> := <literal>
7458 | <unary-operator> ':' <node>
7459 | <binary-operator> ':' <node> ':' <node>
7460 ;
7461
7462 <literal> := 's' <digits=N> ':' <N character symbol name>
7463 | 'S' <digits=N> ':' <N character section name>
7464 | '#' <hexdigits>
7465 ;
7466
7467 <binary-operator> := as in C
7468 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7469
7470static void
a0c8462f
AM
7471set_symbol_value (bfd *bfd_with_globals,
7472 Elf_Internal_Sym *isymbuf,
7473 size_t locsymcount,
7474 size_t symidx,
7475 bfd_vma val)
d9352518 7476{
8977835c
AM
7477 struct elf_link_hash_entry **sym_hashes;
7478 struct elf_link_hash_entry *h;
7479 size_t extsymoff = locsymcount;
d9352518 7480
8977835c 7481 if (symidx < locsymcount)
d9352518 7482 {
8977835c
AM
7483 Elf_Internal_Sym *sym;
7484
7485 sym = isymbuf + symidx;
7486 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7487 {
7488 /* It is a local symbol: move it to the
7489 "absolute" section and give it a value. */
7490 sym->st_shndx = SHN_ABS;
7491 sym->st_value = val;
7492 return;
7493 }
7494 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7495 extsymoff = 0;
d9352518 7496 }
8977835c
AM
7497
7498 /* It is a global symbol: set its link type
7499 to "defined" and give it a value. */
7500
7501 sym_hashes = elf_sym_hashes (bfd_with_globals);
7502 h = sym_hashes [symidx - extsymoff];
7503 while (h->root.type == bfd_link_hash_indirect
7504 || h->root.type == bfd_link_hash_warning)
7505 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7506 h->root.type = bfd_link_hash_defined;
7507 h->root.u.def.value = val;
7508 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7509}
7510
a0c8462f
AM
7511static bfd_boolean
7512resolve_symbol (const char *name,
7513 bfd *input_bfd,
7514 struct elf_final_link_info *finfo,
7515 bfd_vma *result,
7516 Elf_Internal_Sym *isymbuf,
7517 size_t locsymcount)
d9352518 7518{
a0c8462f
AM
7519 Elf_Internal_Sym *sym;
7520 struct bfd_link_hash_entry *global_entry;
7521 const char *candidate = NULL;
7522 Elf_Internal_Shdr *symtab_hdr;
7523 size_t i;
7524
d9352518
DB
7525 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7526
7527 for (i = 0; i < locsymcount; ++ i)
7528 {
8977835c 7529 sym = isymbuf + i;
d9352518
DB
7530
7531 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7532 continue;
7533
7534 candidate = bfd_elf_string_from_elf_section (input_bfd,
7535 symtab_hdr->sh_link,
7536 sym->st_name);
7537#ifdef DEBUG
0f02bbd9
AM
7538 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7539 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7540#endif
7541 if (candidate && strcmp (candidate, name) == 0)
7542 {
0f02bbd9 7543 asection *sec = finfo->sections [i];
d9352518 7544
0f02bbd9
AM
7545 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7546 *result += sec->output_offset + sec->output_section->vma;
d9352518 7547#ifdef DEBUG
0f02bbd9
AM
7548 printf ("Found symbol with value %8.8lx\n",
7549 (unsigned long) *result);
d9352518
DB
7550#endif
7551 return TRUE;
7552 }
7553 }
7554
7555 /* Hmm, haven't found it yet. perhaps it is a global. */
a0c8462f
AM
7556 global_entry = bfd_link_hash_lookup (finfo->info->hash, name,
7557 FALSE, FALSE, TRUE);
d9352518
DB
7558 if (!global_entry)
7559 return FALSE;
a0c8462f 7560
d9352518
DB
7561 if (global_entry->type == bfd_link_hash_defined
7562 || global_entry->type == bfd_link_hash_defweak)
7563 {
a0c8462f
AM
7564 *result = (global_entry->u.def.value
7565 + global_entry->u.def.section->output_section->vma
7566 + global_entry->u.def.section->output_offset);
d9352518 7567#ifdef DEBUG
0f02bbd9
AM
7568 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7569 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7570#endif
7571 return TRUE;
a0c8462f 7572 }
d9352518 7573
d9352518
DB
7574 return FALSE;
7575}
7576
7577static bfd_boolean
a0c8462f
AM
7578resolve_section (const char *name,
7579 asection *sections,
7580 bfd_vma *result)
d9352518 7581{
a0c8462f
AM
7582 asection *curr;
7583 unsigned int len;
d9352518 7584
a0c8462f 7585 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7586 if (strcmp (curr->name, name) == 0)
7587 {
7588 *result = curr->vma;
7589 return TRUE;
7590 }
7591
7592 /* Hmm. still haven't found it. try pseudo-section names. */
a0c8462f 7593 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7594 {
7595 len = strlen (curr->name);
a0c8462f 7596 if (len > strlen (name))
d9352518
DB
7597 continue;
7598
7599 if (strncmp (curr->name, name, len) == 0)
7600 {
7601 if (strncmp (".end", name + len, 4) == 0)
7602 {
7603 *result = curr->vma + curr->size;
7604 return TRUE;
7605 }
7606
7607 /* Insert more pseudo-section names here, if you like. */
7608 }
7609 }
a0c8462f 7610
d9352518
DB
7611 return FALSE;
7612}
7613
7614static void
a0c8462f 7615undefined_reference (const char *reftype, const char *name)
d9352518 7616{
a0c8462f
AM
7617 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7618 reftype, name);
d9352518
DB
7619}
7620
7621static bfd_boolean
a0c8462f
AM
7622eval_symbol (bfd_vma *result,
7623 const char **symp,
7624 bfd *input_bfd,
7625 struct elf_final_link_info *finfo,
7626 bfd_vma dot,
7627 Elf_Internal_Sym *isymbuf,
7628 size_t locsymcount,
7629 int signed_p)
d9352518 7630{
4b93929b
NC
7631 size_t len;
7632 size_t symlen;
a0c8462f
AM
7633 bfd_vma a;
7634 bfd_vma b;
4b93929b 7635 char symbuf[4096];
0f02bbd9 7636 const char *sym = *symp;
a0c8462f
AM
7637 const char *symend;
7638 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7639
7640 len = strlen (sym);
7641 symend = sym + len;
7642
4b93929b 7643 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7644 {
7645 bfd_set_error (bfd_error_invalid_operation);
7646 return FALSE;
7647 }
a0c8462f 7648
d9352518
DB
7649 switch (* sym)
7650 {
7651 case '.':
0f02bbd9
AM
7652 *result = dot;
7653 *symp = sym + 1;
d9352518
DB
7654 return TRUE;
7655
7656 case '#':
0f02bbd9
AM
7657 ++sym;
7658 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7659 return TRUE;
7660
7661 case 'S':
7662 symbol_is_section = TRUE;
a0c8462f 7663 case 's':
0f02bbd9
AM
7664 ++sym;
7665 symlen = strtol (sym, (char **) symp, 10);
7666 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7667
4b93929b 7668 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7669 {
7670 bfd_set_error (bfd_error_invalid_operation);
7671 return FALSE;
7672 }
7673
7674 memcpy (symbuf, sym, symlen);
a0c8462f 7675 symbuf[symlen] = '\0';
0f02bbd9 7676 *symp = sym + symlen;
a0c8462f
AM
7677
7678 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7679 the symbol as a section, or vice-versa. so we're pretty liberal in our
7680 interpretation here; section means "try section first", not "must be a
7681 section", and likewise with symbol. */
7682
a0c8462f 7683 if (symbol_is_section)
d9352518 7684 {
8977835c
AM
7685 if (!resolve_section (symbuf, finfo->output_bfd->sections, result)
7686 && !resolve_symbol (symbuf, input_bfd, finfo, result,
7687 isymbuf, locsymcount))
d9352518
DB
7688 {
7689 undefined_reference ("section", symbuf);
7690 return FALSE;
7691 }
a0c8462f
AM
7692 }
7693 else
d9352518 7694 {
8977835c
AM
7695 if (!resolve_symbol (symbuf, input_bfd, finfo, result,
7696 isymbuf, locsymcount)
7697 && !resolve_section (symbuf, finfo->output_bfd->sections,
7698 result))
d9352518
DB
7699 {
7700 undefined_reference ("symbol", symbuf);
7701 return FALSE;
7702 }
7703 }
7704
7705 return TRUE;
a0c8462f 7706
d9352518
DB
7707 /* All that remains are operators. */
7708
7709#define UNARY_OP(op) \
7710 if (strncmp (sym, #op, strlen (#op)) == 0) \
7711 { \
7712 sym += strlen (#op); \
a0c8462f
AM
7713 if (*sym == ':') \
7714 ++sym; \
0f02bbd9
AM
7715 *symp = sym; \
7716 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7717 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7718 return FALSE; \
7719 if (signed_p) \
0f02bbd9 7720 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7721 else \
7722 *result = op a; \
d9352518
DB
7723 return TRUE; \
7724 }
7725
7726#define BINARY_OP(op) \
7727 if (strncmp (sym, #op, strlen (#op)) == 0) \
7728 { \
7729 sym += strlen (#op); \
a0c8462f
AM
7730 if (*sym == ':') \
7731 ++sym; \
0f02bbd9
AM
7732 *symp = sym; \
7733 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7734 isymbuf, locsymcount, signed_p)) \
a0c8462f 7735 return FALSE; \
0f02bbd9
AM
7736 ++*symp; \
7737 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7738 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7739 return FALSE; \
7740 if (signed_p) \
0f02bbd9 7741 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
7742 else \
7743 *result = a op b; \
d9352518
DB
7744 return TRUE; \
7745 }
7746
7747 default:
7748 UNARY_OP (0-);
7749 BINARY_OP (<<);
7750 BINARY_OP (>>);
7751 BINARY_OP (==);
7752 BINARY_OP (!=);
7753 BINARY_OP (<=);
7754 BINARY_OP (>=);
7755 BINARY_OP (&&);
7756 BINARY_OP (||);
7757 UNARY_OP (~);
7758 UNARY_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 BINARY_OP (<);
7768 BINARY_OP (>);
7769#undef UNARY_OP
7770#undef BINARY_OP
7771 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7772 bfd_set_error (bfd_error_invalid_operation);
7773 return FALSE;
7774 }
7775}
7776
d9352518 7777static void
a0c8462f
AM
7778put_value (bfd_vma size,
7779 unsigned long chunksz,
7780 bfd *input_bfd,
7781 bfd_vma x,
7782 bfd_byte *location)
d9352518
DB
7783{
7784 location += (size - chunksz);
7785
a0c8462f 7786 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
d9352518
DB
7787 {
7788 switch (chunksz)
7789 {
7790 default:
7791 case 0:
7792 abort ();
7793 case 1:
7794 bfd_put_8 (input_bfd, x, location);
7795 break;
7796 case 2:
7797 bfd_put_16 (input_bfd, x, location);
7798 break;
7799 case 4:
7800 bfd_put_32 (input_bfd, x, location);
7801 break;
7802 case 8:
7803#ifdef BFD64
7804 bfd_put_64 (input_bfd, x, location);
7805#else
7806 abort ();
7807#endif
7808 break;
7809 }
7810 }
7811}
7812
a0c8462f
AM
7813static bfd_vma
7814get_value (bfd_vma size,
7815 unsigned long chunksz,
7816 bfd *input_bfd,
7817 bfd_byte *location)
d9352518
DB
7818{
7819 bfd_vma x = 0;
7820
a0c8462f 7821 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
7822 {
7823 switch (chunksz)
7824 {
7825 default:
7826 case 0:
7827 abort ();
7828 case 1:
7829 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7830 break;
7831 case 2:
7832 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7833 break;
7834 case 4:
7835 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7836 break;
7837 case 8:
7838#ifdef BFD64
7839 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7840#else
7841 abort ();
7842#endif
7843 break;
7844 }
7845 }
7846 return x;
7847}
7848
a0c8462f
AM
7849static void
7850decode_complex_addend (unsigned long *start, /* in bits */
7851 unsigned long *oplen, /* in bits */
7852 unsigned long *len, /* in bits */
7853 unsigned long *wordsz, /* in bytes */
7854 unsigned long *chunksz, /* in bytes */
7855 unsigned long *lsb0_p,
7856 unsigned long *signed_p,
7857 unsigned long *trunc_p,
7858 unsigned long encoded)
d9352518
DB
7859{
7860 * start = encoded & 0x3F;
7861 * len = (encoded >> 6) & 0x3F;
7862 * oplen = (encoded >> 12) & 0x3F;
7863 * wordsz = (encoded >> 18) & 0xF;
7864 * chunksz = (encoded >> 22) & 0xF;
7865 * lsb0_p = (encoded >> 27) & 1;
7866 * signed_p = (encoded >> 28) & 1;
7867 * trunc_p = (encoded >> 29) & 1;
7868}
7869
cdfeee4f 7870bfd_reloc_status_type
0f02bbd9 7871bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 7872 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
7873 bfd_byte *contents,
7874 Elf_Internal_Rela *rel,
7875 bfd_vma relocation)
d9352518 7876{
0f02bbd9
AM
7877 bfd_vma shift, x, mask;
7878 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 7879 bfd_reloc_status_type r;
d9352518
DB
7880
7881 /* Perform this reloc, since it is complex.
7882 (this is not to say that it necessarily refers to a complex
7883 symbol; merely that it is a self-describing CGEN based reloc.
7884 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 7885 word size, etc) encoded within it.). */
d9352518 7886
a0c8462f
AM
7887 decode_complex_addend (&start, &oplen, &len, &wordsz,
7888 &chunksz, &lsb0_p, &signed_p,
7889 &trunc_p, rel->r_addend);
d9352518
DB
7890
7891 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7892
7893 if (lsb0_p)
7894 shift = (start + 1) - len;
7895 else
7896 shift = (8 * wordsz) - (start + len);
7897
5dabe785 7898 /* FIXME: octets_per_byte. */
a0c8462f 7899 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
d9352518
DB
7900
7901#ifdef DEBUG
7902 printf ("Doing complex reloc: "
7903 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7904 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7905 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7906 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
7907 oplen, (unsigned long) x, (unsigned long) mask,
7908 (unsigned long) relocation);
d9352518
DB
7909#endif
7910
cdfeee4f 7911 r = bfd_reloc_ok;
d9352518 7912 if (! trunc_p)
cdfeee4f
AM
7913 /* Now do an overflow check. */
7914 r = bfd_check_overflow ((signed_p
7915 ? complain_overflow_signed
7916 : complain_overflow_unsigned),
7917 len, 0, (8 * wordsz),
7918 relocation);
a0c8462f 7919
d9352518
DB
7920 /* Do the deed. */
7921 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7922
7923#ifdef DEBUG
7924 printf (" relocation: %8.8lx\n"
7925 " shifted mask: %8.8lx\n"
7926 " shifted/masked reloc: %8.8lx\n"
7927 " result: %8.8lx\n",
9ccb8af9
AM
7928 (unsigned long) relocation, (unsigned long) (mask << shift),
7929 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 7930#endif
5dabe785 7931 /* FIXME: octets_per_byte. */
d9352518 7932 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
cdfeee4f 7933 return r;
d9352518
DB
7934}
7935
c152c796
AM
7936/* When performing a relocatable link, the input relocations are
7937 preserved. But, if they reference global symbols, the indices
d4730f92
BS
7938 referenced must be updated. Update all the relocations found in
7939 RELDATA. */
c152c796
AM
7940
7941static void
7942elf_link_adjust_relocs (bfd *abfd,
d4730f92 7943 struct bfd_elf_section_reloc_data *reldata)
c152c796
AM
7944{
7945 unsigned int i;
7946 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7947 bfd_byte *erela;
7948 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7949 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7950 bfd_vma r_type_mask;
7951 int r_sym_shift;
d4730f92
BS
7952 unsigned int count = reldata->count;
7953 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 7954
d4730f92 7955 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
7956 {
7957 swap_in = bed->s->swap_reloc_in;
7958 swap_out = bed->s->swap_reloc_out;
7959 }
d4730f92 7960 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
7961 {
7962 swap_in = bed->s->swap_reloca_in;
7963 swap_out = bed->s->swap_reloca_out;
7964 }
7965 else
7966 abort ();
7967
7968 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7969 abort ();
7970
7971 if (bed->s->arch_size == 32)
7972 {
7973 r_type_mask = 0xff;
7974 r_sym_shift = 8;
7975 }
7976 else
7977 {
7978 r_type_mask = 0xffffffff;
7979 r_sym_shift = 32;
7980 }
7981
d4730f92
BS
7982 erela = reldata->hdr->contents;
7983 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
7984 {
7985 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7986 unsigned int j;
7987
7988 if (*rel_hash == NULL)
7989 continue;
7990
7991 BFD_ASSERT ((*rel_hash)->indx >= 0);
7992
7993 (*swap_in) (abfd, erela, irela);
7994 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7995 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7996 | (irela[j].r_info & r_type_mask));
7997 (*swap_out) (abfd, irela, erela);
7998 }
7999}
8000
8001struct elf_link_sort_rela
8002{
8003 union {
8004 bfd_vma offset;
8005 bfd_vma sym_mask;
8006 } u;
8007 enum elf_reloc_type_class type;
8008 /* We use this as an array of size int_rels_per_ext_rel. */
8009 Elf_Internal_Rela rela[1];
8010};
8011
8012static int
8013elf_link_sort_cmp1 (const void *A, const void *B)
8014{
a50b1753
NC
8015 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8016 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8017 int relativea, relativeb;
8018
8019 relativea = a->type == reloc_class_relative;
8020 relativeb = b->type == reloc_class_relative;
8021
8022 if (relativea < relativeb)
8023 return 1;
8024 if (relativea > relativeb)
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_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8029 return 1;
8030 if (a->rela->r_offset < b->rela->r_offset)
8031 return -1;
8032 if (a->rela->r_offset > b->rela->r_offset)
8033 return 1;
8034 return 0;
8035}
8036
8037static int
8038elf_link_sort_cmp2 (const void *A, const void *B)
8039{
a50b1753
NC
8040 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8041 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8042 int copya, copyb;
8043
8044 if (a->u.offset < b->u.offset)
8045 return -1;
8046 if (a->u.offset > b->u.offset)
8047 return 1;
8048 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
8049 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
8050 if (copya < copyb)
8051 return -1;
8052 if (copya > copyb)
8053 return 1;
8054 if (a->rela->r_offset < b->rela->r_offset)
8055 return -1;
8056 if (a->rela->r_offset > b->rela->r_offset)
8057 return 1;
8058 return 0;
8059}
8060
8061static size_t
8062elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8063{
3410fea8 8064 asection *dynamic_relocs;
fc66a176
L
8065 asection *rela_dyn;
8066 asection *rel_dyn;
c152c796
AM
8067 bfd_size_type count, size;
8068 size_t i, ret, sort_elt, ext_size;
8069 bfd_byte *sort, *s_non_relative, *p;
8070 struct elf_link_sort_rela *sq;
8071 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8072 int i2e = bed->s->int_rels_per_ext_rel;
8073 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8074 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8075 struct bfd_link_order *lo;
8076 bfd_vma r_sym_mask;
3410fea8 8077 bfd_boolean use_rela;
c152c796 8078
3410fea8
NC
8079 /* Find a dynamic reloc section. */
8080 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8081 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8082 if (rela_dyn != NULL && rela_dyn->size > 0
8083 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8084 {
3410fea8
NC
8085 bfd_boolean use_rela_initialised = FALSE;
8086
8087 /* This is just here to stop gcc from complaining.
8088 It's initialization checking code is not perfect. */
8089 use_rela = TRUE;
8090
8091 /* Both sections are present. Examine the sizes
8092 of the indirect sections to help us choose. */
8093 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8094 if (lo->type == bfd_indirect_link_order)
8095 {
8096 asection *o = lo->u.indirect.section;
8097
8098 if ((o->size % bed->s->sizeof_rela) == 0)
8099 {
8100 if ((o->size % bed->s->sizeof_rel) == 0)
8101 /* Section size is divisible by both rel and rela sizes.
8102 It is of no help to us. */
8103 ;
8104 else
8105 {
8106 /* Section size is only divisible by rela. */
8107 if (use_rela_initialised && (use_rela == FALSE))
8108 {
8109 _bfd_error_handler
8110 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8111 bfd_set_error (bfd_error_invalid_operation);
8112 return 0;
8113 }
8114 else
8115 {
8116 use_rela = TRUE;
8117 use_rela_initialised = TRUE;
8118 }
8119 }
8120 }
8121 else if ((o->size % bed->s->sizeof_rel) == 0)
8122 {
8123 /* Section size is only divisible by rel. */
8124 if (use_rela_initialised && (use_rela == TRUE))
8125 {
8126 _bfd_error_handler
8127 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8128 bfd_set_error (bfd_error_invalid_operation);
8129 return 0;
8130 }
8131 else
8132 {
8133 use_rela = FALSE;
8134 use_rela_initialised = TRUE;
8135 }
8136 }
8137 else
8138 {
8139 /* The section size is not divisible by either - something is wrong. */
8140 _bfd_error_handler
8141 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8142 bfd_set_error (bfd_error_invalid_operation);
8143 return 0;
8144 }
8145 }
8146
8147 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8148 if (lo->type == bfd_indirect_link_order)
8149 {
8150 asection *o = lo->u.indirect.section;
8151
8152 if ((o->size % bed->s->sizeof_rela) == 0)
8153 {
8154 if ((o->size % bed->s->sizeof_rel) == 0)
8155 /* Section size is divisible by both rel and rela sizes.
8156 It is of no help to us. */
8157 ;
8158 else
8159 {
8160 /* Section size is only divisible by rela. */
8161 if (use_rela_initialised && (use_rela == FALSE))
8162 {
8163 _bfd_error_handler
8164 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8165 bfd_set_error (bfd_error_invalid_operation);
8166 return 0;
8167 }
8168 else
8169 {
8170 use_rela = TRUE;
8171 use_rela_initialised = TRUE;
8172 }
8173 }
8174 }
8175 else if ((o->size % bed->s->sizeof_rel) == 0)
8176 {
8177 /* Section size is only divisible by rel. */
8178 if (use_rela_initialised && (use_rela == TRUE))
8179 {
8180 _bfd_error_handler
8181 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8182 bfd_set_error (bfd_error_invalid_operation);
8183 return 0;
8184 }
8185 else
8186 {
8187 use_rela = FALSE;
8188 use_rela_initialised = TRUE;
8189 }
8190 }
8191 else
8192 {
8193 /* The section size is not divisible by either - something is wrong. */
8194 _bfd_error_handler
8195 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8196 bfd_set_error (bfd_error_invalid_operation);
8197 return 0;
8198 }
8199 }
8200
8201 if (! use_rela_initialised)
8202 /* Make a guess. */
8203 use_rela = TRUE;
c152c796 8204 }
fc66a176
L
8205 else if (rela_dyn != NULL && rela_dyn->size > 0)
8206 use_rela = TRUE;
8207 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8208 use_rela = FALSE;
c152c796 8209 else
fc66a176 8210 return 0;
3410fea8
NC
8211
8212 if (use_rela)
c152c796 8213 {
3410fea8 8214 dynamic_relocs = rela_dyn;
c152c796
AM
8215 ext_size = bed->s->sizeof_rela;
8216 swap_in = bed->s->swap_reloca_in;
8217 swap_out = bed->s->swap_reloca_out;
8218 }
3410fea8
NC
8219 else
8220 {
8221 dynamic_relocs = rel_dyn;
8222 ext_size = bed->s->sizeof_rel;
8223 swap_in = bed->s->swap_reloc_in;
8224 swap_out = bed->s->swap_reloc_out;
8225 }
c152c796
AM
8226
8227 size = 0;
3410fea8 8228 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8229 if (lo->type == bfd_indirect_link_order)
3410fea8 8230 size += lo->u.indirect.section->size;
c152c796 8231
3410fea8 8232 if (size != dynamic_relocs->size)
c152c796
AM
8233 return 0;
8234
8235 sort_elt = (sizeof (struct elf_link_sort_rela)
8236 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8237
8238 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8239 if (count == 0)
8240 return 0;
a50b1753 8241 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8242
c152c796
AM
8243 if (sort == NULL)
8244 {
8245 (*info->callbacks->warning)
8246 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8247 return 0;
8248 }
8249
8250 if (bed->s->arch_size == 32)
8251 r_sym_mask = ~(bfd_vma) 0xff;
8252 else
8253 r_sym_mask = ~(bfd_vma) 0xffffffff;
8254
3410fea8 8255 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8256 if (lo->type == bfd_indirect_link_order)
8257 {
8258 bfd_byte *erel, *erelend;
8259 asection *o = lo->u.indirect.section;
8260
1da212d6
AM
8261 if (o->contents == NULL && o->size != 0)
8262 {
8263 /* This is a reloc section that is being handled as a normal
8264 section. See bfd_section_from_shdr. We can't combine
8265 relocs in this case. */
8266 free (sort);
8267 return 0;
8268 }
c152c796 8269 erel = o->contents;
eea6121a 8270 erelend = o->contents + o->size;
5dabe785 8271 /* FIXME: octets_per_byte. */
c152c796 8272 p = sort + o->output_offset / ext_size * sort_elt;
3410fea8 8273
c152c796
AM
8274 while (erel < erelend)
8275 {
8276 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8277
c152c796
AM
8278 (*swap_in) (abfd, erel, s->rela);
8279 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8280 s->u.sym_mask = r_sym_mask;
8281 p += sort_elt;
8282 erel += ext_size;
8283 }
8284 }
8285
8286 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8287
8288 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8289 {
8290 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8291 if (s->type != reloc_class_relative)
8292 break;
8293 }
8294 ret = i;
8295 s_non_relative = p;
8296
8297 sq = (struct elf_link_sort_rela *) s_non_relative;
8298 for (; i < count; i++, p += sort_elt)
8299 {
8300 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8301 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8302 sq = sp;
8303 sp->u.offset = sq->rela->r_offset;
8304 }
8305
8306 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8307
3410fea8 8308 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8309 if (lo->type == bfd_indirect_link_order)
8310 {
8311 bfd_byte *erel, *erelend;
8312 asection *o = lo->u.indirect.section;
8313
8314 erel = o->contents;
eea6121a 8315 erelend = o->contents + o->size;
5dabe785 8316 /* FIXME: octets_per_byte. */
c152c796
AM
8317 p = sort + o->output_offset / ext_size * sort_elt;
8318 while (erel < erelend)
8319 {
8320 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8321 (*swap_out) (abfd, s->rela, erel);
8322 p += sort_elt;
8323 erel += ext_size;
8324 }
8325 }
8326
8327 free (sort);
3410fea8 8328 *psec = dynamic_relocs;
c152c796
AM
8329 return ret;
8330}
8331
8332/* Flush the output symbols to the file. */
8333
8334static bfd_boolean
8335elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8336 const struct elf_backend_data *bed)
8337{
8338 if (finfo->symbuf_count > 0)
8339 {
8340 Elf_Internal_Shdr *hdr;
8341 file_ptr pos;
8342 bfd_size_type amt;
8343
8344 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8345 pos = hdr->sh_offset + hdr->sh_size;
8346 amt = finfo->symbuf_count * bed->s->sizeof_sym;
8347 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8348 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8349 return FALSE;
8350
8351 hdr->sh_size += amt;
8352 finfo->symbuf_count = 0;
8353 }
8354
8355 return TRUE;
8356}
8357
8358/* Add a symbol to the output symbol table. */
8359
6e0b88f1 8360static int
c152c796
AM
8361elf_link_output_sym (struct elf_final_link_info *finfo,
8362 const char *name,
8363 Elf_Internal_Sym *elfsym,
8364 asection *input_sec,
8365 struct elf_link_hash_entry *h)
8366{
8367 bfd_byte *dest;
8368 Elf_External_Sym_Shndx *destshndx;
6e0b88f1 8369 int (*output_symbol_hook)
c152c796
AM
8370 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8371 struct elf_link_hash_entry *);
8372 const struct elf_backend_data *bed;
8373
8374 bed = get_elf_backend_data (finfo->output_bfd);
8375 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8376 if (output_symbol_hook != NULL)
8377 {
6e0b88f1
AM
8378 int ret = (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h);
8379 if (ret != 1)
8380 return ret;
c152c796
AM
8381 }
8382
8383 if (name == NULL || *name == '\0')
8384 elfsym->st_name = 0;
8385 else if (input_sec->flags & SEC_EXCLUDE)
8386 elfsym->st_name = 0;
8387 else
8388 {
8389 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8390 name, TRUE, FALSE);
8391 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8392 return 0;
c152c796
AM
8393 }
8394
8395 if (finfo->symbuf_count >= finfo->symbuf_size)
8396 {
8397 if (! elf_link_flush_output_syms (finfo, bed))
6e0b88f1 8398 return 0;
c152c796
AM
8399 }
8400
8401 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8402 destshndx = finfo->symshndxbuf;
8403 if (destshndx != NULL)
8404 {
8405 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8406 {
8407 bfd_size_type amt;
8408
8409 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
a50b1753
NC
8410 destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx,
8411 amt * 2);
c152c796 8412 if (destshndx == NULL)
6e0b88f1 8413 return 0;
515ef31d 8414 finfo->symshndxbuf = destshndx;
c152c796
AM
8415 memset ((char *) destshndx + amt, 0, amt);
8416 finfo->shndxbuf_size *= 2;
8417 }
8418 destshndx += bfd_get_symcount (finfo->output_bfd);
8419 }
8420
8421 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8422 finfo->symbuf_count += 1;
8423 bfd_get_symcount (finfo->output_bfd) += 1;
8424
6e0b88f1 8425 return 1;
c152c796
AM
8426}
8427
c0d5a53d
L
8428/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8429
8430static bfd_boolean
8431check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8432{
4fbb74a6
AM
8433 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8434 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
8435 {
8436 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 8437 beyond 64k. */
c0d5a53d
L
8438 (*_bfd_error_handler)
8439 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 8440 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
8441 bfd_set_error (bfd_error_nonrepresentable_section);
8442 return FALSE;
8443 }
8444 return TRUE;
8445}
8446
c152c796
AM
8447/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8448 allowing an unsatisfied unversioned symbol in the DSO to match a
8449 versioned symbol that would normally require an explicit version.
8450 We also handle the case that a DSO references a hidden symbol
8451 which may be satisfied by a versioned symbol in another DSO. */
8452
8453static bfd_boolean
8454elf_link_check_versioned_symbol (struct bfd_link_info *info,
8455 const struct elf_backend_data *bed,
8456 struct elf_link_hash_entry *h)
8457{
8458 bfd *abfd;
8459 struct elf_link_loaded_list *loaded;
8460
8461 if (!is_elf_hash_table (info->hash))
8462 return FALSE;
8463
8464 switch (h->root.type)
8465 {
8466 default:
8467 abfd = NULL;
8468 break;
8469
8470 case bfd_link_hash_undefined:
8471 case bfd_link_hash_undefweak:
8472 abfd = h->root.u.undef.abfd;
8473 if ((abfd->flags & DYNAMIC) == 0
e56f61be 8474 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
8475 return FALSE;
8476 break;
8477
8478 case bfd_link_hash_defined:
8479 case bfd_link_hash_defweak:
8480 abfd = h->root.u.def.section->owner;
8481 break;
8482
8483 case bfd_link_hash_common:
8484 abfd = h->root.u.c.p->section->owner;
8485 break;
8486 }
8487 BFD_ASSERT (abfd != NULL);
8488
8489 for (loaded = elf_hash_table (info)->loaded;
8490 loaded != NULL;
8491 loaded = loaded->next)
8492 {
8493 bfd *input;
8494 Elf_Internal_Shdr *hdr;
8495 bfd_size_type symcount;
8496 bfd_size_type extsymcount;
8497 bfd_size_type extsymoff;
8498 Elf_Internal_Shdr *versymhdr;
8499 Elf_Internal_Sym *isym;
8500 Elf_Internal_Sym *isymend;
8501 Elf_Internal_Sym *isymbuf;
8502 Elf_External_Versym *ever;
8503 Elf_External_Versym *extversym;
8504
8505 input = loaded->abfd;
8506
8507 /* We check each DSO for a possible hidden versioned definition. */
8508 if (input == abfd
8509 || (input->flags & DYNAMIC) == 0
8510 || elf_dynversym (input) == 0)
8511 continue;
8512
8513 hdr = &elf_tdata (input)->dynsymtab_hdr;
8514
8515 symcount = hdr->sh_size / bed->s->sizeof_sym;
8516 if (elf_bad_symtab (input))
8517 {
8518 extsymcount = symcount;
8519 extsymoff = 0;
8520 }
8521 else
8522 {
8523 extsymcount = symcount - hdr->sh_info;
8524 extsymoff = hdr->sh_info;
8525 }
8526
8527 if (extsymcount == 0)
8528 continue;
8529
8530 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8531 NULL, NULL, NULL);
8532 if (isymbuf == NULL)
8533 return FALSE;
8534
8535 /* Read in any version definitions. */
8536 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 8537 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
8538 if (extversym == NULL)
8539 goto error_ret;
8540
8541 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8542 || (bfd_bread (extversym, versymhdr->sh_size, input)
8543 != versymhdr->sh_size))
8544 {
8545 free (extversym);
8546 error_ret:
8547 free (isymbuf);
8548 return FALSE;
8549 }
8550
8551 ever = extversym + extsymoff;
8552 isymend = isymbuf + extsymcount;
8553 for (isym = isymbuf; isym < isymend; isym++, ever++)
8554 {
8555 const char *name;
8556 Elf_Internal_Versym iver;
8557 unsigned short version_index;
8558
8559 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8560 || isym->st_shndx == SHN_UNDEF)
8561 continue;
8562
8563 name = bfd_elf_string_from_elf_section (input,
8564 hdr->sh_link,
8565 isym->st_name);
8566 if (strcmp (name, h->root.root.string) != 0)
8567 continue;
8568
8569 _bfd_elf_swap_versym_in (input, ever, &iver);
8570
d023c380
L
8571 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8572 && !(h->def_regular
8573 && h->forced_local))
c152c796
AM
8574 {
8575 /* If we have a non-hidden versioned sym, then it should
d023c380
L
8576 have provided a definition for the undefined sym unless
8577 it is defined in a non-shared object and forced local.
8578 */
c152c796
AM
8579 abort ();
8580 }
8581
8582 version_index = iver.vs_vers & VERSYM_VERSION;
8583 if (version_index == 1 || version_index == 2)
8584 {
8585 /* This is the base or first version. We can use it. */
8586 free (extversym);
8587 free (isymbuf);
8588 return TRUE;
8589 }
8590 }
8591
8592 free (extversym);
8593 free (isymbuf);
8594 }
8595
8596 return FALSE;
8597}
8598
8599/* Add an external symbol to the symbol table. This is called from
8600 the hash table traversal routine. When generating a shared object,
8601 we go through the symbol table twice. The first time we output
8602 anything that might have been forced to local scope in a version
8603 script. The second time we output the symbols that are still
8604 global symbols. */
8605
8606static bfd_boolean
8607elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
8608{
a50b1753 8609 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
c152c796
AM
8610 struct elf_final_link_info *finfo = eoinfo->finfo;
8611 bfd_boolean strip;
8612 Elf_Internal_Sym sym;
8613 asection *input_sec;
8614 const struct elf_backend_data *bed;
6e0b88f1
AM
8615 long indx;
8616 int ret;
c152c796
AM
8617
8618 if (h->root.type == bfd_link_hash_warning)
8619 {
8620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8621 if (h->root.type == bfd_link_hash_new)
8622 return TRUE;
8623 }
8624
8625 /* Decide whether to output this symbol in this pass. */
8626 if (eoinfo->localsyms)
8627 {
f5385ebf 8628 if (!h->forced_local)
c152c796
AM
8629 return TRUE;
8630 }
8631 else
8632 {
f5385ebf 8633 if (h->forced_local)
c152c796
AM
8634 return TRUE;
8635 }
8636
8637 bed = get_elf_backend_data (finfo->output_bfd);
8638
12ac1cf5 8639 if (h->root.type == bfd_link_hash_undefined)
c152c796 8640 {
12ac1cf5
NC
8641 /* If we have an undefined symbol reference here then it must have
8642 come from a shared library that is being linked in. (Undefined
98da7939
L
8643 references in regular files have already been handled unless
8644 they are in unreferenced sections which are removed by garbage
8645 collection). */
12ac1cf5
NC
8646 bfd_boolean ignore_undef = FALSE;
8647
8648 /* Some symbols may be special in that the fact that they're
8649 undefined can be safely ignored - let backend determine that. */
8650 if (bed->elf_backend_ignore_undef_symbol)
8651 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8652
8653 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 8654 if (!ignore_undef
12ac1cf5 8655 && h->ref_dynamic
98da7939 8656 && (!h->ref_regular || finfo->info->gc_sections)
12ac1cf5
NC
8657 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8658 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
c152c796 8659 {
12ac1cf5 8660 if (! (finfo->info->callbacks->undefined_symbol
98da7939
L
8661 (finfo->info, h->root.root.string,
8662 h->ref_regular ? NULL : h->root.u.undef.abfd,
12ac1cf5
NC
8663 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8664 {
17d078c5 8665 bfd_set_error (bfd_error_bad_value);
12ac1cf5
NC
8666 eoinfo->failed = TRUE;
8667 return FALSE;
8668 }
c152c796
AM
8669 }
8670 }
8671
8672 /* We should also warn if a forced local symbol is referenced from
8673 shared libraries. */
8674 if (! finfo->info->relocatable
8675 && (! finfo->info->shared)
f5385ebf
AM
8676 && h->forced_local
8677 && h->ref_dynamic
8678 && !h->dynamic_def
8679 && !h->dynamic_weak
c152c796
AM
8680 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8681 {
17d078c5
AM
8682 bfd *def_bfd;
8683 const char *msg;
8684
8685 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
8686 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
8687 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
8688 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
8689 else
8690 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8691 def_bfd = finfo->output_bfd;
8692 if (h->root.u.def.section != bfd_abs_section_ptr)
8693 def_bfd = h->root.u.def.section->owner;
8694 (*_bfd_error_handler) (msg, finfo->output_bfd, def_bfd,
8695 h->root.root.string);
8696 bfd_set_error (bfd_error_bad_value);
c152c796
AM
8697 eoinfo->failed = TRUE;
8698 return FALSE;
8699 }
8700
8701 /* We don't want to output symbols that have never been mentioned by
8702 a regular file, or that we have been told to strip. However, if
8703 h->indx is set to -2, the symbol is used by a reloc and we must
8704 output it. */
8705 if (h->indx == -2)
8706 strip = FALSE;
f5385ebf 8707 else if ((h->def_dynamic
77cfaee6
AM
8708 || h->ref_dynamic
8709 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
8710 && !h->def_regular
8711 && !h->ref_regular)
c152c796
AM
8712 strip = TRUE;
8713 else if (finfo->info->strip == strip_all)
8714 strip = TRUE;
8715 else if (finfo->info->strip == strip_some
8716 && bfd_hash_lookup (finfo->info->keep_hash,
8717 h->root.root.string, FALSE, FALSE) == NULL)
8718 strip = TRUE;
8719 else if (finfo->info->strip_discarded
8720 && (h->root.type == bfd_link_hash_defined
8721 || h->root.type == bfd_link_hash_defweak)
8722 && elf_discarded_section (h->root.u.def.section))
8723 strip = TRUE;
9e2278f5
AM
8724 else if ((h->root.type == bfd_link_hash_undefined
8725 || h->root.type == bfd_link_hash_undefweak)
8726 && h->root.u.undef.abfd != NULL
8727 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
8728 strip = TRUE;
c152c796
AM
8729 else
8730 strip = FALSE;
8731
8732 /* If we're stripping it, and it's not a dynamic symbol, there's
57ca8ac7
L
8733 nothing else to do unless it is a forced local symbol or a
8734 STT_GNU_IFUNC symbol. */
c152c796
AM
8735 if (strip
8736 && h->dynindx == -1
57ca8ac7 8737 && h->type != STT_GNU_IFUNC
f5385ebf 8738 && !h->forced_local)
c152c796
AM
8739 return TRUE;
8740
8741 sym.st_value = 0;
8742 sym.st_size = h->size;
8743 sym.st_other = h->other;
f5385ebf 8744 if (h->forced_local)
935bd1e0
L
8745 {
8746 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8747 /* Turn off visibility on local symbol. */
8748 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
8749 }
3e7a7d11
NC
8750 else if (h->unique_global)
8751 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
c152c796
AM
8752 else if (h->root.type == bfd_link_hash_undefweak
8753 || h->root.type == bfd_link_hash_defweak)
8754 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8755 else
8756 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
35fc36a8 8757 sym.st_target_internal = h->target_internal;
c152c796
AM
8758
8759 switch (h->root.type)
8760 {
8761 default:
8762 case bfd_link_hash_new:
8763 case bfd_link_hash_warning:
8764 abort ();
8765 return FALSE;
8766
8767 case bfd_link_hash_undefined:
8768 case bfd_link_hash_undefweak:
8769 input_sec = bfd_und_section_ptr;
8770 sym.st_shndx = SHN_UNDEF;
8771 break;
8772
8773 case bfd_link_hash_defined:
8774 case bfd_link_hash_defweak:
8775 {
8776 input_sec = h->root.u.def.section;
8777 if (input_sec->output_section != NULL)
8778 {
8779 sym.st_shndx =
8780 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8781 input_sec->output_section);
8782 if (sym.st_shndx == SHN_BAD)
8783 {
8784 (*_bfd_error_handler)
d003868e
AM
8785 (_("%B: could not find output section %A for input section %A"),
8786 finfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 8787 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
8788 eoinfo->failed = TRUE;
8789 return FALSE;
8790 }
8791
8792 /* ELF symbols in relocatable files are section relative,
8793 but in nonrelocatable files they are virtual
8794 addresses. */
8795 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8796 if (! finfo->info->relocatable)
8797 {
8798 sym.st_value += input_sec->output_section->vma;
8799 if (h->type == STT_TLS)
8800 {
430a16a5
NC
8801 asection *tls_sec = elf_hash_table (finfo->info)->tls_sec;
8802 if (tls_sec != NULL)
8803 sym.st_value -= tls_sec->vma;
8804 else
8805 {
8806 /* The TLS section may have been garbage collected. */
8807 BFD_ASSERT (finfo->info->gc_sections
8808 && !input_sec->gc_mark);
8809 }
c152c796
AM
8810 }
8811 }
8812 }
8813 else
8814 {
8815 BFD_ASSERT (input_sec->owner == NULL
8816 || (input_sec->owner->flags & DYNAMIC) != 0);
8817 sym.st_shndx = SHN_UNDEF;
8818 input_sec = bfd_und_section_ptr;
8819 }
8820 }
8821 break;
8822
8823 case bfd_link_hash_common:
8824 input_sec = h->root.u.c.p->section;
a4d8e49b 8825 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
8826 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8827 break;
8828
8829 case bfd_link_hash_indirect:
8830 /* These symbols are created by symbol versioning. They point
8831 to the decorated version of the name. For example, if the
8832 symbol foo@@GNU_1.2 is the default, which should be used when
8833 foo is used with no version, then we add an indirect symbol
8834 foo which points to foo@@GNU_1.2. We ignore these symbols,
8835 since the indirected symbol is already in the hash table. */
8836 return TRUE;
8837 }
8838
8839 /* Give the processor backend a chance to tweak the symbol value,
8840 and also to finish up anything that needs to be done for this
8841 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 8842 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 8843 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 8844 if ((h->type == STT_GNU_IFUNC
5f35ea9c 8845 && h->def_regular
3aa14d16
L
8846 && !finfo->info->relocatable)
8847 || ((h->dynindx != -1
8848 || h->forced_local)
8849 && ((finfo->info->shared
8850 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8851 || h->root.type != bfd_link_hash_undefweak))
8852 || !h->forced_local)
8853 && elf_hash_table (finfo->info)->dynamic_sections_created))
c152c796
AM
8854 {
8855 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8856 (finfo->output_bfd, finfo->info, h, &sym)))
8857 {
8858 eoinfo->failed = TRUE;
8859 return FALSE;
8860 }
8861 }
8862
8863 /* If we are marking the symbol as undefined, and there are no
8864 non-weak references to this symbol from a regular object, then
8865 mark the symbol as weak undefined; if there are non-weak
8866 references, mark the symbol as strong. We can't do this earlier,
8867 because it might not be marked as undefined until the
8868 finish_dynamic_symbol routine gets through with it. */
8869 if (sym.st_shndx == SHN_UNDEF
f5385ebf 8870 && h->ref_regular
c152c796
AM
8871 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8872 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8873 {
8874 int bindtype;
2955ec4c
L
8875 unsigned int type = ELF_ST_TYPE (sym.st_info);
8876
8877 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
8878 if (type == STT_GNU_IFUNC)
8879 type = STT_FUNC;
c152c796 8880
f5385ebf 8881 if (h->ref_regular_nonweak)
c152c796
AM
8882 bindtype = STB_GLOBAL;
8883 else
8884 bindtype = STB_WEAK;
2955ec4c 8885 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
8886 }
8887
bda987c2
CD
8888 /* If this is a symbol defined in a dynamic library, don't use the
8889 symbol size from the dynamic library. Relinking an executable
8890 against a new library may introduce gratuitous changes in the
8891 executable's symbols if we keep the size. */
8892 if (sym.st_shndx == SHN_UNDEF
8893 && !h->def_regular
8894 && h->def_dynamic)
8895 sym.st_size = 0;
8896
c152c796
AM
8897 /* If a non-weak symbol with non-default visibility is not defined
8898 locally, it is a fatal error. */
8899 if (! finfo->info->relocatable
8900 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8901 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8902 && h->root.type == bfd_link_hash_undefined
f5385ebf 8903 && !h->def_regular)
c152c796 8904 {
17d078c5
AM
8905 const char *msg;
8906
8907 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
8908 msg = _("%B: protected symbol `%s' isn't defined");
8909 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
8910 msg = _("%B: internal symbol `%s' isn't defined");
8911 else
8912 msg = _("%B: hidden symbol `%s' isn't defined");
8913 (*_bfd_error_handler) (msg, finfo->output_bfd, h->root.root.string);
8914 bfd_set_error (bfd_error_bad_value);
c152c796
AM
8915 eoinfo->failed = TRUE;
8916 return FALSE;
8917 }
8918
8919 /* If this symbol should be put in the .dynsym section, then put it
8920 there now. We already know the symbol index. We also fill in
8921 the entry in the .hash section. */
8922 if (h->dynindx != -1
8923 && elf_hash_table (finfo->info)->dynamic_sections_created)
8924 {
c152c796
AM
8925 bfd_byte *esym;
8926
8927 sym.st_name = h->dynstr_index;
8928 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
c0d5a53d
L
8929 if (! check_dynsym (finfo->output_bfd, &sym))
8930 {
8931 eoinfo->failed = TRUE;
8932 return FALSE;
8933 }
c152c796
AM
8934 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8935
fdc90cb4
JJ
8936 if (finfo->hash_sec != NULL)
8937 {
8938 size_t hash_entry_size;
8939 bfd_byte *bucketpos;
8940 bfd_vma chain;
41198d0c
L
8941 size_t bucketcount;
8942 size_t bucket;
8943
8944 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8945 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
8946
8947 hash_entry_size
8948 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8949 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8950 + (bucket + 2) * hash_entry_size);
8951 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8952 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8953 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8954 ((bfd_byte *) finfo->hash_sec->contents
8955 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8956 }
c152c796
AM
8957
8958 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8959 {
8960 Elf_Internal_Versym iversym;
8961 Elf_External_Versym *eversym;
8962
f5385ebf 8963 if (!h->def_regular)
c152c796
AM
8964 {
8965 if (h->verinfo.verdef == NULL)
8966 iversym.vs_vers = 0;
8967 else
8968 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8969 }
8970 else
8971 {
8972 if (h->verinfo.vertree == NULL)
8973 iversym.vs_vers = 1;
8974 else
8975 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
3e3b46e5
PB
8976 if (finfo->info->create_default_symver)
8977 iversym.vs_vers++;
c152c796
AM
8978 }
8979
f5385ebf 8980 if (h->hidden)
c152c796
AM
8981 iversym.vs_vers |= VERSYM_HIDDEN;
8982
8983 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8984 eversym += h->dynindx;
8985 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8986 }
8987 }
8988
8989 /* If we're stripping it, then it was just a dynamic symbol, and
8990 there's nothing else to do. */
8991 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8992 return TRUE;
8993
6e0b88f1
AM
8994 indx = bfd_get_symcount (finfo->output_bfd);
8995 ret = elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h);
8996 if (ret == 0)
c152c796
AM
8997 {
8998 eoinfo->failed = TRUE;
8999 return FALSE;
9000 }
6e0b88f1
AM
9001 else if (ret == 1)
9002 h->indx = indx;
9003 else if (h->indx == -2)
9004 abort();
c152c796
AM
9005
9006 return TRUE;
9007}
9008
cdd3575c
AM
9009/* Return TRUE if special handling is done for relocs in SEC against
9010 symbols defined in discarded sections. */
9011
c152c796
AM
9012static bfd_boolean
9013elf_section_ignore_discarded_relocs (asection *sec)
9014{
9015 const struct elf_backend_data *bed;
9016
cdd3575c
AM
9017 switch (sec->sec_info_type)
9018 {
9019 case ELF_INFO_TYPE_STABS:
9020 case ELF_INFO_TYPE_EH_FRAME:
9021 return TRUE;
9022 default:
9023 break;
9024 }
c152c796
AM
9025
9026 bed = get_elf_backend_data (sec->owner);
9027 if (bed->elf_backend_ignore_discarded_relocs != NULL
9028 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9029 return TRUE;
9030
9031 return FALSE;
9032}
9033
9e66c942
AM
9034/* Return a mask saying how ld should treat relocations in SEC against
9035 symbols defined in discarded sections. If this function returns
9036 COMPLAIN set, ld will issue a warning message. If this function
9037 returns PRETEND set, and the discarded section was link-once and the
9038 same size as the kept link-once section, ld will pretend that the
9039 symbol was actually defined in the kept section. Otherwise ld will
9040 zero the reloc (at least that is the intent, but some cooperation by
9041 the target dependent code is needed, particularly for REL targets). */
9042
8a696751
AM
9043unsigned int
9044_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9045{
9e66c942 9046 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9047 return PRETEND;
cdd3575c
AM
9048
9049 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9050 return 0;
cdd3575c
AM
9051
9052 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9053 return 0;
cdd3575c 9054
9e66c942 9055 return COMPLAIN | PRETEND;
cdd3575c
AM
9056}
9057
3d7f7666
L
9058/* Find a match between a section and a member of a section group. */
9059
9060static asection *
c0f00686
L
9061match_group_member (asection *sec, asection *group,
9062 struct bfd_link_info *info)
3d7f7666
L
9063{
9064 asection *first = elf_next_in_group (group);
9065 asection *s = first;
9066
9067 while (s != NULL)
9068 {
c0f00686 9069 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9070 return s;
9071
83180ade 9072 s = elf_next_in_group (s);
3d7f7666
L
9073 if (s == first)
9074 break;
9075 }
9076
9077 return NULL;
9078}
9079
01b3c8ab 9080/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9081 to replace it. Return the replacement if it is OK. Otherwise return
9082 NULL. */
01b3c8ab
L
9083
9084asection *
c0f00686 9085_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9086{
9087 asection *kept;
9088
9089 kept = sec->kept_section;
9090 if (kept != NULL)
9091 {
c2370991 9092 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9093 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9094 if (kept != NULL
9095 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9096 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9097 kept = NULL;
c2370991 9098 sec->kept_section = kept;
01b3c8ab
L
9099 }
9100 return kept;
9101}
9102
c152c796
AM
9103/* Link an input file into the linker output file. This function
9104 handles all the sections and relocations of the input file at once.
9105 This is so that we only have to read the local symbols once, and
9106 don't have to keep them in memory. */
9107
9108static bfd_boolean
9109elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
9110{
ece5ef60 9111 int (*relocate_section)
c152c796
AM
9112 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9113 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9114 bfd *output_bfd;
9115 Elf_Internal_Shdr *symtab_hdr;
9116 size_t locsymcount;
9117 size_t extsymoff;
9118 Elf_Internal_Sym *isymbuf;
9119 Elf_Internal_Sym *isym;
9120 Elf_Internal_Sym *isymend;
9121 long *pindex;
9122 asection **ppsection;
9123 asection *o;
9124 const struct elf_backend_data *bed;
c152c796 9125 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9126 bfd_size_type address_size;
9127 bfd_vma r_type_mask;
9128 int r_sym_shift;
c152c796
AM
9129
9130 output_bfd = finfo->output_bfd;
9131 bed = get_elf_backend_data (output_bfd);
9132 relocate_section = bed->elf_backend_relocate_section;
9133
9134 /* If this is a dynamic object, we don't want to do anything here:
9135 we don't want the local symbols, and we don't want the section
9136 contents. */
9137 if ((input_bfd->flags & DYNAMIC) != 0)
9138 return TRUE;
9139
c152c796
AM
9140 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9141 if (elf_bad_symtab (input_bfd))
9142 {
9143 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9144 extsymoff = 0;
9145 }
9146 else
9147 {
9148 locsymcount = symtab_hdr->sh_info;
9149 extsymoff = symtab_hdr->sh_info;
9150 }
9151
9152 /* Read the local symbols. */
9153 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9154 if (isymbuf == NULL && locsymcount != 0)
9155 {
9156 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9157 finfo->internal_syms,
9158 finfo->external_syms,
9159 finfo->locsym_shndx);
9160 if (isymbuf == NULL)
9161 return FALSE;
9162 }
9163
9164 /* Find local symbol sections and adjust values of symbols in
9165 SEC_MERGE sections. Write out those local symbols we know are
9166 going into the output file. */
9167 isymend = isymbuf + locsymcount;
9168 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
9169 isym < isymend;
9170 isym++, pindex++, ppsection++)
9171 {
9172 asection *isec;
9173 const char *name;
9174 Elf_Internal_Sym osym;
6e0b88f1
AM
9175 long indx;
9176 int ret;
c152c796
AM
9177
9178 *pindex = -1;
9179
9180 if (elf_bad_symtab (input_bfd))
9181 {
9182 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9183 {
9184 *ppsection = NULL;
9185 continue;
9186 }
9187 }
9188
9189 if (isym->st_shndx == SHN_UNDEF)
9190 isec = bfd_und_section_ptr;
c152c796
AM
9191 else if (isym->st_shndx == SHN_ABS)
9192 isec = bfd_abs_section_ptr;
9193 else if (isym->st_shndx == SHN_COMMON)
9194 isec = bfd_com_section_ptr;
9195 else
9196 {
cb33740c
AM
9197 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9198 if (isec == NULL)
9199 {
9200 /* Don't attempt to output symbols with st_shnx in the
9201 reserved range other than SHN_ABS and SHN_COMMON. */
9202 *ppsection = NULL;
9203 continue;
9204 }
9205 else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE
9206 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9207 isym->st_value =
9208 _bfd_merged_section_offset (output_bfd, &isec,
9209 elf_section_data (isec)->sec_info,
9210 isym->st_value);
c152c796
AM
9211 }
9212
9213 *ppsection = isec;
9214
9215 /* Don't output the first, undefined, symbol. */
9216 if (ppsection == finfo->sections)
9217 continue;
9218
9219 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9220 {
9221 /* We never output section symbols. Instead, we use the
9222 section symbol of the corresponding section in the output
9223 file. */
9224 continue;
9225 }
9226
9227 /* If we are stripping all symbols, we don't want to output this
9228 one. */
9229 if (finfo->info->strip == strip_all)
9230 continue;
9231
9232 /* If we are discarding all local symbols, we don't want to
9233 output this one. If we are generating a relocatable output
9234 file, then some of the local symbols may be required by
9235 relocs; we output them below as we discover that they are
9236 needed. */
9237 if (finfo->info->discard == discard_all)
9238 continue;
9239
9240 /* If this symbol is defined in a section which we are
f02571c5
AM
9241 discarding, we don't need to keep it. */
9242 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9243 && isym->st_shndx < SHN_LORESERVE
9244 && bfd_section_removed_from_list (output_bfd,
9245 isec->output_section))
e75a280b
L
9246 continue;
9247
c152c796
AM
9248 /* Get the name of the symbol. */
9249 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9250 isym->st_name);
9251 if (name == NULL)
9252 return FALSE;
9253
9254 /* See if we are discarding symbols with this name. */
9255 if ((finfo->info->strip == strip_some
9256 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
9257 == NULL))
9258 || (((finfo->info->discard == discard_sec_merge
9259 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
9260 || finfo->info->discard == discard_l)
9261 && bfd_is_local_label_name (input_bfd, name)))
9262 continue;
9263
c152c796
AM
9264 osym = *isym;
9265
9266 /* Adjust the section index for the output file. */
9267 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9268 isec->output_section);
9269 if (osym.st_shndx == SHN_BAD)
9270 return FALSE;
9271
c152c796
AM
9272 /* ELF symbols in relocatable files are section relative, but
9273 in executable files they are virtual addresses. Note that
9274 this code assumes that all ELF sections have an associated
9275 BFD section with a reasonable value for output_offset; below
9276 we assume that they also have a reasonable value for
9277 output_section. Any special sections must be set up to meet
9278 these requirements. */
9279 osym.st_value += isec->output_offset;
9280 if (! finfo->info->relocatable)
9281 {
9282 osym.st_value += isec->output_section->vma;
9283 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9284 {
9285 /* STT_TLS symbols are relative to PT_TLS segment base. */
9286 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9287 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9288 }
9289 }
9290
6e0b88f1
AM
9291 indx = bfd_get_symcount (output_bfd);
9292 ret = elf_link_output_sym (finfo, name, &osym, isec, NULL);
9293 if (ret == 0)
c152c796 9294 return FALSE;
6e0b88f1
AM
9295 else if (ret == 1)
9296 *pindex = indx;
c152c796
AM
9297 }
9298
310fd250
L
9299 if (bed->s->arch_size == 32)
9300 {
9301 r_type_mask = 0xff;
9302 r_sym_shift = 8;
9303 address_size = 4;
9304 }
9305 else
9306 {
9307 r_type_mask = 0xffffffff;
9308 r_sym_shift = 32;
9309 address_size = 8;
9310 }
9311
c152c796
AM
9312 /* Relocate the contents of each section. */
9313 sym_hashes = elf_sym_hashes (input_bfd);
9314 for (o = input_bfd->sections; o != NULL; o = o->next)
9315 {
9316 bfd_byte *contents;
9317
9318 if (! o->linker_mark)
9319 {
9320 /* This section was omitted from the link. */
9321 continue;
9322 }
9323
bcacc0f5
AM
9324 if (finfo->info->relocatable
9325 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9326 {
9327 /* Deal with the group signature symbol. */
9328 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9329 unsigned long symndx = sec_data->this_hdr.sh_info;
9330 asection *osec = o->output_section;
9331
9332 if (symndx >= locsymcount
9333 || (elf_bad_symtab (input_bfd)
9334 && finfo->sections[symndx] == NULL))
9335 {
9336 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9337 while (h->root.type == bfd_link_hash_indirect
9338 || h->root.type == bfd_link_hash_warning)
9339 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9340 /* Arrange for symbol to be output. */
9341 h->indx = -2;
9342 elf_section_data (osec)->this_hdr.sh_info = -2;
9343 }
9344 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9345 {
9346 /* We'll use the output section target_index. */
9347 asection *sec = finfo->sections[symndx]->output_section;
9348 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9349 }
9350 else
9351 {
9352 if (finfo->indices[symndx] == -1)
9353 {
9354 /* Otherwise output the local symbol now. */
9355 Elf_Internal_Sym sym = isymbuf[symndx];
9356 asection *sec = finfo->sections[symndx]->output_section;
9357 const char *name;
6e0b88f1
AM
9358 long indx;
9359 int ret;
bcacc0f5
AM
9360
9361 name = bfd_elf_string_from_elf_section (input_bfd,
9362 symtab_hdr->sh_link,
9363 sym.st_name);
9364 if (name == NULL)
9365 return FALSE;
9366
9367 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9368 sec);
9369 if (sym.st_shndx == SHN_BAD)
9370 return FALSE;
9371
9372 sym.st_value += o->output_offset;
9373
6e0b88f1
AM
9374 indx = bfd_get_symcount (output_bfd);
9375 ret = elf_link_output_sym (finfo, name, &sym, o, NULL);
9376 if (ret == 0)
bcacc0f5 9377 return FALSE;
6e0b88f1
AM
9378 else if (ret == 1)
9379 finfo->indices[symndx] = indx;
9380 else
9381 abort ();
bcacc0f5
AM
9382 }
9383 elf_section_data (osec)->this_hdr.sh_info
9384 = finfo->indices[symndx];
9385 }
9386 }
9387
c152c796 9388 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 9389 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
9390 continue;
9391
9392 if ((o->flags & SEC_LINKER_CREATED) != 0)
9393 {
9394 /* Section was created by _bfd_elf_link_create_dynamic_sections
9395 or somesuch. */
9396 continue;
9397 }
9398
9399 /* Get the contents of the section. They have been cached by a
9400 relaxation routine. Note that o is a section in an input
9401 file, so the contents field will not have been set by any of
9402 the routines which work on output files. */
9403 if (elf_section_data (o)->this_hdr.contents != NULL)
9404 contents = elf_section_data (o)->this_hdr.contents;
9405 else
9406 {
9407 contents = finfo->contents;
4a114e3e 9408 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
9409 return FALSE;
9410 }
9411
9412 if ((o->flags & SEC_RELOC) != 0)
9413 {
9414 Elf_Internal_Rela *internal_relocs;
0f02bbd9 9415 Elf_Internal_Rela *rel, *relend;
0f02bbd9 9416 int action_discarded;
ece5ef60 9417 int ret;
c152c796
AM
9418
9419 /* Get the swapped relocs. */
9420 internal_relocs
9421 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9422 finfo->internal_relocs, FALSE);
9423 if (internal_relocs == NULL
9424 && o->reloc_count > 0)
9425 return FALSE;
9426
310fd250
L
9427 /* We need to reverse-copy input .ctors/.dtors sections if
9428 they are placed in .init_array/.finit_array for output. */
9429 if (o->size > address_size
9430 && ((strncmp (o->name, ".ctors", 6) == 0
9431 && strcmp (o->output_section->name,
9432 ".init_array") == 0)
9433 || (strncmp (o->name, ".dtors", 6) == 0
9434 && strcmp (o->output_section->name,
9435 ".fini_array") == 0))
9436 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 9437 {
310fd250
L
9438 if (o->size != o->reloc_count * address_size)
9439 {
9440 (*_bfd_error_handler)
9441 (_("error: %B: size of section %A is not "
9442 "multiple of address size"),
9443 input_bfd, o);
9444 bfd_set_error (bfd_error_on_input);
9445 return FALSE;
9446 }
9447 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
9448 }
9449
0f02bbd9 9450 action_discarded = -1;
c152c796 9451 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
9452 action_discarded = (*bed->action_discarded) (o);
9453
9454 /* Run through the relocs evaluating complex reloc symbols and
9455 looking for relocs against symbols from discarded sections
9456 or section symbols from removed link-once sections.
9457 Complain about relocs against discarded sections. Zero
9458 relocs against removed link-once sections. */
9459
9460 rel = internal_relocs;
9461 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9462 for ( ; rel < relend; rel++)
c152c796 9463 {
0f02bbd9
AM
9464 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9465 unsigned int s_type;
9466 asection **ps, *sec;
9467 struct elf_link_hash_entry *h = NULL;
9468 const char *sym_name;
c152c796 9469
0f02bbd9
AM
9470 if (r_symndx == STN_UNDEF)
9471 continue;
c152c796 9472
0f02bbd9
AM
9473 if (r_symndx >= locsymcount
9474 || (elf_bad_symtab (input_bfd)
9475 && finfo->sections[r_symndx] == NULL))
9476 {
9477 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 9478
0f02bbd9
AM
9479 /* Badly formatted input files can contain relocs that
9480 reference non-existant symbols. Check here so that
9481 we do not seg fault. */
9482 if (h == NULL)
c152c796 9483 {
0f02bbd9 9484 char buffer [32];
dce669a1 9485
0f02bbd9
AM
9486 sprintf_vma (buffer, rel->r_info);
9487 (*_bfd_error_handler)
9488 (_("error: %B contains a reloc (0x%s) for section %A "
9489 "that references a non-existent global symbol"),
9490 input_bfd, o, buffer);
9491 bfd_set_error (bfd_error_bad_value);
9492 return FALSE;
9493 }
3b36f7e6 9494
0f02bbd9
AM
9495 while (h->root.type == bfd_link_hash_indirect
9496 || h->root.type == bfd_link_hash_warning)
9497 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 9498
0f02bbd9 9499 s_type = h->type;
cdd3575c 9500
0f02bbd9
AM
9501 ps = NULL;
9502 if (h->root.type == bfd_link_hash_defined
9503 || h->root.type == bfd_link_hash_defweak)
9504 ps = &h->root.u.def.section;
9505
9506 sym_name = h->root.root.string;
9507 }
9508 else
9509 {
9510 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9511
9512 s_type = ELF_ST_TYPE (sym->st_info);
9513 ps = &finfo->sections[r_symndx];
9514 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9515 sym, *ps);
9516 }
c152c796 9517
c301e700
DD
9518 if ((s_type == STT_RELC || s_type == STT_SRELC)
9519 && !finfo->info->relocatable)
0f02bbd9
AM
9520 {
9521 bfd_vma val;
9522 bfd_vma dot = (rel->r_offset
9523 + o->output_offset + o->output_section->vma);
9524#ifdef DEBUG
9525 printf ("Encountered a complex symbol!");
9526 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
9527 input_bfd->filename, o->name,
9528 (long) (rel - internal_relocs));
0f02bbd9
AM
9529 printf (" symbol: idx %8.8lx, name %s\n",
9530 r_symndx, sym_name);
9531 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9532 (unsigned long) rel->r_info,
9533 (unsigned long) rel->r_offset);
9534#endif
9535 if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot,
9536 isymbuf, locsymcount, s_type == STT_SRELC))
9537 return FALSE;
9538
9539 /* Symbol evaluated OK. Update to absolute value. */
9540 set_symbol_value (input_bfd, isymbuf, locsymcount,
9541 r_symndx, val);
9542 continue;
9543 }
9544
9545 if (action_discarded != -1 && ps != NULL)
9546 {
cdd3575c
AM
9547 /* Complain if the definition comes from a
9548 discarded section. */
9549 if ((sec = *ps) != NULL && elf_discarded_section (sec))
9550 {
cf35638d 9551 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 9552 if (action_discarded & COMPLAIN)
e1fffbe6
AM
9553 (*finfo->info->callbacks->einfo)
9554 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 9555 "defined in discarded section `%A' of %B\n"),
e1fffbe6 9556 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 9557
87e5235d 9558 /* Try to do the best we can to support buggy old
e0ae6d6f 9559 versions of gcc. Pretend that the symbol is
87e5235d
AM
9560 really defined in the kept linkonce section.
9561 FIXME: This is quite broken. Modifying the
9562 symbol here means we will be changing all later
e0ae6d6f 9563 uses of the symbol, not just in this section. */
0f02bbd9 9564 if (action_discarded & PRETEND)
87e5235d 9565 {
01b3c8ab
L
9566 asection *kept;
9567
c0f00686
L
9568 kept = _bfd_elf_check_kept_section (sec,
9569 finfo->info);
01b3c8ab 9570 if (kept != NULL)
87e5235d
AM
9571 {
9572 *ps = kept;
9573 continue;
9574 }
9575 }
c152c796
AM
9576 }
9577 }
9578 }
9579
9580 /* Relocate the section by invoking a back end routine.
9581
9582 The back end routine is responsible for adjusting the
9583 section contents as necessary, and (if using Rela relocs
9584 and generating a relocatable output file) adjusting the
9585 reloc addend as necessary.
9586
9587 The back end routine does not have to worry about setting
9588 the reloc address or the reloc symbol index.
9589
9590 The back end routine is given a pointer to the swapped in
9591 internal symbols, and can access the hash table entries
9592 for the external symbols via elf_sym_hashes (input_bfd).
9593
9594 When generating relocatable output, the back end routine
9595 must handle STB_LOCAL/STT_SECTION symbols specially. The
9596 output symbol is going to be a section symbol
9597 corresponding to the output section, which will require
9598 the addend to be adjusted. */
9599
ece5ef60 9600 ret = (*relocate_section) (output_bfd, finfo->info,
c152c796
AM
9601 input_bfd, o, contents,
9602 internal_relocs,
9603 isymbuf,
ece5ef60
AM
9604 finfo->sections);
9605 if (!ret)
c152c796
AM
9606 return FALSE;
9607
ece5ef60
AM
9608 if (ret == 2
9609 || finfo->info->relocatable
9610 || finfo->info->emitrelocations)
c152c796
AM
9611 {
9612 Elf_Internal_Rela *irela;
d4730f92 9613 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
9614 bfd_vma last_offset;
9615 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
9616 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
9617 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 9618 unsigned int next_erel;
c152c796 9619 bfd_boolean rela_normal;
d4730f92 9620 struct bfd_elf_section_data *esdi, *esdo;
c152c796 9621
d4730f92
BS
9622 esdi = elf_section_data (o);
9623 esdo = elf_section_data (o->output_section);
9624 rela_normal = FALSE;
c152c796
AM
9625
9626 /* Adjust the reloc addresses and symbol indices. */
9627
9628 irela = internal_relocs;
9629 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
9630 rel_hash = esdo->rel.hashes + esdo->rel.count;
9631 /* We start processing the REL relocs, if any. When we reach
9632 IRELAMID in the loop, we switch to the RELA relocs. */
9633 irelamid = irela;
9634 if (esdi->rel.hdr != NULL)
9635 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
9636 * bed->s->int_rels_per_ext_rel);
eac338cf 9637 rel_hash_list = rel_hash;
d4730f92 9638 rela_hash_list = NULL;
c152c796
AM
9639 last_offset = o->output_offset;
9640 if (!finfo->info->relocatable)
9641 last_offset += o->output_section->vma;
9642 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9643 {
9644 unsigned long r_symndx;
9645 asection *sec;
9646 Elf_Internal_Sym sym;
9647
9648 if (next_erel == bed->s->int_rels_per_ext_rel)
9649 {
9650 rel_hash++;
9651 next_erel = 0;
9652 }
9653
d4730f92
BS
9654 if (irela == irelamid)
9655 {
9656 rel_hash = esdo->rela.hashes + esdo->rela.count;
9657 rela_hash_list = rel_hash;
9658 rela_normal = bed->rela_normal;
9659 }
9660
c152c796
AM
9661 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9662 finfo->info, o,
9663 irela->r_offset);
9664 if (irela->r_offset >= (bfd_vma) -2)
9665 {
9666 /* This is a reloc for a deleted entry or somesuch.
9667 Turn it into an R_*_NONE reloc, at the same
9668 offset as the last reloc. elf_eh_frame.c and
e460dd0d 9669 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
9670 being ordered. */
9671 irela->r_offset = last_offset;
9672 irela->r_info = 0;
9673 irela->r_addend = 0;
9674 continue;
9675 }
9676
9677 irela->r_offset += o->output_offset;
9678
9679 /* Relocs in an executable have to be virtual addresses. */
9680 if (!finfo->info->relocatable)
9681 irela->r_offset += o->output_section->vma;
9682
9683 last_offset = irela->r_offset;
9684
9685 r_symndx = irela->r_info >> r_sym_shift;
9686 if (r_symndx == STN_UNDEF)
9687 continue;
9688
9689 if (r_symndx >= locsymcount
9690 || (elf_bad_symtab (input_bfd)
9691 && finfo->sections[r_symndx] == NULL))
9692 {
9693 struct elf_link_hash_entry *rh;
9694 unsigned long indx;
9695
9696 /* This is a reloc against a global symbol. We
9697 have not yet output all the local symbols, so
9698 we do not know the symbol index of any global
9699 symbol. We set the rel_hash entry for this
9700 reloc to point to the global hash table entry
9701 for this symbol. The symbol index is then
ee75fd95 9702 set at the end of bfd_elf_final_link. */
c152c796
AM
9703 indx = r_symndx - extsymoff;
9704 rh = elf_sym_hashes (input_bfd)[indx];
9705 while (rh->root.type == bfd_link_hash_indirect
9706 || rh->root.type == bfd_link_hash_warning)
9707 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9708
9709 /* Setting the index to -2 tells
9710 elf_link_output_extsym that this symbol is
9711 used by a reloc. */
9712 BFD_ASSERT (rh->indx < 0);
9713 rh->indx = -2;
9714
9715 *rel_hash = rh;
9716
9717 continue;
9718 }
9719
9720 /* This is a reloc against a local symbol. */
9721
9722 *rel_hash = NULL;
9723 sym = isymbuf[r_symndx];
9724 sec = finfo->sections[r_symndx];
9725 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9726 {
9727 /* I suppose the backend ought to fill in the
9728 section of any STT_SECTION symbol against a
6a8d1586 9729 processor specific section. */
cf35638d 9730 r_symndx = STN_UNDEF;
6a8d1586
AM
9731 if (bfd_is_abs_section (sec))
9732 ;
c152c796
AM
9733 else if (sec == NULL || sec->owner == NULL)
9734 {
9735 bfd_set_error (bfd_error_bad_value);
9736 return FALSE;
9737 }
9738 else
9739 {
6a8d1586
AM
9740 asection *osec = sec->output_section;
9741
9742 /* If we have discarded a section, the output
9743 section will be the absolute section. In
ab96bf03
AM
9744 case of discarded SEC_MERGE sections, use
9745 the kept section. relocate_section should
9746 have already handled discarded linkonce
9747 sections. */
6a8d1586
AM
9748 if (bfd_is_abs_section (osec)
9749 && sec->kept_section != NULL
9750 && sec->kept_section->output_section != NULL)
9751 {
9752 osec = sec->kept_section->output_section;
9753 irela->r_addend -= osec->vma;
9754 }
9755
9756 if (!bfd_is_abs_section (osec))
9757 {
9758 r_symndx = osec->target_index;
cf35638d 9759 if (r_symndx == STN_UNDEF)
74541ad4
AM
9760 {
9761 struct elf_link_hash_table *htab;
9762 asection *oi;
9763
9764 htab = elf_hash_table (finfo->info);
9765 oi = htab->text_index_section;
9766 if ((osec->flags & SEC_READONLY) == 0
9767 && htab->data_index_section != NULL)
9768 oi = htab->data_index_section;
9769
9770 if (oi != NULL)
9771 {
9772 irela->r_addend += osec->vma - oi->vma;
9773 r_symndx = oi->target_index;
9774 }
9775 }
9776
cf35638d 9777 BFD_ASSERT (r_symndx != STN_UNDEF);
6a8d1586 9778 }
c152c796
AM
9779 }
9780
9781 /* Adjust the addend according to where the
9782 section winds up in the output section. */
9783 if (rela_normal)
9784 irela->r_addend += sec->output_offset;
9785 }
9786 else
9787 {
9788 if (finfo->indices[r_symndx] == -1)
9789 {
9790 unsigned long shlink;
9791 const char *name;
9792 asection *osec;
6e0b88f1 9793 long indx;
c152c796
AM
9794
9795 if (finfo->info->strip == strip_all)
9796 {
9797 /* You can't do ld -r -s. */
9798 bfd_set_error (bfd_error_invalid_operation);
9799 return FALSE;
9800 }
9801
9802 /* This symbol was skipped earlier, but
9803 since it is needed by a reloc, we
9804 must output it now. */
9805 shlink = symtab_hdr->sh_link;
9806 name = (bfd_elf_string_from_elf_section
9807 (input_bfd, shlink, sym.st_name));
9808 if (name == NULL)
9809 return FALSE;
9810
9811 osec = sec->output_section;
9812 sym.st_shndx =
9813 _bfd_elf_section_from_bfd_section (output_bfd,
9814 osec);
9815 if (sym.st_shndx == SHN_BAD)
9816 return FALSE;
9817
9818 sym.st_value += sec->output_offset;
9819 if (! finfo->info->relocatable)
9820 {
9821 sym.st_value += osec->vma;
9822 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9823 {
9824 /* STT_TLS symbols are relative to PT_TLS
9825 segment base. */
9826 BFD_ASSERT (elf_hash_table (finfo->info)
9827 ->tls_sec != NULL);
9828 sym.st_value -= (elf_hash_table (finfo->info)
9829 ->tls_sec->vma);
9830 }
9831 }
9832
6e0b88f1
AM
9833 indx = bfd_get_symcount (output_bfd);
9834 ret = elf_link_output_sym (finfo, name, &sym, sec,
9835 NULL);
9836 if (ret == 0)
c152c796 9837 return FALSE;
6e0b88f1
AM
9838 else if (ret == 1)
9839 finfo->indices[r_symndx] = indx;
9840 else
9841 abort ();
c152c796
AM
9842 }
9843
9844 r_symndx = finfo->indices[r_symndx];
9845 }
9846
9847 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9848 | (irela->r_info & r_type_mask));
9849 }
9850
9851 /* Swap out the relocs. */
d4730f92
BS
9852 input_rel_hdr = esdi->rel.hdr;
9853 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 9854 {
d4730f92
BS
9855 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9856 input_rel_hdr,
9857 internal_relocs,
9858 rel_hash_list))
9859 return FALSE;
c152c796
AM
9860 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9861 * bed->s->int_rels_per_ext_rel);
eac338cf 9862 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
9863 }
9864
9865 input_rela_hdr = esdi->rela.hdr;
9866 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
9867 {
eac338cf 9868 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 9869 input_rela_hdr,
eac338cf 9870 internal_relocs,
d4730f92 9871 rela_hash_list))
c152c796
AM
9872 return FALSE;
9873 }
9874 }
9875 }
9876
9877 /* Write out the modified section contents. */
9878 if (bed->elf_backend_write_section
c7b8f16e
JB
9879 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9880 contents))
c152c796
AM
9881 {
9882 /* Section written out. */
9883 }
9884 else switch (o->sec_info_type)
9885 {
9886 case ELF_INFO_TYPE_STABS:
9887 if (! (_bfd_write_section_stabs
9888 (output_bfd,
9889 &elf_hash_table (finfo->info)->stab_info,
9890 o, &elf_section_data (o)->sec_info, contents)))
9891 return FALSE;
9892 break;
9893 case ELF_INFO_TYPE_MERGE:
9894 if (! _bfd_write_merged_section (output_bfd, o,
9895 elf_section_data (o)->sec_info))
9896 return FALSE;
9897 break;
9898 case ELF_INFO_TYPE_EH_FRAME:
9899 {
9900 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9901 o, contents))
9902 return FALSE;
9903 }
9904 break;
9905 default:
9906 {
5dabe785 9907 /* FIXME: octets_per_byte. */
310fd250
L
9908 if (! (o->flags & SEC_EXCLUDE))
9909 {
9910 file_ptr offset = (file_ptr) o->output_offset;
9911 bfd_size_type todo = o->size;
9912 if ((o->flags & SEC_ELF_REVERSE_COPY))
9913 {
9914 /* Reverse-copy input section to output. */
9915 do
9916 {
9917 todo -= address_size;
9918 if (! bfd_set_section_contents (output_bfd,
9919 o->output_section,
9920 contents + todo,
9921 offset,
9922 address_size))
9923 return FALSE;
9924 if (todo == 0)
9925 break;
9926 offset += address_size;
9927 }
9928 while (1);
9929 }
9930 else if (! bfd_set_section_contents (output_bfd,
9931 o->output_section,
9932 contents,
9933 offset, todo))
9934 return FALSE;
9935 }
c152c796
AM
9936 }
9937 break;
9938 }
9939 }
9940
9941 return TRUE;
9942}
9943
9944/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 9945 requested by the linker, and does not come from any input file. This
c152c796
AM
9946 is used to build constructor and destructor tables when linking
9947 with -Ur. */
9948
9949static bfd_boolean
9950elf_reloc_link_order (bfd *output_bfd,
9951 struct bfd_link_info *info,
9952 asection *output_section,
9953 struct bfd_link_order *link_order)
9954{
9955 reloc_howto_type *howto;
9956 long indx;
9957 bfd_vma offset;
9958 bfd_vma addend;
d4730f92 9959 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
9960 struct elf_link_hash_entry **rel_hash_ptr;
9961 Elf_Internal_Shdr *rel_hdr;
9962 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9963 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9964 bfd_byte *erel;
9965 unsigned int i;
d4730f92 9966 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
9967
9968 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9969 if (howto == NULL)
9970 {
9971 bfd_set_error (bfd_error_bad_value);
9972 return FALSE;
9973 }
9974
9975 addend = link_order->u.reloc.p->addend;
9976
d4730f92
BS
9977 if (esdo->rel.hdr)
9978 reldata = &esdo->rel;
9979 else if (esdo->rela.hdr)
9980 reldata = &esdo->rela;
9981 else
9982 {
9983 reldata = NULL;
9984 BFD_ASSERT (0);
9985 }
9986
c152c796 9987 /* Figure out the symbol index. */
d4730f92 9988 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
9989 if (link_order->type == bfd_section_reloc_link_order)
9990 {
9991 indx = link_order->u.reloc.p->u.section->target_index;
9992 BFD_ASSERT (indx != 0);
9993 *rel_hash_ptr = NULL;
9994 }
9995 else
9996 {
9997 struct elf_link_hash_entry *h;
9998
9999 /* Treat a reloc against a defined symbol as though it were
10000 actually against the section. */
10001 h = ((struct elf_link_hash_entry *)
10002 bfd_wrapped_link_hash_lookup (output_bfd, info,
10003 link_order->u.reloc.p->u.name,
10004 FALSE, FALSE, TRUE));
10005 if (h != NULL
10006 && (h->root.type == bfd_link_hash_defined
10007 || h->root.type == bfd_link_hash_defweak))
10008 {
10009 asection *section;
10010
10011 section = h->root.u.def.section;
10012 indx = section->output_section->target_index;
10013 *rel_hash_ptr = NULL;
10014 /* It seems that we ought to add the symbol value to the
10015 addend here, but in practice it has already been added
10016 because it was passed to constructor_callback. */
10017 addend += section->output_section->vma + section->output_offset;
10018 }
10019 else if (h != NULL)
10020 {
10021 /* Setting the index to -2 tells elf_link_output_extsym that
10022 this symbol is used by a reloc. */
10023 h->indx = -2;
10024 *rel_hash_ptr = h;
10025 indx = 0;
10026 }
10027 else
10028 {
10029 if (! ((*info->callbacks->unattached_reloc)
10030 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10031 return FALSE;
10032 indx = 0;
10033 }
10034 }
10035
10036 /* If this is an inplace reloc, we must write the addend into the
10037 object file. */
10038 if (howto->partial_inplace && addend != 0)
10039 {
10040 bfd_size_type size;
10041 bfd_reloc_status_type rstat;
10042 bfd_byte *buf;
10043 bfd_boolean ok;
10044 const char *sym_name;
10045
a50b1753
NC
10046 size = (bfd_size_type) bfd_get_reloc_size (howto);
10047 buf = (bfd_byte *) bfd_zmalloc (size);
c152c796
AM
10048 if (buf == NULL)
10049 return FALSE;
10050 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10051 switch (rstat)
10052 {
10053 case bfd_reloc_ok:
10054 break;
10055
10056 default:
10057 case bfd_reloc_outofrange:
10058 abort ();
10059
10060 case bfd_reloc_overflow:
10061 if (link_order->type == bfd_section_reloc_link_order)
10062 sym_name = bfd_section_name (output_bfd,
10063 link_order->u.reloc.p->u.section);
10064 else
10065 sym_name = link_order->u.reloc.p->u.name;
10066 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
10067 (info, NULL, sym_name, howto->name, addend, NULL,
10068 NULL, (bfd_vma) 0)))
c152c796
AM
10069 {
10070 free (buf);
10071 return FALSE;
10072 }
10073 break;
10074 }
10075 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10076 link_order->offset, size);
10077 free (buf);
10078 if (! ok)
10079 return FALSE;
10080 }
10081
10082 /* The address of a reloc is relative to the section in a
10083 relocatable file, and is a virtual address in an executable
10084 file. */
10085 offset = link_order->offset;
10086 if (! info->relocatable)
10087 offset += output_section->vma;
10088
10089 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10090 {
10091 irel[i].r_offset = offset;
10092 irel[i].r_info = 0;
10093 irel[i].r_addend = 0;
10094 }
10095 if (bed->s->arch_size == 32)
10096 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10097 else
10098 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10099
d4730f92 10100 rel_hdr = reldata->hdr;
c152c796
AM
10101 erel = rel_hdr->contents;
10102 if (rel_hdr->sh_type == SHT_REL)
10103 {
d4730f92 10104 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10105 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10106 }
10107 else
10108 {
10109 irel[0].r_addend = addend;
d4730f92 10110 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10111 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10112 }
10113
d4730f92 10114 ++reldata->count;
c152c796
AM
10115
10116 return TRUE;
10117}
10118
0b52efa6
PB
10119
10120/* Get the output vma of the section pointed to by the sh_link field. */
10121
10122static bfd_vma
10123elf_get_linked_section_vma (struct bfd_link_order *p)
10124{
10125 Elf_Internal_Shdr **elf_shdrp;
10126 asection *s;
10127 int elfsec;
10128
10129 s = p->u.indirect.section;
10130 elf_shdrp = elf_elfsections (s->owner);
10131 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10132 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10133 /* PR 290:
10134 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10135 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10136 sh_info fields. Hence we could get the situation
10137 where elfsec is 0. */
10138 if (elfsec == 0)
10139 {
10140 const struct elf_backend_data *bed
10141 = get_elf_backend_data (s->owner);
10142 if (bed->link_order_error_handler)
d003868e
AM
10143 bed->link_order_error_handler
10144 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10145 return 0;
10146 }
10147 else
10148 {
10149 s = elf_shdrp[elfsec]->bfd_section;
10150 return s->output_section->vma + s->output_offset;
10151 }
0b52efa6
PB
10152}
10153
10154
10155/* Compare two sections based on the locations of the sections they are
10156 linked to. Used by elf_fixup_link_order. */
10157
10158static int
10159compare_link_order (const void * a, const void * b)
10160{
10161 bfd_vma apos;
10162 bfd_vma bpos;
10163
10164 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10165 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10166 if (apos < bpos)
10167 return -1;
10168 return apos > bpos;
10169}
10170
10171
10172/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10173 order as their linked sections. Returns false if this could not be done
10174 because an output section includes both ordered and unordered
10175 sections. Ideally we'd do this in the linker proper. */
10176
10177static bfd_boolean
10178elf_fixup_link_order (bfd *abfd, asection *o)
10179{
10180 int seen_linkorder;
10181 int seen_other;
10182 int n;
10183 struct bfd_link_order *p;
10184 bfd *sub;
10185 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10186 unsigned elfsec;
0b52efa6 10187 struct bfd_link_order **sections;
d33cdfe3 10188 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10189 bfd_vma offset;
3b36f7e6 10190
d33cdfe3
L
10191 other_sec = NULL;
10192 linkorder_sec = NULL;
0b52efa6
PB
10193 seen_other = 0;
10194 seen_linkorder = 0;
8423293d 10195 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10196 {
d33cdfe3 10197 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10198 {
10199 s = p->u.indirect.section;
d33cdfe3
L
10200 sub = s->owner;
10201 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10202 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10203 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10204 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10205 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10206 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10207 {
10208 seen_linkorder++;
10209 linkorder_sec = s;
10210 }
0b52efa6 10211 else
d33cdfe3
L
10212 {
10213 seen_other++;
10214 other_sec = s;
10215 }
0b52efa6
PB
10216 }
10217 else
10218 seen_other++;
d33cdfe3
L
10219
10220 if (seen_other && seen_linkorder)
10221 {
10222 if (other_sec && linkorder_sec)
10223 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10224 o, linkorder_sec,
10225 linkorder_sec->owner, other_sec,
10226 other_sec->owner);
10227 else
10228 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10229 o);
10230 bfd_set_error (bfd_error_bad_value);
10231 return FALSE;
10232 }
0b52efa6
PB
10233 }
10234
10235 if (!seen_linkorder)
10236 return TRUE;
10237
0b52efa6 10238 sections = (struct bfd_link_order **)
14b1c01e
AM
10239 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10240 if (sections == NULL)
10241 return FALSE;
0b52efa6 10242 seen_linkorder = 0;
3b36f7e6 10243
8423293d 10244 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
10245 {
10246 sections[seen_linkorder++] = p;
10247 }
10248 /* Sort the input sections in the order of their linked section. */
10249 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10250 compare_link_order);
10251
10252 /* Change the offsets of the sections. */
10253 offset = 0;
10254 for (n = 0; n < seen_linkorder; n++)
10255 {
10256 s = sections[n]->u.indirect.section;
461686a3 10257 offset &= ~(bfd_vma) 0 << s->alignment_power;
0b52efa6
PB
10258 s->output_offset = offset;
10259 sections[n]->offset = offset;
5dabe785 10260 /* FIXME: octets_per_byte. */
0b52efa6
PB
10261 offset += sections[n]->size;
10262 }
10263
4dd07732 10264 free (sections);
0b52efa6
PB
10265 return TRUE;
10266}
10267
10268
c152c796
AM
10269/* Do the final step of an ELF link. */
10270
10271bfd_boolean
10272bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10273{
10274 bfd_boolean dynamic;
10275 bfd_boolean emit_relocs;
10276 bfd *dynobj;
10277 struct elf_final_link_info finfo;
91d6fa6a
NC
10278 asection *o;
10279 struct bfd_link_order *p;
10280 bfd *sub;
c152c796
AM
10281 bfd_size_type max_contents_size;
10282 bfd_size_type max_external_reloc_size;
10283 bfd_size_type max_internal_reloc_count;
10284 bfd_size_type max_sym_count;
10285 bfd_size_type max_sym_shndx_count;
10286 file_ptr off;
10287 Elf_Internal_Sym elfsym;
10288 unsigned int i;
10289 Elf_Internal_Shdr *symtab_hdr;
10290 Elf_Internal_Shdr *symtab_shndx_hdr;
10291 Elf_Internal_Shdr *symstrtab_hdr;
10292 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10293 struct elf_outext_info eoinfo;
10294 bfd_boolean merged;
10295 size_t relativecount = 0;
10296 asection *reldyn = 0;
10297 bfd_size_type amt;
104d59d1
JM
10298 asection *attr_section = NULL;
10299 bfd_vma attr_size = 0;
10300 const char *std_attrs_section;
c152c796
AM
10301
10302 if (! is_elf_hash_table (info->hash))
10303 return FALSE;
10304
10305 if (info->shared)
10306 abfd->flags |= DYNAMIC;
10307
10308 dynamic = elf_hash_table (info)->dynamic_sections_created;
10309 dynobj = elf_hash_table (info)->dynobj;
10310
10311 emit_relocs = (info->relocatable
a4676736 10312 || info->emitrelocations);
c152c796
AM
10313
10314 finfo.info = info;
10315 finfo.output_bfd = abfd;
10316 finfo.symstrtab = _bfd_elf_stringtab_init ();
10317 if (finfo.symstrtab == NULL)
10318 return FALSE;
10319
10320 if (! dynamic)
10321 {
10322 finfo.dynsym_sec = NULL;
10323 finfo.hash_sec = NULL;
10324 finfo.symver_sec = NULL;
10325 }
10326 else
10327 {
10328 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
10329 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
fdc90cb4 10330 BFD_ASSERT (finfo.dynsym_sec != NULL);
c152c796
AM
10331 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
10332 /* Note that it is OK if symver_sec is NULL. */
10333 }
10334
10335 finfo.contents = NULL;
10336 finfo.external_relocs = NULL;
10337 finfo.internal_relocs = NULL;
10338 finfo.external_syms = NULL;
10339 finfo.locsym_shndx = NULL;
10340 finfo.internal_syms = NULL;
10341 finfo.indices = NULL;
10342 finfo.sections = NULL;
10343 finfo.symbuf = NULL;
10344 finfo.symshndxbuf = NULL;
10345 finfo.symbuf_count = 0;
10346 finfo.shndxbuf_size = 0;
10347
104d59d1
JM
10348 /* The object attributes have been merged. Remove the input
10349 sections from the link, and set the contents of the output
10350 secton. */
10351 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10352 for (o = abfd->sections; o != NULL; o = o->next)
10353 {
10354 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10355 || strcmp (o->name, ".gnu.attributes") == 0)
10356 {
10357 for (p = o->map_head.link_order; p != NULL; p = p->next)
10358 {
10359 asection *input_section;
10360
10361 if (p->type != bfd_indirect_link_order)
10362 continue;
10363 input_section = p->u.indirect.section;
10364 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10365 elf_link_input_bfd ignores this section. */
10366 input_section->flags &= ~SEC_HAS_CONTENTS;
10367 }
a0c8462f 10368
104d59d1
JM
10369 attr_size = bfd_elf_obj_attr_size (abfd);
10370 if (attr_size)
10371 {
10372 bfd_set_section_size (abfd, o, attr_size);
10373 attr_section = o;
10374 /* Skip this section later on. */
10375 o->map_head.link_order = NULL;
10376 }
10377 else
10378 o->flags |= SEC_EXCLUDE;
10379 }
10380 }
10381
c152c796
AM
10382 /* Count up the number of relocations we will output for each output
10383 section, so that we know the sizes of the reloc sections. We
10384 also figure out some maximum sizes. */
10385 max_contents_size = 0;
10386 max_external_reloc_size = 0;
10387 max_internal_reloc_count = 0;
10388 max_sym_count = 0;
10389 max_sym_shndx_count = 0;
10390 merged = FALSE;
10391 for (o = abfd->sections; o != NULL; o = o->next)
10392 {
10393 struct bfd_elf_section_data *esdo = elf_section_data (o);
10394 o->reloc_count = 0;
10395
8423293d 10396 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10397 {
10398 unsigned int reloc_count = 0;
10399 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
10400
10401 if (p->type == bfd_section_reloc_link_order
10402 || p->type == bfd_symbol_reloc_link_order)
10403 reloc_count = 1;
10404 else if (p->type == bfd_indirect_link_order)
10405 {
10406 asection *sec;
10407
10408 sec = p->u.indirect.section;
10409 esdi = elf_section_data (sec);
10410
10411 /* Mark all sections which are to be included in the
10412 link. This will normally be every section. We need
10413 to do this so that we can identify any sections which
10414 the linker has decided to not include. */
10415 sec->linker_mark = TRUE;
10416
10417 if (sec->flags & SEC_MERGE)
10418 merged = TRUE;
10419
10420 if (info->relocatable || info->emitrelocations)
10421 reloc_count = sec->reloc_count;
10422 else if (bed->elf_backend_count_relocs)
58217f29 10423 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 10424
eea6121a
AM
10425 if (sec->rawsize > max_contents_size)
10426 max_contents_size = sec->rawsize;
10427 if (sec->size > max_contents_size)
10428 max_contents_size = sec->size;
c152c796
AM
10429
10430 /* We are interested in just local symbols, not all
10431 symbols. */
10432 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10433 && (sec->owner->flags & DYNAMIC) == 0)
10434 {
10435 size_t sym_count;
10436
10437 if (elf_bad_symtab (sec->owner))
10438 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10439 / bed->s->sizeof_sym);
10440 else
10441 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10442
10443 if (sym_count > max_sym_count)
10444 max_sym_count = sym_count;
10445
10446 if (sym_count > max_sym_shndx_count
10447 && elf_symtab_shndx (sec->owner) != 0)
10448 max_sym_shndx_count = sym_count;
10449
10450 if ((sec->flags & SEC_RELOC) != 0)
10451 {
d4730f92 10452 size_t ext_size = 0;
c152c796 10453
d4730f92
BS
10454 if (esdi->rel.hdr != NULL)
10455 ext_size = esdi->rel.hdr->sh_size;
10456 if (esdi->rela.hdr != NULL)
10457 ext_size += esdi->rela.hdr->sh_size;
7326c758 10458
c152c796
AM
10459 if (ext_size > max_external_reloc_size)
10460 max_external_reloc_size = ext_size;
10461 if (sec->reloc_count > max_internal_reloc_count)
10462 max_internal_reloc_count = sec->reloc_count;
10463 }
10464 }
10465 }
10466
10467 if (reloc_count == 0)
10468 continue;
10469
10470 o->reloc_count += reloc_count;
10471
d4730f92
BS
10472 if (p->type == bfd_indirect_link_order
10473 && (info->relocatable || info->emitrelocations))
c152c796 10474 {
d4730f92
BS
10475 if (esdi->rel.hdr)
10476 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
10477 if (esdi->rela.hdr)
10478 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
10479 }
10480 else
10481 {
10482 if (o->use_rela_p)
10483 esdo->rela.count += reloc_count;
2c2b4ed4 10484 else
d4730f92 10485 esdo->rel.count += reloc_count;
c152c796 10486 }
c152c796
AM
10487 }
10488
10489 if (o->reloc_count > 0)
10490 o->flags |= SEC_RELOC;
10491 else
10492 {
10493 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10494 set it (this is probably a bug) and if it is set
10495 assign_section_numbers will create a reloc section. */
10496 o->flags &=~ SEC_RELOC;
10497 }
10498
10499 /* If the SEC_ALLOC flag is not set, force the section VMA to
10500 zero. This is done in elf_fake_sections as well, but forcing
10501 the VMA to 0 here will ensure that relocs against these
10502 sections are handled correctly. */
10503 if ((o->flags & SEC_ALLOC) == 0
10504 && ! o->user_set_vma)
10505 o->vma = 0;
10506 }
10507
10508 if (! info->relocatable && merged)
10509 elf_link_hash_traverse (elf_hash_table (info),
10510 _bfd_elf_link_sec_merge_syms, abfd);
10511
10512 /* Figure out the file positions for everything but the symbol table
10513 and the relocs. We set symcount to force assign_section_numbers
10514 to create a symbol table. */
10515 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10516 BFD_ASSERT (! abfd->output_has_begun);
10517 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10518 goto error_return;
10519
ee75fd95 10520 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
10521 for (o = abfd->sections; o != NULL; o = o->next)
10522 {
d4730f92 10523 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
10524 if ((o->flags & SEC_RELOC) != 0)
10525 {
d4730f92
BS
10526 if (esdo->rel.hdr
10527 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
10528 goto error_return;
10529
d4730f92
BS
10530 if (esdo->rela.hdr
10531 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
10532 goto error_return;
10533 }
10534
10535 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10536 to count upwards while actually outputting the relocations. */
d4730f92
BS
10537 esdo->rel.count = 0;
10538 esdo->rela.count = 0;
c152c796
AM
10539 }
10540
10541 _bfd_elf_assign_file_positions_for_relocs (abfd);
10542
10543 /* We have now assigned file positions for all the sections except
10544 .symtab and .strtab. We start the .symtab section at the current
10545 file position, and write directly to it. We build the .strtab
10546 section in memory. */
10547 bfd_get_symcount (abfd) = 0;
10548 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10549 /* sh_name is set in prep_headers. */
10550 symtab_hdr->sh_type = SHT_SYMTAB;
10551 /* sh_flags, sh_addr and sh_size all start off zero. */
10552 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10553 /* sh_link is set in assign_section_numbers. */
10554 /* sh_info is set below. */
10555 /* sh_offset is set just below. */
72de5009 10556 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796
AM
10557
10558 off = elf_tdata (abfd)->next_file_pos;
10559 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10560
10561 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10562 incorrect. We do not yet know the size of the .symtab section.
10563 We correct next_file_pos below, after we do know the size. */
10564
10565 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10566 continuously seeking to the right position in the file. */
10567 if (! info->keep_memory || max_sym_count < 20)
10568 finfo.symbuf_size = 20;
10569 else
10570 finfo.symbuf_size = max_sym_count;
10571 amt = finfo.symbuf_size;
10572 amt *= bed->s->sizeof_sym;
a50b1753 10573 finfo.symbuf = (bfd_byte *) bfd_malloc (amt);
c152c796
AM
10574 if (finfo.symbuf == NULL)
10575 goto error_return;
4fbb74a6 10576 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
c152c796
AM
10577 {
10578 /* Wild guess at number of output symbols. realloc'd as needed. */
10579 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10580 finfo.shndxbuf_size = amt;
10581 amt *= sizeof (Elf_External_Sym_Shndx);
a50b1753 10582 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
c152c796
AM
10583 if (finfo.symshndxbuf == NULL)
10584 goto error_return;
10585 }
10586
10587 /* Start writing out the symbol table. The first symbol is always a
10588 dummy symbol. */
10589 if (info->strip != strip_all
10590 || emit_relocs)
10591 {
10592 elfsym.st_value = 0;
10593 elfsym.st_size = 0;
10594 elfsym.st_info = 0;
10595 elfsym.st_other = 0;
10596 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 10597 elfsym.st_target_internal = 0;
6e0b88f1
AM
10598 if (elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10599 NULL) != 1)
c152c796
AM
10600 goto error_return;
10601 }
10602
c152c796
AM
10603 /* Output a symbol for each section. We output these even if we are
10604 discarding local symbols, since they are used for relocs. These
10605 symbols have no names. We store the index of each one in the
10606 index field of the section, so that we can find it again when
10607 outputting relocs. */
10608 if (info->strip != strip_all
10609 || emit_relocs)
10610 {
10611 elfsym.st_size = 0;
10612 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10613 elfsym.st_other = 0;
f0b5bb34 10614 elfsym.st_value = 0;
35fc36a8 10615 elfsym.st_target_internal = 0;
c152c796
AM
10616 for (i = 1; i < elf_numsections (abfd); i++)
10617 {
10618 o = bfd_section_from_elf_index (abfd, i);
10619 if (o != NULL)
f0b5bb34
AM
10620 {
10621 o->target_index = bfd_get_symcount (abfd);
10622 elfsym.st_shndx = i;
10623 if (!info->relocatable)
10624 elfsym.st_value = o->vma;
6e0b88f1 10625 if (elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL) != 1)
f0b5bb34
AM
10626 goto error_return;
10627 }
c152c796
AM
10628 }
10629 }
10630
10631 /* Allocate some memory to hold information read in from the input
10632 files. */
10633 if (max_contents_size != 0)
10634 {
a50b1753 10635 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
c152c796
AM
10636 if (finfo.contents == NULL)
10637 goto error_return;
10638 }
10639
10640 if (max_external_reloc_size != 0)
10641 {
10642 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10643 if (finfo.external_relocs == NULL)
10644 goto error_return;
10645 }
10646
10647 if (max_internal_reloc_count != 0)
10648 {
10649 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10650 amt *= sizeof (Elf_Internal_Rela);
a50b1753 10651 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
c152c796
AM
10652 if (finfo.internal_relocs == NULL)
10653 goto error_return;
10654 }
10655
10656 if (max_sym_count != 0)
10657 {
10658 amt = max_sym_count * bed->s->sizeof_sym;
a50b1753 10659 finfo.external_syms = (bfd_byte *) bfd_malloc (amt);
c152c796
AM
10660 if (finfo.external_syms == NULL)
10661 goto error_return;
10662
10663 amt = max_sym_count * sizeof (Elf_Internal_Sym);
a50b1753 10664 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
c152c796
AM
10665 if (finfo.internal_syms == NULL)
10666 goto error_return;
10667
10668 amt = max_sym_count * sizeof (long);
a50b1753 10669 finfo.indices = (long int *) bfd_malloc (amt);
c152c796
AM
10670 if (finfo.indices == NULL)
10671 goto error_return;
10672
10673 amt = max_sym_count * sizeof (asection *);
a50b1753 10674 finfo.sections = (asection **) bfd_malloc (amt);
c152c796
AM
10675 if (finfo.sections == NULL)
10676 goto error_return;
10677 }
10678
10679 if (max_sym_shndx_count != 0)
10680 {
10681 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
a50b1753 10682 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
c152c796
AM
10683 if (finfo.locsym_shndx == NULL)
10684 goto error_return;
10685 }
10686
10687 if (elf_hash_table (info)->tls_sec)
10688 {
10689 bfd_vma base, end = 0;
10690 asection *sec;
10691
10692 for (sec = elf_hash_table (info)->tls_sec;
10693 sec && (sec->flags & SEC_THREAD_LOCAL);
10694 sec = sec->next)
10695 {
3a800eb9 10696 bfd_size_type size = sec->size;
c152c796 10697
3a800eb9
AM
10698 if (size == 0
10699 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 10700 {
91d6fa6a
NC
10701 struct bfd_link_order *ord = sec->map_tail.link_order;
10702
10703 if (ord != NULL)
10704 size = ord->offset + ord->size;
c152c796
AM
10705 }
10706 end = sec->vma + size;
10707 }
10708 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
10709 /* Only align end of TLS section if static TLS doesn't have special
10710 alignment requirements. */
10711 if (bed->static_tls_alignment == 1)
10712 end = align_power (end,
10713 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
10714 elf_hash_table (info)->tls_size = end - base;
10715 }
10716
0b52efa6
PB
10717 /* Reorder SHF_LINK_ORDER sections. */
10718 for (o = abfd->sections; o != NULL; o = o->next)
10719 {
10720 if (!elf_fixup_link_order (abfd, o))
10721 return FALSE;
10722 }
10723
c152c796
AM
10724 /* Since ELF permits relocations to be against local symbols, we
10725 must have the local symbols available when we do the relocations.
10726 Since we would rather only read the local symbols once, and we
10727 would rather not keep them in memory, we handle all the
10728 relocations for a single input file at the same time.
10729
10730 Unfortunately, there is no way to know the total number of local
10731 symbols until we have seen all of them, and the local symbol
10732 indices precede the global symbol indices. This means that when
10733 we are generating relocatable output, and we see a reloc against
10734 a global symbol, we can not know the symbol index until we have
10735 finished examining all the local symbols to see which ones we are
10736 going to output. To deal with this, we keep the relocations in
10737 memory, and don't output them until the end of the link. This is
10738 an unfortunate waste of memory, but I don't see a good way around
10739 it. Fortunately, it only happens when performing a relocatable
10740 link, which is not the common case. FIXME: If keep_memory is set
10741 we could write the relocs out and then read them again; I don't
10742 know how bad the memory loss will be. */
10743
10744 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10745 sub->output_has_begun = FALSE;
10746 for (o = abfd->sections; o != NULL; o = o->next)
10747 {
8423293d 10748 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10749 {
10750 if (p->type == bfd_indirect_link_order
10751 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10752 == bfd_target_elf_flavour)
10753 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10754 {
10755 if (! sub->output_has_begun)
10756 {
10757 if (! elf_link_input_bfd (&finfo, sub))
10758 goto error_return;
10759 sub->output_has_begun = TRUE;
10760 }
10761 }
10762 else if (p->type == bfd_section_reloc_link_order
10763 || p->type == bfd_symbol_reloc_link_order)
10764 {
10765 if (! elf_reloc_link_order (abfd, info, o, p))
10766 goto error_return;
10767 }
10768 else
10769 {
10770 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
10771 {
10772 if (p->type == bfd_indirect_link_order
10773 && (bfd_get_flavour (sub)
10774 == bfd_target_elf_flavour)
10775 && (elf_elfheader (sub)->e_ident[EI_CLASS]
10776 != bed->s->elfclass))
10777 {
10778 const char *iclass, *oclass;
10779
10780 if (bed->s->elfclass == ELFCLASS64)
10781 {
10782 iclass = "ELFCLASS32";
10783 oclass = "ELFCLASS64";
10784 }
10785 else
10786 {
10787 iclass = "ELFCLASS64";
10788 oclass = "ELFCLASS32";
10789 }
10790
10791 bfd_set_error (bfd_error_wrong_format);
10792 (*_bfd_error_handler)
10793 (_("%B: file class %s incompatible with %s"),
10794 sub, iclass, oclass);
10795 }
10796
10797 goto error_return;
10798 }
c152c796
AM
10799 }
10800 }
10801 }
10802
c0f00686
L
10803 /* Free symbol buffer if needed. */
10804 if (!info->reduce_memory_overheads)
10805 {
10806 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3fcd97f1
JJ
10807 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10808 && elf_tdata (sub)->symbuf)
c0f00686
L
10809 {
10810 free (elf_tdata (sub)->symbuf);
10811 elf_tdata (sub)->symbuf = NULL;
10812 }
10813 }
10814
c152c796
AM
10815 /* Output any global symbols that got converted to local in a
10816 version script or due to symbol visibility. We do this in a
10817 separate step since ELF requires all local symbols to appear
10818 prior to any global symbols. FIXME: We should only do this if
10819 some global symbols were, in fact, converted to become local.
10820 FIXME: Will this work correctly with the Irix 5 linker? */
10821 eoinfo.failed = FALSE;
10822 eoinfo.finfo = &finfo;
10823 eoinfo.localsyms = TRUE;
10824 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10825 &eoinfo);
10826 if (eoinfo.failed)
10827 return FALSE;
10828
4e617b1e
PB
10829 /* If backend needs to output some local symbols not present in the hash
10830 table, do it now. */
10831 if (bed->elf_backend_output_arch_local_syms)
10832 {
6e0b88f1 10833 typedef int (*out_sym_func)
4e617b1e
PB
10834 (void *, const char *, Elf_Internal_Sym *, asection *,
10835 struct elf_link_hash_entry *);
10836
10837 if (! ((*bed->elf_backend_output_arch_local_syms)
10838 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10839 return FALSE;
10840 }
10841
c152c796
AM
10842 /* That wrote out all the local symbols. Finish up the symbol table
10843 with the global symbols. Even if we want to strip everything we
10844 can, we still need to deal with those global symbols that got
10845 converted to local in a version script. */
10846
10847 /* The sh_info field records the index of the first non local symbol. */
10848 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10849
10850 if (dynamic
10851 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10852 {
10853 Elf_Internal_Sym sym;
10854 bfd_byte *dynsym = finfo.dynsym_sec->contents;
10855 long last_local = 0;
10856
10857 /* Write out the section symbols for the output sections. */
67687978 10858 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
10859 {
10860 asection *s;
10861
10862 sym.st_size = 0;
10863 sym.st_name = 0;
10864 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10865 sym.st_other = 0;
35fc36a8 10866 sym.st_target_internal = 0;
c152c796
AM
10867
10868 for (s = abfd->sections; s != NULL; s = s->next)
10869 {
10870 int indx;
10871 bfd_byte *dest;
10872 long dynindx;
10873
c152c796 10874 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
10875 if (dynindx <= 0)
10876 continue;
10877 indx = elf_section_data (s)->this_idx;
c152c796
AM
10878 BFD_ASSERT (indx > 0);
10879 sym.st_shndx = indx;
c0d5a53d
L
10880 if (! check_dynsym (abfd, &sym))
10881 return FALSE;
c152c796
AM
10882 sym.st_value = s->vma;
10883 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
10884 if (last_local < dynindx)
10885 last_local = dynindx;
c152c796
AM
10886 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10887 }
c152c796
AM
10888 }
10889
10890 /* Write out the local dynsyms. */
10891 if (elf_hash_table (info)->dynlocal)
10892 {
10893 struct elf_link_local_dynamic_entry *e;
10894 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10895 {
10896 asection *s;
10897 bfd_byte *dest;
10898
935bd1e0 10899 /* Copy the internal symbol and turn off visibility.
c152c796
AM
10900 Note that we saved a word of storage and overwrote
10901 the original st_name with the dynstr_index. */
10902 sym = e->isym;
935bd1e0 10903 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 10904
cb33740c
AM
10905 s = bfd_section_from_elf_index (e->input_bfd,
10906 e->isym.st_shndx);
10907 if (s != NULL)
c152c796 10908 {
c152c796
AM
10909 sym.st_shndx =
10910 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
10911 if (! check_dynsym (abfd, &sym))
10912 return FALSE;
c152c796
AM
10913 sym.st_value = (s->output_section->vma
10914 + s->output_offset
10915 + e->isym.st_value);
10916 }
10917
10918 if (last_local < e->dynindx)
10919 last_local = e->dynindx;
10920
10921 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10922 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10923 }
10924 }
10925
10926 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10927 last_local + 1;
10928 }
10929
10930 /* We get the global symbols from the hash table. */
10931 eoinfo.failed = FALSE;
10932 eoinfo.localsyms = FALSE;
10933 eoinfo.finfo = &finfo;
10934 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10935 &eoinfo);
10936 if (eoinfo.failed)
10937 return FALSE;
10938
10939 /* If backend needs to output some symbols not present in the hash
10940 table, do it now. */
10941 if (bed->elf_backend_output_arch_syms)
10942 {
6e0b88f1 10943 typedef int (*out_sym_func)
c152c796
AM
10944 (void *, const char *, Elf_Internal_Sym *, asection *,
10945 struct elf_link_hash_entry *);
10946
10947 if (! ((*bed->elf_backend_output_arch_syms)
10948 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10949 return FALSE;
10950 }
10951
10952 /* Flush all symbols to the file. */
10953 if (! elf_link_flush_output_syms (&finfo, bed))
10954 return FALSE;
10955
10956 /* Now we know the size of the symtab section. */
10957 off += symtab_hdr->sh_size;
10958
10959 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10960 if (symtab_shndx_hdr->sh_name != 0)
10961 {
10962 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10963 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10964 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10965 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10966 symtab_shndx_hdr->sh_size = amt;
10967
10968 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10969 off, TRUE);
10970
10971 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10972 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10973 return FALSE;
10974 }
10975
10976
10977 /* Finish up and write out the symbol string table (.strtab)
10978 section. */
10979 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10980 /* sh_name was set in prep_headers. */
10981 symstrtab_hdr->sh_type = SHT_STRTAB;
10982 symstrtab_hdr->sh_flags = 0;
10983 symstrtab_hdr->sh_addr = 0;
10984 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10985 symstrtab_hdr->sh_entsize = 0;
10986 symstrtab_hdr->sh_link = 0;
10987 symstrtab_hdr->sh_info = 0;
10988 /* sh_offset is set just below. */
10989 symstrtab_hdr->sh_addralign = 1;
10990
10991 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10992 elf_tdata (abfd)->next_file_pos = off;
10993
10994 if (bfd_get_symcount (abfd) > 0)
10995 {
10996 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10997 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10998 return FALSE;
10999 }
11000
11001 /* Adjust the relocs to have the correct symbol indices. */
11002 for (o = abfd->sections; o != NULL; o = o->next)
11003 {
d4730f92 11004 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11005 if ((o->flags & SEC_RELOC) == 0)
11006 continue;
11007
d4730f92
BS
11008 if (esdo->rel.hdr != NULL)
11009 elf_link_adjust_relocs (abfd, &esdo->rel);
11010 if (esdo->rela.hdr != NULL)
11011 elf_link_adjust_relocs (abfd, &esdo->rela);
c152c796
AM
11012
11013 /* Set the reloc_count field to 0 to prevent write_relocs from
11014 trying to swap the relocs out itself. */
11015 o->reloc_count = 0;
11016 }
11017
11018 if (dynamic && info->combreloc && dynobj != NULL)
11019 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11020
11021 /* If we are linking against a dynamic object, or generating a
11022 shared library, finish up the dynamic linking information. */
11023 if (dynamic)
11024 {
11025 bfd_byte *dyncon, *dynconend;
11026
11027 /* Fix up .dynamic entries. */
11028 o = bfd_get_section_by_name (dynobj, ".dynamic");
11029 BFD_ASSERT (o != NULL);
11030
11031 dyncon = o->contents;
eea6121a 11032 dynconend = o->contents + o->size;
c152c796
AM
11033 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11034 {
11035 Elf_Internal_Dyn dyn;
11036 const char *name;
11037 unsigned int type;
11038
11039 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11040
11041 switch (dyn.d_tag)
11042 {
11043 default:
11044 continue;
11045 case DT_NULL:
11046 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11047 {
11048 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11049 {
11050 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11051 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11052 default: continue;
11053 }
11054 dyn.d_un.d_val = relativecount;
11055 relativecount = 0;
11056 break;
11057 }
11058 continue;
11059
11060 case DT_INIT:
11061 name = info->init_function;
11062 goto get_sym;
11063 case DT_FINI:
11064 name = info->fini_function;
11065 get_sym:
11066 {
11067 struct elf_link_hash_entry *h;
11068
11069 h = elf_link_hash_lookup (elf_hash_table (info), name,
11070 FALSE, FALSE, TRUE);
11071 if (h != NULL
11072 && (h->root.type == bfd_link_hash_defined
11073 || h->root.type == bfd_link_hash_defweak))
11074 {
bef26483 11075 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11076 o = h->root.u.def.section;
11077 if (o->output_section != NULL)
bef26483 11078 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11079 + o->output_offset);
11080 else
11081 {
11082 /* The symbol is imported from another shared
11083 library and does not apply to this one. */
bef26483 11084 dyn.d_un.d_ptr = 0;
c152c796
AM
11085 }
11086 break;
11087 }
11088 }
11089 continue;
11090
11091 case DT_PREINIT_ARRAYSZ:
11092 name = ".preinit_array";
11093 goto get_size;
11094 case DT_INIT_ARRAYSZ:
11095 name = ".init_array";
11096 goto get_size;
11097 case DT_FINI_ARRAYSZ:
11098 name = ".fini_array";
11099 get_size:
11100 o = bfd_get_section_by_name (abfd, name);
11101 if (o == NULL)
11102 {
11103 (*_bfd_error_handler)
d003868e 11104 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11105 goto error_return;
11106 }
eea6121a 11107 if (o->size == 0)
c152c796
AM
11108 (*_bfd_error_handler)
11109 (_("warning: %s section has zero size"), name);
eea6121a 11110 dyn.d_un.d_val = o->size;
c152c796
AM
11111 break;
11112
11113 case DT_PREINIT_ARRAY:
11114 name = ".preinit_array";
11115 goto get_vma;
11116 case DT_INIT_ARRAY:
11117 name = ".init_array";
11118 goto get_vma;
11119 case DT_FINI_ARRAY:
11120 name = ".fini_array";
11121 goto get_vma;
11122
11123 case DT_HASH:
11124 name = ".hash";
11125 goto get_vma;
fdc90cb4
JJ
11126 case DT_GNU_HASH:
11127 name = ".gnu.hash";
11128 goto get_vma;
c152c796
AM
11129 case DT_STRTAB:
11130 name = ".dynstr";
11131 goto get_vma;
11132 case DT_SYMTAB:
11133 name = ".dynsym";
11134 goto get_vma;
11135 case DT_VERDEF:
11136 name = ".gnu.version_d";
11137 goto get_vma;
11138 case DT_VERNEED:
11139 name = ".gnu.version_r";
11140 goto get_vma;
11141 case DT_VERSYM:
11142 name = ".gnu.version";
11143 get_vma:
11144 o = bfd_get_section_by_name (abfd, name);
11145 if (o == NULL)
11146 {
11147 (*_bfd_error_handler)
d003868e 11148 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11149 goto error_return;
11150 }
11151 dyn.d_un.d_ptr = o->vma;
11152 break;
11153
11154 case DT_REL:
11155 case DT_RELA:
11156 case DT_RELSZ:
11157 case DT_RELASZ:
11158 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11159 type = SHT_REL;
11160 else
11161 type = SHT_RELA;
11162 dyn.d_un.d_val = 0;
bef26483 11163 dyn.d_un.d_ptr = 0;
c152c796
AM
11164 for (i = 1; i < elf_numsections (abfd); i++)
11165 {
11166 Elf_Internal_Shdr *hdr;
11167
11168 hdr = elf_elfsections (abfd)[i];
11169 if (hdr->sh_type == type
11170 && (hdr->sh_flags & SHF_ALLOC) != 0)
11171 {
11172 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11173 dyn.d_un.d_val += hdr->sh_size;
11174 else
11175 {
bef26483
AM
11176 if (dyn.d_un.d_ptr == 0
11177 || hdr->sh_addr < dyn.d_un.d_ptr)
11178 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
11179 }
11180 }
11181 }
11182 break;
11183 }
11184 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11185 }
11186 }
11187
11188 /* If we have created any dynamic sections, then output them. */
11189 if (dynobj != NULL)
11190 {
11191 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11192 goto error_return;
11193
943284cc
DJ
11194 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11195 if (info->warn_shared_textrel && info->shared)
11196 {
11197 bfd_byte *dyncon, *dynconend;
11198
11199 /* Fix up .dynamic entries. */
11200 o = bfd_get_section_by_name (dynobj, ".dynamic");
11201 BFD_ASSERT (o != NULL);
11202
11203 dyncon = o->contents;
11204 dynconend = o->contents + o->size;
11205 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11206 {
11207 Elf_Internal_Dyn dyn;
11208
11209 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11210
11211 if (dyn.d_tag == DT_TEXTREL)
11212 {
a0c8462f 11213 info->callbacks->einfo
9267588c 11214 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
11215 break;
11216 }
11217 }
11218 }
11219
c152c796
AM
11220 for (o = dynobj->sections; o != NULL; o = o->next)
11221 {
11222 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11223 || o->size == 0
c152c796
AM
11224 || o->output_section == bfd_abs_section_ptr)
11225 continue;
11226 if ((o->flags & SEC_LINKER_CREATED) == 0)
11227 {
11228 /* At this point, we are only interested in sections
11229 created by _bfd_elf_link_create_dynamic_sections. */
11230 continue;
11231 }
3722b82f
AM
11232 if (elf_hash_table (info)->stab_info.stabstr == o)
11233 continue;
eea6121a
AM
11234 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11235 continue;
c152c796
AM
11236 if ((elf_section_data (o->output_section)->this_hdr.sh_type
11237 != SHT_STRTAB)
11238 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
11239 {
5dabe785 11240 /* FIXME: octets_per_byte. */
c152c796
AM
11241 if (! bfd_set_section_contents (abfd, o->output_section,
11242 o->contents,
11243 (file_ptr) o->output_offset,
eea6121a 11244 o->size))
c152c796
AM
11245 goto error_return;
11246 }
11247 else
11248 {
11249 /* The contents of the .dynstr section are actually in a
11250 stringtab. */
11251 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11252 if (bfd_seek (abfd, off, SEEK_SET) != 0
11253 || ! _bfd_elf_strtab_emit (abfd,
11254 elf_hash_table (info)->dynstr))
11255 goto error_return;
11256 }
11257 }
11258 }
11259
11260 if (info->relocatable)
11261 {
11262 bfd_boolean failed = FALSE;
11263
11264 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11265 if (failed)
11266 goto error_return;
11267 }
11268
11269 /* If we have optimized stabs strings, output them. */
3722b82f 11270 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
11271 {
11272 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11273 goto error_return;
11274 }
11275
11276 if (info->eh_frame_hdr)
11277 {
11278 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11279 goto error_return;
11280 }
11281
11282 if (finfo.symstrtab != NULL)
11283 _bfd_stringtab_free (finfo.symstrtab);
11284 if (finfo.contents != NULL)
11285 free (finfo.contents);
11286 if (finfo.external_relocs != NULL)
11287 free (finfo.external_relocs);
11288 if (finfo.internal_relocs != NULL)
11289 free (finfo.internal_relocs);
11290 if (finfo.external_syms != NULL)
11291 free (finfo.external_syms);
11292 if (finfo.locsym_shndx != NULL)
11293 free (finfo.locsym_shndx);
11294 if (finfo.internal_syms != NULL)
11295 free (finfo.internal_syms);
11296 if (finfo.indices != NULL)
11297 free (finfo.indices);
11298 if (finfo.sections != NULL)
11299 free (finfo.sections);
11300 if (finfo.symbuf != NULL)
11301 free (finfo.symbuf);
11302 if (finfo.symshndxbuf != NULL)
11303 free (finfo.symshndxbuf);
11304 for (o = abfd->sections; o != NULL; o = o->next)
11305 {
d4730f92
BS
11306 struct bfd_elf_section_data *esdo = elf_section_data (o);
11307 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11308 free (esdo->rel.hashes);
11309 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11310 free (esdo->rela.hashes);
c152c796
AM
11311 }
11312
11313 elf_tdata (abfd)->linker = TRUE;
11314
104d59d1
JM
11315 if (attr_section)
11316 {
a50b1753 11317 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 11318 if (contents == NULL)
d0f16d5e 11319 return FALSE; /* Bail out and fail. */
104d59d1
JM
11320 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11321 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11322 free (contents);
11323 }
11324
c152c796
AM
11325 return TRUE;
11326
11327 error_return:
11328 if (finfo.symstrtab != NULL)
11329 _bfd_stringtab_free (finfo.symstrtab);
11330 if (finfo.contents != NULL)
11331 free (finfo.contents);
11332 if (finfo.external_relocs != NULL)
11333 free (finfo.external_relocs);
11334 if (finfo.internal_relocs != NULL)
11335 free (finfo.internal_relocs);
11336 if (finfo.external_syms != NULL)
11337 free (finfo.external_syms);
11338 if (finfo.locsym_shndx != NULL)
11339 free (finfo.locsym_shndx);
11340 if (finfo.internal_syms != NULL)
11341 free (finfo.internal_syms);
11342 if (finfo.indices != NULL)
11343 free (finfo.indices);
11344 if (finfo.sections != NULL)
11345 free (finfo.sections);
11346 if (finfo.symbuf != NULL)
11347 free (finfo.symbuf);
11348 if (finfo.symshndxbuf != NULL)
11349 free (finfo.symshndxbuf);
11350 for (o = abfd->sections; o != NULL; o = o->next)
11351 {
d4730f92
BS
11352 struct bfd_elf_section_data *esdo = elf_section_data (o);
11353 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11354 free (esdo->rel.hashes);
11355 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11356 free (esdo->rela.hashes);
c152c796
AM
11357 }
11358
11359 return FALSE;
11360}
11361\f
5241d853
RS
11362/* Initialize COOKIE for input bfd ABFD. */
11363
11364static bfd_boolean
11365init_reloc_cookie (struct elf_reloc_cookie *cookie,
11366 struct bfd_link_info *info, bfd *abfd)
11367{
11368 Elf_Internal_Shdr *symtab_hdr;
11369 const struct elf_backend_data *bed;
11370
11371 bed = get_elf_backend_data (abfd);
11372 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11373
11374 cookie->abfd = abfd;
11375 cookie->sym_hashes = elf_sym_hashes (abfd);
11376 cookie->bad_symtab = elf_bad_symtab (abfd);
11377 if (cookie->bad_symtab)
11378 {
11379 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11380 cookie->extsymoff = 0;
11381 }
11382 else
11383 {
11384 cookie->locsymcount = symtab_hdr->sh_info;
11385 cookie->extsymoff = symtab_hdr->sh_info;
11386 }
11387
11388 if (bed->s->arch_size == 32)
11389 cookie->r_sym_shift = 8;
11390 else
11391 cookie->r_sym_shift = 32;
11392
11393 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11394 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11395 {
11396 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11397 cookie->locsymcount, 0,
11398 NULL, NULL, NULL);
11399 if (cookie->locsyms == NULL)
11400 {
11401 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11402 return FALSE;
11403 }
11404 if (info->keep_memory)
11405 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11406 }
11407 return TRUE;
11408}
11409
11410/* Free the memory allocated by init_reloc_cookie, if appropriate. */
11411
11412static void
11413fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11414{
11415 Elf_Internal_Shdr *symtab_hdr;
11416
11417 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11418 if (cookie->locsyms != NULL
11419 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11420 free (cookie->locsyms);
11421}
11422
11423/* Initialize the relocation information in COOKIE for input section SEC
11424 of input bfd ABFD. */
11425
11426static bfd_boolean
11427init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11428 struct bfd_link_info *info, bfd *abfd,
11429 asection *sec)
11430{
11431 const struct elf_backend_data *bed;
11432
11433 if (sec->reloc_count == 0)
11434 {
11435 cookie->rels = NULL;
11436 cookie->relend = NULL;
11437 }
11438 else
11439 {
11440 bed = get_elf_backend_data (abfd);
11441
11442 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11443 info->keep_memory);
11444 if (cookie->rels == NULL)
11445 return FALSE;
11446 cookie->rel = cookie->rels;
11447 cookie->relend = (cookie->rels
11448 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11449 }
11450 cookie->rel = cookie->rels;
11451 return TRUE;
11452}
11453
11454/* Free the memory allocated by init_reloc_cookie_rels,
11455 if appropriate. */
11456
11457static void
11458fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11459 asection *sec)
11460{
11461 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11462 free (cookie->rels);
11463}
11464
11465/* Initialize the whole of COOKIE for input section SEC. */
11466
11467static bfd_boolean
11468init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11469 struct bfd_link_info *info,
11470 asection *sec)
11471{
11472 if (!init_reloc_cookie (cookie, info, sec->owner))
11473 goto error1;
11474 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11475 goto error2;
11476 return TRUE;
11477
11478 error2:
11479 fini_reloc_cookie (cookie, sec->owner);
11480 error1:
11481 return FALSE;
11482}
11483
11484/* Free the memory allocated by init_reloc_cookie_for_section,
11485 if appropriate. */
11486
11487static void
11488fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11489 asection *sec)
11490{
11491 fini_reloc_cookie_rels (cookie, sec);
11492 fini_reloc_cookie (cookie, sec->owner);
11493}
11494\f
c152c796
AM
11495/* Garbage collect unused sections. */
11496
07adf181
AM
11497/* Default gc_mark_hook. */
11498
11499asection *
11500_bfd_elf_gc_mark_hook (asection *sec,
11501 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11502 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11503 struct elf_link_hash_entry *h,
11504 Elf_Internal_Sym *sym)
11505{
bde6f3eb
L
11506 const char *sec_name;
11507
07adf181
AM
11508 if (h != NULL)
11509 {
11510 switch (h->root.type)
11511 {
11512 case bfd_link_hash_defined:
11513 case bfd_link_hash_defweak:
11514 return h->root.u.def.section;
11515
11516 case bfd_link_hash_common:
11517 return h->root.u.c.p->section;
11518
bde6f3eb
L
11519 case bfd_link_hash_undefined:
11520 case bfd_link_hash_undefweak:
11521 /* To work around a glibc bug, keep all XXX input sections
11522 when there is an as yet undefined reference to __start_XXX
11523 or __stop_XXX symbols. The linker will later define such
11524 symbols for orphan input sections that have a name
11525 representable as a C identifier. */
11526 if (strncmp (h->root.root.string, "__start_", 8) == 0)
11527 sec_name = h->root.root.string + 8;
11528 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
11529 sec_name = h->root.root.string + 7;
11530 else
11531 sec_name = NULL;
11532
11533 if (sec_name && *sec_name != '\0')
11534 {
11535 bfd *i;
11536
11537 for (i = info->input_bfds; i; i = i->link_next)
11538 {
11539 sec = bfd_get_section_by_name (i, sec_name);
11540 if (sec)
11541 sec->flags |= SEC_KEEP;
11542 }
11543 }
11544 break;
11545
07adf181
AM
11546 default:
11547 break;
11548 }
11549 }
11550 else
11551 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11552
11553 return NULL;
11554}
11555
5241d853
RS
11556/* COOKIE->rel describes a relocation against section SEC, which is
11557 a section we've decided to keep. Return the section that contains
11558 the relocation symbol, or NULL if no section contains it. */
11559
11560asection *
11561_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11562 elf_gc_mark_hook_fn gc_mark_hook,
11563 struct elf_reloc_cookie *cookie)
11564{
11565 unsigned long r_symndx;
11566 struct elf_link_hash_entry *h;
11567
11568 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 11569 if (r_symndx == STN_UNDEF)
5241d853
RS
11570 return NULL;
11571
11572 if (r_symndx >= cookie->locsymcount
11573 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11574 {
11575 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11576 while (h->root.type == bfd_link_hash_indirect
11577 || h->root.type == bfd_link_hash_warning)
11578 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11579 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11580 }
11581
11582 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11583 &cookie->locsyms[r_symndx]);
11584}
11585
11586/* COOKIE->rel describes a relocation against section SEC, which is
11587 a section we've decided to keep. Mark the section that contains
9d0a14d3 11588 the relocation symbol. */
5241d853
RS
11589
11590bfd_boolean
11591_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11592 asection *sec,
11593 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 11594 struct elf_reloc_cookie *cookie)
5241d853
RS
11595{
11596 asection *rsec;
11597
11598 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11599 if (rsec && !rsec->gc_mark)
11600 {
11601 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11602 rsec->gc_mark = 1;
5241d853
RS
11603 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11604 return FALSE;
11605 }
11606 return TRUE;
11607}
11608
07adf181
AM
11609/* The mark phase of garbage collection. For a given section, mark
11610 it and any sections in this section's group, and all the sections
11611 which define symbols to which it refers. */
11612
ccfa59ea
AM
11613bfd_boolean
11614_bfd_elf_gc_mark (struct bfd_link_info *info,
11615 asection *sec,
6a5bb875 11616 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
11617{
11618 bfd_boolean ret;
9d0a14d3 11619 asection *group_sec, *eh_frame;
c152c796
AM
11620
11621 sec->gc_mark = 1;
11622
11623 /* Mark all the sections in the group. */
11624 group_sec = elf_section_data (sec)->next_in_group;
11625 if (group_sec && !group_sec->gc_mark)
ccfa59ea 11626 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
11627 return FALSE;
11628
11629 /* Look through the section relocs. */
11630 ret = TRUE;
9d0a14d3
RS
11631 eh_frame = elf_eh_frame_section (sec->owner);
11632 if ((sec->flags & SEC_RELOC) != 0
11633 && sec->reloc_count > 0
11634 && sec != eh_frame)
c152c796 11635 {
5241d853 11636 struct elf_reloc_cookie cookie;
c152c796 11637
5241d853
RS
11638 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11639 ret = FALSE;
c152c796 11640 else
c152c796 11641 {
5241d853 11642 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 11643 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
11644 {
11645 ret = FALSE;
11646 break;
11647 }
11648 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
11649 }
11650 }
9d0a14d3
RS
11651
11652 if (ret && eh_frame && elf_fde_list (sec))
11653 {
11654 struct elf_reloc_cookie cookie;
11655
11656 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11657 ret = FALSE;
11658 else
11659 {
11660 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11661 gc_mark_hook, &cookie))
11662 ret = FALSE;
11663 fini_reloc_cookie_for_section (&cookie, eh_frame);
11664 }
11665 }
11666
c152c796
AM
11667 return ret;
11668}
11669
11670/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11671
c17d87de
NC
11672struct elf_gc_sweep_symbol_info
11673{
ccabcbe5
AM
11674 struct bfd_link_info *info;
11675 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11676 bfd_boolean);
11677};
11678
c152c796 11679static bfd_boolean
ccabcbe5 11680elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 11681{
c152c796
AM
11682 if (h->root.type == bfd_link_hash_warning)
11683 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11684
ccabcbe5
AM
11685 if ((h->root.type == bfd_link_hash_defined
11686 || h->root.type == bfd_link_hash_defweak)
11687 && !h->root.u.def.section->gc_mark
11688 && !(h->root.u.def.section->owner->flags & DYNAMIC))
11689 {
a50b1753
NC
11690 struct elf_gc_sweep_symbol_info *inf =
11691 (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5
AM
11692 (*inf->hide_symbol) (inf->info, h, TRUE);
11693 }
c152c796
AM
11694
11695 return TRUE;
11696}
11697
11698/* The sweep phase of garbage collection. Remove all garbage sections. */
11699
11700typedef bfd_boolean (*gc_sweep_hook_fn)
11701 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11702
11703static bfd_boolean
ccabcbe5 11704elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
11705{
11706 bfd *sub;
ccabcbe5
AM
11707 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11708 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11709 unsigned long section_sym_count;
11710 struct elf_gc_sweep_symbol_info sweep_info;
c152c796
AM
11711
11712 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11713 {
11714 asection *o;
11715
11716 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11717 continue;
11718
11719 for (o = sub->sections; o != NULL; o = o->next)
11720 {
a33dafc3
L
11721 /* When any section in a section group is kept, we keep all
11722 sections in the section group. If the first member of
11723 the section group is excluded, we will also exclude the
11724 group section. */
11725 if (o->flags & SEC_GROUP)
11726 {
11727 asection *first = elf_next_in_group (o);
11728 o->gc_mark = first->gc_mark;
11729 }
11730 else if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
24007750 11731 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
a33dafc3 11732 {
24007750 11733 /* Keep debug and special sections. */
a33dafc3
L
11734 o->gc_mark = 1;
11735 }
c152c796
AM
11736
11737 if (o->gc_mark)
11738 continue;
11739
11740 /* Skip sweeping sections already excluded. */
11741 if (o->flags & SEC_EXCLUDE)
11742 continue;
11743
11744 /* Since this is early in the link process, it is simple
11745 to remove a section from the output. */
11746 o->flags |= SEC_EXCLUDE;
11747
c55fe096 11748 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
11749 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11750
c152c796
AM
11751 /* But we also have to update some of the relocation
11752 info we collected before. */
11753 if (gc_sweep_hook
e8aaee2a
AM
11754 && (o->flags & SEC_RELOC) != 0
11755 && o->reloc_count > 0
11756 && !bfd_is_abs_section (o->output_section))
c152c796
AM
11757 {
11758 Elf_Internal_Rela *internal_relocs;
11759 bfd_boolean r;
11760
11761 internal_relocs
11762 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11763 info->keep_memory);
11764 if (internal_relocs == NULL)
11765 return FALSE;
11766
11767 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11768
11769 if (elf_section_data (o)->relocs != internal_relocs)
11770 free (internal_relocs);
11771
11772 if (!r)
11773 return FALSE;
11774 }
11775 }
11776 }
11777
11778 /* Remove the symbols that were in the swept sections from the dynamic
11779 symbol table. GCFIXME: Anyone know how to get them out of the
11780 static symbol table as well? */
ccabcbe5
AM
11781 sweep_info.info = info;
11782 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11783 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11784 &sweep_info);
c152c796 11785
ccabcbe5 11786 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
11787 return TRUE;
11788}
11789
11790/* Propagate collected vtable information. This is called through
11791 elf_link_hash_traverse. */
11792
11793static bfd_boolean
11794elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11795{
11796 if (h->root.type == bfd_link_hash_warning)
11797 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11798
11799 /* Those that are not vtables. */
f6e332e6 11800 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
11801 return TRUE;
11802
11803 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 11804 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
11805 return TRUE;
11806
11807 /* If we've already been done, exit. */
f6e332e6 11808 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
11809 return TRUE;
11810
11811 /* Make sure the parent's table is up to date. */
f6e332e6 11812 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 11813
f6e332e6 11814 if (h->vtable->used == NULL)
c152c796
AM
11815 {
11816 /* None of this table's entries were referenced. Re-use the
11817 parent's table. */
f6e332e6
AM
11818 h->vtable->used = h->vtable->parent->vtable->used;
11819 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
11820 }
11821 else
11822 {
11823 size_t n;
11824 bfd_boolean *cu, *pu;
11825
11826 /* Or the parent's entries into ours. */
f6e332e6 11827 cu = h->vtable->used;
c152c796 11828 cu[-1] = TRUE;
f6e332e6 11829 pu = h->vtable->parent->vtable->used;
c152c796
AM
11830 if (pu != NULL)
11831 {
11832 const struct elf_backend_data *bed;
11833 unsigned int log_file_align;
11834
11835 bed = get_elf_backend_data (h->root.u.def.section->owner);
11836 log_file_align = bed->s->log_file_align;
f6e332e6 11837 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
11838 while (n--)
11839 {
11840 if (*pu)
11841 *cu = TRUE;
11842 pu++;
11843 cu++;
11844 }
11845 }
11846 }
11847
11848 return TRUE;
11849}
11850
11851static bfd_boolean
11852elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11853{
11854 asection *sec;
11855 bfd_vma hstart, hend;
11856 Elf_Internal_Rela *relstart, *relend, *rel;
11857 const struct elf_backend_data *bed;
11858 unsigned int log_file_align;
11859
11860 if (h->root.type == bfd_link_hash_warning)
11861 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11862
11863 /* Take care of both those symbols that do not describe vtables as
11864 well as those that are not loaded. */
f6e332e6 11865 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
11866 return TRUE;
11867
11868 BFD_ASSERT (h->root.type == bfd_link_hash_defined
11869 || h->root.type == bfd_link_hash_defweak);
11870
11871 sec = h->root.u.def.section;
11872 hstart = h->root.u.def.value;
11873 hend = hstart + h->size;
11874
11875 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11876 if (!relstart)
11877 return *(bfd_boolean *) okp = FALSE;
11878 bed = get_elf_backend_data (sec->owner);
11879 log_file_align = bed->s->log_file_align;
11880
11881 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11882
11883 for (rel = relstart; rel < relend; ++rel)
11884 if (rel->r_offset >= hstart && rel->r_offset < hend)
11885 {
11886 /* If the entry is in use, do nothing. */
f6e332e6
AM
11887 if (h->vtable->used
11888 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
11889 {
11890 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 11891 if (h->vtable->used[entry])
c152c796
AM
11892 continue;
11893 }
11894 /* Otherwise, kill it. */
11895 rel->r_offset = rel->r_info = rel->r_addend = 0;
11896 }
11897
11898 return TRUE;
11899}
11900
87538722
AM
11901/* Mark sections containing dynamically referenced symbols. When
11902 building shared libraries, we must assume that any visible symbol is
11903 referenced. */
715df9b8 11904
64d03ab5
AM
11905bfd_boolean
11906bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 11907{
87538722
AM
11908 struct bfd_link_info *info = (struct bfd_link_info *) inf;
11909
715df9b8
EB
11910 if (h->root.type == bfd_link_hash_warning)
11911 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11912
11913 if ((h->root.type == bfd_link_hash_defined
11914 || h->root.type == bfd_link_hash_defweak)
87538722 11915 && (h->ref_dynamic
5adcfd8b 11916 || (!info->executable
87538722
AM
11917 && h->def_regular
11918 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11919 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
715df9b8
EB
11920 h->root.u.def.section->flags |= SEC_KEEP;
11921
11922 return TRUE;
11923}
3b36f7e6 11924
74f0fb50
AM
11925/* Keep all sections containing symbols undefined on the command-line,
11926 and the section containing the entry symbol. */
11927
11928void
11929_bfd_elf_gc_keep (struct bfd_link_info *info)
11930{
11931 struct bfd_sym_chain *sym;
11932
11933 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
11934 {
11935 struct elf_link_hash_entry *h;
11936
11937 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
11938 FALSE, FALSE, FALSE);
11939
11940 if (h != NULL
11941 && (h->root.type == bfd_link_hash_defined
11942 || h->root.type == bfd_link_hash_defweak)
11943 && !bfd_is_abs_section (h->root.u.def.section))
11944 h->root.u.def.section->flags |= SEC_KEEP;
11945 }
11946}
11947
c152c796
AM
11948/* Do mark and sweep of unused sections. */
11949
11950bfd_boolean
11951bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11952{
11953 bfd_boolean ok = TRUE;
11954 bfd *sub;
6a5bb875 11955 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 11956 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 11957
64d03ab5 11958 if (!bed->can_gc_sections
715df9b8 11959 || !is_elf_hash_table (info->hash))
c152c796
AM
11960 {
11961 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11962 return TRUE;
11963 }
11964
74f0fb50
AM
11965 bed->gc_keep (info);
11966
9d0a14d3
RS
11967 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11968 at the .eh_frame section if we can mark the FDEs individually. */
11969 _bfd_elf_begin_eh_frame_parsing (info);
11970 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11971 {
11972 asection *sec;
11973 struct elf_reloc_cookie cookie;
11974
11975 sec = bfd_get_section_by_name (sub, ".eh_frame");
11976 if (sec && init_reloc_cookie_for_section (&cookie, info, sec))
11977 {
11978 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
11979 if (elf_section_data (sec)->sec_info)
11980 elf_eh_frame_section (sub) = sec;
11981 fini_reloc_cookie_for_section (&cookie, sec);
11982 }
11983 }
11984 _bfd_elf_end_eh_frame_parsing (info);
11985
c152c796
AM
11986 /* Apply transitive closure to the vtable entry usage info. */
11987 elf_link_hash_traverse (elf_hash_table (info),
11988 elf_gc_propagate_vtable_entries_used,
11989 &ok);
11990 if (!ok)
11991 return FALSE;
11992
11993 /* Kill the vtable relocations that were not used. */
11994 elf_link_hash_traverse (elf_hash_table (info),
11995 elf_gc_smash_unused_vtentry_relocs,
11996 &ok);
11997 if (!ok)
11998 return FALSE;
11999
715df9b8
EB
12000 /* Mark dynamically referenced symbols. */
12001 if (elf_hash_table (info)->dynamic_sections_created)
12002 elf_link_hash_traverse (elf_hash_table (info),
64d03ab5 12003 bed->gc_mark_dynamic_ref,
87538722 12004 info);
c152c796 12005
715df9b8 12006 /* Grovel through relocs to find out who stays ... */
64d03ab5 12007 gc_mark_hook = bed->gc_mark_hook;
c152c796
AM
12008 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
12009 {
12010 asection *o;
12011
12012 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
12013 continue;
12014
24007750 12015 /* Also keep SHT_NOTE sections. */
c152c796 12016 for (o = sub->sections; o != NULL; o = o->next)
24007750
L
12017 if ((o->flags & SEC_EXCLUDE) == 0
12018 && ((o->flags & SEC_KEEP) != 0
12019 || elf_section_data (o)->this_hdr.sh_type == SHT_NOTE)
12020 && !o->gc_mark)
39c2f51b
AM
12021 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12022 return FALSE;
c152c796
AM
12023 }
12024
6a5bb875
PB
12025 /* Allow the backend to mark additional target specific sections. */
12026 if (bed->gc_mark_extra_sections)
74f0fb50 12027 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 12028
c152c796 12029 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 12030 return elf_gc_sweep (abfd, info);
c152c796
AM
12031}
12032\f
12033/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12034
12035bfd_boolean
12036bfd_elf_gc_record_vtinherit (bfd *abfd,
12037 asection *sec,
12038 struct elf_link_hash_entry *h,
12039 bfd_vma offset)
12040{
12041 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12042 struct elf_link_hash_entry **search, *child;
12043 bfd_size_type extsymcount;
12044 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12045
12046 /* The sh_info field of the symtab header tells us where the
12047 external symbols start. We don't care about the local symbols at
12048 this point. */
12049 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12050 if (!elf_bad_symtab (abfd))
12051 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12052
12053 sym_hashes = elf_sym_hashes (abfd);
12054 sym_hashes_end = sym_hashes + extsymcount;
12055
12056 /* Hunt down the child symbol, which is in this section at the same
12057 offset as the relocation. */
12058 for (search = sym_hashes; search != sym_hashes_end; ++search)
12059 {
12060 if ((child = *search) != NULL
12061 && (child->root.type == bfd_link_hash_defined
12062 || child->root.type == bfd_link_hash_defweak)
12063 && child->root.u.def.section == sec
12064 && child->root.u.def.value == offset)
12065 goto win;
12066 }
12067
d003868e
AM
12068 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12069 abfd, sec, (unsigned long) offset);
c152c796
AM
12070 bfd_set_error (bfd_error_invalid_operation);
12071 return FALSE;
12072
12073 win:
f6e332e6
AM
12074 if (!child->vtable)
12075 {
a50b1753
NC
12076 child->vtable = (struct elf_link_virtual_table_entry *)
12077 bfd_zalloc (abfd, sizeof (*child->vtable));
f6e332e6
AM
12078 if (!child->vtable)
12079 return FALSE;
12080 }
c152c796
AM
12081 if (!h)
12082 {
12083 /* This *should* only be the absolute section. It could potentially
12084 be that someone has defined a non-global vtable though, which
12085 would be bad. It isn't worth paging in the local symbols to be
12086 sure though; that case should simply be handled by the assembler. */
12087
f6e332e6 12088 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
12089 }
12090 else
f6e332e6 12091 child->vtable->parent = h;
c152c796
AM
12092
12093 return TRUE;
12094}
12095
12096/* Called from check_relocs to record the existence of a VTENTRY reloc. */
12097
12098bfd_boolean
12099bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12100 asection *sec ATTRIBUTE_UNUSED,
12101 struct elf_link_hash_entry *h,
12102 bfd_vma addend)
12103{
12104 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12105 unsigned int log_file_align = bed->s->log_file_align;
12106
f6e332e6
AM
12107 if (!h->vtable)
12108 {
a50b1753
NC
12109 h->vtable = (struct elf_link_virtual_table_entry *)
12110 bfd_zalloc (abfd, sizeof (*h->vtable));
f6e332e6
AM
12111 if (!h->vtable)
12112 return FALSE;
12113 }
12114
12115 if (addend >= h->vtable->size)
c152c796
AM
12116 {
12117 size_t size, bytes, file_align;
f6e332e6 12118 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
12119
12120 /* While the symbol is undefined, we have to be prepared to handle
12121 a zero size. */
12122 file_align = 1 << log_file_align;
12123 if (h->root.type == bfd_link_hash_undefined)
12124 size = addend + file_align;
12125 else
12126 {
12127 size = h->size;
12128 if (addend >= size)
12129 {
12130 /* Oops! We've got a reference past the defined end of
12131 the table. This is probably a bug -- shall we warn? */
12132 size = addend + file_align;
12133 }
12134 }
12135 size = (size + file_align - 1) & -file_align;
12136
12137 /* Allocate one extra entry for use as a "done" flag for the
12138 consolidation pass. */
12139 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12140
12141 if (ptr)
12142 {
a50b1753 12143 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
12144
12145 if (ptr != NULL)
12146 {
12147 size_t oldbytes;
12148
f6e332e6 12149 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
12150 * sizeof (bfd_boolean));
12151 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12152 }
12153 }
12154 else
a50b1753 12155 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
12156
12157 if (ptr == NULL)
12158 return FALSE;
12159
12160 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
12161 h->vtable->used = ptr + 1;
12162 h->vtable->size = size;
c152c796
AM
12163 }
12164
f6e332e6 12165 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
12166
12167 return TRUE;
12168}
12169
12170struct alloc_got_off_arg {
12171 bfd_vma gotoff;
10455f89 12172 struct bfd_link_info *info;
c152c796
AM
12173};
12174
12175/* We need a special top-level link routine to convert got reference counts
12176 to real got offsets. */
12177
12178static bfd_boolean
12179elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
12180{
a50b1753 12181 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
12182 bfd *obfd = gofarg->info->output_bfd;
12183 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796
AM
12184
12185 if (h->root.type == bfd_link_hash_warning)
12186 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12187
12188 if (h->got.refcount > 0)
12189 {
12190 h->got.offset = gofarg->gotoff;
10455f89 12191 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
12192 }
12193 else
12194 h->got.offset = (bfd_vma) -1;
12195
12196 return TRUE;
12197}
12198
12199/* And an accompanying bit to work out final got entry offsets once
12200 we're done. Should be called from final_link. */
12201
12202bfd_boolean
12203bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
12204 struct bfd_link_info *info)
12205{
12206 bfd *i;
12207 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12208 bfd_vma gotoff;
c152c796
AM
12209 struct alloc_got_off_arg gofarg;
12210
10455f89
HPN
12211 BFD_ASSERT (abfd == info->output_bfd);
12212
c152c796
AM
12213 if (! is_elf_hash_table (info->hash))
12214 return FALSE;
12215
12216 /* The GOT offset is relative to the .got section, but the GOT header is
12217 put into the .got.plt section, if the backend uses it. */
12218 if (bed->want_got_plt)
12219 gotoff = 0;
12220 else
12221 gotoff = bed->got_header_size;
12222
12223 /* Do the local .got entries first. */
12224 for (i = info->input_bfds; i; i = i->link_next)
12225 {
12226 bfd_signed_vma *local_got;
12227 bfd_size_type j, locsymcount;
12228 Elf_Internal_Shdr *symtab_hdr;
12229
12230 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
12231 continue;
12232
12233 local_got = elf_local_got_refcounts (i);
12234 if (!local_got)
12235 continue;
12236
12237 symtab_hdr = &elf_tdata (i)->symtab_hdr;
12238 if (elf_bad_symtab (i))
12239 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12240 else
12241 locsymcount = symtab_hdr->sh_info;
12242
12243 for (j = 0; j < locsymcount; ++j)
12244 {
12245 if (local_got[j] > 0)
12246 {
12247 local_got[j] = gotoff;
10455f89 12248 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
12249 }
12250 else
12251 local_got[j] = (bfd_vma) -1;
12252 }
12253 }
12254
12255 /* Then the global .got entries. .plt refcounts are handled by
12256 adjust_dynamic_symbol */
12257 gofarg.gotoff = gotoff;
10455f89 12258 gofarg.info = info;
c152c796
AM
12259 elf_link_hash_traverse (elf_hash_table (info),
12260 elf_gc_allocate_got_offsets,
12261 &gofarg);
12262 return TRUE;
12263}
12264
12265/* Many folk need no more in the way of final link than this, once
12266 got entry reference counting is enabled. */
12267
12268bfd_boolean
12269bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
12270{
12271 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
12272 return FALSE;
12273
12274 /* Invoke the regular ELF backend linker to do all the work. */
12275 return bfd_elf_final_link (abfd, info);
12276}
12277
12278bfd_boolean
12279bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
12280{
a50b1753 12281 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
12282
12283 if (rcookie->bad_symtab)
12284 rcookie->rel = rcookie->rels;
12285
12286 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
12287 {
12288 unsigned long r_symndx;
12289
12290 if (! rcookie->bad_symtab)
12291 if (rcookie->rel->r_offset > offset)
12292 return FALSE;
12293 if (rcookie->rel->r_offset != offset)
12294 continue;
12295
12296 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 12297 if (r_symndx == STN_UNDEF)
c152c796
AM
12298 return TRUE;
12299
12300 if (r_symndx >= rcookie->locsymcount
12301 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12302 {
12303 struct elf_link_hash_entry *h;
12304
12305 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
12306
12307 while (h->root.type == bfd_link_hash_indirect
12308 || h->root.type == bfd_link_hash_warning)
12309 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12310
12311 if ((h->root.type == bfd_link_hash_defined
12312 || h->root.type == bfd_link_hash_defweak)
12313 && elf_discarded_section (h->root.u.def.section))
12314 return TRUE;
12315 else
12316 return FALSE;
12317 }
12318 else
12319 {
12320 /* It's not a relocation against a global symbol,
12321 but it could be a relocation against a local
12322 symbol for a discarded section. */
12323 asection *isec;
12324 Elf_Internal_Sym *isym;
12325
12326 /* Need to: get the symbol; get the section. */
12327 isym = &rcookie->locsyms[r_symndx];
cb33740c
AM
12328 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
12329 if (isec != NULL && elf_discarded_section (isec))
12330 return TRUE;
c152c796
AM
12331 }
12332 return FALSE;
12333 }
12334 return FALSE;
12335}
12336
12337/* Discard unneeded references to discarded sections.
12338 Returns TRUE if any section's size was changed. */
12339/* This function assumes that the relocations are in sorted order,
12340 which is true for all known assemblers. */
12341
12342bfd_boolean
12343bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
12344{
12345 struct elf_reloc_cookie cookie;
12346 asection *stab, *eh;
c152c796
AM
12347 const struct elf_backend_data *bed;
12348 bfd *abfd;
c152c796
AM
12349 bfd_boolean ret = FALSE;
12350
12351 if (info->traditional_format
12352 || !is_elf_hash_table (info->hash))
12353 return FALSE;
12354
ca92cecb 12355 _bfd_elf_begin_eh_frame_parsing (info);
c152c796
AM
12356 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
12357 {
12358 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
12359 continue;
12360
12361 bed = get_elf_backend_data (abfd);
12362
12363 if ((abfd->flags & DYNAMIC) != 0)
12364 continue;
12365
8da3dbc5
AM
12366 eh = NULL;
12367 if (!info->relocatable)
12368 {
12369 eh = bfd_get_section_by_name (abfd, ".eh_frame");
12370 if (eh != NULL
eea6121a 12371 && (eh->size == 0
8da3dbc5
AM
12372 || bfd_is_abs_section (eh->output_section)))
12373 eh = NULL;
12374 }
c152c796
AM
12375
12376 stab = bfd_get_section_by_name (abfd, ".stab");
12377 if (stab != NULL
eea6121a 12378 && (stab->size == 0
c152c796
AM
12379 || bfd_is_abs_section (stab->output_section)
12380 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
12381 stab = NULL;
12382
12383 if (stab == NULL
12384 && eh == NULL
12385 && bed->elf_backend_discard_info == NULL)
12386 continue;
12387
5241d853
RS
12388 if (!init_reloc_cookie (&cookie, info, abfd))
12389 return FALSE;
c152c796 12390
5241d853
RS
12391 if (stab != NULL
12392 && stab->reloc_count > 0
12393 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
c152c796 12394 {
5241d853
RS
12395 if (_bfd_discard_section_stabs (abfd, stab,
12396 elf_section_data (stab)->sec_info,
12397 bfd_elf_reloc_symbol_deleted_p,
12398 &cookie))
12399 ret = TRUE;
12400 fini_reloc_cookie_rels (&cookie, stab);
c152c796
AM
12401 }
12402
5241d853
RS
12403 if (eh != NULL
12404 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
c152c796 12405 {
ca92cecb 12406 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
c152c796
AM
12407 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12408 bfd_elf_reloc_symbol_deleted_p,
12409 &cookie))
12410 ret = TRUE;
5241d853 12411 fini_reloc_cookie_rels (&cookie, eh);
c152c796
AM
12412 }
12413
12414 if (bed->elf_backend_discard_info != NULL
12415 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12416 ret = TRUE;
12417
5241d853 12418 fini_reloc_cookie (&cookie, abfd);
c152c796 12419 }
ca92cecb 12420 _bfd_elf_end_eh_frame_parsing (info);
c152c796
AM
12421
12422 if (info->eh_frame_hdr
12423 && !info->relocatable
12424 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12425 ret = TRUE;
12426
12427 return ret;
12428}
082b7297 12429
9659de1c
AM
12430/* For a SHT_GROUP section, return the group signature. For other
12431 sections, return the normal section name. */
12432
12433static const char *
12434section_signature (asection *sec)
12435{
12436 if ((sec->flags & SEC_GROUP) != 0
12437 && elf_next_in_group (sec) != NULL
12438 && elf_group_name (elf_next_in_group (sec)) != NULL)
12439 return elf_group_name (elf_next_in_group (sec));
12440 return sec->name;
12441}
12442
082b7297 12443void
9659de1c 12444_bfd_elf_section_already_linked (bfd *abfd, asection *sec,
c0f00686 12445 struct bfd_link_info *info)
082b7297
L
12446{
12447 flagword flags;
6d2cd210 12448 const char *name, *p;
082b7297
L
12449 struct bfd_section_already_linked *l;
12450 struct bfd_section_already_linked_hash_entry *already_linked_list;
3d7f7666 12451
3d7f7666
L
12452 if (sec->output_section == bfd_abs_section_ptr)
12453 return;
082b7297
L
12454
12455 flags = sec->flags;
3d7f7666 12456
c2370991
AM
12457 /* Return if it isn't a linkonce section. A comdat group section
12458 also has SEC_LINK_ONCE set. */
12459 if ((flags & SEC_LINK_ONCE) == 0)
082b7297
L
12460 return;
12461
c2370991
AM
12462 /* Don't put group member sections on our list of already linked
12463 sections. They are handled as a group via their group section. */
12464 if (elf_sec_group (sec) != NULL)
12465 return;
3d7f7666 12466
082b7297
L
12467 /* FIXME: When doing a relocatable link, we may have trouble
12468 copying relocations in other sections that refer to local symbols
12469 in the section being discarded. Those relocations will have to
12470 be converted somehow; as of this writing I'm not sure that any of
12471 the backends handle that correctly.
12472
12473 It is tempting to instead not discard link once sections when
12474 doing a relocatable link (technically, they should be discarded
12475 whenever we are building constructors). However, that fails,
12476 because the linker winds up combining all the link once sections
12477 into a single large link once section, which defeats the purpose
12478 of having link once sections in the first place.
12479
12480 Also, not merging link once sections in a relocatable link
12481 causes trouble for MIPS ELF, which relies on link once semantics
12482 to handle the .reginfo section correctly. */
12483
9659de1c 12484 name = section_signature (sec);
082b7297 12485
0112cd26 12486 if (CONST_STRNEQ (name, ".gnu.linkonce.")
6d2cd210
JJ
12487 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12488 p++;
12489 else
12490 p = name;
12491
12492 already_linked_list = bfd_section_already_linked_table_lookup (p);
082b7297
L
12493
12494 for (l = already_linked_list->entry; l != NULL; l = l->next)
12495 {
c2370991
AM
12496 /* We may have 2 different types of sections on the list: group
12497 sections and linkonce sections. Match like sections. */
3d7f7666 12498 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9659de1c 12499 && strcmp (name, section_signature (l->sec)) == 0
082b7297
L
12500 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
12501 {
12502 /* The section has already been linked. See if we should
6d2cd210 12503 issue a warning. */
082b7297
L
12504 switch (flags & SEC_LINK_DUPLICATES)
12505 {
12506 default:
12507 abort ();
12508
12509 case SEC_LINK_DUPLICATES_DISCARD:
12510 break;
12511
12512 case SEC_LINK_DUPLICATES_ONE_ONLY:
12513 (*_bfd_error_handler)
c93625e2 12514 (_("%B: ignoring duplicate section `%A'"),
d003868e 12515 abfd, sec);
082b7297
L
12516 break;
12517
12518 case SEC_LINK_DUPLICATES_SAME_SIZE:
12519 if (sec->size != l->sec->size)
12520 (*_bfd_error_handler)
c93625e2 12521 (_("%B: duplicate section `%A' has different size"),
d003868e 12522 abfd, sec);
082b7297 12523 break;
ea5158d8
DJ
12524
12525 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
12526 if (sec->size != l->sec->size)
12527 (*_bfd_error_handler)
c93625e2 12528 (_("%B: duplicate section `%A' has different size"),
ea5158d8
DJ
12529 abfd, sec);
12530 else if (sec->size != 0)
12531 {
12532 bfd_byte *sec_contents, *l_sec_contents;
12533
12534 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
12535 (*_bfd_error_handler)
c93625e2 12536 (_("%B: warning: could not read contents of section `%A'"),
ea5158d8
DJ
12537 abfd, sec);
12538 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
12539 &l_sec_contents))
12540 (*_bfd_error_handler)
c93625e2 12541 (_("%B: warning: could not read contents of section `%A'"),
ea5158d8
DJ
12542 l->sec->owner, l->sec);
12543 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
12544 (*_bfd_error_handler)
c93625e2 12545 (_("%B: warning: duplicate section `%A' has different contents"),
ea5158d8
DJ
12546 abfd, sec);
12547
12548 if (sec_contents)
12549 free (sec_contents);
12550 if (l_sec_contents)
12551 free (l_sec_contents);
12552 }
12553 break;
082b7297
L
12554 }
12555
12556 /* Set the output_section field so that lang_add_section
12557 does not create a lang_input_section structure for this
12558 section. Since there might be a symbol in the section
12559 being discarded, we must retain a pointer to the section
12560 which we are really going to use. */
12561 sec->output_section = bfd_abs_section_ptr;
12562 sec->kept_section = l->sec;
3b36f7e6 12563
082b7297 12564 if (flags & SEC_GROUP)
3d7f7666
L
12565 {
12566 asection *first = elf_next_in_group (sec);
12567 asection *s = first;
12568
12569 while (s != NULL)
12570 {
12571 s->output_section = bfd_abs_section_ptr;
12572 /* Record which group discards it. */
12573 s->kept_section = l->sec;
12574 s = elf_next_in_group (s);
12575 /* These lists are circular. */
12576 if (s == first)
12577 break;
12578 }
12579 }
082b7297
L
12580
12581 return;
12582 }
12583 }
12584
c2370991
AM
12585 /* A single member comdat group section may be discarded by a
12586 linkonce section and vice versa. */
12587
12588 if ((flags & SEC_GROUP) != 0)
3d7f7666 12589 {
c2370991
AM
12590 asection *first = elf_next_in_group (sec);
12591
12592 if (first != NULL && elf_next_in_group (first) == first)
12593 /* Check this single member group against linkonce sections. */
12594 for (l = already_linked_list->entry; l != NULL; l = l->next)
12595 if ((l->sec->flags & SEC_GROUP) == 0
12596 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
12597 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12598 {
12599 first->output_section = bfd_abs_section_ptr;
12600 first->kept_section = l->sec;
12601 sec->output_section = bfd_abs_section_ptr;
12602 break;
12603 }
3d7f7666
L
12604 }
12605 else
c2370991 12606 /* Check this linkonce section against single member groups. */
6d2cd210
JJ
12607 for (l = already_linked_list->entry; l != NULL; l = l->next)
12608 if (l->sec->flags & SEC_GROUP)
12609 {
12610 asection *first = elf_next_in_group (l->sec);
12611
12612 if (first != NULL
12613 && elf_next_in_group (first) == first
c0f00686 12614 && bfd_elf_match_symbols_in_sections (first, sec, info))
6d2cd210
JJ
12615 {
12616 sec->output_section = bfd_abs_section_ptr;
c2370991 12617 sec->kept_section = first;
6d2cd210
JJ
12618 break;
12619 }
12620 }
12621
80c29487
JK
12622 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
12623 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
12624 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
12625 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
12626 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
12627 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
12628 `.gnu.linkonce.t.F' section from a different bfd not requiring any
12629 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
12630 The reverse order cannot happen as there is never a bfd with only the
12631 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
12632 matter as here were are looking only for cross-bfd sections. */
12633
12634 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
12635 for (l = already_linked_list->entry; l != NULL; l = l->next)
12636 if ((l->sec->flags & SEC_GROUP) == 0
12637 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
12638 {
12639 if (abfd != l->sec->owner)
12640 sec->output_section = bfd_abs_section_ptr;
12641 break;
12642 }
12643
082b7297 12644 /* This is the first section with this name. Record it. */
a6626e8c 12645 if (! bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 12646 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
082b7297 12647}
81e1b023 12648
a4d8e49b
L
12649bfd_boolean
12650_bfd_elf_common_definition (Elf_Internal_Sym *sym)
12651{
12652 return sym->st_shndx == SHN_COMMON;
12653}
12654
12655unsigned int
12656_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12657{
12658 return SHN_COMMON;
12659}
12660
12661asection *
12662_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12663{
12664 return bfd_com_section_ptr;
12665}
10455f89
HPN
12666
12667bfd_vma
12668_bfd_elf_default_got_elt_size (bfd *abfd,
12669 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12670 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12671 bfd *ibfd ATTRIBUTE_UNUSED,
12672 unsigned long symndx ATTRIBUTE_UNUSED)
12673{
12674 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12675 return bed->s->arch_size / 8;
12676}
83bac4b0
NC
12677
12678/* Routines to support the creation of dynamic relocs. */
12679
83bac4b0
NC
12680/* Returns the name of the dynamic reloc section associated with SEC. */
12681
12682static const char *
12683get_dynamic_reloc_section_name (bfd * abfd,
12684 asection * sec,
12685 bfd_boolean is_rela)
12686{
ddcf1fcf
BS
12687 char *name;
12688 const char *old_name = bfd_get_section_name (NULL, sec);
12689 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 12690
ddcf1fcf 12691 if (old_name == NULL)
83bac4b0
NC
12692 return NULL;
12693
ddcf1fcf
BS
12694 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
12695 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
12696
12697 return name;
12698}
12699
12700/* Returns the dynamic reloc section associated with SEC.
12701 If necessary compute the name of the dynamic reloc section based
12702 on SEC's name (looked up in ABFD's string table) and the setting
12703 of IS_RELA. */
12704
12705asection *
12706_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
12707 asection * sec,
12708 bfd_boolean is_rela)
12709{
12710 asection * reloc_sec = elf_section_data (sec)->sreloc;
12711
12712 if (reloc_sec == NULL)
12713 {
12714 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12715
12716 if (name != NULL)
12717 {
12718 reloc_sec = bfd_get_section_by_name (abfd, name);
12719
12720 if (reloc_sec != NULL)
12721 elf_section_data (sec)->sreloc = reloc_sec;
12722 }
12723 }
12724
12725 return reloc_sec;
12726}
12727
12728/* Returns the dynamic reloc section associated with SEC. If the
12729 section does not exist it is created and attached to the DYNOBJ
12730 bfd and stored in the SRELOC field of SEC's elf_section_data
12731 structure.
f8076f98 12732
83bac4b0
NC
12733 ALIGNMENT is the alignment for the newly created section and
12734 IS_RELA defines whether the name should be .rela.<SEC's name>
12735 or .rel.<SEC's name>. The section name is looked up in the
12736 string table associated with ABFD. */
12737
12738asection *
12739_bfd_elf_make_dynamic_reloc_section (asection * sec,
12740 bfd * dynobj,
12741 unsigned int alignment,
12742 bfd * abfd,
12743 bfd_boolean is_rela)
12744{
12745 asection * reloc_sec = elf_section_data (sec)->sreloc;
12746
12747 if (reloc_sec == NULL)
12748 {
12749 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12750
12751 if (name == NULL)
12752 return NULL;
12753
12754 reloc_sec = bfd_get_section_by_name (dynobj, name);
12755
12756 if (reloc_sec == NULL)
12757 {
12758 flagword flags;
12759
12760 flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_IN_MEMORY | SEC_LINKER_CREATED);
12761 if ((sec->flags & SEC_ALLOC) != 0)
12762 flags |= SEC_ALLOC | SEC_LOAD;
12763
12764 reloc_sec = bfd_make_section_with_flags (dynobj, name, flags);
12765 if (reloc_sec != NULL)
12766 {
12767 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
12768 reloc_sec = NULL;
12769 }
12770 }
12771
12772 elf_section_data (sec)->sreloc = reloc_sec;
12773 }
12774
12775 return reloc_sec;
12776}
1338dd10
PB
12777
12778/* Copy the ELF symbol type associated with a linker hash entry. */
12779void
12780_bfd_elf_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
12781 struct bfd_link_hash_entry * hdest,
12782 struct bfd_link_hash_entry * hsrc)
12783{
12784 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *)hdest;
12785 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *)hsrc;
12786
12787 ehdest->type = ehsrc->type;
35fc36a8 12788 ehdest->target_internal = ehsrc->target_internal;
1338dd10 12789}
351f65ca
L
12790
12791/* Append a RELA relocation REL to section S in BFD. */
12792
12793void
12794elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12795{
12796 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12797 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
12798 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
12799 bed->s->swap_reloca_out (abfd, rel, loc);
12800}
12801
12802/* Append a REL relocation REL to section S in BFD. */
12803
12804void
12805elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12806{
12807 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12808 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
12809 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
12810 bed->s->swap_reloca_out (abfd, rel, loc);
12811}
This page took 1.470605 seconds and 4 git commands to generate.