Automatic date update in version.in
[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;
252b5132
RH
471 bfd_size_type indx;
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
RH
521
522 if (indx == (bfd_size_type) -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;
701 unsigned long dynstr_index;
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);
8c58d23b
AM
756 if (dynstr_index == (unsigned long) -1)
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
580a2b6e
L
1205 /* When we try to create a default indirect symbol from the dynamic
1206 definition with the default version, we skip it if its type and
40101021 1207 the type of existing regular definition mismatch. */
580a2b6e 1208 if (pold_alignment == NULL
580a2b6e
L
1209 && newdyn
1210 && newdef
1211 && !olddyn
4584ec12
L
1212 && (((olddef || h->root.type == bfd_link_hash_common)
1213 && ELF_ST_TYPE (sym->st_info) != h->type
1214 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1215 && h->type != STT_NOTYPE
1216 && !(newfunc && oldfunc))
1217 || (olddef
1218 && ((h->type == STT_GNU_IFUNC)
1219 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
580a2b6e
L
1220 {
1221 *skip = TRUE;
1222 return TRUE;
1223 }
1224
4c34aff8
AM
1225 /* Check TLS symbols. We don't check undefined symbols introduced
1226 by "ld -u" which have no type (and oldbfd NULL), and we don't
1227 check symbols from plugins because they also have no type. */
1228 if (oldbfd != NULL
1229 && (oldbfd->flags & BFD_PLUGIN) == 0
1230 && (abfd->flags & BFD_PLUGIN) == 0
1231 && ELF_ST_TYPE (sym->st_info) != h->type
1232 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1233 {
1234 bfd *ntbfd, *tbfd;
1235 bfd_boolean ntdef, tdef;
1236 asection *ntsec, *tsec;
1237
1238 if (h->type == STT_TLS)
1239 {
3b36f7e6 1240 ntbfd = abfd;
7479dfd4
L
1241 ntsec = sec;
1242 ntdef = newdef;
1243 tbfd = oldbfd;
1244 tsec = oldsec;
1245 tdef = olddef;
1246 }
1247 else
1248 {
1249 ntbfd = oldbfd;
1250 ntsec = oldsec;
1251 ntdef = olddef;
1252 tbfd = abfd;
1253 tsec = sec;
1254 tdef = newdef;
1255 }
1256
1257 if (tdef && ntdef)
1258 (*_bfd_error_handler)
191c0c42
AM
1259 (_("%s: TLS definition in %B section %A "
1260 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1261 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1262 else if (!tdef && !ntdef)
1263 (*_bfd_error_handler)
191c0c42
AM
1264 (_("%s: TLS reference in %B "
1265 "mismatches non-TLS reference in %B"),
7479dfd4
L
1266 tbfd, ntbfd, h->root.root.string);
1267 else if (tdef)
1268 (*_bfd_error_handler)
191c0c42
AM
1269 (_("%s: TLS definition in %B section %A "
1270 "mismatches non-TLS reference in %B"),
7479dfd4
L
1271 tbfd, tsec, ntbfd, h->root.root.string);
1272 else
1273 (*_bfd_error_handler)
191c0c42
AM
1274 (_("%s: TLS reference in %B "
1275 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1276 tbfd, ntbfd, ntsec, h->root.root.string);
1277
1278 bfd_set_error (bfd_error_bad_value);
1279 return FALSE;
1280 }
1281
45d6a902
AM
1282 /* If the old symbol has non-default visibility, we ignore the new
1283 definition from a dynamic object. */
1284 if (newdyn
9c7a29a3 1285 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1286 && !bfd_is_und_section (sec))
1287 {
1288 *skip = TRUE;
1289 /* Make sure this symbol is dynamic. */
f5385ebf 1290 h->ref_dynamic = 1;
90c984fc 1291 hi->ref_dynamic = 1;
45d6a902
AM
1292 /* A protected symbol has external availability. Make sure it is
1293 recorded as dynamic.
1294
1295 FIXME: Should we check type and size for protected symbol? */
1296 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1297 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1298 else
1299 return TRUE;
1300 }
1301 else if (!newdyn
9c7a29a3 1302 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1303 && h->def_dynamic)
45d6a902
AM
1304 {
1305 /* If the new symbol with non-default visibility comes from a
1306 relocatable file and the old definition comes from a dynamic
1307 object, we remove the old definition. */
6c9b78e6 1308 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1309 {
1310 /* Handle the case where the old dynamic definition is
1311 default versioned. We need to copy the symbol info from
1312 the symbol with default version to the normal one if it
1313 was referenced before. */
1314 if (h->ref_regular)
1315 {
6c9b78e6 1316 hi->root.type = h->root.type;
d2dee3b2 1317 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1318 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1319
6c9b78e6 1320 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1321 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1322 {
aed81c4e
MR
1323 /* If the new symbol is hidden or internal, completely undo
1324 any dynamic link state. */
1325 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1326 h->forced_local = 0;
1327 h->ref_dynamic = 0;
d2dee3b2
L
1328 }
1329 else
aed81c4e
MR
1330 h->ref_dynamic = 1;
1331
1332 h->def_dynamic = 0;
aed81c4e
MR
1333 /* FIXME: Should we check type and size for protected symbol? */
1334 h->size = 0;
1335 h->type = 0;
1336
6c9b78e6 1337 h = hi;
d2dee3b2
L
1338 }
1339 else
6c9b78e6 1340 h = hi;
d2dee3b2 1341 }
1de1a317 1342
f5eda473
AM
1343 /* If the old symbol was undefined before, then it will still be
1344 on the undefs list. If the new symbol is undefined or
1345 common, we can't make it bfd_link_hash_new here, because new
1346 undefined or common symbols will be added to the undefs list
1347 by _bfd_generic_link_add_one_symbol. Symbols may not be
1348 added twice to the undefs list. Also, if the new symbol is
1349 undefweak then we don't want to lose the strong undef. */
1350 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1351 {
1de1a317 1352 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1353 h->root.u.undef.abfd = abfd;
1354 }
1355 else
1356 {
1357 h->root.type = bfd_link_hash_new;
1358 h->root.u.undef.abfd = NULL;
1359 }
1360
f5eda473 1361 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1362 {
f5eda473
AM
1363 /* If the new symbol is hidden or internal, completely undo
1364 any dynamic link state. */
1365 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1366 h->forced_local = 0;
1367 h->ref_dynamic = 0;
45d6a902 1368 }
f5eda473
AM
1369 else
1370 h->ref_dynamic = 1;
1371 h->def_dynamic = 0;
45d6a902
AM
1372 /* FIXME: Should we check type and size for protected symbol? */
1373 h->size = 0;
1374 h->type = 0;
1375 return TRUE;
1376 }
14a793b2 1377
15b43f48
AM
1378 /* If a new weak symbol definition comes from a regular file and the
1379 old symbol comes from a dynamic library, we treat the new one as
1380 strong. Similarly, an old weak symbol definition from a regular
1381 file is treated as strong when the new symbol comes from a dynamic
1382 library. Further, an old weak symbol from a dynamic library is
1383 treated as strong if the new symbol is from a dynamic library.
1384 This reflects the way glibc's ld.so works.
1385
1386 Do this before setting *type_change_ok or *size_change_ok so that
1387 we warn properly when dynamic library symbols are overridden. */
1388
1389 if (newdef && !newdyn && olddyn)
0f8a2703 1390 newweak = FALSE;
15b43f48 1391 if (olddef && newdyn)
0f8a2703
AM
1392 oldweak = FALSE;
1393
d334575b 1394 /* Allow changes between different types of function symbol. */
0a36a439 1395 if (newfunc && oldfunc)
fcb93ecf
PB
1396 *type_change_ok = TRUE;
1397
79349b09
AM
1398 /* It's OK to change the type if either the existing symbol or the
1399 new symbol is weak. A type change is also OK if the old symbol
1400 is undefined and the new symbol is defined. */
252b5132 1401
79349b09
AM
1402 if (oldweak
1403 || newweak
1404 || (newdef
1405 && h->root.type == bfd_link_hash_undefined))
1406 *type_change_ok = TRUE;
1407
1408 /* It's OK to change the size if either the existing symbol or the
1409 new symbol is weak, or if the old symbol is undefined. */
1410
1411 if (*type_change_ok
1412 || h->root.type == bfd_link_hash_undefined)
1413 *size_change_ok = TRUE;
45d6a902 1414
45d6a902
AM
1415 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1416 symbol, respectively, appears to be a common symbol in a dynamic
1417 object. If a symbol appears in an uninitialized section, and is
1418 not weak, and is not a function, then it may be a common symbol
1419 which was resolved when the dynamic object was created. We want
1420 to treat such symbols specially, because they raise special
1421 considerations when setting the symbol size: if the symbol
1422 appears as a common symbol in a regular object, and the size in
1423 the regular object is larger, we must make sure that we use the
1424 larger size. This problematic case can always be avoided in C,
1425 but it must be handled correctly when using Fortran shared
1426 libraries.
1427
1428 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1429 likewise for OLDDYNCOMMON and OLDDEF.
1430
1431 Note that this test is just a heuristic, and that it is quite
1432 possible to have an uninitialized symbol in a shared object which
1433 is really a definition, rather than a common symbol. This could
1434 lead to some minor confusion when the symbol really is a common
1435 symbol in some regular object. However, I think it will be
1436 harmless. */
1437
1438 if (newdyn
1439 && newdef
79349b09 1440 && !newweak
45d6a902
AM
1441 && (sec->flags & SEC_ALLOC) != 0
1442 && (sec->flags & SEC_LOAD) == 0
1443 && sym->st_size > 0
0a36a439 1444 && !newfunc)
45d6a902
AM
1445 newdyncommon = TRUE;
1446 else
1447 newdyncommon = FALSE;
1448
1449 if (olddyn
1450 && olddef
1451 && h->root.type == bfd_link_hash_defined
f5385ebf 1452 && h->def_dynamic
45d6a902
AM
1453 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1454 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1455 && h->size > 0
0a36a439 1456 && !oldfunc)
45d6a902
AM
1457 olddyncommon = TRUE;
1458 else
1459 olddyncommon = FALSE;
1460
a4d8e49b
L
1461 /* We now know everything about the old and new symbols. We ask the
1462 backend to check if we can merge them. */
5d13b3b3
AM
1463 if (bed->merge_symbol != NULL)
1464 {
1465 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1466 return FALSE;
1467 sec = *psec;
1468 }
a4d8e49b 1469
45d6a902
AM
1470 /* If both the old and the new symbols look like common symbols in a
1471 dynamic object, set the size of the symbol to the larger of the
1472 two. */
1473
1474 if (olddyncommon
1475 && newdyncommon
1476 && sym->st_size != h->size)
1477 {
1478 /* Since we think we have two common symbols, issue a multiple
1479 common warning if desired. Note that we only warn if the
1480 size is different. If the size is the same, we simply let
1481 the old symbol override the new one as normally happens with
1482 symbols defined in dynamic objects. */
1483
1484 if (! ((*info->callbacks->multiple_common)
24f58f47 1485 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902 1486 return FALSE;
252b5132 1487
45d6a902
AM
1488 if (sym->st_size > h->size)
1489 h->size = sym->st_size;
252b5132 1490
45d6a902 1491 *size_change_ok = TRUE;
252b5132
RH
1492 }
1493
45d6a902
AM
1494 /* If we are looking at a dynamic object, and we have found a
1495 definition, we need to see if the symbol was already defined by
1496 some other object. If so, we want to use the existing
1497 definition, and we do not want to report a multiple symbol
1498 definition error; we do this by clobbering *PSEC to be
1499 bfd_und_section_ptr.
1500
1501 We treat a common symbol as a definition if the symbol in the
1502 shared library is a function, since common symbols always
1503 represent variables; this can cause confusion in principle, but
1504 any such confusion would seem to indicate an erroneous program or
1505 shared library. We also permit a common symbol in a regular
202ac193
L
1506 object to override a weak symbol in a shared object. A common
1507 symbol in executable also overrides a symbol in a shared object. */
45d6a902
AM
1508
1509 if (newdyn
1510 && newdef
77cfaee6 1511 && (olddef
45d6a902 1512 || (h->root.type == bfd_link_hash_common
202ac193
L
1513 && (newweak
1514 || newfunc
1515 || (!olddyn && bfd_link_executable (info))))))
45d6a902
AM
1516 {
1517 *override = TRUE;
1518 newdef = FALSE;
1519 newdyncommon = FALSE;
252b5132 1520
45d6a902
AM
1521 *psec = sec = bfd_und_section_ptr;
1522 *size_change_ok = TRUE;
252b5132 1523
45d6a902
AM
1524 /* If we get here when the old symbol is a common symbol, then
1525 we are explicitly letting it override a weak symbol or
1526 function in a dynamic object, and we don't want to warn about
1527 a type change. If the old symbol is a defined symbol, a type
1528 change warning may still be appropriate. */
252b5132 1529
45d6a902
AM
1530 if (h->root.type == bfd_link_hash_common)
1531 *type_change_ok = TRUE;
1532 }
1533
1534 /* Handle the special case of an old common symbol merging with a
1535 new symbol which looks like a common symbol in a shared object.
1536 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1537 common symbol, and let _bfd_generic_link_add_one_symbol do the
1538 right thing. */
45d6a902
AM
1539
1540 if (newdyncommon
1541 && h->root.type == bfd_link_hash_common)
1542 {
1543 *override = TRUE;
1544 newdef = FALSE;
1545 newdyncommon = FALSE;
1546 *pvalue = sym->st_size;
a4d8e49b 1547 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1548 *size_change_ok = TRUE;
1549 }
1550
c5e2cead 1551 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1552 if (newdef && olddef && newweak)
54ac0771 1553 {
35ed3f94 1554 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1555 if (!(oldbfd != NULL
1556 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1557 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1558 {
1559 newdef = FALSE;
1560 *skip = TRUE;
1561 }
54ac0771
L
1562
1563 /* Merge st_other. If the symbol already has a dynamic index,
1564 but visibility says it should not be visible, turn it into a
1565 local symbol. */
b8417128 1566 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1567 if (h->dynindx != -1)
1568 switch (ELF_ST_VISIBILITY (h->other))
1569 {
1570 case STV_INTERNAL:
1571 case STV_HIDDEN:
1572 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1573 break;
1574 }
1575 }
c5e2cead 1576
45d6a902
AM
1577 /* If the old symbol is from a dynamic object, and the new symbol is
1578 a definition which is not from a dynamic object, then the new
1579 symbol overrides the old symbol. Symbols from regular files
1580 always take precedence over symbols from dynamic objects, even if
1581 they are defined after the dynamic object in the link.
1582
1583 As above, we again permit a common symbol in a regular object to
1584 override a definition in a shared object if the shared object
0f8a2703 1585 symbol is a function or is weak. */
45d6a902
AM
1586
1587 flip = NULL;
77cfaee6 1588 if (!newdyn
45d6a902
AM
1589 && (newdef
1590 || (bfd_is_com_section (sec)
0a36a439 1591 && (oldweak || oldfunc)))
45d6a902
AM
1592 && olddyn
1593 && olddef
f5385ebf 1594 && h->def_dynamic)
45d6a902
AM
1595 {
1596 /* Change the hash table entry to undefined, and let
1597 _bfd_generic_link_add_one_symbol do the right thing with the
1598 new definition. */
1599
1600 h->root.type = bfd_link_hash_undefined;
1601 h->root.u.undef.abfd = h->root.u.def.section->owner;
1602 *size_change_ok = TRUE;
1603
1604 olddef = FALSE;
1605 olddyncommon = FALSE;
1606
1607 /* We again permit a type change when a common symbol may be
1608 overriding a function. */
1609
1610 if (bfd_is_com_section (sec))
0a36a439
L
1611 {
1612 if (oldfunc)
1613 {
1614 /* If a common symbol overrides a function, make sure
1615 that it isn't defined dynamically nor has type
1616 function. */
1617 h->def_dynamic = 0;
1618 h->type = STT_NOTYPE;
1619 }
1620 *type_change_ok = TRUE;
1621 }
45d6a902 1622
6c9b78e6
AM
1623 if (hi->root.type == bfd_link_hash_indirect)
1624 flip = hi;
45d6a902
AM
1625 else
1626 /* This union may have been set to be non-NULL when this symbol
1627 was seen in a dynamic object. We must force the union to be
1628 NULL, so that it is correct for a regular symbol. */
1629 h->verinfo.vertree = NULL;
1630 }
1631
1632 /* Handle the special case of a new common symbol merging with an
1633 old symbol that looks like it might be a common symbol defined in
1634 a shared object. Note that we have already handled the case in
1635 which a new common symbol should simply override the definition
1636 in the shared library. */
1637
1638 if (! newdyn
1639 && bfd_is_com_section (sec)
1640 && olddyncommon)
1641 {
1642 /* It would be best if we could set the hash table entry to a
1643 common symbol, but we don't know what to use for the section
1644 or the alignment. */
1645 if (! ((*info->callbacks->multiple_common)
24f58f47 1646 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902
AM
1647 return FALSE;
1648
4cc11e76 1649 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1650 larger, pretend that the new symbol has its size. */
1651
1652 if (h->size > *pvalue)
1653 *pvalue = h->size;
1654
af44c138
L
1655 /* We need to remember the alignment required by the symbol
1656 in the dynamic object. */
1657 BFD_ASSERT (pold_alignment);
1658 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1659
1660 olddef = FALSE;
1661 olddyncommon = FALSE;
1662
1663 h->root.type = bfd_link_hash_undefined;
1664 h->root.u.undef.abfd = h->root.u.def.section->owner;
1665
1666 *size_change_ok = TRUE;
1667 *type_change_ok = TRUE;
1668
6c9b78e6
AM
1669 if (hi->root.type == bfd_link_hash_indirect)
1670 flip = hi;
45d6a902
AM
1671 else
1672 h->verinfo.vertree = NULL;
1673 }
1674
1675 if (flip != NULL)
1676 {
1677 /* Handle the case where we had a versioned symbol in a dynamic
1678 library and now find a definition in a normal object. In this
1679 case, we make the versioned symbol point to the normal one. */
45d6a902 1680 flip->root.type = h->root.type;
00cbee0a 1681 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1682 h->root.type = bfd_link_hash_indirect;
1683 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1684 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1685 if (h->def_dynamic)
45d6a902 1686 {
f5385ebf
AM
1687 h->def_dynamic = 0;
1688 flip->ref_dynamic = 1;
45d6a902
AM
1689 }
1690 }
1691
45d6a902
AM
1692 return TRUE;
1693}
1694
1695/* This function is called to create an indirect symbol from the
1696 default for the symbol with the default version if needed. The
4f3fedcf 1697 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1698 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1699
28caa186 1700static bfd_boolean
268b6b39
AM
1701_bfd_elf_add_default_symbol (bfd *abfd,
1702 struct bfd_link_info *info,
1703 struct elf_link_hash_entry *h,
1704 const char *name,
1705 Elf_Internal_Sym *sym,
4f3fedcf
AM
1706 asection *sec,
1707 bfd_vma value,
1708 bfd **poldbfd,
e3c9d234 1709 bfd_boolean *dynsym)
45d6a902
AM
1710{
1711 bfd_boolean type_change_ok;
1712 bfd_boolean size_change_ok;
1713 bfd_boolean skip;
1714 char *shortname;
1715 struct elf_link_hash_entry *hi;
1716 struct bfd_link_hash_entry *bh;
9c5bfbb7 1717 const struct elf_backend_data *bed;
45d6a902
AM
1718 bfd_boolean collect;
1719 bfd_boolean dynamic;
e3c9d234 1720 bfd_boolean override;
45d6a902
AM
1721 char *p;
1722 size_t len, shortlen;
ffd65175 1723 asection *tmp_sec;
6e33951e 1724 bfd_boolean matched;
45d6a902 1725
422f1182
L
1726 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1727 return TRUE;
1728
45d6a902
AM
1729 /* If this symbol has a version, and it is the default version, we
1730 create an indirect symbol from the default name to the fully
1731 decorated name. This will cause external references which do not
1732 specify a version to be bound to this version of the symbol. */
1733 p = strchr (name, ELF_VER_CHR);
422f1182
L
1734 if (h->versioned == unknown)
1735 {
1736 if (p == NULL)
1737 {
1738 h->versioned = unversioned;
1739 return TRUE;
1740 }
1741 else
1742 {
1743 if (p[1] != ELF_VER_CHR)
1744 {
1745 h->versioned = versioned_hidden;
1746 return TRUE;
1747 }
1748 else
1749 h->versioned = versioned;
1750 }
1751 }
4373f8af
L
1752 else
1753 {
1754 /* PR ld/19073: We may see an unversioned definition after the
1755 default version. */
1756 if (p == NULL)
1757 return TRUE;
1758 }
45d6a902 1759
45d6a902
AM
1760 bed = get_elf_backend_data (abfd);
1761 collect = bed->collect;
1762 dynamic = (abfd->flags & DYNAMIC) != 0;
1763
1764 shortlen = p - name;
a50b1753 1765 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1766 if (shortname == NULL)
1767 return FALSE;
1768 memcpy (shortname, name, shortlen);
1769 shortname[shortlen] = '\0';
1770
1771 /* We are going to create a new symbol. Merge it with any existing
1772 symbol with this name. For the purposes of the merge, act as
1773 though we were defining the symbol we just defined, although we
1774 actually going to define an indirect symbol. */
1775 type_change_ok = FALSE;
1776 size_change_ok = FALSE;
6e33951e 1777 matched = TRUE;
ffd65175
AM
1778 tmp_sec = sec;
1779 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1780 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1781 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1782 return FALSE;
1783
1784 if (skip)
1785 goto nondefault;
1786
1787 if (! override)
1788 {
c6e8a9a8 1789 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1790 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1791 {
1792 bh = &hi->root;
1793 if (! (_bfd_generic_link_add_one_symbol
1794 (info, abfd, shortname, BSF_INDIRECT,
1795 bfd_ind_section_ptr,
1796 0, name, FALSE, collect, &bh)))
1797 return FALSE;
1798 hi = (struct elf_link_hash_entry *) bh;
1799 }
45d6a902
AM
1800 }
1801 else
1802 {
1803 /* In this case the symbol named SHORTNAME is overriding the
1804 indirect symbol we want to add. We were planning on making
1805 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1806 is the name without a version. NAME is the fully versioned
1807 name, and it is the default version.
1808
1809 Overriding means that we already saw a definition for the
1810 symbol SHORTNAME in a regular object, and it is overriding
1811 the symbol defined in the dynamic object.
1812
1813 When this happens, we actually want to change NAME, the
1814 symbol we just added, to refer to SHORTNAME. This will cause
1815 references to NAME in the shared object to become references
1816 to SHORTNAME in the regular object. This is what we expect
1817 when we override a function in a shared object: that the
1818 references in the shared object will be mapped to the
1819 definition in the regular object. */
1820
1821 while (hi->root.type == bfd_link_hash_indirect
1822 || hi->root.type == bfd_link_hash_warning)
1823 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1824
1825 h->root.type = bfd_link_hash_indirect;
1826 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1827 if (h->def_dynamic)
45d6a902 1828 {
f5385ebf
AM
1829 h->def_dynamic = 0;
1830 hi->ref_dynamic = 1;
1831 if (hi->ref_regular
1832 || hi->def_regular)
45d6a902 1833 {
c152c796 1834 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1835 return FALSE;
1836 }
1837 }
1838
1839 /* Now set HI to H, so that the following code will set the
1840 other fields correctly. */
1841 hi = h;
1842 }
1843
fab4a87f
L
1844 /* Check if HI is a warning symbol. */
1845 if (hi->root.type == bfd_link_hash_warning)
1846 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1847
45d6a902
AM
1848 /* If there is a duplicate definition somewhere, then HI may not
1849 point to an indirect symbol. We will have reported an error to
1850 the user in that case. */
1851
1852 if (hi->root.type == bfd_link_hash_indirect)
1853 {
1854 struct elf_link_hash_entry *ht;
1855
45d6a902 1856 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1857 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1858
68c88cd4
AM
1859 /* A reference to the SHORTNAME symbol from a dynamic library
1860 will be satisfied by the versioned symbol at runtime. In
1861 effect, we have a reference to the versioned symbol. */
1862 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1863 hi->dynamic_def |= ht->dynamic_def;
1864
45d6a902
AM
1865 /* See if the new flags lead us to realize that the symbol must
1866 be dynamic. */
1867 if (! *dynsym)
1868 {
1869 if (! dynamic)
1870 {
0e1862bb 1871 if (! bfd_link_executable (info)
90c984fc 1872 || hi->def_dynamic
f5385ebf 1873 || hi->ref_dynamic)
45d6a902
AM
1874 *dynsym = TRUE;
1875 }
1876 else
1877 {
f5385ebf 1878 if (hi->ref_regular)
45d6a902
AM
1879 *dynsym = TRUE;
1880 }
1881 }
1882 }
1883
1884 /* We also need to define an indirection from the nondefault version
1885 of the symbol. */
1886
1887nondefault:
1888 len = strlen (name);
a50b1753 1889 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1890 if (shortname == NULL)
1891 return FALSE;
1892 memcpy (shortname, name, shortlen);
1893 memcpy (shortname + shortlen, p + 1, len - shortlen);
1894
1895 /* Once again, merge with any existing symbol. */
1896 type_change_ok = FALSE;
1897 size_change_ok = FALSE;
ffd65175
AM
1898 tmp_sec = sec;
1899 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1900 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1901 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1902 return FALSE;
1903
1904 if (skip)
1905 return TRUE;
1906
1907 if (override)
1908 {
1909 /* Here SHORTNAME is a versioned name, so we don't expect to see
1910 the type of override we do in the case above unless it is
4cc11e76 1911 overridden by a versioned definition. */
45d6a902
AM
1912 if (hi->root.type != bfd_link_hash_defined
1913 && hi->root.type != bfd_link_hash_defweak)
1914 (*_bfd_error_handler)
d003868e
AM
1915 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1916 abfd, shortname);
45d6a902
AM
1917 }
1918 else
1919 {
1920 bh = &hi->root;
1921 if (! (_bfd_generic_link_add_one_symbol
1922 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1923 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1924 return FALSE;
1925 hi = (struct elf_link_hash_entry *) bh;
1926
1927 /* If there is a duplicate definition somewhere, then HI may not
1928 point to an indirect symbol. We will have reported an error
1929 to the user in that case. */
1930
1931 if (hi->root.type == bfd_link_hash_indirect)
1932 {
fcfa13d2 1933 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
1934 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1935 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
1936
1937 /* See if the new flags lead us to realize that the symbol
1938 must be dynamic. */
1939 if (! *dynsym)
1940 {
1941 if (! dynamic)
1942 {
0e1862bb 1943 if (! bfd_link_executable (info)
f5385ebf 1944 || hi->ref_dynamic)
45d6a902
AM
1945 *dynsym = TRUE;
1946 }
1947 else
1948 {
f5385ebf 1949 if (hi->ref_regular)
45d6a902
AM
1950 *dynsym = TRUE;
1951 }
1952 }
1953 }
1954 }
1955
1956 return TRUE;
1957}
1958\f
1959/* This routine is used to export all defined symbols into the dynamic
1960 symbol table. It is called via elf_link_hash_traverse. */
1961
28caa186 1962static bfd_boolean
268b6b39 1963_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1964{
a50b1753 1965 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
1966
1967 /* Ignore indirect symbols. These are added by the versioning code. */
1968 if (h->root.type == bfd_link_hash_indirect)
1969 return TRUE;
1970
7686d77d
AM
1971 /* Ignore this if we won't export it. */
1972 if (!eif->info->export_dynamic && !h->dynamic)
1973 return TRUE;
45d6a902
AM
1974
1975 if (h->dynindx == -1
fd91d419
L
1976 && (h->def_regular || h->ref_regular)
1977 && ! bfd_hide_sym_by_version (eif->info->version_info,
1978 h->root.root.string))
45d6a902 1979 {
fd91d419 1980 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 1981 {
fd91d419
L
1982 eif->failed = TRUE;
1983 return FALSE;
45d6a902
AM
1984 }
1985 }
1986
1987 return TRUE;
1988}
1989\f
1990/* Look through the symbols which are defined in other shared
1991 libraries and referenced here. Update the list of version
1992 dependencies. This will be put into the .gnu.version_r section.
1993 This function is called via elf_link_hash_traverse. */
1994
28caa186 1995static bfd_boolean
268b6b39
AM
1996_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1997 void *data)
45d6a902 1998{
a50b1753 1999 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2000 Elf_Internal_Verneed *t;
2001 Elf_Internal_Vernaux *a;
2002 bfd_size_type amt;
2003
45d6a902
AM
2004 /* We only care about symbols defined in shared objects with version
2005 information. */
f5385ebf
AM
2006 if (!h->def_dynamic
2007 || h->def_regular
45d6a902 2008 || h->dynindx == -1
7b20f099
AM
2009 || h->verinfo.verdef == NULL
2010 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2011 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2012 return TRUE;
2013
2014 /* See if we already know about this version. */
28caa186
AM
2015 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2016 t != NULL;
2017 t = t->vn_nextref)
45d6a902
AM
2018 {
2019 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2020 continue;
2021
2022 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2023 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2024 return TRUE;
2025
2026 break;
2027 }
2028
2029 /* This is a new version. Add it to tree we are building. */
2030
2031 if (t == NULL)
2032 {
2033 amt = sizeof *t;
a50b1753 2034 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2035 if (t == NULL)
2036 {
2037 rinfo->failed = TRUE;
2038 return FALSE;
2039 }
2040
2041 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2042 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2043 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2044 }
2045
2046 amt = sizeof *a;
a50b1753 2047 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2048 if (a == NULL)
2049 {
2050 rinfo->failed = TRUE;
2051 return FALSE;
2052 }
45d6a902
AM
2053
2054 /* Note that we are copying a string pointer here, and testing it
2055 above. If bfd_elf_string_from_elf_section is ever changed to
2056 discard the string data when low in memory, this will have to be
2057 fixed. */
2058 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2059
2060 a->vna_flags = h->verinfo.verdef->vd_flags;
2061 a->vna_nextptr = t->vn_auxptr;
2062
2063 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2064 ++rinfo->vers;
2065
2066 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2067
2068 t->vn_auxptr = a;
2069
2070 return TRUE;
2071}
2072
2073/* Figure out appropriate versions for all the symbols. We may not
2074 have the version number script until we have read all of the input
2075 files, so until that point we don't know which symbols should be
2076 local. This function is called via elf_link_hash_traverse. */
2077
28caa186 2078static bfd_boolean
268b6b39 2079_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2080{
28caa186 2081 struct elf_info_failed *sinfo;
45d6a902 2082 struct bfd_link_info *info;
9c5bfbb7 2083 const struct elf_backend_data *bed;
45d6a902
AM
2084 struct elf_info_failed eif;
2085 char *p;
2086 bfd_size_type amt;
2087
a50b1753 2088 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2089 info = sinfo->info;
2090
45d6a902
AM
2091 /* Fix the symbol flags. */
2092 eif.failed = FALSE;
2093 eif.info = info;
2094 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2095 {
2096 if (eif.failed)
2097 sinfo->failed = TRUE;
2098 return FALSE;
2099 }
2100
2101 /* We only need version numbers for symbols defined in regular
2102 objects. */
f5385ebf 2103 if (!h->def_regular)
45d6a902
AM
2104 return TRUE;
2105
28caa186 2106 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2107 p = strchr (h->root.root.string, ELF_VER_CHR);
2108 if (p != NULL && h->verinfo.vertree == NULL)
2109 {
2110 struct bfd_elf_version_tree *t;
45d6a902 2111
45d6a902
AM
2112 ++p;
2113 if (*p == ELF_VER_CHR)
6e33951e 2114 ++p;
45d6a902
AM
2115
2116 /* If there is no version string, we can just return out. */
2117 if (*p == '\0')
6e33951e 2118 return TRUE;
45d6a902
AM
2119
2120 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2121 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2122 {
2123 if (strcmp (t->name, p) == 0)
2124 {
2125 size_t len;
2126 char *alc;
2127 struct bfd_elf_version_expr *d;
2128
2129 len = p - h->root.root.string;
a50b1753 2130 alc = (char *) bfd_malloc (len);
45d6a902 2131 if (alc == NULL)
14b1c01e
AM
2132 {
2133 sinfo->failed = TRUE;
2134 return FALSE;
2135 }
45d6a902
AM
2136 memcpy (alc, h->root.root.string, len - 1);
2137 alc[len - 1] = '\0';
2138 if (alc[len - 2] == ELF_VER_CHR)
2139 alc[len - 2] = '\0';
2140
2141 h->verinfo.vertree = t;
2142 t->used = TRUE;
2143 d = NULL;
2144
108ba305
JJ
2145 if (t->globals.list != NULL)
2146 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2147
2148 /* See if there is anything to force this symbol to
2149 local scope. */
108ba305 2150 if (d == NULL && t->locals.list != NULL)
45d6a902 2151 {
108ba305
JJ
2152 d = (*t->match) (&t->locals, NULL, alc);
2153 if (d != NULL
2154 && h->dynindx != -1
108ba305
JJ
2155 && ! info->export_dynamic)
2156 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2157 }
2158
2159 free (alc);
2160 break;
2161 }
2162 }
2163
2164 /* If we are building an application, we need to create a
2165 version node for this version. */
0e1862bb 2166 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2167 {
2168 struct bfd_elf_version_tree **pp;
2169 int version_index;
2170
2171 /* If we aren't going to export this symbol, we don't need
2172 to worry about it. */
2173 if (h->dynindx == -1)
2174 return TRUE;
2175
2176 amt = sizeof *t;
a50b1753 2177 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
45d6a902
AM
2178 if (t == NULL)
2179 {
2180 sinfo->failed = TRUE;
2181 return FALSE;
2182 }
2183
45d6a902 2184 t->name = p;
45d6a902
AM
2185 t->name_indx = (unsigned int) -1;
2186 t->used = TRUE;
2187
2188 version_index = 1;
2189 /* Don't count anonymous version tag. */
fd91d419
L
2190 if (sinfo->info->version_info != NULL
2191 && sinfo->info->version_info->vernum == 0)
45d6a902 2192 version_index = 0;
fd91d419
L
2193 for (pp = &sinfo->info->version_info;
2194 *pp != NULL;
2195 pp = &(*pp)->next)
45d6a902
AM
2196 ++version_index;
2197 t->vernum = version_index;
2198
2199 *pp = t;
2200
2201 h->verinfo.vertree = t;
2202 }
2203 else if (t == NULL)
2204 {
2205 /* We could not find the version for a symbol when
2206 generating a shared archive. Return an error. */
2207 (*_bfd_error_handler)
c55fe096 2208 (_("%B: version node not found for symbol %s"),
28caa186 2209 info->output_bfd, h->root.root.string);
45d6a902
AM
2210 bfd_set_error (bfd_error_bad_value);
2211 sinfo->failed = TRUE;
2212 return FALSE;
2213 }
45d6a902
AM
2214 }
2215
2216 /* If we don't have a version for this symbol, see if we can find
2217 something. */
fd91d419 2218 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2219 {
1e8fa21e 2220 bfd_boolean hide;
ae5a3597 2221
fd91d419
L
2222 h->verinfo.vertree
2223 = bfd_find_version_for_sym (sinfo->info->version_info,
2224 h->root.root.string, &hide);
1e8fa21e
AM
2225 if (h->verinfo.vertree != NULL && hide)
2226 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2227 }
2228
2229 return TRUE;
2230}
2231\f
45d6a902
AM
2232/* Read and swap the relocs from the section indicated by SHDR. This
2233 may be either a REL or a RELA section. The relocations are
2234 translated into RELA relocations and stored in INTERNAL_RELOCS,
2235 which should have already been allocated to contain enough space.
2236 The EXTERNAL_RELOCS are a buffer where the external form of the
2237 relocations should be stored.
2238
2239 Returns FALSE if something goes wrong. */
2240
2241static bfd_boolean
268b6b39 2242elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2243 asection *sec,
268b6b39
AM
2244 Elf_Internal_Shdr *shdr,
2245 void *external_relocs,
2246 Elf_Internal_Rela *internal_relocs)
45d6a902 2247{
9c5bfbb7 2248 const struct elf_backend_data *bed;
268b6b39 2249 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2250 const bfd_byte *erela;
2251 const bfd_byte *erelaend;
2252 Elf_Internal_Rela *irela;
243ef1e0
L
2253 Elf_Internal_Shdr *symtab_hdr;
2254 size_t nsyms;
45d6a902 2255
45d6a902
AM
2256 /* Position ourselves at the start of the section. */
2257 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2258 return FALSE;
2259
2260 /* Read the relocations. */
2261 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2262 return FALSE;
2263
243ef1e0 2264 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2265 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2266
45d6a902
AM
2267 bed = get_elf_backend_data (abfd);
2268
2269 /* Convert the external relocations to the internal format. */
2270 if (shdr->sh_entsize == bed->s->sizeof_rel)
2271 swap_in = bed->s->swap_reloc_in;
2272 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2273 swap_in = bed->s->swap_reloca_in;
2274 else
2275 {
2276 bfd_set_error (bfd_error_wrong_format);
2277 return FALSE;
2278 }
2279
a50b1753 2280 erela = (const bfd_byte *) external_relocs;
51992aec 2281 erelaend = erela + shdr->sh_size;
45d6a902
AM
2282 irela = internal_relocs;
2283 while (erela < erelaend)
2284 {
243ef1e0
L
2285 bfd_vma r_symndx;
2286
45d6a902 2287 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2288 r_symndx = ELF32_R_SYM (irela->r_info);
2289 if (bed->s->arch_size == 64)
2290 r_symndx >>= 24;
ce98a316
NC
2291 if (nsyms > 0)
2292 {
2293 if ((size_t) r_symndx >= nsyms)
2294 {
2295 (*_bfd_error_handler)
2296 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2297 " for offset 0x%lx in section `%A'"),
2298 abfd, sec,
2299 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2300 bfd_set_error (bfd_error_bad_value);
2301 return FALSE;
2302 }
2303 }
cf35638d 2304 else if (r_symndx != STN_UNDEF)
243ef1e0
L
2305 {
2306 (*_bfd_error_handler)
ce98a316
NC
2307 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2308 " when the object file has no symbol table"),
d003868e
AM
2309 abfd, sec,
2310 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2311 bfd_set_error (bfd_error_bad_value);
2312 return FALSE;
2313 }
45d6a902
AM
2314 irela += bed->s->int_rels_per_ext_rel;
2315 erela += shdr->sh_entsize;
2316 }
2317
2318 return TRUE;
2319}
2320
2321/* Read and swap the relocs for a section O. They may have been
2322 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2323 not NULL, they are used as buffers to read into. They are known to
2324 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2325 the return value is allocated using either malloc or bfd_alloc,
2326 according to the KEEP_MEMORY argument. If O has two relocation
2327 sections (both REL and RELA relocations), then the REL_HDR
2328 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2329 RELA_HDR relocations. */
45d6a902
AM
2330
2331Elf_Internal_Rela *
268b6b39
AM
2332_bfd_elf_link_read_relocs (bfd *abfd,
2333 asection *o,
2334 void *external_relocs,
2335 Elf_Internal_Rela *internal_relocs,
2336 bfd_boolean keep_memory)
45d6a902 2337{
268b6b39 2338 void *alloc1 = NULL;
45d6a902 2339 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2340 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2341 struct bfd_elf_section_data *esdo = elf_section_data (o);
2342 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2343
d4730f92
BS
2344 if (esdo->relocs != NULL)
2345 return esdo->relocs;
45d6a902
AM
2346
2347 if (o->reloc_count == 0)
2348 return NULL;
2349
45d6a902
AM
2350 if (internal_relocs == NULL)
2351 {
2352 bfd_size_type size;
2353
2354 size = o->reloc_count;
2355 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2356 if (keep_memory)
a50b1753 2357 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2358 else
a50b1753 2359 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2360 if (internal_relocs == NULL)
2361 goto error_return;
2362 }
2363
2364 if (external_relocs == NULL)
2365 {
d4730f92
BS
2366 bfd_size_type size = 0;
2367
2368 if (esdo->rel.hdr)
2369 size += esdo->rel.hdr->sh_size;
2370 if (esdo->rela.hdr)
2371 size += esdo->rela.hdr->sh_size;
45d6a902 2372
268b6b39 2373 alloc1 = bfd_malloc (size);
45d6a902
AM
2374 if (alloc1 == NULL)
2375 goto error_return;
2376 external_relocs = alloc1;
2377 }
2378
d4730f92
BS
2379 internal_rela_relocs = internal_relocs;
2380 if (esdo->rel.hdr)
2381 {
2382 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2383 external_relocs,
2384 internal_relocs))
2385 goto error_return;
2386 external_relocs = (((bfd_byte *) external_relocs)
2387 + esdo->rel.hdr->sh_size);
2388 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2389 * bed->s->int_rels_per_ext_rel);
2390 }
2391
2392 if (esdo->rela.hdr
2393 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2394 external_relocs,
2395 internal_rela_relocs)))
45d6a902
AM
2396 goto error_return;
2397
2398 /* Cache the results for next time, if we can. */
2399 if (keep_memory)
d4730f92 2400 esdo->relocs = internal_relocs;
45d6a902
AM
2401
2402 if (alloc1 != NULL)
2403 free (alloc1);
2404
2405 /* Don't free alloc2, since if it was allocated we are passing it
2406 back (under the name of internal_relocs). */
2407
2408 return internal_relocs;
2409
2410 error_return:
2411 if (alloc1 != NULL)
2412 free (alloc1);
2413 if (alloc2 != NULL)
4dd07732
AM
2414 {
2415 if (keep_memory)
2416 bfd_release (abfd, alloc2);
2417 else
2418 free (alloc2);
2419 }
45d6a902
AM
2420 return NULL;
2421}
2422
2423/* Compute the size of, and allocate space for, REL_HDR which is the
2424 section header for a section containing relocations for O. */
2425
28caa186 2426static bfd_boolean
268b6b39 2427_bfd_elf_link_size_reloc_section (bfd *abfd,
d4730f92 2428 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2429{
d4730f92 2430 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2431
2432 /* That allows us to calculate the size of the section. */
d4730f92 2433 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2434
2435 /* The contents field must last into write_object_contents, so we
2436 allocate it with bfd_alloc rather than malloc. Also since we
2437 cannot be sure that the contents will actually be filled in,
2438 we zero the allocated space. */
a50b1753 2439 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2440 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2441 return FALSE;
2442
d4730f92 2443 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2444 {
2445 struct elf_link_hash_entry **p;
2446
ca4be51c
AM
2447 p = ((struct elf_link_hash_entry **)
2448 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2449 if (p == NULL)
2450 return FALSE;
2451
d4730f92 2452 reldata->hashes = p;
45d6a902
AM
2453 }
2454
2455 return TRUE;
2456}
2457
2458/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2459 originated from the section given by INPUT_REL_HDR) to the
2460 OUTPUT_BFD. */
2461
2462bfd_boolean
268b6b39
AM
2463_bfd_elf_link_output_relocs (bfd *output_bfd,
2464 asection *input_section,
2465 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2466 Elf_Internal_Rela *internal_relocs,
2467 struct elf_link_hash_entry **rel_hash
2468 ATTRIBUTE_UNUSED)
45d6a902
AM
2469{
2470 Elf_Internal_Rela *irela;
2471 Elf_Internal_Rela *irelaend;
2472 bfd_byte *erel;
d4730f92 2473 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2474 asection *output_section;
9c5bfbb7 2475 const struct elf_backend_data *bed;
268b6b39 2476 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2477 struct bfd_elf_section_data *esdo;
45d6a902
AM
2478
2479 output_section = input_section->output_section;
45d6a902 2480
d4730f92
BS
2481 bed = get_elf_backend_data (output_bfd);
2482 esdo = elf_section_data (output_section);
2483 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2484 {
d4730f92
BS
2485 output_reldata = &esdo->rel;
2486 swap_out = bed->s->swap_reloc_out;
45d6a902 2487 }
d4730f92
BS
2488 else if (esdo->rela.hdr
2489 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2490 {
d4730f92
BS
2491 output_reldata = &esdo->rela;
2492 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2493 }
2494 else
2495 {
2496 (*_bfd_error_handler)
d003868e
AM
2497 (_("%B: relocation size mismatch in %B section %A"),
2498 output_bfd, input_section->owner, input_section);
297d8443 2499 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2500 return FALSE;
2501 }
2502
d4730f92
BS
2503 erel = output_reldata->hdr->contents;
2504 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2505 irela = internal_relocs;
2506 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2507 * bed->s->int_rels_per_ext_rel);
2508 while (irela < irelaend)
2509 {
2510 (*swap_out) (output_bfd, irela, erel);
2511 irela += bed->s->int_rels_per_ext_rel;
2512 erel += input_rel_hdr->sh_entsize;
2513 }
2514
2515 /* Bump the counter, so that we know where to add the next set of
2516 relocations. */
d4730f92 2517 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2518
2519 return TRUE;
2520}
2521\f
508c3946
L
2522/* Make weak undefined symbols in PIE dynamic. */
2523
2524bfd_boolean
2525_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2526 struct elf_link_hash_entry *h)
2527{
0e1862bb 2528 if (bfd_link_pie (info)
508c3946
L
2529 && h->dynindx == -1
2530 && h->root.type == bfd_link_hash_undefweak)
2531 return bfd_elf_link_record_dynamic_symbol (info, h);
2532
2533 return TRUE;
2534}
2535
45d6a902
AM
2536/* Fix up the flags for a symbol. This handles various cases which
2537 can only be fixed after all the input files are seen. This is
2538 currently called by both adjust_dynamic_symbol and
2539 assign_sym_version, which is unnecessary but perhaps more robust in
2540 the face of future changes. */
2541
28caa186 2542static bfd_boolean
268b6b39
AM
2543_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2544 struct elf_info_failed *eif)
45d6a902 2545{
33774f08 2546 const struct elf_backend_data *bed;
508c3946 2547
45d6a902
AM
2548 /* If this symbol was mentioned in a non-ELF file, try to set
2549 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2550 permit a non-ELF file to correctly refer to a symbol defined in
2551 an ELF dynamic object. */
f5385ebf 2552 if (h->non_elf)
45d6a902
AM
2553 {
2554 while (h->root.type == bfd_link_hash_indirect)
2555 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2556
2557 if (h->root.type != bfd_link_hash_defined
2558 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2559 {
2560 h->ref_regular = 1;
2561 h->ref_regular_nonweak = 1;
2562 }
45d6a902
AM
2563 else
2564 {
2565 if (h->root.u.def.section->owner != NULL
2566 && (bfd_get_flavour (h->root.u.def.section->owner)
2567 == bfd_target_elf_flavour))
f5385ebf
AM
2568 {
2569 h->ref_regular = 1;
2570 h->ref_regular_nonweak = 1;
2571 }
45d6a902 2572 else
f5385ebf 2573 h->def_regular = 1;
45d6a902
AM
2574 }
2575
2576 if (h->dynindx == -1
f5385ebf
AM
2577 && (h->def_dynamic
2578 || h->ref_dynamic))
45d6a902 2579 {
c152c796 2580 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2581 {
2582 eif->failed = TRUE;
2583 return FALSE;
2584 }
2585 }
2586 }
2587 else
2588 {
f5385ebf 2589 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2590 was first seen in a non-ELF file. Fortunately, if the symbol
2591 was first seen in an ELF file, we're probably OK unless the
2592 symbol was defined in a non-ELF file. Catch that case here.
2593 FIXME: We're still in trouble if the symbol was first seen in
2594 a dynamic object, and then later in a non-ELF regular object. */
2595 if ((h->root.type == bfd_link_hash_defined
2596 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2597 && !h->def_regular
45d6a902
AM
2598 && (h->root.u.def.section->owner != NULL
2599 ? (bfd_get_flavour (h->root.u.def.section->owner)
2600 != bfd_target_elf_flavour)
2601 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2602 && !h->def_dynamic)))
2603 h->def_regular = 1;
45d6a902
AM
2604 }
2605
508c3946 2606 /* Backend specific symbol fixup. */
33774f08
AM
2607 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2608 if (bed->elf_backend_fixup_symbol
2609 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2610 return FALSE;
508c3946 2611
45d6a902
AM
2612 /* If this is a final link, and the symbol was defined as a common
2613 symbol in a regular object file, and there was no definition in
2614 any dynamic object, then the linker will have allocated space for
f5385ebf 2615 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2616 flag will not have been set. */
2617 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2618 && !h->def_regular
2619 && h->ref_regular
2620 && !h->def_dynamic
96f29d96 2621 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2622 h->def_regular = 1;
45d6a902
AM
2623
2624 /* If -Bsymbolic was used (which means to bind references to global
2625 symbols to the definition within the shared object), and this
2626 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2627 need a PLT entry. Likewise, if the symbol has non-default
2628 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2629 will force it local. */
f5385ebf 2630 if (h->needs_plt
0e1862bb 2631 && bfd_link_pic (eif->info)
0eddce27 2632 && is_elf_hash_table (eif->info->hash)
55255dae 2633 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2634 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2635 && h->def_regular)
45d6a902 2636 {
45d6a902
AM
2637 bfd_boolean force_local;
2638
45d6a902
AM
2639 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2640 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2641 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2642 }
2643
2644 /* If a weak undefined symbol has non-default visibility, we also
2645 hide it from the dynamic linker. */
9c7a29a3 2646 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2647 && h->root.type == bfd_link_hash_undefweak)
33774f08 2648 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2649
2650 /* If this is a weak defined symbol in a dynamic object, and we know
2651 the real definition in the dynamic object, copy interesting flags
2652 over to the real definition. */
f6e332e6 2653 if (h->u.weakdef != NULL)
45d6a902 2654 {
45d6a902
AM
2655 /* If the real definition is defined by a regular object file,
2656 don't do anything special. See the longer description in
2657 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2658 if (h->u.weakdef->def_regular)
f6e332e6 2659 h->u.weakdef = NULL;
45d6a902 2660 else
a26587ba 2661 {
4e6b54a6
AM
2662 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2663
2664 while (h->root.type == bfd_link_hash_indirect)
2665 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2666
2667 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2668 || h->root.type == bfd_link_hash_defweak);
2669 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2670 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2671 || weakdef->root.type == bfd_link_hash_defweak);
2672 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2673 }
45d6a902
AM
2674 }
2675
2676 return TRUE;
2677}
2678
2679/* Make the backend pick a good value for a dynamic symbol. This is
2680 called via elf_link_hash_traverse, and also calls itself
2681 recursively. */
2682
28caa186 2683static bfd_boolean
268b6b39 2684_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2685{
a50b1753 2686 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2687 bfd *dynobj;
9c5bfbb7 2688 const struct elf_backend_data *bed;
45d6a902 2689
0eddce27 2690 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2691 return FALSE;
2692
45d6a902
AM
2693 /* Ignore indirect symbols. These are added by the versioning code. */
2694 if (h->root.type == bfd_link_hash_indirect)
2695 return TRUE;
2696
2697 /* Fix the symbol flags. */
2698 if (! _bfd_elf_fix_symbol_flags (h, eif))
2699 return FALSE;
2700
2701 /* If this symbol does not require a PLT entry, and it is not
2702 defined by a dynamic object, or is not referenced by a regular
2703 object, ignore it. We do have to handle a weak defined symbol,
2704 even if no regular object refers to it, if we decided to add it
2705 to the dynamic symbol table. FIXME: Do we normally need to worry
2706 about symbols which are defined by one dynamic object and
2707 referenced by another one? */
f5385ebf 2708 if (!h->needs_plt
91e21fb7 2709 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2710 && (h->def_regular
2711 || !h->def_dynamic
2712 || (!h->ref_regular
f6e332e6 2713 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2714 {
a6aa5195 2715 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2716 return TRUE;
2717 }
2718
2719 /* If we've already adjusted this symbol, don't do it again. This
2720 can happen via a recursive call. */
f5385ebf 2721 if (h->dynamic_adjusted)
45d6a902
AM
2722 return TRUE;
2723
2724 /* Don't look at this symbol again. Note that we must set this
2725 after checking the above conditions, because we may look at a
2726 symbol once, decide not to do anything, and then get called
2727 recursively later after REF_REGULAR is set below. */
f5385ebf 2728 h->dynamic_adjusted = 1;
45d6a902
AM
2729
2730 /* If this is a weak definition, and we know a real definition, and
2731 the real symbol is not itself defined by a regular object file,
2732 then get a good value for the real definition. We handle the
2733 real symbol first, for the convenience of the backend routine.
2734
2735 Note that there is a confusing case here. If the real definition
2736 is defined by a regular object file, we don't get the real symbol
2737 from the dynamic object, but we do get the weak symbol. If the
2738 processor backend uses a COPY reloc, then if some routine in the
2739 dynamic object changes the real symbol, we will not see that
2740 change in the corresponding weak symbol. This is the way other
2741 ELF linkers work as well, and seems to be a result of the shared
2742 library model.
2743
2744 I will clarify this issue. Most SVR4 shared libraries define the
2745 variable _timezone and define timezone as a weak synonym. The
2746 tzset call changes _timezone. If you write
2747 extern int timezone;
2748 int _timezone = 5;
2749 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2750 you might expect that, since timezone is a synonym for _timezone,
2751 the same number will print both times. However, if the processor
2752 backend uses a COPY reloc, then actually timezone will be copied
2753 into your process image, and, since you define _timezone
2754 yourself, _timezone will not. Thus timezone and _timezone will
2755 wind up at different memory locations. The tzset call will set
2756 _timezone, leaving timezone unchanged. */
2757
f6e332e6 2758 if (h->u.weakdef != NULL)
45d6a902 2759 {
ec24dc88
AM
2760 /* If we get to this point, there is an implicit reference to
2761 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2762 h->u.weakdef->ref_regular = 1;
45d6a902 2763
ec24dc88
AM
2764 /* Ensure that the backend adjust_dynamic_symbol function sees
2765 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2766 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2767 return FALSE;
2768 }
2769
2770 /* If a symbol has no type and no size and does not require a PLT
2771 entry, then we are probably about to do the wrong thing here: we
2772 are probably going to create a COPY reloc for an empty object.
2773 This case can arise when a shared object is built with assembly
2774 code, and the assembly code fails to set the symbol type. */
2775 if (h->size == 0
2776 && h->type == STT_NOTYPE
f5385ebf 2777 && !h->needs_plt)
45d6a902
AM
2778 (*_bfd_error_handler)
2779 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2780 h->root.root.string);
2781
2782 dynobj = elf_hash_table (eif->info)->dynobj;
2783 bed = get_elf_backend_data (dynobj);
e7c33416 2784
45d6a902
AM
2785 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2786 {
2787 eif->failed = TRUE;
2788 return FALSE;
2789 }
2790
2791 return TRUE;
2792}
2793
027297b7
L
2794/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2795 DYNBSS. */
2796
2797bfd_boolean
6cabe1ea
AM
2798_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2799 struct elf_link_hash_entry *h,
027297b7
L
2800 asection *dynbss)
2801{
91ac5911 2802 unsigned int power_of_two;
027297b7
L
2803 bfd_vma mask;
2804 asection *sec = h->root.u.def.section;
2805
2806 /* The section aligment of definition is the maximum alignment
91ac5911
L
2807 requirement of symbols defined in the section. Since we don't
2808 know the symbol alignment requirement, we start with the
2809 maximum alignment and check low bits of the symbol address
2810 for the minimum alignment. */
2811 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2812 mask = ((bfd_vma) 1 << power_of_two) - 1;
2813 while ((h->root.u.def.value & mask) != 0)
2814 {
2815 mask >>= 1;
2816 --power_of_two;
2817 }
027297b7 2818
91ac5911
L
2819 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2820 dynbss))
027297b7
L
2821 {
2822 /* Adjust the section alignment if needed. */
2823 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2824 power_of_two))
027297b7
L
2825 return FALSE;
2826 }
2827
91ac5911 2828 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2829 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2830
2831 /* Define the symbol as being at this point in DYNBSS. */
2832 h->root.u.def.section = dynbss;
2833 h->root.u.def.value = dynbss->size;
2834
2835 /* Increment the size of DYNBSS to make room for the symbol. */
2836 dynbss->size += h->size;
2837
f7483970
L
2838 /* No error if extern_protected_data is true. */
2839 if (h->protected_def
889c2a67
L
2840 && (!info->extern_protected_data
2841 || (info->extern_protected_data < 0
2842 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2843 info->callbacks->einfo
2844 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2845 h->root.root.string);
6cabe1ea 2846
027297b7
L
2847 return TRUE;
2848}
2849
45d6a902
AM
2850/* Adjust all external symbols pointing into SEC_MERGE sections
2851 to reflect the object merging within the sections. */
2852
28caa186 2853static bfd_boolean
268b6b39 2854_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2855{
2856 asection *sec;
2857
45d6a902
AM
2858 if ((h->root.type == bfd_link_hash_defined
2859 || h->root.type == bfd_link_hash_defweak)
2860 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2861 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2862 {
a50b1753 2863 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2864
2865 h->root.u.def.value =
2866 _bfd_merged_section_offset (output_bfd,
2867 &h->root.u.def.section,
2868 elf_section_data (sec)->sec_info,
753731ee 2869 h->root.u.def.value);
45d6a902
AM
2870 }
2871
2872 return TRUE;
2873}
986a241f
RH
2874
2875/* Returns false if the symbol referred to by H should be considered
2876 to resolve local to the current module, and true if it should be
2877 considered to bind dynamically. */
2878
2879bfd_boolean
268b6b39
AM
2880_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2881 struct bfd_link_info *info,
89a2ee5a 2882 bfd_boolean not_local_protected)
986a241f
RH
2883{
2884 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2885 const struct elf_backend_data *bed;
2886 struct elf_link_hash_table *hash_table;
986a241f
RH
2887
2888 if (h == NULL)
2889 return FALSE;
2890
2891 while (h->root.type == bfd_link_hash_indirect
2892 || h->root.type == bfd_link_hash_warning)
2893 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2894
2895 /* If it was forced local, then clearly it's not dynamic. */
2896 if (h->dynindx == -1)
2897 return FALSE;
f5385ebf 2898 if (h->forced_local)
986a241f
RH
2899 return FALSE;
2900
2901 /* Identify the cases where name binding rules say that a
2902 visible symbol resolves locally. */
0e1862bb
L
2903 binding_stays_local_p = (bfd_link_executable (info)
2904 || SYMBOLIC_BIND (info, h));
986a241f
RH
2905
2906 switch (ELF_ST_VISIBILITY (h->other))
2907 {
2908 case STV_INTERNAL:
2909 case STV_HIDDEN:
2910 return FALSE;
2911
2912 case STV_PROTECTED:
fcb93ecf
PB
2913 hash_table = elf_hash_table (info);
2914 if (!is_elf_hash_table (hash_table))
2915 return FALSE;
2916
2917 bed = get_elf_backend_data (hash_table->dynobj);
2918
986a241f
RH
2919 /* Proper resolution for function pointer equality may require
2920 that these symbols perhaps be resolved dynamically, even though
2921 we should be resolving them to the current module. */
89a2ee5a 2922 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2923 binding_stays_local_p = TRUE;
2924 break;
2925
2926 default:
986a241f
RH
2927 break;
2928 }
2929
aa37626c 2930 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2931 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2932 return TRUE;
2933
986a241f
RH
2934 /* Otherwise, the symbol is dynamic if binding rules don't tell
2935 us that it remains local. */
2936 return !binding_stays_local_p;
2937}
f6c52c13
AM
2938
2939/* Return true if the symbol referred to by H should be considered
2940 to resolve local to the current module, and false otherwise. Differs
2941 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2942 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2943 for the place where forced_local and dynindx == -1 are tested. If
2944 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2945 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2946 the symbol is local only for defined symbols.
2947 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2948 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2949 treatment of undefined weak symbols. For those that do not make
2950 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2951
2952bfd_boolean
268b6b39
AM
2953_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2954 struct bfd_link_info *info,
2955 bfd_boolean local_protected)
f6c52c13 2956{
fcb93ecf
PB
2957 const struct elf_backend_data *bed;
2958 struct elf_link_hash_table *hash_table;
2959
f6c52c13
AM
2960 /* If it's a local sym, of course we resolve locally. */
2961 if (h == NULL)
2962 return TRUE;
2963
d95edcac
L
2964 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2965 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2966 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2967 return TRUE;
2968
7e2294f9
AO
2969 /* Common symbols that become definitions don't get the DEF_REGULAR
2970 flag set, so test it first, and don't bail out. */
2971 if (ELF_COMMON_DEF_P (h))
2972 /* Do nothing. */;
f6c52c13 2973 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2974 resolve locally. The sym is either undefined or dynamic. */
2975 else if (!h->def_regular)
f6c52c13
AM
2976 return FALSE;
2977
2978 /* Forced local symbols resolve locally. */
f5385ebf 2979 if (h->forced_local)
f6c52c13
AM
2980 return TRUE;
2981
2982 /* As do non-dynamic symbols. */
2983 if (h->dynindx == -1)
2984 return TRUE;
2985
2986 /* At this point, we know the symbol is defined and dynamic. In an
2987 executable it must resolve locally, likewise when building symbolic
2988 shared libraries. */
0e1862bb 2989 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
2990 return TRUE;
2991
2992 /* Now deal with defined dynamic symbols in shared libraries. Ones
2993 with default visibility might not resolve locally. */
2994 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2995 return FALSE;
2996
fcb93ecf
PB
2997 hash_table = elf_hash_table (info);
2998 if (!is_elf_hash_table (hash_table))
2999 return TRUE;
3000
3001 bed = get_elf_backend_data (hash_table->dynobj);
3002
f7483970
L
3003 /* If extern_protected_data is false, STV_PROTECTED non-function
3004 symbols are local. */
889c2a67
L
3005 if ((!info->extern_protected_data
3006 || (info->extern_protected_data < 0
3007 && !bed->extern_protected_data))
3008 && !bed->is_function_type (h->type))
1c16dfa5
L
3009 return TRUE;
3010
f6c52c13 3011 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3012 symbols be treated as dynamic symbols. If the address of a
3013 function not defined in an executable is set to that function's
3014 plt entry in the executable, then the address of the function in
3015 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3016 return local_protected;
3017}
e1918d23
AM
3018
3019/* Caches some TLS segment info, and ensures that the TLS segment vma is
3020 aligned. Returns the first TLS output section. */
3021
3022struct bfd_section *
3023_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3024{
3025 struct bfd_section *sec, *tls;
3026 unsigned int align = 0;
3027
3028 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3029 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3030 break;
3031 tls = sec;
3032
3033 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3034 if (sec->alignment_power > align)
3035 align = sec->alignment_power;
3036
3037 elf_hash_table (info)->tls_sec = tls;
3038
3039 /* Ensure the alignment of the first section is the largest alignment,
3040 so that the tls segment starts aligned. */
3041 if (tls != NULL)
3042 tls->alignment_power = align;
3043
3044 return tls;
3045}
0ad989f9
L
3046
3047/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3048static bfd_boolean
3049is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3050 Elf_Internal_Sym *sym)
3051{
a4d8e49b
L
3052 const struct elf_backend_data *bed;
3053
0ad989f9
L
3054 /* Local symbols do not count, but target specific ones might. */
3055 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3056 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3057 return FALSE;
3058
fcb93ecf 3059 bed = get_elf_backend_data (abfd);
0ad989f9 3060 /* Function symbols do not count. */
fcb93ecf 3061 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3062 return FALSE;
3063
3064 /* If the section is undefined, then so is the symbol. */
3065 if (sym->st_shndx == SHN_UNDEF)
3066 return FALSE;
3067
3068 /* If the symbol is defined in the common section, then
3069 it is a common definition and so does not count. */
a4d8e49b 3070 if (bed->common_definition (sym))
0ad989f9
L
3071 return FALSE;
3072
3073 /* If the symbol is in a target specific section then we
3074 must rely upon the backend to tell us what it is. */
3075 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3076 /* FIXME - this function is not coded yet:
3077
3078 return _bfd_is_global_symbol_definition (abfd, sym);
3079
3080 Instead for now assume that the definition is not global,
3081 Even if this is wrong, at least the linker will behave
3082 in the same way that it used to do. */
3083 return FALSE;
3084
3085 return TRUE;
3086}
3087
3088/* Search the symbol table of the archive element of the archive ABFD
3089 whose archive map contains a mention of SYMDEF, and determine if
3090 the symbol is defined in this element. */
3091static bfd_boolean
3092elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3093{
3094 Elf_Internal_Shdr * hdr;
3095 bfd_size_type symcount;
3096 bfd_size_type extsymcount;
3097 bfd_size_type extsymoff;
3098 Elf_Internal_Sym *isymbuf;
3099 Elf_Internal_Sym *isym;
3100 Elf_Internal_Sym *isymend;
3101 bfd_boolean result;
3102
3103 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3104 if (abfd == NULL)
3105 return FALSE;
3106
f0bf6bfd
L
3107 /* Return FALSE if the object has been claimed by plugin. */
3108 if (abfd->plugin_format == bfd_plugin_yes)
3109 return FALSE;
3110
0ad989f9
L
3111 if (! bfd_check_format (abfd, bfd_object))
3112 return FALSE;
3113
0ad989f9
L
3114 /* Select the appropriate symbol table. */
3115 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3116 hdr = &elf_tdata (abfd)->symtab_hdr;
3117 else
3118 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3119
3120 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3121
3122 /* The sh_info field of the symtab header tells us where the
3123 external symbols start. We don't care about the local symbols. */
3124 if (elf_bad_symtab (abfd))
3125 {
3126 extsymcount = symcount;
3127 extsymoff = 0;
3128 }
3129 else
3130 {
3131 extsymcount = symcount - hdr->sh_info;
3132 extsymoff = hdr->sh_info;
3133 }
3134
3135 if (extsymcount == 0)
3136 return FALSE;
3137
3138 /* Read in the symbol table. */
3139 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3140 NULL, NULL, NULL);
3141 if (isymbuf == NULL)
3142 return FALSE;
3143
3144 /* Scan the symbol table looking for SYMDEF. */
3145 result = FALSE;
3146 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3147 {
3148 const char *name;
3149
3150 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3151 isym->st_name);
3152 if (name == NULL)
3153 break;
3154
3155 if (strcmp (name, symdef->name) == 0)
3156 {
3157 result = is_global_data_symbol_definition (abfd, isym);
3158 break;
3159 }
3160 }
3161
3162 free (isymbuf);
3163
3164 return result;
3165}
3166\f
5a580b3a
AM
3167/* Add an entry to the .dynamic table. */
3168
3169bfd_boolean
3170_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3171 bfd_vma tag,
3172 bfd_vma val)
3173{
3174 struct elf_link_hash_table *hash_table;
3175 const struct elf_backend_data *bed;
3176 asection *s;
3177 bfd_size_type newsize;
3178 bfd_byte *newcontents;
3179 Elf_Internal_Dyn dyn;
3180
3181 hash_table = elf_hash_table (info);
3182 if (! is_elf_hash_table (hash_table))
3183 return FALSE;
3184
3185 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3186 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3187 BFD_ASSERT (s != NULL);
3188
eea6121a 3189 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3190 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3191 if (newcontents == NULL)
3192 return FALSE;
3193
3194 dyn.d_tag = tag;
3195 dyn.d_un.d_val = val;
eea6121a 3196 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3197
eea6121a 3198 s->size = newsize;
5a580b3a
AM
3199 s->contents = newcontents;
3200
3201 return TRUE;
3202}
3203
3204/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3205 otherwise just check whether one already exists. Returns -1 on error,
3206 1 if a DT_NEEDED tag already exists, and 0 on success. */
3207
4ad4eba5 3208static int
7e9f0867
AM
3209elf_add_dt_needed_tag (bfd *abfd,
3210 struct bfd_link_info *info,
4ad4eba5
AM
3211 const char *soname,
3212 bfd_boolean do_it)
5a580b3a
AM
3213{
3214 struct elf_link_hash_table *hash_table;
5a580b3a
AM
3215 bfd_size_type strindex;
3216
7e9f0867
AM
3217 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3218 return -1;
3219
5a580b3a 3220 hash_table = elf_hash_table (info);
5a580b3a
AM
3221 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3222 if (strindex == (bfd_size_type) -1)
3223 return -1;
3224
02be4619 3225 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3226 {
3227 asection *sdyn;
3228 const struct elf_backend_data *bed;
3229 bfd_byte *extdyn;
3230
3231 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3232 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3233 if (sdyn != NULL)
3234 for (extdyn = sdyn->contents;
3235 extdyn < sdyn->contents + sdyn->size;
3236 extdyn += bed->s->sizeof_dyn)
3237 {
3238 Elf_Internal_Dyn dyn;
5a580b3a 3239
7e9f0867
AM
3240 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3241 if (dyn.d_tag == DT_NEEDED
3242 && dyn.d_un.d_val == strindex)
3243 {
3244 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3245 return 1;
3246 }
3247 }
5a580b3a
AM
3248 }
3249
3250 if (do_it)
3251 {
7e9f0867
AM
3252 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3253 return -1;
3254
5a580b3a
AM
3255 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3256 return -1;
3257 }
3258 else
3259 /* We were just checking for existence of the tag. */
3260 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3261
3262 return 0;
3263}
3264
7b15fa7a
AM
3265/* Return true if SONAME is on the needed list between NEEDED and STOP
3266 (or the end of list if STOP is NULL), and needed by a library that
3267 will be loaded. */
3268
010e5ae2 3269static bfd_boolean
7b15fa7a
AM
3270on_needed_list (const char *soname,
3271 struct bfd_link_needed_list *needed,
3272 struct bfd_link_needed_list *stop)
010e5ae2 3273{
7b15fa7a
AM
3274 struct bfd_link_needed_list *look;
3275 for (look = needed; look != stop; look = look->next)
3276 if (strcmp (soname, look->name) == 0
3277 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3278 /* If needed by a library that itself is not directly
3279 needed, recursively check whether that library is
3280 indirectly needed. Since we add DT_NEEDED entries to
3281 the end of the list, library dependencies appear after
3282 the library. Therefore search prior to the current
3283 LOOK, preventing possible infinite recursion. */
3284 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3285 return TRUE;
3286
3287 return FALSE;
3288}
3289
14160578 3290/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3291static int
3292elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3293{
3294 const struct elf_link_hash_entry *h1;
3295 const struct elf_link_hash_entry *h2;
10b7e05b 3296 bfd_signed_vma vdiff;
5a580b3a
AM
3297
3298 h1 = *(const struct elf_link_hash_entry **) arg1;
3299 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3300 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3301 if (vdiff != 0)
3302 return vdiff > 0 ? 1 : -1;
3303 else
3304 {
d3435ae8 3305 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3306 if (sdiff != 0)
3307 return sdiff > 0 ? 1 : -1;
3308 }
14160578
AM
3309 vdiff = h1->size - h2->size;
3310 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3311}
4ad4eba5 3312
5a580b3a
AM
3313/* This function is used to adjust offsets into .dynstr for
3314 dynamic symbols. This is called via elf_link_hash_traverse. */
3315
3316static bfd_boolean
3317elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3318{
a50b1753 3319 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3320
5a580b3a
AM
3321 if (h->dynindx != -1)
3322 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3323 return TRUE;
3324}
3325
3326/* Assign string offsets in .dynstr, update all structures referencing
3327 them. */
3328
4ad4eba5
AM
3329static bfd_boolean
3330elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3331{
3332 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3333 struct elf_link_local_dynamic_entry *entry;
3334 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3335 bfd *dynobj = hash_table->dynobj;
3336 asection *sdyn;
3337 bfd_size_type size;
3338 const struct elf_backend_data *bed;
3339 bfd_byte *extdyn;
3340
3341 _bfd_elf_strtab_finalize (dynstr);
3342 size = _bfd_elf_strtab_size (dynstr);
3343
3344 bed = get_elf_backend_data (dynobj);
3d4d4302 3345 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3346 BFD_ASSERT (sdyn != NULL);
3347
3348 /* Update all .dynamic entries referencing .dynstr strings. */
3349 for (extdyn = sdyn->contents;
eea6121a 3350 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3351 extdyn += bed->s->sizeof_dyn)
3352 {
3353 Elf_Internal_Dyn dyn;
3354
3355 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3356 switch (dyn.d_tag)
3357 {
3358 case DT_STRSZ:
3359 dyn.d_un.d_val = size;
3360 break;
3361 case DT_NEEDED:
3362 case DT_SONAME:
3363 case DT_RPATH:
3364 case DT_RUNPATH:
3365 case DT_FILTER:
3366 case DT_AUXILIARY:
7ee314fa
AM
3367 case DT_AUDIT:
3368 case DT_DEPAUDIT:
5a580b3a
AM
3369 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3370 break;
3371 default:
3372 continue;
3373 }
3374 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3375 }
3376
3377 /* Now update local dynamic symbols. */
3378 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3379 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3380 entry->isym.st_name);
3381
3382 /* And the rest of dynamic symbols. */
3383 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3384
3385 /* Adjust version definitions. */
3386 if (elf_tdata (output_bfd)->cverdefs)
3387 {
3388 asection *s;
3389 bfd_byte *p;
3390 bfd_size_type i;
3391 Elf_Internal_Verdef def;
3392 Elf_Internal_Verdaux defaux;
3393
3d4d4302 3394 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3395 p = s->contents;
3396 do
3397 {
3398 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3399 &def);
3400 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3401 if (def.vd_aux != sizeof (Elf_External_Verdef))
3402 continue;
5a580b3a
AM
3403 for (i = 0; i < def.vd_cnt; ++i)
3404 {
3405 _bfd_elf_swap_verdaux_in (output_bfd,
3406 (Elf_External_Verdaux *) p, &defaux);
3407 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3408 defaux.vda_name);
3409 _bfd_elf_swap_verdaux_out (output_bfd,
3410 &defaux, (Elf_External_Verdaux *) p);
3411 p += sizeof (Elf_External_Verdaux);
3412 }
3413 }
3414 while (def.vd_next);
3415 }
3416
3417 /* Adjust version references. */
3418 if (elf_tdata (output_bfd)->verref)
3419 {
3420 asection *s;
3421 bfd_byte *p;
3422 bfd_size_type i;
3423 Elf_Internal_Verneed need;
3424 Elf_Internal_Vernaux needaux;
3425
3d4d4302 3426 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3427 p = s->contents;
3428 do
3429 {
3430 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3431 &need);
3432 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3433 _bfd_elf_swap_verneed_out (output_bfd, &need,
3434 (Elf_External_Verneed *) p);
3435 p += sizeof (Elf_External_Verneed);
3436 for (i = 0; i < need.vn_cnt; ++i)
3437 {
3438 _bfd_elf_swap_vernaux_in (output_bfd,
3439 (Elf_External_Vernaux *) p, &needaux);
3440 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3441 needaux.vna_name);
3442 _bfd_elf_swap_vernaux_out (output_bfd,
3443 &needaux,
3444 (Elf_External_Vernaux *) p);
3445 p += sizeof (Elf_External_Vernaux);
3446 }
3447 }
3448 while (need.vn_next);
3449 }
3450
3451 return TRUE;
3452}
3453\f
13285a1b
AM
3454/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3455 The default is to only match when the INPUT and OUTPUT are exactly
3456 the same target. */
3457
3458bfd_boolean
3459_bfd_elf_default_relocs_compatible (const bfd_target *input,
3460 const bfd_target *output)
3461{
3462 return input == output;
3463}
3464
3465/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3466 This version is used when different targets for the same architecture
3467 are virtually identical. */
3468
3469bfd_boolean
3470_bfd_elf_relocs_compatible (const bfd_target *input,
3471 const bfd_target *output)
3472{
3473 const struct elf_backend_data *obed, *ibed;
3474
3475 if (input == output)
3476 return TRUE;
3477
3478 ibed = xvec_get_elf_backend_data (input);
3479 obed = xvec_get_elf_backend_data (output);
3480
3481 if (ibed->arch != obed->arch)
3482 return FALSE;
3483
3484 /* If both backends are using this function, deem them compatible. */
3485 return ibed->relocs_compatible == obed->relocs_compatible;
3486}
3487
e5034e59
AM
3488/* Make a special call to the linker "notice" function to tell it that
3489 we are about to handle an as-needed lib, or have finished
1b786873 3490 processing the lib. */
e5034e59
AM
3491
3492bfd_boolean
3493_bfd_elf_notice_as_needed (bfd *ibfd,
3494 struct bfd_link_info *info,
3495 enum notice_asneeded_action act)
3496{
46135103 3497 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3498}
3499
d9689752
L
3500/* Check relocations an ELF object file. */
3501
3502bfd_boolean
3503_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3504{
3505 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3506 struct elf_link_hash_table *htab = elf_hash_table (info);
3507
3508 /* If this object is the same format as the output object, and it is
3509 not a shared library, then let the backend look through the
3510 relocs.
3511
3512 This is required to build global offset table entries and to
3513 arrange for dynamic relocs. It is not required for the
3514 particular common case of linking non PIC code, even when linking
3515 against shared libraries, but unfortunately there is no way of
3516 knowing whether an object file has been compiled PIC or not.
3517 Looking through the relocs is not particularly time consuming.
3518 The problem is that we must either (1) keep the relocs in memory,
3519 which causes the linker to require additional runtime memory or
3520 (2) read the relocs twice from the input file, which wastes time.
3521 This would be a good case for using mmap.
3522
3523 I have no idea how to handle linking PIC code into a file of a
3524 different format. It probably can't be done. */
3525 if ((abfd->flags & DYNAMIC) == 0
3526 && is_elf_hash_table (htab)
3527 && bed->check_relocs != NULL
3528 && elf_object_id (abfd) == elf_hash_table_id (htab)
3529 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3530 {
3531 asection *o;
3532
3533 for (o = abfd->sections; o != NULL; o = o->next)
3534 {
3535 Elf_Internal_Rela *internal_relocs;
3536 bfd_boolean ok;
3537
5ce03cea 3538 /* Don't check relocations in excluded sections. */
d9689752 3539 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3540 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3541 || o->reloc_count == 0
3542 || ((info->strip == strip_all || info->strip == strip_debugger)
3543 && (o->flags & SEC_DEBUGGING) != 0)
3544 || bfd_is_abs_section (o->output_section))
3545 continue;
3546
3547 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3548 info->keep_memory);
3549 if (internal_relocs == NULL)
3550 return FALSE;
3551
3552 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3553
3554 if (elf_section_data (o)->relocs != internal_relocs)
3555 free (internal_relocs);
3556
3557 if (! ok)
3558 return FALSE;
3559 }
3560 }
3561
3562 return TRUE;
3563}
3564
4ad4eba5
AM
3565/* Add symbols from an ELF object file to the linker hash table. */
3566
3567static bfd_boolean
3568elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3569{
a0c402a5 3570 Elf_Internal_Ehdr *ehdr;
4ad4eba5
AM
3571 Elf_Internal_Shdr *hdr;
3572 bfd_size_type symcount;
3573 bfd_size_type extsymcount;
3574 bfd_size_type extsymoff;
3575 struct elf_link_hash_entry **sym_hash;
3576 bfd_boolean dynamic;
3577 Elf_External_Versym *extversym = NULL;
3578 Elf_External_Versym *ever;
3579 struct elf_link_hash_entry *weaks;
3580 struct elf_link_hash_entry **nondeflt_vers = NULL;
3581 bfd_size_type nondeflt_vers_cnt = 0;
3582 Elf_Internal_Sym *isymbuf = NULL;
3583 Elf_Internal_Sym *isym;
3584 Elf_Internal_Sym *isymend;
3585 const struct elf_backend_data *bed;
3586 bfd_boolean add_needed;
66eb6687 3587 struct elf_link_hash_table *htab;
4ad4eba5 3588 bfd_size_type amt;
66eb6687 3589 void *alloc_mark = NULL;
4f87808c
AM
3590 struct bfd_hash_entry **old_table = NULL;
3591 unsigned int old_size = 0;
3592 unsigned int old_count = 0;
66eb6687 3593 void *old_tab = NULL;
66eb6687
AM
3594 void *old_ent;
3595 struct bfd_link_hash_entry *old_undefs = NULL;
3596 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3597 long old_dynsymcount = 0;
a4542f1b 3598 bfd_size_type old_dynstr_size = 0;
66eb6687 3599 size_t tabsize = 0;
db6a5d5f 3600 asection *s;
29a9f53e 3601 bfd_boolean just_syms;
4ad4eba5 3602
66eb6687 3603 htab = elf_hash_table (info);
4ad4eba5 3604 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3605
3606 if ((abfd->flags & DYNAMIC) == 0)
3607 dynamic = FALSE;
3608 else
3609 {
3610 dynamic = TRUE;
3611
3612 /* You can't use -r against a dynamic object. Also, there's no
3613 hope of using a dynamic object which does not exactly match
3614 the format of the output file. */
0e1862bb 3615 if (bfd_link_relocatable (info)
66eb6687 3616 || !is_elf_hash_table (htab)
f13a99db 3617 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3618 {
0e1862bb 3619 if (bfd_link_relocatable (info))
9a0789ec
NC
3620 bfd_set_error (bfd_error_invalid_operation);
3621 else
3622 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3623 goto error_return;
3624 }
3625 }
3626
a0c402a5
L
3627 ehdr = elf_elfheader (abfd);
3628 if (info->warn_alternate_em
3629 && bed->elf_machine_code != ehdr->e_machine
3630 && ((bed->elf_machine_alt1 != 0
3631 && ehdr->e_machine == bed->elf_machine_alt1)
3632 || (bed->elf_machine_alt2 != 0
3633 && ehdr->e_machine == bed->elf_machine_alt2)))
3634 info->callbacks->einfo
3635 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3636 ehdr->e_machine, abfd, bed->elf_machine_code);
3637
4ad4eba5
AM
3638 /* As a GNU extension, any input sections which are named
3639 .gnu.warning.SYMBOL are treated as warning symbols for the given
3640 symbol. This differs from .gnu.warning sections, which generate
3641 warnings when they are included in an output file. */
dd98f8d2 3642 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3643 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3644 {
db6a5d5f 3645 const char *name;
4ad4eba5 3646
db6a5d5f
AM
3647 name = bfd_get_section_name (abfd, s);
3648 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3649 {
db6a5d5f
AM
3650 char *msg;
3651 bfd_size_type sz;
3652
3653 name += sizeof ".gnu.warning." - 1;
3654
3655 /* If this is a shared object, then look up the symbol
3656 in the hash table. If it is there, and it is already
3657 been defined, then we will not be using the entry
3658 from this shared object, so we don't need to warn.
3659 FIXME: If we see the definition in a regular object
3660 later on, we will warn, but we shouldn't. The only
3661 fix is to keep track of what warnings we are supposed
3662 to emit, and then handle them all at the end of the
3663 link. */
3664 if (dynamic)
4ad4eba5 3665 {
db6a5d5f
AM
3666 struct elf_link_hash_entry *h;
3667
3668 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3669
3670 /* FIXME: What about bfd_link_hash_common? */
3671 if (h != NULL
3672 && (h->root.type == bfd_link_hash_defined
3673 || h->root.type == bfd_link_hash_defweak))
3674 continue;
3675 }
4ad4eba5 3676
db6a5d5f
AM
3677 sz = s->size;
3678 msg = (char *) bfd_alloc (abfd, sz + 1);
3679 if (msg == NULL)
3680 goto error_return;
4ad4eba5 3681
db6a5d5f
AM
3682 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3683 goto error_return;
4ad4eba5 3684
db6a5d5f 3685 msg[sz] = '\0';
4ad4eba5 3686
db6a5d5f
AM
3687 if (! (_bfd_generic_link_add_one_symbol
3688 (info, abfd, name, BSF_WARNING, s, 0, msg,
3689 FALSE, bed->collect, NULL)))
3690 goto error_return;
4ad4eba5 3691
0e1862bb 3692 if (bfd_link_executable (info))
db6a5d5f
AM
3693 {
3694 /* Clobber the section size so that the warning does
3695 not get copied into the output file. */
3696 s->size = 0;
11d2f718 3697
db6a5d5f
AM
3698 /* Also set SEC_EXCLUDE, so that symbols defined in
3699 the warning section don't get copied to the output. */
3700 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3701 }
3702 }
3703 }
3704
29a9f53e
L
3705 just_syms = ((s = abfd->sections) != NULL
3706 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3707
4ad4eba5
AM
3708 add_needed = TRUE;
3709 if (! dynamic)
3710 {
3711 /* If we are creating a shared library, create all the dynamic
3712 sections immediately. We need to attach them to something,
3713 so we attach them to this BFD, provided it is the right
bf89386a
L
3714 format and is not from ld --just-symbols. Always create the
3715 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3716 are no input BFD's of the same format as the output, we can't
3717 make a shared library. */
3718 if (!just_syms
bf89386a 3719 && (bfd_link_pic (info)
9c1d7a08
L
3720 || (!bfd_link_relocatable (info)
3721 && (info->export_dynamic || info->dynamic)))
66eb6687 3722 && is_elf_hash_table (htab)
f13a99db 3723 && info->output_bfd->xvec == abfd->xvec
66eb6687 3724 && !htab->dynamic_sections_created)
4ad4eba5
AM
3725 {
3726 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3727 goto error_return;
3728 }
3729 }
66eb6687 3730 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3731 goto error_return;
3732 else
3733 {
4ad4eba5 3734 const char *soname = NULL;
7ee314fa 3735 char *audit = NULL;
4ad4eba5
AM
3736 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3737 int ret;
3738
3739 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3740 ld shouldn't allow it. */
29a9f53e 3741 if (just_syms)
92fd189d 3742 abort ();
4ad4eba5
AM
3743
3744 /* If this dynamic lib was specified on the command line with
3745 --as-needed in effect, then we don't want to add a DT_NEEDED
3746 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3747 in by another lib's DT_NEEDED. When --no-add-needed is used
3748 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3749 any dynamic library in DT_NEEDED tags in the dynamic lib at
3750 all. */
3751 add_needed = (elf_dyn_lib_class (abfd)
3752 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3753 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3754
3755 s = bfd_get_section_by_name (abfd, ".dynamic");
3756 if (s != NULL)
3757 {
3758 bfd_byte *dynbuf;
3759 bfd_byte *extdyn;
cb33740c 3760 unsigned int elfsec;
4ad4eba5
AM
3761 unsigned long shlink;
3762
eea6121a 3763 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3764 {
3765error_free_dyn:
3766 free (dynbuf);
3767 goto error_return;
3768 }
4ad4eba5
AM
3769
3770 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3771 if (elfsec == SHN_BAD)
4ad4eba5
AM
3772 goto error_free_dyn;
3773 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3774
3775 for (extdyn = dynbuf;
eea6121a 3776 extdyn < dynbuf + s->size;
4ad4eba5
AM
3777 extdyn += bed->s->sizeof_dyn)
3778 {
3779 Elf_Internal_Dyn dyn;
3780
3781 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3782 if (dyn.d_tag == DT_SONAME)
3783 {
3784 unsigned int tagv = dyn.d_un.d_val;
3785 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3786 if (soname == NULL)
3787 goto error_free_dyn;
3788 }
3789 if (dyn.d_tag == DT_NEEDED)
3790 {
3791 struct bfd_link_needed_list *n, **pn;
3792 char *fnm, *anm;
3793 unsigned int tagv = dyn.d_un.d_val;
3794
3795 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3796 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3797 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3798 if (n == NULL || fnm == NULL)
3799 goto error_free_dyn;
3800 amt = strlen (fnm) + 1;
a50b1753 3801 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3802 if (anm == NULL)
3803 goto error_free_dyn;
3804 memcpy (anm, fnm, amt);
3805 n->name = anm;
3806 n->by = abfd;
3807 n->next = NULL;
66eb6687 3808 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3809 ;
3810 *pn = n;
3811 }
3812 if (dyn.d_tag == DT_RUNPATH)
3813 {
3814 struct bfd_link_needed_list *n, **pn;
3815 char *fnm, *anm;
3816 unsigned int tagv = dyn.d_un.d_val;
3817
3818 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3819 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3820 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3821 if (n == NULL || fnm == NULL)
3822 goto error_free_dyn;
3823 amt = strlen (fnm) + 1;
a50b1753 3824 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3825 if (anm == NULL)
3826 goto error_free_dyn;
3827 memcpy (anm, fnm, amt);
3828 n->name = anm;
3829 n->by = abfd;
3830 n->next = NULL;
3831 for (pn = & runpath;
3832 *pn != NULL;
3833 pn = &(*pn)->next)
3834 ;
3835 *pn = n;
3836 }
3837 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3838 if (!runpath && dyn.d_tag == DT_RPATH)
3839 {
3840 struct bfd_link_needed_list *n, **pn;
3841 char *fnm, *anm;
3842 unsigned int tagv = dyn.d_un.d_val;
3843
3844 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3845 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3846 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3847 if (n == NULL || fnm == NULL)
3848 goto error_free_dyn;
3849 amt = strlen (fnm) + 1;
a50b1753 3850 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3851 if (anm == NULL)
f8703194 3852 goto error_free_dyn;
4ad4eba5
AM
3853 memcpy (anm, fnm, amt);
3854 n->name = anm;
3855 n->by = abfd;
3856 n->next = NULL;
3857 for (pn = & rpath;
3858 *pn != NULL;
3859 pn = &(*pn)->next)
3860 ;
3861 *pn = n;
3862 }
7ee314fa
AM
3863 if (dyn.d_tag == DT_AUDIT)
3864 {
3865 unsigned int tagv = dyn.d_un.d_val;
3866 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3867 }
4ad4eba5
AM
3868 }
3869
3870 free (dynbuf);
3871 }
3872
3873 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3874 frees all more recently bfd_alloc'd blocks as well. */
3875 if (runpath)
3876 rpath = runpath;
3877
3878 if (rpath)
3879 {
3880 struct bfd_link_needed_list **pn;
66eb6687 3881 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3882 ;
3883 *pn = rpath;
3884 }
3885
3886 /* We do not want to include any of the sections in a dynamic
3887 object in the output file. We hack by simply clobbering the
3888 list of sections in the BFD. This could be handled more
3889 cleanly by, say, a new section flag; the existing
3890 SEC_NEVER_LOAD flag is not the one we want, because that one
3891 still implies that the section takes up space in the output
3892 file. */
3893 bfd_section_list_clear (abfd);
3894
4ad4eba5
AM
3895 /* Find the name to use in a DT_NEEDED entry that refers to this
3896 object. If the object has a DT_SONAME entry, we use it.
3897 Otherwise, if the generic linker stuck something in
3898 elf_dt_name, we use that. Otherwise, we just use the file
3899 name. */
3900 if (soname == NULL || *soname == '\0')
3901 {
3902 soname = elf_dt_name (abfd);
3903 if (soname == NULL || *soname == '\0')
3904 soname = bfd_get_filename (abfd);
3905 }
3906
3907 /* Save the SONAME because sometimes the linker emulation code
3908 will need to know it. */
3909 elf_dt_name (abfd) = soname;
3910
7e9f0867 3911 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3912 if (ret < 0)
3913 goto error_return;
3914
3915 /* If we have already included this dynamic object in the
3916 link, just ignore it. There is no reason to include a
3917 particular dynamic object more than once. */
3918 if (ret > 0)
3919 return TRUE;
7ee314fa
AM
3920
3921 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 3922 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3923 }
3924
3925 /* If this is a dynamic object, we always link against the .dynsym
3926 symbol table, not the .symtab symbol table. The dynamic linker
3927 will only see the .dynsym symbol table, so there is no reason to
3928 look at .symtab for a dynamic object. */
3929
3930 if (! dynamic || elf_dynsymtab (abfd) == 0)
3931 hdr = &elf_tdata (abfd)->symtab_hdr;
3932 else
3933 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3934
3935 symcount = hdr->sh_size / bed->s->sizeof_sym;
3936
3937 /* The sh_info field of the symtab header tells us where the
3938 external symbols start. We don't care about the local symbols at
3939 this point. */
3940 if (elf_bad_symtab (abfd))
3941 {
3942 extsymcount = symcount;
3943 extsymoff = 0;
3944 }
3945 else
3946 {
3947 extsymcount = symcount - hdr->sh_info;
3948 extsymoff = hdr->sh_info;
3949 }
3950
f45794cb 3951 sym_hash = elf_sym_hashes (abfd);
012b2306 3952 if (extsymcount != 0)
4ad4eba5
AM
3953 {
3954 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3955 NULL, NULL, NULL);
3956 if (isymbuf == NULL)
3957 goto error_return;
3958
4ad4eba5 3959 if (sym_hash == NULL)
012b2306
AM
3960 {
3961 /* We store a pointer to the hash table entry for each
3962 external symbol. */
3963 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3964 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3965 if (sym_hash == NULL)
3966 goto error_free_sym;
3967 elf_sym_hashes (abfd) = sym_hash;
3968 }
4ad4eba5
AM
3969 }
3970
3971 if (dynamic)
3972 {
3973 /* Read in any version definitions. */
fc0e6df6
PB
3974 if (!_bfd_elf_slurp_version_tables (abfd,
3975 info->default_imported_symver))
4ad4eba5
AM
3976 goto error_free_sym;
3977
3978 /* Read in the symbol versions, but don't bother to convert them
3979 to internal format. */
3980 if (elf_dynversym (abfd) != 0)
3981 {
3982 Elf_Internal_Shdr *versymhdr;
3983
3984 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 3985 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
3986 if (extversym == NULL)
3987 goto error_free_sym;
3988 amt = versymhdr->sh_size;
3989 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3990 || bfd_bread (extversym, amt, abfd) != amt)
3991 goto error_free_vers;
3992 }
3993 }
3994
66eb6687
AM
3995 /* If we are loading an as-needed shared lib, save the symbol table
3996 state before we start adding symbols. If the lib turns out
3997 to be unneeded, restore the state. */
3998 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3999 {
4000 unsigned int i;
4001 size_t entsize;
4002
4003 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4004 {
4005 struct bfd_hash_entry *p;
2de92251 4006 struct elf_link_hash_entry *h;
66eb6687
AM
4007
4008 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4009 {
4010 h = (struct elf_link_hash_entry *) p;
4011 entsize += htab->root.table.entsize;
4012 if (h->root.type == bfd_link_hash_warning)
4013 entsize += htab->root.table.entsize;
4014 }
66eb6687
AM
4015 }
4016
4017 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4018 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4019 if (old_tab == NULL)
4020 goto error_free_vers;
4021
4022 /* Remember the current objalloc pointer, so that all mem for
4023 symbols added can later be reclaimed. */
4024 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4025 if (alloc_mark == NULL)
4026 goto error_free_vers;
4027
5061a885
AM
4028 /* Make a special call to the linker "notice" function to
4029 tell it that we are about to handle an as-needed lib. */
e5034e59 4030 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4031 goto error_free_vers;
5061a885 4032
f45794cb
AM
4033 /* Clone the symbol table. Remember some pointers into the
4034 symbol table, and dynamic symbol count. */
4035 old_ent = (char *) old_tab + tabsize;
66eb6687 4036 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4037 old_undefs = htab->root.undefs;
4038 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4039 old_table = htab->root.table.table;
4040 old_size = htab->root.table.size;
4041 old_count = htab->root.table.count;
66eb6687 4042 old_dynsymcount = htab->dynsymcount;
a4542f1b 4043 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
66eb6687
AM
4044
4045 for (i = 0; i < htab->root.table.size; i++)
4046 {
4047 struct bfd_hash_entry *p;
2de92251 4048 struct elf_link_hash_entry *h;
66eb6687
AM
4049
4050 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4051 {
4052 memcpy (old_ent, p, htab->root.table.entsize);
4053 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4054 h = (struct elf_link_hash_entry *) p;
4055 if (h->root.type == bfd_link_hash_warning)
4056 {
4057 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4058 old_ent = (char *) old_ent + htab->root.table.entsize;
4059 }
66eb6687
AM
4060 }
4061 }
4062 }
4ad4eba5 4063
66eb6687 4064 weaks = NULL;
4ad4eba5
AM
4065 ever = extversym != NULL ? extversym + extsymoff : NULL;
4066 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4067 isym < isymend;
4068 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4069 {
4070 int bind;
4071 bfd_vma value;
af44c138 4072 asection *sec, *new_sec;
4ad4eba5
AM
4073 flagword flags;
4074 const char *name;
4075 struct elf_link_hash_entry *h;
90c984fc 4076 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4077 bfd_boolean definition;
4078 bfd_boolean size_change_ok;
4079 bfd_boolean type_change_ok;
4080 bfd_boolean new_weakdef;
37a9e49a
L
4081 bfd_boolean new_weak;
4082 bfd_boolean old_weak;
4ad4eba5 4083 bfd_boolean override;
a4d8e49b 4084 bfd_boolean common;
97196564 4085 bfd_boolean discarded;
4ad4eba5
AM
4086 unsigned int old_alignment;
4087 bfd *old_bfd;
6e33951e 4088 bfd_boolean matched;
4ad4eba5
AM
4089
4090 override = FALSE;
4091
4092 flags = BSF_NO_FLAGS;
4093 sec = NULL;
4094 value = isym->st_value;
a4d8e49b 4095 common = bed->common_definition (isym);
97196564 4096 discarded = FALSE;
4ad4eba5
AM
4097
4098 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4099 switch (bind)
4ad4eba5 4100 {
3e7a7d11 4101 case STB_LOCAL:
4ad4eba5
AM
4102 /* This should be impossible, since ELF requires that all
4103 global symbols follow all local symbols, and that sh_info
4104 point to the first global symbol. Unfortunately, Irix 5
4105 screws this up. */
4106 continue;
3e7a7d11
NC
4107
4108 case STB_GLOBAL:
a4d8e49b 4109 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4110 flags = BSF_GLOBAL;
3e7a7d11
NC
4111 break;
4112
4113 case STB_WEAK:
4114 flags = BSF_WEAK;
4115 break;
4116
4117 case STB_GNU_UNIQUE:
4118 flags = BSF_GNU_UNIQUE;
4119 break;
4120
4121 default:
4ad4eba5 4122 /* Leave it up to the processor backend. */
3e7a7d11 4123 break;
4ad4eba5
AM
4124 }
4125
4126 if (isym->st_shndx == SHN_UNDEF)
4127 sec = bfd_und_section_ptr;
cb33740c
AM
4128 else if (isym->st_shndx == SHN_ABS)
4129 sec = bfd_abs_section_ptr;
4130 else if (isym->st_shndx == SHN_COMMON)
4131 {
4132 sec = bfd_com_section_ptr;
4133 /* What ELF calls the size we call the value. What ELF
4134 calls the value we call the alignment. */
4135 value = isym->st_size;
4136 }
4137 else
4ad4eba5
AM
4138 {
4139 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4140 if (sec == NULL)
4141 sec = bfd_abs_section_ptr;
dbaa2011 4142 else if (discarded_section (sec))
529fcb95 4143 {
e5d08002
L
4144 /* Symbols from discarded section are undefined. We keep
4145 its visibility. */
529fcb95 4146 sec = bfd_und_section_ptr;
97196564 4147 discarded = TRUE;
529fcb95
PB
4148 isym->st_shndx = SHN_UNDEF;
4149 }
4ad4eba5
AM
4150 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4151 value -= sec->vma;
4152 }
4ad4eba5
AM
4153
4154 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4155 isym->st_name);
4156 if (name == NULL)
4157 goto error_free_vers;
4158
4159 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4160 && (abfd->flags & BFD_PLUGIN) != 0)
4161 {
4162 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4163
4164 if (xc == NULL)
4165 {
4166 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4167 | SEC_EXCLUDE);
4168 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4169 if (xc == NULL)
4170 goto error_free_vers;
4171 }
4172 sec = xc;
4173 }
4174 else if (isym->st_shndx == SHN_COMMON
4175 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4176 && !bfd_link_relocatable (info))
4ad4eba5
AM
4177 {
4178 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4179
4180 if (tcomm == NULL)
4181 {
02d00247
AM
4182 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4183 | SEC_LINKER_CREATED);
4184 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4185 if (tcomm == NULL)
4ad4eba5
AM
4186 goto error_free_vers;
4187 }
4188 sec = tcomm;
4189 }
66eb6687 4190 else if (bed->elf_add_symbol_hook)
4ad4eba5 4191 {
66eb6687
AM
4192 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4193 &sec, &value))
4ad4eba5
AM
4194 goto error_free_vers;
4195
4196 /* The hook function sets the name to NULL if this symbol
4197 should be skipped for some reason. */
4198 if (name == NULL)
4199 continue;
4200 }
4201
4202 /* Sanity check that all possibilities were handled. */
4203 if (sec == NULL)
4204 {
4205 bfd_set_error (bfd_error_bad_value);
4206 goto error_free_vers;
4207 }
4208
191c0c42
AM
4209 /* Silently discard TLS symbols from --just-syms. There's
4210 no way to combine a static TLS block with a new TLS block
4211 for this executable. */
4212 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4213 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4214 continue;
4215
4ad4eba5
AM
4216 if (bfd_is_und_section (sec)
4217 || bfd_is_com_section (sec))
4218 definition = FALSE;
4219 else
4220 definition = TRUE;
4221
4222 size_change_ok = FALSE;
66eb6687 4223 type_change_ok = bed->type_change_ok;
37a9e49a 4224 old_weak = FALSE;
6e33951e 4225 matched = FALSE;
4ad4eba5
AM
4226 old_alignment = 0;
4227 old_bfd = NULL;
af44c138 4228 new_sec = sec;
4ad4eba5 4229
66eb6687 4230 if (is_elf_hash_table (htab))
4ad4eba5
AM
4231 {
4232 Elf_Internal_Versym iver;
4233 unsigned int vernum = 0;
4234 bfd_boolean skip;
4235
fc0e6df6 4236 if (ever == NULL)
4ad4eba5 4237 {
fc0e6df6
PB
4238 if (info->default_imported_symver)
4239 /* Use the default symbol version created earlier. */
4240 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4241 else
4242 iver.vs_vers = 0;
4243 }
4244 else
4245 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4246
4247 vernum = iver.vs_vers & VERSYM_VERSION;
4248
4249 /* If this is a hidden symbol, or if it is not version
4250 1, we append the version name to the symbol name.
cc86ff91
EB
4251 However, we do not modify a non-hidden absolute symbol
4252 if it is not a function, because it might be the version
4253 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4254 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4255 || (vernum > 1
4256 && (!bfd_is_abs_section (sec)
4257 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4258 {
4259 const char *verstr;
4260 size_t namelen, verlen, newlen;
4261 char *newname, *p;
4262
4263 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4264 {
fc0e6df6
PB
4265 if (vernum > elf_tdata (abfd)->cverdefs)
4266 verstr = NULL;
4267 else if (vernum > 1)
4268 verstr =
4269 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4270 else
4271 verstr = "";
4ad4eba5 4272
fc0e6df6 4273 if (verstr == NULL)
4ad4eba5 4274 {
fc0e6df6
PB
4275 (*_bfd_error_handler)
4276 (_("%B: %s: invalid version %u (max %d)"),
4277 abfd, name, vernum,
4278 elf_tdata (abfd)->cverdefs);
4279 bfd_set_error (bfd_error_bad_value);
4280 goto error_free_vers;
4ad4eba5 4281 }
fc0e6df6
PB
4282 }
4283 else
4284 {
4285 /* We cannot simply test for the number of
4286 entries in the VERNEED section since the
4287 numbers for the needed versions do not start
4288 at 0. */
4289 Elf_Internal_Verneed *t;
4290
4291 verstr = NULL;
4292 for (t = elf_tdata (abfd)->verref;
4293 t != NULL;
4294 t = t->vn_nextref)
4ad4eba5 4295 {
fc0e6df6 4296 Elf_Internal_Vernaux *a;
4ad4eba5 4297
fc0e6df6
PB
4298 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4299 {
4300 if (a->vna_other == vernum)
4ad4eba5 4301 {
fc0e6df6
PB
4302 verstr = a->vna_nodename;
4303 break;
4ad4eba5 4304 }
4ad4eba5 4305 }
fc0e6df6
PB
4306 if (a != NULL)
4307 break;
4308 }
4309 if (verstr == NULL)
4310 {
4311 (*_bfd_error_handler)
4312 (_("%B: %s: invalid needed version %d"),
4313 abfd, name, vernum);
4314 bfd_set_error (bfd_error_bad_value);
4315 goto error_free_vers;
4ad4eba5 4316 }
4ad4eba5 4317 }
fc0e6df6
PB
4318
4319 namelen = strlen (name);
4320 verlen = strlen (verstr);
4321 newlen = namelen + verlen + 2;
4322 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4323 && isym->st_shndx != SHN_UNDEF)
4324 ++newlen;
4325
a50b1753 4326 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4327 if (newname == NULL)
4328 goto error_free_vers;
4329 memcpy (newname, name, namelen);
4330 p = newname + namelen;
4331 *p++ = ELF_VER_CHR;
4332 /* If this is a defined non-hidden version symbol,
4333 we add another @ to the name. This indicates the
4334 default version of the symbol. */
4335 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4336 && isym->st_shndx != SHN_UNDEF)
4337 *p++ = ELF_VER_CHR;
4338 memcpy (p, verstr, verlen + 1);
4339
4340 name = newname;
4ad4eba5
AM
4341 }
4342
cd3416da
AM
4343 /* If this symbol has default visibility and the user has
4344 requested we not re-export it, then mark it as hidden. */
a0d49154 4345 if (!bfd_is_und_section (sec)
cd3416da 4346 && !dynamic
ce875075 4347 && abfd->no_export
cd3416da
AM
4348 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4349 isym->st_other = (STV_HIDDEN
4350 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4351
4f3fedcf
AM
4352 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4353 sym_hash, &old_bfd, &old_weak,
4354 &old_alignment, &skip, &override,
6e33951e
L
4355 &type_change_ok, &size_change_ok,
4356 &matched))
4ad4eba5
AM
4357 goto error_free_vers;
4358
4359 if (skip)
4360 continue;
4361
6e33951e
L
4362 /* Override a definition only if the new symbol matches the
4363 existing one. */
4364 if (override && matched)
4ad4eba5
AM
4365 definition = FALSE;
4366
4367 h = *sym_hash;
4368 while (h->root.type == bfd_link_hash_indirect
4369 || h->root.type == bfd_link_hash_warning)
4370 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4371
4ad4eba5 4372 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4373 && vernum > 1
4374 && definition)
4375 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4376 }
4377
4378 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4379 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4380 (struct bfd_link_hash_entry **) sym_hash)))
4381 goto error_free_vers;
4382
a43942db
MR
4383 if ((flags & BSF_GNU_UNIQUE)
4384 && (abfd->flags & DYNAMIC) == 0
4385 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4386 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4387
4ad4eba5 4388 h = *sym_hash;
90c984fc
L
4389 /* We need to make sure that indirect symbol dynamic flags are
4390 updated. */
4391 hi = h;
4ad4eba5
AM
4392 while (h->root.type == bfd_link_hash_indirect
4393 || h->root.type == bfd_link_hash_warning)
4394 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4395
97196564
L
4396 /* Setting the index to -3 tells elf_link_output_extsym that
4397 this symbol is defined in a discarded section. */
4398 if (discarded)
4399 h->indx = -3;
4400
4ad4eba5
AM
4401 *sym_hash = h;
4402
37a9e49a 4403 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4404 new_weakdef = FALSE;
4405 if (dynamic
4406 && definition
37a9e49a 4407 && new_weak
fcb93ecf 4408 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4409 && is_elf_hash_table (htab)
f6e332e6 4410 && h->u.weakdef == NULL)
4ad4eba5
AM
4411 {
4412 /* Keep a list of all weak defined non function symbols from
4413 a dynamic object, using the weakdef field. Later in this
4414 function we will set the weakdef field to the correct
4415 value. We only put non-function symbols from dynamic
4416 objects on this list, because that happens to be the only
4417 time we need to know the normal symbol corresponding to a
4418 weak symbol, and the information is time consuming to
4419 figure out. If the weakdef field is not already NULL,
4420 then this symbol was already defined by some previous
4421 dynamic object, and we will be using that previous
4422 definition anyhow. */
4423
f6e332e6 4424 h->u.weakdef = weaks;
4ad4eba5
AM
4425 weaks = h;
4426 new_weakdef = TRUE;
4427 }
4428
4429 /* Set the alignment of a common symbol. */
a4d8e49b 4430 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4431 && h->root.type == bfd_link_hash_common)
4432 {
4433 unsigned int align;
4434
a4d8e49b 4435 if (common)
af44c138
L
4436 align = bfd_log2 (isym->st_value);
4437 else
4438 {
4439 /* The new symbol is a common symbol in a shared object.
4440 We need to get the alignment from the section. */
4441 align = new_sec->alignment_power;
4442 }
595213d4 4443 if (align > old_alignment)
4ad4eba5
AM
4444 h->root.u.c.p->alignment_power = align;
4445 else
4446 h->root.u.c.p->alignment_power = old_alignment;
4447 }
4448
66eb6687 4449 if (is_elf_hash_table (htab))
4ad4eba5 4450 {
4f3fedcf
AM
4451 /* Set a flag in the hash table entry indicating the type of
4452 reference or definition we just found. A dynamic symbol
4453 is one which is referenced or defined by both a regular
4454 object and a shared object. */
4455 bfd_boolean dynsym = FALSE;
4456
4457 /* Plugin symbols aren't normal. Don't set def_regular or
4458 ref_regular for them, or make them dynamic. */
4459 if ((abfd->flags & BFD_PLUGIN) != 0)
4460 ;
4461 else if (! dynamic)
4462 {
4463 if (! definition)
4464 {
4465 h->ref_regular = 1;
4466 if (bind != STB_WEAK)
4467 h->ref_regular_nonweak = 1;
4468 }
4469 else
4470 {
4471 h->def_regular = 1;
4472 if (h->def_dynamic)
4473 {
4474 h->def_dynamic = 0;
4475 h->ref_dynamic = 1;
4476 }
4477 }
4478
4479 /* If the indirect symbol has been forced local, don't
4480 make the real symbol dynamic. */
4481 if ((h == hi || !hi->forced_local)
0e1862bb 4482 && (bfd_link_dll (info)
4f3fedcf
AM
4483 || h->def_dynamic
4484 || h->ref_dynamic))
4485 dynsym = TRUE;
4486 }
4487 else
4488 {
4489 if (! definition)
4490 {
4491 h->ref_dynamic = 1;
4492 hi->ref_dynamic = 1;
4493 }
4494 else
4495 {
4496 h->def_dynamic = 1;
4497 hi->def_dynamic = 1;
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)
4503 && (h->def_regular
4504 || h->ref_regular
4505 || (h->u.weakdef != NULL
4506 && ! new_weakdef
4507 && h->u.weakdef->dynindx != -1)))
4508 dynsym = TRUE;
4509 }
4510
4511 /* Check to see if we need to add an indirect symbol for
4512 the default name. */
4513 if (definition
4514 || (!override && h->root.type == bfd_link_hash_common))
4515 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4516 sec, value, &old_bfd, &dynsym))
4517 goto error_free_vers;
4ad4eba5
AM
4518
4519 /* Check the alignment when a common symbol is involved. This
4520 can change when a common symbol is overridden by a normal
4521 definition or a common symbol is ignored due to the old
4522 normal definition. We need to make sure the maximum
4523 alignment is maintained. */
a4d8e49b 4524 if ((old_alignment || common)
4ad4eba5
AM
4525 && h->root.type != bfd_link_hash_common)
4526 {
4527 unsigned int common_align;
4528 unsigned int normal_align;
4529 unsigned int symbol_align;
4530 bfd *normal_bfd;
4531 bfd *common_bfd;
4532
3a81e825
AM
4533 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4534 || h->root.type == bfd_link_hash_defweak);
4535
4ad4eba5
AM
4536 symbol_align = ffs (h->root.u.def.value) - 1;
4537 if (h->root.u.def.section->owner != NULL
4538 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4539 {
4540 normal_align = h->root.u.def.section->alignment_power;
4541 if (normal_align > symbol_align)
4542 normal_align = symbol_align;
4543 }
4544 else
4545 normal_align = symbol_align;
4546
4547 if (old_alignment)
4548 {
4549 common_align = old_alignment;
4550 common_bfd = old_bfd;
4551 normal_bfd = abfd;
4552 }
4553 else
4554 {
4555 common_align = bfd_log2 (isym->st_value);
4556 common_bfd = abfd;
4557 normal_bfd = old_bfd;
4558 }
4559
4560 if (normal_align < common_align)
d07676f8
NC
4561 {
4562 /* PR binutils/2735 */
4563 if (normal_bfd == NULL)
4564 (*_bfd_error_handler)
4f3fedcf
AM
4565 (_("Warning: alignment %u of common symbol `%s' in %B is"
4566 " greater than the alignment (%u) of its section %A"),
d07676f8
NC
4567 common_bfd, h->root.u.def.section,
4568 1 << common_align, name, 1 << normal_align);
4569 else
4570 (*_bfd_error_handler)
4571 (_("Warning: alignment %u of symbol `%s' in %B"
4572 " is smaller than %u in %B"),
4573 normal_bfd, common_bfd,
4574 1 << normal_align, name, 1 << common_align);
4575 }
4ad4eba5
AM
4576 }
4577
83ad0046 4578 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4579 if (isym->st_size != 0
4580 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4581 && (definition || h->size == 0))
4582 {
83ad0046
L
4583 if (h->size != 0
4584 && h->size != isym->st_size
4585 && ! size_change_ok)
4ad4eba5 4586 (*_bfd_error_handler)
d003868e
AM
4587 (_("Warning: size of symbol `%s' changed"
4588 " from %lu in %B to %lu in %B"),
4589 old_bfd, abfd,
4ad4eba5 4590 name, (unsigned long) h->size,
d003868e 4591 (unsigned long) isym->st_size);
4ad4eba5
AM
4592
4593 h->size = isym->st_size;
4594 }
4595
4596 /* If this is a common symbol, then we always want H->SIZE
4597 to be the size of the common symbol. The code just above
4598 won't fix the size if a common symbol becomes larger. We
4599 don't warn about a size change here, because that is
4f3fedcf 4600 covered by --warn-common. Allow changes between different
fcb93ecf 4601 function types. */
4ad4eba5
AM
4602 if (h->root.type == bfd_link_hash_common)
4603 h->size = h->root.u.c.size;
4604
4605 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4606 && ((definition && !new_weak)
4607 || (old_weak && h->root.type == bfd_link_hash_common)
4608 || h->type == STT_NOTYPE))
4ad4eba5 4609 {
2955ec4c
L
4610 unsigned int type = ELF_ST_TYPE (isym->st_info);
4611
4612 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4613 symbol. */
4614 if (type == STT_GNU_IFUNC
4615 && (abfd->flags & DYNAMIC) != 0)
4616 type = STT_FUNC;
4ad4eba5 4617
2955ec4c
L
4618 if (h->type != type)
4619 {
4620 if (h->type != STT_NOTYPE && ! type_change_ok)
4621 (*_bfd_error_handler)
4622 (_("Warning: type of symbol `%s' changed"
4623 " from %d to %d in %B"),
4624 abfd, name, h->type, type);
4625
4626 h->type = type;
4627 }
4ad4eba5
AM
4628 }
4629
54ac0771 4630 /* Merge st_other field. */
b8417128 4631 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4632
c3df8c14 4633 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4634 if (definition
4635 && (sec->flags & SEC_DEBUGGING)
4636 && !bfd_link_relocatable (info))
c3df8c14
AM
4637 dynsym = FALSE;
4638
4f3fedcf
AM
4639 /* Nor should we make plugin symbols dynamic. */
4640 if ((abfd->flags & BFD_PLUGIN) != 0)
4641 dynsym = FALSE;
4642
35fc36a8 4643 if (definition)
35399224
L
4644 {
4645 h->target_internal = isym->st_target_internal;
4646 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4647 }
35fc36a8 4648
4ad4eba5
AM
4649 if (definition && !dynamic)
4650 {
4651 char *p = strchr (name, ELF_VER_CHR);
4652 if (p != NULL && p[1] != ELF_VER_CHR)
4653 {
4654 /* Queue non-default versions so that .symver x, x@FOO
4655 aliases can be checked. */
66eb6687 4656 if (!nondeflt_vers)
4ad4eba5 4657 {
66eb6687
AM
4658 amt = ((isymend - isym + 1)
4659 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4660 nondeflt_vers
4661 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4662 if (!nondeflt_vers)
4663 goto error_free_vers;
4ad4eba5 4664 }
66eb6687 4665 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4666 }
4667 }
4668
4669 if (dynsym && h->dynindx == -1)
4670 {
c152c796 4671 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4672 goto error_free_vers;
f6e332e6 4673 if (h->u.weakdef != NULL
4ad4eba5 4674 && ! new_weakdef
f6e332e6 4675 && h->u.weakdef->dynindx == -1)
4ad4eba5 4676 {
66eb6687 4677 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4678 goto error_free_vers;
4679 }
4680 }
1f599d0e 4681 else if (h->dynindx != -1)
4ad4eba5
AM
4682 /* If the symbol already has a dynamic index, but
4683 visibility says it should not be visible, turn it into
4684 a local symbol. */
4685 switch (ELF_ST_VISIBILITY (h->other))
4686 {
4687 case STV_INTERNAL:
4688 case STV_HIDDEN:
4689 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4690 dynsym = FALSE;
4691 break;
4692 }
4693
aef28989
L
4694 /* Don't add DT_NEEDED for references from the dummy bfd nor
4695 for unmatched symbol. */
4ad4eba5 4696 if (!add_needed
aef28989 4697 && matched
4ad4eba5 4698 && definition
010e5ae2 4699 && ((dynsym
ffa9430d 4700 && h->ref_regular_nonweak
4f3fedcf
AM
4701 && (old_bfd == NULL
4702 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4703 || (h->ref_dynamic_nonweak
010e5ae2 4704 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4705 && !on_needed_list (elf_dt_name (abfd),
4706 htab->needed, NULL))))
4ad4eba5
AM
4707 {
4708 int ret;
4709 const char *soname = elf_dt_name (abfd);
4710
16e4ecc0
AM
4711 info->callbacks->minfo ("%!", soname, old_bfd,
4712 h->root.root.string);
4713
4ad4eba5
AM
4714 /* A symbol from a library loaded via DT_NEEDED of some
4715 other library is referenced by a regular object.
e56f61be 4716 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4717 --no-add-needed is used and the reference was not
4718 a weak one. */
4f3fedcf 4719 if (old_bfd != NULL
b918acf9 4720 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4721 {
4722 (*_bfd_error_handler)
3cbc5de0 4723 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4724 old_bfd, name);
ff5ac77b 4725 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4726 goto error_free_vers;
4727 }
4728
a50b1753 4729 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4730 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4731
4ad4eba5 4732 add_needed = TRUE;
7e9f0867 4733 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4734 if (ret < 0)
4735 goto error_free_vers;
4736
4737 BFD_ASSERT (ret == 0);
4738 }
4739 }
4740 }
4741
66eb6687
AM
4742 if (extversym != NULL)
4743 {
4744 free (extversym);
4745 extversym = NULL;
4746 }
4747
4748 if (isymbuf != NULL)
4749 {
4750 free (isymbuf);
4751 isymbuf = NULL;
4752 }
4753
4754 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4755 {
4756 unsigned int i;
4757
4758 /* Restore the symbol table. */
f45794cb
AM
4759 old_ent = (char *) old_tab + tabsize;
4760 memset (elf_sym_hashes (abfd), 0,
4761 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4762 htab->root.table.table = old_table;
4763 htab->root.table.size = old_size;
4764 htab->root.table.count = old_count;
66eb6687 4765 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4766 htab->root.undefs = old_undefs;
4767 htab->root.undefs_tail = old_undefs_tail;
d45f8bda 4768 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
66eb6687
AM
4769 for (i = 0; i < htab->root.table.size; i++)
4770 {
4771 struct bfd_hash_entry *p;
4772 struct elf_link_hash_entry *h;
3e0882af
L
4773 bfd_size_type size;
4774 unsigned int alignment_power;
66eb6687
AM
4775
4776 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4777 {
4778 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4779 if (h->root.type == bfd_link_hash_warning)
4780 h = (struct elf_link_hash_entry *) h->root.u.i.link;
a4542f1b
AM
4781 if (h->dynindx >= old_dynsymcount
4782 && h->dynstr_index < old_dynstr_size)
66eb6687 4783 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
2de92251 4784
3e0882af
L
4785 /* Preserve the maximum alignment and size for common
4786 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4787 since it can still be loaded at run time by another
3e0882af
L
4788 dynamic lib. */
4789 if (h->root.type == bfd_link_hash_common)
4790 {
4791 size = h->root.u.c.size;
4792 alignment_power = h->root.u.c.p->alignment_power;
4793 }
4794 else
4795 {
4796 size = 0;
4797 alignment_power = 0;
4798 }
66eb6687
AM
4799 memcpy (p, old_ent, htab->root.table.entsize);
4800 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4801 h = (struct elf_link_hash_entry *) p;
4802 if (h->root.type == bfd_link_hash_warning)
4803 {
4804 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4805 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4806 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4807 }
a4542f1b 4808 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4809 {
4810 if (size > h->root.u.c.size)
4811 h->root.u.c.size = size;
4812 if (alignment_power > h->root.u.c.p->alignment_power)
4813 h->root.u.c.p->alignment_power = alignment_power;
4814 }
66eb6687
AM
4815 }
4816 }
4817
5061a885
AM
4818 /* Make a special call to the linker "notice" function to
4819 tell it that symbols added for crefs may need to be removed. */
e5034e59 4820 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4821 goto error_free_vers;
5061a885 4822
66eb6687
AM
4823 free (old_tab);
4824 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4825 alloc_mark);
4826 if (nondeflt_vers != NULL)
4827 free (nondeflt_vers);
4828 return TRUE;
4829 }
2de92251 4830
66eb6687
AM
4831 if (old_tab != NULL)
4832 {
e5034e59 4833 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4834 goto error_free_vers;
66eb6687
AM
4835 free (old_tab);
4836 old_tab = NULL;
4837 }
4838
c6e8a9a8
L
4839 /* Now that all the symbols from this input file are created, if
4840 not performing a relocatable link, handle .symver foo, foo@BAR
4841 such that any relocs against foo become foo@BAR. */
0e1862bb 4842 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5
AM
4843 {
4844 bfd_size_type cnt, symidx;
4845
4846 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4847 {
4848 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4849 char *shortname, *p;
4850
4851 p = strchr (h->root.root.string, ELF_VER_CHR);
4852 if (p == NULL
4853 || (h->root.type != bfd_link_hash_defined
4854 && h->root.type != bfd_link_hash_defweak))
4855 continue;
4856
4857 amt = p - h->root.root.string;
a50b1753 4858 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4859 if (!shortname)
4860 goto error_free_vers;
4ad4eba5
AM
4861 memcpy (shortname, h->root.root.string, amt);
4862 shortname[amt] = '\0';
4863
4864 hi = (struct elf_link_hash_entry *)
66eb6687 4865 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4866 FALSE, FALSE, FALSE);
4867 if (hi != NULL
4868 && hi->root.type == h->root.type
4869 && hi->root.u.def.value == h->root.u.def.value
4870 && hi->root.u.def.section == h->root.u.def.section)
4871 {
4872 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4873 hi->root.type = bfd_link_hash_indirect;
4874 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4875 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4876 sym_hash = elf_sym_hashes (abfd);
4877 if (sym_hash)
4878 for (symidx = 0; symidx < extsymcount; ++symidx)
4879 if (sym_hash[symidx] == hi)
4880 {
4881 sym_hash[symidx] = h;
4882 break;
4883 }
4884 }
4885 free (shortname);
4886 }
4887 free (nondeflt_vers);
4888 nondeflt_vers = NULL;
4889 }
4890
4ad4eba5
AM
4891 /* Now set the weakdefs field correctly for all the weak defined
4892 symbols we found. The only way to do this is to search all the
4893 symbols. Since we only need the information for non functions in
4894 dynamic objects, that's the only time we actually put anything on
4895 the list WEAKS. We need this information so that if a regular
4896 object refers to a symbol defined weakly in a dynamic object, the
4897 real symbol in the dynamic object is also put in the dynamic
4898 symbols; we also must arrange for both symbols to point to the
4899 same memory location. We could handle the general case of symbol
4900 aliasing, but a general symbol alias can only be generated in
4901 assembler code, handling it correctly would be very time
4902 consuming, and other ELF linkers don't handle general aliasing
4903 either. */
4904 if (weaks != NULL)
4905 {
4906 struct elf_link_hash_entry **hpp;
4907 struct elf_link_hash_entry **hppend;
4908 struct elf_link_hash_entry **sorted_sym_hash;
4909 struct elf_link_hash_entry *h;
4910 size_t sym_count;
4911
4912 /* Since we have to search the whole symbol list for each weak
4913 defined symbol, search time for N weak defined symbols will be
4914 O(N^2). Binary search will cut it down to O(NlogN). */
4915 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 4916 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4917 if (sorted_sym_hash == NULL)
4918 goto error_return;
4919 sym_hash = sorted_sym_hash;
4920 hpp = elf_sym_hashes (abfd);
4921 hppend = hpp + extsymcount;
4922 sym_count = 0;
4923 for (; hpp < hppend; hpp++)
4924 {
4925 h = *hpp;
4926 if (h != NULL
4927 && h->root.type == bfd_link_hash_defined
fcb93ecf 4928 && !bed->is_function_type (h->type))
4ad4eba5
AM
4929 {
4930 *sym_hash = h;
4931 sym_hash++;
4932 sym_count++;
4933 }
4934 }
4935
4936 qsort (sorted_sym_hash, sym_count,
4937 sizeof (struct elf_link_hash_entry *),
4938 elf_sort_symbol);
4939
4940 while (weaks != NULL)
4941 {
4942 struct elf_link_hash_entry *hlook;
4943 asection *slook;
4944 bfd_vma vlook;
ed54588d 4945 size_t i, j, idx = 0;
4ad4eba5
AM
4946
4947 hlook = weaks;
f6e332e6
AM
4948 weaks = hlook->u.weakdef;
4949 hlook->u.weakdef = NULL;
4ad4eba5
AM
4950
4951 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4952 || hlook->root.type == bfd_link_hash_defweak
4953 || hlook->root.type == bfd_link_hash_common
4954 || hlook->root.type == bfd_link_hash_indirect);
4955 slook = hlook->root.u.def.section;
4956 vlook = hlook->root.u.def.value;
4957
4ad4eba5
AM
4958 i = 0;
4959 j = sym_count;
14160578 4960 while (i != j)
4ad4eba5
AM
4961 {
4962 bfd_signed_vma vdiff;
4963 idx = (i + j) / 2;
14160578 4964 h = sorted_sym_hash[idx];
4ad4eba5
AM
4965 vdiff = vlook - h->root.u.def.value;
4966 if (vdiff < 0)
4967 j = idx;
4968 else if (vdiff > 0)
4969 i = idx + 1;
4970 else
4971 {
d3435ae8 4972 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4973 if (sdiff < 0)
4974 j = idx;
4975 else if (sdiff > 0)
4976 i = idx + 1;
4977 else
14160578 4978 break;
4ad4eba5
AM
4979 }
4980 }
4981
4982 /* We didn't find a value/section match. */
14160578 4983 if (i == j)
4ad4eba5
AM
4984 continue;
4985
14160578
AM
4986 /* With multiple aliases, or when the weak symbol is already
4987 strongly defined, we have multiple matching symbols and
4988 the binary search above may land on any of them. Step
4989 one past the matching symbol(s). */
4990 while (++idx != j)
4991 {
4992 h = sorted_sym_hash[idx];
4993 if (h->root.u.def.section != slook
4994 || h->root.u.def.value != vlook)
4995 break;
4996 }
4997
4998 /* Now look back over the aliases. Since we sorted by size
4999 as well as value and section, we'll choose the one with
5000 the largest size. */
5001 while (idx-- != i)
4ad4eba5 5002 {
14160578 5003 h = sorted_sym_hash[idx];
4ad4eba5
AM
5004
5005 /* Stop if value or section doesn't match. */
14160578
AM
5006 if (h->root.u.def.section != slook
5007 || h->root.u.def.value != vlook)
4ad4eba5
AM
5008 break;
5009 else if (h != hlook)
5010 {
f6e332e6 5011 hlook->u.weakdef = h;
4ad4eba5
AM
5012
5013 /* If the weak definition is in the list of dynamic
5014 symbols, make sure the real definition is put
5015 there as well. */
5016 if (hlook->dynindx != -1 && h->dynindx == -1)
5017 {
c152c796 5018 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5019 {
5020 err_free_sym_hash:
5021 free (sorted_sym_hash);
5022 goto error_return;
5023 }
4ad4eba5
AM
5024 }
5025
5026 /* If the real definition is in the list of dynamic
5027 symbols, make sure the weak definition is put
5028 there as well. If we don't do this, then the
5029 dynamic loader might not merge the entries for the
5030 real definition and the weak definition. */
5031 if (h->dynindx != -1 && hlook->dynindx == -1)
5032 {
c152c796 5033 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5034 goto err_free_sym_hash;
4ad4eba5
AM
5035 }
5036 break;
5037 }
5038 }
5039 }
5040
5041 free (sorted_sym_hash);
5042 }
5043
33177bb1
AM
5044 if (bed->check_directives
5045 && !(*bed->check_directives) (abfd, info))
5046 return FALSE;
85fbca6a 5047
d9689752
L
5048 if (!info->check_relocs_after_open_input
5049 && !_bfd_elf_link_check_relocs (abfd, info))
5050 return FALSE;
4ad4eba5
AM
5051
5052 /* If this is a non-traditional link, try to optimize the handling
5053 of the .stab/.stabstr sections. */
5054 if (! dynamic
5055 && ! info->traditional_format
66eb6687 5056 && is_elf_hash_table (htab)
4ad4eba5
AM
5057 && (info->strip != strip_all && info->strip != strip_debugger))
5058 {
5059 asection *stabstr;
5060
5061 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5062 if (stabstr != NULL)
5063 {
5064 bfd_size_type string_offset = 0;
5065 asection *stab;
5066
5067 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5068 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5069 && (!stab->name[5] ||
5070 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5071 && (stab->flags & SEC_MERGE) == 0
5072 && !bfd_is_abs_section (stab->output_section))
5073 {
5074 struct bfd_elf_section_data *secdata;
5075
5076 secdata = elf_section_data (stab);
66eb6687
AM
5077 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5078 stabstr, &secdata->sec_info,
4ad4eba5
AM
5079 &string_offset))
5080 goto error_return;
5081 if (secdata->sec_info)
dbaa2011 5082 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5083 }
5084 }
5085 }
5086
66eb6687 5087 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5088 {
5089 /* Add this bfd to the loaded list. */
5090 struct elf_link_loaded_list *n;
5091
ca4be51c 5092 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5093 if (n == NULL)
5094 goto error_return;
5095 n->abfd = abfd;
66eb6687
AM
5096 n->next = htab->loaded;
5097 htab->loaded = n;
4ad4eba5
AM
5098 }
5099
5100 return TRUE;
5101
5102 error_free_vers:
66eb6687
AM
5103 if (old_tab != NULL)
5104 free (old_tab);
4ad4eba5
AM
5105 if (nondeflt_vers != NULL)
5106 free (nondeflt_vers);
5107 if (extversym != NULL)
5108 free (extversym);
5109 error_free_sym:
5110 if (isymbuf != NULL)
5111 free (isymbuf);
5112 error_return:
5113 return FALSE;
5114}
5115
8387904d
AM
5116/* Return the linker hash table entry of a symbol that might be
5117 satisfied by an archive symbol. Return -1 on error. */
5118
5119struct elf_link_hash_entry *
5120_bfd_elf_archive_symbol_lookup (bfd *abfd,
5121 struct bfd_link_info *info,
5122 const char *name)
5123{
5124 struct elf_link_hash_entry *h;
5125 char *p, *copy;
5126 size_t len, first;
5127
2a41f396 5128 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5129 if (h != NULL)
5130 return h;
5131
5132 /* If this is a default version (the name contains @@), look up the
5133 symbol again with only one `@' as well as without the version.
5134 The effect is that references to the symbol with and without the
5135 version will be matched by the default symbol in the archive. */
5136
5137 p = strchr (name, ELF_VER_CHR);
5138 if (p == NULL || p[1] != ELF_VER_CHR)
5139 return h;
5140
5141 /* First check with only one `@'. */
5142 len = strlen (name);
a50b1753 5143 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5144 if (copy == NULL)
5145 return (struct elf_link_hash_entry *) 0 - 1;
5146
5147 first = p - name + 1;
5148 memcpy (copy, name, first);
5149 memcpy (copy + first, name + first + 1, len - first);
5150
2a41f396 5151 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5152 if (h == NULL)
5153 {
5154 /* We also need to check references to the symbol without the
5155 version. */
5156 copy[first - 1] = '\0';
5157 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5158 FALSE, FALSE, TRUE);
8387904d
AM
5159 }
5160
5161 bfd_release (abfd, copy);
5162 return h;
5163}
5164
0ad989f9 5165/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5166 don't use _bfd_generic_link_add_archive_symbols because we need to
5167 handle versioned symbols.
0ad989f9
L
5168
5169 Fortunately, ELF archive handling is simpler than that done by
5170 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5171 oddities. In ELF, if we find a symbol in the archive map, and the
5172 symbol is currently undefined, we know that we must pull in that
5173 object file.
5174
5175 Unfortunately, we do have to make multiple passes over the symbol
5176 table until nothing further is resolved. */
5177
4ad4eba5
AM
5178static bfd_boolean
5179elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5180{
5181 symindex c;
13e570f8 5182 unsigned char *included = NULL;
0ad989f9
L
5183 carsym *symdefs;
5184 bfd_boolean loop;
5185 bfd_size_type amt;
8387904d
AM
5186 const struct elf_backend_data *bed;
5187 struct elf_link_hash_entry * (*archive_symbol_lookup)
5188 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5189
5190 if (! bfd_has_map (abfd))
5191 {
5192 /* An empty archive is a special case. */
5193 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5194 return TRUE;
5195 bfd_set_error (bfd_error_no_armap);
5196 return FALSE;
5197 }
5198
5199 /* Keep track of all symbols we know to be already defined, and all
5200 files we know to be already included. This is to speed up the
5201 second and subsequent passes. */
5202 c = bfd_ardata (abfd)->symdef_count;
5203 if (c == 0)
5204 return TRUE;
5205 amt = c;
13e570f8
AM
5206 amt *= sizeof (*included);
5207 included = (unsigned char *) bfd_zmalloc (amt);
5208 if (included == NULL)
5209 return FALSE;
0ad989f9
L
5210
5211 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5212 bed = get_elf_backend_data (abfd);
5213 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5214
5215 do
5216 {
5217 file_ptr last;
5218 symindex i;
5219 carsym *symdef;
5220 carsym *symdefend;
5221
5222 loop = FALSE;
5223 last = -1;
5224
5225 symdef = symdefs;
5226 symdefend = symdef + c;
5227 for (i = 0; symdef < symdefend; symdef++, i++)
5228 {
5229 struct elf_link_hash_entry *h;
5230 bfd *element;
5231 struct bfd_link_hash_entry *undefs_tail;
5232 symindex mark;
5233
13e570f8 5234 if (included[i])
0ad989f9
L
5235 continue;
5236 if (symdef->file_offset == last)
5237 {
5238 included[i] = TRUE;
5239 continue;
5240 }
5241
8387904d
AM
5242 h = archive_symbol_lookup (abfd, info, symdef->name);
5243 if (h == (struct elf_link_hash_entry *) 0 - 1)
5244 goto error_return;
0ad989f9
L
5245
5246 if (h == NULL)
5247 continue;
5248
5249 if (h->root.type == bfd_link_hash_common)
5250 {
5251 /* We currently have a common symbol. The archive map contains
5252 a reference to this symbol, so we may want to include it. We
5253 only want to include it however, if this archive element
5254 contains a definition of the symbol, not just another common
5255 declaration of it.
5256
5257 Unfortunately some archivers (including GNU ar) will put
5258 declarations of common symbols into their archive maps, as
5259 well as real definitions, so we cannot just go by the archive
5260 map alone. Instead we must read in the element's symbol
5261 table and check that to see what kind of symbol definition
5262 this is. */
5263 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5264 continue;
5265 }
5266 else if (h->root.type != bfd_link_hash_undefined)
5267 {
5268 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5269 /* Symbol must be defined. Don't check it again. */
5270 included[i] = TRUE;
0ad989f9
L
5271 continue;
5272 }
5273
5274 /* We need to include this archive member. */
5275 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5276 if (element == NULL)
5277 goto error_return;
5278
5279 if (! bfd_check_format (element, bfd_object))
5280 goto error_return;
5281
0ad989f9
L
5282 undefs_tail = info->hash->undefs_tail;
5283
0e144ba7
AM
5284 if (!(*info->callbacks
5285 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5286 continue;
0e144ba7 5287 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5288 goto error_return;
5289
5290 /* If there are any new undefined symbols, we need to make
5291 another pass through the archive in order to see whether
5292 they can be defined. FIXME: This isn't perfect, because
5293 common symbols wind up on undefs_tail and because an
5294 undefined symbol which is defined later on in this pass
5295 does not require another pass. This isn't a bug, but it
5296 does make the code less efficient than it could be. */
5297 if (undefs_tail != info->hash->undefs_tail)
5298 loop = TRUE;
5299
5300 /* Look backward to mark all symbols from this object file
5301 which we have already seen in this pass. */
5302 mark = i;
5303 do
5304 {
5305 included[mark] = TRUE;
5306 if (mark == 0)
5307 break;
5308 --mark;
5309 }
5310 while (symdefs[mark].file_offset == symdef->file_offset);
5311
5312 /* We mark subsequent symbols from this object file as we go
5313 on through the loop. */
5314 last = symdef->file_offset;
5315 }
5316 }
5317 while (loop);
5318
0ad989f9
L
5319 free (included);
5320
5321 return TRUE;
5322
5323 error_return:
0ad989f9
L
5324 if (included != NULL)
5325 free (included);
5326 return FALSE;
5327}
4ad4eba5
AM
5328
5329/* Given an ELF BFD, add symbols to the global hash table as
5330 appropriate. */
5331
5332bfd_boolean
5333bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5334{
5335 switch (bfd_get_format (abfd))
5336 {
5337 case bfd_object:
5338 return elf_link_add_object_symbols (abfd, info);
5339 case bfd_archive:
5340 return elf_link_add_archive_symbols (abfd, info);
5341 default:
5342 bfd_set_error (bfd_error_wrong_format);
5343 return FALSE;
5344 }
5345}
5a580b3a 5346\f
14b1c01e
AM
5347struct hash_codes_info
5348{
5349 unsigned long *hashcodes;
5350 bfd_boolean error;
5351};
a0c8462f 5352
5a580b3a
AM
5353/* This function will be called though elf_link_hash_traverse to store
5354 all hash value of the exported symbols in an array. */
5355
5356static bfd_boolean
5357elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5358{
a50b1753 5359 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5360 const char *name;
5a580b3a
AM
5361 unsigned long ha;
5362 char *alc = NULL;
5363
5a580b3a
AM
5364 /* Ignore indirect symbols. These are added by the versioning code. */
5365 if (h->dynindx == -1)
5366 return TRUE;
5367
5368 name = h->root.root.string;
422f1182 5369 if (h->versioned >= versioned)
5a580b3a 5370 {
422f1182
L
5371 char *p = strchr (name, ELF_VER_CHR);
5372 if (p != NULL)
14b1c01e 5373 {
422f1182
L
5374 alc = (char *) bfd_malloc (p - name + 1);
5375 if (alc == NULL)
5376 {
5377 inf->error = TRUE;
5378 return FALSE;
5379 }
5380 memcpy (alc, name, p - name);
5381 alc[p - name] = '\0';
5382 name = alc;
14b1c01e 5383 }
5a580b3a
AM
5384 }
5385
5386 /* Compute the hash value. */
5387 ha = bfd_elf_hash (name);
5388
5389 /* Store the found hash value in the array given as the argument. */
14b1c01e 5390 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5391
5392 /* And store it in the struct so that we can put it in the hash table
5393 later. */
f6e332e6 5394 h->u.elf_hash_value = ha;
5a580b3a
AM
5395
5396 if (alc != NULL)
5397 free (alc);
5398
5399 return TRUE;
5400}
5401
fdc90cb4
JJ
5402struct collect_gnu_hash_codes
5403{
5404 bfd *output_bfd;
5405 const struct elf_backend_data *bed;
5406 unsigned long int nsyms;
5407 unsigned long int maskbits;
5408 unsigned long int *hashcodes;
5409 unsigned long int *hashval;
5410 unsigned long int *indx;
5411 unsigned long int *counts;
5412 bfd_vma *bitmask;
5413 bfd_byte *contents;
5414 long int min_dynindx;
5415 unsigned long int bucketcount;
5416 unsigned long int symindx;
5417 long int local_indx;
5418 long int shift1, shift2;
5419 unsigned long int mask;
14b1c01e 5420 bfd_boolean error;
fdc90cb4
JJ
5421};
5422
5423/* This function will be called though elf_link_hash_traverse to store
5424 all hash value of the exported symbols in an array. */
5425
5426static bfd_boolean
5427elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5428{
a50b1753 5429 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5430 const char *name;
fdc90cb4
JJ
5431 unsigned long ha;
5432 char *alc = NULL;
5433
fdc90cb4
JJ
5434 /* Ignore indirect symbols. These are added by the versioning code. */
5435 if (h->dynindx == -1)
5436 return TRUE;
5437
5438 /* Ignore also local symbols and undefined symbols. */
5439 if (! (*s->bed->elf_hash_symbol) (h))
5440 return TRUE;
5441
5442 name = h->root.root.string;
422f1182 5443 if (h->versioned >= versioned)
fdc90cb4 5444 {
422f1182
L
5445 char *p = strchr (name, ELF_VER_CHR);
5446 if (p != NULL)
14b1c01e 5447 {
422f1182
L
5448 alc = (char *) bfd_malloc (p - name + 1);
5449 if (alc == NULL)
5450 {
5451 s->error = TRUE;
5452 return FALSE;
5453 }
5454 memcpy (alc, name, p - name);
5455 alc[p - name] = '\0';
5456 name = alc;
14b1c01e 5457 }
fdc90cb4
JJ
5458 }
5459
5460 /* Compute the hash value. */
5461 ha = bfd_elf_gnu_hash (name);
5462
5463 /* Store the found hash value in the array for compute_bucket_count,
5464 and also for .dynsym reordering purposes. */
5465 s->hashcodes[s->nsyms] = ha;
5466 s->hashval[h->dynindx] = ha;
5467 ++s->nsyms;
5468 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5469 s->min_dynindx = h->dynindx;
5470
5471 if (alc != NULL)
5472 free (alc);
5473
5474 return TRUE;
5475}
5476
5477/* This function will be called though elf_link_hash_traverse to do
5478 final dynaminc symbol renumbering. */
5479
5480static bfd_boolean
5481elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5482{
a50b1753 5483 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5484 unsigned long int bucket;
5485 unsigned long int val;
5486
fdc90cb4
JJ
5487 /* Ignore indirect symbols. */
5488 if (h->dynindx == -1)
5489 return TRUE;
5490
5491 /* Ignore also local symbols and undefined symbols. */
5492 if (! (*s->bed->elf_hash_symbol) (h))
5493 {
5494 if (h->dynindx >= s->min_dynindx)
5495 h->dynindx = s->local_indx++;
5496 return TRUE;
5497 }
5498
5499 bucket = s->hashval[h->dynindx] % s->bucketcount;
5500 val = (s->hashval[h->dynindx] >> s->shift1)
5501 & ((s->maskbits >> s->shift1) - 1);
5502 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5503 s->bitmask[val]
5504 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5505 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5506 if (s->counts[bucket] == 1)
5507 /* Last element terminates the chain. */
5508 val |= 1;
5509 bfd_put_32 (s->output_bfd, val,
5510 s->contents + (s->indx[bucket] - s->symindx) * 4);
5511 --s->counts[bucket];
5512 h->dynindx = s->indx[bucket]++;
5513 return TRUE;
5514}
5515
5516/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5517
5518bfd_boolean
5519_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5520{
5521 return !(h->forced_local
5522 || h->root.type == bfd_link_hash_undefined
5523 || h->root.type == bfd_link_hash_undefweak
5524 || ((h->root.type == bfd_link_hash_defined
5525 || h->root.type == bfd_link_hash_defweak)
5526 && h->root.u.def.section->output_section == NULL));
5527}
5528
5a580b3a
AM
5529/* Array used to determine the number of hash table buckets to use
5530 based on the number of symbols there are. If there are fewer than
5531 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5532 fewer than 37 we use 17 buckets, and so forth. We never use more
5533 than 32771 buckets. */
5534
5535static const size_t elf_buckets[] =
5536{
5537 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5538 16411, 32771, 0
5539};
5540
5541/* Compute bucket count for hashing table. We do not use a static set
5542 of possible tables sizes anymore. Instead we determine for all
5543 possible reasonable sizes of the table the outcome (i.e., the
5544 number of collisions etc) and choose the best solution. The
5545 weighting functions are not too simple to allow the table to grow
5546 without bounds. Instead one of the weighting factors is the size.
5547 Therefore the result is always a good payoff between few collisions
5548 (= short chain lengths) and table size. */
5549static size_t
b20dd2ce 5550compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5551 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5552 unsigned long int nsyms,
5553 int gnu_hash)
5a580b3a 5554{
5a580b3a 5555 size_t best_size = 0;
5a580b3a 5556 unsigned long int i;
5a580b3a 5557
5a580b3a
AM
5558 /* We have a problem here. The following code to optimize the table
5559 size requires an integer type with more the 32 bits. If
5560 BFD_HOST_U_64_BIT is set we know about such a type. */
5561#ifdef BFD_HOST_U_64_BIT
5562 if (info->optimize)
5563 {
5a580b3a
AM
5564 size_t minsize;
5565 size_t maxsize;
5566 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5567 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5568 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5569 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5570 unsigned long int *counts;
d40f3da9 5571 bfd_size_type amt;
0883b6e0 5572 unsigned int no_improvement_count = 0;
5a580b3a
AM
5573
5574 /* Possible optimization parameters: if we have NSYMS symbols we say
5575 that the hashing table must at least have NSYMS/4 and at most
5576 2*NSYMS buckets. */
5577 minsize = nsyms / 4;
5578 if (minsize == 0)
5579 minsize = 1;
5580 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5581 if (gnu_hash)
5582 {
5583 if (minsize < 2)
5584 minsize = 2;
5585 if ((best_size & 31) == 0)
5586 ++best_size;
5587 }
5a580b3a
AM
5588
5589 /* Create array where we count the collisions in. We must use bfd_malloc
5590 since the size could be large. */
5591 amt = maxsize;
5592 amt *= sizeof (unsigned long int);
a50b1753 5593 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5594 if (counts == NULL)
fdc90cb4 5595 return 0;
5a580b3a
AM
5596
5597 /* Compute the "optimal" size for the hash table. The criteria is a
5598 minimal chain length. The minor criteria is (of course) the size
5599 of the table. */
5600 for (i = minsize; i < maxsize; ++i)
5601 {
5602 /* Walk through the array of hashcodes and count the collisions. */
5603 BFD_HOST_U_64_BIT max;
5604 unsigned long int j;
5605 unsigned long int fact;
5606
fdc90cb4
JJ
5607 if (gnu_hash && (i & 31) == 0)
5608 continue;
5609
5a580b3a
AM
5610 memset (counts, '\0', i * sizeof (unsigned long int));
5611
5612 /* Determine how often each hash bucket is used. */
5613 for (j = 0; j < nsyms; ++j)
5614 ++counts[hashcodes[j] % i];
5615
5616 /* For the weight function we need some information about the
5617 pagesize on the target. This is information need not be 100%
5618 accurate. Since this information is not available (so far) we
5619 define it here to a reasonable default value. If it is crucial
5620 to have a better value some day simply define this value. */
5621# ifndef BFD_TARGET_PAGESIZE
5622# define BFD_TARGET_PAGESIZE (4096)
5623# endif
5624
fdc90cb4
JJ
5625 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5626 and the chains. */
5627 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5628
5629# if 1
5630 /* Variant 1: optimize for short chains. We add the squares
5631 of all the chain lengths (which favors many small chain
5632 over a few long chains). */
5633 for (j = 0; j < i; ++j)
5634 max += counts[j] * counts[j];
5635
5636 /* This adds penalties for the overall size of the table. */
fdc90cb4 5637 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5638 max *= fact * fact;
5639# else
5640 /* Variant 2: Optimize a lot more for small table. Here we
5641 also add squares of the size but we also add penalties for
5642 empty slots (the +1 term). */
5643 for (j = 0; j < i; ++j)
5644 max += (1 + counts[j]) * (1 + counts[j]);
5645
5646 /* The overall size of the table is considered, but not as
5647 strong as in variant 1, where it is squared. */
fdc90cb4 5648 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5649 max *= fact;
5650# endif
5651
5652 /* Compare with current best results. */
5653 if (max < best_chlen)
5654 {
5655 best_chlen = max;
5656 best_size = i;
ca4be51c 5657 no_improvement_count = 0;
5a580b3a 5658 }
0883b6e0
NC
5659 /* PR 11843: Avoid futile long searches for the best bucket size
5660 when there are a large number of symbols. */
5661 else if (++no_improvement_count == 100)
5662 break;
5a580b3a
AM
5663 }
5664
5665 free (counts);
5666 }
5667 else
5668#endif /* defined (BFD_HOST_U_64_BIT) */
5669 {
5670 /* This is the fallback solution if no 64bit type is available or if we
5671 are not supposed to spend much time on optimizations. We select the
5672 bucket count using a fixed set of numbers. */
5673 for (i = 0; elf_buckets[i] != 0; i++)
5674 {
5675 best_size = elf_buckets[i];
fdc90cb4 5676 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5677 break;
5678 }
fdc90cb4
JJ
5679 if (gnu_hash && best_size < 2)
5680 best_size = 2;
5a580b3a
AM
5681 }
5682
5a580b3a
AM
5683 return best_size;
5684}
5685
d0bf826b
AM
5686/* Size any SHT_GROUP section for ld -r. */
5687
5688bfd_boolean
5689_bfd_elf_size_group_sections (struct bfd_link_info *info)
5690{
5691 bfd *ibfd;
5692
c72f2fb2 5693 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5694 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5695 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5696 return FALSE;
5697 return TRUE;
5698}
5699
04c3a755
NS
5700/* Set a default stack segment size. The value in INFO wins. If it
5701 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5702 undefined it is initialized. */
5703
5704bfd_boolean
5705bfd_elf_stack_segment_size (bfd *output_bfd,
5706 struct bfd_link_info *info,
5707 const char *legacy_symbol,
5708 bfd_vma default_size)
5709{
5710 struct elf_link_hash_entry *h = NULL;
5711
5712 /* Look for legacy symbol. */
5713 if (legacy_symbol)
5714 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5715 FALSE, FALSE, FALSE);
5716 if (h && (h->root.type == bfd_link_hash_defined
5717 || h->root.type == bfd_link_hash_defweak)
5718 && h->def_regular
5719 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5720 {
5721 /* The symbol has no type if specified on the command line. */
5722 h->type = STT_OBJECT;
5723 if (info->stacksize)
5724 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5725 output_bfd, legacy_symbol);
5726 else if (h->root.u.def.section != bfd_abs_section_ptr)
5727 (*_bfd_error_handler) (_("%B: %s not absolute"),
5728 output_bfd, legacy_symbol);
5729 else
5730 info->stacksize = h->root.u.def.value;
5731 }
5732
5733 if (!info->stacksize)
5734 /* If the user didn't set a size, or explicitly inhibit the
5735 size, set it now. */
5736 info->stacksize = default_size;
5737
5738 /* Provide the legacy symbol, if it is referenced. */
5739 if (h && (h->root.type == bfd_link_hash_undefined
5740 || h->root.type == bfd_link_hash_undefweak))
5741 {
5742 struct bfd_link_hash_entry *bh = NULL;
5743
5744 if (!(_bfd_generic_link_add_one_symbol
5745 (info, output_bfd, legacy_symbol,
5746 BSF_GLOBAL, bfd_abs_section_ptr,
5747 info->stacksize >= 0 ? info->stacksize : 0,
5748 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5749 return FALSE;
5750
5751 h = (struct elf_link_hash_entry *) bh;
5752 h->def_regular = 1;
5753 h->type = STT_OBJECT;
5754 }
5755
5756 return TRUE;
5757}
5758
5a580b3a
AM
5759/* Set up the sizes and contents of the ELF dynamic sections. This is
5760 called by the ELF linker emulation before_allocation routine. We
5761 must set the sizes of the sections before the linker sets the
5762 addresses of the various sections. */
5763
5764bfd_boolean
5765bfd_elf_size_dynamic_sections (bfd *output_bfd,
5766 const char *soname,
5767 const char *rpath,
5768 const char *filter_shlib,
7ee314fa
AM
5769 const char *audit,
5770 const char *depaudit,
5a580b3a
AM
5771 const char * const *auxiliary_filters,
5772 struct bfd_link_info *info,
fd91d419 5773 asection **sinterpptr)
5a580b3a
AM
5774{
5775 bfd_size_type soname_indx;
5776 bfd *dynobj;
5777 const struct elf_backend_data *bed;
28caa186 5778 struct elf_info_failed asvinfo;
5a580b3a
AM
5779
5780 *sinterpptr = NULL;
5781
5782 soname_indx = (bfd_size_type) -1;
5783
5784 if (!is_elf_hash_table (info->hash))
5785 return TRUE;
5786
6bfdb61b 5787 bed = get_elf_backend_data (output_bfd);
04c3a755
NS
5788
5789 /* Any syms created from now on start with -1 in
5790 got.refcount/offset and plt.refcount/offset. */
5791 elf_hash_table (info)->init_got_refcount
5792 = elf_hash_table (info)->init_got_offset;
5793 elf_hash_table (info)->init_plt_refcount
5794 = elf_hash_table (info)->init_plt_offset;
5795
0e1862bb 5796 if (bfd_link_relocatable (info)
04c3a755
NS
5797 && !_bfd_elf_size_group_sections (info))
5798 return FALSE;
5799
5800 /* The backend may have to create some sections regardless of whether
5801 we're dynamic or not. */
5802 if (bed->elf_backend_always_size_sections
5803 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5804 return FALSE;
5805
5806 /* Determine any GNU_STACK segment requirements, after the backend
5807 has had a chance to set a default segment size. */
5a580b3a 5808 if (info->execstack)
12bd6957 5809 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5a580b3a 5810 else if (info->noexecstack)
12bd6957 5811 elf_stack_flags (output_bfd) = PF_R | PF_W;
5a580b3a
AM
5812 else
5813 {
5814 bfd *inputobj;
5815 asection *notesec = NULL;
5816 int exec = 0;
5817
5818 for (inputobj = info->input_bfds;
5819 inputobj;
c72f2fb2 5820 inputobj = inputobj->link.next)
5a580b3a
AM
5821 {
5822 asection *s;
5823
a92c088a
L
5824 if (inputobj->flags
5825 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5826 continue;
5827 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5828 if (s)
5829 {
5830 if (s->flags & SEC_CODE)
5831 exec = PF_X;
5832 notesec = s;
5833 }
6bfdb61b 5834 else if (bed->default_execstack)
5a580b3a
AM
5835 exec = PF_X;
5836 }
04c3a755 5837 if (notesec || info->stacksize > 0)
12bd6957 5838 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
0e1862bb 5839 if (notesec && exec && bfd_link_relocatable (info)
04c3a755
NS
5840 && notesec->output_section != bfd_abs_section_ptr)
5841 notesec->output_section->flags |= SEC_CODE;
5a580b3a
AM
5842 }
5843
5a580b3a
AM
5844 dynobj = elf_hash_table (info)->dynobj;
5845
9a2a56cc 5846 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
5847 {
5848 struct elf_info_failed eif;
5849 struct elf_link_hash_entry *h;
5850 asection *dynstr;
5851 struct bfd_elf_version_tree *t;
5852 struct bfd_elf_version_expr *d;
046183de 5853 asection *s;
5a580b3a
AM
5854 bfd_boolean all_defined;
5855
3d4d4302 5856 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
9b8b325a 5857 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5a580b3a
AM
5858
5859 if (soname != NULL)
5860 {
5861 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5862 soname, TRUE);
5863 if (soname_indx == (bfd_size_type) -1
5864 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5865 return FALSE;
5866 }
5867
5868 if (info->symbolic)
5869 {
5870 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5871 return FALSE;
5872 info->flags |= DF_SYMBOLIC;
5873 }
5874
5875 if (rpath != NULL)
5876 {
5877 bfd_size_type indx;
b1b00fcc 5878 bfd_vma tag;
5a580b3a
AM
5879
5880 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5881 TRUE);
b1b00fcc 5882 if (indx == (bfd_size_type) -1)
5a580b3a
AM
5883 return FALSE;
5884
b1b00fcc
MF
5885 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5886 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5887 return FALSE;
5a580b3a
AM
5888 }
5889
5890 if (filter_shlib != NULL)
5891 {
5892 bfd_size_type indx;
5893
5894 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5895 filter_shlib, TRUE);
5896 if (indx == (bfd_size_type) -1
5897 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5898 return FALSE;
5899 }
5900
5901 if (auxiliary_filters != NULL)
5902 {
5903 const char * const *p;
5904
5905 for (p = auxiliary_filters; *p != NULL; p++)
5906 {
5907 bfd_size_type indx;
5908
5909 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5910 *p, TRUE);
5911 if (indx == (bfd_size_type) -1
5912 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5913 return FALSE;
5914 }
5915 }
5916
7ee314fa
AM
5917 if (audit != NULL)
5918 {
5919 bfd_size_type indx;
5920
5921 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5922 TRUE);
5923 if (indx == (bfd_size_type) -1
5924 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5925 return FALSE;
5926 }
5927
5928 if (depaudit != NULL)
5929 {
5930 bfd_size_type indx;
5931
5932 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5933 TRUE);
5934 if (indx == (bfd_size_type) -1
5935 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5936 return FALSE;
5937 }
5938
5a580b3a 5939 eif.info = info;
5a580b3a
AM
5940 eif.failed = FALSE;
5941
5942 /* If we are supposed to export all symbols into the dynamic symbol
5943 table (this is not the normal case), then do so. */
55255dae 5944 if (info->export_dynamic
0e1862bb 5945 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
5946 {
5947 elf_link_hash_traverse (elf_hash_table (info),
5948 _bfd_elf_export_symbol,
5949 &eif);
5950 if (eif.failed)
5951 return FALSE;
5952 }
5953
5954 /* Make all global versions with definition. */
fd91d419 5955 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5956 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5957 if (!d->symver && d->literal)
5a580b3a
AM
5958 {
5959 const char *verstr, *name;
5960 size_t namelen, verlen, newlen;
93252b1c 5961 char *newname, *p, leading_char;
5a580b3a
AM
5962 struct elf_link_hash_entry *newh;
5963
93252b1c 5964 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 5965 name = d->pattern;
93252b1c 5966 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
5967 verstr = t->name;
5968 verlen = strlen (verstr);
5969 newlen = namelen + verlen + 3;
5970
a50b1753 5971 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
5972 if (newname == NULL)
5973 return FALSE;
93252b1c
MF
5974 newname[0] = leading_char;
5975 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
5976
5977 /* Check the hidden versioned definition. */
5978 p = newname + namelen;
5979 *p++ = ELF_VER_CHR;
5980 memcpy (p, verstr, verlen + 1);
5981 newh = elf_link_hash_lookup (elf_hash_table (info),
5982 newname, FALSE, FALSE,
5983 FALSE);
5984 if (newh == NULL
5985 || (newh->root.type != bfd_link_hash_defined
5986 && newh->root.type != bfd_link_hash_defweak))
5987 {
5988 /* Check the default versioned definition. */
5989 *p++ = ELF_VER_CHR;
5990 memcpy (p, verstr, verlen + 1);
5991 newh = elf_link_hash_lookup (elf_hash_table (info),
5992 newname, FALSE, FALSE,
5993 FALSE);
5994 }
5995 free (newname);
5996
5997 /* Mark this version if there is a definition and it is
5998 not defined in a shared object. */
5999 if (newh != NULL
f5385ebf 6000 && !newh->def_dynamic
5a580b3a
AM
6001 && (newh->root.type == bfd_link_hash_defined
6002 || newh->root.type == bfd_link_hash_defweak))
6003 d->symver = 1;
6004 }
6005
6006 /* Attach all the symbols to their version information. */
5a580b3a 6007 asvinfo.info = info;
5a580b3a
AM
6008 asvinfo.failed = FALSE;
6009
6010 elf_link_hash_traverse (elf_hash_table (info),
6011 _bfd_elf_link_assign_sym_version,
6012 &asvinfo);
6013 if (asvinfo.failed)
6014 return FALSE;
6015
6016 if (!info->allow_undefined_version)
6017 {
6018 /* Check if all global versions have a definition. */
6019 all_defined = TRUE;
fd91d419 6020 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6021 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6022 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
6023 {
6024 (*_bfd_error_handler)
6025 (_("%s: undefined version: %s"),
6026 d->pattern, t->name);
6027 all_defined = FALSE;
6028 }
6029
6030 if (!all_defined)
6031 {
6032 bfd_set_error (bfd_error_bad_value);
6033 return FALSE;
6034 }
6035 }
6036
6037 /* Find all symbols which were defined in a dynamic object and make
6038 the backend pick a reasonable value for them. */
6039 elf_link_hash_traverse (elf_hash_table (info),
6040 _bfd_elf_adjust_dynamic_symbol,
6041 &eif);
6042 if (eif.failed)
6043 return FALSE;
6044
6045 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 6046 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
6047 now so that we know the final size of the .dynamic section. */
6048
6049 /* If there are initialization and/or finalization functions to
6050 call then add the corresponding DT_INIT/DT_FINI entries. */
6051 h = (info->init_function
6052 ? elf_link_hash_lookup (elf_hash_table (info),
6053 info->init_function, FALSE,
6054 FALSE, FALSE)
6055 : NULL);
6056 if (h != NULL
f5385ebf
AM
6057 && (h->ref_regular
6058 || h->def_regular))
5a580b3a
AM
6059 {
6060 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6061 return FALSE;
6062 }
6063 h = (info->fini_function
6064 ? elf_link_hash_lookup (elf_hash_table (info),
6065 info->fini_function, FALSE,
6066 FALSE, FALSE)
6067 : NULL);
6068 if (h != NULL
f5385ebf
AM
6069 && (h->ref_regular
6070 || h->def_regular))
5a580b3a
AM
6071 {
6072 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6073 return FALSE;
6074 }
6075
046183de
AM
6076 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6077 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6078 {
6079 /* DT_PREINIT_ARRAY is not allowed in shared library. */
0e1862bb 6080 if (! bfd_link_executable (info))
5a580b3a
AM
6081 {
6082 bfd *sub;
6083 asection *o;
6084
6085 for (sub = info->input_bfds; sub != NULL;
c72f2fb2 6086 sub = sub->link.next)
3fcd97f1
JJ
6087 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6088 for (o = sub->sections; o != NULL; o = o->next)
6089 if (elf_section_data (o)->this_hdr.sh_type
6090 == SHT_PREINIT_ARRAY)
6091 {
6092 (*_bfd_error_handler)
6093 (_("%B: .preinit_array section is not allowed in DSO"),
6094 sub);
6095 break;
6096 }
5a580b3a
AM
6097
6098 bfd_set_error (bfd_error_nonrepresentable_section);
6099 return FALSE;
6100 }
6101
6102 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6103 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6104 return FALSE;
6105 }
046183de
AM
6106 s = bfd_get_section_by_name (output_bfd, ".init_array");
6107 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6108 {
6109 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6110 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6111 return FALSE;
6112 }
046183de
AM
6113 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6114 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6115 {
6116 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6117 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6118 return FALSE;
6119 }
6120
3d4d4302 6121 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6122 /* If .dynstr is excluded from the link, we don't want any of
6123 these tags. Strictly, we should be checking each section
6124 individually; This quick check covers for the case where
6125 someone does a /DISCARD/ : { *(*) }. */
6126 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6127 {
6128 bfd_size_type strsize;
6129
6130 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
6131 if ((info->emit_hash
6132 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6133 || (info->emit_gnu_hash
6134 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
6135 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6136 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6137 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6138 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6139 bed->s->sizeof_sym))
6140 return FALSE;
6141 }
6142 }
6143
de231f20
CM
6144 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6145 return FALSE;
6146
5a580b3a
AM
6147 /* The backend must work out the sizes of all the other dynamic
6148 sections. */
9a2a56cc
AM
6149 if (dynobj != NULL
6150 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
6151 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6152 return FALSE;
6153
9a2a56cc 6154 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6155 {
554220db 6156 unsigned long section_sym_count;
fd91d419 6157 struct bfd_elf_version_tree *verdefs;
5a580b3a 6158 asection *s;
5a580b3a
AM
6159
6160 /* Set up the version definition section. */
3d4d4302 6161 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
6162 BFD_ASSERT (s != NULL);
6163
6164 /* We may have created additional version definitions if we are
6165 just linking a regular application. */
fd91d419 6166 verdefs = info->version_info;
5a580b3a
AM
6167
6168 /* Skip anonymous version tag. */
6169 if (verdefs != NULL && verdefs->vernum == 0)
6170 verdefs = verdefs->next;
6171
3e3b46e5 6172 if (verdefs == NULL && !info->create_default_symver)
8423293d 6173 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6174 else
6175 {
6176 unsigned int cdefs;
6177 bfd_size_type size;
6178 struct bfd_elf_version_tree *t;
6179 bfd_byte *p;
6180 Elf_Internal_Verdef def;
6181 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
6182 struct bfd_link_hash_entry *bh;
6183 struct elf_link_hash_entry *h;
6184 const char *name;
5a580b3a
AM
6185
6186 cdefs = 0;
6187 size = 0;
6188
6189 /* Make space for the base version. */
6190 size += sizeof (Elf_External_Verdef);
6191 size += sizeof (Elf_External_Verdaux);
6192 ++cdefs;
6193
3e3b46e5
PB
6194 /* Make space for the default version. */
6195 if (info->create_default_symver)
6196 {
6197 size += sizeof (Elf_External_Verdef);
6198 ++cdefs;
6199 }
6200
5a580b3a
AM
6201 for (t = verdefs; t != NULL; t = t->next)
6202 {
6203 struct bfd_elf_version_deps *n;
6204
a6cc6b3b
RO
6205 /* Don't emit base version twice. */
6206 if (t->vernum == 0)
6207 continue;
6208
5a580b3a
AM
6209 size += sizeof (Elf_External_Verdef);
6210 size += sizeof (Elf_External_Verdaux);
6211 ++cdefs;
6212
6213 for (n = t->deps; n != NULL; n = n->next)
6214 size += sizeof (Elf_External_Verdaux);
6215 }
6216
eea6121a 6217 s->size = size;
a50b1753 6218 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6219 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6220 return FALSE;
6221
6222 /* Fill in the version definition section. */
6223
6224 p = s->contents;
6225
6226 def.vd_version = VER_DEF_CURRENT;
6227 def.vd_flags = VER_FLG_BASE;
6228 def.vd_ndx = 1;
6229 def.vd_cnt = 1;
3e3b46e5
PB
6230 if (info->create_default_symver)
6231 {
6232 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6233 def.vd_next = sizeof (Elf_External_Verdef);
6234 }
6235 else
6236 {
6237 def.vd_aux = sizeof (Elf_External_Verdef);
6238 def.vd_next = (sizeof (Elf_External_Verdef)
6239 + sizeof (Elf_External_Verdaux));
6240 }
5a580b3a
AM
6241
6242 if (soname_indx != (bfd_size_type) -1)
6243 {
6244 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6245 soname_indx);
6246 def.vd_hash = bfd_elf_hash (soname);
6247 defaux.vda_name = soname_indx;
3e3b46e5 6248 name = soname;
5a580b3a
AM
6249 }
6250 else
6251 {
5a580b3a
AM
6252 bfd_size_type indx;
6253
06084812 6254 name = lbasename (output_bfd->filename);
5a580b3a
AM
6255 def.vd_hash = bfd_elf_hash (name);
6256 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6257 name, FALSE);
6258 if (indx == (bfd_size_type) -1)
6259 return FALSE;
6260 defaux.vda_name = indx;
6261 }
6262 defaux.vda_next = 0;
6263
6264 _bfd_elf_swap_verdef_out (output_bfd, &def,
6265 (Elf_External_Verdef *) p);
6266 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6267 if (info->create_default_symver)
6268 {
6269 /* Add a symbol representing this version. */
6270 bh = NULL;
6271 if (! (_bfd_generic_link_add_one_symbol
6272 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6273 0, NULL, FALSE,
6274 get_elf_backend_data (dynobj)->collect, &bh)))
6275 return FALSE;
6276 h = (struct elf_link_hash_entry *) bh;
6277 h->non_elf = 0;
6278 h->def_regular = 1;
6279 h->type = STT_OBJECT;
6280 h->verinfo.vertree = NULL;
6281
6282 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6283 return FALSE;
6284
6285 /* Create a duplicate of the base version with the same
6286 aux block, but different flags. */
6287 def.vd_flags = 0;
6288 def.vd_ndx = 2;
6289 def.vd_aux = sizeof (Elf_External_Verdef);
6290 if (verdefs)
6291 def.vd_next = (sizeof (Elf_External_Verdef)
6292 + sizeof (Elf_External_Verdaux));
6293 else
6294 def.vd_next = 0;
6295 _bfd_elf_swap_verdef_out (output_bfd, &def,
6296 (Elf_External_Verdef *) p);
6297 p += sizeof (Elf_External_Verdef);
6298 }
5a580b3a
AM
6299 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6300 (Elf_External_Verdaux *) p);
6301 p += sizeof (Elf_External_Verdaux);
6302
6303 for (t = verdefs; t != NULL; t = t->next)
6304 {
6305 unsigned int cdeps;
6306 struct bfd_elf_version_deps *n;
5a580b3a 6307
a6cc6b3b
RO
6308 /* Don't emit the base version twice. */
6309 if (t->vernum == 0)
6310 continue;
6311
5a580b3a
AM
6312 cdeps = 0;
6313 for (n = t->deps; n != NULL; n = n->next)
6314 ++cdeps;
6315
6316 /* Add a symbol representing this version. */
6317 bh = NULL;
6318 if (! (_bfd_generic_link_add_one_symbol
6319 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6320 0, NULL, FALSE,
6321 get_elf_backend_data (dynobj)->collect, &bh)))
6322 return FALSE;
6323 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6324 h->non_elf = 0;
6325 h->def_regular = 1;
5a580b3a
AM
6326 h->type = STT_OBJECT;
6327 h->verinfo.vertree = t;
6328
c152c796 6329 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6330 return FALSE;
6331
6332 def.vd_version = VER_DEF_CURRENT;
6333 def.vd_flags = 0;
6334 if (t->globals.list == NULL
6335 && t->locals.list == NULL
6336 && ! t->used)
6337 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6338 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6339 def.vd_cnt = cdeps + 1;
6340 def.vd_hash = bfd_elf_hash (t->name);
6341 def.vd_aux = sizeof (Elf_External_Verdef);
6342 def.vd_next = 0;
a6cc6b3b
RO
6343
6344 /* If a basever node is next, it *must* be the last node in
6345 the chain, otherwise Verdef construction breaks. */
6346 if (t->next != NULL && t->next->vernum == 0)
6347 BFD_ASSERT (t->next->next == NULL);
6348
6349 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6350 def.vd_next = (sizeof (Elf_External_Verdef)
6351 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6352
6353 _bfd_elf_swap_verdef_out (output_bfd, &def,
6354 (Elf_External_Verdef *) p);
6355 p += sizeof (Elf_External_Verdef);
6356
6357 defaux.vda_name = h->dynstr_index;
6358 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6359 h->dynstr_index);
6360 defaux.vda_next = 0;
6361 if (t->deps != NULL)
6362 defaux.vda_next = sizeof (Elf_External_Verdaux);
6363 t->name_indx = defaux.vda_name;
6364
6365 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6366 (Elf_External_Verdaux *) p);
6367 p += sizeof (Elf_External_Verdaux);
6368
6369 for (n = t->deps; n != NULL; n = n->next)
6370 {
6371 if (n->version_needed == NULL)
6372 {
6373 /* This can happen if there was an error in the
6374 version script. */
6375 defaux.vda_name = 0;
6376 }
6377 else
6378 {
6379 defaux.vda_name = n->version_needed->name_indx;
6380 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6381 defaux.vda_name);
6382 }
6383 if (n->next == NULL)
6384 defaux.vda_next = 0;
6385 else
6386 defaux.vda_next = sizeof (Elf_External_Verdaux);
6387
6388 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6389 (Elf_External_Verdaux *) p);
6390 p += sizeof (Elf_External_Verdaux);
6391 }
6392 }
6393
6394 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6395 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6396 return FALSE;
6397
6398 elf_tdata (output_bfd)->cverdefs = cdefs;
6399 }
6400
6401 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6402 {
6403 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6404 return FALSE;
6405 }
6406 else if (info->flags & DF_BIND_NOW)
6407 {
6408 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6409 return FALSE;
6410 }
6411
6412 if (info->flags_1)
6413 {
0e1862bb 6414 if (bfd_link_executable (info))
5a580b3a
AM
6415 info->flags_1 &= ~ (DF_1_INITFIRST
6416 | DF_1_NODELETE
6417 | DF_1_NOOPEN);
6418 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6419 return FALSE;
6420 }
6421
6422 /* Work out the size of the version reference section. */
6423
3d4d4302 6424 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6425 BFD_ASSERT (s != NULL);
6426 {
6427 struct elf_find_verdep_info sinfo;
6428
5a580b3a
AM
6429 sinfo.info = info;
6430 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6431 if (sinfo.vers == 0)
6432 sinfo.vers = 1;
6433 sinfo.failed = FALSE;
6434
6435 elf_link_hash_traverse (elf_hash_table (info),
6436 _bfd_elf_link_find_version_dependencies,
6437 &sinfo);
14b1c01e
AM
6438 if (sinfo.failed)
6439 return FALSE;
5a580b3a
AM
6440
6441 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6442 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6443 else
6444 {
6445 Elf_Internal_Verneed *t;
6446 unsigned int size;
6447 unsigned int crefs;
6448 bfd_byte *p;
6449
a6cc6b3b 6450 /* Build the version dependency section. */
5a580b3a
AM
6451 size = 0;
6452 crefs = 0;
6453 for (t = elf_tdata (output_bfd)->verref;
6454 t != NULL;
6455 t = t->vn_nextref)
6456 {
6457 Elf_Internal_Vernaux *a;
6458
6459 size += sizeof (Elf_External_Verneed);
6460 ++crefs;
6461 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6462 size += sizeof (Elf_External_Vernaux);
6463 }
6464
eea6121a 6465 s->size = size;
a50b1753 6466 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6467 if (s->contents == NULL)
6468 return FALSE;
6469
6470 p = s->contents;
6471 for (t = elf_tdata (output_bfd)->verref;
6472 t != NULL;
6473 t = t->vn_nextref)
6474 {
6475 unsigned int caux;
6476 Elf_Internal_Vernaux *a;
6477 bfd_size_type indx;
6478
6479 caux = 0;
6480 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6481 ++caux;
6482
6483 t->vn_version = VER_NEED_CURRENT;
6484 t->vn_cnt = caux;
6485 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6486 elf_dt_name (t->vn_bfd) != NULL
6487 ? elf_dt_name (t->vn_bfd)
06084812 6488 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6489 FALSE);
6490 if (indx == (bfd_size_type) -1)
6491 return FALSE;
6492 t->vn_file = indx;
6493 t->vn_aux = sizeof (Elf_External_Verneed);
6494 if (t->vn_nextref == NULL)
6495 t->vn_next = 0;
6496 else
6497 t->vn_next = (sizeof (Elf_External_Verneed)
6498 + caux * sizeof (Elf_External_Vernaux));
6499
6500 _bfd_elf_swap_verneed_out (output_bfd, t,
6501 (Elf_External_Verneed *) p);
6502 p += sizeof (Elf_External_Verneed);
6503
6504 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6505 {
6506 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6507 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6508 a->vna_nodename, FALSE);
6509 if (indx == (bfd_size_type) -1)
6510 return FALSE;
6511 a->vna_name = indx;
6512 if (a->vna_nextptr == NULL)
6513 a->vna_next = 0;
6514 else
6515 a->vna_next = sizeof (Elf_External_Vernaux);
6516
6517 _bfd_elf_swap_vernaux_out (output_bfd, a,
6518 (Elf_External_Vernaux *) p);
6519 p += sizeof (Elf_External_Vernaux);
6520 }
6521 }
6522
6523 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6524 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6525 return FALSE;
6526
6527 elf_tdata (output_bfd)->cverrefs = crefs;
6528 }
6529 }
6530
8423293d
AM
6531 if ((elf_tdata (output_bfd)->cverrefs == 0
6532 && elf_tdata (output_bfd)->cverdefs == 0)
6533 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6534 &section_sym_count) == 0)
6535 {
3d4d4302 6536 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6537 s->flags |= SEC_EXCLUDE;
6538 }
6539 }
6540 return TRUE;
6541}
6542
74541ad4
AM
6543/* Find the first non-excluded output section. We'll use its
6544 section symbol for some emitted relocs. */
6545void
6546_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6547{
6548 asection *s;
6549
6550 for (s = output_bfd->sections; s != NULL; s = s->next)
6551 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6552 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6553 {
6554 elf_hash_table (info)->text_index_section = s;
6555 break;
6556 }
6557}
6558
6559/* Find two non-excluded output sections, one for code, one for data.
6560 We'll use their section symbols for some emitted relocs. */
6561void
6562_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6563{
6564 asection *s;
6565
266b05cf
DJ
6566 /* Data first, since setting text_index_section changes
6567 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6568 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6569 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6570 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6571 {
266b05cf 6572 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6573 break;
6574 }
6575
6576 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6577 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6578 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6579 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6580 {
266b05cf 6581 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6582 break;
6583 }
6584
6585 if (elf_hash_table (info)->text_index_section == NULL)
6586 elf_hash_table (info)->text_index_section
6587 = elf_hash_table (info)->data_index_section;
6588}
6589
8423293d
AM
6590bfd_boolean
6591bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6592{
74541ad4
AM
6593 const struct elf_backend_data *bed;
6594
8423293d
AM
6595 if (!is_elf_hash_table (info->hash))
6596 return TRUE;
6597
74541ad4
AM
6598 bed = get_elf_backend_data (output_bfd);
6599 (*bed->elf_backend_init_index_section) (output_bfd, info);
6600
8423293d
AM
6601 if (elf_hash_table (info)->dynamic_sections_created)
6602 {
6603 bfd *dynobj;
8423293d
AM
6604 asection *s;
6605 bfd_size_type dynsymcount;
6606 unsigned long section_sym_count;
8423293d
AM
6607 unsigned int dtagcount;
6608
6609 dynobj = elf_hash_table (info)->dynobj;
6610
5a580b3a
AM
6611 /* Assign dynsym indicies. In a shared library we generate a
6612 section symbol for each output section, which come first.
6613 Next come all of the back-end allocated local dynamic syms,
6614 followed by the rest of the global symbols. */
6615
554220db
AM
6616 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6617 &section_sym_count);
5a580b3a
AM
6618
6619 /* Work out the size of the symbol version section. */
3d4d4302 6620 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6621 BFD_ASSERT (s != NULL);
d5486c43 6622 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6623 {
eea6121a 6624 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6625 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6626 if (s->contents == NULL)
6627 return FALSE;
6628
6629 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6630 return FALSE;
6631 }
6632
6633 /* Set the size of the .dynsym and .hash sections. We counted
6634 the number of dynamic symbols in elf_link_add_object_symbols.
6635 We will build the contents of .dynsym and .hash when we build
6636 the final symbol table, because until then we do not know the
6637 correct value to give the symbols. We built the .dynstr
6638 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6639 s = elf_hash_table (info)->dynsym;
5a580b3a 6640 BFD_ASSERT (s != NULL);
eea6121a 6641 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6642
d5486c43
L
6643 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6644 if (s->contents == NULL)
6645 return FALSE;
5a580b3a 6646
d5486c43
L
6647 /* The first entry in .dynsym is a dummy symbol. Clear all the
6648 section syms, in case we don't output them all. */
6649 ++section_sym_count;
6650 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6651
fdc90cb4
JJ
6652 elf_hash_table (info)->bucketcount = 0;
6653
5a580b3a
AM
6654 /* Compute the size of the hashing table. As a side effect this
6655 computes the hash values for all the names we export. */
fdc90cb4
JJ
6656 if (info->emit_hash)
6657 {
6658 unsigned long int *hashcodes;
14b1c01e 6659 struct hash_codes_info hashinf;
fdc90cb4
JJ
6660 bfd_size_type amt;
6661 unsigned long int nsyms;
6662 size_t bucketcount;
6663 size_t hash_entry_size;
6664
6665 /* Compute the hash values for all exported symbols. At the same
6666 time store the values in an array so that we could use them for
6667 optimizations. */
6668 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6669 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6670 if (hashcodes == NULL)
6671 return FALSE;
14b1c01e
AM
6672 hashinf.hashcodes = hashcodes;
6673 hashinf.error = FALSE;
5a580b3a 6674
fdc90cb4
JJ
6675 /* Put all hash values in HASHCODES. */
6676 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6677 elf_collect_hash_codes, &hashinf);
6678 if (hashinf.error)
4dd07732
AM
6679 {
6680 free (hashcodes);
6681 return FALSE;
6682 }
5a580b3a 6683
14b1c01e 6684 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6685 bucketcount
6686 = compute_bucket_count (info, hashcodes, nsyms, 0);
6687 free (hashcodes);
6688
6689 if (bucketcount == 0)
6690 return FALSE;
5a580b3a 6691
fdc90cb4
JJ
6692 elf_hash_table (info)->bucketcount = bucketcount;
6693
3d4d4302 6694 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6695 BFD_ASSERT (s != NULL);
6696 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6697 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6698 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6699 if (s->contents == NULL)
6700 return FALSE;
6701
6702 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6703 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6704 s->contents + hash_entry_size);
6705 }
6706
6707 if (info->emit_gnu_hash)
6708 {
6709 size_t i, cnt;
6710 unsigned char *contents;
6711 struct collect_gnu_hash_codes cinfo;
6712 bfd_size_type amt;
6713 size_t bucketcount;
6714
6715 memset (&cinfo, 0, sizeof (cinfo));
6716
6717 /* Compute the hash values for all exported symbols. At the same
6718 time store the values in an array so that we could use them for
6719 optimizations. */
6720 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6721 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6722 if (cinfo.hashcodes == NULL)
6723 return FALSE;
6724
6725 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6726 cinfo.min_dynindx = -1;
6727 cinfo.output_bfd = output_bfd;
6728 cinfo.bed = bed;
6729
6730 /* Put all hash values in HASHCODES. */
6731 elf_link_hash_traverse (elf_hash_table (info),
6732 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6733 if (cinfo.error)
4dd07732
AM
6734 {
6735 free (cinfo.hashcodes);
6736 return FALSE;
6737 }
fdc90cb4
JJ
6738
6739 bucketcount
6740 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6741
6742 if (bucketcount == 0)
6743 {
6744 free (cinfo.hashcodes);
6745 return FALSE;
6746 }
6747
3d4d4302 6748 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6749 BFD_ASSERT (s != NULL);
6750
6751 if (cinfo.nsyms == 0)
6752 {
6753 /* Empty .gnu.hash section is special. */
6754 BFD_ASSERT (cinfo.min_dynindx == -1);
6755 free (cinfo.hashcodes);
6756 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6757 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6758 if (contents == NULL)
6759 return FALSE;
6760 s->contents = contents;
6761 /* 1 empty bucket. */
6762 bfd_put_32 (output_bfd, 1, contents);
6763 /* SYMIDX above the special symbol 0. */
6764 bfd_put_32 (output_bfd, 1, contents + 4);
6765 /* Just one word for bitmask. */
6766 bfd_put_32 (output_bfd, 1, contents + 8);
6767 /* Only hash fn bloom filter. */
6768 bfd_put_32 (output_bfd, 0, contents + 12);
6769 /* No hashes are valid - empty bitmask. */
6770 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6771 /* No hashes in the only bucket. */
6772 bfd_put_32 (output_bfd, 0,
6773 contents + 16 + bed->s->arch_size / 8);
6774 }
6775 else
6776 {
9e6619e2 6777 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6778 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6779
9e6619e2
AM
6780 x = cinfo.nsyms;
6781 maskbitslog2 = 1;
6782 while ((x >>= 1) != 0)
6783 ++maskbitslog2;
fdc90cb4
JJ
6784 if (maskbitslog2 < 3)
6785 maskbitslog2 = 5;
6786 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6787 maskbitslog2 = maskbitslog2 + 3;
6788 else
6789 maskbitslog2 = maskbitslog2 + 2;
6790 if (bed->s->arch_size == 64)
6791 {
6792 if (maskbitslog2 == 5)
6793 maskbitslog2 = 6;
6794 cinfo.shift1 = 6;
6795 }
6796 else
6797 cinfo.shift1 = 5;
6798 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6799 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6800 cinfo.maskbits = 1 << maskbitslog2;
6801 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6802 amt = bucketcount * sizeof (unsigned long int) * 2;
6803 amt += maskwords * sizeof (bfd_vma);
a50b1753 6804 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6805 if (cinfo.bitmask == NULL)
6806 {
6807 free (cinfo.hashcodes);
6808 return FALSE;
6809 }
6810
a50b1753 6811 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6812 cinfo.indx = cinfo.counts + bucketcount;
6813 cinfo.symindx = dynsymcount - cinfo.nsyms;
6814 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6815
6816 /* Determine how often each hash bucket is used. */
6817 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6818 for (i = 0; i < cinfo.nsyms; ++i)
6819 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6820
6821 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6822 if (cinfo.counts[i] != 0)
6823 {
6824 cinfo.indx[i] = cnt;
6825 cnt += cinfo.counts[i];
6826 }
6827 BFD_ASSERT (cnt == dynsymcount);
6828 cinfo.bucketcount = bucketcount;
6829 cinfo.local_indx = cinfo.min_dynindx;
6830
6831 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6832 s->size += cinfo.maskbits / 8;
a50b1753 6833 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6834 if (contents == NULL)
6835 {
6836 free (cinfo.bitmask);
6837 free (cinfo.hashcodes);
6838 return FALSE;
6839 }
6840
6841 s->contents = contents;
6842 bfd_put_32 (output_bfd, bucketcount, contents);
6843 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6844 bfd_put_32 (output_bfd, maskwords, contents + 8);
6845 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6846 contents += 16 + cinfo.maskbits / 8;
6847
6848 for (i = 0; i < bucketcount; ++i)
6849 {
6850 if (cinfo.counts[i] == 0)
6851 bfd_put_32 (output_bfd, 0, contents);
6852 else
6853 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6854 contents += 4;
6855 }
6856
6857 cinfo.contents = contents;
6858
6859 /* Renumber dynamic symbols, populate .gnu.hash section. */
6860 elf_link_hash_traverse (elf_hash_table (info),
6861 elf_renumber_gnu_hash_syms, &cinfo);
6862
6863 contents = s->contents + 16;
6864 for (i = 0; i < maskwords; ++i)
6865 {
6866 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6867 contents);
6868 contents += bed->s->arch_size / 8;
6869 }
6870
6871 free (cinfo.bitmask);
6872 free (cinfo.hashcodes);
6873 }
6874 }
5a580b3a 6875
3d4d4302 6876 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6877 BFD_ASSERT (s != NULL);
6878
4ad4eba5 6879 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6880
eea6121a 6881 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6882
6883 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6884 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6885 return FALSE;
6886 }
6887
6888 return TRUE;
6889}
4d269e42 6890\f
4d269e42
AM
6891/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6892
6893static void
6894merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6895 asection *sec)
6896{
dbaa2011
AM
6897 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6898 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
6899}
6900
6901/* Finish SHF_MERGE section merging. */
6902
6903bfd_boolean
630993ec 6904_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
6905{
6906 bfd *ibfd;
6907 asection *sec;
6908
6909 if (!is_elf_hash_table (info->hash))
6910 return FALSE;
6911
c72f2fb2 6912 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
6913 if ((ibfd->flags & DYNAMIC) == 0
6914 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
6915 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6916 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
6917 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6918 if ((sec->flags & SEC_MERGE) != 0
6919 && !bfd_is_abs_section (sec->output_section))
6920 {
6921 struct bfd_elf_section_data *secdata;
6922
6923 secdata = elf_section_data (sec);
630993ec 6924 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
6925 &elf_hash_table (info)->merge_info,
6926 sec, &secdata->sec_info))
6927 return FALSE;
6928 else if (secdata->sec_info)
dbaa2011 6929 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
6930 }
6931
6932 if (elf_hash_table (info)->merge_info != NULL)
630993ec 6933 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
6934 merge_sections_remove_hook);
6935 return TRUE;
6936}
6937
6938/* Create an entry in an ELF linker hash table. */
6939
6940struct bfd_hash_entry *
6941_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6942 struct bfd_hash_table *table,
6943 const char *string)
6944{
6945 /* Allocate the structure if it has not already been allocated by a
6946 subclass. */
6947 if (entry == NULL)
6948 {
a50b1753 6949 entry = (struct bfd_hash_entry *)
ca4be51c 6950 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6951 if (entry == NULL)
6952 return entry;
6953 }
6954
6955 /* Call the allocation method of the superclass. */
6956 entry = _bfd_link_hash_newfunc (entry, table, string);
6957 if (entry != NULL)
6958 {
6959 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6960 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6961
6962 /* Set local fields. */
6963 ret->indx = -1;
6964 ret->dynindx = -1;
6965 ret->got = htab->init_got_refcount;
6966 ret->plt = htab->init_plt_refcount;
6967 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6968 - offsetof (struct elf_link_hash_entry, size)));
6969 /* Assume that we have been called by a non-ELF symbol reader.
6970 This flag is then reset by the code which reads an ELF input
6971 file. This ensures that a symbol created by a non-ELF symbol
6972 reader will have the flag set correctly. */
6973 ret->non_elf = 1;
6974 }
6975
6976 return entry;
6977}
6978
6979/* Copy data from an indirect symbol to its direct symbol, hiding the
6980 old indirect symbol. Also used for copying flags to a weakdef. */
6981
6982void
6983_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6984 struct elf_link_hash_entry *dir,
6985 struct elf_link_hash_entry *ind)
6986{
6987 struct elf_link_hash_table *htab;
6988
6989 /* Copy down any references that we may have already seen to the
6e33951e
L
6990 symbol which just became indirect if DIR isn't a hidden versioned
6991 symbol. */
4d269e42 6992
422f1182 6993 if (dir->versioned != versioned_hidden)
6e33951e
L
6994 {
6995 dir->ref_dynamic |= ind->ref_dynamic;
6996 dir->ref_regular |= ind->ref_regular;
6997 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6998 dir->non_got_ref |= ind->non_got_ref;
6999 dir->needs_plt |= ind->needs_plt;
7000 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7001 }
4d269e42
AM
7002
7003 if (ind->root.type != bfd_link_hash_indirect)
7004 return;
7005
7006 /* Copy over the global and procedure linkage table refcount entries.
7007 These may have been already set up by a check_relocs routine. */
7008 htab = elf_hash_table (info);
7009 if (ind->got.refcount > htab->init_got_refcount.refcount)
7010 {
7011 if (dir->got.refcount < 0)
7012 dir->got.refcount = 0;
7013 dir->got.refcount += ind->got.refcount;
7014 ind->got.refcount = htab->init_got_refcount.refcount;
7015 }
7016
7017 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7018 {
7019 if (dir->plt.refcount < 0)
7020 dir->plt.refcount = 0;
7021 dir->plt.refcount += ind->plt.refcount;
7022 ind->plt.refcount = htab->init_plt_refcount.refcount;
7023 }
7024
7025 if (ind->dynindx != -1)
7026 {
7027 if (dir->dynindx != -1)
7028 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7029 dir->dynindx = ind->dynindx;
7030 dir->dynstr_index = ind->dynstr_index;
7031 ind->dynindx = -1;
7032 ind->dynstr_index = 0;
7033 }
7034}
7035
7036void
7037_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7038 struct elf_link_hash_entry *h,
7039 bfd_boolean force_local)
7040{
3aa14d16
L
7041 /* STT_GNU_IFUNC symbol must go through PLT. */
7042 if (h->type != STT_GNU_IFUNC)
7043 {
7044 h->plt = elf_hash_table (info)->init_plt_offset;
7045 h->needs_plt = 0;
7046 }
4d269e42
AM
7047 if (force_local)
7048 {
7049 h->forced_local = 1;
7050 if (h->dynindx != -1)
7051 {
7052 h->dynindx = -1;
7053 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7054 h->dynstr_index);
7055 }
7056 }
7057}
7058
7bf52ea2
AM
7059/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7060 caller. */
4d269e42
AM
7061
7062bfd_boolean
7063_bfd_elf_link_hash_table_init
7064 (struct elf_link_hash_table *table,
7065 bfd *abfd,
7066 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7067 struct bfd_hash_table *,
7068 const char *),
4dfe6ac6
NC
7069 unsigned int entsize,
7070 enum elf_target_id target_id)
4d269e42
AM
7071{
7072 bfd_boolean ret;
7073 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7074
4d269e42
AM
7075 table->init_got_refcount.refcount = can_refcount - 1;
7076 table->init_plt_refcount.refcount = can_refcount - 1;
7077 table->init_got_offset.offset = -(bfd_vma) 1;
7078 table->init_plt_offset.offset = -(bfd_vma) 1;
7079 /* The first dynamic symbol is a dummy. */
7080 table->dynsymcount = 1;
7081
7082 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7083
4d269e42 7084 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7085 table->hash_table_id = target_id;
4d269e42
AM
7086
7087 return ret;
7088}
7089
7090/* Create an ELF linker hash table. */
7091
7092struct bfd_link_hash_table *
7093_bfd_elf_link_hash_table_create (bfd *abfd)
7094{
7095 struct elf_link_hash_table *ret;
7096 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7097
7bf52ea2 7098 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7099 if (ret == NULL)
7100 return NULL;
7101
7102 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7103 sizeof (struct elf_link_hash_entry),
7104 GENERIC_ELF_DATA))
4d269e42
AM
7105 {
7106 free (ret);
7107 return NULL;
7108 }
d495ab0d 7109 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7110
7111 return &ret->root;
7112}
7113
9f7c3e5e
AM
7114/* Destroy an ELF linker hash table. */
7115
7116void
d495ab0d 7117_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7118{
d495ab0d
AM
7119 struct elf_link_hash_table *htab;
7120
7121 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7122 if (htab->dynstr != NULL)
7123 _bfd_elf_strtab_free (htab->dynstr);
7124 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7125 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7126}
7127
4d269e42
AM
7128/* This is a hook for the ELF emulation code in the generic linker to
7129 tell the backend linker what file name to use for the DT_NEEDED
7130 entry for a dynamic object. */
7131
7132void
7133bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7134{
7135 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7136 && bfd_get_format (abfd) == bfd_object)
7137 elf_dt_name (abfd) = name;
7138}
7139
7140int
7141bfd_elf_get_dyn_lib_class (bfd *abfd)
7142{
7143 int lib_class;
7144 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7145 && bfd_get_format (abfd) == bfd_object)
7146 lib_class = elf_dyn_lib_class (abfd);
7147 else
7148 lib_class = 0;
7149 return lib_class;
7150}
7151
7152void
7153bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7154{
7155 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7156 && bfd_get_format (abfd) == bfd_object)
7157 elf_dyn_lib_class (abfd) = lib_class;
7158}
7159
7160/* Get the list of DT_NEEDED entries for a link. This is a hook for
7161 the linker ELF emulation code. */
7162
7163struct bfd_link_needed_list *
7164bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7165 struct bfd_link_info *info)
7166{
7167 if (! is_elf_hash_table (info->hash))
7168 return NULL;
7169 return elf_hash_table (info)->needed;
7170}
7171
7172/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7173 hook for the linker ELF emulation code. */
7174
7175struct bfd_link_needed_list *
7176bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7177 struct bfd_link_info *info)
7178{
7179 if (! is_elf_hash_table (info->hash))
7180 return NULL;
7181 return elf_hash_table (info)->runpath;
7182}
7183
7184/* Get the name actually used for a dynamic object for a link. This
7185 is the SONAME entry if there is one. Otherwise, it is the string
7186 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7187
7188const char *
7189bfd_elf_get_dt_soname (bfd *abfd)
7190{
7191 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7192 && bfd_get_format (abfd) == bfd_object)
7193 return elf_dt_name (abfd);
7194 return NULL;
7195}
7196
7197/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7198 the ELF linker emulation code. */
7199
7200bfd_boolean
7201bfd_elf_get_bfd_needed_list (bfd *abfd,
7202 struct bfd_link_needed_list **pneeded)
7203{
7204 asection *s;
7205 bfd_byte *dynbuf = NULL;
cb33740c 7206 unsigned int elfsec;
4d269e42
AM
7207 unsigned long shlink;
7208 bfd_byte *extdyn, *extdynend;
7209 size_t extdynsize;
7210 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7211
7212 *pneeded = NULL;
7213
7214 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7215 || bfd_get_format (abfd) != bfd_object)
7216 return TRUE;
7217
7218 s = bfd_get_section_by_name (abfd, ".dynamic");
7219 if (s == NULL || s->size == 0)
7220 return TRUE;
7221
7222 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7223 goto error_return;
7224
7225 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7226 if (elfsec == SHN_BAD)
4d269e42
AM
7227 goto error_return;
7228
7229 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7230
4d269e42
AM
7231 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7232 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7233
7234 extdyn = dynbuf;
7235 extdynend = extdyn + s->size;
7236 for (; extdyn < extdynend; extdyn += extdynsize)
7237 {
7238 Elf_Internal_Dyn dyn;
7239
7240 (*swap_dyn_in) (abfd, extdyn, &dyn);
7241
7242 if (dyn.d_tag == DT_NULL)
7243 break;
7244
7245 if (dyn.d_tag == DT_NEEDED)
7246 {
7247 const char *string;
7248 struct bfd_link_needed_list *l;
7249 unsigned int tagv = dyn.d_un.d_val;
7250 bfd_size_type amt;
7251
7252 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7253 if (string == NULL)
7254 goto error_return;
7255
7256 amt = sizeof *l;
a50b1753 7257 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7258 if (l == NULL)
7259 goto error_return;
7260
7261 l->by = abfd;
7262 l->name = string;
7263 l->next = *pneeded;
7264 *pneeded = l;
7265 }
7266 }
7267
7268 free (dynbuf);
7269
7270 return TRUE;
7271
7272 error_return:
7273 if (dynbuf != NULL)
7274 free (dynbuf);
7275 return FALSE;
7276}
7277
7278struct elf_symbuf_symbol
7279{
7280 unsigned long st_name; /* Symbol name, index in string tbl */
7281 unsigned char st_info; /* Type and binding attributes */
7282 unsigned char st_other; /* Visibilty, and target specific */
7283};
7284
7285struct elf_symbuf_head
7286{
7287 struct elf_symbuf_symbol *ssym;
7288 bfd_size_type count;
7289 unsigned int st_shndx;
7290};
7291
7292struct elf_symbol
7293{
7294 union
7295 {
7296 Elf_Internal_Sym *isym;
7297 struct elf_symbuf_symbol *ssym;
7298 } u;
7299 const char *name;
7300};
7301
7302/* Sort references to symbols by ascending section number. */
7303
7304static int
7305elf_sort_elf_symbol (const void *arg1, const void *arg2)
7306{
7307 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7308 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7309
7310 return s1->st_shndx - s2->st_shndx;
7311}
7312
7313static int
7314elf_sym_name_compare (const void *arg1, const void *arg2)
7315{
7316 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7317 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7318 return strcmp (s1->name, s2->name);
7319}
7320
7321static struct elf_symbuf_head *
7322elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7323{
14b1c01e 7324 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7325 struct elf_symbuf_symbol *ssym;
7326 struct elf_symbuf_head *ssymbuf, *ssymhead;
3ae181ee 7327 bfd_size_type i, shndx_count, total_size;
4d269e42 7328
a50b1753 7329 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7330 if (indbuf == NULL)
7331 return NULL;
7332
7333 for (ind = indbuf, i = 0; i < symcount; i++)
7334 if (isymbuf[i].st_shndx != SHN_UNDEF)
7335 *ind++ = &isymbuf[i];
7336 indbufend = ind;
7337
7338 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7339 elf_sort_elf_symbol);
7340
7341 shndx_count = 0;
7342 if (indbufend > indbuf)
7343 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7344 if (ind[0]->st_shndx != ind[1]->st_shndx)
7345 shndx_count++;
7346
3ae181ee
L
7347 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7348 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7349 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7350 if (ssymbuf == NULL)
7351 {
7352 free (indbuf);
7353 return NULL;
7354 }
7355
3ae181ee 7356 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7357 ssymbuf->ssym = NULL;
7358 ssymbuf->count = shndx_count;
7359 ssymbuf->st_shndx = 0;
7360 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7361 {
7362 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7363 {
7364 ssymhead++;
7365 ssymhead->ssym = ssym;
7366 ssymhead->count = 0;
7367 ssymhead->st_shndx = (*ind)->st_shndx;
7368 }
7369 ssym->st_name = (*ind)->st_name;
7370 ssym->st_info = (*ind)->st_info;
7371 ssym->st_other = (*ind)->st_other;
7372 ssymhead->count++;
7373 }
3ae181ee
L
7374 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7375 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7376 == total_size));
4d269e42
AM
7377
7378 free (indbuf);
7379 return ssymbuf;
7380}
7381
7382/* Check if 2 sections define the same set of local and global
7383 symbols. */
7384
8f317e31 7385static bfd_boolean
4d269e42
AM
7386bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7387 struct bfd_link_info *info)
7388{
7389 bfd *bfd1, *bfd2;
7390 const struct elf_backend_data *bed1, *bed2;
7391 Elf_Internal_Shdr *hdr1, *hdr2;
7392 bfd_size_type symcount1, symcount2;
7393 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7394 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7395 Elf_Internal_Sym *isym, *isymend;
7396 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7397 bfd_size_type count1, count2, i;
cb33740c 7398 unsigned int shndx1, shndx2;
4d269e42
AM
7399 bfd_boolean result;
7400
7401 bfd1 = sec1->owner;
7402 bfd2 = sec2->owner;
7403
4d269e42
AM
7404 /* Both sections have to be in ELF. */
7405 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7406 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7407 return FALSE;
7408
7409 if (elf_section_type (sec1) != elf_section_type (sec2))
7410 return FALSE;
7411
4d269e42
AM
7412 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7413 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7414 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7415 return FALSE;
7416
7417 bed1 = get_elf_backend_data (bfd1);
7418 bed2 = get_elf_backend_data (bfd2);
7419 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7420 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7421 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7422 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7423
7424 if (symcount1 == 0 || symcount2 == 0)
7425 return FALSE;
7426
7427 result = FALSE;
7428 isymbuf1 = NULL;
7429 isymbuf2 = NULL;
a50b1753
NC
7430 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7431 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7432
7433 if (ssymbuf1 == NULL)
7434 {
7435 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7436 NULL, NULL, NULL);
7437 if (isymbuf1 == NULL)
7438 goto done;
7439
7440 if (!info->reduce_memory_overheads)
7441 elf_tdata (bfd1)->symbuf = ssymbuf1
7442 = elf_create_symbuf (symcount1, isymbuf1);
7443 }
7444
7445 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7446 {
7447 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7448 NULL, NULL, NULL);
7449 if (isymbuf2 == NULL)
7450 goto done;
7451
7452 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7453 elf_tdata (bfd2)->symbuf = ssymbuf2
7454 = elf_create_symbuf (symcount2, isymbuf2);
7455 }
7456
7457 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7458 {
7459 /* Optimized faster version. */
7460 bfd_size_type lo, hi, mid;
7461 struct elf_symbol *symp;
7462 struct elf_symbuf_symbol *ssym, *ssymend;
7463
7464 lo = 0;
7465 hi = ssymbuf1->count;
7466 ssymbuf1++;
7467 count1 = 0;
7468 while (lo < hi)
7469 {
7470 mid = (lo + hi) / 2;
cb33740c 7471 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7472 hi = mid;
cb33740c 7473 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7474 lo = mid + 1;
7475 else
7476 {
7477 count1 = ssymbuf1[mid].count;
7478 ssymbuf1 += mid;
7479 break;
7480 }
7481 }
7482
7483 lo = 0;
7484 hi = ssymbuf2->count;
7485 ssymbuf2++;
7486 count2 = 0;
7487 while (lo < hi)
7488 {
7489 mid = (lo + hi) / 2;
cb33740c 7490 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7491 hi = mid;
cb33740c 7492 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7493 lo = mid + 1;
7494 else
7495 {
7496 count2 = ssymbuf2[mid].count;
7497 ssymbuf2 += mid;
7498 break;
7499 }
7500 }
7501
7502 if (count1 == 0 || count2 == 0 || count1 != count2)
7503 goto done;
7504
ca4be51c
AM
7505 symtable1
7506 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7507 symtable2
7508 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7509 if (symtable1 == NULL || symtable2 == NULL)
7510 goto done;
7511
7512 symp = symtable1;
7513 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7514 ssym < ssymend; ssym++, symp++)
7515 {
7516 symp->u.ssym = ssym;
7517 symp->name = bfd_elf_string_from_elf_section (bfd1,
7518 hdr1->sh_link,
7519 ssym->st_name);
7520 }
7521
7522 symp = symtable2;
7523 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7524 ssym < ssymend; ssym++, symp++)
7525 {
7526 symp->u.ssym = ssym;
7527 symp->name = bfd_elf_string_from_elf_section (bfd2,
7528 hdr2->sh_link,
7529 ssym->st_name);
7530 }
7531
7532 /* Sort symbol by name. */
7533 qsort (symtable1, count1, sizeof (struct elf_symbol),
7534 elf_sym_name_compare);
7535 qsort (symtable2, count1, sizeof (struct elf_symbol),
7536 elf_sym_name_compare);
7537
7538 for (i = 0; i < count1; i++)
7539 /* Two symbols must have the same binding, type and name. */
7540 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7541 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7542 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7543 goto done;
7544
7545 result = TRUE;
7546 goto done;
7547 }
7548
a50b1753
NC
7549 symtable1 = (struct elf_symbol *)
7550 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7551 symtable2 = (struct elf_symbol *)
7552 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7553 if (symtable1 == NULL || symtable2 == NULL)
7554 goto done;
7555
7556 /* Count definitions in the section. */
7557 count1 = 0;
7558 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7559 if (isym->st_shndx == shndx1)
4d269e42
AM
7560 symtable1[count1++].u.isym = isym;
7561
7562 count2 = 0;
7563 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7564 if (isym->st_shndx == shndx2)
4d269e42
AM
7565 symtable2[count2++].u.isym = isym;
7566
7567 if (count1 == 0 || count2 == 0 || count1 != count2)
7568 goto done;
7569
7570 for (i = 0; i < count1; i++)
7571 symtable1[i].name
7572 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7573 symtable1[i].u.isym->st_name);
7574
7575 for (i = 0; i < count2; i++)
7576 symtable2[i].name
7577 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7578 symtable2[i].u.isym->st_name);
7579
7580 /* Sort symbol by name. */
7581 qsort (symtable1, count1, sizeof (struct elf_symbol),
7582 elf_sym_name_compare);
7583 qsort (symtable2, count1, sizeof (struct elf_symbol),
7584 elf_sym_name_compare);
7585
7586 for (i = 0; i < count1; i++)
7587 /* Two symbols must have the same binding, type and name. */
7588 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7589 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7590 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7591 goto done;
7592
7593 result = TRUE;
7594
7595done:
7596 if (symtable1)
7597 free (symtable1);
7598 if (symtable2)
7599 free (symtable2);
7600 if (isymbuf1)
7601 free (isymbuf1);
7602 if (isymbuf2)
7603 free (isymbuf2);
7604
7605 return result;
7606}
7607
7608/* Return TRUE if 2 section types are compatible. */
7609
7610bfd_boolean
7611_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7612 bfd *bbfd, const asection *bsec)
7613{
7614 if (asec == NULL
7615 || bsec == NULL
7616 || abfd->xvec->flavour != bfd_target_elf_flavour
7617 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7618 return TRUE;
7619
7620 return elf_section_type (asec) == elf_section_type (bsec);
7621}
7622\f
c152c796
AM
7623/* Final phase of ELF linker. */
7624
7625/* A structure we use to avoid passing large numbers of arguments. */
7626
7627struct elf_final_link_info
7628{
7629 /* General link information. */
7630 struct bfd_link_info *info;
7631 /* Output BFD. */
7632 bfd *output_bfd;
7633 /* Symbol string table. */
ef10c3ac 7634 struct elf_strtab_hash *symstrtab;
c152c796
AM
7635 /* .hash section. */
7636 asection *hash_sec;
7637 /* symbol version section (.gnu.version). */
7638 asection *symver_sec;
7639 /* Buffer large enough to hold contents of any section. */
7640 bfd_byte *contents;
7641 /* Buffer large enough to hold external relocs of any section. */
7642 void *external_relocs;
7643 /* Buffer large enough to hold internal relocs of any section. */
7644 Elf_Internal_Rela *internal_relocs;
7645 /* Buffer large enough to hold external local symbols of any input
7646 BFD. */
7647 bfd_byte *external_syms;
7648 /* And a buffer for symbol section indices. */
7649 Elf_External_Sym_Shndx *locsym_shndx;
7650 /* Buffer large enough to hold internal local symbols of any input
7651 BFD. */
7652 Elf_Internal_Sym *internal_syms;
7653 /* Array large enough to hold a symbol index for each local symbol
7654 of any input BFD. */
7655 long *indices;
7656 /* Array large enough to hold a section pointer for each local
7657 symbol of any input BFD. */
7658 asection **sections;
ef10c3ac 7659 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7660 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7661 /* Number of STT_FILE syms seen. */
7662 size_t filesym_count;
c152c796
AM
7663};
7664
7665/* This struct is used to pass information to elf_link_output_extsym. */
7666
7667struct elf_outext_info
7668{
7669 bfd_boolean failed;
7670 bfd_boolean localsyms;
34a79995 7671 bfd_boolean file_sym_done;
8b127cbc 7672 struct elf_final_link_info *flinfo;
c152c796
AM
7673};
7674
d9352518
DB
7675
7676/* Support for evaluating a complex relocation.
7677
7678 Complex relocations are generalized, self-describing relocations. The
7679 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7680 relocations themselves.
d9352518
DB
7681
7682 The relocations are use a reserved elf-wide relocation type code (R_RELC
7683 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7684 information (start bit, end bit, word width, etc) into the addend. This
7685 information is extracted from CGEN-generated operand tables within gas.
7686
7687 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7688 internal) representing prefix-notation expressions, including but not
7689 limited to those sorts of expressions normally encoded as addends in the
7690 addend field. The symbol mangling format is:
7691
7692 <node> := <literal>
7693 | <unary-operator> ':' <node>
7694 | <binary-operator> ':' <node> ':' <node>
7695 ;
7696
7697 <literal> := 's' <digits=N> ':' <N character symbol name>
7698 | 'S' <digits=N> ':' <N character section name>
7699 | '#' <hexdigits>
7700 ;
7701
7702 <binary-operator> := as in C
7703 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7704
7705static void
a0c8462f
AM
7706set_symbol_value (bfd *bfd_with_globals,
7707 Elf_Internal_Sym *isymbuf,
7708 size_t locsymcount,
7709 size_t symidx,
7710 bfd_vma val)
d9352518 7711{
8977835c
AM
7712 struct elf_link_hash_entry **sym_hashes;
7713 struct elf_link_hash_entry *h;
7714 size_t extsymoff = locsymcount;
d9352518 7715
8977835c 7716 if (symidx < locsymcount)
d9352518 7717 {
8977835c
AM
7718 Elf_Internal_Sym *sym;
7719
7720 sym = isymbuf + symidx;
7721 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7722 {
7723 /* It is a local symbol: move it to the
7724 "absolute" section and give it a value. */
7725 sym->st_shndx = SHN_ABS;
7726 sym->st_value = val;
7727 return;
7728 }
7729 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7730 extsymoff = 0;
d9352518 7731 }
8977835c
AM
7732
7733 /* It is a global symbol: set its link type
7734 to "defined" and give it a value. */
7735
7736 sym_hashes = elf_sym_hashes (bfd_with_globals);
7737 h = sym_hashes [symidx - extsymoff];
7738 while (h->root.type == bfd_link_hash_indirect
7739 || h->root.type == bfd_link_hash_warning)
7740 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7741 h->root.type = bfd_link_hash_defined;
7742 h->root.u.def.value = val;
7743 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7744}
7745
a0c8462f
AM
7746static bfd_boolean
7747resolve_symbol (const char *name,
7748 bfd *input_bfd,
8b127cbc 7749 struct elf_final_link_info *flinfo,
a0c8462f
AM
7750 bfd_vma *result,
7751 Elf_Internal_Sym *isymbuf,
7752 size_t locsymcount)
d9352518 7753{
a0c8462f
AM
7754 Elf_Internal_Sym *sym;
7755 struct bfd_link_hash_entry *global_entry;
7756 const char *candidate = NULL;
7757 Elf_Internal_Shdr *symtab_hdr;
7758 size_t i;
7759
d9352518
DB
7760 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7761
7762 for (i = 0; i < locsymcount; ++ i)
7763 {
8977835c 7764 sym = isymbuf + i;
d9352518
DB
7765
7766 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7767 continue;
7768
7769 candidate = bfd_elf_string_from_elf_section (input_bfd,
7770 symtab_hdr->sh_link,
7771 sym->st_name);
7772#ifdef DEBUG
0f02bbd9
AM
7773 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7774 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7775#endif
7776 if (candidate && strcmp (candidate, name) == 0)
7777 {
8b127cbc 7778 asection *sec = flinfo->sections [i];
d9352518 7779
0f02bbd9
AM
7780 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7781 *result += sec->output_offset + sec->output_section->vma;
d9352518 7782#ifdef DEBUG
0f02bbd9
AM
7783 printf ("Found symbol with value %8.8lx\n",
7784 (unsigned long) *result);
d9352518
DB
7785#endif
7786 return TRUE;
7787 }
7788 }
7789
7790 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7791 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7792 FALSE, FALSE, TRUE);
d9352518
DB
7793 if (!global_entry)
7794 return FALSE;
a0c8462f 7795
d9352518
DB
7796 if (global_entry->type == bfd_link_hash_defined
7797 || global_entry->type == bfd_link_hash_defweak)
7798 {
a0c8462f
AM
7799 *result = (global_entry->u.def.value
7800 + global_entry->u.def.section->output_section->vma
7801 + global_entry->u.def.section->output_offset);
d9352518 7802#ifdef DEBUG
0f02bbd9
AM
7803 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7804 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7805#endif
7806 return TRUE;
a0c8462f 7807 }
d9352518 7808
d9352518
DB
7809 return FALSE;
7810}
7811
37b01f6a
DG
7812/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7813 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7814 names like "foo.end" which is the end address of section "foo". */
7815
d9352518 7816static bfd_boolean
a0c8462f
AM
7817resolve_section (const char *name,
7818 asection *sections,
37b01f6a
DG
7819 bfd_vma *result,
7820 bfd * abfd)
d9352518 7821{
a0c8462f
AM
7822 asection *curr;
7823 unsigned int len;
d9352518 7824
a0c8462f 7825 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7826 if (strcmp (curr->name, name) == 0)
7827 {
7828 *result = curr->vma;
7829 return TRUE;
7830 }
7831
7832 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 7833 /* FIXME: This could be coded more efficiently... */
a0c8462f 7834 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7835 {
7836 len = strlen (curr->name);
a0c8462f 7837 if (len > strlen (name))
d9352518
DB
7838 continue;
7839
7840 if (strncmp (curr->name, name, len) == 0)
7841 {
7842 if (strncmp (".end", name + len, 4) == 0)
7843 {
37b01f6a 7844 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
7845 return TRUE;
7846 }
7847
7848 /* Insert more pseudo-section names here, if you like. */
7849 }
7850 }
a0c8462f 7851
d9352518
DB
7852 return FALSE;
7853}
7854
7855static void
a0c8462f 7856undefined_reference (const char *reftype, const char *name)
d9352518 7857{
a0c8462f
AM
7858 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7859 reftype, name);
d9352518
DB
7860}
7861
7862static bfd_boolean
a0c8462f
AM
7863eval_symbol (bfd_vma *result,
7864 const char **symp,
7865 bfd *input_bfd,
8b127cbc 7866 struct elf_final_link_info *flinfo,
a0c8462f
AM
7867 bfd_vma dot,
7868 Elf_Internal_Sym *isymbuf,
7869 size_t locsymcount,
7870 int signed_p)
d9352518 7871{
4b93929b
NC
7872 size_t len;
7873 size_t symlen;
a0c8462f
AM
7874 bfd_vma a;
7875 bfd_vma b;
4b93929b 7876 char symbuf[4096];
0f02bbd9 7877 const char *sym = *symp;
a0c8462f
AM
7878 const char *symend;
7879 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7880
7881 len = strlen (sym);
7882 symend = sym + len;
7883
4b93929b 7884 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7885 {
7886 bfd_set_error (bfd_error_invalid_operation);
7887 return FALSE;
7888 }
a0c8462f 7889
d9352518
DB
7890 switch (* sym)
7891 {
7892 case '.':
0f02bbd9
AM
7893 *result = dot;
7894 *symp = sym + 1;
d9352518
DB
7895 return TRUE;
7896
7897 case '#':
0f02bbd9
AM
7898 ++sym;
7899 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7900 return TRUE;
7901
7902 case 'S':
7903 symbol_is_section = TRUE;
a0c8462f 7904 case 's':
0f02bbd9
AM
7905 ++sym;
7906 symlen = strtol (sym, (char **) symp, 10);
7907 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7908
4b93929b 7909 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7910 {
7911 bfd_set_error (bfd_error_invalid_operation);
7912 return FALSE;
7913 }
7914
7915 memcpy (symbuf, sym, symlen);
a0c8462f 7916 symbuf[symlen] = '\0';
0f02bbd9 7917 *symp = sym + symlen;
a0c8462f
AM
7918
7919 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7920 the symbol as a section, or vice-versa. so we're pretty liberal in our
7921 interpretation here; section means "try section first", not "must be a
7922 section", and likewise with symbol. */
7923
a0c8462f 7924 if (symbol_is_section)
d9352518 7925 {
37b01f6a 7926 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 7927 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7928 isymbuf, locsymcount))
d9352518
DB
7929 {
7930 undefined_reference ("section", symbuf);
7931 return FALSE;
7932 }
a0c8462f
AM
7933 }
7934 else
d9352518 7935 {
8b127cbc 7936 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7937 isymbuf, locsymcount)
8b127cbc 7938 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 7939 result, input_bfd))
d9352518
DB
7940 {
7941 undefined_reference ("symbol", symbuf);
7942 return FALSE;
7943 }
7944 }
7945
7946 return TRUE;
a0c8462f 7947
d9352518
DB
7948 /* All that remains are operators. */
7949
7950#define UNARY_OP(op) \
7951 if (strncmp (sym, #op, strlen (#op)) == 0) \
7952 { \
7953 sym += strlen (#op); \
a0c8462f
AM
7954 if (*sym == ':') \
7955 ++sym; \
0f02bbd9 7956 *symp = sym; \
8b127cbc 7957 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7958 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7959 return FALSE; \
7960 if (signed_p) \
0f02bbd9 7961 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7962 else \
7963 *result = op a; \
d9352518
DB
7964 return TRUE; \
7965 }
7966
7967#define BINARY_OP(op) \
7968 if (strncmp (sym, #op, strlen (#op)) == 0) \
7969 { \
7970 sym += strlen (#op); \
a0c8462f
AM
7971 if (*sym == ':') \
7972 ++sym; \
0f02bbd9 7973 *symp = sym; \
8b127cbc 7974 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7975 isymbuf, locsymcount, signed_p)) \
a0c8462f 7976 return FALSE; \
0f02bbd9 7977 ++*symp; \
8b127cbc 7978 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 7979 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7980 return FALSE; \
7981 if (signed_p) \
0f02bbd9 7982 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
7983 else \
7984 *result = a op b; \
d9352518
DB
7985 return TRUE; \
7986 }
7987
7988 default:
7989 UNARY_OP (0-);
7990 BINARY_OP (<<);
7991 BINARY_OP (>>);
7992 BINARY_OP (==);
7993 BINARY_OP (!=);
7994 BINARY_OP (<=);
7995 BINARY_OP (>=);
7996 BINARY_OP (&&);
7997 BINARY_OP (||);
7998 UNARY_OP (~);
7999 UNARY_OP (!);
8000 BINARY_OP (*);
8001 BINARY_OP (/);
8002 BINARY_OP (%);
8003 BINARY_OP (^);
8004 BINARY_OP (|);
8005 BINARY_OP (&);
8006 BINARY_OP (+);
8007 BINARY_OP (-);
8008 BINARY_OP (<);
8009 BINARY_OP (>);
8010#undef UNARY_OP
8011#undef BINARY_OP
8012 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8013 bfd_set_error (bfd_error_invalid_operation);
8014 return FALSE;
8015 }
8016}
8017
d9352518 8018static void
a0c8462f
AM
8019put_value (bfd_vma size,
8020 unsigned long chunksz,
8021 bfd *input_bfd,
8022 bfd_vma x,
8023 bfd_byte *location)
d9352518
DB
8024{
8025 location += (size - chunksz);
8026
41cd1ad1 8027 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8028 {
8029 switch (chunksz)
8030 {
d9352518
DB
8031 case 1:
8032 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8033 x >>= 8;
d9352518
DB
8034 break;
8035 case 2:
8036 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8037 x >>= 16;
d9352518
DB
8038 break;
8039 case 4:
8040 bfd_put_32 (input_bfd, x, location);
65164438
NC
8041 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8042 x >>= 16;
8043 x >>= 16;
d9352518 8044 break;
d9352518 8045#ifdef BFD64
41cd1ad1 8046 case 8:
d9352518 8047 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8048 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8049 x >>= 32;
8050 x >>= 32;
8051 break;
d9352518 8052#endif
41cd1ad1
NC
8053 default:
8054 abort ();
d9352518
DB
8055 break;
8056 }
8057 }
8058}
8059
a0c8462f
AM
8060static bfd_vma
8061get_value (bfd_vma size,
8062 unsigned long chunksz,
8063 bfd *input_bfd,
8064 bfd_byte *location)
d9352518 8065{
9b239e0e 8066 int shift;
d9352518
DB
8067 bfd_vma x = 0;
8068
9b239e0e
NC
8069 /* Sanity checks. */
8070 BFD_ASSERT (chunksz <= sizeof (x)
8071 && size >= chunksz
8072 && chunksz != 0
8073 && (size % chunksz) == 0
8074 && input_bfd != NULL
8075 && location != NULL);
8076
8077 if (chunksz == sizeof (x))
8078 {
8079 BFD_ASSERT (size == chunksz);
8080
8081 /* Make sure that we do not perform an undefined shift operation.
8082 We know that size == chunksz so there will only be one iteration
8083 of the loop below. */
8084 shift = 0;
8085 }
8086 else
8087 shift = 8 * chunksz;
8088
a0c8462f 8089 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8090 {
8091 switch (chunksz)
8092 {
d9352518 8093 case 1:
9b239e0e 8094 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8095 break;
8096 case 2:
9b239e0e 8097 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8098 break;
8099 case 4:
9b239e0e 8100 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8101 break;
d9352518 8102#ifdef BFD64
9b239e0e
NC
8103 case 8:
8104 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8105 break;
9b239e0e
NC
8106#endif
8107 default:
8108 abort ();
d9352518
DB
8109 }
8110 }
8111 return x;
8112}
8113
a0c8462f
AM
8114static void
8115decode_complex_addend (unsigned long *start, /* in bits */
8116 unsigned long *oplen, /* in bits */
8117 unsigned long *len, /* in bits */
8118 unsigned long *wordsz, /* in bytes */
8119 unsigned long *chunksz, /* in bytes */
8120 unsigned long *lsb0_p,
8121 unsigned long *signed_p,
8122 unsigned long *trunc_p,
8123 unsigned long encoded)
d9352518
DB
8124{
8125 * start = encoded & 0x3F;
8126 * len = (encoded >> 6) & 0x3F;
8127 * oplen = (encoded >> 12) & 0x3F;
8128 * wordsz = (encoded >> 18) & 0xF;
8129 * chunksz = (encoded >> 22) & 0xF;
8130 * lsb0_p = (encoded >> 27) & 1;
8131 * signed_p = (encoded >> 28) & 1;
8132 * trunc_p = (encoded >> 29) & 1;
8133}
8134
cdfeee4f 8135bfd_reloc_status_type
0f02bbd9 8136bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8137 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8138 bfd_byte *contents,
8139 Elf_Internal_Rela *rel,
8140 bfd_vma relocation)
d9352518 8141{
0f02bbd9
AM
8142 bfd_vma shift, x, mask;
8143 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8144 bfd_reloc_status_type r;
d9352518
DB
8145
8146 /* Perform this reloc, since it is complex.
8147 (this is not to say that it necessarily refers to a complex
8148 symbol; merely that it is a self-describing CGEN based reloc.
8149 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8150 word size, etc) encoded within it.). */
d9352518 8151
a0c8462f
AM
8152 decode_complex_addend (&start, &oplen, &len, &wordsz,
8153 &chunksz, &lsb0_p, &signed_p,
8154 &trunc_p, rel->r_addend);
d9352518
DB
8155
8156 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8157
8158 if (lsb0_p)
8159 shift = (start + 1) - len;
8160 else
8161 shift = (8 * wordsz) - (start + len);
8162
37b01f6a
DG
8163 x = get_value (wordsz, chunksz, input_bfd,
8164 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8165
8166#ifdef DEBUG
8167 printf ("Doing complex reloc: "
8168 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8169 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8170 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8171 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8172 oplen, (unsigned long) x, (unsigned long) mask,
8173 (unsigned long) relocation);
d9352518
DB
8174#endif
8175
cdfeee4f 8176 r = bfd_reloc_ok;
d9352518 8177 if (! trunc_p)
cdfeee4f
AM
8178 /* Now do an overflow check. */
8179 r = bfd_check_overflow ((signed_p
8180 ? complain_overflow_signed
8181 : complain_overflow_unsigned),
8182 len, 0, (8 * wordsz),
8183 relocation);
a0c8462f 8184
d9352518
DB
8185 /* Do the deed. */
8186 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8187
8188#ifdef DEBUG
8189 printf (" relocation: %8.8lx\n"
8190 " shifted mask: %8.8lx\n"
8191 " shifted/masked reloc: %8.8lx\n"
8192 " result: %8.8lx\n",
9ccb8af9
AM
8193 (unsigned long) relocation, (unsigned long) (mask << shift),
8194 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8195#endif
37b01f6a
DG
8196 put_value (wordsz, chunksz, input_bfd, x,
8197 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8198 return r;
d9352518
DB
8199}
8200
0e287786
AM
8201/* Functions to read r_offset from external (target order) reloc
8202 entry. Faster than bfd_getl32 et al, because we let the compiler
8203 know the value is aligned. */
53df40a4 8204
0e287786
AM
8205static bfd_vma
8206ext32l_r_offset (const void *p)
53df40a4
AM
8207{
8208 union aligned32
8209 {
8210 uint32_t v;
8211 unsigned char c[4];
8212 };
8213 const union aligned32 *a
0e287786 8214 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8215
8216 uint32_t aval = ( (uint32_t) a->c[0]
8217 | (uint32_t) a->c[1] << 8
8218 | (uint32_t) a->c[2] << 16
8219 | (uint32_t) a->c[3] << 24);
0e287786 8220 return aval;
53df40a4
AM
8221}
8222
0e287786
AM
8223static bfd_vma
8224ext32b_r_offset (const void *p)
53df40a4
AM
8225{
8226 union aligned32
8227 {
8228 uint32_t v;
8229 unsigned char c[4];
8230 };
8231 const union aligned32 *a
0e287786 8232 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8233
8234 uint32_t aval = ( (uint32_t) a->c[0] << 24
8235 | (uint32_t) a->c[1] << 16
8236 | (uint32_t) a->c[2] << 8
8237 | (uint32_t) a->c[3]);
0e287786 8238 return aval;
53df40a4
AM
8239}
8240
8241#ifdef BFD_HOST_64_BIT
0e287786
AM
8242static bfd_vma
8243ext64l_r_offset (const void *p)
53df40a4
AM
8244{
8245 union aligned64
8246 {
8247 uint64_t v;
8248 unsigned char c[8];
8249 };
8250 const union aligned64 *a
0e287786 8251 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8252
8253 uint64_t aval = ( (uint64_t) a->c[0]
8254 | (uint64_t) a->c[1] << 8
8255 | (uint64_t) a->c[2] << 16
8256 | (uint64_t) a->c[3] << 24
8257 | (uint64_t) a->c[4] << 32
8258 | (uint64_t) a->c[5] << 40
8259 | (uint64_t) a->c[6] << 48
8260 | (uint64_t) a->c[7] << 56);
0e287786 8261 return aval;
53df40a4
AM
8262}
8263
0e287786
AM
8264static bfd_vma
8265ext64b_r_offset (const void *p)
53df40a4
AM
8266{
8267 union aligned64
8268 {
8269 uint64_t v;
8270 unsigned char c[8];
8271 };
8272 const union aligned64 *a
0e287786 8273 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8274
8275 uint64_t aval = ( (uint64_t) a->c[0] << 56
8276 | (uint64_t) a->c[1] << 48
8277 | (uint64_t) a->c[2] << 40
8278 | (uint64_t) a->c[3] << 32
8279 | (uint64_t) a->c[4] << 24
8280 | (uint64_t) a->c[5] << 16
8281 | (uint64_t) a->c[6] << 8
8282 | (uint64_t) a->c[7]);
0e287786 8283 return aval;
53df40a4
AM
8284}
8285#endif
8286
c152c796
AM
8287/* When performing a relocatable link, the input relocations are
8288 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8289 referenced must be updated. Update all the relocations found in
8290 RELDATA. */
c152c796 8291
bca6d0e3 8292static bfd_boolean
c152c796 8293elf_link_adjust_relocs (bfd *abfd,
28dbcedc
AM
8294 struct bfd_elf_section_reloc_data *reldata,
8295 bfd_boolean sort)
c152c796
AM
8296{
8297 unsigned int i;
8298 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8299 bfd_byte *erela;
8300 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8301 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8302 bfd_vma r_type_mask;
8303 int r_sym_shift;
d4730f92
BS
8304 unsigned int count = reldata->count;
8305 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8306
d4730f92 8307 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8308 {
8309 swap_in = bed->s->swap_reloc_in;
8310 swap_out = bed->s->swap_reloc_out;
8311 }
d4730f92 8312 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8313 {
8314 swap_in = bed->s->swap_reloca_in;
8315 swap_out = bed->s->swap_reloca_out;
8316 }
8317 else
8318 abort ();
8319
8320 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8321 abort ();
8322
8323 if (bed->s->arch_size == 32)
8324 {
8325 r_type_mask = 0xff;
8326 r_sym_shift = 8;
8327 }
8328 else
8329 {
8330 r_type_mask = 0xffffffff;
8331 r_sym_shift = 32;
8332 }
8333
d4730f92
BS
8334 erela = reldata->hdr->contents;
8335 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8336 {
8337 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8338 unsigned int j;
8339
8340 if (*rel_hash == NULL)
8341 continue;
8342
8343 BFD_ASSERT ((*rel_hash)->indx >= 0);
8344
8345 (*swap_in) (abfd, erela, irela);
8346 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8347 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8348 | (irela[j].r_info & r_type_mask));
8349 (*swap_out) (abfd, irela, erela);
8350 }
53df40a4 8351
0e287786 8352 if (sort && count != 0)
53df40a4 8353 {
0e287786
AM
8354 bfd_vma (*ext_r_off) (const void *);
8355 bfd_vma r_off;
8356 size_t elt_size;
8357 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8358 bfd_byte *buf = NULL;
28dbcedc
AM
8359
8360 if (bed->s->arch_size == 32)
8361 {
8362 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8363 ext_r_off = ext32l_r_offset;
28dbcedc 8364 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8365 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8366 else
8367 abort ();
8368 }
53df40a4 8369 else
28dbcedc 8370 {
53df40a4 8371#ifdef BFD_HOST_64_BIT
28dbcedc 8372 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8373 ext_r_off = ext64l_r_offset;
28dbcedc 8374 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8375 ext_r_off = ext64b_r_offset;
28dbcedc 8376 else
53df40a4 8377#endif
28dbcedc
AM
8378 abort ();
8379 }
0e287786 8380
bca6d0e3
AM
8381 /* Must use a stable sort here. A modified insertion sort,
8382 since the relocs are mostly sorted already. */
0e287786
AM
8383 elt_size = reldata->hdr->sh_entsize;
8384 base = reldata->hdr->contents;
8385 end = base + count * elt_size;
bca6d0e3 8386 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8387 abort ();
8388
8389 /* Ensure the first element is lowest. This acts as a sentinel,
8390 speeding the main loop below. */
8391 r_off = (*ext_r_off) (base);
8392 for (p = loc = base; (p += elt_size) < end; )
8393 {
8394 bfd_vma r_off2 = (*ext_r_off) (p);
8395 if (r_off > r_off2)
8396 {
8397 r_off = r_off2;
8398 loc = p;
8399 }
8400 }
8401 if (loc != base)
8402 {
8403 /* Don't just swap *base and *loc as that changes the order
8404 of the original base[0] and base[1] if they happen to
8405 have the same r_offset. */
bca6d0e3
AM
8406 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8407 memcpy (onebuf, loc, elt_size);
0e287786 8408 memmove (base + elt_size, base, loc - base);
bca6d0e3 8409 memcpy (base, onebuf, elt_size);
0e287786
AM
8410 }
8411
b29b8669 8412 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8413 {
8414 /* base to p is sorted, *p is next to insert. */
8415 r_off = (*ext_r_off) (p);
8416 /* Search the sorted region for location to insert. */
8417 loc = p - elt_size;
8418 while (r_off < (*ext_r_off) (loc))
8419 loc -= elt_size;
8420 loc += elt_size;
8421 if (loc != p)
8422 {
bca6d0e3
AM
8423 /* Chances are there is a run of relocs to insert here,
8424 from one of more input files. Files are not always
8425 linked in order due to the way elf_link_input_bfd is
8426 called. See pr17666. */
8427 size_t sortlen = p - loc;
8428 bfd_vma r_off2 = (*ext_r_off) (loc);
8429 size_t runlen = elt_size;
8430 size_t buf_size = 96 * 1024;
8431 while (p + runlen < end
8432 && (sortlen <= buf_size
8433 || runlen + elt_size <= buf_size)
8434 && r_off2 > (*ext_r_off) (p + runlen))
8435 runlen += elt_size;
8436 if (buf == NULL)
8437 {
8438 buf = bfd_malloc (buf_size);
8439 if (buf == NULL)
8440 return FALSE;
8441 }
8442 if (runlen < sortlen)
8443 {
8444 memcpy (buf, p, runlen);
8445 memmove (loc + runlen, loc, sortlen);
8446 memcpy (loc, buf, runlen);
8447 }
8448 else
8449 {
8450 memcpy (buf, loc, sortlen);
8451 memmove (loc, p, runlen);
8452 memcpy (loc + runlen, buf, sortlen);
8453 }
b29b8669 8454 p += runlen - elt_size;
0e287786
AM
8455 }
8456 }
8457 /* Hashes are no longer valid. */
28dbcedc
AM
8458 free (reldata->hashes);
8459 reldata->hashes = NULL;
bca6d0e3 8460 free (buf);
53df40a4 8461 }
bca6d0e3 8462 return TRUE;
c152c796
AM
8463}
8464
8465struct elf_link_sort_rela
8466{
8467 union {
8468 bfd_vma offset;
8469 bfd_vma sym_mask;
8470 } u;
8471 enum elf_reloc_type_class type;
8472 /* We use this as an array of size int_rels_per_ext_rel. */
8473 Elf_Internal_Rela rela[1];
8474};
8475
8476static int
8477elf_link_sort_cmp1 (const void *A, const void *B)
8478{
a50b1753
NC
8479 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8480 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8481 int relativea, relativeb;
8482
8483 relativea = a->type == reloc_class_relative;
8484 relativeb = b->type == reloc_class_relative;
8485
8486 if (relativea < relativeb)
8487 return 1;
8488 if (relativea > relativeb)
8489 return -1;
8490 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8491 return -1;
8492 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8493 return 1;
8494 if (a->rela->r_offset < b->rela->r_offset)
8495 return -1;
8496 if (a->rela->r_offset > b->rela->r_offset)
8497 return 1;
8498 return 0;
8499}
8500
8501static int
8502elf_link_sort_cmp2 (const void *A, const void *B)
8503{
a50b1753
NC
8504 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8505 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8506
7e612e98 8507 if (a->type < b->type)
c152c796 8508 return -1;
7e612e98 8509 if (a->type > b->type)
c152c796 8510 return 1;
7e612e98 8511 if (a->u.offset < b->u.offset)
c152c796 8512 return -1;
7e612e98 8513 if (a->u.offset > b->u.offset)
c152c796
AM
8514 return 1;
8515 if (a->rela->r_offset < b->rela->r_offset)
8516 return -1;
8517 if (a->rela->r_offset > b->rela->r_offset)
8518 return 1;
8519 return 0;
8520}
8521
8522static size_t
8523elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8524{
3410fea8 8525 asection *dynamic_relocs;
fc66a176
L
8526 asection *rela_dyn;
8527 asection *rel_dyn;
c152c796
AM
8528 bfd_size_type count, size;
8529 size_t i, ret, sort_elt, ext_size;
8530 bfd_byte *sort, *s_non_relative, *p;
8531 struct elf_link_sort_rela *sq;
8532 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8533 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8534 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8535 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8536 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8537 struct bfd_link_order *lo;
8538 bfd_vma r_sym_mask;
3410fea8 8539 bfd_boolean use_rela;
c152c796 8540
3410fea8
NC
8541 /* Find a dynamic reloc section. */
8542 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8543 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8544 if (rela_dyn != NULL && rela_dyn->size > 0
8545 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8546 {
3410fea8
NC
8547 bfd_boolean use_rela_initialised = FALSE;
8548
8549 /* This is just here to stop gcc from complaining.
c8e44c6d 8550 Its initialization checking code is not perfect. */
3410fea8
NC
8551 use_rela = TRUE;
8552
8553 /* Both sections are present. Examine the sizes
8554 of the indirect sections to help us choose. */
8555 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8556 if (lo->type == bfd_indirect_link_order)
8557 {
8558 asection *o = lo->u.indirect.section;
8559
8560 if ((o->size % bed->s->sizeof_rela) == 0)
8561 {
8562 if ((o->size % bed->s->sizeof_rel) == 0)
8563 /* Section size is divisible by both rel and rela sizes.
8564 It is of no help to us. */
8565 ;
8566 else
8567 {
8568 /* Section size is only divisible by rela. */
8569 if (use_rela_initialised && (use_rela == FALSE))
8570 {
c8e44c6d
AM
8571 _bfd_error_handler (_("%B: Unable to sort relocs - "
8572 "they are in more than one size"),
8573 abfd);
3410fea8
NC
8574 bfd_set_error (bfd_error_invalid_operation);
8575 return 0;
8576 }
8577 else
8578 {
8579 use_rela = TRUE;
8580 use_rela_initialised = TRUE;
8581 }
8582 }
8583 }
8584 else if ((o->size % bed->s->sizeof_rel) == 0)
8585 {
8586 /* Section size is only divisible by rel. */
8587 if (use_rela_initialised && (use_rela == TRUE))
8588 {
c8e44c6d
AM
8589 _bfd_error_handler (_("%B: Unable to sort relocs - "
8590 "they are in more than one size"),
8591 abfd);
3410fea8
NC
8592 bfd_set_error (bfd_error_invalid_operation);
8593 return 0;
8594 }
8595 else
8596 {
8597 use_rela = FALSE;
8598 use_rela_initialised = TRUE;
8599 }
8600 }
8601 else
8602 {
c8e44c6d
AM
8603 /* The section size is not divisible by either -
8604 something is wrong. */
8605 _bfd_error_handler (_("%B: Unable to sort relocs - "
8606 "they are of an unknown size"), abfd);
3410fea8
NC
8607 bfd_set_error (bfd_error_invalid_operation);
8608 return 0;
8609 }
8610 }
8611
8612 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8613 if (lo->type == bfd_indirect_link_order)
8614 {
8615 asection *o = lo->u.indirect.section;
8616
8617 if ((o->size % bed->s->sizeof_rela) == 0)
8618 {
8619 if ((o->size % bed->s->sizeof_rel) == 0)
8620 /* Section size is divisible by both rel and rela sizes.
8621 It is of no help to us. */
8622 ;
8623 else
8624 {
8625 /* Section size is only divisible by rela. */
8626 if (use_rela_initialised && (use_rela == FALSE))
8627 {
c8e44c6d
AM
8628 _bfd_error_handler (_("%B: Unable to sort relocs - "
8629 "they are in more than one size"),
8630 abfd);
3410fea8
NC
8631 bfd_set_error (bfd_error_invalid_operation);
8632 return 0;
8633 }
8634 else
8635 {
8636 use_rela = TRUE;
8637 use_rela_initialised = TRUE;
8638 }
8639 }
8640 }
8641 else if ((o->size % bed->s->sizeof_rel) == 0)
8642 {
8643 /* Section size is only divisible by rel. */
8644 if (use_rela_initialised && (use_rela == TRUE))
8645 {
c8e44c6d
AM
8646 _bfd_error_handler (_("%B: Unable to sort relocs - "
8647 "they are in more than one size"),
8648 abfd);
3410fea8
NC
8649 bfd_set_error (bfd_error_invalid_operation);
8650 return 0;
8651 }
8652 else
8653 {
8654 use_rela = FALSE;
8655 use_rela_initialised = TRUE;
8656 }
8657 }
8658 else
8659 {
c8e44c6d
AM
8660 /* The section size is not divisible by either -
8661 something is wrong. */
8662 _bfd_error_handler (_("%B: Unable to sort relocs - "
8663 "they are of an unknown size"), abfd);
3410fea8
NC
8664 bfd_set_error (bfd_error_invalid_operation);
8665 return 0;
8666 }
8667 }
8668
8669 if (! use_rela_initialised)
8670 /* Make a guess. */
8671 use_rela = TRUE;
c152c796 8672 }
fc66a176
L
8673 else if (rela_dyn != NULL && rela_dyn->size > 0)
8674 use_rela = TRUE;
8675 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8676 use_rela = FALSE;
c152c796 8677 else
fc66a176 8678 return 0;
3410fea8
NC
8679
8680 if (use_rela)
c152c796 8681 {
3410fea8 8682 dynamic_relocs = rela_dyn;
c152c796
AM
8683 ext_size = bed->s->sizeof_rela;
8684 swap_in = bed->s->swap_reloca_in;
8685 swap_out = bed->s->swap_reloca_out;
8686 }
3410fea8
NC
8687 else
8688 {
8689 dynamic_relocs = rel_dyn;
8690 ext_size = bed->s->sizeof_rel;
8691 swap_in = bed->s->swap_reloc_in;
8692 swap_out = bed->s->swap_reloc_out;
8693 }
c152c796
AM
8694
8695 size = 0;
3410fea8 8696 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8697 if (lo->type == bfd_indirect_link_order)
3410fea8 8698 size += lo->u.indirect.section->size;
c152c796 8699
3410fea8 8700 if (size != dynamic_relocs->size)
c152c796
AM
8701 return 0;
8702
8703 sort_elt = (sizeof (struct elf_link_sort_rela)
8704 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8705
8706 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8707 if (count == 0)
8708 return 0;
a50b1753 8709 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8710
c152c796
AM
8711 if (sort == NULL)
8712 {
8713 (*info->callbacks->warning)
8714 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8715 return 0;
8716 }
8717
8718 if (bed->s->arch_size == 32)
8719 r_sym_mask = ~(bfd_vma) 0xff;
8720 else
8721 r_sym_mask = ~(bfd_vma) 0xffffffff;
8722
3410fea8 8723 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8724 if (lo->type == bfd_indirect_link_order)
8725 {
8726 bfd_byte *erel, *erelend;
8727 asection *o = lo->u.indirect.section;
8728
1da212d6
AM
8729 if (o->contents == NULL && o->size != 0)
8730 {
8731 /* This is a reloc section that is being handled as a normal
8732 section. See bfd_section_from_shdr. We can't combine
8733 relocs in this case. */
8734 free (sort);
8735 return 0;
8736 }
c152c796 8737 erel = o->contents;
eea6121a 8738 erelend = o->contents + o->size;
c8e44c6d 8739 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 8740
c152c796
AM
8741 while (erel < erelend)
8742 {
8743 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8744
c152c796 8745 (*swap_in) (abfd, erel, s->rela);
7e612e98 8746 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8747 s->u.sym_mask = r_sym_mask;
8748 p += sort_elt;
8749 erel += ext_size;
8750 }
8751 }
8752
8753 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8754
8755 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8756 {
8757 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8758 if (s->type != reloc_class_relative)
8759 break;
8760 }
8761 ret = i;
8762 s_non_relative = p;
8763
8764 sq = (struct elf_link_sort_rela *) s_non_relative;
8765 for (; i < count; i++, p += sort_elt)
8766 {
8767 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8768 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8769 sq = sp;
8770 sp->u.offset = sq->rela->r_offset;
8771 }
8772
8773 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8774
c8e44c6d
AM
8775 struct elf_link_hash_table *htab = elf_hash_table (info);
8776 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8777 {
8778 /* We have plt relocs in .rela.dyn. */
8779 sq = (struct elf_link_sort_rela *) sort;
8780 for (i = 0; i < count; i++)
8781 if (sq[count - i - 1].type != reloc_class_plt)
8782 break;
8783 if (i != 0 && htab->srelplt->size == i * ext_size)
8784 {
8785 struct bfd_link_order **plo;
8786 /* Put srelplt link_order last. This is so the output_offset
8787 set in the next loop is correct for DT_JMPREL. */
8788 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8789 if ((*plo)->type == bfd_indirect_link_order
8790 && (*plo)->u.indirect.section == htab->srelplt)
8791 {
8792 lo = *plo;
8793 *plo = lo->next;
8794 }
8795 else
8796 plo = &(*plo)->next;
8797 *plo = lo;
8798 lo->next = NULL;
8799 dynamic_relocs->map_tail.link_order = lo;
8800 }
8801 }
8802
8803 p = sort;
3410fea8 8804 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8805 if (lo->type == bfd_indirect_link_order)
8806 {
8807 bfd_byte *erel, *erelend;
8808 asection *o = lo->u.indirect.section;
8809
8810 erel = o->contents;
eea6121a 8811 erelend = o->contents + o->size;
c8e44c6d 8812 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
8813 while (erel < erelend)
8814 {
8815 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8816 (*swap_out) (abfd, s->rela, erel);
8817 p += sort_elt;
8818 erel += ext_size;
8819 }
8820 }
8821
8822 free (sort);
3410fea8 8823 *psec = dynamic_relocs;
c152c796
AM
8824 return ret;
8825}
8826
ef10c3ac 8827/* Add a symbol to the output symbol string table. */
c152c796 8828
6e0b88f1 8829static int
ef10c3ac
L
8830elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8831 const char *name,
8832 Elf_Internal_Sym *elfsym,
8833 asection *input_sec,
8834 struct elf_link_hash_entry *h)
c152c796 8835{
6e0b88f1 8836 int (*output_symbol_hook)
c152c796
AM
8837 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8838 struct elf_link_hash_entry *);
ef10c3ac 8839 struct elf_link_hash_table *hash_table;
c152c796 8840 const struct elf_backend_data *bed;
ef10c3ac 8841 bfd_size_type strtabsize;
c152c796 8842
8539e4e8
AM
8843 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8844
8b127cbc 8845 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
8846 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8847 if (output_symbol_hook != NULL)
8848 {
8b127cbc 8849 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
8850 if (ret != 1)
8851 return ret;
c152c796
AM
8852 }
8853
ef10c3ac
L
8854 if (name == NULL
8855 || *name == '\0'
8856 || (input_sec->flags & SEC_EXCLUDE))
8857 elfsym->st_name = (unsigned long) -1;
c152c796
AM
8858 else
8859 {
ef10c3ac
L
8860 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8861 to get the final offset for st_name. */
8862 elfsym->st_name
8863 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8864 name, FALSE);
c152c796 8865 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8866 return 0;
c152c796
AM
8867 }
8868
ef10c3ac
L
8869 hash_table = elf_hash_table (flinfo->info);
8870 strtabsize = hash_table->strtabsize;
8871 if (strtabsize <= hash_table->strtabcount)
c152c796 8872 {
ef10c3ac
L
8873 strtabsize += strtabsize;
8874 hash_table->strtabsize = strtabsize;
8875 strtabsize *= sizeof (*hash_table->strtab);
8876 hash_table->strtab
8877 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8878 strtabsize);
8879 if (hash_table->strtab == NULL)
6e0b88f1 8880 return 0;
c152c796 8881 }
ef10c3ac
L
8882 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8883 hash_table->strtab[hash_table->strtabcount].dest_index
8884 = hash_table->strtabcount;
8885 hash_table->strtab[hash_table->strtabcount].destshndx_index
8886 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8887
8888 bfd_get_symcount (flinfo->output_bfd) += 1;
8889 hash_table->strtabcount += 1;
8890
8891 return 1;
8892}
8893
8894/* Swap symbols out to the symbol table and flush the output symbols to
8895 the file. */
8896
8897static bfd_boolean
8898elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8899{
8900 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8901 bfd_size_type amt, i;
8902 const struct elf_backend_data *bed;
8903 bfd_byte *symbuf;
8904 Elf_Internal_Shdr *hdr;
8905 file_ptr pos;
8906 bfd_boolean ret;
8907
8908 if (!hash_table->strtabcount)
8909 return TRUE;
8910
8911 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8912
8913 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 8914
ef10c3ac
L
8915 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8916 symbuf = (bfd_byte *) bfd_malloc (amt);
8917 if (symbuf == NULL)
8918 return FALSE;
1b786873 8919
ef10c3ac 8920 if (flinfo->symshndxbuf)
c152c796 8921 {
ef10c3ac
L
8922 amt = (sizeof (Elf_External_Sym_Shndx)
8923 * (bfd_get_symcount (flinfo->output_bfd)));
8924 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8925 if (flinfo->symshndxbuf == NULL)
c152c796 8926 {
ef10c3ac
L
8927 free (symbuf);
8928 return FALSE;
c152c796 8929 }
c152c796
AM
8930 }
8931
ef10c3ac
L
8932 for (i = 0; i < hash_table->strtabcount; i++)
8933 {
8934 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8935 if (elfsym->sym.st_name == (unsigned long) -1)
8936 elfsym->sym.st_name = 0;
8937 else
8938 elfsym->sym.st_name
8939 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8940 elfsym->sym.st_name);
8941 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8942 ((bfd_byte *) symbuf
8943 + (elfsym->dest_index
8944 * bed->s->sizeof_sym)),
8945 (flinfo->symshndxbuf
8946 + elfsym->destshndx_index));
8947 }
8948
8949 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8950 pos = hdr->sh_offset + hdr->sh_size;
8951 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8952 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8953 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8954 {
8955 hdr->sh_size += amt;
8956 ret = TRUE;
8957 }
8958 else
8959 ret = FALSE;
c152c796 8960
ef10c3ac
L
8961 free (symbuf);
8962
8963 free (hash_table->strtab);
8964 hash_table->strtab = NULL;
8965
8966 return ret;
c152c796
AM
8967}
8968
c0d5a53d
L
8969/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8970
8971static bfd_boolean
8972check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8973{
4fbb74a6
AM
8974 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8975 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
8976 {
8977 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 8978 beyond 64k. */
c0d5a53d
L
8979 (*_bfd_error_handler)
8980 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 8981 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
8982 bfd_set_error (bfd_error_nonrepresentable_section);
8983 return FALSE;
8984 }
8985 return TRUE;
8986}
8987
c152c796
AM
8988/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8989 allowing an unsatisfied unversioned symbol in the DSO to match a
8990 versioned symbol that would normally require an explicit version.
8991 We also handle the case that a DSO references a hidden symbol
8992 which may be satisfied by a versioned symbol in another DSO. */
8993
8994static bfd_boolean
8995elf_link_check_versioned_symbol (struct bfd_link_info *info,
8996 const struct elf_backend_data *bed,
8997 struct elf_link_hash_entry *h)
8998{
8999 bfd *abfd;
9000 struct elf_link_loaded_list *loaded;
9001
9002 if (!is_elf_hash_table (info->hash))
9003 return FALSE;
9004
90c984fc
L
9005 /* Check indirect symbol. */
9006 while (h->root.type == bfd_link_hash_indirect)
9007 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9008
c152c796
AM
9009 switch (h->root.type)
9010 {
9011 default:
9012 abfd = NULL;
9013 break;
9014
9015 case bfd_link_hash_undefined:
9016 case bfd_link_hash_undefweak:
9017 abfd = h->root.u.undef.abfd;
9018 if ((abfd->flags & DYNAMIC) == 0
e56f61be 9019 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9020 return FALSE;
9021 break;
9022
9023 case bfd_link_hash_defined:
9024 case bfd_link_hash_defweak:
9025 abfd = h->root.u.def.section->owner;
9026 break;
9027
9028 case bfd_link_hash_common:
9029 abfd = h->root.u.c.p->section->owner;
9030 break;
9031 }
9032 BFD_ASSERT (abfd != NULL);
9033
9034 for (loaded = elf_hash_table (info)->loaded;
9035 loaded != NULL;
9036 loaded = loaded->next)
9037 {
9038 bfd *input;
9039 Elf_Internal_Shdr *hdr;
9040 bfd_size_type symcount;
9041 bfd_size_type extsymcount;
9042 bfd_size_type extsymoff;
9043 Elf_Internal_Shdr *versymhdr;
9044 Elf_Internal_Sym *isym;
9045 Elf_Internal_Sym *isymend;
9046 Elf_Internal_Sym *isymbuf;
9047 Elf_External_Versym *ever;
9048 Elf_External_Versym *extversym;
9049
9050 input = loaded->abfd;
9051
9052 /* We check each DSO for a possible hidden versioned definition. */
9053 if (input == abfd
9054 || (input->flags & DYNAMIC) == 0
9055 || elf_dynversym (input) == 0)
9056 continue;
9057
9058 hdr = &elf_tdata (input)->dynsymtab_hdr;
9059
9060 symcount = hdr->sh_size / bed->s->sizeof_sym;
9061 if (elf_bad_symtab (input))
9062 {
9063 extsymcount = symcount;
9064 extsymoff = 0;
9065 }
9066 else
9067 {
9068 extsymcount = symcount - hdr->sh_info;
9069 extsymoff = hdr->sh_info;
9070 }
9071
9072 if (extsymcount == 0)
9073 continue;
9074
9075 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9076 NULL, NULL, NULL);
9077 if (isymbuf == NULL)
9078 return FALSE;
9079
9080 /* Read in any version definitions. */
9081 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9082 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9083 if (extversym == NULL)
9084 goto error_ret;
9085
9086 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9087 || (bfd_bread (extversym, versymhdr->sh_size, input)
9088 != versymhdr->sh_size))
9089 {
9090 free (extversym);
9091 error_ret:
9092 free (isymbuf);
9093 return FALSE;
9094 }
9095
9096 ever = extversym + extsymoff;
9097 isymend = isymbuf + extsymcount;
9098 for (isym = isymbuf; isym < isymend; isym++, ever++)
9099 {
9100 const char *name;
9101 Elf_Internal_Versym iver;
9102 unsigned short version_index;
9103
9104 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9105 || isym->st_shndx == SHN_UNDEF)
9106 continue;
9107
9108 name = bfd_elf_string_from_elf_section (input,
9109 hdr->sh_link,
9110 isym->st_name);
9111 if (strcmp (name, h->root.root.string) != 0)
9112 continue;
9113
9114 _bfd_elf_swap_versym_in (input, ever, &iver);
9115
d023c380
L
9116 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9117 && !(h->def_regular
9118 && h->forced_local))
c152c796
AM
9119 {
9120 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9121 have provided a definition for the undefined sym unless
9122 it is defined in a non-shared object and forced local.
9123 */
c152c796
AM
9124 abort ();
9125 }
9126
9127 version_index = iver.vs_vers & VERSYM_VERSION;
9128 if (version_index == 1 || version_index == 2)
9129 {
9130 /* This is the base or first version. We can use it. */
9131 free (extversym);
9132 free (isymbuf);
9133 return TRUE;
9134 }
9135 }
9136
9137 free (extversym);
9138 free (isymbuf);
9139 }
9140
9141 return FALSE;
9142}
9143
b8871f35
L
9144/* Convert ELF common symbol TYPE. */
9145
9146static int
9147elf_link_convert_common_type (struct bfd_link_info *info, int type)
9148{
9149 /* Commom symbol can only appear in relocatable link. */
9150 if (!bfd_link_relocatable (info))
9151 abort ();
9152 switch (info->elf_stt_common)
9153 {
9154 case unchanged:
9155 break;
9156 case elf_stt_common:
9157 type = STT_COMMON;
9158 break;
9159 case no_elf_stt_common:
9160 type = STT_OBJECT;
9161 break;
9162 }
9163 return type;
9164}
9165
c152c796
AM
9166/* Add an external symbol to the symbol table. This is called from
9167 the hash table traversal routine. When generating a shared object,
9168 we go through the symbol table twice. The first time we output
9169 anything that might have been forced to local scope in a version
9170 script. The second time we output the symbols that are still
9171 global symbols. */
9172
9173static bfd_boolean
7686d77d 9174elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9175{
7686d77d 9176 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9177 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9178 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9179 bfd_boolean strip;
9180 Elf_Internal_Sym sym;
9181 asection *input_sec;
9182 const struct elf_backend_data *bed;
6e0b88f1
AM
9183 long indx;
9184 int ret;
b8871f35 9185 unsigned int type;
6e33951e
L
9186 /* A symbol is bound locally if it is forced local or it is locally
9187 defined, hidden versioned, not referenced by shared library and
9188 not exported when linking executable. */
9189 bfd_boolean local_bind = (h->forced_local
0e1862bb 9190 || (bfd_link_executable (flinfo->info)
6e33951e
L
9191 && !flinfo->info->export_dynamic
9192 && !h->dynamic
9193 && !h->ref_dynamic
9194 && h->def_regular
422f1182 9195 && h->versioned == versioned_hidden));
c152c796
AM
9196
9197 if (h->root.type == bfd_link_hash_warning)
9198 {
9199 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9200 if (h->root.type == bfd_link_hash_new)
9201 return TRUE;
9202 }
9203
9204 /* Decide whether to output this symbol in this pass. */
9205 if (eoinfo->localsyms)
9206 {
6e33951e 9207 if (!local_bind)
c152c796
AM
9208 return TRUE;
9209 }
9210 else
9211 {
6e33951e 9212 if (local_bind)
c152c796
AM
9213 return TRUE;
9214 }
9215
8b127cbc 9216 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9217
12ac1cf5 9218 if (h->root.type == bfd_link_hash_undefined)
c152c796 9219 {
12ac1cf5
NC
9220 /* If we have an undefined symbol reference here then it must have
9221 come from a shared library that is being linked in. (Undefined
98da7939
L
9222 references in regular files have already been handled unless
9223 they are in unreferenced sections which are removed by garbage
9224 collection). */
12ac1cf5
NC
9225 bfd_boolean ignore_undef = FALSE;
9226
9227 /* Some symbols may be special in that the fact that they're
9228 undefined can be safely ignored - let backend determine that. */
9229 if (bed->elf_backend_ignore_undef_symbol)
9230 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9231
9232 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9233 if (!ignore_undef
12ac1cf5 9234 && h->ref_dynamic
8b127cbc
AM
9235 && (!h->ref_regular || flinfo->info->gc_sections)
9236 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9237 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9238 {
9239 if (!(flinfo->info->callbacks->undefined_symbol
9240 (flinfo->info, h->root.root.string,
9241 h->ref_regular ? NULL : h->root.u.undef.abfd,
9242 NULL, 0,
9243 (flinfo->info->unresolved_syms_in_shared_libs
9244 == RM_GENERATE_ERROR))))
12ac1cf5 9245 {
17d078c5 9246 bfd_set_error (bfd_error_bad_value);
12ac1cf5
NC
9247 eoinfo->failed = TRUE;
9248 return FALSE;
9249 }
c152c796 9250 }
97196564
L
9251
9252 /* Strip a global symbol defined in a discarded section. */
9253 if (h->indx == -3)
9254 return TRUE;
c152c796
AM
9255 }
9256
9257 /* We should also warn if a forced local symbol is referenced from
9258 shared libraries. */
0e1862bb 9259 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9260 && h->forced_local
9261 && h->ref_dynamic
371a5866 9262 && h->def_regular
f5385ebf 9263 && !h->dynamic_def
ee659f1f 9264 && h->ref_dynamic_nonweak
8b127cbc 9265 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9266 {
17d078c5
AM
9267 bfd *def_bfd;
9268 const char *msg;
90c984fc
L
9269 struct elf_link_hash_entry *hi = h;
9270
9271 /* Check indirect symbol. */
9272 while (hi->root.type == bfd_link_hash_indirect)
9273 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9274
9275 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9276 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9277 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9278 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9279 else
9280 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9281 def_bfd = flinfo->output_bfd;
90c984fc
L
9282 if (hi->root.u.def.section != bfd_abs_section_ptr)
9283 def_bfd = hi->root.u.def.section->owner;
8b127cbc 9284 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
17d078c5
AM
9285 h->root.root.string);
9286 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9287 eoinfo->failed = TRUE;
9288 return FALSE;
9289 }
9290
9291 /* We don't want to output symbols that have never been mentioned by
9292 a regular file, or that we have been told to strip. However, if
9293 h->indx is set to -2, the symbol is used by a reloc and we must
9294 output it. */
d983c8c5 9295 strip = FALSE;
c152c796 9296 if (h->indx == -2)
d983c8c5 9297 ;
f5385ebf 9298 else if ((h->def_dynamic
77cfaee6
AM
9299 || h->ref_dynamic
9300 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9301 && !h->def_regular
9302 && !h->ref_regular)
c152c796 9303 strip = TRUE;
8b127cbc 9304 else if (flinfo->info->strip == strip_all)
c152c796 9305 strip = TRUE;
8b127cbc
AM
9306 else if (flinfo->info->strip == strip_some
9307 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9308 h->root.root.string, FALSE, FALSE) == NULL)
9309 strip = TRUE;
d56d55e7
AM
9310 else if ((h->root.type == bfd_link_hash_defined
9311 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9312 && ((flinfo->info->strip_discarded
dbaa2011 9313 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9314 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9315 && h->root.u.def.section->owner != NULL
d56d55e7 9316 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9317 strip = TRUE;
9e2278f5
AM
9318 else if ((h->root.type == bfd_link_hash_undefined
9319 || h->root.type == bfd_link_hash_undefweak)
9320 && h->root.u.undef.abfd != NULL
9321 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9322 strip = TRUE;
c152c796 9323
b8871f35
L
9324 type = h->type;
9325
c152c796 9326 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9327 nothing else to do. However, if it is a forced local symbol or
9328 an ifunc symbol we need to give the backend finish_dynamic_symbol
9329 function a chance to make it dynamic. */
c152c796
AM
9330 if (strip
9331 && h->dynindx == -1
b8871f35 9332 && type != STT_GNU_IFUNC
f5385ebf 9333 && !h->forced_local)
c152c796
AM
9334 return TRUE;
9335
9336 sym.st_value = 0;
9337 sym.st_size = h->size;
9338 sym.st_other = h->other;
c152c796
AM
9339 switch (h->root.type)
9340 {
9341 default:
9342 case bfd_link_hash_new:
9343 case bfd_link_hash_warning:
9344 abort ();
9345 return FALSE;
9346
9347 case bfd_link_hash_undefined:
9348 case bfd_link_hash_undefweak:
9349 input_sec = bfd_und_section_ptr;
9350 sym.st_shndx = SHN_UNDEF;
9351 break;
9352
9353 case bfd_link_hash_defined:
9354 case bfd_link_hash_defweak:
9355 {
9356 input_sec = h->root.u.def.section;
9357 if (input_sec->output_section != NULL)
9358 {
9359 sym.st_shndx =
8b127cbc 9360 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9361 input_sec->output_section);
9362 if (sym.st_shndx == SHN_BAD)
9363 {
9364 (*_bfd_error_handler)
d003868e 9365 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9366 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9367 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9368 eoinfo->failed = TRUE;
9369 return FALSE;
9370 }
9371
9372 /* ELF symbols in relocatable files are section relative,
9373 but in nonrelocatable files they are virtual
9374 addresses. */
9375 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9376 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9377 {
9378 sym.st_value += input_sec->output_section->vma;
9379 if (h->type == STT_TLS)
9380 {
8b127cbc 9381 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9382 if (tls_sec != NULL)
9383 sym.st_value -= tls_sec->vma;
c152c796
AM
9384 }
9385 }
9386 }
9387 else
9388 {
9389 BFD_ASSERT (input_sec->owner == NULL
9390 || (input_sec->owner->flags & DYNAMIC) != 0);
9391 sym.st_shndx = SHN_UNDEF;
9392 input_sec = bfd_und_section_ptr;
9393 }
9394 }
9395 break;
9396
9397 case bfd_link_hash_common:
9398 input_sec = h->root.u.c.p->section;
a4d8e49b 9399 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9400 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9401 break;
9402
9403 case bfd_link_hash_indirect:
9404 /* These symbols are created by symbol versioning. They point
9405 to the decorated version of the name. For example, if the
9406 symbol foo@@GNU_1.2 is the default, which should be used when
9407 foo is used with no version, then we add an indirect symbol
9408 foo which points to foo@@GNU_1.2. We ignore these symbols,
9409 since the indirected symbol is already in the hash table. */
9410 return TRUE;
9411 }
9412
b8871f35
L
9413 if (type == STT_COMMON || type == STT_OBJECT)
9414 switch (h->root.type)
9415 {
9416 case bfd_link_hash_common:
9417 type = elf_link_convert_common_type (flinfo->info, type);
9418 break;
9419 case bfd_link_hash_defined:
9420 case bfd_link_hash_defweak:
9421 if (bed->common_definition (&sym))
9422 type = elf_link_convert_common_type (flinfo->info, type);
9423 else
9424 type = STT_OBJECT;
9425 break;
9426 case bfd_link_hash_undefined:
9427 case bfd_link_hash_undefweak:
9428 break;
9429 default:
9430 abort ();
9431 }
9432
9433 if (local_bind)
9434 {
9435 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9436 /* Turn off visibility on local symbol. */
9437 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9438 }
9439 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9440 else if (h->unique_global && h->def_regular)
9441 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9442 else if (h->root.type == bfd_link_hash_undefweak
9443 || h->root.type == bfd_link_hash_defweak)
9444 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9445 else
9446 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9447 sym.st_target_internal = h->target_internal;
9448
c152c796
AM
9449 /* Give the processor backend a chance to tweak the symbol value,
9450 and also to finish up anything that needs to be done for this
9451 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9452 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9453 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9454 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9455 && h->def_regular
0e1862bb 9456 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9457 || ((h->dynindx != -1
9458 || h->forced_local)
0e1862bb 9459 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9460 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9461 || h->root.type != bfd_link_hash_undefweak))
9462 || !h->forced_local)
8b127cbc 9463 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9464 {
9465 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9466 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9467 {
9468 eoinfo->failed = TRUE;
9469 return FALSE;
9470 }
9471 }
9472
9473 /* If we are marking the symbol as undefined, and there are no
9474 non-weak references to this symbol from a regular object, then
9475 mark the symbol as weak undefined; if there are non-weak
9476 references, mark the symbol as strong. We can't do this earlier,
9477 because it might not be marked as undefined until the
9478 finish_dynamic_symbol routine gets through with it. */
9479 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9480 && h->ref_regular
c152c796
AM
9481 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9482 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9483 {
9484 int bindtype;
b8871f35 9485 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9486
9487 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9488 if (type == STT_GNU_IFUNC)
9489 type = STT_FUNC;
c152c796 9490
f5385ebf 9491 if (h->ref_regular_nonweak)
c152c796
AM
9492 bindtype = STB_GLOBAL;
9493 else
9494 bindtype = STB_WEAK;
2955ec4c 9495 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9496 }
9497
bda987c2
CD
9498 /* If this is a symbol defined in a dynamic library, don't use the
9499 symbol size from the dynamic library. Relinking an executable
9500 against a new library may introduce gratuitous changes in the
9501 executable's symbols if we keep the size. */
9502 if (sym.st_shndx == SHN_UNDEF
9503 && !h->def_regular
9504 && h->def_dynamic)
9505 sym.st_size = 0;
9506
c152c796
AM
9507 /* If a non-weak symbol with non-default visibility is not defined
9508 locally, it is a fatal error. */
0e1862bb 9509 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9510 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9511 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9512 && h->root.type == bfd_link_hash_undefined
f5385ebf 9513 && !h->def_regular)
c152c796 9514 {
17d078c5
AM
9515 const char *msg;
9516
9517 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9518 msg = _("%B: protected symbol `%s' isn't defined");
9519 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9520 msg = _("%B: internal symbol `%s' isn't defined");
9521 else
9522 msg = _("%B: hidden symbol `%s' isn't defined");
8b127cbc 9523 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9524 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9525 eoinfo->failed = TRUE;
9526 return FALSE;
9527 }
9528
9529 /* If this symbol should be put in the .dynsym section, then put it
9530 there now. We already know the symbol index. We also fill in
9531 the entry in the .hash section. */
cae1fbbb 9532 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9533 && h->dynindx != -1
8b127cbc 9534 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9535 {
c152c796
AM
9536 bfd_byte *esym;
9537
90c984fc
L
9538 /* Since there is no version information in the dynamic string,
9539 if there is no version info in symbol version section, we will
1659f720 9540 have a run-time problem if not linking executable, referenced
6e33951e
L
9541 by shared library, not locally defined, or not bound locally.
9542 */
1659f720 9543 if (h->verinfo.verdef == NULL
6e33951e 9544 && !local_bind
0e1862bb 9545 && (!bfd_link_executable (flinfo->info)
1659f720
L
9546 || h->ref_dynamic
9547 || !h->def_regular))
90c984fc
L
9548 {
9549 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9550
9551 if (p && p [1] != '\0')
9552 {
9553 (*_bfd_error_handler)
9554 (_("%B: No symbol version section for versioned symbol `%s'"),
9555 flinfo->output_bfd, h->root.root.string);
9556 eoinfo->failed = TRUE;
9557 return FALSE;
9558 }
9559 }
9560
c152c796 9561 sym.st_name = h->dynstr_index;
cae1fbbb
L
9562 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9563 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9564 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9565 {
9566 eoinfo->failed = TRUE;
9567 return FALSE;
9568 }
8b127cbc 9569 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9570
8b127cbc 9571 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9572 {
9573 size_t hash_entry_size;
9574 bfd_byte *bucketpos;
9575 bfd_vma chain;
41198d0c
L
9576 size_t bucketcount;
9577 size_t bucket;
9578
8b127cbc 9579 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9580 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9581
9582 hash_entry_size
8b127cbc
AM
9583 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9584 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9585 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9586 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9587 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9588 bucketpos);
9589 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9590 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9591 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9592 }
c152c796 9593
8b127cbc 9594 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9595 {
9596 Elf_Internal_Versym iversym;
9597 Elf_External_Versym *eversym;
9598
f5385ebf 9599 if (!h->def_regular)
c152c796 9600 {
7b20f099
AM
9601 if (h->verinfo.verdef == NULL
9602 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9603 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9604 iversym.vs_vers = 0;
9605 else
9606 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9607 }
9608 else
9609 {
9610 if (h->verinfo.vertree == NULL)
9611 iversym.vs_vers = 1;
9612 else
9613 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9614 if (flinfo->info->create_default_symver)
3e3b46e5 9615 iversym.vs_vers++;
c152c796
AM
9616 }
9617
422f1182 9618 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9619 defined locally. */
422f1182 9620 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9621 iversym.vs_vers |= VERSYM_HIDDEN;
9622
8b127cbc 9623 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9624 eversym += h->dynindx;
8b127cbc 9625 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9626 }
9627 }
9628
d983c8c5
AM
9629 /* If the symbol is undefined, and we didn't output it to .dynsym,
9630 strip it from .symtab too. Obviously we can't do this for
9631 relocatable output or when needed for --emit-relocs. */
9632 else if (input_sec == bfd_und_section_ptr
9633 && h->indx != -2
0e1862bb 9634 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9635 return TRUE;
9636 /* Also strip others that we couldn't earlier due to dynamic symbol
9637 processing. */
9638 if (strip)
9639 return TRUE;
9640 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9641 return TRUE;
9642
2ec55de3
AM
9643 /* Output a FILE symbol so that following locals are not associated
9644 with the wrong input file. We need one for forced local symbols
9645 if we've seen more than one FILE symbol or when we have exactly
9646 one FILE symbol but global symbols are present in a file other
9647 than the one with the FILE symbol. We also need one if linker
9648 defined symbols are present. In practice these conditions are
9649 always met, so just emit the FILE symbol unconditionally. */
9650 if (eoinfo->localsyms
9651 && !eoinfo->file_sym_done
9652 && eoinfo->flinfo->filesym_count != 0)
9653 {
9654 Elf_Internal_Sym fsym;
9655
9656 memset (&fsym, 0, sizeof (fsym));
9657 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9658 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9659 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9660 bfd_und_section_ptr, NULL))
2ec55de3
AM
9661 return FALSE;
9662
9663 eoinfo->file_sym_done = TRUE;
9664 }
9665
8b127cbc 9666 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9667 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9668 input_sec, h);
6e0b88f1 9669 if (ret == 0)
c152c796
AM
9670 {
9671 eoinfo->failed = TRUE;
9672 return FALSE;
9673 }
6e0b88f1
AM
9674 else if (ret == 1)
9675 h->indx = indx;
9676 else if (h->indx == -2)
9677 abort();
c152c796
AM
9678
9679 return TRUE;
9680}
9681
cdd3575c
AM
9682/* Return TRUE if special handling is done for relocs in SEC against
9683 symbols defined in discarded sections. */
9684
c152c796
AM
9685static bfd_boolean
9686elf_section_ignore_discarded_relocs (asection *sec)
9687{
9688 const struct elf_backend_data *bed;
9689
cdd3575c
AM
9690 switch (sec->sec_info_type)
9691 {
dbaa2011
AM
9692 case SEC_INFO_TYPE_STABS:
9693 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9694 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9695 return TRUE;
9696 default:
9697 break;
9698 }
c152c796
AM
9699
9700 bed = get_elf_backend_data (sec->owner);
9701 if (bed->elf_backend_ignore_discarded_relocs != NULL
9702 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9703 return TRUE;
9704
9705 return FALSE;
9706}
9707
9e66c942
AM
9708/* Return a mask saying how ld should treat relocations in SEC against
9709 symbols defined in discarded sections. If this function returns
9710 COMPLAIN set, ld will issue a warning message. If this function
9711 returns PRETEND set, and the discarded section was link-once and the
9712 same size as the kept link-once section, ld will pretend that the
9713 symbol was actually defined in the kept section. Otherwise ld will
9714 zero the reloc (at least that is the intent, but some cooperation by
9715 the target dependent code is needed, particularly for REL targets). */
9716
8a696751
AM
9717unsigned int
9718_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9719{
9e66c942 9720 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9721 return PRETEND;
cdd3575c
AM
9722
9723 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9724 return 0;
cdd3575c
AM
9725
9726 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9727 return 0;
cdd3575c 9728
9e66c942 9729 return COMPLAIN | PRETEND;
cdd3575c
AM
9730}
9731
3d7f7666
L
9732/* Find a match between a section and a member of a section group. */
9733
9734static asection *
c0f00686
L
9735match_group_member (asection *sec, asection *group,
9736 struct bfd_link_info *info)
3d7f7666
L
9737{
9738 asection *first = elf_next_in_group (group);
9739 asection *s = first;
9740
9741 while (s != NULL)
9742 {
c0f00686 9743 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9744 return s;
9745
83180ade 9746 s = elf_next_in_group (s);
3d7f7666
L
9747 if (s == first)
9748 break;
9749 }
9750
9751 return NULL;
9752}
9753
01b3c8ab 9754/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9755 to replace it. Return the replacement if it is OK. Otherwise return
9756 NULL. */
01b3c8ab
L
9757
9758asection *
c0f00686 9759_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9760{
9761 asection *kept;
9762
9763 kept = sec->kept_section;
9764 if (kept != NULL)
9765 {
c2370991 9766 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9767 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9768 if (kept != NULL
9769 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9770 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9771 kept = NULL;
c2370991 9772 sec->kept_section = kept;
01b3c8ab
L
9773 }
9774 return kept;
9775}
9776
c152c796
AM
9777/* Link an input file into the linker output file. This function
9778 handles all the sections and relocations of the input file at once.
9779 This is so that we only have to read the local symbols once, and
9780 don't have to keep them in memory. */
9781
9782static bfd_boolean
8b127cbc 9783elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9784{
ece5ef60 9785 int (*relocate_section)
c152c796
AM
9786 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9787 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9788 bfd *output_bfd;
9789 Elf_Internal_Shdr *symtab_hdr;
9790 size_t locsymcount;
9791 size_t extsymoff;
9792 Elf_Internal_Sym *isymbuf;
9793 Elf_Internal_Sym *isym;
9794 Elf_Internal_Sym *isymend;
9795 long *pindex;
9796 asection **ppsection;
9797 asection *o;
9798 const struct elf_backend_data *bed;
c152c796 9799 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9800 bfd_size_type address_size;
9801 bfd_vma r_type_mask;
9802 int r_sym_shift;
ffbc01cc 9803 bfd_boolean have_file_sym = FALSE;
c152c796 9804
8b127cbc 9805 output_bfd = flinfo->output_bfd;
c152c796
AM
9806 bed = get_elf_backend_data (output_bfd);
9807 relocate_section = bed->elf_backend_relocate_section;
9808
9809 /* If this is a dynamic object, we don't want to do anything here:
9810 we don't want the local symbols, and we don't want the section
9811 contents. */
9812 if ((input_bfd->flags & DYNAMIC) != 0)
9813 return TRUE;
9814
c152c796
AM
9815 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9816 if (elf_bad_symtab (input_bfd))
9817 {
9818 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9819 extsymoff = 0;
9820 }
9821 else
9822 {
9823 locsymcount = symtab_hdr->sh_info;
9824 extsymoff = symtab_hdr->sh_info;
9825 }
9826
9827 /* Read the local symbols. */
9828 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9829 if (isymbuf == NULL && locsymcount != 0)
9830 {
9831 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9832 flinfo->internal_syms,
9833 flinfo->external_syms,
9834 flinfo->locsym_shndx);
c152c796
AM
9835 if (isymbuf == NULL)
9836 return FALSE;
9837 }
9838
9839 /* Find local symbol sections and adjust values of symbols in
9840 SEC_MERGE sections. Write out those local symbols we know are
9841 going into the output file. */
9842 isymend = isymbuf + locsymcount;
8b127cbc 9843 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9844 isym < isymend;
9845 isym++, pindex++, ppsection++)
9846 {
9847 asection *isec;
9848 const char *name;
9849 Elf_Internal_Sym osym;
6e0b88f1
AM
9850 long indx;
9851 int ret;
c152c796
AM
9852
9853 *pindex = -1;
9854
9855 if (elf_bad_symtab (input_bfd))
9856 {
9857 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9858 {
9859 *ppsection = NULL;
9860 continue;
9861 }
9862 }
9863
9864 if (isym->st_shndx == SHN_UNDEF)
9865 isec = bfd_und_section_ptr;
c152c796
AM
9866 else if (isym->st_shndx == SHN_ABS)
9867 isec = bfd_abs_section_ptr;
9868 else if (isym->st_shndx == SHN_COMMON)
9869 isec = bfd_com_section_ptr;
9870 else
9871 {
cb33740c
AM
9872 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9873 if (isec == NULL)
9874 {
9875 /* Don't attempt to output symbols with st_shnx in the
9876 reserved range other than SHN_ABS and SHN_COMMON. */
9877 *ppsection = NULL;
9878 continue;
9879 }
dbaa2011 9880 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9881 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9882 isym->st_value =
9883 _bfd_merged_section_offset (output_bfd, &isec,
9884 elf_section_data (isec)->sec_info,
9885 isym->st_value);
c152c796
AM
9886 }
9887
9888 *ppsection = isec;
9889
d983c8c5
AM
9890 /* Don't output the first, undefined, symbol. In fact, don't
9891 output any undefined local symbol. */
9892 if (isec == bfd_und_section_ptr)
c152c796
AM
9893 continue;
9894
9895 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9896 {
9897 /* We never output section symbols. Instead, we use the
9898 section symbol of the corresponding section in the output
9899 file. */
9900 continue;
9901 }
9902
9903 /* If we are stripping all symbols, we don't want to output this
9904 one. */
8b127cbc 9905 if (flinfo->info->strip == strip_all)
c152c796
AM
9906 continue;
9907
9908 /* If we are discarding all local symbols, we don't want to
9909 output this one. If we are generating a relocatable output
9910 file, then some of the local symbols may be required by
9911 relocs; we output them below as we discover that they are
9912 needed. */
8b127cbc 9913 if (flinfo->info->discard == discard_all)
c152c796
AM
9914 continue;
9915
9916 /* If this symbol is defined in a section which we are
f02571c5
AM
9917 discarding, we don't need to keep it. */
9918 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9919 && isym->st_shndx < SHN_LORESERVE
9920 && bfd_section_removed_from_list (output_bfd,
9921 isec->output_section))
e75a280b
L
9922 continue;
9923
c152c796
AM
9924 /* Get the name of the symbol. */
9925 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9926 isym->st_name);
9927 if (name == NULL)
9928 return FALSE;
9929
9930 /* See if we are discarding symbols with this name. */
8b127cbc
AM
9931 if ((flinfo->info->strip == strip_some
9932 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 9933 == NULL))
8b127cbc 9934 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
9935 && (isec->flags & SEC_MERGE)
9936 && !bfd_link_relocatable (flinfo->info))
8b127cbc 9937 || flinfo->info->discard == discard_l)
c152c796
AM
9938 && bfd_is_local_label_name (input_bfd, name)))
9939 continue;
9940
ffbc01cc
AM
9941 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9942 {
ce875075
AM
9943 if (input_bfd->lto_output)
9944 /* -flto puts a temp file name here. This means builds
9945 are not reproducible. Discard the symbol. */
9946 continue;
ffbc01cc
AM
9947 have_file_sym = TRUE;
9948 flinfo->filesym_count += 1;
9949 }
9950 if (!have_file_sym)
9951 {
9952 /* In the absence of debug info, bfd_find_nearest_line uses
9953 FILE symbols to determine the source file for local
9954 function symbols. Provide a FILE symbol here if input
9955 files lack such, so that their symbols won't be
9956 associated with a previous input file. It's not the
9957 source file, but the best we can do. */
9958 have_file_sym = TRUE;
9959 flinfo->filesym_count += 1;
9960 memset (&osym, 0, sizeof (osym));
9961 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9962 osym.st_shndx = SHN_ABS;
ef10c3ac
L
9963 if (!elf_link_output_symstrtab (flinfo,
9964 (input_bfd->lto_output ? NULL
9965 : input_bfd->filename),
9966 &osym, bfd_abs_section_ptr,
9967 NULL))
ffbc01cc
AM
9968 return FALSE;
9969 }
9970
c152c796
AM
9971 osym = *isym;
9972
9973 /* Adjust the section index for the output file. */
9974 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9975 isec->output_section);
9976 if (osym.st_shndx == SHN_BAD)
9977 return FALSE;
9978
c152c796
AM
9979 /* ELF symbols in relocatable files are section relative, but
9980 in executable files they are virtual addresses. Note that
9981 this code assumes that all ELF sections have an associated
9982 BFD section with a reasonable value for output_offset; below
9983 we assume that they also have a reasonable value for
9984 output_section. Any special sections must be set up to meet
9985 these requirements. */
9986 osym.st_value += isec->output_offset;
0e1862bb 9987 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9988 {
9989 osym.st_value += isec->output_section->vma;
9990 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9991 {
9992 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
9993 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9994 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
9995 }
9996 }
9997
6e0b88f1 9998 indx = bfd_get_symcount (output_bfd);
ef10c3ac 9999 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10000 if (ret == 0)
c152c796 10001 return FALSE;
6e0b88f1
AM
10002 else if (ret == 1)
10003 *pindex = indx;
c152c796
AM
10004 }
10005
310fd250
L
10006 if (bed->s->arch_size == 32)
10007 {
10008 r_type_mask = 0xff;
10009 r_sym_shift = 8;
10010 address_size = 4;
10011 }
10012 else
10013 {
10014 r_type_mask = 0xffffffff;
10015 r_sym_shift = 32;
10016 address_size = 8;
10017 }
10018
c152c796
AM
10019 /* Relocate the contents of each section. */
10020 sym_hashes = elf_sym_hashes (input_bfd);
10021 for (o = input_bfd->sections; o != NULL; o = o->next)
10022 {
10023 bfd_byte *contents;
10024
10025 if (! o->linker_mark)
10026 {
10027 /* This section was omitted from the link. */
10028 continue;
10029 }
10030
0e1862bb 10031 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
10032 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10033 {
10034 /* Deal with the group signature symbol. */
10035 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10036 unsigned long symndx = sec_data->this_hdr.sh_info;
10037 asection *osec = o->output_section;
10038
10039 if (symndx >= locsymcount
10040 || (elf_bad_symtab (input_bfd)
8b127cbc 10041 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10042 {
10043 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10044 while (h->root.type == bfd_link_hash_indirect
10045 || h->root.type == bfd_link_hash_warning)
10046 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10047 /* Arrange for symbol to be output. */
10048 h->indx = -2;
10049 elf_section_data (osec)->this_hdr.sh_info = -2;
10050 }
10051 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10052 {
10053 /* We'll use the output section target_index. */
8b127cbc 10054 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10055 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10056 }
10057 else
10058 {
8b127cbc 10059 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10060 {
10061 /* Otherwise output the local symbol now. */
10062 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10063 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10064 const char *name;
6e0b88f1
AM
10065 long indx;
10066 int ret;
bcacc0f5
AM
10067
10068 name = bfd_elf_string_from_elf_section (input_bfd,
10069 symtab_hdr->sh_link,
10070 sym.st_name);
10071 if (name == NULL)
10072 return FALSE;
10073
10074 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10075 sec);
10076 if (sym.st_shndx == SHN_BAD)
10077 return FALSE;
10078
10079 sym.st_value += o->output_offset;
10080
6e0b88f1 10081 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10082 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10083 NULL);
6e0b88f1 10084 if (ret == 0)
bcacc0f5 10085 return FALSE;
6e0b88f1 10086 else if (ret == 1)
8b127cbc 10087 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10088 else
10089 abort ();
bcacc0f5
AM
10090 }
10091 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10092 = flinfo->indices[symndx];
bcacc0f5
AM
10093 }
10094 }
10095
c152c796 10096 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10097 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10098 continue;
10099
10100 if ((o->flags & SEC_LINKER_CREATED) != 0)
10101 {
10102 /* Section was created by _bfd_elf_link_create_dynamic_sections
10103 or somesuch. */
10104 continue;
10105 }
10106
10107 /* Get the contents of the section. They have been cached by a
10108 relaxation routine. Note that o is a section in an input
10109 file, so the contents field will not have been set by any of
10110 the routines which work on output files. */
10111 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10112 {
10113 contents = elf_section_data (o)->this_hdr.contents;
10114 if (bed->caches_rawsize
10115 && o->rawsize != 0
10116 && o->rawsize < o->size)
10117 {
10118 memcpy (flinfo->contents, contents, o->rawsize);
10119 contents = flinfo->contents;
10120 }
10121 }
c152c796
AM
10122 else
10123 {
8b127cbc 10124 contents = flinfo->contents;
4a114e3e 10125 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10126 return FALSE;
10127 }
10128
10129 if ((o->flags & SEC_RELOC) != 0)
10130 {
10131 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10132 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10133 int action_discarded;
ece5ef60 10134 int ret;
c152c796
AM
10135
10136 /* Get the swapped relocs. */
10137 internal_relocs
8b127cbc
AM
10138 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10139 flinfo->internal_relocs, FALSE);
c152c796
AM
10140 if (internal_relocs == NULL
10141 && o->reloc_count > 0)
10142 return FALSE;
10143
310fd250
L
10144 /* We need to reverse-copy input .ctors/.dtors sections if
10145 they are placed in .init_array/.finit_array for output. */
10146 if (o->size > address_size
10147 && ((strncmp (o->name, ".ctors", 6) == 0
10148 && strcmp (o->output_section->name,
10149 ".init_array") == 0)
10150 || (strncmp (o->name, ".dtors", 6) == 0
10151 && strcmp (o->output_section->name,
10152 ".fini_array") == 0))
10153 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10154 {
310fd250
L
10155 if (o->size != o->reloc_count * address_size)
10156 {
10157 (*_bfd_error_handler)
10158 (_("error: %B: size of section %A is not "
10159 "multiple of address size"),
10160 input_bfd, o);
10161 bfd_set_error (bfd_error_on_input);
10162 return FALSE;
10163 }
10164 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10165 }
10166
0f02bbd9 10167 action_discarded = -1;
c152c796 10168 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10169 action_discarded = (*bed->action_discarded) (o);
10170
10171 /* Run through the relocs evaluating complex reloc symbols and
10172 looking for relocs against symbols from discarded sections
10173 or section symbols from removed link-once sections.
10174 Complain about relocs against discarded sections. Zero
10175 relocs against removed link-once sections. */
10176
10177 rel = internal_relocs;
10178 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10179 for ( ; rel < relend; rel++)
c152c796 10180 {
0f02bbd9
AM
10181 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10182 unsigned int s_type;
10183 asection **ps, *sec;
10184 struct elf_link_hash_entry *h = NULL;
10185 const char *sym_name;
c152c796 10186
0f02bbd9
AM
10187 if (r_symndx == STN_UNDEF)
10188 continue;
c152c796 10189
0f02bbd9
AM
10190 if (r_symndx >= locsymcount
10191 || (elf_bad_symtab (input_bfd)
8b127cbc 10192 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10193 {
10194 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10195
0f02bbd9
AM
10196 /* Badly formatted input files can contain relocs that
10197 reference non-existant symbols. Check here so that
10198 we do not seg fault. */
10199 if (h == NULL)
c152c796 10200 {
0f02bbd9 10201 char buffer [32];
dce669a1 10202
0f02bbd9
AM
10203 sprintf_vma (buffer, rel->r_info);
10204 (*_bfd_error_handler)
10205 (_("error: %B contains a reloc (0x%s) for section %A "
10206 "that references a non-existent global symbol"),
10207 input_bfd, o, buffer);
10208 bfd_set_error (bfd_error_bad_value);
10209 return FALSE;
10210 }
3b36f7e6 10211
0f02bbd9
AM
10212 while (h->root.type == bfd_link_hash_indirect
10213 || h->root.type == bfd_link_hash_warning)
10214 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10215
0f02bbd9 10216 s_type = h->type;
cdd3575c 10217
9e2dec47 10218 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10219 mark the symbol as undefined. Note that the
10220 linker may attach linker created dynamic sections
10221 to the plugin bfd. Symbols defined in linker
10222 created sections are not plugin symbols. */
9e2dec47
L
10223 if (h->root.non_ir_ref
10224 && (h->root.type == bfd_link_hash_defined
10225 || h->root.type == bfd_link_hash_defweak)
10226 && (h->root.u.def.section->flags
10227 & SEC_LINKER_CREATED) == 0
10228 && h->root.u.def.section->owner != NULL
10229 && (h->root.u.def.section->owner->flags
10230 & BFD_PLUGIN) != 0)
10231 {
10232 h->root.type = bfd_link_hash_undefined;
10233 h->root.u.undef.abfd = h->root.u.def.section->owner;
10234 }
10235
0f02bbd9
AM
10236 ps = NULL;
10237 if (h->root.type == bfd_link_hash_defined
10238 || h->root.type == bfd_link_hash_defweak)
10239 ps = &h->root.u.def.section;
10240
10241 sym_name = h->root.root.string;
10242 }
10243 else
10244 {
10245 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10246
10247 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10248 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10249 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10250 sym, *ps);
10251 }
c152c796 10252
c301e700 10253 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10254 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10255 {
10256 bfd_vma val;
10257 bfd_vma dot = (rel->r_offset
10258 + o->output_offset + o->output_section->vma);
10259#ifdef DEBUG
10260 printf ("Encountered a complex symbol!");
10261 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10262 input_bfd->filename, o->name,
10263 (long) (rel - internal_relocs));
0f02bbd9
AM
10264 printf (" symbol: idx %8.8lx, name %s\n",
10265 r_symndx, sym_name);
10266 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10267 (unsigned long) rel->r_info,
10268 (unsigned long) rel->r_offset);
10269#endif
8b127cbc 10270 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10271 isymbuf, locsymcount, s_type == STT_SRELC))
10272 return FALSE;
10273
10274 /* Symbol evaluated OK. Update to absolute value. */
10275 set_symbol_value (input_bfd, isymbuf, locsymcount,
10276 r_symndx, val);
10277 continue;
10278 }
10279
10280 if (action_discarded != -1 && ps != NULL)
10281 {
cdd3575c
AM
10282 /* Complain if the definition comes from a
10283 discarded section. */
dbaa2011 10284 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10285 {
cf35638d 10286 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10287 if (action_discarded & COMPLAIN)
8b127cbc 10288 (*flinfo->info->callbacks->einfo)
e1fffbe6 10289 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10290 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10291 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10292
87e5235d 10293 /* Try to do the best we can to support buggy old
e0ae6d6f 10294 versions of gcc. Pretend that the symbol is
87e5235d
AM
10295 really defined in the kept linkonce section.
10296 FIXME: This is quite broken. Modifying the
10297 symbol here means we will be changing all later
e0ae6d6f 10298 uses of the symbol, not just in this section. */
0f02bbd9 10299 if (action_discarded & PRETEND)
87e5235d 10300 {
01b3c8ab
L
10301 asection *kept;
10302
c0f00686 10303 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10304 flinfo->info);
01b3c8ab 10305 if (kept != NULL)
87e5235d
AM
10306 {
10307 *ps = kept;
10308 continue;
10309 }
10310 }
c152c796
AM
10311 }
10312 }
10313 }
10314
10315 /* Relocate the section by invoking a back end routine.
10316
10317 The back end routine is responsible for adjusting the
10318 section contents as necessary, and (if using Rela relocs
10319 and generating a relocatable output file) adjusting the
10320 reloc addend as necessary.
10321
10322 The back end routine does not have to worry about setting
10323 the reloc address or the reloc symbol index.
10324
10325 The back end routine is given a pointer to the swapped in
10326 internal symbols, and can access the hash table entries
10327 for the external symbols via elf_sym_hashes (input_bfd).
10328
10329 When generating relocatable output, the back end routine
10330 must handle STB_LOCAL/STT_SECTION symbols specially. The
10331 output symbol is going to be a section symbol
10332 corresponding to the output section, which will require
10333 the addend to be adjusted. */
10334
8b127cbc 10335 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10336 input_bfd, o, contents,
10337 internal_relocs,
10338 isymbuf,
8b127cbc 10339 flinfo->sections);
ece5ef60 10340 if (!ret)
c152c796
AM
10341 return FALSE;
10342
ece5ef60 10343 if (ret == 2
0e1862bb 10344 || bfd_link_relocatable (flinfo->info)
8b127cbc 10345 || flinfo->info->emitrelocations)
c152c796
AM
10346 {
10347 Elf_Internal_Rela *irela;
d4730f92 10348 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10349 bfd_vma last_offset;
10350 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10351 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10352 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10353 unsigned int next_erel;
c152c796 10354 bfd_boolean rela_normal;
d4730f92 10355 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10356
d4730f92
BS
10357 esdi = elf_section_data (o);
10358 esdo = elf_section_data (o->output_section);
10359 rela_normal = FALSE;
c152c796
AM
10360
10361 /* Adjust the reloc addresses and symbol indices. */
10362
10363 irela = internal_relocs;
10364 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10365 rel_hash = esdo->rel.hashes + esdo->rel.count;
10366 /* We start processing the REL relocs, if any. When we reach
10367 IRELAMID in the loop, we switch to the RELA relocs. */
10368 irelamid = irela;
10369 if (esdi->rel.hdr != NULL)
10370 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10371 * bed->s->int_rels_per_ext_rel);
eac338cf 10372 rel_hash_list = rel_hash;
d4730f92 10373 rela_hash_list = NULL;
c152c796 10374 last_offset = o->output_offset;
0e1862bb 10375 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10376 last_offset += o->output_section->vma;
10377 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10378 {
10379 unsigned long r_symndx;
10380 asection *sec;
10381 Elf_Internal_Sym sym;
10382
10383 if (next_erel == bed->s->int_rels_per_ext_rel)
10384 {
10385 rel_hash++;
10386 next_erel = 0;
10387 }
10388
d4730f92
BS
10389 if (irela == irelamid)
10390 {
10391 rel_hash = esdo->rela.hashes + esdo->rela.count;
10392 rela_hash_list = rel_hash;
10393 rela_normal = bed->rela_normal;
10394 }
10395
c152c796 10396 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10397 flinfo->info, o,
c152c796
AM
10398 irela->r_offset);
10399 if (irela->r_offset >= (bfd_vma) -2)
10400 {
10401 /* This is a reloc for a deleted entry or somesuch.
10402 Turn it into an R_*_NONE reloc, at the same
10403 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10404 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10405 being ordered. */
10406 irela->r_offset = last_offset;
10407 irela->r_info = 0;
10408 irela->r_addend = 0;
10409 continue;
10410 }
10411
10412 irela->r_offset += o->output_offset;
10413
10414 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10415 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10416 irela->r_offset += o->output_section->vma;
10417
10418 last_offset = irela->r_offset;
10419
10420 r_symndx = irela->r_info >> r_sym_shift;
10421 if (r_symndx == STN_UNDEF)
10422 continue;
10423
10424 if (r_symndx >= locsymcount
10425 || (elf_bad_symtab (input_bfd)
8b127cbc 10426 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10427 {
10428 struct elf_link_hash_entry *rh;
10429 unsigned long indx;
10430
10431 /* This is a reloc against a global symbol. We
10432 have not yet output all the local symbols, so
10433 we do not know the symbol index of any global
10434 symbol. We set the rel_hash entry for this
10435 reloc to point to the global hash table entry
10436 for this symbol. The symbol index is then
ee75fd95 10437 set at the end of bfd_elf_final_link. */
c152c796
AM
10438 indx = r_symndx - extsymoff;
10439 rh = elf_sym_hashes (input_bfd)[indx];
10440 while (rh->root.type == bfd_link_hash_indirect
10441 || rh->root.type == bfd_link_hash_warning)
10442 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10443
10444 /* Setting the index to -2 tells
10445 elf_link_output_extsym that this symbol is
10446 used by a reloc. */
10447 BFD_ASSERT (rh->indx < 0);
10448 rh->indx = -2;
10449
10450 *rel_hash = rh;
10451
10452 continue;
10453 }
10454
10455 /* This is a reloc against a local symbol. */
10456
10457 *rel_hash = NULL;
10458 sym = isymbuf[r_symndx];
8b127cbc 10459 sec = flinfo->sections[r_symndx];
c152c796
AM
10460 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10461 {
10462 /* I suppose the backend ought to fill in the
10463 section of any STT_SECTION symbol against a
6a8d1586 10464 processor specific section. */
cf35638d 10465 r_symndx = STN_UNDEF;
6a8d1586
AM
10466 if (bfd_is_abs_section (sec))
10467 ;
c152c796
AM
10468 else if (sec == NULL || sec->owner == NULL)
10469 {
10470 bfd_set_error (bfd_error_bad_value);
10471 return FALSE;
10472 }
10473 else
10474 {
6a8d1586
AM
10475 asection *osec = sec->output_section;
10476
10477 /* If we have discarded a section, the output
10478 section will be the absolute section. In
ab96bf03
AM
10479 case of discarded SEC_MERGE sections, use
10480 the kept section. relocate_section should
10481 have already handled discarded linkonce
10482 sections. */
6a8d1586
AM
10483 if (bfd_is_abs_section (osec)
10484 && sec->kept_section != NULL
10485 && sec->kept_section->output_section != NULL)
10486 {
10487 osec = sec->kept_section->output_section;
10488 irela->r_addend -= osec->vma;
10489 }
10490
10491 if (!bfd_is_abs_section (osec))
10492 {
10493 r_symndx = osec->target_index;
cf35638d 10494 if (r_symndx == STN_UNDEF)
74541ad4 10495 {
051d833a
AM
10496 irela->r_addend += osec->vma;
10497 osec = _bfd_nearby_section (output_bfd, osec,
10498 osec->vma);
10499 irela->r_addend -= osec->vma;
10500 r_symndx = osec->target_index;
74541ad4 10501 }
6a8d1586 10502 }
c152c796
AM
10503 }
10504
10505 /* Adjust the addend according to where the
10506 section winds up in the output section. */
10507 if (rela_normal)
10508 irela->r_addend += sec->output_offset;
10509 }
10510 else
10511 {
8b127cbc 10512 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10513 {
10514 unsigned long shlink;
10515 const char *name;
10516 asection *osec;
6e0b88f1 10517 long indx;
c152c796 10518
8b127cbc 10519 if (flinfo->info->strip == strip_all)
c152c796
AM
10520 {
10521 /* You can't do ld -r -s. */
10522 bfd_set_error (bfd_error_invalid_operation);
10523 return FALSE;
10524 }
10525
10526 /* This symbol was skipped earlier, but
10527 since it is needed by a reloc, we
10528 must output it now. */
10529 shlink = symtab_hdr->sh_link;
10530 name = (bfd_elf_string_from_elf_section
10531 (input_bfd, shlink, sym.st_name));
10532 if (name == NULL)
10533 return FALSE;
10534
10535 osec = sec->output_section;
10536 sym.st_shndx =
10537 _bfd_elf_section_from_bfd_section (output_bfd,
10538 osec);
10539 if (sym.st_shndx == SHN_BAD)
10540 return FALSE;
10541
10542 sym.st_value += sec->output_offset;
0e1862bb 10543 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10544 {
10545 sym.st_value += osec->vma;
10546 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10547 {
10548 /* STT_TLS symbols are relative to PT_TLS
10549 segment base. */
8b127cbc 10550 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10551 ->tls_sec != NULL);
8b127cbc 10552 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10553 ->tls_sec->vma);
10554 }
10555 }
10556
6e0b88f1 10557 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10558 ret = elf_link_output_symstrtab (flinfo, name,
10559 &sym, sec,
10560 NULL);
6e0b88f1 10561 if (ret == 0)
c152c796 10562 return FALSE;
6e0b88f1 10563 else if (ret == 1)
8b127cbc 10564 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10565 else
10566 abort ();
c152c796
AM
10567 }
10568
8b127cbc 10569 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10570 }
10571
10572 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10573 | (irela->r_info & r_type_mask));
10574 }
10575
10576 /* Swap out the relocs. */
d4730f92
BS
10577 input_rel_hdr = esdi->rel.hdr;
10578 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10579 {
d4730f92
BS
10580 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10581 input_rel_hdr,
10582 internal_relocs,
10583 rel_hash_list))
10584 return FALSE;
c152c796
AM
10585 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10586 * bed->s->int_rels_per_ext_rel);
eac338cf 10587 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10588 }
10589
10590 input_rela_hdr = esdi->rela.hdr;
10591 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10592 {
eac338cf 10593 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10594 input_rela_hdr,
eac338cf 10595 internal_relocs,
d4730f92 10596 rela_hash_list))
c152c796
AM
10597 return FALSE;
10598 }
10599 }
10600 }
10601
10602 /* Write out the modified section contents. */
10603 if (bed->elf_backend_write_section
8b127cbc 10604 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10605 contents))
c152c796
AM
10606 {
10607 /* Section written out. */
10608 }
10609 else switch (o->sec_info_type)
10610 {
dbaa2011 10611 case SEC_INFO_TYPE_STABS:
c152c796
AM
10612 if (! (_bfd_write_section_stabs
10613 (output_bfd,
8b127cbc 10614 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10615 o, &elf_section_data (o)->sec_info, contents)))
10616 return FALSE;
10617 break;
dbaa2011 10618 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10619 if (! _bfd_write_merged_section (output_bfd, o,
10620 elf_section_data (o)->sec_info))
10621 return FALSE;
10622 break;
dbaa2011 10623 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10624 {
8b127cbc 10625 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10626 o, contents))
10627 return FALSE;
10628 }
10629 break;
2f0c68f2
CM
10630 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10631 {
10632 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10633 flinfo->info,
10634 o, contents))
10635 return FALSE;
10636 }
10637 break;
c152c796
AM
10638 default:
10639 {
310fd250
L
10640 if (! (o->flags & SEC_EXCLUDE))
10641 {
10642 file_ptr offset = (file_ptr) o->output_offset;
10643 bfd_size_type todo = o->size;
37b01f6a
DG
10644
10645 offset *= bfd_octets_per_byte (output_bfd);
10646
310fd250
L
10647 if ((o->flags & SEC_ELF_REVERSE_COPY))
10648 {
10649 /* Reverse-copy input section to output. */
10650 do
10651 {
10652 todo -= address_size;
10653 if (! bfd_set_section_contents (output_bfd,
10654 o->output_section,
10655 contents + todo,
10656 offset,
10657 address_size))
10658 return FALSE;
10659 if (todo == 0)
10660 break;
10661 offset += address_size;
10662 }
10663 while (1);
10664 }
10665 else if (! bfd_set_section_contents (output_bfd,
10666 o->output_section,
10667 contents,
10668 offset, todo))
10669 return FALSE;
10670 }
c152c796
AM
10671 }
10672 break;
10673 }
10674 }
10675
10676 return TRUE;
10677}
10678
10679/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10680 requested by the linker, and does not come from any input file. This
c152c796
AM
10681 is used to build constructor and destructor tables when linking
10682 with -Ur. */
10683
10684static bfd_boolean
10685elf_reloc_link_order (bfd *output_bfd,
10686 struct bfd_link_info *info,
10687 asection *output_section,
10688 struct bfd_link_order *link_order)
10689{
10690 reloc_howto_type *howto;
10691 long indx;
10692 bfd_vma offset;
10693 bfd_vma addend;
d4730f92 10694 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10695 struct elf_link_hash_entry **rel_hash_ptr;
10696 Elf_Internal_Shdr *rel_hdr;
10697 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10698 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10699 bfd_byte *erel;
10700 unsigned int i;
d4730f92 10701 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10702
10703 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10704 if (howto == NULL)
10705 {
10706 bfd_set_error (bfd_error_bad_value);
10707 return FALSE;
10708 }
10709
10710 addend = link_order->u.reloc.p->addend;
10711
d4730f92
BS
10712 if (esdo->rel.hdr)
10713 reldata = &esdo->rel;
10714 else if (esdo->rela.hdr)
10715 reldata = &esdo->rela;
10716 else
10717 {
10718 reldata = NULL;
10719 BFD_ASSERT (0);
10720 }
10721
c152c796 10722 /* Figure out the symbol index. */
d4730f92 10723 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10724 if (link_order->type == bfd_section_reloc_link_order)
10725 {
10726 indx = link_order->u.reloc.p->u.section->target_index;
10727 BFD_ASSERT (indx != 0);
10728 *rel_hash_ptr = NULL;
10729 }
10730 else
10731 {
10732 struct elf_link_hash_entry *h;
10733
10734 /* Treat a reloc against a defined symbol as though it were
10735 actually against the section. */
10736 h = ((struct elf_link_hash_entry *)
10737 bfd_wrapped_link_hash_lookup (output_bfd, info,
10738 link_order->u.reloc.p->u.name,
10739 FALSE, FALSE, TRUE));
10740 if (h != NULL
10741 && (h->root.type == bfd_link_hash_defined
10742 || h->root.type == bfd_link_hash_defweak))
10743 {
10744 asection *section;
10745
10746 section = h->root.u.def.section;
10747 indx = section->output_section->target_index;
10748 *rel_hash_ptr = NULL;
10749 /* It seems that we ought to add the symbol value to the
10750 addend here, but in practice it has already been added
10751 because it was passed to constructor_callback. */
10752 addend += section->output_section->vma + section->output_offset;
10753 }
10754 else if (h != NULL)
10755 {
10756 /* Setting the index to -2 tells elf_link_output_extsym that
10757 this symbol is used by a reloc. */
10758 h->indx = -2;
10759 *rel_hash_ptr = h;
10760 indx = 0;
10761 }
10762 else
10763 {
10764 if (! ((*info->callbacks->unattached_reloc)
10765 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10766 return FALSE;
10767 indx = 0;
10768 }
10769 }
10770
10771 /* If this is an inplace reloc, we must write the addend into the
10772 object file. */
10773 if (howto->partial_inplace && addend != 0)
10774 {
10775 bfd_size_type size;
10776 bfd_reloc_status_type rstat;
10777 bfd_byte *buf;
10778 bfd_boolean ok;
10779 const char *sym_name;
10780
a50b1753
NC
10781 size = (bfd_size_type) bfd_get_reloc_size (howto);
10782 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10783 if (buf == NULL && size != 0)
c152c796
AM
10784 return FALSE;
10785 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10786 switch (rstat)
10787 {
10788 case bfd_reloc_ok:
10789 break;
10790
10791 default:
10792 case bfd_reloc_outofrange:
10793 abort ();
10794
10795 case bfd_reloc_overflow:
10796 if (link_order->type == bfd_section_reloc_link_order)
10797 sym_name = bfd_section_name (output_bfd,
10798 link_order->u.reloc.p->u.section);
10799 else
10800 sym_name = link_order->u.reloc.p->u.name;
10801 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
10802 (info, NULL, sym_name, howto->name, addend, NULL,
10803 NULL, (bfd_vma) 0)))
c152c796
AM
10804 {
10805 free (buf);
10806 return FALSE;
10807 }
10808 break;
10809 }
37b01f6a 10810
c152c796 10811 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
10812 link_order->offset
10813 * bfd_octets_per_byte (output_bfd),
10814 size);
c152c796
AM
10815 free (buf);
10816 if (! ok)
10817 return FALSE;
10818 }
10819
10820 /* The address of a reloc is relative to the section in a
10821 relocatable file, and is a virtual address in an executable
10822 file. */
10823 offset = link_order->offset;
0e1862bb 10824 if (! bfd_link_relocatable (info))
c152c796
AM
10825 offset += output_section->vma;
10826
10827 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10828 {
10829 irel[i].r_offset = offset;
10830 irel[i].r_info = 0;
10831 irel[i].r_addend = 0;
10832 }
10833 if (bed->s->arch_size == 32)
10834 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10835 else
10836 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10837
d4730f92 10838 rel_hdr = reldata->hdr;
c152c796
AM
10839 erel = rel_hdr->contents;
10840 if (rel_hdr->sh_type == SHT_REL)
10841 {
d4730f92 10842 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10843 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10844 }
10845 else
10846 {
10847 irel[0].r_addend = addend;
d4730f92 10848 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10849 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10850 }
10851
d4730f92 10852 ++reldata->count;
c152c796
AM
10853
10854 return TRUE;
10855}
10856
0b52efa6
PB
10857
10858/* Get the output vma of the section pointed to by the sh_link field. */
10859
10860static bfd_vma
10861elf_get_linked_section_vma (struct bfd_link_order *p)
10862{
10863 Elf_Internal_Shdr **elf_shdrp;
10864 asection *s;
10865 int elfsec;
10866
10867 s = p->u.indirect.section;
10868 elf_shdrp = elf_elfsections (s->owner);
10869 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10870 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10871 /* PR 290:
10872 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10873 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10874 sh_info fields. Hence we could get the situation
10875 where elfsec is 0. */
10876 if (elfsec == 0)
10877 {
10878 const struct elf_backend_data *bed
10879 = get_elf_backend_data (s->owner);
10880 if (bed->link_order_error_handler)
d003868e
AM
10881 bed->link_order_error_handler
10882 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10883 return 0;
10884 }
10885 else
10886 {
10887 s = elf_shdrp[elfsec]->bfd_section;
10888 return s->output_section->vma + s->output_offset;
10889 }
0b52efa6
PB
10890}
10891
10892
10893/* Compare two sections based on the locations of the sections they are
10894 linked to. Used by elf_fixup_link_order. */
10895
10896static int
10897compare_link_order (const void * a, const void * b)
10898{
10899 bfd_vma apos;
10900 bfd_vma bpos;
10901
10902 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10903 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10904 if (apos < bpos)
10905 return -1;
10906 return apos > bpos;
10907}
10908
10909
10910/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10911 order as their linked sections. Returns false if this could not be done
10912 because an output section includes both ordered and unordered
10913 sections. Ideally we'd do this in the linker proper. */
10914
10915static bfd_boolean
10916elf_fixup_link_order (bfd *abfd, asection *o)
10917{
10918 int seen_linkorder;
10919 int seen_other;
10920 int n;
10921 struct bfd_link_order *p;
10922 bfd *sub;
10923 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10924 unsigned elfsec;
0b52efa6 10925 struct bfd_link_order **sections;
d33cdfe3 10926 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10927 bfd_vma offset;
3b36f7e6 10928
d33cdfe3
L
10929 other_sec = NULL;
10930 linkorder_sec = NULL;
0b52efa6
PB
10931 seen_other = 0;
10932 seen_linkorder = 0;
8423293d 10933 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10934 {
d33cdfe3 10935 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10936 {
10937 s = p->u.indirect.section;
d33cdfe3
L
10938 sub = s->owner;
10939 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10940 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10941 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10942 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10943 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10944 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10945 {
10946 seen_linkorder++;
10947 linkorder_sec = s;
10948 }
0b52efa6 10949 else
d33cdfe3
L
10950 {
10951 seen_other++;
10952 other_sec = s;
10953 }
0b52efa6
PB
10954 }
10955 else
10956 seen_other++;
d33cdfe3
L
10957
10958 if (seen_other && seen_linkorder)
10959 {
10960 if (other_sec && linkorder_sec)
10961 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10962 o, linkorder_sec,
10963 linkorder_sec->owner, other_sec,
10964 other_sec->owner);
10965 else
10966 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10967 o);
10968 bfd_set_error (bfd_error_bad_value);
10969 return FALSE;
10970 }
0b52efa6
PB
10971 }
10972
10973 if (!seen_linkorder)
10974 return TRUE;
10975
0b52efa6 10976 sections = (struct bfd_link_order **)
14b1c01e
AM
10977 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10978 if (sections == NULL)
10979 return FALSE;
0b52efa6 10980 seen_linkorder = 0;
3b36f7e6 10981
8423293d 10982 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
10983 {
10984 sections[seen_linkorder++] = p;
10985 }
10986 /* Sort the input sections in the order of their linked section. */
10987 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10988 compare_link_order);
10989
10990 /* Change the offsets of the sections. */
10991 offset = 0;
10992 for (n = 0; n < seen_linkorder; n++)
10993 {
10994 s = sections[n]->u.indirect.section;
461686a3 10995 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 10996 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
10997 sections[n]->offset = offset;
10998 offset += sections[n]->size;
10999 }
11000
4dd07732 11001 free (sections);
0b52efa6
PB
11002 return TRUE;
11003}
11004
9f7c3e5e
AM
11005static void
11006elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11007{
11008 asection *o;
11009
11010 if (flinfo->symstrtab != NULL)
ef10c3ac 11011 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11012 if (flinfo->contents != NULL)
11013 free (flinfo->contents);
11014 if (flinfo->external_relocs != NULL)
11015 free (flinfo->external_relocs);
11016 if (flinfo->internal_relocs != NULL)
11017 free (flinfo->internal_relocs);
11018 if (flinfo->external_syms != NULL)
11019 free (flinfo->external_syms);
11020 if (flinfo->locsym_shndx != NULL)
11021 free (flinfo->locsym_shndx);
11022 if (flinfo->internal_syms != NULL)
11023 free (flinfo->internal_syms);
11024 if (flinfo->indices != NULL)
11025 free (flinfo->indices);
11026 if (flinfo->sections != NULL)
11027 free (flinfo->sections);
9f7c3e5e
AM
11028 if (flinfo->symshndxbuf != NULL)
11029 free (flinfo->symshndxbuf);
11030 for (o = obfd->sections; o != NULL; o = o->next)
11031 {
11032 struct bfd_elf_section_data *esdo = elf_section_data (o);
11033 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11034 free (esdo->rel.hashes);
11035 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11036 free (esdo->rela.hashes);
11037 }
11038}
0b52efa6 11039
c152c796
AM
11040/* Do the final step of an ELF link. */
11041
11042bfd_boolean
11043bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11044{
11045 bfd_boolean dynamic;
11046 bfd_boolean emit_relocs;
11047 bfd *dynobj;
8b127cbc 11048 struct elf_final_link_info flinfo;
91d6fa6a
NC
11049 asection *o;
11050 struct bfd_link_order *p;
11051 bfd *sub;
c152c796
AM
11052 bfd_size_type max_contents_size;
11053 bfd_size_type max_external_reloc_size;
11054 bfd_size_type max_internal_reloc_count;
11055 bfd_size_type max_sym_count;
11056 bfd_size_type max_sym_shndx_count;
c152c796
AM
11057 Elf_Internal_Sym elfsym;
11058 unsigned int i;
11059 Elf_Internal_Shdr *symtab_hdr;
11060 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11061 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11062 struct elf_outext_info eoinfo;
11063 bfd_boolean merged;
11064 size_t relativecount = 0;
11065 asection *reldyn = 0;
11066 bfd_size_type amt;
104d59d1
JM
11067 asection *attr_section = NULL;
11068 bfd_vma attr_size = 0;
11069 const char *std_attrs_section;
c152c796
AM
11070
11071 if (! is_elf_hash_table (info->hash))
11072 return FALSE;
11073
0e1862bb 11074 if (bfd_link_pic (info))
c152c796
AM
11075 abfd->flags |= DYNAMIC;
11076
11077 dynamic = elf_hash_table (info)->dynamic_sections_created;
11078 dynobj = elf_hash_table (info)->dynobj;
11079
0e1862bb 11080 emit_relocs = (bfd_link_relocatable (info)
a4676736 11081 || info->emitrelocations);
c152c796 11082
8b127cbc
AM
11083 flinfo.info = info;
11084 flinfo.output_bfd = abfd;
ef10c3ac 11085 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11086 if (flinfo.symstrtab == NULL)
c152c796
AM
11087 return FALSE;
11088
11089 if (! dynamic)
11090 {
8b127cbc
AM
11091 flinfo.hash_sec = NULL;
11092 flinfo.symver_sec = NULL;
c152c796
AM
11093 }
11094 else
11095 {
3d4d4302 11096 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11097 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11098 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11099 /* Note that it is OK if symver_sec is NULL. */
11100 }
11101
8b127cbc
AM
11102 flinfo.contents = NULL;
11103 flinfo.external_relocs = NULL;
11104 flinfo.internal_relocs = NULL;
11105 flinfo.external_syms = NULL;
11106 flinfo.locsym_shndx = NULL;
11107 flinfo.internal_syms = NULL;
11108 flinfo.indices = NULL;
11109 flinfo.sections = NULL;
8b127cbc 11110 flinfo.symshndxbuf = NULL;
ffbc01cc 11111 flinfo.filesym_count = 0;
c152c796 11112
104d59d1
JM
11113 /* The object attributes have been merged. Remove the input
11114 sections from the link, and set the contents of the output
11115 secton. */
11116 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11117 for (o = abfd->sections; o != NULL; o = o->next)
11118 {
11119 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11120 || strcmp (o->name, ".gnu.attributes") == 0)
11121 {
11122 for (p = o->map_head.link_order; p != NULL; p = p->next)
11123 {
11124 asection *input_section;
11125
11126 if (p->type != bfd_indirect_link_order)
11127 continue;
11128 input_section = p->u.indirect.section;
11129 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11130 elf_link_input_bfd ignores this section. */
11131 input_section->flags &= ~SEC_HAS_CONTENTS;
11132 }
a0c8462f 11133
104d59d1
JM
11134 attr_size = bfd_elf_obj_attr_size (abfd);
11135 if (attr_size)
11136 {
11137 bfd_set_section_size (abfd, o, attr_size);
11138 attr_section = o;
11139 /* Skip this section later on. */
11140 o->map_head.link_order = NULL;
11141 }
11142 else
11143 o->flags |= SEC_EXCLUDE;
11144 }
11145 }
11146
c152c796
AM
11147 /* Count up the number of relocations we will output for each output
11148 section, so that we know the sizes of the reloc sections. We
11149 also figure out some maximum sizes. */
11150 max_contents_size = 0;
11151 max_external_reloc_size = 0;
11152 max_internal_reloc_count = 0;
11153 max_sym_count = 0;
11154 max_sym_shndx_count = 0;
11155 merged = FALSE;
11156 for (o = abfd->sections; o != NULL; o = o->next)
11157 {
11158 struct bfd_elf_section_data *esdo = elf_section_data (o);
11159 o->reloc_count = 0;
11160
8423293d 11161 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11162 {
11163 unsigned int reloc_count = 0;
491d01d3 11164 unsigned int additional_reloc_count = 0;
c152c796 11165 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11166
11167 if (p->type == bfd_section_reloc_link_order
11168 || p->type == bfd_symbol_reloc_link_order)
11169 reloc_count = 1;
11170 else if (p->type == bfd_indirect_link_order)
11171 {
11172 asection *sec;
11173
11174 sec = p->u.indirect.section;
11175 esdi = elf_section_data (sec);
11176
11177 /* Mark all sections which are to be included in the
11178 link. This will normally be every section. We need
11179 to do this so that we can identify any sections which
11180 the linker has decided to not include. */
11181 sec->linker_mark = TRUE;
11182
11183 if (sec->flags & SEC_MERGE)
11184 merged = TRUE;
11185
aed64b35
L
11186 if (esdo->this_hdr.sh_type == SHT_REL
11187 || esdo->this_hdr.sh_type == SHT_RELA)
11188 /* Some backends use reloc_count in relocation sections
11189 to count particular types of relocs. Of course,
11190 reloc sections themselves can't have relocations. */
11191 reloc_count = 0;
0e1862bb 11192 else if (emit_relocs)
491d01d3
YU
11193 {
11194 reloc_count = sec->reloc_count;
11195 if (bed->elf_backend_count_additional_relocs)
11196 {
11197 int c;
11198 c = (*bed->elf_backend_count_additional_relocs) (sec);
11199 additional_reloc_count += c;
11200 }
11201 }
c152c796 11202 else if (bed->elf_backend_count_relocs)
58217f29 11203 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 11204
eea6121a
AM
11205 if (sec->rawsize > max_contents_size)
11206 max_contents_size = sec->rawsize;
11207 if (sec->size > max_contents_size)
11208 max_contents_size = sec->size;
c152c796
AM
11209
11210 /* We are interested in just local symbols, not all
11211 symbols. */
11212 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11213 && (sec->owner->flags & DYNAMIC) == 0)
11214 {
11215 size_t sym_count;
11216
11217 if (elf_bad_symtab (sec->owner))
11218 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11219 / bed->s->sizeof_sym);
11220 else
11221 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11222
11223 if (sym_count > max_sym_count)
11224 max_sym_count = sym_count;
11225
11226 if (sym_count > max_sym_shndx_count
6a40cf0c 11227 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11228 max_sym_shndx_count = sym_count;
11229
11230 if ((sec->flags & SEC_RELOC) != 0)
11231 {
d4730f92 11232 size_t ext_size = 0;
c152c796 11233
d4730f92
BS
11234 if (esdi->rel.hdr != NULL)
11235 ext_size = esdi->rel.hdr->sh_size;
11236 if (esdi->rela.hdr != NULL)
11237 ext_size += esdi->rela.hdr->sh_size;
7326c758 11238
c152c796
AM
11239 if (ext_size > max_external_reloc_size)
11240 max_external_reloc_size = ext_size;
11241 if (sec->reloc_count > max_internal_reloc_count)
11242 max_internal_reloc_count = sec->reloc_count;
11243 }
11244 }
11245 }
11246
11247 if (reloc_count == 0)
11248 continue;
11249
491d01d3 11250 reloc_count += additional_reloc_count;
c152c796
AM
11251 o->reloc_count += reloc_count;
11252
0e1862bb 11253 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11254 {
d4730f92 11255 if (esdi->rel.hdr)
491d01d3
YU
11256 {
11257 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11258 esdo->rel.count += additional_reloc_count;
11259 }
d4730f92 11260 if (esdi->rela.hdr)
491d01d3
YU
11261 {
11262 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11263 esdo->rela.count += additional_reloc_count;
11264 }
d4730f92
BS
11265 }
11266 else
11267 {
11268 if (o->use_rela_p)
11269 esdo->rela.count += reloc_count;
2c2b4ed4 11270 else
d4730f92 11271 esdo->rel.count += reloc_count;
c152c796 11272 }
c152c796
AM
11273 }
11274
11275 if (o->reloc_count > 0)
11276 o->flags |= SEC_RELOC;
11277 else
11278 {
11279 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11280 set it (this is probably a bug) and if it is set
11281 assign_section_numbers will create a reloc section. */
11282 o->flags &=~ SEC_RELOC;
11283 }
11284
11285 /* If the SEC_ALLOC flag is not set, force the section VMA to
11286 zero. This is done in elf_fake_sections as well, but forcing
11287 the VMA to 0 here will ensure that relocs against these
11288 sections are handled correctly. */
11289 if ((o->flags & SEC_ALLOC) == 0
11290 && ! o->user_set_vma)
11291 o->vma = 0;
11292 }
11293
0e1862bb 11294 if (! bfd_link_relocatable (info) && merged)
c152c796
AM
11295 elf_link_hash_traverse (elf_hash_table (info),
11296 _bfd_elf_link_sec_merge_syms, abfd);
11297
11298 /* Figure out the file positions for everything but the symbol table
11299 and the relocs. We set symcount to force assign_section_numbers
11300 to create a symbol table. */
8539e4e8 11301 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11302 BFD_ASSERT (! abfd->output_has_begun);
11303 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11304 goto error_return;
11305
ee75fd95 11306 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11307 for (o = abfd->sections; o != NULL; o = o->next)
11308 {
d4730f92 11309 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11310 if ((o->flags & SEC_RELOC) != 0)
11311 {
d4730f92
BS
11312 if (esdo->rel.hdr
11313 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11314 goto error_return;
11315
d4730f92
BS
11316 if (esdo->rela.hdr
11317 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11318 goto error_return;
11319 }
11320
11321 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11322 to count upwards while actually outputting the relocations. */
d4730f92
BS
11323 esdo->rel.count = 0;
11324 esdo->rela.count = 0;
0ce398f1
L
11325
11326 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11327 {
11328 /* Cache the section contents so that they can be compressed
11329 later. Use bfd_malloc since it will be freed by
11330 bfd_compress_section_contents. */
11331 unsigned char *contents = esdo->this_hdr.contents;
11332 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11333 abort ();
11334 contents
11335 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11336 if (contents == NULL)
11337 goto error_return;
11338 esdo->this_hdr.contents = contents;
11339 }
c152c796
AM
11340 }
11341
c152c796 11342 /* We have now assigned file positions for all the sections except
a485e98e
AM
11343 .symtab, .strtab, and non-loaded reloc sections. We start the
11344 .symtab section at the current file position, and write directly
11345 to it. We build the .strtab section in memory. */
c152c796
AM
11346 bfd_get_symcount (abfd) = 0;
11347 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11348 /* sh_name is set in prep_headers. */
11349 symtab_hdr->sh_type = SHT_SYMTAB;
11350 /* sh_flags, sh_addr and sh_size all start off zero. */
11351 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11352 /* sh_link is set in assign_section_numbers. */
11353 /* sh_info is set below. */
11354 /* sh_offset is set just below. */
72de5009 11355 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11356
ef10c3ac
L
11357 if (max_sym_count < 20)
11358 max_sym_count = 20;
11359 elf_hash_table (info)->strtabsize = max_sym_count;
11360 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11361 elf_hash_table (info)->strtab
11362 = (struct elf_sym_strtab *) bfd_malloc (amt);
11363 if (elf_hash_table (info)->strtab == NULL)
c152c796 11364 goto error_return;
ef10c3ac
L
11365 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11366 flinfo.symshndxbuf
11367 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11368 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11369
8539e4e8 11370 if (info->strip != strip_all || emit_relocs)
c152c796 11371 {
8539e4e8
AM
11372 file_ptr off = elf_next_file_pos (abfd);
11373
11374 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11375
11376 /* Note that at this point elf_next_file_pos (abfd) is
11377 incorrect. We do not yet know the size of the .symtab section.
11378 We correct next_file_pos below, after we do know the size. */
11379
11380 /* Start writing out the symbol table. The first symbol is always a
11381 dummy symbol. */
c152c796
AM
11382 elfsym.st_value = 0;
11383 elfsym.st_size = 0;
11384 elfsym.st_info = 0;
11385 elfsym.st_other = 0;
11386 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11387 elfsym.st_target_internal = 0;
ef10c3ac
L
11388 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11389 bfd_und_section_ptr, NULL) != 1)
c152c796 11390 goto error_return;
c152c796 11391
8539e4e8
AM
11392 /* Output a symbol for each section. We output these even if we are
11393 discarding local symbols, since they are used for relocs. These
11394 symbols have no names. We store the index of each one in the
11395 index field of the section, so that we can find it again when
11396 outputting relocs. */
11397
c152c796
AM
11398 elfsym.st_size = 0;
11399 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11400 elfsym.st_other = 0;
f0b5bb34 11401 elfsym.st_value = 0;
35fc36a8 11402 elfsym.st_target_internal = 0;
c152c796
AM
11403 for (i = 1; i < elf_numsections (abfd); i++)
11404 {
11405 o = bfd_section_from_elf_index (abfd, i);
11406 if (o != NULL)
f0b5bb34
AM
11407 {
11408 o->target_index = bfd_get_symcount (abfd);
11409 elfsym.st_shndx = i;
0e1862bb 11410 if (!bfd_link_relocatable (info))
f0b5bb34 11411 elfsym.st_value = o->vma;
ef10c3ac
L
11412 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11413 NULL) != 1)
f0b5bb34
AM
11414 goto error_return;
11415 }
c152c796
AM
11416 }
11417 }
11418
11419 /* Allocate some memory to hold information read in from the input
11420 files. */
11421 if (max_contents_size != 0)
11422 {
8b127cbc
AM
11423 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11424 if (flinfo.contents == NULL)
c152c796
AM
11425 goto error_return;
11426 }
11427
11428 if (max_external_reloc_size != 0)
11429 {
8b127cbc
AM
11430 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11431 if (flinfo.external_relocs == NULL)
c152c796
AM
11432 goto error_return;
11433 }
11434
11435 if (max_internal_reloc_count != 0)
11436 {
11437 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11438 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11439 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11440 if (flinfo.internal_relocs == NULL)
c152c796
AM
11441 goto error_return;
11442 }
11443
11444 if (max_sym_count != 0)
11445 {
11446 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11447 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11448 if (flinfo.external_syms == NULL)
c152c796
AM
11449 goto error_return;
11450
11451 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11452 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11453 if (flinfo.internal_syms == NULL)
c152c796
AM
11454 goto error_return;
11455
11456 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11457 flinfo.indices = (long int *) bfd_malloc (amt);
11458 if (flinfo.indices == NULL)
c152c796
AM
11459 goto error_return;
11460
11461 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11462 flinfo.sections = (asection **) bfd_malloc (amt);
11463 if (flinfo.sections == NULL)
c152c796
AM
11464 goto error_return;
11465 }
11466
11467 if (max_sym_shndx_count != 0)
11468 {
11469 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11470 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11471 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11472 goto error_return;
11473 }
11474
11475 if (elf_hash_table (info)->tls_sec)
11476 {
11477 bfd_vma base, end = 0;
11478 asection *sec;
11479
11480 for (sec = elf_hash_table (info)->tls_sec;
11481 sec && (sec->flags & SEC_THREAD_LOCAL);
11482 sec = sec->next)
11483 {
3a800eb9 11484 bfd_size_type size = sec->size;
c152c796 11485
3a800eb9
AM
11486 if (size == 0
11487 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11488 {
91d6fa6a
NC
11489 struct bfd_link_order *ord = sec->map_tail.link_order;
11490
11491 if (ord != NULL)
11492 size = ord->offset + ord->size;
c152c796
AM
11493 }
11494 end = sec->vma + size;
11495 }
11496 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
11497 /* Only align end of TLS section if static TLS doesn't have special
11498 alignment requirements. */
11499 if (bed->static_tls_alignment == 1)
11500 end = align_power (end,
11501 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
11502 elf_hash_table (info)->tls_size = end - base;
11503 }
11504
0b52efa6
PB
11505 /* Reorder SHF_LINK_ORDER sections. */
11506 for (o = abfd->sections; o != NULL; o = o->next)
11507 {
11508 if (!elf_fixup_link_order (abfd, o))
11509 return FALSE;
11510 }
11511
2f0c68f2
CM
11512 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11513 return FALSE;
11514
c152c796
AM
11515 /* Since ELF permits relocations to be against local symbols, we
11516 must have the local symbols available when we do the relocations.
11517 Since we would rather only read the local symbols once, and we
11518 would rather not keep them in memory, we handle all the
11519 relocations for a single input file at the same time.
11520
11521 Unfortunately, there is no way to know the total number of local
11522 symbols until we have seen all of them, and the local symbol
11523 indices precede the global symbol indices. This means that when
11524 we are generating relocatable output, and we see a reloc against
11525 a global symbol, we can not know the symbol index until we have
11526 finished examining all the local symbols to see which ones we are
11527 going to output. To deal with this, we keep the relocations in
11528 memory, and don't output them until the end of the link. This is
11529 an unfortunate waste of memory, but I don't see a good way around
11530 it. Fortunately, it only happens when performing a relocatable
11531 link, which is not the common case. FIXME: If keep_memory is set
11532 we could write the relocs out and then read them again; I don't
11533 know how bad the memory loss will be. */
11534
c72f2fb2 11535 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11536 sub->output_has_begun = FALSE;
11537 for (o = abfd->sections; o != NULL; o = o->next)
11538 {
8423293d 11539 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11540 {
11541 if (p->type == bfd_indirect_link_order
11542 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11543 == bfd_target_elf_flavour)
11544 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11545 {
11546 if (! sub->output_has_begun)
11547 {
8b127cbc 11548 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11549 goto error_return;
11550 sub->output_has_begun = TRUE;
11551 }
11552 }
11553 else if (p->type == bfd_section_reloc_link_order
11554 || p->type == bfd_symbol_reloc_link_order)
11555 {
11556 if (! elf_reloc_link_order (abfd, info, o, p))
11557 goto error_return;
11558 }
11559 else
11560 {
11561 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11562 {
11563 if (p->type == bfd_indirect_link_order
11564 && (bfd_get_flavour (sub)
11565 == bfd_target_elf_flavour)
11566 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11567 != bed->s->elfclass))
11568 {
11569 const char *iclass, *oclass;
11570
aebf9be7 11571 switch (bed->s->elfclass)
351f65ca 11572 {
aebf9be7
NC
11573 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11574 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11575 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11576 default: abort ();
351f65ca 11577 }
aebf9be7
NC
11578
11579 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11580 {
aebf9be7
NC
11581 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11582 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11583 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11584 default: abort ();
351f65ca
L
11585 }
11586
11587 bfd_set_error (bfd_error_wrong_format);
11588 (*_bfd_error_handler)
11589 (_("%B: file class %s incompatible with %s"),
11590 sub, iclass, oclass);
11591 }
11592
11593 goto error_return;
11594 }
c152c796
AM
11595 }
11596 }
11597 }
11598
c0f00686
L
11599 /* Free symbol buffer if needed. */
11600 if (!info->reduce_memory_overheads)
11601 {
c72f2fb2 11602 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11603 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11604 && elf_tdata (sub)->symbuf)
c0f00686
L
11605 {
11606 free (elf_tdata (sub)->symbuf);
11607 elf_tdata (sub)->symbuf = NULL;
11608 }
11609 }
11610
c152c796
AM
11611 /* Output any global symbols that got converted to local in a
11612 version script or due to symbol visibility. We do this in a
11613 separate step since ELF requires all local symbols to appear
11614 prior to any global symbols. FIXME: We should only do this if
11615 some global symbols were, in fact, converted to become local.
11616 FIXME: Will this work correctly with the Irix 5 linker? */
11617 eoinfo.failed = FALSE;
8b127cbc 11618 eoinfo.flinfo = &flinfo;
c152c796 11619 eoinfo.localsyms = TRUE;
34a79995 11620 eoinfo.file_sym_done = FALSE;
7686d77d 11621 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11622 if (eoinfo.failed)
11623 return FALSE;
11624
4e617b1e
PB
11625 /* If backend needs to output some local symbols not present in the hash
11626 table, do it now. */
8539e4e8
AM
11627 if (bed->elf_backend_output_arch_local_syms
11628 && (info->strip != strip_all || emit_relocs))
4e617b1e 11629 {
6e0b88f1 11630 typedef int (*out_sym_func)
4e617b1e
PB
11631 (void *, const char *, Elf_Internal_Sym *, asection *,
11632 struct elf_link_hash_entry *);
11633
11634 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11635 (abfd, info, &flinfo,
11636 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11637 return FALSE;
11638 }
11639
c152c796
AM
11640 /* That wrote out all the local symbols. Finish up the symbol table
11641 with the global symbols. Even if we want to strip everything we
11642 can, we still need to deal with those global symbols that got
11643 converted to local in a version script. */
11644
11645 /* The sh_info field records the index of the first non local symbol. */
11646 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11647
11648 if (dynamic
cae1fbbb
L
11649 && elf_hash_table (info)->dynsym != NULL
11650 && (elf_hash_table (info)->dynsym->output_section
11651 != bfd_abs_section_ptr))
c152c796
AM
11652 {
11653 Elf_Internal_Sym sym;
cae1fbbb 11654 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
c152c796
AM
11655 long last_local = 0;
11656
11657 /* Write out the section symbols for the output sections. */
0e1862bb
L
11658 if (bfd_link_pic (info)
11659 || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
11660 {
11661 asection *s;
11662
11663 sym.st_size = 0;
11664 sym.st_name = 0;
11665 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11666 sym.st_other = 0;
35fc36a8 11667 sym.st_target_internal = 0;
c152c796
AM
11668
11669 for (s = abfd->sections; s != NULL; s = s->next)
11670 {
11671 int indx;
11672 bfd_byte *dest;
11673 long dynindx;
11674
c152c796 11675 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11676 if (dynindx <= 0)
11677 continue;
11678 indx = elf_section_data (s)->this_idx;
c152c796
AM
11679 BFD_ASSERT (indx > 0);
11680 sym.st_shndx = indx;
c0d5a53d
L
11681 if (! check_dynsym (abfd, &sym))
11682 return FALSE;
c152c796
AM
11683 sym.st_value = s->vma;
11684 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
11685 if (last_local < dynindx)
11686 last_local = dynindx;
c152c796
AM
11687 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11688 }
c152c796
AM
11689 }
11690
11691 /* Write out the local dynsyms. */
11692 if (elf_hash_table (info)->dynlocal)
11693 {
11694 struct elf_link_local_dynamic_entry *e;
11695 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11696 {
11697 asection *s;
11698 bfd_byte *dest;
11699
935bd1e0 11700 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11701 Note that we saved a word of storage and overwrote
11702 the original st_name with the dynstr_index. */
11703 sym = e->isym;
935bd1e0 11704 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11705
cb33740c
AM
11706 s = bfd_section_from_elf_index (e->input_bfd,
11707 e->isym.st_shndx);
11708 if (s != NULL)
c152c796 11709 {
c152c796
AM
11710 sym.st_shndx =
11711 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11712 if (! check_dynsym (abfd, &sym))
11713 return FALSE;
c152c796
AM
11714 sym.st_value = (s->output_section->vma
11715 + s->output_offset
11716 + e->isym.st_value);
11717 }
11718
11719 if (last_local < e->dynindx)
11720 last_local = e->dynindx;
11721
11722 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11723 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11724 }
11725 }
11726
cae1fbbb 11727 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
c152c796
AM
11728 last_local + 1;
11729 }
11730
11731 /* We get the global symbols from the hash table. */
11732 eoinfo.failed = FALSE;
11733 eoinfo.localsyms = FALSE;
8b127cbc 11734 eoinfo.flinfo = &flinfo;
7686d77d 11735 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11736 if (eoinfo.failed)
11737 return FALSE;
11738
11739 /* If backend needs to output some symbols not present in the hash
11740 table, do it now. */
8539e4e8
AM
11741 if (bed->elf_backend_output_arch_syms
11742 && (info->strip != strip_all || emit_relocs))
c152c796 11743 {
6e0b88f1 11744 typedef int (*out_sym_func)
c152c796
AM
11745 (void *, const char *, Elf_Internal_Sym *, asection *,
11746 struct elf_link_hash_entry *);
11747
11748 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
11749 (abfd, info, &flinfo,
11750 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
11751 return FALSE;
11752 }
11753
ef10c3ac
L
11754 /* Finalize the .strtab section. */
11755 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11756
11757 /* Swap out the .strtab section. */
11758 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
11759 return FALSE;
11760
11761 /* Now we know the size of the symtab section. */
c152c796
AM
11762 if (bfd_get_symcount (abfd) > 0)
11763 {
ee3b52e9
L
11764 /* Finish up and write out the symbol string table (.strtab)
11765 section. */
11766 Elf_Internal_Shdr *symstrtab_hdr;
8539e4e8
AM
11767 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11768
6a40cf0c
NC
11769 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11770 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
8539e4e8
AM
11771 {
11772 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11773 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11774 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11775 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11776 symtab_shndx_hdr->sh_size = amt;
11777
11778 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11779 off, TRUE);
11780
11781 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11782 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11783 return FALSE;
11784 }
ee3b52e9
L
11785
11786 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11787 /* sh_name was set in prep_headers. */
11788 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 11789 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 11790 symstrtab_hdr->sh_addr = 0;
ef10c3ac 11791 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
11792 symstrtab_hdr->sh_entsize = 0;
11793 symstrtab_hdr->sh_link = 0;
11794 symstrtab_hdr->sh_info = 0;
11795 /* sh_offset is set just below. */
11796 symstrtab_hdr->sh_addralign = 1;
11797
11798 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11799 off, TRUE);
11800 elf_next_file_pos (abfd) = off;
11801
c152c796 11802 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 11803 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
11804 return FALSE;
11805 }
11806
11807 /* Adjust the relocs to have the correct symbol indices. */
11808 for (o = abfd->sections; o != NULL; o = o->next)
11809 {
d4730f92 11810 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 11811 bfd_boolean sort;
c152c796
AM
11812 if ((o->flags & SEC_RELOC) == 0)
11813 continue;
11814
28dbcedc 11815 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3
AM
11816 if (esdo->rel.hdr != NULL
11817 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11818 return FALSE;
11819 if (esdo->rela.hdr != NULL
11820 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11821 return FALSE;
c152c796
AM
11822
11823 /* Set the reloc_count field to 0 to prevent write_relocs from
11824 trying to swap the relocs out itself. */
11825 o->reloc_count = 0;
11826 }
11827
11828 if (dynamic && info->combreloc && dynobj != NULL)
11829 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11830
11831 /* If we are linking against a dynamic object, or generating a
11832 shared library, finish up the dynamic linking information. */
11833 if (dynamic)
11834 {
11835 bfd_byte *dyncon, *dynconend;
11836
11837 /* Fix up .dynamic entries. */
3d4d4302 11838 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
11839 BFD_ASSERT (o != NULL);
11840
11841 dyncon = o->contents;
eea6121a 11842 dynconend = o->contents + o->size;
c152c796
AM
11843 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11844 {
11845 Elf_Internal_Dyn dyn;
11846 const char *name;
11847 unsigned int type;
11848
11849 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11850
11851 switch (dyn.d_tag)
11852 {
11853 default:
11854 continue;
11855 case DT_NULL:
11856 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11857 {
11858 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11859 {
11860 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11861 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11862 default: continue;
11863 }
11864 dyn.d_un.d_val = relativecount;
11865 relativecount = 0;
11866 break;
11867 }
11868 continue;
11869
11870 case DT_INIT:
11871 name = info->init_function;
11872 goto get_sym;
11873 case DT_FINI:
11874 name = info->fini_function;
11875 get_sym:
11876 {
11877 struct elf_link_hash_entry *h;
11878
11879 h = elf_link_hash_lookup (elf_hash_table (info), name,
11880 FALSE, FALSE, TRUE);
11881 if (h != NULL
11882 && (h->root.type == bfd_link_hash_defined
11883 || h->root.type == bfd_link_hash_defweak))
11884 {
bef26483 11885 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11886 o = h->root.u.def.section;
11887 if (o->output_section != NULL)
bef26483 11888 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11889 + o->output_offset);
11890 else
11891 {
11892 /* The symbol is imported from another shared
11893 library and does not apply to this one. */
bef26483 11894 dyn.d_un.d_ptr = 0;
c152c796
AM
11895 }
11896 break;
11897 }
11898 }
11899 continue;
11900
11901 case DT_PREINIT_ARRAYSZ:
11902 name = ".preinit_array";
4ade44b7 11903 goto get_out_size;
c152c796
AM
11904 case DT_INIT_ARRAYSZ:
11905 name = ".init_array";
4ade44b7 11906 goto get_out_size;
c152c796
AM
11907 case DT_FINI_ARRAYSZ:
11908 name = ".fini_array";
4ade44b7 11909 get_out_size:
c152c796
AM
11910 o = bfd_get_section_by_name (abfd, name);
11911 if (o == NULL)
11912 {
11913 (*_bfd_error_handler)
4ade44b7 11914 (_("could not find section %s"), name);
c152c796
AM
11915 goto error_return;
11916 }
eea6121a 11917 if (o->size == 0)
c152c796
AM
11918 (*_bfd_error_handler)
11919 (_("warning: %s section has zero size"), name);
eea6121a 11920 dyn.d_un.d_val = o->size;
c152c796
AM
11921 break;
11922
11923 case DT_PREINIT_ARRAY:
11924 name = ".preinit_array";
4ade44b7 11925 goto get_out_vma;
c152c796
AM
11926 case DT_INIT_ARRAY:
11927 name = ".init_array";
4ade44b7 11928 goto get_out_vma;
c152c796
AM
11929 case DT_FINI_ARRAY:
11930 name = ".fini_array";
4ade44b7
AM
11931 get_out_vma:
11932 o = bfd_get_section_by_name (abfd, name);
11933 goto do_vma;
c152c796
AM
11934
11935 case DT_HASH:
11936 name = ".hash";
11937 goto get_vma;
fdc90cb4
JJ
11938 case DT_GNU_HASH:
11939 name = ".gnu.hash";
11940 goto get_vma;
c152c796
AM
11941 case DT_STRTAB:
11942 name = ".dynstr";
11943 goto get_vma;
11944 case DT_SYMTAB:
11945 name = ".dynsym";
11946 goto get_vma;
11947 case DT_VERDEF:
11948 name = ".gnu.version_d";
11949 goto get_vma;
11950 case DT_VERNEED:
11951 name = ".gnu.version_r";
11952 goto get_vma;
11953 case DT_VERSYM:
11954 name = ".gnu.version";
11955 get_vma:
4ade44b7
AM
11956 o = bfd_get_linker_section (dynobj, name);
11957 do_vma:
c152c796
AM
11958 if (o == NULL)
11959 {
11960 (*_bfd_error_handler)
4ade44b7 11961 (_("could not find section %s"), name);
c152c796
AM
11962 goto error_return;
11963 }
894891db
NC
11964 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11965 {
11966 (*_bfd_error_handler)
11967 (_("warning: section '%s' is being made into a note"), name);
11968 bfd_set_error (bfd_error_nonrepresentable_section);
11969 goto error_return;
11970 }
4ade44b7 11971 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
11972 break;
11973
11974 case DT_REL:
11975 case DT_RELA:
11976 case DT_RELSZ:
11977 case DT_RELASZ:
11978 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11979 type = SHT_REL;
11980 else
11981 type = SHT_RELA;
11982 dyn.d_un.d_val = 0;
bef26483 11983 dyn.d_un.d_ptr = 0;
c152c796
AM
11984 for (i = 1; i < elf_numsections (abfd); i++)
11985 {
11986 Elf_Internal_Shdr *hdr;
11987
11988 hdr = elf_elfsections (abfd)[i];
11989 if (hdr->sh_type == type
11990 && (hdr->sh_flags & SHF_ALLOC) != 0)
11991 {
11992 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11993 dyn.d_un.d_val += hdr->sh_size;
11994 else
11995 {
bef26483
AM
11996 if (dyn.d_un.d_ptr == 0
11997 || hdr->sh_addr < dyn.d_un.d_ptr)
11998 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
11999 }
12000 }
12001 }
12002 break;
12003 }
12004 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12005 }
12006 }
12007
12008 /* If we have created any dynamic sections, then output them. */
12009 if (dynobj != NULL)
12010 {
12011 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12012 goto error_return;
12013
943284cc 12014 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12015 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12016 || info->error_textrel)
3d4d4302 12017 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12018 {
12019 bfd_byte *dyncon, *dynconend;
12020
943284cc
DJ
12021 dyncon = o->contents;
12022 dynconend = o->contents + o->size;
12023 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12024 {
12025 Elf_Internal_Dyn dyn;
12026
12027 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12028
12029 if (dyn.d_tag == DT_TEXTREL)
12030 {
c192a133
AM
12031 if (info->error_textrel)
12032 info->callbacks->einfo
12033 (_("%P%X: read-only segment has dynamic relocations.\n"));
12034 else
12035 info->callbacks->einfo
12036 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12037 break;
12038 }
12039 }
12040 }
12041
c152c796
AM
12042 for (o = dynobj->sections; o != NULL; o = o->next)
12043 {
12044 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12045 || o->size == 0
c152c796
AM
12046 || o->output_section == bfd_abs_section_ptr)
12047 continue;
12048 if ((o->flags & SEC_LINKER_CREATED) == 0)
12049 {
12050 /* At this point, we are only interested in sections
12051 created by _bfd_elf_link_create_dynamic_sections. */
12052 continue;
12053 }
3722b82f
AM
12054 if (elf_hash_table (info)->stab_info.stabstr == o)
12055 continue;
eea6121a
AM
12056 if (elf_hash_table (info)->eh_info.hdr_sec == o)
12057 continue;
3d4d4302 12058 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12059 {
12060 if (! bfd_set_section_contents (abfd, o->output_section,
12061 o->contents,
37b01f6a
DG
12062 (file_ptr) o->output_offset
12063 * bfd_octets_per_byte (abfd),
eea6121a 12064 o->size))
c152c796
AM
12065 goto error_return;
12066 }
12067 else
12068 {
12069 /* The contents of the .dynstr section are actually in a
12070 stringtab. */
8539e4e8
AM
12071 file_ptr off;
12072
c152c796
AM
12073 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12074 if (bfd_seek (abfd, off, SEEK_SET) != 0
12075 || ! _bfd_elf_strtab_emit (abfd,
12076 elf_hash_table (info)->dynstr))
12077 goto error_return;
12078 }
12079 }
12080 }
12081
0e1862bb 12082 if (bfd_link_relocatable (info))
c152c796
AM
12083 {
12084 bfd_boolean failed = FALSE;
12085
12086 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12087 if (failed)
12088 goto error_return;
12089 }
12090
12091 /* If we have optimized stabs strings, output them. */
3722b82f 12092 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
12093 {
12094 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
12095 goto error_return;
12096 }
12097
9f7c3e5e
AM
12098 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12099 goto error_return;
c152c796 12100
9f7c3e5e 12101 elf_final_link_free (abfd, &flinfo);
c152c796 12102
12bd6957 12103 elf_linker (abfd) = TRUE;
c152c796 12104
104d59d1
JM
12105 if (attr_section)
12106 {
a50b1753 12107 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12108 if (contents == NULL)
d0f16d5e 12109 return FALSE; /* Bail out and fail. */
104d59d1
JM
12110 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12111 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12112 free (contents);
12113 }
12114
c152c796
AM
12115 return TRUE;
12116
12117 error_return:
9f7c3e5e 12118 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12119 return FALSE;
12120}
12121\f
5241d853
RS
12122/* Initialize COOKIE for input bfd ABFD. */
12123
12124static bfd_boolean
12125init_reloc_cookie (struct elf_reloc_cookie *cookie,
12126 struct bfd_link_info *info, bfd *abfd)
12127{
12128 Elf_Internal_Shdr *symtab_hdr;
12129 const struct elf_backend_data *bed;
12130
12131 bed = get_elf_backend_data (abfd);
12132 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12133
12134 cookie->abfd = abfd;
12135 cookie->sym_hashes = elf_sym_hashes (abfd);
12136 cookie->bad_symtab = elf_bad_symtab (abfd);
12137 if (cookie->bad_symtab)
12138 {
12139 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12140 cookie->extsymoff = 0;
12141 }
12142 else
12143 {
12144 cookie->locsymcount = symtab_hdr->sh_info;
12145 cookie->extsymoff = symtab_hdr->sh_info;
12146 }
12147
12148 if (bed->s->arch_size == 32)
12149 cookie->r_sym_shift = 8;
12150 else
12151 cookie->r_sym_shift = 32;
12152
12153 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12154 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12155 {
12156 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12157 cookie->locsymcount, 0,
12158 NULL, NULL, NULL);
12159 if (cookie->locsyms == NULL)
12160 {
12161 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12162 return FALSE;
12163 }
12164 if (info->keep_memory)
12165 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12166 }
12167 return TRUE;
12168}
12169
12170/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12171
12172static void
12173fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12174{
12175 Elf_Internal_Shdr *symtab_hdr;
12176
12177 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12178 if (cookie->locsyms != NULL
12179 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12180 free (cookie->locsyms);
12181}
12182
12183/* Initialize the relocation information in COOKIE for input section SEC
12184 of input bfd ABFD. */
12185
12186static bfd_boolean
12187init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12188 struct bfd_link_info *info, bfd *abfd,
12189 asection *sec)
12190{
12191 const struct elf_backend_data *bed;
12192
12193 if (sec->reloc_count == 0)
12194 {
12195 cookie->rels = NULL;
12196 cookie->relend = NULL;
12197 }
12198 else
12199 {
12200 bed = get_elf_backend_data (abfd);
12201
12202 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12203 info->keep_memory);
12204 if (cookie->rels == NULL)
12205 return FALSE;
12206 cookie->rel = cookie->rels;
12207 cookie->relend = (cookie->rels
12208 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12209 }
12210 cookie->rel = cookie->rels;
12211 return TRUE;
12212}
12213
12214/* Free the memory allocated by init_reloc_cookie_rels,
12215 if appropriate. */
12216
12217static void
12218fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12219 asection *sec)
12220{
12221 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12222 free (cookie->rels);
12223}
12224
12225/* Initialize the whole of COOKIE for input section SEC. */
12226
12227static bfd_boolean
12228init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12229 struct bfd_link_info *info,
12230 asection *sec)
12231{
12232 if (!init_reloc_cookie (cookie, info, sec->owner))
12233 goto error1;
12234 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12235 goto error2;
12236 return TRUE;
12237
12238 error2:
12239 fini_reloc_cookie (cookie, sec->owner);
12240 error1:
12241 return FALSE;
12242}
12243
12244/* Free the memory allocated by init_reloc_cookie_for_section,
12245 if appropriate. */
12246
12247static void
12248fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12249 asection *sec)
12250{
12251 fini_reloc_cookie_rels (cookie, sec);
12252 fini_reloc_cookie (cookie, sec->owner);
12253}
12254\f
c152c796
AM
12255/* Garbage collect unused sections. */
12256
07adf181
AM
12257/* Default gc_mark_hook. */
12258
12259asection *
12260_bfd_elf_gc_mark_hook (asection *sec,
12261 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12262 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12263 struct elf_link_hash_entry *h,
12264 Elf_Internal_Sym *sym)
12265{
12266 if (h != NULL)
12267 {
12268 switch (h->root.type)
12269 {
12270 case bfd_link_hash_defined:
12271 case bfd_link_hash_defweak:
12272 return h->root.u.def.section;
12273
12274 case bfd_link_hash_common:
12275 return h->root.u.c.p->section;
12276
12277 default:
12278 break;
12279 }
12280 }
12281 else
12282 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12283
12284 return NULL;
12285}
12286
a6a4679f
AM
12287/* For undefined __start_<name> and __stop_<name> symbols, return the
12288 first input section matching <name>. Return NULL otherwise. */
12289
12290asection *
12291_bfd_elf_is_start_stop (const struct bfd_link_info *info,
12292 struct elf_link_hash_entry *h)
12293{
12294 asection *s;
12295 const char *sec_name;
12296
12297 if (h->root.type != bfd_link_hash_undefined
12298 && h->root.type != bfd_link_hash_undefweak)
12299 return NULL;
12300
12301 s = h->root.u.undef.section;
12302 if (s != NULL)
12303 {
12304 if (s == (asection *) 0 - 1)
12305 return NULL;
12306 return s;
12307 }
12308
12309 sec_name = NULL;
12310 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12311 sec_name = h->root.root.string + 8;
12312 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12313 sec_name = h->root.root.string + 7;
12314
12315 if (sec_name != NULL && *sec_name != '\0')
12316 {
12317 bfd *i;
12318
12319 for (i = info->input_bfds; i != NULL; i = i->link.next)
12320 {
12321 s = bfd_get_section_by_name (i, sec_name);
12322 if (s != NULL)
12323 {
12324 h->root.u.undef.section = s;
12325 break;
12326 }
12327 }
12328 }
12329
12330 if (s == NULL)
12331 h->root.u.undef.section = (asection *) 0 - 1;
12332
12333 return s;
12334}
12335
5241d853
RS
12336/* COOKIE->rel describes a relocation against section SEC, which is
12337 a section we've decided to keep. Return the section that contains
12338 the relocation symbol, or NULL if no section contains it. */
12339
12340asection *
12341_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12342 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12343 struct elf_reloc_cookie *cookie,
12344 bfd_boolean *start_stop)
5241d853
RS
12345{
12346 unsigned long r_symndx;
12347 struct elf_link_hash_entry *h;
12348
12349 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12350 if (r_symndx == STN_UNDEF)
5241d853
RS
12351 return NULL;
12352
12353 if (r_symndx >= cookie->locsymcount
12354 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12355 {
12356 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12357 if (h == NULL)
12358 {
12359 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12360 sec->owner);
12361 return NULL;
12362 }
5241d853
RS
12363 while (h->root.type == bfd_link_hash_indirect
12364 || h->root.type == bfd_link_hash_warning)
12365 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12366 h->mark = 1;
4e6b54a6
AM
12367 /* If this symbol is weak and there is a non-weak definition, we
12368 keep the non-weak definition because many backends put
12369 dynamic reloc info on the non-weak definition for code
12370 handling copy relocs. */
12371 if (h->u.weakdef != NULL)
12372 h->u.weakdef->mark = 1;
1cce69b9 12373
a6a4679f 12374 if (start_stop != NULL)
1cce69b9
AM
12375 {
12376 /* To work around a glibc bug, mark all XXX input sections
12377 when there is an as yet undefined reference to __start_XXX
12378 or __stop_XXX symbols. The linker will later define such
12379 symbols for orphan input sections that have a name
12380 representable as a C identifier. */
a6a4679f 12381 asection *s = _bfd_elf_is_start_stop (info, h);
1cce69b9 12382
a6a4679f 12383 if (s != NULL)
1cce69b9 12384 {
a6a4679f
AM
12385 *start_stop = !s->gc_mark;
12386 return s;
1cce69b9
AM
12387 }
12388 }
12389
5241d853
RS
12390 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12391 }
12392
12393 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12394 &cookie->locsyms[r_symndx]);
12395}
12396
12397/* COOKIE->rel describes a relocation against section SEC, which is
12398 a section we've decided to keep. Mark the section that contains
9d0a14d3 12399 the relocation symbol. */
5241d853
RS
12400
12401bfd_boolean
12402_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12403 asection *sec,
12404 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12405 struct elf_reloc_cookie *cookie)
5241d853
RS
12406{
12407 asection *rsec;
1cce69b9 12408 bfd_boolean start_stop = FALSE;
5241d853 12409
1cce69b9
AM
12410 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12411 while (rsec != NULL)
5241d853 12412 {
1cce69b9
AM
12413 if (!rsec->gc_mark)
12414 {
12415 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12416 || (rsec->owner->flags & DYNAMIC) != 0)
12417 rsec->gc_mark = 1;
12418 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12419 return FALSE;
12420 }
12421 if (!start_stop)
12422 break;
199af150 12423 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12424 }
12425 return TRUE;
12426}
12427
07adf181
AM
12428/* The mark phase of garbage collection. For a given section, mark
12429 it and any sections in this section's group, and all the sections
12430 which define symbols to which it refers. */
12431
ccfa59ea
AM
12432bfd_boolean
12433_bfd_elf_gc_mark (struct bfd_link_info *info,
12434 asection *sec,
6a5bb875 12435 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12436{
12437 bfd_boolean ret;
9d0a14d3 12438 asection *group_sec, *eh_frame;
c152c796
AM
12439
12440 sec->gc_mark = 1;
12441
12442 /* Mark all the sections in the group. */
12443 group_sec = elf_section_data (sec)->next_in_group;
12444 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12445 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12446 return FALSE;
12447
12448 /* Look through the section relocs. */
12449 ret = TRUE;
9d0a14d3
RS
12450 eh_frame = elf_eh_frame_section (sec->owner);
12451 if ((sec->flags & SEC_RELOC) != 0
12452 && sec->reloc_count > 0
12453 && sec != eh_frame)
c152c796 12454 {
5241d853 12455 struct elf_reloc_cookie cookie;
c152c796 12456
5241d853
RS
12457 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12458 ret = FALSE;
c152c796 12459 else
c152c796 12460 {
5241d853 12461 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12462 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12463 {
12464 ret = FALSE;
12465 break;
12466 }
12467 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12468 }
12469 }
9d0a14d3
RS
12470
12471 if (ret && eh_frame && elf_fde_list (sec))
12472 {
12473 struct elf_reloc_cookie cookie;
12474
12475 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12476 ret = FALSE;
12477 else
12478 {
12479 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12480 gc_mark_hook, &cookie))
12481 ret = FALSE;
12482 fini_reloc_cookie_for_section (&cookie, eh_frame);
12483 }
12484 }
12485
2f0c68f2
CM
12486 eh_frame = elf_section_eh_frame_entry (sec);
12487 if (ret && eh_frame && !eh_frame->gc_mark)
12488 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12489 ret = FALSE;
12490
c152c796
AM
12491 return ret;
12492}
12493
3c758495
TG
12494/* Scan and mark sections in a special or debug section group. */
12495
12496static void
12497_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12498{
12499 /* Point to first section of section group. */
12500 asection *ssec;
12501 /* Used to iterate the section group. */
12502 asection *msec;
12503
12504 bfd_boolean is_special_grp = TRUE;
12505 bfd_boolean is_debug_grp = TRUE;
12506
12507 /* First scan to see if group contains any section other than debug
12508 and special section. */
12509 ssec = msec = elf_next_in_group (grp);
12510 do
12511 {
12512 if ((msec->flags & SEC_DEBUGGING) == 0)
12513 is_debug_grp = FALSE;
12514
12515 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12516 is_special_grp = FALSE;
12517
12518 msec = elf_next_in_group (msec);
12519 }
12520 while (msec != ssec);
12521
12522 /* If this is a pure debug section group or pure special section group,
12523 keep all sections in this group. */
12524 if (is_debug_grp || is_special_grp)
12525 {
12526 do
12527 {
12528 msec->gc_mark = 1;
12529 msec = elf_next_in_group (msec);
12530 }
12531 while (msec != ssec);
12532 }
12533}
12534
7f6ab9f8
AM
12535/* Keep debug and special sections. */
12536
12537bfd_boolean
12538_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12539 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12540{
12541 bfd *ibfd;
12542
c72f2fb2 12543 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12544 {
12545 asection *isec;
12546 bfd_boolean some_kept;
b40bf0a2 12547 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12548
12549 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12550 continue;
12551
b40bf0a2
NC
12552 /* Ensure all linker created sections are kept,
12553 see if any other section is already marked,
12554 and note if we have any fragmented debug sections. */
12555 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12556 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12557 {
12558 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12559 isec->gc_mark = 1;
12560 else if (isec->gc_mark)
12561 some_kept = TRUE;
b40bf0a2
NC
12562
12563 if (debug_frag_seen == FALSE
12564 && (isec->flags & SEC_DEBUGGING)
12565 && CONST_STRNEQ (isec->name, ".debug_line."))
12566 debug_frag_seen = TRUE;
7f6ab9f8
AM
12567 }
12568
12569 /* If no section in this file will be kept, then we can
b40bf0a2 12570 toss out the debug and special sections. */
7f6ab9f8
AM
12571 if (!some_kept)
12572 continue;
12573
12574 /* Keep debug and special sections like .comment when they are
3c758495
TG
12575 not part of a group. Also keep section groups that contain
12576 just debug sections or special sections. */
7f6ab9f8 12577 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12578 {
12579 if ((isec->flags & SEC_GROUP) != 0)
12580 _bfd_elf_gc_mark_debug_special_section_group (isec);
12581 else if (((isec->flags & SEC_DEBUGGING) != 0
12582 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12583 && elf_next_in_group (isec) == NULL)
12584 isec->gc_mark = 1;
12585 }
b40bf0a2
NC
12586
12587 if (! debug_frag_seen)
12588 continue;
12589
12590 /* Look for CODE sections which are going to be discarded,
12591 and find and discard any fragmented debug sections which
12592 are associated with that code section. */
12593 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12594 if ((isec->flags & SEC_CODE) != 0
12595 && isec->gc_mark == 0)
12596 {
12597 unsigned int ilen;
12598 asection *dsec;
12599
12600 ilen = strlen (isec->name);
12601
12602 /* Association is determined by the name of the debug section
12603 containing the name of the code section as a suffix. For
12604 example .debug_line.text.foo is a debug section associated
12605 with .text.foo. */
12606 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12607 {
12608 unsigned int dlen;
12609
12610 if (dsec->gc_mark == 0
12611 || (dsec->flags & SEC_DEBUGGING) == 0)
12612 continue;
12613
12614 dlen = strlen (dsec->name);
12615
12616 if (dlen > ilen
12617 && strncmp (dsec->name + (dlen - ilen),
12618 isec->name, ilen) == 0)
12619 {
12620 dsec->gc_mark = 0;
b40bf0a2
NC
12621 }
12622 }
12623 }
7f6ab9f8
AM
12624 }
12625 return TRUE;
12626}
12627
c152c796
AM
12628/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12629
c17d87de
NC
12630struct elf_gc_sweep_symbol_info
12631{
ccabcbe5
AM
12632 struct bfd_link_info *info;
12633 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12634 bfd_boolean);
12635};
12636
c152c796 12637static bfd_boolean
ccabcbe5 12638elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 12639{
1d5316ab
AM
12640 if (!h->mark
12641 && (((h->root.type == bfd_link_hash_defined
12642 || h->root.type == bfd_link_hash_defweak)
c4621b33 12643 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6673f753 12644 && h->root.u.def.section->gc_mark))
1d5316ab
AM
12645 || h->root.type == bfd_link_hash_undefined
12646 || h->root.type == bfd_link_hash_undefweak))
12647 {
12648 struct elf_gc_sweep_symbol_info *inf;
12649
12650 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 12651 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
12652 h->def_regular = 0;
12653 h->ref_regular = 0;
12654 h->ref_regular_nonweak = 0;
ccabcbe5 12655 }
c152c796
AM
12656
12657 return TRUE;
12658}
12659
12660/* The sweep phase of garbage collection. Remove all garbage sections. */
12661
12662typedef bfd_boolean (*gc_sweep_hook_fn)
12663 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12664
12665static bfd_boolean
ccabcbe5 12666elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12667{
12668 bfd *sub;
ccabcbe5
AM
12669 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12670 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12671 unsigned long section_sym_count;
12672 struct elf_gc_sweep_symbol_info sweep_info;
c152c796 12673
c72f2fb2 12674 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12675 {
12676 asection *o;
12677
b19a8f85
L
12678 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12679 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12680 continue;
12681
12682 for (o = sub->sections; o != NULL; o = o->next)
12683 {
a33dafc3
L
12684 /* When any section in a section group is kept, we keep all
12685 sections in the section group. If the first member of
12686 the section group is excluded, we will also exclude the
12687 group section. */
12688 if (o->flags & SEC_GROUP)
12689 {
12690 asection *first = elf_next_in_group (o);
12691 o->gc_mark = first->gc_mark;
12692 }
c152c796 12693
1e7eae0d 12694 if (o->gc_mark)
c152c796
AM
12695 continue;
12696
12697 /* Skip sweeping sections already excluded. */
12698 if (o->flags & SEC_EXCLUDE)
12699 continue;
12700
12701 /* Since this is early in the link process, it is simple
12702 to remove a section from the output. */
12703 o->flags |= SEC_EXCLUDE;
12704
c55fe096 12705 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
12706 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12707
c152c796
AM
12708 /* But we also have to update some of the relocation
12709 info we collected before. */
12710 if (gc_sweep_hook
e8aaee2a 12711 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12712 && o->reloc_count != 0
12713 && !((info->strip == strip_all || info->strip == strip_debugger)
12714 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12715 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12716 {
12717 Elf_Internal_Rela *internal_relocs;
12718 bfd_boolean r;
12719
12720 internal_relocs
12721 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12722 info->keep_memory);
12723 if (internal_relocs == NULL)
12724 return FALSE;
12725
12726 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12727
12728 if (elf_section_data (o)->relocs != internal_relocs)
12729 free (internal_relocs);
12730
12731 if (!r)
12732 return FALSE;
12733 }
12734 }
12735 }
12736
12737 /* Remove the symbols that were in the swept sections from the dynamic
12738 symbol table. GCFIXME: Anyone know how to get them out of the
12739 static symbol table as well? */
ccabcbe5
AM
12740 sweep_info.info = info;
12741 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12742 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12743 &sweep_info);
c152c796 12744
ccabcbe5 12745 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
12746 return TRUE;
12747}
12748
12749/* Propagate collected vtable information. This is called through
12750 elf_link_hash_traverse. */
12751
12752static bfd_boolean
12753elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12754{
c152c796 12755 /* Those that are not vtables. */
f6e332e6 12756 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12757 return TRUE;
12758
12759 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12760 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12761 return TRUE;
12762
12763 /* If we've already been done, exit. */
f6e332e6 12764 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12765 return TRUE;
12766
12767 /* Make sure the parent's table is up to date. */
f6e332e6 12768 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 12769
f6e332e6 12770 if (h->vtable->used == NULL)
c152c796
AM
12771 {
12772 /* None of this table's entries were referenced. Re-use the
12773 parent's table. */
f6e332e6
AM
12774 h->vtable->used = h->vtable->parent->vtable->used;
12775 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
12776 }
12777 else
12778 {
12779 size_t n;
12780 bfd_boolean *cu, *pu;
12781
12782 /* Or the parent's entries into ours. */
f6e332e6 12783 cu = h->vtable->used;
c152c796 12784 cu[-1] = TRUE;
f6e332e6 12785 pu = h->vtable->parent->vtable->used;
c152c796
AM
12786 if (pu != NULL)
12787 {
12788 const struct elf_backend_data *bed;
12789 unsigned int log_file_align;
12790
12791 bed = get_elf_backend_data (h->root.u.def.section->owner);
12792 log_file_align = bed->s->log_file_align;
f6e332e6 12793 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
12794 while (n--)
12795 {
12796 if (*pu)
12797 *cu = TRUE;
12798 pu++;
12799 cu++;
12800 }
12801 }
12802 }
12803
12804 return TRUE;
12805}
12806
12807static bfd_boolean
12808elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12809{
12810 asection *sec;
12811 bfd_vma hstart, hend;
12812 Elf_Internal_Rela *relstart, *relend, *rel;
12813 const struct elf_backend_data *bed;
12814 unsigned int log_file_align;
12815
c152c796
AM
12816 /* Take care of both those symbols that do not describe vtables as
12817 well as those that are not loaded. */
f6e332e6 12818 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12819 return TRUE;
12820
12821 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12822 || h->root.type == bfd_link_hash_defweak);
12823
12824 sec = h->root.u.def.section;
12825 hstart = h->root.u.def.value;
12826 hend = hstart + h->size;
12827
12828 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12829 if (!relstart)
12830 return *(bfd_boolean *) okp = FALSE;
12831 bed = get_elf_backend_data (sec->owner);
12832 log_file_align = bed->s->log_file_align;
12833
12834 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12835
12836 for (rel = relstart; rel < relend; ++rel)
12837 if (rel->r_offset >= hstart && rel->r_offset < hend)
12838 {
12839 /* If the entry is in use, do nothing. */
f6e332e6
AM
12840 if (h->vtable->used
12841 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
12842 {
12843 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 12844 if (h->vtable->used[entry])
c152c796
AM
12845 continue;
12846 }
12847 /* Otherwise, kill it. */
12848 rel->r_offset = rel->r_info = rel->r_addend = 0;
12849 }
12850
12851 return TRUE;
12852}
12853
87538722
AM
12854/* Mark sections containing dynamically referenced symbols. When
12855 building shared libraries, we must assume that any visible symbol is
12856 referenced. */
715df9b8 12857
64d03ab5
AM
12858bfd_boolean
12859bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 12860{
87538722 12861 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 12862 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 12863
715df9b8
EB
12864 if ((h->root.type == bfd_link_hash_defined
12865 || h->root.type == bfd_link_hash_defweak)
87538722 12866 && (h->ref_dynamic
c4621b33 12867 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 12868 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 12869 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 12870 && (!bfd_link_executable (info)
b407645f
AM
12871 || info->export_dynamic
12872 || (h->dynamic
12873 && d != NULL
12874 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 12875 && (h->versioned >= versioned
54e8959c
L
12876 || !bfd_hide_sym_by_version (info->version_info,
12877 h->root.root.string)))))
715df9b8
EB
12878 h->root.u.def.section->flags |= SEC_KEEP;
12879
12880 return TRUE;
12881}
3b36f7e6 12882
74f0fb50
AM
12883/* Keep all sections containing symbols undefined on the command-line,
12884 and the section containing the entry symbol. */
12885
12886void
12887_bfd_elf_gc_keep (struct bfd_link_info *info)
12888{
12889 struct bfd_sym_chain *sym;
12890
12891 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12892 {
12893 struct elf_link_hash_entry *h;
12894
12895 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12896 FALSE, FALSE, FALSE);
12897
12898 if (h != NULL
12899 && (h->root.type == bfd_link_hash_defined
12900 || h->root.type == bfd_link_hash_defweak)
12901 && !bfd_is_abs_section (h->root.u.def.section))
12902 h->root.u.def.section->flags |= SEC_KEEP;
12903 }
12904}
12905
2f0c68f2
CM
12906bfd_boolean
12907bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12908 struct bfd_link_info *info)
12909{
12910 bfd *ibfd = info->input_bfds;
12911
12912 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12913 {
12914 asection *sec;
12915 struct elf_reloc_cookie cookie;
12916
12917 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12918 continue;
12919
12920 if (!init_reloc_cookie (&cookie, info, ibfd))
12921 return FALSE;
12922
12923 for (sec = ibfd->sections; sec; sec = sec->next)
12924 {
12925 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12926 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12927 {
12928 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12929 fini_reloc_cookie_rels (&cookie, sec);
12930 }
12931 }
12932 }
12933 return TRUE;
12934}
12935
c152c796
AM
12936/* Do mark and sweep of unused sections. */
12937
12938bfd_boolean
12939bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12940{
12941 bfd_boolean ok = TRUE;
12942 bfd *sub;
6a5bb875 12943 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 12944 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 12945 struct elf_link_hash_table *htab;
c152c796 12946
64d03ab5 12947 if (!bed->can_gc_sections
715df9b8 12948 || !is_elf_hash_table (info->hash))
c152c796
AM
12949 {
12950 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12951 return TRUE;
12952 }
12953
74f0fb50 12954 bed->gc_keep (info);
da44f4e5 12955 htab = elf_hash_table (info);
74f0fb50 12956
9d0a14d3
RS
12957 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12958 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
12959 for (sub = info->input_bfds;
12960 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12961 sub = sub->link.next)
9d0a14d3
RS
12962 {
12963 asection *sec;
12964 struct elf_reloc_cookie cookie;
12965
12966 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 12967 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
12968 {
12969 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
12970 if (elf_section_data (sec)->sec_info
12971 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
12972 elf_eh_frame_section (sub) = sec;
12973 fini_reloc_cookie_for_section (&cookie, sec);
199af150 12974 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
12975 }
12976 }
9d0a14d3 12977
c152c796 12978 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 12979 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
12980 if (!ok)
12981 return FALSE;
12982
12983 /* Kill the vtable relocations that were not used. */
da44f4e5 12984 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
12985 if (!ok)
12986 return FALSE;
12987
715df9b8 12988 /* Mark dynamically referenced symbols. */
da44f4e5
AM
12989 if (htab->dynamic_sections_created)
12990 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 12991
715df9b8 12992 /* Grovel through relocs to find out who stays ... */
64d03ab5 12993 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 12994 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12995 {
12996 asection *o;
12997
b19a8f85
L
12998 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12999 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13000 continue;
13001
7f6ab9f8
AM
13002 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13003 Also treat note sections as a root, if the section is not part
13004 of a group. */
c152c796 13005 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13006 if (!o->gc_mark
13007 && (o->flags & SEC_EXCLUDE) == 0
24007750 13008 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13009 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13010 && elf_next_in_group (o) == NULL )))
13011 {
13012 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13013 return FALSE;
13014 }
c152c796
AM
13015 }
13016
6a5bb875 13017 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13018 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13019
c152c796 13020 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13021 return elf_gc_sweep (abfd, info);
c152c796
AM
13022}
13023\f
13024/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13025
13026bfd_boolean
13027bfd_elf_gc_record_vtinherit (bfd *abfd,
13028 asection *sec,
13029 struct elf_link_hash_entry *h,
13030 bfd_vma offset)
13031{
13032 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13033 struct elf_link_hash_entry **search, *child;
13034 bfd_size_type extsymcount;
13035 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13036
13037 /* The sh_info field of the symtab header tells us where the
13038 external symbols start. We don't care about the local symbols at
13039 this point. */
13040 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13041 if (!elf_bad_symtab (abfd))
13042 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13043
13044 sym_hashes = elf_sym_hashes (abfd);
13045 sym_hashes_end = sym_hashes + extsymcount;
13046
13047 /* Hunt down the child symbol, which is in this section at the same
13048 offset as the relocation. */
13049 for (search = sym_hashes; search != sym_hashes_end; ++search)
13050 {
13051 if ((child = *search) != NULL
13052 && (child->root.type == bfd_link_hash_defined
13053 || child->root.type == bfd_link_hash_defweak)
13054 && child->root.u.def.section == sec
13055 && child->root.u.def.value == offset)
13056 goto win;
13057 }
13058
d003868e
AM
13059 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
13060 abfd, sec, (unsigned long) offset);
c152c796
AM
13061 bfd_set_error (bfd_error_invalid_operation);
13062 return FALSE;
13063
13064 win:
f6e332e6
AM
13065 if (!child->vtable)
13066 {
ca4be51c
AM
13067 child->vtable = ((struct elf_link_virtual_table_entry *)
13068 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
13069 if (!child->vtable)
13070 return FALSE;
13071 }
c152c796
AM
13072 if (!h)
13073 {
13074 /* This *should* only be the absolute section. It could potentially
13075 be that someone has defined a non-global vtable though, which
13076 would be bad. It isn't worth paging in the local symbols to be
13077 sure though; that case should simply be handled by the assembler. */
13078
f6e332e6 13079 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13080 }
13081 else
f6e332e6 13082 child->vtable->parent = h;
c152c796
AM
13083
13084 return TRUE;
13085}
13086
13087/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13088
13089bfd_boolean
13090bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13091 asection *sec ATTRIBUTE_UNUSED,
13092 struct elf_link_hash_entry *h,
13093 bfd_vma addend)
13094{
13095 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13096 unsigned int log_file_align = bed->s->log_file_align;
13097
f6e332e6
AM
13098 if (!h->vtable)
13099 {
ca4be51c
AM
13100 h->vtable = ((struct elf_link_virtual_table_entry *)
13101 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
13102 if (!h->vtable)
13103 return FALSE;
13104 }
13105
13106 if (addend >= h->vtable->size)
c152c796
AM
13107 {
13108 size_t size, bytes, file_align;
f6e332e6 13109 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
13110
13111 /* While the symbol is undefined, we have to be prepared to handle
13112 a zero size. */
13113 file_align = 1 << log_file_align;
13114 if (h->root.type == bfd_link_hash_undefined)
13115 size = addend + file_align;
13116 else
13117 {
13118 size = h->size;
13119 if (addend >= size)
13120 {
13121 /* Oops! We've got a reference past the defined end of
13122 the table. This is probably a bug -- shall we warn? */
13123 size = addend + file_align;
13124 }
13125 }
13126 size = (size + file_align - 1) & -file_align;
13127
13128 /* Allocate one extra entry for use as a "done" flag for the
13129 consolidation pass. */
13130 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13131
13132 if (ptr)
13133 {
a50b1753 13134 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13135
13136 if (ptr != NULL)
13137 {
13138 size_t oldbytes;
13139
f6e332e6 13140 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
13141 * sizeof (bfd_boolean));
13142 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13143 }
13144 }
13145 else
a50b1753 13146 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13147
13148 if (ptr == NULL)
13149 return FALSE;
13150
13151 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
13152 h->vtable->used = ptr + 1;
13153 h->vtable->size = size;
c152c796
AM
13154 }
13155
f6e332e6 13156 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13157
13158 return TRUE;
13159}
13160
ae17ab41
CM
13161/* Map an ELF section header flag to its corresponding string. */
13162typedef struct
13163{
13164 char *flag_name;
13165 flagword flag_value;
13166} elf_flags_to_name_table;
13167
13168static elf_flags_to_name_table elf_flags_to_names [] =
13169{
13170 { "SHF_WRITE", SHF_WRITE },
13171 { "SHF_ALLOC", SHF_ALLOC },
13172 { "SHF_EXECINSTR", SHF_EXECINSTR },
13173 { "SHF_MERGE", SHF_MERGE },
13174 { "SHF_STRINGS", SHF_STRINGS },
13175 { "SHF_INFO_LINK", SHF_INFO_LINK},
13176 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13177 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13178 { "SHF_GROUP", SHF_GROUP },
13179 { "SHF_TLS", SHF_TLS },
13180 { "SHF_MASKOS", SHF_MASKOS },
13181 { "SHF_EXCLUDE", SHF_EXCLUDE },
13182};
13183
b9c361e0
JL
13184/* Returns TRUE if the section is to be included, otherwise FALSE. */
13185bfd_boolean
ae17ab41 13186bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13187 struct flag_info *flaginfo,
b9c361e0 13188 asection *section)
ae17ab41 13189{
8b127cbc 13190 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13191
8b127cbc 13192 if (!flaginfo->flags_initialized)
ae17ab41 13193 {
8b127cbc
AM
13194 bfd *obfd = info->output_bfd;
13195 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13196 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13197 int with_hex = 0;
13198 int without_hex = 0;
13199
8b127cbc 13200 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13201 {
b9c361e0 13202 unsigned i;
8b127cbc 13203 flagword (*lookup) (char *);
ae17ab41 13204
8b127cbc
AM
13205 lookup = bed->elf_backend_lookup_section_flags_hook;
13206 if (lookup != NULL)
ae17ab41 13207 {
8b127cbc 13208 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13209
13210 if (hexval != 0)
13211 {
13212 if (tf->with == with_flags)
13213 with_hex |= hexval;
13214 else if (tf->with == without_flags)
13215 without_hex |= hexval;
13216 tf->valid = TRUE;
13217 continue;
13218 }
ae17ab41 13219 }
8b127cbc 13220 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13221 {
8b127cbc 13222 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13223 {
13224 if (tf->with == with_flags)
13225 with_hex |= elf_flags_to_names[i].flag_value;
13226 else if (tf->with == without_flags)
13227 without_hex |= elf_flags_to_names[i].flag_value;
13228 tf->valid = TRUE;
13229 break;
13230 }
13231 }
8b127cbc 13232 if (!tf->valid)
b9c361e0 13233 {
68ffbac6 13234 info->callbacks->einfo
8b127cbc 13235 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13236 return FALSE;
ae17ab41
CM
13237 }
13238 }
8b127cbc
AM
13239 flaginfo->flags_initialized = TRUE;
13240 flaginfo->only_with_flags |= with_hex;
13241 flaginfo->not_with_flags |= without_hex;
ae17ab41 13242 }
ae17ab41 13243
8b127cbc 13244 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13245 return FALSE;
13246
8b127cbc 13247 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13248 return FALSE;
13249
13250 return TRUE;
ae17ab41
CM
13251}
13252
c152c796
AM
13253struct alloc_got_off_arg {
13254 bfd_vma gotoff;
10455f89 13255 struct bfd_link_info *info;
c152c796
AM
13256};
13257
13258/* We need a special top-level link routine to convert got reference counts
13259 to real got offsets. */
13260
13261static bfd_boolean
13262elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13263{
a50b1753 13264 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13265 bfd *obfd = gofarg->info->output_bfd;
13266 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13267
c152c796
AM
13268 if (h->got.refcount > 0)
13269 {
13270 h->got.offset = gofarg->gotoff;
10455f89 13271 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13272 }
13273 else
13274 h->got.offset = (bfd_vma) -1;
13275
13276 return TRUE;
13277}
13278
13279/* And an accompanying bit to work out final got entry offsets once
13280 we're done. Should be called from final_link. */
13281
13282bfd_boolean
13283bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13284 struct bfd_link_info *info)
13285{
13286 bfd *i;
13287 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13288 bfd_vma gotoff;
c152c796
AM
13289 struct alloc_got_off_arg gofarg;
13290
10455f89
HPN
13291 BFD_ASSERT (abfd == info->output_bfd);
13292
c152c796
AM
13293 if (! is_elf_hash_table (info->hash))
13294 return FALSE;
13295
13296 /* The GOT offset is relative to the .got section, but the GOT header is
13297 put into the .got.plt section, if the backend uses it. */
13298 if (bed->want_got_plt)
13299 gotoff = 0;
13300 else
13301 gotoff = bed->got_header_size;
13302
13303 /* Do the local .got entries first. */
c72f2fb2 13304 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13305 {
13306 bfd_signed_vma *local_got;
13307 bfd_size_type j, locsymcount;
13308 Elf_Internal_Shdr *symtab_hdr;
13309
13310 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13311 continue;
13312
13313 local_got = elf_local_got_refcounts (i);
13314 if (!local_got)
13315 continue;
13316
13317 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13318 if (elf_bad_symtab (i))
13319 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13320 else
13321 locsymcount = symtab_hdr->sh_info;
13322
13323 for (j = 0; j < locsymcount; ++j)
13324 {
13325 if (local_got[j] > 0)
13326 {
13327 local_got[j] = gotoff;
10455f89 13328 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13329 }
13330 else
13331 local_got[j] = (bfd_vma) -1;
13332 }
13333 }
13334
13335 /* Then the global .got entries. .plt refcounts are handled by
13336 adjust_dynamic_symbol */
13337 gofarg.gotoff = gotoff;
10455f89 13338 gofarg.info = info;
c152c796
AM
13339 elf_link_hash_traverse (elf_hash_table (info),
13340 elf_gc_allocate_got_offsets,
13341 &gofarg);
13342 return TRUE;
13343}
13344
13345/* Many folk need no more in the way of final link than this, once
13346 got entry reference counting is enabled. */
13347
13348bfd_boolean
13349bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13350{
13351 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13352 return FALSE;
13353
13354 /* Invoke the regular ELF backend linker to do all the work. */
13355 return bfd_elf_final_link (abfd, info);
13356}
13357
13358bfd_boolean
13359bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13360{
a50b1753 13361 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13362
13363 if (rcookie->bad_symtab)
13364 rcookie->rel = rcookie->rels;
13365
13366 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13367 {
13368 unsigned long r_symndx;
13369
13370 if (! rcookie->bad_symtab)
13371 if (rcookie->rel->r_offset > offset)
13372 return FALSE;
13373 if (rcookie->rel->r_offset != offset)
13374 continue;
13375
13376 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13377 if (r_symndx == STN_UNDEF)
c152c796
AM
13378 return TRUE;
13379
13380 if (r_symndx >= rcookie->locsymcount
13381 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13382 {
13383 struct elf_link_hash_entry *h;
13384
13385 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13386
13387 while (h->root.type == bfd_link_hash_indirect
13388 || h->root.type == bfd_link_hash_warning)
13389 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13390
13391 if ((h->root.type == bfd_link_hash_defined
13392 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13393 && (h->root.u.def.section->owner != rcookie->abfd
13394 || h->root.u.def.section->kept_section != NULL
13395 || discarded_section (h->root.u.def.section)))
c152c796 13396 return TRUE;
c152c796
AM
13397 }
13398 else
13399 {
13400 /* It's not a relocation against a global symbol,
13401 but it could be a relocation against a local
13402 symbol for a discarded section. */
13403 asection *isec;
13404 Elf_Internal_Sym *isym;
13405
13406 /* Need to: get the symbol; get the section. */
13407 isym = &rcookie->locsyms[r_symndx];
cb33740c 13408 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13409 if (isec != NULL
13410 && (isec->kept_section != NULL
13411 || discarded_section (isec)))
cb33740c 13412 return TRUE;
c152c796
AM
13413 }
13414 return FALSE;
13415 }
13416 return FALSE;
13417}
13418
13419/* Discard unneeded references to discarded sections.
75938853
AM
13420 Returns -1 on error, 1 if any section's size was changed, 0 if
13421 nothing changed. This function assumes that the relocations are in
13422 sorted order, which is true for all known assemblers. */
c152c796 13423
75938853 13424int
c152c796
AM
13425bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13426{
13427 struct elf_reloc_cookie cookie;
18cd5bce 13428 asection *o;
c152c796 13429 bfd *abfd;
75938853 13430 int changed = 0;
c152c796
AM
13431
13432 if (info->traditional_format
13433 || !is_elf_hash_table (info->hash))
75938853 13434 return 0;
c152c796 13435
18cd5bce
AM
13436 o = bfd_get_section_by_name (output_bfd, ".stab");
13437 if (o != NULL)
c152c796 13438 {
18cd5bce 13439 asection *i;
c152c796 13440
18cd5bce 13441 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13442 {
18cd5bce
AM
13443 if (i->size == 0
13444 || i->reloc_count == 0
13445 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13446 continue;
c152c796 13447
18cd5bce
AM
13448 abfd = i->owner;
13449 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13450 continue;
c152c796 13451
18cd5bce 13452 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13453 return -1;
c152c796 13454
18cd5bce
AM
13455 if (_bfd_discard_section_stabs (abfd, i,
13456 elf_section_data (i)->sec_info,
5241d853
RS
13457 bfd_elf_reloc_symbol_deleted_p,
13458 &cookie))
75938853 13459 changed = 1;
18cd5bce
AM
13460
13461 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13462 }
18cd5bce
AM
13463 }
13464
2f0c68f2
CM
13465 o = NULL;
13466 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13467 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13468 if (o != NULL)
13469 {
13470 asection *i;
c152c796 13471
18cd5bce 13472 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13473 {
18cd5bce
AM
13474 if (i->size == 0)
13475 continue;
13476
13477 abfd = i->owner;
13478 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13479 continue;
13480
13481 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13482 return -1;
18cd5bce
AM
13483
13484 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13485 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13486 bfd_elf_reloc_symbol_deleted_p,
13487 &cookie))
75938853 13488 changed = 1;
18cd5bce
AM
13489
13490 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13491 }
18cd5bce 13492 }
c152c796 13493
18cd5bce
AM
13494 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13495 {
13496 const struct elf_backend_data *bed;
c152c796 13497
18cd5bce
AM
13498 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13499 continue;
13500
13501 bed = get_elf_backend_data (abfd);
13502
13503 if (bed->elf_backend_discard_info != NULL)
13504 {
13505 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13506 return -1;
18cd5bce
AM
13507
13508 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13509 changed = 1;
18cd5bce
AM
13510
13511 fini_reloc_cookie (&cookie, abfd);
13512 }
c152c796
AM
13513 }
13514
2f0c68f2
CM
13515 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13516 _bfd_elf_end_eh_frame_parsing (info);
13517
13518 if (info->eh_frame_hdr_type
0e1862bb 13519 && !bfd_link_relocatable (info)
c152c796 13520 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13521 changed = 1;
c152c796 13522
75938853 13523 return changed;
c152c796 13524}
082b7297 13525
43e1669b 13526bfd_boolean
0c511000 13527_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13528 asection *sec,
c0f00686 13529 struct bfd_link_info *info)
082b7297
L
13530{
13531 flagword flags;
c77ec726 13532 const char *name, *key;
082b7297
L
13533 struct bfd_section_already_linked *l;
13534 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13535
c77ec726
AM
13536 if (sec->output_section == bfd_abs_section_ptr)
13537 return FALSE;
0c511000 13538
c77ec726 13539 flags = sec->flags;
0c511000 13540
c77ec726
AM
13541 /* Return if it isn't a linkonce section. A comdat group section
13542 also has SEC_LINK_ONCE set. */
13543 if ((flags & SEC_LINK_ONCE) == 0)
13544 return FALSE;
0c511000 13545
c77ec726
AM
13546 /* Don't put group member sections on our list of already linked
13547 sections. They are handled as a group via their group section. */
13548 if (elf_sec_group (sec) != NULL)
13549 return FALSE;
0c511000 13550
c77ec726
AM
13551 /* For a SHT_GROUP section, use the group signature as the key. */
13552 name = sec->name;
13553 if ((flags & SEC_GROUP) != 0
13554 && elf_next_in_group (sec) != NULL
13555 && elf_group_name (elf_next_in_group (sec)) != NULL)
13556 key = elf_group_name (elf_next_in_group (sec));
13557 else
13558 {
13559 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13560 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13561 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13562 key++;
0c511000 13563 else
c77ec726
AM
13564 /* Must be a user linkonce section that doesn't follow gcc's
13565 naming convention. In this case we won't be matching
13566 single member groups. */
13567 key = name;
0c511000 13568 }
6d2cd210 13569
c77ec726 13570 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13571
13572 for (l = already_linked_list->entry; l != NULL; l = l->next)
13573 {
c2370991 13574 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13575 sections with a signature of <key> (<key> is some string),
13576 and linkonce sections named .gnu.linkonce.<type>.<key>.
13577 Match like sections. LTO plugin sections are an exception.
13578 They are always named .gnu.linkonce.t.<key> and match either
13579 type of section. */
13580 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13581 && ((flags & SEC_GROUP) != 0
13582 || strcmp (name, l->sec->name) == 0))
13583 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13584 {
13585 /* The section has already been linked. See if we should
6d2cd210 13586 issue a warning. */
c77ec726
AM
13587 if (!_bfd_handle_already_linked (sec, l, info))
13588 return FALSE;
082b7297 13589
c77ec726 13590 if (flags & SEC_GROUP)
3d7f7666 13591 {
c77ec726
AM
13592 asection *first = elf_next_in_group (sec);
13593 asection *s = first;
3d7f7666 13594
c77ec726 13595 while (s != NULL)
3d7f7666 13596 {
c77ec726
AM
13597 s->output_section = bfd_abs_section_ptr;
13598 /* Record which group discards it. */
13599 s->kept_section = l->sec;
13600 s = elf_next_in_group (s);
13601 /* These lists are circular. */
13602 if (s == first)
13603 break;
3d7f7666
L
13604 }
13605 }
082b7297 13606
43e1669b 13607 return TRUE;
082b7297
L
13608 }
13609 }
13610
c77ec726
AM
13611 /* A single member comdat group section may be discarded by a
13612 linkonce section and vice versa. */
13613 if ((flags & SEC_GROUP) != 0)
3d7f7666 13614 {
c77ec726 13615 asection *first = elf_next_in_group (sec);
c2370991 13616
c77ec726
AM
13617 if (first != NULL && elf_next_in_group (first) == first)
13618 /* Check this single member group against linkonce sections. */
13619 for (l = already_linked_list->entry; l != NULL; l = l->next)
13620 if ((l->sec->flags & SEC_GROUP) == 0
13621 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13622 {
13623 first->output_section = bfd_abs_section_ptr;
13624 first->kept_section = l->sec;
13625 sec->output_section = bfd_abs_section_ptr;
13626 break;
13627 }
13628 }
13629 else
13630 /* Check this linkonce section against single member groups. */
13631 for (l = already_linked_list->entry; l != NULL; l = l->next)
13632 if (l->sec->flags & SEC_GROUP)
6d2cd210 13633 {
c77ec726 13634 asection *first = elf_next_in_group (l->sec);
6d2cd210 13635
c77ec726
AM
13636 if (first != NULL
13637 && elf_next_in_group (first) == first
13638 && bfd_elf_match_symbols_in_sections (first, sec, info))
13639 {
13640 sec->output_section = bfd_abs_section_ptr;
13641 sec->kept_section = first;
13642 break;
13643 }
6d2cd210 13644 }
0c511000 13645
c77ec726
AM
13646 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13647 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13648 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13649 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13650 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13651 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13652 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13653 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13654 The reverse order cannot happen as there is never a bfd with only the
13655 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13656 matter as here were are looking only for cross-bfd sections. */
13657
13658 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13659 for (l = already_linked_list->entry; l != NULL; l = l->next)
13660 if ((l->sec->flags & SEC_GROUP) == 0
13661 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13662 {
13663 if (abfd != l->sec->owner)
13664 sec->output_section = bfd_abs_section_ptr;
13665 break;
13666 }
80c29487 13667
082b7297 13668 /* This is the first section with this name. Record it. */
c77ec726 13669 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13670 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13671 return sec->output_section == bfd_abs_section_ptr;
082b7297 13672}
81e1b023 13673
a4d8e49b
L
13674bfd_boolean
13675_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13676{
13677 return sym->st_shndx == SHN_COMMON;
13678}
13679
13680unsigned int
13681_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13682{
13683 return SHN_COMMON;
13684}
13685
13686asection *
13687_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13688{
13689 return bfd_com_section_ptr;
13690}
10455f89
HPN
13691
13692bfd_vma
13693_bfd_elf_default_got_elt_size (bfd *abfd,
13694 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13695 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13696 bfd *ibfd ATTRIBUTE_UNUSED,
13697 unsigned long symndx ATTRIBUTE_UNUSED)
13698{
13699 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13700 return bed->s->arch_size / 8;
13701}
83bac4b0
NC
13702
13703/* Routines to support the creation of dynamic relocs. */
13704
83bac4b0
NC
13705/* Returns the name of the dynamic reloc section associated with SEC. */
13706
13707static const char *
13708get_dynamic_reloc_section_name (bfd * abfd,
13709 asection * sec,
13710 bfd_boolean is_rela)
13711{
ddcf1fcf
BS
13712 char *name;
13713 const char *old_name = bfd_get_section_name (NULL, sec);
13714 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13715
ddcf1fcf 13716 if (old_name == NULL)
83bac4b0
NC
13717 return NULL;
13718
ddcf1fcf 13719 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13720 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13721
13722 return name;
13723}
13724
13725/* Returns the dynamic reloc section associated with SEC.
13726 If necessary compute the name of the dynamic reloc section based
13727 on SEC's name (looked up in ABFD's string table) and the setting
13728 of IS_RELA. */
13729
13730asection *
13731_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13732 asection * sec,
13733 bfd_boolean is_rela)
13734{
13735 asection * reloc_sec = elf_section_data (sec)->sreloc;
13736
13737 if (reloc_sec == NULL)
13738 {
13739 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13740
13741 if (name != NULL)
13742 {
3d4d4302 13743 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
13744
13745 if (reloc_sec != NULL)
13746 elf_section_data (sec)->sreloc = reloc_sec;
13747 }
13748 }
13749
13750 return reloc_sec;
13751}
13752
13753/* Returns the dynamic reloc section associated with SEC. If the
13754 section does not exist it is created and attached to the DYNOBJ
13755 bfd and stored in the SRELOC field of SEC's elf_section_data
13756 structure.
f8076f98 13757
83bac4b0
NC
13758 ALIGNMENT is the alignment for the newly created section and
13759 IS_RELA defines whether the name should be .rela.<SEC's name>
13760 or .rel.<SEC's name>. The section name is looked up in the
13761 string table associated with ABFD. */
13762
13763asection *
ca4be51c
AM
13764_bfd_elf_make_dynamic_reloc_section (asection *sec,
13765 bfd *dynobj,
13766 unsigned int alignment,
13767 bfd *abfd,
13768 bfd_boolean is_rela)
83bac4b0
NC
13769{
13770 asection * reloc_sec = elf_section_data (sec)->sreloc;
13771
13772 if (reloc_sec == NULL)
13773 {
13774 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13775
13776 if (name == NULL)
13777 return NULL;
13778
3d4d4302 13779 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
13780
13781 if (reloc_sec == NULL)
13782 {
3d4d4302
AM
13783 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13784 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
13785 if ((sec->flags & SEC_ALLOC) != 0)
13786 flags |= SEC_ALLOC | SEC_LOAD;
13787
3d4d4302 13788 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
13789 if (reloc_sec != NULL)
13790 {
8877b5e5
AM
13791 /* _bfd_elf_get_sec_type_attr chooses a section type by
13792 name. Override as it may be wrong, eg. for a user
13793 section named "auto" we'll get ".relauto" which is
13794 seen to be a .rela section. */
13795 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
13796 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13797 reloc_sec = NULL;
13798 }
13799 }
13800
13801 elf_section_data (sec)->sreloc = reloc_sec;
13802 }
13803
13804 return reloc_sec;
13805}
1338dd10 13806
bffebb6b
AM
13807/* Copy the ELF symbol type and other attributes for a linker script
13808 assignment from HSRC to HDEST. Generally this should be treated as
13809 if we found a strong non-dynamic definition for HDEST (except that
13810 ld ignores multiple definition errors). */
1338dd10 13811void
bffebb6b
AM
13812_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13813 struct bfd_link_hash_entry *hdest,
13814 struct bfd_link_hash_entry *hsrc)
1338dd10 13815{
bffebb6b
AM
13816 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13817 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13818 Elf_Internal_Sym isym;
1338dd10
PB
13819
13820 ehdest->type = ehsrc->type;
35fc36a8 13821 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
13822
13823 isym.st_other = ehsrc->other;
b8417128 13824 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 13825}
351f65ca
L
13826
13827/* Append a RELA relocation REL to section S in BFD. */
13828
13829void
13830elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13831{
13832 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13833 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13834 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13835 bed->s->swap_reloca_out (abfd, rel, loc);
13836}
13837
13838/* Append a REL relocation REL to section S in BFD. */
13839
13840void
13841elf_append_rel (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_rel);
13845 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 13846 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 13847}
This page took 2.110293 seconds and 4 git commands to generate.