Skip relocations in non-loaded, non-alloced sections
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
6f2750fe 2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
252b5132 31
28caa186
AM
32/* This struct is used to pass information to routines called via
33 elf_link_hash_traverse which must return failure. */
34
35struct elf_info_failed
36{
37 struct bfd_link_info *info;
28caa186
AM
38 bfd_boolean failed;
39};
40
41/* This structure is used to pass information to
42 _bfd_elf_link_find_version_dependencies. */
43
44struct elf_find_verdep_info
45{
46 /* General link information. */
47 struct bfd_link_info *info;
48 /* The number of dependencies. */
49 unsigned int vers;
50 /* Whether we had a failure. */
51 bfd_boolean failed;
52};
53
54static bfd_boolean _bfd_elf_fix_symbol_flags
55 (struct elf_link_hash_entry *, struct elf_info_failed *);
56
2f0c68f2
CM
57asection *
58_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
59 unsigned long r_symndx,
60 bfd_boolean discard)
61{
62 if (r_symndx >= cookie->locsymcount
63 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
64 {
65 struct elf_link_hash_entry *h;
66
67 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
68
69 while (h->root.type == bfd_link_hash_indirect
70 || h->root.type == bfd_link_hash_warning)
71 h = (struct elf_link_hash_entry *) h->root.u.i.link;
72
73 if ((h->root.type == bfd_link_hash_defined
74 || h->root.type == bfd_link_hash_defweak)
75 && discarded_section (h->root.u.def.section))
76 return h->root.u.def.section;
77 else
78 return NULL;
79 }
80 else
81 {
82 /* It's not a relocation against a global symbol,
83 but it could be a relocation against a local
84 symbol for a discarded section. */
85 asection *isec;
86 Elf_Internal_Sym *isym;
87
88 /* Need to: get the symbol; get the section. */
89 isym = &cookie->locsyms[r_symndx];
90 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
91 if (isec != NULL
92 && discard ? discarded_section (isec) : 1)
93 return isec;
94 }
95 return NULL;
96}
97
d98685ac
AM
98/* Define a symbol in a dynamic linkage section. */
99
100struct elf_link_hash_entry *
101_bfd_elf_define_linkage_sym (bfd *abfd,
102 struct bfd_link_info *info,
103 asection *sec,
104 const char *name)
105{
106 struct elf_link_hash_entry *h;
107 struct bfd_link_hash_entry *bh;
ccabcbe5 108 const struct elf_backend_data *bed;
d98685ac
AM
109
110 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
111 if (h != NULL)
112 {
113 /* Zap symbol defined in an as-needed lib that wasn't linked.
114 This is a symptom of a larger problem: Absolute symbols
115 defined in shared libraries can't be overridden, because we
116 lose the link to the bfd which is via the symbol section. */
117 h->root.type = bfd_link_hash_new;
118 }
119
120 bh = &h->root;
cf18fda4 121 bed = get_elf_backend_data (abfd);
d98685ac 122 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 123 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
124 &bh))
125 return NULL;
126 h = (struct elf_link_hash_entry *) bh;
127 h->def_regular = 1;
e28df02b 128 h->non_elf = 0;
12b2843a 129 h->root.linker_def = 1;
d98685ac 130 h->type = STT_OBJECT;
00b7642b
AM
131 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
132 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 133
ccabcbe5 134 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
135 return h;
136}
137
b34976b6 138bfd_boolean
268b6b39 139_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
140{
141 flagword flags;
aad5d350 142 asection *s;
252b5132 143 struct elf_link_hash_entry *h;
9c5bfbb7 144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 145 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
146
147 /* This function may be called more than once. */
3d4d4302
AM
148 s = bfd_get_linker_section (abfd, ".got");
149 if (s != NULL)
b34976b6 150 return TRUE;
252b5132 151
e5a52504 152 flags = bed->dynamic_sec_flags;
252b5132 153
14b2f831
AM
154 s = bfd_make_section_anyway_with_flags (abfd,
155 (bed->rela_plts_and_copies_p
156 ? ".rela.got" : ".rel.got"),
157 (bed->dynamic_sec_flags
158 | SEC_READONLY));
6de2ae4a
L
159 if (s == NULL
160 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
161 return FALSE;
162 htab->srelgot = s;
252b5132 163
14b2f831 164 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
165 if (s == NULL
166 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->sgot = s;
169
252b5132
RH
170 if (bed->want_got_plt)
171 {
14b2f831 172 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 173 if (s == NULL
6de2ae4a
L
174 || !bfd_set_section_alignment (abfd, s,
175 bed->s->log_file_align))
b34976b6 176 return FALSE;
6de2ae4a 177 htab->sgotplt = s;
252b5132
RH
178 }
179
64e77c6d
L
180 /* The first bit of the global offset table is the header. */
181 s->size += bed->got_header_size;
182
2517a57f
AM
183 if (bed->want_got_sym)
184 {
185 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
186 (or .got.plt) section. We don't do this in the linker script
187 because we don't want to define the symbol if we are not creating
188 a global offset table. */
6de2ae4a
L
189 h = _bfd_elf_define_linkage_sym (abfd, info, s,
190 "_GLOBAL_OFFSET_TABLE_");
2517a57f 191 elf_hash_table (info)->hgot = h;
d98685ac
AM
192 if (h == NULL)
193 return FALSE;
2517a57f 194 }
252b5132 195
b34976b6 196 return TRUE;
252b5132
RH
197}
198\f
7e9f0867
AM
199/* Create a strtab to hold the dynamic symbol names. */
200static bfd_boolean
201_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
202{
203 struct elf_link_hash_table *hash_table;
204
205 hash_table = elf_hash_table (info);
206 if (hash_table->dynobj == NULL)
6cd255ca
L
207 {
208 /* We may not set dynobj, an input file holding linker created
209 dynamic sections to abfd, which may be a dynamic object with
210 its own dynamic sections. We need to find a normal input file
211 to hold linker created sections if possible. */
212 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
213 {
214 bfd *ibfd;
215 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e
L
216 if ((ibfd->flags
217 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
6cd255ca
L
218 {
219 abfd = ibfd;
220 break;
221 }
222 }
223 hash_table->dynobj = abfd;
224 }
7e9f0867
AM
225
226 if (hash_table->dynstr == NULL)
227 {
228 hash_table->dynstr = _bfd_elf_strtab_init ();
229 if (hash_table->dynstr == NULL)
230 return FALSE;
231 }
232 return TRUE;
233}
234
45d6a902
AM
235/* Create some sections which will be filled in with dynamic linking
236 information. ABFD is an input file which requires dynamic sections
237 to be created. The dynamic sections take up virtual memory space
238 when the final executable is run, so we need to create them before
239 addresses are assigned to the output sections. We work out the
240 actual contents and size of these sections later. */
252b5132 241
b34976b6 242bfd_boolean
268b6b39 243_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 244{
45d6a902 245 flagword flags;
91d6fa6a 246 asection *s;
9c5bfbb7 247 const struct elf_backend_data *bed;
9637f6ef 248 struct elf_link_hash_entry *h;
252b5132 249
0eddce27 250 if (! is_elf_hash_table (info->hash))
45d6a902
AM
251 return FALSE;
252
253 if (elf_hash_table (info)->dynamic_sections_created)
254 return TRUE;
255
7e9f0867
AM
256 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
257 return FALSE;
45d6a902 258
7e9f0867 259 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
260 bed = get_elf_backend_data (abfd);
261
262 flags = bed->dynamic_sec_flags;
45d6a902
AM
263
264 /* A dynamically linked executable has a .interp section, but a
265 shared library does not. */
9b8b325a 266 if (bfd_link_executable (info) && !info->nointerp)
252b5132 267 {
14b2f831
AM
268 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
269 flags | SEC_READONLY);
3496cb2a 270 if (s == NULL)
45d6a902
AM
271 return FALSE;
272 }
bb0deeff 273
45d6a902
AM
274 /* Create sections to hold version informations. These are removed
275 if they are not needed. */
14b2f831
AM
276 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
277 flags | SEC_READONLY);
45d6a902 278 if (s == NULL
45d6a902
AM
279 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
280 return FALSE;
281
14b2f831
AM
282 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
283 flags | SEC_READONLY);
45d6a902 284 if (s == NULL
45d6a902
AM
285 || ! bfd_set_section_alignment (abfd, s, 1))
286 return FALSE;
287
14b2f831
AM
288 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
289 flags | SEC_READONLY);
45d6a902 290 if (s == NULL
45d6a902
AM
291 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
292 return FALSE;
293
14b2f831
AM
294 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
295 flags | SEC_READONLY);
45d6a902 296 if (s == NULL
45d6a902
AM
297 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
298 return FALSE;
cae1fbbb 299 elf_hash_table (info)->dynsym = s;
45d6a902 300
14b2f831
AM
301 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
302 flags | SEC_READONLY);
3496cb2a 303 if (s == NULL)
45d6a902
AM
304 return FALSE;
305
14b2f831 306 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 307 if (s == NULL
45d6a902
AM
308 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
309 return FALSE;
310
311 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
312 .dynamic section. We could set _DYNAMIC in a linker script, but we
313 only want to define it if we are, in fact, creating a .dynamic
314 section. We don't want to define it if there is no .dynamic
315 section, since on some ELF platforms the start up code examines it
316 to decide how to initialize the process. */
9637f6ef
L
317 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
318 elf_hash_table (info)->hdynamic = h;
319 if (h == NULL)
45d6a902
AM
320 return FALSE;
321
fdc90cb4
JJ
322 if (info->emit_hash)
323 {
14b2f831
AM
324 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
325 flags | SEC_READONLY);
fdc90cb4
JJ
326 if (s == NULL
327 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
328 return FALSE;
329 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
330 }
331
332 if (info->emit_gnu_hash)
333 {
14b2f831
AM
334 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
335 flags | SEC_READONLY);
fdc90cb4
JJ
336 if (s == NULL
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 return FALSE;
339 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
340 4 32-bit words followed by variable count of 64-bit words, then
341 variable count of 32-bit words. */
342 if (bed->s->arch_size == 64)
343 elf_section_data (s)->this_hdr.sh_entsize = 0;
344 else
345 elf_section_data (s)->this_hdr.sh_entsize = 4;
346 }
45d6a902
AM
347
348 /* Let the backend create the rest of the sections. This lets the
349 backend set the right flags. The backend will normally create
350 the .got and .plt sections. */
894891db
NC
351 if (bed->elf_backend_create_dynamic_sections == NULL
352 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
353 return FALSE;
354
355 elf_hash_table (info)->dynamic_sections_created = TRUE;
356
357 return TRUE;
358}
359
360/* Create dynamic sections when linking against a dynamic object. */
361
362bfd_boolean
268b6b39 363_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
364{
365 flagword flags, pltflags;
7325306f 366 struct elf_link_hash_entry *h;
45d6a902 367 asection *s;
9c5bfbb7 368 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 369 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 370
252b5132
RH
371 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
372 .rel[a].bss sections. */
e5a52504 373 flags = bed->dynamic_sec_flags;
252b5132
RH
374
375 pltflags = flags;
252b5132 376 if (bed->plt_not_loaded)
6df4d94c
MM
377 /* We do not clear SEC_ALLOC here because we still want the OS to
378 allocate space for the section; it's just that there's nothing
379 to read in from the object file. */
5d1634d7 380 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
381 else
382 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
383 if (bed->plt_readonly)
384 pltflags |= SEC_READONLY;
385
14b2f831 386 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 387 if (s == NULL
252b5132 388 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 389 return FALSE;
6de2ae4a 390 htab->splt = s;
252b5132 391
d98685ac
AM
392 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
393 .plt section. */
7325306f
RS
394 if (bed->want_plt_sym)
395 {
396 h = _bfd_elf_define_linkage_sym (abfd, info, s,
397 "_PROCEDURE_LINKAGE_TABLE_");
398 elf_hash_table (info)->hplt = h;
399 if (h == NULL)
400 return FALSE;
401 }
252b5132 402
14b2f831
AM
403 s = bfd_make_section_anyway_with_flags (abfd,
404 (bed->rela_plts_and_copies_p
405 ? ".rela.plt" : ".rel.plt"),
406 flags | SEC_READONLY);
252b5132 407 if (s == NULL
45d6a902 408 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 409 return FALSE;
6de2ae4a 410 htab->srelplt = s;
252b5132
RH
411
412 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 413 return FALSE;
252b5132 414
3018b441
RH
415 if (bed->want_dynbss)
416 {
417 /* The .dynbss section is a place to put symbols which are defined
418 by dynamic objects, are referenced by regular objects, and are
419 not functions. We must allocate space for them in the process
420 image and use a R_*_COPY reloc to tell the dynamic linker to
421 initialize them at run time. The linker script puts the .dynbss
422 section into the .bss section of the final image. */
14b2f831
AM
423 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
424 (SEC_ALLOC | SEC_LINKER_CREATED));
3496cb2a 425 if (s == NULL)
b34976b6 426 return FALSE;
252b5132 427
3018b441 428 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
429 normally needed. We need to create it here, though, so that the
430 linker will map it to an output section. We can't just create it
431 only if we need it, because we will not know whether we need it
432 until we have seen all the input files, and the first time the
433 main linker code calls BFD after examining all the input files
434 (size_dynamic_sections) the input sections have already been
435 mapped to the output sections. If the section turns out not to
436 be needed, we can discard it later. We will never need this
437 section when generating a shared object, since they do not use
438 copy relocs. */
0e1862bb 439 if (! bfd_link_pic (info))
3018b441 440 {
14b2f831
AM
441 s = bfd_make_section_anyway_with_flags (abfd,
442 (bed->rela_plts_and_copies_p
443 ? ".rela.bss" : ".rel.bss"),
444 flags | SEC_READONLY);
3018b441 445 if (s == NULL
45d6a902 446 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 447 return FALSE;
3018b441 448 }
252b5132
RH
449 }
450
b34976b6 451 return TRUE;
252b5132
RH
452}
453\f
252b5132
RH
454/* Record a new dynamic symbol. We record the dynamic symbols as we
455 read the input files, since we need to have a list of all of them
456 before we can determine the final sizes of the output sections.
457 Note that we may actually call this function even though we are not
458 going to output any dynamic symbols; in some cases we know that a
459 symbol should be in the dynamic symbol table, but only if there is
460 one. */
461
b34976b6 462bfd_boolean
c152c796
AM
463bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
464 struct elf_link_hash_entry *h)
252b5132
RH
465{
466 if (h->dynindx == -1)
467 {
2b0f7ef9 468 struct elf_strtab_hash *dynstr;
68b6ddd0 469 char *p;
252b5132 470 const char *name;
ef53be89 471 size_t indx;
252b5132 472
7a13edea
NC
473 /* XXX: The ABI draft says the linker must turn hidden and
474 internal symbols into STB_LOCAL symbols when producing the
475 DSO. However, if ld.so honors st_other in the dynamic table,
476 this would not be necessary. */
477 switch (ELF_ST_VISIBILITY (h->other))
478 {
479 case STV_INTERNAL:
480 case STV_HIDDEN:
9d6eee78
L
481 if (h->root.type != bfd_link_hash_undefined
482 && h->root.type != bfd_link_hash_undefweak)
38048eb9 483 {
f5385ebf 484 h->forced_local = 1;
67687978
PB
485 if (!elf_hash_table (info)->is_relocatable_executable)
486 return TRUE;
7a13edea 487 }
0444bdd4 488
7a13edea
NC
489 default:
490 break;
491 }
492
252b5132
RH
493 h->dynindx = elf_hash_table (info)->dynsymcount;
494 ++elf_hash_table (info)->dynsymcount;
495
496 dynstr = elf_hash_table (info)->dynstr;
497 if (dynstr == NULL)
498 {
499 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 500 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 501 if (dynstr == NULL)
b34976b6 502 return FALSE;
252b5132
RH
503 }
504
505 /* We don't put any version information in the dynamic string
aad5d350 506 table. */
252b5132
RH
507 name = h->root.root.string;
508 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
509 if (p != NULL)
510 /* We know that the p points into writable memory. In fact,
511 there are only a few symbols that have read-only names, being
512 those like _GLOBAL_OFFSET_TABLE_ that are created specially
513 by the backends. Most symbols will have names pointing into
514 an ELF string table read from a file, or to objalloc memory. */
515 *p = 0;
516
517 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
518
519 if (p != NULL)
520 *p = ELF_VER_CHR;
252b5132 521
ef53be89 522 if (indx == (size_t) -1)
b34976b6 523 return FALSE;
252b5132
RH
524 h->dynstr_index = indx;
525 }
526
b34976b6 527 return TRUE;
252b5132 528}
45d6a902 529\f
55255dae
L
530/* Mark a symbol dynamic. */
531
28caa186 532static void
55255dae 533bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
534 struct elf_link_hash_entry *h,
535 Elf_Internal_Sym *sym)
55255dae 536{
40b36307 537 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 538
40b36307 539 /* It may be called more than once on the same H. */
0e1862bb 540 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
541 return;
542
40b36307
L
543 if ((info->dynamic_data
544 && (h->type == STT_OBJECT
b8871f35 545 || h->type == STT_COMMON
40b36307 546 || (sym != NULL
b8871f35
L
547 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
548 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 549 || (d != NULL
40b36307
L
550 && h->root.type == bfd_link_hash_new
551 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
552 h->dynamic = 1;
553}
554
45d6a902
AM
555/* Record an assignment to a symbol made by a linker script. We need
556 this in case some dynamic object refers to this symbol. */
557
558bfd_boolean
fe21a8fc
L
559bfd_elf_record_link_assignment (bfd *output_bfd,
560 struct bfd_link_info *info,
268b6b39 561 const char *name,
fe21a8fc
L
562 bfd_boolean provide,
563 bfd_boolean hidden)
45d6a902 564{
00cbee0a 565 struct elf_link_hash_entry *h, *hv;
4ea42fb7 566 struct elf_link_hash_table *htab;
00cbee0a 567 const struct elf_backend_data *bed;
45d6a902 568
0eddce27 569 if (!is_elf_hash_table (info->hash))
45d6a902
AM
570 return TRUE;
571
4ea42fb7
AM
572 htab = elf_hash_table (info);
573 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 574 if (h == NULL)
4ea42fb7 575 return provide;
45d6a902 576
0f550b3d
L
577 if (h->versioned == unknown)
578 {
579 /* Set versioned if symbol version is unknown. */
580 char *version = strrchr (name, ELF_VER_CHR);
581 if (version)
582 {
583 if (version > name && version[-1] != ELF_VER_CHR)
584 h->versioned = versioned_hidden;
585 else
586 h->versioned = versioned;
587 }
588 }
589
00cbee0a 590 switch (h->root.type)
77cfaee6 591 {
00cbee0a
L
592 case bfd_link_hash_defined:
593 case bfd_link_hash_defweak:
594 case bfd_link_hash_common:
595 break;
596 case bfd_link_hash_undefweak:
597 case bfd_link_hash_undefined:
598 /* Since we're defining the symbol, don't let it seem to have not
599 been defined. record_dynamic_symbol and size_dynamic_sections
600 may depend on this. */
4ea42fb7 601 h->root.type = bfd_link_hash_new;
77cfaee6
AM
602 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
603 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
604 break;
605 case bfd_link_hash_new:
40b36307 606 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 607 h->non_elf = 0;
00cbee0a
L
608 break;
609 case bfd_link_hash_indirect:
610 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 611 the versioned symbol point to this one. */
00cbee0a
L
612 bed = get_elf_backend_data (output_bfd);
613 hv = h;
614 while (hv->root.type == bfd_link_hash_indirect
615 || hv->root.type == bfd_link_hash_warning)
616 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
617 /* We don't need to update h->root.u since linker will set them
618 later. */
619 h->root.type = bfd_link_hash_undefined;
620 hv->root.type = bfd_link_hash_indirect;
621 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
622 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
623 break;
624 case bfd_link_hash_warning:
625 abort ();
626 break;
55255dae 627 }
45d6a902
AM
628
629 /* If this symbol is being provided by the linker script, and it is
630 currently defined by a dynamic object, but not by a regular
631 object, then mark it as undefined so that the generic linker will
632 force the correct value. */
633 if (provide
f5385ebf
AM
634 && h->def_dynamic
635 && !h->def_regular)
45d6a902
AM
636 h->root.type = bfd_link_hash_undefined;
637
638 /* If this symbol is not being provided by the linker script, and it is
639 currently defined by a dynamic object, but not by a regular object,
640 then clear out any version information because the symbol will not be
641 associated with the dynamic object any more. */
642 if (!provide
f5385ebf
AM
643 && h->def_dynamic
644 && !h->def_regular)
45d6a902
AM
645 h->verinfo.verdef = NULL;
646
f5385ebf 647 h->def_regular = 1;
45d6a902 648
eb8476a6 649 if (hidden)
fe21a8fc 650 {
91d6fa6a 651 bed = get_elf_backend_data (output_bfd);
b8297068
AM
652 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
653 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
654 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
655 }
656
6fa3860b
PB
657 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
658 and executables. */
0e1862bb 659 if (!bfd_link_relocatable (info)
6fa3860b
PB
660 && h->dynindx != -1
661 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
662 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
663 h->forced_local = 1;
664
f5385ebf
AM
665 if ((h->def_dynamic
666 || h->ref_dynamic
6b3b0ab8
L
667 || bfd_link_dll (info)
668 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
669 && h->dynindx == -1)
670 {
c152c796 671 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
672 return FALSE;
673
674 /* If this is a weak defined symbol, and we know a corresponding
675 real symbol from the same dynamic object, make sure the real
676 symbol is also made into a dynamic symbol. */
f6e332e6
AM
677 if (h->u.weakdef != NULL
678 && h->u.weakdef->dynindx == -1)
45d6a902 679 {
f6e332e6 680 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
681 return FALSE;
682 }
683 }
684
685 return TRUE;
686}
42751cf3 687
8c58d23b
AM
688/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
689 success, and 2 on a failure caused by attempting to record a symbol
690 in a discarded section, eg. a discarded link-once section symbol. */
691
692int
c152c796
AM
693bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
694 bfd *input_bfd,
695 long input_indx)
8c58d23b
AM
696{
697 bfd_size_type amt;
698 struct elf_link_local_dynamic_entry *entry;
699 struct elf_link_hash_table *eht;
700 struct elf_strtab_hash *dynstr;
ef53be89 701 size_t dynstr_index;
8c58d23b
AM
702 char *name;
703 Elf_External_Sym_Shndx eshndx;
704 char esym[sizeof (Elf64_External_Sym)];
705
0eddce27 706 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
707 return 0;
708
709 /* See if the entry exists already. */
710 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
711 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
712 return 1;
713
714 amt = sizeof (*entry);
a50b1753 715 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
716 if (entry == NULL)
717 return 0;
718
719 /* Go find the symbol, so that we can find it's name. */
720 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 721 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
722 {
723 bfd_release (input_bfd, entry);
724 return 0;
725 }
726
727 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 728 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
729 {
730 asection *s;
731
732 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
733 if (s == NULL || bfd_is_abs_section (s->output_section))
734 {
735 /* We can still bfd_release here as nothing has done another
736 bfd_alloc. We can't do this later in this function. */
737 bfd_release (input_bfd, entry);
738 return 2;
739 }
740 }
741
742 name = (bfd_elf_string_from_elf_section
743 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
744 entry->isym.st_name));
745
746 dynstr = elf_hash_table (info)->dynstr;
747 if (dynstr == NULL)
748 {
749 /* Create a strtab to hold the dynamic symbol names. */
750 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
751 if (dynstr == NULL)
752 return 0;
753 }
754
b34976b6 755 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 756 if (dynstr_index == (size_t) -1)
8c58d23b
AM
757 return 0;
758 entry->isym.st_name = dynstr_index;
759
760 eht = elf_hash_table (info);
761
762 entry->next = eht->dynlocal;
763 eht->dynlocal = entry;
764 entry->input_bfd = input_bfd;
765 entry->input_indx = input_indx;
766 eht->dynsymcount++;
767
768 /* Whatever binding the symbol had before, it's now local. */
769 entry->isym.st_info
770 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
771
772 /* The dynindx will be set at the end of size_dynamic_sections. */
773
774 return 1;
775}
776
30b30c21 777/* Return the dynindex of a local dynamic symbol. */
42751cf3 778
30b30c21 779long
268b6b39
AM
780_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
781 bfd *input_bfd,
782 long input_indx)
30b30c21
RH
783{
784 struct elf_link_local_dynamic_entry *e;
785
786 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
787 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
788 return e->dynindx;
789 return -1;
790}
791
792/* This function is used to renumber the dynamic symbols, if some of
793 them are removed because they are marked as local. This is called
794 via elf_link_hash_traverse. */
795
b34976b6 796static bfd_boolean
268b6b39
AM
797elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
798 void *data)
42751cf3 799{
a50b1753 800 size_t *count = (size_t *) data;
30b30c21 801
6fa3860b
PB
802 if (h->forced_local)
803 return TRUE;
804
805 if (h->dynindx != -1)
806 h->dynindx = ++(*count);
807
808 return TRUE;
809}
810
811
812/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
813 STB_LOCAL binding. */
814
815static bfd_boolean
816elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
817 void *data)
818{
a50b1753 819 size_t *count = (size_t *) data;
6fa3860b 820
6fa3860b
PB
821 if (!h->forced_local)
822 return TRUE;
823
42751cf3 824 if (h->dynindx != -1)
30b30c21
RH
825 h->dynindx = ++(*count);
826
b34976b6 827 return TRUE;
42751cf3 828}
30b30c21 829
aee6f5b4
AO
830/* Return true if the dynamic symbol for a given section should be
831 omitted when creating a shared library. */
832bfd_boolean
833_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
834 struct bfd_link_info *info,
835 asection *p)
836{
74541ad4 837 struct elf_link_hash_table *htab;
ca55926c 838 asection *ip;
74541ad4 839
aee6f5b4
AO
840 switch (elf_section_data (p)->this_hdr.sh_type)
841 {
842 case SHT_PROGBITS:
843 case SHT_NOBITS:
844 /* If sh_type is yet undecided, assume it could be
845 SHT_PROGBITS/SHT_NOBITS. */
846 case SHT_NULL:
74541ad4
AM
847 htab = elf_hash_table (info);
848 if (p == htab->tls_sec)
849 return FALSE;
850
851 if (htab->text_index_section != NULL)
852 return p != htab->text_index_section && p != htab->data_index_section;
853
ca55926c 854 return (htab->dynobj != NULL
3d4d4302 855 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 856 && ip->output_section == p);
aee6f5b4
AO
857
858 /* There shouldn't be section relative relocations
859 against any other section. */
860 default:
861 return TRUE;
862 }
863}
864
062e2358 865/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
866 symbol for each output section, which come first. Next come symbols
867 which have been forced to local binding. Then all of the back-end
868 allocated local dynamic syms, followed by the rest of the global
869 symbols. */
30b30c21 870
554220db
AM
871static unsigned long
872_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
873 struct bfd_link_info *info,
874 unsigned long *section_sym_count)
30b30c21
RH
875{
876 unsigned long dynsymcount = 0;
877
0e1862bb
L
878 if (bfd_link_pic (info)
879 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 880 {
aee6f5b4 881 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
882 asection *p;
883 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 884 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
885 && (p->flags & SEC_ALLOC) != 0
886 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
887 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
888 else
889 elf_section_data (p)->dynindx = 0;
30b30c21 890 }
554220db 891 *section_sym_count = dynsymcount;
30b30c21 892
6fa3860b
PB
893 elf_link_hash_traverse (elf_hash_table (info),
894 elf_link_renumber_local_hash_table_dynsyms,
895 &dynsymcount);
896
30b30c21
RH
897 if (elf_hash_table (info)->dynlocal)
898 {
899 struct elf_link_local_dynamic_entry *p;
900 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
901 p->dynindx = ++dynsymcount;
902 }
903
904 elf_link_hash_traverse (elf_hash_table (info),
905 elf_link_renumber_hash_table_dynsyms,
906 &dynsymcount);
907
d5486c43
L
908 /* There is an unused NULL entry at the head of the table which we
909 must account for in our count even if the table is empty since it
910 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
911 .dynamic section. */
912 dynsymcount++;
30b30c21 913
ccabcbe5
AM
914 elf_hash_table (info)->dynsymcount = dynsymcount;
915 return dynsymcount;
30b30c21 916}
252b5132 917
54ac0771
L
918/* Merge st_other field. */
919
920static void
921elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 922 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 923 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
924{
925 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
926
927 /* If st_other has a processor-specific meaning, specific
cd3416da 928 code might be needed here. */
54ac0771
L
929 if (bed->elf_backend_merge_symbol_attribute)
930 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
931 dynamic);
932
cd3416da 933 if (!dynamic)
54ac0771 934 {
cd3416da
AM
935 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
936 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 937
cd3416da
AM
938 /* Keep the most constraining visibility. Leave the remainder
939 of the st_other field to elf_backend_merge_symbol_attribute. */
940 if (symvis - 1 < hvis - 1)
941 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 942 }
b8417128
AM
943 else if (definition
944 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
945 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 946 h->protected_def = 1;
54ac0771
L
947}
948
4f3fedcf
AM
949/* This function is called when we want to merge a new symbol with an
950 existing symbol. It handles the various cases which arise when we
951 find a definition in a dynamic object, or when there is already a
952 definition in a dynamic object. The new symbol is described by
953 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
954 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
955 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
956 of an old common symbol. We set OVERRIDE if the old symbol is
957 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
958 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
959 to change. By OK to change, we mean that we shouldn't warn if the
960 type or size does change. */
45d6a902 961
8a56bd02 962static bfd_boolean
268b6b39
AM
963_bfd_elf_merge_symbol (bfd *abfd,
964 struct bfd_link_info *info,
965 const char *name,
966 Elf_Internal_Sym *sym,
967 asection **psec,
968 bfd_vma *pvalue,
4f3fedcf
AM
969 struct elf_link_hash_entry **sym_hash,
970 bfd **poldbfd,
37a9e49a 971 bfd_boolean *pold_weak,
af44c138 972 unsigned int *pold_alignment,
268b6b39
AM
973 bfd_boolean *skip,
974 bfd_boolean *override,
975 bfd_boolean *type_change_ok,
6e33951e
L
976 bfd_boolean *size_change_ok,
977 bfd_boolean *matched)
252b5132 978{
7479dfd4 979 asection *sec, *oldsec;
45d6a902 980 struct elf_link_hash_entry *h;
90c984fc 981 struct elf_link_hash_entry *hi;
45d6a902
AM
982 struct elf_link_hash_entry *flip;
983 int bind;
984 bfd *oldbfd;
985 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 986 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 987 const struct elf_backend_data *bed;
6e33951e 988 char *new_version;
45d6a902
AM
989
990 *skip = FALSE;
991 *override = FALSE;
992
993 sec = *psec;
994 bind = ELF_ST_BIND (sym->st_info);
995
996 if (! bfd_is_und_section (sec))
997 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
998 else
999 h = ((struct elf_link_hash_entry *)
1000 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1001 if (h == NULL)
1002 return FALSE;
1003 *sym_hash = h;
252b5132 1004
88ba32a0
L
1005 bed = get_elf_backend_data (abfd);
1006
6e33951e 1007 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1008 if (h->versioned != unversioned)
6e33951e 1009 {
422f1182
L
1010 /* Symbol version is unknown or versioned. */
1011 new_version = strrchr (name, ELF_VER_CHR);
1012 if (new_version)
1013 {
1014 if (h->versioned == unknown)
1015 {
1016 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1017 h->versioned = versioned_hidden;
1018 else
1019 h->versioned = versioned;
1020 }
1021 new_version += 1;
1022 if (new_version[0] == '\0')
1023 new_version = NULL;
1024 }
1025 else
1026 h->versioned = unversioned;
6e33951e 1027 }
422f1182
L
1028 else
1029 new_version = NULL;
6e33951e 1030
90c984fc
L
1031 /* For merging, we only care about real symbols. But we need to make
1032 sure that indirect symbol dynamic flags are updated. */
1033 hi = h;
45d6a902
AM
1034 while (h->root.type == bfd_link_hash_indirect
1035 || h->root.type == bfd_link_hash_warning)
1036 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1037
6e33951e
L
1038 if (!*matched)
1039 {
1040 if (hi == h || h->root.type == bfd_link_hash_new)
1041 *matched = TRUE;
1042 else
1043 {
ae7683d2 1044 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1045 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1046 true if the new symbol is only visible to the symbol with
6e33951e 1047 the same symbol version. */
422f1182
L
1048 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1049 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1050 if (!old_hidden && !new_hidden)
1051 /* The new symbol matches the existing symbol if both
1052 aren't hidden. */
1053 *matched = TRUE;
1054 else
1055 {
1056 /* OLD_VERSION is the symbol version of the existing
1057 symbol. */
422f1182
L
1058 char *old_version;
1059
1060 if (h->versioned >= versioned)
1061 old_version = strrchr (h->root.root.string,
1062 ELF_VER_CHR) + 1;
1063 else
1064 old_version = NULL;
6e33951e
L
1065
1066 /* The new symbol matches the existing symbol if they
1067 have the same symbol version. */
1068 *matched = (old_version == new_version
1069 || (old_version != NULL
1070 && new_version != NULL
1071 && strcmp (old_version, new_version) == 0));
1072 }
1073 }
1074 }
1075
934bce08
AM
1076 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1077 existing symbol. */
1078
1079 oldbfd = NULL;
1080 oldsec = NULL;
1081 switch (h->root.type)
1082 {
1083 default:
1084 break;
1085
1086 case bfd_link_hash_undefined:
1087 case bfd_link_hash_undefweak:
1088 oldbfd = h->root.u.undef.abfd;
1089 break;
1090
1091 case bfd_link_hash_defined:
1092 case bfd_link_hash_defweak:
1093 oldbfd = h->root.u.def.section->owner;
1094 oldsec = h->root.u.def.section;
1095 break;
1096
1097 case bfd_link_hash_common:
1098 oldbfd = h->root.u.c.p->section->owner;
1099 oldsec = h->root.u.c.p->section;
1100 if (pold_alignment)
1101 *pold_alignment = h->root.u.c.p->alignment_power;
1102 break;
1103 }
1104 if (poldbfd && *poldbfd == NULL)
1105 *poldbfd = oldbfd;
1106
1107 /* Differentiate strong and weak symbols. */
1108 newweak = bind == STB_WEAK;
1109 oldweak = (h->root.type == bfd_link_hash_defweak
1110 || h->root.type == bfd_link_hash_undefweak);
1111 if (pold_weak)
1112 *pold_weak = oldweak;
1113
1114 /* This code is for coping with dynamic objects, and is only useful
1115 if we are doing an ELF link. */
1116 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1117 return TRUE;
1118
40b36307 1119 /* We have to check it for every instance since the first few may be
ee659f1f 1120 references and not all compilers emit symbol type for undefined
40b36307
L
1121 symbols. */
1122 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1123
ee659f1f
AM
1124 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1125 respectively, is from a dynamic object. */
1126
1127 newdyn = (abfd->flags & DYNAMIC) != 0;
1128
1129 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1130 syms and defined syms in dynamic libraries respectively.
1131 ref_dynamic on the other hand can be set for a symbol defined in
1132 a dynamic library, and def_dynamic may not be set; When the
1133 definition in a dynamic lib is overridden by a definition in the
1134 executable use of the symbol in the dynamic lib becomes a
1135 reference to the executable symbol. */
1136 if (newdyn)
1137 {
1138 if (bfd_is_und_section (sec))
1139 {
1140 if (bind != STB_WEAK)
1141 {
1142 h->ref_dynamic_nonweak = 1;
1143 hi->ref_dynamic_nonweak = 1;
1144 }
1145 }
1146 else
1147 {
6e33951e
L
1148 /* Update the existing symbol only if they match. */
1149 if (*matched)
1150 h->dynamic_def = 1;
ee659f1f
AM
1151 hi->dynamic_def = 1;
1152 }
1153 }
1154
45d6a902
AM
1155 /* If we just created the symbol, mark it as being an ELF symbol.
1156 Other than that, there is nothing to do--there is no merge issue
1157 with a newly defined symbol--so we just return. */
1158
1159 if (h->root.type == bfd_link_hash_new)
252b5132 1160 {
f5385ebf 1161 h->non_elf = 0;
45d6a902
AM
1162 return TRUE;
1163 }
252b5132 1164
45d6a902
AM
1165 /* In cases involving weak versioned symbols, we may wind up trying
1166 to merge a symbol with itself. Catch that here, to avoid the
1167 confusion that results if we try to override a symbol with
1168 itself. The additional tests catch cases like
1169 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1170 dynamic object, which we do want to handle here. */
1171 if (abfd == oldbfd
895fa45f 1172 && (newweak || oldweak)
45d6a902 1173 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1174 || !h->def_regular))
45d6a902
AM
1175 return TRUE;
1176
707bba77 1177 olddyn = FALSE;
45d6a902
AM
1178 if (oldbfd != NULL)
1179 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1180 else if (oldsec != NULL)
45d6a902 1181 {
707bba77 1182 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1183 indices used by MIPS ELF. */
707bba77 1184 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1185 }
252b5132 1186
45d6a902
AM
1187 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1188 respectively, appear to be a definition rather than reference. */
1189
707bba77 1190 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1191
707bba77
AM
1192 olddef = (h->root.type != bfd_link_hash_undefined
1193 && h->root.type != bfd_link_hash_undefweak
202ac193 1194 && h->root.type != bfd_link_hash_common);
45d6a902 1195
0a36a439
L
1196 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1197 respectively, appear to be a function. */
1198
1199 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1200 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1201
1202 oldfunc = (h->type != STT_NOTYPE
1203 && bed->is_function_type (h->type));
1204
5b677558
AM
1205 /* If creating a default indirect symbol ("foo" or "foo@") from a
1206 dynamic versioned definition ("foo@@") skip doing so if there is
1207 an existing regular definition with a different type. We don't
1208 want, for example, a "time" variable in the executable overriding
1209 a "time" function in a shared library. */
580a2b6e 1210 if (pold_alignment == NULL
580a2b6e
L
1211 && newdyn
1212 && newdef
1213 && !olddyn
5b677558
AM
1214 && (olddef || h->root.type == bfd_link_hash_common)
1215 && ELF_ST_TYPE (sym->st_info) != h->type
1216 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1217 && h->type != STT_NOTYPE
1218 && !(newfunc && oldfunc))
580a2b6e
L
1219 {
1220 *skip = TRUE;
1221 return TRUE;
1222 }
1223
4c34aff8
AM
1224 /* Check TLS symbols. We don't check undefined symbols introduced
1225 by "ld -u" which have no type (and oldbfd NULL), and we don't
1226 check symbols from plugins because they also have no type. */
1227 if (oldbfd != NULL
1228 && (oldbfd->flags & BFD_PLUGIN) == 0
1229 && (abfd->flags & BFD_PLUGIN) == 0
1230 && ELF_ST_TYPE (sym->st_info) != h->type
1231 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1232 {
1233 bfd *ntbfd, *tbfd;
1234 bfd_boolean ntdef, tdef;
1235 asection *ntsec, *tsec;
1236
1237 if (h->type == STT_TLS)
1238 {
3b36f7e6 1239 ntbfd = abfd;
7479dfd4
L
1240 ntsec = sec;
1241 ntdef = newdef;
1242 tbfd = oldbfd;
1243 tsec = oldsec;
1244 tdef = olddef;
1245 }
1246 else
1247 {
1248 ntbfd = oldbfd;
1249 ntsec = oldsec;
1250 ntdef = olddef;
1251 tbfd = abfd;
1252 tsec = sec;
1253 tdef = newdef;
1254 }
1255
1256 if (tdef && ntdef)
1257 (*_bfd_error_handler)
191c0c42
AM
1258 (_("%s: TLS definition in %B section %A "
1259 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1260 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1261 else if (!tdef && !ntdef)
1262 (*_bfd_error_handler)
191c0c42
AM
1263 (_("%s: TLS reference in %B "
1264 "mismatches non-TLS reference in %B"),
7479dfd4
L
1265 tbfd, ntbfd, h->root.root.string);
1266 else if (tdef)
1267 (*_bfd_error_handler)
191c0c42
AM
1268 (_("%s: TLS definition in %B section %A "
1269 "mismatches non-TLS reference in %B"),
7479dfd4
L
1270 tbfd, tsec, ntbfd, h->root.root.string);
1271 else
1272 (*_bfd_error_handler)
191c0c42
AM
1273 (_("%s: TLS reference in %B "
1274 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1275 tbfd, ntbfd, ntsec, h->root.root.string);
1276
1277 bfd_set_error (bfd_error_bad_value);
1278 return FALSE;
1279 }
1280
45d6a902
AM
1281 /* If the old symbol has non-default visibility, we ignore the new
1282 definition from a dynamic object. */
1283 if (newdyn
9c7a29a3 1284 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1285 && !bfd_is_und_section (sec))
1286 {
1287 *skip = TRUE;
1288 /* Make sure this symbol is dynamic. */
f5385ebf 1289 h->ref_dynamic = 1;
90c984fc 1290 hi->ref_dynamic = 1;
45d6a902
AM
1291 /* A protected symbol has external availability. Make sure it is
1292 recorded as dynamic.
1293
1294 FIXME: Should we check type and size for protected symbol? */
1295 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1296 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1297 else
1298 return TRUE;
1299 }
1300 else if (!newdyn
9c7a29a3 1301 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1302 && h->def_dynamic)
45d6a902
AM
1303 {
1304 /* If the new symbol with non-default visibility comes from a
1305 relocatable file and the old definition comes from a dynamic
1306 object, we remove the old definition. */
6c9b78e6 1307 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1308 {
1309 /* Handle the case where the old dynamic definition is
1310 default versioned. We need to copy the symbol info from
1311 the symbol with default version to the normal one if it
1312 was referenced before. */
1313 if (h->ref_regular)
1314 {
6c9b78e6 1315 hi->root.type = h->root.type;
d2dee3b2 1316 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1317 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1318
6c9b78e6 1319 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1320 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1321 {
aed81c4e
MR
1322 /* If the new symbol is hidden or internal, completely undo
1323 any dynamic link state. */
1324 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1325 h->forced_local = 0;
1326 h->ref_dynamic = 0;
d2dee3b2
L
1327 }
1328 else
aed81c4e
MR
1329 h->ref_dynamic = 1;
1330
1331 h->def_dynamic = 0;
aed81c4e
MR
1332 /* FIXME: Should we check type and size for protected symbol? */
1333 h->size = 0;
1334 h->type = 0;
1335
6c9b78e6 1336 h = hi;
d2dee3b2
L
1337 }
1338 else
6c9b78e6 1339 h = hi;
d2dee3b2 1340 }
1de1a317 1341
f5eda473
AM
1342 /* If the old symbol was undefined before, then it will still be
1343 on the undefs list. If the new symbol is undefined or
1344 common, we can't make it bfd_link_hash_new here, because new
1345 undefined or common symbols will be added to the undefs list
1346 by _bfd_generic_link_add_one_symbol. Symbols may not be
1347 added twice to the undefs list. Also, if the new symbol is
1348 undefweak then we don't want to lose the strong undef. */
1349 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1350 {
1de1a317 1351 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1352 h->root.u.undef.abfd = abfd;
1353 }
1354 else
1355 {
1356 h->root.type = bfd_link_hash_new;
1357 h->root.u.undef.abfd = NULL;
1358 }
1359
f5eda473 1360 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1361 {
f5eda473
AM
1362 /* If the new symbol is hidden or internal, completely undo
1363 any dynamic link state. */
1364 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1365 h->forced_local = 0;
1366 h->ref_dynamic = 0;
45d6a902 1367 }
f5eda473
AM
1368 else
1369 h->ref_dynamic = 1;
1370 h->def_dynamic = 0;
45d6a902
AM
1371 /* FIXME: Should we check type and size for protected symbol? */
1372 h->size = 0;
1373 h->type = 0;
1374 return TRUE;
1375 }
14a793b2 1376
15b43f48
AM
1377 /* If a new weak symbol definition comes from a regular file and the
1378 old symbol comes from a dynamic library, we treat the new one as
1379 strong. Similarly, an old weak symbol definition from a regular
1380 file is treated as strong when the new symbol comes from a dynamic
1381 library. Further, an old weak symbol from a dynamic library is
1382 treated as strong if the new symbol is from a dynamic library.
1383 This reflects the way glibc's ld.so works.
1384
1385 Do this before setting *type_change_ok or *size_change_ok so that
1386 we warn properly when dynamic library symbols are overridden. */
1387
1388 if (newdef && !newdyn && olddyn)
0f8a2703 1389 newweak = FALSE;
15b43f48 1390 if (olddef && newdyn)
0f8a2703
AM
1391 oldweak = FALSE;
1392
d334575b 1393 /* Allow changes between different types of function symbol. */
0a36a439 1394 if (newfunc && oldfunc)
fcb93ecf
PB
1395 *type_change_ok = TRUE;
1396
79349b09
AM
1397 /* It's OK to change the type if either the existing symbol or the
1398 new symbol is weak. A type change is also OK if the old symbol
1399 is undefined and the new symbol is defined. */
252b5132 1400
79349b09
AM
1401 if (oldweak
1402 || newweak
1403 || (newdef
1404 && h->root.type == bfd_link_hash_undefined))
1405 *type_change_ok = TRUE;
1406
1407 /* It's OK to change the size if either the existing symbol or the
1408 new symbol is weak, or if the old symbol is undefined. */
1409
1410 if (*type_change_ok
1411 || h->root.type == bfd_link_hash_undefined)
1412 *size_change_ok = TRUE;
45d6a902 1413
45d6a902
AM
1414 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1415 symbol, respectively, appears to be a common symbol in a dynamic
1416 object. If a symbol appears in an uninitialized section, and is
1417 not weak, and is not a function, then it may be a common symbol
1418 which was resolved when the dynamic object was created. We want
1419 to treat such symbols specially, because they raise special
1420 considerations when setting the symbol size: if the symbol
1421 appears as a common symbol in a regular object, and the size in
1422 the regular object is larger, we must make sure that we use the
1423 larger size. This problematic case can always be avoided in C,
1424 but it must be handled correctly when using Fortran shared
1425 libraries.
1426
1427 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1428 likewise for OLDDYNCOMMON and OLDDEF.
1429
1430 Note that this test is just a heuristic, and that it is quite
1431 possible to have an uninitialized symbol in a shared object which
1432 is really a definition, rather than a common symbol. This could
1433 lead to some minor confusion when the symbol really is a common
1434 symbol in some regular object. However, I think it will be
1435 harmless. */
1436
1437 if (newdyn
1438 && newdef
79349b09 1439 && !newweak
45d6a902
AM
1440 && (sec->flags & SEC_ALLOC) != 0
1441 && (sec->flags & SEC_LOAD) == 0
1442 && sym->st_size > 0
0a36a439 1443 && !newfunc)
45d6a902
AM
1444 newdyncommon = TRUE;
1445 else
1446 newdyncommon = FALSE;
1447
1448 if (olddyn
1449 && olddef
1450 && h->root.type == bfd_link_hash_defined
f5385ebf 1451 && h->def_dynamic
45d6a902
AM
1452 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1453 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1454 && h->size > 0
0a36a439 1455 && !oldfunc)
45d6a902
AM
1456 olddyncommon = TRUE;
1457 else
1458 olddyncommon = FALSE;
1459
a4d8e49b
L
1460 /* We now know everything about the old and new symbols. We ask the
1461 backend to check if we can merge them. */
5d13b3b3
AM
1462 if (bed->merge_symbol != NULL)
1463 {
1464 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1465 return FALSE;
1466 sec = *psec;
1467 }
a4d8e49b 1468
45d6a902
AM
1469 /* If both the old and the new symbols look like common symbols in a
1470 dynamic object, set the size of the symbol to the larger of the
1471 two. */
1472
1473 if (olddyncommon
1474 && newdyncommon
1475 && sym->st_size != h->size)
1476 {
1477 /* Since we think we have two common symbols, issue a multiple
1478 common warning if desired. Note that we only warn if the
1479 size is different. If the size is the same, we simply let
1480 the old symbol override the new one as normally happens with
1481 symbols defined in dynamic objects. */
1482
1a72702b
AM
1483 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1484 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1485 if (sym->st_size > h->size)
1486 h->size = sym->st_size;
252b5132 1487
45d6a902 1488 *size_change_ok = TRUE;
252b5132
RH
1489 }
1490
45d6a902
AM
1491 /* If we are looking at a dynamic object, and we have found a
1492 definition, we need to see if the symbol was already defined by
1493 some other object. If so, we want to use the existing
1494 definition, and we do not want to report a multiple symbol
1495 definition error; we do this by clobbering *PSEC to be
1496 bfd_und_section_ptr.
1497
1498 We treat a common symbol as a definition if the symbol in the
1499 shared library is a function, since common symbols always
1500 represent variables; this can cause confusion in principle, but
1501 any such confusion would seem to indicate an erroneous program or
1502 shared library. We also permit a common symbol in a regular
202ac193
L
1503 object to override a weak symbol in a shared object. A common
1504 symbol in executable also overrides a symbol in a shared object. */
45d6a902
AM
1505
1506 if (newdyn
1507 && newdef
77cfaee6 1508 && (olddef
45d6a902 1509 || (h->root.type == bfd_link_hash_common
202ac193
L
1510 && (newweak
1511 || newfunc
1512 || (!olddyn && bfd_link_executable (info))))))
45d6a902
AM
1513 {
1514 *override = TRUE;
1515 newdef = FALSE;
1516 newdyncommon = FALSE;
252b5132 1517
45d6a902
AM
1518 *psec = sec = bfd_und_section_ptr;
1519 *size_change_ok = TRUE;
252b5132 1520
45d6a902
AM
1521 /* If we get here when the old symbol is a common symbol, then
1522 we are explicitly letting it override a weak symbol or
1523 function in a dynamic object, and we don't want to warn about
1524 a type change. If the old symbol is a defined symbol, a type
1525 change warning may still be appropriate. */
252b5132 1526
45d6a902
AM
1527 if (h->root.type == bfd_link_hash_common)
1528 *type_change_ok = TRUE;
1529 }
1530
1531 /* Handle the special case of an old common symbol merging with a
1532 new symbol which looks like a common symbol in a shared object.
1533 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1534 common symbol, and let _bfd_generic_link_add_one_symbol do the
1535 right thing. */
45d6a902
AM
1536
1537 if (newdyncommon
1538 && h->root.type == bfd_link_hash_common)
1539 {
1540 *override = TRUE;
1541 newdef = FALSE;
1542 newdyncommon = FALSE;
1543 *pvalue = sym->st_size;
a4d8e49b 1544 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1545 *size_change_ok = TRUE;
1546 }
1547
c5e2cead 1548 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1549 if (newdef && olddef && newweak)
54ac0771 1550 {
35ed3f94 1551 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1552 if (!(oldbfd != NULL
1553 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1554 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1555 {
1556 newdef = FALSE;
1557 *skip = TRUE;
1558 }
54ac0771
L
1559
1560 /* Merge st_other. If the symbol already has a dynamic index,
1561 but visibility says it should not be visible, turn it into a
1562 local symbol. */
b8417128 1563 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1564 if (h->dynindx != -1)
1565 switch (ELF_ST_VISIBILITY (h->other))
1566 {
1567 case STV_INTERNAL:
1568 case STV_HIDDEN:
1569 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1570 break;
1571 }
1572 }
c5e2cead 1573
45d6a902
AM
1574 /* If the old symbol is from a dynamic object, and the new symbol is
1575 a definition which is not from a dynamic object, then the new
1576 symbol overrides the old symbol. Symbols from regular files
1577 always take precedence over symbols from dynamic objects, even if
1578 they are defined after the dynamic object in the link.
1579
1580 As above, we again permit a common symbol in a regular object to
1581 override a definition in a shared object if the shared object
0f8a2703 1582 symbol is a function or is weak. */
45d6a902
AM
1583
1584 flip = NULL;
77cfaee6 1585 if (!newdyn
45d6a902
AM
1586 && (newdef
1587 || (bfd_is_com_section (sec)
0a36a439 1588 && (oldweak || oldfunc)))
45d6a902
AM
1589 && olddyn
1590 && olddef
f5385ebf 1591 && h->def_dynamic)
45d6a902
AM
1592 {
1593 /* Change the hash table entry to undefined, and let
1594 _bfd_generic_link_add_one_symbol do the right thing with the
1595 new definition. */
1596
1597 h->root.type = bfd_link_hash_undefined;
1598 h->root.u.undef.abfd = h->root.u.def.section->owner;
1599 *size_change_ok = TRUE;
1600
1601 olddef = FALSE;
1602 olddyncommon = FALSE;
1603
1604 /* We again permit a type change when a common symbol may be
1605 overriding a function. */
1606
1607 if (bfd_is_com_section (sec))
0a36a439
L
1608 {
1609 if (oldfunc)
1610 {
1611 /* If a common symbol overrides a function, make sure
1612 that it isn't defined dynamically nor has type
1613 function. */
1614 h->def_dynamic = 0;
1615 h->type = STT_NOTYPE;
1616 }
1617 *type_change_ok = TRUE;
1618 }
45d6a902 1619
6c9b78e6
AM
1620 if (hi->root.type == bfd_link_hash_indirect)
1621 flip = hi;
45d6a902
AM
1622 else
1623 /* This union may have been set to be non-NULL when this symbol
1624 was seen in a dynamic object. We must force the union to be
1625 NULL, so that it is correct for a regular symbol. */
1626 h->verinfo.vertree = NULL;
1627 }
1628
1629 /* Handle the special case of a new common symbol merging with an
1630 old symbol that looks like it might be a common symbol defined in
1631 a shared object. Note that we have already handled the case in
1632 which a new common symbol should simply override the definition
1633 in the shared library. */
1634
1635 if (! newdyn
1636 && bfd_is_com_section (sec)
1637 && olddyncommon)
1638 {
1639 /* It would be best if we could set the hash table entry to a
1640 common symbol, but we don't know what to use for the section
1641 or the alignment. */
1a72702b
AM
1642 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1643 bfd_link_hash_common, sym->st_size);
45d6a902 1644
4cc11e76 1645 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1646 larger, pretend that the new symbol has its size. */
1647
1648 if (h->size > *pvalue)
1649 *pvalue = h->size;
1650
af44c138
L
1651 /* We need to remember the alignment required by the symbol
1652 in the dynamic object. */
1653 BFD_ASSERT (pold_alignment);
1654 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1655
1656 olddef = FALSE;
1657 olddyncommon = FALSE;
1658
1659 h->root.type = bfd_link_hash_undefined;
1660 h->root.u.undef.abfd = h->root.u.def.section->owner;
1661
1662 *size_change_ok = TRUE;
1663 *type_change_ok = TRUE;
1664
6c9b78e6
AM
1665 if (hi->root.type == bfd_link_hash_indirect)
1666 flip = hi;
45d6a902
AM
1667 else
1668 h->verinfo.vertree = NULL;
1669 }
1670
1671 if (flip != NULL)
1672 {
1673 /* Handle the case where we had a versioned symbol in a dynamic
1674 library and now find a definition in a normal object. In this
1675 case, we make the versioned symbol point to the normal one. */
45d6a902 1676 flip->root.type = h->root.type;
00cbee0a 1677 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1678 h->root.type = bfd_link_hash_indirect;
1679 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1680 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1681 if (h->def_dynamic)
45d6a902 1682 {
f5385ebf
AM
1683 h->def_dynamic = 0;
1684 flip->ref_dynamic = 1;
45d6a902
AM
1685 }
1686 }
1687
45d6a902
AM
1688 return TRUE;
1689}
1690
1691/* This function is called to create an indirect symbol from the
1692 default for the symbol with the default version if needed. The
4f3fedcf 1693 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1694 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1695
28caa186 1696static bfd_boolean
268b6b39
AM
1697_bfd_elf_add_default_symbol (bfd *abfd,
1698 struct bfd_link_info *info,
1699 struct elf_link_hash_entry *h,
1700 const char *name,
1701 Elf_Internal_Sym *sym,
4f3fedcf
AM
1702 asection *sec,
1703 bfd_vma value,
1704 bfd **poldbfd,
e3c9d234 1705 bfd_boolean *dynsym)
45d6a902
AM
1706{
1707 bfd_boolean type_change_ok;
1708 bfd_boolean size_change_ok;
1709 bfd_boolean skip;
1710 char *shortname;
1711 struct elf_link_hash_entry *hi;
1712 struct bfd_link_hash_entry *bh;
9c5bfbb7 1713 const struct elf_backend_data *bed;
45d6a902
AM
1714 bfd_boolean collect;
1715 bfd_boolean dynamic;
e3c9d234 1716 bfd_boolean override;
45d6a902
AM
1717 char *p;
1718 size_t len, shortlen;
ffd65175 1719 asection *tmp_sec;
6e33951e 1720 bfd_boolean matched;
45d6a902 1721
422f1182
L
1722 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1723 return TRUE;
1724
45d6a902
AM
1725 /* If this symbol has a version, and it is the default version, we
1726 create an indirect symbol from the default name to the fully
1727 decorated name. This will cause external references which do not
1728 specify a version to be bound to this version of the symbol. */
1729 p = strchr (name, ELF_VER_CHR);
422f1182
L
1730 if (h->versioned == unknown)
1731 {
1732 if (p == NULL)
1733 {
1734 h->versioned = unversioned;
1735 return TRUE;
1736 }
1737 else
1738 {
1739 if (p[1] != ELF_VER_CHR)
1740 {
1741 h->versioned = versioned_hidden;
1742 return TRUE;
1743 }
1744 else
1745 h->versioned = versioned;
1746 }
1747 }
4373f8af
L
1748 else
1749 {
1750 /* PR ld/19073: We may see an unversioned definition after the
1751 default version. */
1752 if (p == NULL)
1753 return TRUE;
1754 }
45d6a902 1755
45d6a902
AM
1756 bed = get_elf_backend_data (abfd);
1757 collect = bed->collect;
1758 dynamic = (abfd->flags & DYNAMIC) != 0;
1759
1760 shortlen = p - name;
a50b1753 1761 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1762 if (shortname == NULL)
1763 return FALSE;
1764 memcpy (shortname, name, shortlen);
1765 shortname[shortlen] = '\0';
1766
1767 /* We are going to create a new symbol. Merge it with any existing
1768 symbol with this name. For the purposes of the merge, act as
1769 though we were defining the symbol we just defined, although we
1770 actually going to define an indirect symbol. */
1771 type_change_ok = FALSE;
1772 size_change_ok = FALSE;
6e33951e 1773 matched = TRUE;
ffd65175
AM
1774 tmp_sec = sec;
1775 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1776 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1777 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1778 return FALSE;
1779
1780 if (skip)
1781 goto nondefault;
1782
5b677558
AM
1783 if (hi->def_regular)
1784 {
1785 /* If the undecorated symbol will have a version added by a
1786 script different to H, then don't indirect to/from the
1787 undecorated symbol. This isn't ideal because we may not yet
1788 have seen symbol versions, if given by a script on the
1789 command line rather than via --version-script. */
1790 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1791 {
1792 bfd_boolean hide;
1793
1794 hi->verinfo.vertree
1795 = bfd_find_version_for_sym (info->version_info,
1796 hi->root.root.string, &hide);
1797 if (hi->verinfo.vertree != NULL && hide)
1798 {
1799 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1800 goto nondefault;
1801 }
1802 }
1803 if (hi->verinfo.vertree != NULL
1804 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1805 goto nondefault;
1806 }
1807
45d6a902
AM
1808 if (! override)
1809 {
c6e8a9a8 1810 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1811 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1812 {
1813 bh = &hi->root;
1814 if (! (_bfd_generic_link_add_one_symbol
1815 (info, abfd, shortname, BSF_INDIRECT,
1816 bfd_ind_section_ptr,
1817 0, name, FALSE, collect, &bh)))
1818 return FALSE;
1819 hi = (struct elf_link_hash_entry *) bh;
1820 }
45d6a902
AM
1821 }
1822 else
1823 {
1824 /* In this case the symbol named SHORTNAME is overriding the
1825 indirect symbol we want to add. We were planning on making
1826 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1827 is the name without a version. NAME is the fully versioned
1828 name, and it is the default version.
1829
1830 Overriding means that we already saw a definition for the
1831 symbol SHORTNAME in a regular object, and it is overriding
1832 the symbol defined in the dynamic object.
1833
1834 When this happens, we actually want to change NAME, the
1835 symbol we just added, to refer to SHORTNAME. This will cause
1836 references to NAME in the shared object to become references
1837 to SHORTNAME in the regular object. This is what we expect
1838 when we override a function in a shared object: that the
1839 references in the shared object will be mapped to the
1840 definition in the regular object. */
1841
1842 while (hi->root.type == bfd_link_hash_indirect
1843 || hi->root.type == bfd_link_hash_warning)
1844 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1845
1846 h->root.type = bfd_link_hash_indirect;
1847 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1848 if (h->def_dynamic)
45d6a902 1849 {
f5385ebf
AM
1850 h->def_dynamic = 0;
1851 hi->ref_dynamic = 1;
1852 if (hi->ref_regular
1853 || hi->def_regular)
45d6a902 1854 {
c152c796 1855 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1856 return FALSE;
1857 }
1858 }
1859
1860 /* Now set HI to H, so that the following code will set the
1861 other fields correctly. */
1862 hi = h;
1863 }
1864
fab4a87f
L
1865 /* Check if HI is a warning symbol. */
1866 if (hi->root.type == bfd_link_hash_warning)
1867 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1868
45d6a902
AM
1869 /* If there is a duplicate definition somewhere, then HI may not
1870 point to an indirect symbol. We will have reported an error to
1871 the user in that case. */
1872
1873 if (hi->root.type == bfd_link_hash_indirect)
1874 {
1875 struct elf_link_hash_entry *ht;
1876
45d6a902 1877 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1878 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1879
68c88cd4
AM
1880 /* A reference to the SHORTNAME symbol from a dynamic library
1881 will be satisfied by the versioned symbol at runtime. In
1882 effect, we have a reference to the versioned symbol. */
1883 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1884 hi->dynamic_def |= ht->dynamic_def;
1885
45d6a902
AM
1886 /* See if the new flags lead us to realize that the symbol must
1887 be dynamic. */
1888 if (! *dynsym)
1889 {
1890 if (! dynamic)
1891 {
0e1862bb 1892 if (! bfd_link_executable (info)
90c984fc 1893 || hi->def_dynamic
f5385ebf 1894 || hi->ref_dynamic)
45d6a902
AM
1895 *dynsym = TRUE;
1896 }
1897 else
1898 {
f5385ebf 1899 if (hi->ref_regular)
45d6a902
AM
1900 *dynsym = TRUE;
1901 }
1902 }
1903 }
1904
1905 /* We also need to define an indirection from the nondefault version
1906 of the symbol. */
1907
1908nondefault:
1909 len = strlen (name);
a50b1753 1910 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1911 if (shortname == NULL)
1912 return FALSE;
1913 memcpy (shortname, name, shortlen);
1914 memcpy (shortname + shortlen, p + 1, len - shortlen);
1915
1916 /* Once again, merge with any existing symbol. */
1917 type_change_ok = FALSE;
1918 size_change_ok = FALSE;
ffd65175
AM
1919 tmp_sec = sec;
1920 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1921 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1922 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1923 return FALSE;
1924
1925 if (skip)
1926 return TRUE;
1927
1928 if (override)
1929 {
1930 /* Here SHORTNAME is a versioned name, so we don't expect to see
1931 the type of override we do in the case above unless it is
4cc11e76 1932 overridden by a versioned definition. */
45d6a902
AM
1933 if (hi->root.type != bfd_link_hash_defined
1934 && hi->root.type != bfd_link_hash_defweak)
1935 (*_bfd_error_handler)
d003868e
AM
1936 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1937 abfd, shortname);
45d6a902
AM
1938 }
1939 else
1940 {
1941 bh = &hi->root;
1942 if (! (_bfd_generic_link_add_one_symbol
1943 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1944 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1945 return FALSE;
1946 hi = (struct elf_link_hash_entry *) bh;
1947
1948 /* If there is a duplicate definition somewhere, then HI may not
1949 point to an indirect symbol. We will have reported an error
1950 to the user in that case. */
1951
1952 if (hi->root.type == bfd_link_hash_indirect)
1953 {
fcfa13d2 1954 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
1955 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1956 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
1957
1958 /* See if the new flags lead us to realize that the symbol
1959 must be dynamic. */
1960 if (! *dynsym)
1961 {
1962 if (! dynamic)
1963 {
0e1862bb 1964 if (! bfd_link_executable (info)
f5385ebf 1965 || hi->ref_dynamic)
45d6a902
AM
1966 *dynsym = TRUE;
1967 }
1968 else
1969 {
f5385ebf 1970 if (hi->ref_regular)
45d6a902
AM
1971 *dynsym = TRUE;
1972 }
1973 }
1974 }
1975 }
1976
1977 return TRUE;
1978}
1979\f
1980/* This routine is used to export all defined symbols into the dynamic
1981 symbol table. It is called via elf_link_hash_traverse. */
1982
28caa186 1983static bfd_boolean
268b6b39 1984_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1985{
a50b1753 1986 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
1987
1988 /* Ignore indirect symbols. These are added by the versioning code. */
1989 if (h->root.type == bfd_link_hash_indirect)
1990 return TRUE;
1991
7686d77d
AM
1992 /* Ignore this if we won't export it. */
1993 if (!eif->info->export_dynamic && !h->dynamic)
1994 return TRUE;
45d6a902
AM
1995
1996 if (h->dynindx == -1
fd91d419
L
1997 && (h->def_regular || h->ref_regular)
1998 && ! bfd_hide_sym_by_version (eif->info->version_info,
1999 h->root.root.string))
45d6a902 2000 {
fd91d419 2001 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2002 {
fd91d419
L
2003 eif->failed = TRUE;
2004 return FALSE;
45d6a902
AM
2005 }
2006 }
2007
2008 return TRUE;
2009}
2010\f
2011/* Look through the symbols which are defined in other shared
2012 libraries and referenced here. Update the list of version
2013 dependencies. This will be put into the .gnu.version_r section.
2014 This function is called via elf_link_hash_traverse. */
2015
28caa186 2016static bfd_boolean
268b6b39
AM
2017_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2018 void *data)
45d6a902 2019{
a50b1753 2020 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2021 Elf_Internal_Verneed *t;
2022 Elf_Internal_Vernaux *a;
2023 bfd_size_type amt;
2024
45d6a902
AM
2025 /* We only care about symbols defined in shared objects with version
2026 information. */
f5385ebf
AM
2027 if (!h->def_dynamic
2028 || h->def_regular
45d6a902 2029 || h->dynindx == -1
7b20f099
AM
2030 || h->verinfo.verdef == NULL
2031 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2032 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2033 return TRUE;
2034
2035 /* See if we already know about this version. */
28caa186
AM
2036 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2037 t != NULL;
2038 t = t->vn_nextref)
45d6a902
AM
2039 {
2040 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2041 continue;
2042
2043 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2044 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2045 return TRUE;
2046
2047 break;
2048 }
2049
2050 /* This is a new version. Add it to tree we are building. */
2051
2052 if (t == NULL)
2053 {
2054 amt = sizeof *t;
a50b1753 2055 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2056 if (t == NULL)
2057 {
2058 rinfo->failed = TRUE;
2059 return FALSE;
2060 }
2061
2062 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2063 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2064 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2065 }
2066
2067 amt = sizeof *a;
a50b1753 2068 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2069 if (a == NULL)
2070 {
2071 rinfo->failed = TRUE;
2072 return FALSE;
2073 }
45d6a902
AM
2074
2075 /* Note that we are copying a string pointer here, and testing it
2076 above. If bfd_elf_string_from_elf_section is ever changed to
2077 discard the string data when low in memory, this will have to be
2078 fixed. */
2079 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2080
2081 a->vna_flags = h->verinfo.verdef->vd_flags;
2082 a->vna_nextptr = t->vn_auxptr;
2083
2084 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2085 ++rinfo->vers;
2086
2087 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2088
2089 t->vn_auxptr = a;
2090
2091 return TRUE;
2092}
2093
2094/* Figure out appropriate versions for all the symbols. We may not
2095 have the version number script until we have read all of the input
2096 files, so until that point we don't know which symbols should be
2097 local. This function is called via elf_link_hash_traverse. */
2098
28caa186 2099static bfd_boolean
268b6b39 2100_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2101{
28caa186 2102 struct elf_info_failed *sinfo;
45d6a902 2103 struct bfd_link_info *info;
9c5bfbb7 2104 const struct elf_backend_data *bed;
45d6a902
AM
2105 struct elf_info_failed eif;
2106 char *p;
45d6a902 2107
a50b1753 2108 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2109 info = sinfo->info;
2110
45d6a902
AM
2111 /* Fix the symbol flags. */
2112 eif.failed = FALSE;
2113 eif.info = info;
2114 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2115 {
2116 if (eif.failed)
2117 sinfo->failed = TRUE;
2118 return FALSE;
2119 }
2120
2121 /* We only need version numbers for symbols defined in regular
2122 objects. */
f5385ebf 2123 if (!h->def_regular)
45d6a902
AM
2124 return TRUE;
2125
28caa186 2126 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2127 p = strchr (h->root.root.string, ELF_VER_CHR);
2128 if (p != NULL && h->verinfo.vertree == NULL)
2129 {
2130 struct bfd_elf_version_tree *t;
45d6a902 2131
45d6a902
AM
2132 ++p;
2133 if (*p == ELF_VER_CHR)
6e33951e 2134 ++p;
45d6a902
AM
2135
2136 /* If there is no version string, we can just return out. */
2137 if (*p == '\0')
6e33951e 2138 return TRUE;
45d6a902
AM
2139
2140 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2141 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2142 {
2143 if (strcmp (t->name, p) == 0)
2144 {
2145 size_t len;
2146 char *alc;
2147 struct bfd_elf_version_expr *d;
2148
2149 len = p - h->root.root.string;
a50b1753 2150 alc = (char *) bfd_malloc (len);
45d6a902 2151 if (alc == NULL)
14b1c01e
AM
2152 {
2153 sinfo->failed = TRUE;
2154 return FALSE;
2155 }
45d6a902
AM
2156 memcpy (alc, h->root.root.string, len - 1);
2157 alc[len - 1] = '\0';
2158 if (alc[len - 2] == ELF_VER_CHR)
2159 alc[len - 2] = '\0';
2160
2161 h->verinfo.vertree = t;
2162 t->used = TRUE;
2163 d = NULL;
2164
108ba305
JJ
2165 if (t->globals.list != NULL)
2166 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2167
2168 /* See if there is anything to force this symbol to
2169 local scope. */
108ba305 2170 if (d == NULL && t->locals.list != NULL)
45d6a902 2171 {
108ba305
JJ
2172 d = (*t->match) (&t->locals, NULL, alc);
2173 if (d != NULL
2174 && h->dynindx != -1
108ba305
JJ
2175 && ! info->export_dynamic)
2176 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2177 }
2178
2179 free (alc);
2180 break;
2181 }
2182 }
2183
2184 /* If we are building an application, we need to create a
2185 version node for this version. */
0e1862bb 2186 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2187 {
2188 struct bfd_elf_version_tree **pp;
2189 int version_index;
2190
2191 /* If we aren't going to export this symbol, we don't need
2192 to worry about it. */
2193 if (h->dynindx == -1)
2194 return TRUE;
2195
ef53be89
AM
2196 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2197 sizeof *t);
45d6a902
AM
2198 if (t == NULL)
2199 {
2200 sinfo->failed = TRUE;
2201 return FALSE;
2202 }
2203
45d6a902 2204 t->name = p;
45d6a902
AM
2205 t->name_indx = (unsigned int) -1;
2206 t->used = TRUE;
2207
2208 version_index = 1;
2209 /* Don't count anonymous version tag. */
fd91d419
L
2210 if (sinfo->info->version_info != NULL
2211 && sinfo->info->version_info->vernum == 0)
45d6a902 2212 version_index = 0;
fd91d419
L
2213 for (pp = &sinfo->info->version_info;
2214 *pp != NULL;
2215 pp = &(*pp)->next)
45d6a902
AM
2216 ++version_index;
2217 t->vernum = version_index;
2218
2219 *pp = t;
2220
2221 h->verinfo.vertree = t;
2222 }
2223 else if (t == NULL)
2224 {
2225 /* We could not find the version for a symbol when
2226 generating a shared archive. Return an error. */
2227 (*_bfd_error_handler)
c55fe096 2228 (_("%B: version node not found for symbol %s"),
28caa186 2229 info->output_bfd, h->root.root.string);
45d6a902
AM
2230 bfd_set_error (bfd_error_bad_value);
2231 sinfo->failed = TRUE;
2232 return FALSE;
2233 }
45d6a902
AM
2234 }
2235
2236 /* If we don't have a version for this symbol, see if we can find
2237 something. */
fd91d419 2238 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2239 {
1e8fa21e 2240 bfd_boolean hide;
ae5a3597 2241
fd91d419
L
2242 h->verinfo.vertree
2243 = bfd_find_version_for_sym (sinfo->info->version_info,
2244 h->root.root.string, &hide);
1e8fa21e
AM
2245 if (h->verinfo.vertree != NULL && hide)
2246 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2247 }
2248
2249 return TRUE;
2250}
2251\f
45d6a902
AM
2252/* Read and swap the relocs from the section indicated by SHDR. This
2253 may be either a REL or a RELA section. The relocations are
2254 translated into RELA relocations and stored in INTERNAL_RELOCS,
2255 which should have already been allocated to contain enough space.
2256 The EXTERNAL_RELOCS are a buffer where the external form of the
2257 relocations should be stored.
2258
2259 Returns FALSE if something goes wrong. */
2260
2261static bfd_boolean
268b6b39 2262elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2263 asection *sec,
268b6b39
AM
2264 Elf_Internal_Shdr *shdr,
2265 void *external_relocs,
2266 Elf_Internal_Rela *internal_relocs)
45d6a902 2267{
9c5bfbb7 2268 const struct elf_backend_data *bed;
268b6b39 2269 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2270 const bfd_byte *erela;
2271 const bfd_byte *erelaend;
2272 Elf_Internal_Rela *irela;
243ef1e0
L
2273 Elf_Internal_Shdr *symtab_hdr;
2274 size_t nsyms;
45d6a902 2275
45d6a902
AM
2276 /* Position ourselves at the start of the section. */
2277 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2278 return FALSE;
2279
2280 /* Read the relocations. */
2281 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2282 return FALSE;
2283
243ef1e0 2284 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2285 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2286
45d6a902
AM
2287 bed = get_elf_backend_data (abfd);
2288
2289 /* Convert the external relocations to the internal format. */
2290 if (shdr->sh_entsize == bed->s->sizeof_rel)
2291 swap_in = bed->s->swap_reloc_in;
2292 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2293 swap_in = bed->s->swap_reloca_in;
2294 else
2295 {
2296 bfd_set_error (bfd_error_wrong_format);
2297 return FALSE;
2298 }
2299
a50b1753 2300 erela = (const bfd_byte *) external_relocs;
51992aec 2301 erelaend = erela + shdr->sh_size;
45d6a902
AM
2302 irela = internal_relocs;
2303 while (erela < erelaend)
2304 {
243ef1e0
L
2305 bfd_vma r_symndx;
2306
45d6a902 2307 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2308 r_symndx = ELF32_R_SYM (irela->r_info);
2309 if (bed->s->arch_size == 64)
2310 r_symndx >>= 24;
ce98a316
NC
2311 if (nsyms > 0)
2312 {
2313 if ((size_t) r_symndx >= nsyms)
2314 {
2315 (*_bfd_error_handler)
2316 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2317 " for offset 0x%lx in section `%A'"),
2318 abfd, sec,
2319 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2320 bfd_set_error (bfd_error_bad_value);
2321 return FALSE;
2322 }
2323 }
cf35638d 2324 else if (r_symndx != STN_UNDEF)
243ef1e0
L
2325 {
2326 (*_bfd_error_handler)
ce98a316
NC
2327 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2328 " when the object file has no symbol table"),
d003868e
AM
2329 abfd, sec,
2330 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2331 bfd_set_error (bfd_error_bad_value);
2332 return FALSE;
2333 }
45d6a902
AM
2334 irela += bed->s->int_rels_per_ext_rel;
2335 erela += shdr->sh_entsize;
2336 }
2337
2338 return TRUE;
2339}
2340
2341/* Read and swap the relocs for a section O. They may have been
2342 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2343 not NULL, they are used as buffers to read into. They are known to
2344 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2345 the return value is allocated using either malloc or bfd_alloc,
2346 according to the KEEP_MEMORY argument. If O has two relocation
2347 sections (both REL and RELA relocations), then the REL_HDR
2348 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2349 RELA_HDR relocations. */
45d6a902
AM
2350
2351Elf_Internal_Rela *
268b6b39
AM
2352_bfd_elf_link_read_relocs (bfd *abfd,
2353 asection *o,
2354 void *external_relocs,
2355 Elf_Internal_Rela *internal_relocs,
2356 bfd_boolean keep_memory)
45d6a902 2357{
268b6b39 2358 void *alloc1 = NULL;
45d6a902 2359 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2360 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2361 struct bfd_elf_section_data *esdo = elf_section_data (o);
2362 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2363
d4730f92
BS
2364 if (esdo->relocs != NULL)
2365 return esdo->relocs;
45d6a902
AM
2366
2367 if (o->reloc_count == 0)
2368 return NULL;
2369
45d6a902
AM
2370 if (internal_relocs == NULL)
2371 {
2372 bfd_size_type size;
2373
2374 size = o->reloc_count;
2375 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2376 if (keep_memory)
a50b1753 2377 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2378 else
a50b1753 2379 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2380 if (internal_relocs == NULL)
2381 goto error_return;
2382 }
2383
2384 if (external_relocs == NULL)
2385 {
d4730f92
BS
2386 bfd_size_type size = 0;
2387
2388 if (esdo->rel.hdr)
2389 size += esdo->rel.hdr->sh_size;
2390 if (esdo->rela.hdr)
2391 size += esdo->rela.hdr->sh_size;
45d6a902 2392
268b6b39 2393 alloc1 = bfd_malloc (size);
45d6a902
AM
2394 if (alloc1 == NULL)
2395 goto error_return;
2396 external_relocs = alloc1;
2397 }
2398
d4730f92
BS
2399 internal_rela_relocs = internal_relocs;
2400 if (esdo->rel.hdr)
2401 {
2402 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2403 external_relocs,
2404 internal_relocs))
2405 goto error_return;
2406 external_relocs = (((bfd_byte *) external_relocs)
2407 + esdo->rel.hdr->sh_size);
2408 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2409 * bed->s->int_rels_per_ext_rel);
2410 }
2411
2412 if (esdo->rela.hdr
2413 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2414 external_relocs,
2415 internal_rela_relocs)))
45d6a902
AM
2416 goto error_return;
2417
2418 /* Cache the results for next time, if we can. */
2419 if (keep_memory)
d4730f92 2420 esdo->relocs = internal_relocs;
45d6a902
AM
2421
2422 if (alloc1 != NULL)
2423 free (alloc1);
2424
2425 /* Don't free alloc2, since if it was allocated we are passing it
2426 back (under the name of internal_relocs). */
2427
2428 return internal_relocs;
2429
2430 error_return:
2431 if (alloc1 != NULL)
2432 free (alloc1);
2433 if (alloc2 != NULL)
4dd07732
AM
2434 {
2435 if (keep_memory)
2436 bfd_release (abfd, alloc2);
2437 else
2438 free (alloc2);
2439 }
45d6a902
AM
2440 return NULL;
2441}
2442
2443/* Compute the size of, and allocate space for, REL_HDR which is the
2444 section header for a section containing relocations for O. */
2445
28caa186 2446static bfd_boolean
268b6b39 2447_bfd_elf_link_size_reloc_section (bfd *abfd,
d4730f92 2448 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2449{
d4730f92 2450 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2451
2452 /* That allows us to calculate the size of the section. */
d4730f92 2453 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2454
2455 /* The contents field must last into write_object_contents, so we
2456 allocate it with bfd_alloc rather than malloc. Also since we
2457 cannot be sure that the contents will actually be filled in,
2458 we zero the allocated space. */
a50b1753 2459 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2460 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2461 return FALSE;
2462
d4730f92 2463 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2464 {
2465 struct elf_link_hash_entry **p;
2466
ca4be51c
AM
2467 p = ((struct elf_link_hash_entry **)
2468 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2469 if (p == NULL)
2470 return FALSE;
2471
d4730f92 2472 reldata->hashes = p;
45d6a902
AM
2473 }
2474
2475 return TRUE;
2476}
2477
2478/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2479 originated from the section given by INPUT_REL_HDR) to the
2480 OUTPUT_BFD. */
2481
2482bfd_boolean
268b6b39
AM
2483_bfd_elf_link_output_relocs (bfd *output_bfd,
2484 asection *input_section,
2485 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2486 Elf_Internal_Rela *internal_relocs,
2487 struct elf_link_hash_entry **rel_hash
2488 ATTRIBUTE_UNUSED)
45d6a902
AM
2489{
2490 Elf_Internal_Rela *irela;
2491 Elf_Internal_Rela *irelaend;
2492 bfd_byte *erel;
d4730f92 2493 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2494 asection *output_section;
9c5bfbb7 2495 const struct elf_backend_data *bed;
268b6b39 2496 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2497 struct bfd_elf_section_data *esdo;
45d6a902
AM
2498
2499 output_section = input_section->output_section;
45d6a902 2500
d4730f92
BS
2501 bed = get_elf_backend_data (output_bfd);
2502 esdo = elf_section_data (output_section);
2503 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2504 {
d4730f92
BS
2505 output_reldata = &esdo->rel;
2506 swap_out = bed->s->swap_reloc_out;
45d6a902 2507 }
d4730f92
BS
2508 else if (esdo->rela.hdr
2509 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2510 {
d4730f92
BS
2511 output_reldata = &esdo->rela;
2512 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2513 }
2514 else
2515 {
2516 (*_bfd_error_handler)
d003868e
AM
2517 (_("%B: relocation size mismatch in %B section %A"),
2518 output_bfd, input_section->owner, input_section);
297d8443 2519 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2520 return FALSE;
2521 }
2522
d4730f92
BS
2523 erel = output_reldata->hdr->contents;
2524 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2525 irela = internal_relocs;
2526 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2527 * bed->s->int_rels_per_ext_rel);
2528 while (irela < irelaend)
2529 {
2530 (*swap_out) (output_bfd, irela, erel);
2531 irela += bed->s->int_rels_per_ext_rel;
2532 erel += input_rel_hdr->sh_entsize;
2533 }
2534
2535 /* Bump the counter, so that we know where to add the next set of
2536 relocations. */
d4730f92 2537 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2538
2539 return TRUE;
2540}
2541\f
508c3946
L
2542/* Make weak undefined symbols in PIE dynamic. */
2543
2544bfd_boolean
2545_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2546 struct elf_link_hash_entry *h)
2547{
0e1862bb 2548 if (bfd_link_pie (info)
508c3946
L
2549 && h->dynindx == -1
2550 && h->root.type == bfd_link_hash_undefweak)
2551 return bfd_elf_link_record_dynamic_symbol (info, h);
2552
2553 return TRUE;
2554}
2555
45d6a902
AM
2556/* Fix up the flags for a symbol. This handles various cases which
2557 can only be fixed after all the input files are seen. This is
2558 currently called by both adjust_dynamic_symbol and
2559 assign_sym_version, which is unnecessary but perhaps more robust in
2560 the face of future changes. */
2561
28caa186 2562static bfd_boolean
268b6b39
AM
2563_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2564 struct elf_info_failed *eif)
45d6a902 2565{
33774f08 2566 const struct elf_backend_data *bed;
508c3946 2567
45d6a902
AM
2568 /* If this symbol was mentioned in a non-ELF file, try to set
2569 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2570 permit a non-ELF file to correctly refer to a symbol defined in
2571 an ELF dynamic object. */
f5385ebf 2572 if (h->non_elf)
45d6a902
AM
2573 {
2574 while (h->root.type == bfd_link_hash_indirect)
2575 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2576
2577 if (h->root.type != bfd_link_hash_defined
2578 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2579 {
2580 h->ref_regular = 1;
2581 h->ref_regular_nonweak = 1;
2582 }
45d6a902
AM
2583 else
2584 {
2585 if (h->root.u.def.section->owner != NULL
2586 && (bfd_get_flavour (h->root.u.def.section->owner)
2587 == bfd_target_elf_flavour))
f5385ebf
AM
2588 {
2589 h->ref_regular = 1;
2590 h->ref_regular_nonweak = 1;
2591 }
45d6a902 2592 else
f5385ebf 2593 h->def_regular = 1;
45d6a902
AM
2594 }
2595
2596 if (h->dynindx == -1
f5385ebf
AM
2597 && (h->def_dynamic
2598 || h->ref_dynamic))
45d6a902 2599 {
c152c796 2600 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2601 {
2602 eif->failed = TRUE;
2603 return FALSE;
2604 }
2605 }
2606 }
2607 else
2608 {
f5385ebf 2609 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2610 was first seen in a non-ELF file. Fortunately, if the symbol
2611 was first seen in an ELF file, we're probably OK unless the
2612 symbol was defined in a non-ELF file. Catch that case here.
2613 FIXME: We're still in trouble if the symbol was first seen in
2614 a dynamic object, and then later in a non-ELF regular object. */
2615 if ((h->root.type == bfd_link_hash_defined
2616 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2617 && !h->def_regular
45d6a902
AM
2618 && (h->root.u.def.section->owner != NULL
2619 ? (bfd_get_flavour (h->root.u.def.section->owner)
2620 != bfd_target_elf_flavour)
2621 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2622 && !h->def_dynamic)))
2623 h->def_regular = 1;
45d6a902
AM
2624 }
2625
508c3946 2626 /* Backend specific symbol fixup. */
33774f08
AM
2627 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2628 if (bed->elf_backend_fixup_symbol
2629 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2630 return FALSE;
508c3946 2631
45d6a902
AM
2632 /* If this is a final link, and the symbol was defined as a common
2633 symbol in a regular object file, and there was no definition in
2634 any dynamic object, then the linker will have allocated space for
f5385ebf 2635 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2636 flag will not have been set. */
2637 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2638 && !h->def_regular
2639 && h->ref_regular
2640 && !h->def_dynamic
96f29d96 2641 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2642 h->def_regular = 1;
45d6a902
AM
2643
2644 /* If -Bsymbolic was used (which means to bind references to global
2645 symbols to the definition within the shared object), and this
2646 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2647 need a PLT entry. Likewise, if the symbol has non-default
2648 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2649 will force it local. */
f5385ebf 2650 if (h->needs_plt
0e1862bb 2651 && bfd_link_pic (eif->info)
0eddce27 2652 && is_elf_hash_table (eif->info->hash)
55255dae 2653 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2654 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2655 && h->def_regular)
45d6a902 2656 {
45d6a902
AM
2657 bfd_boolean force_local;
2658
45d6a902
AM
2659 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2660 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2661 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2662 }
2663
2664 /* If a weak undefined symbol has non-default visibility, we also
2665 hide it from the dynamic linker. */
9c7a29a3 2666 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2667 && h->root.type == bfd_link_hash_undefweak)
33774f08 2668 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2669
2670 /* If this is a weak defined symbol in a dynamic object, and we know
2671 the real definition in the dynamic object, copy interesting flags
2672 over to the real definition. */
f6e332e6 2673 if (h->u.weakdef != NULL)
45d6a902 2674 {
45d6a902
AM
2675 /* If the real definition is defined by a regular object file,
2676 don't do anything special. See the longer description in
2677 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2678 if (h->u.weakdef->def_regular)
f6e332e6 2679 h->u.weakdef = NULL;
45d6a902 2680 else
a26587ba 2681 {
4e6b54a6
AM
2682 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2683
2684 while (h->root.type == bfd_link_hash_indirect)
2685 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2686
2687 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2688 || h->root.type == bfd_link_hash_defweak);
2689 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2690 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2691 || weakdef->root.type == bfd_link_hash_defweak);
2692 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2693 }
45d6a902
AM
2694 }
2695
2696 return TRUE;
2697}
2698
2699/* Make the backend pick a good value for a dynamic symbol. This is
2700 called via elf_link_hash_traverse, and also calls itself
2701 recursively. */
2702
28caa186 2703static bfd_boolean
268b6b39 2704_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2705{
a50b1753 2706 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2707 bfd *dynobj;
9c5bfbb7 2708 const struct elf_backend_data *bed;
45d6a902 2709
0eddce27 2710 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2711 return FALSE;
2712
45d6a902
AM
2713 /* Ignore indirect symbols. These are added by the versioning code. */
2714 if (h->root.type == bfd_link_hash_indirect)
2715 return TRUE;
2716
2717 /* Fix the symbol flags. */
2718 if (! _bfd_elf_fix_symbol_flags (h, eif))
2719 return FALSE;
2720
2721 /* If this symbol does not require a PLT entry, and it is not
2722 defined by a dynamic object, or is not referenced by a regular
2723 object, ignore it. We do have to handle a weak defined symbol,
2724 even if no regular object refers to it, if we decided to add it
2725 to the dynamic symbol table. FIXME: Do we normally need to worry
2726 about symbols which are defined by one dynamic object and
2727 referenced by another one? */
f5385ebf 2728 if (!h->needs_plt
91e21fb7 2729 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2730 && (h->def_regular
2731 || !h->def_dynamic
2732 || (!h->ref_regular
f6e332e6 2733 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2734 {
a6aa5195 2735 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2736 return TRUE;
2737 }
2738
2739 /* If we've already adjusted this symbol, don't do it again. This
2740 can happen via a recursive call. */
f5385ebf 2741 if (h->dynamic_adjusted)
45d6a902
AM
2742 return TRUE;
2743
2744 /* Don't look at this symbol again. Note that we must set this
2745 after checking the above conditions, because we may look at a
2746 symbol once, decide not to do anything, and then get called
2747 recursively later after REF_REGULAR is set below. */
f5385ebf 2748 h->dynamic_adjusted = 1;
45d6a902
AM
2749
2750 /* If this is a weak definition, and we know a real definition, and
2751 the real symbol is not itself defined by a regular object file,
2752 then get a good value for the real definition. We handle the
2753 real symbol first, for the convenience of the backend routine.
2754
2755 Note that there is a confusing case here. If the real definition
2756 is defined by a regular object file, we don't get the real symbol
2757 from the dynamic object, but we do get the weak symbol. If the
2758 processor backend uses a COPY reloc, then if some routine in the
2759 dynamic object changes the real symbol, we will not see that
2760 change in the corresponding weak symbol. This is the way other
2761 ELF linkers work as well, and seems to be a result of the shared
2762 library model.
2763
2764 I will clarify this issue. Most SVR4 shared libraries define the
2765 variable _timezone and define timezone as a weak synonym. The
2766 tzset call changes _timezone. If you write
2767 extern int timezone;
2768 int _timezone = 5;
2769 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2770 you might expect that, since timezone is a synonym for _timezone,
2771 the same number will print both times. However, if the processor
2772 backend uses a COPY reloc, then actually timezone will be copied
2773 into your process image, and, since you define _timezone
2774 yourself, _timezone will not. Thus timezone and _timezone will
2775 wind up at different memory locations. The tzset call will set
2776 _timezone, leaving timezone unchanged. */
2777
f6e332e6 2778 if (h->u.weakdef != NULL)
45d6a902 2779 {
ec24dc88
AM
2780 /* If we get to this point, there is an implicit reference to
2781 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2782 h->u.weakdef->ref_regular = 1;
45d6a902 2783
ec24dc88
AM
2784 /* Ensure that the backend adjust_dynamic_symbol function sees
2785 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2786 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2787 return FALSE;
2788 }
2789
2790 /* If a symbol has no type and no size and does not require a PLT
2791 entry, then we are probably about to do the wrong thing here: we
2792 are probably going to create a COPY reloc for an empty object.
2793 This case can arise when a shared object is built with assembly
2794 code, and the assembly code fails to set the symbol type. */
2795 if (h->size == 0
2796 && h->type == STT_NOTYPE
f5385ebf 2797 && !h->needs_plt)
45d6a902
AM
2798 (*_bfd_error_handler)
2799 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2800 h->root.root.string);
2801
2802 dynobj = elf_hash_table (eif->info)->dynobj;
2803 bed = get_elf_backend_data (dynobj);
e7c33416 2804
45d6a902
AM
2805 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2806 {
2807 eif->failed = TRUE;
2808 return FALSE;
2809 }
2810
2811 return TRUE;
2812}
2813
027297b7
L
2814/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2815 DYNBSS. */
2816
2817bfd_boolean
6cabe1ea
AM
2818_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2819 struct elf_link_hash_entry *h,
027297b7
L
2820 asection *dynbss)
2821{
91ac5911 2822 unsigned int power_of_two;
027297b7
L
2823 bfd_vma mask;
2824 asection *sec = h->root.u.def.section;
2825
2826 /* The section aligment of definition is the maximum alignment
91ac5911
L
2827 requirement of symbols defined in the section. Since we don't
2828 know the symbol alignment requirement, we start with the
2829 maximum alignment and check low bits of the symbol address
2830 for the minimum alignment. */
2831 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2832 mask = ((bfd_vma) 1 << power_of_two) - 1;
2833 while ((h->root.u.def.value & mask) != 0)
2834 {
2835 mask >>= 1;
2836 --power_of_two;
2837 }
027297b7 2838
91ac5911
L
2839 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2840 dynbss))
027297b7
L
2841 {
2842 /* Adjust the section alignment if needed. */
2843 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2844 power_of_two))
027297b7
L
2845 return FALSE;
2846 }
2847
91ac5911 2848 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2849 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2850
2851 /* Define the symbol as being at this point in DYNBSS. */
2852 h->root.u.def.section = dynbss;
2853 h->root.u.def.value = dynbss->size;
2854
2855 /* Increment the size of DYNBSS to make room for the symbol. */
2856 dynbss->size += h->size;
2857
f7483970
L
2858 /* No error if extern_protected_data is true. */
2859 if (h->protected_def
889c2a67
L
2860 && (!info->extern_protected_data
2861 || (info->extern_protected_data < 0
2862 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2863 info->callbacks->einfo
2864 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2865 h->root.root.string);
6cabe1ea 2866
027297b7
L
2867 return TRUE;
2868}
2869
45d6a902
AM
2870/* Adjust all external symbols pointing into SEC_MERGE sections
2871 to reflect the object merging within the sections. */
2872
28caa186 2873static bfd_boolean
268b6b39 2874_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2875{
2876 asection *sec;
2877
45d6a902
AM
2878 if ((h->root.type == bfd_link_hash_defined
2879 || h->root.type == bfd_link_hash_defweak)
2880 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2881 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2882 {
a50b1753 2883 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2884
2885 h->root.u.def.value =
2886 _bfd_merged_section_offset (output_bfd,
2887 &h->root.u.def.section,
2888 elf_section_data (sec)->sec_info,
753731ee 2889 h->root.u.def.value);
45d6a902
AM
2890 }
2891
2892 return TRUE;
2893}
986a241f
RH
2894
2895/* Returns false if the symbol referred to by H should be considered
2896 to resolve local to the current module, and true if it should be
2897 considered to bind dynamically. */
2898
2899bfd_boolean
268b6b39
AM
2900_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2901 struct bfd_link_info *info,
89a2ee5a 2902 bfd_boolean not_local_protected)
986a241f
RH
2903{
2904 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2905 const struct elf_backend_data *bed;
2906 struct elf_link_hash_table *hash_table;
986a241f
RH
2907
2908 if (h == NULL)
2909 return FALSE;
2910
2911 while (h->root.type == bfd_link_hash_indirect
2912 || h->root.type == bfd_link_hash_warning)
2913 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2914
2915 /* If it was forced local, then clearly it's not dynamic. */
2916 if (h->dynindx == -1)
2917 return FALSE;
f5385ebf 2918 if (h->forced_local)
986a241f
RH
2919 return FALSE;
2920
2921 /* Identify the cases where name binding rules say that a
2922 visible symbol resolves locally. */
0e1862bb
L
2923 binding_stays_local_p = (bfd_link_executable (info)
2924 || SYMBOLIC_BIND (info, h));
986a241f
RH
2925
2926 switch (ELF_ST_VISIBILITY (h->other))
2927 {
2928 case STV_INTERNAL:
2929 case STV_HIDDEN:
2930 return FALSE;
2931
2932 case STV_PROTECTED:
fcb93ecf
PB
2933 hash_table = elf_hash_table (info);
2934 if (!is_elf_hash_table (hash_table))
2935 return FALSE;
2936
2937 bed = get_elf_backend_data (hash_table->dynobj);
2938
986a241f
RH
2939 /* Proper resolution for function pointer equality may require
2940 that these symbols perhaps be resolved dynamically, even though
2941 we should be resolving them to the current module. */
89a2ee5a 2942 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2943 binding_stays_local_p = TRUE;
2944 break;
2945
2946 default:
986a241f
RH
2947 break;
2948 }
2949
aa37626c 2950 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2951 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2952 return TRUE;
2953
986a241f
RH
2954 /* Otherwise, the symbol is dynamic if binding rules don't tell
2955 us that it remains local. */
2956 return !binding_stays_local_p;
2957}
f6c52c13
AM
2958
2959/* Return true if the symbol referred to by H should be considered
2960 to resolve local to the current module, and false otherwise. Differs
2961 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2962 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2963 for the place where forced_local and dynindx == -1 are tested. If
2964 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2965 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2966 the symbol is local only for defined symbols.
2967 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2968 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2969 treatment of undefined weak symbols. For those that do not make
2970 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2971
2972bfd_boolean
268b6b39
AM
2973_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2974 struct bfd_link_info *info,
2975 bfd_boolean local_protected)
f6c52c13 2976{
fcb93ecf
PB
2977 const struct elf_backend_data *bed;
2978 struct elf_link_hash_table *hash_table;
2979
f6c52c13
AM
2980 /* If it's a local sym, of course we resolve locally. */
2981 if (h == NULL)
2982 return TRUE;
2983
d95edcac
L
2984 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2985 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2986 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2987 return TRUE;
2988
7e2294f9
AO
2989 /* Common symbols that become definitions don't get the DEF_REGULAR
2990 flag set, so test it first, and don't bail out. */
2991 if (ELF_COMMON_DEF_P (h))
2992 /* Do nothing. */;
f6c52c13 2993 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2994 resolve locally. The sym is either undefined or dynamic. */
2995 else if (!h->def_regular)
f6c52c13
AM
2996 return FALSE;
2997
2998 /* Forced local symbols resolve locally. */
f5385ebf 2999 if (h->forced_local)
f6c52c13
AM
3000 return TRUE;
3001
3002 /* As do non-dynamic symbols. */
3003 if (h->dynindx == -1)
3004 return TRUE;
3005
3006 /* At this point, we know the symbol is defined and dynamic. In an
3007 executable it must resolve locally, likewise when building symbolic
3008 shared libraries. */
0e1862bb 3009 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3010 return TRUE;
3011
3012 /* Now deal with defined dynamic symbols in shared libraries. Ones
3013 with default visibility might not resolve locally. */
3014 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3015 return FALSE;
3016
fcb93ecf
PB
3017 hash_table = elf_hash_table (info);
3018 if (!is_elf_hash_table (hash_table))
3019 return TRUE;
3020
3021 bed = get_elf_backend_data (hash_table->dynobj);
3022
f7483970
L
3023 /* If extern_protected_data is false, STV_PROTECTED non-function
3024 symbols are local. */
889c2a67
L
3025 if ((!info->extern_protected_data
3026 || (info->extern_protected_data < 0
3027 && !bed->extern_protected_data))
3028 && !bed->is_function_type (h->type))
1c16dfa5
L
3029 return TRUE;
3030
f6c52c13 3031 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3032 symbols be treated as dynamic symbols. If the address of a
3033 function not defined in an executable is set to that function's
3034 plt entry in the executable, then the address of the function in
3035 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3036 return local_protected;
3037}
e1918d23
AM
3038
3039/* Caches some TLS segment info, and ensures that the TLS segment vma is
3040 aligned. Returns the first TLS output section. */
3041
3042struct bfd_section *
3043_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3044{
3045 struct bfd_section *sec, *tls;
3046 unsigned int align = 0;
3047
3048 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3049 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3050 break;
3051 tls = sec;
3052
3053 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3054 if (sec->alignment_power > align)
3055 align = sec->alignment_power;
3056
3057 elf_hash_table (info)->tls_sec = tls;
3058
3059 /* Ensure the alignment of the first section is the largest alignment,
3060 so that the tls segment starts aligned. */
3061 if (tls != NULL)
3062 tls->alignment_power = align;
3063
3064 return tls;
3065}
0ad989f9
L
3066
3067/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3068static bfd_boolean
3069is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3070 Elf_Internal_Sym *sym)
3071{
a4d8e49b
L
3072 const struct elf_backend_data *bed;
3073
0ad989f9
L
3074 /* Local symbols do not count, but target specific ones might. */
3075 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3076 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3077 return FALSE;
3078
fcb93ecf 3079 bed = get_elf_backend_data (abfd);
0ad989f9 3080 /* Function symbols do not count. */
fcb93ecf 3081 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3082 return FALSE;
3083
3084 /* If the section is undefined, then so is the symbol. */
3085 if (sym->st_shndx == SHN_UNDEF)
3086 return FALSE;
3087
3088 /* If the symbol is defined in the common section, then
3089 it is a common definition and so does not count. */
a4d8e49b 3090 if (bed->common_definition (sym))
0ad989f9
L
3091 return FALSE;
3092
3093 /* If the symbol is in a target specific section then we
3094 must rely upon the backend to tell us what it is. */
3095 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3096 /* FIXME - this function is not coded yet:
3097
3098 return _bfd_is_global_symbol_definition (abfd, sym);
3099
3100 Instead for now assume that the definition is not global,
3101 Even if this is wrong, at least the linker will behave
3102 in the same way that it used to do. */
3103 return FALSE;
3104
3105 return TRUE;
3106}
3107
3108/* Search the symbol table of the archive element of the archive ABFD
3109 whose archive map contains a mention of SYMDEF, and determine if
3110 the symbol is defined in this element. */
3111static bfd_boolean
3112elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3113{
3114 Elf_Internal_Shdr * hdr;
ef53be89
AM
3115 size_t symcount;
3116 size_t extsymcount;
3117 size_t extsymoff;
0ad989f9
L
3118 Elf_Internal_Sym *isymbuf;
3119 Elf_Internal_Sym *isym;
3120 Elf_Internal_Sym *isymend;
3121 bfd_boolean result;
3122
3123 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3124 if (abfd == NULL)
3125 return FALSE;
3126
f0bf6bfd
L
3127 /* Return FALSE if the object has been claimed by plugin. */
3128 if (abfd->plugin_format == bfd_plugin_yes)
3129 return FALSE;
3130
0ad989f9
L
3131 if (! bfd_check_format (abfd, bfd_object))
3132 return FALSE;
3133
0ad989f9
L
3134 /* Select the appropriate symbol table. */
3135 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3136 hdr = &elf_tdata (abfd)->symtab_hdr;
3137 else
3138 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3139
3140 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3141
3142 /* The sh_info field of the symtab header tells us where the
3143 external symbols start. We don't care about the local symbols. */
3144 if (elf_bad_symtab (abfd))
3145 {
3146 extsymcount = symcount;
3147 extsymoff = 0;
3148 }
3149 else
3150 {
3151 extsymcount = symcount - hdr->sh_info;
3152 extsymoff = hdr->sh_info;
3153 }
3154
3155 if (extsymcount == 0)
3156 return FALSE;
3157
3158 /* Read in the symbol table. */
3159 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3160 NULL, NULL, NULL);
3161 if (isymbuf == NULL)
3162 return FALSE;
3163
3164 /* Scan the symbol table looking for SYMDEF. */
3165 result = FALSE;
3166 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3167 {
3168 const char *name;
3169
3170 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3171 isym->st_name);
3172 if (name == NULL)
3173 break;
3174
3175 if (strcmp (name, symdef->name) == 0)
3176 {
3177 result = is_global_data_symbol_definition (abfd, isym);
3178 break;
3179 }
3180 }
3181
3182 free (isymbuf);
3183
3184 return result;
3185}
3186\f
5a580b3a
AM
3187/* Add an entry to the .dynamic table. */
3188
3189bfd_boolean
3190_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3191 bfd_vma tag,
3192 bfd_vma val)
3193{
3194 struct elf_link_hash_table *hash_table;
3195 const struct elf_backend_data *bed;
3196 asection *s;
3197 bfd_size_type newsize;
3198 bfd_byte *newcontents;
3199 Elf_Internal_Dyn dyn;
3200
3201 hash_table = elf_hash_table (info);
3202 if (! is_elf_hash_table (hash_table))
3203 return FALSE;
3204
3205 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3206 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3207 BFD_ASSERT (s != NULL);
3208
eea6121a 3209 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3210 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3211 if (newcontents == NULL)
3212 return FALSE;
3213
3214 dyn.d_tag = tag;
3215 dyn.d_un.d_val = val;
eea6121a 3216 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3217
eea6121a 3218 s->size = newsize;
5a580b3a
AM
3219 s->contents = newcontents;
3220
3221 return TRUE;
3222}
3223
3224/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3225 otherwise just check whether one already exists. Returns -1 on error,
3226 1 if a DT_NEEDED tag already exists, and 0 on success. */
3227
4ad4eba5 3228static int
7e9f0867
AM
3229elf_add_dt_needed_tag (bfd *abfd,
3230 struct bfd_link_info *info,
4ad4eba5
AM
3231 const char *soname,
3232 bfd_boolean do_it)
5a580b3a
AM
3233{
3234 struct elf_link_hash_table *hash_table;
ef53be89 3235 size_t strindex;
5a580b3a 3236
7e9f0867
AM
3237 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3238 return -1;
3239
5a580b3a 3240 hash_table = elf_hash_table (info);
5a580b3a 3241 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3242 if (strindex == (size_t) -1)
5a580b3a
AM
3243 return -1;
3244
02be4619 3245 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3246 {
3247 asection *sdyn;
3248 const struct elf_backend_data *bed;
3249 bfd_byte *extdyn;
3250
3251 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3252 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3253 if (sdyn != NULL)
3254 for (extdyn = sdyn->contents;
3255 extdyn < sdyn->contents + sdyn->size;
3256 extdyn += bed->s->sizeof_dyn)
3257 {
3258 Elf_Internal_Dyn dyn;
5a580b3a 3259
7e9f0867
AM
3260 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3261 if (dyn.d_tag == DT_NEEDED
3262 && dyn.d_un.d_val == strindex)
3263 {
3264 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3265 return 1;
3266 }
3267 }
5a580b3a
AM
3268 }
3269
3270 if (do_it)
3271 {
7e9f0867
AM
3272 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3273 return -1;
3274
5a580b3a
AM
3275 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3276 return -1;
3277 }
3278 else
3279 /* We were just checking for existence of the tag. */
3280 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3281
3282 return 0;
3283}
3284
7b15fa7a
AM
3285/* Return true if SONAME is on the needed list between NEEDED and STOP
3286 (or the end of list if STOP is NULL), and needed by a library that
3287 will be loaded. */
3288
010e5ae2 3289static bfd_boolean
7b15fa7a
AM
3290on_needed_list (const char *soname,
3291 struct bfd_link_needed_list *needed,
3292 struct bfd_link_needed_list *stop)
010e5ae2 3293{
7b15fa7a
AM
3294 struct bfd_link_needed_list *look;
3295 for (look = needed; look != stop; look = look->next)
3296 if (strcmp (soname, look->name) == 0
3297 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3298 /* If needed by a library that itself is not directly
3299 needed, recursively check whether that library is
3300 indirectly needed. Since we add DT_NEEDED entries to
3301 the end of the list, library dependencies appear after
3302 the library. Therefore search prior to the current
3303 LOOK, preventing possible infinite recursion. */
3304 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3305 return TRUE;
3306
3307 return FALSE;
3308}
3309
14160578 3310/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3311static int
3312elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3313{
3314 const struct elf_link_hash_entry *h1;
3315 const struct elf_link_hash_entry *h2;
10b7e05b 3316 bfd_signed_vma vdiff;
5a580b3a
AM
3317
3318 h1 = *(const struct elf_link_hash_entry **) arg1;
3319 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3320 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3321 if (vdiff != 0)
3322 return vdiff > 0 ? 1 : -1;
3323 else
3324 {
d3435ae8 3325 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3326 if (sdiff != 0)
3327 return sdiff > 0 ? 1 : -1;
3328 }
14160578
AM
3329 vdiff = h1->size - h2->size;
3330 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3331}
4ad4eba5 3332
5a580b3a
AM
3333/* This function is used to adjust offsets into .dynstr for
3334 dynamic symbols. This is called via elf_link_hash_traverse. */
3335
3336static bfd_boolean
3337elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3338{
a50b1753 3339 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3340
5a580b3a
AM
3341 if (h->dynindx != -1)
3342 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3343 return TRUE;
3344}
3345
3346/* Assign string offsets in .dynstr, update all structures referencing
3347 them. */
3348
4ad4eba5
AM
3349static bfd_boolean
3350elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3351{
3352 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3353 struct elf_link_local_dynamic_entry *entry;
3354 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3355 bfd *dynobj = hash_table->dynobj;
3356 asection *sdyn;
3357 bfd_size_type size;
3358 const struct elf_backend_data *bed;
3359 bfd_byte *extdyn;
3360
3361 _bfd_elf_strtab_finalize (dynstr);
3362 size = _bfd_elf_strtab_size (dynstr);
3363
3364 bed = get_elf_backend_data (dynobj);
3d4d4302 3365 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3366 BFD_ASSERT (sdyn != NULL);
3367
3368 /* Update all .dynamic entries referencing .dynstr strings. */
3369 for (extdyn = sdyn->contents;
eea6121a 3370 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3371 extdyn += bed->s->sizeof_dyn)
3372 {
3373 Elf_Internal_Dyn dyn;
3374
3375 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3376 switch (dyn.d_tag)
3377 {
3378 case DT_STRSZ:
3379 dyn.d_un.d_val = size;
3380 break;
3381 case DT_NEEDED:
3382 case DT_SONAME:
3383 case DT_RPATH:
3384 case DT_RUNPATH:
3385 case DT_FILTER:
3386 case DT_AUXILIARY:
7ee314fa
AM
3387 case DT_AUDIT:
3388 case DT_DEPAUDIT:
5a580b3a
AM
3389 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3390 break;
3391 default:
3392 continue;
3393 }
3394 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3395 }
3396
3397 /* Now update local dynamic symbols. */
3398 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3399 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3400 entry->isym.st_name);
3401
3402 /* And the rest of dynamic symbols. */
3403 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3404
3405 /* Adjust version definitions. */
3406 if (elf_tdata (output_bfd)->cverdefs)
3407 {
3408 asection *s;
3409 bfd_byte *p;
ef53be89 3410 size_t i;
5a580b3a
AM
3411 Elf_Internal_Verdef def;
3412 Elf_Internal_Verdaux defaux;
3413
3d4d4302 3414 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3415 p = s->contents;
3416 do
3417 {
3418 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3419 &def);
3420 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3421 if (def.vd_aux != sizeof (Elf_External_Verdef))
3422 continue;
5a580b3a
AM
3423 for (i = 0; i < def.vd_cnt; ++i)
3424 {
3425 _bfd_elf_swap_verdaux_in (output_bfd,
3426 (Elf_External_Verdaux *) p, &defaux);
3427 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3428 defaux.vda_name);
3429 _bfd_elf_swap_verdaux_out (output_bfd,
3430 &defaux, (Elf_External_Verdaux *) p);
3431 p += sizeof (Elf_External_Verdaux);
3432 }
3433 }
3434 while (def.vd_next);
3435 }
3436
3437 /* Adjust version references. */
3438 if (elf_tdata (output_bfd)->verref)
3439 {
3440 asection *s;
3441 bfd_byte *p;
ef53be89 3442 size_t i;
5a580b3a
AM
3443 Elf_Internal_Verneed need;
3444 Elf_Internal_Vernaux needaux;
3445
3d4d4302 3446 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3447 p = s->contents;
3448 do
3449 {
3450 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3451 &need);
3452 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3453 _bfd_elf_swap_verneed_out (output_bfd, &need,
3454 (Elf_External_Verneed *) p);
3455 p += sizeof (Elf_External_Verneed);
3456 for (i = 0; i < need.vn_cnt; ++i)
3457 {
3458 _bfd_elf_swap_vernaux_in (output_bfd,
3459 (Elf_External_Vernaux *) p, &needaux);
3460 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3461 needaux.vna_name);
3462 _bfd_elf_swap_vernaux_out (output_bfd,
3463 &needaux,
3464 (Elf_External_Vernaux *) p);
3465 p += sizeof (Elf_External_Vernaux);
3466 }
3467 }
3468 while (need.vn_next);
3469 }
3470
3471 return TRUE;
3472}
3473\f
13285a1b
AM
3474/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3475 The default is to only match when the INPUT and OUTPUT are exactly
3476 the same target. */
3477
3478bfd_boolean
3479_bfd_elf_default_relocs_compatible (const bfd_target *input,
3480 const bfd_target *output)
3481{
3482 return input == output;
3483}
3484
3485/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3486 This version is used when different targets for the same architecture
3487 are virtually identical. */
3488
3489bfd_boolean
3490_bfd_elf_relocs_compatible (const bfd_target *input,
3491 const bfd_target *output)
3492{
3493 const struct elf_backend_data *obed, *ibed;
3494
3495 if (input == output)
3496 return TRUE;
3497
3498 ibed = xvec_get_elf_backend_data (input);
3499 obed = xvec_get_elf_backend_data (output);
3500
3501 if (ibed->arch != obed->arch)
3502 return FALSE;
3503
3504 /* If both backends are using this function, deem them compatible. */
3505 return ibed->relocs_compatible == obed->relocs_compatible;
3506}
3507
e5034e59
AM
3508/* Make a special call to the linker "notice" function to tell it that
3509 we are about to handle an as-needed lib, or have finished
1b786873 3510 processing the lib. */
e5034e59
AM
3511
3512bfd_boolean
3513_bfd_elf_notice_as_needed (bfd *ibfd,
3514 struct bfd_link_info *info,
3515 enum notice_asneeded_action act)
3516{
46135103 3517 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3518}
3519
d9689752
L
3520/* Check relocations an ELF object file. */
3521
3522bfd_boolean
3523_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3524{
3525 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3526 struct elf_link_hash_table *htab = elf_hash_table (info);
3527
3528 /* If this object is the same format as the output object, and it is
3529 not a shared library, then let the backend look through the
3530 relocs.
3531
3532 This is required to build global offset table entries and to
3533 arrange for dynamic relocs. It is not required for the
3534 particular common case of linking non PIC code, even when linking
3535 against shared libraries, but unfortunately there is no way of
3536 knowing whether an object file has been compiled PIC or not.
3537 Looking through the relocs is not particularly time consuming.
3538 The problem is that we must either (1) keep the relocs in memory,
3539 which causes the linker to require additional runtime memory or
3540 (2) read the relocs twice from the input file, which wastes time.
3541 This would be a good case for using mmap.
3542
3543 I have no idea how to handle linking PIC code into a file of a
3544 different format. It probably can't be done. */
3545 if ((abfd->flags & DYNAMIC) == 0
3546 && is_elf_hash_table (htab)
3547 && bed->check_relocs != NULL
3548 && elf_object_id (abfd) == elf_hash_table_id (htab)
3549 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3550 {
3551 asection *o;
3552
3553 for (o = abfd->sections; o != NULL; o = o->next)
3554 {
3555 Elf_Internal_Rela *internal_relocs;
3556 bfd_boolean ok;
3557
5ce03cea 3558 /* Don't check relocations in excluded sections. */
d9689752 3559 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3560 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3561 || o->reloc_count == 0
3562 || ((info->strip == strip_all || info->strip == strip_debugger)
3563 && (o->flags & SEC_DEBUGGING) != 0)
3564 || bfd_is_abs_section (o->output_section))
3565 continue;
3566
3567 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3568 info->keep_memory);
3569 if (internal_relocs == NULL)
3570 return FALSE;
3571
3572 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3573
3574 if (elf_section_data (o)->relocs != internal_relocs)
3575 free (internal_relocs);
3576
3577 if (! ok)
3578 return FALSE;
3579 }
3580 }
3581
3582 return TRUE;
3583}
3584
4ad4eba5
AM
3585/* Add symbols from an ELF object file to the linker hash table. */
3586
3587static bfd_boolean
3588elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3589{
a0c402a5 3590 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3591 Elf_Internal_Shdr *hdr;
ef53be89
AM
3592 size_t symcount;
3593 size_t extsymcount;
3594 size_t extsymoff;
4ad4eba5
AM
3595 struct elf_link_hash_entry **sym_hash;
3596 bfd_boolean dynamic;
3597 Elf_External_Versym *extversym = NULL;
3598 Elf_External_Versym *ever;
3599 struct elf_link_hash_entry *weaks;
3600 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3601 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3602 Elf_Internal_Sym *isymbuf = NULL;
3603 Elf_Internal_Sym *isym;
3604 Elf_Internal_Sym *isymend;
3605 const struct elf_backend_data *bed;
3606 bfd_boolean add_needed;
66eb6687 3607 struct elf_link_hash_table *htab;
4ad4eba5 3608 bfd_size_type amt;
66eb6687 3609 void *alloc_mark = NULL;
4f87808c
AM
3610 struct bfd_hash_entry **old_table = NULL;
3611 unsigned int old_size = 0;
3612 unsigned int old_count = 0;
66eb6687 3613 void *old_tab = NULL;
66eb6687
AM
3614 void *old_ent;
3615 struct bfd_link_hash_entry *old_undefs = NULL;
3616 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3617 void *old_strtab = NULL;
66eb6687 3618 size_t tabsize = 0;
db6a5d5f 3619 asection *s;
29a9f53e 3620 bfd_boolean just_syms;
4ad4eba5 3621
66eb6687 3622 htab = elf_hash_table (info);
4ad4eba5 3623 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3624
3625 if ((abfd->flags & DYNAMIC) == 0)
3626 dynamic = FALSE;
3627 else
3628 {
3629 dynamic = TRUE;
3630
3631 /* You can't use -r against a dynamic object. Also, there's no
3632 hope of using a dynamic object which does not exactly match
3633 the format of the output file. */
0e1862bb 3634 if (bfd_link_relocatable (info)
66eb6687 3635 || !is_elf_hash_table (htab)
f13a99db 3636 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3637 {
0e1862bb 3638 if (bfd_link_relocatable (info))
9a0789ec
NC
3639 bfd_set_error (bfd_error_invalid_operation);
3640 else
3641 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3642 goto error_return;
3643 }
3644 }
3645
a0c402a5
L
3646 ehdr = elf_elfheader (abfd);
3647 if (info->warn_alternate_em
3648 && bed->elf_machine_code != ehdr->e_machine
3649 && ((bed->elf_machine_alt1 != 0
3650 && ehdr->e_machine == bed->elf_machine_alt1)
3651 || (bed->elf_machine_alt2 != 0
3652 && ehdr->e_machine == bed->elf_machine_alt2)))
3653 info->callbacks->einfo
3654 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3655 ehdr->e_machine, abfd, bed->elf_machine_code);
3656
4ad4eba5
AM
3657 /* As a GNU extension, any input sections which are named
3658 .gnu.warning.SYMBOL are treated as warning symbols for the given
3659 symbol. This differs from .gnu.warning sections, which generate
3660 warnings when they are included in an output file. */
dd98f8d2 3661 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3662 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3663 {
db6a5d5f 3664 const char *name;
4ad4eba5 3665
db6a5d5f
AM
3666 name = bfd_get_section_name (abfd, s);
3667 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3668 {
db6a5d5f
AM
3669 char *msg;
3670 bfd_size_type sz;
3671
3672 name += sizeof ".gnu.warning." - 1;
3673
3674 /* If this is a shared object, then look up the symbol
3675 in the hash table. If it is there, and it is already
3676 been defined, then we will not be using the entry
3677 from this shared object, so we don't need to warn.
3678 FIXME: If we see the definition in a regular object
3679 later on, we will warn, but we shouldn't. The only
3680 fix is to keep track of what warnings we are supposed
3681 to emit, and then handle them all at the end of the
3682 link. */
3683 if (dynamic)
4ad4eba5 3684 {
db6a5d5f
AM
3685 struct elf_link_hash_entry *h;
3686
3687 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3688
3689 /* FIXME: What about bfd_link_hash_common? */
3690 if (h != NULL
3691 && (h->root.type == bfd_link_hash_defined
3692 || h->root.type == bfd_link_hash_defweak))
3693 continue;
3694 }
4ad4eba5 3695
db6a5d5f
AM
3696 sz = s->size;
3697 msg = (char *) bfd_alloc (abfd, sz + 1);
3698 if (msg == NULL)
3699 goto error_return;
4ad4eba5 3700
db6a5d5f
AM
3701 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3702 goto error_return;
4ad4eba5 3703
db6a5d5f 3704 msg[sz] = '\0';
4ad4eba5 3705
db6a5d5f
AM
3706 if (! (_bfd_generic_link_add_one_symbol
3707 (info, abfd, name, BSF_WARNING, s, 0, msg,
3708 FALSE, bed->collect, NULL)))
3709 goto error_return;
4ad4eba5 3710
0e1862bb 3711 if (bfd_link_executable (info))
db6a5d5f
AM
3712 {
3713 /* Clobber the section size so that the warning does
3714 not get copied into the output file. */
3715 s->size = 0;
11d2f718 3716
db6a5d5f
AM
3717 /* Also set SEC_EXCLUDE, so that symbols defined in
3718 the warning section don't get copied to the output. */
3719 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3720 }
3721 }
3722 }
3723
29a9f53e
L
3724 just_syms = ((s = abfd->sections) != NULL
3725 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3726
4ad4eba5
AM
3727 add_needed = TRUE;
3728 if (! dynamic)
3729 {
3730 /* If we are creating a shared library, create all the dynamic
3731 sections immediately. We need to attach them to something,
3732 so we attach them to this BFD, provided it is the right
bf89386a
L
3733 format and is not from ld --just-symbols. Always create the
3734 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3735 are no input BFD's of the same format as the output, we can't
3736 make a shared library. */
3737 if (!just_syms
bf89386a 3738 && (bfd_link_pic (info)
9c1d7a08
L
3739 || (!bfd_link_relocatable (info)
3740 && (info->export_dynamic || info->dynamic)))
66eb6687 3741 && is_elf_hash_table (htab)
f13a99db 3742 && info->output_bfd->xvec == abfd->xvec
66eb6687 3743 && !htab->dynamic_sections_created)
4ad4eba5
AM
3744 {
3745 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3746 goto error_return;
3747 }
3748 }
66eb6687 3749 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3750 goto error_return;
3751 else
3752 {
4ad4eba5 3753 const char *soname = NULL;
7ee314fa 3754 char *audit = NULL;
4ad4eba5
AM
3755 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3756 int ret;
3757
3758 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3759 ld shouldn't allow it. */
29a9f53e 3760 if (just_syms)
92fd189d 3761 abort ();
4ad4eba5
AM
3762
3763 /* If this dynamic lib was specified on the command line with
3764 --as-needed in effect, then we don't want to add a DT_NEEDED
3765 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3766 in by another lib's DT_NEEDED. When --no-add-needed is used
3767 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3768 any dynamic library in DT_NEEDED tags in the dynamic lib at
3769 all. */
3770 add_needed = (elf_dyn_lib_class (abfd)
3771 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3772 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3773
3774 s = bfd_get_section_by_name (abfd, ".dynamic");
3775 if (s != NULL)
3776 {
3777 bfd_byte *dynbuf;
3778 bfd_byte *extdyn;
cb33740c 3779 unsigned int elfsec;
4ad4eba5
AM
3780 unsigned long shlink;
3781
eea6121a 3782 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3783 {
3784error_free_dyn:
3785 free (dynbuf);
3786 goto error_return;
3787 }
4ad4eba5
AM
3788
3789 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3790 if (elfsec == SHN_BAD)
4ad4eba5
AM
3791 goto error_free_dyn;
3792 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3793
3794 for (extdyn = dynbuf;
eea6121a 3795 extdyn < dynbuf + s->size;
4ad4eba5
AM
3796 extdyn += bed->s->sizeof_dyn)
3797 {
3798 Elf_Internal_Dyn dyn;
3799
3800 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3801 if (dyn.d_tag == DT_SONAME)
3802 {
3803 unsigned int tagv = dyn.d_un.d_val;
3804 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3805 if (soname == NULL)
3806 goto error_free_dyn;
3807 }
3808 if (dyn.d_tag == DT_NEEDED)
3809 {
3810 struct bfd_link_needed_list *n, **pn;
3811 char *fnm, *anm;
3812 unsigned int tagv = dyn.d_un.d_val;
3813
3814 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3815 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3816 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3817 if (n == NULL || fnm == NULL)
3818 goto error_free_dyn;
3819 amt = strlen (fnm) + 1;
a50b1753 3820 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3821 if (anm == NULL)
3822 goto error_free_dyn;
3823 memcpy (anm, fnm, amt);
3824 n->name = anm;
3825 n->by = abfd;
3826 n->next = NULL;
66eb6687 3827 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3828 ;
3829 *pn = n;
3830 }
3831 if (dyn.d_tag == DT_RUNPATH)
3832 {
3833 struct bfd_link_needed_list *n, **pn;
3834 char *fnm, *anm;
3835 unsigned int tagv = dyn.d_un.d_val;
3836
3837 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3838 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3839 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3840 if (n == NULL || fnm == NULL)
3841 goto error_free_dyn;
3842 amt = strlen (fnm) + 1;
a50b1753 3843 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3844 if (anm == NULL)
3845 goto error_free_dyn;
3846 memcpy (anm, fnm, amt);
3847 n->name = anm;
3848 n->by = abfd;
3849 n->next = NULL;
3850 for (pn = & runpath;
3851 *pn != NULL;
3852 pn = &(*pn)->next)
3853 ;
3854 *pn = n;
3855 }
3856 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3857 if (!runpath && dyn.d_tag == DT_RPATH)
3858 {
3859 struct bfd_link_needed_list *n, **pn;
3860 char *fnm, *anm;
3861 unsigned int tagv = dyn.d_un.d_val;
3862
3863 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3864 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3865 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3866 if (n == NULL || fnm == NULL)
3867 goto error_free_dyn;
3868 amt = strlen (fnm) + 1;
a50b1753 3869 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3870 if (anm == NULL)
f8703194 3871 goto error_free_dyn;
4ad4eba5
AM
3872 memcpy (anm, fnm, amt);
3873 n->name = anm;
3874 n->by = abfd;
3875 n->next = NULL;
3876 for (pn = & rpath;
3877 *pn != NULL;
3878 pn = &(*pn)->next)
3879 ;
3880 *pn = n;
3881 }
7ee314fa
AM
3882 if (dyn.d_tag == DT_AUDIT)
3883 {
3884 unsigned int tagv = dyn.d_un.d_val;
3885 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3886 }
4ad4eba5
AM
3887 }
3888
3889 free (dynbuf);
3890 }
3891
3892 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3893 frees all more recently bfd_alloc'd blocks as well. */
3894 if (runpath)
3895 rpath = runpath;
3896
3897 if (rpath)
3898 {
3899 struct bfd_link_needed_list **pn;
66eb6687 3900 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3901 ;
3902 *pn = rpath;
3903 }
3904
3905 /* We do not want to include any of the sections in a dynamic
3906 object in the output file. We hack by simply clobbering the
3907 list of sections in the BFD. This could be handled more
3908 cleanly by, say, a new section flag; the existing
3909 SEC_NEVER_LOAD flag is not the one we want, because that one
3910 still implies that the section takes up space in the output
3911 file. */
3912 bfd_section_list_clear (abfd);
3913
4ad4eba5
AM
3914 /* Find the name to use in a DT_NEEDED entry that refers to this
3915 object. If the object has a DT_SONAME entry, we use it.
3916 Otherwise, if the generic linker stuck something in
3917 elf_dt_name, we use that. Otherwise, we just use the file
3918 name. */
3919 if (soname == NULL || *soname == '\0')
3920 {
3921 soname = elf_dt_name (abfd);
3922 if (soname == NULL || *soname == '\0')
3923 soname = bfd_get_filename (abfd);
3924 }
3925
3926 /* Save the SONAME because sometimes the linker emulation code
3927 will need to know it. */
3928 elf_dt_name (abfd) = soname;
3929
7e9f0867 3930 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3931 if (ret < 0)
3932 goto error_return;
3933
3934 /* If we have already included this dynamic object in the
3935 link, just ignore it. There is no reason to include a
3936 particular dynamic object more than once. */
3937 if (ret > 0)
3938 return TRUE;
7ee314fa
AM
3939
3940 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 3941 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3942 }
3943
3944 /* If this is a dynamic object, we always link against the .dynsym
3945 symbol table, not the .symtab symbol table. The dynamic linker
3946 will only see the .dynsym symbol table, so there is no reason to
3947 look at .symtab for a dynamic object. */
3948
3949 if (! dynamic || elf_dynsymtab (abfd) == 0)
3950 hdr = &elf_tdata (abfd)->symtab_hdr;
3951 else
3952 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3953
3954 symcount = hdr->sh_size / bed->s->sizeof_sym;
3955
3956 /* The sh_info field of the symtab header tells us where the
3957 external symbols start. We don't care about the local symbols at
3958 this point. */
3959 if (elf_bad_symtab (abfd))
3960 {
3961 extsymcount = symcount;
3962 extsymoff = 0;
3963 }
3964 else
3965 {
3966 extsymcount = symcount - hdr->sh_info;
3967 extsymoff = hdr->sh_info;
3968 }
3969
f45794cb 3970 sym_hash = elf_sym_hashes (abfd);
012b2306 3971 if (extsymcount != 0)
4ad4eba5
AM
3972 {
3973 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3974 NULL, NULL, NULL);
3975 if (isymbuf == NULL)
3976 goto error_return;
3977
4ad4eba5 3978 if (sym_hash == NULL)
012b2306
AM
3979 {
3980 /* We store a pointer to the hash table entry for each
3981 external symbol. */
ef53be89
AM
3982 amt = extsymcount;
3983 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
3984 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3985 if (sym_hash == NULL)
3986 goto error_free_sym;
3987 elf_sym_hashes (abfd) = sym_hash;
3988 }
4ad4eba5
AM
3989 }
3990
3991 if (dynamic)
3992 {
3993 /* Read in any version definitions. */
fc0e6df6
PB
3994 if (!_bfd_elf_slurp_version_tables (abfd,
3995 info->default_imported_symver))
4ad4eba5
AM
3996 goto error_free_sym;
3997
3998 /* Read in the symbol versions, but don't bother to convert them
3999 to internal format. */
4000 if (elf_dynversym (abfd) != 0)
4001 {
4002 Elf_Internal_Shdr *versymhdr;
4003
4004 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4005 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4006 if (extversym == NULL)
4007 goto error_free_sym;
4008 amt = versymhdr->sh_size;
4009 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4010 || bfd_bread (extversym, amt, abfd) != amt)
4011 goto error_free_vers;
4012 }
4013 }
4014
66eb6687
AM
4015 /* If we are loading an as-needed shared lib, save the symbol table
4016 state before we start adding symbols. If the lib turns out
4017 to be unneeded, restore the state. */
4018 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4019 {
4020 unsigned int i;
4021 size_t entsize;
4022
4023 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4024 {
4025 struct bfd_hash_entry *p;
2de92251 4026 struct elf_link_hash_entry *h;
66eb6687
AM
4027
4028 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4029 {
4030 h = (struct elf_link_hash_entry *) p;
4031 entsize += htab->root.table.entsize;
4032 if (h->root.type == bfd_link_hash_warning)
4033 entsize += htab->root.table.entsize;
4034 }
66eb6687
AM
4035 }
4036
4037 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4038 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4039 if (old_tab == NULL)
4040 goto error_free_vers;
4041
4042 /* Remember the current objalloc pointer, so that all mem for
4043 symbols added can later be reclaimed. */
4044 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4045 if (alloc_mark == NULL)
4046 goto error_free_vers;
4047
5061a885
AM
4048 /* Make a special call to the linker "notice" function to
4049 tell it that we are about to handle an as-needed lib. */
e5034e59 4050 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4051 goto error_free_vers;
5061a885 4052
f45794cb
AM
4053 /* Clone the symbol table. Remember some pointers into the
4054 symbol table, and dynamic symbol count. */
4055 old_ent = (char *) old_tab + tabsize;
66eb6687 4056 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4057 old_undefs = htab->root.undefs;
4058 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4059 old_table = htab->root.table.table;
4060 old_size = htab->root.table.size;
4061 old_count = htab->root.table.count;
5b677558
AM
4062 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4063 if (old_strtab == NULL)
4064 goto error_free_vers;
66eb6687
AM
4065
4066 for (i = 0; i < htab->root.table.size; i++)
4067 {
4068 struct bfd_hash_entry *p;
2de92251 4069 struct elf_link_hash_entry *h;
66eb6687
AM
4070
4071 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4072 {
4073 memcpy (old_ent, p, htab->root.table.entsize);
4074 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4075 h = (struct elf_link_hash_entry *) p;
4076 if (h->root.type == bfd_link_hash_warning)
4077 {
4078 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4079 old_ent = (char *) old_ent + htab->root.table.entsize;
4080 }
66eb6687
AM
4081 }
4082 }
4083 }
4ad4eba5 4084
66eb6687 4085 weaks = NULL;
4ad4eba5
AM
4086 ever = extversym != NULL ? extversym + extsymoff : NULL;
4087 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4088 isym < isymend;
4089 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4090 {
4091 int bind;
4092 bfd_vma value;
af44c138 4093 asection *sec, *new_sec;
4ad4eba5
AM
4094 flagword flags;
4095 const char *name;
4096 struct elf_link_hash_entry *h;
90c984fc 4097 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4098 bfd_boolean definition;
4099 bfd_boolean size_change_ok;
4100 bfd_boolean type_change_ok;
4101 bfd_boolean new_weakdef;
37a9e49a
L
4102 bfd_boolean new_weak;
4103 bfd_boolean old_weak;
4ad4eba5 4104 bfd_boolean override;
a4d8e49b 4105 bfd_boolean common;
97196564 4106 bfd_boolean discarded;
4ad4eba5
AM
4107 unsigned int old_alignment;
4108 bfd *old_bfd;
6e33951e 4109 bfd_boolean matched;
4ad4eba5
AM
4110
4111 override = FALSE;
4112
4113 flags = BSF_NO_FLAGS;
4114 sec = NULL;
4115 value = isym->st_value;
a4d8e49b 4116 common = bed->common_definition (isym);
97196564 4117 discarded = FALSE;
4ad4eba5
AM
4118
4119 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4120 switch (bind)
4ad4eba5 4121 {
3e7a7d11 4122 case STB_LOCAL:
4ad4eba5
AM
4123 /* This should be impossible, since ELF requires that all
4124 global symbols follow all local symbols, and that sh_info
4125 point to the first global symbol. Unfortunately, Irix 5
4126 screws this up. */
4127 continue;
3e7a7d11
NC
4128
4129 case STB_GLOBAL:
a4d8e49b 4130 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4131 flags = BSF_GLOBAL;
3e7a7d11
NC
4132 break;
4133
4134 case STB_WEAK:
4135 flags = BSF_WEAK;
4136 break;
4137
4138 case STB_GNU_UNIQUE:
4139 flags = BSF_GNU_UNIQUE;
4140 break;
4141
4142 default:
4ad4eba5 4143 /* Leave it up to the processor backend. */
3e7a7d11 4144 break;
4ad4eba5
AM
4145 }
4146
4147 if (isym->st_shndx == SHN_UNDEF)
4148 sec = bfd_und_section_ptr;
cb33740c
AM
4149 else if (isym->st_shndx == SHN_ABS)
4150 sec = bfd_abs_section_ptr;
4151 else if (isym->st_shndx == SHN_COMMON)
4152 {
4153 sec = bfd_com_section_ptr;
4154 /* What ELF calls the size we call the value. What ELF
4155 calls the value we call the alignment. */
4156 value = isym->st_size;
4157 }
4158 else
4ad4eba5
AM
4159 {
4160 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4161 if (sec == NULL)
4162 sec = bfd_abs_section_ptr;
dbaa2011 4163 else if (discarded_section (sec))
529fcb95 4164 {
e5d08002
L
4165 /* Symbols from discarded section are undefined. We keep
4166 its visibility. */
529fcb95 4167 sec = bfd_und_section_ptr;
97196564 4168 discarded = TRUE;
529fcb95
PB
4169 isym->st_shndx = SHN_UNDEF;
4170 }
4ad4eba5
AM
4171 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4172 value -= sec->vma;
4173 }
4ad4eba5
AM
4174
4175 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4176 isym->st_name);
4177 if (name == NULL)
4178 goto error_free_vers;
4179
4180 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4181 && (abfd->flags & BFD_PLUGIN) != 0)
4182 {
4183 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4184
4185 if (xc == NULL)
4186 {
4187 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4188 | SEC_EXCLUDE);
4189 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4190 if (xc == NULL)
4191 goto error_free_vers;
4192 }
4193 sec = xc;
4194 }
4195 else if (isym->st_shndx == SHN_COMMON
4196 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4197 && !bfd_link_relocatable (info))
4ad4eba5
AM
4198 {
4199 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4200
4201 if (tcomm == NULL)
4202 {
02d00247
AM
4203 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4204 | SEC_LINKER_CREATED);
4205 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4206 if (tcomm == NULL)
4ad4eba5
AM
4207 goto error_free_vers;
4208 }
4209 sec = tcomm;
4210 }
66eb6687 4211 else if (bed->elf_add_symbol_hook)
4ad4eba5 4212 {
66eb6687
AM
4213 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4214 &sec, &value))
4ad4eba5
AM
4215 goto error_free_vers;
4216
4217 /* The hook function sets the name to NULL if this symbol
4218 should be skipped for some reason. */
4219 if (name == NULL)
4220 continue;
4221 }
4222
4223 /* Sanity check that all possibilities were handled. */
4224 if (sec == NULL)
4225 {
4226 bfd_set_error (bfd_error_bad_value);
4227 goto error_free_vers;
4228 }
4229
191c0c42
AM
4230 /* Silently discard TLS symbols from --just-syms. There's
4231 no way to combine a static TLS block with a new TLS block
4232 for this executable. */
4233 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4234 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4235 continue;
4236
4ad4eba5
AM
4237 if (bfd_is_und_section (sec)
4238 || bfd_is_com_section (sec))
4239 definition = FALSE;
4240 else
4241 definition = TRUE;
4242
4243 size_change_ok = FALSE;
66eb6687 4244 type_change_ok = bed->type_change_ok;
37a9e49a 4245 old_weak = FALSE;
6e33951e 4246 matched = FALSE;
4ad4eba5
AM
4247 old_alignment = 0;
4248 old_bfd = NULL;
af44c138 4249 new_sec = sec;
4ad4eba5 4250
66eb6687 4251 if (is_elf_hash_table (htab))
4ad4eba5
AM
4252 {
4253 Elf_Internal_Versym iver;
4254 unsigned int vernum = 0;
4255 bfd_boolean skip;
4256
fc0e6df6 4257 if (ever == NULL)
4ad4eba5 4258 {
fc0e6df6
PB
4259 if (info->default_imported_symver)
4260 /* Use the default symbol version created earlier. */
4261 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4262 else
4263 iver.vs_vers = 0;
4264 }
4265 else
4266 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4267
4268 vernum = iver.vs_vers & VERSYM_VERSION;
4269
4270 /* If this is a hidden symbol, or if it is not version
4271 1, we append the version name to the symbol name.
cc86ff91
EB
4272 However, we do not modify a non-hidden absolute symbol
4273 if it is not a function, because it might be the version
4274 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4275 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4276 || (vernum > 1
4277 && (!bfd_is_abs_section (sec)
4278 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4279 {
4280 const char *verstr;
4281 size_t namelen, verlen, newlen;
4282 char *newname, *p;
4283
4284 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4285 {
fc0e6df6
PB
4286 if (vernum > elf_tdata (abfd)->cverdefs)
4287 verstr = NULL;
4288 else if (vernum > 1)
4289 verstr =
4290 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4291 else
4292 verstr = "";
4ad4eba5 4293
fc0e6df6 4294 if (verstr == NULL)
4ad4eba5 4295 {
fc0e6df6
PB
4296 (*_bfd_error_handler)
4297 (_("%B: %s: invalid version %u (max %d)"),
4298 abfd, name, vernum,
4299 elf_tdata (abfd)->cverdefs);
4300 bfd_set_error (bfd_error_bad_value);
4301 goto error_free_vers;
4ad4eba5 4302 }
fc0e6df6
PB
4303 }
4304 else
4305 {
4306 /* We cannot simply test for the number of
4307 entries in the VERNEED section since the
4308 numbers for the needed versions do not start
4309 at 0. */
4310 Elf_Internal_Verneed *t;
4311
4312 verstr = NULL;
4313 for (t = elf_tdata (abfd)->verref;
4314 t != NULL;
4315 t = t->vn_nextref)
4ad4eba5 4316 {
fc0e6df6 4317 Elf_Internal_Vernaux *a;
4ad4eba5 4318
fc0e6df6
PB
4319 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4320 {
4321 if (a->vna_other == vernum)
4ad4eba5 4322 {
fc0e6df6
PB
4323 verstr = a->vna_nodename;
4324 break;
4ad4eba5 4325 }
4ad4eba5 4326 }
fc0e6df6
PB
4327 if (a != NULL)
4328 break;
4329 }
4330 if (verstr == NULL)
4331 {
4332 (*_bfd_error_handler)
4333 (_("%B: %s: invalid needed version %d"),
4334 abfd, name, vernum);
4335 bfd_set_error (bfd_error_bad_value);
4336 goto error_free_vers;
4ad4eba5 4337 }
4ad4eba5 4338 }
fc0e6df6
PB
4339
4340 namelen = strlen (name);
4341 verlen = strlen (verstr);
4342 newlen = namelen + verlen + 2;
4343 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4344 && isym->st_shndx != SHN_UNDEF)
4345 ++newlen;
4346
a50b1753 4347 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4348 if (newname == NULL)
4349 goto error_free_vers;
4350 memcpy (newname, name, namelen);
4351 p = newname + namelen;
4352 *p++ = ELF_VER_CHR;
4353 /* If this is a defined non-hidden version symbol,
4354 we add another @ to the name. This indicates the
4355 default version of the symbol. */
4356 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4357 && isym->st_shndx != SHN_UNDEF)
4358 *p++ = ELF_VER_CHR;
4359 memcpy (p, verstr, verlen + 1);
4360
4361 name = newname;
4ad4eba5
AM
4362 }
4363
cd3416da
AM
4364 /* If this symbol has default visibility and the user has
4365 requested we not re-export it, then mark it as hidden. */
a0d49154 4366 if (!bfd_is_und_section (sec)
cd3416da 4367 && !dynamic
ce875075 4368 && abfd->no_export
cd3416da
AM
4369 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4370 isym->st_other = (STV_HIDDEN
4371 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4372
4f3fedcf
AM
4373 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4374 sym_hash, &old_bfd, &old_weak,
4375 &old_alignment, &skip, &override,
6e33951e
L
4376 &type_change_ok, &size_change_ok,
4377 &matched))
4ad4eba5
AM
4378 goto error_free_vers;
4379
4380 if (skip)
4381 continue;
4382
6e33951e
L
4383 /* Override a definition only if the new symbol matches the
4384 existing one. */
4385 if (override && matched)
4ad4eba5
AM
4386 definition = FALSE;
4387
4388 h = *sym_hash;
4389 while (h->root.type == bfd_link_hash_indirect
4390 || h->root.type == bfd_link_hash_warning)
4391 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4392
4ad4eba5 4393 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4394 && vernum > 1
4395 && definition)
4396 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4397 }
4398
4399 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4400 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4401 (struct bfd_link_hash_entry **) sym_hash)))
4402 goto error_free_vers;
4403
a43942db
MR
4404 if ((flags & BSF_GNU_UNIQUE)
4405 && (abfd->flags & DYNAMIC) == 0
4406 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4407 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4408
4ad4eba5 4409 h = *sym_hash;
90c984fc
L
4410 /* We need to make sure that indirect symbol dynamic flags are
4411 updated. */
4412 hi = h;
4ad4eba5
AM
4413 while (h->root.type == bfd_link_hash_indirect
4414 || h->root.type == bfd_link_hash_warning)
4415 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4416
97196564
L
4417 /* Setting the index to -3 tells elf_link_output_extsym that
4418 this symbol is defined in a discarded section. */
4419 if (discarded)
4420 h->indx = -3;
4421
4ad4eba5
AM
4422 *sym_hash = h;
4423
37a9e49a 4424 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4425 new_weakdef = FALSE;
4426 if (dynamic
4427 && definition
37a9e49a 4428 && new_weak
fcb93ecf 4429 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4430 && is_elf_hash_table (htab)
f6e332e6 4431 && h->u.weakdef == NULL)
4ad4eba5
AM
4432 {
4433 /* Keep a list of all weak defined non function symbols from
4434 a dynamic object, using the weakdef field. Later in this
4435 function we will set the weakdef field to the correct
4436 value. We only put non-function symbols from dynamic
4437 objects on this list, because that happens to be the only
4438 time we need to know the normal symbol corresponding to a
4439 weak symbol, and the information is time consuming to
4440 figure out. If the weakdef field is not already NULL,
4441 then this symbol was already defined by some previous
4442 dynamic object, and we will be using that previous
4443 definition anyhow. */
4444
f6e332e6 4445 h->u.weakdef = weaks;
4ad4eba5
AM
4446 weaks = h;
4447 new_weakdef = TRUE;
4448 }
4449
4450 /* Set the alignment of a common symbol. */
a4d8e49b 4451 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4452 && h->root.type == bfd_link_hash_common)
4453 {
4454 unsigned int align;
4455
a4d8e49b 4456 if (common)
af44c138
L
4457 align = bfd_log2 (isym->st_value);
4458 else
4459 {
4460 /* The new symbol is a common symbol in a shared object.
4461 We need to get the alignment from the section. */
4462 align = new_sec->alignment_power;
4463 }
595213d4 4464 if (align > old_alignment)
4ad4eba5
AM
4465 h->root.u.c.p->alignment_power = align;
4466 else
4467 h->root.u.c.p->alignment_power = old_alignment;
4468 }
4469
66eb6687 4470 if (is_elf_hash_table (htab))
4ad4eba5 4471 {
4f3fedcf
AM
4472 /* Set a flag in the hash table entry indicating the type of
4473 reference or definition we just found. A dynamic symbol
4474 is one which is referenced or defined by both a regular
4475 object and a shared object. */
4476 bfd_boolean dynsym = FALSE;
4477
4478 /* Plugin symbols aren't normal. Don't set def_regular or
4479 ref_regular for them, or make them dynamic. */
4480 if ((abfd->flags & BFD_PLUGIN) != 0)
4481 ;
4482 else if (! dynamic)
4483 {
4484 if (! definition)
4485 {
4486 h->ref_regular = 1;
4487 if (bind != STB_WEAK)
4488 h->ref_regular_nonweak = 1;
4489 }
4490 else
4491 {
4492 h->def_regular = 1;
4493 if (h->def_dynamic)
4494 {
4495 h->def_dynamic = 0;
4496 h->ref_dynamic = 1;
4497 }
4498 }
4499
4500 /* If the indirect symbol has been forced local, don't
4501 make the real symbol dynamic. */
4502 if ((h == hi || !hi->forced_local)
0e1862bb 4503 && (bfd_link_dll (info)
4f3fedcf
AM
4504 || h->def_dynamic
4505 || h->ref_dynamic))
4506 dynsym = TRUE;
4507 }
4508 else
4509 {
4510 if (! definition)
4511 {
4512 h->ref_dynamic = 1;
4513 hi->ref_dynamic = 1;
4514 }
4515 else
4516 {
4517 h->def_dynamic = 1;
4518 hi->def_dynamic = 1;
4519 }
4520
4521 /* If the indirect symbol has been forced local, don't
4522 make the real symbol dynamic. */
4523 if ((h == hi || !hi->forced_local)
4524 && (h->def_regular
4525 || h->ref_regular
4526 || (h->u.weakdef != NULL
4527 && ! new_weakdef
4528 && h->u.weakdef->dynindx != -1)))
4529 dynsym = TRUE;
4530 }
4531
4532 /* Check to see if we need to add an indirect symbol for
4533 the default name. */
4534 if (definition
4535 || (!override && h->root.type == bfd_link_hash_common))
4536 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4537 sec, value, &old_bfd, &dynsym))
4538 goto error_free_vers;
4ad4eba5
AM
4539
4540 /* Check the alignment when a common symbol is involved. This
4541 can change when a common symbol is overridden by a normal
4542 definition or a common symbol is ignored due to the old
4543 normal definition. We need to make sure the maximum
4544 alignment is maintained. */
a4d8e49b 4545 if ((old_alignment || common)
4ad4eba5
AM
4546 && h->root.type != bfd_link_hash_common)
4547 {
4548 unsigned int common_align;
4549 unsigned int normal_align;
4550 unsigned int symbol_align;
4551 bfd *normal_bfd;
4552 bfd *common_bfd;
4553
3a81e825
AM
4554 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4555 || h->root.type == bfd_link_hash_defweak);
4556
4ad4eba5
AM
4557 symbol_align = ffs (h->root.u.def.value) - 1;
4558 if (h->root.u.def.section->owner != NULL
4559 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4560 {
4561 normal_align = h->root.u.def.section->alignment_power;
4562 if (normal_align > symbol_align)
4563 normal_align = symbol_align;
4564 }
4565 else
4566 normal_align = symbol_align;
4567
4568 if (old_alignment)
4569 {
4570 common_align = old_alignment;
4571 common_bfd = old_bfd;
4572 normal_bfd = abfd;
4573 }
4574 else
4575 {
4576 common_align = bfd_log2 (isym->st_value);
4577 common_bfd = abfd;
4578 normal_bfd = old_bfd;
4579 }
4580
4581 if (normal_align < common_align)
d07676f8
NC
4582 {
4583 /* PR binutils/2735 */
4584 if (normal_bfd == NULL)
4585 (*_bfd_error_handler)
4f3fedcf
AM
4586 (_("Warning: alignment %u of common symbol `%s' in %B is"
4587 " greater than the alignment (%u) of its section %A"),
d07676f8
NC
4588 common_bfd, h->root.u.def.section,
4589 1 << common_align, name, 1 << normal_align);
4590 else
4591 (*_bfd_error_handler)
4592 (_("Warning: alignment %u of symbol `%s' in %B"
4593 " is smaller than %u in %B"),
4594 normal_bfd, common_bfd,
4595 1 << normal_align, name, 1 << common_align);
4596 }
4ad4eba5
AM
4597 }
4598
83ad0046 4599 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4600 if (isym->st_size != 0
4601 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4602 && (definition || h->size == 0))
4603 {
83ad0046
L
4604 if (h->size != 0
4605 && h->size != isym->st_size
4606 && ! size_change_ok)
4ad4eba5 4607 (*_bfd_error_handler)
d003868e
AM
4608 (_("Warning: size of symbol `%s' changed"
4609 " from %lu in %B to %lu in %B"),
4610 old_bfd, abfd,
4ad4eba5 4611 name, (unsigned long) h->size,
d003868e 4612 (unsigned long) isym->st_size);
4ad4eba5
AM
4613
4614 h->size = isym->st_size;
4615 }
4616
4617 /* If this is a common symbol, then we always want H->SIZE
4618 to be the size of the common symbol. The code just above
4619 won't fix the size if a common symbol becomes larger. We
4620 don't warn about a size change here, because that is
4f3fedcf 4621 covered by --warn-common. Allow changes between different
fcb93ecf 4622 function types. */
4ad4eba5
AM
4623 if (h->root.type == bfd_link_hash_common)
4624 h->size = h->root.u.c.size;
4625
4626 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4627 && ((definition && !new_weak)
4628 || (old_weak && h->root.type == bfd_link_hash_common)
4629 || h->type == STT_NOTYPE))
4ad4eba5 4630 {
2955ec4c
L
4631 unsigned int type = ELF_ST_TYPE (isym->st_info);
4632
4633 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4634 symbol. */
4635 if (type == STT_GNU_IFUNC
4636 && (abfd->flags & DYNAMIC) != 0)
4637 type = STT_FUNC;
4ad4eba5 4638
2955ec4c
L
4639 if (h->type != type)
4640 {
4641 if (h->type != STT_NOTYPE && ! type_change_ok)
4642 (*_bfd_error_handler)
4643 (_("Warning: type of symbol `%s' changed"
4644 " from %d to %d in %B"),
4645 abfd, name, h->type, type);
4646
4647 h->type = type;
4648 }
4ad4eba5
AM
4649 }
4650
54ac0771 4651 /* Merge st_other field. */
b8417128 4652 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4653
c3df8c14 4654 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4655 if (definition
4656 && (sec->flags & SEC_DEBUGGING)
4657 && !bfd_link_relocatable (info))
c3df8c14
AM
4658 dynsym = FALSE;
4659
4f3fedcf
AM
4660 /* Nor should we make plugin symbols dynamic. */
4661 if ((abfd->flags & BFD_PLUGIN) != 0)
4662 dynsym = FALSE;
4663
35fc36a8 4664 if (definition)
35399224
L
4665 {
4666 h->target_internal = isym->st_target_internal;
4667 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4668 }
35fc36a8 4669
4ad4eba5
AM
4670 if (definition && !dynamic)
4671 {
4672 char *p = strchr (name, ELF_VER_CHR);
4673 if (p != NULL && p[1] != ELF_VER_CHR)
4674 {
4675 /* Queue non-default versions so that .symver x, x@FOO
4676 aliases can be checked. */
66eb6687 4677 if (!nondeflt_vers)
4ad4eba5 4678 {
66eb6687
AM
4679 amt = ((isymend - isym + 1)
4680 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4681 nondeflt_vers
4682 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4683 if (!nondeflt_vers)
4684 goto error_free_vers;
4ad4eba5 4685 }
66eb6687 4686 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4687 }
4688 }
4689
4690 if (dynsym && h->dynindx == -1)
4691 {
c152c796 4692 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4693 goto error_free_vers;
f6e332e6 4694 if (h->u.weakdef != NULL
4ad4eba5 4695 && ! new_weakdef
f6e332e6 4696 && h->u.weakdef->dynindx == -1)
4ad4eba5 4697 {
66eb6687 4698 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4699 goto error_free_vers;
4700 }
4701 }
1f599d0e 4702 else if (h->dynindx != -1)
4ad4eba5
AM
4703 /* If the symbol already has a dynamic index, but
4704 visibility says it should not be visible, turn it into
4705 a local symbol. */
4706 switch (ELF_ST_VISIBILITY (h->other))
4707 {
4708 case STV_INTERNAL:
4709 case STV_HIDDEN:
4710 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4711 dynsym = FALSE;
4712 break;
4713 }
4714
aef28989
L
4715 /* Don't add DT_NEEDED for references from the dummy bfd nor
4716 for unmatched symbol. */
4ad4eba5 4717 if (!add_needed
aef28989 4718 && matched
4ad4eba5 4719 && definition
010e5ae2 4720 && ((dynsym
ffa9430d 4721 && h->ref_regular_nonweak
4f3fedcf
AM
4722 && (old_bfd == NULL
4723 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4724 || (h->ref_dynamic_nonweak
010e5ae2 4725 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4726 && !on_needed_list (elf_dt_name (abfd),
4727 htab->needed, NULL))))
4ad4eba5
AM
4728 {
4729 int ret;
4730 const char *soname = elf_dt_name (abfd);
4731
16e4ecc0
AM
4732 info->callbacks->minfo ("%!", soname, old_bfd,
4733 h->root.root.string);
4734
4ad4eba5
AM
4735 /* A symbol from a library loaded via DT_NEEDED of some
4736 other library is referenced by a regular object.
e56f61be 4737 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4738 --no-add-needed is used and the reference was not
4739 a weak one. */
4f3fedcf 4740 if (old_bfd != NULL
b918acf9 4741 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4742 {
4743 (*_bfd_error_handler)
3cbc5de0 4744 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4745 old_bfd, name);
ff5ac77b 4746 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4747 goto error_free_vers;
4748 }
4749
a50b1753 4750 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4751 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4752
4ad4eba5 4753 add_needed = TRUE;
7e9f0867 4754 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4755 if (ret < 0)
4756 goto error_free_vers;
4757
4758 BFD_ASSERT (ret == 0);
4759 }
4760 }
4761 }
4762
66eb6687
AM
4763 if (extversym != NULL)
4764 {
4765 free (extversym);
4766 extversym = NULL;
4767 }
4768
4769 if (isymbuf != NULL)
4770 {
4771 free (isymbuf);
4772 isymbuf = NULL;
4773 }
4774
4775 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4776 {
4777 unsigned int i;
4778
4779 /* Restore the symbol table. */
f45794cb
AM
4780 old_ent = (char *) old_tab + tabsize;
4781 memset (elf_sym_hashes (abfd), 0,
4782 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4783 htab->root.table.table = old_table;
4784 htab->root.table.size = old_size;
4785 htab->root.table.count = old_count;
66eb6687 4786 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4787 htab->root.undefs = old_undefs;
4788 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
4789 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4790 free (old_strtab);
4791 old_strtab = NULL;
66eb6687
AM
4792 for (i = 0; i < htab->root.table.size; i++)
4793 {
4794 struct bfd_hash_entry *p;
4795 struct elf_link_hash_entry *h;
3e0882af
L
4796 bfd_size_type size;
4797 unsigned int alignment_power;
66eb6687
AM
4798
4799 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4800 {
4801 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4802 if (h->root.type == bfd_link_hash_warning)
4803 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4804
3e0882af
L
4805 /* Preserve the maximum alignment and size for common
4806 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4807 since it can still be loaded at run time by another
3e0882af
L
4808 dynamic lib. */
4809 if (h->root.type == bfd_link_hash_common)
4810 {
4811 size = h->root.u.c.size;
4812 alignment_power = h->root.u.c.p->alignment_power;
4813 }
4814 else
4815 {
4816 size = 0;
4817 alignment_power = 0;
4818 }
66eb6687
AM
4819 memcpy (p, old_ent, htab->root.table.entsize);
4820 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4821 h = (struct elf_link_hash_entry *) p;
4822 if (h->root.type == bfd_link_hash_warning)
4823 {
4824 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4825 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4826 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4827 }
a4542f1b 4828 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4829 {
4830 if (size > h->root.u.c.size)
4831 h->root.u.c.size = size;
4832 if (alignment_power > h->root.u.c.p->alignment_power)
4833 h->root.u.c.p->alignment_power = alignment_power;
4834 }
66eb6687
AM
4835 }
4836 }
4837
5061a885
AM
4838 /* Make a special call to the linker "notice" function to
4839 tell it that symbols added for crefs may need to be removed. */
e5034e59 4840 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4841 goto error_free_vers;
5061a885 4842
66eb6687
AM
4843 free (old_tab);
4844 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4845 alloc_mark);
4846 if (nondeflt_vers != NULL)
4847 free (nondeflt_vers);
4848 return TRUE;
4849 }
2de92251 4850
66eb6687
AM
4851 if (old_tab != NULL)
4852 {
e5034e59 4853 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4854 goto error_free_vers;
66eb6687
AM
4855 free (old_tab);
4856 old_tab = NULL;
4857 }
4858
c6e8a9a8
L
4859 /* Now that all the symbols from this input file are created, if
4860 not performing a relocatable link, handle .symver foo, foo@BAR
4861 such that any relocs against foo become foo@BAR. */
0e1862bb 4862 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 4863 {
ef53be89 4864 size_t cnt, symidx;
4ad4eba5
AM
4865
4866 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4867 {
4868 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4869 char *shortname, *p;
4870
4871 p = strchr (h->root.root.string, ELF_VER_CHR);
4872 if (p == NULL
4873 || (h->root.type != bfd_link_hash_defined
4874 && h->root.type != bfd_link_hash_defweak))
4875 continue;
4876
4877 amt = p - h->root.root.string;
a50b1753 4878 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4879 if (!shortname)
4880 goto error_free_vers;
4ad4eba5
AM
4881 memcpy (shortname, h->root.root.string, amt);
4882 shortname[amt] = '\0';
4883
4884 hi = (struct elf_link_hash_entry *)
66eb6687 4885 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4886 FALSE, FALSE, FALSE);
4887 if (hi != NULL
4888 && hi->root.type == h->root.type
4889 && hi->root.u.def.value == h->root.u.def.value
4890 && hi->root.u.def.section == h->root.u.def.section)
4891 {
4892 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4893 hi->root.type = bfd_link_hash_indirect;
4894 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4895 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4896 sym_hash = elf_sym_hashes (abfd);
4897 if (sym_hash)
4898 for (symidx = 0; symidx < extsymcount; ++symidx)
4899 if (sym_hash[symidx] == hi)
4900 {
4901 sym_hash[symidx] = h;
4902 break;
4903 }
4904 }
4905 free (shortname);
4906 }
4907 free (nondeflt_vers);
4908 nondeflt_vers = NULL;
4909 }
4910
4ad4eba5
AM
4911 /* Now set the weakdefs field correctly for all the weak defined
4912 symbols we found. The only way to do this is to search all the
4913 symbols. Since we only need the information for non functions in
4914 dynamic objects, that's the only time we actually put anything on
4915 the list WEAKS. We need this information so that if a regular
4916 object refers to a symbol defined weakly in a dynamic object, the
4917 real symbol in the dynamic object is also put in the dynamic
4918 symbols; we also must arrange for both symbols to point to the
4919 same memory location. We could handle the general case of symbol
4920 aliasing, but a general symbol alias can only be generated in
4921 assembler code, handling it correctly would be very time
4922 consuming, and other ELF linkers don't handle general aliasing
4923 either. */
4924 if (weaks != NULL)
4925 {
4926 struct elf_link_hash_entry **hpp;
4927 struct elf_link_hash_entry **hppend;
4928 struct elf_link_hash_entry **sorted_sym_hash;
4929 struct elf_link_hash_entry *h;
4930 size_t sym_count;
4931
4932 /* Since we have to search the whole symbol list for each weak
4933 defined symbol, search time for N weak defined symbols will be
4934 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
4935 amt = extsymcount;
4936 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 4937 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4938 if (sorted_sym_hash == NULL)
4939 goto error_return;
4940 sym_hash = sorted_sym_hash;
4941 hpp = elf_sym_hashes (abfd);
4942 hppend = hpp + extsymcount;
4943 sym_count = 0;
4944 for (; hpp < hppend; hpp++)
4945 {
4946 h = *hpp;
4947 if (h != NULL
4948 && h->root.type == bfd_link_hash_defined
fcb93ecf 4949 && !bed->is_function_type (h->type))
4ad4eba5
AM
4950 {
4951 *sym_hash = h;
4952 sym_hash++;
4953 sym_count++;
4954 }
4955 }
4956
4957 qsort (sorted_sym_hash, sym_count,
4958 sizeof (struct elf_link_hash_entry *),
4959 elf_sort_symbol);
4960
4961 while (weaks != NULL)
4962 {
4963 struct elf_link_hash_entry *hlook;
4964 asection *slook;
4965 bfd_vma vlook;
ed54588d 4966 size_t i, j, idx = 0;
4ad4eba5
AM
4967
4968 hlook = weaks;
f6e332e6
AM
4969 weaks = hlook->u.weakdef;
4970 hlook->u.weakdef = NULL;
4ad4eba5
AM
4971
4972 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4973 || hlook->root.type == bfd_link_hash_defweak
4974 || hlook->root.type == bfd_link_hash_common
4975 || hlook->root.type == bfd_link_hash_indirect);
4976 slook = hlook->root.u.def.section;
4977 vlook = hlook->root.u.def.value;
4978
4ad4eba5
AM
4979 i = 0;
4980 j = sym_count;
14160578 4981 while (i != j)
4ad4eba5
AM
4982 {
4983 bfd_signed_vma vdiff;
4984 idx = (i + j) / 2;
14160578 4985 h = sorted_sym_hash[idx];
4ad4eba5
AM
4986 vdiff = vlook - h->root.u.def.value;
4987 if (vdiff < 0)
4988 j = idx;
4989 else if (vdiff > 0)
4990 i = idx + 1;
4991 else
4992 {
d3435ae8 4993 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4994 if (sdiff < 0)
4995 j = idx;
4996 else if (sdiff > 0)
4997 i = idx + 1;
4998 else
14160578 4999 break;
4ad4eba5
AM
5000 }
5001 }
5002
5003 /* We didn't find a value/section match. */
14160578 5004 if (i == j)
4ad4eba5
AM
5005 continue;
5006
14160578
AM
5007 /* With multiple aliases, or when the weak symbol is already
5008 strongly defined, we have multiple matching symbols and
5009 the binary search above may land on any of them. Step
5010 one past the matching symbol(s). */
5011 while (++idx != j)
5012 {
5013 h = sorted_sym_hash[idx];
5014 if (h->root.u.def.section != slook
5015 || h->root.u.def.value != vlook)
5016 break;
5017 }
5018
5019 /* Now look back over the aliases. Since we sorted by size
5020 as well as value and section, we'll choose the one with
5021 the largest size. */
5022 while (idx-- != i)
4ad4eba5 5023 {
14160578 5024 h = sorted_sym_hash[idx];
4ad4eba5
AM
5025
5026 /* Stop if value or section doesn't match. */
14160578
AM
5027 if (h->root.u.def.section != slook
5028 || h->root.u.def.value != vlook)
4ad4eba5
AM
5029 break;
5030 else if (h != hlook)
5031 {
f6e332e6 5032 hlook->u.weakdef = h;
4ad4eba5
AM
5033
5034 /* If the weak definition is in the list of dynamic
5035 symbols, make sure the real definition is put
5036 there as well. */
5037 if (hlook->dynindx != -1 && h->dynindx == -1)
5038 {
c152c796 5039 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5040 {
5041 err_free_sym_hash:
5042 free (sorted_sym_hash);
5043 goto error_return;
5044 }
4ad4eba5
AM
5045 }
5046
5047 /* If the real definition is in the list of dynamic
5048 symbols, make sure the weak definition is put
5049 there as well. If we don't do this, then the
5050 dynamic loader might not merge the entries for the
5051 real definition and the weak definition. */
5052 if (h->dynindx != -1 && hlook->dynindx == -1)
5053 {
c152c796 5054 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5055 goto err_free_sym_hash;
4ad4eba5
AM
5056 }
5057 break;
5058 }
5059 }
5060 }
5061
5062 free (sorted_sym_hash);
5063 }
5064
33177bb1
AM
5065 if (bed->check_directives
5066 && !(*bed->check_directives) (abfd, info))
5067 return FALSE;
85fbca6a 5068
d9689752
L
5069 if (!info->check_relocs_after_open_input
5070 && !_bfd_elf_link_check_relocs (abfd, info))
5071 return FALSE;
4ad4eba5
AM
5072
5073 /* If this is a non-traditional link, try to optimize the handling
5074 of the .stab/.stabstr sections. */
5075 if (! dynamic
5076 && ! info->traditional_format
66eb6687 5077 && is_elf_hash_table (htab)
4ad4eba5
AM
5078 && (info->strip != strip_all && info->strip != strip_debugger))
5079 {
5080 asection *stabstr;
5081
5082 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5083 if (stabstr != NULL)
5084 {
5085 bfd_size_type string_offset = 0;
5086 asection *stab;
5087
5088 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5089 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5090 && (!stab->name[5] ||
5091 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5092 && (stab->flags & SEC_MERGE) == 0
5093 && !bfd_is_abs_section (stab->output_section))
5094 {
5095 struct bfd_elf_section_data *secdata;
5096
5097 secdata = elf_section_data (stab);
66eb6687
AM
5098 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5099 stabstr, &secdata->sec_info,
4ad4eba5
AM
5100 &string_offset))
5101 goto error_return;
5102 if (secdata->sec_info)
dbaa2011 5103 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5104 }
5105 }
5106 }
5107
66eb6687 5108 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5109 {
5110 /* Add this bfd to the loaded list. */
5111 struct elf_link_loaded_list *n;
5112
ca4be51c 5113 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5114 if (n == NULL)
5115 goto error_return;
5116 n->abfd = abfd;
66eb6687
AM
5117 n->next = htab->loaded;
5118 htab->loaded = n;
4ad4eba5
AM
5119 }
5120
5121 return TRUE;
5122
5123 error_free_vers:
66eb6687
AM
5124 if (old_tab != NULL)
5125 free (old_tab);
5b677558
AM
5126 if (old_strtab != NULL)
5127 free (old_strtab);
4ad4eba5
AM
5128 if (nondeflt_vers != NULL)
5129 free (nondeflt_vers);
5130 if (extversym != NULL)
5131 free (extversym);
5132 error_free_sym:
5133 if (isymbuf != NULL)
5134 free (isymbuf);
5135 error_return:
5136 return FALSE;
5137}
5138
8387904d
AM
5139/* Return the linker hash table entry of a symbol that might be
5140 satisfied by an archive symbol. Return -1 on error. */
5141
5142struct elf_link_hash_entry *
5143_bfd_elf_archive_symbol_lookup (bfd *abfd,
5144 struct bfd_link_info *info,
5145 const char *name)
5146{
5147 struct elf_link_hash_entry *h;
5148 char *p, *copy;
5149 size_t len, first;
5150
2a41f396 5151 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5152 if (h != NULL)
5153 return h;
5154
5155 /* If this is a default version (the name contains @@), look up the
5156 symbol again with only one `@' as well as without the version.
5157 The effect is that references to the symbol with and without the
5158 version will be matched by the default symbol in the archive. */
5159
5160 p = strchr (name, ELF_VER_CHR);
5161 if (p == NULL || p[1] != ELF_VER_CHR)
5162 return h;
5163
5164 /* First check with only one `@'. */
5165 len = strlen (name);
a50b1753 5166 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5167 if (copy == NULL)
5168 return (struct elf_link_hash_entry *) 0 - 1;
5169
5170 first = p - name + 1;
5171 memcpy (copy, name, first);
5172 memcpy (copy + first, name + first + 1, len - first);
5173
2a41f396 5174 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5175 if (h == NULL)
5176 {
5177 /* We also need to check references to the symbol without the
5178 version. */
5179 copy[first - 1] = '\0';
5180 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5181 FALSE, FALSE, TRUE);
8387904d
AM
5182 }
5183
5184 bfd_release (abfd, copy);
5185 return h;
5186}
5187
0ad989f9 5188/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5189 don't use _bfd_generic_link_add_archive_symbols because we need to
5190 handle versioned symbols.
0ad989f9
L
5191
5192 Fortunately, ELF archive handling is simpler than that done by
5193 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5194 oddities. In ELF, if we find a symbol in the archive map, and the
5195 symbol is currently undefined, we know that we must pull in that
5196 object file.
5197
5198 Unfortunately, we do have to make multiple passes over the symbol
5199 table until nothing further is resolved. */
5200
4ad4eba5
AM
5201static bfd_boolean
5202elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5203{
5204 symindex c;
13e570f8 5205 unsigned char *included = NULL;
0ad989f9
L
5206 carsym *symdefs;
5207 bfd_boolean loop;
5208 bfd_size_type amt;
8387904d
AM
5209 const struct elf_backend_data *bed;
5210 struct elf_link_hash_entry * (*archive_symbol_lookup)
5211 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5212
5213 if (! bfd_has_map (abfd))
5214 {
5215 /* An empty archive is a special case. */
5216 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5217 return TRUE;
5218 bfd_set_error (bfd_error_no_armap);
5219 return FALSE;
5220 }
5221
5222 /* Keep track of all symbols we know to be already defined, and all
5223 files we know to be already included. This is to speed up the
5224 second and subsequent passes. */
5225 c = bfd_ardata (abfd)->symdef_count;
5226 if (c == 0)
5227 return TRUE;
5228 amt = c;
13e570f8
AM
5229 amt *= sizeof (*included);
5230 included = (unsigned char *) bfd_zmalloc (amt);
5231 if (included == NULL)
5232 return FALSE;
0ad989f9
L
5233
5234 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5235 bed = get_elf_backend_data (abfd);
5236 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5237
5238 do
5239 {
5240 file_ptr last;
5241 symindex i;
5242 carsym *symdef;
5243 carsym *symdefend;
5244
5245 loop = FALSE;
5246 last = -1;
5247
5248 symdef = symdefs;
5249 symdefend = symdef + c;
5250 for (i = 0; symdef < symdefend; symdef++, i++)
5251 {
5252 struct elf_link_hash_entry *h;
5253 bfd *element;
5254 struct bfd_link_hash_entry *undefs_tail;
5255 symindex mark;
5256
13e570f8 5257 if (included[i])
0ad989f9
L
5258 continue;
5259 if (symdef->file_offset == last)
5260 {
5261 included[i] = TRUE;
5262 continue;
5263 }
5264
8387904d
AM
5265 h = archive_symbol_lookup (abfd, info, symdef->name);
5266 if (h == (struct elf_link_hash_entry *) 0 - 1)
5267 goto error_return;
0ad989f9
L
5268
5269 if (h == NULL)
5270 continue;
5271
5272 if (h->root.type == bfd_link_hash_common)
5273 {
5274 /* We currently have a common symbol. The archive map contains
5275 a reference to this symbol, so we may want to include it. We
5276 only want to include it however, if this archive element
5277 contains a definition of the symbol, not just another common
5278 declaration of it.
5279
5280 Unfortunately some archivers (including GNU ar) will put
5281 declarations of common symbols into their archive maps, as
5282 well as real definitions, so we cannot just go by the archive
5283 map alone. Instead we must read in the element's symbol
5284 table and check that to see what kind of symbol definition
5285 this is. */
5286 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5287 continue;
5288 }
5289 else if (h->root.type != bfd_link_hash_undefined)
5290 {
5291 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5292 /* Symbol must be defined. Don't check it again. */
5293 included[i] = TRUE;
0ad989f9
L
5294 continue;
5295 }
5296
5297 /* We need to include this archive member. */
5298 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5299 if (element == NULL)
5300 goto error_return;
5301
5302 if (! bfd_check_format (element, bfd_object))
5303 goto error_return;
5304
0ad989f9
L
5305 undefs_tail = info->hash->undefs_tail;
5306
0e144ba7
AM
5307 if (!(*info->callbacks
5308 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5309 continue;
0e144ba7 5310 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5311 goto error_return;
5312
5313 /* If there are any new undefined symbols, we need to make
5314 another pass through the archive in order to see whether
5315 they can be defined. FIXME: This isn't perfect, because
5316 common symbols wind up on undefs_tail and because an
5317 undefined symbol which is defined later on in this pass
5318 does not require another pass. This isn't a bug, but it
5319 does make the code less efficient than it could be. */
5320 if (undefs_tail != info->hash->undefs_tail)
5321 loop = TRUE;
5322
5323 /* Look backward to mark all symbols from this object file
5324 which we have already seen in this pass. */
5325 mark = i;
5326 do
5327 {
5328 included[mark] = TRUE;
5329 if (mark == 0)
5330 break;
5331 --mark;
5332 }
5333 while (symdefs[mark].file_offset == symdef->file_offset);
5334
5335 /* We mark subsequent symbols from this object file as we go
5336 on through the loop. */
5337 last = symdef->file_offset;
5338 }
5339 }
5340 while (loop);
5341
0ad989f9
L
5342 free (included);
5343
5344 return TRUE;
5345
5346 error_return:
0ad989f9
L
5347 if (included != NULL)
5348 free (included);
5349 return FALSE;
5350}
4ad4eba5
AM
5351
5352/* Given an ELF BFD, add symbols to the global hash table as
5353 appropriate. */
5354
5355bfd_boolean
5356bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5357{
5358 switch (bfd_get_format (abfd))
5359 {
5360 case bfd_object:
5361 return elf_link_add_object_symbols (abfd, info);
5362 case bfd_archive:
5363 return elf_link_add_archive_symbols (abfd, info);
5364 default:
5365 bfd_set_error (bfd_error_wrong_format);
5366 return FALSE;
5367 }
5368}
5a580b3a 5369\f
14b1c01e
AM
5370struct hash_codes_info
5371{
5372 unsigned long *hashcodes;
5373 bfd_boolean error;
5374};
a0c8462f 5375
5a580b3a
AM
5376/* This function will be called though elf_link_hash_traverse to store
5377 all hash value of the exported symbols in an array. */
5378
5379static bfd_boolean
5380elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5381{
a50b1753 5382 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5383 const char *name;
5a580b3a
AM
5384 unsigned long ha;
5385 char *alc = NULL;
5386
5a580b3a
AM
5387 /* Ignore indirect symbols. These are added by the versioning code. */
5388 if (h->dynindx == -1)
5389 return TRUE;
5390
5391 name = h->root.root.string;
422f1182 5392 if (h->versioned >= versioned)
5a580b3a 5393 {
422f1182
L
5394 char *p = strchr (name, ELF_VER_CHR);
5395 if (p != NULL)
14b1c01e 5396 {
422f1182
L
5397 alc = (char *) bfd_malloc (p - name + 1);
5398 if (alc == NULL)
5399 {
5400 inf->error = TRUE;
5401 return FALSE;
5402 }
5403 memcpy (alc, name, p - name);
5404 alc[p - name] = '\0';
5405 name = alc;
14b1c01e 5406 }
5a580b3a
AM
5407 }
5408
5409 /* Compute the hash value. */
5410 ha = bfd_elf_hash (name);
5411
5412 /* Store the found hash value in the array given as the argument. */
14b1c01e 5413 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5414
5415 /* And store it in the struct so that we can put it in the hash table
5416 later. */
f6e332e6 5417 h->u.elf_hash_value = ha;
5a580b3a
AM
5418
5419 if (alc != NULL)
5420 free (alc);
5421
5422 return TRUE;
5423}
5424
fdc90cb4
JJ
5425struct collect_gnu_hash_codes
5426{
5427 bfd *output_bfd;
5428 const struct elf_backend_data *bed;
5429 unsigned long int nsyms;
5430 unsigned long int maskbits;
5431 unsigned long int *hashcodes;
5432 unsigned long int *hashval;
5433 unsigned long int *indx;
5434 unsigned long int *counts;
5435 bfd_vma *bitmask;
5436 bfd_byte *contents;
5437 long int min_dynindx;
5438 unsigned long int bucketcount;
5439 unsigned long int symindx;
5440 long int local_indx;
5441 long int shift1, shift2;
5442 unsigned long int mask;
14b1c01e 5443 bfd_boolean error;
fdc90cb4
JJ
5444};
5445
5446/* This function will be called though elf_link_hash_traverse to store
5447 all hash value of the exported symbols in an array. */
5448
5449static bfd_boolean
5450elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5451{
a50b1753 5452 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5453 const char *name;
fdc90cb4
JJ
5454 unsigned long ha;
5455 char *alc = NULL;
5456
fdc90cb4
JJ
5457 /* Ignore indirect symbols. These are added by the versioning code. */
5458 if (h->dynindx == -1)
5459 return TRUE;
5460
5461 /* Ignore also local symbols and undefined symbols. */
5462 if (! (*s->bed->elf_hash_symbol) (h))
5463 return TRUE;
5464
5465 name = h->root.root.string;
422f1182 5466 if (h->versioned >= versioned)
fdc90cb4 5467 {
422f1182
L
5468 char *p = strchr (name, ELF_VER_CHR);
5469 if (p != NULL)
14b1c01e 5470 {
422f1182
L
5471 alc = (char *) bfd_malloc (p - name + 1);
5472 if (alc == NULL)
5473 {
5474 s->error = TRUE;
5475 return FALSE;
5476 }
5477 memcpy (alc, name, p - name);
5478 alc[p - name] = '\0';
5479 name = alc;
14b1c01e 5480 }
fdc90cb4
JJ
5481 }
5482
5483 /* Compute the hash value. */
5484 ha = bfd_elf_gnu_hash (name);
5485
5486 /* Store the found hash value in the array for compute_bucket_count,
5487 and also for .dynsym reordering purposes. */
5488 s->hashcodes[s->nsyms] = ha;
5489 s->hashval[h->dynindx] = ha;
5490 ++s->nsyms;
5491 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5492 s->min_dynindx = h->dynindx;
5493
5494 if (alc != NULL)
5495 free (alc);
5496
5497 return TRUE;
5498}
5499
5500/* This function will be called though elf_link_hash_traverse to do
5501 final dynaminc symbol renumbering. */
5502
5503static bfd_boolean
5504elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5505{
a50b1753 5506 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5507 unsigned long int bucket;
5508 unsigned long int val;
5509
fdc90cb4
JJ
5510 /* Ignore indirect symbols. */
5511 if (h->dynindx == -1)
5512 return TRUE;
5513
5514 /* Ignore also local symbols and undefined symbols. */
5515 if (! (*s->bed->elf_hash_symbol) (h))
5516 {
5517 if (h->dynindx >= s->min_dynindx)
5518 h->dynindx = s->local_indx++;
5519 return TRUE;
5520 }
5521
5522 bucket = s->hashval[h->dynindx] % s->bucketcount;
5523 val = (s->hashval[h->dynindx] >> s->shift1)
5524 & ((s->maskbits >> s->shift1) - 1);
5525 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5526 s->bitmask[val]
5527 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5528 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5529 if (s->counts[bucket] == 1)
5530 /* Last element terminates the chain. */
5531 val |= 1;
5532 bfd_put_32 (s->output_bfd, val,
5533 s->contents + (s->indx[bucket] - s->symindx) * 4);
5534 --s->counts[bucket];
5535 h->dynindx = s->indx[bucket]++;
5536 return TRUE;
5537}
5538
5539/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5540
5541bfd_boolean
5542_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5543{
5544 return !(h->forced_local
5545 || h->root.type == bfd_link_hash_undefined
5546 || h->root.type == bfd_link_hash_undefweak
5547 || ((h->root.type == bfd_link_hash_defined
5548 || h->root.type == bfd_link_hash_defweak)
5549 && h->root.u.def.section->output_section == NULL));
5550}
5551
5a580b3a
AM
5552/* Array used to determine the number of hash table buckets to use
5553 based on the number of symbols there are. If there are fewer than
5554 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5555 fewer than 37 we use 17 buckets, and so forth. We never use more
5556 than 32771 buckets. */
5557
5558static const size_t elf_buckets[] =
5559{
5560 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5561 16411, 32771, 0
5562};
5563
5564/* Compute bucket count for hashing table. We do not use a static set
5565 of possible tables sizes anymore. Instead we determine for all
5566 possible reasonable sizes of the table the outcome (i.e., the
5567 number of collisions etc) and choose the best solution. The
5568 weighting functions are not too simple to allow the table to grow
5569 without bounds. Instead one of the weighting factors is the size.
5570 Therefore the result is always a good payoff between few collisions
5571 (= short chain lengths) and table size. */
5572static size_t
b20dd2ce 5573compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5574 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5575 unsigned long int nsyms,
5576 int gnu_hash)
5a580b3a 5577{
5a580b3a 5578 size_t best_size = 0;
5a580b3a 5579 unsigned long int i;
5a580b3a 5580
5a580b3a
AM
5581 /* We have a problem here. The following code to optimize the table
5582 size requires an integer type with more the 32 bits. If
5583 BFD_HOST_U_64_BIT is set we know about such a type. */
5584#ifdef BFD_HOST_U_64_BIT
5585 if (info->optimize)
5586 {
5a580b3a
AM
5587 size_t minsize;
5588 size_t maxsize;
5589 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5590 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5591 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5592 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5593 unsigned long int *counts;
d40f3da9 5594 bfd_size_type amt;
0883b6e0 5595 unsigned int no_improvement_count = 0;
5a580b3a
AM
5596
5597 /* Possible optimization parameters: if we have NSYMS symbols we say
5598 that the hashing table must at least have NSYMS/4 and at most
5599 2*NSYMS buckets. */
5600 minsize = nsyms / 4;
5601 if (minsize == 0)
5602 minsize = 1;
5603 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5604 if (gnu_hash)
5605 {
5606 if (minsize < 2)
5607 minsize = 2;
5608 if ((best_size & 31) == 0)
5609 ++best_size;
5610 }
5a580b3a
AM
5611
5612 /* Create array where we count the collisions in. We must use bfd_malloc
5613 since the size could be large. */
5614 amt = maxsize;
5615 amt *= sizeof (unsigned long int);
a50b1753 5616 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5617 if (counts == NULL)
fdc90cb4 5618 return 0;
5a580b3a
AM
5619
5620 /* Compute the "optimal" size for the hash table. The criteria is a
5621 minimal chain length. The minor criteria is (of course) the size
5622 of the table. */
5623 for (i = minsize; i < maxsize; ++i)
5624 {
5625 /* Walk through the array of hashcodes and count the collisions. */
5626 BFD_HOST_U_64_BIT max;
5627 unsigned long int j;
5628 unsigned long int fact;
5629
fdc90cb4
JJ
5630 if (gnu_hash && (i & 31) == 0)
5631 continue;
5632
5a580b3a
AM
5633 memset (counts, '\0', i * sizeof (unsigned long int));
5634
5635 /* Determine how often each hash bucket is used. */
5636 for (j = 0; j < nsyms; ++j)
5637 ++counts[hashcodes[j] % i];
5638
5639 /* For the weight function we need some information about the
5640 pagesize on the target. This is information need not be 100%
5641 accurate. Since this information is not available (so far) we
5642 define it here to a reasonable default value. If it is crucial
5643 to have a better value some day simply define this value. */
5644# ifndef BFD_TARGET_PAGESIZE
5645# define BFD_TARGET_PAGESIZE (4096)
5646# endif
5647
fdc90cb4
JJ
5648 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5649 and the chains. */
5650 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5651
5652# if 1
5653 /* Variant 1: optimize for short chains. We add the squares
5654 of all the chain lengths (which favors many small chain
5655 over a few long chains). */
5656 for (j = 0; j < i; ++j)
5657 max += counts[j] * counts[j];
5658
5659 /* This adds penalties for the overall size of the table. */
fdc90cb4 5660 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5661 max *= fact * fact;
5662# else
5663 /* Variant 2: Optimize a lot more for small table. Here we
5664 also add squares of the size but we also add penalties for
5665 empty slots (the +1 term). */
5666 for (j = 0; j < i; ++j)
5667 max += (1 + counts[j]) * (1 + counts[j]);
5668
5669 /* The overall size of the table is considered, but not as
5670 strong as in variant 1, where it is squared. */
fdc90cb4 5671 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5672 max *= fact;
5673# endif
5674
5675 /* Compare with current best results. */
5676 if (max < best_chlen)
5677 {
5678 best_chlen = max;
5679 best_size = i;
ca4be51c 5680 no_improvement_count = 0;
5a580b3a 5681 }
0883b6e0
NC
5682 /* PR 11843: Avoid futile long searches for the best bucket size
5683 when there are a large number of symbols. */
5684 else if (++no_improvement_count == 100)
5685 break;
5a580b3a
AM
5686 }
5687
5688 free (counts);
5689 }
5690 else
5691#endif /* defined (BFD_HOST_U_64_BIT) */
5692 {
5693 /* This is the fallback solution if no 64bit type is available or if we
5694 are not supposed to spend much time on optimizations. We select the
5695 bucket count using a fixed set of numbers. */
5696 for (i = 0; elf_buckets[i] != 0; i++)
5697 {
5698 best_size = elf_buckets[i];
fdc90cb4 5699 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5700 break;
5701 }
fdc90cb4
JJ
5702 if (gnu_hash && best_size < 2)
5703 best_size = 2;
5a580b3a
AM
5704 }
5705
5a580b3a
AM
5706 return best_size;
5707}
5708
d0bf826b
AM
5709/* Size any SHT_GROUP section for ld -r. */
5710
5711bfd_boolean
5712_bfd_elf_size_group_sections (struct bfd_link_info *info)
5713{
5714 bfd *ibfd;
5715
c72f2fb2 5716 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5717 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5718 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5719 return FALSE;
5720 return TRUE;
5721}
5722
04c3a755
NS
5723/* Set a default stack segment size. The value in INFO wins. If it
5724 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5725 undefined it is initialized. */
5726
5727bfd_boolean
5728bfd_elf_stack_segment_size (bfd *output_bfd,
5729 struct bfd_link_info *info,
5730 const char *legacy_symbol,
5731 bfd_vma default_size)
5732{
5733 struct elf_link_hash_entry *h = NULL;
5734
5735 /* Look for legacy symbol. */
5736 if (legacy_symbol)
5737 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5738 FALSE, FALSE, FALSE);
5739 if (h && (h->root.type == bfd_link_hash_defined
5740 || h->root.type == bfd_link_hash_defweak)
5741 && h->def_regular
5742 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5743 {
5744 /* The symbol has no type if specified on the command line. */
5745 h->type = STT_OBJECT;
5746 if (info->stacksize)
5747 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5748 output_bfd, legacy_symbol);
5749 else if (h->root.u.def.section != bfd_abs_section_ptr)
5750 (*_bfd_error_handler) (_("%B: %s not absolute"),
5751 output_bfd, legacy_symbol);
5752 else
5753 info->stacksize = h->root.u.def.value;
5754 }
5755
5756 if (!info->stacksize)
5757 /* If the user didn't set a size, or explicitly inhibit the
5758 size, set it now. */
5759 info->stacksize = default_size;
5760
5761 /* Provide the legacy symbol, if it is referenced. */
5762 if (h && (h->root.type == bfd_link_hash_undefined
5763 || h->root.type == bfd_link_hash_undefweak))
5764 {
5765 struct bfd_link_hash_entry *bh = NULL;
5766
5767 if (!(_bfd_generic_link_add_one_symbol
5768 (info, output_bfd, legacy_symbol,
5769 BSF_GLOBAL, bfd_abs_section_ptr,
5770 info->stacksize >= 0 ? info->stacksize : 0,
5771 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5772 return FALSE;
5773
5774 h = (struct elf_link_hash_entry *) bh;
5775 h->def_regular = 1;
5776 h->type = STT_OBJECT;
5777 }
5778
5779 return TRUE;
5780}
5781
5a580b3a
AM
5782/* Set up the sizes and contents of the ELF dynamic sections. This is
5783 called by the ELF linker emulation before_allocation routine. We
5784 must set the sizes of the sections before the linker sets the
5785 addresses of the various sections. */
5786
5787bfd_boolean
5788bfd_elf_size_dynamic_sections (bfd *output_bfd,
5789 const char *soname,
5790 const char *rpath,
5791 const char *filter_shlib,
7ee314fa
AM
5792 const char *audit,
5793 const char *depaudit,
5a580b3a
AM
5794 const char * const *auxiliary_filters,
5795 struct bfd_link_info *info,
fd91d419 5796 asection **sinterpptr)
5a580b3a 5797{
ef53be89 5798 size_t soname_indx;
5a580b3a
AM
5799 bfd *dynobj;
5800 const struct elf_backend_data *bed;
28caa186 5801 struct elf_info_failed asvinfo;
5a580b3a
AM
5802
5803 *sinterpptr = NULL;
5804
ef53be89 5805 soname_indx = (size_t) -1;
5a580b3a
AM
5806
5807 if (!is_elf_hash_table (info->hash))
5808 return TRUE;
5809
6bfdb61b 5810 bed = get_elf_backend_data (output_bfd);
04c3a755
NS
5811
5812 /* Any syms created from now on start with -1 in
5813 got.refcount/offset and plt.refcount/offset. */
5814 elf_hash_table (info)->init_got_refcount
5815 = elf_hash_table (info)->init_got_offset;
5816 elf_hash_table (info)->init_plt_refcount
5817 = elf_hash_table (info)->init_plt_offset;
5818
0e1862bb 5819 if (bfd_link_relocatable (info)
04c3a755
NS
5820 && !_bfd_elf_size_group_sections (info))
5821 return FALSE;
5822
5823 /* The backend may have to create some sections regardless of whether
5824 we're dynamic or not. */
5825 if (bed->elf_backend_always_size_sections
5826 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5827 return FALSE;
5828
5829 /* Determine any GNU_STACK segment requirements, after the backend
5830 has had a chance to set a default segment size. */
5a580b3a 5831 if (info->execstack)
12bd6957 5832 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5a580b3a 5833 else if (info->noexecstack)
12bd6957 5834 elf_stack_flags (output_bfd) = PF_R | PF_W;
5a580b3a
AM
5835 else
5836 {
5837 bfd *inputobj;
5838 asection *notesec = NULL;
5839 int exec = 0;
5840
5841 for (inputobj = info->input_bfds;
5842 inputobj;
c72f2fb2 5843 inputobj = inputobj->link.next)
5a580b3a
AM
5844 {
5845 asection *s;
5846
a92c088a
L
5847 if (inputobj->flags
5848 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5849 continue;
5850 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5851 if (s)
5852 {
5853 if (s->flags & SEC_CODE)
5854 exec = PF_X;
5855 notesec = s;
5856 }
6bfdb61b 5857 else if (bed->default_execstack)
5a580b3a
AM
5858 exec = PF_X;
5859 }
04c3a755 5860 if (notesec || info->stacksize > 0)
12bd6957 5861 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
0e1862bb 5862 if (notesec && exec && bfd_link_relocatable (info)
04c3a755
NS
5863 && notesec->output_section != bfd_abs_section_ptr)
5864 notesec->output_section->flags |= SEC_CODE;
5a580b3a
AM
5865 }
5866
5a580b3a
AM
5867 dynobj = elf_hash_table (info)->dynobj;
5868
9a2a56cc 5869 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
5870 {
5871 struct elf_info_failed eif;
5872 struct elf_link_hash_entry *h;
5873 asection *dynstr;
5874 struct bfd_elf_version_tree *t;
5875 struct bfd_elf_version_expr *d;
046183de 5876 asection *s;
5a580b3a
AM
5877 bfd_boolean all_defined;
5878
3d4d4302 5879 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
9b8b325a 5880 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5a580b3a
AM
5881
5882 if (soname != NULL)
5883 {
5884 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5885 soname, TRUE);
ef53be89 5886 if (soname_indx == (size_t) -1
5a580b3a
AM
5887 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5888 return FALSE;
5889 }
5890
5891 if (info->symbolic)
5892 {
5893 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5894 return FALSE;
5895 info->flags |= DF_SYMBOLIC;
5896 }
5897
5898 if (rpath != NULL)
5899 {
ef53be89 5900 size_t indx;
b1b00fcc 5901 bfd_vma tag;
5a580b3a
AM
5902
5903 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5904 TRUE);
ef53be89 5905 if (indx == (size_t) -1)
5a580b3a
AM
5906 return FALSE;
5907
b1b00fcc
MF
5908 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5909 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5910 return FALSE;
5a580b3a
AM
5911 }
5912
5913 if (filter_shlib != NULL)
5914 {
ef53be89 5915 size_t indx;
5a580b3a
AM
5916
5917 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5918 filter_shlib, TRUE);
ef53be89 5919 if (indx == (size_t) -1
5a580b3a
AM
5920 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5921 return FALSE;
5922 }
5923
5924 if (auxiliary_filters != NULL)
5925 {
5926 const char * const *p;
5927
5928 for (p = auxiliary_filters; *p != NULL; p++)
5929 {
ef53be89 5930 size_t indx;
5a580b3a
AM
5931
5932 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5933 *p, TRUE);
ef53be89 5934 if (indx == (size_t) -1
5a580b3a
AM
5935 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5936 return FALSE;
5937 }
5938 }
5939
7ee314fa
AM
5940 if (audit != NULL)
5941 {
ef53be89 5942 size_t indx;
7ee314fa
AM
5943
5944 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5945 TRUE);
ef53be89 5946 if (indx == (size_t) -1
7ee314fa
AM
5947 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5948 return FALSE;
5949 }
5950
5951 if (depaudit != NULL)
5952 {
ef53be89 5953 size_t indx;
7ee314fa
AM
5954
5955 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5956 TRUE);
ef53be89 5957 if (indx == (size_t) -1
7ee314fa
AM
5958 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5959 return FALSE;
5960 }
5961
5a580b3a 5962 eif.info = info;
5a580b3a
AM
5963 eif.failed = FALSE;
5964
5965 /* If we are supposed to export all symbols into the dynamic symbol
5966 table (this is not the normal case), then do so. */
55255dae 5967 if (info->export_dynamic
0e1862bb 5968 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
5969 {
5970 elf_link_hash_traverse (elf_hash_table (info),
5971 _bfd_elf_export_symbol,
5972 &eif);
5973 if (eif.failed)
5974 return FALSE;
5975 }
5976
5977 /* Make all global versions with definition. */
fd91d419 5978 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5979 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5980 if (!d->symver && d->literal)
5a580b3a
AM
5981 {
5982 const char *verstr, *name;
5983 size_t namelen, verlen, newlen;
93252b1c 5984 char *newname, *p, leading_char;
5a580b3a
AM
5985 struct elf_link_hash_entry *newh;
5986
93252b1c 5987 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 5988 name = d->pattern;
93252b1c 5989 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
5990 verstr = t->name;
5991 verlen = strlen (verstr);
5992 newlen = namelen + verlen + 3;
5993
a50b1753 5994 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
5995 if (newname == NULL)
5996 return FALSE;
93252b1c
MF
5997 newname[0] = leading_char;
5998 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
5999
6000 /* Check the hidden versioned definition. */
6001 p = newname + namelen;
6002 *p++ = ELF_VER_CHR;
6003 memcpy (p, verstr, verlen + 1);
6004 newh = elf_link_hash_lookup (elf_hash_table (info),
6005 newname, FALSE, FALSE,
6006 FALSE);
6007 if (newh == NULL
6008 || (newh->root.type != bfd_link_hash_defined
6009 && newh->root.type != bfd_link_hash_defweak))
6010 {
6011 /* Check the default versioned definition. */
6012 *p++ = ELF_VER_CHR;
6013 memcpy (p, verstr, verlen + 1);
6014 newh = elf_link_hash_lookup (elf_hash_table (info),
6015 newname, FALSE, FALSE,
6016 FALSE);
6017 }
6018 free (newname);
6019
6020 /* Mark this version if there is a definition and it is
6021 not defined in a shared object. */
6022 if (newh != NULL
f5385ebf 6023 && !newh->def_dynamic
5a580b3a
AM
6024 && (newh->root.type == bfd_link_hash_defined
6025 || newh->root.type == bfd_link_hash_defweak))
6026 d->symver = 1;
6027 }
6028
6029 /* Attach all the symbols to their version information. */
5a580b3a 6030 asvinfo.info = info;
5a580b3a
AM
6031 asvinfo.failed = FALSE;
6032
6033 elf_link_hash_traverse (elf_hash_table (info),
6034 _bfd_elf_link_assign_sym_version,
6035 &asvinfo);
6036 if (asvinfo.failed)
6037 return FALSE;
6038
6039 if (!info->allow_undefined_version)
6040 {
6041 /* Check if all global versions have a definition. */
6042 all_defined = TRUE;
fd91d419 6043 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6044 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6045 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
6046 {
6047 (*_bfd_error_handler)
6048 (_("%s: undefined version: %s"),
6049 d->pattern, t->name);
6050 all_defined = FALSE;
6051 }
6052
6053 if (!all_defined)
6054 {
6055 bfd_set_error (bfd_error_bad_value);
6056 return FALSE;
6057 }
6058 }
6059
6060 /* Find all symbols which were defined in a dynamic object and make
6061 the backend pick a reasonable value for them. */
6062 elf_link_hash_traverse (elf_hash_table (info),
6063 _bfd_elf_adjust_dynamic_symbol,
6064 &eif);
6065 if (eif.failed)
6066 return FALSE;
6067
6068 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 6069 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
6070 now so that we know the final size of the .dynamic section. */
6071
6072 /* If there are initialization and/or finalization functions to
6073 call then add the corresponding DT_INIT/DT_FINI entries. */
6074 h = (info->init_function
6075 ? elf_link_hash_lookup (elf_hash_table (info),
6076 info->init_function, FALSE,
6077 FALSE, FALSE)
6078 : NULL);
6079 if (h != NULL
f5385ebf
AM
6080 && (h->ref_regular
6081 || h->def_regular))
5a580b3a
AM
6082 {
6083 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6084 return FALSE;
6085 }
6086 h = (info->fini_function
6087 ? elf_link_hash_lookup (elf_hash_table (info),
6088 info->fini_function, FALSE,
6089 FALSE, FALSE)
6090 : NULL);
6091 if (h != NULL
f5385ebf
AM
6092 && (h->ref_regular
6093 || h->def_regular))
5a580b3a
AM
6094 {
6095 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6096 return FALSE;
6097 }
6098
046183de
AM
6099 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6100 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6101 {
6102 /* DT_PREINIT_ARRAY is not allowed in shared library. */
0e1862bb 6103 if (! bfd_link_executable (info))
5a580b3a
AM
6104 {
6105 bfd *sub;
6106 asection *o;
6107
6108 for (sub = info->input_bfds; sub != NULL;
c72f2fb2 6109 sub = sub->link.next)
3fcd97f1
JJ
6110 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6111 for (o = sub->sections; o != NULL; o = o->next)
6112 if (elf_section_data (o)->this_hdr.sh_type
6113 == SHT_PREINIT_ARRAY)
6114 {
6115 (*_bfd_error_handler)
6116 (_("%B: .preinit_array section is not allowed in DSO"),
6117 sub);
6118 break;
6119 }
5a580b3a
AM
6120
6121 bfd_set_error (bfd_error_nonrepresentable_section);
6122 return FALSE;
6123 }
6124
6125 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6126 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6127 return FALSE;
6128 }
046183de
AM
6129 s = bfd_get_section_by_name (output_bfd, ".init_array");
6130 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6131 {
6132 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6133 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6134 return FALSE;
6135 }
046183de
AM
6136 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6137 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6138 {
6139 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6140 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6141 return FALSE;
6142 }
6143
3d4d4302 6144 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6145 /* If .dynstr is excluded from the link, we don't want any of
6146 these tags. Strictly, we should be checking each section
6147 individually; This quick check covers for the case where
6148 someone does a /DISCARD/ : { *(*) }. */
6149 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6150 {
6151 bfd_size_type strsize;
6152
6153 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
6154 if ((info->emit_hash
6155 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6156 || (info->emit_gnu_hash
6157 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
6158 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6159 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6160 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6161 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6162 bed->s->sizeof_sym))
6163 return FALSE;
6164 }
6165 }
6166
de231f20
CM
6167 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6168 return FALSE;
6169
5a580b3a
AM
6170 /* The backend must work out the sizes of all the other dynamic
6171 sections. */
9a2a56cc
AM
6172 if (dynobj != NULL
6173 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
6174 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6175 return FALSE;
6176
9a2a56cc 6177 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6178 {
554220db 6179 unsigned long section_sym_count;
fd91d419 6180 struct bfd_elf_version_tree *verdefs;
5a580b3a 6181 asection *s;
5a580b3a
AM
6182
6183 /* Set up the version definition section. */
3d4d4302 6184 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
6185 BFD_ASSERT (s != NULL);
6186
6187 /* We may have created additional version definitions if we are
6188 just linking a regular application. */
fd91d419 6189 verdefs = info->version_info;
5a580b3a
AM
6190
6191 /* Skip anonymous version tag. */
6192 if (verdefs != NULL && verdefs->vernum == 0)
6193 verdefs = verdefs->next;
6194
3e3b46e5 6195 if (verdefs == NULL && !info->create_default_symver)
8423293d 6196 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6197 else
6198 {
6199 unsigned int cdefs;
6200 bfd_size_type size;
6201 struct bfd_elf_version_tree *t;
6202 bfd_byte *p;
6203 Elf_Internal_Verdef def;
6204 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
6205 struct bfd_link_hash_entry *bh;
6206 struct elf_link_hash_entry *h;
6207 const char *name;
5a580b3a
AM
6208
6209 cdefs = 0;
6210 size = 0;
6211
6212 /* Make space for the base version. */
6213 size += sizeof (Elf_External_Verdef);
6214 size += sizeof (Elf_External_Verdaux);
6215 ++cdefs;
6216
3e3b46e5
PB
6217 /* Make space for the default version. */
6218 if (info->create_default_symver)
6219 {
6220 size += sizeof (Elf_External_Verdef);
6221 ++cdefs;
6222 }
6223
5a580b3a
AM
6224 for (t = verdefs; t != NULL; t = t->next)
6225 {
6226 struct bfd_elf_version_deps *n;
6227
a6cc6b3b
RO
6228 /* Don't emit base version twice. */
6229 if (t->vernum == 0)
6230 continue;
6231
5a580b3a
AM
6232 size += sizeof (Elf_External_Verdef);
6233 size += sizeof (Elf_External_Verdaux);
6234 ++cdefs;
6235
6236 for (n = t->deps; n != NULL; n = n->next)
6237 size += sizeof (Elf_External_Verdaux);
6238 }
6239
eea6121a 6240 s->size = size;
a50b1753 6241 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6242 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6243 return FALSE;
6244
6245 /* Fill in the version definition section. */
6246
6247 p = s->contents;
6248
6249 def.vd_version = VER_DEF_CURRENT;
6250 def.vd_flags = VER_FLG_BASE;
6251 def.vd_ndx = 1;
6252 def.vd_cnt = 1;
3e3b46e5
PB
6253 if (info->create_default_symver)
6254 {
6255 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6256 def.vd_next = sizeof (Elf_External_Verdef);
6257 }
6258 else
6259 {
6260 def.vd_aux = sizeof (Elf_External_Verdef);
6261 def.vd_next = (sizeof (Elf_External_Verdef)
6262 + sizeof (Elf_External_Verdaux));
6263 }
5a580b3a 6264
ef53be89 6265 if (soname_indx != (size_t) -1)
5a580b3a
AM
6266 {
6267 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6268 soname_indx);
6269 def.vd_hash = bfd_elf_hash (soname);
6270 defaux.vda_name = soname_indx;
3e3b46e5 6271 name = soname;
5a580b3a
AM
6272 }
6273 else
6274 {
ef53be89 6275 size_t indx;
5a580b3a 6276
06084812 6277 name = lbasename (output_bfd->filename);
5a580b3a
AM
6278 def.vd_hash = bfd_elf_hash (name);
6279 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6280 name, FALSE);
ef53be89 6281 if (indx == (size_t) -1)
5a580b3a
AM
6282 return FALSE;
6283 defaux.vda_name = indx;
6284 }
6285 defaux.vda_next = 0;
6286
6287 _bfd_elf_swap_verdef_out (output_bfd, &def,
6288 (Elf_External_Verdef *) p);
6289 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6290 if (info->create_default_symver)
6291 {
6292 /* Add a symbol representing this version. */
6293 bh = NULL;
6294 if (! (_bfd_generic_link_add_one_symbol
6295 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6296 0, NULL, FALSE,
6297 get_elf_backend_data (dynobj)->collect, &bh)))
6298 return FALSE;
6299 h = (struct elf_link_hash_entry *) bh;
6300 h->non_elf = 0;
6301 h->def_regular = 1;
6302 h->type = STT_OBJECT;
6303 h->verinfo.vertree = NULL;
6304
6305 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6306 return FALSE;
6307
6308 /* Create a duplicate of the base version with the same
6309 aux block, but different flags. */
6310 def.vd_flags = 0;
6311 def.vd_ndx = 2;
6312 def.vd_aux = sizeof (Elf_External_Verdef);
6313 if (verdefs)
6314 def.vd_next = (sizeof (Elf_External_Verdef)
6315 + sizeof (Elf_External_Verdaux));
6316 else
6317 def.vd_next = 0;
6318 _bfd_elf_swap_verdef_out (output_bfd, &def,
6319 (Elf_External_Verdef *) p);
6320 p += sizeof (Elf_External_Verdef);
6321 }
5a580b3a
AM
6322 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6323 (Elf_External_Verdaux *) p);
6324 p += sizeof (Elf_External_Verdaux);
6325
6326 for (t = verdefs; t != NULL; t = t->next)
6327 {
6328 unsigned int cdeps;
6329 struct bfd_elf_version_deps *n;
5a580b3a 6330
a6cc6b3b
RO
6331 /* Don't emit the base version twice. */
6332 if (t->vernum == 0)
6333 continue;
6334
5a580b3a
AM
6335 cdeps = 0;
6336 for (n = t->deps; n != NULL; n = n->next)
6337 ++cdeps;
6338
6339 /* Add a symbol representing this version. */
6340 bh = NULL;
6341 if (! (_bfd_generic_link_add_one_symbol
6342 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6343 0, NULL, FALSE,
6344 get_elf_backend_data (dynobj)->collect, &bh)))
6345 return FALSE;
6346 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6347 h->non_elf = 0;
6348 h->def_regular = 1;
5a580b3a
AM
6349 h->type = STT_OBJECT;
6350 h->verinfo.vertree = t;
6351
c152c796 6352 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6353 return FALSE;
6354
6355 def.vd_version = VER_DEF_CURRENT;
6356 def.vd_flags = 0;
6357 if (t->globals.list == NULL
6358 && t->locals.list == NULL
6359 && ! t->used)
6360 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6361 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6362 def.vd_cnt = cdeps + 1;
6363 def.vd_hash = bfd_elf_hash (t->name);
6364 def.vd_aux = sizeof (Elf_External_Verdef);
6365 def.vd_next = 0;
a6cc6b3b
RO
6366
6367 /* If a basever node is next, it *must* be the last node in
6368 the chain, otherwise Verdef construction breaks. */
6369 if (t->next != NULL && t->next->vernum == 0)
6370 BFD_ASSERT (t->next->next == NULL);
6371
6372 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6373 def.vd_next = (sizeof (Elf_External_Verdef)
6374 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6375
6376 _bfd_elf_swap_verdef_out (output_bfd, &def,
6377 (Elf_External_Verdef *) p);
6378 p += sizeof (Elf_External_Verdef);
6379
6380 defaux.vda_name = h->dynstr_index;
6381 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6382 h->dynstr_index);
6383 defaux.vda_next = 0;
6384 if (t->deps != NULL)
6385 defaux.vda_next = sizeof (Elf_External_Verdaux);
6386 t->name_indx = defaux.vda_name;
6387
6388 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6389 (Elf_External_Verdaux *) p);
6390 p += sizeof (Elf_External_Verdaux);
6391
6392 for (n = t->deps; n != NULL; n = n->next)
6393 {
6394 if (n->version_needed == NULL)
6395 {
6396 /* This can happen if there was an error in the
6397 version script. */
6398 defaux.vda_name = 0;
6399 }
6400 else
6401 {
6402 defaux.vda_name = n->version_needed->name_indx;
6403 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6404 defaux.vda_name);
6405 }
6406 if (n->next == NULL)
6407 defaux.vda_next = 0;
6408 else
6409 defaux.vda_next = sizeof (Elf_External_Verdaux);
6410
6411 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6412 (Elf_External_Verdaux *) p);
6413 p += sizeof (Elf_External_Verdaux);
6414 }
6415 }
6416
6417 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6418 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6419 return FALSE;
6420
6421 elf_tdata (output_bfd)->cverdefs = cdefs;
6422 }
6423
6424 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6425 {
6426 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6427 return FALSE;
6428 }
6429 else if (info->flags & DF_BIND_NOW)
6430 {
6431 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6432 return FALSE;
6433 }
6434
6435 if (info->flags_1)
6436 {
0e1862bb 6437 if (bfd_link_executable (info))
5a580b3a
AM
6438 info->flags_1 &= ~ (DF_1_INITFIRST
6439 | DF_1_NODELETE
6440 | DF_1_NOOPEN);
6441 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6442 return FALSE;
6443 }
6444
6445 /* Work out the size of the version reference section. */
6446
3d4d4302 6447 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6448 BFD_ASSERT (s != NULL);
6449 {
6450 struct elf_find_verdep_info sinfo;
6451
5a580b3a
AM
6452 sinfo.info = info;
6453 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6454 if (sinfo.vers == 0)
6455 sinfo.vers = 1;
6456 sinfo.failed = FALSE;
6457
6458 elf_link_hash_traverse (elf_hash_table (info),
6459 _bfd_elf_link_find_version_dependencies,
6460 &sinfo);
14b1c01e
AM
6461 if (sinfo.failed)
6462 return FALSE;
5a580b3a
AM
6463
6464 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6465 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6466 else
6467 {
6468 Elf_Internal_Verneed *t;
6469 unsigned int size;
6470 unsigned int crefs;
6471 bfd_byte *p;
6472
a6cc6b3b 6473 /* Build the version dependency section. */
5a580b3a
AM
6474 size = 0;
6475 crefs = 0;
6476 for (t = elf_tdata (output_bfd)->verref;
6477 t != NULL;
6478 t = t->vn_nextref)
6479 {
6480 Elf_Internal_Vernaux *a;
6481
6482 size += sizeof (Elf_External_Verneed);
6483 ++crefs;
6484 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6485 size += sizeof (Elf_External_Vernaux);
6486 }
6487
eea6121a 6488 s->size = size;
a50b1753 6489 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6490 if (s->contents == NULL)
6491 return FALSE;
6492
6493 p = s->contents;
6494 for (t = elf_tdata (output_bfd)->verref;
6495 t != NULL;
6496 t = t->vn_nextref)
6497 {
6498 unsigned int caux;
6499 Elf_Internal_Vernaux *a;
ef53be89 6500 size_t indx;
5a580b3a
AM
6501
6502 caux = 0;
6503 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6504 ++caux;
6505
6506 t->vn_version = VER_NEED_CURRENT;
6507 t->vn_cnt = caux;
6508 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6509 elf_dt_name (t->vn_bfd) != NULL
6510 ? elf_dt_name (t->vn_bfd)
06084812 6511 : lbasename (t->vn_bfd->filename),
5a580b3a 6512 FALSE);
ef53be89 6513 if (indx == (size_t) -1)
5a580b3a
AM
6514 return FALSE;
6515 t->vn_file = indx;
6516 t->vn_aux = sizeof (Elf_External_Verneed);
6517 if (t->vn_nextref == NULL)
6518 t->vn_next = 0;
6519 else
6520 t->vn_next = (sizeof (Elf_External_Verneed)
6521 + caux * sizeof (Elf_External_Vernaux));
6522
6523 _bfd_elf_swap_verneed_out (output_bfd, t,
6524 (Elf_External_Verneed *) p);
6525 p += sizeof (Elf_External_Verneed);
6526
6527 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6528 {
6529 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6530 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6531 a->vna_nodename, FALSE);
ef53be89 6532 if (indx == (size_t) -1)
5a580b3a
AM
6533 return FALSE;
6534 a->vna_name = indx;
6535 if (a->vna_nextptr == NULL)
6536 a->vna_next = 0;
6537 else
6538 a->vna_next = sizeof (Elf_External_Vernaux);
6539
6540 _bfd_elf_swap_vernaux_out (output_bfd, a,
6541 (Elf_External_Vernaux *) p);
6542 p += sizeof (Elf_External_Vernaux);
6543 }
6544 }
6545
6546 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6547 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6548 return FALSE;
6549
6550 elf_tdata (output_bfd)->cverrefs = crefs;
6551 }
6552 }
6553
8423293d
AM
6554 if ((elf_tdata (output_bfd)->cverrefs == 0
6555 && elf_tdata (output_bfd)->cverdefs == 0)
6556 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6557 &section_sym_count) == 0)
6558 {
3d4d4302 6559 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6560 s->flags |= SEC_EXCLUDE;
6561 }
6562 }
6563 return TRUE;
6564}
6565
74541ad4
AM
6566/* Find the first non-excluded output section. We'll use its
6567 section symbol for some emitted relocs. */
6568void
6569_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6570{
6571 asection *s;
6572
6573 for (s = output_bfd->sections; s != NULL; s = s->next)
6574 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6575 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6576 {
6577 elf_hash_table (info)->text_index_section = s;
6578 break;
6579 }
6580}
6581
6582/* Find two non-excluded output sections, one for code, one for data.
6583 We'll use their section symbols for some emitted relocs. */
6584void
6585_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6586{
6587 asection *s;
6588
266b05cf
DJ
6589 /* Data first, since setting text_index_section changes
6590 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6591 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6592 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6593 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6594 {
266b05cf 6595 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6596 break;
6597 }
6598
6599 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6600 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6601 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6602 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6603 {
266b05cf 6604 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6605 break;
6606 }
6607
6608 if (elf_hash_table (info)->text_index_section == NULL)
6609 elf_hash_table (info)->text_index_section
6610 = elf_hash_table (info)->data_index_section;
6611}
6612
8423293d
AM
6613bfd_boolean
6614bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6615{
74541ad4
AM
6616 const struct elf_backend_data *bed;
6617
8423293d
AM
6618 if (!is_elf_hash_table (info->hash))
6619 return TRUE;
6620
74541ad4
AM
6621 bed = get_elf_backend_data (output_bfd);
6622 (*bed->elf_backend_init_index_section) (output_bfd, info);
6623
8423293d
AM
6624 if (elf_hash_table (info)->dynamic_sections_created)
6625 {
6626 bfd *dynobj;
8423293d
AM
6627 asection *s;
6628 bfd_size_type dynsymcount;
6629 unsigned long section_sym_count;
8423293d
AM
6630 unsigned int dtagcount;
6631
6632 dynobj = elf_hash_table (info)->dynobj;
6633
5a580b3a
AM
6634 /* Assign dynsym indicies. In a shared library we generate a
6635 section symbol for each output section, which come first.
6636 Next come all of the back-end allocated local dynamic syms,
6637 followed by the rest of the global symbols. */
6638
554220db
AM
6639 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6640 &section_sym_count);
5a580b3a
AM
6641
6642 /* Work out the size of the symbol version section. */
3d4d4302 6643 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6644 BFD_ASSERT (s != NULL);
d5486c43 6645 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6646 {
eea6121a 6647 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6648 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6649 if (s->contents == NULL)
6650 return FALSE;
6651
6652 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6653 return FALSE;
6654 }
6655
6656 /* Set the size of the .dynsym and .hash sections. We counted
6657 the number of dynamic symbols in elf_link_add_object_symbols.
6658 We will build the contents of .dynsym and .hash when we build
6659 the final symbol table, because until then we do not know the
6660 correct value to give the symbols. We built the .dynstr
6661 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6662 s = elf_hash_table (info)->dynsym;
5a580b3a 6663 BFD_ASSERT (s != NULL);
eea6121a 6664 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6665
d5486c43
L
6666 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6667 if (s->contents == NULL)
6668 return FALSE;
5a580b3a 6669
d5486c43
L
6670 /* The first entry in .dynsym is a dummy symbol. Clear all the
6671 section syms, in case we don't output them all. */
6672 ++section_sym_count;
6673 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6674
fdc90cb4
JJ
6675 elf_hash_table (info)->bucketcount = 0;
6676
5a580b3a
AM
6677 /* Compute the size of the hashing table. As a side effect this
6678 computes the hash values for all the names we export. */
fdc90cb4
JJ
6679 if (info->emit_hash)
6680 {
6681 unsigned long int *hashcodes;
14b1c01e 6682 struct hash_codes_info hashinf;
fdc90cb4
JJ
6683 bfd_size_type amt;
6684 unsigned long int nsyms;
6685 size_t bucketcount;
6686 size_t hash_entry_size;
6687
6688 /* Compute the hash values for all exported symbols. At the same
6689 time store the values in an array so that we could use them for
6690 optimizations. */
6691 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6692 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6693 if (hashcodes == NULL)
6694 return FALSE;
14b1c01e
AM
6695 hashinf.hashcodes = hashcodes;
6696 hashinf.error = FALSE;
5a580b3a 6697
fdc90cb4
JJ
6698 /* Put all hash values in HASHCODES. */
6699 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6700 elf_collect_hash_codes, &hashinf);
6701 if (hashinf.error)
4dd07732
AM
6702 {
6703 free (hashcodes);
6704 return FALSE;
6705 }
5a580b3a 6706
14b1c01e 6707 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6708 bucketcount
6709 = compute_bucket_count (info, hashcodes, nsyms, 0);
6710 free (hashcodes);
6711
6712 if (bucketcount == 0)
6713 return FALSE;
5a580b3a 6714
fdc90cb4
JJ
6715 elf_hash_table (info)->bucketcount = bucketcount;
6716
3d4d4302 6717 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6718 BFD_ASSERT (s != NULL);
6719 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6720 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6721 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6722 if (s->contents == NULL)
6723 return FALSE;
6724
6725 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6726 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6727 s->contents + hash_entry_size);
6728 }
6729
6730 if (info->emit_gnu_hash)
6731 {
6732 size_t i, cnt;
6733 unsigned char *contents;
6734 struct collect_gnu_hash_codes cinfo;
6735 bfd_size_type amt;
6736 size_t bucketcount;
6737
6738 memset (&cinfo, 0, sizeof (cinfo));
6739
6740 /* Compute the hash values for all exported symbols. At the same
6741 time store the values in an array so that we could use them for
6742 optimizations. */
6743 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6744 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6745 if (cinfo.hashcodes == NULL)
6746 return FALSE;
6747
6748 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6749 cinfo.min_dynindx = -1;
6750 cinfo.output_bfd = output_bfd;
6751 cinfo.bed = bed;
6752
6753 /* Put all hash values in HASHCODES. */
6754 elf_link_hash_traverse (elf_hash_table (info),
6755 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6756 if (cinfo.error)
4dd07732
AM
6757 {
6758 free (cinfo.hashcodes);
6759 return FALSE;
6760 }
fdc90cb4
JJ
6761
6762 bucketcount
6763 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6764
6765 if (bucketcount == 0)
6766 {
6767 free (cinfo.hashcodes);
6768 return FALSE;
6769 }
6770
3d4d4302 6771 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6772 BFD_ASSERT (s != NULL);
6773
6774 if (cinfo.nsyms == 0)
6775 {
6776 /* Empty .gnu.hash section is special. */
6777 BFD_ASSERT (cinfo.min_dynindx == -1);
6778 free (cinfo.hashcodes);
6779 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6780 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6781 if (contents == NULL)
6782 return FALSE;
6783 s->contents = contents;
6784 /* 1 empty bucket. */
6785 bfd_put_32 (output_bfd, 1, contents);
6786 /* SYMIDX above the special symbol 0. */
6787 bfd_put_32 (output_bfd, 1, contents + 4);
6788 /* Just one word for bitmask. */
6789 bfd_put_32 (output_bfd, 1, contents + 8);
6790 /* Only hash fn bloom filter. */
6791 bfd_put_32 (output_bfd, 0, contents + 12);
6792 /* No hashes are valid - empty bitmask. */
6793 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6794 /* No hashes in the only bucket. */
6795 bfd_put_32 (output_bfd, 0,
6796 contents + 16 + bed->s->arch_size / 8);
6797 }
6798 else
6799 {
9e6619e2 6800 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6801 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6802
9e6619e2
AM
6803 x = cinfo.nsyms;
6804 maskbitslog2 = 1;
6805 while ((x >>= 1) != 0)
6806 ++maskbitslog2;
fdc90cb4
JJ
6807 if (maskbitslog2 < 3)
6808 maskbitslog2 = 5;
6809 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6810 maskbitslog2 = maskbitslog2 + 3;
6811 else
6812 maskbitslog2 = maskbitslog2 + 2;
6813 if (bed->s->arch_size == 64)
6814 {
6815 if (maskbitslog2 == 5)
6816 maskbitslog2 = 6;
6817 cinfo.shift1 = 6;
6818 }
6819 else
6820 cinfo.shift1 = 5;
6821 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6822 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6823 cinfo.maskbits = 1 << maskbitslog2;
6824 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6825 amt = bucketcount * sizeof (unsigned long int) * 2;
6826 amt += maskwords * sizeof (bfd_vma);
a50b1753 6827 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6828 if (cinfo.bitmask == NULL)
6829 {
6830 free (cinfo.hashcodes);
6831 return FALSE;
6832 }
6833
a50b1753 6834 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6835 cinfo.indx = cinfo.counts + bucketcount;
6836 cinfo.symindx = dynsymcount - cinfo.nsyms;
6837 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6838
6839 /* Determine how often each hash bucket is used. */
6840 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6841 for (i = 0; i < cinfo.nsyms; ++i)
6842 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6843
6844 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6845 if (cinfo.counts[i] != 0)
6846 {
6847 cinfo.indx[i] = cnt;
6848 cnt += cinfo.counts[i];
6849 }
6850 BFD_ASSERT (cnt == dynsymcount);
6851 cinfo.bucketcount = bucketcount;
6852 cinfo.local_indx = cinfo.min_dynindx;
6853
6854 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6855 s->size += cinfo.maskbits / 8;
a50b1753 6856 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6857 if (contents == NULL)
6858 {
6859 free (cinfo.bitmask);
6860 free (cinfo.hashcodes);
6861 return FALSE;
6862 }
6863
6864 s->contents = contents;
6865 bfd_put_32 (output_bfd, bucketcount, contents);
6866 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6867 bfd_put_32 (output_bfd, maskwords, contents + 8);
6868 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6869 contents += 16 + cinfo.maskbits / 8;
6870
6871 for (i = 0; i < bucketcount; ++i)
6872 {
6873 if (cinfo.counts[i] == 0)
6874 bfd_put_32 (output_bfd, 0, contents);
6875 else
6876 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6877 contents += 4;
6878 }
6879
6880 cinfo.contents = contents;
6881
6882 /* Renumber dynamic symbols, populate .gnu.hash section. */
6883 elf_link_hash_traverse (elf_hash_table (info),
6884 elf_renumber_gnu_hash_syms, &cinfo);
6885
6886 contents = s->contents + 16;
6887 for (i = 0; i < maskwords; ++i)
6888 {
6889 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6890 contents);
6891 contents += bed->s->arch_size / 8;
6892 }
6893
6894 free (cinfo.bitmask);
6895 free (cinfo.hashcodes);
6896 }
6897 }
5a580b3a 6898
3d4d4302 6899 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6900 BFD_ASSERT (s != NULL);
6901
4ad4eba5 6902 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6903
eea6121a 6904 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6905
6906 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6907 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6908 return FALSE;
6909 }
6910
6911 return TRUE;
6912}
4d269e42 6913\f
4d269e42
AM
6914/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6915
6916static void
6917merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6918 asection *sec)
6919{
dbaa2011
AM
6920 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6921 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
6922}
6923
6924/* Finish SHF_MERGE section merging. */
6925
6926bfd_boolean
630993ec 6927_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
6928{
6929 bfd *ibfd;
6930 asection *sec;
6931
6932 if (!is_elf_hash_table (info->hash))
6933 return FALSE;
6934
c72f2fb2 6935 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
6936 if ((ibfd->flags & DYNAMIC) == 0
6937 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
6938 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6939 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
6940 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6941 if ((sec->flags & SEC_MERGE) != 0
6942 && !bfd_is_abs_section (sec->output_section))
6943 {
6944 struct bfd_elf_section_data *secdata;
6945
6946 secdata = elf_section_data (sec);
630993ec 6947 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
6948 &elf_hash_table (info)->merge_info,
6949 sec, &secdata->sec_info))
6950 return FALSE;
6951 else if (secdata->sec_info)
dbaa2011 6952 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
6953 }
6954
6955 if (elf_hash_table (info)->merge_info != NULL)
630993ec 6956 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
6957 merge_sections_remove_hook);
6958 return TRUE;
6959}
6960
6961/* Create an entry in an ELF linker hash table. */
6962
6963struct bfd_hash_entry *
6964_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6965 struct bfd_hash_table *table,
6966 const char *string)
6967{
6968 /* Allocate the structure if it has not already been allocated by a
6969 subclass. */
6970 if (entry == NULL)
6971 {
a50b1753 6972 entry = (struct bfd_hash_entry *)
ca4be51c 6973 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6974 if (entry == NULL)
6975 return entry;
6976 }
6977
6978 /* Call the allocation method of the superclass. */
6979 entry = _bfd_link_hash_newfunc (entry, table, string);
6980 if (entry != NULL)
6981 {
6982 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6983 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6984
6985 /* Set local fields. */
6986 ret->indx = -1;
6987 ret->dynindx = -1;
6988 ret->got = htab->init_got_refcount;
6989 ret->plt = htab->init_plt_refcount;
6990 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6991 - offsetof (struct elf_link_hash_entry, size)));
6992 /* Assume that we have been called by a non-ELF symbol reader.
6993 This flag is then reset by the code which reads an ELF input
6994 file. This ensures that a symbol created by a non-ELF symbol
6995 reader will have the flag set correctly. */
6996 ret->non_elf = 1;
6997 }
6998
6999 return entry;
7000}
7001
7002/* Copy data from an indirect symbol to its direct symbol, hiding the
7003 old indirect symbol. Also used for copying flags to a weakdef. */
7004
7005void
7006_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7007 struct elf_link_hash_entry *dir,
7008 struct elf_link_hash_entry *ind)
7009{
7010 struct elf_link_hash_table *htab;
7011
7012 /* Copy down any references that we may have already seen to the
6e33951e
L
7013 symbol which just became indirect if DIR isn't a hidden versioned
7014 symbol. */
4d269e42 7015
422f1182 7016 if (dir->versioned != versioned_hidden)
6e33951e
L
7017 {
7018 dir->ref_dynamic |= ind->ref_dynamic;
7019 dir->ref_regular |= ind->ref_regular;
7020 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7021 dir->non_got_ref |= ind->non_got_ref;
7022 dir->needs_plt |= ind->needs_plt;
7023 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7024 }
4d269e42
AM
7025
7026 if (ind->root.type != bfd_link_hash_indirect)
7027 return;
7028
7029 /* Copy over the global and procedure linkage table refcount entries.
7030 These may have been already set up by a check_relocs routine. */
7031 htab = elf_hash_table (info);
7032 if (ind->got.refcount > htab->init_got_refcount.refcount)
7033 {
7034 if (dir->got.refcount < 0)
7035 dir->got.refcount = 0;
7036 dir->got.refcount += ind->got.refcount;
7037 ind->got.refcount = htab->init_got_refcount.refcount;
7038 }
7039
7040 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7041 {
7042 if (dir->plt.refcount < 0)
7043 dir->plt.refcount = 0;
7044 dir->plt.refcount += ind->plt.refcount;
7045 ind->plt.refcount = htab->init_plt_refcount.refcount;
7046 }
7047
7048 if (ind->dynindx != -1)
7049 {
7050 if (dir->dynindx != -1)
7051 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7052 dir->dynindx = ind->dynindx;
7053 dir->dynstr_index = ind->dynstr_index;
7054 ind->dynindx = -1;
7055 ind->dynstr_index = 0;
7056 }
7057}
7058
7059void
7060_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7061 struct elf_link_hash_entry *h,
7062 bfd_boolean force_local)
7063{
3aa14d16
L
7064 /* STT_GNU_IFUNC symbol must go through PLT. */
7065 if (h->type != STT_GNU_IFUNC)
7066 {
7067 h->plt = elf_hash_table (info)->init_plt_offset;
7068 h->needs_plt = 0;
7069 }
4d269e42
AM
7070 if (force_local)
7071 {
7072 h->forced_local = 1;
7073 if (h->dynindx != -1)
7074 {
7075 h->dynindx = -1;
7076 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7077 h->dynstr_index);
7078 }
7079 }
7080}
7081
7bf52ea2
AM
7082/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7083 caller. */
4d269e42
AM
7084
7085bfd_boolean
7086_bfd_elf_link_hash_table_init
7087 (struct elf_link_hash_table *table,
7088 bfd *abfd,
7089 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7090 struct bfd_hash_table *,
7091 const char *),
4dfe6ac6
NC
7092 unsigned int entsize,
7093 enum elf_target_id target_id)
4d269e42
AM
7094{
7095 bfd_boolean ret;
7096 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7097
4d269e42
AM
7098 table->init_got_refcount.refcount = can_refcount - 1;
7099 table->init_plt_refcount.refcount = can_refcount - 1;
7100 table->init_got_offset.offset = -(bfd_vma) 1;
7101 table->init_plt_offset.offset = -(bfd_vma) 1;
7102 /* The first dynamic symbol is a dummy. */
7103 table->dynsymcount = 1;
7104
7105 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7106
4d269e42 7107 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7108 table->hash_table_id = target_id;
4d269e42
AM
7109
7110 return ret;
7111}
7112
7113/* Create an ELF linker hash table. */
7114
7115struct bfd_link_hash_table *
7116_bfd_elf_link_hash_table_create (bfd *abfd)
7117{
7118 struct elf_link_hash_table *ret;
7119 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7120
7bf52ea2 7121 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7122 if (ret == NULL)
7123 return NULL;
7124
7125 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7126 sizeof (struct elf_link_hash_entry),
7127 GENERIC_ELF_DATA))
4d269e42
AM
7128 {
7129 free (ret);
7130 return NULL;
7131 }
d495ab0d 7132 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7133
7134 return &ret->root;
7135}
7136
9f7c3e5e
AM
7137/* Destroy an ELF linker hash table. */
7138
7139void
d495ab0d 7140_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7141{
d495ab0d
AM
7142 struct elf_link_hash_table *htab;
7143
7144 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7145 if (htab->dynstr != NULL)
7146 _bfd_elf_strtab_free (htab->dynstr);
7147 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7148 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7149}
7150
4d269e42
AM
7151/* This is a hook for the ELF emulation code in the generic linker to
7152 tell the backend linker what file name to use for the DT_NEEDED
7153 entry for a dynamic object. */
7154
7155void
7156bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7157{
7158 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7159 && bfd_get_format (abfd) == bfd_object)
7160 elf_dt_name (abfd) = name;
7161}
7162
7163int
7164bfd_elf_get_dyn_lib_class (bfd *abfd)
7165{
7166 int lib_class;
7167 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7168 && bfd_get_format (abfd) == bfd_object)
7169 lib_class = elf_dyn_lib_class (abfd);
7170 else
7171 lib_class = 0;
7172 return lib_class;
7173}
7174
7175void
7176bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7177{
7178 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7179 && bfd_get_format (abfd) == bfd_object)
7180 elf_dyn_lib_class (abfd) = lib_class;
7181}
7182
7183/* Get the list of DT_NEEDED entries for a link. This is a hook for
7184 the linker ELF emulation code. */
7185
7186struct bfd_link_needed_list *
7187bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7188 struct bfd_link_info *info)
7189{
7190 if (! is_elf_hash_table (info->hash))
7191 return NULL;
7192 return elf_hash_table (info)->needed;
7193}
7194
7195/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7196 hook for the linker ELF emulation code. */
7197
7198struct bfd_link_needed_list *
7199bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7200 struct bfd_link_info *info)
7201{
7202 if (! is_elf_hash_table (info->hash))
7203 return NULL;
7204 return elf_hash_table (info)->runpath;
7205}
7206
7207/* Get the name actually used for a dynamic object for a link. This
7208 is the SONAME entry if there is one. Otherwise, it is the string
7209 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7210
7211const char *
7212bfd_elf_get_dt_soname (bfd *abfd)
7213{
7214 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7215 && bfd_get_format (abfd) == bfd_object)
7216 return elf_dt_name (abfd);
7217 return NULL;
7218}
7219
7220/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7221 the ELF linker emulation code. */
7222
7223bfd_boolean
7224bfd_elf_get_bfd_needed_list (bfd *abfd,
7225 struct bfd_link_needed_list **pneeded)
7226{
7227 asection *s;
7228 bfd_byte *dynbuf = NULL;
cb33740c 7229 unsigned int elfsec;
4d269e42
AM
7230 unsigned long shlink;
7231 bfd_byte *extdyn, *extdynend;
7232 size_t extdynsize;
7233 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7234
7235 *pneeded = NULL;
7236
7237 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7238 || bfd_get_format (abfd) != bfd_object)
7239 return TRUE;
7240
7241 s = bfd_get_section_by_name (abfd, ".dynamic");
7242 if (s == NULL || s->size == 0)
7243 return TRUE;
7244
7245 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7246 goto error_return;
7247
7248 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7249 if (elfsec == SHN_BAD)
4d269e42
AM
7250 goto error_return;
7251
7252 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7253
4d269e42
AM
7254 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7255 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7256
7257 extdyn = dynbuf;
7258 extdynend = extdyn + s->size;
7259 for (; extdyn < extdynend; extdyn += extdynsize)
7260 {
7261 Elf_Internal_Dyn dyn;
7262
7263 (*swap_dyn_in) (abfd, extdyn, &dyn);
7264
7265 if (dyn.d_tag == DT_NULL)
7266 break;
7267
7268 if (dyn.d_tag == DT_NEEDED)
7269 {
7270 const char *string;
7271 struct bfd_link_needed_list *l;
7272 unsigned int tagv = dyn.d_un.d_val;
7273 bfd_size_type amt;
7274
7275 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7276 if (string == NULL)
7277 goto error_return;
7278
7279 amt = sizeof *l;
a50b1753 7280 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7281 if (l == NULL)
7282 goto error_return;
7283
7284 l->by = abfd;
7285 l->name = string;
7286 l->next = *pneeded;
7287 *pneeded = l;
7288 }
7289 }
7290
7291 free (dynbuf);
7292
7293 return TRUE;
7294
7295 error_return:
7296 if (dynbuf != NULL)
7297 free (dynbuf);
7298 return FALSE;
7299}
7300
7301struct elf_symbuf_symbol
7302{
7303 unsigned long st_name; /* Symbol name, index in string tbl */
7304 unsigned char st_info; /* Type and binding attributes */
7305 unsigned char st_other; /* Visibilty, and target specific */
7306};
7307
7308struct elf_symbuf_head
7309{
7310 struct elf_symbuf_symbol *ssym;
ef53be89 7311 size_t count;
4d269e42
AM
7312 unsigned int st_shndx;
7313};
7314
7315struct elf_symbol
7316{
7317 union
7318 {
7319 Elf_Internal_Sym *isym;
7320 struct elf_symbuf_symbol *ssym;
7321 } u;
7322 const char *name;
7323};
7324
7325/* Sort references to symbols by ascending section number. */
7326
7327static int
7328elf_sort_elf_symbol (const void *arg1, const void *arg2)
7329{
7330 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7331 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7332
7333 return s1->st_shndx - s2->st_shndx;
7334}
7335
7336static int
7337elf_sym_name_compare (const void *arg1, const void *arg2)
7338{
7339 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7340 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7341 return strcmp (s1->name, s2->name);
7342}
7343
7344static struct elf_symbuf_head *
ef53be89 7345elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7346{
14b1c01e 7347 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7348 struct elf_symbuf_symbol *ssym;
7349 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7350 size_t i, shndx_count, total_size;
4d269e42 7351
a50b1753 7352 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7353 if (indbuf == NULL)
7354 return NULL;
7355
7356 for (ind = indbuf, i = 0; i < symcount; i++)
7357 if (isymbuf[i].st_shndx != SHN_UNDEF)
7358 *ind++ = &isymbuf[i];
7359 indbufend = ind;
7360
7361 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7362 elf_sort_elf_symbol);
7363
7364 shndx_count = 0;
7365 if (indbufend > indbuf)
7366 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7367 if (ind[0]->st_shndx != ind[1]->st_shndx)
7368 shndx_count++;
7369
3ae181ee
L
7370 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7371 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7372 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7373 if (ssymbuf == NULL)
7374 {
7375 free (indbuf);
7376 return NULL;
7377 }
7378
3ae181ee 7379 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7380 ssymbuf->ssym = NULL;
7381 ssymbuf->count = shndx_count;
7382 ssymbuf->st_shndx = 0;
7383 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7384 {
7385 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7386 {
7387 ssymhead++;
7388 ssymhead->ssym = ssym;
7389 ssymhead->count = 0;
7390 ssymhead->st_shndx = (*ind)->st_shndx;
7391 }
7392 ssym->st_name = (*ind)->st_name;
7393 ssym->st_info = (*ind)->st_info;
7394 ssym->st_other = (*ind)->st_other;
7395 ssymhead->count++;
7396 }
ef53be89 7397 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7398 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7399 == total_size));
4d269e42
AM
7400
7401 free (indbuf);
7402 return ssymbuf;
7403}
7404
7405/* Check if 2 sections define the same set of local and global
7406 symbols. */
7407
8f317e31 7408static bfd_boolean
4d269e42
AM
7409bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7410 struct bfd_link_info *info)
7411{
7412 bfd *bfd1, *bfd2;
7413 const struct elf_backend_data *bed1, *bed2;
7414 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7415 size_t symcount1, symcount2;
4d269e42
AM
7416 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7417 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7418 Elf_Internal_Sym *isym, *isymend;
7419 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7420 size_t count1, count2, i;
cb33740c 7421 unsigned int shndx1, shndx2;
4d269e42
AM
7422 bfd_boolean result;
7423
7424 bfd1 = sec1->owner;
7425 bfd2 = sec2->owner;
7426
4d269e42
AM
7427 /* Both sections have to be in ELF. */
7428 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7429 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7430 return FALSE;
7431
7432 if (elf_section_type (sec1) != elf_section_type (sec2))
7433 return FALSE;
7434
4d269e42
AM
7435 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7436 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7437 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7438 return FALSE;
7439
7440 bed1 = get_elf_backend_data (bfd1);
7441 bed2 = get_elf_backend_data (bfd2);
7442 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7443 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7444 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7445 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7446
7447 if (symcount1 == 0 || symcount2 == 0)
7448 return FALSE;
7449
7450 result = FALSE;
7451 isymbuf1 = NULL;
7452 isymbuf2 = NULL;
a50b1753
NC
7453 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7454 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7455
7456 if (ssymbuf1 == NULL)
7457 {
7458 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7459 NULL, NULL, NULL);
7460 if (isymbuf1 == NULL)
7461 goto done;
7462
7463 if (!info->reduce_memory_overheads)
7464 elf_tdata (bfd1)->symbuf = ssymbuf1
7465 = elf_create_symbuf (symcount1, isymbuf1);
7466 }
7467
7468 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7469 {
7470 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7471 NULL, NULL, NULL);
7472 if (isymbuf2 == NULL)
7473 goto done;
7474
7475 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7476 elf_tdata (bfd2)->symbuf = ssymbuf2
7477 = elf_create_symbuf (symcount2, isymbuf2);
7478 }
7479
7480 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7481 {
7482 /* Optimized faster version. */
ef53be89 7483 size_t lo, hi, mid;
4d269e42
AM
7484 struct elf_symbol *symp;
7485 struct elf_symbuf_symbol *ssym, *ssymend;
7486
7487 lo = 0;
7488 hi = ssymbuf1->count;
7489 ssymbuf1++;
7490 count1 = 0;
7491 while (lo < hi)
7492 {
7493 mid = (lo + hi) / 2;
cb33740c 7494 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7495 hi = mid;
cb33740c 7496 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7497 lo = mid + 1;
7498 else
7499 {
7500 count1 = ssymbuf1[mid].count;
7501 ssymbuf1 += mid;
7502 break;
7503 }
7504 }
7505
7506 lo = 0;
7507 hi = ssymbuf2->count;
7508 ssymbuf2++;
7509 count2 = 0;
7510 while (lo < hi)
7511 {
7512 mid = (lo + hi) / 2;
cb33740c 7513 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7514 hi = mid;
cb33740c 7515 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7516 lo = mid + 1;
7517 else
7518 {
7519 count2 = ssymbuf2[mid].count;
7520 ssymbuf2 += mid;
7521 break;
7522 }
7523 }
7524
7525 if (count1 == 0 || count2 == 0 || count1 != count2)
7526 goto done;
7527
ca4be51c
AM
7528 symtable1
7529 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7530 symtable2
7531 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7532 if (symtable1 == NULL || symtable2 == NULL)
7533 goto done;
7534
7535 symp = symtable1;
7536 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7537 ssym < ssymend; ssym++, symp++)
7538 {
7539 symp->u.ssym = ssym;
7540 symp->name = bfd_elf_string_from_elf_section (bfd1,
7541 hdr1->sh_link,
7542 ssym->st_name);
7543 }
7544
7545 symp = symtable2;
7546 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7547 ssym < ssymend; ssym++, symp++)
7548 {
7549 symp->u.ssym = ssym;
7550 symp->name = bfd_elf_string_from_elf_section (bfd2,
7551 hdr2->sh_link,
7552 ssym->st_name);
7553 }
7554
7555 /* Sort symbol by name. */
7556 qsort (symtable1, count1, sizeof (struct elf_symbol),
7557 elf_sym_name_compare);
7558 qsort (symtable2, count1, sizeof (struct elf_symbol),
7559 elf_sym_name_compare);
7560
7561 for (i = 0; i < count1; i++)
7562 /* Two symbols must have the same binding, type and name. */
7563 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7564 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7565 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7566 goto done;
7567
7568 result = TRUE;
7569 goto done;
7570 }
7571
a50b1753
NC
7572 symtable1 = (struct elf_symbol *)
7573 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7574 symtable2 = (struct elf_symbol *)
7575 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7576 if (symtable1 == NULL || symtable2 == NULL)
7577 goto done;
7578
7579 /* Count definitions in the section. */
7580 count1 = 0;
7581 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7582 if (isym->st_shndx == shndx1)
4d269e42
AM
7583 symtable1[count1++].u.isym = isym;
7584
7585 count2 = 0;
7586 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7587 if (isym->st_shndx == shndx2)
4d269e42
AM
7588 symtable2[count2++].u.isym = isym;
7589
7590 if (count1 == 0 || count2 == 0 || count1 != count2)
7591 goto done;
7592
7593 for (i = 0; i < count1; i++)
7594 symtable1[i].name
7595 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7596 symtable1[i].u.isym->st_name);
7597
7598 for (i = 0; i < count2; i++)
7599 symtable2[i].name
7600 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7601 symtable2[i].u.isym->st_name);
7602
7603 /* Sort symbol by name. */
7604 qsort (symtable1, count1, sizeof (struct elf_symbol),
7605 elf_sym_name_compare);
7606 qsort (symtable2, count1, sizeof (struct elf_symbol),
7607 elf_sym_name_compare);
7608
7609 for (i = 0; i < count1; i++)
7610 /* Two symbols must have the same binding, type and name. */
7611 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7612 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7613 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7614 goto done;
7615
7616 result = TRUE;
7617
7618done:
7619 if (symtable1)
7620 free (symtable1);
7621 if (symtable2)
7622 free (symtable2);
7623 if (isymbuf1)
7624 free (isymbuf1);
7625 if (isymbuf2)
7626 free (isymbuf2);
7627
7628 return result;
7629}
7630
7631/* Return TRUE if 2 section types are compatible. */
7632
7633bfd_boolean
7634_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7635 bfd *bbfd, const asection *bsec)
7636{
7637 if (asec == NULL
7638 || bsec == NULL
7639 || abfd->xvec->flavour != bfd_target_elf_flavour
7640 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7641 return TRUE;
7642
7643 return elf_section_type (asec) == elf_section_type (bsec);
7644}
7645\f
c152c796
AM
7646/* Final phase of ELF linker. */
7647
7648/* A structure we use to avoid passing large numbers of arguments. */
7649
7650struct elf_final_link_info
7651{
7652 /* General link information. */
7653 struct bfd_link_info *info;
7654 /* Output BFD. */
7655 bfd *output_bfd;
7656 /* Symbol string table. */
ef10c3ac 7657 struct elf_strtab_hash *symstrtab;
c152c796
AM
7658 /* .hash section. */
7659 asection *hash_sec;
7660 /* symbol version section (.gnu.version). */
7661 asection *symver_sec;
7662 /* Buffer large enough to hold contents of any section. */
7663 bfd_byte *contents;
7664 /* Buffer large enough to hold external relocs of any section. */
7665 void *external_relocs;
7666 /* Buffer large enough to hold internal relocs of any section. */
7667 Elf_Internal_Rela *internal_relocs;
7668 /* Buffer large enough to hold external local symbols of any input
7669 BFD. */
7670 bfd_byte *external_syms;
7671 /* And a buffer for symbol section indices. */
7672 Elf_External_Sym_Shndx *locsym_shndx;
7673 /* Buffer large enough to hold internal local symbols of any input
7674 BFD. */
7675 Elf_Internal_Sym *internal_syms;
7676 /* Array large enough to hold a symbol index for each local symbol
7677 of any input BFD. */
7678 long *indices;
7679 /* Array large enough to hold a section pointer for each local
7680 symbol of any input BFD. */
7681 asection **sections;
ef10c3ac 7682 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7683 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7684 /* Number of STT_FILE syms seen. */
7685 size_t filesym_count;
c152c796
AM
7686};
7687
7688/* This struct is used to pass information to elf_link_output_extsym. */
7689
7690struct elf_outext_info
7691{
7692 bfd_boolean failed;
7693 bfd_boolean localsyms;
34a79995 7694 bfd_boolean file_sym_done;
8b127cbc 7695 struct elf_final_link_info *flinfo;
c152c796
AM
7696};
7697
d9352518
DB
7698
7699/* Support for evaluating a complex relocation.
7700
7701 Complex relocations are generalized, self-describing relocations. The
7702 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7703 relocations themselves.
d9352518
DB
7704
7705 The relocations are use a reserved elf-wide relocation type code (R_RELC
7706 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7707 information (start bit, end bit, word width, etc) into the addend. This
7708 information is extracted from CGEN-generated operand tables within gas.
7709
7710 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7711 internal) representing prefix-notation expressions, including but not
7712 limited to those sorts of expressions normally encoded as addends in the
7713 addend field. The symbol mangling format is:
7714
7715 <node> := <literal>
7716 | <unary-operator> ':' <node>
7717 | <binary-operator> ':' <node> ':' <node>
7718 ;
7719
7720 <literal> := 's' <digits=N> ':' <N character symbol name>
7721 | 'S' <digits=N> ':' <N character section name>
7722 | '#' <hexdigits>
7723 ;
7724
7725 <binary-operator> := as in C
7726 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7727
7728static void
a0c8462f
AM
7729set_symbol_value (bfd *bfd_with_globals,
7730 Elf_Internal_Sym *isymbuf,
7731 size_t locsymcount,
7732 size_t symidx,
7733 bfd_vma val)
d9352518 7734{
8977835c
AM
7735 struct elf_link_hash_entry **sym_hashes;
7736 struct elf_link_hash_entry *h;
7737 size_t extsymoff = locsymcount;
d9352518 7738
8977835c 7739 if (symidx < locsymcount)
d9352518 7740 {
8977835c
AM
7741 Elf_Internal_Sym *sym;
7742
7743 sym = isymbuf + symidx;
7744 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7745 {
7746 /* It is a local symbol: move it to the
7747 "absolute" section and give it a value. */
7748 sym->st_shndx = SHN_ABS;
7749 sym->st_value = val;
7750 return;
7751 }
7752 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7753 extsymoff = 0;
d9352518 7754 }
8977835c
AM
7755
7756 /* It is a global symbol: set its link type
7757 to "defined" and give it a value. */
7758
7759 sym_hashes = elf_sym_hashes (bfd_with_globals);
7760 h = sym_hashes [symidx - extsymoff];
7761 while (h->root.type == bfd_link_hash_indirect
7762 || h->root.type == bfd_link_hash_warning)
7763 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7764 h->root.type = bfd_link_hash_defined;
7765 h->root.u.def.value = val;
7766 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7767}
7768
a0c8462f
AM
7769static bfd_boolean
7770resolve_symbol (const char *name,
7771 bfd *input_bfd,
8b127cbc 7772 struct elf_final_link_info *flinfo,
a0c8462f
AM
7773 bfd_vma *result,
7774 Elf_Internal_Sym *isymbuf,
7775 size_t locsymcount)
d9352518 7776{
a0c8462f
AM
7777 Elf_Internal_Sym *sym;
7778 struct bfd_link_hash_entry *global_entry;
7779 const char *candidate = NULL;
7780 Elf_Internal_Shdr *symtab_hdr;
7781 size_t i;
7782
d9352518
DB
7783 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7784
7785 for (i = 0; i < locsymcount; ++ i)
7786 {
8977835c 7787 sym = isymbuf + i;
d9352518
DB
7788
7789 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7790 continue;
7791
7792 candidate = bfd_elf_string_from_elf_section (input_bfd,
7793 symtab_hdr->sh_link,
7794 sym->st_name);
7795#ifdef DEBUG
0f02bbd9
AM
7796 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7797 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7798#endif
7799 if (candidate && strcmp (candidate, name) == 0)
7800 {
8b127cbc 7801 asection *sec = flinfo->sections [i];
d9352518 7802
0f02bbd9
AM
7803 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7804 *result += sec->output_offset + sec->output_section->vma;
d9352518 7805#ifdef DEBUG
0f02bbd9
AM
7806 printf ("Found symbol with value %8.8lx\n",
7807 (unsigned long) *result);
d9352518
DB
7808#endif
7809 return TRUE;
7810 }
7811 }
7812
7813 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7814 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7815 FALSE, FALSE, TRUE);
d9352518
DB
7816 if (!global_entry)
7817 return FALSE;
a0c8462f 7818
d9352518
DB
7819 if (global_entry->type == bfd_link_hash_defined
7820 || global_entry->type == bfd_link_hash_defweak)
7821 {
a0c8462f
AM
7822 *result = (global_entry->u.def.value
7823 + global_entry->u.def.section->output_section->vma
7824 + global_entry->u.def.section->output_offset);
d9352518 7825#ifdef DEBUG
0f02bbd9
AM
7826 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7827 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7828#endif
7829 return TRUE;
a0c8462f 7830 }
d9352518 7831
d9352518
DB
7832 return FALSE;
7833}
7834
37b01f6a
DG
7835/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7836 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7837 names like "foo.end" which is the end address of section "foo". */
7838
d9352518 7839static bfd_boolean
a0c8462f
AM
7840resolve_section (const char *name,
7841 asection *sections,
37b01f6a
DG
7842 bfd_vma *result,
7843 bfd * abfd)
d9352518 7844{
a0c8462f
AM
7845 asection *curr;
7846 unsigned int len;
d9352518 7847
a0c8462f 7848 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7849 if (strcmp (curr->name, name) == 0)
7850 {
7851 *result = curr->vma;
7852 return TRUE;
7853 }
7854
7855 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 7856 /* FIXME: This could be coded more efficiently... */
a0c8462f 7857 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7858 {
7859 len = strlen (curr->name);
a0c8462f 7860 if (len > strlen (name))
d9352518
DB
7861 continue;
7862
7863 if (strncmp (curr->name, name, len) == 0)
7864 {
7865 if (strncmp (".end", name + len, 4) == 0)
7866 {
37b01f6a 7867 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
7868 return TRUE;
7869 }
7870
7871 /* Insert more pseudo-section names here, if you like. */
7872 }
7873 }
a0c8462f 7874
d9352518
DB
7875 return FALSE;
7876}
7877
7878static void
a0c8462f 7879undefined_reference (const char *reftype, const char *name)
d9352518 7880{
a0c8462f
AM
7881 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7882 reftype, name);
d9352518
DB
7883}
7884
7885static bfd_boolean
a0c8462f
AM
7886eval_symbol (bfd_vma *result,
7887 const char **symp,
7888 bfd *input_bfd,
8b127cbc 7889 struct elf_final_link_info *flinfo,
a0c8462f
AM
7890 bfd_vma dot,
7891 Elf_Internal_Sym *isymbuf,
7892 size_t locsymcount,
7893 int signed_p)
d9352518 7894{
4b93929b
NC
7895 size_t len;
7896 size_t symlen;
a0c8462f
AM
7897 bfd_vma a;
7898 bfd_vma b;
4b93929b 7899 char symbuf[4096];
0f02bbd9 7900 const char *sym = *symp;
a0c8462f
AM
7901 const char *symend;
7902 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7903
7904 len = strlen (sym);
7905 symend = sym + len;
7906
4b93929b 7907 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7908 {
7909 bfd_set_error (bfd_error_invalid_operation);
7910 return FALSE;
7911 }
a0c8462f 7912
d9352518
DB
7913 switch (* sym)
7914 {
7915 case '.':
0f02bbd9
AM
7916 *result = dot;
7917 *symp = sym + 1;
d9352518
DB
7918 return TRUE;
7919
7920 case '#':
0f02bbd9
AM
7921 ++sym;
7922 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7923 return TRUE;
7924
7925 case 'S':
7926 symbol_is_section = TRUE;
a0c8462f 7927 case 's':
0f02bbd9
AM
7928 ++sym;
7929 symlen = strtol (sym, (char **) symp, 10);
7930 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7931
4b93929b 7932 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7933 {
7934 bfd_set_error (bfd_error_invalid_operation);
7935 return FALSE;
7936 }
7937
7938 memcpy (symbuf, sym, symlen);
a0c8462f 7939 symbuf[symlen] = '\0';
0f02bbd9 7940 *symp = sym + symlen;
a0c8462f
AM
7941
7942 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7943 the symbol as a section, or vice-versa. so we're pretty liberal in our
7944 interpretation here; section means "try section first", not "must be a
7945 section", and likewise with symbol. */
7946
a0c8462f 7947 if (symbol_is_section)
d9352518 7948 {
37b01f6a 7949 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 7950 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7951 isymbuf, locsymcount))
d9352518
DB
7952 {
7953 undefined_reference ("section", symbuf);
7954 return FALSE;
7955 }
a0c8462f
AM
7956 }
7957 else
d9352518 7958 {
8b127cbc 7959 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7960 isymbuf, locsymcount)
8b127cbc 7961 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 7962 result, input_bfd))
d9352518
DB
7963 {
7964 undefined_reference ("symbol", symbuf);
7965 return FALSE;
7966 }
7967 }
7968
7969 return TRUE;
a0c8462f 7970
d9352518
DB
7971 /* All that remains are operators. */
7972
7973#define UNARY_OP(op) \
7974 if (strncmp (sym, #op, strlen (#op)) == 0) \
7975 { \
7976 sym += strlen (#op); \
a0c8462f
AM
7977 if (*sym == ':') \
7978 ++sym; \
0f02bbd9 7979 *symp = sym; \
8b127cbc 7980 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7981 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7982 return FALSE; \
7983 if (signed_p) \
0f02bbd9 7984 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7985 else \
7986 *result = op a; \
d9352518
DB
7987 return TRUE; \
7988 }
7989
7990#define BINARY_OP(op) \
7991 if (strncmp (sym, #op, strlen (#op)) == 0) \
7992 { \
7993 sym += strlen (#op); \
a0c8462f
AM
7994 if (*sym == ':') \
7995 ++sym; \
0f02bbd9 7996 *symp = sym; \
8b127cbc 7997 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7998 isymbuf, locsymcount, signed_p)) \
a0c8462f 7999 return FALSE; \
0f02bbd9 8000 ++*symp; \
8b127cbc 8001 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8002 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8003 return FALSE; \
8004 if (signed_p) \
0f02bbd9 8005 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8006 else \
8007 *result = a op b; \
d9352518
DB
8008 return TRUE; \
8009 }
8010
8011 default:
8012 UNARY_OP (0-);
8013 BINARY_OP (<<);
8014 BINARY_OP (>>);
8015 BINARY_OP (==);
8016 BINARY_OP (!=);
8017 BINARY_OP (<=);
8018 BINARY_OP (>=);
8019 BINARY_OP (&&);
8020 BINARY_OP (||);
8021 UNARY_OP (~);
8022 UNARY_OP (!);
8023 BINARY_OP (*);
8024 BINARY_OP (/);
8025 BINARY_OP (%);
8026 BINARY_OP (^);
8027 BINARY_OP (|);
8028 BINARY_OP (&);
8029 BINARY_OP (+);
8030 BINARY_OP (-);
8031 BINARY_OP (<);
8032 BINARY_OP (>);
8033#undef UNARY_OP
8034#undef BINARY_OP
8035 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8036 bfd_set_error (bfd_error_invalid_operation);
8037 return FALSE;
8038 }
8039}
8040
d9352518 8041static void
a0c8462f
AM
8042put_value (bfd_vma size,
8043 unsigned long chunksz,
8044 bfd *input_bfd,
8045 bfd_vma x,
8046 bfd_byte *location)
d9352518
DB
8047{
8048 location += (size - chunksz);
8049
41cd1ad1 8050 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8051 {
8052 switch (chunksz)
8053 {
d9352518
DB
8054 case 1:
8055 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8056 x >>= 8;
d9352518
DB
8057 break;
8058 case 2:
8059 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8060 x >>= 16;
d9352518
DB
8061 break;
8062 case 4:
8063 bfd_put_32 (input_bfd, x, location);
65164438
NC
8064 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8065 x >>= 16;
8066 x >>= 16;
d9352518 8067 break;
d9352518 8068#ifdef BFD64
41cd1ad1 8069 case 8:
d9352518 8070 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8071 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8072 x >>= 32;
8073 x >>= 32;
8074 break;
d9352518 8075#endif
41cd1ad1
NC
8076 default:
8077 abort ();
d9352518
DB
8078 break;
8079 }
8080 }
8081}
8082
a0c8462f
AM
8083static bfd_vma
8084get_value (bfd_vma size,
8085 unsigned long chunksz,
8086 bfd *input_bfd,
8087 bfd_byte *location)
d9352518 8088{
9b239e0e 8089 int shift;
d9352518
DB
8090 bfd_vma x = 0;
8091
9b239e0e
NC
8092 /* Sanity checks. */
8093 BFD_ASSERT (chunksz <= sizeof (x)
8094 && size >= chunksz
8095 && chunksz != 0
8096 && (size % chunksz) == 0
8097 && input_bfd != NULL
8098 && location != NULL);
8099
8100 if (chunksz == sizeof (x))
8101 {
8102 BFD_ASSERT (size == chunksz);
8103
8104 /* Make sure that we do not perform an undefined shift operation.
8105 We know that size == chunksz so there will only be one iteration
8106 of the loop below. */
8107 shift = 0;
8108 }
8109 else
8110 shift = 8 * chunksz;
8111
a0c8462f 8112 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8113 {
8114 switch (chunksz)
8115 {
d9352518 8116 case 1:
9b239e0e 8117 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8118 break;
8119 case 2:
9b239e0e 8120 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8121 break;
8122 case 4:
9b239e0e 8123 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8124 break;
d9352518 8125#ifdef BFD64
9b239e0e
NC
8126 case 8:
8127 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8128 break;
9b239e0e
NC
8129#endif
8130 default:
8131 abort ();
d9352518
DB
8132 }
8133 }
8134 return x;
8135}
8136
a0c8462f
AM
8137static void
8138decode_complex_addend (unsigned long *start, /* in bits */
8139 unsigned long *oplen, /* in bits */
8140 unsigned long *len, /* in bits */
8141 unsigned long *wordsz, /* in bytes */
8142 unsigned long *chunksz, /* in bytes */
8143 unsigned long *lsb0_p,
8144 unsigned long *signed_p,
8145 unsigned long *trunc_p,
8146 unsigned long encoded)
d9352518
DB
8147{
8148 * start = encoded & 0x3F;
8149 * len = (encoded >> 6) & 0x3F;
8150 * oplen = (encoded >> 12) & 0x3F;
8151 * wordsz = (encoded >> 18) & 0xF;
8152 * chunksz = (encoded >> 22) & 0xF;
8153 * lsb0_p = (encoded >> 27) & 1;
8154 * signed_p = (encoded >> 28) & 1;
8155 * trunc_p = (encoded >> 29) & 1;
8156}
8157
cdfeee4f 8158bfd_reloc_status_type
0f02bbd9 8159bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8160 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8161 bfd_byte *contents,
8162 Elf_Internal_Rela *rel,
8163 bfd_vma relocation)
d9352518 8164{
0f02bbd9
AM
8165 bfd_vma shift, x, mask;
8166 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8167 bfd_reloc_status_type r;
d9352518
DB
8168
8169 /* Perform this reloc, since it is complex.
8170 (this is not to say that it necessarily refers to a complex
8171 symbol; merely that it is a self-describing CGEN based reloc.
8172 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8173 word size, etc) encoded within it.). */
d9352518 8174
a0c8462f
AM
8175 decode_complex_addend (&start, &oplen, &len, &wordsz,
8176 &chunksz, &lsb0_p, &signed_p,
8177 &trunc_p, rel->r_addend);
d9352518
DB
8178
8179 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8180
8181 if (lsb0_p)
8182 shift = (start + 1) - len;
8183 else
8184 shift = (8 * wordsz) - (start + len);
8185
37b01f6a
DG
8186 x = get_value (wordsz, chunksz, input_bfd,
8187 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8188
8189#ifdef DEBUG
8190 printf ("Doing complex reloc: "
8191 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8192 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8193 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8194 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8195 oplen, (unsigned long) x, (unsigned long) mask,
8196 (unsigned long) relocation);
d9352518
DB
8197#endif
8198
cdfeee4f 8199 r = bfd_reloc_ok;
d9352518 8200 if (! trunc_p)
cdfeee4f
AM
8201 /* Now do an overflow check. */
8202 r = bfd_check_overflow ((signed_p
8203 ? complain_overflow_signed
8204 : complain_overflow_unsigned),
8205 len, 0, (8 * wordsz),
8206 relocation);
a0c8462f 8207
d9352518
DB
8208 /* Do the deed. */
8209 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8210
8211#ifdef DEBUG
8212 printf (" relocation: %8.8lx\n"
8213 " shifted mask: %8.8lx\n"
8214 " shifted/masked reloc: %8.8lx\n"
8215 " result: %8.8lx\n",
9ccb8af9
AM
8216 (unsigned long) relocation, (unsigned long) (mask << shift),
8217 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8218#endif
37b01f6a
DG
8219 put_value (wordsz, chunksz, input_bfd, x,
8220 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8221 return r;
d9352518
DB
8222}
8223
0e287786
AM
8224/* Functions to read r_offset from external (target order) reloc
8225 entry. Faster than bfd_getl32 et al, because we let the compiler
8226 know the value is aligned. */
53df40a4 8227
0e287786
AM
8228static bfd_vma
8229ext32l_r_offset (const void *p)
53df40a4
AM
8230{
8231 union aligned32
8232 {
8233 uint32_t v;
8234 unsigned char c[4];
8235 };
8236 const union aligned32 *a
0e287786 8237 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8238
8239 uint32_t aval = ( (uint32_t) a->c[0]
8240 | (uint32_t) a->c[1] << 8
8241 | (uint32_t) a->c[2] << 16
8242 | (uint32_t) a->c[3] << 24);
0e287786 8243 return aval;
53df40a4
AM
8244}
8245
0e287786
AM
8246static bfd_vma
8247ext32b_r_offset (const void *p)
53df40a4
AM
8248{
8249 union aligned32
8250 {
8251 uint32_t v;
8252 unsigned char c[4];
8253 };
8254 const union aligned32 *a
0e287786 8255 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8256
8257 uint32_t aval = ( (uint32_t) a->c[0] << 24
8258 | (uint32_t) a->c[1] << 16
8259 | (uint32_t) a->c[2] << 8
8260 | (uint32_t) a->c[3]);
0e287786 8261 return aval;
53df40a4
AM
8262}
8263
8264#ifdef BFD_HOST_64_BIT
0e287786
AM
8265static bfd_vma
8266ext64l_r_offset (const void *p)
53df40a4
AM
8267{
8268 union aligned64
8269 {
8270 uint64_t v;
8271 unsigned char c[8];
8272 };
8273 const union aligned64 *a
0e287786 8274 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8275
8276 uint64_t aval = ( (uint64_t) a->c[0]
8277 | (uint64_t) a->c[1] << 8
8278 | (uint64_t) a->c[2] << 16
8279 | (uint64_t) a->c[3] << 24
8280 | (uint64_t) a->c[4] << 32
8281 | (uint64_t) a->c[5] << 40
8282 | (uint64_t) a->c[6] << 48
8283 | (uint64_t) a->c[7] << 56);
0e287786 8284 return aval;
53df40a4
AM
8285}
8286
0e287786
AM
8287static bfd_vma
8288ext64b_r_offset (const void *p)
53df40a4
AM
8289{
8290 union aligned64
8291 {
8292 uint64_t v;
8293 unsigned char c[8];
8294 };
8295 const union aligned64 *a
0e287786 8296 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8297
8298 uint64_t aval = ( (uint64_t) a->c[0] << 56
8299 | (uint64_t) a->c[1] << 48
8300 | (uint64_t) a->c[2] << 40
8301 | (uint64_t) a->c[3] << 32
8302 | (uint64_t) a->c[4] << 24
8303 | (uint64_t) a->c[5] << 16
8304 | (uint64_t) a->c[6] << 8
8305 | (uint64_t) a->c[7]);
0e287786 8306 return aval;
53df40a4
AM
8307}
8308#endif
8309
c152c796
AM
8310/* When performing a relocatable link, the input relocations are
8311 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8312 referenced must be updated. Update all the relocations found in
8313 RELDATA. */
c152c796 8314
bca6d0e3 8315static bfd_boolean
c152c796 8316elf_link_adjust_relocs (bfd *abfd,
28dbcedc
AM
8317 struct bfd_elf_section_reloc_data *reldata,
8318 bfd_boolean sort)
c152c796
AM
8319{
8320 unsigned int i;
8321 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8322 bfd_byte *erela;
8323 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8324 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8325 bfd_vma r_type_mask;
8326 int r_sym_shift;
d4730f92
BS
8327 unsigned int count = reldata->count;
8328 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8329
d4730f92 8330 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8331 {
8332 swap_in = bed->s->swap_reloc_in;
8333 swap_out = bed->s->swap_reloc_out;
8334 }
d4730f92 8335 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8336 {
8337 swap_in = bed->s->swap_reloca_in;
8338 swap_out = bed->s->swap_reloca_out;
8339 }
8340 else
8341 abort ();
8342
8343 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8344 abort ();
8345
8346 if (bed->s->arch_size == 32)
8347 {
8348 r_type_mask = 0xff;
8349 r_sym_shift = 8;
8350 }
8351 else
8352 {
8353 r_type_mask = 0xffffffff;
8354 r_sym_shift = 32;
8355 }
8356
d4730f92
BS
8357 erela = reldata->hdr->contents;
8358 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8359 {
8360 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8361 unsigned int j;
8362
8363 if (*rel_hash == NULL)
8364 continue;
8365
8366 BFD_ASSERT ((*rel_hash)->indx >= 0);
8367
8368 (*swap_in) (abfd, erela, irela);
8369 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8370 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8371 | (irela[j].r_info & r_type_mask));
8372 (*swap_out) (abfd, irela, erela);
8373 }
53df40a4 8374
0e287786 8375 if (sort && count != 0)
53df40a4 8376 {
0e287786
AM
8377 bfd_vma (*ext_r_off) (const void *);
8378 bfd_vma r_off;
8379 size_t elt_size;
8380 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8381 bfd_byte *buf = NULL;
28dbcedc
AM
8382
8383 if (bed->s->arch_size == 32)
8384 {
8385 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8386 ext_r_off = ext32l_r_offset;
28dbcedc 8387 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8388 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8389 else
8390 abort ();
8391 }
53df40a4 8392 else
28dbcedc 8393 {
53df40a4 8394#ifdef BFD_HOST_64_BIT
28dbcedc 8395 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8396 ext_r_off = ext64l_r_offset;
28dbcedc 8397 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8398 ext_r_off = ext64b_r_offset;
28dbcedc 8399 else
53df40a4 8400#endif
28dbcedc
AM
8401 abort ();
8402 }
0e287786 8403
bca6d0e3
AM
8404 /* Must use a stable sort here. A modified insertion sort,
8405 since the relocs are mostly sorted already. */
0e287786
AM
8406 elt_size = reldata->hdr->sh_entsize;
8407 base = reldata->hdr->contents;
8408 end = base + count * elt_size;
bca6d0e3 8409 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8410 abort ();
8411
8412 /* Ensure the first element is lowest. This acts as a sentinel,
8413 speeding the main loop below. */
8414 r_off = (*ext_r_off) (base);
8415 for (p = loc = base; (p += elt_size) < end; )
8416 {
8417 bfd_vma r_off2 = (*ext_r_off) (p);
8418 if (r_off > r_off2)
8419 {
8420 r_off = r_off2;
8421 loc = p;
8422 }
8423 }
8424 if (loc != base)
8425 {
8426 /* Don't just swap *base and *loc as that changes the order
8427 of the original base[0] and base[1] if they happen to
8428 have the same r_offset. */
bca6d0e3
AM
8429 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8430 memcpy (onebuf, loc, elt_size);
0e287786 8431 memmove (base + elt_size, base, loc - base);
bca6d0e3 8432 memcpy (base, onebuf, elt_size);
0e287786
AM
8433 }
8434
b29b8669 8435 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8436 {
8437 /* base to p is sorted, *p is next to insert. */
8438 r_off = (*ext_r_off) (p);
8439 /* Search the sorted region for location to insert. */
8440 loc = p - elt_size;
8441 while (r_off < (*ext_r_off) (loc))
8442 loc -= elt_size;
8443 loc += elt_size;
8444 if (loc != p)
8445 {
bca6d0e3
AM
8446 /* Chances are there is a run of relocs to insert here,
8447 from one of more input files. Files are not always
8448 linked in order due to the way elf_link_input_bfd is
8449 called. See pr17666. */
8450 size_t sortlen = p - loc;
8451 bfd_vma r_off2 = (*ext_r_off) (loc);
8452 size_t runlen = elt_size;
8453 size_t buf_size = 96 * 1024;
8454 while (p + runlen < end
8455 && (sortlen <= buf_size
8456 || runlen + elt_size <= buf_size)
8457 && r_off2 > (*ext_r_off) (p + runlen))
8458 runlen += elt_size;
8459 if (buf == NULL)
8460 {
8461 buf = bfd_malloc (buf_size);
8462 if (buf == NULL)
8463 return FALSE;
8464 }
8465 if (runlen < sortlen)
8466 {
8467 memcpy (buf, p, runlen);
8468 memmove (loc + runlen, loc, sortlen);
8469 memcpy (loc, buf, runlen);
8470 }
8471 else
8472 {
8473 memcpy (buf, loc, sortlen);
8474 memmove (loc, p, runlen);
8475 memcpy (loc + runlen, buf, sortlen);
8476 }
b29b8669 8477 p += runlen - elt_size;
0e287786
AM
8478 }
8479 }
8480 /* Hashes are no longer valid. */
28dbcedc
AM
8481 free (reldata->hashes);
8482 reldata->hashes = NULL;
bca6d0e3 8483 free (buf);
53df40a4 8484 }
bca6d0e3 8485 return TRUE;
c152c796
AM
8486}
8487
8488struct elf_link_sort_rela
8489{
8490 union {
8491 bfd_vma offset;
8492 bfd_vma sym_mask;
8493 } u;
8494 enum elf_reloc_type_class type;
8495 /* We use this as an array of size int_rels_per_ext_rel. */
8496 Elf_Internal_Rela rela[1];
8497};
8498
8499static int
8500elf_link_sort_cmp1 (const void *A, const void *B)
8501{
a50b1753
NC
8502 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8503 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8504 int relativea, relativeb;
8505
8506 relativea = a->type == reloc_class_relative;
8507 relativeb = b->type == reloc_class_relative;
8508
8509 if (relativea < relativeb)
8510 return 1;
8511 if (relativea > relativeb)
8512 return -1;
8513 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8514 return -1;
8515 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8516 return 1;
8517 if (a->rela->r_offset < b->rela->r_offset)
8518 return -1;
8519 if (a->rela->r_offset > b->rela->r_offset)
8520 return 1;
8521 return 0;
8522}
8523
8524static int
8525elf_link_sort_cmp2 (const void *A, const void *B)
8526{
a50b1753
NC
8527 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8528 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8529
7e612e98 8530 if (a->type < b->type)
c152c796 8531 return -1;
7e612e98 8532 if (a->type > b->type)
c152c796 8533 return 1;
7e612e98 8534 if (a->u.offset < b->u.offset)
c152c796 8535 return -1;
7e612e98 8536 if (a->u.offset > b->u.offset)
c152c796
AM
8537 return 1;
8538 if (a->rela->r_offset < b->rela->r_offset)
8539 return -1;
8540 if (a->rela->r_offset > b->rela->r_offset)
8541 return 1;
8542 return 0;
8543}
8544
8545static size_t
8546elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8547{
3410fea8 8548 asection *dynamic_relocs;
fc66a176
L
8549 asection *rela_dyn;
8550 asection *rel_dyn;
c152c796
AM
8551 bfd_size_type count, size;
8552 size_t i, ret, sort_elt, ext_size;
8553 bfd_byte *sort, *s_non_relative, *p;
8554 struct elf_link_sort_rela *sq;
8555 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8556 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8557 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8558 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8559 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8560 struct bfd_link_order *lo;
8561 bfd_vma r_sym_mask;
3410fea8 8562 bfd_boolean use_rela;
c152c796 8563
3410fea8
NC
8564 /* Find a dynamic reloc section. */
8565 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8566 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8567 if (rela_dyn != NULL && rela_dyn->size > 0
8568 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8569 {
3410fea8
NC
8570 bfd_boolean use_rela_initialised = FALSE;
8571
8572 /* This is just here to stop gcc from complaining.
c8e44c6d 8573 Its initialization checking code is not perfect. */
3410fea8
NC
8574 use_rela = TRUE;
8575
8576 /* Both sections are present. Examine the sizes
8577 of the indirect sections to help us choose. */
8578 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8579 if (lo->type == bfd_indirect_link_order)
8580 {
8581 asection *o = lo->u.indirect.section;
8582
8583 if ((o->size % bed->s->sizeof_rela) == 0)
8584 {
8585 if ((o->size % bed->s->sizeof_rel) == 0)
8586 /* Section size is divisible by both rel and rela sizes.
8587 It is of no help to us. */
8588 ;
8589 else
8590 {
8591 /* Section size is only divisible by rela. */
8592 if (use_rela_initialised && (use_rela == FALSE))
8593 {
c8e44c6d
AM
8594 _bfd_error_handler (_("%B: Unable to sort relocs - "
8595 "they are in more than one size"),
8596 abfd);
3410fea8
NC
8597 bfd_set_error (bfd_error_invalid_operation);
8598 return 0;
8599 }
8600 else
8601 {
8602 use_rela = TRUE;
8603 use_rela_initialised = TRUE;
8604 }
8605 }
8606 }
8607 else if ((o->size % bed->s->sizeof_rel) == 0)
8608 {
8609 /* Section size is only divisible by rel. */
8610 if (use_rela_initialised && (use_rela == TRUE))
8611 {
c8e44c6d
AM
8612 _bfd_error_handler (_("%B: Unable to sort relocs - "
8613 "they are in more than one size"),
8614 abfd);
3410fea8
NC
8615 bfd_set_error (bfd_error_invalid_operation);
8616 return 0;
8617 }
8618 else
8619 {
8620 use_rela = FALSE;
8621 use_rela_initialised = TRUE;
8622 }
8623 }
8624 else
8625 {
c8e44c6d
AM
8626 /* The section size is not divisible by either -
8627 something is wrong. */
8628 _bfd_error_handler (_("%B: Unable to sort relocs - "
8629 "they are of an unknown size"), abfd);
3410fea8
NC
8630 bfd_set_error (bfd_error_invalid_operation);
8631 return 0;
8632 }
8633 }
8634
8635 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8636 if (lo->type == bfd_indirect_link_order)
8637 {
8638 asection *o = lo->u.indirect.section;
8639
8640 if ((o->size % bed->s->sizeof_rela) == 0)
8641 {
8642 if ((o->size % bed->s->sizeof_rel) == 0)
8643 /* Section size is divisible by both rel and rela sizes.
8644 It is of no help to us. */
8645 ;
8646 else
8647 {
8648 /* Section size is only divisible by rela. */
8649 if (use_rela_initialised && (use_rela == FALSE))
8650 {
c8e44c6d
AM
8651 _bfd_error_handler (_("%B: Unable to sort relocs - "
8652 "they are in more than one size"),
8653 abfd);
3410fea8
NC
8654 bfd_set_error (bfd_error_invalid_operation);
8655 return 0;
8656 }
8657 else
8658 {
8659 use_rela = TRUE;
8660 use_rela_initialised = TRUE;
8661 }
8662 }
8663 }
8664 else if ((o->size % bed->s->sizeof_rel) == 0)
8665 {
8666 /* Section size is only divisible by rel. */
8667 if (use_rela_initialised && (use_rela == TRUE))
8668 {
c8e44c6d
AM
8669 _bfd_error_handler (_("%B: Unable to sort relocs - "
8670 "they are in more than one size"),
8671 abfd);
3410fea8
NC
8672 bfd_set_error (bfd_error_invalid_operation);
8673 return 0;
8674 }
8675 else
8676 {
8677 use_rela = FALSE;
8678 use_rela_initialised = TRUE;
8679 }
8680 }
8681 else
8682 {
c8e44c6d
AM
8683 /* The section size is not divisible by either -
8684 something is wrong. */
8685 _bfd_error_handler (_("%B: Unable to sort relocs - "
8686 "they are of an unknown size"), abfd);
3410fea8
NC
8687 bfd_set_error (bfd_error_invalid_operation);
8688 return 0;
8689 }
8690 }
8691
8692 if (! use_rela_initialised)
8693 /* Make a guess. */
8694 use_rela = TRUE;
c152c796 8695 }
fc66a176
L
8696 else if (rela_dyn != NULL && rela_dyn->size > 0)
8697 use_rela = TRUE;
8698 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8699 use_rela = FALSE;
c152c796 8700 else
fc66a176 8701 return 0;
3410fea8
NC
8702
8703 if (use_rela)
c152c796 8704 {
3410fea8 8705 dynamic_relocs = rela_dyn;
c152c796
AM
8706 ext_size = bed->s->sizeof_rela;
8707 swap_in = bed->s->swap_reloca_in;
8708 swap_out = bed->s->swap_reloca_out;
8709 }
3410fea8
NC
8710 else
8711 {
8712 dynamic_relocs = rel_dyn;
8713 ext_size = bed->s->sizeof_rel;
8714 swap_in = bed->s->swap_reloc_in;
8715 swap_out = bed->s->swap_reloc_out;
8716 }
c152c796
AM
8717
8718 size = 0;
3410fea8 8719 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8720 if (lo->type == bfd_indirect_link_order)
3410fea8 8721 size += lo->u.indirect.section->size;
c152c796 8722
3410fea8 8723 if (size != dynamic_relocs->size)
c152c796
AM
8724 return 0;
8725
8726 sort_elt = (sizeof (struct elf_link_sort_rela)
8727 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8728
8729 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8730 if (count == 0)
8731 return 0;
a50b1753 8732 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8733
c152c796
AM
8734 if (sort == NULL)
8735 {
8736 (*info->callbacks->warning)
8737 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8738 return 0;
8739 }
8740
8741 if (bed->s->arch_size == 32)
8742 r_sym_mask = ~(bfd_vma) 0xff;
8743 else
8744 r_sym_mask = ~(bfd_vma) 0xffffffff;
8745
3410fea8 8746 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8747 if (lo->type == bfd_indirect_link_order)
8748 {
8749 bfd_byte *erel, *erelend;
8750 asection *o = lo->u.indirect.section;
8751
1da212d6
AM
8752 if (o->contents == NULL && o->size != 0)
8753 {
8754 /* This is a reloc section that is being handled as a normal
8755 section. See bfd_section_from_shdr. We can't combine
8756 relocs in this case. */
8757 free (sort);
8758 return 0;
8759 }
c152c796 8760 erel = o->contents;
eea6121a 8761 erelend = o->contents + o->size;
c8e44c6d 8762 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 8763
c152c796
AM
8764 while (erel < erelend)
8765 {
8766 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8767
c152c796 8768 (*swap_in) (abfd, erel, s->rela);
7e612e98 8769 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8770 s->u.sym_mask = r_sym_mask;
8771 p += sort_elt;
8772 erel += ext_size;
8773 }
8774 }
8775
8776 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8777
8778 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8779 {
8780 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8781 if (s->type != reloc_class_relative)
8782 break;
8783 }
8784 ret = i;
8785 s_non_relative = p;
8786
8787 sq = (struct elf_link_sort_rela *) s_non_relative;
8788 for (; i < count; i++, p += sort_elt)
8789 {
8790 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8791 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8792 sq = sp;
8793 sp->u.offset = sq->rela->r_offset;
8794 }
8795
8796 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8797
c8e44c6d
AM
8798 struct elf_link_hash_table *htab = elf_hash_table (info);
8799 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8800 {
8801 /* We have plt relocs in .rela.dyn. */
8802 sq = (struct elf_link_sort_rela *) sort;
8803 for (i = 0; i < count; i++)
8804 if (sq[count - i - 1].type != reloc_class_plt)
8805 break;
8806 if (i != 0 && htab->srelplt->size == i * ext_size)
8807 {
8808 struct bfd_link_order **plo;
8809 /* Put srelplt link_order last. This is so the output_offset
8810 set in the next loop is correct for DT_JMPREL. */
8811 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8812 if ((*plo)->type == bfd_indirect_link_order
8813 && (*plo)->u.indirect.section == htab->srelplt)
8814 {
8815 lo = *plo;
8816 *plo = lo->next;
8817 }
8818 else
8819 plo = &(*plo)->next;
8820 *plo = lo;
8821 lo->next = NULL;
8822 dynamic_relocs->map_tail.link_order = lo;
8823 }
8824 }
8825
8826 p = sort;
3410fea8 8827 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8828 if (lo->type == bfd_indirect_link_order)
8829 {
8830 bfd_byte *erel, *erelend;
8831 asection *o = lo->u.indirect.section;
8832
8833 erel = o->contents;
eea6121a 8834 erelend = o->contents + o->size;
c8e44c6d 8835 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
8836 while (erel < erelend)
8837 {
8838 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8839 (*swap_out) (abfd, s->rela, erel);
8840 p += sort_elt;
8841 erel += ext_size;
8842 }
8843 }
8844
8845 free (sort);
3410fea8 8846 *psec = dynamic_relocs;
c152c796
AM
8847 return ret;
8848}
8849
ef10c3ac 8850/* Add a symbol to the output symbol string table. */
c152c796 8851
6e0b88f1 8852static int
ef10c3ac
L
8853elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8854 const char *name,
8855 Elf_Internal_Sym *elfsym,
8856 asection *input_sec,
8857 struct elf_link_hash_entry *h)
c152c796 8858{
6e0b88f1 8859 int (*output_symbol_hook)
c152c796
AM
8860 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8861 struct elf_link_hash_entry *);
ef10c3ac 8862 struct elf_link_hash_table *hash_table;
c152c796 8863 const struct elf_backend_data *bed;
ef10c3ac 8864 bfd_size_type strtabsize;
c152c796 8865
8539e4e8
AM
8866 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8867
8b127cbc 8868 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
8869 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8870 if (output_symbol_hook != NULL)
8871 {
8b127cbc 8872 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
8873 if (ret != 1)
8874 return ret;
c152c796
AM
8875 }
8876
ef10c3ac
L
8877 if (name == NULL
8878 || *name == '\0'
8879 || (input_sec->flags & SEC_EXCLUDE))
8880 elfsym->st_name = (unsigned long) -1;
c152c796
AM
8881 else
8882 {
ef10c3ac
L
8883 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8884 to get the final offset for st_name. */
8885 elfsym->st_name
8886 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8887 name, FALSE);
c152c796 8888 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8889 return 0;
c152c796
AM
8890 }
8891
ef10c3ac
L
8892 hash_table = elf_hash_table (flinfo->info);
8893 strtabsize = hash_table->strtabsize;
8894 if (strtabsize <= hash_table->strtabcount)
c152c796 8895 {
ef10c3ac
L
8896 strtabsize += strtabsize;
8897 hash_table->strtabsize = strtabsize;
8898 strtabsize *= sizeof (*hash_table->strtab);
8899 hash_table->strtab
8900 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8901 strtabsize);
8902 if (hash_table->strtab == NULL)
6e0b88f1 8903 return 0;
c152c796 8904 }
ef10c3ac
L
8905 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8906 hash_table->strtab[hash_table->strtabcount].dest_index
8907 = hash_table->strtabcount;
8908 hash_table->strtab[hash_table->strtabcount].destshndx_index
8909 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8910
8911 bfd_get_symcount (flinfo->output_bfd) += 1;
8912 hash_table->strtabcount += 1;
8913
8914 return 1;
8915}
8916
8917/* Swap symbols out to the symbol table and flush the output symbols to
8918 the file. */
8919
8920static bfd_boolean
8921elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8922{
8923 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
8924 bfd_size_type amt;
8925 size_t i;
ef10c3ac
L
8926 const struct elf_backend_data *bed;
8927 bfd_byte *symbuf;
8928 Elf_Internal_Shdr *hdr;
8929 file_ptr pos;
8930 bfd_boolean ret;
8931
8932 if (!hash_table->strtabcount)
8933 return TRUE;
8934
8935 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8936
8937 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 8938
ef10c3ac
L
8939 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8940 symbuf = (bfd_byte *) bfd_malloc (amt);
8941 if (symbuf == NULL)
8942 return FALSE;
1b786873 8943
ef10c3ac 8944 if (flinfo->symshndxbuf)
c152c796 8945 {
ef53be89
AM
8946 amt = sizeof (Elf_External_Sym_Shndx);
8947 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
8948 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8949 if (flinfo->symshndxbuf == NULL)
c152c796 8950 {
ef10c3ac
L
8951 free (symbuf);
8952 return FALSE;
c152c796 8953 }
c152c796
AM
8954 }
8955
ef10c3ac
L
8956 for (i = 0; i < hash_table->strtabcount; i++)
8957 {
8958 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8959 if (elfsym->sym.st_name == (unsigned long) -1)
8960 elfsym->sym.st_name = 0;
8961 else
8962 elfsym->sym.st_name
8963 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8964 elfsym->sym.st_name);
8965 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8966 ((bfd_byte *) symbuf
8967 + (elfsym->dest_index
8968 * bed->s->sizeof_sym)),
8969 (flinfo->symshndxbuf
8970 + elfsym->destshndx_index));
8971 }
8972
8973 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8974 pos = hdr->sh_offset + hdr->sh_size;
8975 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8976 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8977 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8978 {
8979 hdr->sh_size += amt;
8980 ret = TRUE;
8981 }
8982 else
8983 ret = FALSE;
c152c796 8984
ef10c3ac
L
8985 free (symbuf);
8986
8987 free (hash_table->strtab);
8988 hash_table->strtab = NULL;
8989
8990 return ret;
c152c796
AM
8991}
8992
c0d5a53d
L
8993/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8994
8995static bfd_boolean
8996check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8997{
4fbb74a6
AM
8998 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8999 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9000 {
9001 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9002 beyond 64k. */
c0d5a53d
L
9003 (*_bfd_error_handler)
9004 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9005 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9006 bfd_set_error (bfd_error_nonrepresentable_section);
9007 return FALSE;
9008 }
9009 return TRUE;
9010}
9011
c152c796
AM
9012/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9013 allowing an unsatisfied unversioned symbol in the DSO to match a
9014 versioned symbol that would normally require an explicit version.
9015 We also handle the case that a DSO references a hidden symbol
9016 which may be satisfied by a versioned symbol in another DSO. */
9017
9018static bfd_boolean
9019elf_link_check_versioned_symbol (struct bfd_link_info *info,
9020 const struct elf_backend_data *bed,
9021 struct elf_link_hash_entry *h)
9022{
9023 bfd *abfd;
9024 struct elf_link_loaded_list *loaded;
9025
9026 if (!is_elf_hash_table (info->hash))
9027 return FALSE;
9028
90c984fc
L
9029 /* Check indirect symbol. */
9030 while (h->root.type == bfd_link_hash_indirect)
9031 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9032
c152c796
AM
9033 switch (h->root.type)
9034 {
9035 default:
9036 abfd = NULL;
9037 break;
9038
9039 case bfd_link_hash_undefined:
9040 case bfd_link_hash_undefweak:
9041 abfd = h->root.u.undef.abfd;
9042 if ((abfd->flags & DYNAMIC) == 0
e56f61be 9043 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9044 return FALSE;
9045 break;
9046
9047 case bfd_link_hash_defined:
9048 case bfd_link_hash_defweak:
9049 abfd = h->root.u.def.section->owner;
9050 break;
9051
9052 case bfd_link_hash_common:
9053 abfd = h->root.u.c.p->section->owner;
9054 break;
9055 }
9056 BFD_ASSERT (abfd != NULL);
9057
9058 for (loaded = elf_hash_table (info)->loaded;
9059 loaded != NULL;
9060 loaded = loaded->next)
9061 {
9062 bfd *input;
9063 Elf_Internal_Shdr *hdr;
ef53be89
AM
9064 size_t symcount;
9065 size_t extsymcount;
9066 size_t extsymoff;
c152c796
AM
9067 Elf_Internal_Shdr *versymhdr;
9068 Elf_Internal_Sym *isym;
9069 Elf_Internal_Sym *isymend;
9070 Elf_Internal_Sym *isymbuf;
9071 Elf_External_Versym *ever;
9072 Elf_External_Versym *extversym;
9073
9074 input = loaded->abfd;
9075
9076 /* We check each DSO for a possible hidden versioned definition. */
9077 if (input == abfd
9078 || (input->flags & DYNAMIC) == 0
9079 || elf_dynversym (input) == 0)
9080 continue;
9081
9082 hdr = &elf_tdata (input)->dynsymtab_hdr;
9083
9084 symcount = hdr->sh_size / bed->s->sizeof_sym;
9085 if (elf_bad_symtab (input))
9086 {
9087 extsymcount = symcount;
9088 extsymoff = 0;
9089 }
9090 else
9091 {
9092 extsymcount = symcount - hdr->sh_info;
9093 extsymoff = hdr->sh_info;
9094 }
9095
9096 if (extsymcount == 0)
9097 continue;
9098
9099 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9100 NULL, NULL, NULL);
9101 if (isymbuf == NULL)
9102 return FALSE;
9103
9104 /* Read in any version definitions. */
9105 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9106 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9107 if (extversym == NULL)
9108 goto error_ret;
9109
9110 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9111 || (bfd_bread (extversym, versymhdr->sh_size, input)
9112 != versymhdr->sh_size))
9113 {
9114 free (extversym);
9115 error_ret:
9116 free (isymbuf);
9117 return FALSE;
9118 }
9119
9120 ever = extversym + extsymoff;
9121 isymend = isymbuf + extsymcount;
9122 for (isym = isymbuf; isym < isymend; isym++, ever++)
9123 {
9124 const char *name;
9125 Elf_Internal_Versym iver;
9126 unsigned short version_index;
9127
9128 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9129 || isym->st_shndx == SHN_UNDEF)
9130 continue;
9131
9132 name = bfd_elf_string_from_elf_section (input,
9133 hdr->sh_link,
9134 isym->st_name);
9135 if (strcmp (name, h->root.root.string) != 0)
9136 continue;
9137
9138 _bfd_elf_swap_versym_in (input, ever, &iver);
9139
d023c380
L
9140 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9141 && !(h->def_regular
9142 && h->forced_local))
c152c796
AM
9143 {
9144 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9145 have provided a definition for the undefined sym unless
9146 it is defined in a non-shared object and forced local.
9147 */
c152c796
AM
9148 abort ();
9149 }
9150
9151 version_index = iver.vs_vers & VERSYM_VERSION;
9152 if (version_index == 1 || version_index == 2)
9153 {
9154 /* This is the base or first version. We can use it. */
9155 free (extversym);
9156 free (isymbuf);
9157 return TRUE;
9158 }
9159 }
9160
9161 free (extversym);
9162 free (isymbuf);
9163 }
9164
9165 return FALSE;
9166}
9167
b8871f35
L
9168/* Convert ELF common symbol TYPE. */
9169
9170static int
9171elf_link_convert_common_type (struct bfd_link_info *info, int type)
9172{
9173 /* Commom symbol can only appear in relocatable link. */
9174 if (!bfd_link_relocatable (info))
9175 abort ();
9176 switch (info->elf_stt_common)
9177 {
9178 case unchanged:
9179 break;
9180 case elf_stt_common:
9181 type = STT_COMMON;
9182 break;
9183 case no_elf_stt_common:
9184 type = STT_OBJECT;
9185 break;
9186 }
9187 return type;
9188}
9189
c152c796
AM
9190/* Add an external symbol to the symbol table. This is called from
9191 the hash table traversal routine. When generating a shared object,
9192 we go through the symbol table twice. The first time we output
9193 anything that might have been forced to local scope in a version
9194 script. The second time we output the symbols that are still
9195 global symbols. */
9196
9197static bfd_boolean
7686d77d 9198elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9199{
7686d77d 9200 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9201 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9202 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9203 bfd_boolean strip;
9204 Elf_Internal_Sym sym;
9205 asection *input_sec;
9206 const struct elf_backend_data *bed;
6e0b88f1
AM
9207 long indx;
9208 int ret;
b8871f35 9209 unsigned int type;
6e33951e
L
9210 /* A symbol is bound locally if it is forced local or it is locally
9211 defined, hidden versioned, not referenced by shared library and
9212 not exported when linking executable. */
9213 bfd_boolean local_bind = (h->forced_local
0e1862bb 9214 || (bfd_link_executable (flinfo->info)
6e33951e
L
9215 && !flinfo->info->export_dynamic
9216 && !h->dynamic
9217 && !h->ref_dynamic
9218 && h->def_regular
422f1182 9219 && h->versioned == versioned_hidden));
c152c796
AM
9220
9221 if (h->root.type == bfd_link_hash_warning)
9222 {
9223 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9224 if (h->root.type == bfd_link_hash_new)
9225 return TRUE;
9226 }
9227
9228 /* Decide whether to output this symbol in this pass. */
9229 if (eoinfo->localsyms)
9230 {
6e33951e 9231 if (!local_bind)
c152c796
AM
9232 return TRUE;
9233 }
9234 else
9235 {
6e33951e 9236 if (local_bind)
c152c796
AM
9237 return TRUE;
9238 }
9239
8b127cbc 9240 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9241
12ac1cf5 9242 if (h->root.type == bfd_link_hash_undefined)
c152c796 9243 {
12ac1cf5
NC
9244 /* If we have an undefined symbol reference here then it must have
9245 come from a shared library that is being linked in. (Undefined
98da7939
L
9246 references in regular files have already been handled unless
9247 they are in unreferenced sections which are removed by garbage
9248 collection). */
12ac1cf5
NC
9249 bfd_boolean ignore_undef = FALSE;
9250
9251 /* Some symbols may be special in that the fact that they're
9252 undefined can be safely ignored - let backend determine that. */
9253 if (bed->elf_backend_ignore_undef_symbol)
9254 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9255
9256 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9257 if (!ignore_undef
12ac1cf5 9258 && h->ref_dynamic
8b127cbc
AM
9259 && (!h->ref_regular || flinfo->info->gc_sections)
9260 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9261 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9262 (*flinfo->info->callbacks->undefined_symbol)
9263 (flinfo->info, h->root.root.string,
9264 h->ref_regular ? NULL : h->root.u.undef.abfd,
9265 NULL, 0,
9266 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9267
9268 /* Strip a global symbol defined in a discarded section. */
9269 if (h->indx == -3)
9270 return TRUE;
c152c796
AM
9271 }
9272
9273 /* We should also warn if a forced local symbol is referenced from
9274 shared libraries. */
0e1862bb 9275 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9276 && h->forced_local
9277 && h->ref_dynamic
371a5866 9278 && h->def_regular
f5385ebf 9279 && !h->dynamic_def
ee659f1f 9280 && h->ref_dynamic_nonweak
8b127cbc 9281 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9282 {
17d078c5
AM
9283 bfd *def_bfd;
9284 const char *msg;
90c984fc
L
9285 struct elf_link_hash_entry *hi = h;
9286
9287 /* Check indirect symbol. */
9288 while (hi->root.type == bfd_link_hash_indirect)
9289 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9290
9291 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9292 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9293 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9294 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9295 else
9296 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9297 def_bfd = flinfo->output_bfd;
90c984fc
L
9298 if (hi->root.u.def.section != bfd_abs_section_ptr)
9299 def_bfd = hi->root.u.def.section->owner;
8b127cbc 9300 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
17d078c5
AM
9301 h->root.root.string);
9302 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9303 eoinfo->failed = TRUE;
9304 return FALSE;
9305 }
9306
9307 /* We don't want to output symbols that have never been mentioned by
9308 a regular file, or that we have been told to strip. However, if
9309 h->indx is set to -2, the symbol is used by a reloc and we must
9310 output it. */
d983c8c5 9311 strip = FALSE;
c152c796 9312 if (h->indx == -2)
d983c8c5 9313 ;
f5385ebf 9314 else if ((h->def_dynamic
77cfaee6
AM
9315 || h->ref_dynamic
9316 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9317 && !h->def_regular
9318 && !h->ref_regular)
c152c796 9319 strip = TRUE;
8b127cbc 9320 else if (flinfo->info->strip == strip_all)
c152c796 9321 strip = TRUE;
8b127cbc
AM
9322 else if (flinfo->info->strip == strip_some
9323 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9324 h->root.root.string, FALSE, FALSE) == NULL)
9325 strip = TRUE;
d56d55e7
AM
9326 else if ((h->root.type == bfd_link_hash_defined
9327 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9328 && ((flinfo->info->strip_discarded
dbaa2011 9329 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9330 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9331 && h->root.u.def.section->owner != NULL
d56d55e7 9332 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9333 strip = TRUE;
9e2278f5
AM
9334 else if ((h->root.type == bfd_link_hash_undefined
9335 || h->root.type == bfd_link_hash_undefweak)
9336 && h->root.u.undef.abfd != NULL
9337 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9338 strip = TRUE;
c152c796 9339
b8871f35
L
9340 type = h->type;
9341
c152c796 9342 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9343 nothing else to do. However, if it is a forced local symbol or
9344 an ifunc symbol we need to give the backend finish_dynamic_symbol
9345 function a chance to make it dynamic. */
c152c796
AM
9346 if (strip
9347 && h->dynindx == -1
b8871f35 9348 && type != STT_GNU_IFUNC
f5385ebf 9349 && !h->forced_local)
c152c796
AM
9350 return TRUE;
9351
9352 sym.st_value = 0;
9353 sym.st_size = h->size;
9354 sym.st_other = h->other;
c152c796
AM
9355 switch (h->root.type)
9356 {
9357 default:
9358 case bfd_link_hash_new:
9359 case bfd_link_hash_warning:
9360 abort ();
9361 return FALSE;
9362
9363 case bfd_link_hash_undefined:
9364 case bfd_link_hash_undefweak:
9365 input_sec = bfd_und_section_ptr;
9366 sym.st_shndx = SHN_UNDEF;
9367 break;
9368
9369 case bfd_link_hash_defined:
9370 case bfd_link_hash_defweak:
9371 {
9372 input_sec = h->root.u.def.section;
9373 if (input_sec->output_section != NULL)
9374 {
9375 sym.st_shndx =
8b127cbc 9376 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9377 input_sec->output_section);
9378 if (sym.st_shndx == SHN_BAD)
9379 {
9380 (*_bfd_error_handler)
d003868e 9381 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9382 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9383 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9384 eoinfo->failed = TRUE;
9385 return FALSE;
9386 }
9387
9388 /* ELF symbols in relocatable files are section relative,
9389 but in nonrelocatable files they are virtual
9390 addresses. */
9391 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9392 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9393 {
9394 sym.st_value += input_sec->output_section->vma;
9395 if (h->type == STT_TLS)
9396 {
8b127cbc 9397 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9398 if (tls_sec != NULL)
9399 sym.st_value -= tls_sec->vma;
c152c796
AM
9400 }
9401 }
9402 }
9403 else
9404 {
9405 BFD_ASSERT (input_sec->owner == NULL
9406 || (input_sec->owner->flags & DYNAMIC) != 0);
9407 sym.st_shndx = SHN_UNDEF;
9408 input_sec = bfd_und_section_ptr;
9409 }
9410 }
9411 break;
9412
9413 case bfd_link_hash_common:
9414 input_sec = h->root.u.c.p->section;
a4d8e49b 9415 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9416 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9417 break;
9418
9419 case bfd_link_hash_indirect:
9420 /* These symbols are created by symbol versioning. They point
9421 to the decorated version of the name. For example, if the
9422 symbol foo@@GNU_1.2 is the default, which should be used when
9423 foo is used with no version, then we add an indirect symbol
9424 foo which points to foo@@GNU_1.2. We ignore these symbols,
9425 since the indirected symbol is already in the hash table. */
9426 return TRUE;
9427 }
9428
b8871f35
L
9429 if (type == STT_COMMON || type == STT_OBJECT)
9430 switch (h->root.type)
9431 {
9432 case bfd_link_hash_common:
9433 type = elf_link_convert_common_type (flinfo->info, type);
9434 break;
9435 case bfd_link_hash_defined:
9436 case bfd_link_hash_defweak:
9437 if (bed->common_definition (&sym))
9438 type = elf_link_convert_common_type (flinfo->info, type);
9439 else
9440 type = STT_OBJECT;
9441 break;
9442 case bfd_link_hash_undefined:
9443 case bfd_link_hash_undefweak:
9444 break;
9445 default:
9446 abort ();
9447 }
9448
9449 if (local_bind)
9450 {
9451 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9452 /* Turn off visibility on local symbol. */
9453 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9454 }
9455 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9456 else if (h->unique_global && h->def_regular)
9457 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9458 else if (h->root.type == bfd_link_hash_undefweak
9459 || h->root.type == bfd_link_hash_defweak)
9460 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9461 else
9462 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9463 sym.st_target_internal = h->target_internal;
9464
c152c796
AM
9465 /* Give the processor backend a chance to tweak the symbol value,
9466 and also to finish up anything that needs to be done for this
9467 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9468 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9469 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9470 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9471 && h->def_regular
0e1862bb 9472 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9473 || ((h->dynindx != -1
9474 || h->forced_local)
0e1862bb 9475 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9476 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9477 || h->root.type != bfd_link_hash_undefweak))
9478 || !h->forced_local)
8b127cbc 9479 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9480 {
9481 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9482 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9483 {
9484 eoinfo->failed = TRUE;
9485 return FALSE;
9486 }
9487 }
9488
9489 /* If we are marking the symbol as undefined, and there are no
9490 non-weak references to this symbol from a regular object, then
9491 mark the symbol as weak undefined; if there are non-weak
9492 references, mark the symbol as strong. We can't do this earlier,
9493 because it might not be marked as undefined until the
9494 finish_dynamic_symbol routine gets through with it. */
9495 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9496 && h->ref_regular
c152c796
AM
9497 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9498 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9499 {
9500 int bindtype;
b8871f35 9501 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9502
9503 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9504 if (type == STT_GNU_IFUNC)
9505 type = STT_FUNC;
c152c796 9506
f5385ebf 9507 if (h->ref_regular_nonweak)
c152c796
AM
9508 bindtype = STB_GLOBAL;
9509 else
9510 bindtype = STB_WEAK;
2955ec4c 9511 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9512 }
9513
bda987c2
CD
9514 /* If this is a symbol defined in a dynamic library, don't use the
9515 symbol size from the dynamic library. Relinking an executable
9516 against a new library may introduce gratuitous changes in the
9517 executable's symbols if we keep the size. */
9518 if (sym.st_shndx == SHN_UNDEF
9519 && !h->def_regular
9520 && h->def_dynamic)
9521 sym.st_size = 0;
9522
c152c796
AM
9523 /* If a non-weak symbol with non-default visibility is not defined
9524 locally, it is a fatal error. */
0e1862bb 9525 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9526 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9527 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9528 && h->root.type == bfd_link_hash_undefined
f5385ebf 9529 && !h->def_regular)
c152c796 9530 {
17d078c5
AM
9531 const char *msg;
9532
9533 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9534 msg = _("%B: protected symbol `%s' isn't defined");
9535 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9536 msg = _("%B: internal symbol `%s' isn't defined");
9537 else
9538 msg = _("%B: hidden symbol `%s' isn't defined");
8b127cbc 9539 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9540 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9541 eoinfo->failed = TRUE;
9542 return FALSE;
9543 }
9544
9545 /* If this symbol should be put in the .dynsym section, then put it
9546 there now. We already know the symbol index. We also fill in
9547 the entry in the .hash section. */
cae1fbbb 9548 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9549 && h->dynindx != -1
8b127cbc 9550 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9551 {
c152c796
AM
9552 bfd_byte *esym;
9553
90c984fc
L
9554 /* Since there is no version information in the dynamic string,
9555 if there is no version info in symbol version section, we will
1659f720 9556 have a run-time problem if not linking executable, referenced
6e33951e
L
9557 by shared library, not locally defined, or not bound locally.
9558 */
1659f720 9559 if (h->verinfo.verdef == NULL
6e33951e 9560 && !local_bind
0e1862bb 9561 && (!bfd_link_executable (flinfo->info)
1659f720
L
9562 || h->ref_dynamic
9563 || !h->def_regular))
90c984fc
L
9564 {
9565 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9566
9567 if (p && p [1] != '\0')
9568 {
9569 (*_bfd_error_handler)
9570 (_("%B: No symbol version section for versioned symbol `%s'"),
9571 flinfo->output_bfd, h->root.root.string);
9572 eoinfo->failed = TRUE;
9573 return FALSE;
9574 }
9575 }
9576
c152c796 9577 sym.st_name = h->dynstr_index;
cae1fbbb
L
9578 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9579 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9580 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9581 {
9582 eoinfo->failed = TRUE;
9583 return FALSE;
9584 }
8b127cbc 9585 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9586
8b127cbc 9587 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9588 {
9589 size_t hash_entry_size;
9590 bfd_byte *bucketpos;
9591 bfd_vma chain;
41198d0c
L
9592 size_t bucketcount;
9593 size_t bucket;
9594
8b127cbc 9595 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9596 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9597
9598 hash_entry_size
8b127cbc
AM
9599 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9600 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9601 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9602 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9603 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9604 bucketpos);
9605 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9606 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9607 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9608 }
c152c796 9609
8b127cbc 9610 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9611 {
9612 Elf_Internal_Versym iversym;
9613 Elf_External_Versym *eversym;
9614
f5385ebf 9615 if (!h->def_regular)
c152c796 9616 {
7b20f099
AM
9617 if (h->verinfo.verdef == NULL
9618 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9619 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9620 iversym.vs_vers = 0;
9621 else
9622 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9623 }
9624 else
9625 {
9626 if (h->verinfo.vertree == NULL)
9627 iversym.vs_vers = 1;
9628 else
9629 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9630 if (flinfo->info->create_default_symver)
3e3b46e5 9631 iversym.vs_vers++;
c152c796
AM
9632 }
9633
422f1182 9634 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9635 defined locally. */
422f1182 9636 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9637 iversym.vs_vers |= VERSYM_HIDDEN;
9638
8b127cbc 9639 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9640 eversym += h->dynindx;
8b127cbc 9641 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9642 }
9643 }
9644
d983c8c5
AM
9645 /* If the symbol is undefined, and we didn't output it to .dynsym,
9646 strip it from .symtab too. Obviously we can't do this for
9647 relocatable output or when needed for --emit-relocs. */
9648 else if (input_sec == bfd_und_section_ptr
9649 && h->indx != -2
0e1862bb 9650 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9651 return TRUE;
9652 /* Also strip others that we couldn't earlier due to dynamic symbol
9653 processing. */
9654 if (strip)
9655 return TRUE;
9656 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9657 return TRUE;
9658
2ec55de3
AM
9659 /* Output a FILE symbol so that following locals are not associated
9660 with the wrong input file. We need one for forced local symbols
9661 if we've seen more than one FILE symbol or when we have exactly
9662 one FILE symbol but global symbols are present in a file other
9663 than the one with the FILE symbol. We also need one if linker
9664 defined symbols are present. In practice these conditions are
9665 always met, so just emit the FILE symbol unconditionally. */
9666 if (eoinfo->localsyms
9667 && !eoinfo->file_sym_done
9668 && eoinfo->flinfo->filesym_count != 0)
9669 {
9670 Elf_Internal_Sym fsym;
9671
9672 memset (&fsym, 0, sizeof (fsym));
9673 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9674 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9675 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9676 bfd_und_section_ptr, NULL))
2ec55de3
AM
9677 return FALSE;
9678
9679 eoinfo->file_sym_done = TRUE;
9680 }
9681
8b127cbc 9682 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9683 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9684 input_sec, h);
6e0b88f1 9685 if (ret == 0)
c152c796
AM
9686 {
9687 eoinfo->failed = TRUE;
9688 return FALSE;
9689 }
6e0b88f1
AM
9690 else if (ret == 1)
9691 h->indx = indx;
9692 else if (h->indx == -2)
9693 abort();
c152c796
AM
9694
9695 return TRUE;
9696}
9697
cdd3575c
AM
9698/* Return TRUE if special handling is done for relocs in SEC against
9699 symbols defined in discarded sections. */
9700
c152c796
AM
9701static bfd_boolean
9702elf_section_ignore_discarded_relocs (asection *sec)
9703{
9704 const struct elf_backend_data *bed;
9705
cdd3575c
AM
9706 switch (sec->sec_info_type)
9707 {
dbaa2011
AM
9708 case SEC_INFO_TYPE_STABS:
9709 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9710 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9711 return TRUE;
9712 default:
9713 break;
9714 }
c152c796
AM
9715
9716 bed = get_elf_backend_data (sec->owner);
9717 if (bed->elf_backend_ignore_discarded_relocs != NULL
9718 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9719 return TRUE;
9720
9721 return FALSE;
9722}
9723
9e66c942
AM
9724/* Return a mask saying how ld should treat relocations in SEC against
9725 symbols defined in discarded sections. If this function returns
9726 COMPLAIN set, ld will issue a warning message. If this function
9727 returns PRETEND set, and the discarded section was link-once and the
9728 same size as the kept link-once section, ld will pretend that the
9729 symbol was actually defined in the kept section. Otherwise ld will
9730 zero the reloc (at least that is the intent, but some cooperation by
9731 the target dependent code is needed, particularly for REL targets). */
9732
8a696751
AM
9733unsigned int
9734_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9735{
9e66c942 9736 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9737 return PRETEND;
cdd3575c
AM
9738
9739 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9740 return 0;
cdd3575c
AM
9741
9742 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9743 return 0;
cdd3575c 9744
9e66c942 9745 return COMPLAIN | PRETEND;
cdd3575c
AM
9746}
9747
3d7f7666
L
9748/* Find a match between a section and a member of a section group. */
9749
9750static asection *
c0f00686
L
9751match_group_member (asection *sec, asection *group,
9752 struct bfd_link_info *info)
3d7f7666
L
9753{
9754 asection *first = elf_next_in_group (group);
9755 asection *s = first;
9756
9757 while (s != NULL)
9758 {
c0f00686 9759 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9760 return s;
9761
83180ade 9762 s = elf_next_in_group (s);
3d7f7666
L
9763 if (s == first)
9764 break;
9765 }
9766
9767 return NULL;
9768}
9769
01b3c8ab 9770/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9771 to replace it. Return the replacement if it is OK. Otherwise return
9772 NULL. */
01b3c8ab
L
9773
9774asection *
c0f00686 9775_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9776{
9777 asection *kept;
9778
9779 kept = sec->kept_section;
9780 if (kept != NULL)
9781 {
c2370991 9782 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9783 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9784 if (kept != NULL
9785 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9786 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9787 kept = NULL;
c2370991 9788 sec->kept_section = kept;
01b3c8ab
L
9789 }
9790 return kept;
9791}
9792
c152c796
AM
9793/* Link an input file into the linker output file. This function
9794 handles all the sections and relocations of the input file at once.
9795 This is so that we only have to read the local symbols once, and
9796 don't have to keep them in memory. */
9797
9798static bfd_boolean
8b127cbc 9799elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9800{
ece5ef60 9801 int (*relocate_section)
c152c796
AM
9802 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9803 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9804 bfd *output_bfd;
9805 Elf_Internal_Shdr *symtab_hdr;
9806 size_t locsymcount;
9807 size_t extsymoff;
9808 Elf_Internal_Sym *isymbuf;
9809 Elf_Internal_Sym *isym;
9810 Elf_Internal_Sym *isymend;
9811 long *pindex;
9812 asection **ppsection;
9813 asection *o;
9814 const struct elf_backend_data *bed;
c152c796 9815 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9816 bfd_size_type address_size;
9817 bfd_vma r_type_mask;
9818 int r_sym_shift;
ffbc01cc 9819 bfd_boolean have_file_sym = FALSE;
c152c796 9820
8b127cbc 9821 output_bfd = flinfo->output_bfd;
c152c796
AM
9822 bed = get_elf_backend_data (output_bfd);
9823 relocate_section = bed->elf_backend_relocate_section;
9824
9825 /* If this is a dynamic object, we don't want to do anything here:
9826 we don't want the local symbols, and we don't want the section
9827 contents. */
9828 if ((input_bfd->flags & DYNAMIC) != 0)
9829 return TRUE;
9830
c152c796
AM
9831 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9832 if (elf_bad_symtab (input_bfd))
9833 {
9834 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9835 extsymoff = 0;
9836 }
9837 else
9838 {
9839 locsymcount = symtab_hdr->sh_info;
9840 extsymoff = symtab_hdr->sh_info;
9841 }
9842
9843 /* Read the local symbols. */
9844 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9845 if (isymbuf == NULL && locsymcount != 0)
9846 {
9847 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9848 flinfo->internal_syms,
9849 flinfo->external_syms,
9850 flinfo->locsym_shndx);
c152c796
AM
9851 if (isymbuf == NULL)
9852 return FALSE;
9853 }
9854
9855 /* Find local symbol sections and adjust values of symbols in
9856 SEC_MERGE sections. Write out those local symbols we know are
9857 going into the output file. */
9858 isymend = isymbuf + locsymcount;
8b127cbc 9859 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9860 isym < isymend;
9861 isym++, pindex++, ppsection++)
9862 {
9863 asection *isec;
9864 const char *name;
9865 Elf_Internal_Sym osym;
6e0b88f1
AM
9866 long indx;
9867 int ret;
c152c796
AM
9868
9869 *pindex = -1;
9870
9871 if (elf_bad_symtab (input_bfd))
9872 {
9873 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9874 {
9875 *ppsection = NULL;
9876 continue;
9877 }
9878 }
9879
9880 if (isym->st_shndx == SHN_UNDEF)
9881 isec = bfd_und_section_ptr;
c152c796
AM
9882 else if (isym->st_shndx == SHN_ABS)
9883 isec = bfd_abs_section_ptr;
9884 else if (isym->st_shndx == SHN_COMMON)
9885 isec = bfd_com_section_ptr;
9886 else
9887 {
cb33740c
AM
9888 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9889 if (isec == NULL)
9890 {
9891 /* Don't attempt to output symbols with st_shnx in the
9892 reserved range other than SHN_ABS and SHN_COMMON. */
9893 *ppsection = NULL;
9894 continue;
9895 }
dbaa2011 9896 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9897 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9898 isym->st_value =
9899 _bfd_merged_section_offset (output_bfd, &isec,
9900 elf_section_data (isec)->sec_info,
9901 isym->st_value);
c152c796
AM
9902 }
9903
9904 *ppsection = isec;
9905
d983c8c5
AM
9906 /* Don't output the first, undefined, symbol. In fact, don't
9907 output any undefined local symbol. */
9908 if (isec == bfd_und_section_ptr)
c152c796
AM
9909 continue;
9910
9911 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9912 {
9913 /* We never output section symbols. Instead, we use the
9914 section symbol of the corresponding section in the output
9915 file. */
9916 continue;
9917 }
9918
9919 /* If we are stripping all symbols, we don't want to output this
9920 one. */
8b127cbc 9921 if (flinfo->info->strip == strip_all)
c152c796
AM
9922 continue;
9923
9924 /* If we are discarding all local symbols, we don't want to
9925 output this one. If we are generating a relocatable output
9926 file, then some of the local symbols may be required by
9927 relocs; we output them below as we discover that they are
9928 needed. */
8b127cbc 9929 if (flinfo->info->discard == discard_all)
c152c796
AM
9930 continue;
9931
9932 /* If this symbol is defined in a section which we are
f02571c5
AM
9933 discarding, we don't need to keep it. */
9934 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9935 && isym->st_shndx < SHN_LORESERVE
9936 && bfd_section_removed_from_list (output_bfd,
9937 isec->output_section))
e75a280b
L
9938 continue;
9939
c152c796
AM
9940 /* Get the name of the symbol. */
9941 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9942 isym->st_name);
9943 if (name == NULL)
9944 return FALSE;
9945
9946 /* See if we are discarding symbols with this name. */
8b127cbc
AM
9947 if ((flinfo->info->strip == strip_some
9948 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 9949 == NULL))
8b127cbc 9950 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
9951 && (isec->flags & SEC_MERGE)
9952 && !bfd_link_relocatable (flinfo->info))
8b127cbc 9953 || flinfo->info->discard == discard_l)
c152c796
AM
9954 && bfd_is_local_label_name (input_bfd, name)))
9955 continue;
9956
ffbc01cc
AM
9957 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9958 {
ce875075
AM
9959 if (input_bfd->lto_output)
9960 /* -flto puts a temp file name here. This means builds
9961 are not reproducible. Discard the symbol. */
9962 continue;
ffbc01cc
AM
9963 have_file_sym = TRUE;
9964 flinfo->filesym_count += 1;
9965 }
9966 if (!have_file_sym)
9967 {
9968 /* In the absence of debug info, bfd_find_nearest_line uses
9969 FILE symbols to determine the source file for local
9970 function symbols. Provide a FILE symbol here if input
9971 files lack such, so that their symbols won't be
9972 associated with a previous input file. It's not the
9973 source file, but the best we can do. */
9974 have_file_sym = TRUE;
9975 flinfo->filesym_count += 1;
9976 memset (&osym, 0, sizeof (osym));
9977 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9978 osym.st_shndx = SHN_ABS;
ef10c3ac
L
9979 if (!elf_link_output_symstrtab (flinfo,
9980 (input_bfd->lto_output ? NULL
9981 : input_bfd->filename),
9982 &osym, bfd_abs_section_ptr,
9983 NULL))
ffbc01cc
AM
9984 return FALSE;
9985 }
9986
c152c796
AM
9987 osym = *isym;
9988
9989 /* Adjust the section index for the output file. */
9990 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9991 isec->output_section);
9992 if (osym.st_shndx == SHN_BAD)
9993 return FALSE;
9994
c152c796
AM
9995 /* ELF symbols in relocatable files are section relative, but
9996 in executable files they are virtual addresses. Note that
9997 this code assumes that all ELF sections have an associated
9998 BFD section with a reasonable value for output_offset; below
9999 we assume that they also have a reasonable value for
10000 output_section. Any special sections must be set up to meet
10001 these requirements. */
10002 osym.st_value += isec->output_offset;
0e1862bb 10003 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10004 {
10005 osym.st_value += isec->output_section->vma;
10006 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10007 {
10008 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10009 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10010 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10011 }
10012 }
10013
6e0b88f1 10014 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10015 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10016 if (ret == 0)
c152c796 10017 return FALSE;
6e0b88f1
AM
10018 else if (ret == 1)
10019 *pindex = indx;
c152c796
AM
10020 }
10021
310fd250
L
10022 if (bed->s->arch_size == 32)
10023 {
10024 r_type_mask = 0xff;
10025 r_sym_shift = 8;
10026 address_size = 4;
10027 }
10028 else
10029 {
10030 r_type_mask = 0xffffffff;
10031 r_sym_shift = 32;
10032 address_size = 8;
10033 }
10034
c152c796
AM
10035 /* Relocate the contents of each section. */
10036 sym_hashes = elf_sym_hashes (input_bfd);
10037 for (o = input_bfd->sections; o != NULL; o = o->next)
10038 {
10039 bfd_byte *contents;
10040
10041 if (! o->linker_mark)
10042 {
10043 /* This section was omitted from the link. */
10044 continue;
10045 }
10046
0e1862bb 10047 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
10048 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10049 {
10050 /* Deal with the group signature symbol. */
10051 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10052 unsigned long symndx = sec_data->this_hdr.sh_info;
10053 asection *osec = o->output_section;
10054
10055 if (symndx >= locsymcount
10056 || (elf_bad_symtab (input_bfd)
8b127cbc 10057 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10058 {
10059 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10060 while (h->root.type == bfd_link_hash_indirect
10061 || h->root.type == bfd_link_hash_warning)
10062 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10063 /* Arrange for symbol to be output. */
10064 h->indx = -2;
10065 elf_section_data (osec)->this_hdr.sh_info = -2;
10066 }
10067 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10068 {
10069 /* We'll use the output section target_index. */
8b127cbc 10070 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10071 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10072 }
10073 else
10074 {
8b127cbc 10075 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10076 {
10077 /* Otherwise output the local symbol now. */
10078 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10079 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10080 const char *name;
6e0b88f1
AM
10081 long indx;
10082 int ret;
bcacc0f5
AM
10083
10084 name = bfd_elf_string_from_elf_section (input_bfd,
10085 symtab_hdr->sh_link,
10086 sym.st_name);
10087 if (name == NULL)
10088 return FALSE;
10089
10090 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10091 sec);
10092 if (sym.st_shndx == SHN_BAD)
10093 return FALSE;
10094
10095 sym.st_value += o->output_offset;
10096
6e0b88f1 10097 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10098 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10099 NULL);
6e0b88f1 10100 if (ret == 0)
bcacc0f5 10101 return FALSE;
6e0b88f1 10102 else if (ret == 1)
8b127cbc 10103 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10104 else
10105 abort ();
bcacc0f5
AM
10106 }
10107 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10108 = flinfo->indices[symndx];
bcacc0f5
AM
10109 }
10110 }
10111
c152c796 10112 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10113 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10114 continue;
10115
10116 if ((o->flags & SEC_LINKER_CREATED) != 0)
10117 {
10118 /* Section was created by _bfd_elf_link_create_dynamic_sections
10119 or somesuch. */
10120 continue;
10121 }
10122
10123 /* Get the contents of the section. They have been cached by a
10124 relaxation routine. Note that o is a section in an input
10125 file, so the contents field will not have been set by any of
10126 the routines which work on output files. */
10127 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10128 {
10129 contents = elf_section_data (o)->this_hdr.contents;
10130 if (bed->caches_rawsize
10131 && o->rawsize != 0
10132 && o->rawsize < o->size)
10133 {
10134 memcpy (flinfo->contents, contents, o->rawsize);
10135 contents = flinfo->contents;
10136 }
10137 }
c152c796
AM
10138 else
10139 {
8b127cbc 10140 contents = flinfo->contents;
4a114e3e 10141 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10142 return FALSE;
10143 }
10144
10145 if ((o->flags & SEC_RELOC) != 0)
10146 {
10147 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10148 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10149 int action_discarded;
ece5ef60 10150 int ret;
c152c796
AM
10151
10152 /* Get the swapped relocs. */
10153 internal_relocs
8b127cbc
AM
10154 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10155 flinfo->internal_relocs, FALSE);
c152c796
AM
10156 if (internal_relocs == NULL
10157 && o->reloc_count > 0)
10158 return FALSE;
10159
310fd250
L
10160 /* We need to reverse-copy input .ctors/.dtors sections if
10161 they are placed in .init_array/.finit_array for output. */
10162 if (o->size > address_size
10163 && ((strncmp (o->name, ".ctors", 6) == 0
10164 && strcmp (o->output_section->name,
10165 ".init_array") == 0)
10166 || (strncmp (o->name, ".dtors", 6) == 0
10167 && strcmp (o->output_section->name,
10168 ".fini_array") == 0))
10169 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10170 {
310fd250
L
10171 if (o->size != o->reloc_count * address_size)
10172 {
10173 (*_bfd_error_handler)
10174 (_("error: %B: size of section %A is not "
10175 "multiple of address size"),
10176 input_bfd, o);
10177 bfd_set_error (bfd_error_on_input);
10178 return FALSE;
10179 }
10180 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10181 }
10182
0f02bbd9 10183 action_discarded = -1;
c152c796 10184 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10185 action_discarded = (*bed->action_discarded) (o);
10186
10187 /* Run through the relocs evaluating complex reloc symbols and
10188 looking for relocs against symbols from discarded sections
10189 or section symbols from removed link-once sections.
10190 Complain about relocs against discarded sections. Zero
10191 relocs against removed link-once sections. */
10192
10193 rel = internal_relocs;
10194 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10195 for ( ; rel < relend; rel++)
c152c796 10196 {
0f02bbd9
AM
10197 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10198 unsigned int s_type;
10199 asection **ps, *sec;
10200 struct elf_link_hash_entry *h = NULL;
10201 const char *sym_name;
c152c796 10202
0f02bbd9
AM
10203 if (r_symndx == STN_UNDEF)
10204 continue;
c152c796 10205
0f02bbd9
AM
10206 if (r_symndx >= locsymcount
10207 || (elf_bad_symtab (input_bfd)
8b127cbc 10208 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10209 {
10210 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10211
0f02bbd9
AM
10212 /* Badly formatted input files can contain relocs that
10213 reference non-existant symbols. Check here so that
10214 we do not seg fault. */
10215 if (h == NULL)
c152c796 10216 {
0f02bbd9 10217 char buffer [32];
dce669a1 10218
0f02bbd9
AM
10219 sprintf_vma (buffer, rel->r_info);
10220 (*_bfd_error_handler)
10221 (_("error: %B contains a reloc (0x%s) for section %A "
10222 "that references a non-existent global symbol"),
10223 input_bfd, o, buffer);
10224 bfd_set_error (bfd_error_bad_value);
10225 return FALSE;
10226 }
3b36f7e6 10227
0f02bbd9
AM
10228 while (h->root.type == bfd_link_hash_indirect
10229 || h->root.type == bfd_link_hash_warning)
10230 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10231
0f02bbd9 10232 s_type = h->type;
cdd3575c 10233
9e2dec47 10234 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10235 mark the symbol as undefined. Note that the
10236 linker may attach linker created dynamic sections
10237 to the plugin bfd. Symbols defined in linker
10238 created sections are not plugin symbols. */
9e2dec47
L
10239 if (h->root.non_ir_ref
10240 && (h->root.type == bfd_link_hash_defined
10241 || h->root.type == bfd_link_hash_defweak)
10242 && (h->root.u.def.section->flags
10243 & SEC_LINKER_CREATED) == 0
10244 && h->root.u.def.section->owner != NULL
10245 && (h->root.u.def.section->owner->flags
10246 & BFD_PLUGIN) != 0)
10247 {
10248 h->root.type = bfd_link_hash_undefined;
10249 h->root.u.undef.abfd = h->root.u.def.section->owner;
10250 }
10251
0f02bbd9
AM
10252 ps = NULL;
10253 if (h->root.type == bfd_link_hash_defined
10254 || h->root.type == bfd_link_hash_defweak)
10255 ps = &h->root.u.def.section;
10256
10257 sym_name = h->root.root.string;
10258 }
10259 else
10260 {
10261 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10262
10263 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10264 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10265 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10266 sym, *ps);
10267 }
c152c796 10268
c301e700 10269 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10270 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10271 {
10272 bfd_vma val;
10273 bfd_vma dot = (rel->r_offset
10274 + o->output_offset + o->output_section->vma);
10275#ifdef DEBUG
10276 printf ("Encountered a complex symbol!");
10277 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10278 input_bfd->filename, o->name,
10279 (long) (rel - internal_relocs));
0f02bbd9
AM
10280 printf (" symbol: idx %8.8lx, name %s\n",
10281 r_symndx, sym_name);
10282 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10283 (unsigned long) rel->r_info,
10284 (unsigned long) rel->r_offset);
10285#endif
8b127cbc 10286 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10287 isymbuf, locsymcount, s_type == STT_SRELC))
10288 return FALSE;
10289
10290 /* Symbol evaluated OK. Update to absolute value. */
10291 set_symbol_value (input_bfd, isymbuf, locsymcount,
10292 r_symndx, val);
10293 continue;
10294 }
10295
10296 if (action_discarded != -1 && ps != NULL)
10297 {
cdd3575c
AM
10298 /* Complain if the definition comes from a
10299 discarded section. */
dbaa2011 10300 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10301 {
cf35638d 10302 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10303 if (action_discarded & COMPLAIN)
8b127cbc 10304 (*flinfo->info->callbacks->einfo)
e1fffbe6 10305 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10306 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10307 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10308
87e5235d 10309 /* Try to do the best we can to support buggy old
e0ae6d6f 10310 versions of gcc. Pretend that the symbol is
87e5235d
AM
10311 really defined in the kept linkonce section.
10312 FIXME: This is quite broken. Modifying the
10313 symbol here means we will be changing all later
e0ae6d6f 10314 uses of the symbol, not just in this section. */
0f02bbd9 10315 if (action_discarded & PRETEND)
87e5235d 10316 {
01b3c8ab
L
10317 asection *kept;
10318
c0f00686 10319 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10320 flinfo->info);
01b3c8ab 10321 if (kept != NULL)
87e5235d
AM
10322 {
10323 *ps = kept;
10324 continue;
10325 }
10326 }
c152c796
AM
10327 }
10328 }
10329 }
10330
10331 /* Relocate the section by invoking a back end routine.
10332
10333 The back end routine is responsible for adjusting the
10334 section contents as necessary, and (if using Rela relocs
10335 and generating a relocatable output file) adjusting the
10336 reloc addend as necessary.
10337
10338 The back end routine does not have to worry about setting
10339 the reloc address or the reloc symbol index.
10340
10341 The back end routine is given a pointer to the swapped in
10342 internal symbols, and can access the hash table entries
10343 for the external symbols via elf_sym_hashes (input_bfd).
10344
10345 When generating relocatable output, the back end routine
10346 must handle STB_LOCAL/STT_SECTION symbols specially. The
10347 output symbol is going to be a section symbol
10348 corresponding to the output section, which will require
10349 the addend to be adjusted. */
10350
8b127cbc 10351 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10352 input_bfd, o, contents,
10353 internal_relocs,
10354 isymbuf,
8b127cbc 10355 flinfo->sections);
ece5ef60 10356 if (!ret)
c152c796
AM
10357 return FALSE;
10358
ece5ef60 10359 if (ret == 2
0e1862bb 10360 || bfd_link_relocatable (flinfo->info)
8b127cbc 10361 || flinfo->info->emitrelocations)
c152c796
AM
10362 {
10363 Elf_Internal_Rela *irela;
d4730f92 10364 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10365 bfd_vma last_offset;
10366 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10367 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10368 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10369 unsigned int next_erel;
c152c796 10370 bfd_boolean rela_normal;
d4730f92 10371 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10372
d4730f92
BS
10373 esdi = elf_section_data (o);
10374 esdo = elf_section_data (o->output_section);
10375 rela_normal = FALSE;
c152c796
AM
10376
10377 /* Adjust the reloc addresses and symbol indices. */
10378
10379 irela = internal_relocs;
10380 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10381 rel_hash = esdo->rel.hashes + esdo->rel.count;
10382 /* We start processing the REL relocs, if any. When we reach
10383 IRELAMID in the loop, we switch to the RELA relocs. */
10384 irelamid = irela;
10385 if (esdi->rel.hdr != NULL)
10386 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10387 * bed->s->int_rels_per_ext_rel);
eac338cf 10388 rel_hash_list = rel_hash;
d4730f92 10389 rela_hash_list = NULL;
c152c796 10390 last_offset = o->output_offset;
0e1862bb 10391 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10392 last_offset += o->output_section->vma;
10393 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10394 {
10395 unsigned long r_symndx;
10396 asection *sec;
10397 Elf_Internal_Sym sym;
10398
10399 if (next_erel == bed->s->int_rels_per_ext_rel)
10400 {
10401 rel_hash++;
10402 next_erel = 0;
10403 }
10404
d4730f92
BS
10405 if (irela == irelamid)
10406 {
10407 rel_hash = esdo->rela.hashes + esdo->rela.count;
10408 rela_hash_list = rel_hash;
10409 rela_normal = bed->rela_normal;
10410 }
10411
c152c796 10412 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10413 flinfo->info, o,
c152c796
AM
10414 irela->r_offset);
10415 if (irela->r_offset >= (bfd_vma) -2)
10416 {
10417 /* This is a reloc for a deleted entry or somesuch.
10418 Turn it into an R_*_NONE reloc, at the same
10419 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10420 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10421 being ordered. */
10422 irela->r_offset = last_offset;
10423 irela->r_info = 0;
10424 irela->r_addend = 0;
10425 continue;
10426 }
10427
10428 irela->r_offset += o->output_offset;
10429
10430 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10431 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10432 irela->r_offset += o->output_section->vma;
10433
10434 last_offset = irela->r_offset;
10435
10436 r_symndx = irela->r_info >> r_sym_shift;
10437 if (r_symndx == STN_UNDEF)
10438 continue;
10439
10440 if (r_symndx >= locsymcount
10441 || (elf_bad_symtab (input_bfd)
8b127cbc 10442 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10443 {
10444 struct elf_link_hash_entry *rh;
10445 unsigned long indx;
10446
10447 /* This is a reloc against a global symbol. We
10448 have not yet output all the local symbols, so
10449 we do not know the symbol index of any global
10450 symbol. We set the rel_hash entry for this
10451 reloc to point to the global hash table entry
10452 for this symbol. The symbol index is then
ee75fd95 10453 set at the end of bfd_elf_final_link. */
c152c796
AM
10454 indx = r_symndx - extsymoff;
10455 rh = elf_sym_hashes (input_bfd)[indx];
10456 while (rh->root.type == bfd_link_hash_indirect
10457 || rh->root.type == bfd_link_hash_warning)
10458 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10459
10460 /* Setting the index to -2 tells
10461 elf_link_output_extsym that this symbol is
10462 used by a reloc. */
10463 BFD_ASSERT (rh->indx < 0);
10464 rh->indx = -2;
10465
10466 *rel_hash = rh;
10467
10468 continue;
10469 }
10470
10471 /* This is a reloc against a local symbol. */
10472
10473 *rel_hash = NULL;
10474 sym = isymbuf[r_symndx];
8b127cbc 10475 sec = flinfo->sections[r_symndx];
c152c796
AM
10476 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10477 {
10478 /* I suppose the backend ought to fill in the
10479 section of any STT_SECTION symbol against a
6a8d1586 10480 processor specific section. */
cf35638d 10481 r_symndx = STN_UNDEF;
6a8d1586
AM
10482 if (bfd_is_abs_section (sec))
10483 ;
c152c796
AM
10484 else if (sec == NULL || sec->owner == NULL)
10485 {
10486 bfd_set_error (bfd_error_bad_value);
10487 return FALSE;
10488 }
10489 else
10490 {
6a8d1586
AM
10491 asection *osec = sec->output_section;
10492
10493 /* If we have discarded a section, the output
10494 section will be the absolute section. In
ab96bf03
AM
10495 case of discarded SEC_MERGE sections, use
10496 the kept section. relocate_section should
10497 have already handled discarded linkonce
10498 sections. */
6a8d1586
AM
10499 if (bfd_is_abs_section (osec)
10500 && sec->kept_section != NULL
10501 && sec->kept_section->output_section != NULL)
10502 {
10503 osec = sec->kept_section->output_section;
10504 irela->r_addend -= osec->vma;
10505 }
10506
10507 if (!bfd_is_abs_section (osec))
10508 {
10509 r_symndx = osec->target_index;
cf35638d 10510 if (r_symndx == STN_UNDEF)
74541ad4 10511 {
051d833a
AM
10512 irela->r_addend += osec->vma;
10513 osec = _bfd_nearby_section (output_bfd, osec,
10514 osec->vma);
10515 irela->r_addend -= osec->vma;
10516 r_symndx = osec->target_index;
74541ad4 10517 }
6a8d1586 10518 }
c152c796
AM
10519 }
10520
10521 /* Adjust the addend according to where the
10522 section winds up in the output section. */
10523 if (rela_normal)
10524 irela->r_addend += sec->output_offset;
10525 }
10526 else
10527 {
8b127cbc 10528 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10529 {
10530 unsigned long shlink;
10531 const char *name;
10532 asection *osec;
6e0b88f1 10533 long indx;
c152c796 10534
8b127cbc 10535 if (flinfo->info->strip == strip_all)
c152c796
AM
10536 {
10537 /* You can't do ld -r -s. */
10538 bfd_set_error (bfd_error_invalid_operation);
10539 return FALSE;
10540 }
10541
10542 /* This symbol was skipped earlier, but
10543 since it is needed by a reloc, we
10544 must output it now. */
10545 shlink = symtab_hdr->sh_link;
10546 name = (bfd_elf_string_from_elf_section
10547 (input_bfd, shlink, sym.st_name));
10548 if (name == NULL)
10549 return FALSE;
10550
10551 osec = sec->output_section;
10552 sym.st_shndx =
10553 _bfd_elf_section_from_bfd_section (output_bfd,
10554 osec);
10555 if (sym.st_shndx == SHN_BAD)
10556 return FALSE;
10557
10558 sym.st_value += sec->output_offset;
0e1862bb 10559 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10560 {
10561 sym.st_value += osec->vma;
10562 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10563 {
10564 /* STT_TLS symbols are relative to PT_TLS
10565 segment base. */
8b127cbc 10566 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10567 ->tls_sec != NULL);
8b127cbc 10568 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10569 ->tls_sec->vma);
10570 }
10571 }
10572
6e0b88f1 10573 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10574 ret = elf_link_output_symstrtab (flinfo, name,
10575 &sym, sec,
10576 NULL);
6e0b88f1 10577 if (ret == 0)
c152c796 10578 return FALSE;
6e0b88f1 10579 else if (ret == 1)
8b127cbc 10580 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10581 else
10582 abort ();
c152c796
AM
10583 }
10584
8b127cbc 10585 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10586 }
10587
10588 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10589 | (irela->r_info & r_type_mask));
10590 }
10591
10592 /* Swap out the relocs. */
d4730f92
BS
10593 input_rel_hdr = esdi->rel.hdr;
10594 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10595 {
d4730f92
BS
10596 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10597 input_rel_hdr,
10598 internal_relocs,
10599 rel_hash_list))
10600 return FALSE;
c152c796
AM
10601 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10602 * bed->s->int_rels_per_ext_rel);
eac338cf 10603 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10604 }
10605
10606 input_rela_hdr = esdi->rela.hdr;
10607 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10608 {
eac338cf 10609 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10610 input_rela_hdr,
eac338cf 10611 internal_relocs,
d4730f92 10612 rela_hash_list))
c152c796
AM
10613 return FALSE;
10614 }
10615 }
10616 }
10617
10618 /* Write out the modified section contents. */
10619 if (bed->elf_backend_write_section
8b127cbc 10620 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10621 contents))
c152c796
AM
10622 {
10623 /* Section written out. */
10624 }
10625 else switch (o->sec_info_type)
10626 {
dbaa2011 10627 case SEC_INFO_TYPE_STABS:
c152c796
AM
10628 if (! (_bfd_write_section_stabs
10629 (output_bfd,
8b127cbc 10630 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10631 o, &elf_section_data (o)->sec_info, contents)))
10632 return FALSE;
10633 break;
dbaa2011 10634 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10635 if (! _bfd_write_merged_section (output_bfd, o,
10636 elf_section_data (o)->sec_info))
10637 return FALSE;
10638 break;
dbaa2011 10639 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10640 {
8b127cbc 10641 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10642 o, contents))
10643 return FALSE;
10644 }
10645 break;
2f0c68f2
CM
10646 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10647 {
10648 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10649 flinfo->info,
10650 o, contents))
10651 return FALSE;
10652 }
10653 break;
c152c796
AM
10654 default:
10655 {
310fd250
L
10656 if (! (o->flags & SEC_EXCLUDE))
10657 {
10658 file_ptr offset = (file_ptr) o->output_offset;
10659 bfd_size_type todo = o->size;
37b01f6a
DG
10660
10661 offset *= bfd_octets_per_byte (output_bfd);
10662
310fd250
L
10663 if ((o->flags & SEC_ELF_REVERSE_COPY))
10664 {
10665 /* Reverse-copy input section to output. */
10666 do
10667 {
10668 todo -= address_size;
10669 if (! bfd_set_section_contents (output_bfd,
10670 o->output_section,
10671 contents + todo,
10672 offset,
10673 address_size))
10674 return FALSE;
10675 if (todo == 0)
10676 break;
10677 offset += address_size;
10678 }
10679 while (1);
10680 }
10681 else if (! bfd_set_section_contents (output_bfd,
10682 o->output_section,
10683 contents,
10684 offset, todo))
10685 return FALSE;
10686 }
c152c796
AM
10687 }
10688 break;
10689 }
10690 }
10691
10692 return TRUE;
10693}
10694
10695/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10696 requested by the linker, and does not come from any input file. This
c152c796
AM
10697 is used to build constructor and destructor tables when linking
10698 with -Ur. */
10699
10700static bfd_boolean
10701elf_reloc_link_order (bfd *output_bfd,
10702 struct bfd_link_info *info,
10703 asection *output_section,
10704 struct bfd_link_order *link_order)
10705{
10706 reloc_howto_type *howto;
10707 long indx;
10708 bfd_vma offset;
10709 bfd_vma addend;
d4730f92 10710 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10711 struct elf_link_hash_entry **rel_hash_ptr;
10712 Elf_Internal_Shdr *rel_hdr;
10713 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10714 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10715 bfd_byte *erel;
10716 unsigned int i;
d4730f92 10717 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10718
10719 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10720 if (howto == NULL)
10721 {
10722 bfd_set_error (bfd_error_bad_value);
10723 return FALSE;
10724 }
10725
10726 addend = link_order->u.reloc.p->addend;
10727
d4730f92
BS
10728 if (esdo->rel.hdr)
10729 reldata = &esdo->rel;
10730 else if (esdo->rela.hdr)
10731 reldata = &esdo->rela;
10732 else
10733 {
10734 reldata = NULL;
10735 BFD_ASSERT (0);
10736 }
10737
c152c796 10738 /* Figure out the symbol index. */
d4730f92 10739 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10740 if (link_order->type == bfd_section_reloc_link_order)
10741 {
10742 indx = link_order->u.reloc.p->u.section->target_index;
10743 BFD_ASSERT (indx != 0);
10744 *rel_hash_ptr = NULL;
10745 }
10746 else
10747 {
10748 struct elf_link_hash_entry *h;
10749
10750 /* Treat a reloc against a defined symbol as though it were
10751 actually against the section. */
10752 h = ((struct elf_link_hash_entry *)
10753 bfd_wrapped_link_hash_lookup (output_bfd, info,
10754 link_order->u.reloc.p->u.name,
10755 FALSE, FALSE, TRUE));
10756 if (h != NULL
10757 && (h->root.type == bfd_link_hash_defined
10758 || h->root.type == bfd_link_hash_defweak))
10759 {
10760 asection *section;
10761
10762 section = h->root.u.def.section;
10763 indx = section->output_section->target_index;
10764 *rel_hash_ptr = NULL;
10765 /* It seems that we ought to add the symbol value to the
10766 addend here, but in practice it has already been added
10767 because it was passed to constructor_callback. */
10768 addend += section->output_section->vma + section->output_offset;
10769 }
10770 else if (h != NULL)
10771 {
10772 /* Setting the index to -2 tells elf_link_output_extsym that
10773 this symbol is used by a reloc. */
10774 h->indx = -2;
10775 *rel_hash_ptr = h;
10776 indx = 0;
10777 }
10778 else
10779 {
1a72702b
AM
10780 (*info->callbacks->unattached_reloc)
10781 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
10782 indx = 0;
10783 }
10784 }
10785
10786 /* If this is an inplace reloc, we must write the addend into the
10787 object file. */
10788 if (howto->partial_inplace && addend != 0)
10789 {
10790 bfd_size_type size;
10791 bfd_reloc_status_type rstat;
10792 bfd_byte *buf;
10793 bfd_boolean ok;
10794 const char *sym_name;
10795
a50b1753
NC
10796 size = (bfd_size_type) bfd_get_reloc_size (howto);
10797 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10798 if (buf == NULL && size != 0)
c152c796
AM
10799 return FALSE;
10800 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10801 switch (rstat)
10802 {
10803 case bfd_reloc_ok:
10804 break;
10805
10806 default:
10807 case bfd_reloc_outofrange:
10808 abort ();
10809
10810 case bfd_reloc_overflow:
10811 if (link_order->type == bfd_section_reloc_link_order)
10812 sym_name = bfd_section_name (output_bfd,
10813 link_order->u.reloc.p->u.section);
10814 else
10815 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
10816 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10817 howto->name, addend, NULL, NULL,
10818 (bfd_vma) 0);
c152c796
AM
10819 break;
10820 }
37b01f6a 10821
c152c796 10822 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
10823 link_order->offset
10824 * bfd_octets_per_byte (output_bfd),
10825 size);
c152c796
AM
10826 free (buf);
10827 if (! ok)
10828 return FALSE;
10829 }
10830
10831 /* The address of a reloc is relative to the section in a
10832 relocatable file, and is a virtual address in an executable
10833 file. */
10834 offset = link_order->offset;
0e1862bb 10835 if (! bfd_link_relocatable (info))
c152c796
AM
10836 offset += output_section->vma;
10837
10838 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10839 {
10840 irel[i].r_offset = offset;
10841 irel[i].r_info = 0;
10842 irel[i].r_addend = 0;
10843 }
10844 if (bed->s->arch_size == 32)
10845 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10846 else
10847 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10848
d4730f92 10849 rel_hdr = reldata->hdr;
c152c796
AM
10850 erel = rel_hdr->contents;
10851 if (rel_hdr->sh_type == SHT_REL)
10852 {
d4730f92 10853 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10854 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10855 }
10856 else
10857 {
10858 irel[0].r_addend = addend;
d4730f92 10859 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10860 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10861 }
10862
d4730f92 10863 ++reldata->count;
c152c796
AM
10864
10865 return TRUE;
10866}
10867
0b52efa6
PB
10868
10869/* Get the output vma of the section pointed to by the sh_link field. */
10870
10871static bfd_vma
10872elf_get_linked_section_vma (struct bfd_link_order *p)
10873{
10874 Elf_Internal_Shdr **elf_shdrp;
10875 asection *s;
10876 int elfsec;
10877
10878 s = p->u.indirect.section;
10879 elf_shdrp = elf_elfsections (s->owner);
10880 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10881 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10882 /* PR 290:
10883 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10884 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10885 sh_info fields. Hence we could get the situation
10886 where elfsec is 0. */
10887 if (elfsec == 0)
10888 {
10889 const struct elf_backend_data *bed
10890 = get_elf_backend_data (s->owner);
10891 if (bed->link_order_error_handler)
d003868e
AM
10892 bed->link_order_error_handler
10893 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10894 return 0;
10895 }
10896 else
10897 {
10898 s = elf_shdrp[elfsec]->bfd_section;
10899 return s->output_section->vma + s->output_offset;
10900 }
0b52efa6
PB
10901}
10902
10903
10904/* Compare two sections based on the locations of the sections they are
10905 linked to. Used by elf_fixup_link_order. */
10906
10907static int
10908compare_link_order (const void * a, const void * b)
10909{
10910 bfd_vma apos;
10911 bfd_vma bpos;
10912
10913 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10914 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10915 if (apos < bpos)
10916 return -1;
10917 return apos > bpos;
10918}
10919
10920
10921/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10922 order as their linked sections. Returns false if this could not be done
10923 because an output section includes both ordered and unordered
10924 sections. Ideally we'd do this in the linker proper. */
10925
10926static bfd_boolean
10927elf_fixup_link_order (bfd *abfd, asection *o)
10928{
10929 int seen_linkorder;
10930 int seen_other;
10931 int n;
10932 struct bfd_link_order *p;
10933 bfd *sub;
10934 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10935 unsigned elfsec;
0b52efa6 10936 struct bfd_link_order **sections;
d33cdfe3 10937 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10938 bfd_vma offset;
3b36f7e6 10939
d33cdfe3
L
10940 other_sec = NULL;
10941 linkorder_sec = NULL;
0b52efa6
PB
10942 seen_other = 0;
10943 seen_linkorder = 0;
8423293d 10944 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10945 {
d33cdfe3 10946 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10947 {
10948 s = p->u.indirect.section;
d33cdfe3
L
10949 sub = s->owner;
10950 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10951 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10952 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10953 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10954 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10955 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10956 {
10957 seen_linkorder++;
10958 linkorder_sec = s;
10959 }
0b52efa6 10960 else
d33cdfe3
L
10961 {
10962 seen_other++;
10963 other_sec = s;
10964 }
0b52efa6
PB
10965 }
10966 else
10967 seen_other++;
d33cdfe3
L
10968
10969 if (seen_other && seen_linkorder)
10970 {
10971 if (other_sec && linkorder_sec)
10972 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10973 o, linkorder_sec,
10974 linkorder_sec->owner, other_sec,
10975 other_sec->owner);
10976 else
10977 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10978 o);
10979 bfd_set_error (bfd_error_bad_value);
10980 return FALSE;
10981 }
0b52efa6
PB
10982 }
10983
10984 if (!seen_linkorder)
10985 return TRUE;
10986
0b52efa6 10987 sections = (struct bfd_link_order **)
14b1c01e
AM
10988 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10989 if (sections == NULL)
10990 return FALSE;
0b52efa6 10991 seen_linkorder = 0;
3b36f7e6 10992
8423293d 10993 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
10994 {
10995 sections[seen_linkorder++] = p;
10996 }
10997 /* Sort the input sections in the order of their linked section. */
10998 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10999 compare_link_order);
11000
11001 /* Change the offsets of the sections. */
11002 offset = 0;
11003 for (n = 0; n < seen_linkorder; n++)
11004 {
11005 s = sections[n]->u.indirect.section;
461686a3 11006 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11007 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11008 sections[n]->offset = offset;
11009 offset += sections[n]->size;
11010 }
11011
4dd07732 11012 free (sections);
0b52efa6
PB
11013 return TRUE;
11014}
11015
9f7c3e5e
AM
11016static void
11017elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11018{
11019 asection *o;
11020
11021 if (flinfo->symstrtab != NULL)
ef10c3ac 11022 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11023 if (flinfo->contents != NULL)
11024 free (flinfo->contents);
11025 if (flinfo->external_relocs != NULL)
11026 free (flinfo->external_relocs);
11027 if (flinfo->internal_relocs != NULL)
11028 free (flinfo->internal_relocs);
11029 if (flinfo->external_syms != NULL)
11030 free (flinfo->external_syms);
11031 if (flinfo->locsym_shndx != NULL)
11032 free (flinfo->locsym_shndx);
11033 if (flinfo->internal_syms != NULL)
11034 free (flinfo->internal_syms);
11035 if (flinfo->indices != NULL)
11036 free (flinfo->indices);
11037 if (flinfo->sections != NULL)
11038 free (flinfo->sections);
9f7c3e5e
AM
11039 if (flinfo->symshndxbuf != NULL)
11040 free (flinfo->symshndxbuf);
11041 for (o = obfd->sections; o != NULL; o = o->next)
11042 {
11043 struct bfd_elf_section_data *esdo = elf_section_data (o);
11044 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11045 free (esdo->rel.hashes);
11046 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11047 free (esdo->rela.hashes);
11048 }
11049}
0b52efa6 11050
c152c796
AM
11051/* Do the final step of an ELF link. */
11052
11053bfd_boolean
11054bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11055{
11056 bfd_boolean dynamic;
11057 bfd_boolean emit_relocs;
11058 bfd *dynobj;
8b127cbc 11059 struct elf_final_link_info flinfo;
91d6fa6a
NC
11060 asection *o;
11061 struct bfd_link_order *p;
11062 bfd *sub;
c152c796
AM
11063 bfd_size_type max_contents_size;
11064 bfd_size_type max_external_reloc_size;
11065 bfd_size_type max_internal_reloc_count;
11066 bfd_size_type max_sym_count;
11067 bfd_size_type max_sym_shndx_count;
c152c796
AM
11068 Elf_Internal_Sym elfsym;
11069 unsigned int i;
11070 Elf_Internal_Shdr *symtab_hdr;
11071 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11072 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11073 struct elf_outext_info eoinfo;
11074 bfd_boolean merged;
11075 size_t relativecount = 0;
11076 asection *reldyn = 0;
11077 bfd_size_type amt;
104d59d1
JM
11078 asection *attr_section = NULL;
11079 bfd_vma attr_size = 0;
11080 const char *std_attrs_section;
c152c796
AM
11081
11082 if (! is_elf_hash_table (info->hash))
11083 return FALSE;
11084
0e1862bb 11085 if (bfd_link_pic (info))
c152c796
AM
11086 abfd->flags |= DYNAMIC;
11087
11088 dynamic = elf_hash_table (info)->dynamic_sections_created;
11089 dynobj = elf_hash_table (info)->dynobj;
11090
0e1862bb 11091 emit_relocs = (bfd_link_relocatable (info)
a4676736 11092 || info->emitrelocations);
c152c796 11093
8b127cbc
AM
11094 flinfo.info = info;
11095 flinfo.output_bfd = abfd;
ef10c3ac 11096 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11097 if (flinfo.symstrtab == NULL)
c152c796
AM
11098 return FALSE;
11099
11100 if (! dynamic)
11101 {
8b127cbc
AM
11102 flinfo.hash_sec = NULL;
11103 flinfo.symver_sec = NULL;
c152c796
AM
11104 }
11105 else
11106 {
3d4d4302 11107 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11108 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11109 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11110 /* Note that it is OK if symver_sec is NULL. */
11111 }
11112
8b127cbc
AM
11113 flinfo.contents = NULL;
11114 flinfo.external_relocs = NULL;
11115 flinfo.internal_relocs = NULL;
11116 flinfo.external_syms = NULL;
11117 flinfo.locsym_shndx = NULL;
11118 flinfo.internal_syms = NULL;
11119 flinfo.indices = NULL;
11120 flinfo.sections = NULL;
8b127cbc 11121 flinfo.symshndxbuf = NULL;
ffbc01cc 11122 flinfo.filesym_count = 0;
c152c796 11123
104d59d1
JM
11124 /* The object attributes have been merged. Remove the input
11125 sections from the link, and set the contents of the output
11126 secton. */
11127 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11128 for (o = abfd->sections; o != NULL; o = o->next)
11129 {
11130 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11131 || strcmp (o->name, ".gnu.attributes") == 0)
11132 {
11133 for (p = o->map_head.link_order; p != NULL; p = p->next)
11134 {
11135 asection *input_section;
11136
11137 if (p->type != bfd_indirect_link_order)
11138 continue;
11139 input_section = p->u.indirect.section;
11140 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11141 elf_link_input_bfd ignores this section. */
11142 input_section->flags &= ~SEC_HAS_CONTENTS;
11143 }
a0c8462f 11144
104d59d1
JM
11145 attr_size = bfd_elf_obj_attr_size (abfd);
11146 if (attr_size)
11147 {
11148 bfd_set_section_size (abfd, o, attr_size);
11149 attr_section = o;
11150 /* Skip this section later on. */
11151 o->map_head.link_order = NULL;
11152 }
11153 else
11154 o->flags |= SEC_EXCLUDE;
11155 }
11156 }
11157
c152c796
AM
11158 /* Count up the number of relocations we will output for each output
11159 section, so that we know the sizes of the reloc sections. We
11160 also figure out some maximum sizes. */
11161 max_contents_size = 0;
11162 max_external_reloc_size = 0;
11163 max_internal_reloc_count = 0;
11164 max_sym_count = 0;
11165 max_sym_shndx_count = 0;
11166 merged = FALSE;
11167 for (o = abfd->sections; o != NULL; o = o->next)
11168 {
11169 struct bfd_elf_section_data *esdo = elf_section_data (o);
11170 o->reloc_count = 0;
11171
8423293d 11172 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11173 {
11174 unsigned int reloc_count = 0;
491d01d3 11175 unsigned int additional_reloc_count = 0;
c152c796 11176 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11177
11178 if (p->type == bfd_section_reloc_link_order
11179 || p->type == bfd_symbol_reloc_link_order)
11180 reloc_count = 1;
11181 else if (p->type == bfd_indirect_link_order)
11182 {
11183 asection *sec;
11184
11185 sec = p->u.indirect.section;
11186 esdi = elf_section_data (sec);
11187
11188 /* Mark all sections which are to be included in the
11189 link. This will normally be every section. We need
11190 to do this so that we can identify any sections which
11191 the linker has decided to not include. */
11192 sec->linker_mark = TRUE;
11193
11194 if (sec->flags & SEC_MERGE)
11195 merged = TRUE;
11196
aed64b35
L
11197 if (esdo->this_hdr.sh_type == SHT_REL
11198 || esdo->this_hdr.sh_type == SHT_RELA)
11199 /* Some backends use reloc_count in relocation sections
11200 to count particular types of relocs. Of course,
11201 reloc sections themselves can't have relocations. */
11202 reloc_count = 0;
0e1862bb 11203 else if (emit_relocs)
491d01d3
YU
11204 {
11205 reloc_count = sec->reloc_count;
11206 if (bed->elf_backend_count_additional_relocs)
11207 {
11208 int c;
11209 c = (*bed->elf_backend_count_additional_relocs) (sec);
11210 additional_reloc_count += c;
11211 }
11212 }
c152c796 11213 else if (bed->elf_backend_count_relocs)
58217f29 11214 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 11215
eea6121a
AM
11216 if (sec->rawsize > max_contents_size)
11217 max_contents_size = sec->rawsize;
11218 if (sec->size > max_contents_size)
11219 max_contents_size = sec->size;
c152c796
AM
11220
11221 /* We are interested in just local symbols, not all
11222 symbols. */
11223 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11224 && (sec->owner->flags & DYNAMIC) == 0)
11225 {
11226 size_t sym_count;
11227
11228 if (elf_bad_symtab (sec->owner))
11229 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11230 / bed->s->sizeof_sym);
11231 else
11232 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11233
11234 if (sym_count > max_sym_count)
11235 max_sym_count = sym_count;
11236
11237 if (sym_count > max_sym_shndx_count
6a40cf0c 11238 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11239 max_sym_shndx_count = sym_count;
11240
11241 if ((sec->flags & SEC_RELOC) != 0)
11242 {
d4730f92 11243 size_t ext_size = 0;
c152c796 11244
d4730f92
BS
11245 if (esdi->rel.hdr != NULL)
11246 ext_size = esdi->rel.hdr->sh_size;
11247 if (esdi->rela.hdr != NULL)
11248 ext_size += esdi->rela.hdr->sh_size;
7326c758 11249
c152c796
AM
11250 if (ext_size > max_external_reloc_size)
11251 max_external_reloc_size = ext_size;
11252 if (sec->reloc_count > max_internal_reloc_count)
11253 max_internal_reloc_count = sec->reloc_count;
11254 }
11255 }
11256 }
11257
11258 if (reloc_count == 0)
11259 continue;
11260
491d01d3 11261 reloc_count += additional_reloc_count;
c152c796
AM
11262 o->reloc_count += reloc_count;
11263
0e1862bb 11264 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11265 {
d4730f92 11266 if (esdi->rel.hdr)
491d01d3
YU
11267 {
11268 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11269 esdo->rel.count += additional_reloc_count;
11270 }
d4730f92 11271 if (esdi->rela.hdr)
491d01d3
YU
11272 {
11273 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11274 esdo->rela.count += additional_reloc_count;
11275 }
d4730f92
BS
11276 }
11277 else
11278 {
11279 if (o->use_rela_p)
11280 esdo->rela.count += reloc_count;
2c2b4ed4 11281 else
d4730f92 11282 esdo->rel.count += reloc_count;
c152c796 11283 }
c152c796
AM
11284 }
11285
11286 if (o->reloc_count > 0)
11287 o->flags |= SEC_RELOC;
11288 else
11289 {
11290 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11291 set it (this is probably a bug) and if it is set
11292 assign_section_numbers will create a reloc section. */
11293 o->flags &=~ SEC_RELOC;
11294 }
11295
11296 /* If the SEC_ALLOC flag is not set, force the section VMA to
11297 zero. This is done in elf_fake_sections as well, but forcing
11298 the VMA to 0 here will ensure that relocs against these
11299 sections are handled correctly. */
11300 if ((o->flags & SEC_ALLOC) == 0
11301 && ! o->user_set_vma)
11302 o->vma = 0;
11303 }
11304
0e1862bb 11305 if (! bfd_link_relocatable (info) && merged)
c152c796
AM
11306 elf_link_hash_traverse (elf_hash_table (info),
11307 _bfd_elf_link_sec_merge_syms, abfd);
11308
11309 /* Figure out the file positions for everything but the symbol table
11310 and the relocs. We set symcount to force assign_section_numbers
11311 to create a symbol table. */
8539e4e8 11312 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11313 BFD_ASSERT (! abfd->output_has_begun);
11314 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11315 goto error_return;
11316
ee75fd95 11317 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11318 for (o = abfd->sections; o != NULL; o = o->next)
11319 {
d4730f92 11320 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11321 if ((o->flags & SEC_RELOC) != 0)
11322 {
d4730f92
BS
11323 if (esdo->rel.hdr
11324 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11325 goto error_return;
11326
d4730f92
BS
11327 if (esdo->rela.hdr
11328 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11329 goto error_return;
11330 }
11331
11332 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11333 to count upwards while actually outputting the relocations. */
d4730f92
BS
11334 esdo->rel.count = 0;
11335 esdo->rela.count = 0;
0ce398f1
L
11336
11337 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11338 {
11339 /* Cache the section contents so that they can be compressed
11340 later. Use bfd_malloc since it will be freed by
11341 bfd_compress_section_contents. */
11342 unsigned char *contents = esdo->this_hdr.contents;
11343 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11344 abort ();
11345 contents
11346 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11347 if (contents == NULL)
11348 goto error_return;
11349 esdo->this_hdr.contents = contents;
11350 }
c152c796
AM
11351 }
11352
c152c796 11353 /* We have now assigned file positions for all the sections except
a485e98e
AM
11354 .symtab, .strtab, and non-loaded reloc sections. We start the
11355 .symtab section at the current file position, and write directly
11356 to it. We build the .strtab section in memory. */
c152c796
AM
11357 bfd_get_symcount (abfd) = 0;
11358 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11359 /* sh_name is set in prep_headers. */
11360 symtab_hdr->sh_type = SHT_SYMTAB;
11361 /* sh_flags, sh_addr and sh_size all start off zero. */
11362 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11363 /* sh_link is set in assign_section_numbers. */
11364 /* sh_info is set below. */
11365 /* sh_offset is set just below. */
72de5009 11366 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11367
ef10c3ac
L
11368 if (max_sym_count < 20)
11369 max_sym_count = 20;
11370 elf_hash_table (info)->strtabsize = max_sym_count;
11371 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11372 elf_hash_table (info)->strtab
11373 = (struct elf_sym_strtab *) bfd_malloc (amt);
11374 if (elf_hash_table (info)->strtab == NULL)
c152c796 11375 goto error_return;
ef10c3ac
L
11376 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11377 flinfo.symshndxbuf
11378 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11379 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11380
8539e4e8 11381 if (info->strip != strip_all || emit_relocs)
c152c796 11382 {
8539e4e8
AM
11383 file_ptr off = elf_next_file_pos (abfd);
11384
11385 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11386
11387 /* Note that at this point elf_next_file_pos (abfd) is
11388 incorrect. We do not yet know the size of the .symtab section.
11389 We correct next_file_pos below, after we do know the size. */
11390
11391 /* Start writing out the symbol table. The first symbol is always a
11392 dummy symbol. */
c152c796
AM
11393 elfsym.st_value = 0;
11394 elfsym.st_size = 0;
11395 elfsym.st_info = 0;
11396 elfsym.st_other = 0;
11397 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11398 elfsym.st_target_internal = 0;
ef10c3ac
L
11399 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11400 bfd_und_section_ptr, NULL) != 1)
c152c796 11401 goto error_return;
c152c796 11402
8539e4e8
AM
11403 /* Output a symbol for each section. We output these even if we are
11404 discarding local symbols, since they are used for relocs. These
11405 symbols have no names. We store the index of each one in the
11406 index field of the section, so that we can find it again when
11407 outputting relocs. */
11408
c152c796
AM
11409 elfsym.st_size = 0;
11410 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11411 elfsym.st_other = 0;
f0b5bb34 11412 elfsym.st_value = 0;
35fc36a8 11413 elfsym.st_target_internal = 0;
c152c796
AM
11414 for (i = 1; i < elf_numsections (abfd); i++)
11415 {
11416 o = bfd_section_from_elf_index (abfd, i);
11417 if (o != NULL)
f0b5bb34
AM
11418 {
11419 o->target_index = bfd_get_symcount (abfd);
11420 elfsym.st_shndx = i;
0e1862bb 11421 if (!bfd_link_relocatable (info))
f0b5bb34 11422 elfsym.st_value = o->vma;
ef10c3ac
L
11423 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11424 NULL) != 1)
f0b5bb34
AM
11425 goto error_return;
11426 }
c152c796
AM
11427 }
11428 }
11429
11430 /* Allocate some memory to hold information read in from the input
11431 files. */
11432 if (max_contents_size != 0)
11433 {
8b127cbc
AM
11434 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11435 if (flinfo.contents == NULL)
c152c796
AM
11436 goto error_return;
11437 }
11438
11439 if (max_external_reloc_size != 0)
11440 {
8b127cbc
AM
11441 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11442 if (flinfo.external_relocs == NULL)
c152c796
AM
11443 goto error_return;
11444 }
11445
11446 if (max_internal_reloc_count != 0)
11447 {
11448 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11449 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11450 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11451 if (flinfo.internal_relocs == NULL)
c152c796
AM
11452 goto error_return;
11453 }
11454
11455 if (max_sym_count != 0)
11456 {
11457 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11458 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11459 if (flinfo.external_syms == NULL)
c152c796
AM
11460 goto error_return;
11461
11462 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11463 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11464 if (flinfo.internal_syms == NULL)
c152c796
AM
11465 goto error_return;
11466
11467 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11468 flinfo.indices = (long int *) bfd_malloc (amt);
11469 if (flinfo.indices == NULL)
c152c796
AM
11470 goto error_return;
11471
11472 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11473 flinfo.sections = (asection **) bfd_malloc (amt);
11474 if (flinfo.sections == NULL)
c152c796
AM
11475 goto error_return;
11476 }
11477
11478 if (max_sym_shndx_count != 0)
11479 {
11480 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11481 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11482 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11483 goto error_return;
11484 }
11485
11486 if (elf_hash_table (info)->tls_sec)
11487 {
11488 bfd_vma base, end = 0;
11489 asection *sec;
11490
11491 for (sec = elf_hash_table (info)->tls_sec;
11492 sec && (sec->flags & SEC_THREAD_LOCAL);
11493 sec = sec->next)
11494 {
3a800eb9 11495 bfd_size_type size = sec->size;
c152c796 11496
3a800eb9
AM
11497 if (size == 0
11498 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11499 {
91d6fa6a
NC
11500 struct bfd_link_order *ord = sec->map_tail.link_order;
11501
11502 if (ord != NULL)
11503 size = ord->offset + ord->size;
c152c796
AM
11504 }
11505 end = sec->vma + size;
11506 }
11507 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
11508 /* Only align end of TLS section if static TLS doesn't have special
11509 alignment requirements. */
11510 if (bed->static_tls_alignment == 1)
11511 end = align_power (end,
11512 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
11513 elf_hash_table (info)->tls_size = end - base;
11514 }
11515
0b52efa6
PB
11516 /* Reorder SHF_LINK_ORDER sections. */
11517 for (o = abfd->sections; o != NULL; o = o->next)
11518 {
11519 if (!elf_fixup_link_order (abfd, o))
11520 return FALSE;
11521 }
11522
2f0c68f2
CM
11523 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11524 return FALSE;
11525
c152c796
AM
11526 /* Since ELF permits relocations to be against local symbols, we
11527 must have the local symbols available when we do the relocations.
11528 Since we would rather only read the local symbols once, and we
11529 would rather not keep them in memory, we handle all the
11530 relocations for a single input file at the same time.
11531
11532 Unfortunately, there is no way to know the total number of local
11533 symbols until we have seen all of them, and the local symbol
11534 indices precede the global symbol indices. This means that when
11535 we are generating relocatable output, and we see a reloc against
11536 a global symbol, we can not know the symbol index until we have
11537 finished examining all the local symbols to see which ones we are
11538 going to output. To deal with this, we keep the relocations in
11539 memory, and don't output them until the end of the link. This is
11540 an unfortunate waste of memory, but I don't see a good way around
11541 it. Fortunately, it only happens when performing a relocatable
11542 link, which is not the common case. FIXME: If keep_memory is set
11543 we could write the relocs out and then read them again; I don't
11544 know how bad the memory loss will be. */
11545
c72f2fb2 11546 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11547 sub->output_has_begun = FALSE;
11548 for (o = abfd->sections; o != NULL; o = o->next)
11549 {
8423293d 11550 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11551 {
11552 if (p->type == bfd_indirect_link_order
11553 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11554 == bfd_target_elf_flavour)
11555 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11556 {
11557 if (! sub->output_has_begun)
11558 {
8b127cbc 11559 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11560 goto error_return;
11561 sub->output_has_begun = TRUE;
11562 }
11563 }
11564 else if (p->type == bfd_section_reloc_link_order
11565 || p->type == bfd_symbol_reloc_link_order)
11566 {
11567 if (! elf_reloc_link_order (abfd, info, o, p))
11568 goto error_return;
11569 }
11570 else
11571 {
11572 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11573 {
11574 if (p->type == bfd_indirect_link_order
11575 && (bfd_get_flavour (sub)
11576 == bfd_target_elf_flavour)
11577 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11578 != bed->s->elfclass))
11579 {
11580 const char *iclass, *oclass;
11581
aebf9be7 11582 switch (bed->s->elfclass)
351f65ca 11583 {
aebf9be7
NC
11584 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11585 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11586 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11587 default: abort ();
351f65ca 11588 }
aebf9be7
NC
11589
11590 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11591 {
aebf9be7
NC
11592 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11593 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11594 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11595 default: abort ();
351f65ca
L
11596 }
11597
11598 bfd_set_error (bfd_error_wrong_format);
11599 (*_bfd_error_handler)
11600 (_("%B: file class %s incompatible with %s"),
11601 sub, iclass, oclass);
11602 }
11603
11604 goto error_return;
11605 }
c152c796
AM
11606 }
11607 }
11608 }
11609
c0f00686
L
11610 /* Free symbol buffer if needed. */
11611 if (!info->reduce_memory_overheads)
11612 {
c72f2fb2 11613 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11614 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11615 && elf_tdata (sub)->symbuf)
c0f00686
L
11616 {
11617 free (elf_tdata (sub)->symbuf);
11618 elf_tdata (sub)->symbuf = NULL;
11619 }
11620 }
11621
c152c796
AM
11622 /* Output any global symbols that got converted to local in a
11623 version script or due to symbol visibility. We do this in a
11624 separate step since ELF requires all local symbols to appear
11625 prior to any global symbols. FIXME: We should only do this if
11626 some global symbols were, in fact, converted to become local.
11627 FIXME: Will this work correctly with the Irix 5 linker? */
11628 eoinfo.failed = FALSE;
8b127cbc 11629 eoinfo.flinfo = &flinfo;
c152c796 11630 eoinfo.localsyms = TRUE;
34a79995 11631 eoinfo.file_sym_done = FALSE;
7686d77d 11632 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11633 if (eoinfo.failed)
11634 return FALSE;
11635
4e617b1e
PB
11636 /* If backend needs to output some local symbols not present in the hash
11637 table, do it now. */
8539e4e8
AM
11638 if (bed->elf_backend_output_arch_local_syms
11639 && (info->strip != strip_all || emit_relocs))
4e617b1e 11640 {
6e0b88f1 11641 typedef int (*out_sym_func)
4e617b1e
PB
11642 (void *, const char *, Elf_Internal_Sym *, asection *,
11643 struct elf_link_hash_entry *);
11644
11645 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11646 (abfd, info, &flinfo,
11647 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11648 return FALSE;
11649 }
11650
c152c796
AM
11651 /* That wrote out all the local symbols. Finish up the symbol table
11652 with the global symbols. Even if we want to strip everything we
11653 can, we still need to deal with those global symbols that got
11654 converted to local in a version script. */
11655
11656 /* The sh_info field records the index of the first non local symbol. */
11657 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11658
11659 if (dynamic
cae1fbbb
L
11660 && elf_hash_table (info)->dynsym != NULL
11661 && (elf_hash_table (info)->dynsym->output_section
11662 != bfd_abs_section_ptr))
c152c796
AM
11663 {
11664 Elf_Internal_Sym sym;
cae1fbbb 11665 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
c152c796
AM
11666 long last_local = 0;
11667
11668 /* Write out the section symbols for the output sections. */
0e1862bb
L
11669 if (bfd_link_pic (info)
11670 || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
11671 {
11672 asection *s;
11673
11674 sym.st_size = 0;
11675 sym.st_name = 0;
11676 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11677 sym.st_other = 0;
35fc36a8 11678 sym.st_target_internal = 0;
c152c796
AM
11679
11680 for (s = abfd->sections; s != NULL; s = s->next)
11681 {
11682 int indx;
11683 bfd_byte *dest;
11684 long dynindx;
11685
c152c796 11686 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11687 if (dynindx <= 0)
11688 continue;
11689 indx = elf_section_data (s)->this_idx;
c152c796
AM
11690 BFD_ASSERT (indx > 0);
11691 sym.st_shndx = indx;
c0d5a53d
L
11692 if (! check_dynsym (abfd, &sym))
11693 return FALSE;
c152c796
AM
11694 sym.st_value = s->vma;
11695 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
11696 if (last_local < dynindx)
11697 last_local = dynindx;
c152c796
AM
11698 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11699 }
c152c796
AM
11700 }
11701
11702 /* Write out the local dynsyms. */
11703 if (elf_hash_table (info)->dynlocal)
11704 {
11705 struct elf_link_local_dynamic_entry *e;
11706 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11707 {
11708 asection *s;
11709 bfd_byte *dest;
11710
935bd1e0 11711 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11712 Note that we saved a word of storage and overwrote
11713 the original st_name with the dynstr_index. */
11714 sym = e->isym;
935bd1e0 11715 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11716
cb33740c
AM
11717 s = bfd_section_from_elf_index (e->input_bfd,
11718 e->isym.st_shndx);
11719 if (s != NULL)
c152c796 11720 {
c152c796
AM
11721 sym.st_shndx =
11722 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11723 if (! check_dynsym (abfd, &sym))
11724 return FALSE;
c152c796
AM
11725 sym.st_value = (s->output_section->vma
11726 + s->output_offset
11727 + e->isym.st_value);
11728 }
11729
11730 if (last_local < e->dynindx)
11731 last_local = e->dynindx;
11732
11733 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11734 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11735 }
11736 }
11737
cae1fbbb 11738 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
c152c796
AM
11739 last_local + 1;
11740 }
11741
11742 /* We get the global symbols from the hash table. */
11743 eoinfo.failed = FALSE;
11744 eoinfo.localsyms = FALSE;
8b127cbc 11745 eoinfo.flinfo = &flinfo;
7686d77d 11746 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11747 if (eoinfo.failed)
11748 return FALSE;
11749
11750 /* If backend needs to output some symbols not present in the hash
11751 table, do it now. */
8539e4e8
AM
11752 if (bed->elf_backend_output_arch_syms
11753 && (info->strip != strip_all || emit_relocs))
c152c796 11754 {
6e0b88f1 11755 typedef int (*out_sym_func)
c152c796
AM
11756 (void *, const char *, Elf_Internal_Sym *, asection *,
11757 struct elf_link_hash_entry *);
11758
11759 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
11760 (abfd, info, &flinfo,
11761 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
11762 return FALSE;
11763 }
11764
ef10c3ac
L
11765 /* Finalize the .strtab section. */
11766 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11767
11768 /* Swap out the .strtab section. */
11769 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
11770 return FALSE;
11771
11772 /* Now we know the size of the symtab section. */
c152c796
AM
11773 if (bfd_get_symcount (abfd) > 0)
11774 {
ee3b52e9
L
11775 /* Finish up and write out the symbol string table (.strtab)
11776 section. */
11777 Elf_Internal_Shdr *symstrtab_hdr;
8539e4e8
AM
11778 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11779
6a40cf0c
NC
11780 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11781 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
8539e4e8
AM
11782 {
11783 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11784 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11785 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11786 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11787 symtab_shndx_hdr->sh_size = amt;
11788
11789 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11790 off, TRUE);
11791
11792 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11793 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11794 return FALSE;
11795 }
ee3b52e9
L
11796
11797 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11798 /* sh_name was set in prep_headers. */
11799 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 11800 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 11801 symstrtab_hdr->sh_addr = 0;
ef10c3ac 11802 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
11803 symstrtab_hdr->sh_entsize = 0;
11804 symstrtab_hdr->sh_link = 0;
11805 symstrtab_hdr->sh_info = 0;
11806 /* sh_offset is set just below. */
11807 symstrtab_hdr->sh_addralign = 1;
11808
11809 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11810 off, TRUE);
11811 elf_next_file_pos (abfd) = off;
11812
c152c796 11813 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 11814 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
11815 return FALSE;
11816 }
11817
11818 /* Adjust the relocs to have the correct symbol indices. */
11819 for (o = abfd->sections; o != NULL; o = o->next)
11820 {
d4730f92 11821 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 11822 bfd_boolean sort;
c152c796
AM
11823 if ((o->flags & SEC_RELOC) == 0)
11824 continue;
11825
28dbcedc 11826 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3
AM
11827 if (esdo->rel.hdr != NULL
11828 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11829 return FALSE;
11830 if (esdo->rela.hdr != NULL
11831 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11832 return FALSE;
c152c796
AM
11833
11834 /* Set the reloc_count field to 0 to prevent write_relocs from
11835 trying to swap the relocs out itself. */
11836 o->reloc_count = 0;
11837 }
11838
11839 if (dynamic && info->combreloc && dynobj != NULL)
11840 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11841
11842 /* If we are linking against a dynamic object, or generating a
11843 shared library, finish up the dynamic linking information. */
11844 if (dynamic)
11845 {
11846 bfd_byte *dyncon, *dynconend;
11847
11848 /* Fix up .dynamic entries. */
3d4d4302 11849 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
11850 BFD_ASSERT (o != NULL);
11851
11852 dyncon = o->contents;
eea6121a 11853 dynconend = o->contents + o->size;
c152c796
AM
11854 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11855 {
11856 Elf_Internal_Dyn dyn;
11857 const char *name;
11858 unsigned int type;
11859
11860 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11861
11862 switch (dyn.d_tag)
11863 {
11864 default:
11865 continue;
11866 case DT_NULL:
11867 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11868 {
11869 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11870 {
11871 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11872 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11873 default: continue;
11874 }
11875 dyn.d_un.d_val = relativecount;
11876 relativecount = 0;
11877 break;
11878 }
11879 continue;
11880
11881 case DT_INIT:
11882 name = info->init_function;
11883 goto get_sym;
11884 case DT_FINI:
11885 name = info->fini_function;
11886 get_sym:
11887 {
11888 struct elf_link_hash_entry *h;
11889
11890 h = elf_link_hash_lookup (elf_hash_table (info), name,
11891 FALSE, FALSE, TRUE);
11892 if (h != NULL
11893 && (h->root.type == bfd_link_hash_defined
11894 || h->root.type == bfd_link_hash_defweak))
11895 {
bef26483 11896 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11897 o = h->root.u.def.section;
11898 if (o->output_section != NULL)
bef26483 11899 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11900 + o->output_offset);
11901 else
11902 {
11903 /* The symbol is imported from another shared
11904 library and does not apply to this one. */
bef26483 11905 dyn.d_un.d_ptr = 0;
c152c796
AM
11906 }
11907 break;
11908 }
11909 }
11910 continue;
11911
11912 case DT_PREINIT_ARRAYSZ:
11913 name = ".preinit_array";
4ade44b7 11914 goto get_out_size;
c152c796
AM
11915 case DT_INIT_ARRAYSZ:
11916 name = ".init_array";
4ade44b7 11917 goto get_out_size;
c152c796
AM
11918 case DT_FINI_ARRAYSZ:
11919 name = ".fini_array";
4ade44b7 11920 get_out_size:
c152c796
AM
11921 o = bfd_get_section_by_name (abfd, name);
11922 if (o == NULL)
11923 {
11924 (*_bfd_error_handler)
4ade44b7 11925 (_("could not find section %s"), name);
c152c796
AM
11926 goto error_return;
11927 }
eea6121a 11928 if (o->size == 0)
c152c796
AM
11929 (*_bfd_error_handler)
11930 (_("warning: %s section has zero size"), name);
eea6121a 11931 dyn.d_un.d_val = o->size;
c152c796
AM
11932 break;
11933
11934 case DT_PREINIT_ARRAY:
11935 name = ".preinit_array";
4ade44b7 11936 goto get_out_vma;
c152c796
AM
11937 case DT_INIT_ARRAY:
11938 name = ".init_array";
4ade44b7 11939 goto get_out_vma;
c152c796
AM
11940 case DT_FINI_ARRAY:
11941 name = ".fini_array";
4ade44b7
AM
11942 get_out_vma:
11943 o = bfd_get_section_by_name (abfd, name);
11944 goto do_vma;
c152c796
AM
11945
11946 case DT_HASH:
11947 name = ".hash";
11948 goto get_vma;
fdc90cb4
JJ
11949 case DT_GNU_HASH:
11950 name = ".gnu.hash";
11951 goto get_vma;
c152c796
AM
11952 case DT_STRTAB:
11953 name = ".dynstr";
11954 goto get_vma;
11955 case DT_SYMTAB:
11956 name = ".dynsym";
11957 goto get_vma;
11958 case DT_VERDEF:
11959 name = ".gnu.version_d";
11960 goto get_vma;
11961 case DT_VERNEED:
11962 name = ".gnu.version_r";
11963 goto get_vma;
11964 case DT_VERSYM:
11965 name = ".gnu.version";
11966 get_vma:
4ade44b7
AM
11967 o = bfd_get_linker_section (dynobj, name);
11968 do_vma:
c152c796
AM
11969 if (o == NULL)
11970 {
11971 (*_bfd_error_handler)
4ade44b7 11972 (_("could not find section %s"), name);
c152c796
AM
11973 goto error_return;
11974 }
894891db
NC
11975 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11976 {
11977 (*_bfd_error_handler)
11978 (_("warning: section '%s' is being made into a note"), name);
11979 bfd_set_error (bfd_error_nonrepresentable_section);
11980 goto error_return;
11981 }
4ade44b7 11982 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
11983 break;
11984
11985 case DT_REL:
11986 case DT_RELA:
11987 case DT_RELSZ:
11988 case DT_RELASZ:
11989 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11990 type = SHT_REL;
11991 else
11992 type = SHT_RELA;
11993 dyn.d_un.d_val = 0;
bef26483 11994 dyn.d_un.d_ptr = 0;
c152c796
AM
11995 for (i = 1; i < elf_numsections (abfd); i++)
11996 {
11997 Elf_Internal_Shdr *hdr;
11998
11999 hdr = elf_elfsections (abfd)[i];
12000 if (hdr->sh_type == type
12001 && (hdr->sh_flags & SHF_ALLOC) != 0)
12002 {
12003 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12004 dyn.d_un.d_val += hdr->sh_size;
12005 else
12006 {
bef26483
AM
12007 if (dyn.d_un.d_ptr == 0
12008 || hdr->sh_addr < dyn.d_un.d_ptr)
12009 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
12010 }
12011 }
12012 }
12013 break;
12014 }
12015 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12016 }
12017 }
12018
12019 /* If we have created any dynamic sections, then output them. */
12020 if (dynobj != NULL)
12021 {
12022 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12023 goto error_return;
12024
943284cc 12025 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12026 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12027 || info->error_textrel)
3d4d4302 12028 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12029 {
12030 bfd_byte *dyncon, *dynconend;
12031
943284cc
DJ
12032 dyncon = o->contents;
12033 dynconend = o->contents + o->size;
12034 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12035 {
12036 Elf_Internal_Dyn dyn;
12037
12038 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12039
12040 if (dyn.d_tag == DT_TEXTREL)
12041 {
c192a133
AM
12042 if (info->error_textrel)
12043 info->callbacks->einfo
12044 (_("%P%X: read-only segment has dynamic relocations.\n"));
12045 else
12046 info->callbacks->einfo
12047 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12048 break;
12049 }
12050 }
12051 }
12052
c152c796
AM
12053 for (o = dynobj->sections; o != NULL; o = o->next)
12054 {
12055 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12056 || o->size == 0
c152c796
AM
12057 || o->output_section == bfd_abs_section_ptr)
12058 continue;
12059 if ((o->flags & SEC_LINKER_CREATED) == 0)
12060 {
12061 /* At this point, we are only interested in sections
12062 created by _bfd_elf_link_create_dynamic_sections. */
12063 continue;
12064 }
3722b82f
AM
12065 if (elf_hash_table (info)->stab_info.stabstr == o)
12066 continue;
eea6121a
AM
12067 if (elf_hash_table (info)->eh_info.hdr_sec == o)
12068 continue;
3d4d4302 12069 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12070 {
12071 if (! bfd_set_section_contents (abfd, o->output_section,
12072 o->contents,
37b01f6a
DG
12073 (file_ptr) o->output_offset
12074 * bfd_octets_per_byte (abfd),
eea6121a 12075 o->size))
c152c796
AM
12076 goto error_return;
12077 }
12078 else
12079 {
12080 /* The contents of the .dynstr section are actually in a
12081 stringtab. */
8539e4e8
AM
12082 file_ptr off;
12083
c152c796
AM
12084 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12085 if (bfd_seek (abfd, off, SEEK_SET) != 0
12086 || ! _bfd_elf_strtab_emit (abfd,
12087 elf_hash_table (info)->dynstr))
12088 goto error_return;
12089 }
12090 }
12091 }
12092
0e1862bb 12093 if (bfd_link_relocatable (info))
c152c796
AM
12094 {
12095 bfd_boolean failed = FALSE;
12096
12097 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12098 if (failed)
12099 goto error_return;
12100 }
12101
12102 /* If we have optimized stabs strings, output them. */
3722b82f 12103 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
12104 {
12105 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
12106 goto error_return;
12107 }
12108
9f7c3e5e
AM
12109 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12110 goto error_return;
c152c796 12111
9f7c3e5e 12112 elf_final_link_free (abfd, &flinfo);
c152c796 12113
12bd6957 12114 elf_linker (abfd) = TRUE;
c152c796 12115
104d59d1
JM
12116 if (attr_section)
12117 {
a50b1753 12118 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12119 if (contents == NULL)
d0f16d5e 12120 return FALSE; /* Bail out and fail. */
104d59d1
JM
12121 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12122 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12123 free (contents);
12124 }
12125
c152c796
AM
12126 return TRUE;
12127
12128 error_return:
9f7c3e5e 12129 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12130 return FALSE;
12131}
12132\f
5241d853
RS
12133/* Initialize COOKIE for input bfd ABFD. */
12134
12135static bfd_boolean
12136init_reloc_cookie (struct elf_reloc_cookie *cookie,
12137 struct bfd_link_info *info, bfd *abfd)
12138{
12139 Elf_Internal_Shdr *symtab_hdr;
12140 const struct elf_backend_data *bed;
12141
12142 bed = get_elf_backend_data (abfd);
12143 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12144
12145 cookie->abfd = abfd;
12146 cookie->sym_hashes = elf_sym_hashes (abfd);
12147 cookie->bad_symtab = elf_bad_symtab (abfd);
12148 if (cookie->bad_symtab)
12149 {
12150 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12151 cookie->extsymoff = 0;
12152 }
12153 else
12154 {
12155 cookie->locsymcount = symtab_hdr->sh_info;
12156 cookie->extsymoff = symtab_hdr->sh_info;
12157 }
12158
12159 if (bed->s->arch_size == 32)
12160 cookie->r_sym_shift = 8;
12161 else
12162 cookie->r_sym_shift = 32;
12163
12164 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12165 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12166 {
12167 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12168 cookie->locsymcount, 0,
12169 NULL, NULL, NULL);
12170 if (cookie->locsyms == NULL)
12171 {
12172 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12173 return FALSE;
12174 }
12175 if (info->keep_memory)
12176 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12177 }
12178 return TRUE;
12179}
12180
12181/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12182
12183static void
12184fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12185{
12186 Elf_Internal_Shdr *symtab_hdr;
12187
12188 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12189 if (cookie->locsyms != NULL
12190 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12191 free (cookie->locsyms);
12192}
12193
12194/* Initialize the relocation information in COOKIE for input section SEC
12195 of input bfd ABFD. */
12196
12197static bfd_boolean
12198init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12199 struct bfd_link_info *info, bfd *abfd,
12200 asection *sec)
12201{
12202 const struct elf_backend_data *bed;
12203
12204 if (sec->reloc_count == 0)
12205 {
12206 cookie->rels = NULL;
12207 cookie->relend = NULL;
12208 }
12209 else
12210 {
12211 bed = get_elf_backend_data (abfd);
12212
12213 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12214 info->keep_memory);
12215 if (cookie->rels == NULL)
12216 return FALSE;
12217 cookie->rel = cookie->rels;
12218 cookie->relend = (cookie->rels
12219 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12220 }
12221 cookie->rel = cookie->rels;
12222 return TRUE;
12223}
12224
12225/* Free the memory allocated by init_reloc_cookie_rels,
12226 if appropriate. */
12227
12228static void
12229fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12230 asection *sec)
12231{
12232 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12233 free (cookie->rels);
12234}
12235
12236/* Initialize the whole of COOKIE for input section SEC. */
12237
12238static bfd_boolean
12239init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12240 struct bfd_link_info *info,
12241 asection *sec)
12242{
12243 if (!init_reloc_cookie (cookie, info, sec->owner))
12244 goto error1;
12245 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12246 goto error2;
12247 return TRUE;
12248
12249 error2:
12250 fini_reloc_cookie (cookie, sec->owner);
12251 error1:
12252 return FALSE;
12253}
12254
12255/* Free the memory allocated by init_reloc_cookie_for_section,
12256 if appropriate. */
12257
12258static void
12259fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12260 asection *sec)
12261{
12262 fini_reloc_cookie_rels (cookie, sec);
12263 fini_reloc_cookie (cookie, sec->owner);
12264}
12265\f
c152c796
AM
12266/* Garbage collect unused sections. */
12267
07adf181
AM
12268/* Default gc_mark_hook. */
12269
12270asection *
12271_bfd_elf_gc_mark_hook (asection *sec,
12272 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12273 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12274 struct elf_link_hash_entry *h,
12275 Elf_Internal_Sym *sym)
12276{
12277 if (h != NULL)
12278 {
12279 switch (h->root.type)
12280 {
12281 case bfd_link_hash_defined:
12282 case bfd_link_hash_defweak:
12283 return h->root.u.def.section;
12284
12285 case bfd_link_hash_common:
12286 return h->root.u.c.p->section;
12287
12288 default:
12289 break;
12290 }
12291 }
12292 else
12293 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12294
12295 return NULL;
12296}
12297
a6a4679f
AM
12298/* For undefined __start_<name> and __stop_<name> symbols, return the
12299 first input section matching <name>. Return NULL otherwise. */
12300
12301asection *
12302_bfd_elf_is_start_stop (const struct bfd_link_info *info,
12303 struct elf_link_hash_entry *h)
12304{
12305 asection *s;
12306 const char *sec_name;
12307
12308 if (h->root.type != bfd_link_hash_undefined
12309 && h->root.type != bfd_link_hash_undefweak)
12310 return NULL;
12311
12312 s = h->root.u.undef.section;
12313 if (s != NULL)
12314 {
12315 if (s == (asection *) 0 - 1)
12316 return NULL;
12317 return s;
12318 }
12319
12320 sec_name = NULL;
12321 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12322 sec_name = h->root.root.string + 8;
12323 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12324 sec_name = h->root.root.string + 7;
12325
12326 if (sec_name != NULL && *sec_name != '\0')
12327 {
12328 bfd *i;
12329
12330 for (i = info->input_bfds; i != NULL; i = i->link.next)
12331 {
12332 s = bfd_get_section_by_name (i, sec_name);
12333 if (s != NULL)
12334 {
12335 h->root.u.undef.section = s;
12336 break;
12337 }
12338 }
12339 }
12340
12341 if (s == NULL)
12342 h->root.u.undef.section = (asection *) 0 - 1;
12343
12344 return s;
12345}
12346
5241d853
RS
12347/* COOKIE->rel describes a relocation against section SEC, which is
12348 a section we've decided to keep. Return the section that contains
12349 the relocation symbol, or NULL if no section contains it. */
12350
12351asection *
12352_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12353 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12354 struct elf_reloc_cookie *cookie,
12355 bfd_boolean *start_stop)
5241d853
RS
12356{
12357 unsigned long r_symndx;
12358 struct elf_link_hash_entry *h;
12359
12360 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12361 if (r_symndx == STN_UNDEF)
5241d853
RS
12362 return NULL;
12363
12364 if (r_symndx >= cookie->locsymcount
12365 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12366 {
12367 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12368 if (h == NULL)
12369 {
12370 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12371 sec->owner);
12372 return NULL;
12373 }
5241d853
RS
12374 while (h->root.type == bfd_link_hash_indirect
12375 || h->root.type == bfd_link_hash_warning)
12376 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12377 h->mark = 1;
4e6b54a6
AM
12378 /* If this symbol is weak and there is a non-weak definition, we
12379 keep the non-weak definition because many backends put
12380 dynamic reloc info on the non-weak definition for code
12381 handling copy relocs. */
12382 if (h->u.weakdef != NULL)
12383 h->u.weakdef->mark = 1;
1cce69b9 12384
a6a4679f 12385 if (start_stop != NULL)
1cce69b9
AM
12386 {
12387 /* To work around a glibc bug, mark all XXX input sections
12388 when there is an as yet undefined reference to __start_XXX
12389 or __stop_XXX symbols. The linker will later define such
12390 symbols for orphan input sections that have a name
12391 representable as a C identifier. */
a6a4679f 12392 asection *s = _bfd_elf_is_start_stop (info, h);
1cce69b9 12393
a6a4679f 12394 if (s != NULL)
1cce69b9 12395 {
a6a4679f
AM
12396 *start_stop = !s->gc_mark;
12397 return s;
1cce69b9
AM
12398 }
12399 }
12400
5241d853
RS
12401 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12402 }
12403
12404 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12405 &cookie->locsyms[r_symndx]);
12406}
12407
12408/* COOKIE->rel describes a relocation against section SEC, which is
12409 a section we've decided to keep. Mark the section that contains
9d0a14d3 12410 the relocation symbol. */
5241d853
RS
12411
12412bfd_boolean
12413_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12414 asection *sec,
12415 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12416 struct elf_reloc_cookie *cookie)
5241d853
RS
12417{
12418 asection *rsec;
1cce69b9 12419 bfd_boolean start_stop = FALSE;
5241d853 12420
1cce69b9
AM
12421 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12422 while (rsec != NULL)
5241d853 12423 {
1cce69b9
AM
12424 if (!rsec->gc_mark)
12425 {
12426 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12427 || (rsec->owner->flags & DYNAMIC) != 0)
12428 rsec->gc_mark = 1;
12429 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12430 return FALSE;
12431 }
12432 if (!start_stop)
12433 break;
199af150 12434 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12435 }
12436 return TRUE;
12437}
12438
07adf181
AM
12439/* The mark phase of garbage collection. For a given section, mark
12440 it and any sections in this section's group, and all the sections
12441 which define symbols to which it refers. */
12442
ccfa59ea
AM
12443bfd_boolean
12444_bfd_elf_gc_mark (struct bfd_link_info *info,
12445 asection *sec,
6a5bb875 12446 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12447{
12448 bfd_boolean ret;
9d0a14d3 12449 asection *group_sec, *eh_frame;
c152c796
AM
12450
12451 sec->gc_mark = 1;
12452
12453 /* Mark all the sections in the group. */
12454 group_sec = elf_section_data (sec)->next_in_group;
12455 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12456 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12457 return FALSE;
12458
12459 /* Look through the section relocs. */
12460 ret = TRUE;
9d0a14d3
RS
12461 eh_frame = elf_eh_frame_section (sec->owner);
12462 if ((sec->flags & SEC_RELOC) != 0
12463 && sec->reloc_count > 0
12464 && sec != eh_frame)
c152c796 12465 {
5241d853 12466 struct elf_reloc_cookie cookie;
c152c796 12467
5241d853
RS
12468 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12469 ret = FALSE;
c152c796 12470 else
c152c796 12471 {
5241d853 12472 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12473 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12474 {
12475 ret = FALSE;
12476 break;
12477 }
12478 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12479 }
12480 }
9d0a14d3
RS
12481
12482 if (ret && eh_frame && elf_fde_list (sec))
12483 {
12484 struct elf_reloc_cookie cookie;
12485
12486 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12487 ret = FALSE;
12488 else
12489 {
12490 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12491 gc_mark_hook, &cookie))
12492 ret = FALSE;
12493 fini_reloc_cookie_for_section (&cookie, eh_frame);
12494 }
12495 }
12496
2f0c68f2
CM
12497 eh_frame = elf_section_eh_frame_entry (sec);
12498 if (ret && eh_frame && !eh_frame->gc_mark)
12499 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12500 ret = FALSE;
12501
c152c796
AM
12502 return ret;
12503}
12504
3c758495
TG
12505/* Scan and mark sections in a special or debug section group. */
12506
12507static void
12508_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12509{
12510 /* Point to first section of section group. */
12511 asection *ssec;
12512 /* Used to iterate the section group. */
12513 asection *msec;
12514
12515 bfd_boolean is_special_grp = TRUE;
12516 bfd_boolean is_debug_grp = TRUE;
12517
12518 /* First scan to see if group contains any section other than debug
12519 and special section. */
12520 ssec = msec = elf_next_in_group (grp);
12521 do
12522 {
12523 if ((msec->flags & SEC_DEBUGGING) == 0)
12524 is_debug_grp = FALSE;
12525
12526 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12527 is_special_grp = FALSE;
12528
12529 msec = elf_next_in_group (msec);
12530 }
12531 while (msec != ssec);
12532
12533 /* If this is a pure debug section group or pure special section group,
12534 keep all sections in this group. */
12535 if (is_debug_grp || is_special_grp)
12536 {
12537 do
12538 {
12539 msec->gc_mark = 1;
12540 msec = elf_next_in_group (msec);
12541 }
12542 while (msec != ssec);
12543 }
12544}
12545
7f6ab9f8
AM
12546/* Keep debug and special sections. */
12547
12548bfd_boolean
12549_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12550 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12551{
12552 bfd *ibfd;
12553
c72f2fb2 12554 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12555 {
12556 asection *isec;
12557 bfd_boolean some_kept;
b40bf0a2 12558 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12559
12560 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12561 continue;
12562
b40bf0a2
NC
12563 /* Ensure all linker created sections are kept,
12564 see if any other section is already marked,
12565 and note if we have any fragmented debug sections. */
12566 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12567 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12568 {
12569 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12570 isec->gc_mark = 1;
12571 else if (isec->gc_mark)
12572 some_kept = TRUE;
b40bf0a2
NC
12573
12574 if (debug_frag_seen == FALSE
12575 && (isec->flags & SEC_DEBUGGING)
12576 && CONST_STRNEQ (isec->name, ".debug_line."))
12577 debug_frag_seen = TRUE;
7f6ab9f8
AM
12578 }
12579
12580 /* If no section in this file will be kept, then we can
b40bf0a2 12581 toss out the debug and special sections. */
7f6ab9f8
AM
12582 if (!some_kept)
12583 continue;
12584
12585 /* Keep debug and special sections like .comment when they are
3c758495
TG
12586 not part of a group. Also keep section groups that contain
12587 just debug sections or special sections. */
7f6ab9f8 12588 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12589 {
12590 if ((isec->flags & SEC_GROUP) != 0)
12591 _bfd_elf_gc_mark_debug_special_section_group (isec);
12592 else if (((isec->flags & SEC_DEBUGGING) != 0
12593 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12594 && elf_next_in_group (isec) == NULL)
12595 isec->gc_mark = 1;
12596 }
b40bf0a2
NC
12597
12598 if (! debug_frag_seen)
12599 continue;
12600
12601 /* Look for CODE sections which are going to be discarded,
12602 and find and discard any fragmented debug sections which
12603 are associated with that code section. */
12604 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12605 if ((isec->flags & SEC_CODE) != 0
12606 && isec->gc_mark == 0)
12607 {
12608 unsigned int ilen;
12609 asection *dsec;
12610
12611 ilen = strlen (isec->name);
12612
12613 /* Association is determined by the name of the debug section
12614 containing the name of the code section as a suffix. For
12615 example .debug_line.text.foo is a debug section associated
12616 with .text.foo. */
12617 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12618 {
12619 unsigned int dlen;
12620
12621 if (dsec->gc_mark == 0
12622 || (dsec->flags & SEC_DEBUGGING) == 0)
12623 continue;
12624
12625 dlen = strlen (dsec->name);
12626
12627 if (dlen > ilen
12628 && strncmp (dsec->name + (dlen - ilen),
12629 isec->name, ilen) == 0)
12630 {
12631 dsec->gc_mark = 0;
b40bf0a2
NC
12632 }
12633 }
12634 }
7f6ab9f8
AM
12635 }
12636 return TRUE;
12637}
12638
c152c796
AM
12639/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12640
c17d87de
NC
12641struct elf_gc_sweep_symbol_info
12642{
ccabcbe5
AM
12643 struct bfd_link_info *info;
12644 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12645 bfd_boolean);
12646};
12647
c152c796 12648static bfd_boolean
ccabcbe5 12649elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 12650{
1d5316ab
AM
12651 if (!h->mark
12652 && (((h->root.type == bfd_link_hash_defined
12653 || h->root.type == bfd_link_hash_defweak)
c4621b33 12654 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6673f753 12655 && h->root.u.def.section->gc_mark))
1d5316ab
AM
12656 || h->root.type == bfd_link_hash_undefined
12657 || h->root.type == bfd_link_hash_undefweak))
12658 {
12659 struct elf_gc_sweep_symbol_info *inf;
12660
12661 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 12662 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
12663 h->def_regular = 0;
12664 h->ref_regular = 0;
12665 h->ref_regular_nonweak = 0;
ccabcbe5 12666 }
c152c796
AM
12667
12668 return TRUE;
12669}
12670
12671/* The sweep phase of garbage collection. Remove all garbage sections. */
12672
12673typedef bfd_boolean (*gc_sweep_hook_fn)
12674 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12675
12676static bfd_boolean
ccabcbe5 12677elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12678{
12679 bfd *sub;
ccabcbe5
AM
12680 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12681 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12682 unsigned long section_sym_count;
12683 struct elf_gc_sweep_symbol_info sweep_info;
c152c796 12684
c72f2fb2 12685 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12686 {
12687 asection *o;
12688
b19a8f85
L
12689 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12690 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12691 continue;
12692
12693 for (o = sub->sections; o != NULL; o = o->next)
12694 {
a33dafc3
L
12695 /* When any section in a section group is kept, we keep all
12696 sections in the section group. If the first member of
12697 the section group is excluded, we will also exclude the
12698 group section. */
12699 if (o->flags & SEC_GROUP)
12700 {
12701 asection *first = elf_next_in_group (o);
12702 o->gc_mark = first->gc_mark;
12703 }
c152c796 12704
1e7eae0d 12705 if (o->gc_mark)
c152c796
AM
12706 continue;
12707
12708 /* Skip sweeping sections already excluded. */
12709 if (o->flags & SEC_EXCLUDE)
12710 continue;
12711
12712 /* Since this is early in the link process, it is simple
12713 to remove a section from the output. */
12714 o->flags |= SEC_EXCLUDE;
12715
c55fe096 12716 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
12717 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12718
c152c796
AM
12719 /* But we also have to update some of the relocation
12720 info we collected before. */
12721 if (gc_sweep_hook
e8aaee2a 12722 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12723 && o->reloc_count != 0
12724 && !((info->strip == strip_all || info->strip == strip_debugger)
12725 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12726 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12727 {
12728 Elf_Internal_Rela *internal_relocs;
12729 bfd_boolean r;
12730
12731 internal_relocs
12732 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12733 info->keep_memory);
12734 if (internal_relocs == NULL)
12735 return FALSE;
12736
12737 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12738
12739 if (elf_section_data (o)->relocs != internal_relocs)
12740 free (internal_relocs);
12741
12742 if (!r)
12743 return FALSE;
12744 }
12745 }
12746 }
12747
12748 /* Remove the symbols that were in the swept sections from the dynamic
12749 symbol table. GCFIXME: Anyone know how to get them out of the
12750 static symbol table as well? */
ccabcbe5
AM
12751 sweep_info.info = info;
12752 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12753 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12754 &sweep_info);
c152c796 12755
ccabcbe5 12756 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
12757 return TRUE;
12758}
12759
12760/* Propagate collected vtable information. This is called through
12761 elf_link_hash_traverse. */
12762
12763static bfd_boolean
12764elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12765{
c152c796 12766 /* Those that are not vtables. */
f6e332e6 12767 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12768 return TRUE;
12769
12770 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12771 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12772 return TRUE;
12773
12774 /* If we've already been done, exit. */
f6e332e6 12775 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12776 return TRUE;
12777
12778 /* Make sure the parent's table is up to date. */
f6e332e6 12779 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 12780
f6e332e6 12781 if (h->vtable->used == NULL)
c152c796
AM
12782 {
12783 /* None of this table's entries were referenced. Re-use the
12784 parent's table. */
f6e332e6
AM
12785 h->vtable->used = h->vtable->parent->vtable->used;
12786 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
12787 }
12788 else
12789 {
12790 size_t n;
12791 bfd_boolean *cu, *pu;
12792
12793 /* Or the parent's entries into ours. */
f6e332e6 12794 cu = h->vtable->used;
c152c796 12795 cu[-1] = TRUE;
f6e332e6 12796 pu = h->vtable->parent->vtable->used;
c152c796
AM
12797 if (pu != NULL)
12798 {
12799 const struct elf_backend_data *bed;
12800 unsigned int log_file_align;
12801
12802 bed = get_elf_backend_data (h->root.u.def.section->owner);
12803 log_file_align = bed->s->log_file_align;
f6e332e6 12804 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
12805 while (n--)
12806 {
12807 if (*pu)
12808 *cu = TRUE;
12809 pu++;
12810 cu++;
12811 }
12812 }
12813 }
12814
12815 return TRUE;
12816}
12817
12818static bfd_boolean
12819elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12820{
12821 asection *sec;
12822 bfd_vma hstart, hend;
12823 Elf_Internal_Rela *relstart, *relend, *rel;
12824 const struct elf_backend_data *bed;
12825 unsigned int log_file_align;
12826
c152c796
AM
12827 /* Take care of both those symbols that do not describe vtables as
12828 well as those that are not loaded. */
f6e332e6 12829 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12830 return TRUE;
12831
12832 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12833 || h->root.type == bfd_link_hash_defweak);
12834
12835 sec = h->root.u.def.section;
12836 hstart = h->root.u.def.value;
12837 hend = hstart + h->size;
12838
12839 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12840 if (!relstart)
12841 return *(bfd_boolean *) okp = FALSE;
12842 bed = get_elf_backend_data (sec->owner);
12843 log_file_align = bed->s->log_file_align;
12844
12845 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12846
12847 for (rel = relstart; rel < relend; ++rel)
12848 if (rel->r_offset >= hstart && rel->r_offset < hend)
12849 {
12850 /* If the entry is in use, do nothing. */
f6e332e6
AM
12851 if (h->vtable->used
12852 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
12853 {
12854 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 12855 if (h->vtable->used[entry])
c152c796
AM
12856 continue;
12857 }
12858 /* Otherwise, kill it. */
12859 rel->r_offset = rel->r_info = rel->r_addend = 0;
12860 }
12861
12862 return TRUE;
12863}
12864
87538722
AM
12865/* Mark sections containing dynamically referenced symbols. When
12866 building shared libraries, we must assume that any visible symbol is
12867 referenced. */
715df9b8 12868
64d03ab5
AM
12869bfd_boolean
12870bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 12871{
87538722 12872 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 12873 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 12874
715df9b8
EB
12875 if ((h->root.type == bfd_link_hash_defined
12876 || h->root.type == bfd_link_hash_defweak)
87538722 12877 && (h->ref_dynamic
c4621b33 12878 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 12879 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 12880 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 12881 && (!bfd_link_executable (info)
b407645f
AM
12882 || info->export_dynamic
12883 || (h->dynamic
12884 && d != NULL
12885 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 12886 && (h->versioned >= versioned
54e8959c
L
12887 || !bfd_hide_sym_by_version (info->version_info,
12888 h->root.root.string)))))
715df9b8
EB
12889 h->root.u.def.section->flags |= SEC_KEEP;
12890
12891 return TRUE;
12892}
3b36f7e6 12893
74f0fb50
AM
12894/* Keep all sections containing symbols undefined on the command-line,
12895 and the section containing the entry symbol. */
12896
12897void
12898_bfd_elf_gc_keep (struct bfd_link_info *info)
12899{
12900 struct bfd_sym_chain *sym;
12901
12902 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12903 {
12904 struct elf_link_hash_entry *h;
12905
12906 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12907 FALSE, FALSE, FALSE);
12908
12909 if (h != NULL
12910 && (h->root.type == bfd_link_hash_defined
12911 || h->root.type == bfd_link_hash_defweak)
12912 && !bfd_is_abs_section (h->root.u.def.section))
12913 h->root.u.def.section->flags |= SEC_KEEP;
12914 }
12915}
12916
2f0c68f2
CM
12917bfd_boolean
12918bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12919 struct bfd_link_info *info)
12920{
12921 bfd *ibfd = info->input_bfds;
12922
12923 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12924 {
12925 asection *sec;
12926 struct elf_reloc_cookie cookie;
12927
12928 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12929 continue;
12930
12931 if (!init_reloc_cookie (&cookie, info, ibfd))
12932 return FALSE;
12933
12934 for (sec = ibfd->sections; sec; sec = sec->next)
12935 {
12936 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12937 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12938 {
12939 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12940 fini_reloc_cookie_rels (&cookie, sec);
12941 }
12942 }
12943 }
12944 return TRUE;
12945}
12946
c152c796
AM
12947/* Do mark and sweep of unused sections. */
12948
12949bfd_boolean
12950bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12951{
12952 bfd_boolean ok = TRUE;
12953 bfd *sub;
6a5bb875 12954 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 12955 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 12956 struct elf_link_hash_table *htab;
c152c796 12957
64d03ab5 12958 if (!bed->can_gc_sections
715df9b8 12959 || !is_elf_hash_table (info->hash))
c152c796
AM
12960 {
12961 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12962 return TRUE;
12963 }
12964
74f0fb50 12965 bed->gc_keep (info);
da44f4e5 12966 htab = elf_hash_table (info);
74f0fb50 12967
9d0a14d3
RS
12968 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12969 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
12970 for (sub = info->input_bfds;
12971 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12972 sub = sub->link.next)
9d0a14d3
RS
12973 {
12974 asection *sec;
12975 struct elf_reloc_cookie cookie;
12976
12977 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 12978 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
12979 {
12980 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
12981 if (elf_section_data (sec)->sec_info
12982 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
12983 elf_eh_frame_section (sub) = sec;
12984 fini_reloc_cookie_for_section (&cookie, sec);
199af150 12985 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
12986 }
12987 }
9d0a14d3 12988
c152c796 12989 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 12990 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
12991 if (!ok)
12992 return FALSE;
12993
12994 /* Kill the vtable relocations that were not used. */
da44f4e5 12995 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
12996 if (!ok)
12997 return FALSE;
12998
715df9b8 12999 /* Mark dynamically referenced symbols. */
da44f4e5
AM
13000 if (htab->dynamic_sections_created)
13001 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13002
715df9b8 13003 /* Grovel through relocs to find out who stays ... */
64d03ab5 13004 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13005 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13006 {
13007 asection *o;
13008
b19a8f85
L
13009 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13010 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13011 continue;
13012
7f6ab9f8
AM
13013 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13014 Also treat note sections as a root, if the section is not part
13015 of a group. */
c152c796 13016 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13017 if (!o->gc_mark
13018 && (o->flags & SEC_EXCLUDE) == 0
24007750 13019 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13020 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13021 && elf_next_in_group (o) == NULL )))
13022 {
13023 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13024 return FALSE;
13025 }
c152c796
AM
13026 }
13027
6a5bb875 13028 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13029 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13030
c152c796 13031 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13032 return elf_gc_sweep (abfd, info);
c152c796
AM
13033}
13034\f
13035/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13036
13037bfd_boolean
13038bfd_elf_gc_record_vtinherit (bfd *abfd,
13039 asection *sec,
13040 struct elf_link_hash_entry *h,
13041 bfd_vma offset)
13042{
13043 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13044 struct elf_link_hash_entry **search, *child;
ef53be89 13045 size_t extsymcount;
c152c796
AM
13046 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13047
13048 /* The sh_info field of the symtab header tells us where the
13049 external symbols start. We don't care about the local symbols at
13050 this point. */
13051 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13052 if (!elf_bad_symtab (abfd))
13053 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13054
13055 sym_hashes = elf_sym_hashes (abfd);
13056 sym_hashes_end = sym_hashes + extsymcount;
13057
13058 /* Hunt down the child symbol, which is in this section at the same
13059 offset as the relocation. */
13060 for (search = sym_hashes; search != sym_hashes_end; ++search)
13061 {
13062 if ((child = *search) != NULL
13063 && (child->root.type == bfd_link_hash_defined
13064 || child->root.type == bfd_link_hash_defweak)
13065 && child->root.u.def.section == sec
13066 && child->root.u.def.value == offset)
13067 goto win;
13068 }
13069
d003868e
AM
13070 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
13071 abfd, sec, (unsigned long) offset);
c152c796
AM
13072 bfd_set_error (bfd_error_invalid_operation);
13073 return FALSE;
13074
13075 win:
f6e332e6
AM
13076 if (!child->vtable)
13077 {
ca4be51c
AM
13078 child->vtable = ((struct elf_link_virtual_table_entry *)
13079 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
13080 if (!child->vtable)
13081 return FALSE;
13082 }
c152c796
AM
13083 if (!h)
13084 {
13085 /* This *should* only be the absolute section. It could potentially
13086 be that someone has defined a non-global vtable though, which
13087 would be bad. It isn't worth paging in the local symbols to be
13088 sure though; that case should simply be handled by the assembler. */
13089
f6e332e6 13090 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13091 }
13092 else
f6e332e6 13093 child->vtable->parent = h;
c152c796
AM
13094
13095 return TRUE;
13096}
13097
13098/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13099
13100bfd_boolean
13101bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13102 asection *sec ATTRIBUTE_UNUSED,
13103 struct elf_link_hash_entry *h,
13104 bfd_vma addend)
13105{
13106 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13107 unsigned int log_file_align = bed->s->log_file_align;
13108
f6e332e6
AM
13109 if (!h->vtable)
13110 {
ca4be51c
AM
13111 h->vtable = ((struct elf_link_virtual_table_entry *)
13112 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
13113 if (!h->vtable)
13114 return FALSE;
13115 }
13116
13117 if (addend >= h->vtable->size)
c152c796
AM
13118 {
13119 size_t size, bytes, file_align;
f6e332e6 13120 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
13121
13122 /* While the symbol is undefined, we have to be prepared to handle
13123 a zero size. */
13124 file_align = 1 << log_file_align;
13125 if (h->root.type == bfd_link_hash_undefined)
13126 size = addend + file_align;
13127 else
13128 {
13129 size = h->size;
13130 if (addend >= size)
13131 {
13132 /* Oops! We've got a reference past the defined end of
13133 the table. This is probably a bug -- shall we warn? */
13134 size = addend + file_align;
13135 }
13136 }
13137 size = (size + file_align - 1) & -file_align;
13138
13139 /* Allocate one extra entry for use as a "done" flag for the
13140 consolidation pass. */
13141 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13142
13143 if (ptr)
13144 {
a50b1753 13145 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13146
13147 if (ptr != NULL)
13148 {
13149 size_t oldbytes;
13150
f6e332e6 13151 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
13152 * sizeof (bfd_boolean));
13153 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13154 }
13155 }
13156 else
a50b1753 13157 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13158
13159 if (ptr == NULL)
13160 return FALSE;
13161
13162 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
13163 h->vtable->used = ptr + 1;
13164 h->vtable->size = size;
c152c796
AM
13165 }
13166
f6e332e6 13167 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13168
13169 return TRUE;
13170}
13171
ae17ab41
CM
13172/* Map an ELF section header flag to its corresponding string. */
13173typedef struct
13174{
13175 char *flag_name;
13176 flagword flag_value;
13177} elf_flags_to_name_table;
13178
13179static elf_flags_to_name_table elf_flags_to_names [] =
13180{
13181 { "SHF_WRITE", SHF_WRITE },
13182 { "SHF_ALLOC", SHF_ALLOC },
13183 { "SHF_EXECINSTR", SHF_EXECINSTR },
13184 { "SHF_MERGE", SHF_MERGE },
13185 { "SHF_STRINGS", SHF_STRINGS },
13186 { "SHF_INFO_LINK", SHF_INFO_LINK},
13187 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13188 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13189 { "SHF_GROUP", SHF_GROUP },
13190 { "SHF_TLS", SHF_TLS },
13191 { "SHF_MASKOS", SHF_MASKOS },
13192 { "SHF_EXCLUDE", SHF_EXCLUDE },
13193};
13194
b9c361e0
JL
13195/* Returns TRUE if the section is to be included, otherwise FALSE. */
13196bfd_boolean
ae17ab41 13197bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13198 struct flag_info *flaginfo,
b9c361e0 13199 asection *section)
ae17ab41 13200{
8b127cbc 13201 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13202
8b127cbc 13203 if (!flaginfo->flags_initialized)
ae17ab41 13204 {
8b127cbc
AM
13205 bfd *obfd = info->output_bfd;
13206 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13207 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13208 int with_hex = 0;
13209 int without_hex = 0;
13210
8b127cbc 13211 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13212 {
b9c361e0 13213 unsigned i;
8b127cbc 13214 flagword (*lookup) (char *);
ae17ab41 13215
8b127cbc
AM
13216 lookup = bed->elf_backend_lookup_section_flags_hook;
13217 if (lookup != NULL)
ae17ab41 13218 {
8b127cbc 13219 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13220
13221 if (hexval != 0)
13222 {
13223 if (tf->with == with_flags)
13224 with_hex |= hexval;
13225 else if (tf->with == without_flags)
13226 without_hex |= hexval;
13227 tf->valid = TRUE;
13228 continue;
13229 }
ae17ab41 13230 }
8b127cbc 13231 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13232 {
8b127cbc 13233 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13234 {
13235 if (tf->with == with_flags)
13236 with_hex |= elf_flags_to_names[i].flag_value;
13237 else if (tf->with == without_flags)
13238 without_hex |= elf_flags_to_names[i].flag_value;
13239 tf->valid = TRUE;
13240 break;
13241 }
13242 }
8b127cbc 13243 if (!tf->valid)
b9c361e0 13244 {
68ffbac6 13245 info->callbacks->einfo
8b127cbc 13246 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13247 return FALSE;
ae17ab41
CM
13248 }
13249 }
8b127cbc
AM
13250 flaginfo->flags_initialized = TRUE;
13251 flaginfo->only_with_flags |= with_hex;
13252 flaginfo->not_with_flags |= without_hex;
ae17ab41 13253 }
ae17ab41 13254
8b127cbc 13255 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13256 return FALSE;
13257
8b127cbc 13258 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13259 return FALSE;
13260
13261 return TRUE;
ae17ab41
CM
13262}
13263
c152c796
AM
13264struct alloc_got_off_arg {
13265 bfd_vma gotoff;
10455f89 13266 struct bfd_link_info *info;
c152c796
AM
13267};
13268
13269/* We need a special top-level link routine to convert got reference counts
13270 to real got offsets. */
13271
13272static bfd_boolean
13273elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13274{
a50b1753 13275 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13276 bfd *obfd = gofarg->info->output_bfd;
13277 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13278
c152c796
AM
13279 if (h->got.refcount > 0)
13280 {
13281 h->got.offset = gofarg->gotoff;
10455f89 13282 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13283 }
13284 else
13285 h->got.offset = (bfd_vma) -1;
13286
13287 return TRUE;
13288}
13289
13290/* And an accompanying bit to work out final got entry offsets once
13291 we're done. Should be called from final_link. */
13292
13293bfd_boolean
13294bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13295 struct bfd_link_info *info)
13296{
13297 bfd *i;
13298 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13299 bfd_vma gotoff;
c152c796
AM
13300 struct alloc_got_off_arg gofarg;
13301
10455f89
HPN
13302 BFD_ASSERT (abfd == info->output_bfd);
13303
c152c796
AM
13304 if (! is_elf_hash_table (info->hash))
13305 return FALSE;
13306
13307 /* The GOT offset is relative to the .got section, but the GOT header is
13308 put into the .got.plt section, if the backend uses it. */
13309 if (bed->want_got_plt)
13310 gotoff = 0;
13311 else
13312 gotoff = bed->got_header_size;
13313
13314 /* Do the local .got entries first. */
c72f2fb2 13315 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13316 {
13317 bfd_signed_vma *local_got;
ef53be89 13318 size_t j, locsymcount;
c152c796
AM
13319 Elf_Internal_Shdr *symtab_hdr;
13320
13321 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13322 continue;
13323
13324 local_got = elf_local_got_refcounts (i);
13325 if (!local_got)
13326 continue;
13327
13328 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13329 if (elf_bad_symtab (i))
13330 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13331 else
13332 locsymcount = symtab_hdr->sh_info;
13333
13334 for (j = 0; j < locsymcount; ++j)
13335 {
13336 if (local_got[j] > 0)
13337 {
13338 local_got[j] = gotoff;
10455f89 13339 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13340 }
13341 else
13342 local_got[j] = (bfd_vma) -1;
13343 }
13344 }
13345
13346 /* Then the global .got entries. .plt refcounts are handled by
13347 adjust_dynamic_symbol */
13348 gofarg.gotoff = gotoff;
10455f89 13349 gofarg.info = info;
c152c796
AM
13350 elf_link_hash_traverse (elf_hash_table (info),
13351 elf_gc_allocate_got_offsets,
13352 &gofarg);
13353 return TRUE;
13354}
13355
13356/* Many folk need no more in the way of final link than this, once
13357 got entry reference counting is enabled. */
13358
13359bfd_boolean
13360bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13361{
13362 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13363 return FALSE;
13364
13365 /* Invoke the regular ELF backend linker to do all the work. */
13366 return bfd_elf_final_link (abfd, info);
13367}
13368
13369bfd_boolean
13370bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13371{
a50b1753 13372 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13373
13374 if (rcookie->bad_symtab)
13375 rcookie->rel = rcookie->rels;
13376
13377 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13378 {
13379 unsigned long r_symndx;
13380
13381 if (! rcookie->bad_symtab)
13382 if (rcookie->rel->r_offset > offset)
13383 return FALSE;
13384 if (rcookie->rel->r_offset != offset)
13385 continue;
13386
13387 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13388 if (r_symndx == STN_UNDEF)
c152c796
AM
13389 return TRUE;
13390
13391 if (r_symndx >= rcookie->locsymcount
13392 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13393 {
13394 struct elf_link_hash_entry *h;
13395
13396 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13397
13398 while (h->root.type == bfd_link_hash_indirect
13399 || h->root.type == bfd_link_hash_warning)
13400 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13401
13402 if ((h->root.type == bfd_link_hash_defined
13403 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13404 && (h->root.u.def.section->owner != rcookie->abfd
13405 || h->root.u.def.section->kept_section != NULL
13406 || discarded_section (h->root.u.def.section)))
c152c796 13407 return TRUE;
c152c796
AM
13408 }
13409 else
13410 {
13411 /* It's not a relocation against a global symbol,
13412 but it could be a relocation against a local
13413 symbol for a discarded section. */
13414 asection *isec;
13415 Elf_Internal_Sym *isym;
13416
13417 /* Need to: get the symbol; get the section. */
13418 isym = &rcookie->locsyms[r_symndx];
cb33740c 13419 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13420 if (isec != NULL
13421 && (isec->kept_section != NULL
13422 || discarded_section (isec)))
cb33740c 13423 return TRUE;
c152c796
AM
13424 }
13425 return FALSE;
13426 }
13427 return FALSE;
13428}
13429
13430/* Discard unneeded references to discarded sections.
75938853
AM
13431 Returns -1 on error, 1 if any section's size was changed, 0 if
13432 nothing changed. This function assumes that the relocations are in
13433 sorted order, which is true for all known assemblers. */
c152c796 13434
75938853 13435int
c152c796
AM
13436bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13437{
13438 struct elf_reloc_cookie cookie;
18cd5bce 13439 asection *o;
c152c796 13440 bfd *abfd;
75938853 13441 int changed = 0;
c152c796
AM
13442
13443 if (info->traditional_format
13444 || !is_elf_hash_table (info->hash))
75938853 13445 return 0;
c152c796 13446
18cd5bce
AM
13447 o = bfd_get_section_by_name (output_bfd, ".stab");
13448 if (o != NULL)
c152c796 13449 {
18cd5bce 13450 asection *i;
c152c796 13451
18cd5bce 13452 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13453 {
18cd5bce
AM
13454 if (i->size == 0
13455 || i->reloc_count == 0
13456 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13457 continue;
c152c796 13458
18cd5bce
AM
13459 abfd = i->owner;
13460 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13461 continue;
c152c796 13462
18cd5bce 13463 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13464 return -1;
c152c796 13465
18cd5bce
AM
13466 if (_bfd_discard_section_stabs (abfd, i,
13467 elf_section_data (i)->sec_info,
5241d853
RS
13468 bfd_elf_reloc_symbol_deleted_p,
13469 &cookie))
75938853 13470 changed = 1;
18cd5bce
AM
13471
13472 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13473 }
18cd5bce
AM
13474 }
13475
2f0c68f2
CM
13476 o = NULL;
13477 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13478 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13479 if (o != NULL)
13480 {
13481 asection *i;
c152c796 13482
18cd5bce 13483 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13484 {
18cd5bce
AM
13485 if (i->size == 0)
13486 continue;
13487
13488 abfd = i->owner;
13489 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13490 continue;
13491
13492 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13493 return -1;
18cd5bce
AM
13494
13495 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13496 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13497 bfd_elf_reloc_symbol_deleted_p,
13498 &cookie))
75938853 13499 changed = 1;
18cd5bce
AM
13500
13501 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13502 }
18cd5bce 13503 }
c152c796 13504
18cd5bce
AM
13505 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13506 {
13507 const struct elf_backend_data *bed;
c152c796 13508
18cd5bce
AM
13509 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13510 continue;
13511
13512 bed = get_elf_backend_data (abfd);
13513
13514 if (bed->elf_backend_discard_info != NULL)
13515 {
13516 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13517 return -1;
18cd5bce
AM
13518
13519 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13520 changed = 1;
18cd5bce
AM
13521
13522 fini_reloc_cookie (&cookie, abfd);
13523 }
c152c796
AM
13524 }
13525
2f0c68f2
CM
13526 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13527 _bfd_elf_end_eh_frame_parsing (info);
13528
13529 if (info->eh_frame_hdr_type
0e1862bb 13530 && !bfd_link_relocatable (info)
c152c796 13531 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13532 changed = 1;
c152c796 13533
75938853 13534 return changed;
c152c796 13535}
082b7297 13536
43e1669b 13537bfd_boolean
0c511000 13538_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13539 asection *sec,
c0f00686 13540 struct bfd_link_info *info)
082b7297
L
13541{
13542 flagword flags;
c77ec726 13543 const char *name, *key;
082b7297
L
13544 struct bfd_section_already_linked *l;
13545 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13546
c77ec726
AM
13547 if (sec->output_section == bfd_abs_section_ptr)
13548 return FALSE;
0c511000 13549
c77ec726 13550 flags = sec->flags;
0c511000 13551
c77ec726
AM
13552 /* Return if it isn't a linkonce section. A comdat group section
13553 also has SEC_LINK_ONCE set. */
13554 if ((flags & SEC_LINK_ONCE) == 0)
13555 return FALSE;
0c511000 13556
c77ec726
AM
13557 /* Don't put group member sections on our list of already linked
13558 sections. They are handled as a group via their group section. */
13559 if (elf_sec_group (sec) != NULL)
13560 return FALSE;
0c511000 13561
c77ec726
AM
13562 /* For a SHT_GROUP section, use the group signature as the key. */
13563 name = sec->name;
13564 if ((flags & SEC_GROUP) != 0
13565 && elf_next_in_group (sec) != NULL
13566 && elf_group_name (elf_next_in_group (sec)) != NULL)
13567 key = elf_group_name (elf_next_in_group (sec));
13568 else
13569 {
13570 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13571 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13572 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13573 key++;
0c511000 13574 else
c77ec726
AM
13575 /* Must be a user linkonce section that doesn't follow gcc's
13576 naming convention. In this case we won't be matching
13577 single member groups. */
13578 key = name;
0c511000 13579 }
6d2cd210 13580
c77ec726 13581 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13582
13583 for (l = already_linked_list->entry; l != NULL; l = l->next)
13584 {
c2370991 13585 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13586 sections with a signature of <key> (<key> is some string),
13587 and linkonce sections named .gnu.linkonce.<type>.<key>.
13588 Match like sections. LTO plugin sections are an exception.
13589 They are always named .gnu.linkonce.t.<key> and match either
13590 type of section. */
13591 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13592 && ((flags & SEC_GROUP) != 0
13593 || strcmp (name, l->sec->name) == 0))
13594 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13595 {
13596 /* The section has already been linked. See if we should
6d2cd210 13597 issue a warning. */
c77ec726
AM
13598 if (!_bfd_handle_already_linked (sec, l, info))
13599 return FALSE;
082b7297 13600
c77ec726 13601 if (flags & SEC_GROUP)
3d7f7666 13602 {
c77ec726
AM
13603 asection *first = elf_next_in_group (sec);
13604 asection *s = first;
3d7f7666 13605
c77ec726 13606 while (s != NULL)
3d7f7666 13607 {
c77ec726
AM
13608 s->output_section = bfd_abs_section_ptr;
13609 /* Record which group discards it. */
13610 s->kept_section = l->sec;
13611 s = elf_next_in_group (s);
13612 /* These lists are circular. */
13613 if (s == first)
13614 break;
3d7f7666
L
13615 }
13616 }
082b7297 13617
43e1669b 13618 return TRUE;
082b7297
L
13619 }
13620 }
13621
c77ec726
AM
13622 /* A single member comdat group section may be discarded by a
13623 linkonce section and vice versa. */
13624 if ((flags & SEC_GROUP) != 0)
3d7f7666 13625 {
c77ec726 13626 asection *first = elf_next_in_group (sec);
c2370991 13627
c77ec726
AM
13628 if (first != NULL && elf_next_in_group (first) == first)
13629 /* Check this single member group against linkonce sections. */
13630 for (l = already_linked_list->entry; l != NULL; l = l->next)
13631 if ((l->sec->flags & SEC_GROUP) == 0
13632 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13633 {
13634 first->output_section = bfd_abs_section_ptr;
13635 first->kept_section = l->sec;
13636 sec->output_section = bfd_abs_section_ptr;
13637 break;
13638 }
13639 }
13640 else
13641 /* Check this linkonce section against single member groups. */
13642 for (l = already_linked_list->entry; l != NULL; l = l->next)
13643 if (l->sec->flags & SEC_GROUP)
6d2cd210 13644 {
c77ec726 13645 asection *first = elf_next_in_group (l->sec);
6d2cd210 13646
c77ec726
AM
13647 if (first != NULL
13648 && elf_next_in_group (first) == first
13649 && bfd_elf_match_symbols_in_sections (first, sec, info))
13650 {
13651 sec->output_section = bfd_abs_section_ptr;
13652 sec->kept_section = first;
13653 break;
13654 }
6d2cd210 13655 }
0c511000 13656
c77ec726
AM
13657 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13658 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13659 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13660 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13661 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13662 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13663 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13664 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13665 The reverse order cannot happen as there is never a bfd with only the
13666 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13667 matter as here were are looking only for cross-bfd sections. */
13668
13669 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13670 for (l = already_linked_list->entry; l != NULL; l = l->next)
13671 if ((l->sec->flags & SEC_GROUP) == 0
13672 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13673 {
13674 if (abfd != l->sec->owner)
13675 sec->output_section = bfd_abs_section_ptr;
13676 break;
13677 }
80c29487 13678
082b7297 13679 /* This is the first section with this name. Record it. */
c77ec726 13680 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13681 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13682 return sec->output_section == bfd_abs_section_ptr;
082b7297 13683}
81e1b023 13684
a4d8e49b
L
13685bfd_boolean
13686_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13687{
13688 return sym->st_shndx == SHN_COMMON;
13689}
13690
13691unsigned int
13692_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13693{
13694 return SHN_COMMON;
13695}
13696
13697asection *
13698_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13699{
13700 return bfd_com_section_ptr;
13701}
10455f89
HPN
13702
13703bfd_vma
13704_bfd_elf_default_got_elt_size (bfd *abfd,
13705 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13706 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13707 bfd *ibfd ATTRIBUTE_UNUSED,
13708 unsigned long symndx ATTRIBUTE_UNUSED)
13709{
13710 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13711 return bed->s->arch_size / 8;
13712}
83bac4b0
NC
13713
13714/* Routines to support the creation of dynamic relocs. */
13715
83bac4b0
NC
13716/* Returns the name of the dynamic reloc section associated with SEC. */
13717
13718static const char *
13719get_dynamic_reloc_section_name (bfd * abfd,
13720 asection * sec,
13721 bfd_boolean is_rela)
13722{
ddcf1fcf
BS
13723 char *name;
13724 const char *old_name = bfd_get_section_name (NULL, sec);
13725 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13726
ddcf1fcf 13727 if (old_name == NULL)
83bac4b0
NC
13728 return NULL;
13729
ddcf1fcf 13730 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13731 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13732
13733 return name;
13734}
13735
13736/* Returns the dynamic reloc section associated with SEC.
13737 If necessary compute the name of the dynamic reloc section based
13738 on SEC's name (looked up in ABFD's string table) and the setting
13739 of IS_RELA. */
13740
13741asection *
13742_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13743 asection * sec,
13744 bfd_boolean is_rela)
13745{
13746 asection * reloc_sec = elf_section_data (sec)->sreloc;
13747
13748 if (reloc_sec == NULL)
13749 {
13750 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13751
13752 if (name != NULL)
13753 {
3d4d4302 13754 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
13755
13756 if (reloc_sec != NULL)
13757 elf_section_data (sec)->sreloc = reloc_sec;
13758 }
13759 }
13760
13761 return reloc_sec;
13762}
13763
13764/* Returns the dynamic reloc section associated with SEC. If the
13765 section does not exist it is created and attached to the DYNOBJ
13766 bfd and stored in the SRELOC field of SEC's elf_section_data
13767 structure.
f8076f98 13768
83bac4b0
NC
13769 ALIGNMENT is the alignment for the newly created section and
13770 IS_RELA defines whether the name should be .rela.<SEC's name>
13771 or .rel.<SEC's name>. The section name is looked up in the
13772 string table associated with ABFD. */
13773
13774asection *
ca4be51c
AM
13775_bfd_elf_make_dynamic_reloc_section (asection *sec,
13776 bfd *dynobj,
13777 unsigned int alignment,
13778 bfd *abfd,
13779 bfd_boolean is_rela)
83bac4b0
NC
13780{
13781 asection * reloc_sec = elf_section_data (sec)->sreloc;
13782
13783 if (reloc_sec == NULL)
13784 {
13785 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13786
13787 if (name == NULL)
13788 return NULL;
13789
3d4d4302 13790 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
13791
13792 if (reloc_sec == NULL)
13793 {
3d4d4302
AM
13794 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13795 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
13796 if ((sec->flags & SEC_ALLOC) != 0)
13797 flags |= SEC_ALLOC | SEC_LOAD;
13798
3d4d4302 13799 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
13800 if (reloc_sec != NULL)
13801 {
8877b5e5
AM
13802 /* _bfd_elf_get_sec_type_attr chooses a section type by
13803 name. Override as it may be wrong, eg. for a user
13804 section named "auto" we'll get ".relauto" which is
13805 seen to be a .rela section. */
13806 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
13807 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13808 reloc_sec = NULL;
13809 }
13810 }
13811
13812 elf_section_data (sec)->sreloc = reloc_sec;
13813 }
13814
13815 return reloc_sec;
13816}
1338dd10 13817
bffebb6b
AM
13818/* Copy the ELF symbol type and other attributes for a linker script
13819 assignment from HSRC to HDEST. Generally this should be treated as
13820 if we found a strong non-dynamic definition for HDEST (except that
13821 ld ignores multiple definition errors). */
1338dd10 13822void
bffebb6b
AM
13823_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13824 struct bfd_link_hash_entry *hdest,
13825 struct bfd_link_hash_entry *hsrc)
1338dd10 13826{
bffebb6b
AM
13827 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13828 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13829 Elf_Internal_Sym isym;
1338dd10
PB
13830
13831 ehdest->type = ehsrc->type;
35fc36a8 13832 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
13833
13834 isym.st_other = ehsrc->other;
b8417128 13835 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 13836}
351f65ca
L
13837
13838/* Append a RELA relocation REL to section S in BFD. */
13839
13840void
13841elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13842{
13843 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13844 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13845 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13846 bed->s->swap_reloca_out (abfd, rel, loc);
13847}
13848
13849/* Append a REL relocation REL to section S in BFD. */
13850
13851void
13852elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13853{
13854 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13855 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13856 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 13857 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 13858}
This page took 2.20139 seconds and 4 git commands to generate.