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