PR ld/14207
[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,
dbaa2011 3 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
9dbe8890 4 Free Software Foundation, Inc.
252b5132 5
8fdd7217 6 This file is part of BFD, the Binary File Descriptor library.
252b5132 7
8fdd7217
NC
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
cd123cb7 10 the Free Software Foundation; either version 3 of the License, or
8fdd7217 11 (at your option) any later version.
252b5132 12
8fdd7217
NC
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
252b5132 17
8fdd7217
NC
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
cd123cb7
NC
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
252b5132 22
252b5132 23#include "sysdep.h"
3db64b00 24#include "bfd.h"
252b5132
RH
25#include "bfdlink.h"
26#include "libbfd.h"
27#define ARCH_SIZE 0
28#include "elf-bfd.h"
4ad4eba5 29#include "safe-ctype.h"
ccf2f652 30#include "libiberty.h"
66eb6687 31#include "objalloc.h"
252b5132 32
28caa186
AM
33/* This struct is used to pass information to routines called via
34 elf_link_hash_traverse which must return failure. */
35
36struct elf_info_failed
37{
38 struct bfd_link_info *info;
28caa186
AM
39 bfd_boolean failed;
40};
41
42/* This structure is used to pass information to
43 _bfd_elf_link_find_version_dependencies. */
44
45struct elf_find_verdep_info
46{
47 /* General link information. */
48 struct bfd_link_info *info;
49 /* The number of dependencies. */
50 unsigned int vers;
51 /* Whether we had a failure. */
52 bfd_boolean failed;
53};
54
55static bfd_boolean _bfd_elf_fix_symbol_flags
56 (struct elf_link_hash_entry *, struct elf_info_failed *);
57
d98685ac
AM
58/* Define a symbol in a dynamic linkage section. */
59
60struct elf_link_hash_entry *
61_bfd_elf_define_linkage_sym (bfd *abfd,
62 struct bfd_link_info *info,
63 asection *sec,
64 const char *name)
65{
66 struct elf_link_hash_entry *h;
67 struct bfd_link_hash_entry *bh;
ccabcbe5 68 const struct elf_backend_data *bed;
d98685ac
AM
69
70 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
71 if (h != NULL)
72 {
73 /* Zap symbol defined in an as-needed lib that wasn't linked.
74 This is a symptom of a larger problem: Absolute symbols
75 defined in shared libraries can't be overridden, because we
76 lose the link to the bfd which is via the symbol section. */
77 h->root.type = bfd_link_hash_new;
78 }
79
80 bh = &h->root;
81 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
82 sec, 0, NULL, FALSE,
83 get_elf_backend_data (abfd)->collect,
84 &bh))
85 return NULL;
86 h = (struct elf_link_hash_entry *) bh;
87 h->def_regular = 1;
e28df02b 88 h->non_elf = 0;
d98685ac
AM
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);
6de2ae4a 104 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
105
106 /* This function may be called more than once. */
3d4d4302
AM
107 s = bfd_get_linker_section (abfd, ".got");
108 if (s != NULL)
b34976b6 109 return TRUE;
252b5132 110
e5a52504 111 flags = bed->dynamic_sec_flags;
252b5132 112
14b2f831
AM
113 s = bfd_make_section_anyway_with_flags (abfd,
114 (bed->rela_plts_and_copies_p
115 ? ".rela.got" : ".rel.got"),
116 (bed->dynamic_sec_flags
117 | SEC_READONLY));
6de2ae4a
L
118 if (s == NULL
119 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
120 return FALSE;
121 htab->srelgot = s;
252b5132 122
14b2f831 123 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
124 if (s == NULL
125 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
126 return FALSE;
127 htab->sgot = s;
128
252b5132
RH
129 if (bed->want_got_plt)
130 {
14b2f831 131 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 132 if (s == NULL
6de2ae4a
L
133 || !bfd_set_section_alignment (abfd, s,
134 bed->s->log_file_align))
b34976b6 135 return FALSE;
6de2ae4a 136 htab->sgotplt = s;
252b5132
RH
137 }
138
64e77c6d
L
139 /* The first bit of the global offset table is the header. */
140 s->size += bed->got_header_size;
141
2517a57f
AM
142 if (bed->want_got_sym)
143 {
144 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
145 (or .got.plt) section. We don't do this in the linker script
146 because we don't want to define the symbol if we are not creating
147 a global offset table. */
6de2ae4a
L
148 h = _bfd_elf_define_linkage_sym (abfd, info, s,
149 "_GLOBAL_OFFSET_TABLE_");
2517a57f 150 elf_hash_table (info)->hgot = h;
d98685ac
AM
151 if (h == NULL)
152 return FALSE;
2517a57f 153 }
252b5132 154
b34976b6 155 return TRUE;
252b5132
RH
156}
157\f
7e9f0867
AM
158/* Create a strtab to hold the dynamic symbol names. */
159static bfd_boolean
160_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
161{
162 struct elf_link_hash_table *hash_table;
163
164 hash_table = elf_hash_table (info);
165 if (hash_table->dynobj == NULL)
166 hash_table->dynobj = abfd;
167
168 if (hash_table->dynstr == NULL)
169 {
170 hash_table->dynstr = _bfd_elf_strtab_init ();
171 if (hash_table->dynstr == NULL)
172 return FALSE;
173 }
174 return TRUE;
175}
176
45d6a902
AM
177/* Create some sections which will be filled in with dynamic linking
178 information. ABFD is an input file which requires dynamic sections
179 to be created. The dynamic sections take up virtual memory space
180 when the final executable is run, so we need to create them before
181 addresses are assigned to the output sections. We work out the
182 actual contents and size of these sections later. */
252b5132 183
b34976b6 184bfd_boolean
268b6b39 185_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 186{
45d6a902 187 flagword flags;
91d6fa6a 188 asection *s;
9c5bfbb7 189 const struct elf_backend_data *bed;
252b5132 190
0eddce27 191 if (! is_elf_hash_table (info->hash))
45d6a902
AM
192 return FALSE;
193
194 if (elf_hash_table (info)->dynamic_sections_created)
195 return TRUE;
196
7e9f0867
AM
197 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
198 return FALSE;
45d6a902 199
7e9f0867 200 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
201 bed = get_elf_backend_data (abfd);
202
203 flags = bed->dynamic_sec_flags;
45d6a902
AM
204
205 /* A dynamically linked executable has a .interp section, but a
206 shared library does not. */
36af4a4e 207 if (info->executable)
252b5132 208 {
14b2f831
AM
209 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
210 flags | SEC_READONLY);
3496cb2a 211 if (s == NULL)
45d6a902
AM
212 return FALSE;
213 }
bb0deeff 214
45d6a902
AM
215 /* Create sections to hold version informations. These are removed
216 if they are not needed. */
14b2f831
AM
217 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
218 flags | SEC_READONLY);
45d6a902 219 if (s == NULL
45d6a902
AM
220 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
221 return FALSE;
222
14b2f831
AM
223 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
224 flags | SEC_READONLY);
45d6a902 225 if (s == NULL
45d6a902
AM
226 || ! bfd_set_section_alignment (abfd, s, 1))
227 return FALSE;
228
14b2f831
AM
229 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
230 flags | SEC_READONLY);
45d6a902 231 if (s == NULL
45d6a902
AM
232 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
233 return FALSE;
234
14b2f831
AM
235 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
236 flags | SEC_READONLY);
45d6a902 237 if (s == NULL
45d6a902
AM
238 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
239 return FALSE;
240
14b2f831
AM
241 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
242 flags | SEC_READONLY);
3496cb2a 243 if (s == NULL)
45d6a902
AM
244 return FALSE;
245
14b2f831 246 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 247 if (s == NULL
45d6a902
AM
248 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
249 return FALSE;
250
251 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
252 .dynamic section. We could set _DYNAMIC in a linker script, but we
253 only want to define it if we are, in fact, creating a .dynamic
254 section. We don't want to define it if there is no .dynamic
255 section, since on some ELF platforms the start up code examines it
256 to decide how to initialize the process. */
d98685ac 257 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
45d6a902
AM
258 return FALSE;
259
fdc90cb4
JJ
260 if (info->emit_hash)
261 {
14b2f831
AM
262 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
263 flags | SEC_READONLY);
fdc90cb4
JJ
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 {
14b2f831
AM
272 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
273 flags | SEC_READONLY);
fdc90cb4
JJ
274 if (s == NULL
275 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
276 return FALSE;
277 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
278 4 32-bit words followed by variable count of 64-bit words, then
279 variable count of 32-bit words. */
280 if (bed->s->arch_size == 64)
281 elf_section_data (s)->this_hdr.sh_entsize = 0;
282 else
283 elf_section_data (s)->this_hdr.sh_entsize = 4;
284 }
45d6a902
AM
285
286 /* Let the backend create the rest of the sections. This lets the
287 backend set the right flags. The backend will normally create
288 the .got and .plt sections. */
894891db
NC
289 if (bed->elf_backend_create_dynamic_sections == NULL
290 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
291 return FALSE;
292
293 elf_hash_table (info)->dynamic_sections_created = TRUE;
294
295 return TRUE;
296}
297
298/* Create dynamic sections when linking against a dynamic object. */
299
300bfd_boolean
268b6b39 301_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
302{
303 flagword flags, pltflags;
7325306f 304 struct elf_link_hash_entry *h;
45d6a902 305 asection *s;
9c5bfbb7 306 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 307 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 308
252b5132
RH
309 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
310 .rel[a].bss sections. */
e5a52504 311 flags = bed->dynamic_sec_flags;
252b5132
RH
312
313 pltflags = flags;
252b5132 314 if (bed->plt_not_loaded)
6df4d94c
MM
315 /* We do not clear SEC_ALLOC here because we still want the OS to
316 allocate space for the section; it's just that there's nothing
317 to read in from the object file. */
5d1634d7 318 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
319 else
320 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
321 if (bed->plt_readonly)
322 pltflags |= SEC_READONLY;
323
14b2f831 324 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 325 if (s == NULL
252b5132 326 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 327 return FALSE;
6de2ae4a 328 htab->splt = s;
252b5132 329
d98685ac
AM
330 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
331 .plt section. */
7325306f
RS
332 if (bed->want_plt_sym)
333 {
334 h = _bfd_elf_define_linkage_sym (abfd, info, s,
335 "_PROCEDURE_LINKAGE_TABLE_");
336 elf_hash_table (info)->hplt = h;
337 if (h == NULL)
338 return FALSE;
339 }
252b5132 340
14b2f831
AM
341 s = bfd_make_section_anyway_with_flags (abfd,
342 (bed->rela_plts_and_copies_p
343 ? ".rela.plt" : ".rel.plt"),
344 flags | SEC_READONLY);
252b5132 345 if (s == NULL
45d6a902 346 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 347 return FALSE;
6de2ae4a 348 htab->srelplt = s;
252b5132
RH
349
350 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 351 return FALSE;
252b5132 352
3018b441
RH
353 if (bed->want_dynbss)
354 {
355 /* The .dynbss section is a place to put symbols which are defined
356 by dynamic objects, are referenced by regular objects, and are
357 not functions. We must allocate space for them in the process
358 image and use a R_*_COPY reloc to tell the dynamic linker to
359 initialize them at run time. The linker script puts the .dynbss
360 section into the .bss section of the final image. */
14b2f831
AM
361 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
362 (SEC_ALLOC | SEC_LINKER_CREATED));
3496cb2a 363 if (s == NULL)
b34976b6 364 return FALSE;
252b5132 365
3018b441 366 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
367 normally needed. We need to create it here, though, so that the
368 linker will map it to an output section. We can't just create it
369 only if we need it, because we will not know whether we need it
370 until we have seen all the input files, and the first time the
371 main linker code calls BFD after examining all the input files
372 (size_dynamic_sections) the input sections have already been
373 mapped to the output sections. If the section turns out not to
374 be needed, we can discard it later. We will never need this
375 section when generating a shared object, since they do not use
376 copy relocs. */
3018b441
RH
377 if (! info->shared)
378 {
14b2f831
AM
379 s = bfd_make_section_anyway_with_flags (abfd,
380 (bed->rela_plts_and_copies_p
381 ? ".rela.bss" : ".rel.bss"),
382 flags | SEC_READONLY);
3018b441 383 if (s == NULL
45d6a902 384 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 385 return FALSE;
3018b441 386 }
252b5132
RH
387 }
388
b34976b6 389 return TRUE;
252b5132
RH
390}
391\f
252b5132
RH
392/* Record a new dynamic symbol. We record the dynamic symbols as we
393 read the input files, since we need to have a list of all of them
394 before we can determine the final sizes of the output sections.
395 Note that we may actually call this function even though we are not
396 going to output any dynamic symbols; in some cases we know that a
397 symbol should be in the dynamic symbol table, but only if there is
398 one. */
399
b34976b6 400bfd_boolean
c152c796
AM
401bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
402 struct elf_link_hash_entry *h)
252b5132
RH
403{
404 if (h->dynindx == -1)
405 {
2b0f7ef9 406 struct elf_strtab_hash *dynstr;
68b6ddd0 407 char *p;
252b5132 408 const char *name;
252b5132
RH
409 bfd_size_type indx;
410
7a13edea
NC
411 /* XXX: The ABI draft says the linker must turn hidden and
412 internal symbols into STB_LOCAL symbols when producing the
413 DSO. However, if ld.so honors st_other in the dynamic table,
414 this would not be necessary. */
415 switch (ELF_ST_VISIBILITY (h->other))
416 {
417 case STV_INTERNAL:
418 case STV_HIDDEN:
9d6eee78
L
419 if (h->root.type != bfd_link_hash_undefined
420 && h->root.type != bfd_link_hash_undefweak)
38048eb9 421 {
f5385ebf 422 h->forced_local = 1;
67687978
PB
423 if (!elf_hash_table (info)->is_relocatable_executable)
424 return TRUE;
7a13edea 425 }
0444bdd4 426
7a13edea
NC
427 default:
428 break;
429 }
430
252b5132
RH
431 h->dynindx = elf_hash_table (info)->dynsymcount;
432 ++elf_hash_table (info)->dynsymcount;
433
434 dynstr = elf_hash_table (info)->dynstr;
435 if (dynstr == NULL)
436 {
437 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 438 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 439 if (dynstr == NULL)
b34976b6 440 return FALSE;
252b5132
RH
441 }
442
443 /* We don't put any version information in the dynamic string
aad5d350 444 table. */
252b5132
RH
445 name = h->root.root.string;
446 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
447 if (p != NULL)
448 /* We know that the p points into writable memory. In fact,
449 there are only a few symbols that have read-only names, being
450 those like _GLOBAL_OFFSET_TABLE_ that are created specially
451 by the backends. Most symbols will have names pointing into
452 an ELF string table read from a file, or to objalloc memory. */
453 *p = 0;
454
455 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
456
457 if (p != NULL)
458 *p = ELF_VER_CHR;
252b5132
RH
459
460 if (indx == (bfd_size_type) -1)
b34976b6 461 return FALSE;
252b5132
RH
462 h->dynstr_index = indx;
463 }
464
b34976b6 465 return TRUE;
252b5132 466}
45d6a902 467\f
55255dae
L
468/* Mark a symbol dynamic. */
469
28caa186 470static void
55255dae 471bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
472 struct elf_link_hash_entry *h,
473 Elf_Internal_Sym *sym)
55255dae 474{
40b36307 475 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 476
40b36307
L
477 /* It may be called more than once on the same H. */
478 if(h->dynamic || info->relocatable)
55255dae
L
479 return;
480
40b36307
L
481 if ((info->dynamic_data
482 && (h->type == STT_OBJECT
483 || (sym != NULL
484 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
a0c8462f 485 || (d != NULL
40b36307
L
486 && h->root.type == bfd_link_hash_new
487 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
488 h->dynamic = 1;
489}
490
45d6a902
AM
491/* Record an assignment to a symbol made by a linker script. We need
492 this in case some dynamic object refers to this symbol. */
493
494bfd_boolean
fe21a8fc
L
495bfd_elf_record_link_assignment (bfd *output_bfd,
496 struct bfd_link_info *info,
268b6b39 497 const char *name,
fe21a8fc
L
498 bfd_boolean provide,
499 bfd_boolean hidden)
45d6a902 500{
00cbee0a 501 struct elf_link_hash_entry *h, *hv;
4ea42fb7 502 struct elf_link_hash_table *htab;
00cbee0a 503 const struct elf_backend_data *bed;
45d6a902 504
0eddce27 505 if (!is_elf_hash_table (info->hash))
45d6a902
AM
506 return TRUE;
507
4ea42fb7
AM
508 htab = elf_hash_table (info);
509 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 510 if (h == NULL)
4ea42fb7 511 return provide;
45d6a902 512
00cbee0a 513 switch (h->root.type)
77cfaee6 514 {
00cbee0a
L
515 case bfd_link_hash_defined:
516 case bfd_link_hash_defweak:
517 case bfd_link_hash_common:
518 break;
519 case bfd_link_hash_undefweak:
520 case bfd_link_hash_undefined:
521 /* Since we're defining the symbol, don't let it seem to have not
522 been defined. record_dynamic_symbol and size_dynamic_sections
523 may depend on this. */
4ea42fb7 524 h->root.type = bfd_link_hash_new;
77cfaee6
AM
525 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
526 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
527 break;
528 case bfd_link_hash_new:
40b36307 529 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 530 h->non_elf = 0;
00cbee0a
L
531 break;
532 case bfd_link_hash_indirect:
533 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 534 the versioned symbol point to this one. */
00cbee0a
L
535 bed = get_elf_backend_data (output_bfd);
536 hv = h;
537 while (hv->root.type == bfd_link_hash_indirect
538 || hv->root.type == bfd_link_hash_warning)
539 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
540 /* We don't need to update h->root.u since linker will set them
541 later. */
542 h->root.type = bfd_link_hash_undefined;
543 hv->root.type = bfd_link_hash_indirect;
544 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
545 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
546 break;
547 case bfd_link_hash_warning:
548 abort ();
549 break;
55255dae 550 }
45d6a902
AM
551
552 /* If this symbol is being provided by the linker script, and it is
553 currently defined by a dynamic object, but not by a regular
554 object, then mark it as undefined so that the generic linker will
555 force the correct value. */
556 if (provide
f5385ebf
AM
557 && h->def_dynamic
558 && !h->def_regular)
45d6a902
AM
559 h->root.type = bfd_link_hash_undefined;
560
561 /* If this symbol is not being provided by the linker script, and it is
562 currently defined by a dynamic object, but not by a regular object,
563 then clear out any version information because the symbol will not be
564 associated with the dynamic object any more. */
565 if (!provide
f5385ebf
AM
566 && h->def_dynamic
567 && !h->def_regular)
45d6a902
AM
568 h->verinfo.verdef = NULL;
569
f5385ebf 570 h->def_regular = 1;
45d6a902 571
fe21a8fc
L
572 if (provide && hidden)
573 {
91d6fa6a 574 bed = get_elf_backend_data (output_bfd);
fe21a8fc
L
575 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
576 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
577 }
578
6fa3860b
PB
579 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
580 and executables. */
581 if (!info->relocatable
582 && h->dynindx != -1
583 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
584 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
585 h->forced_local = 1;
586
f5385ebf
AM
587 if ((h->def_dynamic
588 || h->ref_dynamic
67687978
PB
589 || info->shared
590 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
45d6a902
AM
591 && h->dynindx == -1)
592 {
c152c796 593 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
594 return FALSE;
595
596 /* If this is a weak defined symbol, and we know a corresponding
597 real symbol from the same dynamic object, make sure the real
598 symbol is also made into a dynamic symbol. */
f6e332e6
AM
599 if (h->u.weakdef != NULL
600 && h->u.weakdef->dynindx == -1)
45d6a902 601 {
f6e332e6 602 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
603 return FALSE;
604 }
605 }
606
607 return TRUE;
608}
42751cf3 609
8c58d23b
AM
610/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
611 success, and 2 on a failure caused by attempting to record a symbol
612 in a discarded section, eg. a discarded link-once section symbol. */
613
614int
c152c796
AM
615bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
616 bfd *input_bfd,
617 long input_indx)
8c58d23b
AM
618{
619 bfd_size_type amt;
620 struct elf_link_local_dynamic_entry *entry;
621 struct elf_link_hash_table *eht;
622 struct elf_strtab_hash *dynstr;
623 unsigned long dynstr_index;
624 char *name;
625 Elf_External_Sym_Shndx eshndx;
626 char esym[sizeof (Elf64_External_Sym)];
627
0eddce27 628 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
629 return 0;
630
631 /* See if the entry exists already. */
632 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
633 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
634 return 1;
635
636 amt = sizeof (*entry);
a50b1753 637 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
638 if (entry == NULL)
639 return 0;
640
641 /* Go find the symbol, so that we can find it's name. */
642 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 643 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
644 {
645 bfd_release (input_bfd, entry);
646 return 0;
647 }
648
649 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 650 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
651 {
652 asection *s;
653
654 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
655 if (s == NULL || bfd_is_abs_section (s->output_section))
656 {
657 /* We can still bfd_release here as nothing has done another
658 bfd_alloc. We can't do this later in this function. */
659 bfd_release (input_bfd, entry);
660 return 2;
661 }
662 }
663
664 name = (bfd_elf_string_from_elf_section
665 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
666 entry->isym.st_name));
667
668 dynstr = elf_hash_table (info)->dynstr;
669 if (dynstr == NULL)
670 {
671 /* Create a strtab to hold the dynamic symbol names. */
672 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
673 if (dynstr == NULL)
674 return 0;
675 }
676
b34976b6 677 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
8c58d23b
AM
678 if (dynstr_index == (unsigned long) -1)
679 return 0;
680 entry->isym.st_name = dynstr_index;
681
682 eht = elf_hash_table (info);
683
684 entry->next = eht->dynlocal;
685 eht->dynlocal = entry;
686 entry->input_bfd = input_bfd;
687 entry->input_indx = input_indx;
688 eht->dynsymcount++;
689
690 /* Whatever binding the symbol had before, it's now local. */
691 entry->isym.st_info
692 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
693
694 /* The dynindx will be set at the end of size_dynamic_sections. */
695
696 return 1;
697}
698
30b30c21 699/* Return the dynindex of a local dynamic symbol. */
42751cf3 700
30b30c21 701long
268b6b39
AM
702_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
703 bfd *input_bfd,
704 long input_indx)
30b30c21
RH
705{
706 struct elf_link_local_dynamic_entry *e;
707
708 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
709 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
710 return e->dynindx;
711 return -1;
712}
713
714/* This function is used to renumber the dynamic symbols, if some of
715 them are removed because they are marked as local. This is called
716 via elf_link_hash_traverse. */
717
b34976b6 718static bfd_boolean
268b6b39
AM
719elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
720 void *data)
42751cf3 721{
a50b1753 722 size_t *count = (size_t *) data;
30b30c21 723
6fa3860b
PB
724 if (h->forced_local)
725 return TRUE;
726
727 if (h->dynindx != -1)
728 h->dynindx = ++(*count);
729
730 return TRUE;
731}
732
733
734/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
735 STB_LOCAL binding. */
736
737static bfd_boolean
738elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
739 void *data)
740{
a50b1753 741 size_t *count = (size_t *) data;
6fa3860b 742
6fa3860b
PB
743 if (!h->forced_local)
744 return TRUE;
745
42751cf3 746 if (h->dynindx != -1)
30b30c21
RH
747 h->dynindx = ++(*count);
748
b34976b6 749 return TRUE;
42751cf3 750}
30b30c21 751
aee6f5b4
AO
752/* Return true if the dynamic symbol for a given section should be
753 omitted when creating a shared library. */
754bfd_boolean
755_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
756 struct bfd_link_info *info,
757 asection *p)
758{
74541ad4
AM
759 struct elf_link_hash_table *htab;
760
aee6f5b4
AO
761 switch (elf_section_data (p)->this_hdr.sh_type)
762 {
763 case SHT_PROGBITS:
764 case SHT_NOBITS:
765 /* If sh_type is yet undecided, assume it could be
766 SHT_PROGBITS/SHT_NOBITS. */
767 case SHT_NULL:
74541ad4
AM
768 htab = elf_hash_table (info);
769 if (p == htab->tls_sec)
770 return FALSE;
771
772 if (htab->text_index_section != NULL)
773 return p != htab->text_index_section && p != htab->data_index_section;
774
aee6f5b4
AO
775 if (strcmp (p->name, ".got") == 0
776 || strcmp (p->name, ".got.plt") == 0
777 || strcmp (p->name, ".plt") == 0)
778 {
779 asection *ip;
aee6f5b4 780
74541ad4 781 if (htab->dynobj != NULL
3d4d4302 782 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
aee6f5b4
AO
783 && ip->output_section == p)
784 return TRUE;
785 }
786 return FALSE;
787
788 /* There shouldn't be section relative relocations
789 against any other section. */
790 default:
791 return TRUE;
792 }
793}
794
062e2358 795/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
796 symbol for each output section, which come first. Next come symbols
797 which have been forced to local binding. Then all of the back-end
798 allocated local dynamic syms, followed by the rest of the global
799 symbols. */
30b30c21 800
554220db
AM
801static unsigned long
802_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
803 struct bfd_link_info *info,
804 unsigned long *section_sym_count)
30b30c21
RH
805{
806 unsigned long dynsymcount = 0;
807
67687978 808 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
30b30c21 809 {
aee6f5b4 810 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
811 asection *p;
812 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 813 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
814 && (p->flags & SEC_ALLOC) != 0
815 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
816 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
817 else
818 elf_section_data (p)->dynindx = 0;
30b30c21 819 }
554220db 820 *section_sym_count = dynsymcount;
30b30c21 821
6fa3860b
PB
822 elf_link_hash_traverse (elf_hash_table (info),
823 elf_link_renumber_local_hash_table_dynsyms,
824 &dynsymcount);
825
30b30c21
RH
826 if (elf_hash_table (info)->dynlocal)
827 {
828 struct elf_link_local_dynamic_entry *p;
829 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
830 p->dynindx = ++dynsymcount;
831 }
832
833 elf_link_hash_traverse (elf_hash_table (info),
834 elf_link_renumber_hash_table_dynsyms,
835 &dynsymcount);
836
837 /* There is an unused NULL entry at the head of the table which
838 we must account for in our count. Unless there weren't any
839 symbols, which means we'll have no table at all. */
840 if (dynsymcount != 0)
841 ++dynsymcount;
842
ccabcbe5
AM
843 elf_hash_table (info)->dynsymcount = dynsymcount;
844 return dynsymcount;
30b30c21 845}
252b5132 846
54ac0771
L
847/* Merge st_other field. */
848
849static void
850elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
851 Elf_Internal_Sym *isym, bfd_boolean definition,
852 bfd_boolean dynamic)
853{
854 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
855
856 /* If st_other has a processor-specific meaning, specific
857 code might be needed here. We never merge the visibility
858 attribute with the one from a dynamic object. */
859 if (bed->elf_backend_merge_symbol_attribute)
860 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
861 dynamic);
862
863 /* If this symbol has default visibility and the user has requested
864 we not re-export it, then mark it as hidden. */
865 if (definition
866 && !dynamic
867 && (abfd->no_export
868 || (abfd->my_archive && abfd->my_archive->no_export))
869 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
870 isym->st_other = (STV_HIDDEN
871 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
872
873 if (!dynamic && ELF_ST_VISIBILITY (isym->st_other) != 0)
874 {
875 unsigned char hvis, symvis, other, nvis;
876
877 /* Only merge the visibility. Leave the remainder of the
878 st_other field to elf_backend_merge_symbol_attribute. */
879 other = h->other & ~ELF_ST_VISIBILITY (-1);
880
881 /* Combine visibilities, using the most constraining one. */
882 hvis = ELF_ST_VISIBILITY (h->other);
883 symvis = ELF_ST_VISIBILITY (isym->st_other);
884 if (! hvis)
885 nvis = symvis;
886 else if (! symvis)
887 nvis = hvis;
888 else
889 nvis = hvis < symvis ? hvis : symvis;
890
891 h->other = other | nvis;
892 }
893}
894
45d6a902
AM
895/* This function is called when we want to define a new symbol. It
896 handles the various cases which arise when we find a definition in
897 a dynamic object, or when there is already a definition in a
898 dynamic object. The new symbol is described by NAME, SYM, PSEC,
899 and PVALUE. We set SYM_HASH to the hash table entry. We set
900 OVERRIDE if the old symbol is overriding a new definition. We set
901 TYPE_CHANGE_OK if it is OK for the type to change. We set
902 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
903 change, we mean that we shouldn't warn if the type or size does
af44c138
L
904 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
905 object is overridden by a regular object. */
45d6a902
AM
906
907bfd_boolean
268b6b39
AM
908_bfd_elf_merge_symbol (bfd *abfd,
909 struct bfd_link_info *info,
910 const char *name,
911 Elf_Internal_Sym *sym,
912 asection **psec,
913 bfd_vma *pvalue,
af44c138 914 unsigned int *pold_alignment,
268b6b39
AM
915 struct elf_link_hash_entry **sym_hash,
916 bfd_boolean *skip,
917 bfd_boolean *override,
918 bfd_boolean *type_change_ok,
0f8a2703 919 bfd_boolean *size_change_ok)
252b5132 920{
7479dfd4 921 asection *sec, *oldsec;
45d6a902
AM
922 struct elf_link_hash_entry *h;
923 struct elf_link_hash_entry *flip;
924 int bind;
925 bfd *oldbfd;
926 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 927 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 928 const struct elf_backend_data *bed;
45d6a902
AM
929
930 *skip = FALSE;
931 *override = FALSE;
932
933 sec = *psec;
934 bind = ELF_ST_BIND (sym->st_info);
935
cd7be95b
KH
936 /* Silently discard TLS symbols from --just-syms. There's no way to
937 combine a static TLS block with a new TLS block for this executable. */
938 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
dbaa2011 939 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
cd7be95b
KH
940 {
941 *skip = TRUE;
942 return TRUE;
943 }
944
45d6a902
AM
945 if (! bfd_is_und_section (sec))
946 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
947 else
948 h = ((struct elf_link_hash_entry *)
949 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
950 if (h == NULL)
951 return FALSE;
952 *sym_hash = h;
252b5132 953
88ba32a0
L
954 bed = get_elf_backend_data (abfd);
955
45d6a902
AM
956 /* This code is for coping with dynamic objects, and is only useful
957 if we are doing an ELF link. */
88ba32a0 958 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
45d6a902 959 return TRUE;
252b5132 960
45d6a902
AM
961 /* For merging, we only care about real symbols. */
962
963 while (h->root.type == bfd_link_hash_indirect
964 || h->root.type == bfd_link_hash_warning)
965 h = (struct elf_link_hash_entry *) h->root.u.i.link;
966
40b36307
L
967 /* We have to check it for every instance since the first few may be
968 refereences and not all compilers emit symbol type for undefined
969 symbols. */
970 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
971
45d6a902
AM
972 /* If we just created the symbol, mark it as being an ELF symbol.
973 Other than that, there is nothing to do--there is no merge issue
974 with a newly defined symbol--so we just return. */
975
976 if (h->root.type == bfd_link_hash_new)
252b5132 977 {
f5385ebf 978 h->non_elf = 0;
45d6a902
AM
979 return TRUE;
980 }
252b5132 981
7479dfd4
L
982 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
983 existing symbol. */
252b5132 984
45d6a902
AM
985 switch (h->root.type)
986 {
987 default:
988 oldbfd = NULL;
7479dfd4 989 oldsec = NULL;
45d6a902 990 break;
252b5132 991
45d6a902
AM
992 case bfd_link_hash_undefined:
993 case bfd_link_hash_undefweak:
994 oldbfd = h->root.u.undef.abfd;
7479dfd4 995 oldsec = NULL;
45d6a902
AM
996 break;
997
998 case bfd_link_hash_defined:
999 case bfd_link_hash_defweak:
1000 oldbfd = h->root.u.def.section->owner;
7479dfd4 1001 oldsec = h->root.u.def.section;
45d6a902
AM
1002 break;
1003
1004 case bfd_link_hash_common:
1005 oldbfd = h->root.u.c.p->section->owner;
7479dfd4 1006 oldsec = h->root.u.c.p->section;
45d6a902
AM
1007 break;
1008 }
1009
895fa45f
MGD
1010 /* Differentiate strong and weak symbols. */
1011 newweak = bind == STB_WEAK;
1012 oldweak = (h->root.type == bfd_link_hash_defweak
1013 || h->root.type == bfd_link_hash_undefweak);
1014
45d6a902
AM
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
895fa45f 1022 && (newweak || oldweak)
45d6a902 1023 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1024 || !h->def_regular))
45d6a902
AM
1025 return TRUE;
1026
1027 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1028 respectively, is from a dynamic object. */
1029
707bba77 1030 newdyn = (abfd->flags & DYNAMIC) != 0;
45d6a902 1031
707bba77 1032 olddyn = FALSE;
45d6a902
AM
1033 if (oldbfd != NULL)
1034 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1035 else if (oldsec != NULL)
45d6a902 1036 {
707bba77 1037 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1038 indices used by MIPS ELF. */
707bba77 1039 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1040 }
252b5132 1041
45d6a902
AM
1042 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1043 respectively, appear to be a definition rather than reference. */
1044
707bba77 1045 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1046
707bba77
AM
1047 olddef = (h->root.type != bfd_link_hash_undefined
1048 && h->root.type != bfd_link_hash_undefweak
1049 && h->root.type != bfd_link_hash_common);
45d6a902 1050
0a36a439
L
1051 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1052 respectively, appear to be a function. */
1053
1054 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1055 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1056
1057 oldfunc = (h->type != STT_NOTYPE
1058 && bed->is_function_type (h->type));
1059
580a2b6e
L
1060 /* When we try to create a default indirect symbol from the dynamic
1061 definition with the default version, we skip it if its type and
1062 the type of existing regular definition mismatch. We only do it
1063 if the existing regular definition won't be dynamic. */
1064 if (pold_alignment == NULL
1065 && !info->shared
1066 && !info->export_dynamic
1067 && !h->ref_dynamic
1068 && newdyn
1069 && newdef
1070 && !olddyn
1071 && (olddef || h->root.type == bfd_link_hash_common)
1072 && ELF_ST_TYPE (sym->st_info) != h->type
1073 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
fcb93ecf 1074 && h->type != STT_NOTYPE
0a36a439 1075 && !(newfunc && oldfunc))
580a2b6e
L
1076 {
1077 *skip = TRUE;
1078 return TRUE;
1079 }
1080
3a5dbfb2
AM
1081 /* Plugin symbol type isn't currently set. Stop bogus errors. */
1082 if (oldbfd != NULL && (oldbfd->flags & BFD_PLUGIN) != 0)
1083 *type_change_ok = TRUE;
1084
68f49ba3
L
1085 /* Check TLS symbol. We don't check undefined symbol introduced by
1086 "ld -u". */
3a5dbfb2
AM
1087 else if (oldbfd != NULL
1088 && ELF_ST_TYPE (sym->st_info) != h->type
1089 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1090 {
1091 bfd *ntbfd, *tbfd;
1092 bfd_boolean ntdef, tdef;
1093 asection *ntsec, *tsec;
1094
1095 if (h->type == STT_TLS)
1096 {
3b36f7e6 1097 ntbfd = abfd;
7479dfd4
L
1098 ntsec = sec;
1099 ntdef = newdef;
1100 tbfd = oldbfd;
1101 tsec = oldsec;
1102 tdef = olddef;
1103 }
1104 else
1105 {
1106 ntbfd = oldbfd;
1107 ntsec = oldsec;
1108 ntdef = olddef;
1109 tbfd = abfd;
1110 tsec = sec;
1111 tdef = newdef;
1112 }
1113
1114 if (tdef && ntdef)
1115 (*_bfd_error_handler)
fc3e1e3c 1116 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1117 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1118 else if (!tdef && !ntdef)
1119 (*_bfd_error_handler)
fc3e1e3c 1120 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
7479dfd4
L
1121 tbfd, ntbfd, h->root.root.string);
1122 else if (tdef)
1123 (*_bfd_error_handler)
fc3e1e3c 1124 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
7479dfd4
L
1125 tbfd, tsec, ntbfd, h->root.root.string);
1126 else
1127 (*_bfd_error_handler)
fc3e1e3c 1128 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1129 tbfd, ntbfd, ntsec, h->root.root.string);
1130
1131 bfd_set_error (bfd_error_bad_value);
1132 return FALSE;
1133 }
1134
4cc11e76 1135 /* We need to remember if a symbol has a definition in a dynamic
45d6a902
AM
1136 object or is weak in all dynamic objects. Internal and hidden
1137 visibility will make it unavailable to dynamic objects. */
f5385ebf 1138 if (newdyn && !h->dynamic_def)
45d6a902
AM
1139 {
1140 if (!bfd_is_und_section (sec))
f5385ebf 1141 h->dynamic_def = 1;
45d6a902 1142 else
252b5132 1143 {
45d6a902
AM
1144 /* Check if this symbol is weak in all dynamic objects. If it
1145 is the first time we see it in a dynamic object, we mark
1146 if it is weak. Otherwise, we clear it. */
f5385ebf 1147 if (!h->ref_dynamic)
79349b09 1148 {
45d6a902 1149 if (bind == STB_WEAK)
f5385ebf 1150 h->dynamic_weak = 1;
252b5132 1151 }
45d6a902 1152 else if (bind != STB_WEAK)
f5385ebf 1153 h->dynamic_weak = 0;
252b5132 1154 }
45d6a902 1155 }
252b5132 1156
45d6a902
AM
1157 /* If the old symbol has non-default visibility, we ignore the new
1158 definition from a dynamic object. */
1159 if (newdyn
9c7a29a3 1160 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1161 && !bfd_is_und_section (sec))
1162 {
1163 *skip = TRUE;
1164 /* Make sure this symbol is dynamic. */
f5385ebf 1165 h->ref_dynamic = 1;
45d6a902
AM
1166 /* A protected symbol has external availability. Make sure it is
1167 recorded as dynamic.
1168
1169 FIXME: Should we check type and size for protected symbol? */
1170 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1171 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1172 else
1173 return TRUE;
1174 }
1175 else if (!newdyn
9c7a29a3 1176 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1177 && h->def_dynamic)
45d6a902
AM
1178 {
1179 /* If the new symbol with non-default visibility comes from a
1180 relocatable file and the old definition comes from a dynamic
1181 object, we remove the old definition. */
1182 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1183 {
1184 /* Handle the case where the old dynamic definition is
1185 default versioned. We need to copy the symbol info from
1186 the symbol with default version to the normal one if it
1187 was referenced before. */
1188 if (h->ref_regular)
1189 {
d2dee3b2 1190 struct elf_link_hash_entry *vh = *sym_hash;
91d6fa6a 1191
d2dee3b2
L
1192 vh->root.type = h->root.type;
1193 h->root.type = bfd_link_hash_indirect;
1194 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1195 /* Protected symbols will override the dynamic definition
1196 with default version. */
1197 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1198 {
1199 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1200 vh->dynamic_def = 1;
1201 vh->ref_dynamic = 1;
1202 }
1203 else
1204 {
1205 h->root.type = vh->root.type;
1206 vh->ref_dynamic = 0;
1207 /* We have to hide it here since it was made dynamic
1208 global with extra bits when the symbol info was
1209 copied from the old dynamic definition. */
1210 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1211 }
1212 h = vh;
1213 }
1214 else
1215 h = *sym_hash;
1216 }
1de1a317 1217
f5eda473
AM
1218 /* If the old symbol was undefined before, then it will still be
1219 on the undefs list. If the new symbol is undefined or
1220 common, we can't make it bfd_link_hash_new here, because new
1221 undefined or common symbols will be added to the undefs list
1222 by _bfd_generic_link_add_one_symbol. Symbols may not be
1223 added twice to the undefs list. Also, if the new symbol is
1224 undefweak then we don't want to lose the strong undef. */
1225 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1226 {
1de1a317 1227 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1228 h->root.u.undef.abfd = abfd;
1229 }
1230 else
1231 {
1232 h->root.type = bfd_link_hash_new;
1233 h->root.u.undef.abfd = NULL;
1234 }
1235
f5eda473 1236 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1237 {
f5eda473
AM
1238 /* If the new symbol is hidden or internal, completely undo
1239 any dynamic link state. */
1240 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1241 h->forced_local = 0;
1242 h->ref_dynamic = 0;
45d6a902 1243 }
f5eda473
AM
1244 else
1245 h->ref_dynamic = 1;
1246 h->def_dynamic = 0;
1247 h->dynamic_def = 0;
45d6a902
AM
1248 /* FIXME: Should we check type and size for protected symbol? */
1249 h->size = 0;
1250 h->type = 0;
1251 return TRUE;
1252 }
14a793b2 1253
3e7a7d11
NC
1254 if (bind == STB_GNU_UNIQUE)
1255 h->unique_global = 1;
1256
15b43f48
AM
1257 /* If a new weak symbol definition comes from a regular file and the
1258 old symbol comes from a dynamic library, we treat the new one as
1259 strong. Similarly, an old weak symbol definition from a regular
1260 file is treated as strong when the new symbol comes from a dynamic
1261 library. Further, an old weak symbol from a dynamic library is
1262 treated as strong if the new symbol is from a dynamic library.
1263 This reflects the way glibc's ld.so works.
1264
1265 Do this before setting *type_change_ok or *size_change_ok so that
1266 we warn properly when dynamic library symbols are overridden. */
1267
1268 if (newdef && !newdyn && olddyn)
0f8a2703 1269 newweak = FALSE;
15b43f48 1270 if (olddef && newdyn)
0f8a2703
AM
1271 oldweak = FALSE;
1272
d334575b 1273 /* Allow changes between different types of function symbol. */
0a36a439 1274 if (newfunc && oldfunc)
fcb93ecf
PB
1275 *type_change_ok = TRUE;
1276
79349b09
AM
1277 /* It's OK to change the type if either the existing symbol or the
1278 new symbol is weak. A type change is also OK if the old symbol
1279 is undefined and the new symbol is defined. */
252b5132 1280
79349b09
AM
1281 if (oldweak
1282 || newweak
1283 || (newdef
1284 && h->root.type == bfd_link_hash_undefined))
1285 *type_change_ok = TRUE;
1286
1287 /* It's OK to change the size if either the existing symbol or the
1288 new symbol is weak, or if the old symbol is undefined. */
1289
1290 if (*type_change_ok
1291 || h->root.type == bfd_link_hash_undefined)
1292 *size_change_ok = TRUE;
45d6a902 1293
45d6a902
AM
1294 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1295 symbol, respectively, appears to be a common symbol in a dynamic
1296 object. If a symbol appears in an uninitialized section, and is
1297 not weak, and is not a function, then it may be a common symbol
1298 which was resolved when the dynamic object was created. We want
1299 to treat such symbols specially, because they raise special
1300 considerations when setting the symbol size: if the symbol
1301 appears as a common symbol in a regular object, and the size in
1302 the regular object is larger, we must make sure that we use the
1303 larger size. This problematic case can always be avoided in C,
1304 but it must be handled correctly when using Fortran shared
1305 libraries.
1306
1307 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1308 likewise for OLDDYNCOMMON and OLDDEF.
1309
1310 Note that this test is just a heuristic, and that it is quite
1311 possible to have an uninitialized symbol in a shared object which
1312 is really a definition, rather than a common symbol. This could
1313 lead to some minor confusion when the symbol really is a common
1314 symbol in some regular object. However, I think it will be
1315 harmless. */
1316
1317 if (newdyn
1318 && newdef
79349b09 1319 && !newweak
45d6a902
AM
1320 && (sec->flags & SEC_ALLOC) != 0
1321 && (sec->flags & SEC_LOAD) == 0
1322 && sym->st_size > 0
0a36a439 1323 && !newfunc)
45d6a902
AM
1324 newdyncommon = TRUE;
1325 else
1326 newdyncommon = FALSE;
1327
1328 if (olddyn
1329 && olddef
1330 && h->root.type == bfd_link_hash_defined
f5385ebf 1331 && h->def_dynamic
45d6a902
AM
1332 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1333 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1334 && h->size > 0
0a36a439 1335 && !oldfunc)
45d6a902
AM
1336 olddyncommon = TRUE;
1337 else
1338 olddyncommon = FALSE;
1339
a4d8e49b
L
1340 /* We now know everything about the old and new symbols. We ask the
1341 backend to check if we can merge them. */
a4d8e49b
L
1342 if (bed->merge_symbol
1343 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1344 pold_alignment, skip, override,
1345 type_change_ok, size_change_ok,
1346 &newdyn, &newdef, &newdyncommon, &newweak,
1347 abfd, &sec,
1348 &olddyn, &olddef, &olddyncommon, &oldweak,
1349 oldbfd, &oldsec))
1350 return FALSE;
1351
45d6a902
AM
1352 /* If both the old and the new symbols look like common symbols in a
1353 dynamic object, set the size of the symbol to the larger of the
1354 two. */
1355
1356 if (olddyncommon
1357 && newdyncommon
1358 && sym->st_size != h->size)
1359 {
1360 /* Since we think we have two common symbols, issue a multiple
1361 common warning if desired. Note that we only warn if the
1362 size is different. If the size is the same, we simply let
1363 the old symbol override the new one as normally happens with
1364 symbols defined in dynamic objects. */
1365
1366 if (! ((*info->callbacks->multiple_common)
24f58f47 1367 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902 1368 return FALSE;
252b5132 1369
45d6a902
AM
1370 if (sym->st_size > h->size)
1371 h->size = sym->st_size;
252b5132 1372
45d6a902 1373 *size_change_ok = TRUE;
252b5132
RH
1374 }
1375
45d6a902
AM
1376 /* If we are looking at a dynamic object, and we have found a
1377 definition, we need to see if the symbol was already defined by
1378 some other object. If so, we want to use the existing
1379 definition, and we do not want to report a multiple symbol
1380 definition error; we do this by clobbering *PSEC to be
1381 bfd_und_section_ptr.
1382
1383 We treat a common symbol as a definition if the symbol in the
1384 shared library is a function, since common symbols always
1385 represent variables; this can cause confusion in principle, but
1386 any such confusion would seem to indicate an erroneous program or
1387 shared library. We also permit a common symbol in a regular
79349b09 1388 object to override a weak symbol in a shared object. */
45d6a902
AM
1389
1390 if (newdyn
1391 && newdef
77cfaee6 1392 && (olddef
45d6a902 1393 || (h->root.type == bfd_link_hash_common
0a36a439 1394 && (newweak || newfunc))))
45d6a902
AM
1395 {
1396 *override = TRUE;
1397 newdef = FALSE;
1398 newdyncommon = FALSE;
252b5132 1399
45d6a902
AM
1400 *psec = sec = bfd_und_section_ptr;
1401 *size_change_ok = TRUE;
252b5132 1402
45d6a902
AM
1403 /* If we get here when the old symbol is a common symbol, then
1404 we are explicitly letting it override a weak symbol or
1405 function in a dynamic object, and we don't want to warn about
1406 a type change. If the old symbol is a defined symbol, a type
1407 change warning may still be appropriate. */
252b5132 1408
45d6a902
AM
1409 if (h->root.type == bfd_link_hash_common)
1410 *type_change_ok = TRUE;
1411 }
1412
1413 /* Handle the special case of an old common symbol merging with a
1414 new symbol which looks like a common symbol in a shared object.
1415 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1416 common symbol, and let _bfd_generic_link_add_one_symbol do the
1417 right thing. */
45d6a902
AM
1418
1419 if (newdyncommon
1420 && h->root.type == bfd_link_hash_common)
1421 {
1422 *override = TRUE;
1423 newdef = FALSE;
1424 newdyncommon = FALSE;
1425 *pvalue = sym->st_size;
a4d8e49b 1426 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1427 *size_change_ok = TRUE;
1428 }
1429
c5e2cead 1430 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1431 if (newdef && olddef && newweak)
54ac0771 1432 {
35ed3f94 1433 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1434 if (!(oldbfd != NULL
1435 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94
AM
1436 && (abfd->flags & BFD_PLUGIN) == 0))
1437 *skip = TRUE;
54ac0771
L
1438
1439 /* Merge st_other. If the symbol already has a dynamic index,
1440 but visibility says it should not be visible, turn it into a
1441 local symbol. */
1442 elf_merge_st_other (abfd, h, sym, newdef, newdyn);
1443 if (h->dynindx != -1)
1444 switch (ELF_ST_VISIBILITY (h->other))
1445 {
1446 case STV_INTERNAL:
1447 case STV_HIDDEN:
1448 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1449 break;
1450 }
1451 }
c5e2cead 1452
45d6a902
AM
1453 /* If the old symbol is from a dynamic object, and the new symbol is
1454 a definition which is not from a dynamic object, then the new
1455 symbol overrides the old symbol. Symbols from regular files
1456 always take precedence over symbols from dynamic objects, even if
1457 they are defined after the dynamic object in the link.
1458
1459 As above, we again permit a common symbol in a regular object to
1460 override a definition in a shared object if the shared object
0f8a2703 1461 symbol is a function or is weak. */
45d6a902
AM
1462
1463 flip = NULL;
77cfaee6 1464 if (!newdyn
45d6a902
AM
1465 && (newdef
1466 || (bfd_is_com_section (sec)
0a36a439 1467 && (oldweak || oldfunc)))
45d6a902
AM
1468 && olddyn
1469 && olddef
f5385ebf 1470 && h->def_dynamic)
45d6a902
AM
1471 {
1472 /* Change the hash table entry to undefined, and let
1473 _bfd_generic_link_add_one_symbol do the right thing with the
1474 new definition. */
1475
1476 h->root.type = bfd_link_hash_undefined;
1477 h->root.u.undef.abfd = h->root.u.def.section->owner;
1478 *size_change_ok = TRUE;
1479
1480 olddef = FALSE;
1481 olddyncommon = FALSE;
1482
1483 /* We again permit a type change when a common symbol may be
1484 overriding a function. */
1485
1486 if (bfd_is_com_section (sec))
0a36a439
L
1487 {
1488 if (oldfunc)
1489 {
1490 /* If a common symbol overrides a function, make sure
1491 that it isn't defined dynamically nor has type
1492 function. */
1493 h->def_dynamic = 0;
1494 h->type = STT_NOTYPE;
1495 }
1496 *type_change_ok = TRUE;
1497 }
45d6a902
AM
1498
1499 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1500 flip = *sym_hash;
1501 else
1502 /* This union may have been set to be non-NULL when this symbol
1503 was seen in a dynamic object. We must force the union to be
1504 NULL, so that it is correct for a regular symbol. */
1505 h->verinfo.vertree = NULL;
1506 }
1507
1508 /* Handle the special case of a new common symbol merging with an
1509 old symbol that looks like it might be a common symbol defined in
1510 a shared object. Note that we have already handled the case in
1511 which a new common symbol should simply override the definition
1512 in the shared library. */
1513
1514 if (! newdyn
1515 && bfd_is_com_section (sec)
1516 && olddyncommon)
1517 {
1518 /* It would be best if we could set the hash table entry to a
1519 common symbol, but we don't know what to use for the section
1520 or the alignment. */
1521 if (! ((*info->callbacks->multiple_common)
24f58f47 1522 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902
AM
1523 return FALSE;
1524
4cc11e76 1525 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1526 larger, pretend that the new symbol has its size. */
1527
1528 if (h->size > *pvalue)
1529 *pvalue = h->size;
1530
af44c138
L
1531 /* We need to remember the alignment required by the symbol
1532 in the dynamic object. */
1533 BFD_ASSERT (pold_alignment);
1534 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1535
1536 olddef = FALSE;
1537 olddyncommon = FALSE;
1538
1539 h->root.type = bfd_link_hash_undefined;
1540 h->root.u.undef.abfd = h->root.u.def.section->owner;
1541
1542 *size_change_ok = TRUE;
1543 *type_change_ok = TRUE;
1544
1545 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1546 flip = *sym_hash;
1547 else
1548 h->verinfo.vertree = NULL;
1549 }
1550
1551 if (flip != NULL)
1552 {
1553 /* Handle the case where we had a versioned symbol in a dynamic
1554 library and now find a definition in a normal object. In this
1555 case, we make the versioned symbol point to the normal one. */
45d6a902 1556 flip->root.type = h->root.type;
00cbee0a 1557 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1558 h->root.type = bfd_link_hash_indirect;
1559 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1560 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1561 if (h->def_dynamic)
45d6a902 1562 {
f5385ebf
AM
1563 h->def_dynamic = 0;
1564 flip->ref_dynamic = 1;
45d6a902
AM
1565 }
1566 }
1567
45d6a902
AM
1568 return TRUE;
1569}
1570
1571/* This function is called to create an indirect symbol from the
1572 default for the symbol with the default version if needed. The
1573 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
0f8a2703 1574 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1575
28caa186 1576static bfd_boolean
268b6b39
AM
1577_bfd_elf_add_default_symbol (bfd *abfd,
1578 struct bfd_link_info *info,
1579 struct elf_link_hash_entry *h,
1580 const char *name,
1581 Elf_Internal_Sym *sym,
1582 asection **psec,
1583 bfd_vma *value,
1584 bfd_boolean *dynsym,
0f8a2703 1585 bfd_boolean override)
45d6a902
AM
1586{
1587 bfd_boolean type_change_ok;
1588 bfd_boolean size_change_ok;
1589 bfd_boolean skip;
1590 char *shortname;
1591 struct elf_link_hash_entry *hi;
1592 struct bfd_link_hash_entry *bh;
9c5bfbb7 1593 const struct elf_backend_data *bed;
45d6a902
AM
1594 bfd_boolean collect;
1595 bfd_boolean dynamic;
1596 char *p;
1597 size_t len, shortlen;
1598 asection *sec;
1599
1600 /* If this symbol has a version, and it is the default version, we
1601 create an indirect symbol from the default name to the fully
1602 decorated name. This will cause external references which do not
1603 specify a version to be bound to this version of the symbol. */
1604 p = strchr (name, ELF_VER_CHR);
1605 if (p == NULL || p[1] != ELF_VER_CHR)
1606 return TRUE;
1607
1608 if (override)
1609 {
4cc11e76 1610 /* We are overridden by an old definition. We need to check if we
45d6a902
AM
1611 need to create the indirect symbol from the default name. */
1612 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1613 FALSE, FALSE);
1614 BFD_ASSERT (hi != NULL);
1615 if (hi == h)
1616 return TRUE;
1617 while (hi->root.type == bfd_link_hash_indirect
1618 || hi->root.type == bfd_link_hash_warning)
1619 {
1620 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1621 if (hi == h)
1622 return TRUE;
1623 }
1624 }
1625
1626 bed = get_elf_backend_data (abfd);
1627 collect = bed->collect;
1628 dynamic = (abfd->flags & DYNAMIC) != 0;
1629
1630 shortlen = p - name;
a50b1753 1631 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1632 if (shortname == NULL)
1633 return FALSE;
1634 memcpy (shortname, name, shortlen);
1635 shortname[shortlen] = '\0';
1636
1637 /* We are going to create a new symbol. Merge it with any existing
1638 symbol with this name. For the purposes of the merge, act as
1639 though we were defining the symbol we just defined, although we
1640 actually going to define an indirect symbol. */
1641 type_change_ok = FALSE;
1642 size_change_ok = FALSE;
1643 sec = *psec;
1644 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
af44c138
L
1645 NULL, &hi, &skip, &override,
1646 &type_change_ok, &size_change_ok))
45d6a902
AM
1647 return FALSE;
1648
1649 if (skip)
1650 goto nondefault;
1651
1652 if (! override)
1653 {
1654 bh = &hi->root;
1655 if (! (_bfd_generic_link_add_one_symbol
1656 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
268b6b39 1657 0, name, FALSE, collect, &bh)))
45d6a902
AM
1658 return FALSE;
1659 hi = (struct elf_link_hash_entry *) bh;
1660 }
1661 else
1662 {
1663 /* In this case the symbol named SHORTNAME is overriding the
1664 indirect symbol we want to add. We were planning on making
1665 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1666 is the name without a version. NAME is the fully versioned
1667 name, and it is the default version.
1668
1669 Overriding means that we already saw a definition for the
1670 symbol SHORTNAME in a regular object, and it is overriding
1671 the symbol defined in the dynamic object.
1672
1673 When this happens, we actually want to change NAME, the
1674 symbol we just added, to refer to SHORTNAME. This will cause
1675 references to NAME in the shared object to become references
1676 to SHORTNAME in the regular object. This is what we expect
1677 when we override a function in a shared object: that the
1678 references in the shared object will be mapped to the
1679 definition in the regular object. */
1680
1681 while (hi->root.type == bfd_link_hash_indirect
1682 || hi->root.type == bfd_link_hash_warning)
1683 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1684
1685 h->root.type = bfd_link_hash_indirect;
1686 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1687 if (h->def_dynamic)
45d6a902 1688 {
f5385ebf
AM
1689 h->def_dynamic = 0;
1690 hi->ref_dynamic = 1;
1691 if (hi->ref_regular
1692 || hi->def_regular)
45d6a902 1693 {
c152c796 1694 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1695 return FALSE;
1696 }
1697 }
1698
1699 /* Now set HI to H, so that the following code will set the
1700 other fields correctly. */
1701 hi = h;
1702 }
1703
fab4a87f
L
1704 /* Check if HI is a warning symbol. */
1705 if (hi->root.type == bfd_link_hash_warning)
1706 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1707
45d6a902
AM
1708 /* If there is a duplicate definition somewhere, then HI may not
1709 point to an indirect symbol. We will have reported an error to
1710 the user in that case. */
1711
1712 if (hi->root.type == bfd_link_hash_indirect)
1713 {
1714 struct elf_link_hash_entry *ht;
1715
45d6a902 1716 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1717 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902
AM
1718
1719 /* See if the new flags lead us to realize that the symbol must
1720 be dynamic. */
1721 if (! *dynsym)
1722 {
1723 if (! dynamic)
1724 {
ca4a656b 1725 if (! info->executable
f5385ebf 1726 || hi->ref_dynamic)
45d6a902
AM
1727 *dynsym = TRUE;
1728 }
1729 else
1730 {
f5385ebf 1731 if (hi->ref_regular)
45d6a902
AM
1732 *dynsym = TRUE;
1733 }
1734 }
1735 }
1736
1737 /* We also need to define an indirection from the nondefault version
1738 of the symbol. */
1739
1740nondefault:
1741 len = strlen (name);
a50b1753 1742 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1743 if (shortname == NULL)
1744 return FALSE;
1745 memcpy (shortname, name, shortlen);
1746 memcpy (shortname + shortlen, p + 1, len - shortlen);
1747
1748 /* Once again, merge with any existing symbol. */
1749 type_change_ok = FALSE;
1750 size_change_ok = FALSE;
1751 sec = *psec;
1752 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
af44c138
L
1753 NULL, &hi, &skip, &override,
1754 &type_change_ok, &size_change_ok))
45d6a902
AM
1755 return FALSE;
1756
1757 if (skip)
1758 return TRUE;
1759
1760 if (override)
1761 {
1762 /* Here SHORTNAME is a versioned name, so we don't expect to see
1763 the type of override we do in the case above unless it is
4cc11e76 1764 overridden by a versioned definition. */
45d6a902
AM
1765 if (hi->root.type != bfd_link_hash_defined
1766 && hi->root.type != bfd_link_hash_defweak)
1767 (*_bfd_error_handler)
d003868e
AM
1768 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1769 abfd, shortname);
45d6a902
AM
1770 }
1771 else
1772 {
1773 bh = &hi->root;
1774 if (! (_bfd_generic_link_add_one_symbol
1775 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1776 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1777 return FALSE;
1778 hi = (struct elf_link_hash_entry *) bh;
1779
1780 /* If there is a duplicate definition somewhere, then HI may not
1781 point to an indirect symbol. We will have reported an error
1782 to the user in that case. */
1783
1784 if (hi->root.type == bfd_link_hash_indirect)
1785 {
fcfa13d2 1786 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
45d6a902
AM
1787
1788 /* See if the new flags lead us to realize that the symbol
1789 must be dynamic. */
1790 if (! *dynsym)
1791 {
1792 if (! dynamic)
1793 {
ca4a656b 1794 if (! info->executable
f5385ebf 1795 || hi->ref_dynamic)
45d6a902
AM
1796 *dynsym = TRUE;
1797 }
1798 else
1799 {
f5385ebf 1800 if (hi->ref_regular)
45d6a902
AM
1801 *dynsym = TRUE;
1802 }
1803 }
1804 }
1805 }
1806
1807 return TRUE;
1808}
1809\f
1810/* This routine is used to export all defined symbols into the dynamic
1811 symbol table. It is called via elf_link_hash_traverse. */
1812
28caa186 1813static bfd_boolean
268b6b39 1814_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1815{
a50b1753 1816 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
1817
1818 /* Ignore indirect symbols. These are added by the versioning code. */
1819 if (h->root.type == bfd_link_hash_indirect)
1820 return TRUE;
1821
7686d77d
AM
1822 /* Ignore this if we won't export it. */
1823 if (!eif->info->export_dynamic && !h->dynamic)
1824 return TRUE;
45d6a902
AM
1825
1826 if (h->dynindx == -1
fd91d419
L
1827 && (h->def_regular || h->ref_regular)
1828 && ! bfd_hide_sym_by_version (eif->info->version_info,
1829 h->root.root.string))
45d6a902 1830 {
fd91d419 1831 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 1832 {
fd91d419
L
1833 eif->failed = TRUE;
1834 return FALSE;
45d6a902
AM
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{
a50b1753 1850 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
1851 Elf_Internal_Verneed *t;
1852 Elf_Internal_Vernaux *a;
1853 bfd_size_type amt;
1854
45d6a902
AM
1855 /* We only care about symbols defined in shared objects with version
1856 information. */
f5385ebf
AM
1857 if (!h->def_dynamic
1858 || h->def_regular
45d6a902
AM
1859 || h->dynindx == -1
1860 || h->verinfo.verdef == NULL)
1861 return TRUE;
1862
1863 /* See if we already know about this version. */
28caa186
AM
1864 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1865 t != NULL;
1866 t = t->vn_nextref)
45d6a902
AM
1867 {
1868 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1869 continue;
1870
1871 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1872 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1873 return TRUE;
1874
1875 break;
1876 }
1877
1878 /* This is a new version. Add it to tree we are building. */
1879
1880 if (t == NULL)
1881 {
1882 amt = sizeof *t;
a50b1753 1883 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
1884 if (t == NULL)
1885 {
1886 rinfo->failed = TRUE;
1887 return FALSE;
1888 }
1889
1890 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
1891 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
1892 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
1893 }
1894
1895 amt = sizeof *a;
a50b1753 1896 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
1897 if (a == NULL)
1898 {
1899 rinfo->failed = TRUE;
1900 return FALSE;
1901 }
45d6a902
AM
1902
1903 /* Note that we are copying a string pointer here, and testing it
1904 above. If bfd_elf_string_from_elf_section is ever changed to
1905 discard the string data when low in memory, this will have to be
1906 fixed. */
1907 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1908
1909 a->vna_flags = h->verinfo.verdef->vd_flags;
1910 a->vna_nextptr = t->vn_auxptr;
1911
1912 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1913 ++rinfo->vers;
1914
1915 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1916
1917 t->vn_auxptr = a;
1918
1919 return TRUE;
1920}
1921
1922/* Figure out appropriate versions for all the symbols. We may not
1923 have the version number script until we have read all of the input
1924 files, so until that point we don't know which symbols should be
1925 local. This function is called via elf_link_hash_traverse. */
1926
28caa186 1927static bfd_boolean
268b6b39 1928_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 1929{
28caa186 1930 struct elf_info_failed *sinfo;
45d6a902 1931 struct bfd_link_info *info;
9c5bfbb7 1932 const struct elf_backend_data *bed;
45d6a902
AM
1933 struct elf_info_failed eif;
1934 char *p;
1935 bfd_size_type amt;
1936
a50b1753 1937 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
1938 info = sinfo->info;
1939
45d6a902
AM
1940 /* Fix the symbol flags. */
1941 eif.failed = FALSE;
1942 eif.info = info;
1943 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1944 {
1945 if (eif.failed)
1946 sinfo->failed = TRUE;
1947 return FALSE;
1948 }
1949
1950 /* We only need version numbers for symbols defined in regular
1951 objects. */
f5385ebf 1952 if (!h->def_regular)
45d6a902
AM
1953 return TRUE;
1954
28caa186 1955 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
1956 p = strchr (h->root.root.string, ELF_VER_CHR);
1957 if (p != NULL && h->verinfo.vertree == NULL)
1958 {
1959 struct bfd_elf_version_tree *t;
1960 bfd_boolean hidden;
1961
1962 hidden = TRUE;
1963
1964 /* There are two consecutive ELF_VER_CHR characters if this is
1965 not a hidden symbol. */
1966 ++p;
1967 if (*p == ELF_VER_CHR)
1968 {
1969 hidden = FALSE;
1970 ++p;
1971 }
1972
1973 /* If there is no version string, we can just return out. */
1974 if (*p == '\0')
1975 {
1976 if (hidden)
f5385ebf 1977 h->hidden = 1;
45d6a902
AM
1978 return TRUE;
1979 }
1980
1981 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 1982 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
1983 {
1984 if (strcmp (t->name, p) == 0)
1985 {
1986 size_t len;
1987 char *alc;
1988 struct bfd_elf_version_expr *d;
1989
1990 len = p - h->root.root.string;
a50b1753 1991 alc = (char *) bfd_malloc (len);
45d6a902 1992 if (alc == NULL)
14b1c01e
AM
1993 {
1994 sinfo->failed = TRUE;
1995 return FALSE;
1996 }
45d6a902
AM
1997 memcpy (alc, h->root.root.string, len - 1);
1998 alc[len - 1] = '\0';
1999 if (alc[len - 2] == ELF_VER_CHR)
2000 alc[len - 2] = '\0';
2001
2002 h->verinfo.vertree = t;
2003 t->used = TRUE;
2004 d = NULL;
2005
108ba305
JJ
2006 if (t->globals.list != NULL)
2007 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2008
2009 /* See if there is anything to force this symbol to
2010 local scope. */
108ba305 2011 if (d == NULL && t->locals.list != NULL)
45d6a902 2012 {
108ba305
JJ
2013 d = (*t->match) (&t->locals, NULL, alc);
2014 if (d != NULL
2015 && h->dynindx != -1
108ba305
JJ
2016 && ! info->export_dynamic)
2017 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2018 }
2019
2020 free (alc);
2021 break;
2022 }
2023 }
2024
2025 /* If we are building an application, we need to create a
2026 version node for this version. */
36af4a4e 2027 if (t == NULL && info->executable)
45d6a902
AM
2028 {
2029 struct bfd_elf_version_tree **pp;
2030 int version_index;
2031
2032 /* If we aren't going to export this symbol, we don't need
2033 to worry about it. */
2034 if (h->dynindx == -1)
2035 return TRUE;
2036
2037 amt = sizeof *t;
a50b1753 2038 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
45d6a902
AM
2039 if (t == NULL)
2040 {
2041 sinfo->failed = TRUE;
2042 return FALSE;
2043 }
2044
45d6a902 2045 t->name = p;
45d6a902
AM
2046 t->name_indx = (unsigned int) -1;
2047 t->used = TRUE;
2048
2049 version_index = 1;
2050 /* Don't count anonymous version tag. */
fd91d419
L
2051 if (sinfo->info->version_info != NULL
2052 && sinfo->info->version_info->vernum == 0)
45d6a902 2053 version_index = 0;
fd91d419
L
2054 for (pp = &sinfo->info->version_info;
2055 *pp != NULL;
2056 pp = &(*pp)->next)
45d6a902
AM
2057 ++version_index;
2058 t->vernum = version_index;
2059
2060 *pp = t;
2061
2062 h->verinfo.vertree = t;
2063 }
2064 else if (t == NULL)
2065 {
2066 /* We could not find the version for a symbol when
2067 generating a shared archive. Return an error. */
2068 (*_bfd_error_handler)
c55fe096 2069 (_("%B: version node not found for symbol %s"),
28caa186 2070 info->output_bfd, h->root.root.string);
45d6a902
AM
2071 bfd_set_error (bfd_error_bad_value);
2072 sinfo->failed = TRUE;
2073 return FALSE;
2074 }
2075
2076 if (hidden)
f5385ebf 2077 h->hidden = 1;
45d6a902
AM
2078 }
2079
2080 /* If we don't have a version for this symbol, see if we can find
2081 something. */
fd91d419 2082 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2083 {
1e8fa21e 2084 bfd_boolean hide;
ae5a3597 2085
fd91d419
L
2086 h->verinfo.vertree
2087 = bfd_find_version_for_sym (sinfo->info->version_info,
2088 h->root.root.string, &hide);
1e8fa21e
AM
2089 if (h->verinfo.vertree != NULL && hide)
2090 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2091 }
2092
2093 return TRUE;
2094}
2095\f
45d6a902
AM
2096/* Read and swap the relocs from the section indicated by SHDR. This
2097 may be either a REL or a RELA section. The relocations are
2098 translated into RELA relocations and stored in INTERNAL_RELOCS,
2099 which should have already been allocated to contain enough space.
2100 The EXTERNAL_RELOCS are a buffer where the external form of the
2101 relocations should be stored.
2102
2103 Returns FALSE if something goes wrong. */
2104
2105static bfd_boolean
268b6b39 2106elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2107 asection *sec,
268b6b39
AM
2108 Elf_Internal_Shdr *shdr,
2109 void *external_relocs,
2110 Elf_Internal_Rela *internal_relocs)
45d6a902 2111{
9c5bfbb7 2112 const struct elf_backend_data *bed;
268b6b39 2113 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2114 const bfd_byte *erela;
2115 const bfd_byte *erelaend;
2116 Elf_Internal_Rela *irela;
243ef1e0
L
2117 Elf_Internal_Shdr *symtab_hdr;
2118 size_t nsyms;
45d6a902 2119
45d6a902
AM
2120 /* Position ourselves at the start of the section. */
2121 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2122 return FALSE;
2123
2124 /* Read the relocations. */
2125 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2126 return FALSE;
2127
243ef1e0 2128 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2129 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2130
45d6a902
AM
2131 bed = get_elf_backend_data (abfd);
2132
2133 /* Convert the external relocations to the internal format. */
2134 if (shdr->sh_entsize == bed->s->sizeof_rel)
2135 swap_in = bed->s->swap_reloc_in;
2136 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2137 swap_in = bed->s->swap_reloca_in;
2138 else
2139 {
2140 bfd_set_error (bfd_error_wrong_format);
2141 return FALSE;
2142 }
2143
a50b1753 2144 erela = (const bfd_byte *) external_relocs;
51992aec 2145 erelaend = erela + shdr->sh_size;
45d6a902
AM
2146 irela = internal_relocs;
2147 while (erela < erelaend)
2148 {
243ef1e0
L
2149 bfd_vma r_symndx;
2150
45d6a902 2151 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2152 r_symndx = ELF32_R_SYM (irela->r_info);
2153 if (bed->s->arch_size == 64)
2154 r_symndx >>= 24;
ce98a316
NC
2155 if (nsyms > 0)
2156 {
2157 if ((size_t) r_symndx >= nsyms)
2158 {
2159 (*_bfd_error_handler)
2160 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2161 " for offset 0x%lx in section `%A'"),
2162 abfd, sec,
2163 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2164 bfd_set_error (bfd_error_bad_value);
2165 return FALSE;
2166 }
2167 }
cf35638d 2168 else if (r_symndx != STN_UNDEF)
243ef1e0
L
2169 {
2170 (*_bfd_error_handler)
ce98a316
NC
2171 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2172 " when the object file has no symbol table"),
d003868e
AM
2173 abfd, sec,
2174 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2175 bfd_set_error (bfd_error_bad_value);
2176 return FALSE;
2177 }
45d6a902
AM
2178 irela += bed->s->int_rels_per_ext_rel;
2179 erela += shdr->sh_entsize;
2180 }
2181
2182 return TRUE;
2183}
2184
2185/* Read and swap the relocs for a section O. They may have been
2186 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2187 not NULL, they are used as buffers to read into. They are known to
2188 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2189 the return value is allocated using either malloc or bfd_alloc,
2190 according to the KEEP_MEMORY argument. If O has two relocation
2191 sections (both REL and RELA relocations), then the REL_HDR
2192 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2193 RELA_HDR relocations. */
45d6a902
AM
2194
2195Elf_Internal_Rela *
268b6b39
AM
2196_bfd_elf_link_read_relocs (bfd *abfd,
2197 asection *o,
2198 void *external_relocs,
2199 Elf_Internal_Rela *internal_relocs,
2200 bfd_boolean keep_memory)
45d6a902 2201{
268b6b39 2202 void *alloc1 = NULL;
45d6a902 2203 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2204 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2205 struct bfd_elf_section_data *esdo = elf_section_data (o);
2206 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2207
d4730f92
BS
2208 if (esdo->relocs != NULL)
2209 return esdo->relocs;
45d6a902
AM
2210
2211 if (o->reloc_count == 0)
2212 return NULL;
2213
45d6a902
AM
2214 if (internal_relocs == NULL)
2215 {
2216 bfd_size_type size;
2217
2218 size = o->reloc_count;
2219 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2220 if (keep_memory)
a50b1753 2221 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2222 else
a50b1753 2223 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2224 if (internal_relocs == NULL)
2225 goto error_return;
2226 }
2227
2228 if (external_relocs == NULL)
2229 {
d4730f92
BS
2230 bfd_size_type size = 0;
2231
2232 if (esdo->rel.hdr)
2233 size += esdo->rel.hdr->sh_size;
2234 if (esdo->rela.hdr)
2235 size += esdo->rela.hdr->sh_size;
45d6a902 2236
268b6b39 2237 alloc1 = bfd_malloc (size);
45d6a902
AM
2238 if (alloc1 == NULL)
2239 goto error_return;
2240 external_relocs = alloc1;
2241 }
2242
d4730f92
BS
2243 internal_rela_relocs = internal_relocs;
2244 if (esdo->rel.hdr)
2245 {
2246 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2247 external_relocs,
2248 internal_relocs))
2249 goto error_return;
2250 external_relocs = (((bfd_byte *) external_relocs)
2251 + esdo->rel.hdr->sh_size);
2252 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2253 * bed->s->int_rels_per_ext_rel);
2254 }
2255
2256 if (esdo->rela.hdr
2257 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2258 external_relocs,
2259 internal_rela_relocs)))
45d6a902
AM
2260 goto error_return;
2261
2262 /* Cache the results for next time, if we can. */
2263 if (keep_memory)
d4730f92 2264 esdo->relocs = internal_relocs;
45d6a902
AM
2265
2266 if (alloc1 != NULL)
2267 free (alloc1);
2268
2269 /* Don't free alloc2, since if it was allocated we are passing it
2270 back (under the name of internal_relocs). */
2271
2272 return internal_relocs;
2273
2274 error_return:
2275 if (alloc1 != NULL)
2276 free (alloc1);
2277 if (alloc2 != NULL)
4dd07732
AM
2278 {
2279 if (keep_memory)
2280 bfd_release (abfd, alloc2);
2281 else
2282 free (alloc2);
2283 }
45d6a902
AM
2284 return NULL;
2285}
2286
2287/* Compute the size of, and allocate space for, REL_HDR which is the
2288 section header for a section containing relocations for O. */
2289
28caa186 2290static bfd_boolean
268b6b39 2291_bfd_elf_link_size_reloc_section (bfd *abfd,
d4730f92 2292 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2293{
d4730f92 2294 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2295
2296 /* That allows us to calculate the size of the section. */
d4730f92 2297 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2298
2299 /* The contents field must last into write_object_contents, so we
2300 allocate it with bfd_alloc rather than malloc. Also since we
2301 cannot be sure that the contents will actually be filled in,
2302 we zero the allocated space. */
a50b1753 2303 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2304 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2305 return FALSE;
2306
d4730f92 2307 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2308 {
2309 struct elf_link_hash_entry **p;
2310
a50b1753 2311 p = (struct elf_link_hash_entry **)
d4730f92 2312 bfd_zmalloc (reldata->count * sizeof (struct elf_link_hash_entry *));
45d6a902
AM
2313 if (p == NULL)
2314 return FALSE;
2315
d4730f92 2316 reldata->hashes = p;
45d6a902
AM
2317 }
2318
2319 return TRUE;
2320}
2321
2322/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2323 originated from the section given by INPUT_REL_HDR) to the
2324 OUTPUT_BFD. */
2325
2326bfd_boolean
268b6b39
AM
2327_bfd_elf_link_output_relocs (bfd *output_bfd,
2328 asection *input_section,
2329 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2330 Elf_Internal_Rela *internal_relocs,
2331 struct elf_link_hash_entry **rel_hash
2332 ATTRIBUTE_UNUSED)
45d6a902
AM
2333{
2334 Elf_Internal_Rela *irela;
2335 Elf_Internal_Rela *irelaend;
2336 bfd_byte *erel;
d4730f92 2337 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2338 asection *output_section;
9c5bfbb7 2339 const struct elf_backend_data *bed;
268b6b39 2340 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2341 struct bfd_elf_section_data *esdo;
45d6a902
AM
2342
2343 output_section = input_section->output_section;
45d6a902 2344
d4730f92
BS
2345 bed = get_elf_backend_data (output_bfd);
2346 esdo = elf_section_data (output_section);
2347 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2348 {
d4730f92
BS
2349 output_reldata = &esdo->rel;
2350 swap_out = bed->s->swap_reloc_out;
45d6a902 2351 }
d4730f92
BS
2352 else if (esdo->rela.hdr
2353 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2354 {
d4730f92
BS
2355 output_reldata = &esdo->rela;
2356 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2357 }
2358 else
2359 {
2360 (*_bfd_error_handler)
d003868e
AM
2361 (_("%B: relocation size mismatch in %B section %A"),
2362 output_bfd, input_section->owner, input_section);
297d8443 2363 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2364 return FALSE;
2365 }
2366
d4730f92
BS
2367 erel = output_reldata->hdr->contents;
2368 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2369 irela = internal_relocs;
2370 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2371 * bed->s->int_rels_per_ext_rel);
2372 while (irela < irelaend)
2373 {
2374 (*swap_out) (output_bfd, irela, erel);
2375 irela += bed->s->int_rels_per_ext_rel;
2376 erel += input_rel_hdr->sh_entsize;
2377 }
2378
2379 /* Bump the counter, so that we know where to add the next set of
2380 relocations. */
d4730f92 2381 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2382
2383 return TRUE;
2384}
2385\f
508c3946
L
2386/* Make weak undefined symbols in PIE dynamic. */
2387
2388bfd_boolean
2389_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2390 struct elf_link_hash_entry *h)
2391{
2392 if (info->pie
2393 && h->dynindx == -1
2394 && h->root.type == bfd_link_hash_undefweak)
2395 return bfd_elf_link_record_dynamic_symbol (info, h);
2396
2397 return TRUE;
2398}
2399
45d6a902
AM
2400/* Fix up the flags for a symbol. This handles various cases which
2401 can only be fixed after all the input files are seen. This is
2402 currently called by both adjust_dynamic_symbol and
2403 assign_sym_version, which is unnecessary but perhaps more robust in
2404 the face of future changes. */
2405
28caa186 2406static bfd_boolean
268b6b39
AM
2407_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2408 struct elf_info_failed *eif)
45d6a902 2409{
33774f08 2410 const struct elf_backend_data *bed;
508c3946 2411
45d6a902
AM
2412 /* If this symbol was mentioned in a non-ELF file, try to set
2413 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2414 permit a non-ELF file to correctly refer to a symbol defined in
2415 an ELF dynamic object. */
f5385ebf 2416 if (h->non_elf)
45d6a902
AM
2417 {
2418 while (h->root.type == bfd_link_hash_indirect)
2419 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2420
2421 if (h->root.type != bfd_link_hash_defined
2422 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2423 {
2424 h->ref_regular = 1;
2425 h->ref_regular_nonweak = 1;
2426 }
45d6a902
AM
2427 else
2428 {
2429 if (h->root.u.def.section->owner != NULL
2430 && (bfd_get_flavour (h->root.u.def.section->owner)
2431 == bfd_target_elf_flavour))
f5385ebf
AM
2432 {
2433 h->ref_regular = 1;
2434 h->ref_regular_nonweak = 1;
2435 }
45d6a902 2436 else
f5385ebf 2437 h->def_regular = 1;
45d6a902
AM
2438 }
2439
2440 if (h->dynindx == -1
f5385ebf
AM
2441 && (h->def_dynamic
2442 || h->ref_dynamic))
45d6a902 2443 {
c152c796 2444 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2445 {
2446 eif->failed = TRUE;
2447 return FALSE;
2448 }
2449 }
2450 }
2451 else
2452 {
f5385ebf 2453 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2454 was first seen in a non-ELF file. Fortunately, if the symbol
2455 was first seen in an ELF file, we're probably OK unless the
2456 symbol was defined in a non-ELF file. Catch that case here.
2457 FIXME: We're still in trouble if the symbol was first seen in
2458 a dynamic object, and then later in a non-ELF regular object. */
2459 if ((h->root.type == bfd_link_hash_defined
2460 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2461 && !h->def_regular
45d6a902
AM
2462 && (h->root.u.def.section->owner != NULL
2463 ? (bfd_get_flavour (h->root.u.def.section->owner)
2464 != bfd_target_elf_flavour)
2465 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2466 && !h->def_dynamic)))
2467 h->def_regular = 1;
45d6a902
AM
2468 }
2469
508c3946 2470 /* Backend specific symbol fixup. */
33774f08
AM
2471 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2472 if (bed->elf_backend_fixup_symbol
2473 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2474 return FALSE;
508c3946 2475
45d6a902
AM
2476 /* If this is a final link, and the symbol was defined as a common
2477 symbol in a regular object file, and there was no definition in
2478 any dynamic object, then the linker will have allocated space for
f5385ebf 2479 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2480 flag will not have been set. */
2481 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2482 && !h->def_regular
2483 && h->ref_regular
2484 && !h->def_dynamic
45d6a902 2485 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
f5385ebf 2486 h->def_regular = 1;
45d6a902
AM
2487
2488 /* If -Bsymbolic was used (which means to bind references to global
2489 symbols to the definition within the shared object), and this
2490 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2491 need a PLT entry. Likewise, if the symbol has non-default
2492 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2493 will force it local. */
f5385ebf 2494 if (h->needs_plt
45d6a902 2495 && eif->info->shared
0eddce27 2496 && is_elf_hash_table (eif->info->hash)
55255dae 2497 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2498 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2499 && h->def_regular)
45d6a902 2500 {
45d6a902
AM
2501 bfd_boolean force_local;
2502
45d6a902
AM
2503 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2504 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2505 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2506 }
2507
2508 /* If a weak undefined symbol has non-default visibility, we also
2509 hide it from the dynamic linker. */
9c7a29a3 2510 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2511 && h->root.type == bfd_link_hash_undefweak)
33774f08 2512 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2513
2514 /* If this is a weak defined symbol in a dynamic object, and we know
2515 the real definition in the dynamic object, copy interesting flags
2516 over to the real definition. */
f6e332e6 2517 if (h->u.weakdef != NULL)
45d6a902 2518 {
45d6a902
AM
2519 /* If the real definition is defined by a regular object file,
2520 don't do anything special. See the longer description in
2521 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2522 if (h->u.weakdef->def_regular)
f6e332e6 2523 h->u.weakdef = NULL;
45d6a902 2524 else
a26587ba 2525 {
4e6b54a6
AM
2526 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2527
2528 while (h->root.type == bfd_link_hash_indirect)
2529 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2530
2531 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2532 || h->root.type == bfd_link_hash_defweak);
2533 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2534 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2535 || weakdef->root.type == bfd_link_hash_defweak);
2536 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2537 }
45d6a902
AM
2538 }
2539
2540 return TRUE;
2541}
2542
2543/* Make the backend pick a good value for a dynamic symbol. This is
2544 called via elf_link_hash_traverse, and also calls itself
2545 recursively. */
2546
28caa186 2547static bfd_boolean
268b6b39 2548_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2549{
a50b1753 2550 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2551 bfd *dynobj;
9c5bfbb7 2552 const struct elf_backend_data *bed;
45d6a902 2553
0eddce27 2554 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2555 return FALSE;
2556
45d6a902
AM
2557 /* Ignore indirect symbols. These are added by the versioning code. */
2558 if (h->root.type == bfd_link_hash_indirect)
2559 return TRUE;
2560
2561 /* Fix the symbol flags. */
2562 if (! _bfd_elf_fix_symbol_flags (h, eif))
2563 return FALSE;
2564
2565 /* If this symbol does not require a PLT entry, and it is not
2566 defined by a dynamic object, or is not referenced by a regular
2567 object, ignore it. We do have to handle a weak defined symbol,
2568 even if no regular object refers to it, if we decided to add it
2569 to the dynamic symbol table. FIXME: Do we normally need to worry
2570 about symbols which are defined by one dynamic object and
2571 referenced by another one? */
f5385ebf 2572 if (!h->needs_plt
91e21fb7 2573 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2574 && (h->def_regular
2575 || !h->def_dynamic
2576 || (!h->ref_regular
f6e332e6 2577 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2578 {
a6aa5195 2579 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2580 return TRUE;
2581 }
2582
2583 /* If we've already adjusted this symbol, don't do it again. This
2584 can happen via a recursive call. */
f5385ebf 2585 if (h->dynamic_adjusted)
45d6a902
AM
2586 return TRUE;
2587
2588 /* Don't look at this symbol again. Note that we must set this
2589 after checking the above conditions, because we may look at a
2590 symbol once, decide not to do anything, and then get called
2591 recursively later after REF_REGULAR is set below. */
f5385ebf 2592 h->dynamic_adjusted = 1;
45d6a902
AM
2593
2594 /* If this is a weak definition, and we know a real definition, and
2595 the real symbol is not itself defined by a regular object file,
2596 then get a good value for the real definition. We handle the
2597 real symbol first, for the convenience of the backend routine.
2598
2599 Note that there is a confusing case here. If the real definition
2600 is defined by a regular object file, we don't get the real symbol
2601 from the dynamic object, but we do get the weak symbol. If the
2602 processor backend uses a COPY reloc, then if some routine in the
2603 dynamic object changes the real symbol, we will not see that
2604 change in the corresponding weak symbol. This is the way other
2605 ELF linkers work as well, and seems to be a result of the shared
2606 library model.
2607
2608 I will clarify this issue. Most SVR4 shared libraries define the
2609 variable _timezone and define timezone as a weak synonym. The
2610 tzset call changes _timezone. If you write
2611 extern int timezone;
2612 int _timezone = 5;
2613 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2614 you might expect that, since timezone is a synonym for _timezone,
2615 the same number will print both times. However, if the processor
2616 backend uses a COPY reloc, then actually timezone will be copied
2617 into your process image, and, since you define _timezone
2618 yourself, _timezone will not. Thus timezone and _timezone will
2619 wind up at different memory locations. The tzset call will set
2620 _timezone, leaving timezone unchanged. */
2621
f6e332e6 2622 if (h->u.weakdef != NULL)
45d6a902 2623 {
ec24dc88
AM
2624 /* If we get to this point, there is an implicit reference to
2625 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2626 h->u.weakdef->ref_regular = 1;
45d6a902 2627
ec24dc88
AM
2628 /* Ensure that the backend adjust_dynamic_symbol function sees
2629 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2630 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2631 return FALSE;
2632 }
2633
2634 /* If a symbol has no type and no size and does not require a PLT
2635 entry, then we are probably about to do the wrong thing here: we
2636 are probably going to create a COPY reloc for an empty object.
2637 This case can arise when a shared object is built with assembly
2638 code, and the assembly code fails to set the symbol type. */
2639 if (h->size == 0
2640 && h->type == STT_NOTYPE
f5385ebf 2641 && !h->needs_plt)
45d6a902
AM
2642 (*_bfd_error_handler)
2643 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2644 h->root.root.string);
2645
2646 dynobj = elf_hash_table (eif->info)->dynobj;
2647 bed = get_elf_backend_data (dynobj);
e7c33416 2648
45d6a902
AM
2649 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2650 {
2651 eif->failed = TRUE;
2652 return FALSE;
2653 }
2654
2655 return TRUE;
2656}
2657
027297b7
L
2658/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2659 DYNBSS. */
2660
2661bfd_boolean
2662_bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2663 asection *dynbss)
2664{
91ac5911 2665 unsigned int power_of_two;
027297b7
L
2666 bfd_vma mask;
2667 asection *sec = h->root.u.def.section;
2668
2669 /* The section aligment of definition is the maximum alignment
91ac5911
L
2670 requirement of symbols defined in the section. Since we don't
2671 know the symbol alignment requirement, we start with the
2672 maximum alignment and check low bits of the symbol address
2673 for the minimum alignment. */
2674 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2675 mask = ((bfd_vma) 1 << power_of_two) - 1;
2676 while ((h->root.u.def.value & mask) != 0)
2677 {
2678 mask >>= 1;
2679 --power_of_two;
2680 }
027297b7 2681
91ac5911
L
2682 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2683 dynbss))
027297b7
L
2684 {
2685 /* Adjust the section alignment if needed. */
2686 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2687 power_of_two))
027297b7
L
2688 return FALSE;
2689 }
2690
91ac5911 2691 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2692 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2693
2694 /* Define the symbol as being at this point in DYNBSS. */
2695 h->root.u.def.section = dynbss;
2696 h->root.u.def.value = dynbss->size;
2697
2698 /* Increment the size of DYNBSS to make room for the symbol. */
2699 dynbss->size += h->size;
2700
2701 return TRUE;
2702}
2703
45d6a902
AM
2704/* Adjust all external symbols pointing into SEC_MERGE sections
2705 to reflect the object merging within the sections. */
2706
28caa186 2707static bfd_boolean
268b6b39 2708_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2709{
2710 asection *sec;
2711
45d6a902
AM
2712 if ((h->root.type == bfd_link_hash_defined
2713 || h->root.type == bfd_link_hash_defweak)
2714 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2715 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2716 {
a50b1753 2717 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2718
2719 h->root.u.def.value =
2720 _bfd_merged_section_offset (output_bfd,
2721 &h->root.u.def.section,
2722 elf_section_data (sec)->sec_info,
753731ee 2723 h->root.u.def.value);
45d6a902
AM
2724 }
2725
2726 return TRUE;
2727}
986a241f
RH
2728
2729/* Returns false if the symbol referred to by H should be considered
2730 to resolve local to the current module, and true if it should be
2731 considered to bind dynamically. */
2732
2733bfd_boolean
268b6b39
AM
2734_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2735 struct bfd_link_info *info,
89a2ee5a 2736 bfd_boolean not_local_protected)
986a241f
RH
2737{
2738 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2739 const struct elf_backend_data *bed;
2740 struct elf_link_hash_table *hash_table;
986a241f
RH
2741
2742 if (h == NULL)
2743 return FALSE;
2744
2745 while (h->root.type == bfd_link_hash_indirect
2746 || h->root.type == bfd_link_hash_warning)
2747 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2748
2749 /* If it was forced local, then clearly it's not dynamic. */
2750 if (h->dynindx == -1)
2751 return FALSE;
f5385ebf 2752 if (h->forced_local)
986a241f
RH
2753 return FALSE;
2754
2755 /* Identify the cases where name binding rules say that a
2756 visible symbol resolves locally. */
55255dae 2757 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
986a241f
RH
2758
2759 switch (ELF_ST_VISIBILITY (h->other))
2760 {
2761 case STV_INTERNAL:
2762 case STV_HIDDEN:
2763 return FALSE;
2764
2765 case STV_PROTECTED:
fcb93ecf
PB
2766 hash_table = elf_hash_table (info);
2767 if (!is_elf_hash_table (hash_table))
2768 return FALSE;
2769
2770 bed = get_elf_backend_data (hash_table->dynobj);
2771
986a241f
RH
2772 /* Proper resolution for function pointer equality may require
2773 that these symbols perhaps be resolved dynamically, even though
2774 we should be resolving them to the current module. */
89a2ee5a 2775 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2776 binding_stays_local_p = TRUE;
2777 break;
2778
2779 default:
986a241f
RH
2780 break;
2781 }
2782
aa37626c 2783 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2784 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2785 return TRUE;
2786
986a241f
RH
2787 /* Otherwise, the symbol is dynamic if binding rules don't tell
2788 us that it remains local. */
2789 return !binding_stays_local_p;
2790}
f6c52c13
AM
2791
2792/* Return true if the symbol referred to by H should be considered
2793 to resolve local to the current module, and false otherwise. Differs
2794 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2795 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2796 for the place where forced_local and dynindx == -1 are tested. If
2797 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2798 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2799 the symbol is local only for defined symbols.
2800 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2801 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2802 treatment of undefined weak symbols. For those that do not make
2803 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2804
2805bfd_boolean
268b6b39
AM
2806_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2807 struct bfd_link_info *info,
2808 bfd_boolean local_protected)
f6c52c13 2809{
fcb93ecf
PB
2810 const struct elf_backend_data *bed;
2811 struct elf_link_hash_table *hash_table;
2812
f6c52c13
AM
2813 /* If it's a local sym, of course we resolve locally. */
2814 if (h == NULL)
2815 return TRUE;
2816
d95edcac
L
2817 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2818 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2819 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2820 return TRUE;
2821
7e2294f9
AO
2822 /* Common symbols that become definitions don't get the DEF_REGULAR
2823 flag set, so test it first, and don't bail out. */
2824 if (ELF_COMMON_DEF_P (h))
2825 /* Do nothing. */;
f6c52c13 2826 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2827 resolve locally. The sym is either undefined or dynamic. */
2828 else if (!h->def_regular)
f6c52c13
AM
2829 return FALSE;
2830
2831 /* Forced local symbols resolve locally. */
f5385ebf 2832 if (h->forced_local)
f6c52c13
AM
2833 return TRUE;
2834
2835 /* As do non-dynamic symbols. */
2836 if (h->dynindx == -1)
2837 return TRUE;
2838
2839 /* At this point, we know the symbol is defined and dynamic. In an
2840 executable it must resolve locally, likewise when building symbolic
2841 shared libraries. */
55255dae 2842 if (info->executable || SYMBOLIC_BIND (info, h))
f6c52c13
AM
2843 return TRUE;
2844
2845 /* Now deal with defined dynamic symbols in shared libraries. Ones
2846 with default visibility might not resolve locally. */
2847 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2848 return FALSE;
2849
fcb93ecf
PB
2850 hash_table = elf_hash_table (info);
2851 if (!is_elf_hash_table (hash_table))
2852 return TRUE;
2853
2854 bed = get_elf_backend_data (hash_table->dynobj);
2855
1c16dfa5 2856 /* STV_PROTECTED non-function symbols are local. */
fcb93ecf 2857 if (!bed->is_function_type (h->type))
1c16dfa5
L
2858 return TRUE;
2859
f6c52c13 2860 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
2861 symbols be treated as dynamic symbols. If the address of a
2862 function not defined in an executable is set to that function's
2863 plt entry in the executable, then the address of the function in
2864 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
2865 return local_protected;
2866}
e1918d23
AM
2867
2868/* Caches some TLS segment info, and ensures that the TLS segment vma is
2869 aligned. Returns the first TLS output section. */
2870
2871struct bfd_section *
2872_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2873{
2874 struct bfd_section *sec, *tls;
2875 unsigned int align = 0;
2876
2877 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2878 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2879 break;
2880 tls = sec;
2881
2882 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2883 if (sec->alignment_power > align)
2884 align = sec->alignment_power;
2885
2886 elf_hash_table (info)->tls_sec = tls;
2887
2888 /* Ensure the alignment of the first section is the largest alignment,
2889 so that the tls segment starts aligned. */
2890 if (tls != NULL)
2891 tls->alignment_power = align;
2892
2893 return tls;
2894}
0ad989f9
L
2895
2896/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2897static bfd_boolean
2898is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2899 Elf_Internal_Sym *sym)
2900{
a4d8e49b
L
2901 const struct elf_backend_data *bed;
2902
0ad989f9
L
2903 /* Local symbols do not count, but target specific ones might. */
2904 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2905 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2906 return FALSE;
2907
fcb93ecf 2908 bed = get_elf_backend_data (abfd);
0ad989f9 2909 /* Function symbols do not count. */
fcb93ecf 2910 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
2911 return FALSE;
2912
2913 /* If the section is undefined, then so is the symbol. */
2914 if (sym->st_shndx == SHN_UNDEF)
2915 return FALSE;
2916
2917 /* If the symbol is defined in the common section, then
2918 it is a common definition and so does not count. */
a4d8e49b 2919 if (bed->common_definition (sym))
0ad989f9
L
2920 return FALSE;
2921
2922 /* If the symbol is in a target specific section then we
2923 must rely upon the backend to tell us what it is. */
2924 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2925 /* FIXME - this function is not coded yet:
2926
2927 return _bfd_is_global_symbol_definition (abfd, sym);
2928
2929 Instead for now assume that the definition is not global,
2930 Even if this is wrong, at least the linker will behave
2931 in the same way that it used to do. */
2932 return FALSE;
2933
2934 return TRUE;
2935}
2936
2937/* Search the symbol table of the archive element of the archive ABFD
2938 whose archive map contains a mention of SYMDEF, and determine if
2939 the symbol is defined in this element. */
2940static bfd_boolean
2941elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2942{
2943 Elf_Internal_Shdr * hdr;
2944 bfd_size_type symcount;
2945 bfd_size_type extsymcount;
2946 bfd_size_type extsymoff;
2947 Elf_Internal_Sym *isymbuf;
2948 Elf_Internal_Sym *isym;
2949 Elf_Internal_Sym *isymend;
2950 bfd_boolean result;
2951
2952 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2953 if (abfd == NULL)
2954 return FALSE;
2955
2956 if (! bfd_check_format (abfd, bfd_object))
2957 return FALSE;
2958
2959 /* If we have already included the element containing this symbol in the
2960 link then we do not need to include it again. Just claim that any symbol
2961 it contains is not a definition, so that our caller will not decide to
2962 (re)include this element. */
2963 if (abfd->archive_pass)
2964 return FALSE;
2965
2966 /* Select the appropriate symbol table. */
2967 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2968 hdr = &elf_tdata (abfd)->symtab_hdr;
2969 else
2970 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2971
2972 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2973
2974 /* The sh_info field of the symtab header tells us where the
2975 external symbols start. We don't care about the local symbols. */
2976 if (elf_bad_symtab (abfd))
2977 {
2978 extsymcount = symcount;
2979 extsymoff = 0;
2980 }
2981 else
2982 {
2983 extsymcount = symcount - hdr->sh_info;
2984 extsymoff = hdr->sh_info;
2985 }
2986
2987 if (extsymcount == 0)
2988 return FALSE;
2989
2990 /* Read in the symbol table. */
2991 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2992 NULL, NULL, NULL);
2993 if (isymbuf == NULL)
2994 return FALSE;
2995
2996 /* Scan the symbol table looking for SYMDEF. */
2997 result = FALSE;
2998 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2999 {
3000 const char *name;
3001
3002 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3003 isym->st_name);
3004 if (name == NULL)
3005 break;
3006
3007 if (strcmp (name, symdef->name) == 0)
3008 {
3009 result = is_global_data_symbol_definition (abfd, isym);
3010 break;
3011 }
3012 }
3013
3014 free (isymbuf);
3015
3016 return result;
3017}
3018\f
5a580b3a
AM
3019/* Add an entry to the .dynamic table. */
3020
3021bfd_boolean
3022_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3023 bfd_vma tag,
3024 bfd_vma val)
3025{
3026 struct elf_link_hash_table *hash_table;
3027 const struct elf_backend_data *bed;
3028 asection *s;
3029 bfd_size_type newsize;
3030 bfd_byte *newcontents;
3031 Elf_Internal_Dyn dyn;
3032
3033 hash_table = elf_hash_table (info);
3034 if (! is_elf_hash_table (hash_table))
3035 return FALSE;
3036
3037 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3038 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3039 BFD_ASSERT (s != NULL);
3040
eea6121a 3041 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3042 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3043 if (newcontents == NULL)
3044 return FALSE;
3045
3046 dyn.d_tag = tag;
3047 dyn.d_un.d_val = val;
eea6121a 3048 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3049
eea6121a 3050 s->size = newsize;
5a580b3a
AM
3051 s->contents = newcontents;
3052
3053 return TRUE;
3054}
3055
3056/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3057 otherwise just check whether one already exists. Returns -1 on error,
3058 1 if a DT_NEEDED tag already exists, and 0 on success. */
3059
4ad4eba5 3060static int
7e9f0867
AM
3061elf_add_dt_needed_tag (bfd *abfd,
3062 struct bfd_link_info *info,
4ad4eba5
AM
3063 const char *soname,
3064 bfd_boolean do_it)
5a580b3a
AM
3065{
3066 struct elf_link_hash_table *hash_table;
3067 bfd_size_type oldsize;
3068 bfd_size_type strindex;
3069
7e9f0867
AM
3070 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3071 return -1;
3072
5a580b3a
AM
3073 hash_table = elf_hash_table (info);
3074 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3075 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3076 if (strindex == (bfd_size_type) -1)
3077 return -1;
3078
3079 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3080 {
3081 asection *sdyn;
3082 const struct elf_backend_data *bed;
3083 bfd_byte *extdyn;
3084
3085 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3086 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3087 if (sdyn != NULL)
3088 for (extdyn = sdyn->contents;
3089 extdyn < sdyn->contents + sdyn->size;
3090 extdyn += bed->s->sizeof_dyn)
3091 {
3092 Elf_Internal_Dyn dyn;
5a580b3a 3093
7e9f0867
AM
3094 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3095 if (dyn.d_tag == DT_NEEDED
3096 && dyn.d_un.d_val == strindex)
3097 {
3098 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3099 return 1;
3100 }
3101 }
5a580b3a
AM
3102 }
3103
3104 if (do_it)
3105 {
7e9f0867
AM
3106 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3107 return -1;
3108
5a580b3a
AM
3109 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3110 return -1;
3111 }
3112 else
3113 /* We were just checking for existence of the tag. */
3114 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3115
3116 return 0;
3117}
3118
010e5ae2
AM
3119static bfd_boolean
3120on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3121{
3122 for (; needed != NULL; needed = needed->next)
3123 if (strcmp (soname, needed->name) == 0)
3124 return TRUE;
3125
3126 return FALSE;
3127}
3128
5a580b3a 3129/* Sort symbol by value and section. */
4ad4eba5
AM
3130static int
3131elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3132{
3133 const struct elf_link_hash_entry *h1;
3134 const struct elf_link_hash_entry *h2;
10b7e05b 3135 bfd_signed_vma vdiff;
5a580b3a
AM
3136
3137 h1 = *(const struct elf_link_hash_entry **) arg1;
3138 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3139 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3140 if (vdiff != 0)
3141 return vdiff > 0 ? 1 : -1;
3142 else
3143 {
3144 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3145 if (sdiff != 0)
3146 return sdiff > 0 ? 1 : -1;
3147 }
5a580b3a
AM
3148 return 0;
3149}
4ad4eba5 3150
5a580b3a
AM
3151/* This function is used to adjust offsets into .dynstr for
3152 dynamic symbols. This is called via elf_link_hash_traverse. */
3153
3154static bfd_boolean
3155elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3156{
a50b1753 3157 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3158
5a580b3a
AM
3159 if (h->dynindx != -1)
3160 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3161 return TRUE;
3162}
3163
3164/* Assign string offsets in .dynstr, update all structures referencing
3165 them. */
3166
4ad4eba5
AM
3167static bfd_boolean
3168elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3169{
3170 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3171 struct elf_link_local_dynamic_entry *entry;
3172 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3173 bfd *dynobj = hash_table->dynobj;
3174 asection *sdyn;
3175 bfd_size_type size;
3176 const struct elf_backend_data *bed;
3177 bfd_byte *extdyn;
3178
3179 _bfd_elf_strtab_finalize (dynstr);
3180 size = _bfd_elf_strtab_size (dynstr);
3181
3182 bed = get_elf_backend_data (dynobj);
3d4d4302 3183 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3184 BFD_ASSERT (sdyn != NULL);
3185
3186 /* Update all .dynamic entries referencing .dynstr strings. */
3187 for (extdyn = sdyn->contents;
eea6121a 3188 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3189 extdyn += bed->s->sizeof_dyn)
3190 {
3191 Elf_Internal_Dyn dyn;
3192
3193 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3194 switch (dyn.d_tag)
3195 {
3196 case DT_STRSZ:
3197 dyn.d_un.d_val = size;
3198 break;
3199 case DT_NEEDED:
3200 case DT_SONAME:
3201 case DT_RPATH:
3202 case DT_RUNPATH:
3203 case DT_FILTER:
3204 case DT_AUXILIARY:
7ee314fa
AM
3205 case DT_AUDIT:
3206 case DT_DEPAUDIT:
5a580b3a
AM
3207 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3208 break;
3209 default:
3210 continue;
3211 }
3212 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3213 }
3214
3215 /* Now update local dynamic symbols. */
3216 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3217 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3218 entry->isym.st_name);
3219
3220 /* And the rest of dynamic symbols. */
3221 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3222
3223 /* Adjust version definitions. */
3224 if (elf_tdata (output_bfd)->cverdefs)
3225 {
3226 asection *s;
3227 bfd_byte *p;
3228 bfd_size_type i;
3229 Elf_Internal_Verdef def;
3230 Elf_Internal_Verdaux defaux;
3231
3d4d4302 3232 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3233 p = s->contents;
3234 do
3235 {
3236 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3237 &def);
3238 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3239 if (def.vd_aux != sizeof (Elf_External_Verdef))
3240 continue;
5a580b3a
AM
3241 for (i = 0; i < def.vd_cnt; ++i)
3242 {
3243 _bfd_elf_swap_verdaux_in (output_bfd,
3244 (Elf_External_Verdaux *) p, &defaux);
3245 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3246 defaux.vda_name);
3247 _bfd_elf_swap_verdaux_out (output_bfd,
3248 &defaux, (Elf_External_Verdaux *) p);
3249 p += sizeof (Elf_External_Verdaux);
3250 }
3251 }
3252 while (def.vd_next);
3253 }
3254
3255 /* Adjust version references. */
3256 if (elf_tdata (output_bfd)->verref)
3257 {
3258 asection *s;
3259 bfd_byte *p;
3260 bfd_size_type i;
3261 Elf_Internal_Verneed need;
3262 Elf_Internal_Vernaux needaux;
3263
3d4d4302 3264 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3265 p = s->contents;
3266 do
3267 {
3268 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3269 &need);
3270 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3271 _bfd_elf_swap_verneed_out (output_bfd, &need,
3272 (Elf_External_Verneed *) p);
3273 p += sizeof (Elf_External_Verneed);
3274 for (i = 0; i < need.vn_cnt; ++i)
3275 {
3276 _bfd_elf_swap_vernaux_in (output_bfd,
3277 (Elf_External_Vernaux *) p, &needaux);
3278 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3279 needaux.vna_name);
3280 _bfd_elf_swap_vernaux_out (output_bfd,
3281 &needaux,
3282 (Elf_External_Vernaux *) p);
3283 p += sizeof (Elf_External_Vernaux);
3284 }
3285 }
3286 while (need.vn_next);
3287 }
3288
3289 return TRUE;
3290}
3291\f
13285a1b
AM
3292/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3293 The default is to only match when the INPUT and OUTPUT are exactly
3294 the same target. */
3295
3296bfd_boolean
3297_bfd_elf_default_relocs_compatible (const bfd_target *input,
3298 const bfd_target *output)
3299{
3300 return input == output;
3301}
3302
3303/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3304 This version is used when different targets for the same architecture
3305 are virtually identical. */
3306
3307bfd_boolean
3308_bfd_elf_relocs_compatible (const bfd_target *input,
3309 const bfd_target *output)
3310{
3311 const struct elf_backend_data *obed, *ibed;
3312
3313 if (input == output)
3314 return TRUE;
3315
3316 ibed = xvec_get_elf_backend_data (input);
3317 obed = xvec_get_elf_backend_data (output);
3318
3319 if (ibed->arch != obed->arch)
3320 return FALSE;
3321
3322 /* If both backends are using this function, deem them compatible. */
3323 return ibed->relocs_compatible == obed->relocs_compatible;
3324}
3325
4ad4eba5
AM
3326/* Add symbols from an ELF object file to the linker hash table. */
3327
3328static bfd_boolean
3329elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3330{
a0c402a5 3331 Elf_Internal_Ehdr *ehdr;
4ad4eba5
AM
3332 Elf_Internal_Shdr *hdr;
3333 bfd_size_type symcount;
3334 bfd_size_type extsymcount;
3335 bfd_size_type extsymoff;
3336 struct elf_link_hash_entry **sym_hash;
3337 bfd_boolean dynamic;
3338 Elf_External_Versym *extversym = NULL;
3339 Elf_External_Versym *ever;
3340 struct elf_link_hash_entry *weaks;
3341 struct elf_link_hash_entry **nondeflt_vers = NULL;
3342 bfd_size_type nondeflt_vers_cnt = 0;
3343 Elf_Internal_Sym *isymbuf = NULL;
3344 Elf_Internal_Sym *isym;
3345 Elf_Internal_Sym *isymend;
3346 const struct elf_backend_data *bed;
3347 bfd_boolean add_needed;
66eb6687 3348 struct elf_link_hash_table *htab;
4ad4eba5 3349 bfd_size_type amt;
66eb6687 3350 void *alloc_mark = NULL;
4f87808c
AM
3351 struct bfd_hash_entry **old_table = NULL;
3352 unsigned int old_size = 0;
3353 unsigned int old_count = 0;
66eb6687
AM
3354 void *old_tab = NULL;
3355 void *old_hash;
3356 void *old_ent;
3357 struct bfd_link_hash_entry *old_undefs = NULL;
3358 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3359 long old_dynsymcount = 0;
3360 size_t tabsize = 0;
3361 size_t hashsize = 0;
4ad4eba5 3362
66eb6687 3363 htab = elf_hash_table (info);
4ad4eba5 3364 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3365
3366 if ((abfd->flags & DYNAMIC) == 0)
3367 dynamic = FALSE;
3368 else
3369 {
3370 dynamic = TRUE;
3371
3372 /* You can't use -r against a dynamic object. Also, there's no
3373 hope of using a dynamic object which does not exactly match
3374 the format of the output file. */
3375 if (info->relocatable
66eb6687 3376 || !is_elf_hash_table (htab)
f13a99db 3377 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3378 {
9a0789ec
NC
3379 if (info->relocatable)
3380 bfd_set_error (bfd_error_invalid_operation);
3381 else
3382 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3383 goto error_return;
3384 }
3385 }
3386
a0c402a5
L
3387 ehdr = elf_elfheader (abfd);
3388 if (info->warn_alternate_em
3389 && bed->elf_machine_code != ehdr->e_machine
3390 && ((bed->elf_machine_alt1 != 0
3391 && ehdr->e_machine == bed->elf_machine_alt1)
3392 || (bed->elf_machine_alt2 != 0
3393 && ehdr->e_machine == bed->elf_machine_alt2)))
3394 info->callbacks->einfo
3395 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3396 ehdr->e_machine, abfd, bed->elf_machine_code);
3397
4ad4eba5
AM
3398 /* As a GNU extension, any input sections which are named
3399 .gnu.warning.SYMBOL are treated as warning symbols for the given
3400 symbol. This differs from .gnu.warning sections, which generate
3401 warnings when they are included in an output file. */
dd98f8d2
NC
3402 /* PR 12761: Also generate this warning when building shared libraries. */
3403 if (info->executable || info->shared)
4ad4eba5
AM
3404 {
3405 asection *s;
3406
3407 for (s = abfd->sections; s != NULL; s = s->next)
3408 {
3409 const char *name;
3410
3411 name = bfd_get_section_name (abfd, s);
0112cd26 3412 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5
AM
3413 {
3414 char *msg;
3415 bfd_size_type sz;
4ad4eba5
AM
3416
3417 name += sizeof ".gnu.warning." - 1;
3418
3419 /* If this is a shared object, then look up the symbol
3420 in the hash table. If it is there, and it is already
3421 been defined, then we will not be using the entry
3422 from this shared object, so we don't need to warn.
3423 FIXME: If we see the definition in a regular object
3424 later on, we will warn, but we shouldn't. The only
3425 fix is to keep track of what warnings we are supposed
3426 to emit, and then handle them all at the end of the
3427 link. */
3428 if (dynamic)
3429 {
3430 struct elf_link_hash_entry *h;
3431
66eb6687 3432 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4ad4eba5
AM
3433
3434 /* FIXME: What about bfd_link_hash_common? */
3435 if (h != NULL
3436 && (h->root.type == bfd_link_hash_defined
3437 || h->root.type == bfd_link_hash_defweak))
3438 {
3439 /* We don't want to issue this warning. Clobber
3440 the section size so that the warning does not
3441 get copied into the output file. */
eea6121a 3442 s->size = 0;
4ad4eba5
AM
3443 continue;
3444 }
3445 }
3446
eea6121a 3447 sz = s->size;
a50b1753 3448 msg = (char *) bfd_alloc (abfd, sz + 1);
4ad4eba5
AM
3449 if (msg == NULL)
3450 goto error_return;
3451
370a0e1b 3452 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4ad4eba5
AM
3453 goto error_return;
3454
370a0e1b 3455 msg[sz] = '\0';
4ad4eba5
AM
3456
3457 if (! (_bfd_generic_link_add_one_symbol
3458 (info, abfd, name, BSF_WARNING, s, 0, msg,
66eb6687 3459 FALSE, bed->collect, NULL)))
4ad4eba5
AM
3460 goto error_return;
3461
3462 if (! info->relocatable)
3463 {
3464 /* Clobber the section size so that the warning does
3465 not get copied into the output file. */
eea6121a 3466 s->size = 0;
11d2f718
AM
3467
3468 /* Also set SEC_EXCLUDE, so that symbols defined in
3469 the warning section don't get copied to the output. */
3470 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3471 }
3472 }
3473 }
3474 }
3475
3476 add_needed = TRUE;
3477 if (! dynamic)
3478 {
3479 /* If we are creating a shared library, create all the dynamic
3480 sections immediately. We need to attach them to something,
3481 so we attach them to this BFD, provided it is the right
3482 format. FIXME: If there are no input BFD's of the same
3483 format as the output, we can't make a shared library. */
3484 if (info->shared
66eb6687 3485 && is_elf_hash_table (htab)
f13a99db 3486 && info->output_bfd->xvec == abfd->xvec
66eb6687 3487 && !htab->dynamic_sections_created)
4ad4eba5
AM
3488 {
3489 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3490 goto error_return;
3491 }
3492 }
66eb6687 3493 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3494 goto error_return;
3495 else
3496 {
3497 asection *s;
3498 const char *soname = NULL;
7ee314fa 3499 char *audit = NULL;
4ad4eba5
AM
3500 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3501 int ret;
3502
3503 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3504 ld shouldn't allow it. */
4ad4eba5 3505 if ((s = abfd->sections) != NULL
dbaa2011 3506 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
92fd189d 3507 abort ();
4ad4eba5
AM
3508
3509 /* If this dynamic lib was specified on the command line with
3510 --as-needed in effect, then we don't want to add a DT_NEEDED
3511 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3512 in by another lib's DT_NEEDED. When --no-add-needed is used
3513 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3514 any dynamic library in DT_NEEDED tags in the dynamic lib at
3515 all. */
3516 add_needed = (elf_dyn_lib_class (abfd)
3517 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3518 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3519
3520 s = bfd_get_section_by_name (abfd, ".dynamic");
3521 if (s != NULL)
3522 {
3523 bfd_byte *dynbuf;
3524 bfd_byte *extdyn;
cb33740c 3525 unsigned int elfsec;
4ad4eba5
AM
3526 unsigned long shlink;
3527
eea6121a 3528 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3529 {
3530error_free_dyn:
3531 free (dynbuf);
3532 goto error_return;
3533 }
4ad4eba5
AM
3534
3535 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3536 if (elfsec == SHN_BAD)
4ad4eba5
AM
3537 goto error_free_dyn;
3538 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3539
3540 for (extdyn = dynbuf;
eea6121a 3541 extdyn < dynbuf + s->size;
4ad4eba5
AM
3542 extdyn += bed->s->sizeof_dyn)
3543 {
3544 Elf_Internal_Dyn dyn;
3545
3546 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3547 if (dyn.d_tag == DT_SONAME)
3548 {
3549 unsigned int tagv = dyn.d_un.d_val;
3550 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3551 if (soname == NULL)
3552 goto error_free_dyn;
3553 }
3554 if (dyn.d_tag == DT_NEEDED)
3555 {
3556 struct bfd_link_needed_list *n, **pn;
3557 char *fnm, *anm;
3558 unsigned int tagv = dyn.d_un.d_val;
3559
3560 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3561 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3562 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3563 if (n == NULL || fnm == NULL)
3564 goto error_free_dyn;
3565 amt = strlen (fnm) + 1;
a50b1753 3566 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3567 if (anm == NULL)
3568 goto error_free_dyn;
3569 memcpy (anm, fnm, amt);
3570 n->name = anm;
3571 n->by = abfd;
3572 n->next = NULL;
66eb6687 3573 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3574 ;
3575 *pn = n;
3576 }
3577 if (dyn.d_tag == DT_RUNPATH)
3578 {
3579 struct bfd_link_needed_list *n, **pn;
3580 char *fnm, *anm;
3581 unsigned int tagv = dyn.d_un.d_val;
3582
3583 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3584 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3585 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3586 if (n == NULL || fnm == NULL)
3587 goto error_free_dyn;
3588 amt = strlen (fnm) + 1;
a50b1753 3589 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3590 if (anm == NULL)
3591 goto error_free_dyn;
3592 memcpy (anm, fnm, amt);
3593 n->name = anm;
3594 n->by = abfd;
3595 n->next = NULL;
3596 for (pn = & runpath;
3597 *pn != NULL;
3598 pn = &(*pn)->next)
3599 ;
3600 *pn = n;
3601 }
3602 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3603 if (!runpath && dyn.d_tag == DT_RPATH)
3604 {
3605 struct bfd_link_needed_list *n, **pn;
3606 char *fnm, *anm;
3607 unsigned int tagv = dyn.d_un.d_val;
3608
3609 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3610 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3611 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3612 if (n == NULL || fnm == NULL)
3613 goto error_free_dyn;
3614 amt = strlen (fnm) + 1;
a50b1753 3615 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3616 if (anm == NULL)
f8703194 3617 goto error_free_dyn;
4ad4eba5
AM
3618 memcpy (anm, fnm, amt);
3619 n->name = anm;
3620 n->by = abfd;
3621 n->next = NULL;
3622 for (pn = & rpath;
3623 *pn != NULL;
3624 pn = &(*pn)->next)
3625 ;
3626 *pn = n;
3627 }
7ee314fa
AM
3628 if (dyn.d_tag == DT_AUDIT)
3629 {
3630 unsigned int tagv = dyn.d_un.d_val;
3631 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3632 }
4ad4eba5
AM
3633 }
3634
3635 free (dynbuf);
3636 }
3637
3638 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3639 frees all more recently bfd_alloc'd blocks as well. */
3640 if (runpath)
3641 rpath = runpath;
3642
3643 if (rpath)
3644 {
3645 struct bfd_link_needed_list **pn;
66eb6687 3646 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3647 ;
3648 *pn = rpath;
3649 }
3650
3651 /* We do not want to include any of the sections in a dynamic
3652 object in the output file. We hack by simply clobbering the
3653 list of sections in the BFD. This could be handled more
3654 cleanly by, say, a new section flag; the existing
3655 SEC_NEVER_LOAD flag is not the one we want, because that one
3656 still implies that the section takes up space in the output
3657 file. */
3658 bfd_section_list_clear (abfd);
3659
4ad4eba5
AM
3660 /* Find the name to use in a DT_NEEDED entry that refers to this
3661 object. If the object has a DT_SONAME entry, we use it.
3662 Otherwise, if the generic linker stuck something in
3663 elf_dt_name, we use that. Otherwise, we just use the file
3664 name. */
3665 if (soname == NULL || *soname == '\0')
3666 {
3667 soname = elf_dt_name (abfd);
3668 if (soname == NULL || *soname == '\0')
3669 soname = bfd_get_filename (abfd);
3670 }
3671
3672 /* Save the SONAME because sometimes the linker emulation code
3673 will need to know it. */
3674 elf_dt_name (abfd) = soname;
3675
7e9f0867 3676 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3677 if (ret < 0)
3678 goto error_return;
3679
3680 /* If we have already included this dynamic object in the
3681 link, just ignore it. There is no reason to include a
3682 particular dynamic object more than once. */
3683 if (ret > 0)
3684 return TRUE;
7ee314fa
AM
3685
3686 /* Save the DT_AUDIT entry for the linker emulation code. */
3687 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3688 }
3689
3690 /* If this is a dynamic object, we always link against the .dynsym
3691 symbol table, not the .symtab symbol table. The dynamic linker
3692 will only see the .dynsym symbol table, so there is no reason to
3693 look at .symtab for a dynamic object. */
3694
3695 if (! dynamic || elf_dynsymtab (abfd) == 0)
3696 hdr = &elf_tdata (abfd)->symtab_hdr;
3697 else
3698 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3699
3700 symcount = hdr->sh_size / bed->s->sizeof_sym;
3701
3702 /* The sh_info field of the symtab header tells us where the
3703 external symbols start. We don't care about the local symbols at
3704 this point. */
3705 if (elf_bad_symtab (abfd))
3706 {
3707 extsymcount = symcount;
3708 extsymoff = 0;
3709 }
3710 else
3711 {
3712 extsymcount = symcount - hdr->sh_info;
3713 extsymoff = hdr->sh_info;
3714 }
3715
3716 sym_hash = NULL;
3717 if (extsymcount != 0)
3718 {
3719 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3720 NULL, NULL, NULL);
3721 if (isymbuf == NULL)
3722 goto error_return;
3723
3724 /* We store a pointer to the hash table entry for each external
3725 symbol. */
3726 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 3727 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
4ad4eba5
AM
3728 if (sym_hash == NULL)
3729 goto error_free_sym;
3730 elf_sym_hashes (abfd) = sym_hash;
3731 }
3732
3733 if (dynamic)
3734 {
3735 /* Read in any version definitions. */
fc0e6df6
PB
3736 if (!_bfd_elf_slurp_version_tables (abfd,
3737 info->default_imported_symver))
4ad4eba5
AM
3738 goto error_free_sym;
3739
3740 /* Read in the symbol versions, but don't bother to convert them
3741 to internal format. */
3742 if (elf_dynversym (abfd) != 0)
3743 {
3744 Elf_Internal_Shdr *versymhdr;
3745
3746 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 3747 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
3748 if (extversym == NULL)
3749 goto error_free_sym;
3750 amt = versymhdr->sh_size;
3751 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3752 || bfd_bread (extversym, amt, abfd) != amt)
3753 goto error_free_vers;
3754 }
3755 }
3756
66eb6687
AM
3757 /* If we are loading an as-needed shared lib, save the symbol table
3758 state before we start adding symbols. If the lib turns out
3759 to be unneeded, restore the state. */
3760 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3761 {
3762 unsigned int i;
3763 size_t entsize;
3764
3765 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3766 {
3767 struct bfd_hash_entry *p;
2de92251 3768 struct elf_link_hash_entry *h;
66eb6687
AM
3769
3770 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
3771 {
3772 h = (struct elf_link_hash_entry *) p;
3773 entsize += htab->root.table.entsize;
3774 if (h->root.type == bfd_link_hash_warning)
3775 entsize += htab->root.table.entsize;
3776 }
66eb6687
AM
3777 }
3778
3779 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3780 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3781 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3782 if (old_tab == NULL)
3783 goto error_free_vers;
3784
3785 /* Remember the current objalloc pointer, so that all mem for
3786 symbols added can later be reclaimed. */
3787 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3788 if (alloc_mark == NULL)
3789 goto error_free_vers;
3790
5061a885
AM
3791 /* Make a special call to the linker "notice" function to
3792 tell it that we are about to handle an as-needed lib. */
3793 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
16d96b5b 3794 notice_as_needed, 0, NULL))
9af2a943 3795 goto error_free_vers;
5061a885 3796
66eb6687
AM
3797 /* Clone the symbol table and sym hashes. Remember some
3798 pointers into the symbol table, and dynamic symbol count. */
3799 old_hash = (char *) old_tab + tabsize;
3800 old_ent = (char *) old_hash + hashsize;
3801 memcpy (old_tab, htab->root.table.table, tabsize);
3802 memcpy (old_hash, sym_hash, hashsize);
3803 old_undefs = htab->root.undefs;
3804 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
3805 old_table = htab->root.table.table;
3806 old_size = htab->root.table.size;
3807 old_count = htab->root.table.count;
66eb6687
AM
3808 old_dynsymcount = htab->dynsymcount;
3809
3810 for (i = 0; i < htab->root.table.size; i++)
3811 {
3812 struct bfd_hash_entry *p;
2de92251 3813 struct elf_link_hash_entry *h;
66eb6687
AM
3814
3815 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3816 {
3817 memcpy (old_ent, p, htab->root.table.entsize);
3818 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
3819 h = (struct elf_link_hash_entry *) p;
3820 if (h->root.type == bfd_link_hash_warning)
3821 {
3822 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3823 old_ent = (char *) old_ent + htab->root.table.entsize;
3824 }
66eb6687
AM
3825 }
3826 }
3827 }
4ad4eba5 3828
66eb6687 3829 weaks = NULL;
4ad4eba5
AM
3830 ever = extversym != NULL ? extversym + extsymoff : NULL;
3831 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3832 isym < isymend;
3833 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3834 {
3835 int bind;
3836 bfd_vma value;
af44c138 3837 asection *sec, *new_sec;
4ad4eba5
AM
3838 flagword flags;
3839 const char *name;
3840 struct elf_link_hash_entry *h;
3841 bfd_boolean definition;
3842 bfd_boolean size_change_ok;
3843 bfd_boolean type_change_ok;
3844 bfd_boolean new_weakdef;
3845 bfd_boolean override;
a4d8e49b 3846 bfd_boolean common;
4ad4eba5
AM
3847 unsigned int old_alignment;
3848 bfd *old_bfd;
3cbc5de0 3849 bfd * undef_bfd = NULL;
4ad4eba5
AM
3850
3851 override = FALSE;
3852
3853 flags = BSF_NO_FLAGS;
3854 sec = NULL;
3855 value = isym->st_value;
3856 *sym_hash = NULL;
a4d8e49b 3857 common = bed->common_definition (isym);
4ad4eba5
AM
3858
3859 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 3860 switch (bind)
4ad4eba5 3861 {
3e7a7d11 3862 case STB_LOCAL:
4ad4eba5
AM
3863 /* This should be impossible, since ELF requires that all
3864 global symbols follow all local symbols, and that sh_info
3865 point to the first global symbol. Unfortunately, Irix 5
3866 screws this up. */
3867 continue;
3e7a7d11
NC
3868
3869 case STB_GLOBAL:
a4d8e49b 3870 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 3871 flags = BSF_GLOBAL;
3e7a7d11
NC
3872 break;
3873
3874 case STB_WEAK:
3875 flags = BSF_WEAK;
3876 break;
3877
3878 case STB_GNU_UNIQUE:
3879 flags = BSF_GNU_UNIQUE;
3880 break;
3881
3882 default:
4ad4eba5 3883 /* Leave it up to the processor backend. */
3e7a7d11 3884 break;
4ad4eba5
AM
3885 }
3886
3887 if (isym->st_shndx == SHN_UNDEF)
3888 sec = bfd_und_section_ptr;
cb33740c
AM
3889 else if (isym->st_shndx == SHN_ABS)
3890 sec = bfd_abs_section_ptr;
3891 else if (isym->st_shndx == SHN_COMMON)
3892 {
3893 sec = bfd_com_section_ptr;
3894 /* What ELF calls the size we call the value. What ELF
3895 calls the value we call the alignment. */
3896 value = isym->st_size;
3897 }
3898 else
4ad4eba5
AM
3899 {
3900 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3901 if (sec == NULL)
3902 sec = bfd_abs_section_ptr;
dbaa2011 3903 else if (discarded_section (sec))
529fcb95 3904 {
e5d08002
L
3905 /* Symbols from discarded section are undefined. We keep
3906 its visibility. */
529fcb95
PB
3907 sec = bfd_und_section_ptr;
3908 isym->st_shndx = SHN_UNDEF;
3909 }
4ad4eba5
AM
3910 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3911 value -= sec->vma;
3912 }
4ad4eba5
AM
3913
3914 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3915 isym->st_name);
3916 if (name == NULL)
3917 goto error_free_vers;
3918
3919 if (isym->st_shndx == SHN_COMMON
02d00247
AM
3920 && (abfd->flags & BFD_PLUGIN) != 0)
3921 {
3922 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
3923
3924 if (xc == NULL)
3925 {
3926 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
3927 | SEC_EXCLUDE);
3928 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
3929 if (xc == NULL)
3930 goto error_free_vers;
3931 }
3932 sec = xc;
3933 }
3934 else if (isym->st_shndx == SHN_COMMON
3935 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3936 && !info->relocatable)
4ad4eba5
AM
3937 {
3938 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3939
3940 if (tcomm == NULL)
3941 {
02d00247
AM
3942 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
3943 | SEC_LINKER_CREATED);
3944 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 3945 if (tcomm == NULL)
4ad4eba5
AM
3946 goto error_free_vers;
3947 }
3948 sec = tcomm;
3949 }
66eb6687 3950 else if (bed->elf_add_symbol_hook)
4ad4eba5 3951 {
66eb6687
AM
3952 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3953 &sec, &value))
4ad4eba5
AM
3954 goto error_free_vers;
3955
3956 /* The hook function sets the name to NULL if this symbol
3957 should be skipped for some reason. */
3958 if (name == NULL)
3959 continue;
3960 }
3961
3962 /* Sanity check that all possibilities were handled. */
3963 if (sec == NULL)
3964 {
3965 bfd_set_error (bfd_error_bad_value);
3966 goto error_free_vers;
3967 }
3968
3969 if (bfd_is_und_section (sec)
3970 || bfd_is_com_section (sec))
3971 definition = FALSE;
3972 else
3973 definition = TRUE;
3974
3975 size_change_ok = FALSE;
66eb6687 3976 type_change_ok = bed->type_change_ok;
4ad4eba5
AM
3977 old_alignment = 0;
3978 old_bfd = NULL;
af44c138 3979 new_sec = sec;
4ad4eba5 3980
66eb6687 3981 if (is_elf_hash_table (htab))
4ad4eba5
AM
3982 {
3983 Elf_Internal_Versym iver;
3984 unsigned int vernum = 0;
3985 bfd_boolean skip;
3986
b918acf9
NC
3987 /* If this is a definition of a symbol which was previously
3988 referenced in a non-weak manner then make a note of the bfd
3989 that contained the reference. This is used if we need to
3990 refer to the source of the reference later on. */
3991 if (! bfd_is_und_section (sec))
3992 {
3993 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
3994
3995 if (h != NULL
3996 && h->root.type == bfd_link_hash_undefined
3997 && h->root.u.undef.abfd)
3998 undef_bfd = h->root.u.undef.abfd;
3999 }
4000
fc0e6df6 4001 if (ever == NULL)
4ad4eba5 4002 {
fc0e6df6
PB
4003 if (info->default_imported_symver)
4004 /* Use the default symbol version created earlier. */
4005 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4006 else
4007 iver.vs_vers = 0;
4008 }
4009 else
4010 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4011
4012 vernum = iver.vs_vers & VERSYM_VERSION;
4013
4014 /* If this is a hidden symbol, or if it is not version
4015 1, we append the version name to the symbol name.
cc86ff91
EB
4016 However, we do not modify a non-hidden absolute symbol
4017 if it is not a function, because it might be the version
4018 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4019 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4020 || (vernum > 1
4021 && (!bfd_is_abs_section (sec)
4022 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4023 {
4024 const char *verstr;
4025 size_t namelen, verlen, newlen;
4026 char *newname, *p;
4027
4028 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4029 {
fc0e6df6
PB
4030 if (vernum > elf_tdata (abfd)->cverdefs)
4031 verstr = NULL;
4032 else if (vernum > 1)
4033 verstr =
4034 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4035 else
4036 verstr = "";
4ad4eba5 4037
fc0e6df6 4038 if (verstr == NULL)
4ad4eba5 4039 {
fc0e6df6
PB
4040 (*_bfd_error_handler)
4041 (_("%B: %s: invalid version %u (max %d)"),
4042 abfd, name, vernum,
4043 elf_tdata (abfd)->cverdefs);
4044 bfd_set_error (bfd_error_bad_value);
4045 goto error_free_vers;
4ad4eba5 4046 }
fc0e6df6
PB
4047 }
4048 else
4049 {
4050 /* We cannot simply test for the number of
4051 entries in the VERNEED section since the
4052 numbers for the needed versions do not start
4053 at 0. */
4054 Elf_Internal_Verneed *t;
4055
4056 verstr = NULL;
4057 for (t = elf_tdata (abfd)->verref;
4058 t != NULL;
4059 t = t->vn_nextref)
4ad4eba5 4060 {
fc0e6df6 4061 Elf_Internal_Vernaux *a;
4ad4eba5 4062
fc0e6df6
PB
4063 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4064 {
4065 if (a->vna_other == vernum)
4ad4eba5 4066 {
fc0e6df6
PB
4067 verstr = a->vna_nodename;
4068 break;
4ad4eba5 4069 }
4ad4eba5 4070 }
fc0e6df6
PB
4071 if (a != NULL)
4072 break;
4073 }
4074 if (verstr == NULL)
4075 {
4076 (*_bfd_error_handler)
4077 (_("%B: %s: invalid needed version %d"),
4078 abfd, name, vernum);
4079 bfd_set_error (bfd_error_bad_value);
4080 goto error_free_vers;
4ad4eba5 4081 }
4ad4eba5 4082 }
fc0e6df6
PB
4083
4084 namelen = strlen (name);
4085 verlen = strlen (verstr);
4086 newlen = namelen + verlen + 2;
4087 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4088 && isym->st_shndx != SHN_UNDEF)
4089 ++newlen;
4090
a50b1753 4091 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4092 if (newname == NULL)
4093 goto error_free_vers;
4094 memcpy (newname, name, namelen);
4095 p = newname + namelen;
4096 *p++ = ELF_VER_CHR;
4097 /* If this is a defined non-hidden version symbol,
4098 we add another @ to the name. This indicates the
4099 default version of the symbol. */
4100 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4101 && isym->st_shndx != SHN_UNDEF)
4102 *p++ = ELF_VER_CHR;
4103 memcpy (p, verstr, verlen + 1);
4104
4105 name = newname;
4ad4eba5
AM
4106 }
4107
b918acf9
NC
4108 /* If necessary, make a second attempt to locate the bfd
4109 containing an unresolved, non-weak reference to the
4110 current symbol. */
4111 if (! bfd_is_und_section (sec) && undef_bfd == NULL)
3cbc5de0
NC
4112 {
4113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4114
4115 if (h != NULL
b918acf9 4116 && h->root.type == bfd_link_hash_undefined
3cbc5de0
NC
4117 && h->root.u.undef.abfd)
4118 undef_bfd = h->root.u.undef.abfd;
4119 }
4120
af44c138
L
4121 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4122 &value, &old_alignment,
4ad4eba5
AM
4123 sym_hash, &skip, &override,
4124 &type_change_ok, &size_change_ok))
4125 goto error_free_vers;
4126
4127 if (skip)
4128 continue;
4129
4130 if (override)
4131 definition = FALSE;
4132
4133 h = *sym_hash;
4134 while (h->root.type == bfd_link_hash_indirect
4135 || h->root.type == bfd_link_hash_warning)
4136 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4137
4138 /* Remember the old alignment if this is a common symbol, so
4139 that we don't reduce the alignment later on. We can't
4140 check later, because _bfd_generic_link_add_one_symbol
4141 will set a default for the alignment which we want to
4142 override. We also remember the old bfd where the existing
4143 definition comes from. */
4144 switch (h->root.type)
4145 {
4146 default:
4147 break;
4148
4149 case bfd_link_hash_defined:
4150 case bfd_link_hash_defweak:
4151 old_bfd = h->root.u.def.section->owner;
4152 break;
4153
4154 case bfd_link_hash_common:
4155 old_bfd = h->root.u.c.p->section->owner;
4156 old_alignment = h->root.u.c.p->alignment_power;
4157 break;
4158 }
4159
4160 if (elf_tdata (abfd)->verdef != NULL
4161 && ! override
4162 && vernum > 1
4163 && definition)
4164 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4165 }
4166
4167 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4168 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4169 (struct bfd_link_hash_entry **) sym_hash)))
4170 goto error_free_vers;
4171
4172 h = *sym_hash;
4173 while (h->root.type == bfd_link_hash_indirect
4174 || h->root.type == bfd_link_hash_warning)
4175 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4176
4ad4eba5 4177 *sym_hash = h;
d64284fe
L
4178 if (is_elf_hash_table (htab))
4179 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4ad4eba5
AM
4180
4181 new_weakdef = FALSE;
4182 if (dynamic
4183 && definition
4184 && (flags & BSF_WEAK) != 0
fcb93ecf 4185 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4186 && is_elf_hash_table (htab)
f6e332e6 4187 && h->u.weakdef == NULL)
4ad4eba5
AM
4188 {
4189 /* Keep a list of all weak defined non function symbols from
4190 a dynamic object, using the weakdef field. Later in this
4191 function we will set the weakdef field to the correct
4192 value. We only put non-function symbols from dynamic
4193 objects on this list, because that happens to be the only
4194 time we need to know the normal symbol corresponding to a
4195 weak symbol, and the information is time consuming to
4196 figure out. If the weakdef field is not already NULL,
4197 then this symbol was already defined by some previous
4198 dynamic object, and we will be using that previous
4199 definition anyhow. */
4200
f6e332e6 4201 h->u.weakdef = weaks;
4ad4eba5
AM
4202 weaks = h;
4203 new_weakdef = TRUE;
4204 }
4205
4206 /* Set the alignment of a common symbol. */
a4d8e49b 4207 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4208 && h->root.type == bfd_link_hash_common)
4209 {
4210 unsigned int align;
4211
a4d8e49b 4212 if (common)
af44c138
L
4213 align = bfd_log2 (isym->st_value);
4214 else
4215 {
4216 /* The new symbol is a common symbol in a shared object.
4217 We need to get the alignment from the section. */
4218 align = new_sec->alignment_power;
4219 }
595213d4 4220 if (align > old_alignment)
4ad4eba5
AM
4221 h->root.u.c.p->alignment_power = align;
4222 else
4223 h->root.u.c.p->alignment_power = old_alignment;
4224 }
4225
66eb6687 4226 if (is_elf_hash_table (htab))
4ad4eba5 4227 {
4ad4eba5 4228 bfd_boolean dynsym;
4ad4eba5
AM
4229
4230 /* Check the alignment when a common symbol is involved. This
4231 can change when a common symbol is overridden by a normal
4232 definition or a common symbol is ignored due to the old
4233 normal definition. We need to make sure the maximum
4234 alignment is maintained. */
a4d8e49b 4235 if ((old_alignment || common)
4ad4eba5
AM
4236 && h->root.type != bfd_link_hash_common)
4237 {
4238 unsigned int common_align;
4239 unsigned int normal_align;
4240 unsigned int symbol_align;
4241 bfd *normal_bfd;
4242 bfd *common_bfd;
4243
4244 symbol_align = ffs (h->root.u.def.value) - 1;
4245 if (h->root.u.def.section->owner != NULL
4246 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4247 {
4248 normal_align = h->root.u.def.section->alignment_power;
4249 if (normal_align > symbol_align)
4250 normal_align = symbol_align;
4251 }
4252 else
4253 normal_align = symbol_align;
4254
4255 if (old_alignment)
4256 {
4257 common_align = old_alignment;
4258 common_bfd = old_bfd;
4259 normal_bfd = abfd;
4260 }
4261 else
4262 {
4263 common_align = bfd_log2 (isym->st_value);
4264 common_bfd = abfd;
4265 normal_bfd = old_bfd;
4266 }
4267
4268 if (normal_align < common_align)
d07676f8
NC
4269 {
4270 /* PR binutils/2735 */
4271 if (normal_bfd == NULL)
4272 (*_bfd_error_handler)
4273 (_("Warning: alignment %u of common symbol `%s' in %B"
4274 " is greater than the alignment (%u) of its section %A"),
4275 common_bfd, h->root.u.def.section,
4276 1 << common_align, name, 1 << normal_align);
4277 else
4278 (*_bfd_error_handler)
4279 (_("Warning: alignment %u of symbol `%s' in %B"
4280 " is smaller than %u in %B"),
4281 normal_bfd, common_bfd,
4282 1 << normal_align, name, 1 << common_align);
4283 }
4ad4eba5
AM
4284 }
4285
83ad0046
L
4286 /* Remember the symbol size if it isn't undefined. */
4287 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4ad4eba5
AM
4288 && (definition || h->size == 0))
4289 {
83ad0046
L
4290 if (h->size != 0
4291 && h->size != isym->st_size
4292 && ! size_change_ok)
4ad4eba5 4293 (*_bfd_error_handler)
d003868e
AM
4294 (_("Warning: size of symbol `%s' changed"
4295 " from %lu in %B to %lu in %B"),
4296 old_bfd, abfd,
4ad4eba5 4297 name, (unsigned long) h->size,
d003868e 4298 (unsigned long) isym->st_size);
4ad4eba5
AM
4299
4300 h->size = isym->st_size;
4301 }
4302
4303 /* If this is a common symbol, then we always want H->SIZE
4304 to be the size of the common symbol. The code just above
4305 won't fix the size if a common symbol becomes larger. We
4306 don't warn about a size change here, because that is
fcb93ecf
PB
4307 covered by --warn-common. Allow changed between different
4308 function types. */
4ad4eba5
AM
4309 if (h->root.type == bfd_link_hash_common)
4310 h->size = h->root.u.c.size;
4311
4312 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4313 && (definition || h->type == STT_NOTYPE))
4314 {
2955ec4c
L
4315 unsigned int type = ELF_ST_TYPE (isym->st_info);
4316
4317 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4318 symbol. */
4319 if (type == STT_GNU_IFUNC
4320 && (abfd->flags & DYNAMIC) != 0)
4321 type = STT_FUNC;
4ad4eba5 4322
2955ec4c
L
4323 if (h->type != type)
4324 {
4325 if (h->type != STT_NOTYPE && ! type_change_ok)
4326 (*_bfd_error_handler)
4327 (_("Warning: type of symbol `%s' changed"
4328 " from %d to %d in %B"),
4329 abfd, name, h->type, type);
4330
4331 h->type = type;
4332 }
4ad4eba5
AM
4333 }
4334
54ac0771
L
4335 /* Merge st_other field. */
4336 elf_merge_st_other (abfd, h, isym, definition, dynamic);
4ad4eba5
AM
4337
4338 /* Set a flag in the hash table entry indicating the type of
4339 reference or definition we just found. Keep a count of
4340 the number of dynamic symbols we find. A dynamic symbol
4341 is one which is referenced or defined by both a regular
4342 object and a shared object. */
4ad4eba5
AM
4343 dynsym = FALSE;
4344 if (! dynamic)
4345 {
4346 if (! definition)
4347 {
f5385ebf 4348 h->ref_regular = 1;
4ad4eba5 4349 if (bind != STB_WEAK)
f5385ebf 4350 h->ref_regular_nonweak = 1;
4ad4eba5
AM
4351 }
4352 else
d8880531
L
4353 {
4354 h->def_regular = 1;
4355 if (h->def_dynamic)
4356 {
4357 h->def_dynamic = 0;
4358 h->ref_dynamic = 1;
d8880531
L
4359 }
4360 }
4ad4eba5 4361 if (! info->executable
f5385ebf
AM
4362 || h->def_dynamic
4363 || h->ref_dynamic)
4ad4eba5
AM
4364 dynsym = TRUE;
4365 }
4366 else
4367 {
4368 if (! definition)
f5385ebf 4369 h->ref_dynamic = 1;
4ad4eba5 4370 else
54e8959c
L
4371 {
4372 h->def_dynamic = 1;
4373 h->dynamic_def = 1;
4374 }
f5385ebf
AM
4375 if (h->def_regular
4376 || h->ref_regular
f6e332e6 4377 || (h->u.weakdef != NULL
4ad4eba5 4378 && ! new_weakdef
f6e332e6 4379 && h->u.weakdef->dynindx != -1))
4ad4eba5
AM
4380 dynsym = TRUE;
4381 }
4382
c3df8c14 4383 /* We don't want to make debug symbol dynamic. */
b2064611 4384 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
c3df8c14
AM
4385 dynsym = FALSE;
4386
4387 /* Nor should we make plugin symbols dynamic. */
4388 if ((abfd->flags & BFD_PLUGIN) != 0)
4389 dynsym = FALSE;
92b7c7b6 4390
35fc36a8
RS
4391 if (definition)
4392 h->target_internal = isym->st_target_internal;
4393
4ad4eba5
AM
4394 /* Check to see if we need to add an indirect symbol for
4395 the default name. */
4396 if (definition || h->root.type == bfd_link_hash_common)
4397 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4398 &sec, &value, &dynsym,
4399 override))
4400 goto error_free_vers;
4401
4402 if (definition && !dynamic)
4403 {
4404 char *p = strchr (name, ELF_VER_CHR);
4405 if (p != NULL && p[1] != ELF_VER_CHR)
4406 {
4407 /* Queue non-default versions so that .symver x, x@FOO
4408 aliases can be checked. */
66eb6687 4409 if (!nondeflt_vers)
4ad4eba5 4410 {
66eb6687
AM
4411 amt = ((isymend - isym + 1)
4412 * sizeof (struct elf_link_hash_entry *));
a50b1753
NC
4413 nondeflt_vers =
4414 (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4415 if (!nondeflt_vers)
4416 goto error_free_vers;
4ad4eba5 4417 }
66eb6687 4418 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4419 }
4420 }
4421
4422 if (dynsym && h->dynindx == -1)
4423 {
c152c796 4424 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4425 goto error_free_vers;
f6e332e6 4426 if (h->u.weakdef != NULL
4ad4eba5 4427 && ! new_weakdef
f6e332e6 4428 && h->u.weakdef->dynindx == -1)
4ad4eba5 4429 {
66eb6687 4430 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4431 goto error_free_vers;
4432 }
4433 }
4434 else if (dynsym && h->dynindx != -1)
4435 /* If the symbol already has a dynamic index, but
4436 visibility says it should not be visible, turn it into
4437 a local symbol. */
4438 switch (ELF_ST_VISIBILITY (h->other))
4439 {
4440 case STV_INTERNAL:
4441 case STV_HIDDEN:
4442 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4443 dynsym = FALSE;
4444 break;
4445 }
4446
4447 if (!add_needed
4448 && definition
010e5ae2
AM
4449 && ((dynsym
4450 && h->ref_regular)
4451 || (h->ref_dynamic
4452 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4453 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4ad4eba5
AM
4454 {
4455 int ret;
4456 const char *soname = elf_dt_name (abfd);
4457
4458 /* A symbol from a library loaded via DT_NEEDED of some
4459 other library is referenced by a regular object.
e56f61be 4460 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4461 --no-add-needed is used and the reference was not
4462 a weak one. */
4463 if (undef_bfd != NULL
4464 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4465 {
4466 (*_bfd_error_handler)
3cbc5de0 4467 (_("%B: undefined reference to symbol '%s'"),
b918acf9 4468 undef_bfd, name);
3cbc5de0
NC
4469 (*_bfd_error_handler)
4470 (_("note: '%s' is defined in DSO %B so try adding it to the linker command line"),
d003868e 4471 abfd, name);
3cbc5de0 4472 bfd_set_error (bfd_error_invalid_operation);
e56f61be
L
4473 goto error_free_vers;
4474 }
4475
a50b1753
NC
4476 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4477 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4478
4ad4eba5 4479 add_needed = TRUE;
7e9f0867 4480 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4481 if (ret < 0)
4482 goto error_free_vers;
4483
4484 BFD_ASSERT (ret == 0);
4485 }
4486 }
4487 }
4488
66eb6687
AM
4489 if (extversym != NULL)
4490 {
4491 free (extversym);
4492 extversym = NULL;
4493 }
4494
4495 if (isymbuf != NULL)
4496 {
4497 free (isymbuf);
4498 isymbuf = NULL;
4499 }
4500
4501 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4502 {
4503 unsigned int i;
4504
4505 /* Restore the symbol table. */
97fed1c9
JJ
4506 if (bed->as_needed_cleanup)
4507 (*bed->as_needed_cleanup) (abfd, info);
66eb6687
AM
4508 old_hash = (char *) old_tab + tabsize;
4509 old_ent = (char *) old_hash + hashsize;
4510 sym_hash = elf_sym_hashes (abfd);
4f87808c
AM
4511 htab->root.table.table = old_table;
4512 htab->root.table.size = old_size;
4513 htab->root.table.count = old_count;
66eb6687
AM
4514 memcpy (htab->root.table.table, old_tab, tabsize);
4515 memcpy (sym_hash, old_hash, hashsize);
4516 htab->root.undefs = old_undefs;
4517 htab->root.undefs_tail = old_undefs_tail;
4518 for (i = 0; i < htab->root.table.size; i++)
4519 {
4520 struct bfd_hash_entry *p;
4521 struct elf_link_hash_entry *h;
3e0882af
L
4522 bfd_size_type size;
4523 unsigned int alignment_power;
66eb6687
AM
4524
4525 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4526 {
4527 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4528 if (h->root.type == bfd_link_hash_warning)
4529 h = (struct elf_link_hash_entry *) h->root.u.i.link;
66eb6687
AM
4530 if (h->dynindx >= old_dynsymcount)
4531 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
2de92251 4532
3e0882af
L
4533 /* Preserve the maximum alignment and size for common
4534 symbols even if this dynamic lib isn't on DT_NEEDED
4535 since it can still be loaded at the run-time by another
4536 dynamic lib. */
4537 if (h->root.type == bfd_link_hash_common)
4538 {
4539 size = h->root.u.c.size;
4540 alignment_power = h->root.u.c.p->alignment_power;
4541 }
4542 else
4543 {
4544 size = 0;
4545 alignment_power = 0;
4546 }
66eb6687
AM
4547 memcpy (p, old_ent, htab->root.table.entsize);
4548 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4549 h = (struct elf_link_hash_entry *) p;
4550 if (h->root.type == bfd_link_hash_warning)
4551 {
4552 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4553 old_ent = (char *) old_ent + htab->root.table.entsize;
4554 }
3e0882af
L
4555 else if (h->root.type == bfd_link_hash_common)
4556 {
4557 if (size > h->root.u.c.size)
4558 h->root.u.c.size = size;
4559 if (alignment_power > h->root.u.c.p->alignment_power)
4560 h->root.u.c.p->alignment_power = alignment_power;
4561 }
66eb6687
AM
4562 }
4563 }
4564
5061a885
AM
4565 /* Make a special call to the linker "notice" function to
4566 tell it that symbols added for crefs may need to be removed. */
4567 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
16d96b5b 4568 notice_not_needed, 0, NULL))
9af2a943 4569 goto error_free_vers;
5061a885 4570
66eb6687
AM
4571 free (old_tab);
4572 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4573 alloc_mark);
4574 if (nondeflt_vers != NULL)
4575 free (nondeflt_vers);
4576 return TRUE;
4577 }
2de92251 4578
66eb6687
AM
4579 if (old_tab != NULL)
4580 {
5061a885 4581 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
16d96b5b 4582 notice_needed, 0, NULL))
9af2a943 4583 goto error_free_vers;
66eb6687
AM
4584 free (old_tab);
4585 old_tab = NULL;
4586 }
4587
4ad4eba5
AM
4588 /* Now that all the symbols from this input file are created, handle
4589 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4590 if (nondeflt_vers != NULL)
4591 {
4592 bfd_size_type cnt, symidx;
4593
4594 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4595 {
4596 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4597 char *shortname, *p;
4598
4599 p = strchr (h->root.root.string, ELF_VER_CHR);
4600 if (p == NULL
4601 || (h->root.type != bfd_link_hash_defined
4602 && h->root.type != bfd_link_hash_defweak))
4603 continue;
4604
4605 amt = p - h->root.root.string;
a50b1753 4606 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4607 if (!shortname)
4608 goto error_free_vers;
4ad4eba5
AM
4609 memcpy (shortname, h->root.root.string, amt);
4610 shortname[amt] = '\0';
4611
4612 hi = (struct elf_link_hash_entry *)
66eb6687 4613 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4614 FALSE, FALSE, FALSE);
4615 if (hi != NULL
4616 && hi->root.type == h->root.type
4617 && hi->root.u.def.value == h->root.u.def.value
4618 && hi->root.u.def.section == h->root.u.def.section)
4619 {
4620 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4621 hi->root.type = bfd_link_hash_indirect;
4622 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4623 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4624 sym_hash = elf_sym_hashes (abfd);
4625 if (sym_hash)
4626 for (symidx = 0; symidx < extsymcount; ++symidx)
4627 if (sym_hash[symidx] == hi)
4628 {
4629 sym_hash[symidx] = h;
4630 break;
4631 }
4632 }
4633 free (shortname);
4634 }
4635 free (nondeflt_vers);
4636 nondeflt_vers = NULL;
4637 }
4638
4ad4eba5
AM
4639 /* Now set the weakdefs field correctly for all the weak defined
4640 symbols we found. The only way to do this is to search all the
4641 symbols. Since we only need the information for non functions in
4642 dynamic objects, that's the only time we actually put anything on
4643 the list WEAKS. We need this information so that if a regular
4644 object refers to a symbol defined weakly in a dynamic object, the
4645 real symbol in the dynamic object is also put in the dynamic
4646 symbols; we also must arrange for both symbols to point to the
4647 same memory location. We could handle the general case of symbol
4648 aliasing, but a general symbol alias can only be generated in
4649 assembler code, handling it correctly would be very time
4650 consuming, and other ELF linkers don't handle general aliasing
4651 either. */
4652 if (weaks != NULL)
4653 {
4654 struct elf_link_hash_entry **hpp;
4655 struct elf_link_hash_entry **hppend;
4656 struct elf_link_hash_entry **sorted_sym_hash;
4657 struct elf_link_hash_entry *h;
4658 size_t sym_count;
4659
4660 /* Since we have to search the whole symbol list for each weak
4661 defined symbol, search time for N weak defined symbols will be
4662 O(N^2). Binary search will cut it down to O(NlogN). */
4663 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 4664 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4665 if (sorted_sym_hash == NULL)
4666 goto error_return;
4667 sym_hash = sorted_sym_hash;
4668 hpp = elf_sym_hashes (abfd);
4669 hppend = hpp + extsymcount;
4670 sym_count = 0;
4671 for (; hpp < hppend; hpp++)
4672 {
4673 h = *hpp;
4674 if (h != NULL
4675 && h->root.type == bfd_link_hash_defined
fcb93ecf 4676 && !bed->is_function_type (h->type))
4ad4eba5
AM
4677 {
4678 *sym_hash = h;
4679 sym_hash++;
4680 sym_count++;
4681 }
4682 }
4683
4684 qsort (sorted_sym_hash, sym_count,
4685 sizeof (struct elf_link_hash_entry *),
4686 elf_sort_symbol);
4687
4688 while (weaks != NULL)
4689 {
4690 struct elf_link_hash_entry *hlook;
4691 asection *slook;
4692 bfd_vma vlook;
4693 long ilook;
4694 size_t i, j, idx;
4695
4696 hlook = weaks;
f6e332e6
AM
4697 weaks = hlook->u.weakdef;
4698 hlook->u.weakdef = NULL;
4ad4eba5
AM
4699
4700 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4701 || hlook->root.type == bfd_link_hash_defweak
4702 || hlook->root.type == bfd_link_hash_common
4703 || hlook->root.type == bfd_link_hash_indirect);
4704 slook = hlook->root.u.def.section;
4705 vlook = hlook->root.u.def.value;
4706
4707 ilook = -1;
4708 i = 0;
4709 j = sym_count;
4710 while (i < j)
4711 {
4712 bfd_signed_vma vdiff;
4713 idx = (i + j) / 2;
4714 h = sorted_sym_hash [idx];
4715 vdiff = vlook - h->root.u.def.value;
4716 if (vdiff < 0)
4717 j = idx;
4718 else if (vdiff > 0)
4719 i = idx + 1;
4720 else
4721 {
a9b881be 4722 long sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4723 if (sdiff < 0)
4724 j = idx;
4725 else if (sdiff > 0)
4726 i = idx + 1;
4727 else
4728 {
4729 ilook = idx;
4730 break;
4731 }
4732 }
4733 }
4734
4735 /* We didn't find a value/section match. */
4736 if (ilook == -1)
4737 continue;
4738
4739 for (i = ilook; i < sym_count; i++)
4740 {
4741 h = sorted_sym_hash [i];
4742
4743 /* Stop if value or section doesn't match. */
4744 if (h->root.u.def.value != vlook
4745 || h->root.u.def.section != slook)
4746 break;
4747 else if (h != hlook)
4748 {
f6e332e6 4749 hlook->u.weakdef = h;
4ad4eba5
AM
4750
4751 /* If the weak definition is in the list of dynamic
4752 symbols, make sure the real definition is put
4753 there as well. */
4754 if (hlook->dynindx != -1 && h->dynindx == -1)
4755 {
c152c796 4756 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
4757 {
4758 err_free_sym_hash:
4759 free (sorted_sym_hash);
4760 goto error_return;
4761 }
4ad4eba5
AM
4762 }
4763
4764 /* If the real definition is in the list of dynamic
4765 symbols, make sure the weak definition is put
4766 there as well. If we don't do this, then the
4767 dynamic loader might not merge the entries for the
4768 real definition and the weak definition. */
4769 if (h->dynindx != -1 && hlook->dynindx == -1)
4770 {
c152c796 4771 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 4772 goto err_free_sym_hash;
4ad4eba5
AM
4773 }
4774 break;
4775 }
4776 }
4777 }
4778
4779 free (sorted_sym_hash);
4780 }
4781
33177bb1
AM
4782 if (bed->check_directives
4783 && !(*bed->check_directives) (abfd, info))
4784 return FALSE;
85fbca6a 4785
4ad4eba5
AM
4786 /* If this object is the same format as the output object, and it is
4787 not a shared library, then let the backend look through the
4788 relocs.
4789
4790 This is required to build global offset table entries and to
4791 arrange for dynamic relocs. It is not required for the
4792 particular common case of linking non PIC code, even when linking
4793 against shared libraries, but unfortunately there is no way of
4794 knowing whether an object file has been compiled PIC or not.
4795 Looking through the relocs is not particularly time consuming.
4796 The problem is that we must either (1) keep the relocs in memory,
4797 which causes the linker to require additional runtime memory or
4798 (2) read the relocs twice from the input file, which wastes time.
4799 This would be a good case for using mmap.
4800
4801 I have no idea how to handle linking PIC code into a file of a
4802 different format. It probably can't be done. */
4ad4eba5 4803 if (! dynamic
66eb6687 4804 && is_elf_hash_table (htab)
13285a1b 4805 && bed->check_relocs != NULL
39334f3a 4806 && elf_object_id (abfd) == elf_hash_table_id (htab)
f13a99db 4807 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4ad4eba5
AM
4808 {
4809 asection *o;
4810
4811 for (o = abfd->sections; o != NULL; o = o->next)
4812 {
4813 Elf_Internal_Rela *internal_relocs;
4814 bfd_boolean ok;
4815
4816 if ((o->flags & SEC_RELOC) == 0
4817 || o->reloc_count == 0
4818 || ((info->strip == strip_all || info->strip == strip_debugger)
4819 && (o->flags & SEC_DEBUGGING) != 0)
4820 || bfd_is_abs_section (o->output_section))
4821 continue;
4822
4823 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4824 info->keep_memory);
4825 if (internal_relocs == NULL)
4826 goto error_return;
4827
66eb6687 4828 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4ad4eba5
AM
4829
4830 if (elf_section_data (o)->relocs != internal_relocs)
4831 free (internal_relocs);
4832
4833 if (! ok)
4834 goto error_return;
4835 }
4836 }
4837
4838 /* If this is a non-traditional link, try to optimize the handling
4839 of the .stab/.stabstr sections. */
4840 if (! dynamic
4841 && ! info->traditional_format
66eb6687 4842 && is_elf_hash_table (htab)
4ad4eba5
AM
4843 && (info->strip != strip_all && info->strip != strip_debugger))
4844 {
4845 asection *stabstr;
4846
4847 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4848 if (stabstr != NULL)
4849 {
4850 bfd_size_type string_offset = 0;
4851 asection *stab;
4852
4853 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 4854 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
4855 && (!stab->name[5] ||
4856 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4857 && (stab->flags & SEC_MERGE) == 0
4858 && !bfd_is_abs_section (stab->output_section))
4859 {
4860 struct bfd_elf_section_data *secdata;
4861
4862 secdata = elf_section_data (stab);
66eb6687
AM
4863 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4864 stabstr, &secdata->sec_info,
4ad4eba5
AM
4865 &string_offset))
4866 goto error_return;
4867 if (secdata->sec_info)
dbaa2011 4868 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
4869 }
4870 }
4871 }
4872
66eb6687 4873 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
4874 {
4875 /* Add this bfd to the loaded list. */
4876 struct elf_link_loaded_list *n;
4877
a50b1753
NC
4878 n = (struct elf_link_loaded_list *)
4879 bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4ad4eba5
AM
4880 if (n == NULL)
4881 goto error_return;
4882 n->abfd = abfd;
66eb6687
AM
4883 n->next = htab->loaded;
4884 htab->loaded = n;
4ad4eba5
AM
4885 }
4886
4887 return TRUE;
4888
4889 error_free_vers:
66eb6687
AM
4890 if (old_tab != NULL)
4891 free (old_tab);
4ad4eba5
AM
4892 if (nondeflt_vers != NULL)
4893 free (nondeflt_vers);
4894 if (extversym != NULL)
4895 free (extversym);
4896 error_free_sym:
4897 if (isymbuf != NULL)
4898 free (isymbuf);
4899 error_return:
4900 return FALSE;
4901}
4902
8387904d
AM
4903/* Return the linker hash table entry of a symbol that might be
4904 satisfied by an archive symbol. Return -1 on error. */
4905
4906struct elf_link_hash_entry *
4907_bfd_elf_archive_symbol_lookup (bfd *abfd,
4908 struct bfd_link_info *info,
4909 const char *name)
4910{
4911 struct elf_link_hash_entry *h;
4912 char *p, *copy;
4913 size_t len, first;
4914
2a41f396 4915 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
4916 if (h != NULL)
4917 return h;
4918
4919 /* If this is a default version (the name contains @@), look up the
4920 symbol again with only one `@' as well as without the version.
4921 The effect is that references to the symbol with and without the
4922 version will be matched by the default symbol in the archive. */
4923
4924 p = strchr (name, ELF_VER_CHR);
4925 if (p == NULL || p[1] != ELF_VER_CHR)
4926 return h;
4927
4928 /* First check with only one `@'. */
4929 len = strlen (name);
a50b1753 4930 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
4931 if (copy == NULL)
4932 return (struct elf_link_hash_entry *) 0 - 1;
4933
4934 first = p - name + 1;
4935 memcpy (copy, name, first);
4936 memcpy (copy + first, name + first + 1, len - first);
4937
2a41f396 4938 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
4939 if (h == NULL)
4940 {
4941 /* We also need to check references to the symbol without the
4942 version. */
4943 copy[first - 1] = '\0';
4944 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 4945 FALSE, FALSE, TRUE);
8387904d
AM
4946 }
4947
4948 bfd_release (abfd, copy);
4949 return h;
4950}
4951
0ad989f9
L
4952/* Add symbols from an ELF archive file to the linker hash table. We
4953 don't use _bfd_generic_link_add_archive_symbols because of a
4954 problem which arises on UnixWare. The UnixWare libc.so is an
4955 archive which includes an entry libc.so.1 which defines a bunch of
4956 symbols. The libc.so archive also includes a number of other
4957 object files, which also define symbols, some of which are the same
4958 as those defined in libc.so.1. Correct linking requires that we
4959 consider each object file in turn, and include it if it defines any
4960 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4961 this; it looks through the list of undefined symbols, and includes
4962 any object file which defines them. When this algorithm is used on
4963 UnixWare, it winds up pulling in libc.so.1 early and defining a
4964 bunch of symbols. This means that some of the other objects in the
4965 archive are not included in the link, which is incorrect since they
4966 precede libc.so.1 in the archive.
4967
4968 Fortunately, ELF archive handling is simpler than that done by
4969 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4970 oddities. In ELF, if we find a symbol in the archive map, and the
4971 symbol is currently undefined, we know that we must pull in that
4972 object file.
4973
4974 Unfortunately, we do have to make multiple passes over the symbol
4975 table until nothing further is resolved. */
4976
4ad4eba5
AM
4977static bfd_boolean
4978elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
4979{
4980 symindex c;
4981 bfd_boolean *defined = NULL;
4982 bfd_boolean *included = NULL;
4983 carsym *symdefs;
4984 bfd_boolean loop;
4985 bfd_size_type amt;
8387904d
AM
4986 const struct elf_backend_data *bed;
4987 struct elf_link_hash_entry * (*archive_symbol_lookup)
4988 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
4989
4990 if (! bfd_has_map (abfd))
4991 {
4992 /* An empty archive is a special case. */
4993 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4994 return TRUE;
4995 bfd_set_error (bfd_error_no_armap);
4996 return FALSE;
4997 }
4998
4999 /* Keep track of all symbols we know to be already defined, and all
5000 files we know to be already included. This is to speed up the
5001 second and subsequent passes. */
5002 c = bfd_ardata (abfd)->symdef_count;
5003 if (c == 0)
5004 return TRUE;
5005 amt = c;
5006 amt *= sizeof (bfd_boolean);
a50b1753
NC
5007 defined = (bfd_boolean *) bfd_zmalloc (amt);
5008 included = (bfd_boolean *) bfd_zmalloc (amt);
0ad989f9
L
5009 if (defined == NULL || included == NULL)
5010 goto error_return;
5011
5012 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5013 bed = get_elf_backend_data (abfd);
5014 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5015
5016 do
5017 {
5018 file_ptr last;
5019 symindex i;
5020 carsym *symdef;
5021 carsym *symdefend;
5022
5023 loop = FALSE;
5024 last = -1;
5025
5026 symdef = symdefs;
5027 symdefend = symdef + c;
5028 for (i = 0; symdef < symdefend; symdef++, i++)
5029 {
5030 struct elf_link_hash_entry *h;
5031 bfd *element;
5032 struct bfd_link_hash_entry *undefs_tail;
5033 symindex mark;
5034
5035 if (defined[i] || included[i])
5036 continue;
5037 if (symdef->file_offset == last)
5038 {
5039 included[i] = TRUE;
5040 continue;
5041 }
5042
8387904d
AM
5043 h = archive_symbol_lookup (abfd, info, symdef->name);
5044 if (h == (struct elf_link_hash_entry *) 0 - 1)
5045 goto error_return;
0ad989f9
L
5046
5047 if (h == NULL)
5048 continue;
5049
5050 if (h->root.type == bfd_link_hash_common)
5051 {
5052 /* We currently have a common symbol. The archive map contains
5053 a reference to this symbol, so we may want to include it. We
5054 only want to include it however, if this archive element
5055 contains a definition of the symbol, not just another common
5056 declaration of it.
5057
5058 Unfortunately some archivers (including GNU ar) will put
5059 declarations of common symbols into their archive maps, as
5060 well as real definitions, so we cannot just go by the archive
5061 map alone. Instead we must read in the element's symbol
5062 table and check that to see what kind of symbol definition
5063 this is. */
5064 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5065 continue;
5066 }
5067 else if (h->root.type != bfd_link_hash_undefined)
5068 {
5069 if (h->root.type != bfd_link_hash_undefweak)
5070 defined[i] = TRUE;
5071 continue;
5072 }
5073
5074 /* We need to include this archive member. */
5075 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5076 if (element == NULL)
5077 goto error_return;
5078
5079 if (! bfd_check_format (element, bfd_object))
5080 goto error_return;
5081
5082 /* Doublecheck that we have not included this object
5083 already--it should be impossible, but there may be
5084 something wrong with the archive. */
5085 if (element->archive_pass != 0)
5086 {
5087 bfd_set_error (bfd_error_bad_value);
5088 goto error_return;
5089 }
5090 element->archive_pass = 1;
5091
5092 undefs_tail = info->hash->undefs_tail;
5093
0e144ba7
AM
5094 if (!(*info->callbacks
5095 ->add_archive_element) (info, element, symdef->name, &element))
0ad989f9 5096 goto error_return;
0e144ba7 5097 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5098 goto error_return;
5099
5100 /* If there are any new undefined symbols, we need to make
5101 another pass through the archive in order to see whether
5102 they can be defined. FIXME: This isn't perfect, because
5103 common symbols wind up on undefs_tail and because an
5104 undefined symbol which is defined later on in this pass
5105 does not require another pass. This isn't a bug, but it
5106 does make the code less efficient than it could be. */
5107 if (undefs_tail != info->hash->undefs_tail)
5108 loop = TRUE;
5109
5110 /* Look backward to mark all symbols from this object file
5111 which we have already seen in this pass. */
5112 mark = i;
5113 do
5114 {
5115 included[mark] = TRUE;
5116 if (mark == 0)
5117 break;
5118 --mark;
5119 }
5120 while (symdefs[mark].file_offset == symdef->file_offset);
5121
5122 /* We mark subsequent symbols from this object file as we go
5123 on through the loop. */
5124 last = symdef->file_offset;
5125 }
5126 }
5127 while (loop);
5128
5129 free (defined);
5130 free (included);
5131
5132 return TRUE;
5133
5134 error_return:
5135 if (defined != NULL)
5136 free (defined);
5137 if (included != NULL)
5138 free (included);
5139 return FALSE;
5140}
4ad4eba5
AM
5141
5142/* Given an ELF BFD, add symbols to the global hash table as
5143 appropriate. */
5144
5145bfd_boolean
5146bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5147{
5148 switch (bfd_get_format (abfd))
5149 {
5150 case bfd_object:
5151 return elf_link_add_object_symbols (abfd, info);
5152 case bfd_archive:
5153 return elf_link_add_archive_symbols (abfd, info);
5154 default:
5155 bfd_set_error (bfd_error_wrong_format);
5156 return FALSE;
5157 }
5158}
5a580b3a 5159\f
14b1c01e
AM
5160struct hash_codes_info
5161{
5162 unsigned long *hashcodes;
5163 bfd_boolean error;
5164};
a0c8462f 5165
5a580b3a
AM
5166/* This function will be called though elf_link_hash_traverse to store
5167 all hash value of the exported symbols in an array. */
5168
5169static bfd_boolean
5170elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5171{
a50b1753 5172 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a
AM
5173 const char *name;
5174 char *p;
5175 unsigned long ha;
5176 char *alc = NULL;
5177
5a580b3a
AM
5178 /* Ignore indirect symbols. These are added by the versioning code. */
5179 if (h->dynindx == -1)
5180 return TRUE;
5181
5182 name = h->root.root.string;
5183 p = strchr (name, ELF_VER_CHR);
5184 if (p != NULL)
5185 {
a50b1753 5186 alc = (char *) bfd_malloc (p - name + 1);
14b1c01e
AM
5187 if (alc == NULL)
5188 {
5189 inf->error = TRUE;
5190 return FALSE;
5191 }
5a580b3a
AM
5192 memcpy (alc, name, p - name);
5193 alc[p - name] = '\0';
5194 name = alc;
5195 }
5196
5197 /* Compute the hash value. */
5198 ha = bfd_elf_hash (name);
5199
5200 /* Store the found hash value in the array given as the argument. */
14b1c01e 5201 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5202
5203 /* And store it in the struct so that we can put it in the hash table
5204 later. */
f6e332e6 5205 h->u.elf_hash_value = ha;
5a580b3a
AM
5206
5207 if (alc != NULL)
5208 free (alc);
5209
5210 return TRUE;
5211}
5212
fdc90cb4
JJ
5213struct collect_gnu_hash_codes
5214{
5215 bfd *output_bfd;
5216 const struct elf_backend_data *bed;
5217 unsigned long int nsyms;
5218 unsigned long int maskbits;
5219 unsigned long int *hashcodes;
5220 unsigned long int *hashval;
5221 unsigned long int *indx;
5222 unsigned long int *counts;
5223 bfd_vma *bitmask;
5224 bfd_byte *contents;
5225 long int min_dynindx;
5226 unsigned long int bucketcount;
5227 unsigned long int symindx;
5228 long int local_indx;
5229 long int shift1, shift2;
5230 unsigned long int mask;
14b1c01e 5231 bfd_boolean error;
fdc90cb4
JJ
5232};
5233
5234/* This function will be called though elf_link_hash_traverse to store
5235 all hash value of the exported symbols in an array. */
5236
5237static bfd_boolean
5238elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5239{
a50b1753 5240 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5241 const char *name;
5242 char *p;
5243 unsigned long ha;
5244 char *alc = NULL;
5245
fdc90cb4
JJ
5246 /* Ignore indirect symbols. These are added by the versioning code. */
5247 if (h->dynindx == -1)
5248 return TRUE;
5249
5250 /* Ignore also local symbols and undefined symbols. */
5251 if (! (*s->bed->elf_hash_symbol) (h))
5252 return TRUE;
5253
5254 name = h->root.root.string;
5255 p = strchr (name, ELF_VER_CHR);
5256 if (p != NULL)
5257 {
a50b1753 5258 alc = (char *) bfd_malloc (p - name + 1);
14b1c01e
AM
5259 if (alc == NULL)
5260 {
5261 s->error = TRUE;
5262 return FALSE;
5263 }
fdc90cb4
JJ
5264 memcpy (alc, name, p - name);
5265 alc[p - name] = '\0';
5266 name = alc;
5267 }
5268
5269 /* Compute the hash value. */
5270 ha = bfd_elf_gnu_hash (name);
5271
5272 /* Store the found hash value in the array for compute_bucket_count,
5273 and also for .dynsym reordering purposes. */
5274 s->hashcodes[s->nsyms] = ha;
5275 s->hashval[h->dynindx] = ha;
5276 ++s->nsyms;
5277 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5278 s->min_dynindx = h->dynindx;
5279
5280 if (alc != NULL)
5281 free (alc);
5282
5283 return TRUE;
5284}
5285
5286/* This function will be called though elf_link_hash_traverse to do
5287 final dynaminc symbol renumbering. */
5288
5289static bfd_boolean
5290elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5291{
a50b1753 5292 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5293 unsigned long int bucket;
5294 unsigned long int val;
5295
fdc90cb4
JJ
5296 /* Ignore indirect symbols. */
5297 if (h->dynindx == -1)
5298 return TRUE;
5299
5300 /* Ignore also local symbols and undefined symbols. */
5301 if (! (*s->bed->elf_hash_symbol) (h))
5302 {
5303 if (h->dynindx >= s->min_dynindx)
5304 h->dynindx = s->local_indx++;
5305 return TRUE;
5306 }
5307
5308 bucket = s->hashval[h->dynindx] % s->bucketcount;
5309 val = (s->hashval[h->dynindx] >> s->shift1)
5310 & ((s->maskbits >> s->shift1) - 1);
5311 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5312 s->bitmask[val]
5313 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5314 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5315 if (s->counts[bucket] == 1)
5316 /* Last element terminates the chain. */
5317 val |= 1;
5318 bfd_put_32 (s->output_bfd, val,
5319 s->contents + (s->indx[bucket] - s->symindx) * 4);
5320 --s->counts[bucket];
5321 h->dynindx = s->indx[bucket]++;
5322 return TRUE;
5323}
5324
5325/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5326
5327bfd_boolean
5328_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5329{
5330 return !(h->forced_local
5331 || h->root.type == bfd_link_hash_undefined
5332 || h->root.type == bfd_link_hash_undefweak
5333 || ((h->root.type == bfd_link_hash_defined
5334 || h->root.type == bfd_link_hash_defweak)
5335 && h->root.u.def.section->output_section == NULL));
5336}
5337
5a580b3a
AM
5338/* Array used to determine the number of hash table buckets to use
5339 based on the number of symbols there are. If there are fewer than
5340 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5341 fewer than 37 we use 17 buckets, and so forth. We never use more
5342 than 32771 buckets. */
5343
5344static const size_t elf_buckets[] =
5345{
5346 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5347 16411, 32771, 0
5348};
5349
5350/* Compute bucket count for hashing table. We do not use a static set
5351 of possible tables sizes anymore. Instead we determine for all
5352 possible reasonable sizes of the table the outcome (i.e., the
5353 number of collisions etc) and choose the best solution. The
5354 weighting functions are not too simple to allow the table to grow
5355 without bounds. Instead one of the weighting factors is the size.
5356 Therefore the result is always a good payoff between few collisions
5357 (= short chain lengths) and table size. */
5358static size_t
b20dd2ce 5359compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5360 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5361 unsigned long int nsyms,
5362 int gnu_hash)
5a580b3a 5363{
5a580b3a 5364 size_t best_size = 0;
5a580b3a 5365 unsigned long int i;
5a580b3a 5366
5a580b3a
AM
5367 /* We have a problem here. The following code to optimize the table
5368 size requires an integer type with more the 32 bits. If
5369 BFD_HOST_U_64_BIT is set we know about such a type. */
5370#ifdef BFD_HOST_U_64_BIT
5371 if (info->optimize)
5372 {
5a580b3a
AM
5373 size_t minsize;
5374 size_t maxsize;
5375 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5376 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5377 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5378 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5379 unsigned long int *counts;
d40f3da9 5380 bfd_size_type amt;
0883b6e0 5381 unsigned int no_improvement_count = 0;
5a580b3a
AM
5382
5383 /* Possible optimization parameters: if we have NSYMS symbols we say
5384 that the hashing table must at least have NSYMS/4 and at most
5385 2*NSYMS buckets. */
5386 minsize = nsyms / 4;
5387 if (minsize == 0)
5388 minsize = 1;
5389 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5390 if (gnu_hash)
5391 {
5392 if (minsize < 2)
5393 minsize = 2;
5394 if ((best_size & 31) == 0)
5395 ++best_size;
5396 }
5a580b3a
AM
5397
5398 /* Create array where we count the collisions in. We must use bfd_malloc
5399 since the size could be large. */
5400 amt = maxsize;
5401 amt *= sizeof (unsigned long int);
a50b1753 5402 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5403 if (counts == NULL)
fdc90cb4 5404 return 0;
5a580b3a
AM
5405
5406 /* Compute the "optimal" size for the hash table. The criteria is a
5407 minimal chain length. The minor criteria is (of course) the size
5408 of the table. */
5409 for (i = minsize; i < maxsize; ++i)
5410 {
5411 /* Walk through the array of hashcodes and count the collisions. */
5412 BFD_HOST_U_64_BIT max;
5413 unsigned long int j;
5414 unsigned long int fact;
5415
fdc90cb4
JJ
5416 if (gnu_hash && (i & 31) == 0)
5417 continue;
5418
5a580b3a
AM
5419 memset (counts, '\0', i * sizeof (unsigned long int));
5420
5421 /* Determine how often each hash bucket is used. */
5422 for (j = 0; j < nsyms; ++j)
5423 ++counts[hashcodes[j] % i];
5424
5425 /* For the weight function we need some information about the
5426 pagesize on the target. This is information need not be 100%
5427 accurate. Since this information is not available (so far) we
5428 define it here to a reasonable default value. If it is crucial
5429 to have a better value some day simply define this value. */
5430# ifndef BFD_TARGET_PAGESIZE
5431# define BFD_TARGET_PAGESIZE (4096)
5432# endif
5433
fdc90cb4
JJ
5434 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5435 and the chains. */
5436 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5437
5438# if 1
5439 /* Variant 1: optimize for short chains. We add the squares
5440 of all the chain lengths (which favors many small chain
5441 over a few long chains). */
5442 for (j = 0; j < i; ++j)
5443 max += counts[j] * counts[j];
5444
5445 /* This adds penalties for the overall size of the table. */
fdc90cb4 5446 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5447 max *= fact * fact;
5448# else
5449 /* Variant 2: Optimize a lot more for small table. Here we
5450 also add squares of the size but we also add penalties for
5451 empty slots (the +1 term). */
5452 for (j = 0; j < i; ++j)
5453 max += (1 + counts[j]) * (1 + counts[j]);
5454
5455 /* The overall size of the table is considered, but not as
5456 strong as in variant 1, where it is squared. */
fdc90cb4 5457 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5458 max *= fact;
5459# endif
5460
5461 /* Compare with current best results. */
5462 if (max < best_chlen)
5463 {
5464 best_chlen = max;
5465 best_size = i;
0883b6e0 5466 no_improvement_count = 0;
5a580b3a 5467 }
0883b6e0
NC
5468 /* PR 11843: Avoid futile long searches for the best bucket size
5469 when there are a large number of symbols. */
5470 else if (++no_improvement_count == 100)
5471 break;
5a580b3a
AM
5472 }
5473
5474 free (counts);
5475 }
5476 else
5477#endif /* defined (BFD_HOST_U_64_BIT) */
5478 {
5479 /* This is the fallback solution if no 64bit type is available or if we
5480 are not supposed to spend much time on optimizations. We select the
5481 bucket count using a fixed set of numbers. */
5482 for (i = 0; elf_buckets[i] != 0; i++)
5483 {
5484 best_size = elf_buckets[i];
fdc90cb4 5485 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5486 break;
5487 }
fdc90cb4
JJ
5488 if (gnu_hash && best_size < 2)
5489 best_size = 2;
5a580b3a
AM
5490 }
5491
5a580b3a
AM
5492 return best_size;
5493}
5494
d0bf826b
AM
5495/* Size any SHT_GROUP section for ld -r. */
5496
5497bfd_boolean
5498_bfd_elf_size_group_sections (struct bfd_link_info *info)
5499{
5500 bfd *ibfd;
5501
5502 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5503 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5504 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5505 return FALSE;
5506 return TRUE;
5507}
5508
5a580b3a
AM
5509/* Set up the sizes and contents of the ELF dynamic sections. This is
5510 called by the ELF linker emulation before_allocation routine. We
5511 must set the sizes of the sections before the linker sets the
5512 addresses of the various sections. */
5513
5514bfd_boolean
5515bfd_elf_size_dynamic_sections (bfd *output_bfd,
5516 const char *soname,
5517 const char *rpath,
5518 const char *filter_shlib,
7ee314fa
AM
5519 const char *audit,
5520 const char *depaudit,
5a580b3a
AM
5521 const char * const *auxiliary_filters,
5522 struct bfd_link_info *info,
fd91d419 5523 asection **sinterpptr)
5a580b3a
AM
5524{
5525 bfd_size_type soname_indx;
5526 bfd *dynobj;
5527 const struct elf_backend_data *bed;
28caa186 5528 struct elf_info_failed asvinfo;
5a580b3a
AM
5529
5530 *sinterpptr = NULL;
5531
5532 soname_indx = (bfd_size_type) -1;
5533
5534 if (!is_elf_hash_table (info->hash))
5535 return TRUE;
5536
6bfdb61b 5537 bed = get_elf_backend_data (output_bfd);
5a580b3a
AM
5538 if (info->execstack)
5539 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5540 else if (info->noexecstack)
5541 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5542 else
5543 {
5544 bfd *inputobj;
5545 asection *notesec = NULL;
5546 int exec = 0;
5547
5548 for (inputobj = info->input_bfds;
5549 inputobj;
5550 inputobj = inputobj->link_next)
5551 {
5552 asection *s;
5553
a92c088a
L
5554 if (inputobj->flags
5555 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5556 continue;
5557 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5558 if (s)
5559 {
5560 if (s->flags & SEC_CODE)
5561 exec = PF_X;
5562 notesec = s;
5563 }
6bfdb61b 5564 else if (bed->default_execstack)
5a580b3a
AM
5565 exec = PF_X;
5566 }
5567 if (notesec)
5568 {
5569 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5570 if (exec && info->relocatable
5571 && notesec->output_section != bfd_abs_section_ptr)
5572 notesec->output_section->flags |= SEC_CODE;
5573 }
5574 }
5575
5576 /* Any syms created from now on start with -1 in
5577 got.refcount/offset and plt.refcount/offset. */
a6aa5195
AM
5578 elf_hash_table (info)->init_got_refcount
5579 = elf_hash_table (info)->init_got_offset;
5580 elf_hash_table (info)->init_plt_refcount
5581 = elf_hash_table (info)->init_plt_offset;
5a580b3a 5582
d0bf826b
AM
5583 if (info->relocatable
5584 && !_bfd_elf_size_group_sections (info))
5585 return FALSE;
5586
5a580b3a
AM
5587 /* The backend may have to create some sections regardless of whether
5588 we're dynamic or not. */
5a580b3a
AM
5589 if (bed->elf_backend_always_size_sections
5590 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5591 return FALSE;
5592
5593 dynobj = elf_hash_table (info)->dynobj;
5594
9a2a56cc 5595 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
5596 {
5597 struct elf_info_failed eif;
5598 struct elf_link_hash_entry *h;
5599 asection *dynstr;
5600 struct bfd_elf_version_tree *t;
5601 struct bfd_elf_version_expr *d;
046183de 5602 asection *s;
5a580b3a
AM
5603 bfd_boolean all_defined;
5604
3d4d4302 5605 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5a580b3a
AM
5606 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5607
5608 if (soname != NULL)
5609 {
5610 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5611 soname, TRUE);
5612 if (soname_indx == (bfd_size_type) -1
5613 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5614 return FALSE;
5615 }
5616
5617 if (info->symbolic)
5618 {
5619 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5620 return FALSE;
5621 info->flags |= DF_SYMBOLIC;
5622 }
5623
5624 if (rpath != NULL)
5625 {
5626 bfd_size_type indx;
5627
5628 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5629 TRUE);
5630 if (indx == (bfd_size_type) -1
5631 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5632 return FALSE;
5633
5634 if (info->new_dtags)
5635 {
5636 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5637 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5638 return FALSE;
5639 }
5640 }
5641
5642 if (filter_shlib != NULL)
5643 {
5644 bfd_size_type indx;
5645
5646 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5647 filter_shlib, TRUE);
5648 if (indx == (bfd_size_type) -1
5649 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5650 return FALSE;
5651 }
5652
5653 if (auxiliary_filters != NULL)
5654 {
5655 const char * const *p;
5656
5657 for (p = auxiliary_filters; *p != NULL; p++)
5658 {
5659 bfd_size_type indx;
5660
5661 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5662 *p, TRUE);
5663 if (indx == (bfd_size_type) -1
5664 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5665 return FALSE;
5666 }
5667 }
5668
7ee314fa
AM
5669 if (audit != NULL)
5670 {
5671 bfd_size_type indx;
5672
5673 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5674 TRUE);
5675 if (indx == (bfd_size_type) -1
5676 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5677 return FALSE;
5678 }
5679
5680 if (depaudit != NULL)
5681 {
5682 bfd_size_type indx;
5683
5684 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5685 TRUE);
5686 if (indx == (bfd_size_type) -1
5687 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5688 return FALSE;
5689 }
5690
5a580b3a 5691 eif.info = info;
5a580b3a
AM
5692 eif.failed = FALSE;
5693
5694 /* If we are supposed to export all symbols into the dynamic symbol
5695 table (this is not the normal case), then do so. */
55255dae
L
5696 if (info->export_dynamic
5697 || (info->executable && info->dynamic))
5a580b3a
AM
5698 {
5699 elf_link_hash_traverse (elf_hash_table (info),
5700 _bfd_elf_export_symbol,
5701 &eif);
5702 if (eif.failed)
5703 return FALSE;
5704 }
5705
5706 /* Make all global versions with definition. */
fd91d419 5707 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5708 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5709 if (!d->symver && d->literal)
5a580b3a
AM
5710 {
5711 const char *verstr, *name;
5712 size_t namelen, verlen, newlen;
93252b1c 5713 char *newname, *p, leading_char;
5a580b3a
AM
5714 struct elf_link_hash_entry *newh;
5715
93252b1c 5716 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 5717 name = d->pattern;
93252b1c 5718 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
5719 verstr = t->name;
5720 verlen = strlen (verstr);
5721 newlen = namelen + verlen + 3;
5722
a50b1753 5723 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
5724 if (newname == NULL)
5725 return FALSE;
93252b1c
MF
5726 newname[0] = leading_char;
5727 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
5728
5729 /* Check the hidden versioned definition. */
5730 p = newname + namelen;
5731 *p++ = ELF_VER_CHR;
5732 memcpy (p, verstr, verlen + 1);
5733 newh = elf_link_hash_lookup (elf_hash_table (info),
5734 newname, FALSE, FALSE,
5735 FALSE);
5736 if (newh == NULL
5737 || (newh->root.type != bfd_link_hash_defined
5738 && newh->root.type != bfd_link_hash_defweak))
5739 {
5740 /* Check the default versioned definition. */
5741 *p++ = ELF_VER_CHR;
5742 memcpy (p, verstr, verlen + 1);
5743 newh = elf_link_hash_lookup (elf_hash_table (info),
5744 newname, FALSE, FALSE,
5745 FALSE);
5746 }
5747 free (newname);
5748
5749 /* Mark this version if there is a definition and it is
5750 not defined in a shared object. */
5751 if (newh != NULL
f5385ebf 5752 && !newh->def_dynamic
5a580b3a
AM
5753 && (newh->root.type == bfd_link_hash_defined
5754 || newh->root.type == bfd_link_hash_defweak))
5755 d->symver = 1;
5756 }
5757
5758 /* Attach all the symbols to their version information. */
5a580b3a 5759 asvinfo.info = info;
5a580b3a
AM
5760 asvinfo.failed = FALSE;
5761
5762 elf_link_hash_traverse (elf_hash_table (info),
5763 _bfd_elf_link_assign_sym_version,
5764 &asvinfo);
5765 if (asvinfo.failed)
5766 return FALSE;
5767
5768 if (!info->allow_undefined_version)
5769 {
5770 /* Check if all global versions have a definition. */
5771 all_defined = TRUE;
fd91d419 5772 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5773 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5774 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
5775 {
5776 (*_bfd_error_handler)
5777 (_("%s: undefined version: %s"),
5778 d->pattern, t->name);
5779 all_defined = FALSE;
5780 }
5781
5782 if (!all_defined)
5783 {
5784 bfd_set_error (bfd_error_bad_value);
5785 return FALSE;
5786 }
5787 }
5788
5789 /* Find all symbols which were defined in a dynamic object and make
5790 the backend pick a reasonable value for them. */
5791 elf_link_hash_traverse (elf_hash_table (info),
5792 _bfd_elf_adjust_dynamic_symbol,
5793 &eif);
5794 if (eif.failed)
5795 return FALSE;
5796
5797 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 5798 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
5799 now so that we know the final size of the .dynamic section. */
5800
5801 /* If there are initialization and/or finalization functions to
5802 call then add the corresponding DT_INIT/DT_FINI entries. */
5803 h = (info->init_function
5804 ? elf_link_hash_lookup (elf_hash_table (info),
5805 info->init_function, FALSE,
5806 FALSE, FALSE)
5807 : NULL);
5808 if (h != NULL
f5385ebf
AM
5809 && (h->ref_regular
5810 || h->def_regular))
5a580b3a
AM
5811 {
5812 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5813 return FALSE;
5814 }
5815 h = (info->fini_function
5816 ? elf_link_hash_lookup (elf_hash_table (info),
5817 info->fini_function, FALSE,
5818 FALSE, FALSE)
5819 : NULL);
5820 if (h != NULL
f5385ebf
AM
5821 && (h->ref_regular
5822 || h->def_regular))
5a580b3a
AM
5823 {
5824 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5825 return FALSE;
5826 }
5827
046183de
AM
5828 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5829 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5830 {
5831 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5832 if (! info->executable)
5833 {
5834 bfd *sub;
5835 asection *o;
5836
5837 for (sub = info->input_bfds; sub != NULL;
5838 sub = sub->link_next)
3fcd97f1
JJ
5839 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5840 for (o = sub->sections; o != NULL; o = o->next)
5841 if (elf_section_data (o)->this_hdr.sh_type
5842 == SHT_PREINIT_ARRAY)
5843 {
5844 (*_bfd_error_handler)
5845 (_("%B: .preinit_array section is not allowed in DSO"),
5846 sub);
5847 break;
5848 }
5a580b3a
AM
5849
5850 bfd_set_error (bfd_error_nonrepresentable_section);
5851 return FALSE;
5852 }
5853
5854 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5855 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5856 return FALSE;
5857 }
046183de
AM
5858 s = bfd_get_section_by_name (output_bfd, ".init_array");
5859 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5860 {
5861 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5862 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5863 return FALSE;
5864 }
046183de
AM
5865 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5866 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5867 {
5868 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5869 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5870 return FALSE;
5871 }
5872
3d4d4302 5873 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
5874 /* If .dynstr is excluded from the link, we don't want any of
5875 these tags. Strictly, we should be checking each section
5876 individually; This quick check covers for the case where
5877 someone does a /DISCARD/ : { *(*) }. */
5878 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5879 {
5880 bfd_size_type strsize;
5881
5882 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
5883 if ((info->emit_hash
5884 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5885 || (info->emit_gnu_hash
5886 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
5887 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5888 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5889 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5890 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5891 bed->s->sizeof_sym))
5892 return FALSE;
5893 }
5894 }
5895
5896 /* The backend must work out the sizes of all the other dynamic
5897 sections. */
9a2a56cc
AM
5898 if (dynobj != NULL
5899 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
5900 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5901 return FALSE;
5902
9a2a56cc
AM
5903 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5904 return FALSE;
5905
5906 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 5907 {
554220db 5908 unsigned long section_sym_count;
fd91d419 5909 struct bfd_elf_version_tree *verdefs;
5a580b3a 5910 asection *s;
5a580b3a
AM
5911
5912 /* Set up the version definition section. */
3d4d4302 5913 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
5914 BFD_ASSERT (s != NULL);
5915
5916 /* We may have created additional version definitions if we are
5917 just linking a regular application. */
fd91d419 5918 verdefs = info->version_info;
5a580b3a
AM
5919
5920 /* Skip anonymous version tag. */
5921 if (verdefs != NULL && verdefs->vernum == 0)
5922 verdefs = verdefs->next;
5923
3e3b46e5 5924 if (verdefs == NULL && !info->create_default_symver)
8423293d 5925 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
5926 else
5927 {
5928 unsigned int cdefs;
5929 bfd_size_type size;
5930 struct bfd_elf_version_tree *t;
5931 bfd_byte *p;
5932 Elf_Internal_Verdef def;
5933 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
5934 struct bfd_link_hash_entry *bh;
5935 struct elf_link_hash_entry *h;
5936 const char *name;
5a580b3a
AM
5937
5938 cdefs = 0;
5939 size = 0;
5940
5941 /* Make space for the base version. */
5942 size += sizeof (Elf_External_Verdef);
5943 size += sizeof (Elf_External_Verdaux);
5944 ++cdefs;
5945
3e3b46e5
PB
5946 /* Make space for the default version. */
5947 if (info->create_default_symver)
5948 {
5949 size += sizeof (Elf_External_Verdef);
5950 ++cdefs;
5951 }
5952
5a580b3a
AM
5953 for (t = verdefs; t != NULL; t = t->next)
5954 {
5955 struct bfd_elf_version_deps *n;
5956
a6cc6b3b
RO
5957 /* Don't emit base version twice. */
5958 if (t->vernum == 0)
5959 continue;
5960
5a580b3a
AM
5961 size += sizeof (Elf_External_Verdef);
5962 size += sizeof (Elf_External_Verdaux);
5963 ++cdefs;
5964
5965 for (n = t->deps; n != NULL; n = n->next)
5966 size += sizeof (Elf_External_Verdaux);
5967 }
5968
eea6121a 5969 s->size = size;
a50b1753 5970 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 5971 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
5972 return FALSE;
5973
5974 /* Fill in the version definition section. */
5975
5976 p = s->contents;
5977
5978 def.vd_version = VER_DEF_CURRENT;
5979 def.vd_flags = VER_FLG_BASE;
5980 def.vd_ndx = 1;
5981 def.vd_cnt = 1;
3e3b46e5
PB
5982 if (info->create_default_symver)
5983 {
5984 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5985 def.vd_next = sizeof (Elf_External_Verdef);
5986 }
5987 else
5988 {
5989 def.vd_aux = sizeof (Elf_External_Verdef);
5990 def.vd_next = (sizeof (Elf_External_Verdef)
5991 + sizeof (Elf_External_Verdaux));
5992 }
5a580b3a
AM
5993
5994 if (soname_indx != (bfd_size_type) -1)
5995 {
5996 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5997 soname_indx);
5998 def.vd_hash = bfd_elf_hash (soname);
5999 defaux.vda_name = soname_indx;
3e3b46e5 6000 name = soname;
5a580b3a
AM
6001 }
6002 else
6003 {
5a580b3a
AM
6004 bfd_size_type indx;
6005
06084812 6006 name = lbasename (output_bfd->filename);
5a580b3a
AM
6007 def.vd_hash = bfd_elf_hash (name);
6008 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6009 name, FALSE);
6010 if (indx == (bfd_size_type) -1)
6011 return FALSE;
6012 defaux.vda_name = indx;
6013 }
6014 defaux.vda_next = 0;
6015
6016 _bfd_elf_swap_verdef_out (output_bfd, &def,
6017 (Elf_External_Verdef *) p);
6018 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6019 if (info->create_default_symver)
6020 {
6021 /* Add a symbol representing this version. */
6022 bh = NULL;
6023 if (! (_bfd_generic_link_add_one_symbol
6024 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6025 0, NULL, FALSE,
6026 get_elf_backend_data (dynobj)->collect, &bh)))
6027 return FALSE;
6028 h = (struct elf_link_hash_entry *) bh;
6029 h->non_elf = 0;
6030 h->def_regular = 1;
6031 h->type = STT_OBJECT;
6032 h->verinfo.vertree = NULL;
6033
6034 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6035 return FALSE;
6036
6037 /* Create a duplicate of the base version with the same
6038 aux block, but different flags. */
6039 def.vd_flags = 0;
6040 def.vd_ndx = 2;
6041 def.vd_aux = sizeof (Elf_External_Verdef);
6042 if (verdefs)
6043 def.vd_next = (sizeof (Elf_External_Verdef)
6044 + sizeof (Elf_External_Verdaux));
6045 else
6046 def.vd_next = 0;
6047 _bfd_elf_swap_verdef_out (output_bfd, &def,
6048 (Elf_External_Verdef *) p);
6049 p += sizeof (Elf_External_Verdef);
6050 }
5a580b3a
AM
6051 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6052 (Elf_External_Verdaux *) p);
6053 p += sizeof (Elf_External_Verdaux);
6054
6055 for (t = verdefs; t != NULL; t = t->next)
6056 {
6057 unsigned int cdeps;
6058 struct bfd_elf_version_deps *n;
5a580b3a 6059
a6cc6b3b
RO
6060 /* Don't emit the base version twice. */
6061 if (t->vernum == 0)
6062 continue;
6063
5a580b3a
AM
6064 cdeps = 0;
6065 for (n = t->deps; n != NULL; n = n->next)
6066 ++cdeps;
6067
6068 /* Add a symbol representing this version. */
6069 bh = NULL;
6070 if (! (_bfd_generic_link_add_one_symbol
6071 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6072 0, NULL, FALSE,
6073 get_elf_backend_data (dynobj)->collect, &bh)))
6074 return FALSE;
6075 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6076 h->non_elf = 0;
6077 h->def_regular = 1;
5a580b3a
AM
6078 h->type = STT_OBJECT;
6079 h->verinfo.vertree = t;
6080
c152c796 6081 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6082 return FALSE;
6083
6084 def.vd_version = VER_DEF_CURRENT;
6085 def.vd_flags = 0;
6086 if (t->globals.list == NULL
6087 && t->locals.list == NULL
6088 && ! t->used)
6089 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6090 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6091 def.vd_cnt = cdeps + 1;
6092 def.vd_hash = bfd_elf_hash (t->name);
6093 def.vd_aux = sizeof (Elf_External_Verdef);
6094 def.vd_next = 0;
a6cc6b3b
RO
6095
6096 /* If a basever node is next, it *must* be the last node in
6097 the chain, otherwise Verdef construction breaks. */
6098 if (t->next != NULL && t->next->vernum == 0)
6099 BFD_ASSERT (t->next->next == NULL);
6100
6101 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6102 def.vd_next = (sizeof (Elf_External_Verdef)
6103 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6104
6105 _bfd_elf_swap_verdef_out (output_bfd, &def,
6106 (Elf_External_Verdef *) p);
6107 p += sizeof (Elf_External_Verdef);
6108
6109 defaux.vda_name = h->dynstr_index;
6110 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6111 h->dynstr_index);
6112 defaux.vda_next = 0;
6113 if (t->deps != NULL)
6114 defaux.vda_next = sizeof (Elf_External_Verdaux);
6115 t->name_indx = defaux.vda_name;
6116
6117 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6118 (Elf_External_Verdaux *) p);
6119 p += sizeof (Elf_External_Verdaux);
6120
6121 for (n = t->deps; n != NULL; n = n->next)
6122 {
6123 if (n->version_needed == NULL)
6124 {
6125 /* This can happen if there was an error in the
6126 version script. */
6127 defaux.vda_name = 0;
6128 }
6129 else
6130 {
6131 defaux.vda_name = n->version_needed->name_indx;
6132 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6133 defaux.vda_name);
6134 }
6135 if (n->next == NULL)
6136 defaux.vda_next = 0;
6137 else
6138 defaux.vda_next = sizeof (Elf_External_Verdaux);
6139
6140 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6141 (Elf_External_Verdaux *) p);
6142 p += sizeof (Elf_External_Verdaux);
6143 }
6144 }
6145
6146 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6147 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6148 return FALSE;
6149
6150 elf_tdata (output_bfd)->cverdefs = cdefs;
6151 }
6152
6153 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6154 {
6155 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6156 return FALSE;
6157 }
6158 else if (info->flags & DF_BIND_NOW)
6159 {
6160 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6161 return FALSE;
6162 }
6163
6164 if (info->flags_1)
6165 {
6166 if (info->executable)
6167 info->flags_1 &= ~ (DF_1_INITFIRST
6168 | DF_1_NODELETE
6169 | DF_1_NOOPEN);
6170 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6171 return FALSE;
6172 }
6173
6174 /* Work out the size of the version reference section. */
6175
3d4d4302 6176 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6177 BFD_ASSERT (s != NULL);
6178 {
6179 struct elf_find_verdep_info sinfo;
6180
5a580b3a
AM
6181 sinfo.info = info;
6182 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6183 if (sinfo.vers == 0)
6184 sinfo.vers = 1;
6185 sinfo.failed = FALSE;
6186
6187 elf_link_hash_traverse (elf_hash_table (info),
6188 _bfd_elf_link_find_version_dependencies,
6189 &sinfo);
14b1c01e
AM
6190 if (sinfo.failed)
6191 return FALSE;
5a580b3a
AM
6192
6193 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6194 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6195 else
6196 {
6197 Elf_Internal_Verneed *t;
6198 unsigned int size;
6199 unsigned int crefs;
6200 bfd_byte *p;
6201
a6cc6b3b 6202 /* Build the version dependency section. */
5a580b3a
AM
6203 size = 0;
6204 crefs = 0;
6205 for (t = elf_tdata (output_bfd)->verref;
6206 t != NULL;
6207 t = t->vn_nextref)
6208 {
6209 Elf_Internal_Vernaux *a;
6210
6211 size += sizeof (Elf_External_Verneed);
6212 ++crefs;
6213 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6214 size += sizeof (Elf_External_Vernaux);
6215 }
6216
eea6121a 6217 s->size = size;
a50b1753 6218 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6219 if (s->contents == NULL)
6220 return FALSE;
6221
6222 p = s->contents;
6223 for (t = elf_tdata (output_bfd)->verref;
6224 t != NULL;
6225 t = t->vn_nextref)
6226 {
6227 unsigned int caux;
6228 Elf_Internal_Vernaux *a;
6229 bfd_size_type indx;
6230
6231 caux = 0;
6232 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6233 ++caux;
6234
6235 t->vn_version = VER_NEED_CURRENT;
6236 t->vn_cnt = caux;
6237 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6238 elf_dt_name (t->vn_bfd) != NULL
6239 ? elf_dt_name (t->vn_bfd)
06084812 6240 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6241 FALSE);
6242 if (indx == (bfd_size_type) -1)
6243 return FALSE;
6244 t->vn_file = indx;
6245 t->vn_aux = sizeof (Elf_External_Verneed);
6246 if (t->vn_nextref == NULL)
6247 t->vn_next = 0;
6248 else
6249 t->vn_next = (sizeof (Elf_External_Verneed)
6250 + caux * sizeof (Elf_External_Vernaux));
6251
6252 _bfd_elf_swap_verneed_out (output_bfd, t,
6253 (Elf_External_Verneed *) p);
6254 p += sizeof (Elf_External_Verneed);
6255
6256 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6257 {
6258 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6259 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6260 a->vna_nodename, FALSE);
6261 if (indx == (bfd_size_type) -1)
6262 return FALSE;
6263 a->vna_name = indx;
6264 if (a->vna_nextptr == NULL)
6265 a->vna_next = 0;
6266 else
6267 a->vna_next = sizeof (Elf_External_Vernaux);
6268
6269 _bfd_elf_swap_vernaux_out (output_bfd, a,
6270 (Elf_External_Vernaux *) p);
6271 p += sizeof (Elf_External_Vernaux);
6272 }
6273 }
6274
6275 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6276 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6277 return FALSE;
6278
6279 elf_tdata (output_bfd)->cverrefs = crefs;
6280 }
6281 }
6282
8423293d
AM
6283 if ((elf_tdata (output_bfd)->cverrefs == 0
6284 && elf_tdata (output_bfd)->cverdefs == 0)
6285 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6286 &section_sym_count) == 0)
6287 {
3d4d4302 6288 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6289 s->flags |= SEC_EXCLUDE;
6290 }
6291 }
6292 return TRUE;
6293}
6294
74541ad4
AM
6295/* Find the first non-excluded output section. We'll use its
6296 section symbol for some emitted relocs. */
6297void
6298_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6299{
6300 asection *s;
6301
6302 for (s = output_bfd->sections; s != NULL; s = s->next)
6303 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6304 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6305 {
6306 elf_hash_table (info)->text_index_section = s;
6307 break;
6308 }
6309}
6310
6311/* Find two non-excluded output sections, one for code, one for data.
6312 We'll use their section symbols for some emitted relocs. */
6313void
6314_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6315{
6316 asection *s;
6317
266b05cf
DJ
6318 /* Data first, since setting text_index_section changes
6319 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6320 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6321 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6322 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6323 {
266b05cf 6324 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6325 break;
6326 }
6327
6328 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6329 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6330 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6331 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6332 {
266b05cf 6333 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6334 break;
6335 }
6336
6337 if (elf_hash_table (info)->text_index_section == NULL)
6338 elf_hash_table (info)->text_index_section
6339 = elf_hash_table (info)->data_index_section;
6340}
6341
8423293d
AM
6342bfd_boolean
6343bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6344{
74541ad4
AM
6345 const struct elf_backend_data *bed;
6346
8423293d
AM
6347 if (!is_elf_hash_table (info->hash))
6348 return TRUE;
6349
74541ad4
AM
6350 bed = get_elf_backend_data (output_bfd);
6351 (*bed->elf_backend_init_index_section) (output_bfd, info);
6352
8423293d
AM
6353 if (elf_hash_table (info)->dynamic_sections_created)
6354 {
6355 bfd *dynobj;
8423293d
AM
6356 asection *s;
6357 bfd_size_type dynsymcount;
6358 unsigned long section_sym_count;
8423293d
AM
6359 unsigned int dtagcount;
6360
6361 dynobj = elf_hash_table (info)->dynobj;
6362
5a580b3a
AM
6363 /* Assign dynsym indicies. In a shared library we generate a
6364 section symbol for each output section, which come first.
6365 Next come all of the back-end allocated local dynamic syms,
6366 followed by the rest of the global symbols. */
6367
554220db
AM
6368 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6369 &section_sym_count);
5a580b3a
AM
6370
6371 /* Work out the size of the symbol version section. */
3d4d4302 6372 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6373 BFD_ASSERT (s != NULL);
8423293d
AM
6374 if (dynsymcount != 0
6375 && (s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6376 {
eea6121a 6377 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6378 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6379 if (s->contents == NULL)
6380 return FALSE;
6381
6382 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6383 return FALSE;
6384 }
6385
6386 /* Set the size of the .dynsym and .hash sections. We counted
6387 the number of dynamic symbols in elf_link_add_object_symbols.
6388 We will build the contents of .dynsym and .hash when we build
6389 the final symbol table, because until then we do not know the
6390 correct value to give the symbols. We built the .dynstr
6391 section as we went along in elf_link_add_object_symbols. */
3d4d4302 6392 s = bfd_get_linker_section (dynobj, ".dynsym");
5a580b3a 6393 BFD_ASSERT (s != NULL);
eea6121a 6394 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a
AM
6395
6396 if (dynsymcount != 0)
6397 {
a50b1753 6398 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
554220db
AM
6399 if (s->contents == NULL)
6400 return FALSE;
5a580b3a 6401
554220db
AM
6402 /* The first entry in .dynsym is a dummy symbol.
6403 Clear all the section syms, in case we don't output them all. */
6404 ++section_sym_count;
6405 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a
AM
6406 }
6407
fdc90cb4
JJ
6408 elf_hash_table (info)->bucketcount = 0;
6409
5a580b3a
AM
6410 /* Compute the size of the hashing table. As a side effect this
6411 computes the hash values for all the names we export. */
fdc90cb4
JJ
6412 if (info->emit_hash)
6413 {
6414 unsigned long int *hashcodes;
14b1c01e 6415 struct hash_codes_info hashinf;
fdc90cb4
JJ
6416 bfd_size_type amt;
6417 unsigned long int nsyms;
6418 size_t bucketcount;
6419 size_t hash_entry_size;
6420
6421 /* Compute the hash values for all exported symbols. At the same
6422 time store the values in an array so that we could use them for
6423 optimizations. */
6424 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6425 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6426 if (hashcodes == NULL)
6427 return FALSE;
14b1c01e
AM
6428 hashinf.hashcodes = hashcodes;
6429 hashinf.error = FALSE;
5a580b3a 6430
fdc90cb4
JJ
6431 /* Put all hash values in HASHCODES. */
6432 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6433 elf_collect_hash_codes, &hashinf);
6434 if (hashinf.error)
4dd07732
AM
6435 {
6436 free (hashcodes);
6437 return FALSE;
6438 }
5a580b3a 6439
14b1c01e 6440 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6441 bucketcount
6442 = compute_bucket_count (info, hashcodes, nsyms, 0);
6443 free (hashcodes);
6444
6445 if (bucketcount == 0)
6446 return FALSE;
5a580b3a 6447
fdc90cb4
JJ
6448 elf_hash_table (info)->bucketcount = bucketcount;
6449
3d4d4302 6450 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6451 BFD_ASSERT (s != NULL);
6452 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6453 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6454 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6455 if (s->contents == NULL)
6456 return FALSE;
6457
6458 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6459 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6460 s->contents + hash_entry_size);
6461 }
6462
6463 if (info->emit_gnu_hash)
6464 {
6465 size_t i, cnt;
6466 unsigned char *contents;
6467 struct collect_gnu_hash_codes cinfo;
6468 bfd_size_type amt;
6469 size_t bucketcount;
6470
6471 memset (&cinfo, 0, sizeof (cinfo));
6472
6473 /* Compute the hash values for all exported symbols. At the same
6474 time store the values in an array so that we could use them for
6475 optimizations. */
6476 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6477 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6478 if (cinfo.hashcodes == NULL)
6479 return FALSE;
6480
6481 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6482 cinfo.min_dynindx = -1;
6483 cinfo.output_bfd = output_bfd;
6484 cinfo.bed = bed;
6485
6486 /* Put all hash values in HASHCODES. */
6487 elf_link_hash_traverse (elf_hash_table (info),
6488 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6489 if (cinfo.error)
4dd07732
AM
6490 {
6491 free (cinfo.hashcodes);
6492 return FALSE;
6493 }
fdc90cb4
JJ
6494
6495 bucketcount
6496 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6497
6498 if (bucketcount == 0)
6499 {
6500 free (cinfo.hashcodes);
6501 return FALSE;
6502 }
6503
3d4d4302 6504 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6505 BFD_ASSERT (s != NULL);
6506
6507 if (cinfo.nsyms == 0)
6508 {
6509 /* Empty .gnu.hash section is special. */
6510 BFD_ASSERT (cinfo.min_dynindx == -1);
6511 free (cinfo.hashcodes);
6512 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6513 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6514 if (contents == NULL)
6515 return FALSE;
6516 s->contents = contents;
6517 /* 1 empty bucket. */
6518 bfd_put_32 (output_bfd, 1, contents);
6519 /* SYMIDX above the special symbol 0. */
6520 bfd_put_32 (output_bfd, 1, contents + 4);
6521 /* Just one word for bitmask. */
6522 bfd_put_32 (output_bfd, 1, contents + 8);
6523 /* Only hash fn bloom filter. */
6524 bfd_put_32 (output_bfd, 0, contents + 12);
6525 /* No hashes are valid - empty bitmask. */
6526 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6527 /* No hashes in the only bucket. */
6528 bfd_put_32 (output_bfd, 0,
6529 contents + 16 + bed->s->arch_size / 8);
6530 }
6531 else
6532 {
9e6619e2 6533 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6534 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6535
9e6619e2
AM
6536 x = cinfo.nsyms;
6537 maskbitslog2 = 1;
6538 while ((x >>= 1) != 0)
6539 ++maskbitslog2;
fdc90cb4
JJ
6540 if (maskbitslog2 < 3)
6541 maskbitslog2 = 5;
6542 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6543 maskbitslog2 = maskbitslog2 + 3;
6544 else
6545 maskbitslog2 = maskbitslog2 + 2;
6546 if (bed->s->arch_size == 64)
6547 {
6548 if (maskbitslog2 == 5)
6549 maskbitslog2 = 6;
6550 cinfo.shift1 = 6;
6551 }
6552 else
6553 cinfo.shift1 = 5;
6554 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6555 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6556 cinfo.maskbits = 1 << maskbitslog2;
6557 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6558 amt = bucketcount * sizeof (unsigned long int) * 2;
6559 amt += maskwords * sizeof (bfd_vma);
a50b1753 6560 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6561 if (cinfo.bitmask == NULL)
6562 {
6563 free (cinfo.hashcodes);
6564 return FALSE;
6565 }
6566
a50b1753 6567 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6568 cinfo.indx = cinfo.counts + bucketcount;
6569 cinfo.symindx = dynsymcount - cinfo.nsyms;
6570 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6571
6572 /* Determine how often each hash bucket is used. */
6573 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6574 for (i = 0; i < cinfo.nsyms; ++i)
6575 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6576
6577 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6578 if (cinfo.counts[i] != 0)
6579 {
6580 cinfo.indx[i] = cnt;
6581 cnt += cinfo.counts[i];
6582 }
6583 BFD_ASSERT (cnt == dynsymcount);
6584 cinfo.bucketcount = bucketcount;
6585 cinfo.local_indx = cinfo.min_dynindx;
6586
6587 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6588 s->size += cinfo.maskbits / 8;
a50b1753 6589 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6590 if (contents == NULL)
6591 {
6592 free (cinfo.bitmask);
6593 free (cinfo.hashcodes);
6594 return FALSE;
6595 }
6596
6597 s->contents = contents;
6598 bfd_put_32 (output_bfd, bucketcount, contents);
6599 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6600 bfd_put_32 (output_bfd, maskwords, contents + 8);
6601 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6602 contents += 16 + cinfo.maskbits / 8;
6603
6604 for (i = 0; i < bucketcount; ++i)
6605 {
6606 if (cinfo.counts[i] == 0)
6607 bfd_put_32 (output_bfd, 0, contents);
6608 else
6609 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6610 contents += 4;
6611 }
6612
6613 cinfo.contents = contents;
6614
6615 /* Renumber dynamic symbols, populate .gnu.hash section. */
6616 elf_link_hash_traverse (elf_hash_table (info),
6617 elf_renumber_gnu_hash_syms, &cinfo);
6618
6619 contents = s->contents + 16;
6620 for (i = 0; i < maskwords; ++i)
6621 {
6622 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6623 contents);
6624 contents += bed->s->arch_size / 8;
6625 }
6626
6627 free (cinfo.bitmask);
6628 free (cinfo.hashcodes);
6629 }
6630 }
5a580b3a 6631
3d4d4302 6632 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6633 BFD_ASSERT (s != NULL);
6634
4ad4eba5 6635 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6636
eea6121a 6637 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6638
6639 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6640 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6641 return FALSE;
6642 }
6643
6644 return TRUE;
6645}
4d269e42 6646\f
4d269e42
AM
6647/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6648
6649static void
6650merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6651 asection *sec)
6652{
dbaa2011
AM
6653 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6654 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
6655}
6656
6657/* Finish SHF_MERGE section merging. */
6658
6659bfd_boolean
6660_bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6661{
6662 bfd *ibfd;
6663 asection *sec;
6664
6665 if (!is_elf_hash_table (info->hash))
6666 return FALSE;
6667
6668 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6669 if ((ibfd->flags & DYNAMIC) == 0)
6670 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6671 if ((sec->flags & SEC_MERGE) != 0
6672 && !bfd_is_abs_section (sec->output_section))
6673 {
6674 struct bfd_elf_section_data *secdata;
6675
6676 secdata = elf_section_data (sec);
6677 if (! _bfd_add_merge_section (abfd,
6678 &elf_hash_table (info)->merge_info,
6679 sec, &secdata->sec_info))
6680 return FALSE;
6681 else if (secdata->sec_info)
dbaa2011 6682 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
6683 }
6684
6685 if (elf_hash_table (info)->merge_info != NULL)
6686 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6687 merge_sections_remove_hook);
6688 return TRUE;
6689}
6690
6691/* Create an entry in an ELF linker hash table. */
6692
6693struct bfd_hash_entry *
6694_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6695 struct bfd_hash_table *table,
6696 const char *string)
6697{
6698 /* Allocate the structure if it has not already been allocated by a
6699 subclass. */
6700 if (entry == NULL)
6701 {
a50b1753
NC
6702 entry = (struct bfd_hash_entry *)
6703 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6704 if (entry == NULL)
6705 return entry;
6706 }
6707
6708 /* Call the allocation method of the superclass. */
6709 entry = _bfd_link_hash_newfunc (entry, table, string);
6710 if (entry != NULL)
6711 {
6712 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6713 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6714
6715 /* Set local fields. */
6716 ret->indx = -1;
6717 ret->dynindx = -1;
6718 ret->got = htab->init_got_refcount;
6719 ret->plt = htab->init_plt_refcount;
6720 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6721 - offsetof (struct elf_link_hash_entry, size)));
6722 /* Assume that we have been called by a non-ELF symbol reader.
6723 This flag is then reset by the code which reads an ELF input
6724 file. This ensures that a symbol created by a non-ELF symbol
6725 reader will have the flag set correctly. */
6726 ret->non_elf = 1;
6727 }
6728
6729 return entry;
6730}
6731
6732/* Copy data from an indirect symbol to its direct symbol, hiding the
6733 old indirect symbol. Also used for copying flags to a weakdef. */
6734
6735void
6736_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6737 struct elf_link_hash_entry *dir,
6738 struct elf_link_hash_entry *ind)
6739{
6740 struct elf_link_hash_table *htab;
6741
6742 /* Copy down any references that we may have already seen to the
6743 symbol which just became indirect. */
6744
6745 dir->ref_dynamic |= ind->ref_dynamic;
6746 dir->ref_regular |= ind->ref_regular;
6747 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6748 dir->non_got_ref |= ind->non_got_ref;
6749 dir->needs_plt |= ind->needs_plt;
6750 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6751
6752 if (ind->root.type != bfd_link_hash_indirect)
6753 return;
6754
6755 /* Copy over the global and procedure linkage table refcount entries.
6756 These may have been already set up by a check_relocs routine. */
6757 htab = elf_hash_table (info);
6758 if (ind->got.refcount > htab->init_got_refcount.refcount)
6759 {
6760 if (dir->got.refcount < 0)
6761 dir->got.refcount = 0;
6762 dir->got.refcount += ind->got.refcount;
6763 ind->got.refcount = htab->init_got_refcount.refcount;
6764 }
6765
6766 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6767 {
6768 if (dir->plt.refcount < 0)
6769 dir->plt.refcount = 0;
6770 dir->plt.refcount += ind->plt.refcount;
6771 ind->plt.refcount = htab->init_plt_refcount.refcount;
6772 }
6773
6774 if (ind->dynindx != -1)
6775 {
6776 if (dir->dynindx != -1)
6777 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6778 dir->dynindx = ind->dynindx;
6779 dir->dynstr_index = ind->dynstr_index;
6780 ind->dynindx = -1;
6781 ind->dynstr_index = 0;
6782 }
6783}
6784
6785void
6786_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6787 struct elf_link_hash_entry *h,
6788 bfd_boolean force_local)
6789{
3aa14d16
L
6790 /* STT_GNU_IFUNC symbol must go through PLT. */
6791 if (h->type != STT_GNU_IFUNC)
6792 {
6793 h->plt = elf_hash_table (info)->init_plt_offset;
6794 h->needs_plt = 0;
6795 }
4d269e42
AM
6796 if (force_local)
6797 {
6798 h->forced_local = 1;
6799 if (h->dynindx != -1)
6800 {
6801 h->dynindx = -1;
6802 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6803 h->dynstr_index);
6804 }
6805 }
6806}
6807
6808/* Initialize an ELF linker hash table. */
6809
6810bfd_boolean
6811_bfd_elf_link_hash_table_init
6812 (struct elf_link_hash_table *table,
6813 bfd *abfd,
6814 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6815 struct bfd_hash_table *,
6816 const char *),
4dfe6ac6
NC
6817 unsigned int entsize,
6818 enum elf_target_id target_id)
4d269e42
AM
6819{
6820 bfd_boolean ret;
6821 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6822
6823 memset (table, 0, sizeof * table);
6824 table->init_got_refcount.refcount = can_refcount - 1;
6825 table->init_plt_refcount.refcount = can_refcount - 1;
6826 table->init_got_offset.offset = -(bfd_vma) 1;
6827 table->init_plt_offset.offset = -(bfd_vma) 1;
6828 /* The first dynamic symbol is a dummy. */
6829 table->dynsymcount = 1;
6830
6831 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 6832
4d269e42 6833 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 6834 table->hash_table_id = target_id;
4d269e42
AM
6835
6836 return ret;
6837}
6838
6839/* Create an ELF linker hash table. */
6840
6841struct bfd_link_hash_table *
6842_bfd_elf_link_hash_table_create (bfd *abfd)
6843{
6844 struct elf_link_hash_table *ret;
6845 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6846
a50b1753 6847 ret = (struct elf_link_hash_table *) bfd_malloc (amt);
4d269e42
AM
6848 if (ret == NULL)
6849 return NULL;
6850
6851 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
6852 sizeof (struct elf_link_hash_entry),
6853 GENERIC_ELF_DATA))
4d269e42
AM
6854 {
6855 free (ret);
6856 return NULL;
6857 }
6858
6859 return &ret->root;
6860}
6861
6862/* This is a hook for the ELF emulation code in the generic linker to
6863 tell the backend linker what file name to use for the DT_NEEDED
6864 entry for a dynamic object. */
6865
6866void
6867bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6868{
6869 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6870 && bfd_get_format (abfd) == bfd_object)
6871 elf_dt_name (abfd) = name;
6872}
6873
6874int
6875bfd_elf_get_dyn_lib_class (bfd *abfd)
6876{
6877 int lib_class;
6878 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6879 && bfd_get_format (abfd) == bfd_object)
6880 lib_class = elf_dyn_lib_class (abfd);
6881 else
6882 lib_class = 0;
6883 return lib_class;
6884}
6885
6886void
6887bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6888{
6889 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6890 && bfd_get_format (abfd) == bfd_object)
6891 elf_dyn_lib_class (abfd) = lib_class;
6892}
6893
6894/* Get the list of DT_NEEDED entries for a link. This is a hook for
6895 the linker ELF emulation code. */
6896
6897struct bfd_link_needed_list *
6898bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6899 struct bfd_link_info *info)
6900{
6901 if (! is_elf_hash_table (info->hash))
6902 return NULL;
6903 return elf_hash_table (info)->needed;
6904}
6905
6906/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6907 hook for the linker ELF emulation code. */
6908
6909struct bfd_link_needed_list *
6910bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6911 struct bfd_link_info *info)
6912{
6913 if (! is_elf_hash_table (info->hash))
6914 return NULL;
6915 return elf_hash_table (info)->runpath;
6916}
6917
6918/* Get the name actually used for a dynamic object for a link. This
6919 is the SONAME entry if there is one. Otherwise, it is the string
6920 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6921
6922const char *
6923bfd_elf_get_dt_soname (bfd *abfd)
6924{
6925 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6926 && bfd_get_format (abfd) == bfd_object)
6927 return elf_dt_name (abfd);
6928 return NULL;
6929}
6930
6931/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6932 the ELF linker emulation code. */
6933
6934bfd_boolean
6935bfd_elf_get_bfd_needed_list (bfd *abfd,
6936 struct bfd_link_needed_list **pneeded)
6937{
6938 asection *s;
6939 bfd_byte *dynbuf = NULL;
cb33740c 6940 unsigned int elfsec;
4d269e42
AM
6941 unsigned long shlink;
6942 bfd_byte *extdyn, *extdynend;
6943 size_t extdynsize;
6944 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6945
6946 *pneeded = NULL;
6947
6948 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6949 || bfd_get_format (abfd) != bfd_object)
6950 return TRUE;
6951
6952 s = bfd_get_section_by_name (abfd, ".dynamic");
6953 if (s == NULL || s->size == 0)
6954 return TRUE;
6955
6956 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6957 goto error_return;
6958
6959 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 6960 if (elfsec == SHN_BAD)
4d269e42
AM
6961 goto error_return;
6962
6963 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 6964
4d269e42
AM
6965 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6966 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6967
6968 extdyn = dynbuf;
6969 extdynend = extdyn + s->size;
6970 for (; extdyn < extdynend; extdyn += extdynsize)
6971 {
6972 Elf_Internal_Dyn dyn;
6973
6974 (*swap_dyn_in) (abfd, extdyn, &dyn);
6975
6976 if (dyn.d_tag == DT_NULL)
6977 break;
6978
6979 if (dyn.d_tag == DT_NEEDED)
6980 {
6981 const char *string;
6982 struct bfd_link_needed_list *l;
6983 unsigned int tagv = dyn.d_un.d_val;
6984 bfd_size_type amt;
6985
6986 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
6987 if (string == NULL)
6988 goto error_return;
6989
6990 amt = sizeof *l;
a50b1753 6991 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
6992 if (l == NULL)
6993 goto error_return;
6994
6995 l->by = abfd;
6996 l->name = string;
6997 l->next = *pneeded;
6998 *pneeded = l;
6999 }
7000 }
7001
7002 free (dynbuf);
7003
7004 return TRUE;
7005
7006 error_return:
7007 if (dynbuf != NULL)
7008 free (dynbuf);
7009 return FALSE;
7010}
7011
7012struct elf_symbuf_symbol
7013{
7014 unsigned long st_name; /* Symbol name, index in string tbl */
7015 unsigned char st_info; /* Type and binding attributes */
7016 unsigned char st_other; /* Visibilty, and target specific */
7017};
7018
7019struct elf_symbuf_head
7020{
7021 struct elf_symbuf_symbol *ssym;
7022 bfd_size_type count;
7023 unsigned int st_shndx;
7024};
7025
7026struct elf_symbol
7027{
7028 union
7029 {
7030 Elf_Internal_Sym *isym;
7031 struct elf_symbuf_symbol *ssym;
7032 } u;
7033 const char *name;
7034};
7035
7036/* Sort references to symbols by ascending section number. */
7037
7038static int
7039elf_sort_elf_symbol (const void *arg1, const void *arg2)
7040{
7041 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7042 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7043
7044 return s1->st_shndx - s2->st_shndx;
7045}
7046
7047static int
7048elf_sym_name_compare (const void *arg1, const void *arg2)
7049{
7050 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7051 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7052 return strcmp (s1->name, s2->name);
7053}
7054
7055static struct elf_symbuf_head *
7056elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7057{
14b1c01e 7058 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7059 struct elf_symbuf_symbol *ssym;
7060 struct elf_symbuf_head *ssymbuf, *ssymhead;
3ae181ee 7061 bfd_size_type i, shndx_count, total_size;
4d269e42 7062
a50b1753 7063 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7064 if (indbuf == NULL)
7065 return NULL;
7066
7067 for (ind = indbuf, i = 0; i < symcount; i++)
7068 if (isymbuf[i].st_shndx != SHN_UNDEF)
7069 *ind++ = &isymbuf[i];
7070 indbufend = ind;
7071
7072 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7073 elf_sort_elf_symbol);
7074
7075 shndx_count = 0;
7076 if (indbufend > indbuf)
7077 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7078 if (ind[0]->st_shndx != ind[1]->st_shndx)
7079 shndx_count++;
7080
3ae181ee
L
7081 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7082 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7083 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7084 if (ssymbuf == NULL)
7085 {
7086 free (indbuf);
7087 return NULL;
7088 }
7089
3ae181ee 7090 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7091 ssymbuf->ssym = NULL;
7092 ssymbuf->count = shndx_count;
7093 ssymbuf->st_shndx = 0;
7094 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7095 {
7096 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7097 {
7098 ssymhead++;
7099 ssymhead->ssym = ssym;
7100 ssymhead->count = 0;
7101 ssymhead->st_shndx = (*ind)->st_shndx;
7102 }
7103 ssym->st_name = (*ind)->st_name;
7104 ssym->st_info = (*ind)->st_info;
7105 ssym->st_other = (*ind)->st_other;
7106 ssymhead->count++;
7107 }
3ae181ee
L
7108 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7109 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7110 == total_size));
4d269e42
AM
7111
7112 free (indbuf);
7113 return ssymbuf;
7114}
7115
7116/* Check if 2 sections define the same set of local and global
7117 symbols. */
7118
8f317e31 7119static bfd_boolean
4d269e42
AM
7120bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7121 struct bfd_link_info *info)
7122{
7123 bfd *bfd1, *bfd2;
7124 const struct elf_backend_data *bed1, *bed2;
7125 Elf_Internal_Shdr *hdr1, *hdr2;
7126 bfd_size_type symcount1, symcount2;
7127 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7128 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7129 Elf_Internal_Sym *isym, *isymend;
7130 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7131 bfd_size_type count1, count2, i;
cb33740c 7132 unsigned int shndx1, shndx2;
4d269e42
AM
7133 bfd_boolean result;
7134
7135 bfd1 = sec1->owner;
7136 bfd2 = sec2->owner;
7137
4d269e42
AM
7138 /* Both sections have to be in ELF. */
7139 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7140 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7141 return FALSE;
7142
7143 if (elf_section_type (sec1) != elf_section_type (sec2))
7144 return FALSE;
7145
4d269e42
AM
7146 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7147 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7148 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7149 return FALSE;
7150
7151 bed1 = get_elf_backend_data (bfd1);
7152 bed2 = get_elf_backend_data (bfd2);
7153 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7154 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7155 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7156 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7157
7158 if (symcount1 == 0 || symcount2 == 0)
7159 return FALSE;
7160
7161 result = FALSE;
7162 isymbuf1 = NULL;
7163 isymbuf2 = NULL;
a50b1753
NC
7164 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7165 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7166
7167 if (ssymbuf1 == NULL)
7168 {
7169 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7170 NULL, NULL, NULL);
7171 if (isymbuf1 == NULL)
7172 goto done;
7173
7174 if (!info->reduce_memory_overheads)
7175 elf_tdata (bfd1)->symbuf = ssymbuf1
7176 = elf_create_symbuf (symcount1, isymbuf1);
7177 }
7178
7179 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7180 {
7181 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7182 NULL, NULL, NULL);
7183 if (isymbuf2 == NULL)
7184 goto done;
7185
7186 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7187 elf_tdata (bfd2)->symbuf = ssymbuf2
7188 = elf_create_symbuf (symcount2, isymbuf2);
7189 }
7190
7191 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7192 {
7193 /* Optimized faster version. */
7194 bfd_size_type lo, hi, mid;
7195 struct elf_symbol *symp;
7196 struct elf_symbuf_symbol *ssym, *ssymend;
7197
7198 lo = 0;
7199 hi = ssymbuf1->count;
7200 ssymbuf1++;
7201 count1 = 0;
7202 while (lo < hi)
7203 {
7204 mid = (lo + hi) / 2;
cb33740c 7205 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7206 hi = mid;
cb33740c 7207 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7208 lo = mid + 1;
7209 else
7210 {
7211 count1 = ssymbuf1[mid].count;
7212 ssymbuf1 += mid;
7213 break;
7214 }
7215 }
7216
7217 lo = 0;
7218 hi = ssymbuf2->count;
7219 ssymbuf2++;
7220 count2 = 0;
7221 while (lo < hi)
7222 {
7223 mid = (lo + hi) / 2;
cb33740c 7224 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7225 hi = mid;
cb33740c 7226 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7227 lo = mid + 1;
7228 else
7229 {
7230 count2 = ssymbuf2[mid].count;
7231 ssymbuf2 += mid;
7232 break;
7233 }
7234 }
7235
7236 if (count1 == 0 || count2 == 0 || count1 != count2)
7237 goto done;
7238
a50b1753
NC
7239 symtable1 = (struct elf_symbol *)
7240 bfd_malloc (count1 * sizeof (struct elf_symbol));
7241 symtable2 = (struct elf_symbol *)
7242 bfd_malloc (count2 * sizeof (struct elf_symbol));
4d269e42
AM
7243 if (symtable1 == NULL || symtable2 == NULL)
7244 goto done;
7245
7246 symp = symtable1;
7247 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7248 ssym < ssymend; ssym++, symp++)
7249 {
7250 symp->u.ssym = ssym;
7251 symp->name = bfd_elf_string_from_elf_section (bfd1,
7252 hdr1->sh_link,
7253 ssym->st_name);
7254 }
7255
7256 symp = symtable2;
7257 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7258 ssym < ssymend; ssym++, symp++)
7259 {
7260 symp->u.ssym = ssym;
7261 symp->name = bfd_elf_string_from_elf_section (bfd2,
7262 hdr2->sh_link,
7263 ssym->st_name);
7264 }
7265
7266 /* Sort symbol by name. */
7267 qsort (symtable1, count1, sizeof (struct elf_symbol),
7268 elf_sym_name_compare);
7269 qsort (symtable2, count1, sizeof (struct elf_symbol),
7270 elf_sym_name_compare);
7271
7272 for (i = 0; i < count1; i++)
7273 /* Two symbols must have the same binding, type and name. */
7274 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7275 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7276 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7277 goto done;
7278
7279 result = TRUE;
7280 goto done;
7281 }
7282
a50b1753
NC
7283 symtable1 = (struct elf_symbol *)
7284 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7285 symtable2 = (struct elf_symbol *)
7286 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7287 if (symtable1 == NULL || symtable2 == NULL)
7288 goto done;
7289
7290 /* Count definitions in the section. */
7291 count1 = 0;
7292 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7293 if (isym->st_shndx == shndx1)
4d269e42
AM
7294 symtable1[count1++].u.isym = isym;
7295
7296 count2 = 0;
7297 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7298 if (isym->st_shndx == shndx2)
4d269e42
AM
7299 symtable2[count2++].u.isym = isym;
7300
7301 if (count1 == 0 || count2 == 0 || count1 != count2)
7302 goto done;
7303
7304 for (i = 0; i < count1; i++)
7305 symtable1[i].name
7306 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7307 symtable1[i].u.isym->st_name);
7308
7309 for (i = 0; i < count2; i++)
7310 symtable2[i].name
7311 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7312 symtable2[i].u.isym->st_name);
7313
7314 /* Sort symbol by name. */
7315 qsort (symtable1, count1, sizeof (struct elf_symbol),
7316 elf_sym_name_compare);
7317 qsort (symtable2, count1, sizeof (struct elf_symbol),
7318 elf_sym_name_compare);
7319
7320 for (i = 0; i < count1; i++)
7321 /* Two symbols must have the same binding, type and name. */
7322 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7323 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7324 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7325 goto done;
7326
7327 result = TRUE;
7328
7329done:
7330 if (symtable1)
7331 free (symtable1);
7332 if (symtable2)
7333 free (symtable2);
7334 if (isymbuf1)
7335 free (isymbuf1);
7336 if (isymbuf2)
7337 free (isymbuf2);
7338
7339 return result;
7340}
7341
7342/* Return TRUE if 2 section types are compatible. */
7343
7344bfd_boolean
7345_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7346 bfd *bbfd, const asection *bsec)
7347{
7348 if (asec == NULL
7349 || bsec == NULL
7350 || abfd->xvec->flavour != bfd_target_elf_flavour
7351 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7352 return TRUE;
7353
7354 return elf_section_type (asec) == elf_section_type (bsec);
7355}
7356\f
c152c796
AM
7357/* Final phase of ELF linker. */
7358
7359/* A structure we use to avoid passing large numbers of arguments. */
7360
7361struct elf_final_link_info
7362{
7363 /* General link information. */
7364 struct bfd_link_info *info;
7365 /* Output BFD. */
7366 bfd *output_bfd;
7367 /* Symbol string table. */
7368 struct bfd_strtab_hash *symstrtab;
7369 /* .dynsym section. */
7370 asection *dynsym_sec;
7371 /* .hash section. */
7372 asection *hash_sec;
7373 /* symbol version section (.gnu.version). */
7374 asection *symver_sec;
7375 /* Buffer large enough to hold contents of any section. */
7376 bfd_byte *contents;
7377 /* Buffer large enough to hold external relocs of any section. */
7378 void *external_relocs;
7379 /* Buffer large enough to hold internal relocs of any section. */
7380 Elf_Internal_Rela *internal_relocs;
7381 /* Buffer large enough to hold external local symbols of any input
7382 BFD. */
7383 bfd_byte *external_syms;
7384 /* And a buffer for symbol section indices. */
7385 Elf_External_Sym_Shndx *locsym_shndx;
7386 /* Buffer large enough to hold internal local symbols of any input
7387 BFD. */
7388 Elf_Internal_Sym *internal_syms;
7389 /* Array large enough to hold a symbol index for each local symbol
7390 of any input BFD. */
7391 long *indices;
7392 /* Array large enough to hold a section pointer for each local
7393 symbol of any input BFD. */
7394 asection **sections;
7395 /* Buffer to hold swapped out symbols. */
7396 bfd_byte *symbuf;
7397 /* And one for symbol section indices. */
7398 Elf_External_Sym_Shndx *symshndxbuf;
7399 /* Number of swapped out symbols in buffer. */
7400 size_t symbuf_count;
7401 /* Number of symbols which fit in symbuf. */
7402 size_t symbuf_size;
7403 /* And same for symshndxbuf. */
7404 size_t shndxbuf_size;
ffbc01cc
AM
7405 /* Number of STT_FILE syms seen. */
7406 size_t filesym_count;
c152c796
AM
7407};
7408
7409/* This struct is used to pass information to elf_link_output_extsym. */
7410
7411struct elf_outext_info
7412{
7413 bfd_boolean failed;
7414 bfd_boolean localsyms;
ffbc01cc
AM
7415 bfd_boolean need_second_pass;
7416 bfd_boolean second_pass;
8b127cbc 7417 struct elf_final_link_info *flinfo;
c152c796
AM
7418};
7419
d9352518
DB
7420
7421/* Support for evaluating a complex relocation.
7422
7423 Complex relocations are generalized, self-describing relocations. The
7424 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7425 relocations themselves.
d9352518
DB
7426
7427 The relocations are use a reserved elf-wide relocation type code (R_RELC
7428 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7429 information (start bit, end bit, word width, etc) into the addend. This
7430 information is extracted from CGEN-generated operand tables within gas.
7431
7432 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7433 internal) representing prefix-notation expressions, including but not
7434 limited to those sorts of expressions normally encoded as addends in the
7435 addend field. The symbol mangling format is:
7436
7437 <node> := <literal>
7438 | <unary-operator> ':' <node>
7439 | <binary-operator> ':' <node> ':' <node>
7440 ;
7441
7442 <literal> := 's' <digits=N> ':' <N character symbol name>
7443 | 'S' <digits=N> ':' <N character section name>
7444 | '#' <hexdigits>
7445 ;
7446
7447 <binary-operator> := as in C
7448 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7449
7450static void
a0c8462f
AM
7451set_symbol_value (bfd *bfd_with_globals,
7452 Elf_Internal_Sym *isymbuf,
7453 size_t locsymcount,
7454 size_t symidx,
7455 bfd_vma val)
d9352518 7456{
8977835c
AM
7457 struct elf_link_hash_entry **sym_hashes;
7458 struct elf_link_hash_entry *h;
7459 size_t extsymoff = locsymcount;
d9352518 7460
8977835c 7461 if (symidx < locsymcount)
d9352518 7462 {
8977835c
AM
7463 Elf_Internal_Sym *sym;
7464
7465 sym = isymbuf + symidx;
7466 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7467 {
7468 /* It is a local symbol: move it to the
7469 "absolute" section and give it a value. */
7470 sym->st_shndx = SHN_ABS;
7471 sym->st_value = val;
7472 return;
7473 }
7474 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7475 extsymoff = 0;
d9352518 7476 }
8977835c
AM
7477
7478 /* It is a global symbol: set its link type
7479 to "defined" and give it a value. */
7480
7481 sym_hashes = elf_sym_hashes (bfd_with_globals);
7482 h = sym_hashes [symidx - extsymoff];
7483 while (h->root.type == bfd_link_hash_indirect
7484 || h->root.type == bfd_link_hash_warning)
7485 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7486 h->root.type = bfd_link_hash_defined;
7487 h->root.u.def.value = val;
7488 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7489}
7490
a0c8462f
AM
7491static bfd_boolean
7492resolve_symbol (const char *name,
7493 bfd *input_bfd,
8b127cbc 7494 struct elf_final_link_info *flinfo,
a0c8462f
AM
7495 bfd_vma *result,
7496 Elf_Internal_Sym *isymbuf,
7497 size_t locsymcount)
d9352518 7498{
a0c8462f
AM
7499 Elf_Internal_Sym *sym;
7500 struct bfd_link_hash_entry *global_entry;
7501 const char *candidate = NULL;
7502 Elf_Internal_Shdr *symtab_hdr;
7503 size_t i;
7504
d9352518
DB
7505 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7506
7507 for (i = 0; i < locsymcount; ++ i)
7508 {
8977835c 7509 sym = isymbuf + i;
d9352518
DB
7510
7511 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7512 continue;
7513
7514 candidate = bfd_elf_string_from_elf_section (input_bfd,
7515 symtab_hdr->sh_link,
7516 sym->st_name);
7517#ifdef DEBUG
0f02bbd9
AM
7518 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7519 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7520#endif
7521 if (candidate && strcmp (candidate, name) == 0)
7522 {
8b127cbc 7523 asection *sec = flinfo->sections [i];
d9352518 7524
0f02bbd9
AM
7525 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7526 *result += sec->output_offset + sec->output_section->vma;
d9352518 7527#ifdef DEBUG
0f02bbd9
AM
7528 printf ("Found symbol with value %8.8lx\n",
7529 (unsigned long) *result);
d9352518
DB
7530#endif
7531 return TRUE;
7532 }
7533 }
7534
7535 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7536 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7537 FALSE, FALSE, TRUE);
d9352518
DB
7538 if (!global_entry)
7539 return FALSE;
a0c8462f 7540
d9352518
DB
7541 if (global_entry->type == bfd_link_hash_defined
7542 || global_entry->type == bfd_link_hash_defweak)
7543 {
a0c8462f
AM
7544 *result = (global_entry->u.def.value
7545 + global_entry->u.def.section->output_section->vma
7546 + global_entry->u.def.section->output_offset);
d9352518 7547#ifdef DEBUG
0f02bbd9
AM
7548 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7549 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7550#endif
7551 return TRUE;
a0c8462f 7552 }
d9352518 7553
d9352518
DB
7554 return FALSE;
7555}
7556
7557static bfd_boolean
a0c8462f
AM
7558resolve_section (const char *name,
7559 asection *sections,
7560 bfd_vma *result)
d9352518 7561{
a0c8462f
AM
7562 asection *curr;
7563 unsigned int len;
d9352518 7564
a0c8462f 7565 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7566 if (strcmp (curr->name, name) == 0)
7567 {
7568 *result = curr->vma;
7569 return TRUE;
7570 }
7571
7572 /* Hmm. still haven't found it. try pseudo-section names. */
a0c8462f 7573 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7574 {
7575 len = strlen (curr->name);
a0c8462f 7576 if (len > strlen (name))
d9352518
DB
7577 continue;
7578
7579 if (strncmp (curr->name, name, len) == 0)
7580 {
7581 if (strncmp (".end", name + len, 4) == 0)
7582 {
7583 *result = curr->vma + curr->size;
7584 return TRUE;
7585 }
7586
7587 /* Insert more pseudo-section names here, if you like. */
7588 }
7589 }
a0c8462f 7590
d9352518
DB
7591 return FALSE;
7592}
7593
7594static void
a0c8462f 7595undefined_reference (const char *reftype, const char *name)
d9352518 7596{
a0c8462f
AM
7597 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7598 reftype, name);
d9352518
DB
7599}
7600
7601static bfd_boolean
a0c8462f
AM
7602eval_symbol (bfd_vma *result,
7603 const char **symp,
7604 bfd *input_bfd,
8b127cbc 7605 struct elf_final_link_info *flinfo,
a0c8462f
AM
7606 bfd_vma dot,
7607 Elf_Internal_Sym *isymbuf,
7608 size_t locsymcount,
7609 int signed_p)
d9352518 7610{
4b93929b
NC
7611 size_t len;
7612 size_t symlen;
a0c8462f
AM
7613 bfd_vma a;
7614 bfd_vma b;
4b93929b 7615 char symbuf[4096];
0f02bbd9 7616 const char *sym = *symp;
a0c8462f
AM
7617 const char *symend;
7618 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7619
7620 len = strlen (sym);
7621 symend = sym + len;
7622
4b93929b 7623 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7624 {
7625 bfd_set_error (bfd_error_invalid_operation);
7626 return FALSE;
7627 }
a0c8462f 7628
d9352518
DB
7629 switch (* sym)
7630 {
7631 case '.':
0f02bbd9
AM
7632 *result = dot;
7633 *symp = sym + 1;
d9352518
DB
7634 return TRUE;
7635
7636 case '#':
0f02bbd9
AM
7637 ++sym;
7638 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7639 return TRUE;
7640
7641 case 'S':
7642 symbol_is_section = TRUE;
a0c8462f 7643 case 's':
0f02bbd9
AM
7644 ++sym;
7645 symlen = strtol (sym, (char **) symp, 10);
7646 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7647
4b93929b 7648 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7649 {
7650 bfd_set_error (bfd_error_invalid_operation);
7651 return FALSE;
7652 }
7653
7654 memcpy (symbuf, sym, symlen);
a0c8462f 7655 symbuf[symlen] = '\0';
0f02bbd9 7656 *symp = sym + symlen;
a0c8462f
AM
7657
7658 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7659 the symbol as a section, or vice-versa. so we're pretty liberal in our
7660 interpretation here; section means "try section first", not "must be a
7661 section", and likewise with symbol. */
7662
a0c8462f 7663 if (symbol_is_section)
d9352518 7664 {
8b127cbc
AM
7665 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7666 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7667 isymbuf, locsymcount))
d9352518
DB
7668 {
7669 undefined_reference ("section", symbuf);
7670 return FALSE;
7671 }
a0c8462f
AM
7672 }
7673 else
d9352518 7674 {
8b127cbc 7675 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7676 isymbuf, locsymcount)
8b127cbc 7677 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8977835c 7678 result))
d9352518
DB
7679 {
7680 undefined_reference ("symbol", symbuf);
7681 return FALSE;
7682 }
7683 }
7684
7685 return TRUE;
a0c8462f 7686
d9352518
DB
7687 /* All that remains are operators. */
7688
7689#define UNARY_OP(op) \
7690 if (strncmp (sym, #op, strlen (#op)) == 0) \
7691 { \
7692 sym += strlen (#op); \
a0c8462f
AM
7693 if (*sym == ':') \
7694 ++sym; \
0f02bbd9 7695 *symp = sym; \
8b127cbc 7696 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7697 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7698 return FALSE; \
7699 if (signed_p) \
0f02bbd9 7700 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7701 else \
7702 *result = op a; \
d9352518
DB
7703 return TRUE; \
7704 }
7705
7706#define BINARY_OP(op) \
7707 if (strncmp (sym, #op, strlen (#op)) == 0) \
7708 { \
7709 sym += strlen (#op); \
a0c8462f
AM
7710 if (*sym == ':') \
7711 ++sym; \
0f02bbd9 7712 *symp = sym; \
8b127cbc 7713 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7714 isymbuf, locsymcount, signed_p)) \
a0c8462f 7715 return FALSE; \
0f02bbd9 7716 ++*symp; \
8b127cbc 7717 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 7718 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7719 return FALSE; \
7720 if (signed_p) \
0f02bbd9 7721 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
7722 else \
7723 *result = a op b; \
d9352518
DB
7724 return TRUE; \
7725 }
7726
7727 default:
7728 UNARY_OP (0-);
7729 BINARY_OP (<<);
7730 BINARY_OP (>>);
7731 BINARY_OP (==);
7732 BINARY_OP (!=);
7733 BINARY_OP (<=);
7734 BINARY_OP (>=);
7735 BINARY_OP (&&);
7736 BINARY_OP (||);
7737 UNARY_OP (~);
7738 UNARY_OP (!);
7739 BINARY_OP (*);
7740 BINARY_OP (/);
7741 BINARY_OP (%);
7742 BINARY_OP (^);
7743 BINARY_OP (|);
7744 BINARY_OP (&);
7745 BINARY_OP (+);
7746 BINARY_OP (-);
7747 BINARY_OP (<);
7748 BINARY_OP (>);
7749#undef UNARY_OP
7750#undef BINARY_OP
7751 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7752 bfd_set_error (bfd_error_invalid_operation);
7753 return FALSE;
7754 }
7755}
7756
d9352518 7757static void
a0c8462f
AM
7758put_value (bfd_vma size,
7759 unsigned long chunksz,
7760 bfd *input_bfd,
7761 bfd_vma x,
7762 bfd_byte *location)
d9352518
DB
7763{
7764 location += (size - chunksz);
7765
a0c8462f 7766 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
d9352518
DB
7767 {
7768 switch (chunksz)
7769 {
7770 default:
7771 case 0:
7772 abort ();
7773 case 1:
7774 bfd_put_8 (input_bfd, x, location);
7775 break;
7776 case 2:
7777 bfd_put_16 (input_bfd, x, location);
7778 break;
7779 case 4:
7780 bfd_put_32 (input_bfd, x, location);
7781 break;
7782 case 8:
7783#ifdef BFD64
7784 bfd_put_64 (input_bfd, x, location);
7785#else
7786 abort ();
7787#endif
7788 break;
7789 }
7790 }
7791}
7792
a0c8462f
AM
7793static bfd_vma
7794get_value (bfd_vma size,
7795 unsigned long chunksz,
7796 bfd *input_bfd,
7797 bfd_byte *location)
d9352518
DB
7798{
7799 bfd_vma x = 0;
7800
a0c8462f 7801 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
7802 {
7803 switch (chunksz)
7804 {
7805 default:
7806 case 0:
7807 abort ();
7808 case 1:
7809 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7810 break;
7811 case 2:
7812 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7813 break;
7814 case 4:
7815 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7816 break;
7817 case 8:
7818#ifdef BFD64
7819 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7820#else
7821 abort ();
7822#endif
7823 break;
7824 }
7825 }
7826 return x;
7827}
7828
a0c8462f
AM
7829static void
7830decode_complex_addend (unsigned long *start, /* in bits */
7831 unsigned long *oplen, /* in bits */
7832 unsigned long *len, /* in bits */
7833 unsigned long *wordsz, /* in bytes */
7834 unsigned long *chunksz, /* in bytes */
7835 unsigned long *lsb0_p,
7836 unsigned long *signed_p,
7837 unsigned long *trunc_p,
7838 unsigned long encoded)
d9352518
DB
7839{
7840 * start = encoded & 0x3F;
7841 * len = (encoded >> 6) & 0x3F;
7842 * oplen = (encoded >> 12) & 0x3F;
7843 * wordsz = (encoded >> 18) & 0xF;
7844 * chunksz = (encoded >> 22) & 0xF;
7845 * lsb0_p = (encoded >> 27) & 1;
7846 * signed_p = (encoded >> 28) & 1;
7847 * trunc_p = (encoded >> 29) & 1;
7848}
7849
cdfeee4f 7850bfd_reloc_status_type
0f02bbd9 7851bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 7852 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
7853 bfd_byte *contents,
7854 Elf_Internal_Rela *rel,
7855 bfd_vma relocation)
d9352518 7856{
0f02bbd9
AM
7857 bfd_vma shift, x, mask;
7858 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 7859 bfd_reloc_status_type r;
d9352518
DB
7860
7861 /* Perform this reloc, since it is complex.
7862 (this is not to say that it necessarily refers to a complex
7863 symbol; merely that it is a self-describing CGEN based reloc.
7864 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 7865 word size, etc) encoded within it.). */
d9352518 7866
a0c8462f
AM
7867 decode_complex_addend (&start, &oplen, &len, &wordsz,
7868 &chunksz, &lsb0_p, &signed_p,
7869 &trunc_p, rel->r_addend);
d9352518
DB
7870
7871 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7872
7873 if (lsb0_p)
7874 shift = (start + 1) - len;
7875 else
7876 shift = (8 * wordsz) - (start + len);
7877
5dabe785 7878 /* FIXME: octets_per_byte. */
a0c8462f 7879 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
d9352518
DB
7880
7881#ifdef DEBUG
7882 printf ("Doing complex reloc: "
7883 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7884 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7885 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7886 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
7887 oplen, (unsigned long) x, (unsigned long) mask,
7888 (unsigned long) relocation);
d9352518
DB
7889#endif
7890
cdfeee4f 7891 r = bfd_reloc_ok;
d9352518 7892 if (! trunc_p)
cdfeee4f
AM
7893 /* Now do an overflow check. */
7894 r = bfd_check_overflow ((signed_p
7895 ? complain_overflow_signed
7896 : complain_overflow_unsigned),
7897 len, 0, (8 * wordsz),
7898 relocation);
a0c8462f 7899
d9352518
DB
7900 /* Do the deed. */
7901 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7902
7903#ifdef DEBUG
7904 printf (" relocation: %8.8lx\n"
7905 " shifted mask: %8.8lx\n"
7906 " shifted/masked reloc: %8.8lx\n"
7907 " result: %8.8lx\n",
9ccb8af9
AM
7908 (unsigned long) relocation, (unsigned long) (mask << shift),
7909 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 7910#endif
5dabe785 7911 /* FIXME: octets_per_byte. */
d9352518 7912 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
cdfeee4f 7913 return r;
d9352518
DB
7914}
7915
c152c796
AM
7916/* When performing a relocatable link, the input relocations are
7917 preserved. But, if they reference global symbols, the indices
d4730f92
BS
7918 referenced must be updated. Update all the relocations found in
7919 RELDATA. */
c152c796
AM
7920
7921static void
7922elf_link_adjust_relocs (bfd *abfd,
d4730f92 7923 struct bfd_elf_section_reloc_data *reldata)
c152c796
AM
7924{
7925 unsigned int i;
7926 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7927 bfd_byte *erela;
7928 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7929 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7930 bfd_vma r_type_mask;
7931 int r_sym_shift;
d4730f92
BS
7932 unsigned int count = reldata->count;
7933 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 7934
d4730f92 7935 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
7936 {
7937 swap_in = bed->s->swap_reloc_in;
7938 swap_out = bed->s->swap_reloc_out;
7939 }
d4730f92 7940 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
7941 {
7942 swap_in = bed->s->swap_reloca_in;
7943 swap_out = bed->s->swap_reloca_out;
7944 }
7945 else
7946 abort ();
7947
7948 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7949 abort ();
7950
7951 if (bed->s->arch_size == 32)
7952 {
7953 r_type_mask = 0xff;
7954 r_sym_shift = 8;
7955 }
7956 else
7957 {
7958 r_type_mask = 0xffffffff;
7959 r_sym_shift = 32;
7960 }
7961
d4730f92
BS
7962 erela = reldata->hdr->contents;
7963 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
7964 {
7965 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7966 unsigned int j;
7967
7968 if (*rel_hash == NULL)
7969 continue;
7970
7971 BFD_ASSERT ((*rel_hash)->indx >= 0);
7972
7973 (*swap_in) (abfd, erela, irela);
7974 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7975 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7976 | (irela[j].r_info & r_type_mask));
7977 (*swap_out) (abfd, irela, erela);
7978 }
7979}
7980
7981struct elf_link_sort_rela
7982{
7983 union {
7984 bfd_vma offset;
7985 bfd_vma sym_mask;
7986 } u;
7987 enum elf_reloc_type_class type;
7988 /* We use this as an array of size int_rels_per_ext_rel. */
7989 Elf_Internal_Rela rela[1];
7990};
7991
7992static int
7993elf_link_sort_cmp1 (const void *A, const void *B)
7994{
a50b1753
NC
7995 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
7996 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
7997 int relativea, relativeb;
7998
7999 relativea = a->type == reloc_class_relative;
8000 relativeb = b->type == reloc_class_relative;
8001
8002 if (relativea < relativeb)
8003 return 1;
8004 if (relativea > relativeb)
8005 return -1;
8006 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8007 return -1;
8008 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8009 return 1;
8010 if (a->rela->r_offset < b->rela->r_offset)
8011 return -1;
8012 if (a->rela->r_offset > b->rela->r_offset)
8013 return 1;
8014 return 0;
8015}
8016
8017static int
8018elf_link_sort_cmp2 (const void *A, const void *B)
8019{
a50b1753
NC
8020 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8021 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8022 int copya, copyb;
8023
8024 if (a->u.offset < b->u.offset)
8025 return -1;
8026 if (a->u.offset > b->u.offset)
8027 return 1;
8028 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
8029 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
8030 if (copya < copyb)
8031 return -1;
8032 if (copya > copyb)
8033 return 1;
8034 if (a->rela->r_offset < b->rela->r_offset)
8035 return -1;
8036 if (a->rela->r_offset > b->rela->r_offset)
8037 return 1;
8038 return 0;
8039}
8040
8041static size_t
8042elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8043{
3410fea8 8044 asection *dynamic_relocs;
fc66a176
L
8045 asection *rela_dyn;
8046 asection *rel_dyn;
c152c796
AM
8047 bfd_size_type count, size;
8048 size_t i, ret, sort_elt, ext_size;
8049 bfd_byte *sort, *s_non_relative, *p;
8050 struct elf_link_sort_rela *sq;
8051 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8052 int i2e = bed->s->int_rels_per_ext_rel;
8053 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8054 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8055 struct bfd_link_order *lo;
8056 bfd_vma r_sym_mask;
3410fea8 8057 bfd_boolean use_rela;
c152c796 8058
3410fea8
NC
8059 /* Find a dynamic reloc section. */
8060 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8061 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8062 if (rela_dyn != NULL && rela_dyn->size > 0
8063 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8064 {
3410fea8
NC
8065 bfd_boolean use_rela_initialised = FALSE;
8066
8067 /* This is just here to stop gcc from complaining.
8068 It's initialization checking code is not perfect. */
8069 use_rela = TRUE;
8070
8071 /* Both sections are present. Examine the sizes
8072 of the indirect sections to help us choose. */
8073 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8074 if (lo->type == bfd_indirect_link_order)
8075 {
8076 asection *o = lo->u.indirect.section;
8077
8078 if ((o->size % bed->s->sizeof_rela) == 0)
8079 {
8080 if ((o->size % bed->s->sizeof_rel) == 0)
8081 /* Section size is divisible by both rel and rela sizes.
8082 It is of no help to us. */
8083 ;
8084 else
8085 {
8086 /* Section size is only divisible by rela. */
8087 if (use_rela_initialised && (use_rela == FALSE))
8088 {
8089 _bfd_error_handler
8090 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8091 bfd_set_error (bfd_error_invalid_operation);
8092 return 0;
8093 }
8094 else
8095 {
8096 use_rela = TRUE;
8097 use_rela_initialised = TRUE;
8098 }
8099 }
8100 }
8101 else if ((o->size % bed->s->sizeof_rel) == 0)
8102 {
8103 /* Section size is only divisible by rel. */
8104 if (use_rela_initialised && (use_rela == TRUE))
8105 {
8106 _bfd_error_handler
8107 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8108 bfd_set_error (bfd_error_invalid_operation);
8109 return 0;
8110 }
8111 else
8112 {
8113 use_rela = FALSE;
8114 use_rela_initialised = TRUE;
8115 }
8116 }
8117 else
8118 {
8119 /* The section size is not divisible by either - something is wrong. */
8120 _bfd_error_handler
8121 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8122 bfd_set_error (bfd_error_invalid_operation);
8123 return 0;
8124 }
8125 }
8126
8127 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8128 if (lo->type == bfd_indirect_link_order)
8129 {
8130 asection *o = lo->u.indirect.section;
8131
8132 if ((o->size % bed->s->sizeof_rela) == 0)
8133 {
8134 if ((o->size % bed->s->sizeof_rel) == 0)
8135 /* Section size is divisible by both rel and rela sizes.
8136 It is of no help to us. */
8137 ;
8138 else
8139 {
8140 /* Section size is only divisible by rela. */
8141 if (use_rela_initialised && (use_rela == FALSE))
8142 {
8143 _bfd_error_handler
8144 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8145 bfd_set_error (bfd_error_invalid_operation);
8146 return 0;
8147 }
8148 else
8149 {
8150 use_rela = TRUE;
8151 use_rela_initialised = TRUE;
8152 }
8153 }
8154 }
8155 else if ((o->size % bed->s->sizeof_rel) == 0)
8156 {
8157 /* Section size is only divisible by rel. */
8158 if (use_rela_initialised && (use_rela == TRUE))
8159 {
8160 _bfd_error_handler
8161 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8162 bfd_set_error (bfd_error_invalid_operation);
8163 return 0;
8164 }
8165 else
8166 {
8167 use_rela = FALSE;
8168 use_rela_initialised = TRUE;
8169 }
8170 }
8171 else
8172 {
8173 /* The section size is not divisible by either - something is wrong. */
8174 _bfd_error_handler
8175 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8176 bfd_set_error (bfd_error_invalid_operation);
8177 return 0;
8178 }
8179 }
8180
8181 if (! use_rela_initialised)
8182 /* Make a guess. */
8183 use_rela = TRUE;
c152c796 8184 }
fc66a176
L
8185 else if (rela_dyn != NULL && rela_dyn->size > 0)
8186 use_rela = TRUE;
8187 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8188 use_rela = FALSE;
c152c796 8189 else
fc66a176 8190 return 0;
3410fea8
NC
8191
8192 if (use_rela)
c152c796 8193 {
3410fea8 8194 dynamic_relocs = rela_dyn;
c152c796
AM
8195 ext_size = bed->s->sizeof_rela;
8196 swap_in = bed->s->swap_reloca_in;
8197 swap_out = bed->s->swap_reloca_out;
8198 }
3410fea8
NC
8199 else
8200 {
8201 dynamic_relocs = rel_dyn;
8202 ext_size = bed->s->sizeof_rel;
8203 swap_in = bed->s->swap_reloc_in;
8204 swap_out = bed->s->swap_reloc_out;
8205 }
c152c796
AM
8206
8207 size = 0;
3410fea8 8208 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8209 if (lo->type == bfd_indirect_link_order)
3410fea8 8210 size += lo->u.indirect.section->size;
c152c796 8211
3410fea8 8212 if (size != dynamic_relocs->size)
c152c796
AM
8213 return 0;
8214
8215 sort_elt = (sizeof (struct elf_link_sort_rela)
8216 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8217
8218 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8219 if (count == 0)
8220 return 0;
a50b1753 8221 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8222
c152c796
AM
8223 if (sort == NULL)
8224 {
8225 (*info->callbacks->warning)
8226 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8227 return 0;
8228 }
8229
8230 if (bed->s->arch_size == 32)
8231 r_sym_mask = ~(bfd_vma) 0xff;
8232 else
8233 r_sym_mask = ~(bfd_vma) 0xffffffff;
8234
3410fea8 8235 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8236 if (lo->type == bfd_indirect_link_order)
8237 {
8238 bfd_byte *erel, *erelend;
8239 asection *o = lo->u.indirect.section;
8240
1da212d6
AM
8241 if (o->contents == NULL && o->size != 0)
8242 {
8243 /* This is a reloc section that is being handled as a normal
8244 section. See bfd_section_from_shdr. We can't combine
8245 relocs in this case. */
8246 free (sort);
8247 return 0;
8248 }
c152c796 8249 erel = o->contents;
eea6121a 8250 erelend = o->contents + o->size;
5dabe785 8251 /* FIXME: octets_per_byte. */
c152c796 8252 p = sort + o->output_offset / ext_size * sort_elt;
3410fea8 8253
c152c796
AM
8254 while (erel < erelend)
8255 {
8256 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8257
c152c796
AM
8258 (*swap_in) (abfd, erel, s->rela);
8259 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8260 s->u.sym_mask = r_sym_mask;
8261 p += sort_elt;
8262 erel += ext_size;
8263 }
8264 }
8265
8266 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8267
8268 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8269 {
8270 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8271 if (s->type != reloc_class_relative)
8272 break;
8273 }
8274 ret = i;
8275 s_non_relative = p;
8276
8277 sq = (struct elf_link_sort_rela *) s_non_relative;
8278 for (; i < count; i++, p += sort_elt)
8279 {
8280 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8281 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8282 sq = sp;
8283 sp->u.offset = sq->rela->r_offset;
8284 }
8285
8286 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8287
3410fea8 8288 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8289 if (lo->type == bfd_indirect_link_order)
8290 {
8291 bfd_byte *erel, *erelend;
8292 asection *o = lo->u.indirect.section;
8293
8294 erel = o->contents;
eea6121a 8295 erelend = o->contents + o->size;
5dabe785 8296 /* FIXME: octets_per_byte. */
c152c796
AM
8297 p = sort + o->output_offset / ext_size * sort_elt;
8298 while (erel < erelend)
8299 {
8300 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8301 (*swap_out) (abfd, s->rela, erel);
8302 p += sort_elt;
8303 erel += ext_size;
8304 }
8305 }
8306
8307 free (sort);
3410fea8 8308 *psec = dynamic_relocs;
c152c796
AM
8309 return ret;
8310}
8311
8312/* Flush the output symbols to the file. */
8313
8314static bfd_boolean
8b127cbc 8315elf_link_flush_output_syms (struct elf_final_link_info *flinfo,
c152c796
AM
8316 const struct elf_backend_data *bed)
8317{
8b127cbc 8318 if (flinfo->symbuf_count > 0)
c152c796
AM
8319 {
8320 Elf_Internal_Shdr *hdr;
8321 file_ptr pos;
8322 bfd_size_type amt;
8323
8b127cbc 8324 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
c152c796 8325 pos = hdr->sh_offset + hdr->sh_size;
8b127cbc
AM
8326 amt = flinfo->symbuf_count * bed->s->sizeof_sym;
8327 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) != 0
8328 || bfd_bwrite (flinfo->symbuf, amt, flinfo->output_bfd) != amt)
c152c796
AM
8329 return FALSE;
8330
8331 hdr->sh_size += amt;
8b127cbc 8332 flinfo->symbuf_count = 0;
c152c796
AM
8333 }
8334
8335 return TRUE;
8336}
8337
8338/* Add a symbol to the output symbol table. */
8339
6e0b88f1 8340static int
8b127cbc 8341elf_link_output_sym (struct elf_final_link_info *flinfo,
c152c796
AM
8342 const char *name,
8343 Elf_Internal_Sym *elfsym,
8344 asection *input_sec,
8345 struct elf_link_hash_entry *h)
8346{
8347 bfd_byte *dest;
8348 Elf_External_Sym_Shndx *destshndx;
6e0b88f1 8349 int (*output_symbol_hook)
c152c796
AM
8350 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8351 struct elf_link_hash_entry *);
8352 const struct elf_backend_data *bed;
8353
8b127cbc 8354 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
8355 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8356 if (output_symbol_hook != NULL)
8357 {
8b127cbc 8358 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
8359 if (ret != 1)
8360 return ret;
c152c796
AM
8361 }
8362
8363 if (name == NULL || *name == '\0')
8364 elfsym->st_name = 0;
8365 else if (input_sec->flags & SEC_EXCLUDE)
8366 elfsym->st_name = 0;
8367 else
8368 {
8b127cbc 8369 elfsym->st_name = (unsigned long) _bfd_stringtab_add (flinfo->symstrtab,
c152c796
AM
8370 name, TRUE, FALSE);
8371 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8372 return 0;
c152c796
AM
8373 }
8374
8b127cbc 8375 if (flinfo->symbuf_count >= flinfo->symbuf_size)
c152c796 8376 {
8b127cbc 8377 if (! elf_link_flush_output_syms (flinfo, bed))
6e0b88f1 8378 return 0;
c152c796
AM
8379 }
8380
8b127cbc
AM
8381 dest = flinfo->symbuf + flinfo->symbuf_count * bed->s->sizeof_sym;
8382 destshndx = flinfo->symshndxbuf;
c152c796
AM
8383 if (destshndx != NULL)
8384 {
8b127cbc 8385 if (bfd_get_symcount (flinfo->output_bfd) >= flinfo->shndxbuf_size)
c152c796
AM
8386 {
8387 bfd_size_type amt;
8388
8b127cbc 8389 amt = flinfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
a50b1753
NC
8390 destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx,
8391 amt * 2);
c152c796 8392 if (destshndx == NULL)
6e0b88f1 8393 return 0;
8b127cbc 8394 flinfo->symshndxbuf = destshndx;
c152c796 8395 memset ((char *) destshndx + amt, 0, amt);
8b127cbc 8396 flinfo->shndxbuf_size *= 2;
c152c796 8397 }
8b127cbc 8398 destshndx += bfd_get_symcount (flinfo->output_bfd);
c152c796
AM
8399 }
8400
8b127cbc
AM
8401 bed->s->swap_symbol_out (flinfo->output_bfd, elfsym, dest, destshndx);
8402 flinfo->symbuf_count += 1;
8403 bfd_get_symcount (flinfo->output_bfd) += 1;
c152c796 8404
6e0b88f1 8405 return 1;
c152c796
AM
8406}
8407
c0d5a53d
L
8408/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8409
8410static bfd_boolean
8411check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8412{
4fbb74a6
AM
8413 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8414 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
8415 {
8416 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 8417 beyond 64k. */
c0d5a53d
L
8418 (*_bfd_error_handler)
8419 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 8420 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
8421 bfd_set_error (bfd_error_nonrepresentable_section);
8422 return FALSE;
8423 }
8424 return TRUE;
8425}
8426
c152c796
AM
8427/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8428 allowing an unsatisfied unversioned symbol in the DSO to match a
8429 versioned symbol that would normally require an explicit version.
8430 We also handle the case that a DSO references a hidden symbol
8431 which may be satisfied by a versioned symbol in another DSO. */
8432
8433static bfd_boolean
8434elf_link_check_versioned_symbol (struct bfd_link_info *info,
8435 const struct elf_backend_data *bed,
8436 struct elf_link_hash_entry *h)
8437{
8438 bfd *abfd;
8439 struct elf_link_loaded_list *loaded;
8440
8441 if (!is_elf_hash_table (info->hash))
8442 return FALSE;
8443
8444 switch (h->root.type)
8445 {
8446 default:
8447 abfd = NULL;
8448 break;
8449
8450 case bfd_link_hash_undefined:
8451 case bfd_link_hash_undefweak:
8452 abfd = h->root.u.undef.abfd;
8453 if ((abfd->flags & DYNAMIC) == 0
e56f61be 8454 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
8455 return FALSE;
8456 break;
8457
8458 case bfd_link_hash_defined:
8459 case bfd_link_hash_defweak:
8460 abfd = h->root.u.def.section->owner;
8461 break;
8462
8463 case bfd_link_hash_common:
8464 abfd = h->root.u.c.p->section->owner;
8465 break;
8466 }
8467 BFD_ASSERT (abfd != NULL);
8468
8469 for (loaded = elf_hash_table (info)->loaded;
8470 loaded != NULL;
8471 loaded = loaded->next)
8472 {
8473 bfd *input;
8474 Elf_Internal_Shdr *hdr;
8475 bfd_size_type symcount;
8476 bfd_size_type extsymcount;
8477 bfd_size_type extsymoff;
8478 Elf_Internal_Shdr *versymhdr;
8479 Elf_Internal_Sym *isym;
8480 Elf_Internal_Sym *isymend;
8481 Elf_Internal_Sym *isymbuf;
8482 Elf_External_Versym *ever;
8483 Elf_External_Versym *extversym;
8484
8485 input = loaded->abfd;
8486
8487 /* We check each DSO for a possible hidden versioned definition. */
8488 if (input == abfd
8489 || (input->flags & DYNAMIC) == 0
8490 || elf_dynversym (input) == 0)
8491 continue;
8492
8493 hdr = &elf_tdata (input)->dynsymtab_hdr;
8494
8495 symcount = hdr->sh_size / bed->s->sizeof_sym;
8496 if (elf_bad_symtab (input))
8497 {
8498 extsymcount = symcount;
8499 extsymoff = 0;
8500 }
8501 else
8502 {
8503 extsymcount = symcount - hdr->sh_info;
8504 extsymoff = hdr->sh_info;
8505 }
8506
8507 if (extsymcount == 0)
8508 continue;
8509
8510 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8511 NULL, NULL, NULL);
8512 if (isymbuf == NULL)
8513 return FALSE;
8514
8515 /* Read in any version definitions. */
8516 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 8517 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
8518 if (extversym == NULL)
8519 goto error_ret;
8520
8521 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8522 || (bfd_bread (extversym, versymhdr->sh_size, input)
8523 != versymhdr->sh_size))
8524 {
8525 free (extversym);
8526 error_ret:
8527 free (isymbuf);
8528 return FALSE;
8529 }
8530
8531 ever = extversym + extsymoff;
8532 isymend = isymbuf + extsymcount;
8533 for (isym = isymbuf; isym < isymend; isym++, ever++)
8534 {
8535 const char *name;
8536 Elf_Internal_Versym iver;
8537 unsigned short version_index;
8538
8539 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8540 || isym->st_shndx == SHN_UNDEF)
8541 continue;
8542
8543 name = bfd_elf_string_from_elf_section (input,
8544 hdr->sh_link,
8545 isym->st_name);
8546 if (strcmp (name, h->root.root.string) != 0)
8547 continue;
8548
8549 _bfd_elf_swap_versym_in (input, ever, &iver);
8550
d023c380
L
8551 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8552 && !(h->def_regular
8553 && h->forced_local))
c152c796
AM
8554 {
8555 /* If we have a non-hidden versioned sym, then it should
d023c380
L
8556 have provided a definition for the undefined sym unless
8557 it is defined in a non-shared object and forced local.
8558 */
c152c796
AM
8559 abort ();
8560 }
8561
8562 version_index = iver.vs_vers & VERSYM_VERSION;
8563 if (version_index == 1 || version_index == 2)
8564 {
8565 /* This is the base or first version. We can use it. */
8566 free (extversym);
8567 free (isymbuf);
8568 return TRUE;
8569 }
8570 }
8571
8572 free (extversym);
8573 free (isymbuf);
8574 }
8575
8576 return FALSE;
8577}
8578
8579/* Add an external symbol to the symbol table. This is called from
8580 the hash table traversal routine. When generating a shared object,
8581 we go through the symbol table twice. The first time we output
8582 anything that might have been forced to local scope in a version
8583 script. The second time we output the symbols that are still
8584 global symbols. */
8585
8586static bfd_boolean
7686d77d 8587elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 8588{
7686d77d 8589 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 8590 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 8591 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
8592 bfd_boolean strip;
8593 Elf_Internal_Sym sym;
8594 asection *input_sec;
8595 const struct elf_backend_data *bed;
6e0b88f1
AM
8596 long indx;
8597 int ret;
c152c796
AM
8598
8599 if (h->root.type == bfd_link_hash_warning)
8600 {
8601 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8602 if (h->root.type == bfd_link_hash_new)
8603 return TRUE;
8604 }
8605
8606 /* Decide whether to output this symbol in this pass. */
8607 if (eoinfo->localsyms)
8608 {
f5385ebf 8609 if (!h->forced_local)
c152c796 8610 return TRUE;
ffbc01cc
AM
8611 if (eoinfo->second_pass
8612 && !((h->root.type == bfd_link_hash_defined
8613 || h->root.type == bfd_link_hash_defweak)
8614 && h->root.u.def.section->output_section != NULL))
8615 return TRUE;
c152c796
AM
8616 }
8617 else
8618 {
f5385ebf 8619 if (h->forced_local)
c152c796
AM
8620 return TRUE;
8621 }
8622
8b127cbc 8623 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 8624
12ac1cf5 8625 if (h->root.type == bfd_link_hash_undefined)
c152c796 8626 {
12ac1cf5
NC
8627 /* If we have an undefined symbol reference here then it must have
8628 come from a shared library that is being linked in. (Undefined
98da7939
L
8629 references in regular files have already been handled unless
8630 they are in unreferenced sections which are removed by garbage
8631 collection). */
12ac1cf5
NC
8632 bfd_boolean ignore_undef = FALSE;
8633
8634 /* Some symbols may be special in that the fact that they're
8635 undefined can be safely ignored - let backend determine that. */
8636 if (bed->elf_backend_ignore_undef_symbol)
8637 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8638
8639 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 8640 if (!ignore_undef
12ac1cf5 8641 && h->ref_dynamic
8b127cbc
AM
8642 && (!h->ref_regular || flinfo->info->gc_sections)
8643 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
8644 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8645 {
8646 if (!(flinfo->info->callbacks->undefined_symbol
8647 (flinfo->info, h->root.root.string,
8648 h->ref_regular ? NULL : h->root.u.undef.abfd,
8649 NULL, 0,
8650 (flinfo->info->unresolved_syms_in_shared_libs
8651 == RM_GENERATE_ERROR))))
12ac1cf5 8652 {
17d078c5 8653 bfd_set_error (bfd_error_bad_value);
12ac1cf5
NC
8654 eoinfo->failed = TRUE;
8655 return FALSE;
8656 }
c152c796
AM
8657 }
8658 }
8659
8660 /* We should also warn if a forced local symbol is referenced from
8661 shared libraries. */
8b127cbc
AM
8662 if (!flinfo->info->relocatable
8663 && flinfo->info->executable
f5385ebf
AM
8664 && h->forced_local
8665 && h->ref_dynamic
371a5866 8666 && h->def_regular
f5385ebf
AM
8667 && !h->dynamic_def
8668 && !h->dynamic_weak
8b127cbc 8669 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 8670 {
17d078c5
AM
8671 bfd *def_bfd;
8672 const char *msg;
8673
8674 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
8675 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
8676 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
8677 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
8678 else
8679 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 8680 def_bfd = flinfo->output_bfd;
17d078c5
AM
8681 if (h->root.u.def.section != bfd_abs_section_ptr)
8682 def_bfd = h->root.u.def.section->owner;
8b127cbc 8683 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
17d078c5
AM
8684 h->root.root.string);
8685 bfd_set_error (bfd_error_bad_value);
c152c796
AM
8686 eoinfo->failed = TRUE;
8687 return FALSE;
8688 }
8689
8690 /* We don't want to output symbols that have never been mentioned by
8691 a regular file, or that we have been told to strip. However, if
8692 h->indx is set to -2, the symbol is used by a reloc and we must
8693 output it. */
8694 if (h->indx == -2)
8695 strip = FALSE;
f5385ebf 8696 else if ((h->def_dynamic
77cfaee6
AM
8697 || h->ref_dynamic
8698 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
8699 && !h->def_regular
8700 && !h->ref_regular)
c152c796 8701 strip = TRUE;
8b127cbc 8702 else if (flinfo->info->strip == strip_all)
c152c796 8703 strip = TRUE;
8b127cbc
AM
8704 else if (flinfo->info->strip == strip_some
8705 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
8706 h->root.root.string, FALSE, FALSE) == NULL)
8707 strip = TRUE;
d56d55e7
AM
8708 else if ((h->root.type == bfd_link_hash_defined
8709 || h->root.type == bfd_link_hash_defweak)
8b127cbc 8710 && ((flinfo->info->strip_discarded
dbaa2011 8711 && discarded_section (h->root.u.def.section))
d56d55e7
AM
8712 || (h->root.u.def.section->owner != NULL
8713 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 8714 strip = TRUE;
9e2278f5
AM
8715 else if ((h->root.type == bfd_link_hash_undefined
8716 || h->root.type == bfd_link_hash_undefweak)
8717 && h->root.u.undef.abfd != NULL
8718 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
8719 strip = TRUE;
c152c796
AM
8720 else
8721 strip = FALSE;
8722
8723 /* If we're stripping it, and it's not a dynamic symbol, there's
57ca8ac7
L
8724 nothing else to do unless it is a forced local symbol or a
8725 STT_GNU_IFUNC symbol. */
c152c796
AM
8726 if (strip
8727 && h->dynindx == -1
57ca8ac7 8728 && h->type != STT_GNU_IFUNC
f5385ebf 8729 && !h->forced_local)
c152c796
AM
8730 return TRUE;
8731
8732 sym.st_value = 0;
8733 sym.st_size = h->size;
8734 sym.st_other = h->other;
f5385ebf 8735 if (h->forced_local)
935bd1e0
L
8736 {
8737 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8738 /* Turn off visibility on local symbol. */
8739 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
8740 }
3e7a7d11
NC
8741 else if (h->unique_global)
8742 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
c152c796
AM
8743 else if (h->root.type == bfd_link_hash_undefweak
8744 || h->root.type == bfd_link_hash_defweak)
8745 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8746 else
8747 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
35fc36a8 8748 sym.st_target_internal = h->target_internal;
c152c796
AM
8749
8750 switch (h->root.type)
8751 {
8752 default:
8753 case bfd_link_hash_new:
8754 case bfd_link_hash_warning:
8755 abort ();
8756 return FALSE;
8757
8758 case bfd_link_hash_undefined:
8759 case bfd_link_hash_undefweak:
8760 input_sec = bfd_und_section_ptr;
8761 sym.st_shndx = SHN_UNDEF;
8762 break;
8763
8764 case bfd_link_hash_defined:
8765 case bfd_link_hash_defweak:
8766 {
8767 input_sec = h->root.u.def.section;
8768 if (input_sec->output_section != NULL)
8769 {
ffbc01cc
AM
8770 if (eoinfo->localsyms && flinfo->filesym_count == 1)
8771 {
8772 bfd_boolean second_pass_sym
8773 = (input_sec->owner == flinfo->output_bfd
8774 || input_sec->owner == NULL
8775 || (input_sec->flags & SEC_LINKER_CREATED) != 0
8776 || (input_sec->owner->flags & BFD_LINKER_CREATED) != 0);
8777
8778 eoinfo->need_second_pass |= second_pass_sym;
8779 if (eoinfo->second_pass != second_pass_sym)
8780 return TRUE;
8781 }
8782
c152c796 8783 sym.st_shndx =
8b127cbc 8784 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
8785 input_sec->output_section);
8786 if (sym.st_shndx == SHN_BAD)
8787 {
8788 (*_bfd_error_handler)
d003868e 8789 (_("%B: could not find output section %A for input section %A"),
8b127cbc 8790 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 8791 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
8792 eoinfo->failed = TRUE;
8793 return FALSE;
8794 }
8795
8796 /* ELF symbols in relocatable files are section relative,
8797 but in nonrelocatable files they are virtual
8798 addresses. */
8799 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8b127cbc 8800 if (!flinfo->info->relocatable)
c152c796
AM
8801 {
8802 sym.st_value += input_sec->output_section->vma;
8803 if (h->type == STT_TLS)
8804 {
8b127cbc 8805 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
8806 if (tls_sec != NULL)
8807 sym.st_value -= tls_sec->vma;
8808 else
8809 {
8810 /* The TLS section may have been garbage collected. */
8b127cbc 8811 BFD_ASSERT (flinfo->info->gc_sections
430a16a5
NC
8812 && !input_sec->gc_mark);
8813 }
c152c796
AM
8814 }
8815 }
8816 }
8817 else
8818 {
8819 BFD_ASSERT (input_sec->owner == NULL
8820 || (input_sec->owner->flags & DYNAMIC) != 0);
8821 sym.st_shndx = SHN_UNDEF;
8822 input_sec = bfd_und_section_ptr;
8823 }
8824 }
8825 break;
8826
8827 case bfd_link_hash_common:
8828 input_sec = h->root.u.c.p->section;
a4d8e49b 8829 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
8830 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8831 break;
8832
8833 case bfd_link_hash_indirect:
8834 /* These symbols are created by symbol versioning. They point
8835 to the decorated version of the name. For example, if the
8836 symbol foo@@GNU_1.2 is the default, which should be used when
8837 foo is used with no version, then we add an indirect symbol
8838 foo which points to foo@@GNU_1.2. We ignore these symbols,
8839 since the indirected symbol is already in the hash table. */
8840 return TRUE;
8841 }
8842
8843 /* Give the processor backend a chance to tweak the symbol value,
8844 and also to finish up anything that needs to be done for this
8845 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 8846 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 8847 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 8848 if ((h->type == STT_GNU_IFUNC
5f35ea9c 8849 && h->def_regular
8b127cbc 8850 && !flinfo->info->relocatable)
3aa14d16
L
8851 || ((h->dynindx != -1
8852 || h->forced_local)
8b127cbc 8853 && ((flinfo->info->shared
3aa14d16
L
8854 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8855 || h->root.type != bfd_link_hash_undefweak))
8856 || !h->forced_local)
8b127cbc 8857 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
8858 {
8859 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 8860 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
8861 {
8862 eoinfo->failed = TRUE;
8863 return FALSE;
8864 }
8865 }
8866
8867 /* If we are marking the symbol as undefined, and there are no
8868 non-weak references to this symbol from a regular object, then
8869 mark the symbol as weak undefined; if there are non-weak
8870 references, mark the symbol as strong. We can't do this earlier,
8871 because it might not be marked as undefined until the
8872 finish_dynamic_symbol routine gets through with it. */
8873 if (sym.st_shndx == SHN_UNDEF
f5385ebf 8874 && h->ref_regular
c152c796
AM
8875 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8876 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8877 {
8878 int bindtype;
2955ec4c
L
8879 unsigned int type = ELF_ST_TYPE (sym.st_info);
8880
8881 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
8882 if (type == STT_GNU_IFUNC)
8883 type = STT_FUNC;
c152c796 8884
f5385ebf 8885 if (h->ref_regular_nonweak)
c152c796
AM
8886 bindtype = STB_GLOBAL;
8887 else
8888 bindtype = STB_WEAK;
2955ec4c 8889 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
8890 }
8891
bda987c2
CD
8892 /* If this is a symbol defined in a dynamic library, don't use the
8893 symbol size from the dynamic library. Relinking an executable
8894 against a new library may introduce gratuitous changes in the
8895 executable's symbols if we keep the size. */
8896 if (sym.st_shndx == SHN_UNDEF
8897 && !h->def_regular
8898 && h->def_dynamic)
8899 sym.st_size = 0;
8900
c152c796
AM
8901 /* If a non-weak symbol with non-default visibility is not defined
8902 locally, it is a fatal error. */
8b127cbc 8903 if (!flinfo->info->relocatable
c152c796
AM
8904 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8905 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8906 && h->root.type == bfd_link_hash_undefined
f5385ebf 8907 && !h->def_regular)
c152c796 8908 {
17d078c5
AM
8909 const char *msg;
8910
8911 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
8912 msg = _("%B: protected symbol `%s' isn't defined");
8913 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
8914 msg = _("%B: internal symbol `%s' isn't defined");
8915 else
8916 msg = _("%B: hidden symbol `%s' isn't defined");
8b127cbc 8917 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 8918 bfd_set_error (bfd_error_bad_value);
c152c796
AM
8919 eoinfo->failed = TRUE;
8920 return FALSE;
8921 }
8922
8923 /* If this symbol should be put in the .dynsym section, then put it
8924 there now. We already know the symbol index. We also fill in
8925 the entry in the .hash section. */
8b127cbc 8926 if (flinfo->dynsym_sec != NULL
202e2356 8927 && h->dynindx != -1
8b127cbc 8928 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 8929 {
c152c796
AM
8930 bfd_byte *esym;
8931
8932 sym.st_name = h->dynstr_index;
8b127cbc
AM
8933 esym = flinfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
8934 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
8935 {
8936 eoinfo->failed = TRUE;
8937 return FALSE;
8938 }
8b127cbc 8939 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 8940
8b127cbc 8941 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
8942 {
8943 size_t hash_entry_size;
8944 bfd_byte *bucketpos;
8945 bfd_vma chain;
41198d0c
L
8946 size_t bucketcount;
8947 size_t bucket;
8948
8b127cbc 8949 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 8950 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
8951
8952 hash_entry_size
8b127cbc
AM
8953 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
8954 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 8955 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
8956 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
8957 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
8958 bucketpos);
8959 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
8960 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
8961 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8962 }
c152c796 8963
8b127cbc 8964 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
8965 {
8966 Elf_Internal_Versym iversym;
8967 Elf_External_Versym *eversym;
8968
f5385ebf 8969 if (!h->def_regular)
c152c796
AM
8970 {
8971 if (h->verinfo.verdef == NULL)
8972 iversym.vs_vers = 0;
8973 else
8974 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8975 }
8976 else
8977 {
8978 if (h->verinfo.vertree == NULL)
8979 iversym.vs_vers = 1;
8980 else
8981 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 8982 if (flinfo->info->create_default_symver)
3e3b46e5 8983 iversym.vs_vers++;
c152c796
AM
8984 }
8985
f5385ebf 8986 if (h->hidden)
c152c796
AM
8987 iversym.vs_vers |= VERSYM_HIDDEN;
8988
8b127cbc 8989 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 8990 eversym += h->dynindx;
8b127cbc 8991 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
8992 }
8993 }
8994
8995 /* If we're stripping it, then it was just a dynamic symbol, and
8996 there's nothing else to do. */
8997 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8998 return TRUE;
8999
8b127cbc
AM
9000 indx = bfd_get_symcount (flinfo->output_bfd);
9001 ret = elf_link_output_sym (flinfo, h->root.root.string, &sym, input_sec, h);
6e0b88f1 9002 if (ret == 0)
c152c796
AM
9003 {
9004 eoinfo->failed = TRUE;
9005 return FALSE;
9006 }
6e0b88f1
AM
9007 else if (ret == 1)
9008 h->indx = indx;
9009 else if (h->indx == -2)
9010 abort();
c152c796
AM
9011
9012 return TRUE;
9013}
9014
cdd3575c
AM
9015/* Return TRUE if special handling is done for relocs in SEC against
9016 symbols defined in discarded sections. */
9017
c152c796
AM
9018static bfd_boolean
9019elf_section_ignore_discarded_relocs (asection *sec)
9020{
9021 const struct elf_backend_data *bed;
9022
cdd3575c
AM
9023 switch (sec->sec_info_type)
9024 {
dbaa2011
AM
9025 case SEC_INFO_TYPE_STABS:
9026 case SEC_INFO_TYPE_EH_FRAME:
cdd3575c
AM
9027 return TRUE;
9028 default:
9029 break;
9030 }
c152c796
AM
9031
9032 bed = get_elf_backend_data (sec->owner);
9033 if (bed->elf_backend_ignore_discarded_relocs != NULL
9034 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9035 return TRUE;
9036
9037 return FALSE;
9038}
9039
9e66c942
AM
9040/* Return a mask saying how ld should treat relocations in SEC against
9041 symbols defined in discarded sections. If this function returns
9042 COMPLAIN set, ld will issue a warning message. If this function
9043 returns PRETEND set, and the discarded section was link-once and the
9044 same size as the kept link-once section, ld will pretend that the
9045 symbol was actually defined in the kept section. Otherwise ld will
9046 zero the reloc (at least that is the intent, but some cooperation by
9047 the target dependent code is needed, particularly for REL targets). */
9048
8a696751
AM
9049unsigned int
9050_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9051{
9e66c942 9052 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9053 return PRETEND;
cdd3575c
AM
9054
9055 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9056 return 0;
cdd3575c
AM
9057
9058 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9059 return 0;
cdd3575c 9060
9e66c942 9061 return COMPLAIN | PRETEND;
cdd3575c
AM
9062}
9063
3d7f7666
L
9064/* Find a match between a section and a member of a section group. */
9065
9066static asection *
c0f00686
L
9067match_group_member (asection *sec, asection *group,
9068 struct bfd_link_info *info)
3d7f7666
L
9069{
9070 asection *first = elf_next_in_group (group);
9071 asection *s = first;
9072
9073 while (s != NULL)
9074 {
c0f00686 9075 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9076 return s;
9077
83180ade 9078 s = elf_next_in_group (s);
3d7f7666
L
9079 if (s == first)
9080 break;
9081 }
9082
9083 return NULL;
9084}
9085
01b3c8ab 9086/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9087 to replace it. Return the replacement if it is OK. Otherwise return
9088 NULL. */
01b3c8ab
L
9089
9090asection *
c0f00686 9091_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9092{
9093 asection *kept;
9094
9095 kept = sec->kept_section;
9096 if (kept != NULL)
9097 {
c2370991 9098 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9099 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9100 if (kept != NULL
9101 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9102 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9103 kept = NULL;
c2370991 9104 sec->kept_section = kept;
01b3c8ab
L
9105 }
9106 return kept;
9107}
9108
c152c796
AM
9109/* Link an input file into the linker output file. This function
9110 handles all the sections and relocations of the input file at once.
9111 This is so that we only have to read the local symbols once, and
9112 don't have to keep them in memory. */
9113
9114static bfd_boolean
8b127cbc 9115elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9116{
ece5ef60 9117 int (*relocate_section)
c152c796
AM
9118 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9119 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9120 bfd *output_bfd;
9121 Elf_Internal_Shdr *symtab_hdr;
9122 size_t locsymcount;
9123 size_t extsymoff;
9124 Elf_Internal_Sym *isymbuf;
9125 Elf_Internal_Sym *isym;
9126 Elf_Internal_Sym *isymend;
9127 long *pindex;
9128 asection **ppsection;
9129 asection *o;
9130 const struct elf_backend_data *bed;
c152c796 9131 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9132 bfd_size_type address_size;
9133 bfd_vma r_type_mask;
9134 int r_sym_shift;
ffbc01cc 9135 bfd_boolean have_file_sym = FALSE;
c152c796 9136
8b127cbc 9137 output_bfd = flinfo->output_bfd;
c152c796
AM
9138 bed = get_elf_backend_data (output_bfd);
9139 relocate_section = bed->elf_backend_relocate_section;
9140
9141 /* If this is a dynamic object, we don't want to do anything here:
9142 we don't want the local symbols, and we don't want the section
9143 contents. */
9144 if ((input_bfd->flags & DYNAMIC) != 0)
9145 return TRUE;
9146
c152c796
AM
9147 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9148 if (elf_bad_symtab (input_bfd))
9149 {
9150 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9151 extsymoff = 0;
9152 }
9153 else
9154 {
9155 locsymcount = symtab_hdr->sh_info;
9156 extsymoff = symtab_hdr->sh_info;
9157 }
9158
9159 /* Read the local symbols. */
9160 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9161 if (isymbuf == NULL && locsymcount != 0)
9162 {
9163 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9164 flinfo->internal_syms,
9165 flinfo->external_syms,
9166 flinfo->locsym_shndx);
c152c796
AM
9167 if (isymbuf == NULL)
9168 return FALSE;
9169 }
9170
9171 /* Find local symbol sections and adjust values of symbols in
9172 SEC_MERGE sections. Write out those local symbols we know are
9173 going into the output file. */
9174 isymend = isymbuf + locsymcount;
8b127cbc 9175 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9176 isym < isymend;
9177 isym++, pindex++, ppsection++)
9178 {
9179 asection *isec;
9180 const char *name;
9181 Elf_Internal_Sym osym;
6e0b88f1
AM
9182 long indx;
9183 int ret;
c152c796
AM
9184
9185 *pindex = -1;
9186
9187 if (elf_bad_symtab (input_bfd))
9188 {
9189 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9190 {
9191 *ppsection = NULL;
9192 continue;
9193 }
9194 }
9195
9196 if (isym->st_shndx == SHN_UNDEF)
9197 isec = bfd_und_section_ptr;
c152c796
AM
9198 else if (isym->st_shndx == SHN_ABS)
9199 isec = bfd_abs_section_ptr;
9200 else if (isym->st_shndx == SHN_COMMON)
9201 isec = bfd_com_section_ptr;
9202 else
9203 {
cb33740c
AM
9204 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9205 if (isec == NULL)
9206 {
9207 /* Don't attempt to output symbols with st_shnx in the
9208 reserved range other than SHN_ABS and SHN_COMMON. */
9209 *ppsection = NULL;
9210 continue;
9211 }
dbaa2011 9212 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9213 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9214 isym->st_value =
9215 _bfd_merged_section_offset (output_bfd, &isec,
9216 elf_section_data (isec)->sec_info,
9217 isym->st_value);
c152c796
AM
9218 }
9219
9220 *ppsection = isec;
9221
9222 /* Don't output the first, undefined, symbol. */
8b127cbc 9223 if (ppsection == flinfo->sections)
c152c796
AM
9224 continue;
9225
9226 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9227 {
9228 /* We never output section symbols. Instead, we use the
9229 section symbol of the corresponding section in the output
9230 file. */
9231 continue;
9232 }
9233
9234 /* If we are stripping all symbols, we don't want to output this
9235 one. */
8b127cbc 9236 if (flinfo->info->strip == strip_all)
c152c796
AM
9237 continue;
9238
9239 /* If we are discarding all local symbols, we don't want to
9240 output this one. If we are generating a relocatable output
9241 file, then some of the local symbols may be required by
9242 relocs; we output them below as we discover that they are
9243 needed. */
8b127cbc 9244 if (flinfo->info->discard == discard_all)
c152c796
AM
9245 continue;
9246
9247 /* If this symbol is defined in a section which we are
f02571c5
AM
9248 discarding, we don't need to keep it. */
9249 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9250 && isym->st_shndx < SHN_LORESERVE
9251 && bfd_section_removed_from_list (output_bfd,
9252 isec->output_section))
e75a280b
L
9253 continue;
9254
c152c796
AM
9255 /* Get the name of the symbol. */
9256 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9257 isym->st_name);
9258 if (name == NULL)
9259 return FALSE;
9260
9261 /* See if we are discarding symbols with this name. */
8b127cbc
AM
9262 if ((flinfo->info->strip == strip_some
9263 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 9264 == NULL))
8b127cbc
AM
9265 || (((flinfo->info->discard == discard_sec_merge
9266 && (isec->flags & SEC_MERGE) && !flinfo->info->relocatable)
9267 || flinfo->info->discard == discard_l)
c152c796
AM
9268 && bfd_is_local_label_name (input_bfd, name)))
9269 continue;
9270
ffbc01cc
AM
9271 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9272 {
9273 have_file_sym = TRUE;
9274 flinfo->filesym_count += 1;
9275 }
9276 if (!have_file_sym)
9277 {
9278 /* In the absence of debug info, bfd_find_nearest_line uses
9279 FILE symbols to determine the source file for local
9280 function symbols. Provide a FILE symbol here if input
9281 files lack such, so that their symbols won't be
9282 associated with a previous input file. It's not the
9283 source file, but the best we can do. */
9284 have_file_sym = TRUE;
9285 flinfo->filesym_count += 1;
9286 memset (&osym, 0, sizeof (osym));
9287 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9288 osym.st_shndx = SHN_ABS;
9289 if (!elf_link_output_sym (flinfo, input_bfd->filename, &osym,
9290 bfd_abs_section_ptr, NULL))
9291 return FALSE;
9292 }
9293
c152c796
AM
9294 osym = *isym;
9295
9296 /* Adjust the section index for the output file. */
9297 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9298 isec->output_section);
9299 if (osym.st_shndx == SHN_BAD)
9300 return FALSE;
9301
c152c796
AM
9302 /* ELF symbols in relocatable files are section relative, but
9303 in executable files they are virtual addresses. Note that
9304 this code assumes that all ELF sections have an associated
9305 BFD section with a reasonable value for output_offset; below
9306 we assume that they also have a reasonable value for
9307 output_section. Any special sections must be set up to meet
9308 these requirements. */
9309 osym.st_value += isec->output_offset;
8b127cbc 9310 if (!flinfo->info->relocatable)
c152c796
AM
9311 {
9312 osym.st_value += isec->output_section->vma;
9313 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9314 {
9315 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
9316 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9317 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
9318 }
9319 }
9320
6e0b88f1 9321 indx = bfd_get_symcount (output_bfd);
8b127cbc 9322 ret = elf_link_output_sym (flinfo, name, &osym, isec, NULL);
6e0b88f1 9323 if (ret == 0)
c152c796 9324 return FALSE;
6e0b88f1
AM
9325 else if (ret == 1)
9326 *pindex = indx;
c152c796
AM
9327 }
9328
310fd250
L
9329 if (bed->s->arch_size == 32)
9330 {
9331 r_type_mask = 0xff;
9332 r_sym_shift = 8;
9333 address_size = 4;
9334 }
9335 else
9336 {
9337 r_type_mask = 0xffffffff;
9338 r_sym_shift = 32;
9339 address_size = 8;
9340 }
9341
c152c796
AM
9342 /* Relocate the contents of each section. */
9343 sym_hashes = elf_sym_hashes (input_bfd);
9344 for (o = input_bfd->sections; o != NULL; o = o->next)
9345 {
9346 bfd_byte *contents;
9347
9348 if (! o->linker_mark)
9349 {
9350 /* This section was omitted from the link. */
9351 continue;
9352 }
9353
8b127cbc 9354 if (flinfo->info->relocatable
bcacc0f5
AM
9355 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9356 {
9357 /* Deal with the group signature symbol. */
9358 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9359 unsigned long symndx = sec_data->this_hdr.sh_info;
9360 asection *osec = o->output_section;
9361
9362 if (symndx >= locsymcount
9363 || (elf_bad_symtab (input_bfd)
8b127cbc 9364 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
9365 {
9366 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9367 while (h->root.type == bfd_link_hash_indirect
9368 || h->root.type == bfd_link_hash_warning)
9369 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9370 /* Arrange for symbol to be output. */
9371 h->indx = -2;
9372 elf_section_data (osec)->this_hdr.sh_info = -2;
9373 }
9374 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9375 {
9376 /* We'll use the output section target_index. */
8b127cbc 9377 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
9378 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9379 }
9380 else
9381 {
8b127cbc 9382 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
9383 {
9384 /* Otherwise output the local symbol now. */
9385 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 9386 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 9387 const char *name;
6e0b88f1
AM
9388 long indx;
9389 int ret;
bcacc0f5
AM
9390
9391 name = bfd_elf_string_from_elf_section (input_bfd,
9392 symtab_hdr->sh_link,
9393 sym.st_name);
9394 if (name == NULL)
9395 return FALSE;
9396
9397 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9398 sec);
9399 if (sym.st_shndx == SHN_BAD)
9400 return FALSE;
9401
9402 sym.st_value += o->output_offset;
9403
6e0b88f1 9404 indx = bfd_get_symcount (output_bfd);
8b127cbc 9405 ret = elf_link_output_sym (flinfo, name, &sym, o, NULL);
6e0b88f1 9406 if (ret == 0)
bcacc0f5 9407 return FALSE;
6e0b88f1 9408 else if (ret == 1)
8b127cbc 9409 flinfo->indices[symndx] = indx;
6e0b88f1
AM
9410 else
9411 abort ();
bcacc0f5
AM
9412 }
9413 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 9414 = flinfo->indices[symndx];
bcacc0f5
AM
9415 }
9416 }
9417
c152c796 9418 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 9419 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
9420 continue;
9421
9422 if ((o->flags & SEC_LINKER_CREATED) != 0)
9423 {
9424 /* Section was created by _bfd_elf_link_create_dynamic_sections
9425 or somesuch. */
9426 continue;
9427 }
9428
9429 /* Get the contents of the section. They have been cached by a
9430 relaxation routine. Note that o is a section in an input
9431 file, so the contents field will not have been set by any of
9432 the routines which work on output files. */
9433 if (elf_section_data (o)->this_hdr.contents != NULL)
9434 contents = elf_section_data (o)->this_hdr.contents;
9435 else
9436 {
8b127cbc 9437 contents = flinfo->contents;
4a114e3e 9438 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
9439 return FALSE;
9440 }
9441
9442 if ((o->flags & SEC_RELOC) != 0)
9443 {
9444 Elf_Internal_Rela *internal_relocs;
0f02bbd9 9445 Elf_Internal_Rela *rel, *relend;
0f02bbd9 9446 int action_discarded;
ece5ef60 9447 int ret;
c152c796
AM
9448
9449 /* Get the swapped relocs. */
9450 internal_relocs
8b127cbc
AM
9451 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
9452 flinfo->internal_relocs, FALSE);
c152c796
AM
9453 if (internal_relocs == NULL
9454 && o->reloc_count > 0)
9455 return FALSE;
9456
310fd250
L
9457 /* We need to reverse-copy input .ctors/.dtors sections if
9458 they are placed in .init_array/.finit_array for output. */
9459 if (o->size > address_size
9460 && ((strncmp (o->name, ".ctors", 6) == 0
9461 && strcmp (o->output_section->name,
9462 ".init_array") == 0)
9463 || (strncmp (o->name, ".dtors", 6) == 0
9464 && strcmp (o->output_section->name,
9465 ".fini_array") == 0))
9466 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 9467 {
310fd250
L
9468 if (o->size != o->reloc_count * address_size)
9469 {
9470 (*_bfd_error_handler)
9471 (_("error: %B: size of section %A is not "
9472 "multiple of address size"),
9473 input_bfd, o);
9474 bfd_set_error (bfd_error_on_input);
9475 return FALSE;
9476 }
9477 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
9478 }
9479
0f02bbd9 9480 action_discarded = -1;
c152c796 9481 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
9482 action_discarded = (*bed->action_discarded) (o);
9483
9484 /* Run through the relocs evaluating complex reloc symbols and
9485 looking for relocs against symbols from discarded sections
9486 or section symbols from removed link-once sections.
9487 Complain about relocs against discarded sections. Zero
9488 relocs against removed link-once sections. */
9489
9490 rel = internal_relocs;
9491 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9492 for ( ; rel < relend; rel++)
c152c796 9493 {
0f02bbd9
AM
9494 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9495 unsigned int s_type;
9496 asection **ps, *sec;
9497 struct elf_link_hash_entry *h = NULL;
9498 const char *sym_name;
c152c796 9499
0f02bbd9
AM
9500 if (r_symndx == STN_UNDEF)
9501 continue;
c152c796 9502
0f02bbd9
AM
9503 if (r_symndx >= locsymcount
9504 || (elf_bad_symtab (input_bfd)
8b127cbc 9505 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
9506 {
9507 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 9508
0f02bbd9
AM
9509 /* Badly formatted input files can contain relocs that
9510 reference non-existant symbols. Check here so that
9511 we do not seg fault. */
9512 if (h == NULL)
c152c796 9513 {
0f02bbd9 9514 char buffer [32];
dce669a1 9515
0f02bbd9
AM
9516 sprintf_vma (buffer, rel->r_info);
9517 (*_bfd_error_handler)
9518 (_("error: %B contains a reloc (0x%s) for section %A "
9519 "that references a non-existent global symbol"),
9520 input_bfd, o, buffer);
9521 bfd_set_error (bfd_error_bad_value);
9522 return FALSE;
9523 }
3b36f7e6 9524
0f02bbd9
AM
9525 while (h->root.type == bfd_link_hash_indirect
9526 || h->root.type == bfd_link_hash_warning)
9527 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 9528
0f02bbd9 9529 s_type = h->type;
cdd3575c 9530
0f02bbd9
AM
9531 ps = NULL;
9532 if (h->root.type == bfd_link_hash_defined
9533 || h->root.type == bfd_link_hash_defweak)
9534 ps = &h->root.u.def.section;
9535
9536 sym_name = h->root.root.string;
9537 }
9538 else
9539 {
9540 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9541
9542 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 9543 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
9544 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9545 sym, *ps);
9546 }
c152c796 9547
c301e700 9548 if ((s_type == STT_RELC || s_type == STT_SRELC)
8b127cbc 9549 && !flinfo->info->relocatable)
0f02bbd9
AM
9550 {
9551 bfd_vma val;
9552 bfd_vma dot = (rel->r_offset
9553 + o->output_offset + o->output_section->vma);
9554#ifdef DEBUG
9555 printf ("Encountered a complex symbol!");
9556 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
9557 input_bfd->filename, o->name,
9558 (long) (rel - internal_relocs));
0f02bbd9
AM
9559 printf (" symbol: idx %8.8lx, name %s\n",
9560 r_symndx, sym_name);
9561 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9562 (unsigned long) rel->r_info,
9563 (unsigned long) rel->r_offset);
9564#endif
8b127cbc 9565 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
9566 isymbuf, locsymcount, s_type == STT_SRELC))
9567 return FALSE;
9568
9569 /* Symbol evaluated OK. Update to absolute value. */
9570 set_symbol_value (input_bfd, isymbuf, locsymcount,
9571 r_symndx, val);
9572 continue;
9573 }
9574
9575 if (action_discarded != -1 && ps != NULL)
9576 {
cdd3575c
AM
9577 /* Complain if the definition comes from a
9578 discarded section. */
dbaa2011 9579 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 9580 {
cf35638d 9581 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 9582 if (action_discarded & COMPLAIN)
8b127cbc 9583 (*flinfo->info->callbacks->einfo)
e1fffbe6 9584 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 9585 "defined in discarded section `%A' of %B\n"),
e1fffbe6 9586 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 9587
87e5235d 9588 /* Try to do the best we can to support buggy old
e0ae6d6f 9589 versions of gcc. Pretend that the symbol is
87e5235d
AM
9590 really defined in the kept linkonce section.
9591 FIXME: This is quite broken. Modifying the
9592 symbol here means we will be changing all later
e0ae6d6f 9593 uses of the symbol, not just in this section. */
0f02bbd9 9594 if (action_discarded & PRETEND)
87e5235d 9595 {
01b3c8ab
L
9596 asection *kept;
9597
c0f00686 9598 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 9599 flinfo->info);
01b3c8ab 9600 if (kept != NULL)
87e5235d
AM
9601 {
9602 *ps = kept;
9603 continue;
9604 }
9605 }
c152c796
AM
9606 }
9607 }
9608 }
9609
9610 /* Relocate the section by invoking a back end routine.
9611
9612 The back end routine is responsible for adjusting the
9613 section contents as necessary, and (if using Rela relocs
9614 and generating a relocatable output file) adjusting the
9615 reloc addend as necessary.
9616
9617 The back end routine does not have to worry about setting
9618 the reloc address or the reloc symbol index.
9619
9620 The back end routine is given a pointer to the swapped in
9621 internal symbols, and can access the hash table entries
9622 for the external symbols via elf_sym_hashes (input_bfd).
9623
9624 When generating relocatable output, the back end routine
9625 must handle STB_LOCAL/STT_SECTION symbols specially. The
9626 output symbol is going to be a section symbol
9627 corresponding to the output section, which will require
9628 the addend to be adjusted. */
9629
8b127cbc 9630 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
9631 input_bfd, o, contents,
9632 internal_relocs,
9633 isymbuf,
8b127cbc 9634 flinfo->sections);
ece5ef60 9635 if (!ret)
c152c796
AM
9636 return FALSE;
9637
ece5ef60 9638 if (ret == 2
8b127cbc
AM
9639 || flinfo->info->relocatable
9640 || flinfo->info->emitrelocations)
c152c796
AM
9641 {
9642 Elf_Internal_Rela *irela;
d4730f92 9643 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
9644 bfd_vma last_offset;
9645 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
9646 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
9647 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 9648 unsigned int next_erel;
c152c796 9649 bfd_boolean rela_normal;
d4730f92 9650 struct bfd_elf_section_data *esdi, *esdo;
c152c796 9651
d4730f92
BS
9652 esdi = elf_section_data (o);
9653 esdo = elf_section_data (o->output_section);
9654 rela_normal = FALSE;
c152c796
AM
9655
9656 /* Adjust the reloc addresses and symbol indices. */
9657
9658 irela = internal_relocs;
9659 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
9660 rel_hash = esdo->rel.hashes + esdo->rel.count;
9661 /* We start processing the REL relocs, if any. When we reach
9662 IRELAMID in the loop, we switch to the RELA relocs. */
9663 irelamid = irela;
9664 if (esdi->rel.hdr != NULL)
9665 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
9666 * bed->s->int_rels_per_ext_rel);
eac338cf 9667 rel_hash_list = rel_hash;
d4730f92 9668 rela_hash_list = NULL;
c152c796 9669 last_offset = o->output_offset;
8b127cbc 9670 if (!flinfo->info->relocatable)
c152c796
AM
9671 last_offset += o->output_section->vma;
9672 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9673 {
9674 unsigned long r_symndx;
9675 asection *sec;
9676 Elf_Internal_Sym sym;
9677
9678 if (next_erel == bed->s->int_rels_per_ext_rel)
9679 {
9680 rel_hash++;
9681 next_erel = 0;
9682 }
9683
d4730f92
BS
9684 if (irela == irelamid)
9685 {
9686 rel_hash = esdo->rela.hashes + esdo->rela.count;
9687 rela_hash_list = rel_hash;
9688 rela_normal = bed->rela_normal;
9689 }
9690
c152c796 9691 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 9692 flinfo->info, o,
c152c796
AM
9693 irela->r_offset);
9694 if (irela->r_offset >= (bfd_vma) -2)
9695 {
9696 /* This is a reloc for a deleted entry or somesuch.
9697 Turn it into an R_*_NONE reloc, at the same
9698 offset as the last reloc. elf_eh_frame.c and
e460dd0d 9699 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
9700 being ordered. */
9701 irela->r_offset = last_offset;
9702 irela->r_info = 0;
9703 irela->r_addend = 0;
9704 continue;
9705 }
9706
9707 irela->r_offset += o->output_offset;
9708
9709 /* Relocs in an executable have to be virtual addresses. */
8b127cbc 9710 if (!flinfo->info->relocatable)
c152c796
AM
9711 irela->r_offset += o->output_section->vma;
9712
9713 last_offset = irela->r_offset;
9714
9715 r_symndx = irela->r_info >> r_sym_shift;
9716 if (r_symndx == STN_UNDEF)
9717 continue;
9718
9719 if (r_symndx >= locsymcount
9720 || (elf_bad_symtab (input_bfd)
8b127cbc 9721 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
9722 {
9723 struct elf_link_hash_entry *rh;
9724 unsigned long indx;
9725
9726 /* This is a reloc against a global symbol. We
9727 have not yet output all the local symbols, so
9728 we do not know the symbol index of any global
9729 symbol. We set the rel_hash entry for this
9730 reloc to point to the global hash table entry
9731 for this symbol. The symbol index is then
ee75fd95 9732 set at the end of bfd_elf_final_link. */
c152c796
AM
9733 indx = r_symndx - extsymoff;
9734 rh = elf_sym_hashes (input_bfd)[indx];
9735 while (rh->root.type == bfd_link_hash_indirect
9736 || rh->root.type == bfd_link_hash_warning)
9737 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9738
9739 /* Setting the index to -2 tells
9740 elf_link_output_extsym that this symbol is
9741 used by a reloc. */
9742 BFD_ASSERT (rh->indx < 0);
9743 rh->indx = -2;
9744
9745 *rel_hash = rh;
9746
9747 continue;
9748 }
9749
9750 /* This is a reloc against a local symbol. */
9751
9752 *rel_hash = NULL;
9753 sym = isymbuf[r_symndx];
8b127cbc 9754 sec = flinfo->sections[r_symndx];
c152c796
AM
9755 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9756 {
9757 /* I suppose the backend ought to fill in the
9758 section of any STT_SECTION symbol against a
6a8d1586 9759 processor specific section. */
cf35638d 9760 r_symndx = STN_UNDEF;
6a8d1586
AM
9761 if (bfd_is_abs_section (sec))
9762 ;
c152c796
AM
9763 else if (sec == NULL || sec->owner == NULL)
9764 {
9765 bfd_set_error (bfd_error_bad_value);
9766 return FALSE;
9767 }
9768 else
9769 {
6a8d1586
AM
9770 asection *osec = sec->output_section;
9771
9772 /* If we have discarded a section, the output
9773 section will be the absolute section. In
ab96bf03
AM
9774 case of discarded SEC_MERGE sections, use
9775 the kept section. relocate_section should
9776 have already handled discarded linkonce
9777 sections. */
6a8d1586
AM
9778 if (bfd_is_abs_section (osec)
9779 && sec->kept_section != NULL
9780 && sec->kept_section->output_section != NULL)
9781 {
9782 osec = sec->kept_section->output_section;
9783 irela->r_addend -= osec->vma;
9784 }
9785
9786 if (!bfd_is_abs_section (osec))
9787 {
9788 r_symndx = osec->target_index;
cf35638d 9789 if (r_symndx == STN_UNDEF)
74541ad4 9790 {
051d833a
AM
9791 irela->r_addend += osec->vma;
9792 osec = _bfd_nearby_section (output_bfd, osec,
9793 osec->vma);
9794 irela->r_addend -= osec->vma;
9795 r_symndx = osec->target_index;
74541ad4 9796 }
6a8d1586 9797 }
c152c796
AM
9798 }
9799
9800 /* Adjust the addend according to where the
9801 section winds up in the output section. */
9802 if (rela_normal)
9803 irela->r_addend += sec->output_offset;
9804 }
9805 else
9806 {
8b127cbc 9807 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
9808 {
9809 unsigned long shlink;
9810 const char *name;
9811 asection *osec;
6e0b88f1 9812 long indx;
c152c796 9813
8b127cbc 9814 if (flinfo->info->strip == strip_all)
c152c796
AM
9815 {
9816 /* You can't do ld -r -s. */
9817 bfd_set_error (bfd_error_invalid_operation);
9818 return FALSE;
9819 }
9820
9821 /* This symbol was skipped earlier, but
9822 since it is needed by a reloc, we
9823 must output it now. */
9824 shlink = symtab_hdr->sh_link;
9825 name = (bfd_elf_string_from_elf_section
9826 (input_bfd, shlink, sym.st_name));
9827 if (name == NULL)
9828 return FALSE;
9829
9830 osec = sec->output_section;
9831 sym.st_shndx =
9832 _bfd_elf_section_from_bfd_section (output_bfd,
9833 osec);
9834 if (sym.st_shndx == SHN_BAD)
9835 return FALSE;
9836
9837 sym.st_value += sec->output_offset;
8b127cbc 9838 if (!flinfo->info->relocatable)
c152c796
AM
9839 {
9840 sym.st_value += osec->vma;
9841 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9842 {
9843 /* STT_TLS symbols are relative to PT_TLS
9844 segment base. */
8b127cbc 9845 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 9846 ->tls_sec != NULL);
8b127cbc 9847 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
9848 ->tls_sec->vma);
9849 }
9850 }
9851
6e0b88f1 9852 indx = bfd_get_symcount (output_bfd);
8b127cbc 9853 ret = elf_link_output_sym (flinfo, name, &sym, sec,
6e0b88f1
AM
9854 NULL);
9855 if (ret == 0)
c152c796 9856 return FALSE;
6e0b88f1 9857 else if (ret == 1)
8b127cbc 9858 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
9859 else
9860 abort ();
c152c796
AM
9861 }
9862
8b127cbc 9863 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
9864 }
9865
9866 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9867 | (irela->r_info & r_type_mask));
9868 }
9869
9870 /* Swap out the relocs. */
d4730f92
BS
9871 input_rel_hdr = esdi->rel.hdr;
9872 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 9873 {
d4730f92
BS
9874 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9875 input_rel_hdr,
9876 internal_relocs,
9877 rel_hash_list))
9878 return FALSE;
c152c796
AM
9879 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9880 * bed->s->int_rels_per_ext_rel);
eac338cf 9881 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
9882 }
9883
9884 input_rela_hdr = esdi->rela.hdr;
9885 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
9886 {
eac338cf 9887 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 9888 input_rela_hdr,
eac338cf 9889 internal_relocs,
d4730f92 9890 rela_hash_list))
c152c796
AM
9891 return FALSE;
9892 }
9893 }
9894 }
9895
9896 /* Write out the modified section contents. */
9897 if (bed->elf_backend_write_section
8b127cbc 9898 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 9899 contents))
c152c796
AM
9900 {
9901 /* Section written out. */
9902 }
9903 else switch (o->sec_info_type)
9904 {
dbaa2011 9905 case SEC_INFO_TYPE_STABS:
c152c796
AM
9906 if (! (_bfd_write_section_stabs
9907 (output_bfd,
8b127cbc 9908 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
9909 o, &elf_section_data (o)->sec_info, contents)))
9910 return FALSE;
9911 break;
dbaa2011 9912 case SEC_INFO_TYPE_MERGE:
c152c796
AM
9913 if (! _bfd_write_merged_section (output_bfd, o,
9914 elf_section_data (o)->sec_info))
9915 return FALSE;
9916 break;
dbaa2011 9917 case SEC_INFO_TYPE_EH_FRAME:
c152c796 9918 {
8b127cbc 9919 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
9920 o, contents))
9921 return FALSE;
9922 }
9923 break;
9924 default:
9925 {
5dabe785 9926 /* FIXME: octets_per_byte. */
310fd250
L
9927 if (! (o->flags & SEC_EXCLUDE))
9928 {
9929 file_ptr offset = (file_ptr) o->output_offset;
9930 bfd_size_type todo = o->size;
9931 if ((o->flags & SEC_ELF_REVERSE_COPY))
9932 {
9933 /* Reverse-copy input section to output. */
9934 do
9935 {
9936 todo -= address_size;
9937 if (! bfd_set_section_contents (output_bfd,
9938 o->output_section,
9939 contents + todo,
9940 offset,
9941 address_size))
9942 return FALSE;
9943 if (todo == 0)
9944 break;
9945 offset += address_size;
9946 }
9947 while (1);
9948 }
9949 else if (! bfd_set_section_contents (output_bfd,
9950 o->output_section,
9951 contents,
9952 offset, todo))
9953 return FALSE;
9954 }
c152c796
AM
9955 }
9956 break;
9957 }
9958 }
9959
9960 return TRUE;
9961}
9962
9963/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 9964 requested by the linker, and does not come from any input file. This
c152c796
AM
9965 is used to build constructor and destructor tables when linking
9966 with -Ur. */
9967
9968static bfd_boolean
9969elf_reloc_link_order (bfd *output_bfd,
9970 struct bfd_link_info *info,
9971 asection *output_section,
9972 struct bfd_link_order *link_order)
9973{
9974 reloc_howto_type *howto;
9975 long indx;
9976 bfd_vma offset;
9977 bfd_vma addend;
d4730f92 9978 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
9979 struct elf_link_hash_entry **rel_hash_ptr;
9980 Elf_Internal_Shdr *rel_hdr;
9981 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9982 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9983 bfd_byte *erel;
9984 unsigned int i;
d4730f92 9985 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
9986
9987 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9988 if (howto == NULL)
9989 {
9990 bfd_set_error (bfd_error_bad_value);
9991 return FALSE;
9992 }
9993
9994 addend = link_order->u.reloc.p->addend;
9995
d4730f92
BS
9996 if (esdo->rel.hdr)
9997 reldata = &esdo->rel;
9998 else if (esdo->rela.hdr)
9999 reldata = &esdo->rela;
10000 else
10001 {
10002 reldata = NULL;
10003 BFD_ASSERT (0);
10004 }
10005
c152c796 10006 /* Figure out the symbol index. */
d4730f92 10007 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10008 if (link_order->type == bfd_section_reloc_link_order)
10009 {
10010 indx = link_order->u.reloc.p->u.section->target_index;
10011 BFD_ASSERT (indx != 0);
10012 *rel_hash_ptr = NULL;
10013 }
10014 else
10015 {
10016 struct elf_link_hash_entry *h;
10017
10018 /* Treat a reloc against a defined symbol as though it were
10019 actually against the section. */
10020 h = ((struct elf_link_hash_entry *)
10021 bfd_wrapped_link_hash_lookup (output_bfd, info,
10022 link_order->u.reloc.p->u.name,
10023 FALSE, FALSE, TRUE));
10024 if (h != NULL
10025 && (h->root.type == bfd_link_hash_defined
10026 || h->root.type == bfd_link_hash_defweak))
10027 {
10028 asection *section;
10029
10030 section = h->root.u.def.section;
10031 indx = section->output_section->target_index;
10032 *rel_hash_ptr = NULL;
10033 /* It seems that we ought to add the symbol value to the
10034 addend here, but in practice it has already been added
10035 because it was passed to constructor_callback. */
10036 addend += section->output_section->vma + section->output_offset;
10037 }
10038 else if (h != NULL)
10039 {
10040 /* Setting the index to -2 tells elf_link_output_extsym that
10041 this symbol is used by a reloc. */
10042 h->indx = -2;
10043 *rel_hash_ptr = h;
10044 indx = 0;
10045 }
10046 else
10047 {
10048 if (! ((*info->callbacks->unattached_reloc)
10049 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10050 return FALSE;
10051 indx = 0;
10052 }
10053 }
10054
10055 /* If this is an inplace reloc, we must write the addend into the
10056 object file. */
10057 if (howto->partial_inplace && addend != 0)
10058 {
10059 bfd_size_type size;
10060 bfd_reloc_status_type rstat;
10061 bfd_byte *buf;
10062 bfd_boolean ok;
10063 const char *sym_name;
10064
a50b1753
NC
10065 size = (bfd_size_type) bfd_get_reloc_size (howto);
10066 buf = (bfd_byte *) bfd_zmalloc (size);
c152c796
AM
10067 if (buf == NULL)
10068 return FALSE;
10069 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10070 switch (rstat)
10071 {
10072 case bfd_reloc_ok:
10073 break;
10074
10075 default:
10076 case bfd_reloc_outofrange:
10077 abort ();
10078
10079 case bfd_reloc_overflow:
10080 if (link_order->type == bfd_section_reloc_link_order)
10081 sym_name = bfd_section_name (output_bfd,
10082 link_order->u.reloc.p->u.section);
10083 else
10084 sym_name = link_order->u.reloc.p->u.name;
10085 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
10086 (info, NULL, sym_name, howto->name, addend, NULL,
10087 NULL, (bfd_vma) 0)))
c152c796
AM
10088 {
10089 free (buf);
10090 return FALSE;
10091 }
10092 break;
10093 }
10094 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10095 link_order->offset, size);
10096 free (buf);
10097 if (! ok)
10098 return FALSE;
10099 }
10100
10101 /* The address of a reloc is relative to the section in a
10102 relocatable file, and is a virtual address in an executable
10103 file. */
10104 offset = link_order->offset;
10105 if (! info->relocatable)
10106 offset += output_section->vma;
10107
10108 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10109 {
10110 irel[i].r_offset = offset;
10111 irel[i].r_info = 0;
10112 irel[i].r_addend = 0;
10113 }
10114 if (bed->s->arch_size == 32)
10115 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10116 else
10117 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10118
d4730f92 10119 rel_hdr = reldata->hdr;
c152c796
AM
10120 erel = rel_hdr->contents;
10121 if (rel_hdr->sh_type == SHT_REL)
10122 {
d4730f92 10123 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10124 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10125 }
10126 else
10127 {
10128 irel[0].r_addend = addend;
d4730f92 10129 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10130 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10131 }
10132
d4730f92 10133 ++reldata->count;
c152c796
AM
10134
10135 return TRUE;
10136}
10137
0b52efa6
PB
10138
10139/* Get the output vma of the section pointed to by the sh_link field. */
10140
10141static bfd_vma
10142elf_get_linked_section_vma (struct bfd_link_order *p)
10143{
10144 Elf_Internal_Shdr **elf_shdrp;
10145 asection *s;
10146 int elfsec;
10147
10148 s = p->u.indirect.section;
10149 elf_shdrp = elf_elfsections (s->owner);
10150 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10151 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10152 /* PR 290:
10153 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10154 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10155 sh_info fields. Hence we could get the situation
10156 where elfsec is 0. */
10157 if (elfsec == 0)
10158 {
10159 const struct elf_backend_data *bed
10160 = get_elf_backend_data (s->owner);
10161 if (bed->link_order_error_handler)
d003868e
AM
10162 bed->link_order_error_handler
10163 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10164 return 0;
10165 }
10166 else
10167 {
10168 s = elf_shdrp[elfsec]->bfd_section;
10169 return s->output_section->vma + s->output_offset;
10170 }
0b52efa6
PB
10171}
10172
10173
10174/* Compare two sections based on the locations of the sections they are
10175 linked to. Used by elf_fixup_link_order. */
10176
10177static int
10178compare_link_order (const void * a, const void * b)
10179{
10180 bfd_vma apos;
10181 bfd_vma bpos;
10182
10183 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10184 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10185 if (apos < bpos)
10186 return -1;
10187 return apos > bpos;
10188}
10189
10190
10191/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10192 order as their linked sections. Returns false if this could not be done
10193 because an output section includes both ordered and unordered
10194 sections. Ideally we'd do this in the linker proper. */
10195
10196static bfd_boolean
10197elf_fixup_link_order (bfd *abfd, asection *o)
10198{
10199 int seen_linkorder;
10200 int seen_other;
10201 int n;
10202 struct bfd_link_order *p;
10203 bfd *sub;
10204 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10205 unsigned elfsec;
0b52efa6 10206 struct bfd_link_order **sections;
d33cdfe3 10207 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10208 bfd_vma offset;
3b36f7e6 10209
d33cdfe3
L
10210 other_sec = NULL;
10211 linkorder_sec = NULL;
0b52efa6
PB
10212 seen_other = 0;
10213 seen_linkorder = 0;
8423293d 10214 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10215 {
d33cdfe3 10216 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10217 {
10218 s = p->u.indirect.section;
d33cdfe3
L
10219 sub = s->owner;
10220 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10221 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10222 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10223 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10224 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10225 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10226 {
10227 seen_linkorder++;
10228 linkorder_sec = s;
10229 }
0b52efa6 10230 else
d33cdfe3
L
10231 {
10232 seen_other++;
10233 other_sec = s;
10234 }
0b52efa6
PB
10235 }
10236 else
10237 seen_other++;
d33cdfe3
L
10238
10239 if (seen_other && seen_linkorder)
10240 {
10241 if (other_sec && linkorder_sec)
10242 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10243 o, linkorder_sec,
10244 linkorder_sec->owner, other_sec,
10245 other_sec->owner);
10246 else
10247 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10248 o);
10249 bfd_set_error (bfd_error_bad_value);
10250 return FALSE;
10251 }
0b52efa6
PB
10252 }
10253
10254 if (!seen_linkorder)
10255 return TRUE;
10256
0b52efa6 10257 sections = (struct bfd_link_order **)
14b1c01e
AM
10258 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10259 if (sections == NULL)
10260 return FALSE;
0b52efa6 10261 seen_linkorder = 0;
3b36f7e6 10262
8423293d 10263 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
10264 {
10265 sections[seen_linkorder++] = p;
10266 }
10267 /* Sort the input sections in the order of their linked section. */
10268 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10269 compare_link_order);
10270
10271 /* Change the offsets of the sections. */
10272 offset = 0;
10273 for (n = 0; n < seen_linkorder; n++)
10274 {
10275 s = sections[n]->u.indirect.section;
461686a3 10276 offset &= ~(bfd_vma) 0 << s->alignment_power;
0b52efa6
PB
10277 s->output_offset = offset;
10278 sections[n]->offset = offset;
5dabe785 10279 /* FIXME: octets_per_byte. */
0b52efa6
PB
10280 offset += sections[n]->size;
10281 }
10282
4dd07732 10283 free (sections);
0b52efa6
PB
10284 return TRUE;
10285}
10286
10287
c152c796
AM
10288/* Do the final step of an ELF link. */
10289
10290bfd_boolean
10291bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10292{
10293 bfd_boolean dynamic;
10294 bfd_boolean emit_relocs;
10295 bfd *dynobj;
8b127cbc 10296 struct elf_final_link_info flinfo;
91d6fa6a
NC
10297 asection *o;
10298 struct bfd_link_order *p;
10299 bfd *sub;
c152c796
AM
10300 bfd_size_type max_contents_size;
10301 bfd_size_type max_external_reloc_size;
10302 bfd_size_type max_internal_reloc_count;
10303 bfd_size_type max_sym_count;
10304 bfd_size_type max_sym_shndx_count;
10305 file_ptr off;
10306 Elf_Internal_Sym elfsym;
10307 unsigned int i;
10308 Elf_Internal_Shdr *symtab_hdr;
10309 Elf_Internal_Shdr *symtab_shndx_hdr;
10310 Elf_Internal_Shdr *symstrtab_hdr;
10311 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10312 struct elf_outext_info eoinfo;
10313 bfd_boolean merged;
10314 size_t relativecount = 0;
10315 asection *reldyn = 0;
10316 bfd_size_type amt;
104d59d1
JM
10317 asection *attr_section = NULL;
10318 bfd_vma attr_size = 0;
10319 const char *std_attrs_section;
c152c796
AM
10320
10321 if (! is_elf_hash_table (info->hash))
10322 return FALSE;
10323
10324 if (info->shared)
10325 abfd->flags |= DYNAMIC;
10326
10327 dynamic = elf_hash_table (info)->dynamic_sections_created;
10328 dynobj = elf_hash_table (info)->dynobj;
10329
10330 emit_relocs = (info->relocatable
a4676736 10331 || info->emitrelocations);
c152c796 10332
8b127cbc
AM
10333 flinfo.info = info;
10334 flinfo.output_bfd = abfd;
10335 flinfo.symstrtab = _bfd_elf_stringtab_init ();
10336 if (flinfo.symstrtab == NULL)
c152c796
AM
10337 return FALSE;
10338
10339 if (! dynamic)
10340 {
8b127cbc
AM
10341 flinfo.dynsym_sec = NULL;
10342 flinfo.hash_sec = NULL;
10343 flinfo.symver_sec = NULL;
c152c796
AM
10344 }
10345 else
10346 {
3d4d4302
AM
10347 flinfo.dynsym_sec = bfd_get_linker_section (dynobj, ".dynsym");
10348 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 10349 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 10350 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
10351 /* Note that it is OK if symver_sec is NULL. */
10352 }
10353
8b127cbc
AM
10354 flinfo.contents = NULL;
10355 flinfo.external_relocs = NULL;
10356 flinfo.internal_relocs = NULL;
10357 flinfo.external_syms = NULL;
10358 flinfo.locsym_shndx = NULL;
10359 flinfo.internal_syms = NULL;
10360 flinfo.indices = NULL;
10361 flinfo.sections = NULL;
10362 flinfo.symbuf = NULL;
10363 flinfo.symshndxbuf = NULL;
10364 flinfo.symbuf_count = 0;
10365 flinfo.shndxbuf_size = 0;
ffbc01cc 10366 flinfo.filesym_count = 0;
c152c796 10367
104d59d1
JM
10368 /* The object attributes have been merged. Remove the input
10369 sections from the link, and set the contents of the output
10370 secton. */
10371 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10372 for (o = abfd->sections; o != NULL; o = o->next)
10373 {
10374 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10375 || strcmp (o->name, ".gnu.attributes") == 0)
10376 {
10377 for (p = o->map_head.link_order; p != NULL; p = p->next)
10378 {
10379 asection *input_section;
10380
10381 if (p->type != bfd_indirect_link_order)
10382 continue;
10383 input_section = p->u.indirect.section;
10384 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10385 elf_link_input_bfd ignores this section. */
10386 input_section->flags &= ~SEC_HAS_CONTENTS;
10387 }
a0c8462f 10388
104d59d1
JM
10389 attr_size = bfd_elf_obj_attr_size (abfd);
10390 if (attr_size)
10391 {
10392 bfd_set_section_size (abfd, o, attr_size);
10393 attr_section = o;
10394 /* Skip this section later on. */
10395 o->map_head.link_order = NULL;
10396 }
10397 else
10398 o->flags |= SEC_EXCLUDE;
10399 }
10400 }
10401
c152c796
AM
10402 /* Count up the number of relocations we will output for each output
10403 section, so that we know the sizes of the reloc sections. We
10404 also figure out some maximum sizes. */
10405 max_contents_size = 0;
10406 max_external_reloc_size = 0;
10407 max_internal_reloc_count = 0;
10408 max_sym_count = 0;
10409 max_sym_shndx_count = 0;
10410 merged = FALSE;
10411 for (o = abfd->sections; o != NULL; o = o->next)
10412 {
10413 struct bfd_elf_section_data *esdo = elf_section_data (o);
10414 o->reloc_count = 0;
10415
8423293d 10416 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10417 {
10418 unsigned int reloc_count = 0;
10419 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
10420
10421 if (p->type == bfd_section_reloc_link_order
10422 || p->type == bfd_symbol_reloc_link_order)
10423 reloc_count = 1;
10424 else if (p->type == bfd_indirect_link_order)
10425 {
10426 asection *sec;
10427
10428 sec = p->u.indirect.section;
10429 esdi = elf_section_data (sec);
10430
10431 /* Mark all sections which are to be included in the
10432 link. This will normally be every section. We need
10433 to do this so that we can identify any sections which
10434 the linker has decided to not include. */
10435 sec->linker_mark = TRUE;
10436
10437 if (sec->flags & SEC_MERGE)
10438 merged = TRUE;
10439
aed64b35
L
10440 if (esdo->this_hdr.sh_type == SHT_REL
10441 || esdo->this_hdr.sh_type == SHT_RELA)
10442 /* Some backends use reloc_count in relocation sections
10443 to count particular types of relocs. Of course,
10444 reloc sections themselves can't have relocations. */
10445 reloc_count = 0;
10446 else if (info->relocatable || info->emitrelocations)
c152c796
AM
10447 reloc_count = sec->reloc_count;
10448 else if (bed->elf_backend_count_relocs)
58217f29 10449 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 10450
eea6121a
AM
10451 if (sec->rawsize > max_contents_size)
10452 max_contents_size = sec->rawsize;
10453 if (sec->size > max_contents_size)
10454 max_contents_size = sec->size;
c152c796
AM
10455
10456 /* We are interested in just local symbols, not all
10457 symbols. */
10458 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10459 && (sec->owner->flags & DYNAMIC) == 0)
10460 {
10461 size_t sym_count;
10462
10463 if (elf_bad_symtab (sec->owner))
10464 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10465 / bed->s->sizeof_sym);
10466 else
10467 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10468
10469 if (sym_count > max_sym_count)
10470 max_sym_count = sym_count;
10471
10472 if (sym_count > max_sym_shndx_count
10473 && elf_symtab_shndx (sec->owner) != 0)
10474 max_sym_shndx_count = sym_count;
10475
10476 if ((sec->flags & SEC_RELOC) != 0)
10477 {
d4730f92 10478 size_t ext_size = 0;
c152c796 10479
d4730f92
BS
10480 if (esdi->rel.hdr != NULL)
10481 ext_size = esdi->rel.hdr->sh_size;
10482 if (esdi->rela.hdr != NULL)
10483 ext_size += esdi->rela.hdr->sh_size;
7326c758 10484
c152c796
AM
10485 if (ext_size > max_external_reloc_size)
10486 max_external_reloc_size = ext_size;
10487 if (sec->reloc_count > max_internal_reloc_count)
10488 max_internal_reloc_count = sec->reloc_count;
10489 }
10490 }
10491 }
10492
10493 if (reloc_count == 0)
10494 continue;
10495
10496 o->reloc_count += reloc_count;
10497
d4730f92
BS
10498 if (p->type == bfd_indirect_link_order
10499 && (info->relocatable || info->emitrelocations))
c152c796 10500 {
d4730f92
BS
10501 if (esdi->rel.hdr)
10502 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
10503 if (esdi->rela.hdr)
10504 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
10505 }
10506 else
10507 {
10508 if (o->use_rela_p)
10509 esdo->rela.count += reloc_count;
2c2b4ed4 10510 else
d4730f92 10511 esdo->rel.count += reloc_count;
c152c796 10512 }
c152c796
AM
10513 }
10514
10515 if (o->reloc_count > 0)
10516 o->flags |= SEC_RELOC;
10517 else
10518 {
10519 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10520 set it (this is probably a bug) and if it is set
10521 assign_section_numbers will create a reloc section. */
10522 o->flags &=~ SEC_RELOC;
10523 }
10524
10525 /* If the SEC_ALLOC flag is not set, force the section VMA to
10526 zero. This is done in elf_fake_sections as well, but forcing
10527 the VMA to 0 here will ensure that relocs against these
10528 sections are handled correctly. */
10529 if ((o->flags & SEC_ALLOC) == 0
10530 && ! o->user_set_vma)
10531 o->vma = 0;
10532 }
10533
10534 if (! info->relocatable && merged)
10535 elf_link_hash_traverse (elf_hash_table (info),
10536 _bfd_elf_link_sec_merge_syms, abfd);
10537
10538 /* Figure out the file positions for everything but the symbol table
10539 and the relocs. We set symcount to force assign_section_numbers
10540 to create a symbol table. */
10541 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10542 BFD_ASSERT (! abfd->output_has_begun);
10543 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10544 goto error_return;
10545
ee75fd95 10546 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
10547 for (o = abfd->sections; o != NULL; o = o->next)
10548 {
d4730f92 10549 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
10550 if ((o->flags & SEC_RELOC) != 0)
10551 {
d4730f92
BS
10552 if (esdo->rel.hdr
10553 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
10554 goto error_return;
10555
d4730f92
BS
10556 if (esdo->rela.hdr
10557 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
10558 goto error_return;
10559 }
10560
10561 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10562 to count upwards while actually outputting the relocations. */
d4730f92
BS
10563 esdo->rel.count = 0;
10564 esdo->rela.count = 0;
c152c796
AM
10565 }
10566
10567 _bfd_elf_assign_file_positions_for_relocs (abfd);
10568
10569 /* We have now assigned file positions for all the sections except
10570 .symtab and .strtab. We start the .symtab section at the current
10571 file position, and write directly to it. We build the .strtab
10572 section in memory. */
10573 bfd_get_symcount (abfd) = 0;
10574 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10575 /* sh_name is set in prep_headers. */
10576 symtab_hdr->sh_type = SHT_SYMTAB;
10577 /* sh_flags, sh_addr and sh_size all start off zero. */
10578 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10579 /* sh_link is set in assign_section_numbers. */
10580 /* sh_info is set below. */
10581 /* sh_offset is set just below. */
72de5009 10582 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796
AM
10583
10584 off = elf_tdata (abfd)->next_file_pos;
10585 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10586
10587 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10588 incorrect. We do not yet know the size of the .symtab section.
10589 We correct next_file_pos below, after we do know the size. */
10590
10591 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10592 continuously seeking to the right position in the file. */
10593 if (! info->keep_memory || max_sym_count < 20)
8b127cbc 10594 flinfo.symbuf_size = 20;
c152c796 10595 else
8b127cbc
AM
10596 flinfo.symbuf_size = max_sym_count;
10597 amt = flinfo.symbuf_size;
c152c796 10598 amt *= bed->s->sizeof_sym;
8b127cbc
AM
10599 flinfo.symbuf = (bfd_byte *) bfd_malloc (amt);
10600 if (flinfo.symbuf == NULL)
c152c796 10601 goto error_return;
4fbb74a6 10602 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
c152c796
AM
10603 {
10604 /* Wild guess at number of output symbols. realloc'd as needed. */
10605 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8b127cbc 10606 flinfo.shndxbuf_size = amt;
c152c796 10607 amt *= sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
10608 flinfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10609 if (flinfo.symshndxbuf == NULL)
c152c796
AM
10610 goto error_return;
10611 }
10612
10613 /* Start writing out the symbol table. The first symbol is always a
10614 dummy symbol. */
10615 if (info->strip != strip_all
10616 || emit_relocs)
10617 {
10618 elfsym.st_value = 0;
10619 elfsym.st_size = 0;
10620 elfsym.st_info = 0;
10621 elfsym.st_other = 0;
10622 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 10623 elfsym.st_target_internal = 0;
8b127cbc 10624 if (elf_link_output_sym (&flinfo, NULL, &elfsym, bfd_und_section_ptr,
6e0b88f1 10625 NULL) != 1)
c152c796
AM
10626 goto error_return;
10627 }
10628
c152c796
AM
10629 /* Output a symbol for each section. We output these even if we are
10630 discarding local symbols, since they are used for relocs. These
10631 symbols have no names. We store the index of each one in the
10632 index field of the section, so that we can find it again when
10633 outputting relocs. */
10634 if (info->strip != strip_all
10635 || emit_relocs)
10636 {
10637 elfsym.st_size = 0;
10638 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10639 elfsym.st_other = 0;
f0b5bb34 10640 elfsym.st_value = 0;
35fc36a8 10641 elfsym.st_target_internal = 0;
c152c796
AM
10642 for (i = 1; i < elf_numsections (abfd); i++)
10643 {
10644 o = bfd_section_from_elf_index (abfd, i);
10645 if (o != NULL)
f0b5bb34
AM
10646 {
10647 o->target_index = bfd_get_symcount (abfd);
10648 elfsym.st_shndx = i;
10649 if (!info->relocatable)
10650 elfsym.st_value = o->vma;
8b127cbc 10651 if (elf_link_output_sym (&flinfo, NULL, &elfsym, o, NULL) != 1)
f0b5bb34
AM
10652 goto error_return;
10653 }
c152c796
AM
10654 }
10655 }
10656
10657 /* Allocate some memory to hold information read in from the input
10658 files. */
10659 if (max_contents_size != 0)
10660 {
8b127cbc
AM
10661 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
10662 if (flinfo.contents == NULL)
c152c796
AM
10663 goto error_return;
10664 }
10665
10666 if (max_external_reloc_size != 0)
10667 {
8b127cbc
AM
10668 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
10669 if (flinfo.external_relocs == NULL)
c152c796
AM
10670 goto error_return;
10671 }
10672
10673 if (max_internal_reloc_count != 0)
10674 {
10675 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10676 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
10677 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
10678 if (flinfo.internal_relocs == NULL)
c152c796
AM
10679 goto error_return;
10680 }
10681
10682 if (max_sym_count != 0)
10683 {
10684 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
10685 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
10686 if (flinfo.external_syms == NULL)
c152c796
AM
10687 goto error_return;
10688
10689 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
10690 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
10691 if (flinfo.internal_syms == NULL)
c152c796
AM
10692 goto error_return;
10693
10694 amt = max_sym_count * sizeof (long);
8b127cbc
AM
10695 flinfo.indices = (long int *) bfd_malloc (amt);
10696 if (flinfo.indices == NULL)
c152c796
AM
10697 goto error_return;
10698
10699 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
10700 flinfo.sections = (asection **) bfd_malloc (amt);
10701 if (flinfo.sections == NULL)
c152c796
AM
10702 goto error_return;
10703 }
10704
10705 if (max_sym_shndx_count != 0)
10706 {
10707 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
10708 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
10709 if (flinfo.locsym_shndx == NULL)
c152c796
AM
10710 goto error_return;
10711 }
10712
10713 if (elf_hash_table (info)->tls_sec)
10714 {
10715 bfd_vma base, end = 0;
10716 asection *sec;
10717
10718 for (sec = elf_hash_table (info)->tls_sec;
10719 sec && (sec->flags & SEC_THREAD_LOCAL);
10720 sec = sec->next)
10721 {
3a800eb9 10722 bfd_size_type size = sec->size;
c152c796 10723
3a800eb9
AM
10724 if (size == 0
10725 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 10726 {
91d6fa6a
NC
10727 struct bfd_link_order *ord = sec->map_tail.link_order;
10728
10729 if (ord != NULL)
10730 size = ord->offset + ord->size;
c152c796
AM
10731 }
10732 end = sec->vma + size;
10733 }
10734 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
10735 /* Only align end of TLS section if static TLS doesn't have special
10736 alignment requirements. */
10737 if (bed->static_tls_alignment == 1)
10738 end = align_power (end,
10739 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
10740 elf_hash_table (info)->tls_size = end - base;
10741 }
10742
0b52efa6
PB
10743 /* Reorder SHF_LINK_ORDER sections. */
10744 for (o = abfd->sections; o != NULL; o = o->next)
10745 {
10746 if (!elf_fixup_link_order (abfd, o))
10747 return FALSE;
10748 }
10749
c152c796
AM
10750 /* Since ELF permits relocations to be against local symbols, we
10751 must have the local symbols available when we do the relocations.
10752 Since we would rather only read the local symbols once, and we
10753 would rather not keep them in memory, we handle all the
10754 relocations for a single input file at the same time.
10755
10756 Unfortunately, there is no way to know the total number of local
10757 symbols until we have seen all of them, and the local symbol
10758 indices precede the global symbol indices. This means that when
10759 we are generating relocatable output, and we see a reloc against
10760 a global symbol, we can not know the symbol index until we have
10761 finished examining all the local symbols to see which ones we are
10762 going to output. To deal with this, we keep the relocations in
10763 memory, and don't output them until the end of the link. This is
10764 an unfortunate waste of memory, but I don't see a good way around
10765 it. Fortunately, it only happens when performing a relocatable
10766 link, which is not the common case. FIXME: If keep_memory is set
10767 we could write the relocs out and then read them again; I don't
10768 know how bad the memory loss will be. */
10769
10770 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10771 sub->output_has_begun = FALSE;
10772 for (o = abfd->sections; o != NULL; o = o->next)
10773 {
8423293d 10774 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10775 {
10776 if (p->type == bfd_indirect_link_order
10777 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10778 == bfd_target_elf_flavour)
10779 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10780 {
10781 if (! sub->output_has_begun)
10782 {
8b127cbc 10783 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
10784 goto error_return;
10785 sub->output_has_begun = TRUE;
10786 }
10787 }
10788 else if (p->type == bfd_section_reloc_link_order
10789 || p->type == bfd_symbol_reloc_link_order)
10790 {
10791 if (! elf_reloc_link_order (abfd, info, o, p))
10792 goto error_return;
10793 }
10794 else
10795 {
10796 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
10797 {
10798 if (p->type == bfd_indirect_link_order
10799 && (bfd_get_flavour (sub)
10800 == bfd_target_elf_flavour)
10801 && (elf_elfheader (sub)->e_ident[EI_CLASS]
10802 != bed->s->elfclass))
10803 {
10804 const char *iclass, *oclass;
10805
10806 if (bed->s->elfclass == ELFCLASS64)
10807 {
10808 iclass = "ELFCLASS32";
10809 oclass = "ELFCLASS64";
10810 }
10811 else
10812 {
10813 iclass = "ELFCLASS64";
10814 oclass = "ELFCLASS32";
10815 }
10816
10817 bfd_set_error (bfd_error_wrong_format);
10818 (*_bfd_error_handler)
10819 (_("%B: file class %s incompatible with %s"),
10820 sub, iclass, oclass);
10821 }
10822
10823 goto error_return;
10824 }
c152c796
AM
10825 }
10826 }
10827 }
10828
c0f00686
L
10829 /* Free symbol buffer if needed. */
10830 if (!info->reduce_memory_overheads)
10831 {
10832 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3fcd97f1
JJ
10833 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10834 && elf_tdata (sub)->symbuf)
c0f00686
L
10835 {
10836 free (elf_tdata (sub)->symbuf);
10837 elf_tdata (sub)->symbuf = NULL;
10838 }
10839 }
10840
ffbc01cc
AM
10841 /* Output a FILE symbol so that following locals are not associated
10842 with the wrong input file. */
10843 memset (&elfsym, 0, sizeof (elfsym));
10844 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10845 elfsym.st_shndx = SHN_ABS;
10846
10847 if (flinfo.filesym_count > 1
10848 && !elf_link_output_sym (&flinfo, NULL, &elfsym,
10849 bfd_und_section_ptr, NULL))
10850 return FALSE;
10851
c152c796
AM
10852 /* Output any global symbols that got converted to local in a
10853 version script or due to symbol visibility. We do this in a
10854 separate step since ELF requires all local symbols to appear
10855 prior to any global symbols. FIXME: We should only do this if
10856 some global symbols were, in fact, converted to become local.
10857 FIXME: Will this work correctly with the Irix 5 linker? */
10858 eoinfo.failed = FALSE;
8b127cbc 10859 eoinfo.flinfo = &flinfo;
c152c796 10860 eoinfo.localsyms = TRUE;
ffbc01cc
AM
10861 eoinfo.need_second_pass = FALSE;
10862 eoinfo.second_pass = FALSE;
7686d77d 10863 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
10864 if (eoinfo.failed)
10865 return FALSE;
10866
ffbc01cc
AM
10867 if (flinfo.filesym_count == 1
10868 && !elf_link_output_sym (&flinfo, NULL, &elfsym,
10869 bfd_und_section_ptr, NULL))
10870 return FALSE;
10871
10872 if (eoinfo.need_second_pass)
10873 {
10874 eoinfo.second_pass = TRUE;
10875 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
10876 if (eoinfo.failed)
10877 return FALSE;
10878 }
10879
4e617b1e
PB
10880 /* If backend needs to output some local symbols not present in the hash
10881 table, do it now. */
10882 if (bed->elf_backend_output_arch_local_syms)
10883 {
6e0b88f1 10884 typedef int (*out_sym_func)
4e617b1e
PB
10885 (void *, const char *, Elf_Internal_Sym *, asection *,
10886 struct elf_link_hash_entry *);
10887
10888 if (! ((*bed->elf_backend_output_arch_local_syms)
8b127cbc 10889 (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym)))
4e617b1e
PB
10890 return FALSE;
10891 }
10892
c152c796
AM
10893 /* That wrote out all the local symbols. Finish up the symbol table
10894 with the global symbols. Even if we want to strip everything we
10895 can, we still need to deal with those global symbols that got
10896 converted to local in a version script. */
10897
10898 /* The sh_info field records the index of the first non local symbol. */
10899 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10900
10901 if (dynamic
8b127cbc
AM
10902 && flinfo.dynsym_sec != NULL
10903 && flinfo.dynsym_sec->output_section != bfd_abs_section_ptr)
c152c796
AM
10904 {
10905 Elf_Internal_Sym sym;
8b127cbc 10906 bfd_byte *dynsym = flinfo.dynsym_sec->contents;
c152c796
AM
10907 long last_local = 0;
10908
10909 /* Write out the section symbols for the output sections. */
67687978 10910 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
10911 {
10912 asection *s;
10913
10914 sym.st_size = 0;
10915 sym.st_name = 0;
10916 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10917 sym.st_other = 0;
35fc36a8 10918 sym.st_target_internal = 0;
c152c796
AM
10919
10920 for (s = abfd->sections; s != NULL; s = s->next)
10921 {
10922 int indx;
10923 bfd_byte *dest;
10924 long dynindx;
10925
c152c796 10926 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
10927 if (dynindx <= 0)
10928 continue;
10929 indx = elf_section_data (s)->this_idx;
c152c796
AM
10930 BFD_ASSERT (indx > 0);
10931 sym.st_shndx = indx;
c0d5a53d
L
10932 if (! check_dynsym (abfd, &sym))
10933 return FALSE;
c152c796
AM
10934 sym.st_value = s->vma;
10935 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
10936 if (last_local < dynindx)
10937 last_local = dynindx;
c152c796
AM
10938 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10939 }
c152c796
AM
10940 }
10941
10942 /* Write out the local dynsyms. */
10943 if (elf_hash_table (info)->dynlocal)
10944 {
10945 struct elf_link_local_dynamic_entry *e;
10946 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10947 {
10948 asection *s;
10949 bfd_byte *dest;
10950
935bd1e0 10951 /* Copy the internal symbol and turn off visibility.
c152c796
AM
10952 Note that we saved a word of storage and overwrote
10953 the original st_name with the dynstr_index. */
10954 sym = e->isym;
935bd1e0 10955 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 10956
cb33740c
AM
10957 s = bfd_section_from_elf_index (e->input_bfd,
10958 e->isym.st_shndx);
10959 if (s != NULL)
c152c796 10960 {
c152c796
AM
10961 sym.st_shndx =
10962 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
10963 if (! check_dynsym (abfd, &sym))
10964 return FALSE;
c152c796
AM
10965 sym.st_value = (s->output_section->vma
10966 + s->output_offset
10967 + e->isym.st_value);
10968 }
10969
10970 if (last_local < e->dynindx)
10971 last_local = e->dynindx;
10972
10973 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10974 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10975 }
10976 }
10977
8b127cbc 10978 elf_section_data (flinfo.dynsym_sec->output_section)->this_hdr.sh_info =
c152c796
AM
10979 last_local + 1;
10980 }
10981
10982 /* We get the global symbols from the hash table. */
10983 eoinfo.failed = FALSE;
10984 eoinfo.localsyms = FALSE;
8b127cbc 10985 eoinfo.flinfo = &flinfo;
7686d77d 10986 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
10987 if (eoinfo.failed)
10988 return FALSE;
10989
10990 /* If backend needs to output some symbols not present in the hash
10991 table, do it now. */
10992 if (bed->elf_backend_output_arch_syms)
10993 {
6e0b88f1 10994 typedef int (*out_sym_func)
c152c796
AM
10995 (void *, const char *, Elf_Internal_Sym *, asection *,
10996 struct elf_link_hash_entry *);
10997
10998 if (! ((*bed->elf_backend_output_arch_syms)
8b127cbc 10999 (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym)))
c152c796
AM
11000 return FALSE;
11001 }
11002
11003 /* Flush all symbols to the file. */
8b127cbc 11004 if (! elf_link_flush_output_syms (&flinfo, bed))
c152c796
AM
11005 return FALSE;
11006
11007 /* Now we know the size of the symtab section. */
11008 off += symtab_hdr->sh_size;
11009
11010 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
11011 if (symtab_shndx_hdr->sh_name != 0)
11012 {
11013 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11014 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11015 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11016 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11017 symtab_shndx_hdr->sh_size = amt;
11018
11019 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11020 off, TRUE);
11021
11022 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8b127cbc 11023 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
c152c796
AM
11024 return FALSE;
11025 }
11026
11027
11028 /* Finish up and write out the symbol string table (.strtab)
11029 section. */
11030 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11031 /* sh_name was set in prep_headers. */
11032 symstrtab_hdr->sh_type = SHT_STRTAB;
11033 symstrtab_hdr->sh_flags = 0;
11034 symstrtab_hdr->sh_addr = 0;
8b127cbc 11035 symstrtab_hdr->sh_size = _bfd_stringtab_size (flinfo.symstrtab);
c152c796
AM
11036 symstrtab_hdr->sh_entsize = 0;
11037 symstrtab_hdr->sh_link = 0;
11038 symstrtab_hdr->sh_info = 0;
11039 /* sh_offset is set just below. */
11040 symstrtab_hdr->sh_addralign = 1;
11041
11042 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
11043 elf_tdata (abfd)->next_file_pos = off;
11044
11045 if (bfd_get_symcount (abfd) > 0)
11046 {
11047 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8b127cbc 11048 || ! _bfd_stringtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
11049 return FALSE;
11050 }
11051
11052 /* Adjust the relocs to have the correct symbol indices. */
11053 for (o = abfd->sections; o != NULL; o = o->next)
11054 {
d4730f92 11055 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11056 if ((o->flags & SEC_RELOC) == 0)
11057 continue;
11058
d4730f92
BS
11059 if (esdo->rel.hdr != NULL)
11060 elf_link_adjust_relocs (abfd, &esdo->rel);
11061 if (esdo->rela.hdr != NULL)
11062 elf_link_adjust_relocs (abfd, &esdo->rela);
c152c796
AM
11063
11064 /* Set the reloc_count field to 0 to prevent write_relocs from
11065 trying to swap the relocs out itself. */
11066 o->reloc_count = 0;
11067 }
11068
11069 if (dynamic && info->combreloc && dynobj != NULL)
11070 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11071
11072 /* If we are linking against a dynamic object, or generating a
11073 shared library, finish up the dynamic linking information. */
11074 if (dynamic)
11075 {
11076 bfd_byte *dyncon, *dynconend;
11077
11078 /* Fix up .dynamic entries. */
3d4d4302 11079 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
11080 BFD_ASSERT (o != NULL);
11081
11082 dyncon = o->contents;
eea6121a 11083 dynconend = o->contents + o->size;
c152c796
AM
11084 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11085 {
11086 Elf_Internal_Dyn dyn;
11087 const char *name;
11088 unsigned int type;
11089
11090 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11091
11092 switch (dyn.d_tag)
11093 {
11094 default:
11095 continue;
11096 case DT_NULL:
11097 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11098 {
11099 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11100 {
11101 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11102 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11103 default: continue;
11104 }
11105 dyn.d_un.d_val = relativecount;
11106 relativecount = 0;
11107 break;
11108 }
11109 continue;
11110
11111 case DT_INIT:
11112 name = info->init_function;
11113 goto get_sym;
11114 case DT_FINI:
11115 name = info->fini_function;
11116 get_sym:
11117 {
11118 struct elf_link_hash_entry *h;
11119
11120 h = elf_link_hash_lookup (elf_hash_table (info), name,
11121 FALSE, FALSE, TRUE);
11122 if (h != NULL
11123 && (h->root.type == bfd_link_hash_defined
11124 || h->root.type == bfd_link_hash_defweak))
11125 {
bef26483 11126 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11127 o = h->root.u.def.section;
11128 if (o->output_section != NULL)
bef26483 11129 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11130 + o->output_offset);
11131 else
11132 {
11133 /* The symbol is imported from another shared
11134 library and does not apply to this one. */
bef26483 11135 dyn.d_un.d_ptr = 0;
c152c796
AM
11136 }
11137 break;
11138 }
11139 }
11140 continue;
11141
11142 case DT_PREINIT_ARRAYSZ:
11143 name = ".preinit_array";
11144 goto get_size;
11145 case DT_INIT_ARRAYSZ:
11146 name = ".init_array";
11147 goto get_size;
11148 case DT_FINI_ARRAYSZ:
11149 name = ".fini_array";
11150 get_size:
11151 o = bfd_get_section_by_name (abfd, name);
11152 if (o == NULL)
11153 {
11154 (*_bfd_error_handler)
d003868e 11155 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11156 goto error_return;
11157 }
eea6121a 11158 if (o->size == 0)
c152c796
AM
11159 (*_bfd_error_handler)
11160 (_("warning: %s section has zero size"), name);
eea6121a 11161 dyn.d_un.d_val = o->size;
c152c796
AM
11162 break;
11163
11164 case DT_PREINIT_ARRAY:
11165 name = ".preinit_array";
11166 goto get_vma;
11167 case DT_INIT_ARRAY:
11168 name = ".init_array";
11169 goto get_vma;
11170 case DT_FINI_ARRAY:
11171 name = ".fini_array";
11172 goto get_vma;
11173
11174 case DT_HASH:
11175 name = ".hash";
11176 goto get_vma;
fdc90cb4
JJ
11177 case DT_GNU_HASH:
11178 name = ".gnu.hash";
11179 goto get_vma;
c152c796
AM
11180 case DT_STRTAB:
11181 name = ".dynstr";
11182 goto get_vma;
11183 case DT_SYMTAB:
11184 name = ".dynsym";
11185 goto get_vma;
11186 case DT_VERDEF:
11187 name = ".gnu.version_d";
11188 goto get_vma;
11189 case DT_VERNEED:
11190 name = ".gnu.version_r";
11191 goto get_vma;
11192 case DT_VERSYM:
11193 name = ".gnu.version";
11194 get_vma:
11195 o = bfd_get_section_by_name (abfd, name);
11196 if (o == NULL)
11197 {
11198 (*_bfd_error_handler)
d003868e 11199 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11200 goto error_return;
11201 }
894891db
NC
11202 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11203 {
11204 (*_bfd_error_handler)
11205 (_("warning: section '%s' is being made into a note"), name);
11206 bfd_set_error (bfd_error_nonrepresentable_section);
11207 goto error_return;
11208 }
c152c796
AM
11209 dyn.d_un.d_ptr = o->vma;
11210 break;
11211
11212 case DT_REL:
11213 case DT_RELA:
11214 case DT_RELSZ:
11215 case DT_RELASZ:
11216 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11217 type = SHT_REL;
11218 else
11219 type = SHT_RELA;
11220 dyn.d_un.d_val = 0;
bef26483 11221 dyn.d_un.d_ptr = 0;
c152c796
AM
11222 for (i = 1; i < elf_numsections (abfd); i++)
11223 {
11224 Elf_Internal_Shdr *hdr;
11225
11226 hdr = elf_elfsections (abfd)[i];
11227 if (hdr->sh_type == type
11228 && (hdr->sh_flags & SHF_ALLOC) != 0)
11229 {
11230 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11231 dyn.d_un.d_val += hdr->sh_size;
11232 else
11233 {
bef26483
AM
11234 if (dyn.d_un.d_ptr == 0
11235 || hdr->sh_addr < dyn.d_un.d_ptr)
11236 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
11237 }
11238 }
11239 }
11240 break;
11241 }
11242 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11243 }
11244 }
11245
11246 /* If we have created any dynamic sections, then output them. */
11247 if (dynobj != NULL)
11248 {
11249 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11250 goto error_return;
11251
943284cc 11252 /* Check for DT_TEXTREL (late, in case the backend removes it). */
be7b303d
AM
11253 if (((info->warn_shared_textrel && info->shared)
11254 || info->error_textrel)
3d4d4302 11255 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
11256 {
11257 bfd_byte *dyncon, *dynconend;
11258
943284cc
DJ
11259 dyncon = o->contents;
11260 dynconend = o->contents + o->size;
11261 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11262 {
11263 Elf_Internal_Dyn dyn;
11264
11265 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11266
11267 if (dyn.d_tag == DT_TEXTREL)
11268 {
c192a133
AM
11269 if (info->error_textrel)
11270 info->callbacks->einfo
11271 (_("%P%X: read-only segment has dynamic relocations.\n"));
11272 else
11273 info->callbacks->einfo
11274 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
11275 break;
11276 }
11277 }
11278 }
11279
c152c796
AM
11280 for (o = dynobj->sections; o != NULL; o = o->next)
11281 {
11282 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11283 || o->size == 0
c152c796
AM
11284 || o->output_section == bfd_abs_section_ptr)
11285 continue;
11286 if ((o->flags & SEC_LINKER_CREATED) == 0)
11287 {
11288 /* At this point, we are only interested in sections
11289 created by _bfd_elf_link_create_dynamic_sections. */
11290 continue;
11291 }
3722b82f
AM
11292 if (elf_hash_table (info)->stab_info.stabstr == o)
11293 continue;
eea6121a
AM
11294 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11295 continue;
3d4d4302 11296 if (strcmp (o->name, ".dynstr") != 0)
c152c796 11297 {
5dabe785 11298 /* FIXME: octets_per_byte. */
c152c796
AM
11299 if (! bfd_set_section_contents (abfd, o->output_section,
11300 o->contents,
11301 (file_ptr) o->output_offset,
eea6121a 11302 o->size))
c152c796
AM
11303 goto error_return;
11304 }
11305 else
11306 {
11307 /* The contents of the .dynstr section are actually in a
11308 stringtab. */
11309 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11310 if (bfd_seek (abfd, off, SEEK_SET) != 0
11311 || ! _bfd_elf_strtab_emit (abfd,
11312 elf_hash_table (info)->dynstr))
11313 goto error_return;
11314 }
11315 }
11316 }
11317
11318 if (info->relocatable)
11319 {
11320 bfd_boolean failed = FALSE;
11321
11322 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11323 if (failed)
11324 goto error_return;
11325 }
11326
11327 /* If we have optimized stabs strings, output them. */
3722b82f 11328 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
11329 {
11330 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11331 goto error_return;
11332 }
11333
11334 if (info->eh_frame_hdr)
11335 {
11336 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11337 goto error_return;
11338 }
11339
8b127cbc
AM
11340 if (flinfo.symstrtab != NULL)
11341 _bfd_stringtab_free (flinfo.symstrtab);
11342 if (flinfo.contents != NULL)
11343 free (flinfo.contents);
11344 if (flinfo.external_relocs != NULL)
11345 free (flinfo.external_relocs);
11346 if (flinfo.internal_relocs != NULL)
11347 free (flinfo.internal_relocs);
11348 if (flinfo.external_syms != NULL)
11349 free (flinfo.external_syms);
11350 if (flinfo.locsym_shndx != NULL)
11351 free (flinfo.locsym_shndx);
11352 if (flinfo.internal_syms != NULL)
11353 free (flinfo.internal_syms);
11354 if (flinfo.indices != NULL)
11355 free (flinfo.indices);
11356 if (flinfo.sections != NULL)
11357 free (flinfo.sections);
11358 if (flinfo.symbuf != NULL)
11359 free (flinfo.symbuf);
11360 if (flinfo.symshndxbuf != NULL)
11361 free (flinfo.symshndxbuf);
c152c796
AM
11362 for (o = abfd->sections; o != NULL; o = o->next)
11363 {
d4730f92
BS
11364 struct bfd_elf_section_data *esdo = elf_section_data (o);
11365 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11366 free (esdo->rel.hashes);
11367 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11368 free (esdo->rela.hashes);
c152c796
AM
11369 }
11370
11371 elf_tdata (abfd)->linker = TRUE;
11372
104d59d1
JM
11373 if (attr_section)
11374 {
a50b1753 11375 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 11376 if (contents == NULL)
d0f16d5e 11377 return FALSE; /* Bail out and fail. */
104d59d1
JM
11378 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11379 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11380 free (contents);
11381 }
11382
c152c796
AM
11383 return TRUE;
11384
11385 error_return:
8b127cbc
AM
11386 if (flinfo.symstrtab != NULL)
11387 _bfd_stringtab_free (flinfo.symstrtab);
11388 if (flinfo.contents != NULL)
11389 free (flinfo.contents);
11390 if (flinfo.external_relocs != NULL)
11391 free (flinfo.external_relocs);
11392 if (flinfo.internal_relocs != NULL)
11393 free (flinfo.internal_relocs);
11394 if (flinfo.external_syms != NULL)
11395 free (flinfo.external_syms);
11396 if (flinfo.locsym_shndx != NULL)
11397 free (flinfo.locsym_shndx);
11398 if (flinfo.internal_syms != NULL)
11399 free (flinfo.internal_syms);
11400 if (flinfo.indices != NULL)
11401 free (flinfo.indices);
11402 if (flinfo.sections != NULL)
11403 free (flinfo.sections);
11404 if (flinfo.symbuf != NULL)
11405 free (flinfo.symbuf);
11406 if (flinfo.symshndxbuf != NULL)
11407 free (flinfo.symshndxbuf);
c152c796
AM
11408 for (o = abfd->sections; o != NULL; o = o->next)
11409 {
d4730f92
BS
11410 struct bfd_elf_section_data *esdo = elf_section_data (o);
11411 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11412 free (esdo->rel.hashes);
11413 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11414 free (esdo->rela.hashes);
c152c796
AM
11415 }
11416
11417 return FALSE;
11418}
11419\f
5241d853
RS
11420/* Initialize COOKIE for input bfd ABFD. */
11421
11422static bfd_boolean
11423init_reloc_cookie (struct elf_reloc_cookie *cookie,
11424 struct bfd_link_info *info, bfd *abfd)
11425{
11426 Elf_Internal_Shdr *symtab_hdr;
11427 const struct elf_backend_data *bed;
11428
11429 bed = get_elf_backend_data (abfd);
11430 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11431
11432 cookie->abfd = abfd;
11433 cookie->sym_hashes = elf_sym_hashes (abfd);
11434 cookie->bad_symtab = elf_bad_symtab (abfd);
11435 if (cookie->bad_symtab)
11436 {
11437 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11438 cookie->extsymoff = 0;
11439 }
11440 else
11441 {
11442 cookie->locsymcount = symtab_hdr->sh_info;
11443 cookie->extsymoff = symtab_hdr->sh_info;
11444 }
11445
11446 if (bed->s->arch_size == 32)
11447 cookie->r_sym_shift = 8;
11448 else
11449 cookie->r_sym_shift = 32;
11450
11451 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11452 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11453 {
11454 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11455 cookie->locsymcount, 0,
11456 NULL, NULL, NULL);
11457 if (cookie->locsyms == NULL)
11458 {
11459 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11460 return FALSE;
11461 }
11462 if (info->keep_memory)
11463 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11464 }
11465 return TRUE;
11466}
11467
11468/* Free the memory allocated by init_reloc_cookie, if appropriate. */
11469
11470static void
11471fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11472{
11473 Elf_Internal_Shdr *symtab_hdr;
11474
11475 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11476 if (cookie->locsyms != NULL
11477 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11478 free (cookie->locsyms);
11479}
11480
11481/* Initialize the relocation information in COOKIE for input section SEC
11482 of input bfd ABFD. */
11483
11484static bfd_boolean
11485init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11486 struct bfd_link_info *info, bfd *abfd,
11487 asection *sec)
11488{
11489 const struct elf_backend_data *bed;
11490
11491 if (sec->reloc_count == 0)
11492 {
11493 cookie->rels = NULL;
11494 cookie->relend = NULL;
11495 }
11496 else
11497 {
11498 bed = get_elf_backend_data (abfd);
11499
11500 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11501 info->keep_memory);
11502 if (cookie->rels == NULL)
11503 return FALSE;
11504 cookie->rel = cookie->rels;
11505 cookie->relend = (cookie->rels
11506 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11507 }
11508 cookie->rel = cookie->rels;
11509 return TRUE;
11510}
11511
11512/* Free the memory allocated by init_reloc_cookie_rels,
11513 if appropriate. */
11514
11515static void
11516fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11517 asection *sec)
11518{
11519 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11520 free (cookie->rels);
11521}
11522
11523/* Initialize the whole of COOKIE for input section SEC. */
11524
11525static bfd_boolean
11526init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11527 struct bfd_link_info *info,
11528 asection *sec)
11529{
11530 if (!init_reloc_cookie (cookie, info, sec->owner))
11531 goto error1;
11532 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11533 goto error2;
11534 return TRUE;
11535
11536 error2:
11537 fini_reloc_cookie (cookie, sec->owner);
11538 error1:
11539 return FALSE;
11540}
11541
11542/* Free the memory allocated by init_reloc_cookie_for_section,
11543 if appropriate. */
11544
11545static void
11546fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11547 asection *sec)
11548{
11549 fini_reloc_cookie_rels (cookie, sec);
11550 fini_reloc_cookie (cookie, sec->owner);
11551}
11552\f
c152c796
AM
11553/* Garbage collect unused sections. */
11554
07adf181
AM
11555/* Default gc_mark_hook. */
11556
11557asection *
11558_bfd_elf_gc_mark_hook (asection *sec,
11559 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11560 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11561 struct elf_link_hash_entry *h,
11562 Elf_Internal_Sym *sym)
11563{
bde6f3eb
L
11564 const char *sec_name;
11565
07adf181
AM
11566 if (h != NULL)
11567 {
11568 switch (h->root.type)
11569 {
11570 case bfd_link_hash_defined:
11571 case bfd_link_hash_defweak:
11572 return h->root.u.def.section;
11573
11574 case bfd_link_hash_common:
11575 return h->root.u.c.p->section;
11576
bde6f3eb
L
11577 case bfd_link_hash_undefined:
11578 case bfd_link_hash_undefweak:
11579 /* To work around a glibc bug, keep all XXX input sections
11580 when there is an as yet undefined reference to __start_XXX
11581 or __stop_XXX symbols. The linker will later define such
11582 symbols for orphan input sections that have a name
11583 representable as a C identifier. */
11584 if (strncmp (h->root.root.string, "__start_", 8) == 0)
11585 sec_name = h->root.root.string + 8;
11586 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
11587 sec_name = h->root.root.string + 7;
11588 else
11589 sec_name = NULL;
11590
11591 if (sec_name && *sec_name != '\0')
11592 {
11593 bfd *i;
11594
11595 for (i = info->input_bfds; i; i = i->link_next)
11596 {
11597 sec = bfd_get_section_by_name (i, sec_name);
11598 if (sec)
11599 sec->flags |= SEC_KEEP;
11600 }
11601 }
11602 break;
11603
07adf181
AM
11604 default:
11605 break;
11606 }
11607 }
11608 else
11609 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11610
11611 return NULL;
11612}
11613
5241d853
RS
11614/* COOKIE->rel describes a relocation against section SEC, which is
11615 a section we've decided to keep. Return the section that contains
11616 the relocation symbol, or NULL if no section contains it. */
11617
11618asection *
11619_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11620 elf_gc_mark_hook_fn gc_mark_hook,
11621 struct elf_reloc_cookie *cookie)
11622{
11623 unsigned long r_symndx;
11624 struct elf_link_hash_entry *h;
11625
11626 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 11627 if (r_symndx == STN_UNDEF)
5241d853
RS
11628 return NULL;
11629
11630 if (r_symndx >= cookie->locsymcount
11631 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11632 {
11633 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11634 while (h->root.type == bfd_link_hash_indirect
11635 || h->root.type == bfd_link_hash_warning)
11636 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 11637 h->mark = 1;
4e6b54a6
AM
11638 /* If this symbol is weak and there is a non-weak definition, we
11639 keep the non-weak definition because many backends put
11640 dynamic reloc info on the non-weak definition for code
11641 handling copy relocs. */
11642 if (h->u.weakdef != NULL)
11643 h->u.weakdef->mark = 1;
5241d853
RS
11644 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11645 }
11646
11647 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11648 &cookie->locsyms[r_symndx]);
11649}
11650
11651/* COOKIE->rel describes a relocation against section SEC, which is
11652 a section we've decided to keep. Mark the section that contains
9d0a14d3 11653 the relocation symbol. */
5241d853
RS
11654
11655bfd_boolean
11656_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11657 asection *sec,
11658 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 11659 struct elf_reloc_cookie *cookie)
5241d853
RS
11660{
11661 asection *rsec;
11662
11663 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11664 if (rsec && !rsec->gc_mark)
11665 {
a66eed7a
AM
11666 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
11667 || (rsec->owner->flags & DYNAMIC) != 0)
5241d853 11668 rsec->gc_mark = 1;
5241d853
RS
11669 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11670 return FALSE;
11671 }
11672 return TRUE;
11673}
11674
07adf181
AM
11675/* The mark phase of garbage collection. For a given section, mark
11676 it and any sections in this section's group, and all the sections
11677 which define symbols to which it refers. */
11678
ccfa59ea
AM
11679bfd_boolean
11680_bfd_elf_gc_mark (struct bfd_link_info *info,
11681 asection *sec,
6a5bb875 11682 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
11683{
11684 bfd_boolean ret;
9d0a14d3 11685 asection *group_sec, *eh_frame;
c152c796
AM
11686
11687 sec->gc_mark = 1;
11688
11689 /* Mark all the sections in the group. */
11690 group_sec = elf_section_data (sec)->next_in_group;
11691 if (group_sec && !group_sec->gc_mark)
ccfa59ea 11692 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
11693 return FALSE;
11694
11695 /* Look through the section relocs. */
11696 ret = TRUE;
9d0a14d3
RS
11697 eh_frame = elf_eh_frame_section (sec->owner);
11698 if ((sec->flags & SEC_RELOC) != 0
11699 && sec->reloc_count > 0
11700 && sec != eh_frame)
c152c796 11701 {
5241d853 11702 struct elf_reloc_cookie cookie;
c152c796 11703
5241d853
RS
11704 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11705 ret = FALSE;
c152c796 11706 else
c152c796 11707 {
5241d853 11708 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 11709 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
11710 {
11711 ret = FALSE;
11712 break;
11713 }
11714 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
11715 }
11716 }
9d0a14d3
RS
11717
11718 if (ret && eh_frame && elf_fde_list (sec))
11719 {
11720 struct elf_reloc_cookie cookie;
11721
11722 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11723 ret = FALSE;
11724 else
11725 {
11726 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11727 gc_mark_hook, &cookie))
11728 ret = FALSE;
11729 fini_reloc_cookie_for_section (&cookie, eh_frame);
11730 }
11731 }
11732
c152c796
AM
11733 return ret;
11734}
11735
7f6ab9f8
AM
11736/* Keep debug and special sections. */
11737
11738bfd_boolean
11739_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
11740 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
11741{
11742 bfd *ibfd;
11743
11744 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11745 {
11746 asection *isec;
11747 bfd_boolean some_kept;
11748
11749 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
11750 continue;
11751
11752 /* Ensure all linker created sections are kept, and see whether
11753 any other section is already marked. */
11754 some_kept = FALSE;
11755 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
11756 {
11757 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11758 isec->gc_mark = 1;
11759 else if (isec->gc_mark)
11760 some_kept = TRUE;
11761 }
11762
11763 /* If no section in this file will be kept, then we can
11764 toss out debug sections. */
11765 if (!some_kept)
11766 continue;
11767
11768 /* Keep debug and special sections like .comment when they are
c227efa6 11769 not part of a group, or when we have single-member groups. */
7f6ab9f8 11770 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
c227efa6
AM
11771 if ((elf_next_in_group (isec) == NULL
11772 || elf_next_in_group (isec) == isec)
7f6ab9f8
AM
11773 && ((isec->flags & SEC_DEBUGGING) != 0
11774 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0))
11775 isec->gc_mark = 1;
11776 }
11777 return TRUE;
11778}
11779
c152c796
AM
11780/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11781
c17d87de
NC
11782struct elf_gc_sweep_symbol_info
11783{
ccabcbe5
AM
11784 struct bfd_link_info *info;
11785 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11786 bfd_boolean);
11787};
11788
c152c796 11789static bfd_boolean
ccabcbe5 11790elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 11791{
1d5316ab
AM
11792 if (!h->mark
11793 && (((h->root.type == bfd_link_hash_defined
11794 || h->root.type == bfd_link_hash_defweak)
6673f753
AM
11795 && !(h->def_regular
11796 && h->root.u.def.section->gc_mark))
1d5316ab
AM
11797 || h->root.type == bfd_link_hash_undefined
11798 || h->root.type == bfd_link_hash_undefweak))
11799 {
11800 struct elf_gc_sweep_symbol_info *inf;
11801
11802 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 11803 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
11804 h->def_regular = 0;
11805 h->ref_regular = 0;
11806 h->ref_regular_nonweak = 0;
ccabcbe5 11807 }
c152c796
AM
11808
11809 return TRUE;
11810}
11811
11812/* The sweep phase of garbage collection. Remove all garbage sections. */
11813
11814typedef bfd_boolean (*gc_sweep_hook_fn)
11815 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11816
11817static bfd_boolean
ccabcbe5 11818elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
11819{
11820 bfd *sub;
ccabcbe5
AM
11821 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11822 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11823 unsigned long section_sym_count;
11824 struct elf_gc_sweep_symbol_info sweep_info;
c152c796
AM
11825
11826 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11827 {
11828 asection *o;
11829
11830 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11831 continue;
11832
11833 for (o = sub->sections; o != NULL; o = o->next)
11834 {
a33dafc3
L
11835 /* When any section in a section group is kept, we keep all
11836 sections in the section group. If the first member of
11837 the section group is excluded, we will also exclude the
11838 group section. */
11839 if (o->flags & SEC_GROUP)
11840 {
11841 asection *first = elf_next_in_group (o);
11842 o->gc_mark = first->gc_mark;
11843 }
c152c796
AM
11844
11845 if (o->gc_mark)
11846 continue;
11847
11848 /* Skip sweeping sections already excluded. */
11849 if (o->flags & SEC_EXCLUDE)
11850 continue;
11851
11852 /* Since this is early in the link process, it is simple
11853 to remove a section from the output. */
11854 o->flags |= SEC_EXCLUDE;
11855
c55fe096 11856 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
11857 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11858
c152c796
AM
11859 /* But we also have to update some of the relocation
11860 info we collected before. */
11861 if (gc_sweep_hook
e8aaee2a
AM
11862 && (o->flags & SEC_RELOC) != 0
11863 && o->reloc_count > 0
11864 && !bfd_is_abs_section (o->output_section))
c152c796
AM
11865 {
11866 Elf_Internal_Rela *internal_relocs;
11867 bfd_boolean r;
11868
11869 internal_relocs
11870 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11871 info->keep_memory);
11872 if (internal_relocs == NULL)
11873 return FALSE;
11874
11875 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11876
11877 if (elf_section_data (o)->relocs != internal_relocs)
11878 free (internal_relocs);
11879
11880 if (!r)
11881 return FALSE;
11882 }
11883 }
11884 }
11885
11886 /* Remove the symbols that were in the swept sections from the dynamic
11887 symbol table. GCFIXME: Anyone know how to get them out of the
11888 static symbol table as well? */
ccabcbe5
AM
11889 sweep_info.info = info;
11890 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11891 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11892 &sweep_info);
c152c796 11893
ccabcbe5 11894 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
11895 return TRUE;
11896}
11897
11898/* Propagate collected vtable information. This is called through
11899 elf_link_hash_traverse. */
11900
11901static bfd_boolean
11902elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11903{
c152c796 11904 /* Those that are not vtables. */
f6e332e6 11905 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
11906 return TRUE;
11907
11908 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 11909 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
11910 return TRUE;
11911
11912 /* If we've already been done, exit. */
f6e332e6 11913 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
11914 return TRUE;
11915
11916 /* Make sure the parent's table is up to date. */
f6e332e6 11917 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 11918
f6e332e6 11919 if (h->vtable->used == NULL)
c152c796
AM
11920 {
11921 /* None of this table's entries were referenced. Re-use the
11922 parent's table. */
f6e332e6
AM
11923 h->vtable->used = h->vtable->parent->vtable->used;
11924 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
11925 }
11926 else
11927 {
11928 size_t n;
11929 bfd_boolean *cu, *pu;
11930
11931 /* Or the parent's entries into ours. */
f6e332e6 11932 cu = h->vtable->used;
c152c796 11933 cu[-1] = TRUE;
f6e332e6 11934 pu = h->vtable->parent->vtable->used;
c152c796
AM
11935 if (pu != NULL)
11936 {
11937 const struct elf_backend_data *bed;
11938 unsigned int log_file_align;
11939
11940 bed = get_elf_backend_data (h->root.u.def.section->owner);
11941 log_file_align = bed->s->log_file_align;
f6e332e6 11942 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
11943 while (n--)
11944 {
11945 if (*pu)
11946 *cu = TRUE;
11947 pu++;
11948 cu++;
11949 }
11950 }
11951 }
11952
11953 return TRUE;
11954}
11955
11956static bfd_boolean
11957elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11958{
11959 asection *sec;
11960 bfd_vma hstart, hend;
11961 Elf_Internal_Rela *relstart, *relend, *rel;
11962 const struct elf_backend_data *bed;
11963 unsigned int log_file_align;
11964
c152c796
AM
11965 /* Take care of both those symbols that do not describe vtables as
11966 well as those that are not loaded. */
f6e332e6 11967 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
11968 return TRUE;
11969
11970 BFD_ASSERT (h->root.type == bfd_link_hash_defined
11971 || h->root.type == bfd_link_hash_defweak);
11972
11973 sec = h->root.u.def.section;
11974 hstart = h->root.u.def.value;
11975 hend = hstart + h->size;
11976
11977 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11978 if (!relstart)
11979 return *(bfd_boolean *) okp = FALSE;
11980 bed = get_elf_backend_data (sec->owner);
11981 log_file_align = bed->s->log_file_align;
11982
11983 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11984
11985 for (rel = relstart; rel < relend; ++rel)
11986 if (rel->r_offset >= hstart && rel->r_offset < hend)
11987 {
11988 /* If the entry is in use, do nothing. */
f6e332e6
AM
11989 if (h->vtable->used
11990 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
11991 {
11992 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 11993 if (h->vtable->used[entry])
c152c796
AM
11994 continue;
11995 }
11996 /* Otherwise, kill it. */
11997 rel->r_offset = rel->r_info = rel->r_addend = 0;
11998 }
11999
12000 return TRUE;
12001}
12002
87538722
AM
12003/* Mark sections containing dynamically referenced symbols. When
12004 building shared libraries, we must assume that any visible symbol is
12005 referenced. */
715df9b8 12006
64d03ab5
AM
12007bfd_boolean
12008bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 12009{
87538722
AM
12010 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12011
715df9b8
EB
12012 if ((h->root.type == bfd_link_hash_defined
12013 || h->root.type == bfd_link_hash_defweak)
87538722 12014 && (h->ref_dynamic
409ff343 12015 || ((!info->executable || info->export_dynamic)
87538722
AM
12016 && h->def_regular
12017 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 12018 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
54e8959c
L
12019 && (strchr (h->root.root.string, ELF_VER_CHR) != NULL
12020 || !bfd_hide_sym_by_version (info->version_info,
12021 h->root.root.string)))))
715df9b8
EB
12022 h->root.u.def.section->flags |= SEC_KEEP;
12023
12024 return TRUE;
12025}
3b36f7e6 12026
74f0fb50
AM
12027/* Keep all sections containing symbols undefined on the command-line,
12028 and the section containing the entry symbol. */
12029
12030void
12031_bfd_elf_gc_keep (struct bfd_link_info *info)
12032{
12033 struct bfd_sym_chain *sym;
12034
12035 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12036 {
12037 struct elf_link_hash_entry *h;
12038
12039 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12040 FALSE, FALSE, FALSE);
12041
12042 if (h != NULL
12043 && (h->root.type == bfd_link_hash_defined
12044 || h->root.type == bfd_link_hash_defweak)
12045 && !bfd_is_abs_section (h->root.u.def.section))
12046 h->root.u.def.section->flags |= SEC_KEEP;
12047 }
12048}
12049
c152c796
AM
12050/* Do mark and sweep of unused sections. */
12051
12052bfd_boolean
12053bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12054{
12055 bfd_boolean ok = TRUE;
12056 bfd *sub;
6a5bb875 12057 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 12058 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 12059
64d03ab5 12060 if (!bed->can_gc_sections
715df9b8 12061 || !is_elf_hash_table (info->hash))
c152c796
AM
12062 {
12063 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12064 return TRUE;
12065 }
12066
74f0fb50
AM
12067 bed->gc_keep (info);
12068
9d0a14d3
RS
12069 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12070 at the .eh_frame section if we can mark the FDEs individually. */
12071 _bfd_elf_begin_eh_frame_parsing (info);
12072 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
12073 {
12074 asection *sec;
12075 struct elf_reloc_cookie cookie;
12076
12077 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 12078 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
12079 {
12080 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
12081 if (elf_section_data (sec)->sec_info
12082 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
12083 elf_eh_frame_section (sub) = sec;
12084 fini_reloc_cookie_for_section (&cookie, sec);
9a2a56cc 12085 sec = bfd_get_next_section_by_name (sec);
9d0a14d3
RS
12086 }
12087 }
12088 _bfd_elf_end_eh_frame_parsing (info);
12089
c152c796
AM
12090 /* Apply transitive closure to the vtable entry usage info. */
12091 elf_link_hash_traverse (elf_hash_table (info),
12092 elf_gc_propagate_vtable_entries_used,
12093 &ok);
12094 if (!ok)
12095 return FALSE;
12096
12097 /* Kill the vtable relocations that were not used. */
12098 elf_link_hash_traverse (elf_hash_table (info),
12099 elf_gc_smash_unused_vtentry_relocs,
12100 &ok);
12101 if (!ok)
12102 return FALSE;
12103
715df9b8
EB
12104 /* Mark dynamically referenced symbols. */
12105 if (elf_hash_table (info)->dynamic_sections_created)
12106 elf_link_hash_traverse (elf_hash_table (info),
64d03ab5 12107 bed->gc_mark_dynamic_ref,
87538722 12108 info);
c152c796 12109
715df9b8 12110 /* Grovel through relocs to find out who stays ... */
64d03ab5 12111 gc_mark_hook = bed->gc_mark_hook;
c152c796
AM
12112 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
12113 {
12114 asection *o;
12115
12116 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
12117 continue;
12118
7f6ab9f8
AM
12119 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12120 Also treat note sections as a root, if the section is not part
12121 of a group. */
c152c796 12122 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
12123 if (!o->gc_mark
12124 && (o->flags & SEC_EXCLUDE) == 0
24007750 12125 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
12126 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12127 && elf_next_in_group (o) == NULL )))
12128 {
12129 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12130 return FALSE;
12131 }
c152c796
AM
12132 }
12133
6a5bb875 12134 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 12135 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 12136
c152c796 12137 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 12138 return elf_gc_sweep (abfd, info);
c152c796
AM
12139}
12140\f
12141/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12142
12143bfd_boolean
12144bfd_elf_gc_record_vtinherit (bfd *abfd,
12145 asection *sec,
12146 struct elf_link_hash_entry *h,
12147 bfd_vma offset)
12148{
12149 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12150 struct elf_link_hash_entry **search, *child;
12151 bfd_size_type extsymcount;
12152 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12153
12154 /* The sh_info field of the symtab header tells us where the
12155 external symbols start. We don't care about the local symbols at
12156 this point. */
12157 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12158 if (!elf_bad_symtab (abfd))
12159 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12160
12161 sym_hashes = elf_sym_hashes (abfd);
12162 sym_hashes_end = sym_hashes + extsymcount;
12163
12164 /* Hunt down the child symbol, which is in this section at the same
12165 offset as the relocation. */
12166 for (search = sym_hashes; search != sym_hashes_end; ++search)
12167 {
12168 if ((child = *search) != NULL
12169 && (child->root.type == bfd_link_hash_defined
12170 || child->root.type == bfd_link_hash_defweak)
12171 && child->root.u.def.section == sec
12172 && child->root.u.def.value == offset)
12173 goto win;
12174 }
12175
d003868e
AM
12176 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12177 abfd, sec, (unsigned long) offset);
c152c796
AM
12178 bfd_set_error (bfd_error_invalid_operation);
12179 return FALSE;
12180
12181 win:
f6e332e6
AM
12182 if (!child->vtable)
12183 {
a50b1753
NC
12184 child->vtable = (struct elf_link_virtual_table_entry *)
12185 bfd_zalloc (abfd, sizeof (*child->vtable));
f6e332e6
AM
12186 if (!child->vtable)
12187 return FALSE;
12188 }
c152c796
AM
12189 if (!h)
12190 {
12191 /* This *should* only be the absolute section. It could potentially
12192 be that someone has defined a non-global vtable though, which
12193 would be bad. It isn't worth paging in the local symbols to be
12194 sure though; that case should simply be handled by the assembler. */
12195
f6e332e6 12196 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
12197 }
12198 else
f6e332e6 12199 child->vtable->parent = h;
c152c796
AM
12200
12201 return TRUE;
12202}
12203
12204/* Called from check_relocs to record the existence of a VTENTRY reloc. */
12205
12206bfd_boolean
12207bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12208 asection *sec ATTRIBUTE_UNUSED,
12209 struct elf_link_hash_entry *h,
12210 bfd_vma addend)
12211{
12212 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12213 unsigned int log_file_align = bed->s->log_file_align;
12214
f6e332e6
AM
12215 if (!h->vtable)
12216 {
a50b1753
NC
12217 h->vtable = (struct elf_link_virtual_table_entry *)
12218 bfd_zalloc (abfd, sizeof (*h->vtable));
f6e332e6
AM
12219 if (!h->vtable)
12220 return FALSE;
12221 }
12222
12223 if (addend >= h->vtable->size)
c152c796
AM
12224 {
12225 size_t size, bytes, file_align;
f6e332e6 12226 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
12227
12228 /* While the symbol is undefined, we have to be prepared to handle
12229 a zero size. */
12230 file_align = 1 << log_file_align;
12231 if (h->root.type == bfd_link_hash_undefined)
12232 size = addend + file_align;
12233 else
12234 {
12235 size = h->size;
12236 if (addend >= size)
12237 {
12238 /* Oops! We've got a reference past the defined end of
12239 the table. This is probably a bug -- shall we warn? */
12240 size = addend + file_align;
12241 }
12242 }
12243 size = (size + file_align - 1) & -file_align;
12244
12245 /* Allocate one extra entry for use as a "done" flag for the
12246 consolidation pass. */
12247 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12248
12249 if (ptr)
12250 {
a50b1753 12251 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
12252
12253 if (ptr != NULL)
12254 {
12255 size_t oldbytes;
12256
f6e332e6 12257 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
12258 * sizeof (bfd_boolean));
12259 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12260 }
12261 }
12262 else
a50b1753 12263 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
12264
12265 if (ptr == NULL)
12266 return FALSE;
12267
12268 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
12269 h->vtable->used = ptr + 1;
12270 h->vtable->size = size;
c152c796
AM
12271 }
12272
f6e332e6 12273 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
12274
12275 return TRUE;
12276}
12277
ae17ab41
CM
12278/* Map an ELF section header flag to its corresponding string. */
12279typedef struct
12280{
12281 char *flag_name;
12282 flagword flag_value;
12283} elf_flags_to_name_table;
12284
12285static elf_flags_to_name_table elf_flags_to_names [] =
12286{
12287 { "SHF_WRITE", SHF_WRITE },
12288 { "SHF_ALLOC", SHF_ALLOC },
12289 { "SHF_EXECINSTR", SHF_EXECINSTR },
12290 { "SHF_MERGE", SHF_MERGE },
12291 { "SHF_STRINGS", SHF_STRINGS },
12292 { "SHF_INFO_LINK", SHF_INFO_LINK},
12293 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12294 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12295 { "SHF_GROUP", SHF_GROUP },
12296 { "SHF_TLS", SHF_TLS },
12297 { "SHF_MASKOS", SHF_MASKOS },
12298 { "SHF_EXCLUDE", SHF_EXCLUDE },
12299};
12300
b9c361e0
JL
12301/* Returns TRUE if the section is to be included, otherwise FALSE. */
12302bfd_boolean
ae17ab41 12303bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 12304 struct flag_info *flaginfo,
b9c361e0 12305 asection *section)
ae17ab41 12306{
8b127cbc 12307 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 12308
8b127cbc 12309 if (!flaginfo->flags_initialized)
ae17ab41 12310 {
8b127cbc
AM
12311 bfd *obfd = info->output_bfd;
12312 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12313 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
12314 int with_hex = 0;
12315 int without_hex = 0;
12316
8b127cbc 12317 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 12318 {
b9c361e0 12319 unsigned i;
8b127cbc 12320 flagword (*lookup) (char *);
ae17ab41 12321
8b127cbc
AM
12322 lookup = bed->elf_backend_lookup_section_flags_hook;
12323 if (lookup != NULL)
ae17ab41 12324 {
8b127cbc 12325 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
12326
12327 if (hexval != 0)
12328 {
12329 if (tf->with == with_flags)
12330 with_hex |= hexval;
12331 else if (tf->with == without_flags)
12332 without_hex |= hexval;
12333 tf->valid = TRUE;
12334 continue;
12335 }
ae17ab41 12336 }
8b127cbc 12337 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 12338 {
8b127cbc 12339 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
12340 {
12341 if (tf->with == with_flags)
12342 with_hex |= elf_flags_to_names[i].flag_value;
12343 else if (tf->with == without_flags)
12344 without_hex |= elf_flags_to_names[i].flag_value;
12345 tf->valid = TRUE;
12346 break;
12347 }
12348 }
8b127cbc 12349 if (!tf->valid)
b9c361e0
JL
12350 {
12351 info->callbacks->einfo
8b127cbc 12352 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 12353 return FALSE;
ae17ab41
CM
12354 }
12355 }
8b127cbc
AM
12356 flaginfo->flags_initialized = TRUE;
12357 flaginfo->only_with_flags |= with_hex;
12358 flaginfo->not_with_flags |= without_hex;
ae17ab41 12359 }
ae17ab41 12360
8b127cbc 12361 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
12362 return FALSE;
12363
8b127cbc 12364 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
12365 return FALSE;
12366
12367 return TRUE;
ae17ab41
CM
12368}
12369
c152c796
AM
12370struct alloc_got_off_arg {
12371 bfd_vma gotoff;
10455f89 12372 struct bfd_link_info *info;
c152c796
AM
12373};
12374
12375/* We need a special top-level link routine to convert got reference counts
12376 to real got offsets. */
12377
12378static bfd_boolean
12379elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
12380{
a50b1753 12381 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
12382 bfd *obfd = gofarg->info->output_bfd;
12383 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 12384
c152c796
AM
12385 if (h->got.refcount > 0)
12386 {
12387 h->got.offset = gofarg->gotoff;
10455f89 12388 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
12389 }
12390 else
12391 h->got.offset = (bfd_vma) -1;
12392
12393 return TRUE;
12394}
12395
12396/* And an accompanying bit to work out final got entry offsets once
12397 we're done. Should be called from final_link. */
12398
12399bfd_boolean
12400bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
12401 struct bfd_link_info *info)
12402{
12403 bfd *i;
12404 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12405 bfd_vma gotoff;
c152c796
AM
12406 struct alloc_got_off_arg gofarg;
12407
10455f89
HPN
12408 BFD_ASSERT (abfd == info->output_bfd);
12409
c152c796
AM
12410 if (! is_elf_hash_table (info->hash))
12411 return FALSE;
12412
12413 /* The GOT offset is relative to the .got section, but the GOT header is
12414 put into the .got.plt section, if the backend uses it. */
12415 if (bed->want_got_plt)
12416 gotoff = 0;
12417 else
12418 gotoff = bed->got_header_size;
12419
12420 /* Do the local .got entries first. */
12421 for (i = info->input_bfds; i; i = i->link_next)
12422 {
12423 bfd_signed_vma *local_got;
12424 bfd_size_type j, locsymcount;
12425 Elf_Internal_Shdr *symtab_hdr;
12426
12427 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
12428 continue;
12429
12430 local_got = elf_local_got_refcounts (i);
12431 if (!local_got)
12432 continue;
12433
12434 symtab_hdr = &elf_tdata (i)->symtab_hdr;
12435 if (elf_bad_symtab (i))
12436 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12437 else
12438 locsymcount = symtab_hdr->sh_info;
12439
12440 for (j = 0; j < locsymcount; ++j)
12441 {
12442 if (local_got[j] > 0)
12443 {
12444 local_got[j] = gotoff;
10455f89 12445 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
12446 }
12447 else
12448 local_got[j] = (bfd_vma) -1;
12449 }
12450 }
12451
12452 /* Then the global .got entries. .plt refcounts are handled by
12453 adjust_dynamic_symbol */
12454 gofarg.gotoff = gotoff;
10455f89 12455 gofarg.info = info;
c152c796
AM
12456 elf_link_hash_traverse (elf_hash_table (info),
12457 elf_gc_allocate_got_offsets,
12458 &gofarg);
12459 return TRUE;
12460}
12461
12462/* Many folk need no more in the way of final link than this, once
12463 got entry reference counting is enabled. */
12464
12465bfd_boolean
12466bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
12467{
12468 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
12469 return FALSE;
12470
12471 /* Invoke the regular ELF backend linker to do all the work. */
12472 return bfd_elf_final_link (abfd, info);
12473}
12474
12475bfd_boolean
12476bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
12477{
a50b1753 12478 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
12479
12480 if (rcookie->bad_symtab)
12481 rcookie->rel = rcookie->rels;
12482
12483 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
12484 {
12485 unsigned long r_symndx;
12486
12487 if (! rcookie->bad_symtab)
12488 if (rcookie->rel->r_offset > offset)
12489 return FALSE;
12490 if (rcookie->rel->r_offset != offset)
12491 continue;
12492
12493 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 12494 if (r_symndx == STN_UNDEF)
c152c796
AM
12495 return TRUE;
12496
12497 if (r_symndx >= rcookie->locsymcount
12498 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12499 {
12500 struct elf_link_hash_entry *h;
12501
12502 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
12503
12504 while (h->root.type == bfd_link_hash_indirect
12505 || h->root.type == bfd_link_hash_warning)
12506 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12507
12508 if ((h->root.type == bfd_link_hash_defined
12509 || h->root.type == bfd_link_hash_defweak)
dbaa2011 12510 && discarded_section (h->root.u.def.section))
c152c796
AM
12511 return TRUE;
12512 else
12513 return FALSE;
12514 }
12515 else
12516 {
12517 /* It's not a relocation against a global symbol,
12518 but it could be a relocation against a local
12519 symbol for a discarded section. */
12520 asection *isec;
12521 Elf_Internal_Sym *isym;
12522
12523 /* Need to: get the symbol; get the section. */
12524 isym = &rcookie->locsyms[r_symndx];
cb33740c 12525 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
dbaa2011 12526 if (isec != NULL && discarded_section (isec))
cb33740c 12527 return TRUE;
c152c796
AM
12528 }
12529 return FALSE;
12530 }
12531 return FALSE;
12532}
12533
12534/* Discard unneeded references to discarded sections.
12535 Returns TRUE if any section's size was changed. */
12536/* This function assumes that the relocations are in sorted order,
12537 which is true for all known assemblers. */
12538
12539bfd_boolean
12540bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
12541{
12542 struct elf_reloc_cookie cookie;
12543 asection *stab, *eh;
c152c796
AM
12544 const struct elf_backend_data *bed;
12545 bfd *abfd;
c152c796
AM
12546 bfd_boolean ret = FALSE;
12547
12548 if (info->traditional_format
12549 || !is_elf_hash_table (info->hash))
12550 return FALSE;
12551
ca92cecb 12552 _bfd_elf_begin_eh_frame_parsing (info);
c152c796
AM
12553 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
12554 {
12555 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
12556 continue;
12557
12558 bed = get_elf_backend_data (abfd);
12559
8da3dbc5
AM
12560 eh = NULL;
12561 if (!info->relocatable)
12562 {
12563 eh = bfd_get_section_by_name (abfd, ".eh_frame");
7e01508c
AM
12564 while (eh != NULL
12565 && (eh->size == 0
12566 || bfd_is_abs_section (eh->output_section)))
12567 eh = bfd_get_next_section_by_name (eh);
8da3dbc5 12568 }
c152c796
AM
12569
12570 stab = bfd_get_section_by_name (abfd, ".stab");
12571 if (stab != NULL
eea6121a 12572 && (stab->size == 0
c152c796 12573 || bfd_is_abs_section (stab->output_section)
dbaa2011 12574 || stab->sec_info_type != SEC_INFO_TYPE_STABS))
c152c796
AM
12575 stab = NULL;
12576
12577 if (stab == NULL
12578 && eh == NULL
12579 && bed->elf_backend_discard_info == NULL)
12580 continue;
12581
5241d853
RS
12582 if (!init_reloc_cookie (&cookie, info, abfd))
12583 return FALSE;
c152c796 12584
5241d853
RS
12585 if (stab != NULL
12586 && stab->reloc_count > 0
12587 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
c152c796 12588 {
5241d853
RS
12589 if (_bfd_discard_section_stabs (abfd, stab,
12590 elf_section_data (stab)->sec_info,
12591 bfd_elf_reloc_symbol_deleted_p,
12592 &cookie))
12593 ret = TRUE;
12594 fini_reloc_cookie_rels (&cookie, stab);
c152c796
AM
12595 }
12596
90061c33
AM
12597 while (eh != NULL
12598 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
c152c796 12599 {
ca92cecb 12600 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
c152c796
AM
12601 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12602 bfd_elf_reloc_symbol_deleted_p,
12603 &cookie))
12604 ret = TRUE;
5241d853 12605 fini_reloc_cookie_rels (&cookie, eh);
90061c33 12606 eh = bfd_get_next_section_by_name (eh);
c152c796
AM
12607 }
12608
12609 if (bed->elf_backend_discard_info != NULL
12610 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12611 ret = TRUE;
12612
5241d853 12613 fini_reloc_cookie (&cookie, abfd);
c152c796 12614 }
ca92cecb 12615 _bfd_elf_end_eh_frame_parsing (info);
c152c796
AM
12616
12617 if (info->eh_frame_hdr
12618 && !info->relocatable
12619 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12620 ret = TRUE;
12621
12622 return ret;
12623}
082b7297 12624
43e1669b 12625bfd_boolean
0c511000 12626_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 12627 asection *sec,
c0f00686 12628 struct bfd_link_info *info)
082b7297
L
12629{
12630 flagword flags;
c77ec726 12631 const char *name, *key;
082b7297
L
12632 struct bfd_section_already_linked *l;
12633 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 12634
c77ec726
AM
12635 if (sec->output_section == bfd_abs_section_ptr)
12636 return FALSE;
0c511000 12637
c77ec726 12638 flags = sec->flags;
0c511000 12639
c77ec726
AM
12640 /* Return if it isn't a linkonce section. A comdat group section
12641 also has SEC_LINK_ONCE set. */
12642 if ((flags & SEC_LINK_ONCE) == 0)
12643 return FALSE;
0c511000 12644
c77ec726
AM
12645 /* Don't put group member sections on our list of already linked
12646 sections. They are handled as a group via their group section. */
12647 if (elf_sec_group (sec) != NULL)
12648 return FALSE;
0c511000 12649
c77ec726
AM
12650 /* For a SHT_GROUP section, use the group signature as the key. */
12651 name = sec->name;
12652 if ((flags & SEC_GROUP) != 0
12653 && elf_next_in_group (sec) != NULL
12654 && elf_group_name (elf_next_in_group (sec)) != NULL)
12655 key = elf_group_name (elf_next_in_group (sec));
12656 else
12657 {
12658 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 12659 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
12660 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12661 key++;
0c511000 12662 else
c77ec726
AM
12663 /* Must be a user linkonce section that doesn't follow gcc's
12664 naming convention. In this case we won't be matching
12665 single member groups. */
12666 key = name;
0c511000 12667 }
6d2cd210 12668
c77ec726 12669 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
12670
12671 for (l = already_linked_list->entry; l != NULL; l = l->next)
12672 {
c2370991 12673 /* We may have 2 different types of sections on the list: group
c77ec726
AM
12674 sections with a signature of <key> (<key> is some string),
12675 and linkonce sections named .gnu.linkonce.<type>.<key>.
12676 Match like sections. LTO plugin sections are an exception.
12677 They are always named .gnu.linkonce.t.<key> and match either
12678 type of section. */
12679 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
12680 && ((flags & SEC_GROUP) != 0
12681 || strcmp (name, l->sec->name) == 0))
12682 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
12683 {
12684 /* The section has already been linked. See if we should
6d2cd210 12685 issue a warning. */
c77ec726
AM
12686 if (!_bfd_handle_already_linked (sec, l, info))
12687 return FALSE;
082b7297 12688
c77ec726 12689 if (flags & SEC_GROUP)
3d7f7666 12690 {
c77ec726
AM
12691 asection *first = elf_next_in_group (sec);
12692 asection *s = first;
3d7f7666 12693
c77ec726 12694 while (s != NULL)
3d7f7666 12695 {
c77ec726
AM
12696 s->output_section = bfd_abs_section_ptr;
12697 /* Record which group discards it. */
12698 s->kept_section = l->sec;
12699 s = elf_next_in_group (s);
12700 /* These lists are circular. */
12701 if (s == first)
12702 break;
3d7f7666
L
12703 }
12704 }
082b7297 12705
43e1669b 12706 return TRUE;
082b7297
L
12707 }
12708 }
12709
c77ec726
AM
12710 /* A single member comdat group section may be discarded by a
12711 linkonce section and vice versa. */
12712 if ((flags & SEC_GROUP) != 0)
3d7f7666 12713 {
c77ec726 12714 asection *first = elf_next_in_group (sec);
c2370991 12715
c77ec726
AM
12716 if (first != NULL && elf_next_in_group (first) == first)
12717 /* Check this single member group against linkonce sections. */
12718 for (l = already_linked_list->entry; l != NULL; l = l->next)
12719 if ((l->sec->flags & SEC_GROUP) == 0
12720 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12721 {
12722 first->output_section = bfd_abs_section_ptr;
12723 first->kept_section = l->sec;
12724 sec->output_section = bfd_abs_section_ptr;
12725 break;
12726 }
12727 }
12728 else
12729 /* Check this linkonce section against single member groups. */
12730 for (l = already_linked_list->entry; l != NULL; l = l->next)
12731 if (l->sec->flags & SEC_GROUP)
6d2cd210 12732 {
c77ec726 12733 asection *first = elf_next_in_group (l->sec);
6d2cd210 12734
c77ec726
AM
12735 if (first != NULL
12736 && elf_next_in_group (first) == first
12737 && bfd_elf_match_symbols_in_sections (first, sec, info))
12738 {
12739 sec->output_section = bfd_abs_section_ptr;
12740 sec->kept_section = first;
12741 break;
12742 }
6d2cd210 12743 }
0c511000 12744
c77ec726
AM
12745 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
12746 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
12747 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
12748 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
12749 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
12750 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
12751 `.gnu.linkonce.t.F' section from a different bfd not requiring any
12752 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
12753 The reverse order cannot happen as there is never a bfd with only the
12754 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
12755 matter as here were are looking only for cross-bfd sections. */
12756
12757 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
12758 for (l = already_linked_list->entry; l != NULL; l = l->next)
12759 if ((l->sec->flags & SEC_GROUP) == 0
12760 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
12761 {
12762 if (abfd != l->sec->owner)
12763 sec->output_section = bfd_abs_section_ptr;
12764 break;
12765 }
80c29487 12766
082b7297 12767 /* This is the first section with this name. Record it. */
c77ec726 12768 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 12769 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 12770 return sec->output_section == bfd_abs_section_ptr;
082b7297 12771}
81e1b023 12772
a4d8e49b
L
12773bfd_boolean
12774_bfd_elf_common_definition (Elf_Internal_Sym *sym)
12775{
12776 return sym->st_shndx == SHN_COMMON;
12777}
12778
12779unsigned int
12780_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12781{
12782 return SHN_COMMON;
12783}
12784
12785asection *
12786_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12787{
12788 return bfd_com_section_ptr;
12789}
10455f89
HPN
12790
12791bfd_vma
12792_bfd_elf_default_got_elt_size (bfd *abfd,
12793 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12794 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12795 bfd *ibfd ATTRIBUTE_UNUSED,
12796 unsigned long symndx ATTRIBUTE_UNUSED)
12797{
12798 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12799 return bed->s->arch_size / 8;
12800}
83bac4b0
NC
12801
12802/* Routines to support the creation of dynamic relocs. */
12803
83bac4b0
NC
12804/* Returns the name of the dynamic reloc section associated with SEC. */
12805
12806static const char *
12807get_dynamic_reloc_section_name (bfd * abfd,
12808 asection * sec,
12809 bfd_boolean is_rela)
12810{
ddcf1fcf
BS
12811 char *name;
12812 const char *old_name = bfd_get_section_name (NULL, sec);
12813 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 12814
ddcf1fcf 12815 if (old_name == NULL)
83bac4b0
NC
12816 return NULL;
12817
ddcf1fcf
BS
12818 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
12819 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
12820
12821 return name;
12822}
12823
12824/* Returns the dynamic reloc section associated with SEC.
12825 If necessary compute the name of the dynamic reloc section based
12826 on SEC's name (looked up in ABFD's string table) and the setting
12827 of IS_RELA. */
12828
12829asection *
12830_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
12831 asection * sec,
12832 bfd_boolean is_rela)
12833{
12834 asection * reloc_sec = elf_section_data (sec)->sreloc;
12835
12836 if (reloc_sec == NULL)
12837 {
12838 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12839
12840 if (name != NULL)
12841 {
3d4d4302 12842 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
12843
12844 if (reloc_sec != NULL)
12845 elf_section_data (sec)->sreloc = reloc_sec;
12846 }
12847 }
12848
12849 return reloc_sec;
12850}
12851
12852/* Returns the dynamic reloc section associated with SEC. If the
12853 section does not exist it is created and attached to the DYNOBJ
12854 bfd and stored in the SRELOC field of SEC's elf_section_data
12855 structure.
f8076f98 12856
83bac4b0
NC
12857 ALIGNMENT is the alignment for the newly created section and
12858 IS_RELA defines whether the name should be .rela.<SEC's name>
12859 or .rel.<SEC's name>. The section name is looked up in the
12860 string table associated with ABFD. */
12861
12862asection *
12863_bfd_elf_make_dynamic_reloc_section (asection * sec,
12864 bfd * dynobj,
12865 unsigned int alignment,
12866 bfd * abfd,
12867 bfd_boolean is_rela)
12868{
12869 asection * reloc_sec = elf_section_data (sec)->sreloc;
12870
12871 if (reloc_sec == NULL)
12872 {
12873 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12874
12875 if (name == NULL)
12876 return NULL;
12877
3d4d4302 12878 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
12879
12880 if (reloc_sec == NULL)
12881 {
3d4d4302
AM
12882 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
12883 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
12884 if ((sec->flags & SEC_ALLOC) != 0)
12885 flags |= SEC_ALLOC | SEC_LOAD;
12886
3d4d4302 12887 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
12888 if (reloc_sec != NULL)
12889 {
12890 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
12891 reloc_sec = NULL;
12892 }
12893 }
12894
12895 elf_section_data (sec)->sreloc = reloc_sec;
12896 }
12897
12898 return reloc_sec;
12899}
1338dd10
PB
12900
12901/* Copy the ELF symbol type associated with a linker hash entry. */
12902void
12903_bfd_elf_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
12904 struct bfd_link_hash_entry * hdest,
12905 struct bfd_link_hash_entry * hsrc)
12906{
12907 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *)hdest;
12908 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *)hsrc;
12909
12910 ehdest->type = ehsrc->type;
35fc36a8 12911 ehdest->target_internal = ehsrc->target_internal;
1338dd10 12912}
351f65ca
L
12913
12914/* Append a RELA relocation REL to section S in BFD. */
12915
12916void
12917elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12918{
12919 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12920 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
12921 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
12922 bed->s->swap_reloca_out (abfd, rel, loc);
12923}
12924
12925/* Append a REL relocation REL to section S in BFD. */
12926
12927void
12928elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12929{
12930 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12931 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
12932 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
12933 bed->s->swap_reloca_out (abfd, rel, loc);
12934}
This page took 1.603673 seconds and 4 git commands to generate.