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
5b677558
AM
1205 /* If creating a default indirect symbol ("foo" or "foo@") from a
1206 dynamic versioned definition ("foo@@") skip doing so if there is
1207 an existing regular definition with a different type. We don't
1208 want, for example, a "time" variable in the executable overriding
1209 a "time" function in a shared library. */
580a2b6e 1210 if (pold_alignment == NULL
580a2b6e
L
1211 && newdyn
1212 && newdef
1213 && !olddyn
5b677558
AM
1214 && (olddef || h->root.type == bfd_link_hash_common)
1215 && ELF_ST_TYPE (sym->st_info) != h->type
1216 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1217 && h->type != STT_NOTYPE
1218 && !(newfunc && oldfunc))
580a2b6e
L
1219 {
1220 *skip = TRUE;
1221 return TRUE;
1222 }
1223
4c34aff8
AM
1224 /* Check TLS symbols. We don't check undefined symbols introduced
1225 by "ld -u" which have no type (and oldbfd NULL), and we don't
1226 check symbols from plugins because they also have no type. */
1227 if (oldbfd != NULL
1228 && (oldbfd->flags & BFD_PLUGIN) == 0
1229 && (abfd->flags & BFD_PLUGIN) == 0
1230 && ELF_ST_TYPE (sym->st_info) != h->type
1231 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1232 {
1233 bfd *ntbfd, *tbfd;
1234 bfd_boolean ntdef, tdef;
1235 asection *ntsec, *tsec;
1236
1237 if (h->type == STT_TLS)
1238 {
3b36f7e6 1239 ntbfd = abfd;
7479dfd4
L
1240 ntsec = sec;
1241 ntdef = newdef;
1242 tbfd = oldbfd;
1243 tsec = oldsec;
1244 tdef = olddef;
1245 }
1246 else
1247 {
1248 ntbfd = oldbfd;
1249 ntsec = oldsec;
1250 ntdef = olddef;
1251 tbfd = abfd;
1252 tsec = sec;
1253 tdef = newdef;
1254 }
1255
1256 if (tdef && ntdef)
1257 (*_bfd_error_handler)
191c0c42
AM
1258 (_("%s: TLS definition in %B section %A "
1259 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1260 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1261 else if (!tdef && !ntdef)
1262 (*_bfd_error_handler)
191c0c42
AM
1263 (_("%s: TLS reference in %B "
1264 "mismatches non-TLS reference in %B"),
7479dfd4
L
1265 tbfd, ntbfd, h->root.root.string);
1266 else if (tdef)
1267 (*_bfd_error_handler)
191c0c42
AM
1268 (_("%s: TLS definition in %B section %A "
1269 "mismatches non-TLS reference in %B"),
7479dfd4
L
1270 tbfd, tsec, ntbfd, h->root.root.string);
1271 else
1272 (*_bfd_error_handler)
191c0c42
AM
1273 (_("%s: TLS reference in %B "
1274 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1275 tbfd, ntbfd, ntsec, h->root.root.string);
1276
1277 bfd_set_error (bfd_error_bad_value);
1278 return FALSE;
1279 }
1280
45d6a902
AM
1281 /* If the old symbol has non-default visibility, we ignore the new
1282 definition from a dynamic object. */
1283 if (newdyn
9c7a29a3 1284 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1285 && !bfd_is_und_section (sec))
1286 {
1287 *skip = TRUE;
1288 /* Make sure this symbol is dynamic. */
f5385ebf 1289 h->ref_dynamic = 1;
90c984fc 1290 hi->ref_dynamic = 1;
45d6a902
AM
1291 /* A protected symbol has external availability. Make sure it is
1292 recorded as dynamic.
1293
1294 FIXME: Should we check type and size for protected symbol? */
1295 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1296 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1297 else
1298 return TRUE;
1299 }
1300 else if (!newdyn
9c7a29a3 1301 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1302 && h->def_dynamic)
45d6a902
AM
1303 {
1304 /* If the new symbol with non-default visibility comes from a
1305 relocatable file and the old definition comes from a dynamic
1306 object, we remove the old definition. */
6c9b78e6 1307 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1308 {
1309 /* Handle the case where the old dynamic definition is
1310 default versioned. We need to copy the symbol info from
1311 the symbol with default version to the normal one if it
1312 was referenced before. */
1313 if (h->ref_regular)
1314 {
6c9b78e6 1315 hi->root.type = h->root.type;
d2dee3b2 1316 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1317 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1318
6c9b78e6 1319 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1320 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1321 {
aed81c4e
MR
1322 /* If the new symbol is hidden or internal, completely undo
1323 any dynamic link state. */
1324 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1325 h->forced_local = 0;
1326 h->ref_dynamic = 0;
d2dee3b2
L
1327 }
1328 else
aed81c4e
MR
1329 h->ref_dynamic = 1;
1330
1331 h->def_dynamic = 0;
aed81c4e
MR
1332 /* FIXME: Should we check type and size for protected symbol? */
1333 h->size = 0;
1334 h->type = 0;
1335
6c9b78e6 1336 h = hi;
d2dee3b2
L
1337 }
1338 else
6c9b78e6 1339 h = hi;
d2dee3b2 1340 }
1de1a317 1341
f5eda473
AM
1342 /* If the old symbol was undefined before, then it will still be
1343 on the undefs list. If the new symbol is undefined or
1344 common, we can't make it bfd_link_hash_new here, because new
1345 undefined or common symbols will be added to the undefs list
1346 by _bfd_generic_link_add_one_symbol. Symbols may not be
1347 added twice to the undefs list. Also, if the new symbol is
1348 undefweak then we don't want to lose the strong undef. */
1349 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1350 {
1de1a317 1351 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1352 h->root.u.undef.abfd = abfd;
1353 }
1354 else
1355 {
1356 h->root.type = bfd_link_hash_new;
1357 h->root.u.undef.abfd = NULL;
1358 }
1359
f5eda473 1360 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1361 {
f5eda473
AM
1362 /* If the new symbol is hidden or internal, completely undo
1363 any dynamic link state. */
1364 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1365 h->forced_local = 0;
1366 h->ref_dynamic = 0;
45d6a902 1367 }
f5eda473
AM
1368 else
1369 h->ref_dynamic = 1;
1370 h->def_dynamic = 0;
45d6a902
AM
1371 /* FIXME: Should we check type and size for protected symbol? */
1372 h->size = 0;
1373 h->type = 0;
1374 return TRUE;
1375 }
14a793b2 1376
15b43f48
AM
1377 /* If a new weak symbol definition comes from a regular file and the
1378 old symbol comes from a dynamic library, we treat the new one as
1379 strong. Similarly, an old weak symbol definition from a regular
1380 file is treated as strong when the new symbol comes from a dynamic
1381 library. Further, an old weak symbol from a dynamic library is
1382 treated as strong if the new symbol is from a dynamic library.
1383 This reflects the way glibc's ld.so works.
1384
1385 Do this before setting *type_change_ok or *size_change_ok so that
1386 we warn properly when dynamic library symbols are overridden. */
1387
1388 if (newdef && !newdyn && olddyn)
0f8a2703 1389 newweak = FALSE;
15b43f48 1390 if (olddef && newdyn)
0f8a2703
AM
1391 oldweak = FALSE;
1392
d334575b 1393 /* Allow changes between different types of function symbol. */
0a36a439 1394 if (newfunc && oldfunc)
fcb93ecf
PB
1395 *type_change_ok = TRUE;
1396
79349b09
AM
1397 /* It's OK to change the type if either the existing symbol or the
1398 new symbol is weak. A type change is also OK if the old symbol
1399 is undefined and the new symbol is defined. */
252b5132 1400
79349b09
AM
1401 if (oldweak
1402 || newweak
1403 || (newdef
1404 && h->root.type == bfd_link_hash_undefined))
1405 *type_change_ok = TRUE;
1406
1407 /* It's OK to change the size if either the existing symbol or the
1408 new symbol is weak, or if the old symbol is undefined. */
1409
1410 if (*type_change_ok
1411 || h->root.type == bfd_link_hash_undefined)
1412 *size_change_ok = TRUE;
45d6a902 1413
45d6a902
AM
1414 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1415 symbol, respectively, appears to be a common symbol in a dynamic
1416 object. If a symbol appears in an uninitialized section, and is
1417 not weak, and is not a function, then it may be a common symbol
1418 which was resolved when the dynamic object was created. We want
1419 to treat such symbols specially, because they raise special
1420 considerations when setting the symbol size: if the symbol
1421 appears as a common symbol in a regular object, and the size in
1422 the regular object is larger, we must make sure that we use the
1423 larger size. This problematic case can always be avoided in C,
1424 but it must be handled correctly when using Fortran shared
1425 libraries.
1426
1427 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1428 likewise for OLDDYNCOMMON and OLDDEF.
1429
1430 Note that this test is just a heuristic, and that it is quite
1431 possible to have an uninitialized symbol in a shared object which
1432 is really a definition, rather than a common symbol. This could
1433 lead to some minor confusion when the symbol really is a common
1434 symbol in some regular object. However, I think it will be
1435 harmless. */
1436
1437 if (newdyn
1438 && newdef
79349b09 1439 && !newweak
45d6a902
AM
1440 && (sec->flags & SEC_ALLOC) != 0
1441 && (sec->flags & SEC_LOAD) == 0
1442 && sym->st_size > 0
0a36a439 1443 && !newfunc)
45d6a902
AM
1444 newdyncommon = TRUE;
1445 else
1446 newdyncommon = FALSE;
1447
1448 if (olddyn
1449 && olddef
1450 && h->root.type == bfd_link_hash_defined
f5385ebf 1451 && h->def_dynamic
45d6a902
AM
1452 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1453 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1454 && h->size > 0
0a36a439 1455 && !oldfunc)
45d6a902
AM
1456 olddyncommon = TRUE;
1457 else
1458 olddyncommon = FALSE;
1459
a4d8e49b
L
1460 /* We now know everything about the old and new symbols. We ask the
1461 backend to check if we can merge them. */
5d13b3b3
AM
1462 if (bed->merge_symbol != NULL)
1463 {
1464 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1465 return FALSE;
1466 sec = *psec;
1467 }
a4d8e49b 1468
45d6a902
AM
1469 /* If both the old and the new symbols look like common symbols in a
1470 dynamic object, set the size of the symbol to the larger of the
1471 two. */
1472
1473 if (olddyncommon
1474 && newdyncommon
1475 && sym->st_size != h->size)
1476 {
1477 /* Since we think we have two common symbols, issue a multiple
1478 common warning if desired. Note that we only warn if the
1479 size is different. If the size is the same, we simply let
1480 the old symbol override the new one as normally happens with
1481 symbols defined in dynamic objects. */
1482
1a72702b
AM
1483 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1484 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1485 if (sym->st_size > h->size)
1486 h->size = sym->st_size;
252b5132 1487
45d6a902 1488 *size_change_ok = TRUE;
252b5132
RH
1489 }
1490
45d6a902
AM
1491 /* If we are looking at a dynamic object, and we have found a
1492 definition, we need to see if the symbol was already defined by
1493 some other object. If so, we want to use the existing
1494 definition, and we do not want to report a multiple symbol
1495 definition error; we do this by clobbering *PSEC to be
1496 bfd_und_section_ptr.
1497
1498 We treat a common symbol as a definition if the symbol in the
1499 shared library is a function, since common symbols always
1500 represent variables; this can cause confusion in principle, but
1501 any such confusion would seem to indicate an erroneous program or
1502 shared library. We also permit a common symbol in a regular
202ac193
L
1503 object to override a weak symbol in a shared object. A common
1504 symbol in executable also overrides a symbol in a shared object. */
45d6a902
AM
1505
1506 if (newdyn
1507 && newdef
77cfaee6 1508 && (olddef
45d6a902 1509 || (h->root.type == bfd_link_hash_common
202ac193
L
1510 && (newweak
1511 || newfunc
1512 || (!olddyn && bfd_link_executable (info))))))
45d6a902
AM
1513 {
1514 *override = TRUE;
1515 newdef = FALSE;
1516 newdyncommon = FALSE;
252b5132 1517
45d6a902
AM
1518 *psec = sec = bfd_und_section_ptr;
1519 *size_change_ok = TRUE;
252b5132 1520
45d6a902
AM
1521 /* If we get here when the old symbol is a common symbol, then
1522 we are explicitly letting it override a weak symbol or
1523 function in a dynamic object, and we don't want to warn about
1524 a type change. If the old symbol is a defined symbol, a type
1525 change warning may still be appropriate. */
252b5132 1526
45d6a902
AM
1527 if (h->root.type == bfd_link_hash_common)
1528 *type_change_ok = TRUE;
1529 }
1530
1531 /* Handle the special case of an old common symbol merging with a
1532 new symbol which looks like a common symbol in a shared object.
1533 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1534 common symbol, and let _bfd_generic_link_add_one_symbol do the
1535 right thing. */
45d6a902
AM
1536
1537 if (newdyncommon
1538 && h->root.type == bfd_link_hash_common)
1539 {
1540 *override = TRUE;
1541 newdef = FALSE;
1542 newdyncommon = FALSE;
1543 *pvalue = sym->st_size;
a4d8e49b 1544 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1545 *size_change_ok = TRUE;
1546 }
1547
c5e2cead 1548 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1549 if (newdef && olddef && newweak)
54ac0771 1550 {
35ed3f94 1551 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1552 if (!(oldbfd != NULL
1553 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1554 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1555 {
1556 newdef = FALSE;
1557 *skip = TRUE;
1558 }
54ac0771
L
1559
1560 /* Merge st_other. If the symbol already has a dynamic index,
1561 but visibility says it should not be visible, turn it into a
1562 local symbol. */
b8417128 1563 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1564 if (h->dynindx != -1)
1565 switch (ELF_ST_VISIBILITY (h->other))
1566 {
1567 case STV_INTERNAL:
1568 case STV_HIDDEN:
1569 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1570 break;
1571 }
1572 }
c5e2cead 1573
45d6a902
AM
1574 /* If the old symbol is from a dynamic object, and the new symbol is
1575 a definition which is not from a dynamic object, then the new
1576 symbol overrides the old symbol. Symbols from regular files
1577 always take precedence over symbols from dynamic objects, even if
1578 they are defined after the dynamic object in the link.
1579
1580 As above, we again permit a common symbol in a regular object to
1581 override a definition in a shared object if the shared object
0f8a2703 1582 symbol is a function or is weak. */
45d6a902
AM
1583
1584 flip = NULL;
77cfaee6 1585 if (!newdyn
45d6a902
AM
1586 && (newdef
1587 || (bfd_is_com_section (sec)
0a36a439 1588 && (oldweak || oldfunc)))
45d6a902
AM
1589 && olddyn
1590 && olddef
f5385ebf 1591 && h->def_dynamic)
45d6a902
AM
1592 {
1593 /* Change the hash table entry to undefined, and let
1594 _bfd_generic_link_add_one_symbol do the right thing with the
1595 new definition. */
1596
1597 h->root.type = bfd_link_hash_undefined;
1598 h->root.u.undef.abfd = h->root.u.def.section->owner;
1599 *size_change_ok = TRUE;
1600
1601 olddef = FALSE;
1602 olddyncommon = FALSE;
1603
1604 /* We again permit a type change when a common symbol may be
1605 overriding a function. */
1606
1607 if (bfd_is_com_section (sec))
0a36a439
L
1608 {
1609 if (oldfunc)
1610 {
1611 /* If a common symbol overrides a function, make sure
1612 that it isn't defined dynamically nor has type
1613 function. */
1614 h->def_dynamic = 0;
1615 h->type = STT_NOTYPE;
1616 }
1617 *type_change_ok = TRUE;
1618 }
45d6a902 1619
6c9b78e6
AM
1620 if (hi->root.type == bfd_link_hash_indirect)
1621 flip = hi;
45d6a902
AM
1622 else
1623 /* This union may have been set to be non-NULL when this symbol
1624 was seen in a dynamic object. We must force the union to be
1625 NULL, so that it is correct for a regular symbol. */
1626 h->verinfo.vertree = NULL;
1627 }
1628
1629 /* Handle the special case of a new common symbol merging with an
1630 old symbol that looks like it might be a common symbol defined in
1631 a shared object. Note that we have already handled the case in
1632 which a new common symbol should simply override the definition
1633 in the shared library. */
1634
1635 if (! newdyn
1636 && bfd_is_com_section (sec)
1637 && olddyncommon)
1638 {
1639 /* It would be best if we could set the hash table entry to a
1640 common symbol, but we don't know what to use for the section
1641 or the alignment. */
1a72702b
AM
1642 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1643 bfd_link_hash_common, sym->st_size);
45d6a902 1644
4cc11e76 1645 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1646 larger, pretend that the new symbol has its size. */
1647
1648 if (h->size > *pvalue)
1649 *pvalue = h->size;
1650
af44c138
L
1651 /* We need to remember the alignment required by the symbol
1652 in the dynamic object. */
1653 BFD_ASSERT (pold_alignment);
1654 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1655
1656 olddef = FALSE;
1657 olddyncommon = FALSE;
1658
1659 h->root.type = bfd_link_hash_undefined;
1660 h->root.u.undef.abfd = h->root.u.def.section->owner;
1661
1662 *size_change_ok = TRUE;
1663 *type_change_ok = TRUE;
1664
6c9b78e6
AM
1665 if (hi->root.type == bfd_link_hash_indirect)
1666 flip = hi;
45d6a902
AM
1667 else
1668 h->verinfo.vertree = NULL;
1669 }
1670
1671 if (flip != NULL)
1672 {
1673 /* Handle the case where we had a versioned symbol in a dynamic
1674 library and now find a definition in a normal object. In this
1675 case, we make the versioned symbol point to the normal one. */
45d6a902 1676 flip->root.type = h->root.type;
00cbee0a 1677 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1678 h->root.type = bfd_link_hash_indirect;
1679 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1680 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1681 if (h->def_dynamic)
45d6a902 1682 {
f5385ebf
AM
1683 h->def_dynamic = 0;
1684 flip->ref_dynamic = 1;
45d6a902
AM
1685 }
1686 }
1687
45d6a902
AM
1688 return TRUE;
1689}
1690
1691/* This function is called to create an indirect symbol from the
1692 default for the symbol with the default version if needed. The
4f3fedcf 1693 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1694 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1695
28caa186 1696static bfd_boolean
268b6b39
AM
1697_bfd_elf_add_default_symbol (bfd *abfd,
1698 struct bfd_link_info *info,
1699 struct elf_link_hash_entry *h,
1700 const char *name,
1701 Elf_Internal_Sym *sym,
4f3fedcf
AM
1702 asection *sec,
1703 bfd_vma value,
1704 bfd **poldbfd,
e3c9d234 1705 bfd_boolean *dynsym)
45d6a902
AM
1706{
1707 bfd_boolean type_change_ok;
1708 bfd_boolean size_change_ok;
1709 bfd_boolean skip;
1710 char *shortname;
1711 struct elf_link_hash_entry *hi;
1712 struct bfd_link_hash_entry *bh;
9c5bfbb7 1713 const struct elf_backend_data *bed;
45d6a902
AM
1714 bfd_boolean collect;
1715 bfd_boolean dynamic;
e3c9d234 1716 bfd_boolean override;
45d6a902
AM
1717 char *p;
1718 size_t len, shortlen;
ffd65175 1719 asection *tmp_sec;
6e33951e 1720 bfd_boolean matched;
45d6a902 1721
422f1182
L
1722 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1723 return TRUE;
1724
45d6a902
AM
1725 /* If this symbol has a version, and it is the default version, we
1726 create an indirect symbol from the default name to the fully
1727 decorated name. This will cause external references which do not
1728 specify a version to be bound to this version of the symbol. */
1729 p = strchr (name, ELF_VER_CHR);
422f1182
L
1730 if (h->versioned == unknown)
1731 {
1732 if (p == NULL)
1733 {
1734 h->versioned = unversioned;
1735 return TRUE;
1736 }
1737 else
1738 {
1739 if (p[1] != ELF_VER_CHR)
1740 {
1741 h->versioned = versioned_hidden;
1742 return TRUE;
1743 }
1744 else
1745 h->versioned = versioned;
1746 }
1747 }
4373f8af
L
1748 else
1749 {
1750 /* PR ld/19073: We may see an unversioned definition after the
1751 default version. */
1752 if (p == NULL)
1753 return TRUE;
1754 }
45d6a902 1755
45d6a902
AM
1756 bed = get_elf_backend_data (abfd);
1757 collect = bed->collect;
1758 dynamic = (abfd->flags & DYNAMIC) != 0;
1759
1760 shortlen = p - name;
a50b1753 1761 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1762 if (shortname == NULL)
1763 return FALSE;
1764 memcpy (shortname, name, shortlen);
1765 shortname[shortlen] = '\0';
1766
1767 /* We are going to create a new symbol. Merge it with any existing
1768 symbol with this name. For the purposes of the merge, act as
1769 though we were defining the symbol we just defined, although we
1770 actually going to define an indirect symbol. */
1771 type_change_ok = FALSE;
1772 size_change_ok = FALSE;
6e33951e 1773 matched = TRUE;
ffd65175
AM
1774 tmp_sec = sec;
1775 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1776 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1777 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1778 return FALSE;
1779
1780 if (skip)
1781 goto nondefault;
1782
5b677558
AM
1783 if (hi->def_regular)
1784 {
1785 /* If the undecorated symbol will have a version added by a
1786 script different to H, then don't indirect to/from the
1787 undecorated symbol. This isn't ideal because we may not yet
1788 have seen symbol versions, if given by a script on the
1789 command line rather than via --version-script. */
1790 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1791 {
1792 bfd_boolean hide;
1793
1794 hi->verinfo.vertree
1795 = bfd_find_version_for_sym (info->version_info,
1796 hi->root.root.string, &hide);
1797 if (hi->verinfo.vertree != NULL && hide)
1798 {
1799 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1800 goto nondefault;
1801 }
1802 }
1803 if (hi->verinfo.vertree != NULL
1804 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1805 goto nondefault;
1806 }
1807
45d6a902
AM
1808 if (! override)
1809 {
c6e8a9a8 1810 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1811 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1812 {
1813 bh = &hi->root;
1814 if (! (_bfd_generic_link_add_one_symbol
1815 (info, abfd, shortname, BSF_INDIRECT,
1816 bfd_ind_section_ptr,
1817 0, name, FALSE, collect, &bh)))
1818 return FALSE;
1819 hi = (struct elf_link_hash_entry *) bh;
1820 }
45d6a902
AM
1821 }
1822 else
1823 {
1824 /* In this case the symbol named SHORTNAME is overriding the
1825 indirect symbol we want to add. We were planning on making
1826 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1827 is the name without a version. NAME is the fully versioned
1828 name, and it is the default version.
1829
1830 Overriding means that we already saw a definition for the
1831 symbol SHORTNAME in a regular object, and it is overriding
1832 the symbol defined in the dynamic object.
1833
1834 When this happens, we actually want to change NAME, the
1835 symbol we just added, to refer to SHORTNAME. This will cause
1836 references to NAME in the shared object to become references
1837 to SHORTNAME in the regular object. This is what we expect
1838 when we override a function in a shared object: that the
1839 references in the shared object will be mapped to the
1840 definition in the regular object. */
1841
1842 while (hi->root.type == bfd_link_hash_indirect
1843 || hi->root.type == bfd_link_hash_warning)
1844 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1845
1846 h->root.type = bfd_link_hash_indirect;
1847 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1848 if (h->def_dynamic)
45d6a902 1849 {
f5385ebf
AM
1850 h->def_dynamic = 0;
1851 hi->ref_dynamic = 1;
1852 if (hi->ref_regular
1853 || hi->def_regular)
45d6a902 1854 {
c152c796 1855 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1856 return FALSE;
1857 }
1858 }
1859
1860 /* Now set HI to H, so that the following code will set the
1861 other fields correctly. */
1862 hi = h;
1863 }
1864
fab4a87f
L
1865 /* Check if HI is a warning symbol. */
1866 if (hi->root.type == bfd_link_hash_warning)
1867 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1868
45d6a902
AM
1869 /* If there is a duplicate definition somewhere, then HI may not
1870 point to an indirect symbol. We will have reported an error to
1871 the user in that case. */
1872
1873 if (hi->root.type == bfd_link_hash_indirect)
1874 {
1875 struct elf_link_hash_entry *ht;
1876
45d6a902 1877 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1878 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1879
68c88cd4
AM
1880 /* A reference to the SHORTNAME symbol from a dynamic library
1881 will be satisfied by the versioned symbol at runtime. In
1882 effect, we have a reference to the versioned symbol. */
1883 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1884 hi->dynamic_def |= ht->dynamic_def;
1885
45d6a902
AM
1886 /* See if the new flags lead us to realize that the symbol must
1887 be dynamic. */
1888 if (! *dynsym)
1889 {
1890 if (! dynamic)
1891 {
0e1862bb 1892 if (! bfd_link_executable (info)
90c984fc 1893 || hi->def_dynamic
f5385ebf 1894 || hi->ref_dynamic)
45d6a902
AM
1895 *dynsym = TRUE;
1896 }
1897 else
1898 {
f5385ebf 1899 if (hi->ref_regular)
45d6a902
AM
1900 *dynsym = TRUE;
1901 }
1902 }
1903 }
1904
1905 /* We also need to define an indirection from the nondefault version
1906 of the symbol. */
1907
1908nondefault:
1909 len = strlen (name);
a50b1753 1910 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1911 if (shortname == NULL)
1912 return FALSE;
1913 memcpy (shortname, name, shortlen);
1914 memcpy (shortname + shortlen, p + 1, len - shortlen);
1915
1916 /* Once again, merge with any existing symbol. */
1917 type_change_ok = FALSE;
1918 size_change_ok = FALSE;
ffd65175
AM
1919 tmp_sec = sec;
1920 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1921 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1922 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1923 return FALSE;
1924
1925 if (skip)
1926 return TRUE;
1927
1928 if (override)
1929 {
1930 /* Here SHORTNAME is a versioned name, so we don't expect to see
1931 the type of override we do in the case above unless it is
4cc11e76 1932 overridden by a versioned definition. */
45d6a902
AM
1933 if (hi->root.type != bfd_link_hash_defined
1934 && hi->root.type != bfd_link_hash_defweak)
1935 (*_bfd_error_handler)
d003868e
AM
1936 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1937 abfd, shortname);
45d6a902
AM
1938 }
1939 else
1940 {
1941 bh = &hi->root;
1942 if (! (_bfd_generic_link_add_one_symbol
1943 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1944 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1945 return FALSE;
1946 hi = (struct elf_link_hash_entry *) bh;
1947
1948 /* If there is a duplicate definition somewhere, then HI may not
1949 point to an indirect symbol. We will have reported an error
1950 to the user in that case. */
1951
1952 if (hi->root.type == bfd_link_hash_indirect)
1953 {
fcfa13d2 1954 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
1955 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1956 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
1957
1958 /* See if the new flags lead us to realize that the symbol
1959 must be dynamic. */
1960 if (! *dynsym)
1961 {
1962 if (! dynamic)
1963 {
0e1862bb 1964 if (! bfd_link_executable (info)
f5385ebf 1965 || hi->ref_dynamic)
45d6a902
AM
1966 *dynsym = TRUE;
1967 }
1968 else
1969 {
f5385ebf 1970 if (hi->ref_regular)
45d6a902
AM
1971 *dynsym = TRUE;
1972 }
1973 }
1974 }
1975 }
1976
1977 return TRUE;
1978}
1979\f
1980/* This routine is used to export all defined symbols into the dynamic
1981 symbol table. It is called via elf_link_hash_traverse. */
1982
28caa186 1983static bfd_boolean
268b6b39 1984_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1985{
a50b1753 1986 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
1987
1988 /* Ignore indirect symbols. These are added by the versioning code. */
1989 if (h->root.type == bfd_link_hash_indirect)
1990 return TRUE;
1991
7686d77d
AM
1992 /* Ignore this if we won't export it. */
1993 if (!eif->info->export_dynamic && !h->dynamic)
1994 return TRUE;
45d6a902
AM
1995
1996 if (h->dynindx == -1
fd91d419
L
1997 && (h->def_regular || h->ref_regular)
1998 && ! bfd_hide_sym_by_version (eif->info->version_info,
1999 h->root.root.string))
45d6a902 2000 {
fd91d419 2001 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2002 {
fd91d419
L
2003 eif->failed = TRUE;
2004 return FALSE;
45d6a902
AM
2005 }
2006 }
2007
2008 return TRUE;
2009}
2010\f
2011/* Look through the symbols which are defined in other shared
2012 libraries and referenced here. Update the list of version
2013 dependencies. This will be put into the .gnu.version_r section.
2014 This function is called via elf_link_hash_traverse. */
2015
28caa186 2016static bfd_boolean
268b6b39
AM
2017_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2018 void *data)
45d6a902 2019{
a50b1753 2020 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2021 Elf_Internal_Verneed *t;
2022 Elf_Internal_Vernaux *a;
2023 bfd_size_type amt;
2024
45d6a902
AM
2025 /* We only care about symbols defined in shared objects with version
2026 information. */
f5385ebf
AM
2027 if (!h->def_dynamic
2028 || h->def_regular
45d6a902 2029 || h->dynindx == -1
7b20f099
AM
2030 || h->verinfo.verdef == NULL
2031 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2032 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2033 return TRUE;
2034
2035 /* See if we already know about this version. */
28caa186
AM
2036 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2037 t != NULL;
2038 t = t->vn_nextref)
45d6a902
AM
2039 {
2040 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2041 continue;
2042
2043 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2044 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2045 return TRUE;
2046
2047 break;
2048 }
2049
2050 /* This is a new version. Add it to tree we are building. */
2051
2052 if (t == NULL)
2053 {
2054 amt = sizeof *t;
a50b1753 2055 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2056 if (t == NULL)
2057 {
2058 rinfo->failed = TRUE;
2059 return FALSE;
2060 }
2061
2062 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2063 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2064 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2065 }
2066
2067 amt = sizeof *a;
a50b1753 2068 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2069 if (a == NULL)
2070 {
2071 rinfo->failed = TRUE;
2072 return FALSE;
2073 }
45d6a902
AM
2074
2075 /* Note that we are copying a string pointer here, and testing it
2076 above. If bfd_elf_string_from_elf_section is ever changed to
2077 discard the string data when low in memory, this will have to be
2078 fixed. */
2079 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2080
2081 a->vna_flags = h->verinfo.verdef->vd_flags;
2082 a->vna_nextptr = t->vn_auxptr;
2083
2084 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2085 ++rinfo->vers;
2086
2087 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2088
2089 t->vn_auxptr = a;
2090
2091 return TRUE;
2092}
2093
2094/* Figure out appropriate versions for all the symbols. We may not
2095 have the version number script until we have read all of the input
2096 files, so until that point we don't know which symbols should be
2097 local. This function is called via elf_link_hash_traverse. */
2098
28caa186 2099static bfd_boolean
268b6b39 2100_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2101{
28caa186 2102 struct elf_info_failed *sinfo;
45d6a902 2103 struct bfd_link_info *info;
9c5bfbb7 2104 const struct elf_backend_data *bed;
45d6a902
AM
2105 struct elf_info_failed eif;
2106 char *p;
2107 bfd_size_type amt;
2108
a50b1753 2109 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2110 info = sinfo->info;
2111
45d6a902
AM
2112 /* Fix the symbol flags. */
2113 eif.failed = FALSE;
2114 eif.info = info;
2115 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2116 {
2117 if (eif.failed)
2118 sinfo->failed = TRUE;
2119 return FALSE;
2120 }
2121
2122 /* We only need version numbers for symbols defined in regular
2123 objects. */
f5385ebf 2124 if (!h->def_regular)
45d6a902
AM
2125 return TRUE;
2126
28caa186 2127 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2128 p = strchr (h->root.root.string, ELF_VER_CHR);
2129 if (p != NULL && h->verinfo.vertree == NULL)
2130 {
2131 struct bfd_elf_version_tree *t;
45d6a902 2132
45d6a902
AM
2133 ++p;
2134 if (*p == ELF_VER_CHR)
6e33951e 2135 ++p;
45d6a902
AM
2136
2137 /* If there is no version string, we can just return out. */
2138 if (*p == '\0')
6e33951e 2139 return TRUE;
45d6a902
AM
2140
2141 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2142 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2143 {
2144 if (strcmp (t->name, p) == 0)
2145 {
2146 size_t len;
2147 char *alc;
2148 struct bfd_elf_version_expr *d;
2149
2150 len = p - h->root.root.string;
a50b1753 2151 alc = (char *) bfd_malloc (len);
45d6a902 2152 if (alc == NULL)
14b1c01e
AM
2153 {
2154 sinfo->failed = TRUE;
2155 return FALSE;
2156 }
45d6a902
AM
2157 memcpy (alc, h->root.root.string, len - 1);
2158 alc[len - 1] = '\0';
2159 if (alc[len - 2] == ELF_VER_CHR)
2160 alc[len - 2] = '\0';
2161
2162 h->verinfo.vertree = t;
2163 t->used = TRUE;
2164 d = NULL;
2165
108ba305
JJ
2166 if (t->globals.list != NULL)
2167 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2168
2169 /* See if there is anything to force this symbol to
2170 local scope. */
108ba305 2171 if (d == NULL && t->locals.list != NULL)
45d6a902 2172 {
108ba305
JJ
2173 d = (*t->match) (&t->locals, NULL, alc);
2174 if (d != NULL
2175 && h->dynindx != -1
108ba305
JJ
2176 && ! info->export_dynamic)
2177 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2178 }
2179
2180 free (alc);
2181 break;
2182 }
2183 }
2184
2185 /* If we are building an application, we need to create a
2186 version node for this version. */
0e1862bb 2187 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2188 {
2189 struct bfd_elf_version_tree **pp;
2190 int version_index;
2191
2192 /* If we aren't going to export this symbol, we don't need
2193 to worry about it. */
2194 if (h->dynindx == -1)
2195 return TRUE;
2196
2197 amt = sizeof *t;
a50b1753 2198 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
45d6a902
AM
2199 if (t == NULL)
2200 {
2201 sinfo->failed = TRUE;
2202 return FALSE;
2203 }
2204
45d6a902 2205 t->name = p;
45d6a902
AM
2206 t->name_indx = (unsigned int) -1;
2207 t->used = TRUE;
2208
2209 version_index = 1;
2210 /* Don't count anonymous version tag. */
fd91d419
L
2211 if (sinfo->info->version_info != NULL
2212 && sinfo->info->version_info->vernum == 0)
45d6a902 2213 version_index = 0;
fd91d419
L
2214 for (pp = &sinfo->info->version_info;
2215 *pp != NULL;
2216 pp = &(*pp)->next)
45d6a902
AM
2217 ++version_index;
2218 t->vernum = version_index;
2219
2220 *pp = t;
2221
2222 h->verinfo.vertree = t;
2223 }
2224 else if (t == NULL)
2225 {
2226 /* We could not find the version for a symbol when
2227 generating a shared archive. Return an error. */
2228 (*_bfd_error_handler)
c55fe096 2229 (_("%B: version node not found for symbol %s"),
28caa186 2230 info->output_bfd, h->root.root.string);
45d6a902
AM
2231 bfd_set_error (bfd_error_bad_value);
2232 sinfo->failed = TRUE;
2233 return FALSE;
2234 }
45d6a902
AM
2235 }
2236
2237 /* If we don't have a version for this symbol, see if we can find
2238 something. */
fd91d419 2239 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2240 {
1e8fa21e 2241 bfd_boolean hide;
ae5a3597 2242
fd91d419
L
2243 h->verinfo.vertree
2244 = bfd_find_version_for_sym (sinfo->info->version_info,
2245 h->root.root.string, &hide);
1e8fa21e
AM
2246 if (h->verinfo.vertree != NULL && hide)
2247 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2248 }
2249
2250 return TRUE;
2251}
2252\f
45d6a902
AM
2253/* Read and swap the relocs from the section indicated by SHDR. This
2254 may be either a REL or a RELA section. The relocations are
2255 translated into RELA relocations and stored in INTERNAL_RELOCS,
2256 which should have already been allocated to contain enough space.
2257 The EXTERNAL_RELOCS are a buffer where the external form of the
2258 relocations should be stored.
2259
2260 Returns FALSE if something goes wrong. */
2261
2262static bfd_boolean
268b6b39 2263elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2264 asection *sec,
268b6b39
AM
2265 Elf_Internal_Shdr *shdr,
2266 void *external_relocs,
2267 Elf_Internal_Rela *internal_relocs)
45d6a902 2268{
9c5bfbb7 2269 const struct elf_backend_data *bed;
268b6b39 2270 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2271 const bfd_byte *erela;
2272 const bfd_byte *erelaend;
2273 Elf_Internal_Rela *irela;
243ef1e0
L
2274 Elf_Internal_Shdr *symtab_hdr;
2275 size_t nsyms;
45d6a902 2276
45d6a902
AM
2277 /* Position ourselves at the start of the section. */
2278 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2279 return FALSE;
2280
2281 /* Read the relocations. */
2282 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2283 return FALSE;
2284
243ef1e0 2285 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2286 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2287
45d6a902
AM
2288 bed = get_elf_backend_data (abfd);
2289
2290 /* Convert the external relocations to the internal format. */
2291 if (shdr->sh_entsize == bed->s->sizeof_rel)
2292 swap_in = bed->s->swap_reloc_in;
2293 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2294 swap_in = bed->s->swap_reloca_in;
2295 else
2296 {
2297 bfd_set_error (bfd_error_wrong_format);
2298 return FALSE;
2299 }
2300
a50b1753 2301 erela = (const bfd_byte *) external_relocs;
51992aec 2302 erelaend = erela + shdr->sh_size;
45d6a902
AM
2303 irela = internal_relocs;
2304 while (erela < erelaend)
2305 {
243ef1e0
L
2306 bfd_vma r_symndx;
2307
45d6a902 2308 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2309 r_symndx = ELF32_R_SYM (irela->r_info);
2310 if (bed->s->arch_size == 64)
2311 r_symndx >>= 24;
ce98a316
NC
2312 if (nsyms > 0)
2313 {
2314 if ((size_t) r_symndx >= nsyms)
2315 {
2316 (*_bfd_error_handler)
2317 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2318 " for offset 0x%lx in section `%A'"),
2319 abfd, sec,
2320 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2321 bfd_set_error (bfd_error_bad_value);
2322 return FALSE;
2323 }
2324 }
cf35638d 2325 else if (r_symndx != STN_UNDEF)
243ef1e0
L
2326 {
2327 (*_bfd_error_handler)
ce98a316
NC
2328 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2329 " when the object file has no symbol table"),
d003868e
AM
2330 abfd, sec,
2331 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2332 bfd_set_error (bfd_error_bad_value);
2333 return FALSE;
2334 }
45d6a902
AM
2335 irela += bed->s->int_rels_per_ext_rel;
2336 erela += shdr->sh_entsize;
2337 }
2338
2339 return TRUE;
2340}
2341
2342/* Read and swap the relocs for a section O. They may have been
2343 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2344 not NULL, they are used as buffers to read into. They are known to
2345 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2346 the return value is allocated using either malloc or bfd_alloc,
2347 according to the KEEP_MEMORY argument. If O has two relocation
2348 sections (both REL and RELA relocations), then the REL_HDR
2349 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2350 RELA_HDR relocations. */
45d6a902
AM
2351
2352Elf_Internal_Rela *
268b6b39
AM
2353_bfd_elf_link_read_relocs (bfd *abfd,
2354 asection *o,
2355 void *external_relocs,
2356 Elf_Internal_Rela *internal_relocs,
2357 bfd_boolean keep_memory)
45d6a902 2358{
268b6b39 2359 void *alloc1 = NULL;
45d6a902 2360 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2361 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2362 struct bfd_elf_section_data *esdo = elf_section_data (o);
2363 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2364
d4730f92
BS
2365 if (esdo->relocs != NULL)
2366 return esdo->relocs;
45d6a902
AM
2367
2368 if (o->reloc_count == 0)
2369 return NULL;
2370
45d6a902
AM
2371 if (internal_relocs == NULL)
2372 {
2373 bfd_size_type size;
2374
2375 size = o->reloc_count;
2376 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2377 if (keep_memory)
a50b1753 2378 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2379 else
a50b1753 2380 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2381 if (internal_relocs == NULL)
2382 goto error_return;
2383 }
2384
2385 if (external_relocs == NULL)
2386 {
d4730f92
BS
2387 bfd_size_type size = 0;
2388
2389 if (esdo->rel.hdr)
2390 size += esdo->rel.hdr->sh_size;
2391 if (esdo->rela.hdr)
2392 size += esdo->rela.hdr->sh_size;
45d6a902 2393
268b6b39 2394 alloc1 = bfd_malloc (size);
45d6a902
AM
2395 if (alloc1 == NULL)
2396 goto error_return;
2397 external_relocs = alloc1;
2398 }
2399
d4730f92
BS
2400 internal_rela_relocs = internal_relocs;
2401 if (esdo->rel.hdr)
2402 {
2403 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2404 external_relocs,
2405 internal_relocs))
2406 goto error_return;
2407 external_relocs = (((bfd_byte *) external_relocs)
2408 + esdo->rel.hdr->sh_size);
2409 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2410 * bed->s->int_rels_per_ext_rel);
2411 }
2412
2413 if (esdo->rela.hdr
2414 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2415 external_relocs,
2416 internal_rela_relocs)))
45d6a902
AM
2417 goto error_return;
2418
2419 /* Cache the results for next time, if we can. */
2420 if (keep_memory)
d4730f92 2421 esdo->relocs = internal_relocs;
45d6a902
AM
2422
2423 if (alloc1 != NULL)
2424 free (alloc1);
2425
2426 /* Don't free alloc2, since if it was allocated we are passing it
2427 back (under the name of internal_relocs). */
2428
2429 return internal_relocs;
2430
2431 error_return:
2432 if (alloc1 != NULL)
2433 free (alloc1);
2434 if (alloc2 != NULL)
4dd07732
AM
2435 {
2436 if (keep_memory)
2437 bfd_release (abfd, alloc2);
2438 else
2439 free (alloc2);
2440 }
45d6a902
AM
2441 return NULL;
2442}
2443
2444/* Compute the size of, and allocate space for, REL_HDR which is the
2445 section header for a section containing relocations for O. */
2446
28caa186 2447static bfd_boolean
268b6b39 2448_bfd_elf_link_size_reloc_section (bfd *abfd,
d4730f92 2449 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2450{
d4730f92 2451 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2452
2453 /* That allows us to calculate the size of the section. */
d4730f92 2454 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2455
2456 /* The contents field must last into write_object_contents, so we
2457 allocate it with bfd_alloc rather than malloc. Also since we
2458 cannot be sure that the contents will actually be filled in,
2459 we zero the allocated space. */
a50b1753 2460 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2461 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2462 return FALSE;
2463
d4730f92 2464 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2465 {
2466 struct elf_link_hash_entry **p;
2467
ca4be51c
AM
2468 p = ((struct elf_link_hash_entry **)
2469 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2470 if (p == NULL)
2471 return FALSE;
2472
d4730f92 2473 reldata->hashes = p;
45d6a902
AM
2474 }
2475
2476 return TRUE;
2477}
2478
2479/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2480 originated from the section given by INPUT_REL_HDR) to the
2481 OUTPUT_BFD. */
2482
2483bfd_boolean
268b6b39
AM
2484_bfd_elf_link_output_relocs (bfd *output_bfd,
2485 asection *input_section,
2486 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2487 Elf_Internal_Rela *internal_relocs,
2488 struct elf_link_hash_entry **rel_hash
2489 ATTRIBUTE_UNUSED)
45d6a902
AM
2490{
2491 Elf_Internal_Rela *irela;
2492 Elf_Internal_Rela *irelaend;
2493 bfd_byte *erel;
d4730f92 2494 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2495 asection *output_section;
9c5bfbb7 2496 const struct elf_backend_data *bed;
268b6b39 2497 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2498 struct bfd_elf_section_data *esdo;
45d6a902
AM
2499
2500 output_section = input_section->output_section;
45d6a902 2501
d4730f92
BS
2502 bed = get_elf_backend_data (output_bfd);
2503 esdo = elf_section_data (output_section);
2504 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2505 {
d4730f92
BS
2506 output_reldata = &esdo->rel;
2507 swap_out = bed->s->swap_reloc_out;
45d6a902 2508 }
d4730f92
BS
2509 else if (esdo->rela.hdr
2510 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2511 {
d4730f92
BS
2512 output_reldata = &esdo->rela;
2513 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2514 }
2515 else
2516 {
2517 (*_bfd_error_handler)
d003868e
AM
2518 (_("%B: relocation size mismatch in %B section %A"),
2519 output_bfd, input_section->owner, input_section);
297d8443 2520 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2521 return FALSE;
2522 }
2523
d4730f92
BS
2524 erel = output_reldata->hdr->contents;
2525 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2526 irela = internal_relocs;
2527 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2528 * bed->s->int_rels_per_ext_rel);
2529 while (irela < irelaend)
2530 {
2531 (*swap_out) (output_bfd, irela, erel);
2532 irela += bed->s->int_rels_per_ext_rel;
2533 erel += input_rel_hdr->sh_entsize;
2534 }
2535
2536 /* Bump the counter, so that we know where to add the next set of
2537 relocations. */
d4730f92 2538 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2539
2540 return TRUE;
2541}
2542\f
508c3946
L
2543/* Make weak undefined symbols in PIE dynamic. */
2544
2545bfd_boolean
2546_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2547 struct elf_link_hash_entry *h)
2548{
0e1862bb 2549 if (bfd_link_pie (info)
508c3946
L
2550 && h->dynindx == -1
2551 && h->root.type == bfd_link_hash_undefweak)
2552 return bfd_elf_link_record_dynamic_symbol (info, h);
2553
2554 return TRUE;
2555}
2556
45d6a902
AM
2557/* Fix up the flags for a symbol. This handles various cases which
2558 can only be fixed after all the input files are seen. This is
2559 currently called by both adjust_dynamic_symbol and
2560 assign_sym_version, which is unnecessary but perhaps more robust in
2561 the face of future changes. */
2562
28caa186 2563static bfd_boolean
268b6b39
AM
2564_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2565 struct elf_info_failed *eif)
45d6a902 2566{
33774f08 2567 const struct elf_backend_data *bed;
508c3946 2568
45d6a902
AM
2569 /* If this symbol was mentioned in a non-ELF file, try to set
2570 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2571 permit a non-ELF file to correctly refer to a symbol defined in
2572 an ELF dynamic object. */
f5385ebf 2573 if (h->non_elf)
45d6a902
AM
2574 {
2575 while (h->root.type == bfd_link_hash_indirect)
2576 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2577
2578 if (h->root.type != bfd_link_hash_defined
2579 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2580 {
2581 h->ref_regular = 1;
2582 h->ref_regular_nonweak = 1;
2583 }
45d6a902
AM
2584 else
2585 {
2586 if (h->root.u.def.section->owner != NULL
2587 && (bfd_get_flavour (h->root.u.def.section->owner)
2588 == bfd_target_elf_flavour))
f5385ebf
AM
2589 {
2590 h->ref_regular = 1;
2591 h->ref_regular_nonweak = 1;
2592 }
45d6a902 2593 else
f5385ebf 2594 h->def_regular = 1;
45d6a902
AM
2595 }
2596
2597 if (h->dynindx == -1
f5385ebf
AM
2598 && (h->def_dynamic
2599 || h->ref_dynamic))
45d6a902 2600 {
c152c796 2601 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2602 {
2603 eif->failed = TRUE;
2604 return FALSE;
2605 }
2606 }
2607 }
2608 else
2609 {
f5385ebf 2610 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2611 was first seen in a non-ELF file. Fortunately, if the symbol
2612 was first seen in an ELF file, we're probably OK unless the
2613 symbol was defined in a non-ELF file. Catch that case here.
2614 FIXME: We're still in trouble if the symbol was first seen in
2615 a dynamic object, and then later in a non-ELF regular object. */
2616 if ((h->root.type == bfd_link_hash_defined
2617 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2618 && !h->def_regular
45d6a902
AM
2619 && (h->root.u.def.section->owner != NULL
2620 ? (bfd_get_flavour (h->root.u.def.section->owner)
2621 != bfd_target_elf_flavour)
2622 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2623 && !h->def_dynamic)))
2624 h->def_regular = 1;
45d6a902
AM
2625 }
2626
508c3946 2627 /* Backend specific symbol fixup. */
33774f08
AM
2628 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2629 if (bed->elf_backend_fixup_symbol
2630 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2631 return FALSE;
508c3946 2632
45d6a902
AM
2633 /* If this is a final link, and the symbol was defined as a common
2634 symbol in a regular object file, and there was no definition in
2635 any dynamic object, then the linker will have allocated space for
f5385ebf 2636 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2637 flag will not have been set. */
2638 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2639 && !h->def_regular
2640 && h->ref_regular
2641 && !h->def_dynamic
96f29d96 2642 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2643 h->def_regular = 1;
45d6a902
AM
2644
2645 /* If -Bsymbolic was used (which means to bind references to global
2646 symbols to the definition within the shared object), and this
2647 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2648 need a PLT entry. Likewise, if the symbol has non-default
2649 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2650 will force it local. */
f5385ebf 2651 if (h->needs_plt
0e1862bb 2652 && bfd_link_pic (eif->info)
0eddce27 2653 && is_elf_hash_table (eif->info->hash)
55255dae 2654 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2655 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2656 && h->def_regular)
45d6a902 2657 {
45d6a902
AM
2658 bfd_boolean force_local;
2659
45d6a902
AM
2660 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2661 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2662 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2663 }
2664
2665 /* If a weak undefined symbol has non-default visibility, we also
2666 hide it from the dynamic linker. */
9c7a29a3 2667 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2668 && h->root.type == bfd_link_hash_undefweak)
33774f08 2669 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2670
2671 /* If this is a weak defined symbol in a dynamic object, and we know
2672 the real definition in the dynamic object, copy interesting flags
2673 over to the real definition. */
f6e332e6 2674 if (h->u.weakdef != NULL)
45d6a902 2675 {
45d6a902
AM
2676 /* If the real definition is defined by a regular object file,
2677 don't do anything special. See the longer description in
2678 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2679 if (h->u.weakdef->def_regular)
f6e332e6 2680 h->u.weakdef = NULL;
45d6a902 2681 else
a26587ba 2682 {
4e6b54a6
AM
2683 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2684
2685 while (h->root.type == bfd_link_hash_indirect)
2686 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2687
2688 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2689 || h->root.type == bfd_link_hash_defweak);
2690 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2691 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2692 || weakdef->root.type == bfd_link_hash_defweak);
2693 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2694 }
45d6a902
AM
2695 }
2696
2697 return TRUE;
2698}
2699
2700/* Make the backend pick a good value for a dynamic symbol. This is
2701 called via elf_link_hash_traverse, and also calls itself
2702 recursively. */
2703
28caa186 2704static bfd_boolean
268b6b39 2705_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2706{
a50b1753 2707 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2708 bfd *dynobj;
9c5bfbb7 2709 const struct elf_backend_data *bed;
45d6a902 2710
0eddce27 2711 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2712 return FALSE;
2713
45d6a902
AM
2714 /* Ignore indirect symbols. These are added by the versioning code. */
2715 if (h->root.type == bfd_link_hash_indirect)
2716 return TRUE;
2717
2718 /* Fix the symbol flags. */
2719 if (! _bfd_elf_fix_symbol_flags (h, eif))
2720 return FALSE;
2721
2722 /* If this symbol does not require a PLT entry, and it is not
2723 defined by a dynamic object, or is not referenced by a regular
2724 object, ignore it. We do have to handle a weak defined symbol,
2725 even if no regular object refers to it, if we decided to add it
2726 to the dynamic symbol table. FIXME: Do we normally need to worry
2727 about symbols which are defined by one dynamic object and
2728 referenced by another one? */
f5385ebf 2729 if (!h->needs_plt
91e21fb7 2730 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2731 && (h->def_regular
2732 || !h->def_dynamic
2733 || (!h->ref_regular
f6e332e6 2734 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2735 {
a6aa5195 2736 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2737 return TRUE;
2738 }
2739
2740 /* If we've already adjusted this symbol, don't do it again. This
2741 can happen via a recursive call. */
f5385ebf 2742 if (h->dynamic_adjusted)
45d6a902
AM
2743 return TRUE;
2744
2745 /* Don't look at this symbol again. Note that we must set this
2746 after checking the above conditions, because we may look at a
2747 symbol once, decide not to do anything, and then get called
2748 recursively later after REF_REGULAR is set below. */
f5385ebf 2749 h->dynamic_adjusted = 1;
45d6a902
AM
2750
2751 /* If this is a weak definition, and we know a real definition, and
2752 the real symbol is not itself defined by a regular object file,
2753 then get a good value for the real definition. We handle the
2754 real symbol first, for the convenience of the backend routine.
2755
2756 Note that there is a confusing case here. If the real definition
2757 is defined by a regular object file, we don't get the real symbol
2758 from the dynamic object, but we do get the weak symbol. If the
2759 processor backend uses a COPY reloc, then if some routine in the
2760 dynamic object changes the real symbol, we will not see that
2761 change in the corresponding weak symbol. This is the way other
2762 ELF linkers work as well, and seems to be a result of the shared
2763 library model.
2764
2765 I will clarify this issue. Most SVR4 shared libraries define the
2766 variable _timezone and define timezone as a weak synonym. The
2767 tzset call changes _timezone. If you write
2768 extern int timezone;
2769 int _timezone = 5;
2770 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2771 you might expect that, since timezone is a synonym for _timezone,
2772 the same number will print both times. However, if the processor
2773 backend uses a COPY reloc, then actually timezone will be copied
2774 into your process image, and, since you define _timezone
2775 yourself, _timezone will not. Thus timezone and _timezone will
2776 wind up at different memory locations. The tzset call will set
2777 _timezone, leaving timezone unchanged. */
2778
f6e332e6 2779 if (h->u.weakdef != NULL)
45d6a902 2780 {
ec24dc88
AM
2781 /* If we get to this point, there is an implicit reference to
2782 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2783 h->u.weakdef->ref_regular = 1;
45d6a902 2784
ec24dc88
AM
2785 /* Ensure that the backend adjust_dynamic_symbol function sees
2786 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2787 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2788 return FALSE;
2789 }
2790
2791 /* If a symbol has no type and no size and does not require a PLT
2792 entry, then we are probably about to do the wrong thing here: we
2793 are probably going to create a COPY reloc for an empty object.
2794 This case can arise when a shared object is built with assembly
2795 code, and the assembly code fails to set the symbol type. */
2796 if (h->size == 0
2797 && h->type == STT_NOTYPE
f5385ebf 2798 && !h->needs_plt)
45d6a902
AM
2799 (*_bfd_error_handler)
2800 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2801 h->root.root.string);
2802
2803 dynobj = elf_hash_table (eif->info)->dynobj;
2804 bed = get_elf_backend_data (dynobj);
e7c33416 2805
45d6a902
AM
2806 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2807 {
2808 eif->failed = TRUE;
2809 return FALSE;
2810 }
2811
2812 return TRUE;
2813}
2814
027297b7
L
2815/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2816 DYNBSS. */
2817
2818bfd_boolean
6cabe1ea
AM
2819_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2820 struct elf_link_hash_entry *h,
027297b7
L
2821 asection *dynbss)
2822{
91ac5911 2823 unsigned int power_of_two;
027297b7
L
2824 bfd_vma mask;
2825 asection *sec = h->root.u.def.section;
2826
2827 /* The section aligment of definition is the maximum alignment
91ac5911
L
2828 requirement of symbols defined in the section. Since we don't
2829 know the symbol alignment requirement, we start with the
2830 maximum alignment and check low bits of the symbol address
2831 for the minimum alignment. */
2832 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2833 mask = ((bfd_vma) 1 << power_of_two) - 1;
2834 while ((h->root.u.def.value & mask) != 0)
2835 {
2836 mask >>= 1;
2837 --power_of_two;
2838 }
027297b7 2839
91ac5911
L
2840 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2841 dynbss))
027297b7
L
2842 {
2843 /* Adjust the section alignment if needed. */
2844 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2845 power_of_two))
027297b7
L
2846 return FALSE;
2847 }
2848
91ac5911 2849 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2850 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2851
2852 /* Define the symbol as being at this point in DYNBSS. */
2853 h->root.u.def.section = dynbss;
2854 h->root.u.def.value = dynbss->size;
2855
2856 /* Increment the size of DYNBSS to make room for the symbol. */
2857 dynbss->size += h->size;
2858
f7483970
L
2859 /* No error if extern_protected_data is true. */
2860 if (h->protected_def
889c2a67
L
2861 && (!info->extern_protected_data
2862 || (info->extern_protected_data < 0
2863 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2864 info->callbacks->einfo
2865 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2866 h->root.root.string);
6cabe1ea 2867
027297b7
L
2868 return TRUE;
2869}
2870
45d6a902
AM
2871/* Adjust all external symbols pointing into SEC_MERGE sections
2872 to reflect the object merging within the sections. */
2873
28caa186 2874static bfd_boolean
268b6b39 2875_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2876{
2877 asection *sec;
2878
45d6a902
AM
2879 if ((h->root.type == bfd_link_hash_defined
2880 || h->root.type == bfd_link_hash_defweak)
2881 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2882 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2883 {
a50b1753 2884 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2885
2886 h->root.u.def.value =
2887 _bfd_merged_section_offset (output_bfd,
2888 &h->root.u.def.section,
2889 elf_section_data (sec)->sec_info,
753731ee 2890 h->root.u.def.value);
45d6a902
AM
2891 }
2892
2893 return TRUE;
2894}
986a241f
RH
2895
2896/* Returns false if the symbol referred to by H should be considered
2897 to resolve local to the current module, and true if it should be
2898 considered to bind dynamically. */
2899
2900bfd_boolean
268b6b39
AM
2901_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2902 struct bfd_link_info *info,
89a2ee5a 2903 bfd_boolean not_local_protected)
986a241f
RH
2904{
2905 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2906 const struct elf_backend_data *bed;
2907 struct elf_link_hash_table *hash_table;
986a241f
RH
2908
2909 if (h == NULL)
2910 return FALSE;
2911
2912 while (h->root.type == bfd_link_hash_indirect
2913 || h->root.type == bfd_link_hash_warning)
2914 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2915
2916 /* If it was forced local, then clearly it's not dynamic. */
2917 if (h->dynindx == -1)
2918 return FALSE;
f5385ebf 2919 if (h->forced_local)
986a241f
RH
2920 return FALSE;
2921
2922 /* Identify the cases where name binding rules say that a
2923 visible symbol resolves locally. */
0e1862bb
L
2924 binding_stays_local_p = (bfd_link_executable (info)
2925 || SYMBOLIC_BIND (info, h));
986a241f
RH
2926
2927 switch (ELF_ST_VISIBILITY (h->other))
2928 {
2929 case STV_INTERNAL:
2930 case STV_HIDDEN:
2931 return FALSE;
2932
2933 case STV_PROTECTED:
fcb93ecf
PB
2934 hash_table = elf_hash_table (info);
2935 if (!is_elf_hash_table (hash_table))
2936 return FALSE;
2937
2938 bed = get_elf_backend_data (hash_table->dynobj);
2939
986a241f
RH
2940 /* Proper resolution for function pointer equality may require
2941 that these symbols perhaps be resolved dynamically, even though
2942 we should be resolving them to the current module. */
89a2ee5a 2943 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2944 binding_stays_local_p = TRUE;
2945 break;
2946
2947 default:
986a241f
RH
2948 break;
2949 }
2950
aa37626c 2951 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2952 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2953 return TRUE;
2954
986a241f
RH
2955 /* Otherwise, the symbol is dynamic if binding rules don't tell
2956 us that it remains local. */
2957 return !binding_stays_local_p;
2958}
f6c52c13
AM
2959
2960/* Return true if the symbol referred to by H should be considered
2961 to resolve local to the current module, and false otherwise. Differs
2962 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2963 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2964 for the place where forced_local and dynindx == -1 are tested. If
2965 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2966 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2967 the symbol is local only for defined symbols.
2968 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2969 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2970 treatment of undefined weak symbols. For those that do not make
2971 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2972
2973bfd_boolean
268b6b39
AM
2974_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2975 struct bfd_link_info *info,
2976 bfd_boolean local_protected)
f6c52c13 2977{
fcb93ecf
PB
2978 const struct elf_backend_data *bed;
2979 struct elf_link_hash_table *hash_table;
2980
f6c52c13
AM
2981 /* If it's a local sym, of course we resolve locally. */
2982 if (h == NULL)
2983 return TRUE;
2984
d95edcac
L
2985 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2986 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2987 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2988 return TRUE;
2989
7e2294f9
AO
2990 /* Common symbols that become definitions don't get the DEF_REGULAR
2991 flag set, so test it first, and don't bail out. */
2992 if (ELF_COMMON_DEF_P (h))
2993 /* Do nothing. */;
f6c52c13 2994 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2995 resolve locally. The sym is either undefined or dynamic. */
2996 else if (!h->def_regular)
f6c52c13
AM
2997 return FALSE;
2998
2999 /* Forced local symbols resolve locally. */
f5385ebf 3000 if (h->forced_local)
f6c52c13
AM
3001 return TRUE;
3002
3003 /* As do non-dynamic symbols. */
3004 if (h->dynindx == -1)
3005 return TRUE;
3006
3007 /* At this point, we know the symbol is defined and dynamic. In an
3008 executable it must resolve locally, likewise when building symbolic
3009 shared libraries. */
0e1862bb 3010 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3011 return TRUE;
3012
3013 /* Now deal with defined dynamic symbols in shared libraries. Ones
3014 with default visibility might not resolve locally. */
3015 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3016 return FALSE;
3017
fcb93ecf
PB
3018 hash_table = elf_hash_table (info);
3019 if (!is_elf_hash_table (hash_table))
3020 return TRUE;
3021
3022 bed = get_elf_backend_data (hash_table->dynobj);
3023
f7483970
L
3024 /* If extern_protected_data is false, STV_PROTECTED non-function
3025 symbols are local. */
889c2a67
L
3026 if ((!info->extern_protected_data
3027 || (info->extern_protected_data < 0
3028 && !bed->extern_protected_data))
3029 && !bed->is_function_type (h->type))
1c16dfa5
L
3030 return TRUE;
3031
f6c52c13 3032 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3033 symbols be treated as dynamic symbols. If the address of a
3034 function not defined in an executable is set to that function's
3035 plt entry in the executable, then the address of the function in
3036 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3037 return local_protected;
3038}
e1918d23
AM
3039
3040/* Caches some TLS segment info, and ensures that the TLS segment vma is
3041 aligned. Returns the first TLS output section. */
3042
3043struct bfd_section *
3044_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3045{
3046 struct bfd_section *sec, *tls;
3047 unsigned int align = 0;
3048
3049 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3050 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3051 break;
3052 tls = sec;
3053
3054 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3055 if (sec->alignment_power > align)
3056 align = sec->alignment_power;
3057
3058 elf_hash_table (info)->tls_sec = tls;
3059
3060 /* Ensure the alignment of the first section is the largest alignment,
3061 so that the tls segment starts aligned. */
3062 if (tls != NULL)
3063 tls->alignment_power = align;
3064
3065 return tls;
3066}
0ad989f9
L
3067
3068/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3069static bfd_boolean
3070is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3071 Elf_Internal_Sym *sym)
3072{
a4d8e49b
L
3073 const struct elf_backend_data *bed;
3074
0ad989f9
L
3075 /* Local symbols do not count, but target specific ones might. */
3076 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3077 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3078 return FALSE;
3079
fcb93ecf 3080 bed = get_elf_backend_data (abfd);
0ad989f9 3081 /* Function symbols do not count. */
fcb93ecf 3082 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3083 return FALSE;
3084
3085 /* If the section is undefined, then so is the symbol. */
3086 if (sym->st_shndx == SHN_UNDEF)
3087 return FALSE;
3088
3089 /* If the symbol is defined in the common section, then
3090 it is a common definition and so does not count. */
a4d8e49b 3091 if (bed->common_definition (sym))
0ad989f9
L
3092 return FALSE;
3093
3094 /* If the symbol is in a target specific section then we
3095 must rely upon the backend to tell us what it is. */
3096 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3097 /* FIXME - this function is not coded yet:
3098
3099 return _bfd_is_global_symbol_definition (abfd, sym);
3100
3101 Instead for now assume that the definition is not global,
3102 Even if this is wrong, at least the linker will behave
3103 in the same way that it used to do. */
3104 return FALSE;
3105
3106 return TRUE;
3107}
3108
3109/* Search the symbol table of the archive element of the archive ABFD
3110 whose archive map contains a mention of SYMDEF, and determine if
3111 the symbol is defined in this element. */
3112static bfd_boolean
3113elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3114{
3115 Elf_Internal_Shdr * hdr;
3116 bfd_size_type symcount;
3117 bfd_size_type extsymcount;
3118 bfd_size_type extsymoff;
3119 Elf_Internal_Sym *isymbuf;
3120 Elf_Internal_Sym *isym;
3121 Elf_Internal_Sym *isymend;
3122 bfd_boolean result;
3123
3124 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3125 if (abfd == NULL)
3126 return FALSE;
3127
f0bf6bfd
L
3128 /* Return FALSE if the object has been claimed by plugin. */
3129 if (abfd->plugin_format == bfd_plugin_yes)
3130 return FALSE;
3131
0ad989f9
L
3132 if (! bfd_check_format (abfd, bfd_object))
3133 return FALSE;
3134
0ad989f9
L
3135 /* Select the appropriate symbol table. */
3136 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3137 hdr = &elf_tdata (abfd)->symtab_hdr;
3138 else
3139 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3140
3141 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3142
3143 /* The sh_info field of the symtab header tells us where the
3144 external symbols start. We don't care about the local symbols. */
3145 if (elf_bad_symtab (abfd))
3146 {
3147 extsymcount = symcount;
3148 extsymoff = 0;
3149 }
3150 else
3151 {
3152 extsymcount = symcount - hdr->sh_info;
3153 extsymoff = hdr->sh_info;
3154 }
3155
3156 if (extsymcount == 0)
3157 return FALSE;
3158
3159 /* Read in the symbol table. */
3160 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3161 NULL, NULL, NULL);
3162 if (isymbuf == NULL)
3163 return FALSE;
3164
3165 /* Scan the symbol table looking for SYMDEF. */
3166 result = FALSE;
3167 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3168 {
3169 const char *name;
3170
3171 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3172 isym->st_name);
3173 if (name == NULL)
3174 break;
3175
3176 if (strcmp (name, symdef->name) == 0)
3177 {
3178 result = is_global_data_symbol_definition (abfd, isym);
3179 break;
3180 }
3181 }
3182
3183 free (isymbuf);
3184
3185 return result;
3186}
3187\f
5a580b3a
AM
3188/* Add an entry to the .dynamic table. */
3189
3190bfd_boolean
3191_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3192 bfd_vma tag,
3193 bfd_vma val)
3194{
3195 struct elf_link_hash_table *hash_table;
3196 const struct elf_backend_data *bed;
3197 asection *s;
3198 bfd_size_type newsize;
3199 bfd_byte *newcontents;
3200 Elf_Internal_Dyn dyn;
3201
3202 hash_table = elf_hash_table (info);
3203 if (! is_elf_hash_table (hash_table))
3204 return FALSE;
3205
3206 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3207 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3208 BFD_ASSERT (s != NULL);
3209
eea6121a 3210 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3211 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3212 if (newcontents == NULL)
3213 return FALSE;
3214
3215 dyn.d_tag = tag;
3216 dyn.d_un.d_val = val;
eea6121a 3217 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3218
eea6121a 3219 s->size = newsize;
5a580b3a
AM
3220 s->contents = newcontents;
3221
3222 return TRUE;
3223}
3224
3225/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3226 otherwise just check whether one already exists. Returns -1 on error,
3227 1 if a DT_NEEDED tag already exists, and 0 on success. */
3228
4ad4eba5 3229static int
7e9f0867
AM
3230elf_add_dt_needed_tag (bfd *abfd,
3231 struct bfd_link_info *info,
4ad4eba5
AM
3232 const char *soname,
3233 bfd_boolean do_it)
5a580b3a
AM
3234{
3235 struct elf_link_hash_table *hash_table;
5a580b3a
AM
3236 bfd_size_type strindex;
3237
7e9f0867
AM
3238 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3239 return -1;
3240
5a580b3a 3241 hash_table = elf_hash_table (info);
5a580b3a
AM
3242 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3243 if (strindex == (bfd_size_type) -1)
3244 return -1;
3245
02be4619 3246 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3247 {
3248 asection *sdyn;
3249 const struct elf_backend_data *bed;
3250 bfd_byte *extdyn;
3251
3252 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3253 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3254 if (sdyn != NULL)
3255 for (extdyn = sdyn->contents;
3256 extdyn < sdyn->contents + sdyn->size;
3257 extdyn += bed->s->sizeof_dyn)
3258 {
3259 Elf_Internal_Dyn dyn;
5a580b3a 3260
7e9f0867
AM
3261 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3262 if (dyn.d_tag == DT_NEEDED
3263 && dyn.d_un.d_val == strindex)
3264 {
3265 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3266 return 1;
3267 }
3268 }
5a580b3a
AM
3269 }
3270
3271 if (do_it)
3272 {
7e9f0867
AM
3273 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3274 return -1;
3275
5a580b3a
AM
3276 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3277 return -1;
3278 }
3279 else
3280 /* We were just checking for existence of the tag. */
3281 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3282
3283 return 0;
3284}
3285
7b15fa7a
AM
3286/* Return true if SONAME is on the needed list between NEEDED and STOP
3287 (or the end of list if STOP is NULL), and needed by a library that
3288 will be loaded. */
3289
010e5ae2 3290static bfd_boolean
7b15fa7a
AM
3291on_needed_list (const char *soname,
3292 struct bfd_link_needed_list *needed,
3293 struct bfd_link_needed_list *stop)
010e5ae2 3294{
7b15fa7a
AM
3295 struct bfd_link_needed_list *look;
3296 for (look = needed; look != stop; look = look->next)
3297 if (strcmp (soname, look->name) == 0
3298 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3299 /* If needed by a library that itself is not directly
3300 needed, recursively check whether that library is
3301 indirectly needed. Since we add DT_NEEDED entries to
3302 the end of the list, library dependencies appear after
3303 the library. Therefore search prior to the current
3304 LOOK, preventing possible infinite recursion. */
3305 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3306 return TRUE;
3307
3308 return FALSE;
3309}
3310
14160578 3311/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3312static int
3313elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3314{
3315 const struct elf_link_hash_entry *h1;
3316 const struct elf_link_hash_entry *h2;
10b7e05b 3317 bfd_signed_vma vdiff;
5a580b3a
AM
3318
3319 h1 = *(const struct elf_link_hash_entry **) arg1;
3320 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3321 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3322 if (vdiff != 0)
3323 return vdiff > 0 ? 1 : -1;
3324 else
3325 {
d3435ae8 3326 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3327 if (sdiff != 0)
3328 return sdiff > 0 ? 1 : -1;
3329 }
14160578
AM
3330 vdiff = h1->size - h2->size;
3331 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3332}
4ad4eba5 3333
5a580b3a
AM
3334/* This function is used to adjust offsets into .dynstr for
3335 dynamic symbols. This is called via elf_link_hash_traverse. */
3336
3337static bfd_boolean
3338elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3339{
a50b1753 3340 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3341
5a580b3a
AM
3342 if (h->dynindx != -1)
3343 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3344 return TRUE;
3345}
3346
3347/* Assign string offsets in .dynstr, update all structures referencing
3348 them. */
3349
4ad4eba5
AM
3350static bfd_boolean
3351elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3352{
3353 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3354 struct elf_link_local_dynamic_entry *entry;
3355 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3356 bfd *dynobj = hash_table->dynobj;
3357 asection *sdyn;
3358 bfd_size_type size;
3359 const struct elf_backend_data *bed;
3360 bfd_byte *extdyn;
3361
3362 _bfd_elf_strtab_finalize (dynstr);
3363 size = _bfd_elf_strtab_size (dynstr);
3364
3365 bed = get_elf_backend_data (dynobj);
3d4d4302 3366 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3367 BFD_ASSERT (sdyn != NULL);
3368
3369 /* Update all .dynamic entries referencing .dynstr strings. */
3370 for (extdyn = sdyn->contents;
eea6121a 3371 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3372 extdyn += bed->s->sizeof_dyn)
3373 {
3374 Elf_Internal_Dyn dyn;
3375
3376 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3377 switch (dyn.d_tag)
3378 {
3379 case DT_STRSZ:
3380 dyn.d_un.d_val = size;
3381 break;
3382 case DT_NEEDED:
3383 case DT_SONAME:
3384 case DT_RPATH:
3385 case DT_RUNPATH:
3386 case DT_FILTER:
3387 case DT_AUXILIARY:
7ee314fa
AM
3388 case DT_AUDIT:
3389 case DT_DEPAUDIT:
5a580b3a
AM
3390 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3391 break;
3392 default:
3393 continue;
3394 }
3395 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3396 }
3397
3398 /* Now update local dynamic symbols. */
3399 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3400 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3401 entry->isym.st_name);
3402
3403 /* And the rest of dynamic symbols. */
3404 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3405
3406 /* Adjust version definitions. */
3407 if (elf_tdata (output_bfd)->cverdefs)
3408 {
3409 asection *s;
3410 bfd_byte *p;
3411 bfd_size_type i;
3412 Elf_Internal_Verdef def;
3413 Elf_Internal_Verdaux defaux;
3414
3d4d4302 3415 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3416 p = s->contents;
3417 do
3418 {
3419 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3420 &def);
3421 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3422 if (def.vd_aux != sizeof (Elf_External_Verdef))
3423 continue;
5a580b3a
AM
3424 for (i = 0; i < def.vd_cnt; ++i)
3425 {
3426 _bfd_elf_swap_verdaux_in (output_bfd,
3427 (Elf_External_Verdaux *) p, &defaux);
3428 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3429 defaux.vda_name);
3430 _bfd_elf_swap_verdaux_out (output_bfd,
3431 &defaux, (Elf_External_Verdaux *) p);
3432 p += sizeof (Elf_External_Verdaux);
3433 }
3434 }
3435 while (def.vd_next);
3436 }
3437
3438 /* Adjust version references. */
3439 if (elf_tdata (output_bfd)->verref)
3440 {
3441 asection *s;
3442 bfd_byte *p;
3443 bfd_size_type i;
3444 Elf_Internal_Verneed need;
3445 Elf_Internal_Vernaux needaux;
3446
3d4d4302 3447 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3448 p = s->contents;
3449 do
3450 {
3451 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3452 &need);
3453 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3454 _bfd_elf_swap_verneed_out (output_bfd, &need,
3455 (Elf_External_Verneed *) p);
3456 p += sizeof (Elf_External_Verneed);
3457 for (i = 0; i < need.vn_cnt; ++i)
3458 {
3459 _bfd_elf_swap_vernaux_in (output_bfd,
3460 (Elf_External_Vernaux *) p, &needaux);
3461 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3462 needaux.vna_name);
3463 _bfd_elf_swap_vernaux_out (output_bfd,
3464 &needaux,
3465 (Elf_External_Vernaux *) p);
3466 p += sizeof (Elf_External_Vernaux);
3467 }
3468 }
3469 while (need.vn_next);
3470 }
3471
3472 return TRUE;
3473}
3474\f
13285a1b
AM
3475/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3476 The default is to only match when the INPUT and OUTPUT are exactly
3477 the same target. */
3478
3479bfd_boolean
3480_bfd_elf_default_relocs_compatible (const bfd_target *input,
3481 const bfd_target *output)
3482{
3483 return input == output;
3484}
3485
3486/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3487 This version is used when different targets for the same architecture
3488 are virtually identical. */
3489
3490bfd_boolean
3491_bfd_elf_relocs_compatible (const bfd_target *input,
3492 const bfd_target *output)
3493{
3494 const struct elf_backend_data *obed, *ibed;
3495
3496 if (input == output)
3497 return TRUE;
3498
3499 ibed = xvec_get_elf_backend_data (input);
3500 obed = xvec_get_elf_backend_data (output);
3501
3502 if (ibed->arch != obed->arch)
3503 return FALSE;
3504
3505 /* If both backends are using this function, deem them compatible. */
3506 return ibed->relocs_compatible == obed->relocs_compatible;
3507}
3508
e5034e59
AM
3509/* Make a special call to the linker "notice" function to tell it that
3510 we are about to handle an as-needed lib, or have finished
1b786873 3511 processing the lib. */
e5034e59
AM
3512
3513bfd_boolean
3514_bfd_elf_notice_as_needed (bfd *ibfd,
3515 struct bfd_link_info *info,
3516 enum notice_asneeded_action act)
3517{
46135103 3518 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3519}
3520
d9689752
L
3521/* Check relocations an ELF object file. */
3522
3523bfd_boolean
3524_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3525{
3526 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3527 struct elf_link_hash_table *htab = elf_hash_table (info);
3528
3529 /* If this object is the same format as the output object, and it is
3530 not a shared library, then let the backend look through the
3531 relocs.
3532
3533 This is required to build global offset table entries and to
3534 arrange for dynamic relocs. It is not required for the
3535 particular common case of linking non PIC code, even when linking
3536 against shared libraries, but unfortunately there is no way of
3537 knowing whether an object file has been compiled PIC or not.
3538 Looking through the relocs is not particularly time consuming.
3539 The problem is that we must either (1) keep the relocs in memory,
3540 which causes the linker to require additional runtime memory or
3541 (2) read the relocs twice from the input file, which wastes time.
3542 This would be a good case for using mmap.
3543
3544 I have no idea how to handle linking PIC code into a file of a
3545 different format. It probably can't be done. */
3546 if ((abfd->flags & DYNAMIC) == 0
3547 && is_elf_hash_table (htab)
3548 && bed->check_relocs != NULL
3549 && elf_object_id (abfd) == elf_hash_table_id (htab)
3550 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3551 {
3552 asection *o;
3553
3554 for (o = abfd->sections; o != NULL; o = o->next)
3555 {
3556 Elf_Internal_Rela *internal_relocs;
3557 bfd_boolean ok;
3558
5ce03cea 3559 /* Don't check relocations in excluded sections. */
d9689752 3560 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3561 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3562 || o->reloc_count == 0
3563 || ((info->strip == strip_all || info->strip == strip_debugger)
3564 && (o->flags & SEC_DEBUGGING) != 0)
3565 || bfd_is_abs_section (o->output_section))
3566 continue;
3567
3568 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3569 info->keep_memory);
3570 if (internal_relocs == NULL)
3571 return FALSE;
3572
3573 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3574
3575 if (elf_section_data (o)->relocs != internal_relocs)
3576 free (internal_relocs);
3577
3578 if (! ok)
3579 return FALSE;
3580 }
3581 }
3582
3583 return TRUE;
3584}
3585
4ad4eba5
AM
3586/* Add symbols from an ELF object file to the linker hash table. */
3587
3588static bfd_boolean
3589elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3590{
a0c402a5 3591 Elf_Internal_Ehdr *ehdr;
4ad4eba5
AM
3592 Elf_Internal_Shdr *hdr;
3593 bfd_size_type symcount;
3594 bfd_size_type extsymcount;
3595 bfd_size_type extsymoff;
3596 struct elf_link_hash_entry **sym_hash;
3597 bfd_boolean dynamic;
3598 Elf_External_Versym *extversym = NULL;
3599 Elf_External_Versym *ever;
3600 struct elf_link_hash_entry *weaks;
3601 struct elf_link_hash_entry **nondeflt_vers = NULL;
3602 bfd_size_type nondeflt_vers_cnt = 0;
3603 Elf_Internal_Sym *isymbuf = NULL;
3604 Elf_Internal_Sym *isym;
3605 Elf_Internal_Sym *isymend;
3606 const struct elf_backend_data *bed;
3607 bfd_boolean add_needed;
66eb6687 3608 struct elf_link_hash_table *htab;
4ad4eba5 3609 bfd_size_type amt;
66eb6687 3610 void *alloc_mark = NULL;
4f87808c
AM
3611 struct bfd_hash_entry **old_table = NULL;
3612 unsigned int old_size = 0;
3613 unsigned int old_count = 0;
66eb6687 3614 void *old_tab = NULL;
66eb6687
AM
3615 void *old_ent;
3616 struct bfd_link_hash_entry *old_undefs = NULL;
3617 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3618 void *old_strtab = NULL;
66eb6687 3619 size_t tabsize = 0;
db6a5d5f 3620 asection *s;
29a9f53e 3621 bfd_boolean just_syms;
4ad4eba5 3622
66eb6687 3623 htab = elf_hash_table (info);
4ad4eba5 3624 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3625
3626 if ((abfd->flags & DYNAMIC) == 0)
3627 dynamic = FALSE;
3628 else
3629 {
3630 dynamic = TRUE;
3631
3632 /* You can't use -r against a dynamic object. Also, there's no
3633 hope of using a dynamic object which does not exactly match
3634 the format of the output file. */
0e1862bb 3635 if (bfd_link_relocatable (info)
66eb6687 3636 || !is_elf_hash_table (htab)
f13a99db 3637 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3638 {
0e1862bb 3639 if (bfd_link_relocatable (info))
9a0789ec
NC
3640 bfd_set_error (bfd_error_invalid_operation);
3641 else
3642 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3643 goto error_return;
3644 }
3645 }
3646
a0c402a5
L
3647 ehdr = elf_elfheader (abfd);
3648 if (info->warn_alternate_em
3649 && bed->elf_machine_code != ehdr->e_machine
3650 && ((bed->elf_machine_alt1 != 0
3651 && ehdr->e_machine == bed->elf_machine_alt1)
3652 || (bed->elf_machine_alt2 != 0
3653 && ehdr->e_machine == bed->elf_machine_alt2)))
3654 info->callbacks->einfo
3655 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3656 ehdr->e_machine, abfd, bed->elf_machine_code);
3657
4ad4eba5
AM
3658 /* As a GNU extension, any input sections which are named
3659 .gnu.warning.SYMBOL are treated as warning symbols for the given
3660 symbol. This differs from .gnu.warning sections, which generate
3661 warnings when they are included in an output file. */
dd98f8d2 3662 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3663 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3664 {
db6a5d5f 3665 const char *name;
4ad4eba5 3666
db6a5d5f
AM
3667 name = bfd_get_section_name (abfd, s);
3668 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3669 {
db6a5d5f
AM
3670 char *msg;
3671 bfd_size_type sz;
3672
3673 name += sizeof ".gnu.warning." - 1;
3674
3675 /* If this is a shared object, then look up the symbol
3676 in the hash table. If it is there, and it is already
3677 been defined, then we will not be using the entry
3678 from this shared object, so we don't need to warn.
3679 FIXME: If we see the definition in a regular object
3680 later on, we will warn, but we shouldn't. The only
3681 fix is to keep track of what warnings we are supposed
3682 to emit, and then handle them all at the end of the
3683 link. */
3684 if (dynamic)
4ad4eba5 3685 {
db6a5d5f
AM
3686 struct elf_link_hash_entry *h;
3687
3688 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3689
3690 /* FIXME: What about bfd_link_hash_common? */
3691 if (h != NULL
3692 && (h->root.type == bfd_link_hash_defined
3693 || h->root.type == bfd_link_hash_defweak))
3694 continue;
3695 }
4ad4eba5 3696
db6a5d5f
AM
3697 sz = s->size;
3698 msg = (char *) bfd_alloc (abfd, sz + 1);
3699 if (msg == NULL)
3700 goto error_return;
4ad4eba5 3701
db6a5d5f
AM
3702 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3703 goto error_return;
4ad4eba5 3704
db6a5d5f 3705 msg[sz] = '\0';
4ad4eba5 3706
db6a5d5f
AM
3707 if (! (_bfd_generic_link_add_one_symbol
3708 (info, abfd, name, BSF_WARNING, s, 0, msg,
3709 FALSE, bed->collect, NULL)))
3710 goto error_return;
4ad4eba5 3711
0e1862bb 3712 if (bfd_link_executable (info))
db6a5d5f
AM
3713 {
3714 /* Clobber the section size so that the warning does
3715 not get copied into the output file. */
3716 s->size = 0;
11d2f718 3717
db6a5d5f
AM
3718 /* Also set SEC_EXCLUDE, so that symbols defined in
3719 the warning section don't get copied to the output. */
3720 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3721 }
3722 }
3723 }
3724
29a9f53e
L
3725 just_syms = ((s = abfd->sections) != NULL
3726 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3727
4ad4eba5
AM
3728 add_needed = TRUE;
3729 if (! dynamic)
3730 {
3731 /* If we are creating a shared library, create all the dynamic
3732 sections immediately. We need to attach them to something,
3733 so we attach them to this BFD, provided it is the right
bf89386a
L
3734 format and is not from ld --just-symbols. Always create the
3735 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3736 are no input BFD's of the same format as the output, we can't
3737 make a shared library. */
3738 if (!just_syms
bf89386a 3739 && (bfd_link_pic (info)
9c1d7a08
L
3740 || (!bfd_link_relocatable (info)
3741 && (info->export_dynamic || info->dynamic)))
66eb6687 3742 && is_elf_hash_table (htab)
f13a99db 3743 && info->output_bfd->xvec == abfd->xvec
66eb6687 3744 && !htab->dynamic_sections_created)
4ad4eba5
AM
3745 {
3746 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3747 goto error_return;
3748 }
3749 }
66eb6687 3750 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3751 goto error_return;
3752 else
3753 {
4ad4eba5 3754 const char *soname = NULL;
7ee314fa 3755 char *audit = NULL;
4ad4eba5
AM
3756 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3757 int ret;
3758
3759 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3760 ld shouldn't allow it. */
29a9f53e 3761 if (just_syms)
92fd189d 3762 abort ();
4ad4eba5
AM
3763
3764 /* If this dynamic lib was specified on the command line with
3765 --as-needed in effect, then we don't want to add a DT_NEEDED
3766 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3767 in by another lib's DT_NEEDED. When --no-add-needed is used
3768 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3769 any dynamic library in DT_NEEDED tags in the dynamic lib at
3770 all. */
3771 add_needed = (elf_dyn_lib_class (abfd)
3772 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3773 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3774
3775 s = bfd_get_section_by_name (abfd, ".dynamic");
3776 if (s != NULL)
3777 {
3778 bfd_byte *dynbuf;
3779 bfd_byte *extdyn;
cb33740c 3780 unsigned int elfsec;
4ad4eba5
AM
3781 unsigned long shlink;
3782
eea6121a 3783 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3784 {
3785error_free_dyn:
3786 free (dynbuf);
3787 goto error_return;
3788 }
4ad4eba5
AM
3789
3790 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3791 if (elfsec == SHN_BAD)
4ad4eba5
AM
3792 goto error_free_dyn;
3793 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3794
3795 for (extdyn = dynbuf;
eea6121a 3796 extdyn < dynbuf + s->size;
4ad4eba5
AM
3797 extdyn += bed->s->sizeof_dyn)
3798 {
3799 Elf_Internal_Dyn dyn;
3800
3801 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3802 if (dyn.d_tag == DT_SONAME)
3803 {
3804 unsigned int tagv = dyn.d_un.d_val;
3805 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3806 if (soname == NULL)
3807 goto error_free_dyn;
3808 }
3809 if (dyn.d_tag == DT_NEEDED)
3810 {
3811 struct bfd_link_needed_list *n, **pn;
3812 char *fnm, *anm;
3813 unsigned int tagv = dyn.d_un.d_val;
3814
3815 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3816 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3817 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3818 if (n == NULL || fnm == NULL)
3819 goto error_free_dyn;
3820 amt = strlen (fnm) + 1;
a50b1753 3821 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3822 if (anm == NULL)
3823 goto error_free_dyn;
3824 memcpy (anm, fnm, amt);
3825 n->name = anm;
3826 n->by = abfd;
3827 n->next = NULL;
66eb6687 3828 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3829 ;
3830 *pn = n;
3831 }
3832 if (dyn.d_tag == DT_RUNPATH)
3833 {
3834 struct bfd_link_needed_list *n, **pn;
3835 char *fnm, *anm;
3836 unsigned int tagv = dyn.d_un.d_val;
3837
3838 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3839 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3840 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3841 if (n == NULL || fnm == NULL)
3842 goto error_free_dyn;
3843 amt = strlen (fnm) + 1;
a50b1753 3844 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3845 if (anm == NULL)
3846 goto error_free_dyn;
3847 memcpy (anm, fnm, amt);
3848 n->name = anm;
3849 n->by = abfd;
3850 n->next = NULL;
3851 for (pn = & runpath;
3852 *pn != NULL;
3853 pn = &(*pn)->next)
3854 ;
3855 *pn = n;
3856 }
3857 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3858 if (!runpath && dyn.d_tag == DT_RPATH)
3859 {
3860 struct bfd_link_needed_list *n, **pn;
3861 char *fnm, *anm;
3862 unsigned int tagv = dyn.d_un.d_val;
3863
3864 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3865 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3866 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3867 if (n == NULL || fnm == NULL)
3868 goto error_free_dyn;
3869 amt = strlen (fnm) + 1;
a50b1753 3870 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3871 if (anm == NULL)
f8703194 3872 goto error_free_dyn;
4ad4eba5
AM
3873 memcpy (anm, fnm, amt);
3874 n->name = anm;
3875 n->by = abfd;
3876 n->next = NULL;
3877 for (pn = & rpath;
3878 *pn != NULL;
3879 pn = &(*pn)->next)
3880 ;
3881 *pn = n;
3882 }
7ee314fa
AM
3883 if (dyn.d_tag == DT_AUDIT)
3884 {
3885 unsigned int tagv = dyn.d_un.d_val;
3886 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3887 }
4ad4eba5
AM
3888 }
3889
3890 free (dynbuf);
3891 }
3892
3893 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3894 frees all more recently bfd_alloc'd blocks as well. */
3895 if (runpath)
3896 rpath = runpath;
3897
3898 if (rpath)
3899 {
3900 struct bfd_link_needed_list **pn;
66eb6687 3901 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3902 ;
3903 *pn = rpath;
3904 }
3905
3906 /* We do not want to include any of the sections in a dynamic
3907 object in the output file. We hack by simply clobbering the
3908 list of sections in the BFD. This could be handled more
3909 cleanly by, say, a new section flag; the existing
3910 SEC_NEVER_LOAD flag is not the one we want, because that one
3911 still implies that the section takes up space in the output
3912 file. */
3913 bfd_section_list_clear (abfd);
3914
4ad4eba5
AM
3915 /* Find the name to use in a DT_NEEDED entry that refers to this
3916 object. If the object has a DT_SONAME entry, we use it.
3917 Otherwise, if the generic linker stuck something in
3918 elf_dt_name, we use that. Otherwise, we just use the file
3919 name. */
3920 if (soname == NULL || *soname == '\0')
3921 {
3922 soname = elf_dt_name (abfd);
3923 if (soname == NULL || *soname == '\0')
3924 soname = bfd_get_filename (abfd);
3925 }
3926
3927 /* Save the SONAME because sometimes the linker emulation code
3928 will need to know it. */
3929 elf_dt_name (abfd) = soname;
3930
7e9f0867 3931 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3932 if (ret < 0)
3933 goto error_return;
3934
3935 /* If we have already included this dynamic object in the
3936 link, just ignore it. There is no reason to include a
3937 particular dynamic object more than once. */
3938 if (ret > 0)
3939 return TRUE;
7ee314fa
AM
3940
3941 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 3942 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3943 }
3944
3945 /* If this is a dynamic object, we always link against the .dynsym
3946 symbol table, not the .symtab symbol table. The dynamic linker
3947 will only see the .dynsym symbol table, so there is no reason to
3948 look at .symtab for a dynamic object. */
3949
3950 if (! dynamic || elf_dynsymtab (abfd) == 0)
3951 hdr = &elf_tdata (abfd)->symtab_hdr;
3952 else
3953 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3954
3955 symcount = hdr->sh_size / bed->s->sizeof_sym;
3956
3957 /* The sh_info field of the symtab header tells us where the
3958 external symbols start. We don't care about the local symbols at
3959 this point. */
3960 if (elf_bad_symtab (abfd))
3961 {
3962 extsymcount = symcount;
3963 extsymoff = 0;
3964 }
3965 else
3966 {
3967 extsymcount = symcount - hdr->sh_info;
3968 extsymoff = hdr->sh_info;
3969 }
3970
f45794cb 3971 sym_hash = elf_sym_hashes (abfd);
012b2306 3972 if (extsymcount != 0)
4ad4eba5
AM
3973 {
3974 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3975 NULL, NULL, NULL);
3976 if (isymbuf == NULL)
3977 goto error_return;
3978
4ad4eba5 3979 if (sym_hash == NULL)
012b2306
AM
3980 {
3981 /* We store a pointer to the hash table entry for each
3982 external symbol. */
3983 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3984 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3985 if (sym_hash == NULL)
3986 goto error_free_sym;
3987 elf_sym_hashes (abfd) = sym_hash;
3988 }
4ad4eba5
AM
3989 }
3990
3991 if (dynamic)
3992 {
3993 /* Read in any version definitions. */
fc0e6df6
PB
3994 if (!_bfd_elf_slurp_version_tables (abfd,
3995 info->default_imported_symver))
4ad4eba5
AM
3996 goto error_free_sym;
3997
3998 /* Read in the symbol versions, but don't bother to convert them
3999 to internal format. */
4000 if (elf_dynversym (abfd) != 0)
4001 {
4002 Elf_Internal_Shdr *versymhdr;
4003
4004 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4005 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4006 if (extversym == NULL)
4007 goto error_free_sym;
4008 amt = versymhdr->sh_size;
4009 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4010 || bfd_bread (extversym, amt, abfd) != amt)
4011 goto error_free_vers;
4012 }
4013 }
4014
66eb6687
AM
4015 /* If we are loading an as-needed shared lib, save the symbol table
4016 state before we start adding symbols. If the lib turns out
4017 to be unneeded, restore the state. */
4018 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4019 {
4020 unsigned int i;
4021 size_t entsize;
4022
4023 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4024 {
4025 struct bfd_hash_entry *p;
2de92251 4026 struct elf_link_hash_entry *h;
66eb6687
AM
4027
4028 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4029 {
4030 h = (struct elf_link_hash_entry *) p;
4031 entsize += htab->root.table.entsize;
4032 if (h->root.type == bfd_link_hash_warning)
4033 entsize += htab->root.table.entsize;
4034 }
66eb6687
AM
4035 }
4036
4037 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4038 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4039 if (old_tab == NULL)
4040 goto error_free_vers;
4041
4042 /* Remember the current objalloc pointer, so that all mem for
4043 symbols added can later be reclaimed. */
4044 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4045 if (alloc_mark == NULL)
4046 goto error_free_vers;
4047
5061a885
AM
4048 /* Make a special call to the linker "notice" function to
4049 tell it that we are about to handle an as-needed lib. */
e5034e59 4050 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4051 goto error_free_vers;
5061a885 4052
f45794cb
AM
4053 /* Clone the symbol table. Remember some pointers into the
4054 symbol table, and dynamic symbol count. */
4055 old_ent = (char *) old_tab + tabsize;
66eb6687 4056 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4057 old_undefs = htab->root.undefs;
4058 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4059 old_table = htab->root.table.table;
4060 old_size = htab->root.table.size;
4061 old_count = htab->root.table.count;
5b677558
AM
4062 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4063 if (old_strtab == NULL)
4064 goto error_free_vers;
66eb6687
AM
4065
4066 for (i = 0; i < htab->root.table.size; i++)
4067 {
4068 struct bfd_hash_entry *p;
2de92251 4069 struct elf_link_hash_entry *h;
66eb6687
AM
4070
4071 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4072 {
4073 memcpy (old_ent, p, htab->root.table.entsize);
4074 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4075 h = (struct elf_link_hash_entry *) p;
4076 if (h->root.type == bfd_link_hash_warning)
4077 {
4078 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4079 old_ent = (char *) old_ent + htab->root.table.entsize;
4080 }
66eb6687
AM
4081 }
4082 }
4083 }
4ad4eba5 4084
66eb6687 4085 weaks = NULL;
4ad4eba5
AM
4086 ever = extversym != NULL ? extversym + extsymoff : NULL;
4087 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4088 isym < isymend;
4089 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4090 {
4091 int bind;
4092 bfd_vma value;
af44c138 4093 asection *sec, *new_sec;
4ad4eba5
AM
4094 flagword flags;
4095 const char *name;
4096 struct elf_link_hash_entry *h;
90c984fc 4097 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4098 bfd_boolean definition;
4099 bfd_boolean size_change_ok;
4100 bfd_boolean type_change_ok;
4101 bfd_boolean new_weakdef;
37a9e49a
L
4102 bfd_boolean new_weak;
4103 bfd_boolean old_weak;
4ad4eba5 4104 bfd_boolean override;
a4d8e49b 4105 bfd_boolean common;
97196564 4106 bfd_boolean discarded;
4ad4eba5
AM
4107 unsigned int old_alignment;
4108 bfd *old_bfd;
6e33951e 4109 bfd_boolean matched;
4ad4eba5
AM
4110
4111 override = FALSE;
4112
4113 flags = BSF_NO_FLAGS;
4114 sec = NULL;
4115 value = isym->st_value;
a4d8e49b 4116 common = bed->common_definition (isym);
97196564 4117 discarded = FALSE;
4ad4eba5
AM
4118
4119 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4120 switch (bind)
4ad4eba5 4121 {
3e7a7d11 4122 case STB_LOCAL:
4ad4eba5
AM
4123 /* This should be impossible, since ELF requires that all
4124 global symbols follow all local symbols, and that sh_info
4125 point to the first global symbol. Unfortunately, Irix 5
4126 screws this up. */
4127 continue;
3e7a7d11
NC
4128
4129 case STB_GLOBAL:
a4d8e49b 4130 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4131 flags = BSF_GLOBAL;
3e7a7d11
NC
4132 break;
4133
4134 case STB_WEAK:
4135 flags = BSF_WEAK;
4136 break;
4137
4138 case STB_GNU_UNIQUE:
4139 flags = BSF_GNU_UNIQUE;
4140 break;
4141
4142 default:
4ad4eba5 4143 /* Leave it up to the processor backend. */
3e7a7d11 4144 break;
4ad4eba5
AM
4145 }
4146
4147 if (isym->st_shndx == SHN_UNDEF)
4148 sec = bfd_und_section_ptr;
cb33740c
AM
4149 else if (isym->st_shndx == SHN_ABS)
4150 sec = bfd_abs_section_ptr;
4151 else if (isym->st_shndx == SHN_COMMON)
4152 {
4153 sec = bfd_com_section_ptr;
4154 /* What ELF calls the size we call the value. What ELF
4155 calls the value we call the alignment. */
4156 value = isym->st_size;
4157 }
4158 else
4ad4eba5
AM
4159 {
4160 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4161 if (sec == NULL)
4162 sec = bfd_abs_section_ptr;
dbaa2011 4163 else if (discarded_section (sec))
529fcb95 4164 {
e5d08002
L
4165 /* Symbols from discarded section are undefined. We keep
4166 its visibility. */
529fcb95 4167 sec = bfd_und_section_ptr;
97196564 4168 discarded = TRUE;
529fcb95
PB
4169 isym->st_shndx = SHN_UNDEF;
4170 }
4ad4eba5
AM
4171 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4172 value -= sec->vma;
4173 }
4ad4eba5
AM
4174
4175 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4176 isym->st_name);
4177 if (name == NULL)
4178 goto error_free_vers;
4179
4180 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4181 && (abfd->flags & BFD_PLUGIN) != 0)
4182 {
4183 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4184
4185 if (xc == NULL)
4186 {
4187 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4188 | SEC_EXCLUDE);
4189 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4190 if (xc == NULL)
4191 goto error_free_vers;
4192 }
4193 sec = xc;
4194 }
4195 else if (isym->st_shndx == SHN_COMMON
4196 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4197 && !bfd_link_relocatable (info))
4ad4eba5
AM
4198 {
4199 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4200
4201 if (tcomm == NULL)
4202 {
02d00247
AM
4203 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4204 | SEC_LINKER_CREATED);
4205 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4206 if (tcomm == NULL)
4ad4eba5
AM
4207 goto error_free_vers;
4208 }
4209 sec = tcomm;
4210 }
66eb6687 4211 else if (bed->elf_add_symbol_hook)
4ad4eba5 4212 {
66eb6687
AM
4213 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4214 &sec, &value))
4ad4eba5
AM
4215 goto error_free_vers;
4216
4217 /* The hook function sets the name to NULL if this symbol
4218 should be skipped for some reason. */
4219 if (name == NULL)
4220 continue;
4221 }
4222
4223 /* Sanity check that all possibilities were handled. */
4224 if (sec == NULL)
4225 {
4226 bfd_set_error (bfd_error_bad_value);
4227 goto error_free_vers;
4228 }
4229
191c0c42
AM
4230 /* Silently discard TLS symbols from --just-syms. There's
4231 no way to combine a static TLS block with a new TLS block
4232 for this executable. */
4233 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4234 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4235 continue;
4236
4ad4eba5
AM
4237 if (bfd_is_und_section (sec)
4238 || bfd_is_com_section (sec))
4239 definition = FALSE;
4240 else
4241 definition = TRUE;
4242
4243 size_change_ok = FALSE;
66eb6687 4244 type_change_ok = bed->type_change_ok;
37a9e49a 4245 old_weak = FALSE;
6e33951e 4246 matched = FALSE;
4ad4eba5
AM
4247 old_alignment = 0;
4248 old_bfd = NULL;
af44c138 4249 new_sec = sec;
4ad4eba5 4250
66eb6687 4251 if (is_elf_hash_table (htab))
4ad4eba5
AM
4252 {
4253 Elf_Internal_Versym iver;
4254 unsigned int vernum = 0;
4255 bfd_boolean skip;
4256
fc0e6df6 4257 if (ever == NULL)
4ad4eba5 4258 {
fc0e6df6
PB
4259 if (info->default_imported_symver)
4260 /* Use the default symbol version created earlier. */
4261 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4262 else
4263 iver.vs_vers = 0;
4264 }
4265 else
4266 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4267
4268 vernum = iver.vs_vers & VERSYM_VERSION;
4269
4270 /* If this is a hidden symbol, or if it is not version
4271 1, we append the version name to the symbol name.
cc86ff91
EB
4272 However, we do not modify a non-hidden absolute symbol
4273 if it is not a function, because it might be the version
4274 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4275 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4276 || (vernum > 1
4277 && (!bfd_is_abs_section (sec)
4278 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4279 {
4280 const char *verstr;
4281 size_t namelen, verlen, newlen;
4282 char *newname, *p;
4283
4284 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4285 {
fc0e6df6
PB
4286 if (vernum > elf_tdata (abfd)->cverdefs)
4287 verstr = NULL;
4288 else if (vernum > 1)
4289 verstr =
4290 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4291 else
4292 verstr = "";
4ad4eba5 4293
fc0e6df6 4294 if (verstr == NULL)
4ad4eba5 4295 {
fc0e6df6
PB
4296 (*_bfd_error_handler)
4297 (_("%B: %s: invalid version %u (max %d)"),
4298 abfd, name, vernum,
4299 elf_tdata (abfd)->cverdefs);
4300 bfd_set_error (bfd_error_bad_value);
4301 goto error_free_vers;
4ad4eba5 4302 }
fc0e6df6
PB
4303 }
4304 else
4305 {
4306 /* We cannot simply test for the number of
4307 entries in the VERNEED section since the
4308 numbers for the needed versions do not start
4309 at 0. */
4310 Elf_Internal_Verneed *t;
4311
4312 verstr = NULL;
4313 for (t = elf_tdata (abfd)->verref;
4314 t != NULL;
4315 t = t->vn_nextref)
4ad4eba5 4316 {
fc0e6df6 4317 Elf_Internal_Vernaux *a;
4ad4eba5 4318
fc0e6df6
PB
4319 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4320 {
4321 if (a->vna_other == vernum)
4ad4eba5 4322 {
fc0e6df6
PB
4323 verstr = a->vna_nodename;
4324 break;
4ad4eba5 4325 }
4ad4eba5 4326 }
fc0e6df6
PB
4327 if (a != NULL)
4328 break;
4329 }
4330 if (verstr == NULL)
4331 {
4332 (*_bfd_error_handler)
4333 (_("%B: %s: invalid needed version %d"),
4334 abfd, name, vernum);
4335 bfd_set_error (bfd_error_bad_value);
4336 goto error_free_vers;
4ad4eba5 4337 }
4ad4eba5 4338 }
fc0e6df6
PB
4339
4340 namelen = strlen (name);
4341 verlen = strlen (verstr);
4342 newlen = namelen + verlen + 2;
4343 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4344 && isym->st_shndx != SHN_UNDEF)
4345 ++newlen;
4346
a50b1753 4347 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4348 if (newname == NULL)
4349 goto error_free_vers;
4350 memcpy (newname, name, namelen);
4351 p = newname + namelen;
4352 *p++ = ELF_VER_CHR;
4353 /* If this is a defined non-hidden version symbol,
4354 we add another @ to the name. This indicates the
4355 default version of the symbol. */
4356 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4357 && isym->st_shndx != SHN_UNDEF)
4358 *p++ = ELF_VER_CHR;
4359 memcpy (p, verstr, verlen + 1);
4360
4361 name = newname;
4ad4eba5
AM
4362 }
4363
cd3416da
AM
4364 /* If this symbol has default visibility and the user has
4365 requested we not re-export it, then mark it as hidden. */
a0d49154 4366 if (!bfd_is_und_section (sec)
cd3416da 4367 && !dynamic
ce875075 4368 && abfd->no_export
cd3416da
AM
4369 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4370 isym->st_other = (STV_HIDDEN
4371 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4372
4f3fedcf
AM
4373 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4374 sym_hash, &old_bfd, &old_weak,
4375 &old_alignment, &skip, &override,
6e33951e
L
4376 &type_change_ok, &size_change_ok,
4377 &matched))
4ad4eba5
AM
4378 goto error_free_vers;
4379
4380 if (skip)
4381 continue;
4382
6e33951e
L
4383 /* Override a definition only if the new symbol matches the
4384 existing one. */
4385 if (override && matched)
4ad4eba5
AM
4386 definition = FALSE;
4387
4388 h = *sym_hash;
4389 while (h->root.type == bfd_link_hash_indirect
4390 || h->root.type == bfd_link_hash_warning)
4391 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4392
4ad4eba5 4393 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4394 && vernum > 1
4395 && definition)
4396 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4397 }
4398
4399 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4400 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4401 (struct bfd_link_hash_entry **) sym_hash)))
4402 goto error_free_vers;
4403
a43942db
MR
4404 if ((flags & BSF_GNU_UNIQUE)
4405 && (abfd->flags & DYNAMIC) == 0
4406 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4407 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4408
4ad4eba5 4409 h = *sym_hash;
90c984fc
L
4410 /* We need to make sure that indirect symbol dynamic flags are
4411 updated. */
4412 hi = h;
4ad4eba5
AM
4413 while (h->root.type == bfd_link_hash_indirect
4414 || h->root.type == bfd_link_hash_warning)
4415 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4416
97196564
L
4417 /* Setting the index to -3 tells elf_link_output_extsym that
4418 this symbol is defined in a discarded section. */
4419 if (discarded)
4420 h->indx = -3;
4421
4ad4eba5
AM
4422 *sym_hash = h;
4423
37a9e49a 4424 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4425 new_weakdef = FALSE;
4426 if (dynamic
4427 && definition
37a9e49a 4428 && new_weak
fcb93ecf 4429 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4430 && is_elf_hash_table (htab)
f6e332e6 4431 && h->u.weakdef == NULL)
4ad4eba5
AM
4432 {
4433 /* Keep a list of all weak defined non function symbols from
4434 a dynamic object, using the weakdef field. Later in this
4435 function we will set the weakdef field to the correct
4436 value. We only put non-function symbols from dynamic
4437 objects on this list, because that happens to be the only
4438 time we need to know the normal symbol corresponding to a
4439 weak symbol, and the information is time consuming to
4440 figure out. If the weakdef field is not already NULL,
4441 then this symbol was already defined by some previous
4442 dynamic object, and we will be using that previous
4443 definition anyhow. */
4444
f6e332e6 4445 h->u.weakdef = weaks;
4ad4eba5
AM
4446 weaks = h;
4447 new_weakdef = TRUE;
4448 }
4449
4450 /* Set the alignment of a common symbol. */
a4d8e49b 4451 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4452 && h->root.type == bfd_link_hash_common)
4453 {
4454 unsigned int align;
4455
a4d8e49b 4456 if (common)
af44c138
L
4457 align = bfd_log2 (isym->st_value);
4458 else
4459 {
4460 /* The new symbol is a common symbol in a shared object.
4461 We need to get the alignment from the section. */
4462 align = new_sec->alignment_power;
4463 }
595213d4 4464 if (align > old_alignment)
4ad4eba5
AM
4465 h->root.u.c.p->alignment_power = align;
4466 else
4467 h->root.u.c.p->alignment_power = old_alignment;
4468 }
4469
66eb6687 4470 if (is_elf_hash_table (htab))
4ad4eba5 4471 {
4f3fedcf
AM
4472 /* Set a flag in the hash table entry indicating the type of
4473 reference or definition we just found. A dynamic symbol
4474 is one which is referenced or defined by both a regular
4475 object and a shared object. */
4476 bfd_boolean dynsym = FALSE;
4477
4478 /* Plugin symbols aren't normal. Don't set def_regular or
4479 ref_regular for them, or make them dynamic. */
4480 if ((abfd->flags & BFD_PLUGIN) != 0)
4481 ;
4482 else if (! dynamic)
4483 {
4484 if (! definition)
4485 {
4486 h->ref_regular = 1;
4487 if (bind != STB_WEAK)
4488 h->ref_regular_nonweak = 1;
4489 }
4490 else
4491 {
4492 h->def_regular = 1;
4493 if (h->def_dynamic)
4494 {
4495 h->def_dynamic = 0;
4496 h->ref_dynamic = 1;
4497 }
4498 }
4499
4500 /* If the indirect symbol has been forced local, don't
4501 make the real symbol dynamic. */
4502 if ((h == hi || !hi->forced_local)
0e1862bb 4503 && (bfd_link_dll (info)
4f3fedcf
AM
4504 || h->def_dynamic
4505 || h->ref_dynamic))
4506 dynsym = TRUE;
4507 }
4508 else
4509 {
4510 if (! definition)
4511 {
4512 h->ref_dynamic = 1;
4513 hi->ref_dynamic = 1;
4514 }
4515 else
4516 {
4517 h->def_dynamic = 1;
4518 hi->def_dynamic = 1;
4519 }
4520
4521 /* If the indirect symbol has been forced local, don't
4522 make the real symbol dynamic. */
4523 if ((h == hi || !hi->forced_local)
4524 && (h->def_regular
4525 || h->ref_regular
4526 || (h->u.weakdef != NULL
4527 && ! new_weakdef
4528 && h->u.weakdef->dynindx != -1)))
4529 dynsym = TRUE;
4530 }
4531
4532 /* Check to see if we need to add an indirect symbol for
4533 the default name. */
4534 if (definition
4535 || (!override && h->root.type == bfd_link_hash_common))
4536 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4537 sec, value, &old_bfd, &dynsym))
4538 goto error_free_vers;
4ad4eba5
AM
4539
4540 /* Check the alignment when a common symbol is involved. This
4541 can change when a common symbol is overridden by a normal
4542 definition or a common symbol is ignored due to the old
4543 normal definition. We need to make sure the maximum
4544 alignment is maintained. */
a4d8e49b 4545 if ((old_alignment || common)
4ad4eba5
AM
4546 && h->root.type != bfd_link_hash_common)
4547 {
4548 unsigned int common_align;
4549 unsigned int normal_align;
4550 unsigned int symbol_align;
4551 bfd *normal_bfd;
4552 bfd *common_bfd;
4553
3a81e825
AM
4554 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4555 || h->root.type == bfd_link_hash_defweak);
4556
4ad4eba5
AM
4557 symbol_align = ffs (h->root.u.def.value) - 1;
4558 if (h->root.u.def.section->owner != NULL
4559 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4560 {
4561 normal_align = h->root.u.def.section->alignment_power;
4562 if (normal_align > symbol_align)
4563 normal_align = symbol_align;
4564 }
4565 else
4566 normal_align = symbol_align;
4567
4568 if (old_alignment)
4569 {
4570 common_align = old_alignment;
4571 common_bfd = old_bfd;
4572 normal_bfd = abfd;
4573 }
4574 else
4575 {
4576 common_align = bfd_log2 (isym->st_value);
4577 common_bfd = abfd;
4578 normal_bfd = old_bfd;
4579 }
4580
4581 if (normal_align < common_align)
d07676f8
NC
4582 {
4583 /* PR binutils/2735 */
4584 if (normal_bfd == NULL)
4585 (*_bfd_error_handler)
4f3fedcf
AM
4586 (_("Warning: alignment %u of common symbol `%s' in %B is"
4587 " greater than the alignment (%u) of its section %A"),
d07676f8
NC
4588 common_bfd, h->root.u.def.section,
4589 1 << common_align, name, 1 << normal_align);
4590 else
4591 (*_bfd_error_handler)
4592 (_("Warning: alignment %u of symbol `%s' in %B"
4593 " is smaller than %u in %B"),
4594 normal_bfd, common_bfd,
4595 1 << normal_align, name, 1 << common_align);
4596 }
4ad4eba5
AM
4597 }
4598
83ad0046 4599 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4600 if (isym->st_size != 0
4601 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4602 && (definition || h->size == 0))
4603 {
83ad0046
L
4604 if (h->size != 0
4605 && h->size != isym->st_size
4606 && ! size_change_ok)
4ad4eba5 4607 (*_bfd_error_handler)
d003868e
AM
4608 (_("Warning: size of symbol `%s' changed"
4609 " from %lu in %B to %lu in %B"),
4610 old_bfd, abfd,
4ad4eba5 4611 name, (unsigned long) h->size,
d003868e 4612 (unsigned long) isym->st_size);
4ad4eba5
AM
4613
4614 h->size = isym->st_size;
4615 }
4616
4617 /* If this is a common symbol, then we always want H->SIZE
4618 to be the size of the common symbol. The code just above
4619 won't fix the size if a common symbol becomes larger. We
4620 don't warn about a size change here, because that is
4f3fedcf 4621 covered by --warn-common. Allow changes between different
fcb93ecf 4622 function types. */
4ad4eba5
AM
4623 if (h->root.type == bfd_link_hash_common)
4624 h->size = h->root.u.c.size;
4625
4626 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4627 && ((definition && !new_weak)
4628 || (old_weak && h->root.type == bfd_link_hash_common)
4629 || h->type == STT_NOTYPE))
4ad4eba5 4630 {
2955ec4c
L
4631 unsigned int type = ELF_ST_TYPE (isym->st_info);
4632
4633 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4634 symbol. */
4635 if (type == STT_GNU_IFUNC
4636 && (abfd->flags & DYNAMIC) != 0)
4637 type = STT_FUNC;
4ad4eba5 4638
2955ec4c
L
4639 if (h->type != type)
4640 {
4641 if (h->type != STT_NOTYPE && ! type_change_ok)
4642 (*_bfd_error_handler)
4643 (_("Warning: type of symbol `%s' changed"
4644 " from %d to %d in %B"),
4645 abfd, name, h->type, type);
4646
4647 h->type = type;
4648 }
4ad4eba5
AM
4649 }
4650
54ac0771 4651 /* Merge st_other field. */
b8417128 4652 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4653
c3df8c14 4654 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4655 if (definition
4656 && (sec->flags & SEC_DEBUGGING)
4657 && !bfd_link_relocatable (info))
c3df8c14
AM
4658 dynsym = FALSE;
4659
4f3fedcf
AM
4660 /* Nor should we make plugin symbols dynamic. */
4661 if ((abfd->flags & BFD_PLUGIN) != 0)
4662 dynsym = FALSE;
4663
35fc36a8 4664 if (definition)
35399224
L
4665 {
4666 h->target_internal = isym->st_target_internal;
4667 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4668 }
35fc36a8 4669
4ad4eba5
AM
4670 if (definition && !dynamic)
4671 {
4672 char *p = strchr (name, ELF_VER_CHR);
4673 if (p != NULL && p[1] != ELF_VER_CHR)
4674 {
4675 /* Queue non-default versions so that .symver x, x@FOO
4676 aliases can be checked. */
66eb6687 4677 if (!nondeflt_vers)
4ad4eba5 4678 {
66eb6687
AM
4679 amt = ((isymend - isym + 1)
4680 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4681 nondeflt_vers
4682 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4683 if (!nondeflt_vers)
4684 goto error_free_vers;
4ad4eba5 4685 }
66eb6687 4686 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4687 }
4688 }
4689
4690 if (dynsym && h->dynindx == -1)
4691 {
c152c796 4692 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4693 goto error_free_vers;
f6e332e6 4694 if (h->u.weakdef != NULL
4ad4eba5 4695 && ! new_weakdef
f6e332e6 4696 && h->u.weakdef->dynindx == -1)
4ad4eba5 4697 {
66eb6687 4698 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4699 goto error_free_vers;
4700 }
4701 }
1f599d0e 4702 else if (h->dynindx != -1)
4ad4eba5
AM
4703 /* If the symbol already has a dynamic index, but
4704 visibility says it should not be visible, turn it into
4705 a local symbol. */
4706 switch (ELF_ST_VISIBILITY (h->other))
4707 {
4708 case STV_INTERNAL:
4709 case STV_HIDDEN:
4710 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4711 dynsym = FALSE;
4712 break;
4713 }
4714
aef28989
L
4715 /* Don't add DT_NEEDED for references from the dummy bfd nor
4716 for unmatched symbol. */
4ad4eba5 4717 if (!add_needed
aef28989 4718 && matched
4ad4eba5 4719 && definition
010e5ae2 4720 && ((dynsym
ffa9430d 4721 && h->ref_regular_nonweak
4f3fedcf
AM
4722 && (old_bfd == NULL
4723 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4724 || (h->ref_dynamic_nonweak
010e5ae2 4725 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4726 && !on_needed_list (elf_dt_name (abfd),
4727 htab->needed, NULL))))
4ad4eba5
AM
4728 {
4729 int ret;
4730 const char *soname = elf_dt_name (abfd);
4731
16e4ecc0
AM
4732 info->callbacks->minfo ("%!", soname, old_bfd,
4733 h->root.root.string);
4734
4ad4eba5
AM
4735 /* A symbol from a library loaded via DT_NEEDED of some
4736 other library is referenced by a regular object.
e56f61be 4737 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4738 --no-add-needed is used and the reference was not
4739 a weak one. */
4f3fedcf 4740 if (old_bfd != NULL
b918acf9 4741 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4742 {
4743 (*_bfd_error_handler)
3cbc5de0 4744 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4745 old_bfd, name);
ff5ac77b 4746 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4747 goto error_free_vers;
4748 }
4749
a50b1753 4750 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4751 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4752
4ad4eba5 4753 add_needed = TRUE;
7e9f0867 4754 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4755 if (ret < 0)
4756 goto error_free_vers;
4757
4758 BFD_ASSERT (ret == 0);
4759 }
4760 }
4761 }
4762
66eb6687
AM
4763 if (extversym != NULL)
4764 {
4765 free (extversym);
4766 extversym = NULL;
4767 }
4768
4769 if (isymbuf != NULL)
4770 {
4771 free (isymbuf);
4772 isymbuf = NULL;
4773 }
4774
4775 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4776 {
4777 unsigned int i;
4778
4779 /* Restore the symbol table. */
f45794cb
AM
4780 old_ent = (char *) old_tab + tabsize;
4781 memset (elf_sym_hashes (abfd), 0,
4782 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4783 htab->root.table.table = old_table;
4784 htab->root.table.size = old_size;
4785 htab->root.table.count = old_count;
66eb6687 4786 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4787 htab->root.undefs = old_undefs;
4788 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
4789 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4790 free (old_strtab);
4791 old_strtab = NULL;
66eb6687
AM
4792 for (i = 0; i < htab->root.table.size; i++)
4793 {
4794 struct bfd_hash_entry *p;
4795 struct elf_link_hash_entry *h;
3e0882af
L
4796 bfd_size_type size;
4797 unsigned int alignment_power;
66eb6687
AM
4798
4799 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4800 {
4801 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4802 if (h->root.type == bfd_link_hash_warning)
4803 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4804
3e0882af
L
4805 /* Preserve the maximum alignment and size for common
4806 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4807 since it can still be loaded at run time by another
3e0882af
L
4808 dynamic lib. */
4809 if (h->root.type == bfd_link_hash_common)
4810 {
4811 size = h->root.u.c.size;
4812 alignment_power = h->root.u.c.p->alignment_power;
4813 }
4814 else
4815 {
4816 size = 0;
4817 alignment_power = 0;
4818 }
66eb6687
AM
4819 memcpy (p, old_ent, htab->root.table.entsize);
4820 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4821 h = (struct elf_link_hash_entry *) p;
4822 if (h->root.type == bfd_link_hash_warning)
4823 {
4824 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4825 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4826 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4827 }
a4542f1b 4828 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4829 {
4830 if (size > h->root.u.c.size)
4831 h->root.u.c.size = size;
4832 if (alignment_power > h->root.u.c.p->alignment_power)
4833 h->root.u.c.p->alignment_power = alignment_power;
4834 }
66eb6687
AM
4835 }
4836 }
4837
5061a885
AM
4838 /* Make a special call to the linker "notice" function to
4839 tell it that symbols added for crefs may need to be removed. */
e5034e59 4840 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4841 goto error_free_vers;
5061a885 4842
66eb6687
AM
4843 free (old_tab);
4844 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4845 alloc_mark);
4846 if (nondeflt_vers != NULL)
4847 free (nondeflt_vers);
4848 return TRUE;
4849 }
2de92251 4850
66eb6687
AM
4851 if (old_tab != NULL)
4852 {
e5034e59 4853 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4854 goto error_free_vers;
66eb6687
AM
4855 free (old_tab);
4856 old_tab = NULL;
4857 }
4858
c6e8a9a8
L
4859 /* Now that all the symbols from this input file are created, if
4860 not performing a relocatable link, handle .symver foo, foo@BAR
4861 such that any relocs against foo become foo@BAR. */
0e1862bb 4862 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5
AM
4863 {
4864 bfd_size_type cnt, symidx;
4865
4866 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4867 {
4868 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4869 char *shortname, *p;
4870
4871 p = strchr (h->root.root.string, ELF_VER_CHR);
4872 if (p == NULL
4873 || (h->root.type != bfd_link_hash_defined
4874 && h->root.type != bfd_link_hash_defweak))
4875 continue;
4876
4877 amt = p - h->root.root.string;
a50b1753 4878 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4879 if (!shortname)
4880 goto error_free_vers;
4ad4eba5
AM
4881 memcpy (shortname, h->root.root.string, amt);
4882 shortname[amt] = '\0';
4883
4884 hi = (struct elf_link_hash_entry *)
66eb6687 4885 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4886 FALSE, FALSE, FALSE);
4887 if (hi != NULL
4888 && hi->root.type == h->root.type
4889 && hi->root.u.def.value == h->root.u.def.value
4890 && hi->root.u.def.section == h->root.u.def.section)
4891 {
4892 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4893 hi->root.type = bfd_link_hash_indirect;
4894 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4895 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4896 sym_hash = elf_sym_hashes (abfd);
4897 if (sym_hash)
4898 for (symidx = 0; symidx < extsymcount; ++symidx)
4899 if (sym_hash[symidx] == hi)
4900 {
4901 sym_hash[symidx] = h;
4902 break;
4903 }
4904 }
4905 free (shortname);
4906 }
4907 free (nondeflt_vers);
4908 nondeflt_vers = NULL;
4909 }
4910
4ad4eba5
AM
4911 /* Now set the weakdefs field correctly for all the weak defined
4912 symbols we found. The only way to do this is to search all the
4913 symbols. Since we only need the information for non functions in
4914 dynamic objects, that's the only time we actually put anything on
4915 the list WEAKS. We need this information so that if a regular
4916 object refers to a symbol defined weakly in a dynamic object, the
4917 real symbol in the dynamic object is also put in the dynamic
4918 symbols; we also must arrange for both symbols to point to the
4919 same memory location. We could handle the general case of symbol
4920 aliasing, but a general symbol alias can only be generated in
4921 assembler code, handling it correctly would be very time
4922 consuming, and other ELF linkers don't handle general aliasing
4923 either. */
4924 if (weaks != NULL)
4925 {
4926 struct elf_link_hash_entry **hpp;
4927 struct elf_link_hash_entry **hppend;
4928 struct elf_link_hash_entry **sorted_sym_hash;
4929 struct elf_link_hash_entry *h;
4930 size_t sym_count;
4931
4932 /* Since we have to search the whole symbol list for each weak
4933 defined symbol, search time for N weak defined symbols will be
4934 O(N^2). Binary search will cut it down to O(NlogN). */
4935 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 4936 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4937 if (sorted_sym_hash == NULL)
4938 goto error_return;
4939 sym_hash = sorted_sym_hash;
4940 hpp = elf_sym_hashes (abfd);
4941 hppend = hpp + extsymcount;
4942 sym_count = 0;
4943 for (; hpp < hppend; hpp++)
4944 {
4945 h = *hpp;
4946 if (h != NULL
4947 && h->root.type == bfd_link_hash_defined
fcb93ecf 4948 && !bed->is_function_type (h->type))
4ad4eba5
AM
4949 {
4950 *sym_hash = h;
4951 sym_hash++;
4952 sym_count++;
4953 }
4954 }
4955
4956 qsort (sorted_sym_hash, sym_count,
4957 sizeof (struct elf_link_hash_entry *),
4958 elf_sort_symbol);
4959
4960 while (weaks != NULL)
4961 {
4962 struct elf_link_hash_entry *hlook;
4963 asection *slook;
4964 bfd_vma vlook;
ed54588d 4965 size_t i, j, idx = 0;
4ad4eba5
AM
4966
4967 hlook = weaks;
f6e332e6
AM
4968 weaks = hlook->u.weakdef;
4969 hlook->u.weakdef = NULL;
4ad4eba5
AM
4970
4971 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4972 || hlook->root.type == bfd_link_hash_defweak
4973 || hlook->root.type == bfd_link_hash_common
4974 || hlook->root.type == bfd_link_hash_indirect);
4975 slook = hlook->root.u.def.section;
4976 vlook = hlook->root.u.def.value;
4977
4ad4eba5
AM
4978 i = 0;
4979 j = sym_count;
14160578 4980 while (i != j)
4ad4eba5
AM
4981 {
4982 bfd_signed_vma vdiff;
4983 idx = (i + j) / 2;
14160578 4984 h = sorted_sym_hash[idx];
4ad4eba5
AM
4985 vdiff = vlook - h->root.u.def.value;
4986 if (vdiff < 0)
4987 j = idx;
4988 else if (vdiff > 0)
4989 i = idx + 1;
4990 else
4991 {
d3435ae8 4992 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4993 if (sdiff < 0)
4994 j = idx;
4995 else if (sdiff > 0)
4996 i = idx + 1;
4997 else
14160578 4998 break;
4ad4eba5
AM
4999 }
5000 }
5001
5002 /* We didn't find a value/section match. */
14160578 5003 if (i == j)
4ad4eba5
AM
5004 continue;
5005
14160578
AM
5006 /* With multiple aliases, or when the weak symbol is already
5007 strongly defined, we have multiple matching symbols and
5008 the binary search above may land on any of them. Step
5009 one past the matching symbol(s). */
5010 while (++idx != j)
5011 {
5012 h = sorted_sym_hash[idx];
5013 if (h->root.u.def.section != slook
5014 || h->root.u.def.value != vlook)
5015 break;
5016 }
5017
5018 /* Now look back over the aliases. Since we sorted by size
5019 as well as value and section, we'll choose the one with
5020 the largest size. */
5021 while (idx-- != i)
4ad4eba5 5022 {
14160578 5023 h = sorted_sym_hash[idx];
4ad4eba5
AM
5024
5025 /* Stop if value or section doesn't match. */
14160578
AM
5026 if (h->root.u.def.section != slook
5027 || h->root.u.def.value != vlook)
4ad4eba5
AM
5028 break;
5029 else if (h != hlook)
5030 {
f6e332e6 5031 hlook->u.weakdef = h;
4ad4eba5
AM
5032
5033 /* If the weak definition is in the list of dynamic
5034 symbols, make sure the real definition is put
5035 there as well. */
5036 if (hlook->dynindx != -1 && h->dynindx == -1)
5037 {
c152c796 5038 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5039 {
5040 err_free_sym_hash:
5041 free (sorted_sym_hash);
5042 goto error_return;
5043 }
4ad4eba5
AM
5044 }
5045
5046 /* If the real definition is in the list of dynamic
5047 symbols, make sure the weak definition is put
5048 there as well. If we don't do this, then the
5049 dynamic loader might not merge the entries for the
5050 real definition and the weak definition. */
5051 if (h->dynindx != -1 && hlook->dynindx == -1)
5052 {
c152c796 5053 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5054 goto err_free_sym_hash;
4ad4eba5
AM
5055 }
5056 break;
5057 }
5058 }
5059 }
5060
5061 free (sorted_sym_hash);
5062 }
5063
33177bb1
AM
5064 if (bed->check_directives
5065 && !(*bed->check_directives) (abfd, info))
5066 return FALSE;
85fbca6a 5067
d9689752
L
5068 if (!info->check_relocs_after_open_input
5069 && !_bfd_elf_link_check_relocs (abfd, info))
5070 return FALSE;
4ad4eba5
AM
5071
5072 /* If this is a non-traditional link, try to optimize the handling
5073 of the .stab/.stabstr sections. */
5074 if (! dynamic
5075 && ! info->traditional_format
66eb6687 5076 && is_elf_hash_table (htab)
4ad4eba5
AM
5077 && (info->strip != strip_all && info->strip != strip_debugger))
5078 {
5079 asection *stabstr;
5080
5081 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5082 if (stabstr != NULL)
5083 {
5084 bfd_size_type string_offset = 0;
5085 asection *stab;
5086
5087 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5088 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5089 && (!stab->name[5] ||
5090 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5091 && (stab->flags & SEC_MERGE) == 0
5092 && !bfd_is_abs_section (stab->output_section))
5093 {
5094 struct bfd_elf_section_data *secdata;
5095
5096 secdata = elf_section_data (stab);
66eb6687
AM
5097 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5098 stabstr, &secdata->sec_info,
4ad4eba5
AM
5099 &string_offset))
5100 goto error_return;
5101 if (secdata->sec_info)
dbaa2011 5102 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5103 }
5104 }
5105 }
5106
66eb6687 5107 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5108 {
5109 /* Add this bfd to the loaded list. */
5110 struct elf_link_loaded_list *n;
5111
ca4be51c 5112 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5113 if (n == NULL)
5114 goto error_return;
5115 n->abfd = abfd;
66eb6687
AM
5116 n->next = htab->loaded;
5117 htab->loaded = n;
4ad4eba5
AM
5118 }
5119
5120 return TRUE;
5121
5122 error_free_vers:
66eb6687
AM
5123 if (old_tab != NULL)
5124 free (old_tab);
5b677558
AM
5125 if (old_strtab != NULL)
5126 free (old_strtab);
4ad4eba5
AM
5127 if (nondeflt_vers != NULL)
5128 free (nondeflt_vers);
5129 if (extversym != NULL)
5130 free (extversym);
5131 error_free_sym:
5132 if (isymbuf != NULL)
5133 free (isymbuf);
5134 error_return:
5135 return FALSE;
5136}
5137
8387904d
AM
5138/* Return the linker hash table entry of a symbol that might be
5139 satisfied by an archive symbol. Return -1 on error. */
5140
5141struct elf_link_hash_entry *
5142_bfd_elf_archive_symbol_lookup (bfd *abfd,
5143 struct bfd_link_info *info,
5144 const char *name)
5145{
5146 struct elf_link_hash_entry *h;
5147 char *p, *copy;
5148 size_t len, first;
5149
2a41f396 5150 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5151 if (h != NULL)
5152 return h;
5153
5154 /* If this is a default version (the name contains @@), look up the
5155 symbol again with only one `@' as well as without the version.
5156 The effect is that references to the symbol with and without the
5157 version will be matched by the default symbol in the archive. */
5158
5159 p = strchr (name, ELF_VER_CHR);
5160 if (p == NULL || p[1] != ELF_VER_CHR)
5161 return h;
5162
5163 /* First check with only one `@'. */
5164 len = strlen (name);
a50b1753 5165 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5166 if (copy == NULL)
5167 return (struct elf_link_hash_entry *) 0 - 1;
5168
5169 first = p - name + 1;
5170 memcpy (copy, name, first);
5171 memcpy (copy + first, name + first + 1, len - first);
5172
2a41f396 5173 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5174 if (h == NULL)
5175 {
5176 /* We also need to check references to the symbol without the
5177 version. */
5178 copy[first - 1] = '\0';
5179 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5180 FALSE, FALSE, TRUE);
8387904d
AM
5181 }
5182
5183 bfd_release (abfd, copy);
5184 return h;
5185}
5186
0ad989f9 5187/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5188 don't use _bfd_generic_link_add_archive_symbols because we need to
5189 handle versioned symbols.
0ad989f9
L
5190
5191 Fortunately, ELF archive handling is simpler than that done by
5192 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5193 oddities. In ELF, if we find a symbol in the archive map, and the
5194 symbol is currently undefined, we know that we must pull in that
5195 object file.
5196
5197 Unfortunately, we do have to make multiple passes over the symbol
5198 table until nothing further is resolved. */
5199
4ad4eba5
AM
5200static bfd_boolean
5201elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5202{
5203 symindex c;
13e570f8 5204 unsigned char *included = NULL;
0ad989f9
L
5205 carsym *symdefs;
5206 bfd_boolean loop;
5207 bfd_size_type amt;
8387904d
AM
5208 const struct elf_backend_data *bed;
5209 struct elf_link_hash_entry * (*archive_symbol_lookup)
5210 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5211
5212 if (! bfd_has_map (abfd))
5213 {
5214 /* An empty archive is a special case. */
5215 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5216 return TRUE;
5217 bfd_set_error (bfd_error_no_armap);
5218 return FALSE;
5219 }
5220
5221 /* Keep track of all symbols we know to be already defined, and all
5222 files we know to be already included. This is to speed up the
5223 second and subsequent passes. */
5224 c = bfd_ardata (abfd)->symdef_count;
5225 if (c == 0)
5226 return TRUE;
5227 amt = c;
13e570f8
AM
5228 amt *= sizeof (*included);
5229 included = (unsigned char *) bfd_zmalloc (amt);
5230 if (included == NULL)
5231 return FALSE;
0ad989f9
L
5232
5233 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5234 bed = get_elf_backend_data (abfd);
5235 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5236
5237 do
5238 {
5239 file_ptr last;
5240 symindex i;
5241 carsym *symdef;
5242 carsym *symdefend;
5243
5244 loop = FALSE;
5245 last = -1;
5246
5247 symdef = symdefs;
5248 symdefend = symdef + c;
5249 for (i = 0; symdef < symdefend; symdef++, i++)
5250 {
5251 struct elf_link_hash_entry *h;
5252 bfd *element;
5253 struct bfd_link_hash_entry *undefs_tail;
5254 symindex mark;
5255
13e570f8 5256 if (included[i])
0ad989f9
L
5257 continue;
5258 if (symdef->file_offset == last)
5259 {
5260 included[i] = TRUE;
5261 continue;
5262 }
5263
8387904d
AM
5264 h = archive_symbol_lookup (abfd, info, symdef->name);
5265 if (h == (struct elf_link_hash_entry *) 0 - 1)
5266 goto error_return;
0ad989f9
L
5267
5268 if (h == NULL)
5269 continue;
5270
5271 if (h->root.type == bfd_link_hash_common)
5272 {
5273 /* We currently have a common symbol. The archive map contains
5274 a reference to this symbol, so we may want to include it. We
5275 only want to include it however, if this archive element
5276 contains a definition of the symbol, not just another common
5277 declaration of it.
5278
5279 Unfortunately some archivers (including GNU ar) will put
5280 declarations of common symbols into their archive maps, as
5281 well as real definitions, so we cannot just go by the archive
5282 map alone. Instead we must read in the element's symbol
5283 table and check that to see what kind of symbol definition
5284 this is. */
5285 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5286 continue;
5287 }
5288 else if (h->root.type != bfd_link_hash_undefined)
5289 {
5290 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5291 /* Symbol must be defined. Don't check it again. */
5292 included[i] = TRUE;
0ad989f9
L
5293 continue;
5294 }
5295
5296 /* We need to include this archive member. */
5297 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5298 if (element == NULL)
5299 goto error_return;
5300
5301 if (! bfd_check_format (element, bfd_object))
5302 goto error_return;
5303
0ad989f9
L
5304 undefs_tail = info->hash->undefs_tail;
5305
0e144ba7
AM
5306 if (!(*info->callbacks
5307 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5308 continue;
0e144ba7 5309 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5310 goto error_return;
5311
5312 /* If there are any new undefined symbols, we need to make
5313 another pass through the archive in order to see whether
5314 they can be defined. FIXME: This isn't perfect, because
5315 common symbols wind up on undefs_tail and because an
5316 undefined symbol which is defined later on in this pass
5317 does not require another pass. This isn't a bug, but it
5318 does make the code less efficient than it could be. */
5319 if (undefs_tail != info->hash->undefs_tail)
5320 loop = TRUE;
5321
5322 /* Look backward to mark all symbols from this object file
5323 which we have already seen in this pass. */
5324 mark = i;
5325 do
5326 {
5327 included[mark] = TRUE;
5328 if (mark == 0)
5329 break;
5330 --mark;
5331 }
5332 while (symdefs[mark].file_offset == symdef->file_offset);
5333
5334 /* We mark subsequent symbols from this object file as we go
5335 on through the loop. */
5336 last = symdef->file_offset;
5337 }
5338 }
5339 while (loop);
5340
0ad989f9
L
5341 free (included);
5342
5343 return TRUE;
5344
5345 error_return:
0ad989f9
L
5346 if (included != NULL)
5347 free (included);
5348 return FALSE;
5349}
4ad4eba5
AM
5350
5351/* Given an ELF BFD, add symbols to the global hash table as
5352 appropriate. */
5353
5354bfd_boolean
5355bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5356{
5357 switch (bfd_get_format (abfd))
5358 {
5359 case bfd_object:
5360 return elf_link_add_object_symbols (abfd, info);
5361 case bfd_archive:
5362 return elf_link_add_archive_symbols (abfd, info);
5363 default:
5364 bfd_set_error (bfd_error_wrong_format);
5365 return FALSE;
5366 }
5367}
5a580b3a 5368\f
14b1c01e
AM
5369struct hash_codes_info
5370{
5371 unsigned long *hashcodes;
5372 bfd_boolean error;
5373};
a0c8462f 5374
5a580b3a
AM
5375/* This function will be called though elf_link_hash_traverse to store
5376 all hash value of the exported symbols in an array. */
5377
5378static bfd_boolean
5379elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5380{
a50b1753 5381 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5382 const char *name;
5a580b3a
AM
5383 unsigned long ha;
5384 char *alc = NULL;
5385
5a580b3a
AM
5386 /* Ignore indirect symbols. These are added by the versioning code. */
5387 if (h->dynindx == -1)
5388 return TRUE;
5389
5390 name = h->root.root.string;
422f1182 5391 if (h->versioned >= versioned)
5a580b3a 5392 {
422f1182
L
5393 char *p = strchr (name, ELF_VER_CHR);
5394 if (p != NULL)
14b1c01e 5395 {
422f1182
L
5396 alc = (char *) bfd_malloc (p - name + 1);
5397 if (alc == NULL)
5398 {
5399 inf->error = TRUE;
5400 return FALSE;
5401 }
5402 memcpy (alc, name, p - name);
5403 alc[p - name] = '\0';
5404 name = alc;
14b1c01e 5405 }
5a580b3a
AM
5406 }
5407
5408 /* Compute the hash value. */
5409 ha = bfd_elf_hash (name);
5410
5411 /* Store the found hash value in the array given as the argument. */
14b1c01e 5412 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5413
5414 /* And store it in the struct so that we can put it in the hash table
5415 later. */
f6e332e6 5416 h->u.elf_hash_value = ha;
5a580b3a
AM
5417
5418 if (alc != NULL)
5419 free (alc);
5420
5421 return TRUE;
5422}
5423
fdc90cb4
JJ
5424struct collect_gnu_hash_codes
5425{
5426 bfd *output_bfd;
5427 const struct elf_backend_data *bed;
5428 unsigned long int nsyms;
5429 unsigned long int maskbits;
5430 unsigned long int *hashcodes;
5431 unsigned long int *hashval;
5432 unsigned long int *indx;
5433 unsigned long int *counts;
5434 bfd_vma *bitmask;
5435 bfd_byte *contents;
5436 long int min_dynindx;
5437 unsigned long int bucketcount;
5438 unsigned long int symindx;
5439 long int local_indx;
5440 long int shift1, shift2;
5441 unsigned long int mask;
14b1c01e 5442 bfd_boolean error;
fdc90cb4
JJ
5443};
5444
5445/* This function will be called though elf_link_hash_traverse to store
5446 all hash value of the exported symbols in an array. */
5447
5448static bfd_boolean
5449elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5450{
a50b1753 5451 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5452 const char *name;
fdc90cb4
JJ
5453 unsigned long ha;
5454 char *alc = NULL;
5455
fdc90cb4
JJ
5456 /* Ignore indirect symbols. These are added by the versioning code. */
5457 if (h->dynindx == -1)
5458 return TRUE;
5459
5460 /* Ignore also local symbols and undefined symbols. */
5461 if (! (*s->bed->elf_hash_symbol) (h))
5462 return TRUE;
5463
5464 name = h->root.root.string;
422f1182 5465 if (h->versioned >= versioned)
fdc90cb4 5466 {
422f1182
L
5467 char *p = strchr (name, ELF_VER_CHR);
5468 if (p != NULL)
14b1c01e 5469 {
422f1182
L
5470 alc = (char *) bfd_malloc (p - name + 1);
5471 if (alc == NULL)
5472 {
5473 s->error = TRUE;
5474 return FALSE;
5475 }
5476 memcpy (alc, name, p - name);
5477 alc[p - name] = '\0';
5478 name = alc;
14b1c01e 5479 }
fdc90cb4
JJ
5480 }
5481
5482 /* Compute the hash value. */
5483 ha = bfd_elf_gnu_hash (name);
5484
5485 /* Store the found hash value in the array for compute_bucket_count,
5486 and also for .dynsym reordering purposes. */
5487 s->hashcodes[s->nsyms] = ha;
5488 s->hashval[h->dynindx] = ha;
5489 ++s->nsyms;
5490 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5491 s->min_dynindx = h->dynindx;
5492
5493 if (alc != NULL)
5494 free (alc);
5495
5496 return TRUE;
5497}
5498
5499/* This function will be called though elf_link_hash_traverse to do
5500 final dynaminc symbol renumbering. */
5501
5502static bfd_boolean
5503elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5504{
a50b1753 5505 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5506 unsigned long int bucket;
5507 unsigned long int val;
5508
fdc90cb4
JJ
5509 /* Ignore indirect symbols. */
5510 if (h->dynindx == -1)
5511 return TRUE;
5512
5513 /* Ignore also local symbols and undefined symbols. */
5514 if (! (*s->bed->elf_hash_symbol) (h))
5515 {
5516 if (h->dynindx >= s->min_dynindx)
5517 h->dynindx = s->local_indx++;
5518 return TRUE;
5519 }
5520
5521 bucket = s->hashval[h->dynindx] % s->bucketcount;
5522 val = (s->hashval[h->dynindx] >> s->shift1)
5523 & ((s->maskbits >> s->shift1) - 1);
5524 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5525 s->bitmask[val]
5526 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5527 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5528 if (s->counts[bucket] == 1)
5529 /* Last element terminates the chain. */
5530 val |= 1;
5531 bfd_put_32 (s->output_bfd, val,
5532 s->contents + (s->indx[bucket] - s->symindx) * 4);
5533 --s->counts[bucket];
5534 h->dynindx = s->indx[bucket]++;
5535 return TRUE;
5536}
5537
5538/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5539
5540bfd_boolean
5541_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5542{
5543 return !(h->forced_local
5544 || h->root.type == bfd_link_hash_undefined
5545 || h->root.type == bfd_link_hash_undefweak
5546 || ((h->root.type == bfd_link_hash_defined
5547 || h->root.type == bfd_link_hash_defweak)
5548 && h->root.u.def.section->output_section == NULL));
5549}
5550
5a580b3a
AM
5551/* Array used to determine the number of hash table buckets to use
5552 based on the number of symbols there are. If there are fewer than
5553 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5554 fewer than 37 we use 17 buckets, and so forth. We never use more
5555 than 32771 buckets. */
5556
5557static const size_t elf_buckets[] =
5558{
5559 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5560 16411, 32771, 0
5561};
5562
5563/* Compute bucket count for hashing table. We do not use a static set
5564 of possible tables sizes anymore. Instead we determine for all
5565 possible reasonable sizes of the table the outcome (i.e., the
5566 number of collisions etc) and choose the best solution. The
5567 weighting functions are not too simple to allow the table to grow
5568 without bounds. Instead one of the weighting factors is the size.
5569 Therefore the result is always a good payoff between few collisions
5570 (= short chain lengths) and table size. */
5571static size_t
b20dd2ce 5572compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5573 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5574 unsigned long int nsyms,
5575 int gnu_hash)
5a580b3a 5576{
5a580b3a 5577 size_t best_size = 0;
5a580b3a 5578 unsigned long int i;
5a580b3a 5579
5a580b3a
AM
5580 /* We have a problem here. The following code to optimize the table
5581 size requires an integer type with more the 32 bits. If
5582 BFD_HOST_U_64_BIT is set we know about such a type. */
5583#ifdef BFD_HOST_U_64_BIT
5584 if (info->optimize)
5585 {
5a580b3a
AM
5586 size_t minsize;
5587 size_t maxsize;
5588 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5589 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5590 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5591 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5592 unsigned long int *counts;
d40f3da9 5593 bfd_size_type amt;
0883b6e0 5594 unsigned int no_improvement_count = 0;
5a580b3a
AM
5595
5596 /* Possible optimization parameters: if we have NSYMS symbols we say
5597 that the hashing table must at least have NSYMS/4 and at most
5598 2*NSYMS buckets. */
5599 minsize = nsyms / 4;
5600 if (minsize == 0)
5601 minsize = 1;
5602 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5603 if (gnu_hash)
5604 {
5605 if (minsize < 2)
5606 minsize = 2;
5607 if ((best_size & 31) == 0)
5608 ++best_size;
5609 }
5a580b3a
AM
5610
5611 /* Create array where we count the collisions in. We must use bfd_malloc
5612 since the size could be large. */
5613 amt = maxsize;
5614 amt *= sizeof (unsigned long int);
a50b1753 5615 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5616 if (counts == NULL)
fdc90cb4 5617 return 0;
5a580b3a
AM
5618
5619 /* Compute the "optimal" size for the hash table. The criteria is a
5620 minimal chain length. The minor criteria is (of course) the size
5621 of the table. */
5622 for (i = minsize; i < maxsize; ++i)
5623 {
5624 /* Walk through the array of hashcodes and count the collisions. */
5625 BFD_HOST_U_64_BIT max;
5626 unsigned long int j;
5627 unsigned long int fact;
5628
fdc90cb4
JJ
5629 if (gnu_hash && (i & 31) == 0)
5630 continue;
5631
5a580b3a
AM
5632 memset (counts, '\0', i * sizeof (unsigned long int));
5633
5634 /* Determine how often each hash bucket is used. */
5635 for (j = 0; j < nsyms; ++j)
5636 ++counts[hashcodes[j] % i];
5637
5638 /* For the weight function we need some information about the
5639 pagesize on the target. This is information need not be 100%
5640 accurate. Since this information is not available (so far) we
5641 define it here to a reasonable default value. If it is crucial
5642 to have a better value some day simply define this value. */
5643# ifndef BFD_TARGET_PAGESIZE
5644# define BFD_TARGET_PAGESIZE (4096)
5645# endif
5646
fdc90cb4
JJ
5647 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5648 and the chains. */
5649 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5650
5651# if 1
5652 /* Variant 1: optimize for short chains. We add the squares
5653 of all the chain lengths (which favors many small chain
5654 over a few long chains). */
5655 for (j = 0; j < i; ++j)
5656 max += counts[j] * counts[j];
5657
5658 /* This adds penalties for the overall size of the table. */
fdc90cb4 5659 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5660 max *= fact * fact;
5661# else
5662 /* Variant 2: Optimize a lot more for small table. Here we
5663 also add squares of the size but we also add penalties for
5664 empty slots (the +1 term). */
5665 for (j = 0; j < i; ++j)
5666 max += (1 + counts[j]) * (1 + counts[j]);
5667
5668 /* The overall size of the table is considered, but not as
5669 strong as in variant 1, where it is squared. */
fdc90cb4 5670 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5671 max *= fact;
5672# endif
5673
5674 /* Compare with current best results. */
5675 if (max < best_chlen)
5676 {
5677 best_chlen = max;
5678 best_size = i;
ca4be51c 5679 no_improvement_count = 0;
5a580b3a 5680 }
0883b6e0
NC
5681 /* PR 11843: Avoid futile long searches for the best bucket size
5682 when there are a large number of symbols. */
5683 else if (++no_improvement_count == 100)
5684 break;
5a580b3a
AM
5685 }
5686
5687 free (counts);
5688 }
5689 else
5690#endif /* defined (BFD_HOST_U_64_BIT) */
5691 {
5692 /* This is the fallback solution if no 64bit type is available or if we
5693 are not supposed to spend much time on optimizations. We select the
5694 bucket count using a fixed set of numbers. */
5695 for (i = 0; elf_buckets[i] != 0; i++)
5696 {
5697 best_size = elf_buckets[i];
fdc90cb4 5698 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5699 break;
5700 }
fdc90cb4
JJ
5701 if (gnu_hash && best_size < 2)
5702 best_size = 2;
5a580b3a
AM
5703 }
5704
5a580b3a
AM
5705 return best_size;
5706}
5707
d0bf826b
AM
5708/* Size any SHT_GROUP section for ld -r. */
5709
5710bfd_boolean
5711_bfd_elf_size_group_sections (struct bfd_link_info *info)
5712{
5713 bfd *ibfd;
5714
c72f2fb2 5715 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5716 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5717 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5718 return FALSE;
5719 return TRUE;
5720}
5721
04c3a755
NS
5722/* Set a default stack segment size. The value in INFO wins. If it
5723 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5724 undefined it is initialized. */
5725
5726bfd_boolean
5727bfd_elf_stack_segment_size (bfd *output_bfd,
5728 struct bfd_link_info *info,
5729 const char *legacy_symbol,
5730 bfd_vma default_size)
5731{
5732 struct elf_link_hash_entry *h = NULL;
5733
5734 /* Look for legacy symbol. */
5735 if (legacy_symbol)
5736 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5737 FALSE, FALSE, FALSE);
5738 if (h && (h->root.type == bfd_link_hash_defined
5739 || h->root.type == bfd_link_hash_defweak)
5740 && h->def_regular
5741 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5742 {
5743 /* The symbol has no type if specified on the command line. */
5744 h->type = STT_OBJECT;
5745 if (info->stacksize)
5746 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5747 output_bfd, legacy_symbol);
5748 else if (h->root.u.def.section != bfd_abs_section_ptr)
5749 (*_bfd_error_handler) (_("%B: %s not absolute"),
5750 output_bfd, legacy_symbol);
5751 else
5752 info->stacksize = h->root.u.def.value;
5753 }
5754
5755 if (!info->stacksize)
5756 /* If the user didn't set a size, or explicitly inhibit the
5757 size, set it now. */
5758 info->stacksize = default_size;
5759
5760 /* Provide the legacy symbol, if it is referenced. */
5761 if (h && (h->root.type == bfd_link_hash_undefined
5762 || h->root.type == bfd_link_hash_undefweak))
5763 {
5764 struct bfd_link_hash_entry *bh = NULL;
5765
5766 if (!(_bfd_generic_link_add_one_symbol
5767 (info, output_bfd, legacy_symbol,
5768 BSF_GLOBAL, bfd_abs_section_ptr,
5769 info->stacksize >= 0 ? info->stacksize : 0,
5770 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5771 return FALSE;
5772
5773 h = (struct elf_link_hash_entry *) bh;
5774 h->def_regular = 1;
5775 h->type = STT_OBJECT;
5776 }
5777
5778 return TRUE;
5779}
5780
5a580b3a
AM
5781/* Set up the sizes and contents of the ELF dynamic sections. This is
5782 called by the ELF linker emulation before_allocation routine. We
5783 must set the sizes of the sections before the linker sets the
5784 addresses of the various sections. */
5785
5786bfd_boolean
5787bfd_elf_size_dynamic_sections (bfd *output_bfd,
5788 const char *soname,
5789 const char *rpath,
5790 const char *filter_shlib,
7ee314fa
AM
5791 const char *audit,
5792 const char *depaudit,
5a580b3a
AM
5793 const char * const *auxiliary_filters,
5794 struct bfd_link_info *info,
fd91d419 5795 asection **sinterpptr)
5a580b3a
AM
5796{
5797 bfd_size_type soname_indx;
5798 bfd *dynobj;
5799 const struct elf_backend_data *bed;
28caa186 5800 struct elf_info_failed asvinfo;
5a580b3a
AM
5801
5802 *sinterpptr = NULL;
5803
5804 soname_indx = (bfd_size_type) -1;
5805
5806 if (!is_elf_hash_table (info->hash))
5807 return TRUE;
5808
6bfdb61b 5809 bed = get_elf_backend_data (output_bfd);
04c3a755
NS
5810
5811 /* Any syms created from now on start with -1 in
5812 got.refcount/offset and plt.refcount/offset. */
5813 elf_hash_table (info)->init_got_refcount
5814 = elf_hash_table (info)->init_got_offset;
5815 elf_hash_table (info)->init_plt_refcount
5816 = elf_hash_table (info)->init_plt_offset;
5817
0e1862bb 5818 if (bfd_link_relocatable (info)
04c3a755
NS
5819 && !_bfd_elf_size_group_sections (info))
5820 return FALSE;
5821
5822 /* The backend may have to create some sections regardless of whether
5823 we're dynamic or not. */
5824 if (bed->elf_backend_always_size_sections
5825 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5826 return FALSE;
5827
5828 /* Determine any GNU_STACK segment requirements, after the backend
5829 has had a chance to set a default segment size. */
5a580b3a 5830 if (info->execstack)
12bd6957 5831 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5a580b3a 5832 else if (info->noexecstack)
12bd6957 5833 elf_stack_flags (output_bfd) = PF_R | PF_W;
5a580b3a
AM
5834 else
5835 {
5836 bfd *inputobj;
5837 asection *notesec = NULL;
5838 int exec = 0;
5839
5840 for (inputobj = info->input_bfds;
5841 inputobj;
c72f2fb2 5842 inputobj = inputobj->link.next)
5a580b3a
AM
5843 {
5844 asection *s;
5845
a92c088a
L
5846 if (inputobj->flags
5847 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5848 continue;
5849 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5850 if (s)
5851 {
5852 if (s->flags & SEC_CODE)
5853 exec = PF_X;
5854 notesec = s;
5855 }
6bfdb61b 5856 else if (bed->default_execstack)
5a580b3a
AM
5857 exec = PF_X;
5858 }
04c3a755 5859 if (notesec || info->stacksize > 0)
12bd6957 5860 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
0e1862bb 5861 if (notesec && exec && bfd_link_relocatable (info)
04c3a755
NS
5862 && notesec->output_section != bfd_abs_section_ptr)
5863 notesec->output_section->flags |= SEC_CODE;
5a580b3a
AM
5864 }
5865
5a580b3a
AM
5866 dynobj = elf_hash_table (info)->dynobj;
5867
9a2a56cc 5868 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
5869 {
5870 struct elf_info_failed eif;
5871 struct elf_link_hash_entry *h;
5872 asection *dynstr;
5873 struct bfd_elf_version_tree *t;
5874 struct bfd_elf_version_expr *d;
046183de 5875 asection *s;
5a580b3a
AM
5876 bfd_boolean all_defined;
5877
3d4d4302 5878 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
9b8b325a 5879 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5a580b3a
AM
5880
5881 if (soname != NULL)
5882 {
5883 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5884 soname, TRUE);
5885 if (soname_indx == (bfd_size_type) -1
5886 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5887 return FALSE;
5888 }
5889
5890 if (info->symbolic)
5891 {
5892 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5893 return FALSE;
5894 info->flags |= DF_SYMBOLIC;
5895 }
5896
5897 if (rpath != NULL)
5898 {
5899 bfd_size_type indx;
b1b00fcc 5900 bfd_vma tag;
5a580b3a
AM
5901
5902 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5903 TRUE);
b1b00fcc 5904 if (indx == (bfd_size_type) -1)
5a580b3a
AM
5905 return FALSE;
5906
b1b00fcc
MF
5907 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5908 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5909 return FALSE;
5a580b3a
AM
5910 }
5911
5912 if (filter_shlib != NULL)
5913 {
5914 bfd_size_type indx;
5915
5916 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5917 filter_shlib, TRUE);
5918 if (indx == (bfd_size_type) -1
5919 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5920 return FALSE;
5921 }
5922
5923 if (auxiliary_filters != NULL)
5924 {
5925 const char * const *p;
5926
5927 for (p = auxiliary_filters; *p != NULL; p++)
5928 {
5929 bfd_size_type indx;
5930
5931 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5932 *p, TRUE);
5933 if (indx == (bfd_size_type) -1
5934 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5935 return FALSE;
5936 }
5937 }
5938
7ee314fa
AM
5939 if (audit != NULL)
5940 {
5941 bfd_size_type indx;
5942
5943 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5944 TRUE);
5945 if (indx == (bfd_size_type) -1
5946 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5947 return FALSE;
5948 }
5949
5950 if (depaudit != NULL)
5951 {
5952 bfd_size_type indx;
5953
5954 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5955 TRUE);
5956 if (indx == (bfd_size_type) -1
5957 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5958 return FALSE;
5959 }
5960
5a580b3a 5961 eif.info = info;
5a580b3a
AM
5962 eif.failed = FALSE;
5963
5964 /* If we are supposed to export all symbols into the dynamic symbol
5965 table (this is not the normal case), then do so. */
55255dae 5966 if (info->export_dynamic
0e1862bb 5967 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
5968 {
5969 elf_link_hash_traverse (elf_hash_table (info),
5970 _bfd_elf_export_symbol,
5971 &eif);
5972 if (eif.failed)
5973 return FALSE;
5974 }
5975
5976 /* Make all global versions with definition. */
fd91d419 5977 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5978 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5979 if (!d->symver && d->literal)
5a580b3a
AM
5980 {
5981 const char *verstr, *name;
5982 size_t namelen, verlen, newlen;
93252b1c 5983 char *newname, *p, leading_char;
5a580b3a
AM
5984 struct elf_link_hash_entry *newh;
5985
93252b1c 5986 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 5987 name = d->pattern;
93252b1c 5988 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
5989 verstr = t->name;
5990 verlen = strlen (verstr);
5991 newlen = namelen + verlen + 3;
5992
a50b1753 5993 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
5994 if (newname == NULL)
5995 return FALSE;
93252b1c
MF
5996 newname[0] = leading_char;
5997 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
5998
5999 /* Check the hidden versioned definition. */
6000 p = newname + namelen;
6001 *p++ = ELF_VER_CHR;
6002 memcpy (p, verstr, verlen + 1);
6003 newh = elf_link_hash_lookup (elf_hash_table (info),
6004 newname, FALSE, FALSE,
6005 FALSE);
6006 if (newh == NULL
6007 || (newh->root.type != bfd_link_hash_defined
6008 && newh->root.type != bfd_link_hash_defweak))
6009 {
6010 /* Check the default versioned definition. */
6011 *p++ = ELF_VER_CHR;
6012 memcpy (p, verstr, verlen + 1);
6013 newh = elf_link_hash_lookup (elf_hash_table (info),
6014 newname, FALSE, FALSE,
6015 FALSE);
6016 }
6017 free (newname);
6018
6019 /* Mark this version if there is a definition and it is
6020 not defined in a shared object. */
6021 if (newh != NULL
f5385ebf 6022 && !newh->def_dynamic
5a580b3a
AM
6023 && (newh->root.type == bfd_link_hash_defined
6024 || newh->root.type == bfd_link_hash_defweak))
6025 d->symver = 1;
6026 }
6027
6028 /* Attach all the symbols to their version information. */
5a580b3a 6029 asvinfo.info = info;
5a580b3a
AM
6030 asvinfo.failed = FALSE;
6031
6032 elf_link_hash_traverse (elf_hash_table (info),
6033 _bfd_elf_link_assign_sym_version,
6034 &asvinfo);
6035 if (asvinfo.failed)
6036 return FALSE;
6037
6038 if (!info->allow_undefined_version)
6039 {
6040 /* Check if all global versions have a definition. */
6041 all_defined = TRUE;
fd91d419 6042 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6043 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6044 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
6045 {
6046 (*_bfd_error_handler)
6047 (_("%s: undefined version: %s"),
6048 d->pattern, t->name);
6049 all_defined = FALSE;
6050 }
6051
6052 if (!all_defined)
6053 {
6054 bfd_set_error (bfd_error_bad_value);
6055 return FALSE;
6056 }
6057 }
6058
6059 /* Find all symbols which were defined in a dynamic object and make
6060 the backend pick a reasonable value for them. */
6061 elf_link_hash_traverse (elf_hash_table (info),
6062 _bfd_elf_adjust_dynamic_symbol,
6063 &eif);
6064 if (eif.failed)
6065 return FALSE;
6066
6067 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 6068 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
6069 now so that we know the final size of the .dynamic section. */
6070
6071 /* If there are initialization and/or finalization functions to
6072 call then add the corresponding DT_INIT/DT_FINI entries. */
6073 h = (info->init_function
6074 ? elf_link_hash_lookup (elf_hash_table (info),
6075 info->init_function, FALSE,
6076 FALSE, FALSE)
6077 : NULL);
6078 if (h != NULL
f5385ebf
AM
6079 && (h->ref_regular
6080 || h->def_regular))
5a580b3a
AM
6081 {
6082 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6083 return FALSE;
6084 }
6085 h = (info->fini_function
6086 ? elf_link_hash_lookup (elf_hash_table (info),
6087 info->fini_function, FALSE,
6088 FALSE, FALSE)
6089 : NULL);
6090 if (h != NULL
f5385ebf
AM
6091 && (h->ref_regular
6092 || h->def_regular))
5a580b3a
AM
6093 {
6094 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6095 return FALSE;
6096 }
6097
046183de
AM
6098 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6099 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6100 {
6101 /* DT_PREINIT_ARRAY is not allowed in shared library. */
0e1862bb 6102 if (! bfd_link_executable (info))
5a580b3a
AM
6103 {
6104 bfd *sub;
6105 asection *o;
6106
6107 for (sub = info->input_bfds; sub != NULL;
c72f2fb2 6108 sub = sub->link.next)
3fcd97f1
JJ
6109 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6110 for (o = sub->sections; o != NULL; o = o->next)
6111 if (elf_section_data (o)->this_hdr.sh_type
6112 == SHT_PREINIT_ARRAY)
6113 {
6114 (*_bfd_error_handler)
6115 (_("%B: .preinit_array section is not allowed in DSO"),
6116 sub);
6117 break;
6118 }
5a580b3a
AM
6119
6120 bfd_set_error (bfd_error_nonrepresentable_section);
6121 return FALSE;
6122 }
6123
6124 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6125 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6126 return FALSE;
6127 }
046183de
AM
6128 s = bfd_get_section_by_name (output_bfd, ".init_array");
6129 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6130 {
6131 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6132 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6133 return FALSE;
6134 }
046183de
AM
6135 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6136 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6137 {
6138 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6139 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6140 return FALSE;
6141 }
6142
3d4d4302 6143 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6144 /* If .dynstr is excluded from the link, we don't want any of
6145 these tags. Strictly, we should be checking each section
6146 individually; This quick check covers for the case where
6147 someone does a /DISCARD/ : { *(*) }. */
6148 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6149 {
6150 bfd_size_type strsize;
6151
6152 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
6153 if ((info->emit_hash
6154 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6155 || (info->emit_gnu_hash
6156 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
6157 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6158 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6159 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6160 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6161 bed->s->sizeof_sym))
6162 return FALSE;
6163 }
6164 }
6165
de231f20
CM
6166 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6167 return FALSE;
6168
5a580b3a
AM
6169 /* The backend must work out the sizes of all the other dynamic
6170 sections. */
9a2a56cc
AM
6171 if (dynobj != NULL
6172 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
6173 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6174 return FALSE;
6175
9a2a56cc 6176 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6177 {
554220db 6178 unsigned long section_sym_count;
fd91d419 6179 struct bfd_elf_version_tree *verdefs;
5a580b3a 6180 asection *s;
5a580b3a
AM
6181
6182 /* Set up the version definition section. */
3d4d4302 6183 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
6184 BFD_ASSERT (s != NULL);
6185
6186 /* We may have created additional version definitions if we are
6187 just linking a regular application. */
fd91d419 6188 verdefs = info->version_info;
5a580b3a
AM
6189
6190 /* Skip anonymous version tag. */
6191 if (verdefs != NULL && verdefs->vernum == 0)
6192 verdefs = verdefs->next;
6193
3e3b46e5 6194 if (verdefs == NULL && !info->create_default_symver)
8423293d 6195 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6196 else
6197 {
6198 unsigned int cdefs;
6199 bfd_size_type size;
6200 struct bfd_elf_version_tree *t;
6201 bfd_byte *p;
6202 Elf_Internal_Verdef def;
6203 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
6204 struct bfd_link_hash_entry *bh;
6205 struct elf_link_hash_entry *h;
6206 const char *name;
5a580b3a
AM
6207
6208 cdefs = 0;
6209 size = 0;
6210
6211 /* Make space for the base version. */
6212 size += sizeof (Elf_External_Verdef);
6213 size += sizeof (Elf_External_Verdaux);
6214 ++cdefs;
6215
3e3b46e5
PB
6216 /* Make space for the default version. */
6217 if (info->create_default_symver)
6218 {
6219 size += sizeof (Elf_External_Verdef);
6220 ++cdefs;
6221 }
6222
5a580b3a
AM
6223 for (t = verdefs; t != NULL; t = t->next)
6224 {
6225 struct bfd_elf_version_deps *n;
6226
a6cc6b3b
RO
6227 /* Don't emit base version twice. */
6228 if (t->vernum == 0)
6229 continue;
6230
5a580b3a
AM
6231 size += sizeof (Elf_External_Verdef);
6232 size += sizeof (Elf_External_Verdaux);
6233 ++cdefs;
6234
6235 for (n = t->deps; n != NULL; n = n->next)
6236 size += sizeof (Elf_External_Verdaux);
6237 }
6238
eea6121a 6239 s->size = size;
a50b1753 6240 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6241 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6242 return FALSE;
6243
6244 /* Fill in the version definition section. */
6245
6246 p = s->contents;
6247
6248 def.vd_version = VER_DEF_CURRENT;
6249 def.vd_flags = VER_FLG_BASE;
6250 def.vd_ndx = 1;
6251 def.vd_cnt = 1;
3e3b46e5
PB
6252 if (info->create_default_symver)
6253 {
6254 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6255 def.vd_next = sizeof (Elf_External_Verdef);
6256 }
6257 else
6258 {
6259 def.vd_aux = sizeof (Elf_External_Verdef);
6260 def.vd_next = (sizeof (Elf_External_Verdef)
6261 + sizeof (Elf_External_Verdaux));
6262 }
5a580b3a
AM
6263
6264 if (soname_indx != (bfd_size_type) -1)
6265 {
6266 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6267 soname_indx);
6268 def.vd_hash = bfd_elf_hash (soname);
6269 defaux.vda_name = soname_indx;
3e3b46e5 6270 name = soname;
5a580b3a
AM
6271 }
6272 else
6273 {
5a580b3a
AM
6274 bfd_size_type indx;
6275
06084812 6276 name = lbasename (output_bfd->filename);
5a580b3a
AM
6277 def.vd_hash = bfd_elf_hash (name);
6278 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6279 name, FALSE);
6280 if (indx == (bfd_size_type) -1)
6281 return FALSE;
6282 defaux.vda_name = indx;
6283 }
6284 defaux.vda_next = 0;
6285
6286 _bfd_elf_swap_verdef_out (output_bfd, &def,
6287 (Elf_External_Verdef *) p);
6288 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6289 if (info->create_default_symver)
6290 {
6291 /* Add a symbol representing this version. */
6292 bh = NULL;
6293 if (! (_bfd_generic_link_add_one_symbol
6294 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6295 0, NULL, FALSE,
6296 get_elf_backend_data (dynobj)->collect, &bh)))
6297 return FALSE;
6298 h = (struct elf_link_hash_entry *) bh;
6299 h->non_elf = 0;
6300 h->def_regular = 1;
6301 h->type = STT_OBJECT;
6302 h->verinfo.vertree = NULL;
6303
6304 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6305 return FALSE;
6306
6307 /* Create a duplicate of the base version with the same
6308 aux block, but different flags. */
6309 def.vd_flags = 0;
6310 def.vd_ndx = 2;
6311 def.vd_aux = sizeof (Elf_External_Verdef);
6312 if (verdefs)
6313 def.vd_next = (sizeof (Elf_External_Verdef)
6314 + sizeof (Elf_External_Verdaux));
6315 else
6316 def.vd_next = 0;
6317 _bfd_elf_swap_verdef_out (output_bfd, &def,
6318 (Elf_External_Verdef *) p);
6319 p += sizeof (Elf_External_Verdef);
6320 }
5a580b3a
AM
6321 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6322 (Elf_External_Verdaux *) p);
6323 p += sizeof (Elf_External_Verdaux);
6324
6325 for (t = verdefs; t != NULL; t = t->next)
6326 {
6327 unsigned int cdeps;
6328 struct bfd_elf_version_deps *n;
5a580b3a 6329
a6cc6b3b
RO
6330 /* Don't emit the base version twice. */
6331 if (t->vernum == 0)
6332 continue;
6333
5a580b3a
AM
6334 cdeps = 0;
6335 for (n = t->deps; n != NULL; n = n->next)
6336 ++cdeps;
6337
6338 /* Add a symbol representing this version. */
6339 bh = NULL;
6340 if (! (_bfd_generic_link_add_one_symbol
6341 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6342 0, NULL, FALSE,
6343 get_elf_backend_data (dynobj)->collect, &bh)))
6344 return FALSE;
6345 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6346 h->non_elf = 0;
6347 h->def_regular = 1;
5a580b3a
AM
6348 h->type = STT_OBJECT;
6349 h->verinfo.vertree = t;
6350
c152c796 6351 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6352 return FALSE;
6353
6354 def.vd_version = VER_DEF_CURRENT;
6355 def.vd_flags = 0;
6356 if (t->globals.list == NULL
6357 && t->locals.list == NULL
6358 && ! t->used)
6359 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6360 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6361 def.vd_cnt = cdeps + 1;
6362 def.vd_hash = bfd_elf_hash (t->name);
6363 def.vd_aux = sizeof (Elf_External_Verdef);
6364 def.vd_next = 0;
a6cc6b3b
RO
6365
6366 /* If a basever node is next, it *must* be the last node in
6367 the chain, otherwise Verdef construction breaks. */
6368 if (t->next != NULL && t->next->vernum == 0)
6369 BFD_ASSERT (t->next->next == NULL);
6370
6371 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6372 def.vd_next = (sizeof (Elf_External_Verdef)
6373 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6374
6375 _bfd_elf_swap_verdef_out (output_bfd, &def,
6376 (Elf_External_Verdef *) p);
6377 p += sizeof (Elf_External_Verdef);
6378
6379 defaux.vda_name = h->dynstr_index;
6380 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6381 h->dynstr_index);
6382 defaux.vda_next = 0;
6383 if (t->deps != NULL)
6384 defaux.vda_next = sizeof (Elf_External_Verdaux);
6385 t->name_indx = defaux.vda_name;
6386
6387 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6388 (Elf_External_Verdaux *) p);
6389 p += sizeof (Elf_External_Verdaux);
6390
6391 for (n = t->deps; n != NULL; n = n->next)
6392 {
6393 if (n->version_needed == NULL)
6394 {
6395 /* This can happen if there was an error in the
6396 version script. */
6397 defaux.vda_name = 0;
6398 }
6399 else
6400 {
6401 defaux.vda_name = n->version_needed->name_indx;
6402 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6403 defaux.vda_name);
6404 }
6405 if (n->next == NULL)
6406 defaux.vda_next = 0;
6407 else
6408 defaux.vda_next = sizeof (Elf_External_Verdaux);
6409
6410 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6411 (Elf_External_Verdaux *) p);
6412 p += sizeof (Elf_External_Verdaux);
6413 }
6414 }
6415
6416 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6417 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6418 return FALSE;
6419
6420 elf_tdata (output_bfd)->cverdefs = cdefs;
6421 }
6422
6423 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6424 {
6425 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6426 return FALSE;
6427 }
6428 else if (info->flags & DF_BIND_NOW)
6429 {
6430 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6431 return FALSE;
6432 }
6433
6434 if (info->flags_1)
6435 {
0e1862bb 6436 if (bfd_link_executable (info))
5a580b3a
AM
6437 info->flags_1 &= ~ (DF_1_INITFIRST
6438 | DF_1_NODELETE
6439 | DF_1_NOOPEN);
6440 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6441 return FALSE;
6442 }
6443
6444 /* Work out the size of the version reference section. */
6445
3d4d4302 6446 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6447 BFD_ASSERT (s != NULL);
6448 {
6449 struct elf_find_verdep_info sinfo;
6450
5a580b3a
AM
6451 sinfo.info = info;
6452 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6453 if (sinfo.vers == 0)
6454 sinfo.vers = 1;
6455 sinfo.failed = FALSE;
6456
6457 elf_link_hash_traverse (elf_hash_table (info),
6458 _bfd_elf_link_find_version_dependencies,
6459 &sinfo);
14b1c01e
AM
6460 if (sinfo.failed)
6461 return FALSE;
5a580b3a
AM
6462
6463 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6464 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6465 else
6466 {
6467 Elf_Internal_Verneed *t;
6468 unsigned int size;
6469 unsigned int crefs;
6470 bfd_byte *p;
6471
a6cc6b3b 6472 /* Build the version dependency section. */
5a580b3a
AM
6473 size = 0;
6474 crefs = 0;
6475 for (t = elf_tdata (output_bfd)->verref;
6476 t != NULL;
6477 t = t->vn_nextref)
6478 {
6479 Elf_Internal_Vernaux *a;
6480
6481 size += sizeof (Elf_External_Verneed);
6482 ++crefs;
6483 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6484 size += sizeof (Elf_External_Vernaux);
6485 }
6486
eea6121a 6487 s->size = size;
a50b1753 6488 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6489 if (s->contents == NULL)
6490 return FALSE;
6491
6492 p = s->contents;
6493 for (t = elf_tdata (output_bfd)->verref;
6494 t != NULL;
6495 t = t->vn_nextref)
6496 {
6497 unsigned int caux;
6498 Elf_Internal_Vernaux *a;
6499 bfd_size_type indx;
6500
6501 caux = 0;
6502 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6503 ++caux;
6504
6505 t->vn_version = VER_NEED_CURRENT;
6506 t->vn_cnt = caux;
6507 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6508 elf_dt_name (t->vn_bfd) != NULL
6509 ? elf_dt_name (t->vn_bfd)
06084812 6510 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6511 FALSE);
6512 if (indx == (bfd_size_type) -1)
6513 return FALSE;
6514 t->vn_file = indx;
6515 t->vn_aux = sizeof (Elf_External_Verneed);
6516 if (t->vn_nextref == NULL)
6517 t->vn_next = 0;
6518 else
6519 t->vn_next = (sizeof (Elf_External_Verneed)
6520 + caux * sizeof (Elf_External_Vernaux));
6521
6522 _bfd_elf_swap_verneed_out (output_bfd, t,
6523 (Elf_External_Verneed *) p);
6524 p += sizeof (Elf_External_Verneed);
6525
6526 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6527 {
6528 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6529 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6530 a->vna_nodename, FALSE);
6531 if (indx == (bfd_size_type) -1)
6532 return FALSE;
6533 a->vna_name = indx;
6534 if (a->vna_nextptr == NULL)
6535 a->vna_next = 0;
6536 else
6537 a->vna_next = sizeof (Elf_External_Vernaux);
6538
6539 _bfd_elf_swap_vernaux_out (output_bfd, a,
6540 (Elf_External_Vernaux *) p);
6541 p += sizeof (Elf_External_Vernaux);
6542 }
6543 }
6544
6545 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6546 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6547 return FALSE;
6548
6549 elf_tdata (output_bfd)->cverrefs = crefs;
6550 }
6551 }
6552
8423293d
AM
6553 if ((elf_tdata (output_bfd)->cverrefs == 0
6554 && elf_tdata (output_bfd)->cverdefs == 0)
6555 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6556 &section_sym_count) == 0)
6557 {
3d4d4302 6558 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6559 s->flags |= SEC_EXCLUDE;
6560 }
6561 }
6562 return TRUE;
6563}
6564
74541ad4
AM
6565/* Find the first non-excluded output section. We'll use its
6566 section symbol for some emitted relocs. */
6567void
6568_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6569{
6570 asection *s;
6571
6572 for (s = output_bfd->sections; s != NULL; s = s->next)
6573 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6574 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6575 {
6576 elf_hash_table (info)->text_index_section = s;
6577 break;
6578 }
6579}
6580
6581/* Find two non-excluded output sections, one for code, one for data.
6582 We'll use their section symbols for some emitted relocs. */
6583void
6584_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6585{
6586 asection *s;
6587
266b05cf
DJ
6588 /* Data first, since setting text_index_section changes
6589 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6590 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6591 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6592 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6593 {
266b05cf 6594 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6595 break;
6596 }
6597
6598 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6599 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6600 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6601 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6602 {
266b05cf 6603 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6604 break;
6605 }
6606
6607 if (elf_hash_table (info)->text_index_section == NULL)
6608 elf_hash_table (info)->text_index_section
6609 = elf_hash_table (info)->data_index_section;
6610}
6611
8423293d
AM
6612bfd_boolean
6613bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6614{
74541ad4
AM
6615 const struct elf_backend_data *bed;
6616
8423293d
AM
6617 if (!is_elf_hash_table (info->hash))
6618 return TRUE;
6619
74541ad4
AM
6620 bed = get_elf_backend_data (output_bfd);
6621 (*bed->elf_backend_init_index_section) (output_bfd, info);
6622
8423293d
AM
6623 if (elf_hash_table (info)->dynamic_sections_created)
6624 {
6625 bfd *dynobj;
8423293d
AM
6626 asection *s;
6627 bfd_size_type dynsymcount;
6628 unsigned long section_sym_count;
8423293d
AM
6629 unsigned int dtagcount;
6630
6631 dynobj = elf_hash_table (info)->dynobj;
6632
5a580b3a
AM
6633 /* Assign dynsym indicies. In a shared library we generate a
6634 section symbol for each output section, which come first.
6635 Next come all of the back-end allocated local dynamic syms,
6636 followed by the rest of the global symbols. */
6637
554220db
AM
6638 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6639 &section_sym_count);
5a580b3a
AM
6640
6641 /* Work out the size of the symbol version section. */
3d4d4302 6642 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6643 BFD_ASSERT (s != NULL);
d5486c43 6644 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6645 {
eea6121a 6646 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6647 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6648 if (s->contents == NULL)
6649 return FALSE;
6650
6651 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6652 return FALSE;
6653 }
6654
6655 /* Set the size of the .dynsym and .hash sections. We counted
6656 the number of dynamic symbols in elf_link_add_object_symbols.
6657 We will build the contents of .dynsym and .hash when we build
6658 the final symbol table, because until then we do not know the
6659 correct value to give the symbols. We built the .dynstr
6660 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6661 s = elf_hash_table (info)->dynsym;
5a580b3a 6662 BFD_ASSERT (s != NULL);
eea6121a 6663 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6664
d5486c43
L
6665 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6666 if (s->contents == NULL)
6667 return FALSE;
5a580b3a 6668
d5486c43
L
6669 /* The first entry in .dynsym is a dummy symbol. Clear all the
6670 section syms, in case we don't output them all. */
6671 ++section_sym_count;
6672 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6673
fdc90cb4
JJ
6674 elf_hash_table (info)->bucketcount = 0;
6675
5a580b3a
AM
6676 /* Compute the size of the hashing table. As a side effect this
6677 computes the hash values for all the names we export. */
fdc90cb4
JJ
6678 if (info->emit_hash)
6679 {
6680 unsigned long int *hashcodes;
14b1c01e 6681 struct hash_codes_info hashinf;
fdc90cb4
JJ
6682 bfd_size_type amt;
6683 unsigned long int nsyms;
6684 size_t bucketcount;
6685 size_t hash_entry_size;
6686
6687 /* Compute the hash values for all exported symbols. At the same
6688 time store the values in an array so that we could use them for
6689 optimizations. */
6690 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6691 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6692 if (hashcodes == NULL)
6693 return FALSE;
14b1c01e
AM
6694 hashinf.hashcodes = hashcodes;
6695 hashinf.error = FALSE;
5a580b3a 6696
fdc90cb4
JJ
6697 /* Put all hash values in HASHCODES. */
6698 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6699 elf_collect_hash_codes, &hashinf);
6700 if (hashinf.error)
4dd07732
AM
6701 {
6702 free (hashcodes);
6703 return FALSE;
6704 }
5a580b3a 6705
14b1c01e 6706 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6707 bucketcount
6708 = compute_bucket_count (info, hashcodes, nsyms, 0);
6709 free (hashcodes);
6710
6711 if (bucketcount == 0)
6712 return FALSE;
5a580b3a 6713
fdc90cb4
JJ
6714 elf_hash_table (info)->bucketcount = bucketcount;
6715
3d4d4302 6716 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6717 BFD_ASSERT (s != NULL);
6718 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6719 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6720 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6721 if (s->contents == NULL)
6722 return FALSE;
6723
6724 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6725 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6726 s->contents + hash_entry_size);
6727 }
6728
6729 if (info->emit_gnu_hash)
6730 {
6731 size_t i, cnt;
6732 unsigned char *contents;
6733 struct collect_gnu_hash_codes cinfo;
6734 bfd_size_type amt;
6735 size_t bucketcount;
6736
6737 memset (&cinfo, 0, sizeof (cinfo));
6738
6739 /* Compute the hash values for all exported symbols. At the same
6740 time store the values in an array so that we could use them for
6741 optimizations. */
6742 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6743 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6744 if (cinfo.hashcodes == NULL)
6745 return FALSE;
6746
6747 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6748 cinfo.min_dynindx = -1;
6749 cinfo.output_bfd = output_bfd;
6750 cinfo.bed = bed;
6751
6752 /* Put all hash values in HASHCODES. */
6753 elf_link_hash_traverse (elf_hash_table (info),
6754 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6755 if (cinfo.error)
4dd07732
AM
6756 {
6757 free (cinfo.hashcodes);
6758 return FALSE;
6759 }
fdc90cb4
JJ
6760
6761 bucketcount
6762 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6763
6764 if (bucketcount == 0)
6765 {
6766 free (cinfo.hashcodes);
6767 return FALSE;
6768 }
6769
3d4d4302 6770 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6771 BFD_ASSERT (s != NULL);
6772
6773 if (cinfo.nsyms == 0)
6774 {
6775 /* Empty .gnu.hash section is special. */
6776 BFD_ASSERT (cinfo.min_dynindx == -1);
6777 free (cinfo.hashcodes);
6778 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6779 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6780 if (contents == NULL)
6781 return FALSE;
6782 s->contents = contents;
6783 /* 1 empty bucket. */
6784 bfd_put_32 (output_bfd, 1, contents);
6785 /* SYMIDX above the special symbol 0. */
6786 bfd_put_32 (output_bfd, 1, contents + 4);
6787 /* Just one word for bitmask. */
6788 bfd_put_32 (output_bfd, 1, contents + 8);
6789 /* Only hash fn bloom filter. */
6790 bfd_put_32 (output_bfd, 0, contents + 12);
6791 /* No hashes are valid - empty bitmask. */
6792 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6793 /* No hashes in the only bucket. */
6794 bfd_put_32 (output_bfd, 0,
6795 contents + 16 + bed->s->arch_size / 8);
6796 }
6797 else
6798 {
9e6619e2 6799 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6800 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6801
9e6619e2
AM
6802 x = cinfo.nsyms;
6803 maskbitslog2 = 1;
6804 while ((x >>= 1) != 0)
6805 ++maskbitslog2;
fdc90cb4
JJ
6806 if (maskbitslog2 < 3)
6807 maskbitslog2 = 5;
6808 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6809 maskbitslog2 = maskbitslog2 + 3;
6810 else
6811 maskbitslog2 = maskbitslog2 + 2;
6812 if (bed->s->arch_size == 64)
6813 {
6814 if (maskbitslog2 == 5)
6815 maskbitslog2 = 6;
6816 cinfo.shift1 = 6;
6817 }
6818 else
6819 cinfo.shift1 = 5;
6820 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6821 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6822 cinfo.maskbits = 1 << maskbitslog2;
6823 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6824 amt = bucketcount * sizeof (unsigned long int) * 2;
6825 amt += maskwords * sizeof (bfd_vma);
a50b1753 6826 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6827 if (cinfo.bitmask == NULL)
6828 {
6829 free (cinfo.hashcodes);
6830 return FALSE;
6831 }
6832
a50b1753 6833 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6834 cinfo.indx = cinfo.counts + bucketcount;
6835 cinfo.symindx = dynsymcount - cinfo.nsyms;
6836 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6837
6838 /* Determine how often each hash bucket is used. */
6839 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6840 for (i = 0; i < cinfo.nsyms; ++i)
6841 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6842
6843 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6844 if (cinfo.counts[i] != 0)
6845 {
6846 cinfo.indx[i] = cnt;
6847 cnt += cinfo.counts[i];
6848 }
6849 BFD_ASSERT (cnt == dynsymcount);
6850 cinfo.bucketcount = bucketcount;
6851 cinfo.local_indx = cinfo.min_dynindx;
6852
6853 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6854 s->size += cinfo.maskbits / 8;
a50b1753 6855 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6856 if (contents == NULL)
6857 {
6858 free (cinfo.bitmask);
6859 free (cinfo.hashcodes);
6860 return FALSE;
6861 }
6862
6863 s->contents = contents;
6864 bfd_put_32 (output_bfd, bucketcount, contents);
6865 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6866 bfd_put_32 (output_bfd, maskwords, contents + 8);
6867 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6868 contents += 16 + cinfo.maskbits / 8;
6869
6870 for (i = 0; i < bucketcount; ++i)
6871 {
6872 if (cinfo.counts[i] == 0)
6873 bfd_put_32 (output_bfd, 0, contents);
6874 else
6875 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6876 contents += 4;
6877 }
6878
6879 cinfo.contents = contents;
6880
6881 /* Renumber dynamic symbols, populate .gnu.hash section. */
6882 elf_link_hash_traverse (elf_hash_table (info),
6883 elf_renumber_gnu_hash_syms, &cinfo);
6884
6885 contents = s->contents + 16;
6886 for (i = 0; i < maskwords; ++i)
6887 {
6888 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6889 contents);
6890 contents += bed->s->arch_size / 8;
6891 }
6892
6893 free (cinfo.bitmask);
6894 free (cinfo.hashcodes);
6895 }
6896 }
5a580b3a 6897
3d4d4302 6898 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6899 BFD_ASSERT (s != NULL);
6900
4ad4eba5 6901 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6902
eea6121a 6903 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6904
6905 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6906 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6907 return FALSE;
6908 }
6909
6910 return TRUE;
6911}
4d269e42 6912\f
4d269e42
AM
6913/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6914
6915static void
6916merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6917 asection *sec)
6918{
dbaa2011
AM
6919 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6920 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
6921}
6922
6923/* Finish SHF_MERGE section merging. */
6924
6925bfd_boolean
630993ec 6926_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
6927{
6928 bfd *ibfd;
6929 asection *sec;
6930
6931 if (!is_elf_hash_table (info->hash))
6932 return FALSE;
6933
c72f2fb2 6934 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
6935 if ((ibfd->flags & DYNAMIC) == 0
6936 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
6937 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6938 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
6939 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6940 if ((sec->flags & SEC_MERGE) != 0
6941 && !bfd_is_abs_section (sec->output_section))
6942 {
6943 struct bfd_elf_section_data *secdata;
6944
6945 secdata = elf_section_data (sec);
630993ec 6946 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
6947 &elf_hash_table (info)->merge_info,
6948 sec, &secdata->sec_info))
6949 return FALSE;
6950 else if (secdata->sec_info)
dbaa2011 6951 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
6952 }
6953
6954 if (elf_hash_table (info)->merge_info != NULL)
630993ec 6955 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
6956 merge_sections_remove_hook);
6957 return TRUE;
6958}
6959
6960/* Create an entry in an ELF linker hash table. */
6961
6962struct bfd_hash_entry *
6963_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6964 struct bfd_hash_table *table,
6965 const char *string)
6966{
6967 /* Allocate the structure if it has not already been allocated by a
6968 subclass. */
6969 if (entry == NULL)
6970 {
a50b1753 6971 entry = (struct bfd_hash_entry *)
ca4be51c 6972 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6973 if (entry == NULL)
6974 return entry;
6975 }
6976
6977 /* Call the allocation method of the superclass. */
6978 entry = _bfd_link_hash_newfunc (entry, table, string);
6979 if (entry != NULL)
6980 {
6981 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6982 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6983
6984 /* Set local fields. */
6985 ret->indx = -1;
6986 ret->dynindx = -1;
6987 ret->got = htab->init_got_refcount;
6988 ret->plt = htab->init_plt_refcount;
6989 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6990 - offsetof (struct elf_link_hash_entry, size)));
6991 /* Assume that we have been called by a non-ELF symbol reader.
6992 This flag is then reset by the code which reads an ELF input
6993 file. This ensures that a symbol created by a non-ELF symbol
6994 reader will have the flag set correctly. */
6995 ret->non_elf = 1;
6996 }
6997
6998 return entry;
6999}
7000
7001/* Copy data from an indirect symbol to its direct symbol, hiding the
7002 old indirect symbol. Also used for copying flags to a weakdef. */
7003
7004void
7005_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7006 struct elf_link_hash_entry *dir,
7007 struct elf_link_hash_entry *ind)
7008{
7009 struct elf_link_hash_table *htab;
7010
7011 /* Copy down any references that we may have already seen to the
6e33951e
L
7012 symbol which just became indirect if DIR isn't a hidden versioned
7013 symbol. */
4d269e42 7014
422f1182 7015 if (dir->versioned != versioned_hidden)
6e33951e
L
7016 {
7017 dir->ref_dynamic |= ind->ref_dynamic;
7018 dir->ref_regular |= ind->ref_regular;
7019 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7020 dir->non_got_ref |= ind->non_got_ref;
7021 dir->needs_plt |= ind->needs_plt;
7022 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7023 }
4d269e42
AM
7024
7025 if (ind->root.type != bfd_link_hash_indirect)
7026 return;
7027
7028 /* Copy over the global and procedure linkage table refcount entries.
7029 These may have been already set up by a check_relocs routine. */
7030 htab = elf_hash_table (info);
7031 if (ind->got.refcount > htab->init_got_refcount.refcount)
7032 {
7033 if (dir->got.refcount < 0)
7034 dir->got.refcount = 0;
7035 dir->got.refcount += ind->got.refcount;
7036 ind->got.refcount = htab->init_got_refcount.refcount;
7037 }
7038
7039 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7040 {
7041 if (dir->plt.refcount < 0)
7042 dir->plt.refcount = 0;
7043 dir->plt.refcount += ind->plt.refcount;
7044 ind->plt.refcount = htab->init_plt_refcount.refcount;
7045 }
7046
7047 if (ind->dynindx != -1)
7048 {
7049 if (dir->dynindx != -1)
7050 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7051 dir->dynindx = ind->dynindx;
7052 dir->dynstr_index = ind->dynstr_index;
7053 ind->dynindx = -1;
7054 ind->dynstr_index = 0;
7055 }
7056}
7057
7058void
7059_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7060 struct elf_link_hash_entry *h,
7061 bfd_boolean force_local)
7062{
3aa14d16
L
7063 /* STT_GNU_IFUNC symbol must go through PLT. */
7064 if (h->type != STT_GNU_IFUNC)
7065 {
7066 h->plt = elf_hash_table (info)->init_plt_offset;
7067 h->needs_plt = 0;
7068 }
4d269e42
AM
7069 if (force_local)
7070 {
7071 h->forced_local = 1;
7072 if (h->dynindx != -1)
7073 {
7074 h->dynindx = -1;
7075 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7076 h->dynstr_index);
7077 }
7078 }
7079}
7080
7bf52ea2
AM
7081/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7082 caller. */
4d269e42
AM
7083
7084bfd_boolean
7085_bfd_elf_link_hash_table_init
7086 (struct elf_link_hash_table *table,
7087 bfd *abfd,
7088 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7089 struct bfd_hash_table *,
7090 const char *),
4dfe6ac6
NC
7091 unsigned int entsize,
7092 enum elf_target_id target_id)
4d269e42
AM
7093{
7094 bfd_boolean ret;
7095 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7096
4d269e42
AM
7097 table->init_got_refcount.refcount = can_refcount - 1;
7098 table->init_plt_refcount.refcount = can_refcount - 1;
7099 table->init_got_offset.offset = -(bfd_vma) 1;
7100 table->init_plt_offset.offset = -(bfd_vma) 1;
7101 /* The first dynamic symbol is a dummy. */
7102 table->dynsymcount = 1;
7103
7104 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7105
4d269e42 7106 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7107 table->hash_table_id = target_id;
4d269e42
AM
7108
7109 return ret;
7110}
7111
7112/* Create an ELF linker hash table. */
7113
7114struct bfd_link_hash_table *
7115_bfd_elf_link_hash_table_create (bfd *abfd)
7116{
7117 struct elf_link_hash_table *ret;
7118 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7119
7bf52ea2 7120 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7121 if (ret == NULL)
7122 return NULL;
7123
7124 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7125 sizeof (struct elf_link_hash_entry),
7126 GENERIC_ELF_DATA))
4d269e42
AM
7127 {
7128 free (ret);
7129 return NULL;
7130 }
d495ab0d 7131 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7132
7133 return &ret->root;
7134}
7135
9f7c3e5e
AM
7136/* Destroy an ELF linker hash table. */
7137
7138void
d495ab0d 7139_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7140{
d495ab0d
AM
7141 struct elf_link_hash_table *htab;
7142
7143 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7144 if (htab->dynstr != NULL)
7145 _bfd_elf_strtab_free (htab->dynstr);
7146 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7147 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7148}
7149
4d269e42
AM
7150/* This is a hook for the ELF emulation code in the generic linker to
7151 tell the backend linker what file name to use for the DT_NEEDED
7152 entry for a dynamic object. */
7153
7154void
7155bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7156{
7157 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7158 && bfd_get_format (abfd) == bfd_object)
7159 elf_dt_name (abfd) = name;
7160}
7161
7162int
7163bfd_elf_get_dyn_lib_class (bfd *abfd)
7164{
7165 int lib_class;
7166 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7167 && bfd_get_format (abfd) == bfd_object)
7168 lib_class = elf_dyn_lib_class (abfd);
7169 else
7170 lib_class = 0;
7171 return lib_class;
7172}
7173
7174void
7175bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7176{
7177 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7178 && bfd_get_format (abfd) == bfd_object)
7179 elf_dyn_lib_class (abfd) = lib_class;
7180}
7181
7182/* Get the list of DT_NEEDED entries for a link. This is a hook for
7183 the linker ELF emulation code. */
7184
7185struct bfd_link_needed_list *
7186bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7187 struct bfd_link_info *info)
7188{
7189 if (! is_elf_hash_table (info->hash))
7190 return NULL;
7191 return elf_hash_table (info)->needed;
7192}
7193
7194/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7195 hook for the linker ELF emulation code. */
7196
7197struct bfd_link_needed_list *
7198bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7199 struct bfd_link_info *info)
7200{
7201 if (! is_elf_hash_table (info->hash))
7202 return NULL;
7203 return elf_hash_table (info)->runpath;
7204}
7205
7206/* Get the name actually used for a dynamic object for a link. This
7207 is the SONAME entry if there is one. Otherwise, it is the string
7208 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7209
7210const char *
7211bfd_elf_get_dt_soname (bfd *abfd)
7212{
7213 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7214 && bfd_get_format (abfd) == bfd_object)
7215 return elf_dt_name (abfd);
7216 return NULL;
7217}
7218
7219/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7220 the ELF linker emulation code. */
7221
7222bfd_boolean
7223bfd_elf_get_bfd_needed_list (bfd *abfd,
7224 struct bfd_link_needed_list **pneeded)
7225{
7226 asection *s;
7227 bfd_byte *dynbuf = NULL;
cb33740c 7228 unsigned int elfsec;
4d269e42
AM
7229 unsigned long shlink;
7230 bfd_byte *extdyn, *extdynend;
7231 size_t extdynsize;
7232 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7233
7234 *pneeded = NULL;
7235
7236 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7237 || bfd_get_format (abfd) != bfd_object)
7238 return TRUE;
7239
7240 s = bfd_get_section_by_name (abfd, ".dynamic");
7241 if (s == NULL || s->size == 0)
7242 return TRUE;
7243
7244 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7245 goto error_return;
7246
7247 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7248 if (elfsec == SHN_BAD)
4d269e42
AM
7249 goto error_return;
7250
7251 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7252
4d269e42
AM
7253 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7254 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7255
7256 extdyn = dynbuf;
7257 extdynend = extdyn + s->size;
7258 for (; extdyn < extdynend; extdyn += extdynsize)
7259 {
7260 Elf_Internal_Dyn dyn;
7261
7262 (*swap_dyn_in) (abfd, extdyn, &dyn);
7263
7264 if (dyn.d_tag == DT_NULL)
7265 break;
7266
7267 if (dyn.d_tag == DT_NEEDED)
7268 {
7269 const char *string;
7270 struct bfd_link_needed_list *l;
7271 unsigned int tagv = dyn.d_un.d_val;
7272 bfd_size_type amt;
7273
7274 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7275 if (string == NULL)
7276 goto error_return;
7277
7278 amt = sizeof *l;
a50b1753 7279 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7280 if (l == NULL)
7281 goto error_return;
7282
7283 l->by = abfd;
7284 l->name = string;
7285 l->next = *pneeded;
7286 *pneeded = l;
7287 }
7288 }
7289
7290 free (dynbuf);
7291
7292 return TRUE;
7293
7294 error_return:
7295 if (dynbuf != NULL)
7296 free (dynbuf);
7297 return FALSE;
7298}
7299
7300struct elf_symbuf_symbol
7301{
7302 unsigned long st_name; /* Symbol name, index in string tbl */
7303 unsigned char st_info; /* Type and binding attributes */
7304 unsigned char st_other; /* Visibilty, and target specific */
7305};
7306
7307struct elf_symbuf_head
7308{
7309 struct elf_symbuf_symbol *ssym;
7310 bfd_size_type count;
7311 unsigned int st_shndx;
7312};
7313
7314struct elf_symbol
7315{
7316 union
7317 {
7318 Elf_Internal_Sym *isym;
7319 struct elf_symbuf_symbol *ssym;
7320 } u;
7321 const char *name;
7322};
7323
7324/* Sort references to symbols by ascending section number. */
7325
7326static int
7327elf_sort_elf_symbol (const void *arg1, const void *arg2)
7328{
7329 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7330 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7331
7332 return s1->st_shndx - s2->st_shndx;
7333}
7334
7335static int
7336elf_sym_name_compare (const void *arg1, const void *arg2)
7337{
7338 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7339 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7340 return strcmp (s1->name, s2->name);
7341}
7342
7343static struct elf_symbuf_head *
7344elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7345{
14b1c01e 7346 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7347 struct elf_symbuf_symbol *ssym;
7348 struct elf_symbuf_head *ssymbuf, *ssymhead;
3ae181ee 7349 bfd_size_type i, shndx_count, total_size;
4d269e42 7350
a50b1753 7351 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7352 if (indbuf == NULL)
7353 return NULL;
7354
7355 for (ind = indbuf, i = 0; i < symcount; i++)
7356 if (isymbuf[i].st_shndx != SHN_UNDEF)
7357 *ind++ = &isymbuf[i];
7358 indbufend = ind;
7359
7360 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7361 elf_sort_elf_symbol);
7362
7363 shndx_count = 0;
7364 if (indbufend > indbuf)
7365 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7366 if (ind[0]->st_shndx != ind[1]->st_shndx)
7367 shndx_count++;
7368
3ae181ee
L
7369 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7370 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7371 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7372 if (ssymbuf == NULL)
7373 {
7374 free (indbuf);
7375 return NULL;
7376 }
7377
3ae181ee 7378 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7379 ssymbuf->ssym = NULL;
7380 ssymbuf->count = shndx_count;
7381 ssymbuf->st_shndx = 0;
7382 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7383 {
7384 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7385 {
7386 ssymhead++;
7387 ssymhead->ssym = ssym;
7388 ssymhead->count = 0;
7389 ssymhead->st_shndx = (*ind)->st_shndx;
7390 }
7391 ssym->st_name = (*ind)->st_name;
7392 ssym->st_info = (*ind)->st_info;
7393 ssym->st_other = (*ind)->st_other;
7394 ssymhead->count++;
7395 }
3ae181ee
L
7396 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7397 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7398 == total_size));
4d269e42
AM
7399
7400 free (indbuf);
7401 return ssymbuf;
7402}
7403
7404/* Check if 2 sections define the same set of local and global
7405 symbols. */
7406
8f317e31 7407static bfd_boolean
4d269e42
AM
7408bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7409 struct bfd_link_info *info)
7410{
7411 bfd *bfd1, *bfd2;
7412 const struct elf_backend_data *bed1, *bed2;
7413 Elf_Internal_Shdr *hdr1, *hdr2;
7414 bfd_size_type symcount1, symcount2;
7415 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7416 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7417 Elf_Internal_Sym *isym, *isymend;
7418 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7419 bfd_size_type count1, count2, i;
cb33740c 7420 unsigned int shndx1, shndx2;
4d269e42
AM
7421 bfd_boolean result;
7422
7423 bfd1 = sec1->owner;
7424 bfd2 = sec2->owner;
7425
4d269e42
AM
7426 /* Both sections have to be in ELF. */
7427 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7428 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7429 return FALSE;
7430
7431 if (elf_section_type (sec1) != elf_section_type (sec2))
7432 return FALSE;
7433
4d269e42
AM
7434 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7435 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7436 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7437 return FALSE;
7438
7439 bed1 = get_elf_backend_data (bfd1);
7440 bed2 = get_elf_backend_data (bfd2);
7441 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7442 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7443 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7444 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7445
7446 if (symcount1 == 0 || symcount2 == 0)
7447 return FALSE;
7448
7449 result = FALSE;
7450 isymbuf1 = NULL;
7451 isymbuf2 = NULL;
a50b1753
NC
7452 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7453 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7454
7455 if (ssymbuf1 == NULL)
7456 {
7457 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7458 NULL, NULL, NULL);
7459 if (isymbuf1 == NULL)
7460 goto done;
7461
7462 if (!info->reduce_memory_overheads)
7463 elf_tdata (bfd1)->symbuf = ssymbuf1
7464 = elf_create_symbuf (symcount1, isymbuf1);
7465 }
7466
7467 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7468 {
7469 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7470 NULL, NULL, NULL);
7471 if (isymbuf2 == NULL)
7472 goto done;
7473
7474 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7475 elf_tdata (bfd2)->symbuf = ssymbuf2
7476 = elf_create_symbuf (symcount2, isymbuf2);
7477 }
7478
7479 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7480 {
7481 /* Optimized faster version. */
7482 bfd_size_type lo, hi, mid;
7483 struct elf_symbol *symp;
7484 struct elf_symbuf_symbol *ssym, *ssymend;
7485
7486 lo = 0;
7487 hi = ssymbuf1->count;
7488 ssymbuf1++;
7489 count1 = 0;
7490 while (lo < hi)
7491 {
7492 mid = (lo + hi) / 2;
cb33740c 7493 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7494 hi = mid;
cb33740c 7495 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7496 lo = mid + 1;
7497 else
7498 {
7499 count1 = ssymbuf1[mid].count;
7500 ssymbuf1 += mid;
7501 break;
7502 }
7503 }
7504
7505 lo = 0;
7506 hi = ssymbuf2->count;
7507 ssymbuf2++;
7508 count2 = 0;
7509 while (lo < hi)
7510 {
7511 mid = (lo + hi) / 2;
cb33740c 7512 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7513 hi = mid;
cb33740c 7514 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7515 lo = mid + 1;
7516 else
7517 {
7518 count2 = ssymbuf2[mid].count;
7519 ssymbuf2 += mid;
7520 break;
7521 }
7522 }
7523
7524 if (count1 == 0 || count2 == 0 || count1 != count2)
7525 goto done;
7526
ca4be51c
AM
7527 symtable1
7528 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7529 symtable2
7530 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7531 if (symtable1 == NULL || symtable2 == NULL)
7532 goto done;
7533
7534 symp = symtable1;
7535 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7536 ssym < ssymend; ssym++, symp++)
7537 {
7538 symp->u.ssym = ssym;
7539 symp->name = bfd_elf_string_from_elf_section (bfd1,
7540 hdr1->sh_link,
7541 ssym->st_name);
7542 }
7543
7544 symp = symtable2;
7545 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7546 ssym < ssymend; ssym++, symp++)
7547 {
7548 symp->u.ssym = ssym;
7549 symp->name = bfd_elf_string_from_elf_section (bfd2,
7550 hdr2->sh_link,
7551 ssym->st_name);
7552 }
7553
7554 /* Sort symbol by name. */
7555 qsort (symtable1, count1, sizeof (struct elf_symbol),
7556 elf_sym_name_compare);
7557 qsort (symtable2, count1, sizeof (struct elf_symbol),
7558 elf_sym_name_compare);
7559
7560 for (i = 0; i < count1; i++)
7561 /* Two symbols must have the same binding, type and name. */
7562 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7563 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7564 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7565 goto done;
7566
7567 result = TRUE;
7568 goto done;
7569 }
7570
a50b1753
NC
7571 symtable1 = (struct elf_symbol *)
7572 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7573 symtable2 = (struct elf_symbol *)
7574 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7575 if (symtable1 == NULL || symtable2 == NULL)
7576 goto done;
7577
7578 /* Count definitions in the section. */
7579 count1 = 0;
7580 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7581 if (isym->st_shndx == shndx1)
4d269e42
AM
7582 symtable1[count1++].u.isym = isym;
7583
7584 count2 = 0;
7585 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7586 if (isym->st_shndx == shndx2)
4d269e42
AM
7587 symtable2[count2++].u.isym = isym;
7588
7589 if (count1 == 0 || count2 == 0 || count1 != count2)
7590 goto done;
7591
7592 for (i = 0; i < count1; i++)
7593 symtable1[i].name
7594 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7595 symtable1[i].u.isym->st_name);
7596
7597 for (i = 0; i < count2; i++)
7598 symtable2[i].name
7599 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7600 symtable2[i].u.isym->st_name);
7601
7602 /* Sort symbol by name. */
7603 qsort (symtable1, count1, sizeof (struct elf_symbol),
7604 elf_sym_name_compare);
7605 qsort (symtable2, count1, sizeof (struct elf_symbol),
7606 elf_sym_name_compare);
7607
7608 for (i = 0; i < count1; i++)
7609 /* Two symbols must have the same binding, type and name. */
7610 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7611 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7612 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7613 goto done;
7614
7615 result = TRUE;
7616
7617done:
7618 if (symtable1)
7619 free (symtable1);
7620 if (symtable2)
7621 free (symtable2);
7622 if (isymbuf1)
7623 free (isymbuf1);
7624 if (isymbuf2)
7625 free (isymbuf2);
7626
7627 return result;
7628}
7629
7630/* Return TRUE if 2 section types are compatible. */
7631
7632bfd_boolean
7633_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7634 bfd *bbfd, const asection *bsec)
7635{
7636 if (asec == NULL
7637 || bsec == NULL
7638 || abfd->xvec->flavour != bfd_target_elf_flavour
7639 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7640 return TRUE;
7641
7642 return elf_section_type (asec) == elf_section_type (bsec);
7643}
7644\f
c152c796
AM
7645/* Final phase of ELF linker. */
7646
7647/* A structure we use to avoid passing large numbers of arguments. */
7648
7649struct elf_final_link_info
7650{
7651 /* General link information. */
7652 struct bfd_link_info *info;
7653 /* Output BFD. */
7654 bfd *output_bfd;
7655 /* Symbol string table. */
ef10c3ac 7656 struct elf_strtab_hash *symstrtab;
c152c796
AM
7657 /* .hash section. */
7658 asection *hash_sec;
7659 /* symbol version section (.gnu.version). */
7660 asection *symver_sec;
7661 /* Buffer large enough to hold contents of any section. */
7662 bfd_byte *contents;
7663 /* Buffer large enough to hold external relocs of any section. */
7664 void *external_relocs;
7665 /* Buffer large enough to hold internal relocs of any section. */
7666 Elf_Internal_Rela *internal_relocs;
7667 /* Buffer large enough to hold external local symbols of any input
7668 BFD. */
7669 bfd_byte *external_syms;
7670 /* And a buffer for symbol section indices. */
7671 Elf_External_Sym_Shndx *locsym_shndx;
7672 /* Buffer large enough to hold internal local symbols of any input
7673 BFD. */
7674 Elf_Internal_Sym *internal_syms;
7675 /* Array large enough to hold a symbol index for each local symbol
7676 of any input BFD. */
7677 long *indices;
7678 /* Array large enough to hold a section pointer for each local
7679 symbol of any input BFD. */
7680 asection **sections;
ef10c3ac 7681 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7682 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7683 /* Number of STT_FILE syms seen. */
7684 size_t filesym_count;
c152c796
AM
7685};
7686
7687/* This struct is used to pass information to elf_link_output_extsym. */
7688
7689struct elf_outext_info
7690{
7691 bfd_boolean failed;
7692 bfd_boolean localsyms;
34a79995 7693 bfd_boolean file_sym_done;
8b127cbc 7694 struct elf_final_link_info *flinfo;
c152c796
AM
7695};
7696
d9352518
DB
7697
7698/* Support for evaluating a complex relocation.
7699
7700 Complex relocations are generalized, self-describing relocations. The
7701 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7702 relocations themselves.
d9352518
DB
7703
7704 The relocations are use a reserved elf-wide relocation type code (R_RELC
7705 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7706 information (start bit, end bit, word width, etc) into the addend. This
7707 information is extracted from CGEN-generated operand tables within gas.
7708
7709 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7710 internal) representing prefix-notation expressions, including but not
7711 limited to those sorts of expressions normally encoded as addends in the
7712 addend field. The symbol mangling format is:
7713
7714 <node> := <literal>
7715 | <unary-operator> ':' <node>
7716 | <binary-operator> ':' <node> ':' <node>
7717 ;
7718
7719 <literal> := 's' <digits=N> ':' <N character symbol name>
7720 | 'S' <digits=N> ':' <N character section name>
7721 | '#' <hexdigits>
7722 ;
7723
7724 <binary-operator> := as in C
7725 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7726
7727static void
a0c8462f
AM
7728set_symbol_value (bfd *bfd_with_globals,
7729 Elf_Internal_Sym *isymbuf,
7730 size_t locsymcount,
7731 size_t symidx,
7732 bfd_vma val)
d9352518 7733{
8977835c
AM
7734 struct elf_link_hash_entry **sym_hashes;
7735 struct elf_link_hash_entry *h;
7736 size_t extsymoff = locsymcount;
d9352518 7737
8977835c 7738 if (symidx < locsymcount)
d9352518 7739 {
8977835c
AM
7740 Elf_Internal_Sym *sym;
7741
7742 sym = isymbuf + symidx;
7743 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7744 {
7745 /* It is a local symbol: move it to the
7746 "absolute" section and give it a value. */
7747 sym->st_shndx = SHN_ABS;
7748 sym->st_value = val;
7749 return;
7750 }
7751 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7752 extsymoff = 0;
d9352518 7753 }
8977835c
AM
7754
7755 /* It is a global symbol: set its link type
7756 to "defined" and give it a value. */
7757
7758 sym_hashes = elf_sym_hashes (bfd_with_globals);
7759 h = sym_hashes [symidx - extsymoff];
7760 while (h->root.type == bfd_link_hash_indirect
7761 || h->root.type == bfd_link_hash_warning)
7762 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7763 h->root.type = bfd_link_hash_defined;
7764 h->root.u.def.value = val;
7765 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7766}
7767
a0c8462f
AM
7768static bfd_boolean
7769resolve_symbol (const char *name,
7770 bfd *input_bfd,
8b127cbc 7771 struct elf_final_link_info *flinfo,
a0c8462f
AM
7772 bfd_vma *result,
7773 Elf_Internal_Sym *isymbuf,
7774 size_t locsymcount)
d9352518 7775{
a0c8462f
AM
7776 Elf_Internal_Sym *sym;
7777 struct bfd_link_hash_entry *global_entry;
7778 const char *candidate = NULL;
7779 Elf_Internal_Shdr *symtab_hdr;
7780 size_t i;
7781
d9352518
DB
7782 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7783
7784 for (i = 0; i < locsymcount; ++ i)
7785 {
8977835c 7786 sym = isymbuf + i;
d9352518
DB
7787
7788 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7789 continue;
7790
7791 candidate = bfd_elf_string_from_elf_section (input_bfd,
7792 symtab_hdr->sh_link,
7793 sym->st_name);
7794#ifdef DEBUG
0f02bbd9
AM
7795 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7796 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7797#endif
7798 if (candidate && strcmp (candidate, name) == 0)
7799 {
8b127cbc 7800 asection *sec = flinfo->sections [i];
d9352518 7801
0f02bbd9
AM
7802 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7803 *result += sec->output_offset + sec->output_section->vma;
d9352518 7804#ifdef DEBUG
0f02bbd9
AM
7805 printf ("Found symbol with value %8.8lx\n",
7806 (unsigned long) *result);
d9352518
DB
7807#endif
7808 return TRUE;
7809 }
7810 }
7811
7812 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7813 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7814 FALSE, FALSE, TRUE);
d9352518
DB
7815 if (!global_entry)
7816 return FALSE;
a0c8462f 7817
d9352518
DB
7818 if (global_entry->type == bfd_link_hash_defined
7819 || global_entry->type == bfd_link_hash_defweak)
7820 {
a0c8462f
AM
7821 *result = (global_entry->u.def.value
7822 + global_entry->u.def.section->output_section->vma
7823 + global_entry->u.def.section->output_offset);
d9352518 7824#ifdef DEBUG
0f02bbd9
AM
7825 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7826 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7827#endif
7828 return TRUE;
a0c8462f 7829 }
d9352518 7830
d9352518
DB
7831 return FALSE;
7832}
7833
37b01f6a
DG
7834/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7835 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7836 names like "foo.end" which is the end address of section "foo". */
7837
d9352518 7838static bfd_boolean
a0c8462f
AM
7839resolve_section (const char *name,
7840 asection *sections,
37b01f6a
DG
7841 bfd_vma *result,
7842 bfd * abfd)
d9352518 7843{
a0c8462f
AM
7844 asection *curr;
7845 unsigned int len;
d9352518 7846
a0c8462f 7847 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7848 if (strcmp (curr->name, name) == 0)
7849 {
7850 *result = curr->vma;
7851 return TRUE;
7852 }
7853
7854 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 7855 /* FIXME: This could be coded more efficiently... */
a0c8462f 7856 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7857 {
7858 len = strlen (curr->name);
a0c8462f 7859 if (len > strlen (name))
d9352518
DB
7860 continue;
7861
7862 if (strncmp (curr->name, name, len) == 0)
7863 {
7864 if (strncmp (".end", name + len, 4) == 0)
7865 {
37b01f6a 7866 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
7867 return TRUE;
7868 }
7869
7870 /* Insert more pseudo-section names here, if you like. */
7871 }
7872 }
a0c8462f 7873
d9352518
DB
7874 return FALSE;
7875}
7876
7877static void
a0c8462f 7878undefined_reference (const char *reftype, const char *name)
d9352518 7879{
a0c8462f
AM
7880 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7881 reftype, name);
d9352518
DB
7882}
7883
7884static bfd_boolean
a0c8462f
AM
7885eval_symbol (bfd_vma *result,
7886 const char **symp,
7887 bfd *input_bfd,
8b127cbc 7888 struct elf_final_link_info *flinfo,
a0c8462f
AM
7889 bfd_vma dot,
7890 Elf_Internal_Sym *isymbuf,
7891 size_t locsymcount,
7892 int signed_p)
d9352518 7893{
4b93929b
NC
7894 size_t len;
7895 size_t symlen;
a0c8462f
AM
7896 bfd_vma a;
7897 bfd_vma b;
4b93929b 7898 char symbuf[4096];
0f02bbd9 7899 const char *sym = *symp;
a0c8462f
AM
7900 const char *symend;
7901 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7902
7903 len = strlen (sym);
7904 symend = sym + len;
7905
4b93929b 7906 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7907 {
7908 bfd_set_error (bfd_error_invalid_operation);
7909 return FALSE;
7910 }
a0c8462f 7911
d9352518
DB
7912 switch (* sym)
7913 {
7914 case '.':
0f02bbd9
AM
7915 *result = dot;
7916 *symp = sym + 1;
d9352518
DB
7917 return TRUE;
7918
7919 case '#':
0f02bbd9
AM
7920 ++sym;
7921 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7922 return TRUE;
7923
7924 case 'S':
7925 symbol_is_section = TRUE;
a0c8462f 7926 case 's':
0f02bbd9
AM
7927 ++sym;
7928 symlen = strtol (sym, (char **) symp, 10);
7929 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7930
4b93929b 7931 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7932 {
7933 bfd_set_error (bfd_error_invalid_operation);
7934 return FALSE;
7935 }
7936
7937 memcpy (symbuf, sym, symlen);
a0c8462f 7938 symbuf[symlen] = '\0';
0f02bbd9 7939 *symp = sym + symlen;
a0c8462f
AM
7940
7941 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7942 the symbol as a section, or vice-versa. so we're pretty liberal in our
7943 interpretation here; section means "try section first", not "must be a
7944 section", and likewise with symbol. */
7945
a0c8462f 7946 if (symbol_is_section)
d9352518 7947 {
37b01f6a 7948 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 7949 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7950 isymbuf, locsymcount))
d9352518
DB
7951 {
7952 undefined_reference ("section", symbuf);
7953 return FALSE;
7954 }
a0c8462f
AM
7955 }
7956 else
d9352518 7957 {
8b127cbc 7958 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7959 isymbuf, locsymcount)
8b127cbc 7960 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 7961 result, input_bfd))
d9352518
DB
7962 {
7963 undefined_reference ("symbol", symbuf);
7964 return FALSE;
7965 }
7966 }
7967
7968 return TRUE;
a0c8462f 7969
d9352518
DB
7970 /* All that remains are operators. */
7971
7972#define UNARY_OP(op) \
7973 if (strncmp (sym, #op, strlen (#op)) == 0) \
7974 { \
7975 sym += strlen (#op); \
a0c8462f
AM
7976 if (*sym == ':') \
7977 ++sym; \
0f02bbd9 7978 *symp = sym; \
8b127cbc 7979 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7980 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7981 return FALSE; \
7982 if (signed_p) \
0f02bbd9 7983 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7984 else \
7985 *result = op a; \
d9352518
DB
7986 return TRUE; \
7987 }
7988
7989#define BINARY_OP(op) \
7990 if (strncmp (sym, #op, strlen (#op)) == 0) \
7991 { \
7992 sym += strlen (#op); \
a0c8462f
AM
7993 if (*sym == ':') \
7994 ++sym; \
0f02bbd9 7995 *symp = sym; \
8b127cbc 7996 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7997 isymbuf, locsymcount, signed_p)) \
a0c8462f 7998 return FALSE; \
0f02bbd9 7999 ++*symp; \
8b127cbc 8000 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8001 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8002 return FALSE; \
8003 if (signed_p) \
0f02bbd9 8004 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8005 else \
8006 *result = a op b; \
d9352518
DB
8007 return TRUE; \
8008 }
8009
8010 default:
8011 UNARY_OP (0-);
8012 BINARY_OP (<<);
8013 BINARY_OP (>>);
8014 BINARY_OP (==);
8015 BINARY_OP (!=);
8016 BINARY_OP (<=);
8017 BINARY_OP (>=);
8018 BINARY_OP (&&);
8019 BINARY_OP (||);
8020 UNARY_OP (~);
8021 UNARY_OP (!);
8022 BINARY_OP (*);
8023 BINARY_OP (/);
8024 BINARY_OP (%);
8025 BINARY_OP (^);
8026 BINARY_OP (|);
8027 BINARY_OP (&);
8028 BINARY_OP (+);
8029 BINARY_OP (-);
8030 BINARY_OP (<);
8031 BINARY_OP (>);
8032#undef UNARY_OP
8033#undef BINARY_OP
8034 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8035 bfd_set_error (bfd_error_invalid_operation);
8036 return FALSE;
8037 }
8038}
8039
d9352518 8040static void
a0c8462f
AM
8041put_value (bfd_vma size,
8042 unsigned long chunksz,
8043 bfd *input_bfd,
8044 bfd_vma x,
8045 bfd_byte *location)
d9352518
DB
8046{
8047 location += (size - chunksz);
8048
41cd1ad1 8049 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8050 {
8051 switch (chunksz)
8052 {
d9352518
DB
8053 case 1:
8054 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8055 x >>= 8;
d9352518
DB
8056 break;
8057 case 2:
8058 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8059 x >>= 16;
d9352518
DB
8060 break;
8061 case 4:
8062 bfd_put_32 (input_bfd, x, location);
65164438
NC
8063 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8064 x >>= 16;
8065 x >>= 16;
d9352518 8066 break;
d9352518 8067#ifdef BFD64
41cd1ad1 8068 case 8:
d9352518 8069 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8070 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8071 x >>= 32;
8072 x >>= 32;
8073 break;
d9352518 8074#endif
41cd1ad1
NC
8075 default:
8076 abort ();
d9352518
DB
8077 break;
8078 }
8079 }
8080}
8081
a0c8462f
AM
8082static bfd_vma
8083get_value (bfd_vma size,
8084 unsigned long chunksz,
8085 bfd *input_bfd,
8086 bfd_byte *location)
d9352518 8087{
9b239e0e 8088 int shift;
d9352518
DB
8089 bfd_vma x = 0;
8090
9b239e0e
NC
8091 /* Sanity checks. */
8092 BFD_ASSERT (chunksz <= sizeof (x)
8093 && size >= chunksz
8094 && chunksz != 0
8095 && (size % chunksz) == 0
8096 && input_bfd != NULL
8097 && location != NULL);
8098
8099 if (chunksz == sizeof (x))
8100 {
8101 BFD_ASSERT (size == chunksz);
8102
8103 /* Make sure that we do not perform an undefined shift operation.
8104 We know that size == chunksz so there will only be one iteration
8105 of the loop below. */
8106 shift = 0;
8107 }
8108 else
8109 shift = 8 * chunksz;
8110
a0c8462f 8111 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8112 {
8113 switch (chunksz)
8114 {
d9352518 8115 case 1:
9b239e0e 8116 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8117 break;
8118 case 2:
9b239e0e 8119 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8120 break;
8121 case 4:
9b239e0e 8122 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8123 break;
d9352518 8124#ifdef BFD64
9b239e0e
NC
8125 case 8:
8126 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8127 break;
9b239e0e
NC
8128#endif
8129 default:
8130 abort ();
d9352518
DB
8131 }
8132 }
8133 return x;
8134}
8135
a0c8462f
AM
8136static void
8137decode_complex_addend (unsigned long *start, /* in bits */
8138 unsigned long *oplen, /* in bits */
8139 unsigned long *len, /* in bits */
8140 unsigned long *wordsz, /* in bytes */
8141 unsigned long *chunksz, /* in bytes */
8142 unsigned long *lsb0_p,
8143 unsigned long *signed_p,
8144 unsigned long *trunc_p,
8145 unsigned long encoded)
d9352518
DB
8146{
8147 * start = encoded & 0x3F;
8148 * len = (encoded >> 6) & 0x3F;
8149 * oplen = (encoded >> 12) & 0x3F;
8150 * wordsz = (encoded >> 18) & 0xF;
8151 * chunksz = (encoded >> 22) & 0xF;
8152 * lsb0_p = (encoded >> 27) & 1;
8153 * signed_p = (encoded >> 28) & 1;
8154 * trunc_p = (encoded >> 29) & 1;
8155}
8156
cdfeee4f 8157bfd_reloc_status_type
0f02bbd9 8158bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8159 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8160 bfd_byte *contents,
8161 Elf_Internal_Rela *rel,
8162 bfd_vma relocation)
d9352518 8163{
0f02bbd9
AM
8164 bfd_vma shift, x, mask;
8165 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8166 bfd_reloc_status_type r;
d9352518
DB
8167
8168 /* Perform this reloc, since it is complex.
8169 (this is not to say that it necessarily refers to a complex
8170 symbol; merely that it is a self-describing CGEN based reloc.
8171 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8172 word size, etc) encoded within it.). */
d9352518 8173
a0c8462f
AM
8174 decode_complex_addend (&start, &oplen, &len, &wordsz,
8175 &chunksz, &lsb0_p, &signed_p,
8176 &trunc_p, rel->r_addend);
d9352518
DB
8177
8178 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8179
8180 if (lsb0_p)
8181 shift = (start + 1) - len;
8182 else
8183 shift = (8 * wordsz) - (start + len);
8184
37b01f6a
DG
8185 x = get_value (wordsz, chunksz, input_bfd,
8186 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8187
8188#ifdef DEBUG
8189 printf ("Doing complex reloc: "
8190 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8191 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8192 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8193 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8194 oplen, (unsigned long) x, (unsigned long) mask,
8195 (unsigned long) relocation);
d9352518
DB
8196#endif
8197
cdfeee4f 8198 r = bfd_reloc_ok;
d9352518 8199 if (! trunc_p)
cdfeee4f
AM
8200 /* Now do an overflow check. */
8201 r = bfd_check_overflow ((signed_p
8202 ? complain_overflow_signed
8203 : complain_overflow_unsigned),
8204 len, 0, (8 * wordsz),
8205 relocation);
a0c8462f 8206
d9352518
DB
8207 /* Do the deed. */
8208 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8209
8210#ifdef DEBUG
8211 printf (" relocation: %8.8lx\n"
8212 " shifted mask: %8.8lx\n"
8213 " shifted/masked reloc: %8.8lx\n"
8214 " result: %8.8lx\n",
9ccb8af9
AM
8215 (unsigned long) relocation, (unsigned long) (mask << shift),
8216 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8217#endif
37b01f6a
DG
8218 put_value (wordsz, chunksz, input_bfd, x,
8219 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8220 return r;
d9352518
DB
8221}
8222
0e287786
AM
8223/* Functions to read r_offset from external (target order) reloc
8224 entry. Faster than bfd_getl32 et al, because we let the compiler
8225 know the value is aligned. */
53df40a4 8226
0e287786
AM
8227static bfd_vma
8228ext32l_r_offset (const void *p)
53df40a4
AM
8229{
8230 union aligned32
8231 {
8232 uint32_t v;
8233 unsigned char c[4];
8234 };
8235 const union aligned32 *a
0e287786 8236 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8237
8238 uint32_t aval = ( (uint32_t) a->c[0]
8239 | (uint32_t) a->c[1] << 8
8240 | (uint32_t) a->c[2] << 16
8241 | (uint32_t) a->c[3] << 24);
0e287786 8242 return aval;
53df40a4
AM
8243}
8244
0e287786
AM
8245static bfd_vma
8246ext32b_r_offset (const void *p)
53df40a4
AM
8247{
8248 union aligned32
8249 {
8250 uint32_t v;
8251 unsigned char c[4];
8252 };
8253 const union aligned32 *a
0e287786 8254 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8255
8256 uint32_t aval = ( (uint32_t) a->c[0] << 24
8257 | (uint32_t) a->c[1] << 16
8258 | (uint32_t) a->c[2] << 8
8259 | (uint32_t) a->c[3]);
0e287786 8260 return aval;
53df40a4
AM
8261}
8262
8263#ifdef BFD_HOST_64_BIT
0e287786
AM
8264static bfd_vma
8265ext64l_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]
8276 | (uint64_t) a->c[1] << 8
8277 | (uint64_t) a->c[2] << 16
8278 | (uint64_t) a->c[3] << 24
8279 | (uint64_t) a->c[4] << 32
8280 | (uint64_t) a->c[5] << 40
8281 | (uint64_t) a->c[6] << 48
8282 | (uint64_t) a->c[7] << 56);
0e287786 8283 return aval;
53df40a4
AM
8284}
8285
0e287786
AM
8286static bfd_vma
8287ext64b_r_offset (const void *p)
53df40a4
AM
8288{
8289 union aligned64
8290 {
8291 uint64_t v;
8292 unsigned char c[8];
8293 };
8294 const union aligned64 *a
0e287786 8295 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8296
8297 uint64_t aval = ( (uint64_t) a->c[0] << 56
8298 | (uint64_t) a->c[1] << 48
8299 | (uint64_t) a->c[2] << 40
8300 | (uint64_t) a->c[3] << 32
8301 | (uint64_t) a->c[4] << 24
8302 | (uint64_t) a->c[5] << 16
8303 | (uint64_t) a->c[6] << 8
8304 | (uint64_t) a->c[7]);
0e287786 8305 return aval;
53df40a4
AM
8306}
8307#endif
8308
c152c796
AM
8309/* When performing a relocatable link, the input relocations are
8310 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8311 referenced must be updated. Update all the relocations found in
8312 RELDATA. */
c152c796 8313
bca6d0e3 8314static bfd_boolean
c152c796 8315elf_link_adjust_relocs (bfd *abfd,
28dbcedc
AM
8316 struct bfd_elf_section_reloc_data *reldata,
8317 bfd_boolean sort)
c152c796
AM
8318{
8319 unsigned int i;
8320 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8321 bfd_byte *erela;
8322 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8323 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8324 bfd_vma r_type_mask;
8325 int r_sym_shift;
d4730f92
BS
8326 unsigned int count = reldata->count;
8327 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8328
d4730f92 8329 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8330 {
8331 swap_in = bed->s->swap_reloc_in;
8332 swap_out = bed->s->swap_reloc_out;
8333 }
d4730f92 8334 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8335 {
8336 swap_in = bed->s->swap_reloca_in;
8337 swap_out = bed->s->swap_reloca_out;
8338 }
8339 else
8340 abort ();
8341
8342 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8343 abort ();
8344
8345 if (bed->s->arch_size == 32)
8346 {
8347 r_type_mask = 0xff;
8348 r_sym_shift = 8;
8349 }
8350 else
8351 {
8352 r_type_mask = 0xffffffff;
8353 r_sym_shift = 32;
8354 }
8355
d4730f92
BS
8356 erela = reldata->hdr->contents;
8357 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8358 {
8359 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8360 unsigned int j;
8361
8362 if (*rel_hash == NULL)
8363 continue;
8364
8365 BFD_ASSERT ((*rel_hash)->indx >= 0);
8366
8367 (*swap_in) (abfd, erela, irela);
8368 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8369 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8370 | (irela[j].r_info & r_type_mask));
8371 (*swap_out) (abfd, irela, erela);
8372 }
53df40a4 8373
0e287786 8374 if (sort && count != 0)
53df40a4 8375 {
0e287786
AM
8376 bfd_vma (*ext_r_off) (const void *);
8377 bfd_vma r_off;
8378 size_t elt_size;
8379 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8380 bfd_byte *buf = NULL;
28dbcedc
AM
8381
8382 if (bed->s->arch_size == 32)
8383 {
8384 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8385 ext_r_off = ext32l_r_offset;
28dbcedc 8386 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8387 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8388 else
8389 abort ();
8390 }
53df40a4 8391 else
28dbcedc 8392 {
53df40a4 8393#ifdef BFD_HOST_64_BIT
28dbcedc 8394 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8395 ext_r_off = ext64l_r_offset;
28dbcedc 8396 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8397 ext_r_off = ext64b_r_offset;
28dbcedc 8398 else
53df40a4 8399#endif
28dbcedc
AM
8400 abort ();
8401 }
0e287786 8402
bca6d0e3
AM
8403 /* Must use a stable sort here. A modified insertion sort,
8404 since the relocs are mostly sorted already. */
0e287786
AM
8405 elt_size = reldata->hdr->sh_entsize;
8406 base = reldata->hdr->contents;
8407 end = base + count * elt_size;
bca6d0e3 8408 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8409 abort ();
8410
8411 /* Ensure the first element is lowest. This acts as a sentinel,
8412 speeding the main loop below. */
8413 r_off = (*ext_r_off) (base);
8414 for (p = loc = base; (p += elt_size) < end; )
8415 {
8416 bfd_vma r_off2 = (*ext_r_off) (p);
8417 if (r_off > r_off2)
8418 {
8419 r_off = r_off2;
8420 loc = p;
8421 }
8422 }
8423 if (loc != base)
8424 {
8425 /* Don't just swap *base and *loc as that changes the order
8426 of the original base[0] and base[1] if they happen to
8427 have the same r_offset. */
bca6d0e3
AM
8428 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8429 memcpy (onebuf, loc, elt_size);
0e287786 8430 memmove (base + elt_size, base, loc - base);
bca6d0e3 8431 memcpy (base, onebuf, elt_size);
0e287786
AM
8432 }
8433
b29b8669 8434 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8435 {
8436 /* base to p is sorted, *p is next to insert. */
8437 r_off = (*ext_r_off) (p);
8438 /* Search the sorted region for location to insert. */
8439 loc = p - elt_size;
8440 while (r_off < (*ext_r_off) (loc))
8441 loc -= elt_size;
8442 loc += elt_size;
8443 if (loc != p)
8444 {
bca6d0e3
AM
8445 /* Chances are there is a run of relocs to insert here,
8446 from one of more input files. Files are not always
8447 linked in order due to the way elf_link_input_bfd is
8448 called. See pr17666. */
8449 size_t sortlen = p - loc;
8450 bfd_vma r_off2 = (*ext_r_off) (loc);
8451 size_t runlen = elt_size;
8452 size_t buf_size = 96 * 1024;
8453 while (p + runlen < end
8454 && (sortlen <= buf_size
8455 || runlen + elt_size <= buf_size)
8456 && r_off2 > (*ext_r_off) (p + runlen))
8457 runlen += elt_size;
8458 if (buf == NULL)
8459 {
8460 buf = bfd_malloc (buf_size);
8461 if (buf == NULL)
8462 return FALSE;
8463 }
8464 if (runlen < sortlen)
8465 {
8466 memcpy (buf, p, runlen);
8467 memmove (loc + runlen, loc, sortlen);
8468 memcpy (loc, buf, runlen);
8469 }
8470 else
8471 {
8472 memcpy (buf, loc, sortlen);
8473 memmove (loc, p, runlen);
8474 memcpy (loc + runlen, buf, sortlen);
8475 }
b29b8669 8476 p += runlen - elt_size;
0e287786
AM
8477 }
8478 }
8479 /* Hashes are no longer valid. */
28dbcedc
AM
8480 free (reldata->hashes);
8481 reldata->hashes = NULL;
bca6d0e3 8482 free (buf);
53df40a4 8483 }
bca6d0e3 8484 return TRUE;
c152c796
AM
8485}
8486
8487struct elf_link_sort_rela
8488{
8489 union {
8490 bfd_vma offset;
8491 bfd_vma sym_mask;
8492 } u;
8493 enum elf_reloc_type_class type;
8494 /* We use this as an array of size int_rels_per_ext_rel. */
8495 Elf_Internal_Rela rela[1];
8496};
8497
8498static int
8499elf_link_sort_cmp1 (const void *A, const void *B)
8500{
a50b1753
NC
8501 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8502 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8503 int relativea, relativeb;
8504
8505 relativea = a->type == reloc_class_relative;
8506 relativeb = b->type == reloc_class_relative;
8507
8508 if (relativea < relativeb)
8509 return 1;
8510 if (relativea > relativeb)
8511 return -1;
8512 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8513 return -1;
8514 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8515 return 1;
8516 if (a->rela->r_offset < b->rela->r_offset)
8517 return -1;
8518 if (a->rela->r_offset > b->rela->r_offset)
8519 return 1;
8520 return 0;
8521}
8522
8523static int
8524elf_link_sort_cmp2 (const void *A, const void *B)
8525{
a50b1753
NC
8526 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8527 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8528
7e612e98 8529 if (a->type < b->type)
c152c796 8530 return -1;
7e612e98 8531 if (a->type > b->type)
c152c796 8532 return 1;
7e612e98 8533 if (a->u.offset < b->u.offset)
c152c796 8534 return -1;
7e612e98 8535 if (a->u.offset > b->u.offset)
c152c796
AM
8536 return 1;
8537 if (a->rela->r_offset < b->rela->r_offset)
8538 return -1;
8539 if (a->rela->r_offset > b->rela->r_offset)
8540 return 1;
8541 return 0;
8542}
8543
8544static size_t
8545elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8546{
3410fea8 8547 asection *dynamic_relocs;
fc66a176
L
8548 asection *rela_dyn;
8549 asection *rel_dyn;
c152c796
AM
8550 bfd_size_type count, size;
8551 size_t i, ret, sort_elt, ext_size;
8552 bfd_byte *sort, *s_non_relative, *p;
8553 struct elf_link_sort_rela *sq;
8554 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8555 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8556 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8557 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8558 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8559 struct bfd_link_order *lo;
8560 bfd_vma r_sym_mask;
3410fea8 8561 bfd_boolean use_rela;
c152c796 8562
3410fea8
NC
8563 /* Find a dynamic reloc section. */
8564 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8565 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8566 if (rela_dyn != NULL && rela_dyn->size > 0
8567 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8568 {
3410fea8
NC
8569 bfd_boolean use_rela_initialised = FALSE;
8570
8571 /* This is just here to stop gcc from complaining.
c8e44c6d 8572 Its initialization checking code is not perfect. */
3410fea8
NC
8573 use_rela = TRUE;
8574
8575 /* Both sections are present. Examine the sizes
8576 of the indirect sections to help us choose. */
8577 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8578 if (lo->type == bfd_indirect_link_order)
8579 {
8580 asection *o = lo->u.indirect.section;
8581
8582 if ((o->size % bed->s->sizeof_rela) == 0)
8583 {
8584 if ((o->size % bed->s->sizeof_rel) == 0)
8585 /* Section size is divisible by both rel and rela sizes.
8586 It is of no help to us. */
8587 ;
8588 else
8589 {
8590 /* Section size is only divisible by rela. */
8591 if (use_rela_initialised && (use_rela == FALSE))
8592 {
c8e44c6d
AM
8593 _bfd_error_handler (_("%B: Unable to sort relocs - "
8594 "they are in more than one size"),
8595 abfd);
3410fea8
NC
8596 bfd_set_error (bfd_error_invalid_operation);
8597 return 0;
8598 }
8599 else
8600 {
8601 use_rela = TRUE;
8602 use_rela_initialised = TRUE;
8603 }
8604 }
8605 }
8606 else if ((o->size % bed->s->sizeof_rel) == 0)
8607 {
8608 /* Section size is only divisible by rel. */
8609 if (use_rela_initialised && (use_rela == TRUE))
8610 {
c8e44c6d
AM
8611 _bfd_error_handler (_("%B: Unable to sort relocs - "
8612 "they are in more than one size"),
8613 abfd);
3410fea8
NC
8614 bfd_set_error (bfd_error_invalid_operation);
8615 return 0;
8616 }
8617 else
8618 {
8619 use_rela = FALSE;
8620 use_rela_initialised = TRUE;
8621 }
8622 }
8623 else
8624 {
c8e44c6d
AM
8625 /* The section size is not divisible by either -
8626 something is wrong. */
8627 _bfd_error_handler (_("%B: Unable to sort relocs - "
8628 "they are of an unknown size"), abfd);
3410fea8
NC
8629 bfd_set_error (bfd_error_invalid_operation);
8630 return 0;
8631 }
8632 }
8633
8634 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8635 if (lo->type == bfd_indirect_link_order)
8636 {
8637 asection *o = lo->u.indirect.section;
8638
8639 if ((o->size % bed->s->sizeof_rela) == 0)
8640 {
8641 if ((o->size % bed->s->sizeof_rel) == 0)
8642 /* Section size is divisible by both rel and rela sizes.
8643 It is of no help to us. */
8644 ;
8645 else
8646 {
8647 /* Section size is only divisible by rela. */
8648 if (use_rela_initialised && (use_rela == FALSE))
8649 {
c8e44c6d
AM
8650 _bfd_error_handler (_("%B: Unable to sort relocs - "
8651 "they are in more than one size"),
8652 abfd);
3410fea8
NC
8653 bfd_set_error (bfd_error_invalid_operation);
8654 return 0;
8655 }
8656 else
8657 {
8658 use_rela = TRUE;
8659 use_rela_initialised = TRUE;
8660 }
8661 }
8662 }
8663 else if ((o->size % bed->s->sizeof_rel) == 0)
8664 {
8665 /* Section size is only divisible by rel. */
8666 if (use_rela_initialised && (use_rela == TRUE))
8667 {
c8e44c6d
AM
8668 _bfd_error_handler (_("%B: Unable to sort relocs - "
8669 "they are in more than one size"),
8670 abfd);
3410fea8
NC
8671 bfd_set_error (bfd_error_invalid_operation);
8672 return 0;
8673 }
8674 else
8675 {
8676 use_rela = FALSE;
8677 use_rela_initialised = TRUE;
8678 }
8679 }
8680 else
8681 {
c8e44c6d
AM
8682 /* The section size is not divisible by either -
8683 something is wrong. */
8684 _bfd_error_handler (_("%B: Unable to sort relocs - "
8685 "they are of an unknown size"), abfd);
3410fea8
NC
8686 bfd_set_error (bfd_error_invalid_operation);
8687 return 0;
8688 }
8689 }
8690
8691 if (! use_rela_initialised)
8692 /* Make a guess. */
8693 use_rela = TRUE;
c152c796 8694 }
fc66a176
L
8695 else if (rela_dyn != NULL && rela_dyn->size > 0)
8696 use_rela = TRUE;
8697 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8698 use_rela = FALSE;
c152c796 8699 else
fc66a176 8700 return 0;
3410fea8
NC
8701
8702 if (use_rela)
c152c796 8703 {
3410fea8 8704 dynamic_relocs = rela_dyn;
c152c796
AM
8705 ext_size = bed->s->sizeof_rela;
8706 swap_in = bed->s->swap_reloca_in;
8707 swap_out = bed->s->swap_reloca_out;
8708 }
3410fea8
NC
8709 else
8710 {
8711 dynamic_relocs = rel_dyn;
8712 ext_size = bed->s->sizeof_rel;
8713 swap_in = bed->s->swap_reloc_in;
8714 swap_out = bed->s->swap_reloc_out;
8715 }
c152c796
AM
8716
8717 size = 0;
3410fea8 8718 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8719 if (lo->type == bfd_indirect_link_order)
3410fea8 8720 size += lo->u.indirect.section->size;
c152c796 8721
3410fea8 8722 if (size != dynamic_relocs->size)
c152c796
AM
8723 return 0;
8724
8725 sort_elt = (sizeof (struct elf_link_sort_rela)
8726 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8727
8728 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8729 if (count == 0)
8730 return 0;
a50b1753 8731 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8732
c152c796
AM
8733 if (sort == NULL)
8734 {
8735 (*info->callbacks->warning)
8736 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8737 return 0;
8738 }
8739
8740 if (bed->s->arch_size == 32)
8741 r_sym_mask = ~(bfd_vma) 0xff;
8742 else
8743 r_sym_mask = ~(bfd_vma) 0xffffffff;
8744
3410fea8 8745 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8746 if (lo->type == bfd_indirect_link_order)
8747 {
8748 bfd_byte *erel, *erelend;
8749 asection *o = lo->u.indirect.section;
8750
1da212d6
AM
8751 if (o->contents == NULL && o->size != 0)
8752 {
8753 /* This is a reloc section that is being handled as a normal
8754 section. See bfd_section_from_shdr. We can't combine
8755 relocs in this case. */
8756 free (sort);
8757 return 0;
8758 }
c152c796 8759 erel = o->contents;
eea6121a 8760 erelend = o->contents + o->size;
c8e44c6d 8761 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 8762
c152c796
AM
8763 while (erel < erelend)
8764 {
8765 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8766
c152c796 8767 (*swap_in) (abfd, erel, s->rela);
7e612e98 8768 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8769 s->u.sym_mask = r_sym_mask;
8770 p += sort_elt;
8771 erel += ext_size;
8772 }
8773 }
8774
8775 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8776
8777 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8778 {
8779 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8780 if (s->type != reloc_class_relative)
8781 break;
8782 }
8783 ret = i;
8784 s_non_relative = p;
8785
8786 sq = (struct elf_link_sort_rela *) s_non_relative;
8787 for (; i < count; i++, p += sort_elt)
8788 {
8789 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8790 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8791 sq = sp;
8792 sp->u.offset = sq->rela->r_offset;
8793 }
8794
8795 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8796
c8e44c6d
AM
8797 struct elf_link_hash_table *htab = elf_hash_table (info);
8798 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8799 {
8800 /* We have plt relocs in .rela.dyn. */
8801 sq = (struct elf_link_sort_rela *) sort;
8802 for (i = 0; i < count; i++)
8803 if (sq[count - i - 1].type != reloc_class_plt)
8804 break;
8805 if (i != 0 && htab->srelplt->size == i * ext_size)
8806 {
8807 struct bfd_link_order **plo;
8808 /* Put srelplt link_order last. This is so the output_offset
8809 set in the next loop is correct for DT_JMPREL. */
8810 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8811 if ((*plo)->type == bfd_indirect_link_order
8812 && (*plo)->u.indirect.section == htab->srelplt)
8813 {
8814 lo = *plo;
8815 *plo = lo->next;
8816 }
8817 else
8818 plo = &(*plo)->next;
8819 *plo = lo;
8820 lo->next = NULL;
8821 dynamic_relocs->map_tail.link_order = lo;
8822 }
8823 }
8824
8825 p = sort;
3410fea8 8826 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8827 if (lo->type == bfd_indirect_link_order)
8828 {
8829 bfd_byte *erel, *erelend;
8830 asection *o = lo->u.indirect.section;
8831
8832 erel = o->contents;
eea6121a 8833 erelend = o->contents + o->size;
c8e44c6d 8834 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
8835 while (erel < erelend)
8836 {
8837 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8838 (*swap_out) (abfd, s->rela, erel);
8839 p += sort_elt;
8840 erel += ext_size;
8841 }
8842 }
8843
8844 free (sort);
3410fea8 8845 *psec = dynamic_relocs;
c152c796
AM
8846 return ret;
8847}
8848
ef10c3ac 8849/* Add a symbol to the output symbol string table. */
c152c796 8850
6e0b88f1 8851static int
ef10c3ac
L
8852elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8853 const char *name,
8854 Elf_Internal_Sym *elfsym,
8855 asection *input_sec,
8856 struct elf_link_hash_entry *h)
c152c796 8857{
6e0b88f1 8858 int (*output_symbol_hook)
c152c796
AM
8859 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8860 struct elf_link_hash_entry *);
ef10c3ac 8861 struct elf_link_hash_table *hash_table;
c152c796 8862 const struct elf_backend_data *bed;
ef10c3ac 8863 bfd_size_type strtabsize;
c152c796 8864
8539e4e8
AM
8865 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8866
8b127cbc 8867 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
8868 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8869 if (output_symbol_hook != NULL)
8870 {
8b127cbc 8871 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
8872 if (ret != 1)
8873 return ret;
c152c796
AM
8874 }
8875
ef10c3ac
L
8876 if (name == NULL
8877 || *name == '\0'
8878 || (input_sec->flags & SEC_EXCLUDE))
8879 elfsym->st_name = (unsigned long) -1;
c152c796
AM
8880 else
8881 {
ef10c3ac
L
8882 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8883 to get the final offset for st_name. */
8884 elfsym->st_name
8885 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8886 name, FALSE);
c152c796 8887 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8888 return 0;
c152c796
AM
8889 }
8890
ef10c3ac
L
8891 hash_table = elf_hash_table (flinfo->info);
8892 strtabsize = hash_table->strtabsize;
8893 if (strtabsize <= hash_table->strtabcount)
c152c796 8894 {
ef10c3ac
L
8895 strtabsize += strtabsize;
8896 hash_table->strtabsize = strtabsize;
8897 strtabsize *= sizeof (*hash_table->strtab);
8898 hash_table->strtab
8899 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8900 strtabsize);
8901 if (hash_table->strtab == NULL)
6e0b88f1 8902 return 0;
c152c796 8903 }
ef10c3ac
L
8904 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8905 hash_table->strtab[hash_table->strtabcount].dest_index
8906 = hash_table->strtabcount;
8907 hash_table->strtab[hash_table->strtabcount].destshndx_index
8908 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8909
8910 bfd_get_symcount (flinfo->output_bfd) += 1;
8911 hash_table->strtabcount += 1;
8912
8913 return 1;
8914}
8915
8916/* Swap symbols out to the symbol table and flush the output symbols to
8917 the file. */
8918
8919static bfd_boolean
8920elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8921{
8922 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8923 bfd_size_type amt, i;
8924 const struct elf_backend_data *bed;
8925 bfd_byte *symbuf;
8926 Elf_Internal_Shdr *hdr;
8927 file_ptr pos;
8928 bfd_boolean ret;
8929
8930 if (!hash_table->strtabcount)
8931 return TRUE;
8932
8933 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8934
8935 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 8936
ef10c3ac
L
8937 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8938 symbuf = (bfd_byte *) bfd_malloc (amt);
8939 if (symbuf == NULL)
8940 return FALSE;
1b786873 8941
ef10c3ac 8942 if (flinfo->symshndxbuf)
c152c796 8943 {
ef10c3ac
L
8944 amt = (sizeof (Elf_External_Sym_Shndx)
8945 * (bfd_get_symcount (flinfo->output_bfd)));
8946 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8947 if (flinfo->symshndxbuf == NULL)
c152c796 8948 {
ef10c3ac
L
8949 free (symbuf);
8950 return FALSE;
c152c796 8951 }
c152c796
AM
8952 }
8953
ef10c3ac
L
8954 for (i = 0; i < hash_table->strtabcount; i++)
8955 {
8956 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8957 if (elfsym->sym.st_name == (unsigned long) -1)
8958 elfsym->sym.st_name = 0;
8959 else
8960 elfsym->sym.st_name
8961 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8962 elfsym->sym.st_name);
8963 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8964 ((bfd_byte *) symbuf
8965 + (elfsym->dest_index
8966 * bed->s->sizeof_sym)),
8967 (flinfo->symshndxbuf
8968 + elfsym->destshndx_index));
8969 }
8970
8971 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8972 pos = hdr->sh_offset + hdr->sh_size;
8973 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8974 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8975 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8976 {
8977 hdr->sh_size += amt;
8978 ret = TRUE;
8979 }
8980 else
8981 ret = FALSE;
c152c796 8982
ef10c3ac
L
8983 free (symbuf);
8984
8985 free (hash_table->strtab);
8986 hash_table->strtab = NULL;
8987
8988 return ret;
c152c796
AM
8989}
8990
c0d5a53d
L
8991/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8992
8993static bfd_boolean
8994check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8995{
4fbb74a6
AM
8996 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8997 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
8998 {
8999 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9000 beyond 64k. */
c0d5a53d
L
9001 (*_bfd_error_handler)
9002 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9003 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9004 bfd_set_error (bfd_error_nonrepresentable_section);
9005 return FALSE;
9006 }
9007 return TRUE;
9008}
9009
c152c796
AM
9010/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9011 allowing an unsatisfied unversioned symbol in the DSO to match a
9012 versioned symbol that would normally require an explicit version.
9013 We also handle the case that a DSO references a hidden symbol
9014 which may be satisfied by a versioned symbol in another DSO. */
9015
9016static bfd_boolean
9017elf_link_check_versioned_symbol (struct bfd_link_info *info,
9018 const struct elf_backend_data *bed,
9019 struct elf_link_hash_entry *h)
9020{
9021 bfd *abfd;
9022 struct elf_link_loaded_list *loaded;
9023
9024 if (!is_elf_hash_table (info->hash))
9025 return FALSE;
9026
90c984fc
L
9027 /* Check indirect symbol. */
9028 while (h->root.type == bfd_link_hash_indirect)
9029 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9030
c152c796
AM
9031 switch (h->root.type)
9032 {
9033 default:
9034 abfd = NULL;
9035 break;
9036
9037 case bfd_link_hash_undefined:
9038 case bfd_link_hash_undefweak:
9039 abfd = h->root.u.undef.abfd;
9040 if ((abfd->flags & DYNAMIC) == 0
e56f61be 9041 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9042 return FALSE;
9043 break;
9044
9045 case bfd_link_hash_defined:
9046 case bfd_link_hash_defweak:
9047 abfd = h->root.u.def.section->owner;
9048 break;
9049
9050 case bfd_link_hash_common:
9051 abfd = h->root.u.c.p->section->owner;
9052 break;
9053 }
9054 BFD_ASSERT (abfd != NULL);
9055
9056 for (loaded = elf_hash_table (info)->loaded;
9057 loaded != NULL;
9058 loaded = loaded->next)
9059 {
9060 bfd *input;
9061 Elf_Internal_Shdr *hdr;
9062 bfd_size_type symcount;
9063 bfd_size_type extsymcount;
9064 bfd_size_type extsymoff;
9065 Elf_Internal_Shdr *versymhdr;
9066 Elf_Internal_Sym *isym;
9067 Elf_Internal_Sym *isymend;
9068 Elf_Internal_Sym *isymbuf;
9069 Elf_External_Versym *ever;
9070 Elf_External_Versym *extversym;
9071
9072 input = loaded->abfd;
9073
9074 /* We check each DSO for a possible hidden versioned definition. */
9075 if (input == abfd
9076 || (input->flags & DYNAMIC) == 0
9077 || elf_dynversym (input) == 0)
9078 continue;
9079
9080 hdr = &elf_tdata (input)->dynsymtab_hdr;
9081
9082 symcount = hdr->sh_size / bed->s->sizeof_sym;
9083 if (elf_bad_symtab (input))
9084 {
9085 extsymcount = symcount;
9086 extsymoff = 0;
9087 }
9088 else
9089 {
9090 extsymcount = symcount - hdr->sh_info;
9091 extsymoff = hdr->sh_info;
9092 }
9093
9094 if (extsymcount == 0)
9095 continue;
9096
9097 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9098 NULL, NULL, NULL);
9099 if (isymbuf == NULL)
9100 return FALSE;
9101
9102 /* Read in any version definitions. */
9103 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9104 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9105 if (extversym == NULL)
9106 goto error_ret;
9107
9108 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9109 || (bfd_bread (extversym, versymhdr->sh_size, input)
9110 != versymhdr->sh_size))
9111 {
9112 free (extversym);
9113 error_ret:
9114 free (isymbuf);
9115 return FALSE;
9116 }
9117
9118 ever = extversym + extsymoff;
9119 isymend = isymbuf + extsymcount;
9120 for (isym = isymbuf; isym < isymend; isym++, ever++)
9121 {
9122 const char *name;
9123 Elf_Internal_Versym iver;
9124 unsigned short version_index;
9125
9126 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9127 || isym->st_shndx == SHN_UNDEF)
9128 continue;
9129
9130 name = bfd_elf_string_from_elf_section (input,
9131 hdr->sh_link,
9132 isym->st_name);
9133 if (strcmp (name, h->root.root.string) != 0)
9134 continue;
9135
9136 _bfd_elf_swap_versym_in (input, ever, &iver);
9137
d023c380
L
9138 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9139 && !(h->def_regular
9140 && h->forced_local))
c152c796
AM
9141 {
9142 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9143 have provided a definition for the undefined sym unless
9144 it is defined in a non-shared object and forced local.
9145 */
c152c796
AM
9146 abort ();
9147 }
9148
9149 version_index = iver.vs_vers & VERSYM_VERSION;
9150 if (version_index == 1 || version_index == 2)
9151 {
9152 /* This is the base or first version. We can use it. */
9153 free (extversym);
9154 free (isymbuf);
9155 return TRUE;
9156 }
9157 }
9158
9159 free (extversym);
9160 free (isymbuf);
9161 }
9162
9163 return FALSE;
9164}
9165
b8871f35
L
9166/* Convert ELF common symbol TYPE. */
9167
9168static int
9169elf_link_convert_common_type (struct bfd_link_info *info, int type)
9170{
9171 /* Commom symbol can only appear in relocatable link. */
9172 if (!bfd_link_relocatable (info))
9173 abort ();
9174 switch (info->elf_stt_common)
9175 {
9176 case unchanged:
9177 break;
9178 case elf_stt_common:
9179 type = STT_COMMON;
9180 break;
9181 case no_elf_stt_common:
9182 type = STT_OBJECT;
9183 break;
9184 }
9185 return type;
9186}
9187
c152c796
AM
9188/* Add an external symbol to the symbol table. This is called from
9189 the hash table traversal routine. When generating a shared object,
9190 we go through the symbol table twice. The first time we output
9191 anything that might have been forced to local scope in a version
9192 script. The second time we output the symbols that are still
9193 global symbols. */
9194
9195static bfd_boolean
7686d77d 9196elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9197{
7686d77d 9198 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9199 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9200 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9201 bfd_boolean strip;
9202 Elf_Internal_Sym sym;
9203 asection *input_sec;
9204 const struct elf_backend_data *bed;
6e0b88f1
AM
9205 long indx;
9206 int ret;
b8871f35 9207 unsigned int type;
6e33951e
L
9208 /* A symbol is bound locally if it is forced local or it is locally
9209 defined, hidden versioned, not referenced by shared library and
9210 not exported when linking executable. */
9211 bfd_boolean local_bind = (h->forced_local
0e1862bb 9212 || (bfd_link_executable (flinfo->info)
6e33951e
L
9213 && !flinfo->info->export_dynamic
9214 && !h->dynamic
9215 && !h->ref_dynamic
9216 && h->def_regular
422f1182 9217 && h->versioned == versioned_hidden));
c152c796
AM
9218
9219 if (h->root.type == bfd_link_hash_warning)
9220 {
9221 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9222 if (h->root.type == bfd_link_hash_new)
9223 return TRUE;
9224 }
9225
9226 /* Decide whether to output this symbol in this pass. */
9227 if (eoinfo->localsyms)
9228 {
6e33951e 9229 if (!local_bind)
c152c796
AM
9230 return TRUE;
9231 }
9232 else
9233 {
6e33951e 9234 if (local_bind)
c152c796
AM
9235 return TRUE;
9236 }
9237
8b127cbc 9238 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9239
12ac1cf5 9240 if (h->root.type == bfd_link_hash_undefined)
c152c796 9241 {
12ac1cf5
NC
9242 /* If we have an undefined symbol reference here then it must have
9243 come from a shared library that is being linked in. (Undefined
98da7939
L
9244 references in regular files have already been handled unless
9245 they are in unreferenced sections which are removed by garbage
9246 collection). */
12ac1cf5
NC
9247 bfd_boolean ignore_undef = FALSE;
9248
9249 /* Some symbols may be special in that the fact that they're
9250 undefined can be safely ignored - let backend determine that. */
9251 if (bed->elf_backend_ignore_undef_symbol)
9252 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9253
9254 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9255 if (!ignore_undef
12ac1cf5 9256 && h->ref_dynamic
8b127cbc
AM
9257 && (!h->ref_regular || flinfo->info->gc_sections)
9258 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9259 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9260 (*flinfo->info->callbacks->undefined_symbol)
9261 (flinfo->info, h->root.root.string,
9262 h->ref_regular ? NULL : h->root.u.undef.abfd,
9263 NULL, 0,
9264 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9265
9266 /* Strip a global symbol defined in a discarded section. */
9267 if (h->indx == -3)
9268 return TRUE;
c152c796
AM
9269 }
9270
9271 /* We should also warn if a forced local symbol is referenced from
9272 shared libraries. */
0e1862bb 9273 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9274 && h->forced_local
9275 && h->ref_dynamic
371a5866 9276 && h->def_regular
f5385ebf 9277 && !h->dynamic_def
ee659f1f 9278 && h->ref_dynamic_nonweak
8b127cbc 9279 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9280 {
17d078c5
AM
9281 bfd *def_bfd;
9282 const char *msg;
90c984fc
L
9283 struct elf_link_hash_entry *hi = h;
9284
9285 /* Check indirect symbol. */
9286 while (hi->root.type == bfd_link_hash_indirect)
9287 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9288
9289 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9290 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9291 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9292 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9293 else
9294 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9295 def_bfd = flinfo->output_bfd;
90c984fc
L
9296 if (hi->root.u.def.section != bfd_abs_section_ptr)
9297 def_bfd = hi->root.u.def.section->owner;
8b127cbc 9298 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
17d078c5
AM
9299 h->root.root.string);
9300 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9301 eoinfo->failed = TRUE;
9302 return FALSE;
9303 }
9304
9305 /* We don't want to output symbols that have never been mentioned by
9306 a regular file, or that we have been told to strip. However, if
9307 h->indx is set to -2, the symbol is used by a reloc and we must
9308 output it. */
d983c8c5 9309 strip = FALSE;
c152c796 9310 if (h->indx == -2)
d983c8c5 9311 ;
f5385ebf 9312 else if ((h->def_dynamic
77cfaee6
AM
9313 || h->ref_dynamic
9314 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9315 && !h->def_regular
9316 && !h->ref_regular)
c152c796 9317 strip = TRUE;
8b127cbc 9318 else if (flinfo->info->strip == strip_all)
c152c796 9319 strip = TRUE;
8b127cbc
AM
9320 else if (flinfo->info->strip == strip_some
9321 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9322 h->root.root.string, FALSE, FALSE) == NULL)
9323 strip = TRUE;
d56d55e7
AM
9324 else if ((h->root.type == bfd_link_hash_defined
9325 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9326 && ((flinfo->info->strip_discarded
dbaa2011 9327 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9328 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9329 && h->root.u.def.section->owner != NULL
d56d55e7 9330 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9331 strip = TRUE;
9e2278f5
AM
9332 else if ((h->root.type == bfd_link_hash_undefined
9333 || h->root.type == bfd_link_hash_undefweak)
9334 && h->root.u.undef.abfd != NULL
9335 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9336 strip = TRUE;
c152c796 9337
b8871f35
L
9338 type = h->type;
9339
c152c796 9340 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9341 nothing else to do. However, if it is a forced local symbol or
9342 an ifunc symbol we need to give the backend finish_dynamic_symbol
9343 function a chance to make it dynamic. */
c152c796
AM
9344 if (strip
9345 && h->dynindx == -1
b8871f35 9346 && type != STT_GNU_IFUNC
f5385ebf 9347 && !h->forced_local)
c152c796
AM
9348 return TRUE;
9349
9350 sym.st_value = 0;
9351 sym.st_size = h->size;
9352 sym.st_other = h->other;
c152c796
AM
9353 switch (h->root.type)
9354 {
9355 default:
9356 case bfd_link_hash_new:
9357 case bfd_link_hash_warning:
9358 abort ();
9359 return FALSE;
9360
9361 case bfd_link_hash_undefined:
9362 case bfd_link_hash_undefweak:
9363 input_sec = bfd_und_section_ptr;
9364 sym.st_shndx = SHN_UNDEF;
9365 break;
9366
9367 case bfd_link_hash_defined:
9368 case bfd_link_hash_defweak:
9369 {
9370 input_sec = h->root.u.def.section;
9371 if (input_sec->output_section != NULL)
9372 {
9373 sym.st_shndx =
8b127cbc 9374 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9375 input_sec->output_section);
9376 if (sym.st_shndx == SHN_BAD)
9377 {
9378 (*_bfd_error_handler)
d003868e 9379 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9380 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9381 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9382 eoinfo->failed = TRUE;
9383 return FALSE;
9384 }
9385
9386 /* ELF symbols in relocatable files are section relative,
9387 but in nonrelocatable files they are virtual
9388 addresses. */
9389 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9390 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9391 {
9392 sym.st_value += input_sec->output_section->vma;
9393 if (h->type == STT_TLS)
9394 {
8b127cbc 9395 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9396 if (tls_sec != NULL)
9397 sym.st_value -= tls_sec->vma;
c152c796
AM
9398 }
9399 }
9400 }
9401 else
9402 {
9403 BFD_ASSERT (input_sec->owner == NULL
9404 || (input_sec->owner->flags & DYNAMIC) != 0);
9405 sym.st_shndx = SHN_UNDEF;
9406 input_sec = bfd_und_section_ptr;
9407 }
9408 }
9409 break;
9410
9411 case bfd_link_hash_common:
9412 input_sec = h->root.u.c.p->section;
a4d8e49b 9413 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9414 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9415 break;
9416
9417 case bfd_link_hash_indirect:
9418 /* These symbols are created by symbol versioning. They point
9419 to the decorated version of the name. For example, if the
9420 symbol foo@@GNU_1.2 is the default, which should be used when
9421 foo is used with no version, then we add an indirect symbol
9422 foo which points to foo@@GNU_1.2. We ignore these symbols,
9423 since the indirected symbol is already in the hash table. */
9424 return TRUE;
9425 }
9426
b8871f35
L
9427 if (type == STT_COMMON || type == STT_OBJECT)
9428 switch (h->root.type)
9429 {
9430 case bfd_link_hash_common:
9431 type = elf_link_convert_common_type (flinfo->info, type);
9432 break;
9433 case bfd_link_hash_defined:
9434 case bfd_link_hash_defweak:
9435 if (bed->common_definition (&sym))
9436 type = elf_link_convert_common_type (flinfo->info, type);
9437 else
9438 type = STT_OBJECT;
9439 break;
9440 case bfd_link_hash_undefined:
9441 case bfd_link_hash_undefweak:
9442 break;
9443 default:
9444 abort ();
9445 }
9446
9447 if (local_bind)
9448 {
9449 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9450 /* Turn off visibility on local symbol. */
9451 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9452 }
9453 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9454 else if (h->unique_global && h->def_regular)
9455 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9456 else if (h->root.type == bfd_link_hash_undefweak
9457 || h->root.type == bfd_link_hash_defweak)
9458 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9459 else
9460 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9461 sym.st_target_internal = h->target_internal;
9462
c152c796
AM
9463 /* Give the processor backend a chance to tweak the symbol value,
9464 and also to finish up anything that needs to be done for this
9465 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9466 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9467 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9468 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9469 && h->def_regular
0e1862bb 9470 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9471 || ((h->dynindx != -1
9472 || h->forced_local)
0e1862bb 9473 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9474 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9475 || h->root.type != bfd_link_hash_undefweak))
9476 || !h->forced_local)
8b127cbc 9477 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9478 {
9479 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9480 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9481 {
9482 eoinfo->failed = TRUE;
9483 return FALSE;
9484 }
9485 }
9486
9487 /* If we are marking the symbol as undefined, and there are no
9488 non-weak references to this symbol from a regular object, then
9489 mark the symbol as weak undefined; if there are non-weak
9490 references, mark the symbol as strong. We can't do this earlier,
9491 because it might not be marked as undefined until the
9492 finish_dynamic_symbol routine gets through with it. */
9493 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9494 && h->ref_regular
c152c796
AM
9495 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9496 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9497 {
9498 int bindtype;
b8871f35 9499 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9500
9501 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9502 if (type == STT_GNU_IFUNC)
9503 type = STT_FUNC;
c152c796 9504
f5385ebf 9505 if (h->ref_regular_nonweak)
c152c796
AM
9506 bindtype = STB_GLOBAL;
9507 else
9508 bindtype = STB_WEAK;
2955ec4c 9509 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9510 }
9511
bda987c2
CD
9512 /* If this is a symbol defined in a dynamic library, don't use the
9513 symbol size from the dynamic library. Relinking an executable
9514 against a new library may introduce gratuitous changes in the
9515 executable's symbols if we keep the size. */
9516 if (sym.st_shndx == SHN_UNDEF
9517 && !h->def_regular
9518 && h->def_dynamic)
9519 sym.st_size = 0;
9520
c152c796
AM
9521 /* If a non-weak symbol with non-default visibility is not defined
9522 locally, it is a fatal error. */
0e1862bb 9523 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9524 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9525 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9526 && h->root.type == bfd_link_hash_undefined
f5385ebf 9527 && !h->def_regular)
c152c796 9528 {
17d078c5
AM
9529 const char *msg;
9530
9531 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9532 msg = _("%B: protected symbol `%s' isn't defined");
9533 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9534 msg = _("%B: internal symbol `%s' isn't defined");
9535 else
9536 msg = _("%B: hidden symbol `%s' isn't defined");
8b127cbc 9537 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9538 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9539 eoinfo->failed = TRUE;
9540 return FALSE;
9541 }
9542
9543 /* If this symbol should be put in the .dynsym section, then put it
9544 there now. We already know the symbol index. We also fill in
9545 the entry in the .hash section. */
cae1fbbb 9546 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9547 && h->dynindx != -1
8b127cbc 9548 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9549 {
c152c796
AM
9550 bfd_byte *esym;
9551
90c984fc
L
9552 /* Since there is no version information in the dynamic string,
9553 if there is no version info in symbol version section, we will
1659f720 9554 have a run-time problem if not linking executable, referenced
6e33951e
L
9555 by shared library, not locally defined, or not bound locally.
9556 */
1659f720 9557 if (h->verinfo.verdef == NULL
6e33951e 9558 && !local_bind
0e1862bb 9559 && (!bfd_link_executable (flinfo->info)
1659f720
L
9560 || h->ref_dynamic
9561 || !h->def_regular))
90c984fc
L
9562 {
9563 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9564
9565 if (p && p [1] != '\0')
9566 {
9567 (*_bfd_error_handler)
9568 (_("%B: No symbol version section for versioned symbol `%s'"),
9569 flinfo->output_bfd, h->root.root.string);
9570 eoinfo->failed = TRUE;
9571 return FALSE;
9572 }
9573 }
9574
c152c796 9575 sym.st_name = h->dynstr_index;
cae1fbbb
L
9576 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9577 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9578 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9579 {
9580 eoinfo->failed = TRUE;
9581 return FALSE;
9582 }
8b127cbc 9583 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9584
8b127cbc 9585 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9586 {
9587 size_t hash_entry_size;
9588 bfd_byte *bucketpos;
9589 bfd_vma chain;
41198d0c
L
9590 size_t bucketcount;
9591 size_t bucket;
9592
8b127cbc 9593 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9594 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9595
9596 hash_entry_size
8b127cbc
AM
9597 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9598 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9599 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9600 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9601 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9602 bucketpos);
9603 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9604 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9605 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9606 }
c152c796 9607
8b127cbc 9608 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9609 {
9610 Elf_Internal_Versym iversym;
9611 Elf_External_Versym *eversym;
9612
f5385ebf 9613 if (!h->def_regular)
c152c796 9614 {
7b20f099
AM
9615 if (h->verinfo.verdef == NULL
9616 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9617 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9618 iversym.vs_vers = 0;
9619 else
9620 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9621 }
9622 else
9623 {
9624 if (h->verinfo.vertree == NULL)
9625 iversym.vs_vers = 1;
9626 else
9627 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9628 if (flinfo->info->create_default_symver)
3e3b46e5 9629 iversym.vs_vers++;
c152c796
AM
9630 }
9631
422f1182 9632 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9633 defined locally. */
422f1182 9634 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9635 iversym.vs_vers |= VERSYM_HIDDEN;
9636
8b127cbc 9637 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9638 eversym += h->dynindx;
8b127cbc 9639 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9640 }
9641 }
9642
d983c8c5
AM
9643 /* If the symbol is undefined, and we didn't output it to .dynsym,
9644 strip it from .symtab too. Obviously we can't do this for
9645 relocatable output or when needed for --emit-relocs. */
9646 else if (input_sec == bfd_und_section_ptr
9647 && h->indx != -2
0e1862bb 9648 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9649 return TRUE;
9650 /* Also strip others that we couldn't earlier due to dynamic symbol
9651 processing. */
9652 if (strip)
9653 return TRUE;
9654 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9655 return TRUE;
9656
2ec55de3
AM
9657 /* Output a FILE symbol so that following locals are not associated
9658 with the wrong input file. We need one for forced local symbols
9659 if we've seen more than one FILE symbol or when we have exactly
9660 one FILE symbol but global symbols are present in a file other
9661 than the one with the FILE symbol. We also need one if linker
9662 defined symbols are present. In practice these conditions are
9663 always met, so just emit the FILE symbol unconditionally. */
9664 if (eoinfo->localsyms
9665 && !eoinfo->file_sym_done
9666 && eoinfo->flinfo->filesym_count != 0)
9667 {
9668 Elf_Internal_Sym fsym;
9669
9670 memset (&fsym, 0, sizeof (fsym));
9671 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9672 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9673 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9674 bfd_und_section_ptr, NULL))
2ec55de3
AM
9675 return FALSE;
9676
9677 eoinfo->file_sym_done = TRUE;
9678 }
9679
8b127cbc 9680 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9681 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9682 input_sec, h);
6e0b88f1 9683 if (ret == 0)
c152c796
AM
9684 {
9685 eoinfo->failed = TRUE;
9686 return FALSE;
9687 }
6e0b88f1
AM
9688 else if (ret == 1)
9689 h->indx = indx;
9690 else if (h->indx == -2)
9691 abort();
c152c796
AM
9692
9693 return TRUE;
9694}
9695
cdd3575c
AM
9696/* Return TRUE if special handling is done for relocs in SEC against
9697 symbols defined in discarded sections. */
9698
c152c796
AM
9699static bfd_boolean
9700elf_section_ignore_discarded_relocs (asection *sec)
9701{
9702 const struct elf_backend_data *bed;
9703
cdd3575c
AM
9704 switch (sec->sec_info_type)
9705 {
dbaa2011
AM
9706 case SEC_INFO_TYPE_STABS:
9707 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9708 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9709 return TRUE;
9710 default:
9711 break;
9712 }
c152c796
AM
9713
9714 bed = get_elf_backend_data (sec->owner);
9715 if (bed->elf_backend_ignore_discarded_relocs != NULL
9716 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9717 return TRUE;
9718
9719 return FALSE;
9720}
9721
9e66c942
AM
9722/* Return a mask saying how ld should treat relocations in SEC against
9723 symbols defined in discarded sections. If this function returns
9724 COMPLAIN set, ld will issue a warning message. If this function
9725 returns PRETEND set, and the discarded section was link-once and the
9726 same size as the kept link-once section, ld will pretend that the
9727 symbol was actually defined in the kept section. Otherwise ld will
9728 zero the reloc (at least that is the intent, but some cooperation by
9729 the target dependent code is needed, particularly for REL targets). */
9730
8a696751
AM
9731unsigned int
9732_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9733{
9e66c942 9734 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9735 return PRETEND;
cdd3575c
AM
9736
9737 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9738 return 0;
cdd3575c
AM
9739
9740 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9741 return 0;
cdd3575c 9742
9e66c942 9743 return COMPLAIN | PRETEND;
cdd3575c
AM
9744}
9745
3d7f7666
L
9746/* Find a match between a section and a member of a section group. */
9747
9748static asection *
c0f00686
L
9749match_group_member (asection *sec, asection *group,
9750 struct bfd_link_info *info)
3d7f7666
L
9751{
9752 asection *first = elf_next_in_group (group);
9753 asection *s = first;
9754
9755 while (s != NULL)
9756 {
c0f00686 9757 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9758 return s;
9759
83180ade 9760 s = elf_next_in_group (s);
3d7f7666
L
9761 if (s == first)
9762 break;
9763 }
9764
9765 return NULL;
9766}
9767
01b3c8ab 9768/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9769 to replace it. Return the replacement if it is OK. Otherwise return
9770 NULL. */
01b3c8ab
L
9771
9772asection *
c0f00686 9773_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9774{
9775 asection *kept;
9776
9777 kept = sec->kept_section;
9778 if (kept != NULL)
9779 {
c2370991 9780 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9781 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9782 if (kept != NULL
9783 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9784 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9785 kept = NULL;
c2370991 9786 sec->kept_section = kept;
01b3c8ab
L
9787 }
9788 return kept;
9789}
9790
c152c796
AM
9791/* Link an input file into the linker output file. This function
9792 handles all the sections and relocations of the input file at once.
9793 This is so that we only have to read the local symbols once, and
9794 don't have to keep them in memory. */
9795
9796static bfd_boolean
8b127cbc 9797elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9798{
ece5ef60 9799 int (*relocate_section)
c152c796
AM
9800 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9801 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9802 bfd *output_bfd;
9803 Elf_Internal_Shdr *symtab_hdr;
9804 size_t locsymcount;
9805 size_t extsymoff;
9806 Elf_Internal_Sym *isymbuf;
9807 Elf_Internal_Sym *isym;
9808 Elf_Internal_Sym *isymend;
9809 long *pindex;
9810 asection **ppsection;
9811 asection *o;
9812 const struct elf_backend_data *bed;
c152c796 9813 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9814 bfd_size_type address_size;
9815 bfd_vma r_type_mask;
9816 int r_sym_shift;
ffbc01cc 9817 bfd_boolean have_file_sym = FALSE;
c152c796 9818
8b127cbc 9819 output_bfd = flinfo->output_bfd;
c152c796
AM
9820 bed = get_elf_backend_data (output_bfd);
9821 relocate_section = bed->elf_backend_relocate_section;
9822
9823 /* If this is a dynamic object, we don't want to do anything here:
9824 we don't want the local symbols, and we don't want the section
9825 contents. */
9826 if ((input_bfd->flags & DYNAMIC) != 0)
9827 return TRUE;
9828
c152c796
AM
9829 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9830 if (elf_bad_symtab (input_bfd))
9831 {
9832 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9833 extsymoff = 0;
9834 }
9835 else
9836 {
9837 locsymcount = symtab_hdr->sh_info;
9838 extsymoff = symtab_hdr->sh_info;
9839 }
9840
9841 /* Read the local symbols. */
9842 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9843 if (isymbuf == NULL && locsymcount != 0)
9844 {
9845 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9846 flinfo->internal_syms,
9847 flinfo->external_syms,
9848 flinfo->locsym_shndx);
c152c796
AM
9849 if (isymbuf == NULL)
9850 return FALSE;
9851 }
9852
9853 /* Find local symbol sections and adjust values of symbols in
9854 SEC_MERGE sections. Write out those local symbols we know are
9855 going into the output file. */
9856 isymend = isymbuf + locsymcount;
8b127cbc 9857 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9858 isym < isymend;
9859 isym++, pindex++, ppsection++)
9860 {
9861 asection *isec;
9862 const char *name;
9863 Elf_Internal_Sym osym;
6e0b88f1
AM
9864 long indx;
9865 int ret;
c152c796
AM
9866
9867 *pindex = -1;
9868
9869 if (elf_bad_symtab (input_bfd))
9870 {
9871 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9872 {
9873 *ppsection = NULL;
9874 continue;
9875 }
9876 }
9877
9878 if (isym->st_shndx == SHN_UNDEF)
9879 isec = bfd_und_section_ptr;
c152c796
AM
9880 else if (isym->st_shndx == SHN_ABS)
9881 isec = bfd_abs_section_ptr;
9882 else if (isym->st_shndx == SHN_COMMON)
9883 isec = bfd_com_section_ptr;
9884 else
9885 {
cb33740c
AM
9886 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9887 if (isec == NULL)
9888 {
9889 /* Don't attempt to output symbols with st_shnx in the
9890 reserved range other than SHN_ABS and SHN_COMMON. */
9891 *ppsection = NULL;
9892 continue;
9893 }
dbaa2011 9894 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9895 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9896 isym->st_value =
9897 _bfd_merged_section_offset (output_bfd, &isec,
9898 elf_section_data (isec)->sec_info,
9899 isym->st_value);
c152c796
AM
9900 }
9901
9902 *ppsection = isec;
9903
d983c8c5
AM
9904 /* Don't output the first, undefined, symbol. In fact, don't
9905 output any undefined local symbol. */
9906 if (isec == bfd_und_section_ptr)
c152c796
AM
9907 continue;
9908
9909 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9910 {
9911 /* We never output section symbols. Instead, we use the
9912 section symbol of the corresponding section in the output
9913 file. */
9914 continue;
9915 }
9916
9917 /* If we are stripping all symbols, we don't want to output this
9918 one. */
8b127cbc 9919 if (flinfo->info->strip == strip_all)
c152c796
AM
9920 continue;
9921
9922 /* If we are discarding all local symbols, we don't want to
9923 output this one. If we are generating a relocatable output
9924 file, then some of the local symbols may be required by
9925 relocs; we output them below as we discover that they are
9926 needed. */
8b127cbc 9927 if (flinfo->info->discard == discard_all)
c152c796
AM
9928 continue;
9929
9930 /* If this symbol is defined in a section which we are
f02571c5
AM
9931 discarding, we don't need to keep it. */
9932 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9933 && isym->st_shndx < SHN_LORESERVE
9934 && bfd_section_removed_from_list (output_bfd,
9935 isec->output_section))
e75a280b
L
9936 continue;
9937
c152c796
AM
9938 /* Get the name of the symbol. */
9939 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9940 isym->st_name);
9941 if (name == NULL)
9942 return FALSE;
9943
9944 /* See if we are discarding symbols with this name. */
8b127cbc
AM
9945 if ((flinfo->info->strip == strip_some
9946 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 9947 == NULL))
8b127cbc 9948 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
9949 && (isec->flags & SEC_MERGE)
9950 && !bfd_link_relocatable (flinfo->info))
8b127cbc 9951 || flinfo->info->discard == discard_l)
c152c796
AM
9952 && bfd_is_local_label_name (input_bfd, name)))
9953 continue;
9954
ffbc01cc
AM
9955 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9956 {
ce875075
AM
9957 if (input_bfd->lto_output)
9958 /* -flto puts a temp file name here. This means builds
9959 are not reproducible. Discard the symbol. */
9960 continue;
ffbc01cc
AM
9961 have_file_sym = TRUE;
9962 flinfo->filesym_count += 1;
9963 }
9964 if (!have_file_sym)
9965 {
9966 /* In the absence of debug info, bfd_find_nearest_line uses
9967 FILE symbols to determine the source file for local
9968 function symbols. Provide a FILE symbol here if input
9969 files lack such, so that their symbols won't be
9970 associated with a previous input file. It's not the
9971 source file, but the best we can do. */
9972 have_file_sym = TRUE;
9973 flinfo->filesym_count += 1;
9974 memset (&osym, 0, sizeof (osym));
9975 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9976 osym.st_shndx = SHN_ABS;
ef10c3ac
L
9977 if (!elf_link_output_symstrtab (flinfo,
9978 (input_bfd->lto_output ? NULL
9979 : input_bfd->filename),
9980 &osym, bfd_abs_section_ptr,
9981 NULL))
ffbc01cc
AM
9982 return FALSE;
9983 }
9984
c152c796
AM
9985 osym = *isym;
9986
9987 /* Adjust the section index for the output file. */
9988 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9989 isec->output_section);
9990 if (osym.st_shndx == SHN_BAD)
9991 return FALSE;
9992
c152c796
AM
9993 /* ELF symbols in relocatable files are section relative, but
9994 in executable files they are virtual addresses. Note that
9995 this code assumes that all ELF sections have an associated
9996 BFD section with a reasonable value for output_offset; below
9997 we assume that they also have a reasonable value for
9998 output_section. Any special sections must be set up to meet
9999 these requirements. */
10000 osym.st_value += isec->output_offset;
0e1862bb 10001 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10002 {
10003 osym.st_value += isec->output_section->vma;
10004 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10005 {
10006 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10007 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10008 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10009 }
10010 }
10011
6e0b88f1 10012 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10013 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10014 if (ret == 0)
c152c796 10015 return FALSE;
6e0b88f1
AM
10016 else if (ret == 1)
10017 *pindex = indx;
c152c796
AM
10018 }
10019
310fd250
L
10020 if (bed->s->arch_size == 32)
10021 {
10022 r_type_mask = 0xff;
10023 r_sym_shift = 8;
10024 address_size = 4;
10025 }
10026 else
10027 {
10028 r_type_mask = 0xffffffff;
10029 r_sym_shift = 32;
10030 address_size = 8;
10031 }
10032
c152c796
AM
10033 /* Relocate the contents of each section. */
10034 sym_hashes = elf_sym_hashes (input_bfd);
10035 for (o = input_bfd->sections; o != NULL; o = o->next)
10036 {
10037 bfd_byte *contents;
10038
10039 if (! o->linker_mark)
10040 {
10041 /* This section was omitted from the link. */
10042 continue;
10043 }
10044
0e1862bb 10045 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
10046 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10047 {
10048 /* Deal with the group signature symbol. */
10049 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10050 unsigned long symndx = sec_data->this_hdr.sh_info;
10051 asection *osec = o->output_section;
10052
10053 if (symndx >= locsymcount
10054 || (elf_bad_symtab (input_bfd)
8b127cbc 10055 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10056 {
10057 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10058 while (h->root.type == bfd_link_hash_indirect
10059 || h->root.type == bfd_link_hash_warning)
10060 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10061 /* Arrange for symbol to be output. */
10062 h->indx = -2;
10063 elf_section_data (osec)->this_hdr.sh_info = -2;
10064 }
10065 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10066 {
10067 /* We'll use the output section target_index. */
8b127cbc 10068 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10069 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10070 }
10071 else
10072 {
8b127cbc 10073 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10074 {
10075 /* Otherwise output the local symbol now. */
10076 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10077 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10078 const char *name;
6e0b88f1
AM
10079 long indx;
10080 int ret;
bcacc0f5
AM
10081
10082 name = bfd_elf_string_from_elf_section (input_bfd,
10083 symtab_hdr->sh_link,
10084 sym.st_name);
10085 if (name == NULL)
10086 return FALSE;
10087
10088 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10089 sec);
10090 if (sym.st_shndx == SHN_BAD)
10091 return FALSE;
10092
10093 sym.st_value += o->output_offset;
10094
6e0b88f1 10095 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10096 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10097 NULL);
6e0b88f1 10098 if (ret == 0)
bcacc0f5 10099 return FALSE;
6e0b88f1 10100 else if (ret == 1)
8b127cbc 10101 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10102 else
10103 abort ();
bcacc0f5
AM
10104 }
10105 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10106 = flinfo->indices[symndx];
bcacc0f5
AM
10107 }
10108 }
10109
c152c796 10110 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10111 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10112 continue;
10113
10114 if ((o->flags & SEC_LINKER_CREATED) != 0)
10115 {
10116 /* Section was created by _bfd_elf_link_create_dynamic_sections
10117 or somesuch. */
10118 continue;
10119 }
10120
10121 /* Get the contents of the section. They have been cached by a
10122 relaxation routine. Note that o is a section in an input
10123 file, so the contents field will not have been set by any of
10124 the routines which work on output files. */
10125 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10126 {
10127 contents = elf_section_data (o)->this_hdr.contents;
10128 if (bed->caches_rawsize
10129 && o->rawsize != 0
10130 && o->rawsize < o->size)
10131 {
10132 memcpy (flinfo->contents, contents, o->rawsize);
10133 contents = flinfo->contents;
10134 }
10135 }
c152c796
AM
10136 else
10137 {
8b127cbc 10138 contents = flinfo->contents;
4a114e3e 10139 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10140 return FALSE;
10141 }
10142
10143 if ((o->flags & SEC_RELOC) != 0)
10144 {
10145 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10146 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10147 int action_discarded;
ece5ef60 10148 int ret;
c152c796
AM
10149
10150 /* Get the swapped relocs. */
10151 internal_relocs
8b127cbc
AM
10152 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10153 flinfo->internal_relocs, FALSE);
c152c796
AM
10154 if (internal_relocs == NULL
10155 && o->reloc_count > 0)
10156 return FALSE;
10157
310fd250
L
10158 /* We need to reverse-copy input .ctors/.dtors sections if
10159 they are placed in .init_array/.finit_array for output. */
10160 if (o->size > address_size
10161 && ((strncmp (o->name, ".ctors", 6) == 0
10162 && strcmp (o->output_section->name,
10163 ".init_array") == 0)
10164 || (strncmp (o->name, ".dtors", 6) == 0
10165 && strcmp (o->output_section->name,
10166 ".fini_array") == 0))
10167 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10168 {
310fd250
L
10169 if (o->size != o->reloc_count * address_size)
10170 {
10171 (*_bfd_error_handler)
10172 (_("error: %B: size of section %A is not "
10173 "multiple of address size"),
10174 input_bfd, o);
10175 bfd_set_error (bfd_error_on_input);
10176 return FALSE;
10177 }
10178 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10179 }
10180
0f02bbd9 10181 action_discarded = -1;
c152c796 10182 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10183 action_discarded = (*bed->action_discarded) (o);
10184
10185 /* Run through the relocs evaluating complex reloc symbols and
10186 looking for relocs against symbols from discarded sections
10187 or section symbols from removed link-once sections.
10188 Complain about relocs against discarded sections. Zero
10189 relocs against removed link-once sections. */
10190
10191 rel = internal_relocs;
10192 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10193 for ( ; rel < relend; rel++)
c152c796 10194 {
0f02bbd9
AM
10195 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10196 unsigned int s_type;
10197 asection **ps, *sec;
10198 struct elf_link_hash_entry *h = NULL;
10199 const char *sym_name;
c152c796 10200
0f02bbd9
AM
10201 if (r_symndx == STN_UNDEF)
10202 continue;
c152c796 10203
0f02bbd9
AM
10204 if (r_symndx >= locsymcount
10205 || (elf_bad_symtab (input_bfd)
8b127cbc 10206 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10207 {
10208 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10209
0f02bbd9
AM
10210 /* Badly formatted input files can contain relocs that
10211 reference non-existant symbols. Check here so that
10212 we do not seg fault. */
10213 if (h == NULL)
c152c796 10214 {
0f02bbd9 10215 char buffer [32];
dce669a1 10216
0f02bbd9
AM
10217 sprintf_vma (buffer, rel->r_info);
10218 (*_bfd_error_handler)
10219 (_("error: %B contains a reloc (0x%s) for section %A "
10220 "that references a non-existent global symbol"),
10221 input_bfd, o, buffer);
10222 bfd_set_error (bfd_error_bad_value);
10223 return FALSE;
10224 }
3b36f7e6 10225
0f02bbd9
AM
10226 while (h->root.type == bfd_link_hash_indirect
10227 || h->root.type == bfd_link_hash_warning)
10228 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10229
0f02bbd9 10230 s_type = h->type;
cdd3575c 10231
9e2dec47 10232 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10233 mark the symbol as undefined. Note that the
10234 linker may attach linker created dynamic sections
10235 to the plugin bfd. Symbols defined in linker
10236 created sections are not plugin symbols. */
9e2dec47
L
10237 if (h->root.non_ir_ref
10238 && (h->root.type == bfd_link_hash_defined
10239 || h->root.type == bfd_link_hash_defweak)
10240 && (h->root.u.def.section->flags
10241 & SEC_LINKER_CREATED) == 0
10242 && h->root.u.def.section->owner != NULL
10243 && (h->root.u.def.section->owner->flags
10244 & BFD_PLUGIN) != 0)
10245 {
10246 h->root.type = bfd_link_hash_undefined;
10247 h->root.u.undef.abfd = h->root.u.def.section->owner;
10248 }
10249
0f02bbd9
AM
10250 ps = NULL;
10251 if (h->root.type == bfd_link_hash_defined
10252 || h->root.type == bfd_link_hash_defweak)
10253 ps = &h->root.u.def.section;
10254
10255 sym_name = h->root.root.string;
10256 }
10257 else
10258 {
10259 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10260
10261 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10262 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10263 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10264 sym, *ps);
10265 }
c152c796 10266
c301e700 10267 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10268 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10269 {
10270 bfd_vma val;
10271 bfd_vma dot = (rel->r_offset
10272 + o->output_offset + o->output_section->vma);
10273#ifdef DEBUG
10274 printf ("Encountered a complex symbol!");
10275 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10276 input_bfd->filename, o->name,
10277 (long) (rel - internal_relocs));
0f02bbd9
AM
10278 printf (" symbol: idx %8.8lx, name %s\n",
10279 r_symndx, sym_name);
10280 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10281 (unsigned long) rel->r_info,
10282 (unsigned long) rel->r_offset);
10283#endif
8b127cbc 10284 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10285 isymbuf, locsymcount, s_type == STT_SRELC))
10286 return FALSE;
10287
10288 /* Symbol evaluated OK. Update to absolute value. */
10289 set_symbol_value (input_bfd, isymbuf, locsymcount,
10290 r_symndx, val);
10291 continue;
10292 }
10293
10294 if (action_discarded != -1 && ps != NULL)
10295 {
cdd3575c
AM
10296 /* Complain if the definition comes from a
10297 discarded section. */
dbaa2011 10298 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10299 {
cf35638d 10300 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10301 if (action_discarded & COMPLAIN)
8b127cbc 10302 (*flinfo->info->callbacks->einfo)
e1fffbe6 10303 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10304 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10305 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10306
87e5235d 10307 /* Try to do the best we can to support buggy old
e0ae6d6f 10308 versions of gcc. Pretend that the symbol is
87e5235d
AM
10309 really defined in the kept linkonce section.
10310 FIXME: This is quite broken. Modifying the
10311 symbol here means we will be changing all later
e0ae6d6f 10312 uses of the symbol, not just in this section. */
0f02bbd9 10313 if (action_discarded & PRETEND)
87e5235d 10314 {
01b3c8ab
L
10315 asection *kept;
10316
c0f00686 10317 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10318 flinfo->info);
01b3c8ab 10319 if (kept != NULL)
87e5235d
AM
10320 {
10321 *ps = kept;
10322 continue;
10323 }
10324 }
c152c796
AM
10325 }
10326 }
10327 }
10328
10329 /* Relocate the section by invoking a back end routine.
10330
10331 The back end routine is responsible for adjusting the
10332 section contents as necessary, and (if using Rela relocs
10333 and generating a relocatable output file) adjusting the
10334 reloc addend as necessary.
10335
10336 The back end routine does not have to worry about setting
10337 the reloc address or the reloc symbol index.
10338
10339 The back end routine is given a pointer to the swapped in
10340 internal symbols, and can access the hash table entries
10341 for the external symbols via elf_sym_hashes (input_bfd).
10342
10343 When generating relocatable output, the back end routine
10344 must handle STB_LOCAL/STT_SECTION symbols specially. The
10345 output symbol is going to be a section symbol
10346 corresponding to the output section, which will require
10347 the addend to be adjusted. */
10348
8b127cbc 10349 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10350 input_bfd, o, contents,
10351 internal_relocs,
10352 isymbuf,
8b127cbc 10353 flinfo->sections);
ece5ef60 10354 if (!ret)
c152c796
AM
10355 return FALSE;
10356
ece5ef60 10357 if (ret == 2
0e1862bb 10358 || bfd_link_relocatable (flinfo->info)
8b127cbc 10359 || flinfo->info->emitrelocations)
c152c796
AM
10360 {
10361 Elf_Internal_Rela *irela;
d4730f92 10362 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10363 bfd_vma last_offset;
10364 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10365 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10366 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10367 unsigned int next_erel;
c152c796 10368 bfd_boolean rela_normal;
d4730f92 10369 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10370
d4730f92
BS
10371 esdi = elf_section_data (o);
10372 esdo = elf_section_data (o->output_section);
10373 rela_normal = FALSE;
c152c796
AM
10374
10375 /* Adjust the reloc addresses and symbol indices. */
10376
10377 irela = internal_relocs;
10378 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10379 rel_hash = esdo->rel.hashes + esdo->rel.count;
10380 /* We start processing the REL relocs, if any. When we reach
10381 IRELAMID in the loop, we switch to the RELA relocs. */
10382 irelamid = irela;
10383 if (esdi->rel.hdr != NULL)
10384 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10385 * bed->s->int_rels_per_ext_rel);
eac338cf 10386 rel_hash_list = rel_hash;
d4730f92 10387 rela_hash_list = NULL;
c152c796 10388 last_offset = o->output_offset;
0e1862bb 10389 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10390 last_offset += o->output_section->vma;
10391 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10392 {
10393 unsigned long r_symndx;
10394 asection *sec;
10395 Elf_Internal_Sym sym;
10396
10397 if (next_erel == bed->s->int_rels_per_ext_rel)
10398 {
10399 rel_hash++;
10400 next_erel = 0;
10401 }
10402
d4730f92
BS
10403 if (irela == irelamid)
10404 {
10405 rel_hash = esdo->rela.hashes + esdo->rela.count;
10406 rela_hash_list = rel_hash;
10407 rela_normal = bed->rela_normal;
10408 }
10409
c152c796 10410 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10411 flinfo->info, o,
c152c796
AM
10412 irela->r_offset);
10413 if (irela->r_offset >= (bfd_vma) -2)
10414 {
10415 /* This is a reloc for a deleted entry or somesuch.
10416 Turn it into an R_*_NONE reloc, at the same
10417 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10418 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10419 being ordered. */
10420 irela->r_offset = last_offset;
10421 irela->r_info = 0;
10422 irela->r_addend = 0;
10423 continue;
10424 }
10425
10426 irela->r_offset += o->output_offset;
10427
10428 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10429 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10430 irela->r_offset += o->output_section->vma;
10431
10432 last_offset = irela->r_offset;
10433
10434 r_symndx = irela->r_info >> r_sym_shift;
10435 if (r_symndx == STN_UNDEF)
10436 continue;
10437
10438 if (r_symndx >= locsymcount
10439 || (elf_bad_symtab (input_bfd)
8b127cbc 10440 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10441 {
10442 struct elf_link_hash_entry *rh;
10443 unsigned long indx;
10444
10445 /* This is a reloc against a global symbol. We
10446 have not yet output all the local symbols, so
10447 we do not know the symbol index of any global
10448 symbol. We set the rel_hash entry for this
10449 reloc to point to the global hash table entry
10450 for this symbol. The symbol index is then
ee75fd95 10451 set at the end of bfd_elf_final_link. */
c152c796
AM
10452 indx = r_symndx - extsymoff;
10453 rh = elf_sym_hashes (input_bfd)[indx];
10454 while (rh->root.type == bfd_link_hash_indirect
10455 || rh->root.type == bfd_link_hash_warning)
10456 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10457
10458 /* Setting the index to -2 tells
10459 elf_link_output_extsym that this symbol is
10460 used by a reloc. */
10461 BFD_ASSERT (rh->indx < 0);
10462 rh->indx = -2;
10463
10464 *rel_hash = rh;
10465
10466 continue;
10467 }
10468
10469 /* This is a reloc against a local symbol. */
10470
10471 *rel_hash = NULL;
10472 sym = isymbuf[r_symndx];
8b127cbc 10473 sec = flinfo->sections[r_symndx];
c152c796
AM
10474 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10475 {
10476 /* I suppose the backend ought to fill in the
10477 section of any STT_SECTION symbol against a
6a8d1586 10478 processor specific section. */
cf35638d 10479 r_symndx = STN_UNDEF;
6a8d1586
AM
10480 if (bfd_is_abs_section (sec))
10481 ;
c152c796
AM
10482 else if (sec == NULL || sec->owner == NULL)
10483 {
10484 bfd_set_error (bfd_error_bad_value);
10485 return FALSE;
10486 }
10487 else
10488 {
6a8d1586
AM
10489 asection *osec = sec->output_section;
10490
10491 /* If we have discarded a section, the output
10492 section will be the absolute section. In
ab96bf03
AM
10493 case of discarded SEC_MERGE sections, use
10494 the kept section. relocate_section should
10495 have already handled discarded linkonce
10496 sections. */
6a8d1586
AM
10497 if (bfd_is_abs_section (osec)
10498 && sec->kept_section != NULL
10499 && sec->kept_section->output_section != NULL)
10500 {
10501 osec = sec->kept_section->output_section;
10502 irela->r_addend -= osec->vma;
10503 }
10504
10505 if (!bfd_is_abs_section (osec))
10506 {
10507 r_symndx = osec->target_index;
cf35638d 10508 if (r_symndx == STN_UNDEF)
74541ad4 10509 {
051d833a
AM
10510 irela->r_addend += osec->vma;
10511 osec = _bfd_nearby_section (output_bfd, osec,
10512 osec->vma);
10513 irela->r_addend -= osec->vma;
10514 r_symndx = osec->target_index;
74541ad4 10515 }
6a8d1586 10516 }
c152c796
AM
10517 }
10518
10519 /* Adjust the addend according to where the
10520 section winds up in the output section. */
10521 if (rela_normal)
10522 irela->r_addend += sec->output_offset;
10523 }
10524 else
10525 {
8b127cbc 10526 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10527 {
10528 unsigned long shlink;
10529 const char *name;
10530 asection *osec;
6e0b88f1 10531 long indx;
c152c796 10532
8b127cbc 10533 if (flinfo->info->strip == strip_all)
c152c796
AM
10534 {
10535 /* You can't do ld -r -s. */
10536 bfd_set_error (bfd_error_invalid_operation);
10537 return FALSE;
10538 }
10539
10540 /* This symbol was skipped earlier, but
10541 since it is needed by a reloc, we
10542 must output it now. */
10543 shlink = symtab_hdr->sh_link;
10544 name = (bfd_elf_string_from_elf_section
10545 (input_bfd, shlink, sym.st_name));
10546 if (name == NULL)
10547 return FALSE;
10548
10549 osec = sec->output_section;
10550 sym.st_shndx =
10551 _bfd_elf_section_from_bfd_section (output_bfd,
10552 osec);
10553 if (sym.st_shndx == SHN_BAD)
10554 return FALSE;
10555
10556 sym.st_value += sec->output_offset;
0e1862bb 10557 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10558 {
10559 sym.st_value += osec->vma;
10560 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10561 {
10562 /* STT_TLS symbols are relative to PT_TLS
10563 segment base. */
8b127cbc 10564 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10565 ->tls_sec != NULL);
8b127cbc 10566 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10567 ->tls_sec->vma);
10568 }
10569 }
10570
6e0b88f1 10571 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10572 ret = elf_link_output_symstrtab (flinfo, name,
10573 &sym, sec,
10574 NULL);
6e0b88f1 10575 if (ret == 0)
c152c796 10576 return FALSE;
6e0b88f1 10577 else if (ret == 1)
8b127cbc 10578 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10579 else
10580 abort ();
c152c796
AM
10581 }
10582
8b127cbc 10583 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10584 }
10585
10586 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10587 | (irela->r_info & r_type_mask));
10588 }
10589
10590 /* Swap out the relocs. */
d4730f92
BS
10591 input_rel_hdr = esdi->rel.hdr;
10592 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10593 {
d4730f92
BS
10594 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10595 input_rel_hdr,
10596 internal_relocs,
10597 rel_hash_list))
10598 return FALSE;
c152c796
AM
10599 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10600 * bed->s->int_rels_per_ext_rel);
eac338cf 10601 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10602 }
10603
10604 input_rela_hdr = esdi->rela.hdr;
10605 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10606 {
eac338cf 10607 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10608 input_rela_hdr,
eac338cf 10609 internal_relocs,
d4730f92 10610 rela_hash_list))
c152c796
AM
10611 return FALSE;
10612 }
10613 }
10614 }
10615
10616 /* Write out the modified section contents. */
10617 if (bed->elf_backend_write_section
8b127cbc 10618 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10619 contents))
c152c796
AM
10620 {
10621 /* Section written out. */
10622 }
10623 else switch (o->sec_info_type)
10624 {
dbaa2011 10625 case SEC_INFO_TYPE_STABS:
c152c796
AM
10626 if (! (_bfd_write_section_stabs
10627 (output_bfd,
8b127cbc 10628 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10629 o, &elf_section_data (o)->sec_info, contents)))
10630 return FALSE;
10631 break;
dbaa2011 10632 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10633 if (! _bfd_write_merged_section (output_bfd, o,
10634 elf_section_data (o)->sec_info))
10635 return FALSE;
10636 break;
dbaa2011 10637 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10638 {
8b127cbc 10639 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10640 o, contents))
10641 return FALSE;
10642 }
10643 break;
2f0c68f2
CM
10644 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10645 {
10646 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10647 flinfo->info,
10648 o, contents))
10649 return FALSE;
10650 }
10651 break;
c152c796
AM
10652 default:
10653 {
310fd250
L
10654 if (! (o->flags & SEC_EXCLUDE))
10655 {
10656 file_ptr offset = (file_ptr) o->output_offset;
10657 bfd_size_type todo = o->size;
37b01f6a
DG
10658
10659 offset *= bfd_octets_per_byte (output_bfd);
10660
310fd250
L
10661 if ((o->flags & SEC_ELF_REVERSE_COPY))
10662 {
10663 /* Reverse-copy input section to output. */
10664 do
10665 {
10666 todo -= address_size;
10667 if (! bfd_set_section_contents (output_bfd,
10668 o->output_section,
10669 contents + todo,
10670 offset,
10671 address_size))
10672 return FALSE;
10673 if (todo == 0)
10674 break;
10675 offset += address_size;
10676 }
10677 while (1);
10678 }
10679 else if (! bfd_set_section_contents (output_bfd,
10680 o->output_section,
10681 contents,
10682 offset, todo))
10683 return FALSE;
10684 }
c152c796
AM
10685 }
10686 break;
10687 }
10688 }
10689
10690 return TRUE;
10691}
10692
10693/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10694 requested by the linker, and does not come from any input file. This
c152c796
AM
10695 is used to build constructor and destructor tables when linking
10696 with -Ur. */
10697
10698static bfd_boolean
10699elf_reloc_link_order (bfd *output_bfd,
10700 struct bfd_link_info *info,
10701 asection *output_section,
10702 struct bfd_link_order *link_order)
10703{
10704 reloc_howto_type *howto;
10705 long indx;
10706 bfd_vma offset;
10707 bfd_vma addend;
d4730f92 10708 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10709 struct elf_link_hash_entry **rel_hash_ptr;
10710 Elf_Internal_Shdr *rel_hdr;
10711 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10712 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10713 bfd_byte *erel;
10714 unsigned int i;
d4730f92 10715 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10716
10717 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10718 if (howto == NULL)
10719 {
10720 bfd_set_error (bfd_error_bad_value);
10721 return FALSE;
10722 }
10723
10724 addend = link_order->u.reloc.p->addend;
10725
d4730f92
BS
10726 if (esdo->rel.hdr)
10727 reldata = &esdo->rel;
10728 else if (esdo->rela.hdr)
10729 reldata = &esdo->rela;
10730 else
10731 {
10732 reldata = NULL;
10733 BFD_ASSERT (0);
10734 }
10735
c152c796 10736 /* Figure out the symbol index. */
d4730f92 10737 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10738 if (link_order->type == bfd_section_reloc_link_order)
10739 {
10740 indx = link_order->u.reloc.p->u.section->target_index;
10741 BFD_ASSERT (indx != 0);
10742 *rel_hash_ptr = NULL;
10743 }
10744 else
10745 {
10746 struct elf_link_hash_entry *h;
10747
10748 /* Treat a reloc against a defined symbol as though it were
10749 actually against the section. */
10750 h = ((struct elf_link_hash_entry *)
10751 bfd_wrapped_link_hash_lookup (output_bfd, info,
10752 link_order->u.reloc.p->u.name,
10753 FALSE, FALSE, TRUE));
10754 if (h != NULL
10755 && (h->root.type == bfd_link_hash_defined
10756 || h->root.type == bfd_link_hash_defweak))
10757 {
10758 asection *section;
10759
10760 section = h->root.u.def.section;
10761 indx = section->output_section->target_index;
10762 *rel_hash_ptr = NULL;
10763 /* It seems that we ought to add the symbol value to the
10764 addend here, but in practice it has already been added
10765 because it was passed to constructor_callback. */
10766 addend += section->output_section->vma + section->output_offset;
10767 }
10768 else if (h != NULL)
10769 {
10770 /* Setting the index to -2 tells elf_link_output_extsym that
10771 this symbol is used by a reloc. */
10772 h->indx = -2;
10773 *rel_hash_ptr = h;
10774 indx = 0;
10775 }
10776 else
10777 {
1a72702b
AM
10778 (*info->callbacks->unattached_reloc)
10779 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
10780 indx = 0;
10781 }
10782 }
10783
10784 /* If this is an inplace reloc, we must write the addend into the
10785 object file. */
10786 if (howto->partial_inplace && addend != 0)
10787 {
10788 bfd_size_type size;
10789 bfd_reloc_status_type rstat;
10790 bfd_byte *buf;
10791 bfd_boolean ok;
10792 const char *sym_name;
10793
a50b1753
NC
10794 size = (bfd_size_type) bfd_get_reloc_size (howto);
10795 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10796 if (buf == NULL && size != 0)
c152c796
AM
10797 return FALSE;
10798 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10799 switch (rstat)
10800 {
10801 case bfd_reloc_ok:
10802 break;
10803
10804 default:
10805 case bfd_reloc_outofrange:
10806 abort ();
10807
10808 case bfd_reloc_overflow:
10809 if (link_order->type == bfd_section_reloc_link_order)
10810 sym_name = bfd_section_name (output_bfd,
10811 link_order->u.reloc.p->u.section);
10812 else
10813 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
10814 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10815 howto->name, addend, NULL, NULL,
10816 (bfd_vma) 0);
c152c796
AM
10817 break;
10818 }
37b01f6a 10819
c152c796 10820 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
10821 link_order->offset
10822 * bfd_octets_per_byte (output_bfd),
10823 size);
c152c796
AM
10824 free (buf);
10825 if (! ok)
10826 return FALSE;
10827 }
10828
10829 /* The address of a reloc is relative to the section in a
10830 relocatable file, and is a virtual address in an executable
10831 file. */
10832 offset = link_order->offset;
0e1862bb 10833 if (! bfd_link_relocatable (info))
c152c796
AM
10834 offset += output_section->vma;
10835
10836 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10837 {
10838 irel[i].r_offset = offset;
10839 irel[i].r_info = 0;
10840 irel[i].r_addend = 0;
10841 }
10842 if (bed->s->arch_size == 32)
10843 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10844 else
10845 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10846
d4730f92 10847 rel_hdr = reldata->hdr;
c152c796
AM
10848 erel = rel_hdr->contents;
10849 if (rel_hdr->sh_type == SHT_REL)
10850 {
d4730f92 10851 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10852 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10853 }
10854 else
10855 {
10856 irel[0].r_addend = addend;
d4730f92 10857 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10858 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10859 }
10860
d4730f92 10861 ++reldata->count;
c152c796
AM
10862
10863 return TRUE;
10864}
10865
0b52efa6
PB
10866
10867/* Get the output vma of the section pointed to by the sh_link field. */
10868
10869static bfd_vma
10870elf_get_linked_section_vma (struct bfd_link_order *p)
10871{
10872 Elf_Internal_Shdr **elf_shdrp;
10873 asection *s;
10874 int elfsec;
10875
10876 s = p->u.indirect.section;
10877 elf_shdrp = elf_elfsections (s->owner);
10878 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10879 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10880 /* PR 290:
10881 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10882 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10883 sh_info fields. Hence we could get the situation
10884 where elfsec is 0. */
10885 if (elfsec == 0)
10886 {
10887 const struct elf_backend_data *bed
10888 = get_elf_backend_data (s->owner);
10889 if (bed->link_order_error_handler)
d003868e
AM
10890 bed->link_order_error_handler
10891 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10892 return 0;
10893 }
10894 else
10895 {
10896 s = elf_shdrp[elfsec]->bfd_section;
10897 return s->output_section->vma + s->output_offset;
10898 }
0b52efa6
PB
10899}
10900
10901
10902/* Compare two sections based on the locations of the sections they are
10903 linked to. Used by elf_fixup_link_order. */
10904
10905static int
10906compare_link_order (const void * a, const void * b)
10907{
10908 bfd_vma apos;
10909 bfd_vma bpos;
10910
10911 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10912 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10913 if (apos < bpos)
10914 return -1;
10915 return apos > bpos;
10916}
10917
10918
10919/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10920 order as their linked sections. Returns false if this could not be done
10921 because an output section includes both ordered and unordered
10922 sections. Ideally we'd do this in the linker proper. */
10923
10924static bfd_boolean
10925elf_fixup_link_order (bfd *abfd, asection *o)
10926{
10927 int seen_linkorder;
10928 int seen_other;
10929 int n;
10930 struct bfd_link_order *p;
10931 bfd *sub;
10932 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10933 unsigned elfsec;
0b52efa6 10934 struct bfd_link_order **sections;
d33cdfe3 10935 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10936 bfd_vma offset;
3b36f7e6 10937
d33cdfe3
L
10938 other_sec = NULL;
10939 linkorder_sec = NULL;
0b52efa6
PB
10940 seen_other = 0;
10941 seen_linkorder = 0;
8423293d 10942 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10943 {
d33cdfe3 10944 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10945 {
10946 s = p->u.indirect.section;
d33cdfe3
L
10947 sub = s->owner;
10948 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10949 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10950 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10951 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10952 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10953 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10954 {
10955 seen_linkorder++;
10956 linkorder_sec = s;
10957 }
0b52efa6 10958 else
d33cdfe3
L
10959 {
10960 seen_other++;
10961 other_sec = s;
10962 }
0b52efa6
PB
10963 }
10964 else
10965 seen_other++;
d33cdfe3
L
10966
10967 if (seen_other && seen_linkorder)
10968 {
10969 if (other_sec && linkorder_sec)
10970 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10971 o, linkorder_sec,
10972 linkorder_sec->owner, other_sec,
10973 other_sec->owner);
10974 else
10975 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10976 o);
10977 bfd_set_error (bfd_error_bad_value);
10978 return FALSE;
10979 }
0b52efa6
PB
10980 }
10981
10982 if (!seen_linkorder)
10983 return TRUE;
10984
0b52efa6 10985 sections = (struct bfd_link_order **)
14b1c01e
AM
10986 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10987 if (sections == NULL)
10988 return FALSE;
0b52efa6 10989 seen_linkorder = 0;
3b36f7e6 10990
8423293d 10991 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
10992 {
10993 sections[seen_linkorder++] = p;
10994 }
10995 /* Sort the input sections in the order of their linked section. */
10996 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10997 compare_link_order);
10998
10999 /* Change the offsets of the sections. */
11000 offset = 0;
11001 for (n = 0; n < seen_linkorder; n++)
11002 {
11003 s = sections[n]->u.indirect.section;
461686a3 11004 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11005 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11006 sections[n]->offset = offset;
11007 offset += sections[n]->size;
11008 }
11009
4dd07732 11010 free (sections);
0b52efa6
PB
11011 return TRUE;
11012}
11013
9f7c3e5e
AM
11014static void
11015elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11016{
11017 asection *o;
11018
11019 if (flinfo->symstrtab != NULL)
ef10c3ac 11020 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11021 if (flinfo->contents != NULL)
11022 free (flinfo->contents);
11023 if (flinfo->external_relocs != NULL)
11024 free (flinfo->external_relocs);
11025 if (flinfo->internal_relocs != NULL)
11026 free (flinfo->internal_relocs);
11027 if (flinfo->external_syms != NULL)
11028 free (flinfo->external_syms);
11029 if (flinfo->locsym_shndx != NULL)
11030 free (flinfo->locsym_shndx);
11031 if (flinfo->internal_syms != NULL)
11032 free (flinfo->internal_syms);
11033 if (flinfo->indices != NULL)
11034 free (flinfo->indices);
11035 if (flinfo->sections != NULL)
11036 free (flinfo->sections);
9f7c3e5e
AM
11037 if (flinfo->symshndxbuf != NULL)
11038 free (flinfo->symshndxbuf);
11039 for (o = obfd->sections; o != NULL; o = o->next)
11040 {
11041 struct bfd_elf_section_data *esdo = elf_section_data (o);
11042 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11043 free (esdo->rel.hashes);
11044 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11045 free (esdo->rela.hashes);
11046 }
11047}
0b52efa6 11048
c152c796
AM
11049/* Do the final step of an ELF link. */
11050
11051bfd_boolean
11052bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11053{
11054 bfd_boolean dynamic;
11055 bfd_boolean emit_relocs;
11056 bfd *dynobj;
8b127cbc 11057 struct elf_final_link_info flinfo;
91d6fa6a
NC
11058 asection *o;
11059 struct bfd_link_order *p;
11060 bfd *sub;
c152c796
AM
11061 bfd_size_type max_contents_size;
11062 bfd_size_type max_external_reloc_size;
11063 bfd_size_type max_internal_reloc_count;
11064 bfd_size_type max_sym_count;
11065 bfd_size_type max_sym_shndx_count;
c152c796
AM
11066 Elf_Internal_Sym elfsym;
11067 unsigned int i;
11068 Elf_Internal_Shdr *symtab_hdr;
11069 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11070 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11071 struct elf_outext_info eoinfo;
11072 bfd_boolean merged;
11073 size_t relativecount = 0;
11074 asection *reldyn = 0;
11075 bfd_size_type amt;
104d59d1
JM
11076 asection *attr_section = NULL;
11077 bfd_vma attr_size = 0;
11078 const char *std_attrs_section;
c152c796
AM
11079
11080 if (! is_elf_hash_table (info->hash))
11081 return FALSE;
11082
0e1862bb 11083 if (bfd_link_pic (info))
c152c796
AM
11084 abfd->flags |= DYNAMIC;
11085
11086 dynamic = elf_hash_table (info)->dynamic_sections_created;
11087 dynobj = elf_hash_table (info)->dynobj;
11088
0e1862bb 11089 emit_relocs = (bfd_link_relocatable (info)
a4676736 11090 || info->emitrelocations);
c152c796 11091
8b127cbc
AM
11092 flinfo.info = info;
11093 flinfo.output_bfd = abfd;
ef10c3ac 11094 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11095 if (flinfo.symstrtab == NULL)
c152c796
AM
11096 return FALSE;
11097
11098 if (! dynamic)
11099 {
8b127cbc
AM
11100 flinfo.hash_sec = NULL;
11101 flinfo.symver_sec = NULL;
c152c796
AM
11102 }
11103 else
11104 {
3d4d4302 11105 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11106 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11107 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11108 /* Note that it is OK if symver_sec is NULL. */
11109 }
11110
8b127cbc
AM
11111 flinfo.contents = NULL;
11112 flinfo.external_relocs = NULL;
11113 flinfo.internal_relocs = NULL;
11114 flinfo.external_syms = NULL;
11115 flinfo.locsym_shndx = NULL;
11116 flinfo.internal_syms = NULL;
11117 flinfo.indices = NULL;
11118 flinfo.sections = NULL;
8b127cbc 11119 flinfo.symshndxbuf = NULL;
ffbc01cc 11120 flinfo.filesym_count = 0;
c152c796 11121
104d59d1
JM
11122 /* The object attributes have been merged. Remove the input
11123 sections from the link, and set the contents of the output
11124 secton. */
11125 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11126 for (o = abfd->sections; o != NULL; o = o->next)
11127 {
11128 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11129 || strcmp (o->name, ".gnu.attributes") == 0)
11130 {
11131 for (p = o->map_head.link_order; p != NULL; p = p->next)
11132 {
11133 asection *input_section;
11134
11135 if (p->type != bfd_indirect_link_order)
11136 continue;
11137 input_section = p->u.indirect.section;
11138 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11139 elf_link_input_bfd ignores this section. */
11140 input_section->flags &= ~SEC_HAS_CONTENTS;
11141 }
a0c8462f 11142
104d59d1
JM
11143 attr_size = bfd_elf_obj_attr_size (abfd);
11144 if (attr_size)
11145 {
11146 bfd_set_section_size (abfd, o, attr_size);
11147 attr_section = o;
11148 /* Skip this section later on. */
11149 o->map_head.link_order = NULL;
11150 }
11151 else
11152 o->flags |= SEC_EXCLUDE;
11153 }
11154 }
11155
c152c796
AM
11156 /* Count up the number of relocations we will output for each output
11157 section, so that we know the sizes of the reloc sections. We
11158 also figure out some maximum sizes. */
11159 max_contents_size = 0;
11160 max_external_reloc_size = 0;
11161 max_internal_reloc_count = 0;
11162 max_sym_count = 0;
11163 max_sym_shndx_count = 0;
11164 merged = FALSE;
11165 for (o = abfd->sections; o != NULL; o = o->next)
11166 {
11167 struct bfd_elf_section_data *esdo = elf_section_data (o);
11168 o->reloc_count = 0;
11169
8423293d 11170 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11171 {
11172 unsigned int reloc_count = 0;
491d01d3 11173 unsigned int additional_reloc_count = 0;
c152c796 11174 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11175
11176 if (p->type == bfd_section_reloc_link_order
11177 || p->type == bfd_symbol_reloc_link_order)
11178 reloc_count = 1;
11179 else if (p->type == bfd_indirect_link_order)
11180 {
11181 asection *sec;
11182
11183 sec = p->u.indirect.section;
11184 esdi = elf_section_data (sec);
11185
11186 /* Mark all sections which are to be included in the
11187 link. This will normally be every section. We need
11188 to do this so that we can identify any sections which
11189 the linker has decided to not include. */
11190 sec->linker_mark = TRUE;
11191
11192 if (sec->flags & SEC_MERGE)
11193 merged = TRUE;
11194
aed64b35
L
11195 if (esdo->this_hdr.sh_type == SHT_REL
11196 || esdo->this_hdr.sh_type == SHT_RELA)
11197 /* Some backends use reloc_count in relocation sections
11198 to count particular types of relocs. Of course,
11199 reloc sections themselves can't have relocations. */
11200 reloc_count = 0;
0e1862bb 11201 else if (emit_relocs)
491d01d3
YU
11202 {
11203 reloc_count = sec->reloc_count;
11204 if (bed->elf_backend_count_additional_relocs)
11205 {
11206 int c;
11207 c = (*bed->elf_backend_count_additional_relocs) (sec);
11208 additional_reloc_count += c;
11209 }
11210 }
c152c796 11211 else if (bed->elf_backend_count_relocs)
58217f29 11212 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 11213
eea6121a
AM
11214 if (sec->rawsize > max_contents_size)
11215 max_contents_size = sec->rawsize;
11216 if (sec->size > max_contents_size)
11217 max_contents_size = sec->size;
c152c796
AM
11218
11219 /* We are interested in just local symbols, not all
11220 symbols. */
11221 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11222 && (sec->owner->flags & DYNAMIC) == 0)
11223 {
11224 size_t sym_count;
11225
11226 if (elf_bad_symtab (sec->owner))
11227 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11228 / bed->s->sizeof_sym);
11229 else
11230 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11231
11232 if (sym_count > max_sym_count)
11233 max_sym_count = sym_count;
11234
11235 if (sym_count > max_sym_shndx_count
6a40cf0c 11236 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11237 max_sym_shndx_count = sym_count;
11238
11239 if ((sec->flags & SEC_RELOC) != 0)
11240 {
d4730f92 11241 size_t ext_size = 0;
c152c796 11242
d4730f92
BS
11243 if (esdi->rel.hdr != NULL)
11244 ext_size = esdi->rel.hdr->sh_size;
11245 if (esdi->rela.hdr != NULL)
11246 ext_size += esdi->rela.hdr->sh_size;
7326c758 11247
c152c796
AM
11248 if (ext_size > max_external_reloc_size)
11249 max_external_reloc_size = ext_size;
11250 if (sec->reloc_count > max_internal_reloc_count)
11251 max_internal_reloc_count = sec->reloc_count;
11252 }
11253 }
11254 }
11255
11256 if (reloc_count == 0)
11257 continue;
11258
491d01d3 11259 reloc_count += additional_reloc_count;
c152c796
AM
11260 o->reloc_count += reloc_count;
11261
0e1862bb 11262 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11263 {
d4730f92 11264 if (esdi->rel.hdr)
491d01d3
YU
11265 {
11266 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11267 esdo->rel.count += additional_reloc_count;
11268 }
d4730f92 11269 if (esdi->rela.hdr)
491d01d3
YU
11270 {
11271 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11272 esdo->rela.count += additional_reloc_count;
11273 }
d4730f92
BS
11274 }
11275 else
11276 {
11277 if (o->use_rela_p)
11278 esdo->rela.count += reloc_count;
2c2b4ed4 11279 else
d4730f92 11280 esdo->rel.count += reloc_count;
c152c796 11281 }
c152c796
AM
11282 }
11283
11284 if (o->reloc_count > 0)
11285 o->flags |= SEC_RELOC;
11286 else
11287 {
11288 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11289 set it (this is probably a bug) and if it is set
11290 assign_section_numbers will create a reloc section. */
11291 o->flags &=~ SEC_RELOC;
11292 }
11293
11294 /* If the SEC_ALLOC flag is not set, force the section VMA to
11295 zero. This is done in elf_fake_sections as well, but forcing
11296 the VMA to 0 here will ensure that relocs against these
11297 sections are handled correctly. */
11298 if ((o->flags & SEC_ALLOC) == 0
11299 && ! o->user_set_vma)
11300 o->vma = 0;
11301 }
11302
0e1862bb 11303 if (! bfd_link_relocatable (info) && merged)
c152c796
AM
11304 elf_link_hash_traverse (elf_hash_table (info),
11305 _bfd_elf_link_sec_merge_syms, abfd);
11306
11307 /* Figure out the file positions for everything but the symbol table
11308 and the relocs. We set symcount to force assign_section_numbers
11309 to create a symbol table. */
8539e4e8 11310 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11311 BFD_ASSERT (! abfd->output_has_begun);
11312 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11313 goto error_return;
11314
ee75fd95 11315 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11316 for (o = abfd->sections; o != NULL; o = o->next)
11317 {
d4730f92 11318 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11319 if ((o->flags & SEC_RELOC) != 0)
11320 {
d4730f92
BS
11321 if (esdo->rel.hdr
11322 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11323 goto error_return;
11324
d4730f92
BS
11325 if (esdo->rela.hdr
11326 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11327 goto error_return;
11328 }
11329
11330 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11331 to count upwards while actually outputting the relocations. */
d4730f92
BS
11332 esdo->rel.count = 0;
11333 esdo->rela.count = 0;
0ce398f1
L
11334
11335 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11336 {
11337 /* Cache the section contents so that they can be compressed
11338 later. Use bfd_malloc since it will be freed by
11339 bfd_compress_section_contents. */
11340 unsigned char *contents = esdo->this_hdr.contents;
11341 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11342 abort ();
11343 contents
11344 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11345 if (contents == NULL)
11346 goto error_return;
11347 esdo->this_hdr.contents = contents;
11348 }
c152c796
AM
11349 }
11350
c152c796 11351 /* We have now assigned file positions for all the sections except
a485e98e
AM
11352 .symtab, .strtab, and non-loaded reloc sections. We start the
11353 .symtab section at the current file position, and write directly
11354 to it. We build the .strtab section in memory. */
c152c796
AM
11355 bfd_get_symcount (abfd) = 0;
11356 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11357 /* sh_name is set in prep_headers. */
11358 symtab_hdr->sh_type = SHT_SYMTAB;
11359 /* sh_flags, sh_addr and sh_size all start off zero. */
11360 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11361 /* sh_link is set in assign_section_numbers. */
11362 /* sh_info is set below. */
11363 /* sh_offset is set just below. */
72de5009 11364 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11365
ef10c3ac
L
11366 if (max_sym_count < 20)
11367 max_sym_count = 20;
11368 elf_hash_table (info)->strtabsize = max_sym_count;
11369 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11370 elf_hash_table (info)->strtab
11371 = (struct elf_sym_strtab *) bfd_malloc (amt);
11372 if (elf_hash_table (info)->strtab == NULL)
c152c796 11373 goto error_return;
ef10c3ac
L
11374 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11375 flinfo.symshndxbuf
11376 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11377 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11378
8539e4e8 11379 if (info->strip != strip_all || emit_relocs)
c152c796 11380 {
8539e4e8
AM
11381 file_ptr off = elf_next_file_pos (abfd);
11382
11383 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11384
11385 /* Note that at this point elf_next_file_pos (abfd) is
11386 incorrect. We do not yet know the size of the .symtab section.
11387 We correct next_file_pos below, after we do know the size. */
11388
11389 /* Start writing out the symbol table. The first symbol is always a
11390 dummy symbol. */
c152c796
AM
11391 elfsym.st_value = 0;
11392 elfsym.st_size = 0;
11393 elfsym.st_info = 0;
11394 elfsym.st_other = 0;
11395 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11396 elfsym.st_target_internal = 0;
ef10c3ac
L
11397 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11398 bfd_und_section_ptr, NULL) != 1)
c152c796 11399 goto error_return;
c152c796 11400
8539e4e8
AM
11401 /* Output a symbol for each section. We output these even if we are
11402 discarding local symbols, since they are used for relocs. These
11403 symbols have no names. We store the index of each one in the
11404 index field of the section, so that we can find it again when
11405 outputting relocs. */
11406
c152c796
AM
11407 elfsym.st_size = 0;
11408 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11409 elfsym.st_other = 0;
f0b5bb34 11410 elfsym.st_value = 0;
35fc36a8 11411 elfsym.st_target_internal = 0;
c152c796
AM
11412 for (i = 1; i < elf_numsections (abfd); i++)
11413 {
11414 o = bfd_section_from_elf_index (abfd, i);
11415 if (o != NULL)
f0b5bb34
AM
11416 {
11417 o->target_index = bfd_get_symcount (abfd);
11418 elfsym.st_shndx = i;
0e1862bb 11419 if (!bfd_link_relocatable (info))
f0b5bb34 11420 elfsym.st_value = o->vma;
ef10c3ac
L
11421 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11422 NULL) != 1)
f0b5bb34
AM
11423 goto error_return;
11424 }
c152c796
AM
11425 }
11426 }
11427
11428 /* Allocate some memory to hold information read in from the input
11429 files. */
11430 if (max_contents_size != 0)
11431 {
8b127cbc
AM
11432 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11433 if (flinfo.contents == NULL)
c152c796
AM
11434 goto error_return;
11435 }
11436
11437 if (max_external_reloc_size != 0)
11438 {
8b127cbc
AM
11439 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11440 if (flinfo.external_relocs == NULL)
c152c796
AM
11441 goto error_return;
11442 }
11443
11444 if (max_internal_reloc_count != 0)
11445 {
11446 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11447 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11448 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11449 if (flinfo.internal_relocs == NULL)
c152c796
AM
11450 goto error_return;
11451 }
11452
11453 if (max_sym_count != 0)
11454 {
11455 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11456 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11457 if (flinfo.external_syms == NULL)
c152c796
AM
11458 goto error_return;
11459
11460 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11461 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11462 if (flinfo.internal_syms == NULL)
c152c796
AM
11463 goto error_return;
11464
11465 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11466 flinfo.indices = (long int *) bfd_malloc (amt);
11467 if (flinfo.indices == NULL)
c152c796
AM
11468 goto error_return;
11469
11470 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11471 flinfo.sections = (asection **) bfd_malloc (amt);
11472 if (flinfo.sections == NULL)
c152c796
AM
11473 goto error_return;
11474 }
11475
11476 if (max_sym_shndx_count != 0)
11477 {
11478 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11479 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11480 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11481 goto error_return;
11482 }
11483
11484 if (elf_hash_table (info)->tls_sec)
11485 {
11486 bfd_vma base, end = 0;
11487 asection *sec;
11488
11489 for (sec = elf_hash_table (info)->tls_sec;
11490 sec && (sec->flags & SEC_THREAD_LOCAL);
11491 sec = sec->next)
11492 {
3a800eb9 11493 bfd_size_type size = sec->size;
c152c796 11494
3a800eb9
AM
11495 if (size == 0
11496 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11497 {
91d6fa6a
NC
11498 struct bfd_link_order *ord = sec->map_tail.link_order;
11499
11500 if (ord != NULL)
11501 size = ord->offset + ord->size;
c152c796
AM
11502 }
11503 end = sec->vma + size;
11504 }
11505 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
11506 /* Only align end of TLS section if static TLS doesn't have special
11507 alignment requirements. */
11508 if (bed->static_tls_alignment == 1)
11509 end = align_power (end,
11510 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
11511 elf_hash_table (info)->tls_size = end - base;
11512 }
11513
0b52efa6
PB
11514 /* Reorder SHF_LINK_ORDER sections. */
11515 for (o = abfd->sections; o != NULL; o = o->next)
11516 {
11517 if (!elf_fixup_link_order (abfd, o))
11518 return FALSE;
11519 }
11520
2f0c68f2
CM
11521 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11522 return FALSE;
11523
c152c796
AM
11524 /* Since ELF permits relocations to be against local symbols, we
11525 must have the local symbols available when we do the relocations.
11526 Since we would rather only read the local symbols once, and we
11527 would rather not keep them in memory, we handle all the
11528 relocations for a single input file at the same time.
11529
11530 Unfortunately, there is no way to know the total number of local
11531 symbols until we have seen all of them, and the local symbol
11532 indices precede the global symbol indices. This means that when
11533 we are generating relocatable output, and we see a reloc against
11534 a global symbol, we can not know the symbol index until we have
11535 finished examining all the local symbols to see which ones we are
11536 going to output. To deal with this, we keep the relocations in
11537 memory, and don't output them until the end of the link. This is
11538 an unfortunate waste of memory, but I don't see a good way around
11539 it. Fortunately, it only happens when performing a relocatable
11540 link, which is not the common case. FIXME: If keep_memory is set
11541 we could write the relocs out and then read them again; I don't
11542 know how bad the memory loss will be. */
11543
c72f2fb2 11544 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11545 sub->output_has_begun = FALSE;
11546 for (o = abfd->sections; o != NULL; o = o->next)
11547 {
8423293d 11548 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11549 {
11550 if (p->type == bfd_indirect_link_order
11551 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11552 == bfd_target_elf_flavour)
11553 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11554 {
11555 if (! sub->output_has_begun)
11556 {
8b127cbc 11557 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11558 goto error_return;
11559 sub->output_has_begun = TRUE;
11560 }
11561 }
11562 else if (p->type == bfd_section_reloc_link_order
11563 || p->type == bfd_symbol_reloc_link_order)
11564 {
11565 if (! elf_reloc_link_order (abfd, info, o, p))
11566 goto error_return;
11567 }
11568 else
11569 {
11570 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11571 {
11572 if (p->type == bfd_indirect_link_order
11573 && (bfd_get_flavour (sub)
11574 == bfd_target_elf_flavour)
11575 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11576 != bed->s->elfclass))
11577 {
11578 const char *iclass, *oclass;
11579
aebf9be7 11580 switch (bed->s->elfclass)
351f65ca 11581 {
aebf9be7
NC
11582 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11583 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11584 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11585 default: abort ();
351f65ca 11586 }
aebf9be7
NC
11587
11588 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11589 {
aebf9be7
NC
11590 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11591 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11592 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11593 default: abort ();
351f65ca
L
11594 }
11595
11596 bfd_set_error (bfd_error_wrong_format);
11597 (*_bfd_error_handler)
11598 (_("%B: file class %s incompatible with %s"),
11599 sub, iclass, oclass);
11600 }
11601
11602 goto error_return;
11603 }
c152c796
AM
11604 }
11605 }
11606 }
11607
c0f00686
L
11608 /* Free symbol buffer if needed. */
11609 if (!info->reduce_memory_overheads)
11610 {
c72f2fb2 11611 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11612 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11613 && elf_tdata (sub)->symbuf)
c0f00686
L
11614 {
11615 free (elf_tdata (sub)->symbuf);
11616 elf_tdata (sub)->symbuf = NULL;
11617 }
11618 }
11619
c152c796
AM
11620 /* Output any global symbols that got converted to local in a
11621 version script or due to symbol visibility. We do this in a
11622 separate step since ELF requires all local symbols to appear
11623 prior to any global symbols. FIXME: We should only do this if
11624 some global symbols were, in fact, converted to become local.
11625 FIXME: Will this work correctly with the Irix 5 linker? */
11626 eoinfo.failed = FALSE;
8b127cbc 11627 eoinfo.flinfo = &flinfo;
c152c796 11628 eoinfo.localsyms = TRUE;
34a79995 11629 eoinfo.file_sym_done = FALSE;
7686d77d 11630 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11631 if (eoinfo.failed)
11632 return FALSE;
11633
4e617b1e
PB
11634 /* If backend needs to output some local symbols not present in the hash
11635 table, do it now. */
8539e4e8
AM
11636 if (bed->elf_backend_output_arch_local_syms
11637 && (info->strip != strip_all || emit_relocs))
4e617b1e 11638 {
6e0b88f1 11639 typedef int (*out_sym_func)
4e617b1e
PB
11640 (void *, const char *, Elf_Internal_Sym *, asection *,
11641 struct elf_link_hash_entry *);
11642
11643 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11644 (abfd, info, &flinfo,
11645 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11646 return FALSE;
11647 }
11648
c152c796
AM
11649 /* That wrote out all the local symbols. Finish up the symbol table
11650 with the global symbols. Even if we want to strip everything we
11651 can, we still need to deal with those global symbols that got
11652 converted to local in a version script. */
11653
11654 /* The sh_info field records the index of the first non local symbol. */
11655 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11656
11657 if (dynamic
cae1fbbb
L
11658 && elf_hash_table (info)->dynsym != NULL
11659 && (elf_hash_table (info)->dynsym->output_section
11660 != bfd_abs_section_ptr))
c152c796
AM
11661 {
11662 Elf_Internal_Sym sym;
cae1fbbb 11663 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
c152c796
AM
11664 long last_local = 0;
11665
11666 /* Write out the section symbols for the output sections. */
0e1862bb
L
11667 if (bfd_link_pic (info)
11668 || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
11669 {
11670 asection *s;
11671
11672 sym.st_size = 0;
11673 sym.st_name = 0;
11674 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11675 sym.st_other = 0;
35fc36a8 11676 sym.st_target_internal = 0;
c152c796
AM
11677
11678 for (s = abfd->sections; s != NULL; s = s->next)
11679 {
11680 int indx;
11681 bfd_byte *dest;
11682 long dynindx;
11683
c152c796 11684 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11685 if (dynindx <= 0)
11686 continue;
11687 indx = elf_section_data (s)->this_idx;
c152c796
AM
11688 BFD_ASSERT (indx > 0);
11689 sym.st_shndx = indx;
c0d5a53d
L
11690 if (! check_dynsym (abfd, &sym))
11691 return FALSE;
c152c796
AM
11692 sym.st_value = s->vma;
11693 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
11694 if (last_local < dynindx)
11695 last_local = dynindx;
c152c796
AM
11696 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11697 }
c152c796
AM
11698 }
11699
11700 /* Write out the local dynsyms. */
11701 if (elf_hash_table (info)->dynlocal)
11702 {
11703 struct elf_link_local_dynamic_entry *e;
11704 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11705 {
11706 asection *s;
11707 bfd_byte *dest;
11708
935bd1e0 11709 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11710 Note that we saved a word of storage and overwrote
11711 the original st_name with the dynstr_index. */
11712 sym = e->isym;
935bd1e0 11713 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11714
cb33740c
AM
11715 s = bfd_section_from_elf_index (e->input_bfd,
11716 e->isym.st_shndx);
11717 if (s != NULL)
c152c796 11718 {
c152c796
AM
11719 sym.st_shndx =
11720 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11721 if (! check_dynsym (abfd, &sym))
11722 return FALSE;
c152c796
AM
11723 sym.st_value = (s->output_section->vma
11724 + s->output_offset
11725 + e->isym.st_value);
11726 }
11727
11728 if (last_local < e->dynindx)
11729 last_local = e->dynindx;
11730
11731 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11732 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11733 }
11734 }
11735
cae1fbbb 11736 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
c152c796
AM
11737 last_local + 1;
11738 }
11739
11740 /* We get the global symbols from the hash table. */
11741 eoinfo.failed = FALSE;
11742 eoinfo.localsyms = FALSE;
8b127cbc 11743 eoinfo.flinfo = &flinfo;
7686d77d 11744 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11745 if (eoinfo.failed)
11746 return FALSE;
11747
11748 /* If backend needs to output some symbols not present in the hash
11749 table, do it now. */
8539e4e8
AM
11750 if (bed->elf_backend_output_arch_syms
11751 && (info->strip != strip_all || emit_relocs))
c152c796 11752 {
6e0b88f1 11753 typedef int (*out_sym_func)
c152c796
AM
11754 (void *, const char *, Elf_Internal_Sym *, asection *,
11755 struct elf_link_hash_entry *);
11756
11757 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
11758 (abfd, info, &flinfo,
11759 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
11760 return FALSE;
11761 }
11762
ef10c3ac
L
11763 /* Finalize the .strtab section. */
11764 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11765
11766 /* Swap out the .strtab section. */
11767 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
11768 return FALSE;
11769
11770 /* Now we know the size of the symtab section. */
c152c796
AM
11771 if (bfd_get_symcount (abfd) > 0)
11772 {
ee3b52e9
L
11773 /* Finish up and write out the symbol string table (.strtab)
11774 section. */
11775 Elf_Internal_Shdr *symstrtab_hdr;
8539e4e8
AM
11776 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11777
6a40cf0c
NC
11778 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11779 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
8539e4e8
AM
11780 {
11781 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11782 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11783 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11784 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11785 symtab_shndx_hdr->sh_size = amt;
11786
11787 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11788 off, TRUE);
11789
11790 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11791 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11792 return FALSE;
11793 }
ee3b52e9
L
11794
11795 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11796 /* sh_name was set in prep_headers. */
11797 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 11798 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 11799 symstrtab_hdr->sh_addr = 0;
ef10c3ac 11800 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
11801 symstrtab_hdr->sh_entsize = 0;
11802 symstrtab_hdr->sh_link = 0;
11803 symstrtab_hdr->sh_info = 0;
11804 /* sh_offset is set just below. */
11805 symstrtab_hdr->sh_addralign = 1;
11806
11807 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11808 off, TRUE);
11809 elf_next_file_pos (abfd) = off;
11810
c152c796 11811 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 11812 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
11813 return FALSE;
11814 }
11815
11816 /* Adjust the relocs to have the correct symbol indices. */
11817 for (o = abfd->sections; o != NULL; o = o->next)
11818 {
d4730f92 11819 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 11820 bfd_boolean sort;
c152c796
AM
11821 if ((o->flags & SEC_RELOC) == 0)
11822 continue;
11823
28dbcedc 11824 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3
AM
11825 if (esdo->rel.hdr != NULL
11826 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11827 return FALSE;
11828 if (esdo->rela.hdr != NULL
11829 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11830 return FALSE;
c152c796
AM
11831
11832 /* Set the reloc_count field to 0 to prevent write_relocs from
11833 trying to swap the relocs out itself. */
11834 o->reloc_count = 0;
11835 }
11836
11837 if (dynamic && info->combreloc && dynobj != NULL)
11838 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11839
11840 /* If we are linking against a dynamic object, or generating a
11841 shared library, finish up the dynamic linking information. */
11842 if (dynamic)
11843 {
11844 bfd_byte *dyncon, *dynconend;
11845
11846 /* Fix up .dynamic entries. */
3d4d4302 11847 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
11848 BFD_ASSERT (o != NULL);
11849
11850 dyncon = o->contents;
eea6121a 11851 dynconend = o->contents + o->size;
c152c796
AM
11852 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11853 {
11854 Elf_Internal_Dyn dyn;
11855 const char *name;
11856 unsigned int type;
11857
11858 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11859
11860 switch (dyn.d_tag)
11861 {
11862 default:
11863 continue;
11864 case DT_NULL:
11865 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11866 {
11867 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11868 {
11869 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11870 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11871 default: continue;
11872 }
11873 dyn.d_un.d_val = relativecount;
11874 relativecount = 0;
11875 break;
11876 }
11877 continue;
11878
11879 case DT_INIT:
11880 name = info->init_function;
11881 goto get_sym;
11882 case DT_FINI:
11883 name = info->fini_function;
11884 get_sym:
11885 {
11886 struct elf_link_hash_entry *h;
11887
11888 h = elf_link_hash_lookup (elf_hash_table (info), name,
11889 FALSE, FALSE, TRUE);
11890 if (h != NULL
11891 && (h->root.type == bfd_link_hash_defined
11892 || h->root.type == bfd_link_hash_defweak))
11893 {
bef26483 11894 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11895 o = h->root.u.def.section;
11896 if (o->output_section != NULL)
bef26483 11897 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11898 + o->output_offset);
11899 else
11900 {
11901 /* The symbol is imported from another shared
11902 library and does not apply to this one. */
bef26483 11903 dyn.d_un.d_ptr = 0;
c152c796
AM
11904 }
11905 break;
11906 }
11907 }
11908 continue;
11909
11910 case DT_PREINIT_ARRAYSZ:
11911 name = ".preinit_array";
4ade44b7 11912 goto get_out_size;
c152c796
AM
11913 case DT_INIT_ARRAYSZ:
11914 name = ".init_array";
4ade44b7 11915 goto get_out_size;
c152c796
AM
11916 case DT_FINI_ARRAYSZ:
11917 name = ".fini_array";
4ade44b7 11918 get_out_size:
c152c796
AM
11919 o = bfd_get_section_by_name (abfd, name);
11920 if (o == NULL)
11921 {
11922 (*_bfd_error_handler)
4ade44b7 11923 (_("could not find section %s"), name);
c152c796
AM
11924 goto error_return;
11925 }
eea6121a 11926 if (o->size == 0)
c152c796
AM
11927 (*_bfd_error_handler)
11928 (_("warning: %s section has zero size"), name);
eea6121a 11929 dyn.d_un.d_val = o->size;
c152c796
AM
11930 break;
11931
11932 case DT_PREINIT_ARRAY:
11933 name = ".preinit_array";
4ade44b7 11934 goto get_out_vma;
c152c796
AM
11935 case DT_INIT_ARRAY:
11936 name = ".init_array";
4ade44b7 11937 goto get_out_vma;
c152c796
AM
11938 case DT_FINI_ARRAY:
11939 name = ".fini_array";
4ade44b7
AM
11940 get_out_vma:
11941 o = bfd_get_section_by_name (abfd, name);
11942 goto do_vma;
c152c796
AM
11943
11944 case DT_HASH:
11945 name = ".hash";
11946 goto get_vma;
fdc90cb4
JJ
11947 case DT_GNU_HASH:
11948 name = ".gnu.hash";
11949 goto get_vma;
c152c796
AM
11950 case DT_STRTAB:
11951 name = ".dynstr";
11952 goto get_vma;
11953 case DT_SYMTAB:
11954 name = ".dynsym";
11955 goto get_vma;
11956 case DT_VERDEF:
11957 name = ".gnu.version_d";
11958 goto get_vma;
11959 case DT_VERNEED:
11960 name = ".gnu.version_r";
11961 goto get_vma;
11962 case DT_VERSYM:
11963 name = ".gnu.version";
11964 get_vma:
4ade44b7
AM
11965 o = bfd_get_linker_section (dynobj, name);
11966 do_vma:
c152c796
AM
11967 if (o == NULL)
11968 {
11969 (*_bfd_error_handler)
4ade44b7 11970 (_("could not find section %s"), name);
c152c796
AM
11971 goto error_return;
11972 }
894891db
NC
11973 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11974 {
11975 (*_bfd_error_handler)
11976 (_("warning: section '%s' is being made into a note"), name);
11977 bfd_set_error (bfd_error_nonrepresentable_section);
11978 goto error_return;
11979 }
4ade44b7 11980 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
11981 break;
11982
11983 case DT_REL:
11984 case DT_RELA:
11985 case DT_RELSZ:
11986 case DT_RELASZ:
11987 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11988 type = SHT_REL;
11989 else
11990 type = SHT_RELA;
11991 dyn.d_un.d_val = 0;
bef26483 11992 dyn.d_un.d_ptr = 0;
c152c796
AM
11993 for (i = 1; i < elf_numsections (abfd); i++)
11994 {
11995 Elf_Internal_Shdr *hdr;
11996
11997 hdr = elf_elfsections (abfd)[i];
11998 if (hdr->sh_type == type
11999 && (hdr->sh_flags & SHF_ALLOC) != 0)
12000 {
12001 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12002 dyn.d_un.d_val += hdr->sh_size;
12003 else
12004 {
bef26483
AM
12005 if (dyn.d_un.d_ptr == 0
12006 || hdr->sh_addr < dyn.d_un.d_ptr)
12007 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
12008 }
12009 }
12010 }
12011 break;
12012 }
12013 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12014 }
12015 }
12016
12017 /* If we have created any dynamic sections, then output them. */
12018 if (dynobj != NULL)
12019 {
12020 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12021 goto error_return;
12022
943284cc 12023 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12024 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12025 || info->error_textrel)
3d4d4302 12026 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12027 {
12028 bfd_byte *dyncon, *dynconend;
12029
943284cc
DJ
12030 dyncon = o->contents;
12031 dynconend = o->contents + o->size;
12032 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12033 {
12034 Elf_Internal_Dyn dyn;
12035
12036 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12037
12038 if (dyn.d_tag == DT_TEXTREL)
12039 {
c192a133
AM
12040 if (info->error_textrel)
12041 info->callbacks->einfo
12042 (_("%P%X: read-only segment has dynamic relocations.\n"));
12043 else
12044 info->callbacks->einfo
12045 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12046 break;
12047 }
12048 }
12049 }
12050
c152c796
AM
12051 for (o = dynobj->sections; o != NULL; o = o->next)
12052 {
12053 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12054 || o->size == 0
c152c796
AM
12055 || o->output_section == bfd_abs_section_ptr)
12056 continue;
12057 if ((o->flags & SEC_LINKER_CREATED) == 0)
12058 {
12059 /* At this point, we are only interested in sections
12060 created by _bfd_elf_link_create_dynamic_sections. */
12061 continue;
12062 }
3722b82f
AM
12063 if (elf_hash_table (info)->stab_info.stabstr == o)
12064 continue;
eea6121a
AM
12065 if (elf_hash_table (info)->eh_info.hdr_sec == o)
12066 continue;
3d4d4302 12067 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12068 {
12069 if (! bfd_set_section_contents (abfd, o->output_section,
12070 o->contents,
37b01f6a
DG
12071 (file_ptr) o->output_offset
12072 * bfd_octets_per_byte (abfd),
eea6121a 12073 o->size))
c152c796
AM
12074 goto error_return;
12075 }
12076 else
12077 {
12078 /* The contents of the .dynstr section are actually in a
12079 stringtab. */
8539e4e8
AM
12080 file_ptr off;
12081
c152c796
AM
12082 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12083 if (bfd_seek (abfd, off, SEEK_SET) != 0
12084 || ! _bfd_elf_strtab_emit (abfd,
12085 elf_hash_table (info)->dynstr))
12086 goto error_return;
12087 }
12088 }
12089 }
12090
0e1862bb 12091 if (bfd_link_relocatable (info))
c152c796
AM
12092 {
12093 bfd_boolean failed = FALSE;
12094
12095 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12096 if (failed)
12097 goto error_return;
12098 }
12099
12100 /* If we have optimized stabs strings, output them. */
3722b82f 12101 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
12102 {
12103 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
12104 goto error_return;
12105 }
12106
9f7c3e5e
AM
12107 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12108 goto error_return;
c152c796 12109
9f7c3e5e 12110 elf_final_link_free (abfd, &flinfo);
c152c796 12111
12bd6957 12112 elf_linker (abfd) = TRUE;
c152c796 12113
104d59d1
JM
12114 if (attr_section)
12115 {
a50b1753 12116 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12117 if (contents == NULL)
d0f16d5e 12118 return FALSE; /* Bail out and fail. */
104d59d1
JM
12119 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12120 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12121 free (contents);
12122 }
12123
c152c796
AM
12124 return TRUE;
12125
12126 error_return:
9f7c3e5e 12127 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12128 return FALSE;
12129}
12130\f
5241d853
RS
12131/* Initialize COOKIE for input bfd ABFD. */
12132
12133static bfd_boolean
12134init_reloc_cookie (struct elf_reloc_cookie *cookie,
12135 struct bfd_link_info *info, bfd *abfd)
12136{
12137 Elf_Internal_Shdr *symtab_hdr;
12138 const struct elf_backend_data *bed;
12139
12140 bed = get_elf_backend_data (abfd);
12141 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12142
12143 cookie->abfd = abfd;
12144 cookie->sym_hashes = elf_sym_hashes (abfd);
12145 cookie->bad_symtab = elf_bad_symtab (abfd);
12146 if (cookie->bad_symtab)
12147 {
12148 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12149 cookie->extsymoff = 0;
12150 }
12151 else
12152 {
12153 cookie->locsymcount = symtab_hdr->sh_info;
12154 cookie->extsymoff = symtab_hdr->sh_info;
12155 }
12156
12157 if (bed->s->arch_size == 32)
12158 cookie->r_sym_shift = 8;
12159 else
12160 cookie->r_sym_shift = 32;
12161
12162 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12163 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12164 {
12165 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12166 cookie->locsymcount, 0,
12167 NULL, NULL, NULL);
12168 if (cookie->locsyms == NULL)
12169 {
12170 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12171 return FALSE;
12172 }
12173 if (info->keep_memory)
12174 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12175 }
12176 return TRUE;
12177}
12178
12179/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12180
12181static void
12182fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12183{
12184 Elf_Internal_Shdr *symtab_hdr;
12185
12186 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12187 if (cookie->locsyms != NULL
12188 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12189 free (cookie->locsyms);
12190}
12191
12192/* Initialize the relocation information in COOKIE for input section SEC
12193 of input bfd ABFD. */
12194
12195static bfd_boolean
12196init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12197 struct bfd_link_info *info, bfd *abfd,
12198 asection *sec)
12199{
12200 const struct elf_backend_data *bed;
12201
12202 if (sec->reloc_count == 0)
12203 {
12204 cookie->rels = NULL;
12205 cookie->relend = NULL;
12206 }
12207 else
12208 {
12209 bed = get_elf_backend_data (abfd);
12210
12211 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12212 info->keep_memory);
12213 if (cookie->rels == NULL)
12214 return FALSE;
12215 cookie->rel = cookie->rels;
12216 cookie->relend = (cookie->rels
12217 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12218 }
12219 cookie->rel = cookie->rels;
12220 return TRUE;
12221}
12222
12223/* Free the memory allocated by init_reloc_cookie_rels,
12224 if appropriate. */
12225
12226static void
12227fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12228 asection *sec)
12229{
12230 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12231 free (cookie->rels);
12232}
12233
12234/* Initialize the whole of COOKIE for input section SEC. */
12235
12236static bfd_boolean
12237init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12238 struct bfd_link_info *info,
12239 asection *sec)
12240{
12241 if (!init_reloc_cookie (cookie, info, sec->owner))
12242 goto error1;
12243 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12244 goto error2;
12245 return TRUE;
12246
12247 error2:
12248 fini_reloc_cookie (cookie, sec->owner);
12249 error1:
12250 return FALSE;
12251}
12252
12253/* Free the memory allocated by init_reloc_cookie_for_section,
12254 if appropriate. */
12255
12256static void
12257fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12258 asection *sec)
12259{
12260 fini_reloc_cookie_rels (cookie, sec);
12261 fini_reloc_cookie (cookie, sec->owner);
12262}
12263\f
c152c796
AM
12264/* Garbage collect unused sections. */
12265
07adf181
AM
12266/* Default gc_mark_hook. */
12267
12268asection *
12269_bfd_elf_gc_mark_hook (asection *sec,
12270 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12271 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12272 struct elf_link_hash_entry *h,
12273 Elf_Internal_Sym *sym)
12274{
12275 if (h != NULL)
12276 {
12277 switch (h->root.type)
12278 {
12279 case bfd_link_hash_defined:
12280 case bfd_link_hash_defweak:
12281 return h->root.u.def.section;
12282
12283 case bfd_link_hash_common:
12284 return h->root.u.c.p->section;
12285
12286 default:
12287 break;
12288 }
12289 }
12290 else
12291 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12292
12293 return NULL;
12294}
12295
a6a4679f
AM
12296/* For undefined __start_<name> and __stop_<name> symbols, return the
12297 first input section matching <name>. Return NULL otherwise. */
12298
12299asection *
12300_bfd_elf_is_start_stop (const struct bfd_link_info *info,
12301 struct elf_link_hash_entry *h)
12302{
12303 asection *s;
12304 const char *sec_name;
12305
12306 if (h->root.type != bfd_link_hash_undefined
12307 && h->root.type != bfd_link_hash_undefweak)
12308 return NULL;
12309
12310 s = h->root.u.undef.section;
12311 if (s != NULL)
12312 {
12313 if (s == (asection *) 0 - 1)
12314 return NULL;
12315 return s;
12316 }
12317
12318 sec_name = NULL;
12319 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12320 sec_name = h->root.root.string + 8;
12321 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12322 sec_name = h->root.root.string + 7;
12323
12324 if (sec_name != NULL && *sec_name != '\0')
12325 {
12326 bfd *i;
12327
12328 for (i = info->input_bfds; i != NULL; i = i->link.next)
12329 {
12330 s = bfd_get_section_by_name (i, sec_name);
12331 if (s != NULL)
12332 {
12333 h->root.u.undef.section = s;
12334 break;
12335 }
12336 }
12337 }
12338
12339 if (s == NULL)
12340 h->root.u.undef.section = (asection *) 0 - 1;
12341
12342 return s;
12343}
12344
5241d853
RS
12345/* COOKIE->rel describes a relocation against section SEC, which is
12346 a section we've decided to keep. Return the section that contains
12347 the relocation symbol, or NULL if no section contains it. */
12348
12349asection *
12350_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12351 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12352 struct elf_reloc_cookie *cookie,
12353 bfd_boolean *start_stop)
5241d853
RS
12354{
12355 unsigned long r_symndx;
12356 struct elf_link_hash_entry *h;
12357
12358 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12359 if (r_symndx == STN_UNDEF)
5241d853
RS
12360 return NULL;
12361
12362 if (r_symndx >= cookie->locsymcount
12363 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12364 {
12365 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12366 if (h == NULL)
12367 {
12368 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12369 sec->owner);
12370 return NULL;
12371 }
5241d853
RS
12372 while (h->root.type == bfd_link_hash_indirect
12373 || h->root.type == bfd_link_hash_warning)
12374 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12375 h->mark = 1;
4e6b54a6
AM
12376 /* If this symbol is weak and there is a non-weak definition, we
12377 keep the non-weak definition because many backends put
12378 dynamic reloc info on the non-weak definition for code
12379 handling copy relocs. */
12380 if (h->u.weakdef != NULL)
12381 h->u.weakdef->mark = 1;
1cce69b9 12382
a6a4679f 12383 if (start_stop != NULL)
1cce69b9
AM
12384 {
12385 /* To work around a glibc bug, mark all XXX input sections
12386 when there is an as yet undefined reference to __start_XXX
12387 or __stop_XXX symbols. The linker will later define such
12388 symbols for orphan input sections that have a name
12389 representable as a C identifier. */
a6a4679f 12390 asection *s = _bfd_elf_is_start_stop (info, h);
1cce69b9 12391
a6a4679f 12392 if (s != NULL)
1cce69b9 12393 {
a6a4679f
AM
12394 *start_stop = !s->gc_mark;
12395 return s;
1cce69b9
AM
12396 }
12397 }
12398
5241d853
RS
12399 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12400 }
12401
12402 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12403 &cookie->locsyms[r_symndx]);
12404}
12405
12406/* COOKIE->rel describes a relocation against section SEC, which is
12407 a section we've decided to keep. Mark the section that contains
9d0a14d3 12408 the relocation symbol. */
5241d853
RS
12409
12410bfd_boolean
12411_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12412 asection *sec,
12413 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12414 struct elf_reloc_cookie *cookie)
5241d853
RS
12415{
12416 asection *rsec;
1cce69b9 12417 bfd_boolean start_stop = FALSE;
5241d853 12418
1cce69b9
AM
12419 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12420 while (rsec != NULL)
5241d853 12421 {
1cce69b9
AM
12422 if (!rsec->gc_mark)
12423 {
12424 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12425 || (rsec->owner->flags & DYNAMIC) != 0)
12426 rsec->gc_mark = 1;
12427 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12428 return FALSE;
12429 }
12430 if (!start_stop)
12431 break;
199af150 12432 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12433 }
12434 return TRUE;
12435}
12436
07adf181
AM
12437/* The mark phase of garbage collection. For a given section, mark
12438 it and any sections in this section's group, and all the sections
12439 which define symbols to which it refers. */
12440
ccfa59ea
AM
12441bfd_boolean
12442_bfd_elf_gc_mark (struct bfd_link_info *info,
12443 asection *sec,
6a5bb875 12444 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12445{
12446 bfd_boolean ret;
9d0a14d3 12447 asection *group_sec, *eh_frame;
c152c796
AM
12448
12449 sec->gc_mark = 1;
12450
12451 /* Mark all the sections in the group. */
12452 group_sec = elf_section_data (sec)->next_in_group;
12453 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12454 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12455 return FALSE;
12456
12457 /* Look through the section relocs. */
12458 ret = TRUE;
9d0a14d3
RS
12459 eh_frame = elf_eh_frame_section (sec->owner);
12460 if ((sec->flags & SEC_RELOC) != 0
12461 && sec->reloc_count > 0
12462 && sec != eh_frame)
c152c796 12463 {
5241d853 12464 struct elf_reloc_cookie cookie;
c152c796 12465
5241d853
RS
12466 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12467 ret = FALSE;
c152c796 12468 else
c152c796 12469 {
5241d853 12470 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12471 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12472 {
12473 ret = FALSE;
12474 break;
12475 }
12476 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12477 }
12478 }
9d0a14d3
RS
12479
12480 if (ret && eh_frame && elf_fde_list (sec))
12481 {
12482 struct elf_reloc_cookie cookie;
12483
12484 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12485 ret = FALSE;
12486 else
12487 {
12488 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12489 gc_mark_hook, &cookie))
12490 ret = FALSE;
12491 fini_reloc_cookie_for_section (&cookie, eh_frame);
12492 }
12493 }
12494
2f0c68f2
CM
12495 eh_frame = elf_section_eh_frame_entry (sec);
12496 if (ret && eh_frame && !eh_frame->gc_mark)
12497 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12498 ret = FALSE;
12499
c152c796
AM
12500 return ret;
12501}
12502
3c758495
TG
12503/* Scan and mark sections in a special or debug section group. */
12504
12505static void
12506_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12507{
12508 /* Point to first section of section group. */
12509 asection *ssec;
12510 /* Used to iterate the section group. */
12511 asection *msec;
12512
12513 bfd_boolean is_special_grp = TRUE;
12514 bfd_boolean is_debug_grp = TRUE;
12515
12516 /* First scan to see if group contains any section other than debug
12517 and special section. */
12518 ssec = msec = elf_next_in_group (grp);
12519 do
12520 {
12521 if ((msec->flags & SEC_DEBUGGING) == 0)
12522 is_debug_grp = FALSE;
12523
12524 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12525 is_special_grp = FALSE;
12526
12527 msec = elf_next_in_group (msec);
12528 }
12529 while (msec != ssec);
12530
12531 /* If this is a pure debug section group or pure special section group,
12532 keep all sections in this group. */
12533 if (is_debug_grp || is_special_grp)
12534 {
12535 do
12536 {
12537 msec->gc_mark = 1;
12538 msec = elf_next_in_group (msec);
12539 }
12540 while (msec != ssec);
12541 }
12542}
12543
7f6ab9f8
AM
12544/* Keep debug and special sections. */
12545
12546bfd_boolean
12547_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12548 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12549{
12550 bfd *ibfd;
12551
c72f2fb2 12552 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12553 {
12554 asection *isec;
12555 bfd_boolean some_kept;
b40bf0a2 12556 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12557
12558 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12559 continue;
12560
b40bf0a2
NC
12561 /* Ensure all linker created sections are kept,
12562 see if any other section is already marked,
12563 and note if we have any fragmented debug sections. */
12564 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12565 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12566 {
12567 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12568 isec->gc_mark = 1;
12569 else if (isec->gc_mark)
12570 some_kept = TRUE;
b40bf0a2
NC
12571
12572 if (debug_frag_seen == FALSE
12573 && (isec->flags & SEC_DEBUGGING)
12574 && CONST_STRNEQ (isec->name, ".debug_line."))
12575 debug_frag_seen = TRUE;
7f6ab9f8
AM
12576 }
12577
12578 /* If no section in this file will be kept, then we can
b40bf0a2 12579 toss out the debug and special sections. */
7f6ab9f8
AM
12580 if (!some_kept)
12581 continue;
12582
12583 /* Keep debug and special sections like .comment when they are
3c758495
TG
12584 not part of a group. Also keep section groups that contain
12585 just debug sections or special sections. */
7f6ab9f8 12586 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12587 {
12588 if ((isec->flags & SEC_GROUP) != 0)
12589 _bfd_elf_gc_mark_debug_special_section_group (isec);
12590 else if (((isec->flags & SEC_DEBUGGING) != 0
12591 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12592 && elf_next_in_group (isec) == NULL)
12593 isec->gc_mark = 1;
12594 }
b40bf0a2
NC
12595
12596 if (! debug_frag_seen)
12597 continue;
12598
12599 /* Look for CODE sections which are going to be discarded,
12600 and find and discard any fragmented debug sections which
12601 are associated with that code section. */
12602 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12603 if ((isec->flags & SEC_CODE) != 0
12604 && isec->gc_mark == 0)
12605 {
12606 unsigned int ilen;
12607 asection *dsec;
12608
12609 ilen = strlen (isec->name);
12610
12611 /* Association is determined by the name of the debug section
12612 containing the name of the code section as a suffix. For
12613 example .debug_line.text.foo is a debug section associated
12614 with .text.foo. */
12615 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12616 {
12617 unsigned int dlen;
12618
12619 if (dsec->gc_mark == 0
12620 || (dsec->flags & SEC_DEBUGGING) == 0)
12621 continue;
12622
12623 dlen = strlen (dsec->name);
12624
12625 if (dlen > ilen
12626 && strncmp (dsec->name + (dlen - ilen),
12627 isec->name, ilen) == 0)
12628 {
12629 dsec->gc_mark = 0;
b40bf0a2
NC
12630 }
12631 }
12632 }
7f6ab9f8
AM
12633 }
12634 return TRUE;
12635}
12636
c152c796
AM
12637/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12638
c17d87de
NC
12639struct elf_gc_sweep_symbol_info
12640{
ccabcbe5
AM
12641 struct bfd_link_info *info;
12642 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12643 bfd_boolean);
12644};
12645
c152c796 12646static bfd_boolean
ccabcbe5 12647elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 12648{
1d5316ab
AM
12649 if (!h->mark
12650 && (((h->root.type == bfd_link_hash_defined
12651 || h->root.type == bfd_link_hash_defweak)
c4621b33 12652 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6673f753 12653 && h->root.u.def.section->gc_mark))
1d5316ab
AM
12654 || h->root.type == bfd_link_hash_undefined
12655 || h->root.type == bfd_link_hash_undefweak))
12656 {
12657 struct elf_gc_sweep_symbol_info *inf;
12658
12659 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 12660 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
12661 h->def_regular = 0;
12662 h->ref_regular = 0;
12663 h->ref_regular_nonweak = 0;
ccabcbe5 12664 }
c152c796
AM
12665
12666 return TRUE;
12667}
12668
12669/* The sweep phase of garbage collection. Remove all garbage sections. */
12670
12671typedef bfd_boolean (*gc_sweep_hook_fn)
12672 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12673
12674static bfd_boolean
ccabcbe5 12675elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12676{
12677 bfd *sub;
ccabcbe5
AM
12678 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12679 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12680 unsigned long section_sym_count;
12681 struct elf_gc_sweep_symbol_info sweep_info;
c152c796 12682
c72f2fb2 12683 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12684 {
12685 asection *o;
12686
b19a8f85
L
12687 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12688 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12689 continue;
12690
12691 for (o = sub->sections; o != NULL; o = o->next)
12692 {
a33dafc3
L
12693 /* When any section in a section group is kept, we keep all
12694 sections in the section group. If the first member of
12695 the section group is excluded, we will also exclude the
12696 group section. */
12697 if (o->flags & SEC_GROUP)
12698 {
12699 asection *first = elf_next_in_group (o);
12700 o->gc_mark = first->gc_mark;
12701 }
c152c796 12702
1e7eae0d 12703 if (o->gc_mark)
c152c796
AM
12704 continue;
12705
12706 /* Skip sweeping sections already excluded. */
12707 if (o->flags & SEC_EXCLUDE)
12708 continue;
12709
12710 /* Since this is early in the link process, it is simple
12711 to remove a section from the output. */
12712 o->flags |= SEC_EXCLUDE;
12713
c55fe096 12714 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
12715 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12716
c152c796
AM
12717 /* But we also have to update some of the relocation
12718 info we collected before. */
12719 if (gc_sweep_hook
e8aaee2a 12720 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12721 && o->reloc_count != 0
12722 && !((info->strip == strip_all || info->strip == strip_debugger)
12723 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12724 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12725 {
12726 Elf_Internal_Rela *internal_relocs;
12727 bfd_boolean r;
12728
12729 internal_relocs
12730 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12731 info->keep_memory);
12732 if (internal_relocs == NULL)
12733 return FALSE;
12734
12735 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12736
12737 if (elf_section_data (o)->relocs != internal_relocs)
12738 free (internal_relocs);
12739
12740 if (!r)
12741 return FALSE;
12742 }
12743 }
12744 }
12745
12746 /* Remove the symbols that were in the swept sections from the dynamic
12747 symbol table. GCFIXME: Anyone know how to get them out of the
12748 static symbol table as well? */
ccabcbe5
AM
12749 sweep_info.info = info;
12750 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12751 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12752 &sweep_info);
c152c796 12753
ccabcbe5 12754 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
12755 return TRUE;
12756}
12757
12758/* Propagate collected vtable information. This is called through
12759 elf_link_hash_traverse. */
12760
12761static bfd_boolean
12762elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12763{
c152c796 12764 /* Those that are not vtables. */
f6e332e6 12765 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12766 return TRUE;
12767
12768 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12769 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12770 return TRUE;
12771
12772 /* If we've already been done, exit. */
f6e332e6 12773 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12774 return TRUE;
12775
12776 /* Make sure the parent's table is up to date. */
f6e332e6 12777 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 12778
f6e332e6 12779 if (h->vtable->used == NULL)
c152c796
AM
12780 {
12781 /* None of this table's entries were referenced. Re-use the
12782 parent's table. */
f6e332e6
AM
12783 h->vtable->used = h->vtable->parent->vtable->used;
12784 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
12785 }
12786 else
12787 {
12788 size_t n;
12789 bfd_boolean *cu, *pu;
12790
12791 /* Or the parent's entries into ours. */
f6e332e6 12792 cu = h->vtable->used;
c152c796 12793 cu[-1] = TRUE;
f6e332e6 12794 pu = h->vtable->parent->vtable->used;
c152c796
AM
12795 if (pu != NULL)
12796 {
12797 const struct elf_backend_data *bed;
12798 unsigned int log_file_align;
12799
12800 bed = get_elf_backend_data (h->root.u.def.section->owner);
12801 log_file_align = bed->s->log_file_align;
f6e332e6 12802 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
12803 while (n--)
12804 {
12805 if (*pu)
12806 *cu = TRUE;
12807 pu++;
12808 cu++;
12809 }
12810 }
12811 }
12812
12813 return TRUE;
12814}
12815
12816static bfd_boolean
12817elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12818{
12819 asection *sec;
12820 bfd_vma hstart, hend;
12821 Elf_Internal_Rela *relstart, *relend, *rel;
12822 const struct elf_backend_data *bed;
12823 unsigned int log_file_align;
12824
c152c796
AM
12825 /* Take care of both those symbols that do not describe vtables as
12826 well as those that are not loaded. */
f6e332e6 12827 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12828 return TRUE;
12829
12830 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12831 || h->root.type == bfd_link_hash_defweak);
12832
12833 sec = h->root.u.def.section;
12834 hstart = h->root.u.def.value;
12835 hend = hstart + h->size;
12836
12837 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12838 if (!relstart)
12839 return *(bfd_boolean *) okp = FALSE;
12840 bed = get_elf_backend_data (sec->owner);
12841 log_file_align = bed->s->log_file_align;
12842
12843 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12844
12845 for (rel = relstart; rel < relend; ++rel)
12846 if (rel->r_offset >= hstart && rel->r_offset < hend)
12847 {
12848 /* If the entry is in use, do nothing. */
f6e332e6
AM
12849 if (h->vtable->used
12850 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
12851 {
12852 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 12853 if (h->vtable->used[entry])
c152c796
AM
12854 continue;
12855 }
12856 /* Otherwise, kill it. */
12857 rel->r_offset = rel->r_info = rel->r_addend = 0;
12858 }
12859
12860 return TRUE;
12861}
12862
87538722
AM
12863/* Mark sections containing dynamically referenced symbols. When
12864 building shared libraries, we must assume that any visible symbol is
12865 referenced. */
715df9b8 12866
64d03ab5
AM
12867bfd_boolean
12868bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 12869{
87538722 12870 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 12871 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 12872
715df9b8
EB
12873 if ((h->root.type == bfd_link_hash_defined
12874 || h->root.type == bfd_link_hash_defweak)
87538722 12875 && (h->ref_dynamic
c4621b33 12876 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 12877 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 12878 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 12879 && (!bfd_link_executable (info)
b407645f
AM
12880 || info->export_dynamic
12881 || (h->dynamic
12882 && d != NULL
12883 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 12884 && (h->versioned >= versioned
54e8959c
L
12885 || !bfd_hide_sym_by_version (info->version_info,
12886 h->root.root.string)))))
715df9b8
EB
12887 h->root.u.def.section->flags |= SEC_KEEP;
12888
12889 return TRUE;
12890}
3b36f7e6 12891
74f0fb50
AM
12892/* Keep all sections containing symbols undefined on the command-line,
12893 and the section containing the entry symbol. */
12894
12895void
12896_bfd_elf_gc_keep (struct bfd_link_info *info)
12897{
12898 struct bfd_sym_chain *sym;
12899
12900 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12901 {
12902 struct elf_link_hash_entry *h;
12903
12904 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12905 FALSE, FALSE, FALSE);
12906
12907 if (h != NULL
12908 && (h->root.type == bfd_link_hash_defined
12909 || h->root.type == bfd_link_hash_defweak)
12910 && !bfd_is_abs_section (h->root.u.def.section))
12911 h->root.u.def.section->flags |= SEC_KEEP;
12912 }
12913}
12914
2f0c68f2
CM
12915bfd_boolean
12916bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12917 struct bfd_link_info *info)
12918{
12919 bfd *ibfd = info->input_bfds;
12920
12921 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12922 {
12923 asection *sec;
12924 struct elf_reloc_cookie cookie;
12925
12926 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12927 continue;
12928
12929 if (!init_reloc_cookie (&cookie, info, ibfd))
12930 return FALSE;
12931
12932 for (sec = ibfd->sections; sec; sec = sec->next)
12933 {
12934 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12935 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12936 {
12937 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12938 fini_reloc_cookie_rels (&cookie, sec);
12939 }
12940 }
12941 }
12942 return TRUE;
12943}
12944
c152c796
AM
12945/* Do mark and sweep of unused sections. */
12946
12947bfd_boolean
12948bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12949{
12950 bfd_boolean ok = TRUE;
12951 bfd *sub;
6a5bb875 12952 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 12953 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 12954 struct elf_link_hash_table *htab;
c152c796 12955
64d03ab5 12956 if (!bed->can_gc_sections
715df9b8 12957 || !is_elf_hash_table (info->hash))
c152c796
AM
12958 {
12959 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12960 return TRUE;
12961 }
12962
74f0fb50 12963 bed->gc_keep (info);
da44f4e5 12964 htab = elf_hash_table (info);
74f0fb50 12965
9d0a14d3
RS
12966 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12967 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
12968 for (sub = info->input_bfds;
12969 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12970 sub = sub->link.next)
9d0a14d3
RS
12971 {
12972 asection *sec;
12973 struct elf_reloc_cookie cookie;
12974
12975 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 12976 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
12977 {
12978 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
12979 if (elf_section_data (sec)->sec_info
12980 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
12981 elf_eh_frame_section (sub) = sec;
12982 fini_reloc_cookie_for_section (&cookie, sec);
199af150 12983 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
12984 }
12985 }
9d0a14d3 12986
c152c796 12987 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 12988 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
12989 if (!ok)
12990 return FALSE;
12991
12992 /* Kill the vtable relocations that were not used. */
da44f4e5 12993 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
12994 if (!ok)
12995 return FALSE;
12996
715df9b8 12997 /* Mark dynamically referenced symbols. */
da44f4e5
AM
12998 if (htab->dynamic_sections_created)
12999 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13000
715df9b8 13001 /* Grovel through relocs to find out who stays ... */
64d03ab5 13002 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13003 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13004 {
13005 asection *o;
13006
b19a8f85
L
13007 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13008 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13009 continue;
13010
7f6ab9f8
AM
13011 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13012 Also treat note sections as a root, if the section is not part
13013 of a group. */
c152c796 13014 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13015 if (!o->gc_mark
13016 && (o->flags & SEC_EXCLUDE) == 0
24007750 13017 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13018 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13019 && elf_next_in_group (o) == NULL )))
13020 {
13021 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13022 return FALSE;
13023 }
c152c796
AM
13024 }
13025
6a5bb875 13026 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13027 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13028
c152c796 13029 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13030 return elf_gc_sweep (abfd, info);
c152c796
AM
13031}
13032\f
13033/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13034
13035bfd_boolean
13036bfd_elf_gc_record_vtinherit (bfd *abfd,
13037 asection *sec,
13038 struct elf_link_hash_entry *h,
13039 bfd_vma offset)
13040{
13041 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13042 struct elf_link_hash_entry **search, *child;
13043 bfd_size_type extsymcount;
13044 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13045
13046 /* The sh_info field of the symtab header tells us where the
13047 external symbols start. We don't care about the local symbols at
13048 this point. */
13049 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13050 if (!elf_bad_symtab (abfd))
13051 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13052
13053 sym_hashes = elf_sym_hashes (abfd);
13054 sym_hashes_end = sym_hashes + extsymcount;
13055
13056 /* Hunt down the child symbol, which is in this section at the same
13057 offset as the relocation. */
13058 for (search = sym_hashes; search != sym_hashes_end; ++search)
13059 {
13060 if ((child = *search) != NULL
13061 && (child->root.type == bfd_link_hash_defined
13062 || child->root.type == bfd_link_hash_defweak)
13063 && child->root.u.def.section == sec
13064 && child->root.u.def.value == offset)
13065 goto win;
13066 }
13067
d003868e
AM
13068 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
13069 abfd, sec, (unsigned long) offset);
c152c796
AM
13070 bfd_set_error (bfd_error_invalid_operation);
13071 return FALSE;
13072
13073 win:
f6e332e6
AM
13074 if (!child->vtable)
13075 {
ca4be51c
AM
13076 child->vtable = ((struct elf_link_virtual_table_entry *)
13077 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
13078 if (!child->vtable)
13079 return FALSE;
13080 }
c152c796
AM
13081 if (!h)
13082 {
13083 /* This *should* only be the absolute section. It could potentially
13084 be that someone has defined a non-global vtable though, which
13085 would be bad. It isn't worth paging in the local symbols to be
13086 sure though; that case should simply be handled by the assembler. */
13087
f6e332e6 13088 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13089 }
13090 else
f6e332e6 13091 child->vtable->parent = h;
c152c796
AM
13092
13093 return TRUE;
13094}
13095
13096/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13097
13098bfd_boolean
13099bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13100 asection *sec ATTRIBUTE_UNUSED,
13101 struct elf_link_hash_entry *h,
13102 bfd_vma addend)
13103{
13104 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13105 unsigned int log_file_align = bed->s->log_file_align;
13106
f6e332e6
AM
13107 if (!h->vtable)
13108 {
ca4be51c
AM
13109 h->vtable = ((struct elf_link_virtual_table_entry *)
13110 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
13111 if (!h->vtable)
13112 return FALSE;
13113 }
13114
13115 if (addend >= h->vtable->size)
c152c796
AM
13116 {
13117 size_t size, bytes, file_align;
f6e332e6 13118 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
13119
13120 /* While the symbol is undefined, we have to be prepared to handle
13121 a zero size. */
13122 file_align = 1 << log_file_align;
13123 if (h->root.type == bfd_link_hash_undefined)
13124 size = addend + file_align;
13125 else
13126 {
13127 size = h->size;
13128 if (addend >= size)
13129 {
13130 /* Oops! We've got a reference past the defined end of
13131 the table. This is probably a bug -- shall we warn? */
13132 size = addend + file_align;
13133 }
13134 }
13135 size = (size + file_align - 1) & -file_align;
13136
13137 /* Allocate one extra entry for use as a "done" flag for the
13138 consolidation pass. */
13139 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13140
13141 if (ptr)
13142 {
a50b1753 13143 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13144
13145 if (ptr != NULL)
13146 {
13147 size_t oldbytes;
13148
f6e332e6 13149 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
13150 * sizeof (bfd_boolean));
13151 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13152 }
13153 }
13154 else
a50b1753 13155 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13156
13157 if (ptr == NULL)
13158 return FALSE;
13159
13160 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
13161 h->vtable->used = ptr + 1;
13162 h->vtable->size = size;
c152c796
AM
13163 }
13164
f6e332e6 13165 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13166
13167 return TRUE;
13168}
13169
ae17ab41
CM
13170/* Map an ELF section header flag to its corresponding string. */
13171typedef struct
13172{
13173 char *flag_name;
13174 flagword flag_value;
13175} elf_flags_to_name_table;
13176
13177static elf_flags_to_name_table elf_flags_to_names [] =
13178{
13179 { "SHF_WRITE", SHF_WRITE },
13180 { "SHF_ALLOC", SHF_ALLOC },
13181 { "SHF_EXECINSTR", SHF_EXECINSTR },
13182 { "SHF_MERGE", SHF_MERGE },
13183 { "SHF_STRINGS", SHF_STRINGS },
13184 { "SHF_INFO_LINK", SHF_INFO_LINK},
13185 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13186 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13187 { "SHF_GROUP", SHF_GROUP },
13188 { "SHF_TLS", SHF_TLS },
13189 { "SHF_MASKOS", SHF_MASKOS },
13190 { "SHF_EXCLUDE", SHF_EXCLUDE },
13191};
13192
b9c361e0
JL
13193/* Returns TRUE if the section is to be included, otherwise FALSE. */
13194bfd_boolean
ae17ab41 13195bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13196 struct flag_info *flaginfo,
b9c361e0 13197 asection *section)
ae17ab41 13198{
8b127cbc 13199 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13200
8b127cbc 13201 if (!flaginfo->flags_initialized)
ae17ab41 13202 {
8b127cbc
AM
13203 bfd *obfd = info->output_bfd;
13204 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13205 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13206 int with_hex = 0;
13207 int without_hex = 0;
13208
8b127cbc 13209 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13210 {
b9c361e0 13211 unsigned i;
8b127cbc 13212 flagword (*lookup) (char *);
ae17ab41 13213
8b127cbc
AM
13214 lookup = bed->elf_backend_lookup_section_flags_hook;
13215 if (lookup != NULL)
ae17ab41 13216 {
8b127cbc 13217 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13218
13219 if (hexval != 0)
13220 {
13221 if (tf->with == with_flags)
13222 with_hex |= hexval;
13223 else if (tf->with == without_flags)
13224 without_hex |= hexval;
13225 tf->valid = TRUE;
13226 continue;
13227 }
ae17ab41 13228 }
8b127cbc 13229 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13230 {
8b127cbc 13231 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13232 {
13233 if (tf->with == with_flags)
13234 with_hex |= elf_flags_to_names[i].flag_value;
13235 else if (tf->with == without_flags)
13236 without_hex |= elf_flags_to_names[i].flag_value;
13237 tf->valid = TRUE;
13238 break;
13239 }
13240 }
8b127cbc 13241 if (!tf->valid)
b9c361e0 13242 {
68ffbac6 13243 info->callbacks->einfo
8b127cbc 13244 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13245 return FALSE;
ae17ab41
CM
13246 }
13247 }
8b127cbc
AM
13248 flaginfo->flags_initialized = TRUE;
13249 flaginfo->only_with_flags |= with_hex;
13250 flaginfo->not_with_flags |= without_hex;
ae17ab41 13251 }
ae17ab41 13252
8b127cbc 13253 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13254 return FALSE;
13255
8b127cbc 13256 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13257 return FALSE;
13258
13259 return TRUE;
ae17ab41
CM
13260}
13261
c152c796
AM
13262struct alloc_got_off_arg {
13263 bfd_vma gotoff;
10455f89 13264 struct bfd_link_info *info;
c152c796
AM
13265};
13266
13267/* We need a special top-level link routine to convert got reference counts
13268 to real got offsets. */
13269
13270static bfd_boolean
13271elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13272{
a50b1753 13273 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13274 bfd *obfd = gofarg->info->output_bfd;
13275 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13276
c152c796
AM
13277 if (h->got.refcount > 0)
13278 {
13279 h->got.offset = gofarg->gotoff;
10455f89 13280 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13281 }
13282 else
13283 h->got.offset = (bfd_vma) -1;
13284
13285 return TRUE;
13286}
13287
13288/* And an accompanying bit to work out final got entry offsets once
13289 we're done. Should be called from final_link. */
13290
13291bfd_boolean
13292bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13293 struct bfd_link_info *info)
13294{
13295 bfd *i;
13296 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13297 bfd_vma gotoff;
c152c796
AM
13298 struct alloc_got_off_arg gofarg;
13299
10455f89
HPN
13300 BFD_ASSERT (abfd == info->output_bfd);
13301
c152c796
AM
13302 if (! is_elf_hash_table (info->hash))
13303 return FALSE;
13304
13305 /* The GOT offset is relative to the .got section, but the GOT header is
13306 put into the .got.plt section, if the backend uses it. */
13307 if (bed->want_got_plt)
13308 gotoff = 0;
13309 else
13310 gotoff = bed->got_header_size;
13311
13312 /* Do the local .got entries first. */
c72f2fb2 13313 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13314 {
13315 bfd_signed_vma *local_got;
13316 bfd_size_type j, locsymcount;
13317 Elf_Internal_Shdr *symtab_hdr;
13318
13319 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13320 continue;
13321
13322 local_got = elf_local_got_refcounts (i);
13323 if (!local_got)
13324 continue;
13325
13326 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13327 if (elf_bad_symtab (i))
13328 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13329 else
13330 locsymcount = symtab_hdr->sh_info;
13331
13332 for (j = 0; j < locsymcount; ++j)
13333 {
13334 if (local_got[j] > 0)
13335 {
13336 local_got[j] = gotoff;
10455f89 13337 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13338 }
13339 else
13340 local_got[j] = (bfd_vma) -1;
13341 }
13342 }
13343
13344 /* Then the global .got entries. .plt refcounts are handled by
13345 adjust_dynamic_symbol */
13346 gofarg.gotoff = gotoff;
10455f89 13347 gofarg.info = info;
c152c796
AM
13348 elf_link_hash_traverse (elf_hash_table (info),
13349 elf_gc_allocate_got_offsets,
13350 &gofarg);
13351 return TRUE;
13352}
13353
13354/* Many folk need no more in the way of final link than this, once
13355 got entry reference counting is enabled. */
13356
13357bfd_boolean
13358bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13359{
13360 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13361 return FALSE;
13362
13363 /* Invoke the regular ELF backend linker to do all the work. */
13364 return bfd_elf_final_link (abfd, info);
13365}
13366
13367bfd_boolean
13368bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13369{
a50b1753 13370 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13371
13372 if (rcookie->bad_symtab)
13373 rcookie->rel = rcookie->rels;
13374
13375 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13376 {
13377 unsigned long r_symndx;
13378
13379 if (! rcookie->bad_symtab)
13380 if (rcookie->rel->r_offset > offset)
13381 return FALSE;
13382 if (rcookie->rel->r_offset != offset)
13383 continue;
13384
13385 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13386 if (r_symndx == STN_UNDEF)
c152c796
AM
13387 return TRUE;
13388
13389 if (r_symndx >= rcookie->locsymcount
13390 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13391 {
13392 struct elf_link_hash_entry *h;
13393
13394 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13395
13396 while (h->root.type == bfd_link_hash_indirect
13397 || h->root.type == bfd_link_hash_warning)
13398 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13399
13400 if ((h->root.type == bfd_link_hash_defined
13401 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13402 && (h->root.u.def.section->owner != rcookie->abfd
13403 || h->root.u.def.section->kept_section != NULL
13404 || discarded_section (h->root.u.def.section)))
c152c796 13405 return TRUE;
c152c796
AM
13406 }
13407 else
13408 {
13409 /* It's not a relocation against a global symbol,
13410 but it could be a relocation against a local
13411 symbol for a discarded section. */
13412 asection *isec;
13413 Elf_Internal_Sym *isym;
13414
13415 /* Need to: get the symbol; get the section. */
13416 isym = &rcookie->locsyms[r_symndx];
cb33740c 13417 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13418 if (isec != NULL
13419 && (isec->kept_section != NULL
13420 || discarded_section (isec)))
cb33740c 13421 return TRUE;
c152c796
AM
13422 }
13423 return FALSE;
13424 }
13425 return FALSE;
13426}
13427
13428/* Discard unneeded references to discarded sections.
75938853
AM
13429 Returns -1 on error, 1 if any section's size was changed, 0 if
13430 nothing changed. This function assumes that the relocations are in
13431 sorted order, which is true for all known assemblers. */
c152c796 13432
75938853 13433int
c152c796
AM
13434bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13435{
13436 struct elf_reloc_cookie cookie;
18cd5bce 13437 asection *o;
c152c796 13438 bfd *abfd;
75938853 13439 int changed = 0;
c152c796
AM
13440
13441 if (info->traditional_format
13442 || !is_elf_hash_table (info->hash))
75938853 13443 return 0;
c152c796 13444
18cd5bce
AM
13445 o = bfd_get_section_by_name (output_bfd, ".stab");
13446 if (o != NULL)
c152c796 13447 {
18cd5bce 13448 asection *i;
c152c796 13449
18cd5bce 13450 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13451 {
18cd5bce
AM
13452 if (i->size == 0
13453 || i->reloc_count == 0
13454 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13455 continue;
c152c796 13456
18cd5bce
AM
13457 abfd = i->owner;
13458 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13459 continue;
c152c796 13460
18cd5bce 13461 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13462 return -1;
c152c796 13463
18cd5bce
AM
13464 if (_bfd_discard_section_stabs (abfd, i,
13465 elf_section_data (i)->sec_info,
5241d853
RS
13466 bfd_elf_reloc_symbol_deleted_p,
13467 &cookie))
75938853 13468 changed = 1;
18cd5bce
AM
13469
13470 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13471 }
18cd5bce
AM
13472 }
13473
2f0c68f2
CM
13474 o = NULL;
13475 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13476 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13477 if (o != NULL)
13478 {
13479 asection *i;
c152c796 13480
18cd5bce 13481 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13482 {
18cd5bce
AM
13483 if (i->size == 0)
13484 continue;
13485
13486 abfd = i->owner;
13487 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13488 continue;
13489
13490 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13491 return -1;
18cd5bce
AM
13492
13493 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13494 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13495 bfd_elf_reloc_symbol_deleted_p,
13496 &cookie))
75938853 13497 changed = 1;
18cd5bce
AM
13498
13499 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13500 }
18cd5bce 13501 }
c152c796 13502
18cd5bce
AM
13503 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13504 {
13505 const struct elf_backend_data *bed;
c152c796 13506
18cd5bce
AM
13507 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13508 continue;
13509
13510 bed = get_elf_backend_data (abfd);
13511
13512 if (bed->elf_backend_discard_info != NULL)
13513 {
13514 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13515 return -1;
18cd5bce
AM
13516
13517 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13518 changed = 1;
18cd5bce
AM
13519
13520 fini_reloc_cookie (&cookie, abfd);
13521 }
c152c796
AM
13522 }
13523
2f0c68f2
CM
13524 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13525 _bfd_elf_end_eh_frame_parsing (info);
13526
13527 if (info->eh_frame_hdr_type
0e1862bb 13528 && !bfd_link_relocatable (info)
c152c796 13529 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13530 changed = 1;
c152c796 13531
75938853 13532 return changed;
c152c796 13533}
082b7297 13534
43e1669b 13535bfd_boolean
0c511000 13536_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13537 asection *sec,
c0f00686 13538 struct bfd_link_info *info)
082b7297
L
13539{
13540 flagword flags;
c77ec726 13541 const char *name, *key;
082b7297
L
13542 struct bfd_section_already_linked *l;
13543 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13544
c77ec726
AM
13545 if (sec->output_section == bfd_abs_section_ptr)
13546 return FALSE;
0c511000 13547
c77ec726 13548 flags = sec->flags;
0c511000 13549
c77ec726
AM
13550 /* Return if it isn't a linkonce section. A comdat group section
13551 also has SEC_LINK_ONCE set. */
13552 if ((flags & SEC_LINK_ONCE) == 0)
13553 return FALSE;
0c511000 13554
c77ec726
AM
13555 /* Don't put group member sections on our list of already linked
13556 sections. They are handled as a group via their group section. */
13557 if (elf_sec_group (sec) != NULL)
13558 return FALSE;
0c511000 13559
c77ec726
AM
13560 /* For a SHT_GROUP section, use the group signature as the key. */
13561 name = sec->name;
13562 if ((flags & SEC_GROUP) != 0
13563 && elf_next_in_group (sec) != NULL
13564 && elf_group_name (elf_next_in_group (sec)) != NULL)
13565 key = elf_group_name (elf_next_in_group (sec));
13566 else
13567 {
13568 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13569 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13570 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13571 key++;
0c511000 13572 else
c77ec726
AM
13573 /* Must be a user linkonce section that doesn't follow gcc's
13574 naming convention. In this case we won't be matching
13575 single member groups. */
13576 key = name;
0c511000 13577 }
6d2cd210 13578
c77ec726 13579 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13580
13581 for (l = already_linked_list->entry; l != NULL; l = l->next)
13582 {
c2370991 13583 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13584 sections with a signature of <key> (<key> is some string),
13585 and linkonce sections named .gnu.linkonce.<type>.<key>.
13586 Match like sections. LTO plugin sections are an exception.
13587 They are always named .gnu.linkonce.t.<key> and match either
13588 type of section. */
13589 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13590 && ((flags & SEC_GROUP) != 0
13591 || strcmp (name, l->sec->name) == 0))
13592 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13593 {
13594 /* The section has already been linked. See if we should
6d2cd210 13595 issue a warning. */
c77ec726
AM
13596 if (!_bfd_handle_already_linked (sec, l, info))
13597 return FALSE;
082b7297 13598
c77ec726 13599 if (flags & SEC_GROUP)
3d7f7666 13600 {
c77ec726
AM
13601 asection *first = elf_next_in_group (sec);
13602 asection *s = first;
3d7f7666 13603
c77ec726 13604 while (s != NULL)
3d7f7666 13605 {
c77ec726
AM
13606 s->output_section = bfd_abs_section_ptr;
13607 /* Record which group discards it. */
13608 s->kept_section = l->sec;
13609 s = elf_next_in_group (s);
13610 /* These lists are circular. */
13611 if (s == first)
13612 break;
3d7f7666
L
13613 }
13614 }
082b7297 13615
43e1669b 13616 return TRUE;
082b7297
L
13617 }
13618 }
13619
c77ec726
AM
13620 /* A single member comdat group section may be discarded by a
13621 linkonce section and vice versa. */
13622 if ((flags & SEC_GROUP) != 0)
3d7f7666 13623 {
c77ec726 13624 asection *first = elf_next_in_group (sec);
c2370991 13625
c77ec726
AM
13626 if (first != NULL && elf_next_in_group (first) == first)
13627 /* Check this single member group against linkonce sections. */
13628 for (l = already_linked_list->entry; l != NULL; l = l->next)
13629 if ((l->sec->flags & SEC_GROUP) == 0
13630 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13631 {
13632 first->output_section = bfd_abs_section_ptr;
13633 first->kept_section = l->sec;
13634 sec->output_section = bfd_abs_section_ptr;
13635 break;
13636 }
13637 }
13638 else
13639 /* Check this linkonce section against single member groups. */
13640 for (l = already_linked_list->entry; l != NULL; l = l->next)
13641 if (l->sec->flags & SEC_GROUP)
6d2cd210 13642 {
c77ec726 13643 asection *first = elf_next_in_group (l->sec);
6d2cd210 13644
c77ec726
AM
13645 if (first != NULL
13646 && elf_next_in_group (first) == first
13647 && bfd_elf_match_symbols_in_sections (first, sec, info))
13648 {
13649 sec->output_section = bfd_abs_section_ptr;
13650 sec->kept_section = first;
13651 break;
13652 }
6d2cd210 13653 }
0c511000 13654
c77ec726
AM
13655 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13656 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13657 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13658 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13659 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13660 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13661 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13662 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13663 The reverse order cannot happen as there is never a bfd with only the
13664 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13665 matter as here were are looking only for cross-bfd sections. */
13666
13667 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13668 for (l = already_linked_list->entry; l != NULL; l = l->next)
13669 if ((l->sec->flags & SEC_GROUP) == 0
13670 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13671 {
13672 if (abfd != l->sec->owner)
13673 sec->output_section = bfd_abs_section_ptr;
13674 break;
13675 }
80c29487 13676
082b7297 13677 /* This is the first section with this name. Record it. */
c77ec726 13678 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13679 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13680 return sec->output_section == bfd_abs_section_ptr;
082b7297 13681}
81e1b023 13682
a4d8e49b
L
13683bfd_boolean
13684_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13685{
13686 return sym->st_shndx == SHN_COMMON;
13687}
13688
13689unsigned int
13690_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13691{
13692 return SHN_COMMON;
13693}
13694
13695asection *
13696_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13697{
13698 return bfd_com_section_ptr;
13699}
10455f89
HPN
13700
13701bfd_vma
13702_bfd_elf_default_got_elt_size (bfd *abfd,
13703 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13704 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13705 bfd *ibfd ATTRIBUTE_UNUSED,
13706 unsigned long symndx ATTRIBUTE_UNUSED)
13707{
13708 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13709 return bed->s->arch_size / 8;
13710}
83bac4b0
NC
13711
13712/* Routines to support the creation of dynamic relocs. */
13713
83bac4b0
NC
13714/* Returns the name of the dynamic reloc section associated with SEC. */
13715
13716static const char *
13717get_dynamic_reloc_section_name (bfd * abfd,
13718 asection * sec,
13719 bfd_boolean is_rela)
13720{
ddcf1fcf
BS
13721 char *name;
13722 const char *old_name = bfd_get_section_name (NULL, sec);
13723 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13724
ddcf1fcf 13725 if (old_name == NULL)
83bac4b0
NC
13726 return NULL;
13727
ddcf1fcf 13728 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13729 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13730
13731 return name;
13732}
13733
13734/* Returns the dynamic reloc section associated with SEC.
13735 If necessary compute the name of the dynamic reloc section based
13736 on SEC's name (looked up in ABFD's string table) and the setting
13737 of IS_RELA. */
13738
13739asection *
13740_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13741 asection * sec,
13742 bfd_boolean is_rela)
13743{
13744 asection * reloc_sec = elf_section_data (sec)->sreloc;
13745
13746 if (reloc_sec == NULL)
13747 {
13748 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13749
13750 if (name != NULL)
13751 {
3d4d4302 13752 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
13753
13754 if (reloc_sec != NULL)
13755 elf_section_data (sec)->sreloc = reloc_sec;
13756 }
13757 }
13758
13759 return reloc_sec;
13760}
13761
13762/* Returns the dynamic reloc section associated with SEC. If the
13763 section does not exist it is created and attached to the DYNOBJ
13764 bfd and stored in the SRELOC field of SEC's elf_section_data
13765 structure.
f8076f98 13766
83bac4b0
NC
13767 ALIGNMENT is the alignment for the newly created section and
13768 IS_RELA defines whether the name should be .rela.<SEC's name>
13769 or .rel.<SEC's name>. The section name is looked up in the
13770 string table associated with ABFD. */
13771
13772asection *
ca4be51c
AM
13773_bfd_elf_make_dynamic_reloc_section (asection *sec,
13774 bfd *dynobj,
13775 unsigned int alignment,
13776 bfd *abfd,
13777 bfd_boolean is_rela)
83bac4b0
NC
13778{
13779 asection * reloc_sec = elf_section_data (sec)->sreloc;
13780
13781 if (reloc_sec == NULL)
13782 {
13783 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13784
13785 if (name == NULL)
13786 return NULL;
13787
3d4d4302 13788 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
13789
13790 if (reloc_sec == NULL)
13791 {
3d4d4302
AM
13792 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13793 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
13794 if ((sec->flags & SEC_ALLOC) != 0)
13795 flags |= SEC_ALLOC | SEC_LOAD;
13796
3d4d4302 13797 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
13798 if (reloc_sec != NULL)
13799 {
8877b5e5
AM
13800 /* _bfd_elf_get_sec_type_attr chooses a section type by
13801 name. Override as it may be wrong, eg. for a user
13802 section named "auto" we'll get ".relauto" which is
13803 seen to be a .rela section. */
13804 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
13805 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13806 reloc_sec = NULL;
13807 }
13808 }
13809
13810 elf_section_data (sec)->sreloc = reloc_sec;
13811 }
13812
13813 return reloc_sec;
13814}
1338dd10 13815
bffebb6b
AM
13816/* Copy the ELF symbol type and other attributes for a linker script
13817 assignment from HSRC to HDEST. Generally this should be treated as
13818 if we found a strong non-dynamic definition for HDEST (except that
13819 ld ignores multiple definition errors). */
1338dd10 13820void
bffebb6b
AM
13821_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13822 struct bfd_link_hash_entry *hdest,
13823 struct bfd_link_hash_entry *hsrc)
1338dd10 13824{
bffebb6b
AM
13825 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13826 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13827 Elf_Internal_Sym isym;
1338dd10
PB
13828
13829 ehdest->type = ehsrc->type;
35fc36a8 13830 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
13831
13832 isym.st_other = ehsrc->other;
b8417128 13833 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 13834}
351f65ca
L
13835
13836/* Append a RELA relocation REL to section S in BFD. */
13837
13838void
13839elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13840{
13841 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13842 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13843 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13844 bed->s->swap_reloca_out (abfd, rel, loc);
13845}
13846
13847/* Append a REL relocation REL to section S in BFD. */
13848
13849void
13850elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13851{
13852 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13853 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13854 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 13855 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 13856}
This page took 2.673613 seconds and 4 git commands to generate.