Pass SIGLIBRT directly to child processes.
[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"
08ce1d72 31#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
32#include "plugin.h"
33#endif
252b5132 34
28caa186
AM
35/* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
37
38struct elf_info_failed
39{
40 struct bfd_link_info *info;
28caa186
AM
41 bfd_boolean failed;
42};
43
44/* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
46
47struct elf_find_verdep_info
48{
49 /* General link information. */
50 struct bfd_link_info *info;
51 /* The number of dependencies. */
52 unsigned int vers;
53 /* Whether we had a failure. */
54 bfd_boolean failed;
55};
56
57static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry *, struct elf_info_failed *);
59
2f0c68f2
CM
60asection *
61_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 unsigned long r_symndx,
63 bfd_boolean discard)
64{
65 if (r_symndx >= cookie->locsymcount
66 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
67 {
68 struct elf_link_hash_entry *h;
69
70 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
71
72 while (h->root.type == bfd_link_hash_indirect
73 || h->root.type == bfd_link_hash_warning)
74 h = (struct elf_link_hash_entry *) h->root.u.i.link;
75
76 if ((h->root.type == bfd_link_hash_defined
77 || h->root.type == bfd_link_hash_defweak)
78 && discarded_section (h->root.u.def.section))
79 return h->root.u.def.section;
80 else
81 return NULL;
82 }
83 else
84 {
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
88 asection *isec;
89 Elf_Internal_Sym *isym;
90
91 /* Need to: get the symbol; get the section. */
92 isym = &cookie->locsyms[r_symndx];
93 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
94 if (isec != NULL
95 && discard ? discarded_section (isec) : 1)
96 return isec;
97 }
98 return NULL;
99}
100
d98685ac
AM
101/* Define a symbol in a dynamic linkage section. */
102
103struct elf_link_hash_entry *
104_bfd_elf_define_linkage_sym (bfd *abfd,
105 struct bfd_link_info *info,
106 asection *sec,
107 const char *name)
108{
109 struct elf_link_hash_entry *h;
110 struct bfd_link_hash_entry *bh;
ccabcbe5 111 const struct elf_backend_data *bed;
d98685ac
AM
112
113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
114 if (h != NULL)
115 {
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h->root.type = bfd_link_hash_new;
121 }
122
123 bh = &h->root;
cf18fda4 124 bed = get_elf_backend_data (abfd);
d98685ac 125 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 126 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
127 &bh))
128 return NULL;
129 h = (struct elf_link_hash_entry *) bh;
130 h->def_regular = 1;
e28df02b 131 h->non_elf = 0;
12b2843a 132 h->root.linker_def = 1;
d98685ac 133 h->type = STT_OBJECT;
00b7642b
AM
134 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
135 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 136
ccabcbe5 137 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
138 return h;
139}
140
b34976b6 141bfd_boolean
268b6b39 142_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
143{
144 flagword flags;
aad5d350 145 asection *s;
252b5132 146 struct elf_link_hash_entry *h;
9c5bfbb7 147 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 148 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
149
150 /* This function may be called more than once. */
3d4d4302
AM
151 s = bfd_get_linker_section (abfd, ".got");
152 if (s != NULL)
b34976b6 153 return TRUE;
252b5132 154
e5a52504 155 flags = bed->dynamic_sec_flags;
252b5132 156
14b2f831
AM
157 s = bfd_make_section_anyway_with_flags (abfd,
158 (bed->rela_plts_and_copies_p
159 ? ".rela.got" : ".rel.got"),
160 (bed->dynamic_sec_flags
161 | SEC_READONLY));
6de2ae4a
L
162 if (s == NULL
163 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
164 return FALSE;
165 htab->srelgot = s;
252b5132 166
14b2f831 167 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
168 if (s == NULL
169 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 return FALSE;
171 htab->sgot = s;
172
252b5132
RH
173 if (bed->want_got_plt)
174 {
14b2f831 175 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 176 if (s == NULL
6de2ae4a
L
177 || !bfd_set_section_alignment (abfd, s,
178 bed->s->log_file_align))
b34976b6 179 return FALSE;
6de2ae4a 180 htab->sgotplt = s;
252b5132
RH
181 }
182
64e77c6d
L
183 /* The first bit of the global offset table is the header. */
184 s->size += bed->got_header_size;
185
2517a57f
AM
186 if (bed->want_got_sym)
187 {
188 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
189 (or .got.plt) section. We don't do this in the linker script
190 because we don't want to define the symbol if we are not creating
191 a global offset table. */
6de2ae4a
L
192 h = _bfd_elf_define_linkage_sym (abfd, info, s,
193 "_GLOBAL_OFFSET_TABLE_");
2517a57f 194 elf_hash_table (info)->hgot = h;
d98685ac
AM
195 if (h == NULL)
196 return FALSE;
2517a57f 197 }
252b5132 198
b34976b6 199 return TRUE;
252b5132
RH
200}
201\f
7e9f0867
AM
202/* Create a strtab to hold the dynamic symbol names. */
203static bfd_boolean
204_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
205{
206 struct elf_link_hash_table *hash_table;
207
208 hash_table = elf_hash_table (info);
209 if (hash_table->dynobj == NULL)
6cd255ca
L
210 {
211 /* We may not set dynobj, an input file holding linker created
212 dynamic sections to abfd, which may be a dynamic object with
213 its own dynamic sections. We need to find a normal input file
214 to hold linker created sections if possible. */
215 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
216 {
217 bfd *ibfd;
218 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e
L
219 if ((ibfd->flags
220 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
6cd255ca
L
221 {
222 abfd = ibfd;
223 break;
224 }
225 }
226 hash_table->dynobj = abfd;
227 }
7e9f0867
AM
228
229 if (hash_table->dynstr == NULL)
230 {
231 hash_table->dynstr = _bfd_elf_strtab_init ();
232 if (hash_table->dynstr == NULL)
233 return FALSE;
234 }
235 return TRUE;
236}
237
45d6a902
AM
238/* Create some sections which will be filled in with dynamic linking
239 information. ABFD is an input file which requires dynamic sections
240 to be created. The dynamic sections take up virtual memory space
241 when the final executable is run, so we need to create them before
242 addresses are assigned to the output sections. We work out the
243 actual contents and size of these sections later. */
252b5132 244
b34976b6 245bfd_boolean
268b6b39 246_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 247{
45d6a902 248 flagword flags;
91d6fa6a 249 asection *s;
9c5bfbb7 250 const struct elf_backend_data *bed;
9637f6ef 251 struct elf_link_hash_entry *h;
252b5132 252
0eddce27 253 if (! is_elf_hash_table (info->hash))
45d6a902
AM
254 return FALSE;
255
256 if (elf_hash_table (info)->dynamic_sections_created)
257 return TRUE;
258
7e9f0867
AM
259 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
260 return FALSE;
45d6a902 261
7e9f0867 262 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
263 bed = get_elf_backend_data (abfd);
264
265 flags = bed->dynamic_sec_flags;
45d6a902
AM
266
267 /* A dynamically linked executable has a .interp section, but a
268 shared library does not. */
9b8b325a 269 if (bfd_link_executable (info) && !info->nointerp)
252b5132 270 {
14b2f831
AM
271 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
272 flags | SEC_READONLY);
3496cb2a 273 if (s == NULL)
45d6a902
AM
274 return FALSE;
275 }
bb0deeff 276
45d6a902
AM
277 /* Create sections to hold version informations. These are removed
278 if they are not needed. */
14b2f831
AM
279 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
280 flags | SEC_READONLY);
45d6a902 281 if (s == NULL
45d6a902
AM
282 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
283 return FALSE;
284
14b2f831
AM
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
286 flags | SEC_READONLY);
45d6a902 287 if (s == NULL
45d6a902
AM
288 || ! bfd_set_section_alignment (abfd, s, 1))
289 return FALSE;
290
14b2f831
AM
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
292 flags | SEC_READONLY);
45d6a902 293 if (s == NULL
45d6a902
AM
294 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
295 return FALSE;
296
14b2f831
AM
297 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
298 flags | SEC_READONLY);
45d6a902 299 if (s == NULL
45d6a902
AM
300 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
301 return FALSE;
cae1fbbb 302 elf_hash_table (info)->dynsym = s;
45d6a902 303
14b2f831
AM
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
305 flags | SEC_READONLY);
3496cb2a 306 if (s == NULL)
45d6a902
AM
307 return FALSE;
308
14b2f831 309 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 310 if (s == NULL
45d6a902
AM
311 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
312 return FALSE;
313
314 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
315 .dynamic section. We could set _DYNAMIC in a linker script, but we
316 only want to define it if we are, in fact, creating a .dynamic
317 section. We don't want to define it if there is no .dynamic
318 section, since on some ELF platforms the start up code examines it
319 to decide how to initialize the process. */
9637f6ef
L
320 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
321 elf_hash_table (info)->hdynamic = h;
322 if (h == NULL)
45d6a902
AM
323 return FALSE;
324
fdc90cb4
JJ
325 if (info->emit_hash)
326 {
14b2f831
AM
327 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
328 flags | SEC_READONLY);
fdc90cb4
JJ
329 if (s == NULL
330 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
331 return FALSE;
332 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
333 }
334
335 if (info->emit_gnu_hash)
336 {
14b2f831
AM
337 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
338 flags | SEC_READONLY);
fdc90cb4
JJ
339 if (s == NULL
340 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
341 return FALSE;
342 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
343 4 32-bit words followed by variable count of 64-bit words, then
344 variable count of 32-bit words. */
345 if (bed->s->arch_size == 64)
346 elf_section_data (s)->this_hdr.sh_entsize = 0;
347 else
348 elf_section_data (s)->this_hdr.sh_entsize = 4;
349 }
45d6a902
AM
350
351 /* Let the backend create the rest of the sections. This lets the
352 backend set the right flags. The backend will normally create
353 the .got and .plt sections. */
894891db
NC
354 if (bed->elf_backend_create_dynamic_sections == NULL
355 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
356 return FALSE;
357
358 elf_hash_table (info)->dynamic_sections_created = TRUE;
359
360 return TRUE;
361}
362
363/* Create dynamic sections when linking against a dynamic object. */
364
365bfd_boolean
268b6b39 366_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
367{
368 flagword flags, pltflags;
7325306f 369 struct elf_link_hash_entry *h;
45d6a902 370 asection *s;
9c5bfbb7 371 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 372 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 373
252b5132
RH
374 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
375 .rel[a].bss sections. */
e5a52504 376 flags = bed->dynamic_sec_flags;
252b5132
RH
377
378 pltflags = flags;
252b5132 379 if (bed->plt_not_loaded)
6df4d94c
MM
380 /* We do not clear SEC_ALLOC here because we still want the OS to
381 allocate space for the section; it's just that there's nothing
382 to read in from the object file. */
5d1634d7 383 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
384 else
385 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
386 if (bed->plt_readonly)
387 pltflags |= SEC_READONLY;
388
14b2f831 389 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 390 if (s == NULL
252b5132 391 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 392 return FALSE;
6de2ae4a 393 htab->splt = s;
252b5132 394
d98685ac
AM
395 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
396 .plt section. */
7325306f
RS
397 if (bed->want_plt_sym)
398 {
399 h = _bfd_elf_define_linkage_sym (abfd, info, s,
400 "_PROCEDURE_LINKAGE_TABLE_");
401 elf_hash_table (info)->hplt = h;
402 if (h == NULL)
403 return FALSE;
404 }
252b5132 405
14b2f831
AM
406 s = bfd_make_section_anyway_with_flags (abfd,
407 (bed->rela_plts_and_copies_p
408 ? ".rela.plt" : ".rel.plt"),
409 flags | SEC_READONLY);
252b5132 410 if (s == NULL
45d6a902 411 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 412 return FALSE;
6de2ae4a 413 htab->srelplt = s;
252b5132
RH
414
415 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 416 return FALSE;
252b5132 417
3018b441
RH
418 if (bed->want_dynbss)
419 {
420 /* The .dynbss section is a place to put symbols which are defined
421 by dynamic objects, are referenced by regular objects, and are
422 not functions. We must allocate space for them in the process
423 image and use a R_*_COPY reloc to tell the dynamic linker to
424 initialize them at run time. The linker script puts the .dynbss
425 section into the .bss section of the final image. */
14b2f831
AM
426 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
427 (SEC_ALLOC | SEC_LINKER_CREATED));
3496cb2a 428 if (s == NULL)
b34976b6 429 return FALSE;
252b5132 430
3018b441 431 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
432 normally needed. We need to create it here, though, so that the
433 linker will map it to an output section. We can't just create it
434 only if we need it, because we will not know whether we need it
435 until we have seen all the input files, and the first time the
436 main linker code calls BFD after examining all the input files
437 (size_dynamic_sections) the input sections have already been
438 mapped to the output sections. If the section turns out not to
439 be needed, we can discard it later. We will never need this
440 section when generating a shared object, since they do not use
441 copy relocs. */
0e1862bb 442 if (! bfd_link_pic (info))
3018b441 443 {
14b2f831
AM
444 s = bfd_make_section_anyway_with_flags (abfd,
445 (bed->rela_plts_and_copies_p
446 ? ".rela.bss" : ".rel.bss"),
447 flags | SEC_READONLY);
3018b441 448 if (s == NULL
45d6a902 449 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 450 return FALSE;
3018b441 451 }
252b5132
RH
452 }
453
b34976b6 454 return TRUE;
252b5132
RH
455}
456\f
252b5132
RH
457/* Record a new dynamic symbol. We record the dynamic symbols as we
458 read the input files, since we need to have a list of all of them
459 before we can determine the final sizes of the output sections.
460 Note that we may actually call this function even though we are not
461 going to output any dynamic symbols; in some cases we know that a
462 symbol should be in the dynamic symbol table, but only if there is
463 one. */
464
b34976b6 465bfd_boolean
c152c796
AM
466bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
467 struct elf_link_hash_entry *h)
252b5132
RH
468{
469 if (h->dynindx == -1)
470 {
2b0f7ef9 471 struct elf_strtab_hash *dynstr;
68b6ddd0 472 char *p;
252b5132 473 const char *name;
ef53be89 474 size_t indx;
252b5132 475
7a13edea
NC
476 /* XXX: The ABI draft says the linker must turn hidden and
477 internal symbols into STB_LOCAL symbols when producing the
478 DSO. However, if ld.so honors st_other in the dynamic table,
479 this would not be necessary. */
480 switch (ELF_ST_VISIBILITY (h->other))
481 {
482 case STV_INTERNAL:
483 case STV_HIDDEN:
9d6eee78
L
484 if (h->root.type != bfd_link_hash_undefined
485 && h->root.type != bfd_link_hash_undefweak)
38048eb9 486 {
f5385ebf 487 h->forced_local = 1;
67687978
PB
488 if (!elf_hash_table (info)->is_relocatable_executable)
489 return TRUE;
7a13edea 490 }
0444bdd4 491
7a13edea
NC
492 default:
493 break;
494 }
495
252b5132
RH
496 h->dynindx = elf_hash_table (info)->dynsymcount;
497 ++elf_hash_table (info)->dynsymcount;
498
499 dynstr = elf_hash_table (info)->dynstr;
500 if (dynstr == NULL)
501 {
502 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 503 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 504 if (dynstr == NULL)
b34976b6 505 return FALSE;
252b5132
RH
506 }
507
508 /* We don't put any version information in the dynamic string
aad5d350 509 table. */
252b5132
RH
510 name = h->root.root.string;
511 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
512 if (p != NULL)
513 /* We know that the p points into writable memory. In fact,
514 there are only a few symbols that have read-only names, being
515 those like _GLOBAL_OFFSET_TABLE_ that are created specially
516 by the backends. Most symbols will have names pointing into
517 an ELF string table read from a file, or to objalloc memory. */
518 *p = 0;
519
520 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
521
522 if (p != NULL)
523 *p = ELF_VER_CHR;
252b5132 524
ef53be89 525 if (indx == (size_t) -1)
b34976b6 526 return FALSE;
252b5132
RH
527 h->dynstr_index = indx;
528 }
529
b34976b6 530 return TRUE;
252b5132 531}
45d6a902 532\f
55255dae
L
533/* Mark a symbol dynamic. */
534
28caa186 535static void
55255dae 536bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
537 struct elf_link_hash_entry *h,
538 Elf_Internal_Sym *sym)
55255dae 539{
40b36307 540 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 541
40b36307 542 /* It may be called more than once on the same H. */
0e1862bb 543 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
544 return;
545
40b36307
L
546 if ((info->dynamic_data
547 && (h->type == STT_OBJECT
b8871f35 548 || h->type == STT_COMMON
40b36307 549 || (sym != NULL
b8871f35
L
550 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
551 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 552 || (d != NULL
40b36307
L
553 && h->root.type == bfd_link_hash_new
554 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
555 h->dynamic = 1;
556}
557
45d6a902
AM
558/* Record an assignment to a symbol made by a linker script. We need
559 this in case some dynamic object refers to this symbol. */
560
561bfd_boolean
fe21a8fc
L
562bfd_elf_record_link_assignment (bfd *output_bfd,
563 struct bfd_link_info *info,
268b6b39 564 const char *name,
fe21a8fc
L
565 bfd_boolean provide,
566 bfd_boolean hidden)
45d6a902 567{
00cbee0a 568 struct elf_link_hash_entry *h, *hv;
4ea42fb7 569 struct elf_link_hash_table *htab;
00cbee0a 570 const struct elf_backend_data *bed;
45d6a902 571
0eddce27 572 if (!is_elf_hash_table (info->hash))
45d6a902
AM
573 return TRUE;
574
4ea42fb7
AM
575 htab = elf_hash_table (info);
576 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 577 if (h == NULL)
4ea42fb7 578 return provide;
45d6a902 579
0f550b3d
L
580 if (h->versioned == unknown)
581 {
582 /* Set versioned if symbol version is unknown. */
583 char *version = strrchr (name, ELF_VER_CHR);
584 if (version)
585 {
586 if (version > name && version[-1] != ELF_VER_CHR)
587 h->versioned = versioned_hidden;
588 else
589 h->versioned = versioned;
590 }
591 }
592
00cbee0a 593 switch (h->root.type)
77cfaee6 594 {
00cbee0a
L
595 case bfd_link_hash_defined:
596 case bfd_link_hash_defweak:
597 case bfd_link_hash_common:
598 break;
599 case bfd_link_hash_undefweak:
600 case bfd_link_hash_undefined:
601 /* Since we're defining the symbol, don't let it seem to have not
602 been defined. record_dynamic_symbol and size_dynamic_sections
603 may depend on this. */
4ea42fb7 604 h->root.type = bfd_link_hash_new;
77cfaee6
AM
605 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
606 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
607 break;
608 case bfd_link_hash_new:
40b36307 609 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 610 h->non_elf = 0;
00cbee0a
L
611 break;
612 case bfd_link_hash_indirect:
613 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 614 the versioned symbol point to this one. */
00cbee0a
L
615 bed = get_elf_backend_data (output_bfd);
616 hv = h;
617 while (hv->root.type == bfd_link_hash_indirect
618 || hv->root.type == bfd_link_hash_warning)
619 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
620 /* We don't need to update h->root.u since linker will set them
621 later. */
622 h->root.type = bfd_link_hash_undefined;
623 hv->root.type = bfd_link_hash_indirect;
624 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
625 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
626 break;
627 case bfd_link_hash_warning:
628 abort ();
629 break;
55255dae 630 }
45d6a902
AM
631
632 /* If this symbol is being provided by the linker script, and it is
633 currently defined by a dynamic object, but not by a regular
634 object, then mark it as undefined so that the generic linker will
635 force the correct value. */
636 if (provide
f5385ebf
AM
637 && h->def_dynamic
638 && !h->def_regular)
45d6a902
AM
639 h->root.type = bfd_link_hash_undefined;
640
641 /* If this symbol is not being provided by the linker script, and it is
642 currently defined by a dynamic object, but not by a regular object,
643 then clear out any version information because the symbol will not be
644 associated with the dynamic object any more. */
645 if (!provide
f5385ebf
AM
646 && h->def_dynamic
647 && !h->def_regular)
45d6a902
AM
648 h->verinfo.verdef = NULL;
649
f5385ebf 650 h->def_regular = 1;
45d6a902 651
eb8476a6 652 if (hidden)
fe21a8fc 653 {
91d6fa6a 654 bed = get_elf_backend_data (output_bfd);
b8297068
AM
655 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
656 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
657 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
658 }
659
6fa3860b
PB
660 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
661 and executables. */
0e1862bb 662 if (!bfd_link_relocatable (info)
6fa3860b
PB
663 && h->dynindx != -1
664 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
665 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
666 h->forced_local = 1;
667
f5385ebf
AM
668 if ((h->def_dynamic
669 || h->ref_dynamic
6b3b0ab8
L
670 || bfd_link_dll (info)
671 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
672 && h->dynindx == -1)
673 {
c152c796 674 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
675 return FALSE;
676
677 /* If this is a weak defined symbol, and we know a corresponding
678 real symbol from the same dynamic object, make sure the real
679 symbol is also made into a dynamic symbol. */
f6e332e6
AM
680 if (h->u.weakdef != NULL
681 && h->u.weakdef->dynindx == -1)
45d6a902 682 {
f6e332e6 683 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
684 return FALSE;
685 }
686 }
687
688 return TRUE;
689}
42751cf3 690
8c58d23b
AM
691/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
692 success, and 2 on a failure caused by attempting to record a symbol
693 in a discarded section, eg. a discarded link-once section symbol. */
694
695int
c152c796
AM
696bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
697 bfd *input_bfd,
698 long input_indx)
8c58d23b
AM
699{
700 bfd_size_type amt;
701 struct elf_link_local_dynamic_entry *entry;
702 struct elf_link_hash_table *eht;
703 struct elf_strtab_hash *dynstr;
ef53be89 704 size_t dynstr_index;
8c58d23b
AM
705 char *name;
706 Elf_External_Sym_Shndx eshndx;
707 char esym[sizeof (Elf64_External_Sym)];
708
0eddce27 709 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
710 return 0;
711
712 /* See if the entry exists already. */
713 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
714 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
715 return 1;
716
717 amt = sizeof (*entry);
a50b1753 718 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
719 if (entry == NULL)
720 return 0;
721
722 /* Go find the symbol, so that we can find it's name. */
723 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 724 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
725 {
726 bfd_release (input_bfd, entry);
727 return 0;
728 }
729
730 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 731 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
732 {
733 asection *s;
734
735 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
736 if (s == NULL || bfd_is_abs_section (s->output_section))
737 {
738 /* We can still bfd_release here as nothing has done another
739 bfd_alloc. We can't do this later in this function. */
740 bfd_release (input_bfd, entry);
741 return 2;
742 }
743 }
744
745 name = (bfd_elf_string_from_elf_section
746 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
747 entry->isym.st_name));
748
749 dynstr = elf_hash_table (info)->dynstr;
750 if (dynstr == NULL)
751 {
752 /* Create a strtab to hold the dynamic symbol names. */
753 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
754 if (dynstr == NULL)
755 return 0;
756 }
757
b34976b6 758 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 759 if (dynstr_index == (size_t) -1)
8c58d23b
AM
760 return 0;
761 entry->isym.st_name = dynstr_index;
762
763 eht = elf_hash_table (info);
764
765 entry->next = eht->dynlocal;
766 eht->dynlocal = entry;
767 entry->input_bfd = input_bfd;
768 entry->input_indx = input_indx;
769 eht->dynsymcount++;
770
771 /* Whatever binding the symbol had before, it's now local. */
772 entry->isym.st_info
773 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
774
775 /* The dynindx will be set at the end of size_dynamic_sections. */
776
777 return 1;
778}
779
30b30c21 780/* Return the dynindex of a local dynamic symbol. */
42751cf3 781
30b30c21 782long
268b6b39
AM
783_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
784 bfd *input_bfd,
785 long input_indx)
30b30c21
RH
786{
787 struct elf_link_local_dynamic_entry *e;
788
789 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
790 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
791 return e->dynindx;
792 return -1;
793}
794
795/* This function is used to renumber the dynamic symbols, if some of
796 them are removed because they are marked as local. This is called
797 via elf_link_hash_traverse. */
798
b34976b6 799static bfd_boolean
268b6b39
AM
800elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
801 void *data)
42751cf3 802{
a50b1753 803 size_t *count = (size_t *) data;
30b30c21 804
6fa3860b
PB
805 if (h->forced_local)
806 return TRUE;
807
808 if (h->dynindx != -1)
809 h->dynindx = ++(*count);
810
811 return TRUE;
812}
813
814
815/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
816 STB_LOCAL binding. */
817
818static bfd_boolean
819elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
820 void *data)
821{
a50b1753 822 size_t *count = (size_t *) data;
6fa3860b 823
6fa3860b
PB
824 if (!h->forced_local)
825 return TRUE;
826
42751cf3 827 if (h->dynindx != -1)
30b30c21
RH
828 h->dynindx = ++(*count);
829
b34976b6 830 return TRUE;
42751cf3 831}
30b30c21 832
aee6f5b4
AO
833/* Return true if the dynamic symbol for a given section should be
834 omitted when creating a shared library. */
835bfd_boolean
836_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
837 struct bfd_link_info *info,
838 asection *p)
839{
74541ad4 840 struct elf_link_hash_table *htab;
ca55926c 841 asection *ip;
74541ad4 842
aee6f5b4
AO
843 switch (elf_section_data (p)->this_hdr.sh_type)
844 {
845 case SHT_PROGBITS:
846 case SHT_NOBITS:
847 /* If sh_type is yet undecided, assume it could be
848 SHT_PROGBITS/SHT_NOBITS. */
849 case SHT_NULL:
74541ad4
AM
850 htab = elf_hash_table (info);
851 if (p == htab->tls_sec)
852 return FALSE;
853
854 if (htab->text_index_section != NULL)
855 return p != htab->text_index_section && p != htab->data_index_section;
856
ca55926c 857 return (htab->dynobj != NULL
3d4d4302 858 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 859 && ip->output_section == p);
aee6f5b4
AO
860
861 /* There shouldn't be section relative relocations
862 against any other section. */
863 default:
864 return TRUE;
865 }
866}
867
062e2358 868/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
869 symbol for each output section, which come first. Next come symbols
870 which have been forced to local binding. Then all of the back-end
871 allocated local dynamic syms, followed by the rest of the global
872 symbols. */
30b30c21 873
554220db
AM
874static unsigned long
875_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
876 struct bfd_link_info *info,
877 unsigned long *section_sym_count)
30b30c21
RH
878{
879 unsigned long dynsymcount = 0;
880
0e1862bb
L
881 if (bfd_link_pic (info)
882 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 883 {
aee6f5b4 884 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
885 asection *p;
886 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 887 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
888 && (p->flags & SEC_ALLOC) != 0
889 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
890 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
891 else
892 elf_section_data (p)->dynindx = 0;
30b30c21 893 }
554220db 894 *section_sym_count = dynsymcount;
30b30c21 895
6fa3860b
PB
896 elf_link_hash_traverse (elf_hash_table (info),
897 elf_link_renumber_local_hash_table_dynsyms,
898 &dynsymcount);
899
30b30c21
RH
900 if (elf_hash_table (info)->dynlocal)
901 {
902 struct elf_link_local_dynamic_entry *p;
903 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
904 p->dynindx = ++dynsymcount;
905 }
906
907 elf_link_hash_traverse (elf_hash_table (info),
908 elf_link_renumber_hash_table_dynsyms,
909 &dynsymcount);
910
d5486c43
L
911 /* There is an unused NULL entry at the head of the table which we
912 must account for in our count even if the table is empty since it
913 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
914 .dynamic section. */
915 dynsymcount++;
30b30c21 916
ccabcbe5
AM
917 elf_hash_table (info)->dynsymcount = dynsymcount;
918 return dynsymcount;
30b30c21 919}
252b5132 920
54ac0771
L
921/* Merge st_other field. */
922
923static void
924elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 925 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 926 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
927{
928 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
929
930 /* If st_other has a processor-specific meaning, specific
cd3416da 931 code might be needed here. */
54ac0771
L
932 if (bed->elf_backend_merge_symbol_attribute)
933 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
934 dynamic);
935
cd3416da 936 if (!dynamic)
54ac0771 937 {
cd3416da
AM
938 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
939 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 940
cd3416da
AM
941 /* Keep the most constraining visibility. Leave the remainder
942 of the st_other field to elf_backend_merge_symbol_attribute. */
943 if (symvis - 1 < hvis - 1)
944 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 945 }
b8417128
AM
946 else if (definition
947 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
948 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 949 h->protected_def = 1;
54ac0771
L
950}
951
4f3fedcf
AM
952/* This function is called when we want to merge a new symbol with an
953 existing symbol. It handles the various cases which arise when we
954 find a definition in a dynamic object, or when there is already a
955 definition in a dynamic object. The new symbol is described by
956 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
957 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
958 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
959 of an old common symbol. We set OVERRIDE if the old symbol is
960 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
961 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
962 to change. By OK to change, we mean that we shouldn't warn if the
963 type or size does change. */
45d6a902 964
8a56bd02 965static bfd_boolean
268b6b39
AM
966_bfd_elf_merge_symbol (bfd *abfd,
967 struct bfd_link_info *info,
968 const char *name,
969 Elf_Internal_Sym *sym,
970 asection **psec,
971 bfd_vma *pvalue,
4f3fedcf
AM
972 struct elf_link_hash_entry **sym_hash,
973 bfd **poldbfd,
37a9e49a 974 bfd_boolean *pold_weak,
af44c138 975 unsigned int *pold_alignment,
268b6b39
AM
976 bfd_boolean *skip,
977 bfd_boolean *override,
978 bfd_boolean *type_change_ok,
6e33951e
L
979 bfd_boolean *size_change_ok,
980 bfd_boolean *matched)
252b5132 981{
7479dfd4 982 asection *sec, *oldsec;
45d6a902 983 struct elf_link_hash_entry *h;
90c984fc 984 struct elf_link_hash_entry *hi;
45d6a902
AM
985 struct elf_link_hash_entry *flip;
986 int bind;
987 bfd *oldbfd;
988 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 989 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 990 const struct elf_backend_data *bed;
6e33951e 991 char *new_version;
45d6a902
AM
992
993 *skip = FALSE;
994 *override = FALSE;
995
996 sec = *psec;
997 bind = ELF_ST_BIND (sym->st_info);
998
999 if (! bfd_is_und_section (sec))
1000 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1001 else
1002 h = ((struct elf_link_hash_entry *)
1003 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1004 if (h == NULL)
1005 return FALSE;
1006 *sym_hash = h;
252b5132 1007
88ba32a0
L
1008 bed = get_elf_backend_data (abfd);
1009
6e33951e 1010 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1011 if (h->versioned != unversioned)
6e33951e 1012 {
422f1182
L
1013 /* Symbol version is unknown or versioned. */
1014 new_version = strrchr (name, ELF_VER_CHR);
1015 if (new_version)
1016 {
1017 if (h->versioned == unknown)
1018 {
1019 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1020 h->versioned = versioned_hidden;
1021 else
1022 h->versioned = versioned;
1023 }
1024 new_version += 1;
1025 if (new_version[0] == '\0')
1026 new_version = NULL;
1027 }
1028 else
1029 h->versioned = unversioned;
6e33951e 1030 }
422f1182
L
1031 else
1032 new_version = NULL;
6e33951e 1033
90c984fc
L
1034 /* For merging, we only care about real symbols. But we need to make
1035 sure that indirect symbol dynamic flags are updated. */
1036 hi = h;
45d6a902
AM
1037 while (h->root.type == bfd_link_hash_indirect
1038 || h->root.type == bfd_link_hash_warning)
1039 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1040
6e33951e
L
1041 if (!*matched)
1042 {
1043 if (hi == h || h->root.type == bfd_link_hash_new)
1044 *matched = TRUE;
1045 else
1046 {
ae7683d2 1047 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1048 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1049 true if the new symbol is only visible to the symbol with
6e33951e 1050 the same symbol version. */
422f1182
L
1051 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1052 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1053 if (!old_hidden && !new_hidden)
1054 /* The new symbol matches the existing symbol if both
1055 aren't hidden. */
1056 *matched = TRUE;
1057 else
1058 {
1059 /* OLD_VERSION is the symbol version of the existing
1060 symbol. */
422f1182
L
1061 char *old_version;
1062
1063 if (h->versioned >= versioned)
1064 old_version = strrchr (h->root.root.string,
1065 ELF_VER_CHR) + 1;
1066 else
1067 old_version = NULL;
6e33951e
L
1068
1069 /* The new symbol matches the existing symbol if they
1070 have the same symbol version. */
1071 *matched = (old_version == new_version
1072 || (old_version != NULL
1073 && new_version != NULL
1074 && strcmp (old_version, new_version) == 0));
1075 }
1076 }
1077 }
1078
934bce08
AM
1079 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1080 existing symbol. */
1081
1082 oldbfd = NULL;
1083 oldsec = NULL;
1084 switch (h->root.type)
1085 {
1086 default:
1087 break;
1088
1089 case bfd_link_hash_undefined:
1090 case bfd_link_hash_undefweak:
1091 oldbfd = h->root.u.undef.abfd;
1092 break;
1093
1094 case bfd_link_hash_defined:
1095 case bfd_link_hash_defweak:
1096 oldbfd = h->root.u.def.section->owner;
1097 oldsec = h->root.u.def.section;
1098 break;
1099
1100 case bfd_link_hash_common:
1101 oldbfd = h->root.u.c.p->section->owner;
1102 oldsec = h->root.u.c.p->section;
1103 if (pold_alignment)
1104 *pold_alignment = h->root.u.c.p->alignment_power;
1105 break;
1106 }
1107 if (poldbfd && *poldbfd == NULL)
1108 *poldbfd = oldbfd;
1109
1110 /* Differentiate strong and weak symbols. */
1111 newweak = bind == STB_WEAK;
1112 oldweak = (h->root.type == bfd_link_hash_defweak
1113 || h->root.type == bfd_link_hash_undefweak);
1114 if (pold_weak)
1115 *pold_weak = oldweak;
1116
1117 /* This code is for coping with dynamic objects, and is only useful
1118 if we are doing an ELF link. */
1119 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1120 return TRUE;
1121
40b36307 1122 /* We have to check it for every instance since the first few may be
ee659f1f 1123 references and not all compilers emit symbol type for undefined
40b36307
L
1124 symbols. */
1125 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1126
ee659f1f
AM
1127 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1128 respectively, is from a dynamic object. */
1129
1130 newdyn = (abfd->flags & DYNAMIC) != 0;
1131
1132 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1133 syms and defined syms in dynamic libraries respectively.
1134 ref_dynamic on the other hand can be set for a symbol defined in
1135 a dynamic library, and def_dynamic may not be set; When the
1136 definition in a dynamic lib is overridden by a definition in the
1137 executable use of the symbol in the dynamic lib becomes a
1138 reference to the executable symbol. */
1139 if (newdyn)
1140 {
1141 if (bfd_is_und_section (sec))
1142 {
1143 if (bind != STB_WEAK)
1144 {
1145 h->ref_dynamic_nonweak = 1;
1146 hi->ref_dynamic_nonweak = 1;
1147 }
1148 }
1149 else
1150 {
6e33951e
L
1151 /* Update the existing symbol only if they match. */
1152 if (*matched)
1153 h->dynamic_def = 1;
ee659f1f
AM
1154 hi->dynamic_def = 1;
1155 }
1156 }
1157
45d6a902
AM
1158 /* If we just created the symbol, mark it as being an ELF symbol.
1159 Other than that, there is nothing to do--there is no merge issue
1160 with a newly defined symbol--so we just return. */
1161
1162 if (h->root.type == bfd_link_hash_new)
252b5132 1163 {
f5385ebf 1164 h->non_elf = 0;
45d6a902
AM
1165 return TRUE;
1166 }
252b5132 1167
45d6a902
AM
1168 /* In cases involving weak versioned symbols, we may wind up trying
1169 to merge a symbol with itself. Catch that here, to avoid the
1170 confusion that results if we try to override a symbol with
1171 itself. The additional tests catch cases like
1172 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1173 dynamic object, which we do want to handle here. */
1174 if (abfd == oldbfd
895fa45f 1175 && (newweak || oldweak)
45d6a902 1176 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1177 || !h->def_regular))
45d6a902
AM
1178 return TRUE;
1179
707bba77 1180 olddyn = FALSE;
45d6a902
AM
1181 if (oldbfd != NULL)
1182 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1183 else if (oldsec != NULL)
45d6a902 1184 {
707bba77 1185 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1186 indices used by MIPS ELF. */
707bba77 1187 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1188 }
252b5132 1189
45d6a902
AM
1190 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1191 respectively, appear to be a definition rather than reference. */
1192
707bba77 1193 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1194
707bba77
AM
1195 olddef = (h->root.type != bfd_link_hash_undefined
1196 && h->root.type != bfd_link_hash_undefweak
202ac193 1197 && h->root.type != bfd_link_hash_common);
45d6a902 1198
0a36a439
L
1199 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1200 respectively, appear to be a function. */
1201
1202 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1203 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1204
1205 oldfunc = (h->type != STT_NOTYPE
1206 && bed->is_function_type (h->type));
1207
5b677558
AM
1208 /* If creating a default indirect symbol ("foo" or "foo@") from a
1209 dynamic versioned definition ("foo@@") skip doing so if there is
1210 an existing regular definition with a different type. We don't
1211 want, for example, a "time" variable in the executable overriding
1212 a "time" function in a shared library. */
580a2b6e 1213 if (pold_alignment == NULL
580a2b6e
L
1214 && newdyn
1215 && newdef
1216 && !olddyn
5b677558
AM
1217 && (olddef || h->root.type == bfd_link_hash_common)
1218 && ELF_ST_TYPE (sym->st_info) != h->type
1219 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1220 && h->type != STT_NOTYPE
1221 && !(newfunc && oldfunc))
580a2b6e
L
1222 {
1223 *skip = TRUE;
1224 return TRUE;
1225 }
1226
4c34aff8
AM
1227 /* Check TLS symbols. We don't check undefined symbols introduced
1228 by "ld -u" which have no type (and oldbfd NULL), and we don't
1229 check symbols from plugins because they also have no type. */
1230 if (oldbfd != NULL
1231 && (oldbfd->flags & BFD_PLUGIN) == 0
1232 && (abfd->flags & BFD_PLUGIN) == 0
1233 && ELF_ST_TYPE (sym->st_info) != h->type
1234 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1235 {
1236 bfd *ntbfd, *tbfd;
1237 bfd_boolean ntdef, tdef;
1238 asection *ntsec, *tsec;
1239
1240 if (h->type == STT_TLS)
1241 {
3b36f7e6 1242 ntbfd = abfd;
7479dfd4
L
1243 ntsec = sec;
1244 ntdef = newdef;
1245 tbfd = oldbfd;
1246 tsec = oldsec;
1247 tdef = olddef;
1248 }
1249 else
1250 {
1251 ntbfd = oldbfd;
1252 ntsec = oldsec;
1253 ntdef = olddef;
1254 tbfd = abfd;
1255 tsec = sec;
1256 tdef = newdef;
1257 }
1258
1259 if (tdef && ntdef)
1260 (*_bfd_error_handler)
191c0c42
AM
1261 (_("%s: TLS definition in %B section %A "
1262 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1263 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1264 else if (!tdef && !ntdef)
1265 (*_bfd_error_handler)
191c0c42
AM
1266 (_("%s: TLS reference in %B "
1267 "mismatches non-TLS reference in %B"),
7479dfd4
L
1268 tbfd, ntbfd, h->root.root.string);
1269 else if (tdef)
1270 (*_bfd_error_handler)
191c0c42
AM
1271 (_("%s: TLS definition in %B section %A "
1272 "mismatches non-TLS reference in %B"),
7479dfd4
L
1273 tbfd, tsec, ntbfd, h->root.root.string);
1274 else
1275 (*_bfd_error_handler)
191c0c42
AM
1276 (_("%s: TLS reference in %B "
1277 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1278 tbfd, ntbfd, ntsec, h->root.root.string);
1279
1280 bfd_set_error (bfd_error_bad_value);
1281 return FALSE;
1282 }
1283
45d6a902
AM
1284 /* If the old symbol has non-default visibility, we ignore the new
1285 definition from a dynamic object. */
1286 if (newdyn
9c7a29a3 1287 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1288 && !bfd_is_und_section (sec))
1289 {
1290 *skip = TRUE;
1291 /* Make sure this symbol is dynamic. */
f5385ebf 1292 h->ref_dynamic = 1;
90c984fc 1293 hi->ref_dynamic = 1;
45d6a902
AM
1294 /* A protected symbol has external availability. Make sure it is
1295 recorded as dynamic.
1296
1297 FIXME: Should we check type and size for protected symbol? */
1298 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1299 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1300 else
1301 return TRUE;
1302 }
1303 else if (!newdyn
9c7a29a3 1304 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1305 && h->def_dynamic)
45d6a902
AM
1306 {
1307 /* If the new symbol with non-default visibility comes from a
1308 relocatable file and the old definition comes from a dynamic
1309 object, we remove the old definition. */
6c9b78e6 1310 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1311 {
1312 /* Handle the case where the old dynamic definition is
1313 default versioned. We need to copy the symbol info from
1314 the symbol with default version to the normal one if it
1315 was referenced before. */
1316 if (h->ref_regular)
1317 {
6c9b78e6 1318 hi->root.type = h->root.type;
d2dee3b2 1319 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1320 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1321
6c9b78e6 1322 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1323 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1324 {
aed81c4e
MR
1325 /* If the new symbol is hidden or internal, completely undo
1326 any dynamic link state. */
1327 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1328 h->forced_local = 0;
1329 h->ref_dynamic = 0;
d2dee3b2
L
1330 }
1331 else
aed81c4e
MR
1332 h->ref_dynamic = 1;
1333
1334 h->def_dynamic = 0;
aed81c4e
MR
1335 /* FIXME: Should we check type and size for protected symbol? */
1336 h->size = 0;
1337 h->type = 0;
1338
6c9b78e6 1339 h = hi;
d2dee3b2
L
1340 }
1341 else
6c9b78e6 1342 h = hi;
d2dee3b2 1343 }
1de1a317 1344
f5eda473
AM
1345 /* If the old symbol was undefined before, then it will still be
1346 on the undefs list. If the new symbol is undefined or
1347 common, we can't make it bfd_link_hash_new here, because new
1348 undefined or common symbols will be added to the undefs list
1349 by _bfd_generic_link_add_one_symbol. Symbols may not be
1350 added twice to the undefs list. Also, if the new symbol is
1351 undefweak then we don't want to lose the strong undef. */
1352 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1353 {
1de1a317 1354 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1355 h->root.u.undef.abfd = abfd;
1356 }
1357 else
1358 {
1359 h->root.type = bfd_link_hash_new;
1360 h->root.u.undef.abfd = NULL;
1361 }
1362
f5eda473 1363 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1364 {
f5eda473
AM
1365 /* If the new symbol is hidden or internal, completely undo
1366 any dynamic link state. */
1367 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1368 h->forced_local = 0;
1369 h->ref_dynamic = 0;
45d6a902 1370 }
f5eda473
AM
1371 else
1372 h->ref_dynamic = 1;
1373 h->def_dynamic = 0;
45d6a902
AM
1374 /* FIXME: Should we check type and size for protected symbol? */
1375 h->size = 0;
1376 h->type = 0;
1377 return TRUE;
1378 }
14a793b2 1379
15b43f48
AM
1380 /* If a new weak symbol definition comes from a regular file and the
1381 old symbol comes from a dynamic library, we treat the new one as
1382 strong. Similarly, an old weak symbol definition from a regular
1383 file is treated as strong when the new symbol comes from a dynamic
1384 library. Further, an old weak symbol from a dynamic library is
1385 treated as strong if the new symbol is from a dynamic library.
1386 This reflects the way glibc's ld.so works.
1387
1388 Do this before setting *type_change_ok or *size_change_ok so that
1389 we warn properly when dynamic library symbols are overridden. */
1390
1391 if (newdef && !newdyn && olddyn)
0f8a2703 1392 newweak = FALSE;
15b43f48 1393 if (olddef && newdyn)
0f8a2703
AM
1394 oldweak = FALSE;
1395
d334575b 1396 /* Allow changes between different types of function symbol. */
0a36a439 1397 if (newfunc && oldfunc)
fcb93ecf
PB
1398 *type_change_ok = TRUE;
1399
79349b09
AM
1400 /* It's OK to change the type if either the existing symbol or the
1401 new symbol is weak. A type change is also OK if the old symbol
1402 is undefined and the new symbol is defined. */
252b5132 1403
79349b09
AM
1404 if (oldweak
1405 || newweak
1406 || (newdef
1407 && h->root.type == bfd_link_hash_undefined))
1408 *type_change_ok = TRUE;
1409
1410 /* It's OK to change the size if either the existing symbol or the
1411 new symbol is weak, or if the old symbol is undefined. */
1412
1413 if (*type_change_ok
1414 || h->root.type == bfd_link_hash_undefined)
1415 *size_change_ok = TRUE;
45d6a902 1416
45d6a902
AM
1417 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1418 symbol, respectively, appears to be a common symbol in a dynamic
1419 object. If a symbol appears in an uninitialized section, and is
1420 not weak, and is not a function, then it may be a common symbol
1421 which was resolved when the dynamic object was created. We want
1422 to treat such symbols specially, because they raise special
1423 considerations when setting the symbol size: if the symbol
1424 appears as a common symbol in a regular object, and the size in
1425 the regular object is larger, we must make sure that we use the
1426 larger size. This problematic case can always be avoided in C,
1427 but it must be handled correctly when using Fortran shared
1428 libraries.
1429
1430 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1431 likewise for OLDDYNCOMMON and OLDDEF.
1432
1433 Note that this test is just a heuristic, and that it is quite
1434 possible to have an uninitialized symbol in a shared object which
1435 is really a definition, rather than a common symbol. This could
1436 lead to some minor confusion when the symbol really is a common
1437 symbol in some regular object. However, I think it will be
1438 harmless. */
1439
1440 if (newdyn
1441 && newdef
79349b09 1442 && !newweak
45d6a902
AM
1443 && (sec->flags & SEC_ALLOC) != 0
1444 && (sec->flags & SEC_LOAD) == 0
1445 && sym->st_size > 0
0a36a439 1446 && !newfunc)
45d6a902
AM
1447 newdyncommon = TRUE;
1448 else
1449 newdyncommon = FALSE;
1450
1451 if (olddyn
1452 && olddef
1453 && h->root.type == bfd_link_hash_defined
f5385ebf 1454 && h->def_dynamic
45d6a902
AM
1455 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1456 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1457 && h->size > 0
0a36a439 1458 && !oldfunc)
45d6a902
AM
1459 olddyncommon = TRUE;
1460 else
1461 olddyncommon = FALSE;
1462
a4d8e49b
L
1463 /* We now know everything about the old and new symbols. We ask the
1464 backend to check if we can merge them. */
5d13b3b3
AM
1465 if (bed->merge_symbol != NULL)
1466 {
1467 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1468 return FALSE;
1469 sec = *psec;
1470 }
a4d8e49b 1471
45d6a902
AM
1472 /* If both the old and the new symbols look like common symbols in a
1473 dynamic object, set the size of the symbol to the larger of the
1474 two. */
1475
1476 if (olddyncommon
1477 && newdyncommon
1478 && sym->st_size != h->size)
1479 {
1480 /* Since we think we have two common symbols, issue a multiple
1481 common warning if desired. Note that we only warn if the
1482 size is different. If the size is the same, we simply let
1483 the old symbol override the new one as normally happens with
1484 symbols defined in dynamic objects. */
1485
1a72702b
AM
1486 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1487 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1488 if (sym->st_size > h->size)
1489 h->size = sym->st_size;
252b5132 1490
45d6a902 1491 *size_change_ok = TRUE;
252b5132
RH
1492 }
1493
45d6a902
AM
1494 /* If we are looking at a dynamic object, and we have found a
1495 definition, we need to see if the symbol was already defined by
1496 some other object. If so, we want to use the existing
1497 definition, and we do not want to report a multiple symbol
1498 definition error; we do this by clobbering *PSEC to be
1499 bfd_und_section_ptr.
1500
1501 We treat a common symbol as a definition if the symbol in the
1502 shared library is a function, since common symbols always
1503 represent variables; this can cause confusion in principle, but
1504 any such confusion would seem to indicate an erroneous program or
1505 shared library. We also permit a common symbol in a regular
202ac193
L
1506 object to override a weak symbol in a shared object. A common
1507 symbol in executable also overrides a symbol in a shared object. */
45d6a902
AM
1508
1509 if (newdyn
1510 && newdef
77cfaee6 1511 && (olddef
45d6a902 1512 || (h->root.type == bfd_link_hash_common
202ac193
L
1513 && (newweak
1514 || newfunc
1515 || (!olddyn && bfd_link_executable (info))))))
45d6a902
AM
1516 {
1517 *override = TRUE;
1518 newdef = FALSE;
1519 newdyncommon = FALSE;
252b5132 1520
45d6a902
AM
1521 *psec = sec = bfd_und_section_ptr;
1522 *size_change_ok = TRUE;
252b5132 1523
45d6a902
AM
1524 /* If we get here when the old symbol is a common symbol, then
1525 we are explicitly letting it override a weak symbol or
1526 function in a dynamic object, and we don't want to warn about
1527 a type change. If the old symbol is a defined symbol, a type
1528 change warning may still be appropriate. */
252b5132 1529
45d6a902
AM
1530 if (h->root.type == bfd_link_hash_common)
1531 *type_change_ok = TRUE;
1532 }
1533
1534 /* Handle the special case of an old common symbol merging with a
1535 new symbol which looks like a common symbol in a shared object.
1536 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1537 common symbol, and let _bfd_generic_link_add_one_symbol do the
1538 right thing. */
45d6a902
AM
1539
1540 if (newdyncommon
1541 && h->root.type == bfd_link_hash_common)
1542 {
1543 *override = TRUE;
1544 newdef = FALSE;
1545 newdyncommon = FALSE;
1546 *pvalue = sym->st_size;
a4d8e49b 1547 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1548 *size_change_ok = TRUE;
1549 }
1550
c5e2cead 1551 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1552 if (newdef && olddef && newweak)
54ac0771 1553 {
35ed3f94 1554 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1555 if (!(oldbfd != NULL
1556 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1557 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1558 {
1559 newdef = FALSE;
1560 *skip = TRUE;
1561 }
54ac0771
L
1562
1563 /* Merge st_other. If the symbol already has a dynamic index,
1564 but visibility says it should not be visible, turn it into a
1565 local symbol. */
b8417128 1566 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1567 if (h->dynindx != -1)
1568 switch (ELF_ST_VISIBILITY (h->other))
1569 {
1570 case STV_INTERNAL:
1571 case STV_HIDDEN:
1572 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1573 break;
1574 }
1575 }
c5e2cead 1576
45d6a902
AM
1577 /* If the old symbol is from a dynamic object, and the new symbol is
1578 a definition which is not from a dynamic object, then the new
1579 symbol overrides the old symbol. Symbols from regular files
1580 always take precedence over symbols from dynamic objects, even if
1581 they are defined after the dynamic object in the link.
1582
1583 As above, we again permit a common symbol in a regular object to
1584 override a definition in a shared object if the shared object
0f8a2703 1585 symbol is a function or is weak. */
45d6a902
AM
1586
1587 flip = NULL;
77cfaee6 1588 if (!newdyn
45d6a902
AM
1589 && (newdef
1590 || (bfd_is_com_section (sec)
0a36a439 1591 && (oldweak || oldfunc)))
45d6a902
AM
1592 && olddyn
1593 && olddef
f5385ebf 1594 && h->def_dynamic)
45d6a902
AM
1595 {
1596 /* Change the hash table entry to undefined, and let
1597 _bfd_generic_link_add_one_symbol do the right thing with the
1598 new definition. */
1599
1600 h->root.type = bfd_link_hash_undefined;
1601 h->root.u.undef.abfd = h->root.u.def.section->owner;
1602 *size_change_ok = TRUE;
1603
1604 olddef = FALSE;
1605 olddyncommon = FALSE;
1606
1607 /* We again permit a type change when a common symbol may be
1608 overriding a function. */
1609
1610 if (bfd_is_com_section (sec))
0a36a439
L
1611 {
1612 if (oldfunc)
1613 {
1614 /* If a common symbol overrides a function, make sure
1615 that it isn't defined dynamically nor has type
1616 function. */
1617 h->def_dynamic = 0;
1618 h->type = STT_NOTYPE;
1619 }
1620 *type_change_ok = TRUE;
1621 }
45d6a902 1622
6c9b78e6
AM
1623 if (hi->root.type == bfd_link_hash_indirect)
1624 flip = hi;
45d6a902
AM
1625 else
1626 /* This union may have been set to be non-NULL when this symbol
1627 was seen in a dynamic object. We must force the union to be
1628 NULL, so that it is correct for a regular symbol. */
1629 h->verinfo.vertree = NULL;
1630 }
1631
1632 /* Handle the special case of a new common symbol merging with an
1633 old symbol that looks like it might be a common symbol defined in
1634 a shared object. Note that we have already handled the case in
1635 which a new common symbol should simply override the definition
1636 in the shared library. */
1637
1638 if (! newdyn
1639 && bfd_is_com_section (sec)
1640 && olddyncommon)
1641 {
1642 /* It would be best if we could set the hash table entry to a
1643 common symbol, but we don't know what to use for the section
1644 or the alignment. */
1a72702b
AM
1645 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1646 bfd_link_hash_common, sym->st_size);
45d6a902 1647
4cc11e76 1648 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1649 larger, pretend that the new symbol has its size. */
1650
1651 if (h->size > *pvalue)
1652 *pvalue = h->size;
1653
af44c138
L
1654 /* We need to remember the alignment required by the symbol
1655 in the dynamic object. */
1656 BFD_ASSERT (pold_alignment);
1657 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1658
1659 olddef = FALSE;
1660 olddyncommon = FALSE;
1661
1662 h->root.type = bfd_link_hash_undefined;
1663 h->root.u.undef.abfd = h->root.u.def.section->owner;
1664
1665 *size_change_ok = TRUE;
1666 *type_change_ok = TRUE;
1667
6c9b78e6
AM
1668 if (hi->root.type == bfd_link_hash_indirect)
1669 flip = hi;
45d6a902
AM
1670 else
1671 h->verinfo.vertree = NULL;
1672 }
1673
1674 if (flip != NULL)
1675 {
1676 /* Handle the case where we had a versioned symbol in a dynamic
1677 library and now find a definition in a normal object. In this
1678 case, we make the versioned symbol point to the normal one. */
45d6a902 1679 flip->root.type = h->root.type;
00cbee0a 1680 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1681 h->root.type = bfd_link_hash_indirect;
1682 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1683 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1684 if (h->def_dynamic)
45d6a902 1685 {
f5385ebf
AM
1686 h->def_dynamic = 0;
1687 flip->ref_dynamic = 1;
45d6a902
AM
1688 }
1689 }
1690
45d6a902
AM
1691 return TRUE;
1692}
1693
1694/* This function is called to create an indirect symbol from the
1695 default for the symbol with the default version if needed. The
4f3fedcf 1696 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1697 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1698
28caa186 1699static bfd_boolean
268b6b39
AM
1700_bfd_elf_add_default_symbol (bfd *abfd,
1701 struct bfd_link_info *info,
1702 struct elf_link_hash_entry *h,
1703 const char *name,
1704 Elf_Internal_Sym *sym,
4f3fedcf
AM
1705 asection *sec,
1706 bfd_vma value,
1707 bfd **poldbfd,
e3c9d234 1708 bfd_boolean *dynsym)
45d6a902
AM
1709{
1710 bfd_boolean type_change_ok;
1711 bfd_boolean size_change_ok;
1712 bfd_boolean skip;
1713 char *shortname;
1714 struct elf_link_hash_entry *hi;
1715 struct bfd_link_hash_entry *bh;
9c5bfbb7 1716 const struct elf_backend_data *bed;
45d6a902
AM
1717 bfd_boolean collect;
1718 bfd_boolean dynamic;
e3c9d234 1719 bfd_boolean override;
45d6a902
AM
1720 char *p;
1721 size_t len, shortlen;
ffd65175 1722 asection *tmp_sec;
6e33951e 1723 bfd_boolean matched;
45d6a902 1724
422f1182
L
1725 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1726 return TRUE;
1727
45d6a902
AM
1728 /* If this symbol has a version, and it is the default version, we
1729 create an indirect symbol from the default name to the fully
1730 decorated name. This will cause external references which do not
1731 specify a version to be bound to this version of the symbol. */
1732 p = strchr (name, ELF_VER_CHR);
422f1182
L
1733 if (h->versioned == unknown)
1734 {
1735 if (p == NULL)
1736 {
1737 h->versioned = unversioned;
1738 return TRUE;
1739 }
1740 else
1741 {
1742 if (p[1] != ELF_VER_CHR)
1743 {
1744 h->versioned = versioned_hidden;
1745 return TRUE;
1746 }
1747 else
1748 h->versioned = versioned;
1749 }
1750 }
4373f8af
L
1751 else
1752 {
1753 /* PR ld/19073: We may see an unversioned definition after the
1754 default version. */
1755 if (p == NULL)
1756 return TRUE;
1757 }
45d6a902 1758
45d6a902
AM
1759 bed = get_elf_backend_data (abfd);
1760 collect = bed->collect;
1761 dynamic = (abfd->flags & DYNAMIC) != 0;
1762
1763 shortlen = p - name;
a50b1753 1764 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1765 if (shortname == NULL)
1766 return FALSE;
1767 memcpy (shortname, name, shortlen);
1768 shortname[shortlen] = '\0';
1769
1770 /* We are going to create a new symbol. Merge it with any existing
1771 symbol with this name. For the purposes of the merge, act as
1772 though we were defining the symbol we just defined, although we
1773 actually going to define an indirect symbol. */
1774 type_change_ok = FALSE;
1775 size_change_ok = FALSE;
6e33951e 1776 matched = TRUE;
ffd65175
AM
1777 tmp_sec = sec;
1778 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1779 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1780 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1781 return FALSE;
1782
1783 if (skip)
1784 goto nondefault;
1785
5b677558
AM
1786 if (hi->def_regular)
1787 {
1788 /* If the undecorated symbol will have a version added by a
1789 script different to H, then don't indirect to/from the
1790 undecorated symbol. This isn't ideal because we may not yet
1791 have seen symbol versions, if given by a script on the
1792 command line rather than via --version-script. */
1793 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1794 {
1795 bfd_boolean hide;
1796
1797 hi->verinfo.vertree
1798 = bfd_find_version_for_sym (info->version_info,
1799 hi->root.root.string, &hide);
1800 if (hi->verinfo.vertree != NULL && hide)
1801 {
1802 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1803 goto nondefault;
1804 }
1805 }
1806 if (hi->verinfo.vertree != NULL
1807 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1808 goto nondefault;
1809 }
1810
45d6a902
AM
1811 if (! override)
1812 {
c6e8a9a8 1813 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1814 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1815 {
1816 bh = &hi->root;
1817 if (! (_bfd_generic_link_add_one_symbol
1818 (info, abfd, shortname, BSF_INDIRECT,
1819 bfd_ind_section_ptr,
1820 0, name, FALSE, collect, &bh)))
1821 return FALSE;
1822 hi = (struct elf_link_hash_entry *) bh;
1823 }
45d6a902
AM
1824 }
1825 else
1826 {
1827 /* In this case the symbol named SHORTNAME is overriding the
1828 indirect symbol we want to add. We were planning on making
1829 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1830 is the name without a version. NAME is the fully versioned
1831 name, and it is the default version.
1832
1833 Overriding means that we already saw a definition for the
1834 symbol SHORTNAME in a regular object, and it is overriding
1835 the symbol defined in the dynamic object.
1836
1837 When this happens, we actually want to change NAME, the
1838 symbol we just added, to refer to SHORTNAME. This will cause
1839 references to NAME in the shared object to become references
1840 to SHORTNAME in the regular object. This is what we expect
1841 when we override a function in a shared object: that the
1842 references in the shared object will be mapped to the
1843 definition in the regular object. */
1844
1845 while (hi->root.type == bfd_link_hash_indirect
1846 || hi->root.type == bfd_link_hash_warning)
1847 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1848
1849 h->root.type = bfd_link_hash_indirect;
1850 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1851 if (h->def_dynamic)
45d6a902 1852 {
f5385ebf
AM
1853 h->def_dynamic = 0;
1854 hi->ref_dynamic = 1;
1855 if (hi->ref_regular
1856 || hi->def_regular)
45d6a902 1857 {
c152c796 1858 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1859 return FALSE;
1860 }
1861 }
1862
1863 /* Now set HI to H, so that the following code will set the
1864 other fields correctly. */
1865 hi = h;
1866 }
1867
fab4a87f
L
1868 /* Check if HI is a warning symbol. */
1869 if (hi->root.type == bfd_link_hash_warning)
1870 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1871
45d6a902
AM
1872 /* If there is a duplicate definition somewhere, then HI may not
1873 point to an indirect symbol. We will have reported an error to
1874 the user in that case. */
1875
1876 if (hi->root.type == bfd_link_hash_indirect)
1877 {
1878 struct elf_link_hash_entry *ht;
1879
45d6a902 1880 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1881 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1882
68c88cd4
AM
1883 /* A reference to the SHORTNAME symbol from a dynamic library
1884 will be satisfied by the versioned symbol at runtime. In
1885 effect, we have a reference to the versioned symbol. */
1886 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1887 hi->dynamic_def |= ht->dynamic_def;
1888
45d6a902
AM
1889 /* See if the new flags lead us to realize that the symbol must
1890 be dynamic. */
1891 if (! *dynsym)
1892 {
1893 if (! dynamic)
1894 {
0e1862bb 1895 if (! bfd_link_executable (info)
90c984fc 1896 || hi->def_dynamic
f5385ebf 1897 || hi->ref_dynamic)
45d6a902
AM
1898 *dynsym = TRUE;
1899 }
1900 else
1901 {
f5385ebf 1902 if (hi->ref_regular)
45d6a902
AM
1903 *dynsym = TRUE;
1904 }
1905 }
1906 }
1907
1908 /* We also need to define an indirection from the nondefault version
1909 of the symbol. */
1910
1911nondefault:
1912 len = strlen (name);
a50b1753 1913 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1914 if (shortname == NULL)
1915 return FALSE;
1916 memcpy (shortname, name, shortlen);
1917 memcpy (shortname + shortlen, p + 1, len - shortlen);
1918
1919 /* Once again, merge with any existing symbol. */
1920 type_change_ok = FALSE;
1921 size_change_ok = FALSE;
ffd65175
AM
1922 tmp_sec = sec;
1923 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1924 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1925 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1926 return FALSE;
1927
1928 if (skip)
1929 return TRUE;
1930
1931 if (override)
1932 {
1933 /* Here SHORTNAME is a versioned name, so we don't expect to see
1934 the type of override we do in the case above unless it is
4cc11e76 1935 overridden by a versioned definition. */
45d6a902
AM
1936 if (hi->root.type != bfd_link_hash_defined
1937 && hi->root.type != bfd_link_hash_defweak)
1938 (*_bfd_error_handler)
d003868e
AM
1939 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1940 abfd, shortname);
45d6a902
AM
1941 }
1942 else
1943 {
1944 bh = &hi->root;
1945 if (! (_bfd_generic_link_add_one_symbol
1946 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1947 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1948 return FALSE;
1949 hi = (struct elf_link_hash_entry *) bh;
1950
1951 /* If there is a duplicate definition somewhere, then HI may not
1952 point to an indirect symbol. We will have reported an error
1953 to the user in that case. */
1954
1955 if (hi->root.type == bfd_link_hash_indirect)
1956 {
fcfa13d2 1957 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
1958 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1959 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
1960
1961 /* See if the new flags lead us to realize that the symbol
1962 must be dynamic. */
1963 if (! *dynsym)
1964 {
1965 if (! dynamic)
1966 {
0e1862bb 1967 if (! bfd_link_executable (info)
f5385ebf 1968 || hi->ref_dynamic)
45d6a902
AM
1969 *dynsym = TRUE;
1970 }
1971 else
1972 {
f5385ebf 1973 if (hi->ref_regular)
45d6a902
AM
1974 *dynsym = TRUE;
1975 }
1976 }
1977 }
1978 }
1979
1980 return TRUE;
1981}
1982\f
1983/* This routine is used to export all defined symbols into the dynamic
1984 symbol table. It is called via elf_link_hash_traverse. */
1985
28caa186 1986static bfd_boolean
268b6b39 1987_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1988{
a50b1753 1989 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
1990
1991 /* Ignore indirect symbols. These are added by the versioning code. */
1992 if (h->root.type == bfd_link_hash_indirect)
1993 return TRUE;
1994
7686d77d
AM
1995 /* Ignore this if we won't export it. */
1996 if (!eif->info->export_dynamic && !h->dynamic)
1997 return TRUE;
45d6a902
AM
1998
1999 if (h->dynindx == -1
fd91d419
L
2000 && (h->def_regular || h->ref_regular)
2001 && ! bfd_hide_sym_by_version (eif->info->version_info,
2002 h->root.root.string))
45d6a902 2003 {
fd91d419 2004 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2005 {
fd91d419
L
2006 eif->failed = TRUE;
2007 return FALSE;
45d6a902
AM
2008 }
2009 }
2010
2011 return TRUE;
2012}
2013\f
2014/* Look through the symbols which are defined in other shared
2015 libraries and referenced here. Update the list of version
2016 dependencies. This will be put into the .gnu.version_r section.
2017 This function is called via elf_link_hash_traverse. */
2018
28caa186 2019static bfd_boolean
268b6b39
AM
2020_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2021 void *data)
45d6a902 2022{
a50b1753 2023 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2024 Elf_Internal_Verneed *t;
2025 Elf_Internal_Vernaux *a;
2026 bfd_size_type amt;
2027
45d6a902
AM
2028 /* We only care about symbols defined in shared objects with version
2029 information. */
f5385ebf
AM
2030 if (!h->def_dynamic
2031 || h->def_regular
45d6a902 2032 || h->dynindx == -1
7b20f099
AM
2033 || h->verinfo.verdef == NULL
2034 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2035 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2036 return TRUE;
2037
2038 /* See if we already know about this version. */
28caa186
AM
2039 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2040 t != NULL;
2041 t = t->vn_nextref)
45d6a902
AM
2042 {
2043 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2044 continue;
2045
2046 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2047 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2048 return TRUE;
2049
2050 break;
2051 }
2052
2053 /* This is a new version. Add it to tree we are building. */
2054
2055 if (t == NULL)
2056 {
2057 amt = sizeof *t;
a50b1753 2058 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2059 if (t == NULL)
2060 {
2061 rinfo->failed = TRUE;
2062 return FALSE;
2063 }
2064
2065 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2066 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2067 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2068 }
2069
2070 amt = sizeof *a;
a50b1753 2071 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2072 if (a == NULL)
2073 {
2074 rinfo->failed = TRUE;
2075 return FALSE;
2076 }
45d6a902
AM
2077
2078 /* Note that we are copying a string pointer here, and testing it
2079 above. If bfd_elf_string_from_elf_section is ever changed to
2080 discard the string data when low in memory, this will have to be
2081 fixed. */
2082 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2083
2084 a->vna_flags = h->verinfo.verdef->vd_flags;
2085 a->vna_nextptr = t->vn_auxptr;
2086
2087 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2088 ++rinfo->vers;
2089
2090 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2091
2092 t->vn_auxptr = a;
2093
2094 return TRUE;
2095}
2096
2097/* Figure out appropriate versions for all the symbols. We may not
2098 have the version number script until we have read all of the input
2099 files, so until that point we don't know which symbols should be
2100 local. This function is called via elf_link_hash_traverse. */
2101
28caa186 2102static bfd_boolean
268b6b39 2103_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2104{
28caa186 2105 struct elf_info_failed *sinfo;
45d6a902 2106 struct bfd_link_info *info;
9c5bfbb7 2107 const struct elf_backend_data *bed;
45d6a902
AM
2108 struct elf_info_failed eif;
2109 char *p;
45d6a902 2110
a50b1753 2111 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2112 info = sinfo->info;
2113
45d6a902
AM
2114 /* Fix the symbol flags. */
2115 eif.failed = FALSE;
2116 eif.info = info;
2117 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2118 {
2119 if (eif.failed)
2120 sinfo->failed = TRUE;
2121 return FALSE;
2122 }
2123
2124 /* We only need version numbers for symbols defined in regular
2125 objects. */
f5385ebf 2126 if (!h->def_regular)
45d6a902
AM
2127 return TRUE;
2128
28caa186 2129 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2130 p = strchr (h->root.root.string, ELF_VER_CHR);
2131 if (p != NULL && h->verinfo.vertree == NULL)
2132 {
2133 struct bfd_elf_version_tree *t;
45d6a902 2134
45d6a902
AM
2135 ++p;
2136 if (*p == ELF_VER_CHR)
6e33951e 2137 ++p;
45d6a902
AM
2138
2139 /* If there is no version string, we can just return out. */
2140 if (*p == '\0')
6e33951e 2141 return TRUE;
45d6a902
AM
2142
2143 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2144 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2145 {
2146 if (strcmp (t->name, p) == 0)
2147 {
2148 size_t len;
2149 char *alc;
2150 struct bfd_elf_version_expr *d;
2151
2152 len = p - h->root.root.string;
a50b1753 2153 alc = (char *) bfd_malloc (len);
45d6a902 2154 if (alc == NULL)
14b1c01e
AM
2155 {
2156 sinfo->failed = TRUE;
2157 return FALSE;
2158 }
45d6a902
AM
2159 memcpy (alc, h->root.root.string, len - 1);
2160 alc[len - 1] = '\0';
2161 if (alc[len - 2] == ELF_VER_CHR)
2162 alc[len - 2] = '\0';
2163
2164 h->verinfo.vertree = t;
2165 t->used = TRUE;
2166 d = NULL;
2167
108ba305
JJ
2168 if (t->globals.list != NULL)
2169 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2170
2171 /* See if there is anything to force this symbol to
2172 local scope. */
108ba305 2173 if (d == NULL && t->locals.list != NULL)
45d6a902 2174 {
108ba305
JJ
2175 d = (*t->match) (&t->locals, NULL, alc);
2176 if (d != NULL
2177 && h->dynindx != -1
108ba305
JJ
2178 && ! info->export_dynamic)
2179 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2180 }
2181
2182 free (alc);
2183 break;
2184 }
2185 }
2186
2187 /* If we are building an application, we need to create a
2188 version node for this version. */
0e1862bb 2189 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2190 {
2191 struct bfd_elf_version_tree **pp;
2192 int version_index;
2193
2194 /* If we aren't going to export this symbol, we don't need
2195 to worry about it. */
2196 if (h->dynindx == -1)
2197 return TRUE;
2198
ef53be89
AM
2199 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2200 sizeof *t);
45d6a902
AM
2201 if (t == NULL)
2202 {
2203 sinfo->failed = TRUE;
2204 return FALSE;
2205 }
2206
45d6a902 2207 t->name = p;
45d6a902
AM
2208 t->name_indx = (unsigned int) -1;
2209 t->used = TRUE;
2210
2211 version_index = 1;
2212 /* Don't count anonymous version tag. */
fd91d419
L
2213 if (sinfo->info->version_info != NULL
2214 && sinfo->info->version_info->vernum == 0)
45d6a902 2215 version_index = 0;
fd91d419
L
2216 for (pp = &sinfo->info->version_info;
2217 *pp != NULL;
2218 pp = &(*pp)->next)
45d6a902
AM
2219 ++version_index;
2220 t->vernum = version_index;
2221
2222 *pp = t;
2223
2224 h->verinfo.vertree = t;
2225 }
2226 else if (t == NULL)
2227 {
2228 /* We could not find the version for a symbol when
2229 generating a shared archive. Return an error. */
2230 (*_bfd_error_handler)
c55fe096 2231 (_("%B: version node not found for symbol %s"),
28caa186 2232 info->output_bfd, h->root.root.string);
45d6a902
AM
2233 bfd_set_error (bfd_error_bad_value);
2234 sinfo->failed = TRUE;
2235 return FALSE;
2236 }
45d6a902
AM
2237 }
2238
2239 /* If we don't have a version for this symbol, see if we can find
2240 something. */
fd91d419 2241 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2242 {
1e8fa21e 2243 bfd_boolean hide;
ae5a3597 2244
fd91d419
L
2245 h->verinfo.vertree
2246 = bfd_find_version_for_sym (sinfo->info->version_info,
2247 h->root.root.string, &hide);
1e8fa21e
AM
2248 if (h->verinfo.vertree != NULL && hide)
2249 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2250 }
2251
2252 return TRUE;
2253}
2254\f
45d6a902
AM
2255/* Read and swap the relocs from the section indicated by SHDR. This
2256 may be either a REL or a RELA section. The relocations are
2257 translated into RELA relocations and stored in INTERNAL_RELOCS,
2258 which should have already been allocated to contain enough space.
2259 The EXTERNAL_RELOCS are a buffer where the external form of the
2260 relocations should be stored.
2261
2262 Returns FALSE if something goes wrong. */
2263
2264static bfd_boolean
268b6b39 2265elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2266 asection *sec,
268b6b39
AM
2267 Elf_Internal_Shdr *shdr,
2268 void *external_relocs,
2269 Elf_Internal_Rela *internal_relocs)
45d6a902 2270{
9c5bfbb7 2271 const struct elf_backend_data *bed;
268b6b39 2272 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2273 const bfd_byte *erela;
2274 const bfd_byte *erelaend;
2275 Elf_Internal_Rela *irela;
243ef1e0
L
2276 Elf_Internal_Shdr *symtab_hdr;
2277 size_t nsyms;
45d6a902 2278
45d6a902
AM
2279 /* Position ourselves at the start of the section. */
2280 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2281 return FALSE;
2282
2283 /* Read the relocations. */
2284 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2285 return FALSE;
2286
243ef1e0 2287 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2288 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2289
45d6a902
AM
2290 bed = get_elf_backend_data (abfd);
2291
2292 /* Convert the external relocations to the internal format. */
2293 if (shdr->sh_entsize == bed->s->sizeof_rel)
2294 swap_in = bed->s->swap_reloc_in;
2295 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2296 swap_in = bed->s->swap_reloca_in;
2297 else
2298 {
2299 bfd_set_error (bfd_error_wrong_format);
2300 return FALSE;
2301 }
2302
a50b1753 2303 erela = (const bfd_byte *) external_relocs;
51992aec 2304 erelaend = erela + shdr->sh_size;
45d6a902
AM
2305 irela = internal_relocs;
2306 while (erela < erelaend)
2307 {
243ef1e0
L
2308 bfd_vma r_symndx;
2309
45d6a902 2310 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2311 r_symndx = ELF32_R_SYM (irela->r_info);
2312 if (bed->s->arch_size == 64)
2313 r_symndx >>= 24;
ce98a316
NC
2314 if (nsyms > 0)
2315 {
2316 if ((size_t) r_symndx >= nsyms)
2317 {
2318 (*_bfd_error_handler)
2319 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2320 " for offset 0x%lx in section `%A'"),
2321 abfd, sec,
2322 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2323 bfd_set_error (bfd_error_bad_value);
2324 return FALSE;
2325 }
2326 }
cf35638d 2327 else if (r_symndx != STN_UNDEF)
243ef1e0
L
2328 {
2329 (*_bfd_error_handler)
ce98a316
NC
2330 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2331 " when the object file has no symbol table"),
d003868e
AM
2332 abfd, sec,
2333 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2334 bfd_set_error (bfd_error_bad_value);
2335 return FALSE;
2336 }
45d6a902
AM
2337 irela += bed->s->int_rels_per_ext_rel;
2338 erela += shdr->sh_entsize;
2339 }
2340
2341 return TRUE;
2342}
2343
2344/* Read and swap the relocs for a section O. They may have been
2345 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2346 not NULL, they are used as buffers to read into. They are known to
2347 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2348 the return value is allocated using either malloc or bfd_alloc,
2349 according to the KEEP_MEMORY argument. If O has two relocation
2350 sections (both REL and RELA relocations), then the REL_HDR
2351 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2352 RELA_HDR relocations. */
45d6a902
AM
2353
2354Elf_Internal_Rela *
268b6b39
AM
2355_bfd_elf_link_read_relocs (bfd *abfd,
2356 asection *o,
2357 void *external_relocs,
2358 Elf_Internal_Rela *internal_relocs,
2359 bfd_boolean keep_memory)
45d6a902 2360{
268b6b39 2361 void *alloc1 = NULL;
45d6a902 2362 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2363 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2364 struct bfd_elf_section_data *esdo = elf_section_data (o);
2365 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2366
d4730f92
BS
2367 if (esdo->relocs != NULL)
2368 return esdo->relocs;
45d6a902
AM
2369
2370 if (o->reloc_count == 0)
2371 return NULL;
2372
45d6a902
AM
2373 if (internal_relocs == NULL)
2374 {
2375 bfd_size_type size;
2376
2377 size = o->reloc_count;
2378 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2379 if (keep_memory)
a50b1753 2380 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2381 else
a50b1753 2382 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2383 if (internal_relocs == NULL)
2384 goto error_return;
2385 }
2386
2387 if (external_relocs == NULL)
2388 {
d4730f92
BS
2389 bfd_size_type size = 0;
2390
2391 if (esdo->rel.hdr)
2392 size += esdo->rel.hdr->sh_size;
2393 if (esdo->rela.hdr)
2394 size += esdo->rela.hdr->sh_size;
45d6a902 2395
268b6b39 2396 alloc1 = bfd_malloc (size);
45d6a902
AM
2397 if (alloc1 == NULL)
2398 goto error_return;
2399 external_relocs = alloc1;
2400 }
2401
d4730f92
BS
2402 internal_rela_relocs = internal_relocs;
2403 if (esdo->rel.hdr)
2404 {
2405 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2406 external_relocs,
2407 internal_relocs))
2408 goto error_return;
2409 external_relocs = (((bfd_byte *) external_relocs)
2410 + esdo->rel.hdr->sh_size);
2411 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2412 * bed->s->int_rels_per_ext_rel);
2413 }
2414
2415 if (esdo->rela.hdr
2416 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2417 external_relocs,
2418 internal_rela_relocs)))
45d6a902
AM
2419 goto error_return;
2420
2421 /* Cache the results for next time, if we can. */
2422 if (keep_memory)
d4730f92 2423 esdo->relocs = internal_relocs;
45d6a902
AM
2424
2425 if (alloc1 != NULL)
2426 free (alloc1);
2427
2428 /* Don't free alloc2, since if it was allocated we are passing it
2429 back (under the name of internal_relocs). */
2430
2431 return internal_relocs;
2432
2433 error_return:
2434 if (alloc1 != NULL)
2435 free (alloc1);
2436 if (alloc2 != NULL)
4dd07732
AM
2437 {
2438 if (keep_memory)
2439 bfd_release (abfd, alloc2);
2440 else
2441 free (alloc2);
2442 }
45d6a902
AM
2443 return NULL;
2444}
2445
2446/* Compute the size of, and allocate space for, REL_HDR which is the
2447 section header for a section containing relocations for O. */
2448
28caa186 2449static bfd_boolean
268b6b39 2450_bfd_elf_link_size_reloc_section (bfd *abfd,
d4730f92 2451 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2452{
d4730f92 2453 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2454
2455 /* That allows us to calculate the size of the section. */
d4730f92 2456 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2457
2458 /* The contents field must last into write_object_contents, so we
2459 allocate it with bfd_alloc rather than malloc. Also since we
2460 cannot be sure that the contents will actually be filled in,
2461 we zero the allocated space. */
a50b1753 2462 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2463 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2464 return FALSE;
2465
d4730f92 2466 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2467 {
2468 struct elf_link_hash_entry **p;
2469
ca4be51c
AM
2470 p = ((struct elf_link_hash_entry **)
2471 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2472 if (p == NULL)
2473 return FALSE;
2474
d4730f92 2475 reldata->hashes = p;
45d6a902
AM
2476 }
2477
2478 return TRUE;
2479}
2480
2481/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2482 originated from the section given by INPUT_REL_HDR) to the
2483 OUTPUT_BFD. */
2484
2485bfd_boolean
268b6b39
AM
2486_bfd_elf_link_output_relocs (bfd *output_bfd,
2487 asection *input_section,
2488 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2489 Elf_Internal_Rela *internal_relocs,
2490 struct elf_link_hash_entry **rel_hash
2491 ATTRIBUTE_UNUSED)
45d6a902
AM
2492{
2493 Elf_Internal_Rela *irela;
2494 Elf_Internal_Rela *irelaend;
2495 bfd_byte *erel;
d4730f92 2496 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2497 asection *output_section;
9c5bfbb7 2498 const struct elf_backend_data *bed;
268b6b39 2499 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2500 struct bfd_elf_section_data *esdo;
45d6a902
AM
2501
2502 output_section = input_section->output_section;
45d6a902 2503
d4730f92
BS
2504 bed = get_elf_backend_data (output_bfd);
2505 esdo = elf_section_data (output_section);
2506 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2507 {
d4730f92
BS
2508 output_reldata = &esdo->rel;
2509 swap_out = bed->s->swap_reloc_out;
45d6a902 2510 }
d4730f92
BS
2511 else if (esdo->rela.hdr
2512 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2513 {
d4730f92
BS
2514 output_reldata = &esdo->rela;
2515 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2516 }
2517 else
2518 {
2519 (*_bfd_error_handler)
d003868e
AM
2520 (_("%B: relocation size mismatch in %B section %A"),
2521 output_bfd, input_section->owner, input_section);
297d8443 2522 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2523 return FALSE;
2524 }
2525
d4730f92
BS
2526 erel = output_reldata->hdr->contents;
2527 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2528 irela = internal_relocs;
2529 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2530 * bed->s->int_rels_per_ext_rel);
2531 while (irela < irelaend)
2532 {
2533 (*swap_out) (output_bfd, irela, erel);
2534 irela += bed->s->int_rels_per_ext_rel;
2535 erel += input_rel_hdr->sh_entsize;
2536 }
2537
2538 /* Bump the counter, so that we know where to add the next set of
2539 relocations. */
d4730f92 2540 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2541
2542 return TRUE;
2543}
2544\f
508c3946
L
2545/* Make weak undefined symbols in PIE dynamic. */
2546
2547bfd_boolean
2548_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2549 struct elf_link_hash_entry *h)
2550{
0e1862bb 2551 if (bfd_link_pie (info)
508c3946
L
2552 && h->dynindx == -1
2553 && h->root.type == bfd_link_hash_undefweak)
2554 return bfd_elf_link_record_dynamic_symbol (info, h);
2555
2556 return TRUE;
2557}
2558
45d6a902
AM
2559/* Fix up the flags for a symbol. This handles various cases which
2560 can only be fixed after all the input files are seen. This is
2561 currently called by both adjust_dynamic_symbol and
2562 assign_sym_version, which is unnecessary but perhaps more robust in
2563 the face of future changes. */
2564
28caa186 2565static bfd_boolean
268b6b39
AM
2566_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2567 struct elf_info_failed *eif)
45d6a902 2568{
33774f08 2569 const struct elf_backend_data *bed;
508c3946 2570
45d6a902
AM
2571 /* If this symbol was mentioned in a non-ELF file, try to set
2572 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2573 permit a non-ELF file to correctly refer to a symbol defined in
2574 an ELF dynamic object. */
f5385ebf 2575 if (h->non_elf)
45d6a902
AM
2576 {
2577 while (h->root.type == bfd_link_hash_indirect)
2578 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2579
2580 if (h->root.type != bfd_link_hash_defined
2581 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2582 {
2583 h->ref_regular = 1;
2584 h->ref_regular_nonweak = 1;
2585 }
45d6a902
AM
2586 else
2587 {
2588 if (h->root.u.def.section->owner != NULL
2589 && (bfd_get_flavour (h->root.u.def.section->owner)
2590 == bfd_target_elf_flavour))
f5385ebf
AM
2591 {
2592 h->ref_regular = 1;
2593 h->ref_regular_nonweak = 1;
2594 }
45d6a902 2595 else
f5385ebf 2596 h->def_regular = 1;
45d6a902
AM
2597 }
2598
2599 if (h->dynindx == -1
f5385ebf
AM
2600 && (h->def_dynamic
2601 || h->ref_dynamic))
45d6a902 2602 {
c152c796 2603 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2604 {
2605 eif->failed = TRUE;
2606 return FALSE;
2607 }
2608 }
2609 }
2610 else
2611 {
f5385ebf 2612 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2613 was first seen in a non-ELF file. Fortunately, if the symbol
2614 was first seen in an ELF file, we're probably OK unless the
2615 symbol was defined in a non-ELF file. Catch that case here.
2616 FIXME: We're still in trouble if the symbol was first seen in
2617 a dynamic object, and then later in a non-ELF regular object. */
2618 if ((h->root.type == bfd_link_hash_defined
2619 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2620 && !h->def_regular
45d6a902
AM
2621 && (h->root.u.def.section->owner != NULL
2622 ? (bfd_get_flavour (h->root.u.def.section->owner)
2623 != bfd_target_elf_flavour)
2624 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2625 && !h->def_dynamic)))
2626 h->def_regular = 1;
45d6a902
AM
2627 }
2628
508c3946 2629 /* Backend specific symbol fixup. */
33774f08
AM
2630 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2631 if (bed->elf_backend_fixup_symbol
2632 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2633 return FALSE;
508c3946 2634
45d6a902
AM
2635 /* If this is a final link, and the symbol was defined as a common
2636 symbol in a regular object file, and there was no definition in
2637 any dynamic object, then the linker will have allocated space for
f5385ebf 2638 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2639 flag will not have been set. */
2640 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2641 && !h->def_regular
2642 && h->ref_regular
2643 && !h->def_dynamic
96f29d96 2644 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2645 h->def_regular = 1;
45d6a902
AM
2646
2647 /* If -Bsymbolic was used (which means to bind references to global
2648 symbols to the definition within the shared object), and this
2649 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2650 need a PLT entry. Likewise, if the symbol has non-default
2651 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2652 will force it local. */
f5385ebf 2653 if (h->needs_plt
0e1862bb 2654 && bfd_link_pic (eif->info)
0eddce27 2655 && is_elf_hash_table (eif->info->hash)
55255dae 2656 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2657 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2658 && h->def_regular)
45d6a902 2659 {
45d6a902
AM
2660 bfd_boolean force_local;
2661
45d6a902
AM
2662 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2663 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2664 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2665 }
2666
2667 /* If a weak undefined symbol has non-default visibility, we also
2668 hide it from the dynamic linker. */
9c7a29a3 2669 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2670 && h->root.type == bfd_link_hash_undefweak)
33774f08 2671 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2672
2673 /* If this is a weak defined symbol in a dynamic object, and we know
2674 the real definition in the dynamic object, copy interesting flags
2675 over to the real definition. */
f6e332e6 2676 if (h->u.weakdef != NULL)
45d6a902 2677 {
45d6a902
AM
2678 /* If the real definition is defined by a regular object file,
2679 don't do anything special. See the longer description in
2680 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2681 if (h->u.weakdef->def_regular)
f6e332e6 2682 h->u.weakdef = NULL;
45d6a902 2683 else
a26587ba 2684 {
4e6b54a6
AM
2685 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2686
2687 while (h->root.type == bfd_link_hash_indirect)
2688 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2689
2690 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2691 || h->root.type == bfd_link_hash_defweak);
2692 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2693 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2694 || weakdef->root.type == bfd_link_hash_defweak);
2695 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2696 }
45d6a902
AM
2697 }
2698
2699 return TRUE;
2700}
2701
2702/* Make the backend pick a good value for a dynamic symbol. This is
2703 called via elf_link_hash_traverse, and also calls itself
2704 recursively. */
2705
28caa186 2706static bfd_boolean
268b6b39 2707_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2708{
a50b1753 2709 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2710 bfd *dynobj;
9c5bfbb7 2711 const struct elf_backend_data *bed;
45d6a902 2712
0eddce27 2713 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2714 return FALSE;
2715
45d6a902
AM
2716 /* Ignore indirect symbols. These are added by the versioning code. */
2717 if (h->root.type == bfd_link_hash_indirect)
2718 return TRUE;
2719
2720 /* Fix the symbol flags. */
2721 if (! _bfd_elf_fix_symbol_flags (h, eif))
2722 return FALSE;
2723
2724 /* If this symbol does not require a PLT entry, and it is not
2725 defined by a dynamic object, or is not referenced by a regular
2726 object, ignore it. We do have to handle a weak defined symbol,
2727 even if no regular object refers to it, if we decided to add it
2728 to the dynamic symbol table. FIXME: Do we normally need to worry
2729 about symbols which are defined by one dynamic object and
2730 referenced by another one? */
f5385ebf 2731 if (!h->needs_plt
91e21fb7 2732 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2733 && (h->def_regular
2734 || !h->def_dynamic
2735 || (!h->ref_regular
f6e332e6 2736 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2737 {
a6aa5195 2738 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2739 return TRUE;
2740 }
2741
2742 /* If we've already adjusted this symbol, don't do it again. This
2743 can happen via a recursive call. */
f5385ebf 2744 if (h->dynamic_adjusted)
45d6a902
AM
2745 return TRUE;
2746
2747 /* Don't look at this symbol again. Note that we must set this
2748 after checking the above conditions, because we may look at a
2749 symbol once, decide not to do anything, and then get called
2750 recursively later after REF_REGULAR is set below. */
f5385ebf 2751 h->dynamic_adjusted = 1;
45d6a902
AM
2752
2753 /* If this is a weak definition, and we know a real definition, and
2754 the real symbol is not itself defined by a regular object file,
2755 then get a good value for the real definition. We handle the
2756 real symbol first, for the convenience of the backend routine.
2757
2758 Note that there is a confusing case here. If the real definition
2759 is defined by a regular object file, we don't get the real symbol
2760 from the dynamic object, but we do get the weak symbol. If the
2761 processor backend uses a COPY reloc, then if some routine in the
2762 dynamic object changes the real symbol, we will not see that
2763 change in the corresponding weak symbol. This is the way other
2764 ELF linkers work as well, and seems to be a result of the shared
2765 library model.
2766
2767 I will clarify this issue. Most SVR4 shared libraries define the
2768 variable _timezone and define timezone as a weak synonym. The
2769 tzset call changes _timezone. If you write
2770 extern int timezone;
2771 int _timezone = 5;
2772 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2773 you might expect that, since timezone is a synonym for _timezone,
2774 the same number will print both times. However, if the processor
2775 backend uses a COPY reloc, then actually timezone will be copied
2776 into your process image, and, since you define _timezone
2777 yourself, _timezone will not. Thus timezone and _timezone will
2778 wind up at different memory locations. The tzset call will set
2779 _timezone, leaving timezone unchanged. */
2780
f6e332e6 2781 if (h->u.weakdef != NULL)
45d6a902 2782 {
ec24dc88
AM
2783 /* If we get to this point, there is an implicit reference to
2784 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2785 h->u.weakdef->ref_regular = 1;
45d6a902 2786
ec24dc88
AM
2787 /* Ensure that the backend adjust_dynamic_symbol function sees
2788 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2789 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2790 return FALSE;
2791 }
2792
2793 /* If a symbol has no type and no size and does not require a PLT
2794 entry, then we are probably about to do the wrong thing here: we
2795 are probably going to create a COPY reloc for an empty object.
2796 This case can arise when a shared object is built with assembly
2797 code, and the assembly code fails to set the symbol type. */
2798 if (h->size == 0
2799 && h->type == STT_NOTYPE
f5385ebf 2800 && !h->needs_plt)
45d6a902
AM
2801 (*_bfd_error_handler)
2802 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2803 h->root.root.string);
2804
2805 dynobj = elf_hash_table (eif->info)->dynobj;
2806 bed = get_elf_backend_data (dynobj);
e7c33416 2807
45d6a902
AM
2808 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2809 {
2810 eif->failed = TRUE;
2811 return FALSE;
2812 }
2813
2814 return TRUE;
2815}
2816
027297b7
L
2817/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2818 DYNBSS. */
2819
2820bfd_boolean
6cabe1ea
AM
2821_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2822 struct elf_link_hash_entry *h,
027297b7
L
2823 asection *dynbss)
2824{
91ac5911 2825 unsigned int power_of_two;
027297b7
L
2826 bfd_vma mask;
2827 asection *sec = h->root.u.def.section;
2828
2829 /* The section aligment of definition is the maximum alignment
91ac5911
L
2830 requirement of symbols defined in the section. Since we don't
2831 know the symbol alignment requirement, we start with the
2832 maximum alignment and check low bits of the symbol address
2833 for the minimum alignment. */
2834 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2835 mask = ((bfd_vma) 1 << power_of_two) - 1;
2836 while ((h->root.u.def.value & mask) != 0)
2837 {
2838 mask >>= 1;
2839 --power_of_two;
2840 }
027297b7 2841
91ac5911
L
2842 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2843 dynbss))
027297b7
L
2844 {
2845 /* Adjust the section alignment if needed. */
2846 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2847 power_of_two))
027297b7
L
2848 return FALSE;
2849 }
2850
91ac5911 2851 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2852 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2853
2854 /* Define the symbol as being at this point in DYNBSS. */
2855 h->root.u.def.section = dynbss;
2856 h->root.u.def.value = dynbss->size;
2857
2858 /* Increment the size of DYNBSS to make room for the symbol. */
2859 dynbss->size += h->size;
2860
f7483970
L
2861 /* No error if extern_protected_data is true. */
2862 if (h->protected_def
889c2a67
L
2863 && (!info->extern_protected_data
2864 || (info->extern_protected_data < 0
2865 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2866 info->callbacks->einfo
2867 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2868 h->root.root.string);
6cabe1ea 2869
027297b7
L
2870 return TRUE;
2871}
2872
45d6a902
AM
2873/* Adjust all external symbols pointing into SEC_MERGE sections
2874 to reflect the object merging within the sections. */
2875
28caa186 2876static bfd_boolean
268b6b39 2877_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2878{
2879 asection *sec;
2880
45d6a902
AM
2881 if ((h->root.type == bfd_link_hash_defined
2882 || h->root.type == bfd_link_hash_defweak)
2883 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2884 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2885 {
a50b1753 2886 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2887
2888 h->root.u.def.value =
2889 _bfd_merged_section_offset (output_bfd,
2890 &h->root.u.def.section,
2891 elf_section_data (sec)->sec_info,
753731ee 2892 h->root.u.def.value);
45d6a902
AM
2893 }
2894
2895 return TRUE;
2896}
986a241f
RH
2897
2898/* Returns false if the symbol referred to by H should be considered
2899 to resolve local to the current module, and true if it should be
2900 considered to bind dynamically. */
2901
2902bfd_boolean
268b6b39
AM
2903_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2904 struct bfd_link_info *info,
89a2ee5a 2905 bfd_boolean not_local_protected)
986a241f
RH
2906{
2907 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2908 const struct elf_backend_data *bed;
2909 struct elf_link_hash_table *hash_table;
986a241f
RH
2910
2911 if (h == NULL)
2912 return FALSE;
2913
2914 while (h->root.type == bfd_link_hash_indirect
2915 || h->root.type == bfd_link_hash_warning)
2916 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2917
2918 /* If it was forced local, then clearly it's not dynamic. */
2919 if (h->dynindx == -1)
2920 return FALSE;
f5385ebf 2921 if (h->forced_local)
986a241f
RH
2922 return FALSE;
2923
2924 /* Identify the cases where name binding rules say that a
2925 visible symbol resolves locally. */
0e1862bb
L
2926 binding_stays_local_p = (bfd_link_executable (info)
2927 || SYMBOLIC_BIND (info, h));
986a241f
RH
2928
2929 switch (ELF_ST_VISIBILITY (h->other))
2930 {
2931 case STV_INTERNAL:
2932 case STV_HIDDEN:
2933 return FALSE;
2934
2935 case STV_PROTECTED:
fcb93ecf
PB
2936 hash_table = elf_hash_table (info);
2937 if (!is_elf_hash_table (hash_table))
2938 return FALSE;
2939
2940 bed = get_elf_backend_data (hash_table->dynobj);
2941
986a241f
RH
2942 /* Proper resolution for function pointer equality may require
2943 that these symbols perhaps be resolved dynamically, even though
2944 we should be resolving them to the current module. */
89a2ee5a 2945 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2946 binding_stays_local_p = TRUE;
2947 break;
2948
2949 default:
986a241f
RH
2950 break;
2951 }
2952
aa37626c 2953 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2954 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2955 return TRUE;
2956
986a241f
RH
2957 /* Otherwise, the symbol is dynamic if binding rules don't tell
2958 us that it remains local. */
2959 return !binding_stays_local_p;
2960}
f6c52c13
AM
2961
2962/* Return true if the symbol referred to by H should be considered
2963 to resolve local to the current module, and false otherwise. Differs
2964 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2965 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2966 for the place where forced_local and dynindx == -1 are tested. If
2967 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2968 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2969 the symbol is local only for defined symbols.
2970 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2971 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2972 treatment of undefined weak symbols. For those that do not make
2973 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2974
2975bfd_boolean
268b6b39
AM
2976_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2977 struct bfd_link_info *info,
2978 bfd_boolean local_protected)
f6c52c13 2979{
fcb93ecf
PB
2980 const struct elf_backend_data *bed;
2981 struct elf_link_hash_table *hash_table;
2982
f6c52c13
AM
2983 /* If it's a local sym, of course we resolve locally. */
2984 if (h == NULL)
2985 return TRUE;
2986
d95edcac
L
2987 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2988 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2989 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2990 return TRUE;
2991
7e2294f9
AO
2992 /* Common symbols that become definitions don't get the DEF_REGULAR
2993 flag set, so test it first, and don't bail out. */
2994 if (ELF_COMMON_DEF_P (h))
2995 /* Do nothing. */;
f6c52c13 2996 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2997 resolve locally. The sym is either undefined or dynamic. */
2998 else if (!h->def_regular)
f6c52c13
AM
2999 return FALSE;
3000
3001 /* Forced local symbols resolve locally. */
f5385ebf 3002 if (h->forced_local)
f6c52c13
AM
3003 return TRUE;
3004
3005 /* As do non-dynamic symbols. */
3006 if (h->dynindx == -1)
3007 return TRUE;
3008
3009 /* At this point, we know the symbol is defined and dynamic. In an
3010 executable it must resolve locally, likewise when building symbolic
3011 shared libraries. */
0e1862bb 3012 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3013 return TRUE;
3014
3015 /* Now deal with defined dynamic symbols in shared libraries. Ones
3016 with default visibility might not resolve locally. */
3017 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3018 return FALSE;
3019
fcb93ecf
PB
3020 hash_table = elf_hash_table (info);
3021 if (!is_elf_hash_table (hash_table))
3022 return TRUE;
3023
3024 bed = get_elf_backend_data (hash_table->dynobj);
3025
f7483970
L
3026 /* If extern_protected_data is false, STV_PROTECTED non-function
3027 symbols are local. */
889c2a67
L
3028 if ((!info->extern_protected_data
3029 || (info->extern_protected_data < 0
3030 && !bed->extern_protected_data))
3031 && !bed->is_function_type (h->type))
1c16dfa5
L
3032 return TRUE;
3033
f6c52c13 3034 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3035 symbols be treated as dynamic symbols. If the address of a
3036 function not defined in an executable is set to that function's
3037 plt entry in the executable, then the address of the function in
3038 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3039 return local_protected;
3040}
e1918d23
AM
3041
3042/* Caches some TLS segment info, and ensures that the TLS segment vma is
3043 aligned. Returns the first TLS output section. */
3044
3045struct bfd_section *
3046_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3047{
3048 struct bfd_section *sec, *tls;
3049 unsigned int align = 0;
3050
3051 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3052 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3053 break;
3054 tls = sec;
3055
3056 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3057 if (sec->alignment_power > align)
3058 align = sec->alignment_power;
3059
3060 elf_hash_table (info)->tls_sec = tls;
3061
3062 /* Ensure the alignment of the first section is the largest alignment,
3063 so that the tls segment starts aligned. */
3064 if (tls != NULL)
3065 tls->alignment_power = align;
3066
3067 return tls;
3068}
0ad989f9
L
3069
3070/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3071static bfd_boolean
3072is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3073 Elf_Internal_Sym *sym)
3074{
a4d8e49b
L
3075 const struct elf_backend_data *bed;
3076
0ad989f9
L
3077 /* Local symbols do not count, but target specific ones might. */
3078 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3079 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3080 return FALSE;
3081
fcb93ecf 3082 bed = get_elf_backend_data (abfd);
0ad989f9 3083 /* Function symbols do not count. */
fcb93ecf 3084 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3085 return FALSE;
3086
3087 /* If the section is undefined, then so is the symbol. */
3088 if (sym->st_shndx == SHN_UNDEF)
3089 return FALSE;
3090
3091 /* If the symbol is defined in the common section, then
3092 it is a common definition and so does not count. */
a4d8e49b 3093 if (bed->common_definition (sym))
0ad989f9
L
3094 return FALSE;
3095
3096 /* If the symbol is in a target specific section then we
3097 must rely upon the backend to tell us what it is. */
3098 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3099 /* FIXME - this function is not coded yet:
3100
3101 return _bfd_is_global_symbol_definition (abfd, sym);
3102
3103 Instead for now assume that the definition is not global,
3104 Even if this is wrong, at least the linker will behave
3105 in the same way that it used to do. */
3106 return FALSE;
3107
3108 return TRUE;
3109}
3110
3111/* Search the symbol table of the archive element of the archive ABFD
3112 whose archive map contains a mention of SYMDEF, and determine if
3113 the symbol is defined in this element. */
3114static bfd_boolean
3115elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3116{
3117 Elf_Internal_Shdr * hdr;
ef53be89
AM
3118 size_t symcount;
3119 size_t extsymcount;
3120 size_t extsymoff;
0ad989f9
L
3121 Elf_Internal_Sym *isymbuf;
3122 Elf_Internal_Sym *isym;
3123 Elf_Internal_Sym *isymend;
3124 bfd_boolean result;
3125
3126 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3127 if (abfd == NULL)
3128 return FALSE;
3129
3130 if (! bfd_check_format (abfd, bfd_object))
3131 return FALSE;
3132
7dc3990e
L
3133 /* Select the appropriate symbol table. If we don't know if the
3134 object file is an IR object, give linker LTO plugin a chance to
3135 get the correct symbol table. */
3136 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3137#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3138 || (abfd->plugin_format == bfd_plugin_unknown
3139 && bfd_link_plugin_object_p (abfd))
3140#endif
3141 )
3142 {
3143 /* Use the IR symbol table if the object has been claimed by
3144 plugin. */
3145 abfd = abfd->plugin_dummy_bfd;
3146 hdr = &elf_tdata (abfd)->symtab_hdr;
3147 }
3148 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3149 hdr = &elf_tdata (abfd)->symtab_hdr;
3150 else
3151 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3152
3153 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3154
3155 /* The sh_info field of the symtab header tells us where the
3156 external symbols start. We don't care about the local symbols. */
3157 if (elf_bad_symtab (abfd))
3158 {
3159 extsymcount = symcount;
3160 extsymoff = 0;
3161 }
3162 else
3163 {
3164 extsymcount = symcount - hdr->sh_info;
3165 extsymoff = hdr->sh_info;
3166 }
3167
3168 if (extsymcount == 0)
3169 return FALSE;
3170
3171 /* Read in the symbol table. */
3172 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3173 NULL, NULL, NULL);
3174 if (isymbuf == NULL)
3175 return FALSE;
3176
3177 /* Scan the symbol table looking for SYMDEF. */
3178 result = FALSE;
3179 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3180 {
3181 const char *name;
3182
3183 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3184 isym->st_name);
3185 if (name == NULL)
3186 break;
3187
3188 if (strcmp (name, symdef->name) == 0)
3189 {
3190 result = is_global_data_symbol_definition (abfd, isym);
3191 break;
3192 }
3193 }
3194
3195 free (isymbuf);
3196
3197 return result;
3198}
3199\f
5a580b3a
AM
3200/* Add an entry to the .dynamic table. */
3201
3202bfd_boolean
3203_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3204 bfd_vma tag,
3205 bfd_vma val)
3206{
3207 struct elf_link_hash_table *hash_table;
3208 const struct elf_backend_data *bed;
3209 asection *s;
3210 bfd_size_type newsize;
3211 bfd_byte *newcontents;
3212 Elf_Internal_Dyn dyn;
3213
3214 hash_table = elf_hash_table (info);
3215 if (! is_elf_hash_table (hash_table))
3216 return FALSE;
3217
3218 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3219 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3220 BFD_ASSERT (s != NULL);
3221
eea6121a 3222 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3223 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3224 if (newcontents == NULL)
3225 return FALSE;
3226
3227 dyn.d_tag = tag;
3228 dyn.d_un.d_val = val;
eea6121a 3229 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3230
eea6121a 3231 s->size = newsize;
5a580b3a
AM
3232 s->contents = newcontents;
3233
3234 return TRUE;
3235}
3236
3237/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3238 otherwise just check whether one already exists. Returns -1 on error,
3239 1 if a DT_NEEDED tag already exists, and 0 on success. */
3240
4ad4eba5 3241static int
7e9f0867
AM
3242elf_add_dt_needed_tag (bfd *abfd,
3243 struct bfd_link_info *info,
4ad4eba5
AM
3244 const char *soname,
3245 bfd_boolean do_it)
5a580b3a
AM
3246{
3247 struct elf_link_hash_table *hash_table;
ef53be89 3248 size_t strindex;
5a580b3a 3249
7e9f0867
AM
3250 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3251 return -1;
3252
5a580b3a 3253 hash_table = elf_hash_table (info);
5a580b3a 3254 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3255 if (strindex == (size_t) -1)
5a580b3a
AM
3256 return -1;
3257
02be4619 3258 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3259 {
3260 asection *sdyn;
3261 const struct elf_backend_data *bed;
3262 bfd_byte *extdyn;
3263
3264 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3265 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3266 if (sdyn != NULL)
3267 for (extdyn = sdyn->contents;
3268 extdyn < sdyn->contents + sdyn->size;
3269 extdyn += bed->s->sizeof_dyn)
3270 {
3271 Elf_Internal_Dyn dyn;
5a580b3a 3272
7e9f0867
AM
3273 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3274 if (dyn.d_tag == DT_NEEDED
3275 && dyn.d_un.d_val == strindex)
3276 {
3277 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3278 return 1;
3279 }
3280 }
5a580b3a
AM
3281 }
3282
3283 if (do_it)
3284 {
7e9f0867
AM
3285 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3286 return -1;
3287
5a580b3a
AM
3288 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3289 return -1;
3290 }
3291 else
3292 /* We were just checking for existence of the tag. */
3293 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3294
3295 return 0;
3296}
3297
7b15fa7a
AM
3298/* Return true if SONAME is on the needed list between NEEDED and STOP
3299 (or the end of list if STOP is NULL), and needed by a library that
3300 will be loaded. */
3301
010e5ae2 3302static bfd_boolean
7b15fa7a
AM
3303on_needed_list (const char *soname,
3304 struct bfd_link_needed_list *needed,
3305 struct bfd_link_needed_list *stop)
010e5ae2 3306{
7b15fa7a
AM
3307 struct bfd_link_needed_list *look;
3308 for (look = needed; look != stop; look = look->next)
3309 if (strcmp (soname, look->name) == 0
3310 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3311 /* If needed by a library that itself is not directly
3312 needed, recursively check whether that library is
3313 indirectly needed. Since we add DT_NEEDED entries to
3314 the end of the list, library dependencies appear after
3315 the library. Therefore search prior to the current
3316 LOOK, preventing possible infinite recursion. */
3317 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3318 return TRUE;
3319
3320 return FALSE;
3321}
3322
14160578 3323/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3324static int
3325elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3326{
3327 const struct elf_link_hash_entry *h1;
3328 const struct elf_link_hash_entry *h2;
10b7e05b 3329 bfd_signed_vma vdiff;
5a580b3a
AM
3330
3331 h1 = *(const struct elf_link_hash_entry **) arg1;
3332 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3333 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3334 if (vdiff != 0)
3335 return vdiff > 0 ? 1 : -1;
3336 else
3337 {
d3435ae8 3338 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3339 if (sdiff != 0)
3340 return sdiff > 0 ? 1 : -1;
3341 }
14160578
AM
3342 vdiff = h1->size - h2->size;
3343 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3344}
4ad4eba5 3345
5a580b3a
AM
3346/* This function is used to adjust offsets into .dynstr for
3347 dynamic symbols. This is called via elf_link_hash_traverse. */
3348
3349static bfd_boolean
3350elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3351{
a50b1753 3352 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3353
5a580b3a
AM
3354 if (h->dynindx != -1)
3355 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3356 return TRUE;
3357}
3358
3359/* Assign string offsets in .dynstr, update all structures referencing
3360 them. */
3361
4ad4eba5
AM
3362static bfd_boolean
3363elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3364{
3365 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3366 struct elf_link_local_dynamic_entry *entry;
3367 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3368 bfd *dynobj = hash_table->dynobj;
3369 asection *sdyn;
3370 bfd_size_type size;
3371 const struct elf_backend_data *bed;
3372 bfd_byte *extdyn;
3373
3374 _bfd_elf_strtab_finalize (dynstr);
3375 size = _bfd_elf_strtab_size (dynstr);
3376
3377 bed = get_elf_backend_data (dynobj);
3d4d4302 3378 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3379 BFD_ASSERT (sdyn != NULL);
3380
3381 /* Update all .dynamic entries referencing .dynstr strings. */
3382 for (extdyn = sdyn->contents;
eea6121a 3383 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3384 extdyn += bed->s->sizeof_dyn)
3385 {
3386 Elf_Internal_Dyn dyn;
3387
3388 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3389 switch (dyn.d_tag)
3390 {
3391 case DT_STRSZ:
3392 dyn.d_un.d_val = size;
3393 break;
3394 case DT_NEEDED:
3395 case DT_SONAME:
3396 case DT_RPATH:
3397 case DT_RUNPATH:
3398 case DT_FILTER:
3399 case DT_AUXILIARY:
7ee314fa
AM
3400 case DT_AUDIT:
3401 case DT_DEPAUDIT:
5a580b3a
AM
3402 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3403 break;
3404 default:
3405 continue;
3406 }
3407 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3408 }
3409
3410 /* Now update local dynamic symbols. */
3411 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3412 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3413 entry->isym.st_name);
3414
3415 /* And the rest of dynamic symbols. */
3416 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3417
3418 /* Adjust version definitions. */
3419 if (elf_tdata (output_bfd)->cverdefs)
3420 {
3421 asection *s;
3422 bfd_byte *p;
ef53be89 3423 size_t i;
5a580b3a
AM
3424 Elf_Internal_Verdef def;
3425 Elf_Internal_Verdaux defaux;
3426
3d4d4302 3427 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3428 p = s->contents;
3429 do
3430 {
3431 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3432 &def);
3433 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3434 if (def.vd_aux != sizeof (Elf_External_Verdef))
3435 continue;
5a580b3a
AM
3436 for (i = 0; i < def.vd_cnt; ++i)
3437 {
3438 _bfd_elf_swap_verdaux_in (output_bfd,
3439 (Elf_External_Verdaux *) p, &defaux);
3440 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3441 defaux.vda_name);
3442 _bfd_elf_swap_verdaux_out (output_bfd,
3443 &defaux, (Elf_External_Verdaux *) p);
3444 p += sizeof (Elf_External_Verdaux);
3445 }
3446 }
3447 while (def.vd_next);
3448 }
3449
3450 /* Adjust version references. */
3451 if (elf_tdata (output_bfd)->verref)
3452 {
3453 asection *s;
3454 bfd_byte *p;
ef53be89 3455 size_t i;
5a580b3a
AM
3456 Elf_Internal_Verneed need;
3457 Elf_Internal_Vernaux needaux;
3458
3d4d4302 3459 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3460 p = s->contents;
3461 do
3462 {
3463 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3464 &need);
3465 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3466 _bfd_elf_swap_verneed_out (output_bfd, &need,
3467 (Elf_External_Verneed *) p);
3468 p += sizeof (Elf_External_Verneed);
3469 for (i = 0; i < need.vn_cnt; ++i)
3470 {
3471 _bfd_elf_swap_vernaux_in (output_bfd,
3472 (Elf_External_Vernaux *) p, &needaux);
3473 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3474 needaux.vna_name);
3475 _bfd_elf_swap_vernaux_out (output_bfd,
3476 &needaux,
3477 (Elf_External_Vernaux *) p);
3478 p += sizeof (Elf_External_Vernaux);
3479 }
3480 }
3481 while (need.vn_next);
3482 }
3483
3484 return TRUE;
3485}
3486\f
13285a1b
AM
3487/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3488 The default is to only match when the INPUT and OUTPUT are exactly
3489 the same target. */
3490
3491bfd_boolean
3492_bfd_elf_default_relocs_compatible (const bfd_target *input,
3493 const bfd_target *output)
3494{
3495 return input == output;
3496}
3497
3498/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3499 This version is used when different targets for the same architecture
3500 are virtually identical. */
3501
3502bfd_boolean
3503_bfd_elf_relocs_compatible (const bfd_target *input,
3504 const bfd_target *output)
3505{
3506 const struct elf_backend_data *obed, *ibed;
3507
3508 if (input == output)
3509 return TRUE;
3510
3511 ibed = xvec_get_elf_backend_data (input);
3512 obed = xvec_get_elf_backend_data (output);
3513
3514 if (ibed->arch != obed->arch)
3515 return FALSE;
3516
3517 /* If both backends are using this function, deem them compatible. */
3518 return ibed->relocs_compatible == obed->relocs_compatible;
3519}
3520
e5034e59
AM
3521/* Make a special call to the linker "notice" function to tell it that
3522 we are about to handle an as-needed lib, or have finished
1b786873 3523 processing the lib. */
e5034e59
AM
3524
3525bfd_boolean
3526_bfd_elf_notice_as_needed (bfd *ibfd,
3527 struct bfd_link_info *info,
3528 enum notice_asneeded_action act)
3529{
46135103 3530 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3531}
3532
d9689752
L
3533/* Check relocations an ELF object file. */
3534
3535bfd_boolean
3536_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3537{
3538 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3539 struct elf_link_hash_table *htab = elf_hash_table (info);
3540
3541 /* If this object is the same format as the output object, and it is
3542 not a shared library, then let the backend look through the
3543 relocs.
3544
3545 This is required to build global offset table entries and to
3546 arrange for dynamic relocs. It is not required for the
3547 particular common case of linking non PIC code, even when linking
3548 against shared libraries, but unfortunately there is no way of
3549 knowing whether an object file has been compiled PIC or not.
3550 Looking through the relocs is not particularly time consuming.
3551 The problem is that we must either (1) keep the relocs in memory,
3552 which causes the linker to require additional runtime memory or
3553 (2) read the relocs twice from the input file, which wastes time.
3554 This would be a good case for using mmap.
3555
3556 I have no idea how to handle linking PIC code into a file of a
3557 different format. It probably can't be done. */
3558 if ((abfd->flags & DYNAMIC) == 0
3559 && is_elf_hash_table (htab)
3560 && bed->check_relocs != NULL
3561 && elf_object_id (abfd) == elf_hash_table_id (htab)
3562 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3563 {
3564 asection *o;
3565
3566 for (o = abfd->sections; o != NULL; o = o->next)
3567 {
3568 Elf_Internal_Rela *internal_relocs;
3569 bfd_boolean ok;
3570
5ce03cea 3571 /* Don't check relocations in excluded sections. */
d9689752 3572 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3573 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3574 || o->reloc_count == 0
3575 || ((info->strip == strip_all || info->strip == strip_debugger)
3576 && (o->flags & SEC_DEBUGGING) != 0)
3577 || bfd_is_abs_section (o->output_section))
3578 continue;
3579
3580 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3581 info->keep_memory);
3582 if (internal_relocs == NULL)
3583 return FALSE;
3584
3585 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3586
3587 if (elf_section_data (o)->relocs != internal_relocs)
3588 free (internal_relocs);
3589
3590 if (! ok)
3591 return FALSE;
3592 }
3593 }
3594
3595 return TRUE;
3596}
3597
4ad4eba5
AM
3598/* Add symbols from an ELF object file to the linker hash table. */
3599
3600static bfd_boolean
3601elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3602{
a0c402a5 3603 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3604 Elf_Internal_Shdr *hdr;
ef53be89
AM
3605 size_t symcount;
3606 size_t extsymcount;
3607 size_t extsymoff;
4ad4eba5
AM
3608 struct elf_link_hash_entry **sym_hash;
3609 bfd_boolean dynamic;
3610 Elf_External_Versym *extversym = NULL;
3611 Elf_External_Versym *ever;
3612 struct elf_link_hash_entry *weaks;
3613 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3614 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3615 Elf_Internal_Sym *isymbuf = NULL;
3616 Elf_Internal_Sym *isym;
3617 Elf_Internal_Sym *isymend;
3618 const struct elf_backend_data *bed;
3619 bfd_boolean add_needed;
66eb6687 3620 struct elf_link_hash_table *htab;
4ad4eba5 3621 bfd_size_type amt;
66eb6687 3622 void *alloc_mark = NULL;
4f87808c
AM
3623 struct bfd_hash_entry **old_table = NULL;
3624 unsigned int old_size = 0;
3625 unsigned int old_count = 0;
66eb6687 3626 void *old_tab = NULL;
66eb6687
AM
3627 void *old_ent;
3628 struct bfd_link_hash_entry *old_undefs = NULL;
3629 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3630 void *old_strtab = NULL;
66eb6687 3631 size_t tabsize = 0;
db6a5d5f 3632 asection *s;
29a9f53e 3633 bfd_boolean just_syms;
4ad4eba5 3634
66eb6687 3635 htab = elf_hash_table (info);
4ad4eba5 3636 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3637
3638 if ((abfd->flags & DYNAMIC) == 0)
3639 dynamic = FALSE;
3640 else
3641 {
3642 dynamic = TRUE;
3643
3644 /* You can't use -r against a dynamic object. Also, there's no
3645 hope of using a dynamic object which does not exactly match
3646 the format of the output file. */
0e1862bb 3647 if (bfd_link_relocatable (info)
66eb6687 3648 || !is_elf_hash_table (htab)
f13a99db 3649 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3650 {
0e1862bb 3651 if (bfd_link_relocatable (info))
9a0789ec
NC
3652 bfd_set_error (bfd_error_invalid_operation);
3653 else
3654 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3655 goto error_return;
3656 }
3657 }
3658
a0c402a5
L
3659 ehdr = elf_elfheader (abfd);
3660 if (info->warn_alternate_em
3661 && bed->elf_machine_code != ehdr->e_machine
3662 && ((bed->elf_machine_alt1 != 0
3663 && ehdr->e_machine == bed->elf_machine_alt1)
3664 || (bed->elf_machine_alt2 != 0
3665 && ehdr->e_machine == bed->elf_machine_alt2)))
3666 info->callbacks->einfo
3667 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3668 ehdr->e_machine, abfd, bed->elf_machine_code);
3669
4ad4eba5
AM
3670 /* As a GNU extension, any input sections which are named
3671 .gnu.warning.SYMBOL are treated as warning symbols for the given
3672 symbol. This differs from .gnu.warning sections, which generate
3673 warnings when they are included in an output file. */
dd98f8d2 3674 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3675 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3676 {
db6a5d5f 3677 const char *name;
4ad4eba5 3678
db6a5d5f
AM
3679 name = bfd_get_section_name (abfd, s);
3680 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3681 {
db6a5d5f
AM
3682 char *msg;
3683 bfd_size_type sz;
3684
3685 name += sizeof ".gnu.warning." - 1;
3686
3687 /* If this is a shared object, then look up the symbol
3688 in the hash table. If it is there, and it is already
3689 been defined, then we will not be using the entry
3690 from this shared object, so we don't need to warn.
3691 FIXME: If we see the definition in a regular object
3692 later on, we will warn, but we shouldn't. The only
3693 fix is to keep track of what warnings we are supposed
3694 to emit, and then handle them all at the end of the
3695 link. */
3696 if (dynamic)
4ad4eba5 3697 {
db6a5d5f
AM
3698 struct elf_link_hash_entry *h;
3699
3700 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3701
3702 /* FIXME: What about bfd_link_hash_common? */
3703 if (h != NULL
3704 && (h->root.type == bfd_link_hash_defined
3705 || h->root.type == bfd_link_hash_defweak))
3706 continue;
3707 }
4ad4eba5 3708
db6a5d5f
AM
3709 sz = s->size;
3710 msg = (char *) bfd_alloc (abfd, sz + 1);
3711 if (msg == NULL)
3712 goto error_return;
4ad4eba5 3713
db6a5d5f
AM
3714 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3715 goto error_return;
4ad4eba5 3716
db6a5d5f 3717 msg[sz] = '\0';
4ad4eba5 3718
db6a5d5f
AM
3719 if (! (_bfd_generic_link_add_one_symbol
3720 (info, abfd, name, BSF_WARNING, s, 0, msg,
3721 FALSE, bed->collect, NULL)))
3722 goto error_return;
4ad4eba5 3723
0e1862bb 3724 if (bfd_link_executable (info))
db6a5d5f
AM
3725 {
3726 /* Clobber the section size so that the warning does
3727 not get copied into the output file. */
3728 s->size = 0;
11d2f718 3729
db6a5d5f
AM
3730 /* Also set SEC_EXCLUDE, so that symbols defined in
3731 the warning section don't get copied to the output. */
3732 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3733 }
3734 }
3735 }
3736
29a9f53e
L
3737 just_syms = ((s = abfd->sections) != NULL
3738 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3739
4ad4eba5
AM
3740 add_needed = TRUE;
3741 if (! dynamic)
3742 {
3743 /* If we are creating a shared library, create all the dynamic
3744 sections immediately. We need to attach them to something,
3745 so we attach them to this BFD, provided it is the right
bf89386a
L
3746 format and is not from ld --just-symbols. Always create the
3747 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3748 are no input BFD's of the same format as the output, we can't
3749 make a shared library. */
3750 if (!just_syms
bf89386a 3751 && (bfd_link_pic (info)
9c1d7a08
L
3752 || (!bfd_link_relocatable (info)
3753 && (info->export_dynamic || info->dynamic)))
66eb6687 3754 && is_elf_hash_table (htab)
f13a99db 3755 && info->output_bfd->xvec == abfd->xvec
66eb6687 3756 && !htab->dynamic_sections_created)
4ad4eba5
AM
3757 {
3758 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3759 goto error_return;
3760 }
3761 }
66eb6687 3762 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3763 goto error_return;
3764 else
3765 {
4ad4eba5 3766 const char *soname = NULL;
7ee314fa 3767 char *audit = NULL;
4ad4eba5
AM
3768 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3769 int ret;
3770
3771 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3772 ld shouldn't allow it. */
29a9f53e 3773 if (just_syms)
92fd189d 3774 abort ();
4ad4eba5
AM
3775
3776 /* If this dynamic lib was specified on the command line with
3777 --as-needed in effect, then we don't want to add a DT_NEEDED
3778 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3779 in by another lib's DT_NEEDED. When --no-add-needed is used
3780 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3781 any dynamic library in DT_NEEDED tags in the dynamic lib at
3782 all. */
3783 add_needed = (elf_dyn_lib_class (abfd)
3784 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3785 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3786
3787 s = bfd_get_section_by_name (abfd, ".dynamic");
3788 if (s != NULL)
3789 {
3790 bfd_byte *dynbuf;
3791 bfd_byte *extdyn;
cb33740c 3792 unsigned int elfsec;
4ad4eba5
AM
3793 unsigned long shlink;
3794
eea6121a 3795 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3796 {
3797error_free_dyn:
3798 free (dynbuf);
3799 goto error_return;
3800 }
4ad4eba5
AM
3801
3802 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3803 if (elfsec == SHN_BAD)
4ad4eba5
AM
3804 goto error_free_dyn;
3805 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3806
3807 for (extdyn = dynbuf;
eea6121a 3808 extdyn < dynbuf + s->size;
4ad4eba5
AM
3809 extdyn += bed->s->sizeof_dyn)
3810 {
3811 Elf_Internal_Dyn dyn;
3812
3813 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3814 if (dyn.d_tag == DT_SONAME)
3815 {
3816 unsigned int tagv = dyn.d_un.d_val;
3817 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3818 if (soname == NULL)
3819 goto error_free_dyn;
3820 }
3821 if (dyn.d_tag == DT_NEEDED)
3822 {
3823 struct bfd_link_needed_list *n, **pn;
3824 char *fnm, *anm;
3825 unsigned int tagv = dyn.d_un.d_val;
3826
3827 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3828 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3829 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3830 if (n == NULL || fnm == NULL)
3831 goto error_free_dyn;
3832 amt = strlen (fnm) + 1;
a50b1753 3833 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3834 if (anm == NULL)
3835 goto error_free_dyn;
3836 memcpy (anm, fnm, amt);
3837 n->name = anm;
3838 n->by = abfd;
3839 n->next = NULL;
66eb6687 3840 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3841 ;
3842 *pn = n;
3843 }
3844 if (dyn.d_tag == DT_RUNPATH)
3845 {
3846 struct bfd_link_needed_list *n, **pn;
3847 char *fnm, *anm;
3848 unsigned int tagv = dyn.d_un.d_val;
3849
3850 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3851 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3852 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3853 if (n == NULL || fnm == NULL)
3854 goto error_free_dyn;
3855 amt = strlen (fnm) + 1;
a50b1753 3856 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3857 if (anm == NULL)
3858 goto error_free_dyn;
3859 memcpy (anm, fnm, amt);
3860 n->name = anm;
3861 n->by = abfd;
3862 n->next = NULL;
3863 for (pn = & runpath;
3864 *pn != NULL;
3865 pn = &(*pn)->next)
3866 ;
3867 *pn = n;
3868 }
3869 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3870 if (!runpath && dyn.d_tag == DT_RPATH)
3871 {
3872 struct bfd_link_needed_list *n, **pn;
3873 char *fnm, *anm;
3874 unsigned int tagv = dyn.d_un.d_val;
3875
3876 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3877 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3878 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3879 if (n == NULL || fnm == NULL)
3880 goto error_free_dyn;
3881 amt = strlen (fnm) + 1;
a50b1753 3882 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3883 if (anm == NULL)
f8703194 3884 goto error_free_dyn;
4ad4eba5
AM
3885 memcpy (anm, fnm, amt);
3886 n->name = anm;
3887 n->by = abfd;
3888 n->next = NULL;
3889 for (pn = & rpath;
3890 *pn != NULL;
3891 pn = &(*pn)->next)
3892 ;
3893 *pn = n;
3894 }
7ee314fa
AM
3895 if (dyn.d_tag == DT_AUDIT)
3896 {
3897 unsigned int tagv = dyn.d_un.d_val;
3898 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3899 }
4ad4eba5
AM
3900 }
3901
3902 free (dynbuf);
3903 }
3904
3905 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3906 frees all more recently bfd_alloc'd blocks as well. */
3907 if (runpath)
3908 rpath = runpath;
3909
3910 if (rpath)
3911 {
3912 struct bfd_link_needed_list **pn;
66eb6687 3913 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3914 ;
3915 *pn = rpath;
3916 }
3917
3918 /* We do not want to include any of the sections in a dynamic
3919 object in the output file. We hack by simply clobbering the
3920 list of sections in the BFD. This could be handled more
3921 cleanly by, say, a new section flag; the existing
3922 SEC_NEVER_LOAD flag is not the one we want, because that one
3923 still implies that the section takes up space in the output
3924 file. */
3925 bfd_section_list_clear (abfd);
3926
4ad4eba5
AM
3927 /* Find the name to use in a DT_NEEDED entry that refers to this
3928 object. If the object has a DT_SONAME entry, we use it.
3929 Otherwise, if the generic linker stuck something in
3930 elf_dt_name, we use that. Otherwise, we just use the file
3931 name. */
3932 if (soname == NULL || *soname == '\0')
3933 {
3934 soname = elf_dt_name (abfd);
3935 if (soname == NULL || *soname == '\0')
3936 soname = bfd_get_filename (abfd);
3937 }
3938
3939 /* Save the SONAME because sometimes the linker emulation code
3940 will need to know it. */
3941 elf_dt_name (abfd) = soname;
3942
7e9f0867 3943 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3944 if (ret < 0)
3945 goto error_return;
3946
3947 /* If we have already included this dynamic object in the
3948 link, just ignore it. There is no reason to include a
3949 particular dynamic object more than once. */
3950 if (ret > 0)
3951 return TRUE;
7ee314fa
AM
3952
3953 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 3954 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3955 }
3956
3957 /* If this is a dynamic object, we always link against the .dynsym
3958 symbol table, not the .symtab symbol table. The dynamic linker
3959 will only see the .dynsym symbol table, so there is no reason to
3960 look at .symtab for a dynamic object. */
3961
3962 if (! dynamic || elf_dynsymtab (abfd) == 0)
3963 hdr = &elf_tdata (abfd)->symtab_hdr;
3964 else
3965 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3966
3967 symcount = hdr->sh_size / bed->s->sizeof_sym;
3968
3969 /* The sh_info field of the symtab header tells us where the
3970 external symbols start. We don't care about the local symbols at
3971 this point. */
3972 if (elf_bad_symtab (abfd))
3973 {
3974 extsymcount = symcount;
3975 extsymoff = 0;
3976 }
3977 else
3978 {
3979 extsymcount = symcount - hdr->sh_info;
3980 extsymoff = hdr->sh_info;
3981 }
3982
f45794cb 3983 sym_hash = elf_sym_hashes (abfd);
012b2306 3984 if (extsymcount != 0)
4ad4eba5
AM
3985 {
3986 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3987 NULL, NULL, NULL);
3988 if (isymbuf == NULL)
3989 goto error_return;
3990
4ad4eba5 3991 if (sym_hash == NULL)
012b2306
AM
3992 {
3993 /* We store a pointer to the hash table entry for each
3994 external symbol. */
ef53be89
AM
3995 amt = extsymcount;
3996 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
3997 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3998 if (sym_hash == NULL)
3999 goto error_free_sym;
4000 elf_sym_hashes (abfd) = sym_hash;
4001 }
4ad4eba5
AM
4002 }
4003
4004 if (dynamic)
4005 {
4006 /* Read in any version definitions. */
fc0e6df6
PB
4007 if (!_bfd_elf_slurp_version_tables (abfd,
4008 info->default_imported_symver))
4ad4eba5
AM
4009 goto error_free_sym;
4010
4011 /* Read in the symbol versions, but don't bother to convert them
4012 to internal format. */
4013 if (elf_dynversym (abfd) != 0)
4014 {
4015 Elf_Internal_Shdr *versymhdr;
4016
4017 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4018 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4019 if (extversym == NULL)
4020 goto error_free_sym;
4021 amt = versymhdr->sh_size;
4022 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4023 || bfd_bread (extversym, amt, abfd) != amt)
4024 goto error_free_vers;
4025 }
4026 }
4027
66eb6687
AM
4028 /* If we are loading an as-needed shared lib, save the symbol table
4029 state before we start adding symbols. If the lib turns out
4030 to be unneeded, restore the state. */
4031 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4032 {
4033 unsigned int i;
4034 size_t entsize;
4035
4036 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4037 {
4038 struct bfd_hash_entry *p;
2de92251 4039 struct elf_link_hash_entry *h;
66eb6687
AM
4040
4041 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4042 {
4043 h = (struct elf_link_hash_entry *) p;
4044 entsize += htab->root.table.entsize;
4045 if (h->root.type == bfd_link_hash_warning)
4046 entsize += htab->root.table.entsize;
4047 }
66eb6687
AM
4048 }
4049
4050 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4051 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4052 if (old_tab == NULL)
4053 goto error_free_vers;
4054
4055 /* Remember the current objalloc pointer, so that all mem for
4056 symbols added can later be reclaimed. */
4057 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4058 if (alloc_mark == NULL)
4059 goto error_free_vers;
4060
5061a885
AM
4061 /* Make a special call to the linker "notice" function to
4062 tell it that we are about to handle an as-needed lib. */
e5034e59 4063 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4064 goto error_free_vers;
5061a885 4065
f45794cb
AM
4066 /* Clone the symbol table. Remember some pointers into the
4067 symbol table, and dynamic symbol count. */
4068 old_ent = (char *) old_tab + tabsize;
66eb6687 4069 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4070 old_undefs = htab->root.undefs;
4071 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4072 old_table = htab->root.table.table;
4073 old_size = htab->root.table.size;
4074 old_count = htab->root.table.count;
5b677558
AM
4075 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4076 if (old_strtab == NULL)
4077 goto error_free_vers;
66eb6687
AM
4078
4079 for (i = 0; i < htab->root.table.size; i++)
4080 {
4081 struct bfd_hash_entry *p;
2de92251 4082 struct elf_link_hash_entry *h;
66eb6687
AM
4083
4084 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4085 {
4086 memcpy (old_ent, p, htab->root.table.entsize);
4087 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4088 h = (struct elf_link_hash_entry *) p;
4089 if (h->root.type == bfd_link_hash_warning)
4090 {
4091 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4092 old_ent = (char *) old_ent + htab->root.table.entsize;
4093 }
66eb6687
AM
4094 }
4095 }
4096 }
4ad4eba5 4097
66eb6687 4098 weaks = NULL;
4ad4eba5
AM
4099 ever = extversym != NULL ? extversym + extsymoff : NULL;
4100 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4101 isym < isymend;
4102 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4103 {
4104 int bind;
4105 bfd_vma value;
af44c138 4106 asection *sec, *new_sec;
4ad4eba5
AM
4107 flagword flags;
4108 const char *name;
4109 struct elf_link_hash_entry *h;
90c984fc 4110 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4111 bfd_boolean definition;
4112 bfd_boolean size_change_ok;
4113 bfd_boolean type_change_ok;
4114 bfd_boolean new_weakdef;
37a9e49a
L
4115 bfd_boolean new_weak;
4116 bfd_boolean old_weak;
4ad4eba5 4117 bfd_boolean override;
a4d8e49b 4118 bfd_boolean common;
97196564 4119 bfd_boolean discarded;
4ad4eba5
AM
4120 unsigned int old_alignment;
4121 bfd *old_bfd;
6e33951e 4122 bfd_boolean matched;
4ad4eba5
AM
4123
4124 override = FALSE;
4125
4126 flags = BSF_NO_FLAGS;
4127 sec = NULL;
4128 value = isym->st_value;
a4d8e49b 4129 common = bed->common_definition (isym);
97196564 4130 discarded = FALSE;
4ad4eba5
AM
4131
4132 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4133 switch (bind)
4ad4eba5 4134 {
3e7a7d11 4135 case STB_LOCAL:
4ad4eba5
AM
4136 /* This should be impossible, since ELF requires that all
4137 global symbols follow all local symbols, and that sh_info
4138 point to the first global symbol. Unfortunately, Irix 5
4139 screws this up. */
4140 continue;
3e7a7d11
NC
4141
4142 case STB_GLOBAL:
a4d8e49b 4143 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4144 flags = BSF_GLOBAL;
3e7a7d11
NC
4145 break;
4146
4147 case STB_WEAK:
4148 flags = BSF_WEAK;
4149 break;
4150
4151 case STB_GNU_UNIQUE:
4152 flags = BSF_GNU_UNIQUE;
4153 break;
4154
4155 default:
4ad4eba5 4156 /* Leave it up to the processor backend. */
3e7a7d11 4157 break;
4ad4eba5
AM
4158 }
4159
4160 if (isym->st_shndx == SHN_UNDEF)
4161 sec = bfd_und_section_ptr;
cb33740c
AM
4162 else if (isym->st_shndx == SHN_ABS)
4163 sec = bfd_abs_section_ptr;
4164 else if (isym->st_shndx == SHN_COMMON)
4165 {
4166 sec = bfd_com_section_ptr;
4167 /* What ELF calls the size we call the value. What ELF
4168 calls the value we call the alignment. */
4169 value = isym->st_size;
4170 }
4171 else
4ad4eba5
AM
4172 {
4173 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4174 if (sec == NULL)
4175 sec = bfd_abs_section_ptr;
dbaa2011 4176 else if (discarded_section (sec))
529fcb95 4177 {
e5d08002
L
4178 /* Symbols from discarded section are undefined. We keep
4179 its visibility. */
529fcb95 4180 sec = bfd_und_section_ptr;
97196564 4181 discarded = TRUE;
529fcb95
PB
4182 isym->st_shndx = SHN_UNDEF;
4183 }
4ad4eba5
AM
4184 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4185 value -= sec->vma;
4186 }
4ad4eba5
AM
4187
4188 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4189 isym->st_name);
4190 if (name == NULL)
4191 goto error_free_vers;
4192
4193 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4194 && (abfd->flags & BFD_PLUGIN) != 0)
4195 {
4196 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4197
4198 if (xc == NULL)
4199 {
4200 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4201 | SEC_EXCLUDE);
4202 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4203 if (xc == NULL)
4204 goto error_free_vers;
4205 }
4206 sec = xc;
4207 }
4208 else if (isym->st_shndx == SHN_COMMON
4209 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4210 && !bfd_link_relocatable (info))
4ad4eba5
AM
4211 {
4212 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4213
4214 if (tcomm == NULL)
4215 {
02d00247
AM
4216 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4217 | SEC_LINKER_CREATED);
4218 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4219 if (tcomm == NULL)
4ad4eba5
AM
4220 goto error_free_vers;
4221 }
4222 sec = tcomm;
4223 }
66eb6687 4224 else if (bed->elf_add_symbol_hook)
4ad4eba5 4225 {
66eb6687
AM
4226 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4227 &sec, &value))
4ad4eba5
AM
4228 goto error_free_vers;
4229
4230 /* The hook function sets the name to NULL if this symbol
4231 should be skipped for some reason. */
4232 if (name == NULL)
4233 continue;
4234 }
4235
4236 /* Sanity check that all possibilities were handled. */
4237 if (sec == NULL)
4238 {
4239 bfd_set_error (bfd_error_bad_value);
4240 goto error_free_vers;
4241 }
4242
191c0c42
AM
4243 /* Silently discard TLS symbols from --just-syms. There's
4244 no way to combine a static TLS block with a new TLS block
4245 for this executable. */
4246 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4247 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4248 continue;
4249
4ad4eba5
AM
4250 if (bfd_is_und_section (sec)
4251 || bfd_is_com_section (sec))
4252 definition = FALSE;
4253 else
4254 definition = TRUE;
4255
4256 size_change_ok = FALSE;
66eb6687 4257 type_change_ok = bed->type_change_ok;
37a9e49a 4258 old_weak = FALSE;
6e33951e 4259 matched = FALSE;
4ad4eba5
AM
4260 old_alignment = 0;
4261 old_bfd = NULL;
af44c138 4262 new_sec = sec;
4ad4eba5 4263
66eb6687 4264 if (is_elf_hash_table (htab))
4ad4eba5
AM
4265 {
4266 Elf_Internal_Versym iver;
4267 unsigned int vernum = 0;
4268 bfd_boolean skip;
4269
fc0e6df6 4270 if (ever == NULL)
4ad4eba5 4271 {
fc0e6df6
PB
4272 if (info->default_imported_symver)
4273 /* Use the default symbol version created earlier. */
4274 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4275 else
4276 iver.vs_vers = 0;
4277 }
4278 else
4279 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4280
4281 vernum = iver.vs_vers & VERSYM_VERSION;
4282
4283 /* If this is a hidden symbol, or if it is not version
4284 1, we append the version name to the symbol name.
cc86ff91
EB
4285 However, we do not modify a non-hidden absolute symbol
4286 if it is not a function, because it might be the version
4287 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4288 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4289 || (vernum > 1
4290 && (!bfd_is_abs_section (sec)
4291 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4292 {
4293 const char *verstr;
4294 size_t namelen, verlen, newlen;
4295 char *newname, *p;
4296
4297 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4298 {
fc0e6df6
PB
4299 if (vernum > elf_tdata (abfd)->cverdefs)
4300 verstr = NULL;
4301 else if (vernum > 1)
4302 verstr =
4303 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4304 else
4305 verstr = "";
4ad4eba5 4306
fc0e6df6 4307 if (verstr == NULL)
4ad4eba5 4308 {
fc0e6df6
PB
4309 (*_bfd_error_handler)
4310 (_("%B: %s: invalid version %u (max %d)"),
4311 abfd, name, vernum,
4312 elf_tdata (abfd)->cverdefs);
4313 bfd_set_error (bfd_error_bad_value);
4314 goto error_free_vers;
4ad4eba5 4315 }
fc0e6df6
PB
4316 }
4317 else
4318 {
4319 /* We cannot simply test for the number of
4320 entries in the VERNEED section since the
4321 numbers for the needed versions do not start
4322 at 0. */
4323 Elf_Internal_Verneed *t;
4324
4325 verstr = NULL;
4326 for (t = elf_tdata (abfd)->verref;
4327 t != NULL;
4328 t = t->vn_nextref)
4ad4eba5 4329 {
fc0e6df6 4330 Elf_Internal_Vernaux *a;
4ad4eba5 4331
fc0e6df6
PB
4332 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4333 {
4334 if (a->vna_other == vernum)
4ad4eba5 4335 {
fc0e6df6
PB
4336 verstr = a->vna_nodename;
4337 break;
4ad4eba5 4338 }
4ad4eba5 4339 }
fc0e6df6
PB
4340 if (a != NULL)
4341 break;
4342 }
4343 if (verstr == NULL)
4344 {
4345 (*_bfd_error_handler)
4346 (_("%B: %s: invalid needed version %d"),
4347 abfd, name, vernum);
4348 bfd_set_error (bfd_error_bad_value);
4349 goto error_free_vers;
4ad4eba5 4350 }
4ad4eba5 4351 }
fc0e6df6
PB
4352
4353 namelen = strlen (name);
4354 verlen = strlen (verstr);
4355 newlen = namelen + verlen + 2;
4356 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4357 && isym->st_shndx != SHN_UNDEF)
4358 ++newlen;
4359
a50b1753 4360 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4361 if (newname == NULL)
4362 goto error_free_vers;
4363 memcpy (newname, name, namelen);
4364 p = newname + namelen;
4365 *p++ = ELF_VER_CHR;
4366 /* If this is a defined non-hidden version symbol,
4367 we add another @ to the name. This indicates the
4368 default version of the symbol. */
4369 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4370 && isym->st_shndx != SHN_UNDEF)
4371 *p++ = ELF_VER_CHR;
4372 memcpy (p, verstr, verlen + 1);
4373
4374 name = newname;
4ad4eba5
AM
4375 }
4376
cd3416da
AM
4377 /* If this symbol has default visibility and the user has
4378 requested we not re-export it, then mark it as hidden. */
a0d49154 4379 if (!bfd_is_und_section (sec)
cd3416da 4380 && !dynamic
ce875075 4381 && abfd->no_export
cd3416da
AM
4382 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4383 isym->st_other = (STV_HIDDEN
4384 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4385
4f3fedcf
AM
4386 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4387 sym_hash, &old_bfd, &old_weak,
4388 &old_alignment, &skip, &override,
6e33951e
L
4389 &type_change_ok, &size_change_ok,
4390 &matched))
4ad4eba5
AM
4391 goto error_free_vers;
4392
4393 if (skip)
4394 continue;
4395
6e33951e
L
4396 /* Override a definition only if the new symbol matches the
4397 existing one. */
4398 if (override && matched)
4ad4eba5
AM
4399 definition = FALSE;
4400
4401 h = *sym_hash;
4402 while (h->root.type == bfd_link_hash_indirect
4403 || h->root.type == bfd_link_hash_warning)
4404 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4405
4ad4eba5 4406 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4407 && vernum > 1
4408 && definition)
4409 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4410 }
4411
4412 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4413 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4414 (struct bfd_link_hash_entry **) sym_hash)))
4415 goto error_free_vers;
4416
a43942db
MR
4417 if ((flags & BSF_GNU_UNIQUE)
4418 && (abfd->flags & DYNAMIC) == 0
4419 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4420 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4421
4ad4eba5 4422 h = *sym_hash;
90c984fc
L
4423 /* We need to make sure that indirect symbol dynamic flags are
4424 updated. */
4425 hi = h;
4ad4eba5
AM
4426 while (h->root.type == bfd_link_hash_indirect
4427 || h->root.type == bfd_link_hash_warning)
4428 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4429
97196564
L
4430 /* Setting the index to -3 tells elf_link_output_extsym that
4431 this symbol is defined in a discarded section. */
4432 if (discarded)
4433 h->indx = -3;
4434
4ad4eba5
AM
4435 *sym_hash = h;
4436
37a9e49a 4437 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4438 new_weakdef = FALSE;
4439 if (dynamic
4440 && definition
37a9e49a 4441 && new_weak
fcb93ecf 4442 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4443 && is_elf_hash_table (htab)
f6e332e6 4444 && h->u.weakdef == NULL)
4ad4eba5
AM
4445 {
4446 /* Keep a list of all weak defined non function symbols from
4447 a dynamic object, using the weakdef field. Later in this
4448 function we will set the weakdef field to the correct
4449 value. We only put non-function symbols from dynamic
4450 objects on this list, because that happens to be the only
4451 time we need to know the normal symbol corresponding to a
4452 weak symbol, and the information is time consuming to
4453 figure out. If the weakdef field is not already NULL,
4454 then this symbol was already defined by some previous
4455 dynamic object, and we will be using that previous
4456 definition anyhow. */
4457
f6e332e6 4458 h->u.weakdef = weaks;
4ad4eba5
AM
4459 weaks = h;
4460 new_weakdef = TRUE;
4461 }
4462
4463 /* Set the alignment of a common symbol. */
a4d8e49b 4464 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4465 && h->root.type == bfd_link_hash_common)
4466 {
4467 unsigned int align;
4468
a4d8e49b 4469 if (common)
af44c138
L
4470 align = bfd_log2 (isym->st_value);
4471 else
4472 {
4473 /* The new symbol is a common symbol in a shared object.
4474 We need to get the alignment from the section. */
4475 align = new_sec->alignment_power;
4476 }
595213d4 4477 if (align > old_alignment)
4ad4eba5
AM
4478 h->root.u.c.p->alignment_power = align;
4479 else
4480 h->root.u.c.p->alignment_power = old_alignment;
4481 }
4482
66eb6687 4483 if (is_elf_hash_table (htab))
4ad4eba5 4484 {
4f3fedcf
AM
4485 /* Set a flag in the hash table entry indicating the type of
4486 reference or definition we just found. A dynamic symbol
4487 is one which is referenced or defined by both a regular
4488 object and a shared object. */
4489 bfd_boolean dynsym = FALSE;
4490
4491 /* Plugin symbols aren't normal. Don't set def_regular or
4492 ref_regular for them, or make them dynamic. */
4493 if ((abfd->flags & BFD_PLUGIN) != 0)
4494 ;
4495 else if (! dynamic)
4496 {
4497 if (! definition)
4498 {
4499 h->ref_regular = 1;
4500 if (bind != STB_WEAK)
4501 h->ref_regular_nonweak = 1;
4502 }
4503 else
4504 {
4505 h->def_regular = 1;
4506 if (h->def_dynamic)
4507 {
4508 h->def_dynamic = 0;
4509 h->ref_dynamic = 1;
4510 }
4511 }
4512
4513 /* If the indirect symbol has been forced local, don't
4514 make the real symbol dynamic. */
4515 if ((h == hi || !hi->forced_local)
0e1862bb 4516 && (bfd_link_dll (info)
4f3fedcf
AM
4517 || h->def_dynamic
4518 || h->ref_dynamic))
4519 dynsym = TRUE;
4520 }
4521 else
4522 {
4523 if (! definition)
4524 {
4525 h->ref_dynamic = 1;
4526 hi->ref_dynamic = 1;
4527 }
4528 else
4529 {
4530 h->def_dynamic = 1;
4531 hi->def_dynamic = 1;
4532 }
4533
4534 /* If the indirect symbol has been forced local, don't
4535 make the real symbol dynamic. */
4536 if ((h == hi || !hi->forced_local)
4537 && (h->def_regular
4538 || h->ref_regular
4539 || (h->u.weakdef != NULL
4540 && ! new_weakdef
4541 && h->u.weakdef->dynindx != -1)))
4542 dynsym = TRUE;
4543 }
4544
4545 /* Check to see if we need to add an indirect symbol for
4546 the default name. */
4547 if (definition
4548 || (!override && h->root.type == bfd_link_hash_common))
4549 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4550 sec, value, &old_bfd, &dynsym))
4551 goto error_free_vers;
4ad4eba5
AM
4552
4553 /* Check the alignment when a common symbol is involved. This
4554 can change when a common symbol is overridden by a normal
4555 definition or a common symbol is ignored due to the old
4556 normal definition. We need to make sure the maximum
4557 alignment is maintained. */
a4d8e49b 4558 if ((old_alignment || common)
4ad4eba5
AM
4559 && h->root.type != bfd_link_hash_common)
4560 {
4561 unsigned int common_align;
4562 unsigned int normal_align;
4563 unsigned int symbol_align;
4564 bfd *normal_bfd;
4565 bfd *common_bfd;
4566
3a81e825
AM
4567 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4568 || h->root.type == bfd_link_hash_defweak);
4569
4ad4eba5
AM
4570 symbol_align = ffs (h->root.u.def.value) - 1;
4571 if (h->root.u.def.section->owner != NULL
0616a280
AM
4572 && (h->root.u.def.section->owner->flags
4573 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4574 {
4575 normal_align = h->root.u.def.section->alignment_power;
4576 if (normal_align > symbol_align)
4577 normal_align = symbol_align;
4578 }
4579 else
4580 normal_align = symbol_align;
4581
4582 if (old_alignment)
4583 {
4584 common_align = old_alignment;
4585 common_bfd = old_bfd;
4586 normal_bfd = abfd;
4587 }
4588 else
4589 {
4590 common_align = bfd_log2 (isym->st_value);
4591 common_bfd = abfd;
4592 normal_bfd = old_bfd;
4593 }
4594
4595 if (normal_align < common_align)
d07676f8
NC
4596 {
4597 /* PR binutils/2735 */
4598 if (normal_bfd == NULL)
4599 (*_bfd_error_handler)
4f3fedcf
AM
4600 (_("Warning: alignment %u of common symbol `%s' in %B is"
4601 " greater than the alignment (%u) of its section %A"),
d07676f8
NC
4602 common_bfd, h->root.u.def.section,
4603 1 << common_align, name, 1 << normal_align);
4604 else
4605 (*_bfd_error_handler)
4606 (_("Warning: alignment %u of symbol `%s' in %B"
4607 " is smaller than %u in %B"),
4608 normal_bfd, common_bfd,
4609 1 << normal_align, name, 1 << common_align);
4610 }
4ad4eba5
AM
4611 }
4612
83ad0046 4613 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4614 if (isym->st_size != 0
4615 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4616 && (definition || h->size == 0))
4617 {
83ad0046
L
4618 if (h->size != 0
4619 && h->size != isym->st_size
4620 && ! size_change_ok)
4ad4eba5 4621 (*_bfd_error_handler)
d003868e
AM
4622 (_("Warning: size of symbol `%s' changed"
4623 " from %lu in %B to %lu in %B"),
4624 old_bfd, abfd,
4ad4eba5 4625 name, (unsigned long) h->size,
d003868e 4626 (unsigned long) isym->st_size);
4ad4eba5
AM
4627
4628 h->size = isym->st_size;
4629 }
4630
4631 /* If this is a common symbol, then we always want H->SIZE
4632 to be the size of the common symbol. The code just above
4633 won't fix the size if a common symbol becomes larger. We
4634 don't warn about a size change here, because that is
4f3fedcf 4635 covered by --warn-common. Allow changes between different
fcb93ecf 4636 function types. */
4ad4eba5
AM
4637 if (h->root.type == bfd_link_hash_common)
4638 h->size = h->root.u.c.size;
4639
4640 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4641 && ((definition && !new_weak)
4642 || (old_weak && h->root.type == bfd_link_hash_common)
4643 || h->type == STT_NOTYPE))
4ad4eba5 4644 {
2955ec4c
L
4645 unsigned int type = ELF_ST_TYPE (isym->st_info);
4646
4647 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4648 symbol. */
4649 if (type == STT_GNU_IFUNC
4650 && (abfd->flags & DYNAMIC) != 0)
4651 type = STT_FUNC;
4ad4eba5 4652
2955ec4c
L
4653 if (h->type != type)
4654 {
4655 if (h->type != STT_NOTYPE && ! type_change_ok)
4656 (*_bfd_error_handler)
4657 (_("Warning: type of symbol `%s' changed"
4658 " from %d to %d in %B"),
4659 abfd, name, h->type, type);
4660
4661 h->type = type;
4662 }
4ad4eba5
AM
4663 }
4664
54ac0771 4665 /* Merge st_other field. */
b8417128 4666 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4667
c3df8c14 4668 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4669 if (definition
4670 && (sec->flags & SEC_DEBUGGING)
4671 && !bfd_link_relocatable (info))
c3df8c14
AM
4672 dynsym = FALSE;
4673
4f3fedcf
AM
4674 /* Nor should we make plugin symbols dynamic. */
4675 if ((abfd->flags & BFD_PLUGIN) != 0)
4676 dynsym = FALSE;
4677
35fc36a8 4678 if (definition)
35399224
L
4679 {
4680 h->target_internal = isym->st_target_internal;
4681 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4682 }
35fc36a8 4683
4ad4eba5
AM
4684 if (definition && !dynamic)
4685 {
4686 char *p = strchr (name, ELF_VER_CHR);
4687 if (p != NULL && p[1] != ELF_VER_CHR)
4688 {
4689 /* Queue non-default versions so that .symver x, x@FOO
4690 aliases can be checked. */
66eb6687 4691 if (!nondeflt_vers)
4ad4eba5 4692 {
66eb6687
AM
4693 amt = ((isymend - isym + 1)
4694 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4695 nondeflt_vers
4696 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4697 if (!nondeflt_vers)
4698 goto error_free_vers;
4ad4eba5 4699 }
66eb6687 4700 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4701 }
4702 }
4703
4704 if (dynsym && h->dynindx == -1)
4705 {
c152c796 4706 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4707 goto error_free_vers;
f6e332e6 4708 if (h->u.weakdef != NULL
4ad4eba5 4709 && ! new_weakdef
f6e332e6 4710 && h->u.weakdef->dynindx == -1)
4ad4eba5 4711 {
66eb6687 4712 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4713 goto error_free_vers;
4714 }
4715 }
1f599d0e 4716 else if (h->dynindx != -1)
4ad4eba5
AM
4717 /* If the symbol already has a dynamic index, but
4718 visibility says it should not be visible, turn it into
4719 a local symbol. */
4720 switch (ELF_ST_VISIBILITY (h->other))
4721 {
4722 case STV_INTERNAL:
4723 case STV_HIDDEN:
4724 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4725 dynsym = FALSE;
4726 break;
4727 }
4728
aef28989
L
4729 /* Don't add DT_NEEDED for references from the dummy bfd nor
4730 for unmatched symbol. */
4ad4eba5 4731 if (!add_needed
aef28989 4732 && matched
4ad4eba5 4733 && definition
010e5ae2 4734 && ((dynsym
ffa9430d 4735 && h->ref_regular_nonweak
4f3fedcf
AM
4736 && (old_bfd == NULL
4737 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4738 || (h->ref_dynamic_nonweak
010e5ae2 4739 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4740 && !on_needed_list (elf_dt_name (abfd),
4741 htab->needed, NULL))))
4ad4eba5
AM
4742 {
4743 int ret;
4744 const char *soname = elf_dt_name (abfd);
4745
16e4ecc0
AM
4746 info->callbacks->minfo ("%!", soname, old_bfd,
4747 h->root.root.string);
4748
4ad4eba5
AM
4749 /* A symbol from a library loaded via DT_NEEDED of some
4750 other library is referenced by a regular object.
e56f61be 4751 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4752 --no-add-needed is used and the reference was not
4753 a weak one. */
4f3fedcf 4754 if (old_bfd != NULL
b918acf9 4755 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4756 {
4757 (*_bfd_error_handler)
3cbc5de0 4758 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4759 old_bfd, name);
ff5ac77b 4760 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4761 goto error_free_vers;
4762 }
4763
a50b1753 4764 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4765 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4766
4ad4eba5 4767 add_needed = TRUE;
7e9f0867 4768 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4769 if (ret < 0)
4770 goto error_free_vers;
4771
4772 BFD_ASSERT (ret == 0);
4773 }
4774 }
4775 }
4776
66eb6687
AM
4777 if (extversym != NULL)
4778 {
4779 free (extversym);
4780 extversym = NULL;
4781 }
4782
4783 if (isymbuf != NULL)
4784 {
4785 free (isymbuf);
4786 isymbuf = NULL;
4787 }
4788
4789 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4790 {
4791 unsigned int i;
4792
4793 /* Restore the symbol table. */
f45794cb
AM
4794 old_ent = (char *) old_tab + tabsize;
4795 memset (elf_sym_hashes (abfd), 0,
4796 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4797 htab->root.table.table = old_table;
4798 htab->root.table.size = old_size;
4799 htab->root.table.count = old_count;
66eb6687 4800 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4801 htab->root.undefs = old_undefs;
4802 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
4803 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4804 free (old_strtab);
4805 old_strtab = NULL;
66eb6687
AM
4806 for (i = 0; i < htab->root.table.size; i++)
4807 {
4808 struct bfd_hash_entry *p;
4809 struct elf_link_hash_entry *h;
3e0882af
L
4810 bfd_size_type size;
4811 unsigned int alignment_power;
66eb6687
AM
4812
4813 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4814 {
4815 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4816 if (h->root.type == bfd_link_hash_warning)
4817 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4818
3e0882af
L
4819 /* Preserve the maximum alignment and size for common
4820 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4821 since it can still be loaded at run time by another
3e0882af
L
4822 dynamic lib. */
4823 if (h->root.type == bfd_link_hash_common)
4824 {
4825 size = h->root.u.c.size;
4826 alignment_power = h->root.u.c.p->alignment_power;
4827 }
4828 else
4829 {
4830 size = 0;
4831 alignment_power = 0;
4832 }
66eb6687
AM
4833 memcpy (p, old_ent, htab->root.table.entsize);
4834 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4835 h = (struct elf_link_hash_entry *) p;
4836 if (h->root.type == bfd_link_hash_warning)
4837 {
4838 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4839 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4840 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4841 }
a4542f1b 4842 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4843 {
4844 if (size > h->root.u.c.size)
4845 h->root.u.c.size = size;
4846 if (alignment_power > h->root.u.c.p->alignment_power)
4847 h->root.u.c.p->alignment_power = alignment_power;
4848 }
66eb6687
AM
4849 }
4850 }
4851
5061a885
AM
4852 /* Make a special call to the linker "notice" function to
4853 tell it that symbols added for crefs may need to be removed. */
e5034e59 4854 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4855 goto error_free_vers;
5061a885 4856
66eb6687
AM
4857 free (old_tab);
4858 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4859 alloc_mark);
4860 if (nondeflt_vers != NULL)
4861 free (nondeflt_vers);
4862 return TRUE;
4863 }
2de92251 4864
66eb6687
AM
4865 if (old_tab != NULL)
4866 {
e5034e59 4867 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4868 goto error_free_vers;
66eb6687
AM
4869 free (old_tab);
4870 old_tab = NULL;
4871 }
4872
c6e8a9a8
L
4873 /* Now that all the symbols from this input file are created, if
4874 not performing a relocatable link, handle .symver foo, foo@BAR
4875 such that any relocs against foo become foo@BAR. */
0e1862bb 4876 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 4877 {
ef53be89 4878 size_t cnt, symidx;
4ad4eba5
AM
4879
4880 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4881 {
4882 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4883 char *shortname, *p;
4884
4885 p = strchr (h->root.root.string, ELF_VER_CHR);
4886 if (p == NULL
4887 || (h->root.type != bfd_link_hash_defined
4888 && h->root.type != bfd_link_hash_defweak))
4889 continue;
4890
4891 amt = p - h->root.root.string;
a50b1753 4892 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4893 if (!shortname)
4894 goto error_free_vers;
4ad4eba5
AM
4895 memcpy (shortname, h->root.root.string, amt);
4896 shortname[amt] = '\0';
4897
4898 hi = (struct elf_link_hash_entry *)
66eb6687 4899 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4900 FALSE, FALSE, FALSE);
4901 if (hi != NULL
4902 && hi->root.type == h->root.type
4903 && hi->root.u.def.value == h->root.u.def.value
4904 && hi->root.u.def.section == h->root.u.def.section)
4905 {
4906 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4907 hi->root.type = bfd_link_hash_indirect;
4908 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4909 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4910 sym_hash = elf_sym_hashes (abfd);
4911 if (sym_hash)
4912 for (symidx = 0; symidx < extsymcount; ++symidx)
4913 if (sym_hash[symidx] == hi)
4914 {
4915 sym_hash[symidx] = h;
4916 break;
4917 }
4918 }
4919 free (shortname);
4920 }
4921 free (nondeflt_vers);
4922 nondeflt_vers = NULL;
4923 }
4924
4ad4eba5
AM
4925 /* Now set the weakdefs field correctly for all the weak defined
4926 symbols we found. The only way to do this is to search all the
4927 symbols. Since we only need the information for non functions in
4928 dynamic objects, that's the only time we actually put anything on
4929 the list WEAKS. We need this information so that if a regular
4930 object refers to a symbol defined weakly in a dynamic object, the
4931 real symbol in the dynamic object is also put in the dynamic
4932 symbols; we also must arrange for both symbols to point to the
4933 same memory location. We could handle the general case of symbol
4934 aliasing, but a general symbol alias can only be generated in
4935 assembler code, handling it correctly would be very time
4936 consuming, and other ELF linkers don't handle general aliasing
4937 either. */
4938 if (weaks != NULL)
4939 {
4940 struct elf_link_hash_entry **hpp;
4941 struct elf_link_hash_entry **hppend;
4942 struct elf_link_hash_entry **sorted_sym_hash;
4943 struct elf_link_hash_entry *h;
4944 size_t sym_count;
4945
4946 /* Since we have to search the whole symbol list for each weak
4947 defined symbol, search time for N weak defined symbols will be
4948 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
4949 amt = extsymcount;
4950 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 4951 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4952 if (sorted_sym_hash == NULL)
4953 goto error_return;
4954 sym_hash = sorted_sym_hash;
4955 hpp = elf_sym_hashes (abfd);
4956 hppend = hpp + extsymcount;
4957 sym_count = 0;
4958 for (; hpp < hppend; hpp++)
4959 {
4960 h = *hpp;
4961 if (h != NULL
4962 && h->root.type == bfd_link_hash_defined
fcb93ecf 4963 && !bed->is_function_type (h->type))
4ad4eba5
AM
4964 {
4965 *sym_hash = h;
4966 sym_hash++;
4967 sym_count++;
4968 }
4969 }
4970
4971 qsort (sorted_sym_hash, sym_count,
4972 sizeof (struct elf_link_hash_entry *),
4973 elf_sort_symbol);
4974
4975 while (weaks != NULL)
4976 {
4977 struct elf_link_hash_entry *hlook;
4978 asection *slook;
4979 bfd_vma vlook;
ed54588d 4980 size_t i, j, idx = 0;
4ad4eba5
AM
4981
4982 hlook = weaks;
f6e332e6
AM
4983 weaks = hlook->u.weakdef;
4984 hlook->u.weakdef = NULL;
4ad4eba5
AM
4985
4986 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4987 || hlook->root.type == bfd_link_hash_defweak
4988 || hlook->root.type == bfd_link_hash_common
4989 || hlook->root.type == bfd_link_hash_indirect);
4990 slook = hlook->root.u.def.section;
4991 vlook = hlook->root.u.def.value;
4992
4ad4eba5
AM
4993 i = 0;
4994 j = sym_count;
14160578 4995 while (i != j)
4ad4eba5
AM
4996 {
4997 bfd_signed_vma vdiff;
4998 idx = (i + j) / 2;
14160578 4999 h = sorted_sym_hash[idx];
4ad4eba5
AM
5000 vdiff = vlook - h->root.u.def.value;
5001 if (vdiff < 0)
5002 j = idx;
5003 else if (vdiff > 0)
5004 i = idx + 1;
5005 else
5006 {
d3435ae8 5007 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5008 if (sdiff < 0)
5009 j = idx;
5010 else if (sdiff > 0)
5011 i = idx + 1;
5012 else
14160578 5013 break;
4ad4eba5
AM
5014 }
5015 }
5016
5017 /* We didn't find a value/section match. */
14160578 5018 if (i == j)
4ad4eba5
AM
5019 continue;
5020
14160578
AM
5021 /* With multiple aliases, or when the weak symbol is already
5022 strongly defined, we have multiple matching symbols and
5023 the binary search above may land on any of them. Step
5024 one past the matching symbol(s). */
5025 while (++idx != j)
5026 {
5027 h = sorted_sym_hash[idx];
5028 if (h->root.u.def.section != slook
5029 || h->root.u.def.value != vlook)
5030 break;
5031 }
5032
5033 /* Now look back over the aliases. Since we sorted by size
5034 as well as value and section, we'll choose the one with
5035 the largest size. */
5036 while (idx-- != i)
4ad4eba5 5037 {
14160578 5038 h = sorted_sym_hash[idx];
4ad4eba5
AM
5039
5040 /* Stop if value or section doesn't match. */
14160578
AM
5041 if (h->root.u.def.section != slook
5042 || h->root.u.def.value != vlook)
4ad4eba5
AM
5043 break;
5044 else if (h != hlook)
5045 {
f6e332e6 5046 hlook->u.weakdef = h;
4ad4eba5
AM
5047
5048 /* If the weak definition is in the list of dynamic
5049 symbols, make sure the real definition is put
5050 there as well. */
5051 if (hlook->dynindx != -1 && h->dynindx == -1)
5052 {
c152c796 5053 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5054 {
5055 err_free_sym_hash:
5056 free (sorted_sym_hash);
5057 goto error_return;
5058 }
4ad4eba5
AM
5059 }
5060
5061 /* If the real definition is in the list of dynamic
5062 symbols, make sure the weak definition is put
5063 there as well. If we don't do this, then the
5064 dynamic loader might not merge the entries for the
5065 real definition and the weak definition. */
5066 if (h->dynindx != -1 && hlook->dynindx == -1)
5067 {
c152c796 5068 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5069 goto err_free_sym_hash;
4ad4eba5
AM
5070 }
5071 break;
5072 }
5073 }
5074 }
5075
5076 free (sorted_sym_hash);
5077 }
5078
33177bb1
AM
5079 if (bed->check_directives
5080 && !(*bed->check_directives) (abfd, info))
5081 return FALSE;
85fbca6a 5082
d9689752
L
5083 if (!info->check_relocs_after_open_input
5084 && !_bfd_elf_link_check_relocs (abfd, info))
5085 return FALSE;
4ad4eba5
AM
5086
5087 /* If this is a non-traditional link, try to optimize the handling
5088 of the .stab/.stabstr sections. */
5089 if (! dynamic
5090 && ! info->traditional_format
66eb6687 5091 && is_elf_hash_table (htab)
4ad4eba5
AM
5092 && (info->strip != strip_all && info->strip != strip_debugger))
5093 {
5094 asection *stabstr;
5095
5096 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5097 if (stabstr != NULL)
5098 {
5099 bfd_size_type string_offset = 0;
5100 asection *stab;
5101
5102 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5103 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5104 && (!stab->name[5] ||
5105 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5106 && (stab->flags & SEC_MERGE) == 0
5107 && !bfd_is_abs_section (stab->output_section))
5108 {
5109 struct bfd_elf_section_data *secdata;
5110
5111 secdata = elf_section_data (stab);
66eb6687
AM
5112 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5113 stabstr, &secdata->sec_info,
4ad4eba5
AM
5114 &string_offset))
5115 goto error_return;
5116 if (secdata->sec_info)
dbaa2011 5117 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5118 }
5119 }
5120 }
5121
66eb6687 5122 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5123 {
5124 /* Add this bfd to the loaded list. */
5125 struct elf_link_loaded_list *n;
5126
ca4be51c 5127 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5128 if (n == NULL)
5129 goto error_return;
5130 n->abfd = abfd;
66eb6687
AM
5131 n->next = htab->loaded;
5132 htab->loaded = n;
4ad4eba5
AM
5133 }
5134
5135 return TRUE;
5136
5137 error_free_vers:
66eb6687
AM
5138 if (old_tab != NULL)
5139 free (old_tab);
5b677558
AM
5140 if (old_strtab != NULL)
5141 free (old_strtab);
4ad4eba5
AM
5142 if (nondeflt_vers != NULL)
5143 free (nondeflt_vers);
5144 if (extversym != NULL)
5145 free (extversym);
5146 error_free_sym:
5147 if (isymbuf != NULL)
5148 free (isymbuf);
5149 error_return:
5150 return FALSE;
5151}
5152
8387904d
AM
5153/* Return the linker hash table entry of a symbol that might be
5154 satisfied by an archive symbol. Return -1 on error. */
5155
5156struct elf_link_hash_entry *
5157_bfd_elf_archive_symbol_lookup (bfd *abfd,
5158 struct bfd_link_info *info,
5159 const char *name)
5160{
5161 struct elf_link_hash_entry *h;
5162 char *p, *copy;
5163 size_t len, first;
5164
2a41f396 5165 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5166 if (h != NULL)
5167 return h;
5168
5169 /* If this is a default version (the name contains @@), look up the
5170 symbol again with only one `@' as well as without the version.
5171 The effect is that references to the symbol with and without the
5172 version will be matched by the default symbol in the archive. */
5173
5174 p = strchr (name, ELF_VER_CHR);
5175 if (p == NULL || p[1] != ELF_VER_CHR)
5176 return h;
5177
5178 /* First check with only one `@'. */
5179 len = strlen (name);
a50b1753 5180 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5181 if (copy == NULL)
5182 return (struct elf_link_hash_entry *) 0 - 1;
5183
5184 first = p - name + 1;
5185 memcpy (copy, name, first);
5186 memcpy (copy + first, name + first + 1, len - first);
5187
2a41f396 5188 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5189 if (h == NULL)
5190 {
5191 /* We also need to check references to the symbol without the
5192 version. */
5193 copy[first - 1] = '\0';
5194 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5195 FALSE, FALSE, TRUE);
8387904d
AM
5196 }
5197
5198 bfd_release (abfd, copy);
5199 return h;
5200}
5201
0ad989f9 5202/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5203 don't use _bfd_generic_link_add_archive_symbols because we need to
5204 handle versioned symbols.
0ad989f9
L
5205
5206 Fortunately, ELF archive handling is simpler than that done by
5207 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5208 oddities. In ELF, if we find a symbol in the archive map, and the
5209 symbol is currently undefined, we know that we must pull in that
5210 object file.
5211
5212 Unfortunately, we do have to make multiple passes over the symbol
5213 table until nothing further is resolved. */
5214
4ad4eba5
AM
5215static bfd_boolean
5216elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5217{
5218 symindex c;
13e570f8 5219 unsigned char *included = NULL;
0ad989f9
L
5220 carsym *symdefs;
5221 bfd_boolean loop;
5222 bfd_size_type amt;
8387904d
AM
5223 const struct elf_backend_data *bed;
5224 struct elf_link_hash_entry * (*archive_symbol_lookup)
5225 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5226
5227 if (! bfd_has_map (abfd))
5228 {
5229 /* An empty archive is a special case. */
5230 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5231 return TRUE;
5232 bfd_set_error (bfd_error_no_armap);
5233 return FALSE;
5234 }
5235
5236 /* Keep track of all symbols we know to be already defined, and all
5237 files we know to be already included. This is to speed up the
5238 second and subsequent passes. */
5239 c = bfd_ardata (abfd)->symdef_count;
5240 if (c == 0)
5241 return TRUE;
5242 amt = c;
13e570f8
AM
5243 amt *= sizeof (*included);
5244 included = (unsigned char *) bfd_zmalloc (amt);
5245 if (included == NULL)
5246 return FALSE;
0ad989f9
L
5247
5248 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5249 bed = get_elf_backend_data (abfd);
5250 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5251
5252 do
5253 {
5254 file_ptr last;
5255 symindex i;
5256 carsym *symdef;
5257 carsym *symdefend;
5258
5259 loop = FALSE;
5260 last = -1;
5261
5262 symdef = symdefs;
5263 symdefend = symdef + c;
5264 for (i = 0; symdef < symdefend; symdef++, i++)
5265 {
5266 struct elf_link_hash_entry *h;
5267 bfd *element;
5268 struct bfd_link_hash_entry *undefs_tail;
5269 symindex mark;
5270
13e570f8 5271 if (included[i])
0ad989f9
L
5272 continue;
5273 if (symdef->file_offset == last)
5274 {
5275 included[i] = TRUE;
5276 continue;
5277 }
5278
8387904d
AM
5279 h = archive_symbol_lookup (abfd, info, symdef->name);
5280 if (h == (struct elf_link_hash_entry *) 0 - 1)
5281 goto error_return;
0ad989f9
L
5282
5283 if (h == NULL)
5284 continue;
5285
5286 if (h->root.type == bfd_link_hash_common)
5287 {
5288 /* We currently have a common symbol. The archive map contains
5289 a reference to this symbol, so we may want to include it. We
5290 only want to include it however, if this archive element
5291 contains a definition of the symbol, not just another common
5292 declaration of it.
5293
5294 Unfortunately some archivers (including GNU ar) will put
5295 declarations of common symbols into their archive maps, as
5296 well as real definitions, so we cannot just go by the archive
5297 map alone. Instead we must read in the element's symbol
5298 table and check that to see what kind of symbol definition
5299 this is. */
5300 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5301 continue;
5302 }
5303 else if (h->root.type != bfd_link_hash_undefined)
5304 {
5305 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5306 /* Symbol must be defined. Don't check it again. */
5307 included[i] = TRUE;
0ad989f9
L
5308 continue;
5309 }
5310
5311 /* We need to include this archive member. */
5312 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5313 if (element == NULL)
5314 goto error_return;
5315
5316 if (! bfd_check_format (element, bfd_object))
5317 goto error_return;
5318
0ad989f9
L
5319 undefs_tail = info->hash->undefs_tail;
5320
0e144ba7
AM
5321 if (!(*info->callbacks
5322 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5323 continue;
0e144ba7 5324 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5325 goto error_return;
5326
5327 /* If there are any new undefined symbols, we need to make
5328 another pass through the archive in order to see whether
5329 they can be defined. FIXME: This isn't perfect, because
5330 common symbols wind up on undefs_tail and because an
5331 undefined symbol which is defined later on in this pass
5332 does not require another pass. This isn't a bug, but it
5333 does make the code less efficient than it could be. */
5334 if (undefs_tail != info->hash->undefs_tail)
5335 loop = TRUE;
5336
5337 /* Look backward to mark all symbols from this object file
5338 which we have already seen in this pass. */
5339 mark = i;
5340 do
5341 {
5342 included[mark] = TRUE;
5343 if (mark == 0)
5344 break;
5345 --mark;
5346 }
5347 while (symdefs[mark].file_offset == symdef->file_offset);
5348
5349 /* We mark subsequent symbols from this object file as we go
5350 on through the loop. */
5351 last = symdef->file_offset;
5352 }
5353 }
5354 while (loop);
5355
0ad989f9
L
5356 free (included);
5357
5358 return TRUE;
5359
5360 error_return:
0ad989f9
L
5361 if (included != NULL)
5362 free (included);
5363 return FALSE;
5364}
4ad4eba5
AM
5365
5366/* Given an ELF BFD, add symbols to the global hash table as
5367 appropriate. */
5368
5369bfd_boolean
5370bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5371{
5372 switch (bfd_get_format (abfd))
5373 {
5374 case bfd_object:
5375 return elf_link_add_object_symbols (abfd, info);
5376 case bfd_archive:
5377 return elf_link_add_archive_symbols (abfd, info);
5378 default:
5379 bfd_set_error (bfd_error_wrong_format);
5380 return FALSE;
5381 }
5382}
5a580b3a 5383\f
14b1c01e
AM
5384struct hash_codes_info
5385{
5386 unsigned long *hashcodes;
5387 bfd_boolean error;
5388};
a0c8462f 5389
5a580b3a
AM
5390/* This function will be called though elf_link_hash_traverse to store
5391 all hash value of the exported symbols in an array. */
5392
5393static bfd_boolean
5394elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5395{
a50b1753 5396 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5397 const char *name;
5a580b3a
AM
5398 unsigned long ha;
5399 char *alc = NULL;
5400
5a580b3a
AM
5401 /* Ignore indirect symbols. These are added by the versioning code. */
5402 if (h->dynindx == -1)
5403 return TRUE;
5404
5405 name = h->root.root.string;
422f1182 5406 if (h->versioned >= versioned)
5a580b3a 5407 {
422f1182
L
5408 char *p = strchr (name, ELF_VER_CHR);
5409 if (p != NULL)
14b1c01e 5410 {
422f1182
L
5411 alc = (char *) bfd_malloc (p - name + 1);
5412 if (alc == NULL)
5413 {
5414 inf->error = TRUE;
5415 return FALSE;
5416 }
5417 memcpy (alc, name, p - name);
5418 alc[p - name] = '\0';
5419 name = alc;
14b1c01e 5420 }
5a580b3a
AM
5421 }
5422
5423 /* Compute the hash value. */
5424 ha = bfd_elf_hash (name);
5425
5426 /* Store the found hash value in the array given as the argument. */
14b1c01e 5427 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5428
5429 /* And store it in the struct so that we can put it in the hash table
5430 later. */
f6e332e6 5431 h->u.elf_hash_value = ha;
5a580b3a
AM
5432
5433 if (alc != NULL)
5434 free (alc);
5435
5436 return TRUE;
5437}
5438
fdc90cb4
JJ
5439struct collect_gnu_hash_codes
5440{
5441 bfd *output_bfd;
5442 const struct elf_backend_data *bed;
5443 unsigned long int nsyms;
5444 unsigned long int maskbits;
5445 unsigned long int *hashcodes;
5446 unsigned long int *hashval;
5447 unsigned long int *indx;
5448 unsigned long int *counts;
5449 bfd_vma *bitmask;
5450 bfd_byte *contents;
5451 long int min_dynindx;
5452 unsigned long int bucketcount;
5453 unsigned long int symindx;
5454 long int local_indx;
5455 long int shift1, shift2;
5456 unsigned long int mask;
14b1c01e 5457 bfd_boolean error;
fdc90cb4
JJ
5458};
5459
5460/* This function will be called though elf_link_hash_traverse to store
5461 all hash value of the exported symbols in an array. */
5462
5463static bfd_boolean
5464elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5465{
a50b1753 5466 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5467 const char *name;
fdc90cb4
JJ
5468 unsigned long ha;
5469 char *alc = NULL;
5470
fdc90cb4
JJ
5471 /* Ignore indirect symbols. These are added by the versioning code. */
5472 if (h->dynindx == -1)
5473 return TRUE;
5474
5475 /* Ignore also local symbols and undefined symbols. */
5476 if (! (*s->bed->elf_hash_symbol) (h))
5477 return TRUE;
5478
5479 name = h->root.root.string;
422f1182 5480 if (h->versioned >= versioned)
fdc90cb4 5481 {
422f1182
L
5482 char *p = strchr (name, ELF_VER_CHR);
5483 if (p != NULL)
14b1c01e 5484 {
422f1182
L
5485 alc = (char *) bfd_malloc (p - name + 1);
5486 if (alc == NULL)
5487 {
5488 s->error = TRUE;
5489 return FALSE;
5490 }
5491 memcpy (alc, name, p - name);
5492 alc[p - name] = '\0';
5493 name = alc;
14b1c01e 5494 }
fdc90cb4
JJ
5495 }
5496
5497 /* Compute the hash value. */
5498 ha = bfd_elf_gnu_hash (name);
5499
5500 /* Store the found hash value in the array for compute_bucket_count,
5501 and also for .dynsym reordering purposes. */
5502 s->hashcodes[s->nsyms] = ha;
5503 s->hashval[h->dynindx] = ha;
5504 ++s->nsyms;
5505 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5506 s->min_dynindx = h->dynindx;
5507
5508 if (alc != NULL)
5509 free (alc);
5510
5511 return TRUE;
5512}
5513
5514/* This function will be called though elf_link_hash_traverse to do
5515 final dynaminc symbol renumbering. */
5516
5517static bfd_boolean
5518elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5519{
a50b1753 5520 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5521 unsigned long int bucket;
5522 unsigned long int val;
5523
fdc90cb4
JJ
5524 /* Ignore indirect symbols. */
5525 if (h->dynindx == -1)
5526 return TRUE;
5527
5528 /* Ignore also local symbols and undefined symbols. */
5529 if (! (*s->bed->elf_hash_symbol) (h))
5530 {
5531 if (h->dynindx >= s->min_dynindx)
5532 h->dynindx = s->local_indx++;
5533 return TRUE;
5534 }
5535
5536 bucket = s->hashval[h->dynindx] % s->bucketcount;
5537 val = (s->hashval[h->dynindx] >> s->shift1)
5538 & ((s->maskbits >> s->shift1) - 1);
5539 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5540 s->bitmask[val]
5541 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5542 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5543 if (s->counts[bucket] == 1)
5544 /* Last element terminates the chain. */
5545 val |= 1;
5546 bfd_put_32 (s->output_bfd, val,
5547 s->contents + (s->indx[bucket] - s->symindx) * 4);
5548 --s->counts[bucket];
5549 h->dynindx = s->indx[bucket]++;
5550 return TRUE;
5551}
5552
5553/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5554
5555bfd_boolean
5556_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5557{
5558 return !(h->forced_local
5559 || h->root.type == bfd_link_hash_undefined
5560 || h->root.type == bfd_link_hash_undefweak
5561 || ((h->root.type == bfd_link_hash_defined
5562 || h->root.type == bfd_link_hash_defweak)
5563 && h->root.u.def.section->output_section == NULL));
5564}
5565
5a580b3a
AM
5566/* Array used to determine the number of hash table buckets to use
5567 based on the number of symbols there are. If there are fewer than
5568 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5569 fewer than 37 we use 17 buckets, and so forth. We never use more
5570 than 32771 buckets. */
5571
5572static const size_t elf_buckets[] =
5573{
5574 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5575 16411, 32771, 0
5576};
5577
5578/* Compute bucket count for hashing table. We do not use a static set
5579 of possible tables sizes anymore. Instead we determine for all
5580 possible reasonable sizes of the table the outcome (i.e., the
5581 number of collisions etc) and choose the best solution. The
5582 weighting functions are not too simple to allow the table to grow
5583 without bounds. Instead one of the weighting factors is the size.
5584 Therefore the result is always a good payoff between few collisions
5585 (= short chain lengths) and table size. */
5586static size_t
b20dd2ce 5587compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5588 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5589 unsigned long int nsyms,
5590 int gnu_hash)
5a580b3a 5591{
5a580b3a 5592 size_t best_size = 0;
5a580b3a 5593 unsigned long int i;
5a580b3a 5594
5a580b3a
AM
5595 /* We have a problem here. The following code to optimize the table
5596 size requires an integer type with more the 32 bits. If
5597 BFD_HOST_U_64_BIT is set we know about such a type. */
5598#ifdef BFD_HOST_U_64_BIT
5599 if (info->optimize)
5600 {
5a580b3a
AM
5601 size_t minsize;
5602 size_t maxsize;
5603 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5604 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5605 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5606 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5607 unsigned long int *counts;
d40f3da9 5608 bfd_size_type amt;
0883b6e0 5609 unsigned int no_improvement_count = 0;
5a580b3a
AM
5610
5611 /* Possible optimization parameters: if we have NSYMS symbols we say
5612 that the hashing table must at least have NSYMS/4 and at most
5613 2*NSYMS buckets. */
5614 minsize = nsyms / 4;
5615 if (minsize == 0)
5616 minsize = 1;
5617 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5618 if (gnu_hash)
5619 {
5620 if (minsize < 2)
5621 minsize = 2;
5622 if ((best_size & 31) == 0)
5623 ++best_size;
5624 }
5a580b3a
AM
5625
5626 /* Create array where we count the collisions in. We must use bfd_malloc
5627 since the size could be large. */
5628 amt = maxsize;
5629 amt *= sizeof (unsigned long int);
a50b1753 5630 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5631 if (counts == NULL)
fdc90cb4 5632 return 0;
5a580b3a
AM
5633
5634 /* Compute the "optimal" size for the hash table. The criteria is a
5635 minimal chain length. The minor criteria is (of course) the size
5636 of the table. */
5637 for (i = minsize; i < maxsize; ++i)
5638 {
5639 /* Walk through the array of hashcodes and count the collisions. */
5640 BFD_HOST_U_64_BIT max;
5641 unsigned long int j;
5642 unsigned long int fact;
5643
fdc90cb4
JJ
5644 if (gnu_hash && (i & 31) == 0)
5645 continue;
5646
5a580b3a
AM
5647 memset (counts, '\0', i * sizeof (unsigned long int));
5648
5649 /* Determine how often each hash bucket is used. */
5650 for (j = 0; j < nsyms; ++j)
5651 ++counts[hashcodes[j] % i];
5652
5653 /* For the weight function we need some information about the
5654 pagesize on the target. This is information need not be 100%
5655 accurate. Since this information is not available (so far) we
5656 define it here to a reasonable default value. If it is crucial
5657 to have a better value some day simply define this value. */
5658# ifndef BFD_TARGET_PAGESIZE
5659# define BFD_TARGET_PAGESIZE (4096)
5660# endif
5661
fdc90cb4
JJ
5662 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5663 and the chains. */
5664 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5665
5666# if 1
5667 /* Variant 1: optimize for short chains. We add the squares
5668 of all the chain lengths (which favors many small chain
5669 over a few long chains). */
5670 for (j = 0; j < i; ++j)
5671 max += counts[j] * counts[j];
5672
5673 /* This adds penalties for the overall size of the table. */
fdc90cb4 5674 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5675 max *= fact * fact;
5676# else
5677 /* Variant 2: Optimize a lot more for small table. Here we
5678 also add squares of the size but we also add penalties for
5679 empty slots (the +1 term). */
5680 for (j = 0; j < i; ++j)
5681 max += (1 + counts[j]) * (1 + counts[j]);
5682
5683 /* The overall size of the table is considered, but not as
5684 strong as in variant 1, where it is squared. */
fdc90cb4 5685 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5686 max *= fact;
5687# endif
5688
5689 /* Compare with current best results. */
5690 if (max < best_chlen)
5691 {
5692 best_chlen = max;
5693 best_size = i;
ca4be51c 5694 no_improvement_count = 0;
5a580b3a 5695 }
0883b6e0
NC
5696 /* PR 11843: Avoid futile long searches for the best bucket size
5697 when there are a large number of symbols. */
5698 else if (++no_improvement_count == 100)
5699 break;
5a580b3a
AM
5700 }
5701
5702 free (counts);
5703 }
5704 else
5705#endif /* defined (BFD_HOST_U_64_BIT) */
5706 {
5707 /* This is the fallback solution if no 64bit type is available or if we
5708 are not supposed to spend much time on optimizations. We select the
5709 bucket count using a fixed set of numbers. */
5710 for (i = 0; elf_buckets[i] != 0; i++)
5711 {
5712 best_size = elf_buckets[i];
fdc90cb4 5713 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5714 break;
5715 }
fdc90cb4
JJ
5716 if (gnu_hash && best_size < 2)
5717 best_size = 2;
5a580b3a
AM
5718 }
5719
5a580b3a
AM
5720 return best_size;
5721}
5722
d0bf826b
AM
5723/* Size any SHT_GROUP section for ld -r. */
5724
5725bfd_boolean
5726_bfd_elf_size_group_sections (struct bfd_link_info *info)
5727{
5728 bfd *ibfd;
5729
c72f2fb2 5730 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5731 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5732 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5733 return FALSE;
5734 return TRUE;
5735}
5736
04c3a755
NS
5737/* Set a default stack segment size. The value in INFO wins. If it
5738 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5739 undefined it is initialized. */
5740
5741bfd_boolean
5742bfd_elf_stack_segment_size (bfd *output_bfd,
5743 struct bfd_link_info *info,
5744 const char *legacy_symbol,
5745 bfd_vma default_size)
5746{
5747 struct elf_link_hash_entry *h = NULL;
5748
5749 /* Look for legacy symbol. */
5750 if (legacy_symbol)
5751 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5752 FALSE, FALSE, FALSE);
5753 if (h && (h->root.type == bfd_link_hash_defined
5754 || h->root.type == bfd_link_hash_defweak)
5755 && h->def_regular
5756 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5757 {
5758 /* The symbol has no type if specified on the command line. */
5759 h->type = STT_OBJECT;
5760 if (info->stacksize)
5761 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5762 output_bfd, legacy_symbol);
5763 else if (h->root.u.def.section != bfd_abs_section_ptr)
5764 (*_bfd_error_handler) (_("%B: %s not absolute"),
5765 output_bfd, legacy_symbol);
5766 else
5767 info->stacksize = h->root.u.def.value;
5768 }
5769
5770 if (!info->stacksize)
5771 /* If the user didn't set a size, or explicitly inhibit the
5772 size, set it now. */
5773 info->stacksize = default_size;
5774
5775 /* Provide the legacy symbol, if it is referenced. */
5776 if (h && (h->root.type == bfd_link_hash_undefined
5777 || h->root.type == bfd_link_hash_undefweak))
5778 {
5779 struct bfd_link_hash_entry *bh = NULL;
5780
5781 if (!(_bfd_generic_link_add_one_symbol
5782 (info, output_bfd, legacy_symbol,
5783 BSF_GLOBAL, bfd_abs_section_ptr,
5784 info->stacksize >= 0 ? info->stacksize : 0,
5785 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5786 return FALSE;
5787
5788 h = (struct elf_link_hash_entry *) bh;
5789 h->def_regular = 1;
5790 h->type = STT_OBJECT;
5791 }
5792
5793 return TRUE;
5794}
5795
5a580b3a
AM
5796/* Set up the sizes and contents of the ELF dynamic sections. This is
5797 called by the ELF linker emulation before_allocation routine. We
5798 must set the sizes of the sections before the linker sets the
5799 addresses of the various sections. */
5800
5801bfd_boolean
5802bfd_elf_size_dynamic_sections (bfd *output_bfd,
5803 const char *soname,
5804 const char *rpath,
5805 const char *filter_shlib,
7ee314fa
AM
5806 const char *audit,
5807 const char *depaudit,
5a580b3a
AM
5808 const char * const *auxiliary_filters,
5809 struct bfd_link_info *info,
fd91d419 5810 asection **sinterpptr)
5a580b3a 5811{
ef53be89 5812 size_t soname_indx;
5a580b3a
AM
5813 bfd *dynobj;
5814 const struct elf_backend_data *bed;
28caa186 5815 struct elf_info_failed asvinfo;
5a580b3a
AM
5816
5817 *sinterpptr = NULL;
5818
ef53be89 5819 soname_indx = (size_t) -1;
5a580b3a
AM
5820
5821 if (!is_elf_hash_table (info->hash))
5822 return TRUE;
5823
6bfdb61b 5824 bed = get_elf_backend_data (output_bfd);
04c3a755
NS
5825
5826 /* Any syms created from now on start with -1 in
5827 got.refcount/offset and plt.refcount/offset. */
5828 elf_hash_table (info)->init_got_refcount
5829 = elf_hash_table (info)->init_got_offset;
5830 elf_hash_table (info)->init_plt_refcount
5831 = elf_hash_table (info)->init_plt_offset;
5832
0e1862bb 5833 if (bfd_link_relocatable (info)
04c3a755
NS
5834 && !_bfd_elf_size_group_sections (info))
5835 return FALSE;
5836
5837 /* The backend may have to create some sections regardless of whether
5838 we're dynamic or not. */
5839 if (bed->elf_backend_always_size_sections
5840 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5841 return FALSE;
5842
5843 /* Determine any GNU_STACK segment requirements, after the backend
5844 has had a chance to set a default segment size. */
5a580b3a 5845 if (info->execstack)
12bd6957 5846 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5a580b3a 5847 else if (info->noexecstack)
12bd6957 5848 elf_stack_flags (output_bfd) = PF_R | PF_W;
5a580b3a
AM
5849 else
5850 {
5851 bfd *inputobj;
5852 asection *notesec = NULL;
5853 int exec = 0;
5854
5855 for (inputobj = info->input_bfds;
5856 inputobj;
c72f2fb2 5857 inputobj = inputobj->link.next)
5a580b3a
AM
5858 {
5859 asection *s;
5860
a92c088a
L
5861 if (inputobj->flags
5862 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5863 continue;
5864 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5865 if (s)
5866 {
5867 if (s->flags & SEC_CODE)
5868 exec = PF_X;
5869 notesec = s;
5870 }
6bfdb61b 5871 else if (bed->default_execstack)
5a580b3a
AM
5872 exec = PF_X;
5873 }
04c3a755 5874 if (notesec || info->stacksize > 0)
12bd6957 5875 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
0e1862bb 5876 if (notesec && exec && bfd_link_relocatable (info)
04c3a755
NS
5877 && notesec->output_section != bfd_abs_section_ptr)
5878 notesec->output_section->flags |= SEC_CODE;
5a580b3a
AM
5879 }
5880
5a580b3a
AM
5881 dynobj = elf_hash_table (info)->dynobj;
5882
9a2a56cc 5883 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
5884 {
5885 struct elf_info_failed eif;
5886 struct elf_link_hash_entry *h;
5887 asection *dynstr;
5888 struct bfd_elf_version_tree *t;
5889 struct bfd_elf_version_expr *d;
046183de 5890 asection *s;
5a580b3a
AM
5891 bfd_boolean all_defined;
5892
3d4d4302 5893 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
9b8b325a 5894 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5a580b3a
AM
5895
5896 if (soname != NULL)
5897 {
5898 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5899 soname, TRUE);
ef53be89 5900 if (soname_indx == (size_t) -1
5a580b3a
AM
5901 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5902 return FALSE;
5903 }
5904
5905 if (info->symbolic)
5906 {
5907 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5908 return FALSE;
5909 info->flags |= DF_SYMBOLIC;
5910 }
5911
5912 if (rpath != NULL)
5913 {
ef53be89 5914 size_t indx;
b1b00fcc 5915 bfd_vma tag;
5a580b3a
AM
5916
5917 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5918 TRUE);
ef53be89 5919 if (indx == (size_t) -1)
5a580b3a
AM
5920 return FALSE;
5921
b1b00fcc
MF
5922 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5923 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5924 return FALSE;
5a580b3a
AM
5925 }
5926
5927 if (filter_shlib != NULL)
5928 {
ef53be89 5929 size_t indx;
5a580b3a
AM
5930
5931 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5932 filter_shlib, TRUE);
ef53be89 5933 if (indx == (size_t) -1
5a580b3a
AM
5934 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5935 return FALSE;
5936 }
5937
5938 if (auxiliary_filters != NULL)
5939 {
5940 const char * const *p;
5941
5942 for (p = auxiliary_filters; *p != NULL; p++)
5943 {
ef53be89 5944 size_t indx;
5a580b3a
AM
5945
5946 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5947 *p, TRUE);
ef53be89 5948 if (indx == (size_t) -1
5a580b3a
AM
5949 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5950 return FALSE;
5951 }
5952 }
5953
7ee314fa
AM
5954 if (audit != NULL)
5955 {
ef53be89 5956 size_t indx;
7ee314fa
AM
5957
5958 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5959 TRUE);
ef53be89 5960 if (indx == (size_t) -1
7ee314fa
AM
5961 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5962 return FALSE;
5963 }
5964
5965 if (depaudit != NULL)
5966 {
ef53be89 5967 size_t indx;
7ee314fa
AM
5968
5969 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5970 TRUE);
ef53be89 5971 if (indx == (size_t) -1
7ee314fa
AM
5972 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5973 return FALSE;
5974 }
5975
5a580b3a 5976 eif.info = info;
5a580b3a
AM
5977 eif.failed = FALSE;
5978
5979 /* If we are supposed to export all symbols into the dynamic symbol
5980 table (this is not the normal case), then do so. */
55255dae 5981 if (info->export_dynamic
0e1862bb 5982 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
5983 {
5984 elf_link_hash_traverse (elf_hash_table (info),
5985 _bfd_elf_export_symbol,
5986 &eif);
5987 if (eif.failed)
5988 return FALSE;
5989 }
5990
5991 /* Make all global versions with definition. */
fd91d419 5992 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5993 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5994 if (!d->symver && d->literal)
5a580b3a
AM
5995 {
5996 const char *verstr, *name;
5997 size_t namelen, verlen, newlen;
93252b1c 5998 char *newname, *p, leading_char;
5a580b3a
AM
5999 struct elf_link_hash_entry *newh;
6000
93252b1c 6001 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6002 name = d->pattern;
93252b1c 6003 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6004 verstr = t->name;
6005 verlen = strlen (verstr);
6006 newlen = namelen + verlen + 3;
6007
a50b1753 6008 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6009 if (newname == NULL)
6010 return FALSE;
93252b1c
MF
6011 newname[0] = leading_char;
6012 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6013
6014 /* Check the hidden versioned definition. */
6015 p = newname + namelen;
6016 *p++ = ELF_VER_CHR;
6017 memcpy (p, verstr, verlen + 1);
6018 newh = elf_link_hash_lookup (elf_hash_table (info),
6019 newname, FALSE, FALSE,
6020 FALSE);
6021 if (newh == NULL
6022 || (newh->root.type != bfd_link_hash_defined
6023 && newh->root.type != bfd_link_hash_defweak))
6024 {
6025 /* Check the default versioned definition. */
6026 *p++ = ELF_VER_CHR;
6027 memcpy (p, verstr, verlen + 1);
6028 newh = elf_link_hash_lookup (elf_hash_table (info),
6029 newname, FALSE, FALSE,
6030 FALSE);
6031 }
6032 free (newname);
6033
6034 /* Mark this version if there is a definition and it is
6035 not defined in a shared object. */
6036 if (newh != NULL
f5385ebf 6037 && !newh->def_dynamic
5a580b3a
AM
6038 && (newh->root.type == bfd_link_hash_defined
6039 || newh->root.type == bfd_link_hash_defweak))
6040 d->symver = 1;
6041 }
6042
6043 /* Attach all the symbols to their version information. */
5a580b3a 6044 asvinfo.info = info;
5a580b3a
AM
6045 asvinfo.failed = FALSE;
6046
6047 elf_link_hash_traverse (elf_hash_table (info),
6048 _bfd_elf_link_assign_sym_version,
6049 &asvinfo);
6050 if (asvinfo.failed)
6051 return FALSE;
6052
6053 if (!info->allow_undefined_version)
6054 {
6055 /* Check if all global versions have a definition. */
6056 all_defined = TRUE;
fd91d419 6057 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6058 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6059 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
6060 {
6061 (*_bfd_error_handler)
6062 (_("%s: undefined version: %s"),
6063 d->pattern, t->name);
6064 all_defined = FALSE;
6065 }
6066
6067 if (!all_defined)
6068 {
6069 bfd_set_error (bfd_error_bad_value);
6070 return FALSE;
6071 }
6072 }
6073
6074 /* Find all symbols which were defined in a dynamic object and make
6075 the backend pick a reasonable value for them. */
6076 elf_link_hash_traverse (elf_hash_table (info),
6077 _bfd_elf_adjust_dynamic_symbol,
6078 &eif);
6079 if (eif.failed)
6080 return FALSE;
6081
6082 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 6083 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
6084 now so that we know the final size of the .dynamic section. */
6085
6086 /* If there are initialization and/or finalization functions to
6087 call then add the corresponding DT_INIT/DT_FINI entries. */
6088 h = (info->init_function
6089 ? elf_link_hash_lookup (elf_hash_table (info),
6090 info->init_function, FALSE,
6091 FALSE, FALSE)
6092 : NULL);
6093 if (h != NULL
f5385ebf
AM
6094 && (h->ref_regular
6095 || h->def_regular))
5a580b3a
AM
6096 {
6097 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6098 return FALSE;
6099 }
6100 h = (info->fini_function
6101 ? elf_link_hash_lookup (elf_hash_table (info),
6102 info->fini_function, FALSE,
6103 FALSE, FALSE)
6104 : NULL);
6105 if (h != NULL
f5385ebf
AM
6106 && (h->ref_regular
6107 || h->def_regular))
5a580b3a
AM
6108 {
6109 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6110 return FALSE;
6111 }
6112
046183de
AM
6113 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6114 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6115 {
6116 /* DT_PREINIT_ARRAY is not allowed in shared library. */
0e1862bb 6117 if (! bfd_link_executable (info))
5a580b3a
AM
6118 {
6119 bfd *sub;
6120 asection *o;
6121
6122 for (sub = info->input_bfds; sub != NULL;
c72f2fb2 6123 sub = sub->link.next)
3fcd97f1
JJ
6124 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6125 for (o = sub->sections; o != NULL; o = o->next)
6126 if (elf_section_data (o)->this_hdr.sh_type
6127 == SHT_PREINIT_ARRAY)
6128 {
6129 (*_bfd_error_handler)
6130 (_("%B: .preinit_array section is not allowed in DSO"),
6131 sub);
6132 break;
6133 }
5a580b3a
AM
6134
6135 bfd_set_error (bfd_error_nonrepresentable_section);
6136 return FALSE;
6137 }
6138
6139 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6140 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6141 return FALSE;
6142 }
046183de
AM
6143 s = bfd_get_section_by_name (output_bfd, ".init_array");
6144 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6145 {
6146 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6147 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6148 return FALSE;
6149 }
046183de
AM
6150 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6151 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6152 {
6153 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6154 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6155 return FALSE;
6156 }
6157
3d4d4302 6158 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6159 /* If .dynstr is excluded from the link, we don't want any of
6160 these tags. Strictly, we should be checking each section
6161 individually; This quick check covers for the case where
6162 someone does a /DISCARD/ : { *(*) }. */
6163 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6164 {
6165 bfd_size_type strsize;
6166
6167 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
6168 if ((info->emit_hash
6169 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6170 || (info->emit_gnu_hash
6171 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
6172 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6173 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6174 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6175 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6176 bed->s->sizeof_sym))
6177 return FALSE;
6178 }
6179 }
6180
de231f20
CM
6181 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6182 return FALSE;
6183
5a580b3a
AM
6184 /* The backend must work out the sizes of all the other dynamic
6185 sections. */
9a2a56cc
AM
6186 if (dynobj != NULL
6187 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
6188 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6189 return FALSE;
6190
9a2a56cc 6191 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6192 {
554220db 6193 unsigned long section_sym_count;
fd91d419 6194 struct bfd_elf_version_tree *verdefs;
5a580b3a 6195 asection *s;
5a580b3a
AM
6196
6197 /* Set up the version definition section. */
3d4d4302 6198 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
6199 BFD_ASSERT (s != NULL);
6200
6201 /* We may have created additional version definitions if we are
6202 just linking a regular application. */
fd91d419 6203 verdefs = info->version_info;
5a580b3a
AM
6204
6205 /* Skip anonymous version tag. */
6206 if (verdefs != NULL && verdefs->vernum == 0)
6207 verdefs = verdefs->next;
6208
3e3b46e5 6209 if (verdefs == NULL && !info->create_default_symver)
8423293d 6210 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6211 else
6212 {
6213 unsigned int cdefs;
6214 bfd_size_type size;
6215 struct bfd_elf_version_tree *t;
6216 bfd_byte *p;
6217 Elf_Internal_Verdef def;
6218 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
6219 struct bfd_link_hash_entry *bh;
6220 struct elf_link_hash_entry *h;
6221 const char *name;
5a580b3a
AM
6222
6223 cdefs = 0;
6224 size = 0;
6225
6226 /* Make space for the base version. */
6227 size += sizeof (Elf_External_Verdef);
6228 size += sizeof (Elf_External_Verdaux);
6229 ++cdefs;
6230
3e3b46e5
PB
6231 /* Make space for the default version. */
6232 if (info->create_default_symver)
6233 {
6234 size += sizeof (Elf_External_Verdef);
6235 ++cdefs;
6236 }
6237
5a580b3a
AM
6238 for (t = verdefs; t != NULL; t = t->next)
6239 {
6240 struct bfd_elf_version_deps *n;
6241
a6cc6b3b
RO
6242 /* Don't emit base version twice. */
6243 if (t->vernum == 0)
6244 continue;
6245
5a580b3a
AM
6246 size += sizeof (Elf_External_Verdef);
6247 size += sizeof (Elf_External_Verdaux);
6248 ++cdefs;
6249
6250 for (n = t->deps; n != NULL; n = n->next)
6251 size += sizeof (Elf_External_Verdaux);
6252 }
6253
eea6121a 6254 s->size = size;
a50b1753 6255 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6256 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6257 return FALSE;
6258
6259 /* Fill in the version definition section. */
6260
6261 p = s->contents;
6262
6263 def.vd_version = VER_DEF_CURRENT;
6264 def.vd_flags = VER_FLG_BASE;
6265 def.vd_ndx = 1;
6266 def.vd_cnt = 1;
3e3b46e5
PB
6267 if (info->create_default_symver)
6268 {
6269 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6270 def.vd_next = sizeof (Elf_External_Verdef);
6271 }
6272 else
6273 {
6274 def.vd_aux = sizeof (Elf_External_Verdef);
6275 def.vd_next = (sizeof (Elf_External_Verdef)
6276 + sizeof (Elf_External_Verdaux));
6277 }
5a580b3a 6278
ef53be89 6279 if (soname_indx != (size_t) -1)
5a580b3a
AM
6280 {
6281 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6282 soname_indx);
6283 def.vd_hash = bfd_elf_hash (soname);
6284 defaux.vda_name = soname_indx;
3e3b46e5 6285 name = soname;
5a580b3a
AM
6286 }
6287 else
6288 {
ef53be89 6289 size_t indx;
5a580b3a 6290
06084812 6291 name = lbasename (output_bfd->filename);
5a580b3a
AM
6292 def.vd_hash = bfd_elf_hash (name);
6293 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6294 name, FALSE);
ef53be89 6295 if (indx == (size_t) -1)
5a580b3a
AM
6296 return FALSE;
6297 defaux.vda_name = indx;
6298 }
6299 defaux.vda_next = 0;
6300
6301 _bfd_elf_swap_verdef_out (output_bfd, &def,
6302 (Elf_External_Verdef *) p);
6303 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6304 if (info->create_default_symver)
6305 {
6306 /* Add a symbol representing this version. */
6307 bh = NULL;
6308 if (! (_bfd_generic_link_add_one_symbol
6309 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6310 0, NULL, FALSE,
6311 get_elf_backend_data (dynobj)->collect, &bh)))
6312 return FALSE;
6313 h = (struct elf_link_hash_entry *) bh;
6314 h->non_elf = 0;
6315 h->def_regular = 1;
6316 h->type = STT_OBJECT;
6317 h->verinfo.vertree = NULL;
6318
6319 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6320 return FALSE;
6321
6322 /* Create a duplicate of the base version with the same
6323 aux block, but different flags. */
6324 def.vd_flags = 0;
6325 def.vd_ndx = 2;
6326 def.vd_aux = sizeof (Elf_External_Verdef);
6327 if (verdefs)
6328 def.vd_next = (sizeof (Elf_External_Verdef)
6329 + sizeof (Elf_External_Verdaux));
6330 else
6331 def.vd_next = 0;
6332 _bfd_elf_swap_verdef_out (output_bfd, &def,
6333 (Elf_External_Verdef *) p);
6334 p += sizeof (Elf_External_Verdef);
6335 }
5a580b3a
AM
6336 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6337 (Elf_External_Verdaux *) p);
6338 p += sizeof (Elf_External_Verdaux);
6339
6340 for (t = verdefs; t != NULL; t = t->next)
6341 {
6342 unsigned int cdeps;
6343 struct bfd_elf_version_deps *n;
5a580b3a 6344
a6cc6b3b
RO
6345 /* Don't emit the base version twice. */
6346 if (t->vernum == 0)
6347 continue;
6348
5a580b3a
AM
6349 cdeps = 0;
6350 for (n = t->deps; n != NULL; n = n->next)
6351 ++cdeps;
6352
6353 /* Add a symbol representing this version. */
6354 bh = NULL;
6355 if (! (_bfd_generic_link_add_one_symbol
6356 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6357 0, NULL, FALSE,
6358 get_elf_backend_data (dynobj)->collect, &bh)))
6359 return FALSE;
6360 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6361 h->non_elf = 0;
6362 h->def_regular = 1;
5a580b3a
AM
6363 h->type = STT_OBJECT;
6364 h->verinfo.vertree = t;
6365
c152c796 6366 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6367 return FALSE;
6368
6369 def.vd_version = VER_DEF_CURRENT;
6370 def.vd_flags = 0;
6371 if (t->globals.list == NULL
6372 && t->locals.list == NULL
6373 && ! t->used)
6374 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6375 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6376 def.vd_cnt = cdeps + 1;
6377 def.vd_hash = bfd_elf_hash (t->name);
6378 def.vd_aux = sizeof (Elf_External_Verdef);
6379 def.vd_next = 0;
a6cc6b3b
RO
6380
6381 /* If a basever node is next, it *must* be the last node in
6382 the chain, otherwise Verdef construction breaks. */
6383 if (t->next != NULL && t->next->vernum == 0)
6384 BFD_ASSERT (t->next->next == NULL);
6385
6386 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6387 def.vd_next = (sizeof (Elf_External_Verdef)
6388 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6389
6390 _bfd_elf_swap_verdef_out (output_bfd, &def,
6391 (Elf_External_Verdef *) p);
6392 p += sizeof (Elf_External_Verdef);
6393
6394 defaux.vda_name = h->dynstr_index;
6395 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6396 h->dynstr_index);
6397 defaux.vda_next = 0;
6398 if (t->deps != NULL)
6399 defaux.vda_next = sizeof (Elf_External_Verdaux);
6400 t->name_indx = defaux.vda_name;
6401
6402 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6403 (Elf_External_Verdaux *) p);
6404 p += sizeof (Elf_External_Verdaux);
6405
6406 for (n = t->deps; n != NULL; n = n->next)
6407 {
6408 if (n->version_needed == NULL)
6409 {
6410 /* This can happen if there was an error in the
6411 version script. */
6412 defaux.vda_name = 0;
6413 }
6414 else
6415 {
6416 defaux.vda_name = n->version_needed->name_indx;
6417 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6418 defaux.vda_name);
6419 }
6420 if (n->next == NULL)
6421 defaux.vda_next = 0;
6422 else
6423 defaux.vda_next = sizeof (Elf_External_Verdaux);
6424
6425 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6426 (Elf_External_Verdaux *) p);
6427 p += sizeof (Elf_External_Verdaux);
6428 }
6429 }
6430
6431 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6432 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6433 return FALSE;
6434
6435 elf_tdata (output_bfd)->cverdefs = cdefs;
6436 }
6437
6438 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6439 {
6440 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6441 return FALSE;
6442 }
6443 else if (info->flags & DF_BIND_NOW)
6444 {
6445 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6446 return FALSE;
6447 }
6448
6449 if (info->flags_1)
6450 {
0e1862bb 6451 if (bfd_link_executable (info))
5a580b3a
AM
6452 info->flags_1 &= ~ (DF_1_INITFIRST
6453 | DF_1_NODELETE
6454 | DF_1_NOOPEN);
6455 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6456 return FALSE;
6457 }
6458
6459 /* Work out the size of the version reference section. */
6460
3d4d4302 6461 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6462 BFD_ASSERT (s != NULL);
6463 {
6464 struct elf_find_verdep_info sinfo;
6465
5a580b3a
AM
6466 sinfo.info = info;
6467 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6468 if (sinfo.vers == 0)
6469 sinfo.vers = 1;
6470 sinfo.failed = FALSE;
6471
6472 elf_link_hash_traverse (elf_hash_table (info),
6473 _bfd_elf_link_find_version_dependencies,
6474 &sinfo);
14b1c01e
AM
6475 if (sinfo.failed)
6476 return FALSE;
5a580b3a
AM
6477
6478 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6479 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6480 else
6481 {
6482 Elf_Internal_Verneed *t;
6483 unsigned int size;
6484 unsigned int crefs;
6485 bfd_byte *p;
6486
a6cc6b3b 6487 /* Build the version dependency section. */
5a580b3a
AM
6488 size = 0;
6489 crefs = 0;
6490 for (t = elf_tdata (output_bfd)->verref;
6491 t != NULL;
6492 t = t->vn_nextref)
6493 {
6494 Elf_Internal_Vernaux *a;
6495
6496 size += sizeof (Elf_External_Verneed);
6497 ++crefs;
6498 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6499 size += sizeof (Elf_External_Vernaux);
6500 }
6501
eea6121a 6502 s->size = size;
a50b1753 6503 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6504 if (s->contents == NULL)
6505 return FALSE;
6506
6507 p = s->contents;
6508 for (t = elf_tdata (output_bfd)->verref;
6509 t != NULL;
6510 t = t->vn_nextref)
6511 {
6512 unsigned int caux;
6513 Elf_Internal_Vernaux *a;
ef53be89 6514 size_t indx;
5a580b3a
AM
6515
6516 caux = 0;
6517 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6518 ++caux;
6519
6520 t->vn_version = VER_NEED_CURRENT;
6521 t->vn_cnt = caux;
6522 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6523 elf_dt_name (t->vn_bfd) != NULL
6524 ? elf_dt_name (t->vn_bfd)
06084812 6525 : lbasename (t->vn_bfd->filename),
5a580b3a 6526 FALSE);
ef53be89 6527 if (indx == (size_t) -1)
5a580b3a
AM
6528 return FALSE;
6529 t->vn_file = indx;
6530 t->vn_aux = sizeof (Elf_External_Verneed);
6531 if (t->vn_nextref == NULL)
6532 t->vn_next = 0;
6533 else
6534 t->vn_next = (sizeof (Elf_External_Verneed)
6535 + caux * sizeof (Elf_External_Vernaux));
6536
6537 _bfd_elf_swap_verneed_out (output_bfd, t,
6538 (Elf_External_Verneed *) p);
6539 p += sizeof (Elf_External_Verneed);
6540
6541 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6542 {
6543 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6544 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6545 a->vna_nodename, FALSE);
ef53be89 6546 if (indx == (size_t) -1)
5a580b3a
AM
6547 return FALSE;
6548 a->vna_name = indx;
6549 if (a->vna_nextptr == NULL)
6550 a->vna_next = 0;
6551 else
6552 a->vna_next = sizeof (Elf_External_Vernaux);
6553
6554 _bfd_elf_swap_vernaux_out (output_bfd, a,
6555 (Elf_External_Vernaux *) p);
6556 p += sizeof (Elf_External_Vernaux);
6557 }
6558 }
6559
6560 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6561 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6562 return FALSE;
6563
6564 elf_tdata (output_bfd)->cverrefs = crefs;
6565 }
6566 }
6567
8423293d
AM
6568 if ((elf_tdata (output_bfd)->cverrefs == 0
6569 && elf_tdata (output_bfd)->cverdefs == 0)
6570 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6571 &section_sym_count) == 0)
6572 {
3d4d4302 6573 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6574 s->flags |= SEC_EXCLUDE;
6575 }
6576 }
6577 return TRUE;
6578}
6579
74541ad4
AM
6580/* Find the first non-excluded output section. We'll use its
6581 section symbol for some emitted relocs. */
6582void
6583_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6584{
6585 asection *s;
6586
6587 for (s = output_bfd->sections; s != NULL; s = s->next)
6588 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6589 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6590 {
6591 elf_hash_table (info)->text_index_section = s;
6592 break;
6593 }
6594}
6595
6596/* Find two non-excluded output sections, one for code, one for data.
6597 We'll use their section symbols for some emitted relocs. */
6598void
6599_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6600{
6601 asection *s;
6602
266b05cf
DJ
6603 /* Data first, since setting text_index_section changes
6604 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6605 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6606 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6607 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6608 {
266b05cf 6609 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6610 break;
6611 }
6612
6613 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6614 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6615 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6616 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6617 {
266b05cf 6618 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6619 break;
6620 }
6621
6622 if (elf_hash_table (info)->text_index_section == NULL)
6623 elf_hash_table (info)->text_index_section
6624 = elf_hash_table (info)->data_index_section;
6625}
6626
8423293d
AM
6627bfd_boolean
6628bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6629{
74541ad4
AM
6630 const struct elf_backend_data *bed;
6631
8423293d
AM
6632 if (!is_elf_hash_table (info->hash))
6633 return TRUE;
6634
74541ad4
AM
6635 bed = get_elf_backend_data (output_bfd);
6636 (*bed->elf_backend_init_index_section) (output_bfd, info);
6637
8423293d
AM
6638 if (elf_hash_table (info)->dynamic_sections_created)
6639 {
6640 bfd *dynobj;
8423293d
AM
6641 asection *s;
6642 bfd_size_type dynsymcount;
6643 unsigned long section_sym_count;
8423293d
AM
6644 unsigned int dtagcount;
6645
6646 dynobj = elf_hash_table (info)->dynobj;
6647
5a580b3a
AM
6648 /* Assign dynsym indicies. In a shared library we generate a
6649 section symbol for each output section, which come first.
6650 Next come all of the back-end allocated local dynamic syms,
6651 followed by the rest of the global symbols. */
6652
554220db
AM
6653 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6654 &section_sym_count);
5a580b3a
AM
6655
6656 /* Work out the size of the symbol version section. */
3d4d4302 6657 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6658 BFD_ASSERT (s != NULL);
d5486c43 6659 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6660 {
eea6121a 6661 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6662 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6663 if (s->contents == NULL)
6664 return FALSE;
6665
6666 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6667 return FALSE;
6668 }
6669
6670 /* Set the size of the .dynsym and .hash sections. We counted
6671 the number of dynamic symbols in elf_link_add_object_symbols.
6672 We will build the contents of .dynsym and .hash when we build
6673 the final symbol table, because until then we do not know the
6674 correct value to give the symbols. We built the .dynstr
6675 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6676 s = elf_hash_table (info)->dynsym;
5a580b3a 6677 BFD_ASSERT (s != NULL);
eea6121a 6678 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6679
d5486c43
L
6680 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6681 if (s->contents == NULL)
6682 return FALSE;
5a580b3a 6683
d5486c43
L
6684 /* The first entry in .dynsym is a dummy symbol. Clear all the
6685 section syms, in case we don't output them all. */
6686 ++section_sym_count;
6687 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6688
fdc90cb4
JJ
6689 elf_hash_table (info)->bucketcount = 0;
6690
5a580b3a
AM
6691 /* Compute the size of the hashing table. As a side effect this
6692 computes the hash values for all the names we export. */
fdc90cb4
JJ
6693 if (info->emit_hash)
6694 {
6695 unsigned long int *hashcodes;
14b1c01e 6696 struct hash_codes_info hashinf;
fdc90cb4
JJ
6697 bfd_size_type amt;
6698 unsigned long int nsyms;
6699 size_t bucketcount;
6700 size_t hash_entry_size;
6701
6702 /* Compute the hash values for all exported symbols. At the same
6703 time store the values in an array so that we could use them for
6704 optimizations. */
6705 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6706 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6707 if (hashcodes == NULL)
6708 return FALSE;
14b1c01e
AM
6709 hashinf.hashcodes = hashcodes;
6710 hashinf.error = FALSE;
5a580b3a 6711
fdc90cb4
JJ
6712 /* Put all hash values in HASHCODES. */
6713 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6714 elf_collect_hash_codes, &hashinf);
6715 if (hashinf.error)
4dd07732
AM
6716 {
6717 free (hashcodes);
6718 return FALSE;
6719 }
5a580b3a 6720
14b1c01e 6721 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6722 bucketcount
6723 = compute_bucket_count (info, hashcodes, nsyms, 0);
6724 free (hashcodes);
6725
6726 if (bucketcount == 0)
6727 return FALSE;
5a580b3a 6728
fdc90cb4
JJ
6729 elf_hash_table (info)->bucketcount = bucketcount;
6730
3d4d4302 6731 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6732 BFD_ASSERT (s != NULL);
6733 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6734 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6735 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6736 if (s->contents == NULL)
6737 return FALSE;
6738
6739 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6740 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6741 s->contents + hash_entry_size);
6742 }
6743
6744 if (info->emit_gnu_hash)
6745 {
6746 size_t i, cnt;
6747 unsigned char *contents;
6748 struct collect_gnu_hash_codes cinfo;
6749 bfd_size_type amt;
6750 size_t bucketcount;
6751
6752 memset (&cinfo, 0, sizeof (cinfo));
6753
6754 /* Compute the hash values for all exported symbols. At the same
6755 time store the values in an array so that we could use them for
6756 optimizations. */
6757 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6758 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6759 if (cinfo.hashcodes == NULL)
6760 return FALSE;
6761
6762 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6763 cinfo.min_dynindx = -1;
6764 cinfo.output_bfd = output_bfd;
6765 cinfo.bed = bed;
6766
6767 /* Put all hash values in HASHCODES. */
6768 elf_link_hash_traverse (elf_hash_table (info),
6769 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6770 if (cinfo.error)
4dd07732
AM
6771 {
6772 free (cinfo.hashcodes);
6773 return FALSE;
6774 }
fdc90cb4
JJ
6775
6776 bucketcount
6777 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6778
6779 if (bucketcount == 0)
6780 {
6781 free (cinfo.hashcodes);
6782 return FALSE;
6783 }
6784
3d4d4302 6785 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6786 BFD_ASSERT (s != NULL);
6787
6788 if (cinfo.nsyms == 0)
6789 {
6790 /* Empty .gnu.hash section is special. */
6791 BFD_ASSERT (cinfo.min_dynindx == -1);
6792 free (cinfo.hashcodes);
6793 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6794 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6795 if (contents == NULL)
6796 return FALSE;
6797 s->contents = contents;
6798 /* 1 empty bucket. */
6799 bfd_put_32 (output_bfd, 1, contents);
6800 /* SYMIDX above the special symbol 0. */
6801 bfd_put_32 (output_bfd, 1, contents + 4);
6802 /* Just one word for bitmask. */
6803 bfd_put_32 (output_bfd, 1, contents + 8);
6804 /* Only hash fn bloom filter. */
6805 bfd_put_32 (output_bfd, 0, contents + 12);
6806 /* No hashes are valid - empty bitmask. */
6807 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6808 /* No hashes in the only bucket. */
6809 bfd_put_32 (output_bfd, 0,
6810 contents + 16 + bed->s->arch_size / 8);
6811 }
6812 else
6813 {
9e6619e2 6814 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6815 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6816
9e6619e2
AM
6817 x = cinfo.nsyms;
6818 maskbitslog2 = 1;
6819 while ((x >>= 1) != 0)
6820 ++maskbitslog2;
fdc90cb4
JJ
6821 if (maskbitslog2 < 3)
6822 maskbitslog2 = 5;
6823 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6824 maskbitslog2 = maskbitslog2 + 3;
6825 else
6826 maskbitslog2 = maskbitslog2 + 2;
6827 if (bed->s->arch_size == 64)
6828 {
6829 if (maskbitslog2 == 5)
6830 maskbitslog2 = 6;
6831 cinfo.shift1 = 6;
6832 }
6833 else
6834 cinfo.shift1 = 5;
6835 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6836 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6837 cinfo.maskbits = 1 << maskbitslog2;
6838 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6839 amt = bucketcount * sizeof (unsigned long int) * 2;
6840 amt += maskwords * sizeof (bfd_vma);
a50b1753 6841 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6842 if (cinfo.bitmask == NULL)
6843 {
6844 free (cinfo.hashcodes);
6845 return FALSE;
6846 }
6847
a50b1753 6848 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6849 cinfo.indx = cinfo.counts + bucketcount;
6850 cinfo.symindx = dynsymcount - cinfo.nsyms;
6851 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6852
6853 /* Determine how often each hash bucket is used. */
6854 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6855 for (i = 0; i < cinfo.nsyms; ++i)
6856 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6857
6858 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6859 if (cinfo.counts[i] != 0)
6860 {
6861 cinfo.indx[i] = cnt;
6862 cnt += cinfo.counts[i];
6863 }
6864 BFD_ASSERT (cnt == dynsymcount);
6865 cinfo.bucketcount = bucketcount;
6866 cinfo.local_indx = cinfo.min_dynindx;
6867
6868 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6869 s->size += cinfo.maskbits / 8;
a50b1753 6870 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6871 if (contents == NULL)
6872 {
6873 free (cinfo.bitmask);
6874 free (cinfo.hashcodes);
6875 return FALSE;
6876 }
6877
6878 s->contents = contents;
6879 bfd_put_32 (output_bfd, bucketcount, contents);
6880 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6881 bfd_put_32 (output_bfd, maskwords, contents + 8);
6882 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6883 contents += 16 + cinfo.maskbits / 8;
6884
6885 for (i = 0; i < bucketcount; ++i)
6886 {
6887 if (cinfo.counts[i] == 0)
6888 bfd_put_32 (output_bfd, 0, contents);
6889 else
6890 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6891 contents += 4;
6892 }
6893
6894 cinfo.contents = contents;
6895
6896 /* Renumber dynamic symbols, populate .gnu.hash section. */
6897 elf_link_hash_traverse (elf_hash_table (info),
6898 elf_renumber_gnu_hash_syms, &cinfo);
6899
6900 contents = s->contents + 16;
6901 for (i = 0; i < maskwords; ++i)
6902 {
6903 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6904 contents);
6905 contents += bed->s->arch_size / 8;
6906 }
6907
6908 free (cinfo.bitmask);
6909 free (cinfo.hashcodes);
6910 }
6911 }
5a580b3a 6912
3d4d4302 6913 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6914 BFD_ASSERT (s != NULL);
6915
4ad4eba5 6916 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6917
eea6121a 6918 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6919
6920 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6921 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6922 return FALSE;
6923 }
6924
6925 return TRUE;
6926}
4d269e42 6927\f
4d269e42
AM
6928/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6929
6930static void
6931merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6932 asection *sec)
6933{
dbaa2011
AM
6934 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6935 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
6936}
6937
6938/* Finish SHF_MERGE section merging. */
6939
6940bfd_boolean
630993ec 6941_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
6942{
6943 bfd *ibfd;
6944 asection *sec;
6945
6946 if (!is_elf_hash_table (info->hash))
6947 return FALSE;
6948
c72f2fb2 6949 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
6950 if ((ibfd->flags & DYNAMIC) == 0
6951 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
6952 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6953 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
6954 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6955 if ((sec->flags & SEC_MERGE) != 0
6956 && !bfd_is_abs_section (sec->output_section))
6957 {
6958 struct bfd_elf_section_data *secdata;
6959
6960 secdata = elf_section_data (sec);
630993ec 6961 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
6962 &elf_hash_table (info)->merge_info,
6963 sec, &secdata->sec_info))
6964 return FALSE;
6965 else if (secdata->sec_info)
dbaa2011 6966 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
6967 }
6968
6969 if (elf_hash_table (info)->merge_info != NULL)
630993ec 6970 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
6971 merge_sections_remove_hook);
6972 return TRUE;
6973}
6974
6975/* Create an entry in an ELF linker hash table. */
6976
6977struct bfd_hash_entry *
6978_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6979 struct bfd_hash_table *table,
6980 const char *string)
6981{
6982 /* Allocate the structure if it has not already been allocated by a
6983 subclass. */
6984 if (entry == NULL)
6985 {
a50b1753 6986 entry = (struct bfd_hash_entry *)
ca4be51c 6987 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6988 if (entry == NULL)
6989 return entry;
6990 }
6991
6992 /* Call the allocation method of the superclass. */
6993 entry = _bfd_link_hash_newfunc (entry, table, string);
6994 if (entry != NULL)
6995 {
6996 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6997 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6998
6999 /* Set local fields. */
7000 ret->indx = -1;
7001 ret->dynindx = -1;
7002 ret->got = htab->init_got_refcount;
7003 ret->plt = htab->init_plt_refcount;
7004 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7005 - offsetof (struct elf_link_hash_entry, size)));
7006 /* Assume that we have been called by a non-ELF symbol reader.
7007 This flag is then reset by the code which reads an ELF input
7008 file. This ensures that a symbol created by a non-ELF symbol
7009 reader will have the flag set correctly. */
7010 ret->non_elf = 1;
7011 }
7012
7013 return entry;
7014}
7015
7016/* Copy data from an indirect symbol to its direct symbol, hiding the
7017 old indirect symbol. Also used for copying flags to a weakdef. */
7018
7019void
7020_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7021 struct elf_link_hash_entry *dir,
7022 struct elf_link_hash_entry *ind)
7023{
7024 struct elf_link_hash_table *htab;
7025
7026 /* Copy down any references that we may have already seen to the
6e33951e
L
7027 symbol which just became indirect if DIR isn't a hidden versioned
7028 symbol. */
4d269e42 7029
422f1182 7030 if (dir->versioned != versioned_hidden)
6e33951e
L
7031 {
7032 dir->ref_dynamic |= ind->ref_dynamic;
7033 dir->ref_regular |= ind->ref_regular;
7034 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7035 dir->non_got_ref |= ind->non_got_ref;
7036 dir->needs_plt |= ind->needs_plt;
7037 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7038 }
4d269e42
AM
7039
7040 if (ind->root.type != bfd_link_hash_indirect)
7041 return;
7042
7043 /* Copy over the global and procedure linkage table refcount entries.
7044 These may have been already set up by a check_relocs routine. */
7045 htab = elf_hash_table (info);
7046 if (ind->got.refcount > htab->init_got_refcount.refcount)
7047 {
7048 if (dir->got.refcount < 0)
7049 dir->got.refcount = 0;
7050 dir->got.refcount += ind->got.refcount;
7051 ind->got.refcount = htab->init_got_refcount.refcount;
7052 }
7053
7054 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7055 {
7056 if (dir->plt.refcount < 0)
7057 dir->plt.refcount = 0;
7058 dir->plt.refcount += ind->plt.refcount;
7059 ind->plt.refcount = htab->init_plt_refcount.refcount;
7060 }
7061
7062 if (ind->dynindx != -1)
7063 {
7064 if (dir->dynindx != -1)
7065 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7066 dir->dynindx = ind->dynindx;
7067 dir->dynstr_index = ind->dynstr_index;
7068 ind->dynindx = -1;
7069 ind->dynstr_index = 0;
7070 }
7071}
7072
7073void
7074_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7075 struct elf_link_hash_entry *h,
7076 bfd_boolean force_local)
7077{
3aa14d16
L
7078 /* STT_GNU_IFUNC symbol must go through PLT. */
7079 if (h->type != STT_GNU_IFUNC)
7080 {
7081 h->plt = elf_hash_table (info)->init_plt_offset;
7082 h->needs_plt = 0;
7083 }
4d269e42
AM
7084 if (force_local)
7085 {
7086 h->forced_local = 1;
7087 if (h->dynindx != -1)
7088 {
7089 h->dynindx = -1;
7090 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7091 h->dynstr_index);
7092 }
7093 }
7094}
7095
7bf52ea2
AM
7096/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7097 caller. */
4d269e42
AM
7098
7099bfd_boolean
7100_bfd_elf_link_hash_table_init
7101 (struct elf_link_hash_table *table,
7102 bfd *abfd,
7103 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7104 struct bfd_hash_table *,
7105 const char *),
4dfe6ac6
NC
7106 unsigned int entsize,
7107 enum elf_target_id target_id)
4d269e42
AM
7108{
7109 bfd_boolean ret;
7110 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7111
4d269e42
AM
7112 table->init_got_refcount.refcount = can_refcount - 1;
7113 table->init_plt_refcount.refcount = can_refcount - 1;
7114 table->init_got_offset.offset = -(bfd_vma) 1;
7115 table->init_plt_offset.offset = -(bfd_vma) 1;
7116 /* The first dynamic symbol is a dummy. */
7117 table->dynsymcount = 1;
7118
7119 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7120
4d269e42 7121 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7122 table->hash_table_id = target_id;
4d269e42
AM
7123
7124 return ret;
7125}
7126
7127/* Create an ELF linker hash table. */
7128
7129struct bfd_link_hash_table *
7130_bfd_elf_link_hash_table_create (bfd *abfd)
7131{
7132 struct elf_link_hash_table *ret;
7133 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7134
7bf52ea2 7135 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7136 if (ret == NULL)
7137 return NULL;
7138
7139 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7140 sizeof (struct elf_link_hash_entry),
7141 GENERIC_ELF_DATA))
4d269e42
AM
7142 {
7143 free (ret);
7144 return NULL;
7145 }
d495ab0d 7146 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7147
7148 return &ret->root;
7149}
7150
9f7c3e5e
AM
7151/* Destroy an ELF linker hash table. */
7152
7153void
d495ab0d 7154_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7155{
d495ab0d
AM
7156 struct elf_link_hash_table *htab;
7157
7158 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7159 if (htab->dynstr != NULL)
7160 _bfd_elf_strtab_free (htab->dynstr);
7161 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7162 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7163}
7164
4d269e42
AM
7165/* This is a hook for the ELF emulation code in the generic linker to
7166 tell the backend linker what file name to use for the DT_NEEDED
7167 entry for a dynamic object. */
7168
7169void
7170bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7171{
7172 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7173 && bfd_get_format (abfd) == bfd_object)
7174 elf_dt_name (abfd) = name;
7175}
7176
7177int
7178bfd_elf_get_dyn_lib_class (bfd *abfd)
7179{
7180 int lib_class;
7181 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7182 && bfd_get_format (abfd) == bfd_object)
7183 lib_class = elf_dyn_lib_class (abfd);
7184 else
7185 lib_class = 0;
7186 return lib_class;
7187}
7188
7189void
7190bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7191{
7192 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7193 && bfd_get_format (abfd) == bfd_object)
7194 elf_dyn_lib_class (abfd) = lib_class;
7195}
7196
7197/* Get the list of DT_NEEDED entries for a link. This is a hook for
7198 the linker ELF emulation code. */
7199
7200struct bfd_link_needed_list *
7201bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7202 struct bfd_link_info *info)
7203{
7204 if (! is_elf_hash_table (info->hash))
7205 return NULL;
7206 return elf_hash_table (info)->needed;
7207}
7208
7209/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7210 hook for the linker ELF emulation code. */
7211
7212struct bfd_link_needed_list *
7213bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7214 struct bfd_link_info *info)
7215{
7216 if (! is_elf_hash_table (info->hash))
7217 return NULL;
7218 return elf_hash_table (info)->runpath;
7219}
7220
7221/* Get the name actually used for a dynamic object for a link. This
7222 is the SONAME entry if there is one. Otherwise, it is the string
7223 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7224
7225const char *
7226bfd_elf_get_dt_soname (bfd *abfd)
7227{
7228 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7229 && bfd_get_format (abfd) == bfd_object)
7230 return elf_dt_name (abfd);
7231 return NULL;
7232}
7233
7234/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7235 the ELF linker emulation code. */
7236
7237bfd_boolean
7238bfd_elf_get_bfd_needed_list (bfd *abfd,
7239 struct bfd_link_needed_list **pneeded)
7240{
7241 asection *s;
7242 bfd_byte *dynbuf = NULL;
cb33740c 7243 unsigned int elfsec;
4d269e42
AM
7244 unsigned long shlink;
7245 bfd_byte *extdyn, *extdynend;
7246 size_t extdynsize;
7247 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7248
7249 *pneeded = NULL;
7250
7251 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7252 || bfd_get_format (abfd) != bfd_object)
7253 return TRUE;
7254
7255 s = bfd_get_section_by_name (abfd, ".dynamic");
7256 if (s == NULL || s->size == 0)
7257 return TRUE;
7258
7259 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7260 goto error_return;
7261
7262 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7263 if (elfsec == SHN_BAD)
4d269e42
AM
7264 goto error_return;
7265
7266 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7267
4d269e42
AM
7268 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7269 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7270
7271 extdyn = dynbuf;
7272 extdynend = extdyn + s->size;
7273 for (; extdyn < extdynend; extdyn += extdynsize)
7274 {
7275 Elf_Internal_Dyn dyn;
7276
7277 (*swap_dyn_in) (abfd, extdyn, &dyn);
7278
7279 if (dyn.d_tag == DT_NULL)
7280 break;
7281
7282 if (dyn.d_tag == DT_NEEDED)
7283 {
7284 const char *string;
7285 struct bfd_link_needed_list *l;
7286 unsigned int tagv = dyn.d_un.d_val;
7287 bfd_size_type amt;
7288
7289 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7290 if (string == NULL)
7291 goto error_return;
7292
7293 amt = sizeof *l;
a50b1753 7294 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7295 if (l == NULL)
7296 goto error_return;
7297
7298 l->by = abfd;
7299 l->name = string;
7300 l->next = *pneeded;
7301 *pneeded = l;
7302 }
7303 }
7304
7305 free (dynbuf);
7306
7307 return TRUE;
7308
7309 error_return:
7310 if (dynbuf != NULL)
7311 free (dynbuf);
7312 return FALSE;
7313}
7314
7315struct elf_symbuf_symbol
7316{
7317 unsigned long st_name; /* Symbol name, index in string tbl */
7318 unsigned char st_info; /* Type and binding attributes */
7319 unsigned char st_other; /* Visibilty, and target specific */
7320};
7321
7322struct elf_symbuf_head
7323{
7324 struct elf_symbuf_symbol *ssym;
ef53be89 7325 size_t count;
4d269e42
AM
7326 unsigned int st_shndx;
7327};
7328
7329struct elf_symbol
7330{
7331 union
7332 {
7333 Elf_Internal_Sym *isym;
7334 struct elf_symbuf_symbol *ssym;
7335 } u;
7336 const char *name;
7337};
7338
7339/* Sort references to symbols by ascending section number. */
7340
7341static int
7342elf_sort_elf_symbol (const void *arg1, const void *arg2)
7343{
7344 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7345 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7346
7347 return s1->st_shndx - s2->st_shndx;
7348}
7349
7350static int
7351elf_sym_name_compare (const void *arg1, const void *arg2)
7352{
7353 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7354 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7355 return strcmp (s1->name, s2->name);
7356}
7357
7358static struct elf_symbuf_head *
ef53be89 7359elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7360{
14b1c01e 7361 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7362 struct elf_symbuf_symbol *ssym;
7363 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7364 size_t i, shndx_count, total_size;
4d269e42 7365
a50b1753 7366 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7367 if (indbuf == NULL)
7368 return NULL;
7369
7370 for (ind = indbuf, i = 0; i < symcount; i++)
7371 if (isymbuf[i].st_shndx != SHN_UNDEF)
7372 *ind++ = &isymbuf[i];
7373 indbufend = ind;
7374
7375 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7376 elf_sort_elf_symbol);
7377
7378 shndx_count = 0;
7379 if (indbufend > indbuf)
7380 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7381 if (ind[0]->st_shndx != ind[1]->st_shndx)
7382 shndx_count++;
7383
3ae181ee
L
7384 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7385 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7386 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7387 if (ssymbuf == NULL)
7388 {
7389 free (indbuf);
7390 return NULL;
7391 }
7392
3ae181ee 7393 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7394 ssymbuf->ssym = NULL;
7395 ssymbuf->count = shndx_count;
7396 ssymbuf->st_shndx = 0;
7397 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7398 {
7399 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7400 {
7401 ssymhead++;
7402 ssymhead->ssym = ssym;
7403 ssymhead->count = 0;
7404 ssymhead->st_shndx = (*ind)->st_shndx;
7405 }
7406 ssym->st_name = (*ind)->st_name;
7407 ssym->st_info = (*ind)->st_info;
7408 ssym->st_other = (*ind)->st_other;
7409 ssymhead->count++;
7410 }
ef53be89 7411 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7412 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7413 == total_size));
4d269e42
AM
7414
7415 free (indbuf);
7416 return ssymbuf;
7417}
7418
7419/* Check if 2 sections define the same set of local and global
7420 symbols. */
7421
8f317e31 7422static bfd_boolean
4d269e42
AM
7423bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7424 struct bfd_link_info *info)
7425{
7426 bfd *bfd1, *bfd2;
7427 const struct elf_backend_data *bed1, *bed2;
7428 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7429 size_t symcount1, symcount2;
4d269e42
AM
7430 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7431 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7432 Elf_Internal_Sym *isym, *isymend;
7433 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7434 size_t count1, count2, i;
cb33740c 7435 unsigned int shndx1, shndx2;
4d269e42
AM
7436 bfd_boolean result;
7437
7438 bfd1 = sec1->owner;
7439 bfd2 = sec2->owner;
7440
4d269e42
AM
7441 /* Both sections have to be in ELF. */
7442 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7443 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7444 return FALSE;
7445
7446 if (elf_section_type (sec1) != elf_section_type (sec2))
7447 return FALSE;
7448
4d269e42
AM
7449 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7450 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7451 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7452 return FALSE;
7453
7454 bed1 = get_elf_backend_data (bfd1);
7455 bed2 = get_elf_backend_data (bfd2);
7456 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7457 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7458 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7459 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7460
7461 if (symcount1 == 0 || symcount2 == 0)
7462 return FALSE;
7463
7464 result = FALSE;
7465 isymbuf1 = NULL;
7466 isymbuf2 = NULL;
a50b1753
NC
7467 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7468 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7469
7470 if (ssymbuf1 == NULL)
7471 {
7472 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7473 NULL, NULL, NULL);
7474 if (isymbuf1 == NULL)
7475 goto done;
7476
7477 if (!info->reduce_memory_overheads)
7478 elf_tdata (bfd1)->symbuf = ssymbuf1
7479 = elf_create_symbuf (symcount1, isymbuf1);
7480 }
7481
7482 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7483 {
7484 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7485 NULL, NULL, NULL);
7486 if (isymbuf2 == NULL)
7487 goto done;
7488
7489 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7490 elf_tdata (bfd2)->symbuf = ssymbuf2
7491 = elf_create_symbuf (symcount2, isymbuf2);
7492 }
7493
7494 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7495 {
7496 /* Optimized faster version. */
ef53be89 7497 size_t lo, hi, mid;
4d269e42
AM
7498 struct elf_symbol *symp;
7499 struct elf_symbuf_symbol *ssym, *ssymend;
7500
7501 lo = 0;
7502 hi = ssymbuf1->count;
7503 ssymbuf1++;
7504 count1 = 0;
7505 while (lo < hi)
7506 {
7507 mid = (lo + hi) / 2;
cb33740c 7508 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7509 hi = mid;
cb33740c 7510 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7511 lo = mid + 1;
7512 else
7513 {
7514 count1 = ssymbuf1[mid].count;
7515 ssymbuf1 += mid;
7516 break;
7517 }
7518 }
7519
7520 lo = 0;
7521 hi = ssymbuf2->count;
7522 ssymbuf2++;
7523 count2 = 0;
7524 while (lo < hi)
7525 {
7526 mid = (lo + hi) / 2;
cb33740c 7527 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7528 hi = mid;
cb33740c 7529 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7530 lo = mid + 1;
7531 else
7532 {
7533 count2 = ssymbuf2[mid].count;
7534 ssymbuf2 += mid;
7535 break;
7536 }
7537 }
7538
7539 if (count1 == 0 || count2 == 0 || count1 != count2)
7540 goto done;
7541
ca4be51c
AM
7542 symtable1
7543 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7544 symtable2
7545 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7546 if (symtable1 == NULL || symtable2 == NULL)
7547 goto done;
7548
7549 symp = symtable1;
7550 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7551 ssym < ssymend; ssym++, symp++)
7552 {
7553 symp->u.ssym = ssym;
7554 symp->name = bfd_elf_string_from_elf_section (bfd1,
7555 hdr1->sh_link,
7556 ssym->st_name);
7557 }
7558
7559 symp = symtable2;
7560 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7561 ssym < ssymend; ssym++, symp++)
7562 {
7563 symp->u.ssym = ssym;
7564 symp->name = bfd_elf_string_from_elf_section (bfd2,
7565 hdr2->sh_link,
7566 ssym->st_name);
7567 }
7568
7569 /* Sort symbol by name. */
7570 qsort (symtable1, count1, sizeof (struct elf_symbol),
7571 elf_sym_name_compare);
7572 qsort (symtable2, count1, sizeof (struct elf_symbol),
7573 elf_sym_name_compare);
7574
7575 for (i = 0; i < count1; i++)
7576 /* Two symbols must have the same binding, type and name. */
7577 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7578 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7579 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7580 goto done;
7581
7582 result = TRUE;
7583 goto done;
7584 }
7585
a50b1753
NC
7586 symtable1 = (struct elf_symbol *)
7587 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7588 symtable2 = (struct elf_symbol *)
7589 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7590 if (symtable1 == NULL || symtable2 == NULL)
7591 goto done;
7592
7593 /* Count definitions in the section. */
7594 count1 = 0;
7595 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7596 if (isym->st_shndx == shndx1)
4d269e42
AM
7597 symtable1[count1++].u.isym = isym;
7598
7599 count2 = 0;
7600 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7601 if (isym->st_shndx == shndx2)
4d269e42
AM
7602 symtable2[count2++].u.isym = isym;
7603
7604 if (count1 == 0 || count2 == 0 || count1 != count2)
7605 goto done;
7606
7607 for (i = 0; i < count1; i++)
7608 symtable1[i].name
7609 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7610 symtable1[i].u.isym->st_name);
7611
7612 for (i = 0; i < count2; i++)
7613 symtable2[i].name
7614 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7615 symtable2[i].u.isym->st_name);
7616
7617 /* Sort symbol by name. */
7618 qsort (symtable1, count1, sizeof (struct elf_symbol),
7619 elf_sym_name_compare);
7620 qsort (symtable2, count1, sizeof (struct elf_symbol),
7621 elf_sym_name_compare);
7622
7623 for (i = 0; i < count1; i++)
7624 /* Two symbols must have the same binding, type and name. */
7625 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7626 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7627 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7628 goto done;
7629
7630 result = TRUE;
7631
7632done:
7633 if (symtable1)
7634 free (symtable1);
7635 if (symtable2)
7636 free (symtable2);
7637 if (isymbuf1)
7638 free (isymbuf1);
7639 if (isymbuf2)
7640 free (isymbuf2);
7641
7642 return result;
7643}
7644
7645/* Return TRUE if 2 section types are compatible. */
7646
7647bfd_boolean
7648_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7649 bfd *bbfd, const asection *bsec)
7650{
7651 if (asec == NULL
7652 || bsec == NULL
7653 || abfd->xvec->flavour != bfd_target_elf_flavour
7654 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7655 return TRUE;
7656
7657 return elf_section_type (asec) == elf_section_type (bsec);
7658}
7659\f
c152c796
AM
7660/* Final phase of ELF linker. */
7661
7662/* A structure we use to avoid passing large numbers of arguments. */
7663
7664struct elf_final_link_info
7665{
7666 /* General link information. */
7667 struct bfd_link_info *info;
7668 /* Output BFD. */
7669 bfd *output_bfd;
7670 /* Symbol string table. */
ef10c3ac 7671 struct elf_strtab_hash *symstrtab;
c152c796
AM
7672 /* .hash section. */
7673 asection *hash_sec;
7674 /* symbol version section (.gnu.version). */
7675 asection *symver_sec;
7676 /* Buffer large enough to hold contents of any section. */
7677 bfd_byte *contents;
7678 /* Buffer large enough to hold external relocs of any section. */
7679 void *external_relocs;
7680 /* Buffer large enough to hold internal relocs of any section. */
7681 Elf_Internal_Rela *internal_relocs;
7682 /* Buffer large enough to hold external local symbols of any input
7683 BFD. */
7684 bfd_byte *external_syms;
7685 /* And a buffer for symbol section indices. */
7686 Elf_External_Sym_Shndx *locsym_shndx;
7687 /* Buffer large enough to hold internal local symbols of any input
7688 BFD. */
7689 Elf_Internal_Sym *internal_syms;
7690 /* Array large enough to hold a symbol index for each local symbol
7691 of any input BFD. */
7692 long *indices;
7693 /* Array large enough to hold a section pointer for each local
7694 symbol of any input BFD. */
7695 asection **sections;
ef10c3ac 7696 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7697 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7698 /* Number of STT_FILE syms seen. */
7699 size_t filesym_count;
c152c796
AM
7700};
7701
7702/* This struct is used to pass information to elf_link_output_extsym. */
7703
7704struct elf_outext_info
7705{
7706 bfd_boolean failed;
7707 bfd_boolean localsyms;
34a79995 7708 bfd_boolean file_sym_done;
8b127cbc 7709 struct elf_final_link_info *flinfo;
c152c796
AM
7710};
7711
d9352518
DB
7712
7713/* Support for evaluating a complex relocation.
7714
7715 Complex relocations are generalized, self-describing relocations. The
7716 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7717 relocations themselves.
d9352518
DB
7718
7719 The relocations are use a reserved elf-wide relocation type code (R_RELC
7720 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7721 information (start bit, end bit, word width, etc) into the addend. This
7722 information is extracted from CGEN-generated operand tables within gas.
7723
7724 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7725 internal) representing prefix-notation expressions, including but not
7726 limited to those sorts of expressions normally encoded as addends in the
7727 addend field. The symbol mangling format is:
7728
7729 <node> := <literal>
7730 | <unary-operator> ':' <node>
7731 | <binary-operator> ':' <node> ':' <node>
7732 ;
7733
7734 <literal> := 's' <digits=N> ':' <N character symbol name>
7735 | 'S' <digits=N> ':' <N character section name>
7736 | '#' <hexdigits>
7737 ;
7738
7739 <binary-operator> := as in C
7740 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7741
7742static void
a0c8462f
AM
7743set_symbol_value (bfd *bfd_with_globals,
7744 Elf_Internal_Sym *isymbuf,
7745 size_t locsymcount,
7746 size_t symidx,
7747 bfd_vma val)
d9352518 7748{
8977835c
AM
7749 struct elf_link_hash_entry **sym_hashes;
7750 struct elf_link_hash_entry *h;
7751 size_t extsymoff = locsymcount;
d9352518 7752
8977835c 7753 if (symidx < locsymcount)
d9352518 7754 {
8977835c
AM
7755 Elf_Internal_Sym *sym;
7756
7757 sym = isymbuf + symidx;
7758 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7759 {
7760 /* It is a local symbol: move it to the
7761 "absolute" section and give it a value. */
7762 sym->st_shndx = SHN_ABS;
7763 sym->st_value = val;
7764 return;
7765 }
7766 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7767 extsymoff = 0;
d9352518 7768 }
8977835c
AM
7769
7770 /* It is a global symbol: set its link type
7771 to "defined" and give it a value. */
7772
7773 sym_hashes = elf_sym_hashes (bfd_with_globals);
7774 h = sym_hashes [symidx - extsymoff];
7775 while (h->root.type == bfd_link_hash_indirect
7776 || h->root.type == bfd_link_hash_warning)
7777 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7778 h->root.type = bfd_link_hash_defined;
7779 h->root.u.def.value = val;
7780 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7781}
7782
a0c8462f
AM
7783static bfd_boolean
7784resolve_symbol (const char *name,
7785 bfd *input_bfd,
8b127cbc 7786 struct elf_final_link_info *flinfo,
a0c8462f
AM
7787 bfd_vma *result,
7788 Elf_Internal_Sym *isymbuf,
7789 size_t locsymcount)
d9352518 7790{
a0c8462f
AM
7791 Elf_Internal_Sym *sym;
7792 struct bfd_link_hash_entry *global_entry;
7793 const char *candidate = NULL;
7794 Elf_Internal_Shdr *symtab_hdr;
7795 size_t i;
7796
d9352518
DB
7797 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7798
7799 for (i = 0; i < locsymcount; ++ i)
7800 {
8977835c 7801 sym = isymbuf + i;
d9352518
DB
7802
7803 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7804 continue;
7805
7806 candidate = bfd_elf_string_from_elf_section (input_bfd,
7807 symtab_hdr->sh_link,
7808 sym->st_name);
7809#ifdef DEBUG
0f02bbd9
AM
7810 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7811 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7812#endif
7813 if (candidate && strcmp (candidate, name) == 0)
7814 {
8b127cbc 7815 asection *sec = flinfo->sections [i];
d9352518 7816
0f02bbd9
AM
7817 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7818 *result += sec->output_offset + sec->output_section->vma;
d9352518 7819#ifdef DEBUG
0f02bbd9
AM
7820 printf ("Found symbol with value %8.8lx\n",
7821 (unsigned long) *result);
d9352518
DB
7822#endif
7823 return TRUE;
7824 }
7825 }
7826
7827 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7828 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7829 FALSE, FALSE, TRUE);
d9352518
DB
7830 if (!global_entry)
7831 return FALSE;
a0c8462f 7832
d9352518
DB
7833 if (global_entry->type == bfd_link_hash_defined
7834 || global_entry->type == bfd_link_hash_defweak)
7835 {
a0c8462f
AM
7836 *result = (global_entry->u.def.value
7837 + global_entry->u.def.section->output_section->vma
7838 + global_entry->u.def.section->output_offset);
d9352518 7839#ifdef DEBUG
0f02bbd9
AM
7840 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7841 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7842#endif
7843 return TRUE;
a0c8462f 7844 }
d9352518 7845
d9352518
DB
7846 return FALSE;
7847}
7848
37b01f6a
DG
7849/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7850 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7851 names like "foo.end" which is the end address of section "foo". */
7852
d9352518 7853static bfd_boolean
a0c8462f
AM
7854resolve_section (const char *name,
7855 asection *sections,
37b01f6a
DG
7856 bfd_vma *result,
7857 bfd * abfd)
d9352518 7858{
a0c8462f
AM
7859 asection *curr;
7860 unsigned int len;
d9352518 7861
a0c8462f 7862 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7863 if (strcmp (curr->name, name) == 0)
7864 {
7865 *result = curr->vma;
7866 return TRUE;
7867 }
7868
7869 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 7870 /* FIXME: This could be coded more efficiently... */
a0c8462f 7871 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7872 {
7873 len = strlen (curr->name);
a0c8462f 7874 if (len > strlen (name))
d9352518
DB
7875 continue;
7876
7877 if (strncmp (curr->name, name, len) == 0)
7878 {
7879 if (strncmp (".end", name + len, 4) == 0)
7880 {
37b01f6a 7881 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
7882 return TRUE;
7883 }
7884
7885 /* Insert more pseudo-section names here, if you like. */
7886 }
7887 }
a0c8462f 7888
d9352518
DB
7889 return FALSE;
7890}
7891
7892static void
a0c8462f 7893undefined_reference (const char *reftype, const char *name)
d9352518 7894{
a0c8462f
AM
7895 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7896 reftype, name);
d9352518
DB
7897}
7898
7899static bfd_boolean
a0c8462f
AM
7900eval_symbol (bfd_vma *result,
7901 const char **symp,
7902 bfd *input_bfd,
8b127cbc 7903 struct elf_final_link_info *flinfo,
a0c8462f
AM
7904 bfd_vma dot,
7905 Elf_Internal_Sym *isymbuf,
7906 size_t locsymcount,
7907 int signed_p)
d9352518 7908{
4b93929b
NC
7909 size_t len;
7910 size_t symlen;
a0c8462f
AM
7911 bfd_vma a;
7912 bfd_vma b;
4b93929b 7913 char symbuf[4096];
0f02bbd9 7914 const char *sym = *symp;
a0c8462f
AM
7915 const char *symend;
7916 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7917
7918 len = strlen (sym);
7919 symend = sym + len;
7920
4b93929b 7921 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7922 {
7923 bfd_set_error (bfd_error_invalid_operation);
7924 return FALSE;
7925 }
a0c8462f 7926
d9352518
DB
7927 switch (* sym)
7928 {
7929 case '.':
0f02bbd9
AM
7930 *result = dot;
7931 *symp = sym + 1;
d9352518
DB
7932 return TRUE;
7933
7934 case '#':
0f02bbd9
AM
7935 ++sym;
7936 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7937 return TRUE;
7938
7939 case 'S':
7940 symbol_is_section = TRUE;
a0c8462f 7941 case 's':
0f02bbd9
AM
7942 ++sym;
7943 symlen = strtol (sym, (char **) symp, 10);
7944 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7945
4b93929b 7946 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7947 {
7948 bfd_set_error (bfd_error_invalid_operation);
7949 return FALSE;
7950 }
7951
7952 memcpy (symbuf, sym, symlen);
a0c8462f 7953 symbuf[symlen] = '\0';
0f02bbd9 7954 *symp = sym + symlen;
a0c8462f
AM
7955
7956 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7957 the symbol as a section, or vice-versa. so we're pretty liberal in our
7958 interpretation here; section means "try section first", not "must be a
7959 section", and likewise with symbol. */
7960
a0c8462f 7961 if (symbol_is_section)
d9352518 7962 {
37b01f6a 7963 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 7964 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7965 isymbuf, locsymcount))
d9352518
DB
7966 {
7967 undefined_reference ("section", symbuf);
7968 return FALSE;
7969 }
a0c8462f
AM
7970 }
7971 else
d9352518 7972 {
8b127cbc 7973 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7974 isymbuf, locsymcount)
8b127cbc 7975 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 7976 result, input_bfd))
d9352518
DB
7977 {
7978 undefined_reference ("symbol", symbuf);
7979 return FALSE;
7980 }
7981 }
7982
7983 return TRUE;
a0c8462f 7984
d9352518
DB
7985 /* All that remains are operators. */
7986
7987#define UNARY_OP(op) \
7988 if (strncmp (sym, #op, strlen (#op)) == 0) \
7989 { \
7990 sym += strlen (#op); \
a0c8462f
AM
7991 if (*sym == ':') \
7992 ++sym; \
0f02bbd9 7993 *symp = sym; \
8b127cbc 7994 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7995 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7996 return FALSE; \
7997 if (signed_p) \
0f02bbd9 7998 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7999 else \
8000 *result = op a; \
d9352518
DB
8001 return TRUE; \
8002 }
8003
8004#define BINARY_OP(op) \
8005 if (strncmp (sym, #op, strlen (#op)) == 0) \
8006 { \
8007 sym += strlen (#op); \
a0c8462f
AM
8008 if (*sym == ':') \
8009 ++sym; \
0f02bbd9 8010 *symp = sym; \
8b127cbc 8011 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8012 isymbuf, locsymcount, signed_p)) \
a0c8462f 8013 return FALSE; \
0f02bbd9 8014 ++*symp; \
8b127cbc 8015 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8016 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8017 return FALSE; \
8018 if (signed_p) \
0f02bbd9 8019 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8020 else \
8021 *result = a op b; \
d9352518
DB
8022 return TRUE; \
8023 }
8024
8025 default:
8026 UNARY_OP (0-);
8027 BINARY_OP (<<);
8028 BINARY_OP (>>);
8029 BINARY_OP (==);
8030 BINARY_OP (!=);
8031 BINARY_OP (<=);
8032 BINARY_OP (>=);
8033 BINARY_OP (&&);
8034 BINARY_OP (||);
8035 UNARY_OP (~);
8036 UNARY_OP (!);
8037 BINARY_OP (*);
8038 BINARY_OP (/);
8039 BINARY_OP (%);
8040 BINARY_OP (^);
8041 BINARY_OP (|);
8042 BINARY_OP (&);
8043 BINARY_OP (+);
8044 BINARY_OP (-);
8045 BINARY_OP (<);
8046 BINARY_OP (>);
8047#undef UNARY_OP
8048#undef BINARY_OP
8049 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8050 bfd_set_error (bfd_error_invalid_operation);
8051 return FALSE;
8052 }
8053}
8054
d9352518 8055static void
a0c8462f
AM
8056put_value (bfd_vma size,
8057 unsigned long chunksz,
8058 bfd *input_bfd,
8059 bfd_vma x,
8060 bfd_byte *location)
d9352518
DB
8061{
8062 location += (size - chunksz);
8063
41cd1ad1 8064 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8065 {
8066 switch (chunksz)
8067 {
d9352518
DB
8068 case 1:
8069 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8070 x >>= 8;
d9352518
DB
8071 break;
8072 case 2:
8073 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8074 x >>= 16;
d9352518
DB
8075 break;
8076 case 4:
8077 bfd_put_32 (input_bfd, x, location);
65164438
NC
8078 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8079 x >>= 16;
8080 x >>= 16;
d9352518 8081 break;
d9352518 8082#ifdef BFD64
41cd1ad1 8083 case 8:
d9352518 8084 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8085 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8086 x >>= 32;
8087 x >>= 32;
8088 break;
d9352518 8089#endif
41cd1ad1
NC
8090 default:
8091 abort ();
d9352518
DB
8092 break;
8093 }
8094 }
8095}
8096
a0c8462f
AM
8097static bfd_vma
8098get_value (bfd_vma size,
8099 unsigned long chunksz,
8100 bfd *input_bfd,
8101 bfd_byte *location)
d9352518 8102{
9b239e0e 8103 int shift;
d9352518
DB
8104 bfd_vma x = 0;
8105
9b239e0e
NC
8106 /* Sanity checks. */
8107 BFD_ASSERT (chunksz <= sizeof (x)
8108 && size >= chunksz
8109 && chunksz != 0
8110 && (size % chunksz) == 0
8111 && input_bfd != NULL
8112 && location != NULL);
8113
8114 if (chunksz == sizeof (x))
8115 {
8116 BFD_ASSERT (size == chunksz);
8117
8118 /* Make sure that we do not perform an undefined shift operation.
8119 We know that size == chunksz so there will only be one iteration
8120 of the loop below. */
8121 shift = 0;
8122 }
8123 else
8124 shift = 8 * chunksz;
8125
a0c8462f 8126 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8127 {
8128 switch (chunksz)
8129 {
d9352518 8130 case 1:
9b239e0e 8131 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8132 break;
8133 case 2:
9b239e0e 8134 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8135 break;
8136 case 4:
9b239e0e 8137 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8138 break;
d9352518 8139#ifdef BFD64
9b239e0e
NC
8140 case 8:
8141 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8142 break;
9b239e0e
NC
8143#endif
8144 default:
8145 abort ();
d9352518
DB
8146 }
8147 }
8148 return x;
8149}
8150
a0c8462f
AM
8151static void
8152decode_complex_addend (unsigned long *start, /* in bits */
8153 unsigned long *oplen, /* in bits */
8154 unsigned long *len, /* in bits */
8155 unsigned long *wordsz, /* in bytes */
8156 unsigned long *chunksz, /* in bytes */
8157 unsigned long *lsb0_p,
8158 unsigned long *signed_p,
8159 unsigned long *trunc_p,
8160 unsigned long encoded)
d9352518
DB
8161{
8162 * start = encoded & 0x3F;
8163 * len = (encoded >> 6) & 0x3F;
8164 * oplen = (encoded >> 12) & 0x3F;
8165 * wordsz = (encoded >> 18) & 0xF;
8166 * chunksz = (encoded >> 22) & 0xF;
8167 * lsb0_p = (encoded >> 27) & 1;
8168 * signed_p = (encoded >> 28) & 1;
8169 * trunc_p = (encoded >> 29) & 1;
8170}
8171
cdfeee4f 8172bfd_reloc_status_type
0f02bbd9 8173bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8174 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8175 bfd_byte *contents,
8176 Elf_Internal_Rela *rel,
8177 bfd_vma relocation)
d9352518 8178{
0f02bbd9
AM
8179 bfd_vma shift, x, mask;
8180 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8181 bfd_reloc_status_type r;
d9352518
DB
8182
8183 /* Perform this reloc, since it is complex.
8184 (this is not to say that it necessarily refers to a complex
8185 symbol; merely that it is a self-describing CGEN based reloc.
8186 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8187 word size, etc) encoded within it.). */
d9352518 8188
a0c8462f
AM
8189 decode_complex_addend (&start, &oplen, &len, &wordsz,
8190 &chunksz, &lsb0_p, &signed_p,
8191 &trunc_p, rel->r_addend);
d9352518
DB
8192
8193 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8194
8195 if (lsb0_p)
8196 shift = (start + 1) - len;
8197 else
8198 shift = (8 * wordsz) - (start + len);
8199
37b01f6a
DG
8200 x = get_value (wordsz, chunksz, input_bfd,
8201 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8202
8203#ifdef DEBUG
8204 printf ("Doing complex reloc: "
8205 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8206 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8207 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8208 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8209 oplen, (unsigned long) x, (unsigned long) mask,
8210 (unsigned long) relocation);
d9352518
DB
8211#endif
8212
cdfeee4f 8213 r = bfd_reloc_ok;
d9352518 8214 if (! trunc_p)
cdfeee4f
AM
8215 /* Now do an overflow check. */
8216 r = bfd_check_overflow ((signed_p
8217 ? complain_overflow_signed
8218 : complain_overflow_unsigned),
8219 len, 0, (8 * wordsz),
8220 relocation);
a0c8462f 8221
d9352518
DB
8222 /* Do the deed. */
8223 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8224
8225#ifdef DEBUG
8226 printf (" relocation: %8.8lx\n"
8227 " shifted mask: %8.8lx\n"
8228 " shifted/masked reloc: %8.8lx\n"
8229 " result: %8.8lx\n",
9ccb8af9
AM
8230 (unsigned long) relocation, (unsigned long) (mask << shift),
8231 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8232#endif
37b01f6a
DG
8233 put_value (wordsz, chunksz, input_bfd, x,
8234 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8235 return r;
d9352518
DB
8236}
8237
0e287786
AM
8238/* Functions to read r_offset from external (target order) reloc
8239 entry. Faster than bfd_getl32 et al, because we let the compiler
8240 know the value is aligned. */
53df40a4 8241
0e287786
AM
8242static bfd_vma
8243ext32l_r_offset (const void *p)
53df40a4
AM
8244{
8245 union aligned32
8246 {
8247 uint32_t v;
8248 unsigned char c[4];
8249 };
8250 const union aligned32 *a
0e287786 8251 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8252
8253 uint32_t aval = ( (uint32_t) a->c[0]
8254 | (uint32_t) a->c[1] << 8
8255 | (uint32_t) a->c[2] << 16
8256 | (uint32_t) a->c[3] << 24);
0e287786 8257 return aval;
53df40a4
AM
8258}
8259
0e287786
AM
8260static bfd_vma
8261ext32b_r_offset (const void *p)
53df40a4
AM
8262{
8263 union aligned32
8264 {
8265 uint32_t v;
8266 unsigned char c[4];
8267 };
8268 const union aligned32 *a
0e287786 8269 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8270
8271 uint32_t aval = ( (uint32_t) a->c[0] << 24
8272 | (uint32_t) a->c[1] << 16
8273 | (uint32_t) a->c[2] << 8
8274 | (uint32_t) a->c[3]);
0e287786 8275 return aval;
53df40a4
AM
8276}
8277
8278#ifdef BFD_HOST_64_BIT
0e287786
AM
8279static bfd_vma
8280ext64l_r_offset (const void *p)
53df40a4
AM
8281{
8282 union aligned64
8283 {
8284 uint64_t v;
8285 unsigned char c[8];
8286 };
8287 const union aligned64 *a
0e287786 8288 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8289
8290 uint64_t aval = ( (uint64_t) a->c[0]
8291 | (uint64_t) a->c[1] << 8
8292 | (uint64_t) a->c[2] << 16
8293 | (uint64_t) a->c[3] << 24
8294 | (uint64_t) a->c[4] << 32
8295 | (uint64_t) a->c[5] << 40
8296 | (uint64_t) a->c[6] << 48
8297 | (uint64_t) a->c[7] << 56);
0e287786 8298 return aval;
53df40a4
AM
8299}
8300
0e287786
AM
8301static bfd_vma
8302ext64b_r_offset (const void *p)
53df40a4
AM
8303{
8304 union aligned64
8305 {
8306 uint64_t v;
8307 unsigned char c[8];
8308 };
8309 const union aligned64 *a
0e287786 8310 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8311
8312 uint64_t aval = ( (uint64_t) a->c[0] << 56
8313 | (uint64_t) a->c[1] << 48
8314 | (uint64_t) a->c[2] << 40
8315 | (uint64_t) a->c[3] << 32
8316 | (uint64_t) a->c[4] << 24
8317 | (uint64_t) a->c[5] << 16
8318 | (uint64_t) a->c[6] << 8
8319 | (uint64_t) a->c[7]);
0e287786 8320 return aval;
53df40a4
AM
8321}
8322#endif
8323
c152c796
AM
8324/* When performing a relocatable link, the input relocations are
8325 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8326 referenced must be updated. Update all the relocations found in
8327 RELDATA. */
c152c796 8328
bca6d0e3 8329static bfd_boolean
c152c796 8330elf_link_adjust_relocs (bfd *abfd,
28dbcedc
AM
8331 struct bfd_elf_section_reloc_data *reldata,
8332 bfd_boolean sort)
c152c796
AM
8333{
8334 unsigned int i;
8335 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8336 bfd_byte *erela;
8337 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8338 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8339 bfd_vma r_type_mask;
8340 int r_sym_shift;
d4730f92
BS
8341 unsigned int count = reldata->count;
8342 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8343
d4730f92 8344 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8345 {
8346 swap_in = bed->s->swap_reloc_in;
8347 swap_out = bed->s->swap_reloc_out;
8348 }
d4730f92 8349 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8350 {
8351 swap_in = bed->s->swap_reloca_in;
8352 swap_out = bed->s->swap_reloca_out;
8353 }
8354 else
8355 abort ();
8356
8357 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8358 abort ();
8359
8360 if (bed->s->arch_size == 32)
8361 {
8362 r_type_mask = 0xff;
8363 r_sym_shift = 8;
8364 }
8365 else
8366 {
8367 r_type_mask = 0xffffffff;
8368 r_sym_shift = 32;
8369 }
8370
d4730f92
BS
8371 erela = reldata->hdr->contents;
8372 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8373 {
8374 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8375 unsigned int j;
8376
8377 if (*rel_hash == NULL)
8378 continue;
8379
8380 BFD_ASSERT ((*rel_hash)->indx >= 0);
8381
8382 (*swap_in) (abfd, erela, irela);
8383 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8384 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8385 | (irela[j].r_info & r_type_mask));
8386 (*swap_out) (abfd, irela, erela);
8387 }
53df40a4 8388
0e287786 8389 if (sort && count != 0)
53df40a4 8390 {
0e287786
AM
8391 bfd_vma (*ext_r_off) (const void *);
8392 bfd_vma r_off;
8393 size_t elt_size;
8394 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8395 bfd_byte *buf = NULL;
28dbcedc
AM
8396
8397 if (bed->s->arch_size == 32)
8398 {
8399 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8400 ext_r_off = ext32l_r_offset;
28dbcedc 8401 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8402 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8403 else
8404 abort ();
8405 }
53df40a4 8406 else
28dbcedc 8407 {
53df40a4 8408#ifdef BFD_HOST_64_BIT
28dbcedc 8409 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8410 ext_r_off = ext64l_r_offset;
28dbcedc 8411 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8412 ext_r_off = ext64b_r_offset;
28dbcedc 8413 else
53df40a4 8414#endif
28dbcedc
AM
8415 abort ();
8416 }
0e287786 8417
bca6d0e3
AM
8418 /* Must use a stable sort here. A modified insertion sort,
8419 since the relocs are mostly sorted already. */
0e287786
AM
8420 elt_size = reldata->hdr->sh_entsize;
8421 base = reldata->hdr->contents;
8422 end = base + count * elt_size;
bca6d0e3 8423 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8424 abort ();
8425
8426 /* Ensure the first element is lowest. This acts as a sentinel,
8427 speeding the main loop below. */
8428 r_off = (*ext_r_off) (base);
8429 for (p = loc = base; (p += elt_size) < end; )
8430 {
8431 bfd_vma r_off2 = (*ext_r_off) (p);
8432 if (r_off > r_off2)
8433 {
8434 r_off = r_off2;
8435 loc = p;
8436 }
8437 }
8438 if (loc != base)
8439 {
8440 /* Don't just swap *base and *loc as that changes the order
8441 of the original base[0] and base[1] if they happen to
8442 have the same r_offset. */
bca6d0e3
AM
8443 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8444 memcpy (onebuf, loc, elt_size);
0e287786 8445 memmove (base + elt_size, base, loc - base);
bca6d0e3 8446 memcpy (base, onebuf, elt_size);
0e287786
AM
8447 }
8448
b29b8669 8449 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8450 {
8451 /* base to p is sorted, *p is next to insert. */
8452 r_off = (*ext_r_off) (p);
8453 /* Search the sorted region for location to insert. */
8454 loc = p - elt_size;
8455 while (r_off < (*ext_r_off) (loc))
8456 loc -= elt_size;
8457 loc += elt_size;
8458 if (loc != p)
8459 {
bca6d0e3
AM
8460 /* Chances are there is a run of relocs to insert here,
8461 from one of more input files. Files are not always
8462 linked in order due to the way elf_link_input_bfd is
8463 called. See pr17666. */
8464 size_t sortlen = p - loc;
8465 bfd_vma r_off2 = (*ext_r_off) (loc);
8466 size_t runlen = elt_size;
8467 size_t buf_size = 96 * 1024;
8468 while (p + runlen < end
8469 && (sortlen <= buf_size
8470 || runlen + elt_size <= buf_size)
8471 && r_off2 > (*ext_r_off) (p + runlen))
8472 runlen += elt_size;
8473 if (buf == NULL)
8474 {
8475 buf = bfd_malloc (buf_size);
8476 if (buf == NULL)
8477 return FALSE;
8478 }
8479 if (runlen < sortlen)
8480 {
8481 memcpy (buf, p, runlen);
8482 memmove (loc + runlen, loc, sortlen);
8483 memcpy (loc, buf, runlen);
8484 }
8485 else
8486 {
8487 memcpy (buf, loc, sortlen);
8488 memmove (loc, p, runlen);
8489 memcpy (loc + runlen, buf, sortlen);
8490 }
b29b8669 8491 p += runlen - elt_size;
0e287786
AM
8492 }
8493 }
8494 /* Hashes are no longer valid. */
28dbcedc
AM
8495 free (reldata->hashes);
8496 reldata->hashes = NULL;
bca6d0e3 8497 free (buf);
53df40a4 8498 }
bca6d0e3 8499 return TRUE;
c152c796
AM
8500}
8501
8502struct elf_link_sort_rela
8503{
8504 union {
8505 bfd_vma offset;
8506 bfd_vma sym_mask;
8507 } u;
8508 enum elf_reloc_type_class type;
8509 /* We use this as an array of size int_rels_per_ext_rel. */
8510 Elf_Internal_Rela rela[1];
8511};
8512
8513static int
8514elf_link_sort_cmp1 (const void *A, const void *B)
8515{
a50b1753
NC
8516 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8517 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8518 int relativea, relativeb;
8519
8520 relativea = a->type == reloc_class_relative;
8521 relativeb = b->type == reloc_class_relative;
8522
8523 if (relativea < relativeb)
8524 return 1;
8525 if (relativea > relativeb)
8526 return -1;
8527 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8528 return -1;
8529 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8530 return 1;
8531 if (a->rela->r_offset < b->rela->r_offset)
8532 return -1;
8533 if (a->rela->r_offset > b->rela->r_offset)
8534 return 1;
8535 return 0;
8536}
8537
8538static int
8539elf_link_sort_cmp2 (const void *A, const void *B)
8540{
a50b1753
NC
8541 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8542 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8543
7e612e98 8544 if (a->type < b->type)
c152c796 8545 return -1;
7e612e98 8546 if (a->type > b->type)
c152c796 8547 return 1;
7e612e98 8548 if (a->u.offset < b->u.offset)
c152c796 8549 return -1;
7e612e98 8550 if (a->u.offset > b->u.offset)
c152c796
AM
8551 return 1;
8552 if (a->rela->r_offset < b->rela->r_offset)
8553 return -1;
8554 if (a->rela->r_offset > b->rela->r_offset)
8555 return 1;
8556 return 0;
8557}
8558
8559static size_t
8560elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8561{
3410fea8 8562 asection *dynamic_relocs;
fc66a176
L
8563 asection *rela_dyn;
8564 asection *rel_dyn;
c152c796
AM
8565 bfd_size_type count, size;
8566 size_t i, ret, sort_elt, ext_size;
8567 bfd_byte *sort, *s_non_relative, *p;
8568 struct elf_link_sort_rela *sq;
8569 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8570 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8571 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8572 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8573 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8574 struct bfd_link_order *lo;
8575 bfd_vma r_sym_mask;
3410fea8 8576 bfd_boolean use_rela;
c152c796 8577
3410fea8
NC
8578 /* Find a dynamic reloc section. */
8579 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8580 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8581 if (rela_dyn != NULL && rela_dyn->size > 0
8582 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8583 {
3410fea8
NC
8584 bfd_boolean use_rela_initialised = FALSE;
8585
8586 /* This is just here to stop gcc from complaining.
c8e44c6d 8587 Its initialization checking code is not perfect. */
3410fea8
NC
8588 use_rela = TRUE;
8589
8590 /* Both sections are present. Examine the sizes
8591 of the indirect sections to help us choose. */
8592 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8593 if (lo->type == bfd_indirect_link_order)
8594 {
8595 asection *o = lo->u.indirect.section;
8596
8597 if ((o->size % bed->s->sizeof_rela) == 0)
8598 {
8599 if ((o->size % bed->s->sizeof_rel) == 0)
8600 /* Section size is divisible by both rel and rela sizes.
8601 It is of no help to us. */
8602 ;
8603 else
8604 {
8605 /* Section size is only divisible by rela. */
8606 if (use_rela_initialised && (use_rela == FALSE))
8607 {
c8e44c6d
AM
8608 _bfd_error_handler (_("%B: Unable to sort relocs - "
8609 "they are in more than one size"),
8610 abfd);
3410fea8
NC
8611 bfd_set_error (bfd_error_invalid_operation);
8612 return 0;
8613 }
8614 else
8615 {
8616 use_rela = TRUE;
8617 use_rela_initialised = TRUE;
8618 }
8619 }
8620 }
8621 else if ((o->size % bed->s->sizeof_rel) == 0)
8622 {
8623 /* Section size is only divisible by rel. */
8624 if (use_rela_initialised && (use_rela == TRUE))
8625 {
c8e44c6d
AM
8626 _bfd_error_handler (_("%B: Unable to sort relocs - "
8627 "they are in more than one size"),
8628 abfd);
3410fea8
NC
8629 bfd_set_error (bfd_error_invalid_operation);
8630 return 0;
8631 }
8632 else
8633 {
8634 use_rela = FALSE;
8635 use_rela_initialised = TRUE;
8636 }
8637 }
8638 else
8639 {
c8e44c6d
AM
8640 /* The section size is not divisible by either -
8641 something is wrong. */
8642 _bfd_error_handler (_("%B: Unable to sort relocs - "
8643 "they are of an unknown size"), abfd);
3410fea8
NC
8644 bfd_set_error (bfd_error_invalid_operation);
8645 return 0;
8646 }
8647 }
8648
8649 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8650 if (lo->type == bfd_indirect_link_order)
8651 {
8652 asection *o = lo->u.indirect.section;
8653
8654 if ((o->size % bed->s->sizeof_rela) == 0)
8655 {
8656 if ((o->size % bed->s->sizeof_rel) == 0)
8657 /* Section size is divisible by both rel and rela sizes.
8658 It is of no help to us. */
8659 ;
8660 else
8661 {
8662 /* Section size is only divisible by rela. */
8663 if (use_rela_initialised && (use_rela == FALSE))
8664 {
c8e44c6d
AM
8665 _bfd_error_handler (_("%B: Unable to sort relocs - "
8666 "they are in more than one size"),
8667 abfd);
3410fea8
NC
8668 bfd_set_error (bfd_error_invalid_operation);
8669 return 0;
8670 }
8671 else
8672 {
8673 use_rela = TRUE;
8674 use_rela_initialised = TRUE;
8675 }
8676 }
8677 }
8678 else if ((o->size % bed->s->sizeof_rel) == 0)
8679 {
8680 /* Section size is only divisible by rel. */
8681 if (use_rela_initialised && (use_rela == TRUE))
8682 {
c8e44c6d
AM
8683 _bfd_error_handler (_("%B: Unable to sort relocs - "
8684 "they are in more than one size"),
8685 abfd);
3410fea8
NC
8686 bfd_set_error (bfd_error_invalid_operation);
8687 return 0;
8688 }
8689 else
8690 {
8691 use_rela = FALSE;
8692 use_rela_initialised = TRUE;
8693 }
8694 }
8695 else
8696 {
c8e44c6d
AM
8697 /* The section size is not divisible by either -
8698 something is wrong. */
8699 _bfd_error_handler (_("%B: Unable to sort relocs - "
8700 "they are of an unknown size"), abfd);
3410fea8
NC
8701 bfd_set_error (bfd_error_invalid_operation);
8702 return 0;
8703 }
8704 }
8705
8706 if (! use_rela_initialised)
8707 /* Make a guess. */
8708 use_rela = TRUE;
c152c796 8709 }
fc66a176
L
8710 else if (rela_dyn != NULL && rela_dyn->size > 0)
8711 use_rela = TRUE;
8712 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8713 use_rela = FALSE;
c152c796 8714 else
fc66a176 8715 return 0;
3410fea8
NC
8716
8717 if (use_rela)
c152c796 8718 {
3410fea8 8719 dynamic_relocs = rela_dyn;
c152c796
AM
8720 ext_size = bed->s->sizeof_rela;
8721 swap_in = bed->s->swap_reloca_in;
8722 swap_out = bed->s->swap_reloca_out;
8723 }
3410fea8
NC
8724 else
8725 {
8726 dynamic_relocs = rel_dyn;
8727 ext_size = bed->s->sizeof_rel;
8728 swap_in = bed->s->swap_reloc_in;
8729 swap_out = bed->s->swap_reloc_out;
8730 }
c152c796
AM
8731
8732 size = 0;
3410fea8 8733 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8734 if (lo->type == bfd_indirect_link_order)
3410fea8 8735 size += lo->u.indirect.section->size;
c152c796 8736
3410fea8 8737 if (size != dynamic_relocs->size)
c152c796
AM
8738 return 0;
8739
8740 sort_elt = (sizeof (struct elf_link_sort_rela)
8741 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8742
8743 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8744 if (count == 0)
8745 return 0;
a50b1753 8746 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8747
c152c796
AM
8748 if (sort == NULL)
8749 {
8750 (*info->callbacks->warning)
8751 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8752 return 0;
8753 }
8754
8755 if (bed->s->arch_size == 32)
8756 r_sym_mask = ~(bfd_vma) 0xff;
8757 else
8758 r_sym_mask = ~(bfd_vma) 0xffffffff;
8759
3410fea8 8760 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8761 if (lo->type == bfd_indirect_link_order)
8762 {
8763 bfd_byte *erel, *erelend;
8764 asection *o = lo->u.indirect.section;
8765
1da212d6
AM
8766 if (o->contents == NULL && o->size != 0)
8767 {
8768 /* This is a reloc section that is being handled as a normal
8769 section. See bfd_section_from_shdr. We can't combine
8770 relocs in this case. */
8771 free (sort);
8772 return 0;
8773 }
c152c796 8774 erel = o->contents;
eea6121a 8775 erelend = o->contents + o->size;
c8e44c6d 8776 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 8777
c152c796
AM
8778 while (erel < erelend)
8779 {
8780 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8781
c152c796 8782 (*swap_in) (abfd, erel, s->rela);
7e612e98 8783 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8784 s->u.sym_mask = r_sym_mask;
8785 p += sort_elt;
8786 erel += ext_size;
8787 }
8788 }
8789
8790 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8791
8792 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8793 {
8794 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8795 if (s->type != reloc_class_relative)
8796 break;
8797 }
8798 ret = i;
8799 s_non_relative = p;
8800
8801 sq = (struct elf_link_sort_rela *) s_non_relative;
8802 for (; i < count; i++, p += sort_elt)
8803 {
8804 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8805 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8806 sq = sp;
8807 sp->u.offset = sq->rela->r_offset;
8808 }
8809
8810 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8811
c8e44c6d
AM
8812 struct elf_link_hash_table *htab = elf_hash_table (info);
8813 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8814 {
8815 /* We have plt relocs in .rela.dyn. */
8816 sq = (struct elf_link_sort_rela *) sort;
8817 for (i = 0; i < count; i++)
8818 if (sq[count - i - 1].type != reloc_class_plt)
8819 break;
8820 if (i != 0 && htab->srelplt->size == i * ext_size)
8821 {
8822 struct bfd_link_order **plo;
8823 /* Put srelplt link_order last. This is so the output_offset
8824 set in the next loop is correct for DT_JMPREL. */
8825 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8826 if ((*plo)->type == bfd_indirect_link_order
8827 && (*plo)->u.indirect.section == htab->srelplt)
8828 {
8829 lo = *plo;
8830 *plo = lo->next;
8831 }
8832 else
8833 plo = &(*plo)->next;
8834 *plo = lo;
8835 lo->next = NULL;
8836 dynamic_relocs->map_tail.link_order = lo;
8837 }
8838 }
8839
8840 p = sort;
3410fea8 8841 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8842 if (lo->type == bfd_indirect_link_order)
8843 {
8844 bfd_byte *erel, *erelend;
8845 asection *o = lo->u.indirect.section;
8846
8847 erel = o->contents;
eea6121a 8848 erelend = o->contents + o->size;
c8e44c6d 8849 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
8850 while (erel < erelend)
8851 {
8852 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8853 (*swap_out) (abfd, s->rela, erel);
8854 p += sort_elt;
8855 erel += ext_size;
8856 }
8857 }
8858
8859 free (sort);
3410fea8 8860 *psec = dynamic_relocs;
c152c796
AM
8861 return ret;
8862}
8863
ef10c3ac 8864/* Add a symbol to the output symbol string table. */
c152c796 8865
6e0b88f1 8866static int
ef10c3ac
L
8867elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8868 const char *name,
8869 Elf_Internal_Sym *elfsym,
8870 asection *input_sec,
8871 struct elf_link_hash_entry *h)
c152c796 8872{
6e0b88f1 8873 int (*output_symbol_hook)
c152c796
AM
8874 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8875 struct elf_link_hash_entry *);
ef10c3ac 8876 struct elf_link_hash_table *hash_table;
c152c796 8877 const struct elf_backend_data *bed;
ef10c3ac 8878 bfd_size_type strtabsize;
c152c796 8879
8539e4e8
AM
8880 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8881
8b127cbc 8882 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
8883 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8884 if (output_symbol_hook != NULL)
8885 {
8b127cbc 8886 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
8887 if (ret != 1)
8888 return ret;
c152c796
AM
8889 }
8890
ef10c3ac
L
8891 if (name == NULL
8892 || *name == '\0'
8893 || (input_sec->flags & SEC_EXCLUDE))
8894 elfsym->st_name = (unsigned long) -1;
c152c796
AM
8895 else
8896 {
ef10c3ac
L
8897 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8898 to get the final offset for st_name. */
8899 elfsym->st_name
8900 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8901 name, FALSE);
c152c796 8902 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8903 return 0;
c152c796
AM
8904 }
8905
ef10c3ac
L
8906 hash_table = elf_hash_table (flinfo->info);
8907 strtabsize = hash_table->strtabsize;
8908 if (strtabsize <= hash_table->strtabcount)
c152c796 8909 {
ef10c3ac
L
8910 strtabsize += strtabsize;
8911 hash_table->strtabsize = strtabsize;
8912 strtabsize *= sizeof (*hash_table->strtab);
8913 hash_table->strtab
8914 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8915 strtabsize);
8916 if (hash_table->strtab == NULL)
6e0b88f1 8917 return 0;
c152c796 8918 }
ef10c3ac
L
8919 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8920 hash_table->strtab[hash_table->strtabcount].dest_index
8921 = hash_table->strtabcount;
8922 hash_table->strtab[hash_table->strtabcount].destshndx_index
8923 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8924
8925 bfd_get_symcount (flinfo->output_bfd) += 1;
8926 hash_table->strtabcount += 1;
8927
8928 return 1;
8929}
8930
8931/* Swap symbols out to the symbol table and flush the output symbols to
8932 the file. */
8933
8934static bfd_boolean
8935elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8936{
8937 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
8938 bfd_size_type amt;
8939 size_t i;
ef10c3ac
L
8940 const struct elf_backend_data *bed;
8941 bfd_byte *symbuf;
8942 Elf_Internal_Shdr *hdr;
8943 file_ptr pos;
8944 bfd_boolean ret;
8945
8946 if (!hash_table->strtabcount)
8947 return TRUE;
8948
8949 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8950
8951 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 8952
ef10c3ac
L
8953 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8954 symbuf = (bfd_byte *) bfd_malloc (amt);
8955 if (symbuf == NULL)
8956 return FALSE;
1b786873 8957
ef10c3ac 8958 if (flinfo->symshndxbuf)
c152c796 8959 {
ef53be89
AM
8960 amt = sizeof (Elf_External_Sym_Shndx);
8961 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
8962 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8963 if (flinfo->symshndxbuf == NULL)
c152c796 8964 {
ef10c3ac
L
8965 free (symbuf);
8966 return FALSE;
c152c796 8967 }
c152c796
AM
8968 }
8969
ef10c3ac
L
8970 for (i = 0; i < hash_table->strtabcount; i++)
8971 {
8972 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8973 if (elfsym->sym.st_name == (unsigned long) -1)
8974 elfsym->sym.st_name = 0;
8975 else
8976 elfsym->sym.st_name
8977 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8978 elfsym->sym.st_name);
8979 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8980 ((bfd_byte *) symbuf
8981 + (elfsym->dest_index
8982 * bed->s->sizeof_sym)),
8983 (flinfo->symshndxbuf
8984 + elfsym->destshndx_index));
8985 }
8986
8987 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8988 pos = hdr->sh_offset + hdr->sh_size;
8989 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8990 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8991 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8992 {
8993 hdr->sh_size += amt;
8994 ret = TRUE;
8995 }
8996 else
8997 ret = FALSE;
c152c796 8998
ef10c3ac
L
8999 free (symbuf);
9000
9001 free (hash_table->strtab);
9002 hash_table->strtab = NULL;
9003
9004 return ret;
c152c796
AM
9005}
9006
c0d5a53d
L
9007/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9008
9009static bfd_boolean
9010check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9011{
4fbb74a6
AM
9012 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9013 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9014 {
9015 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9016 beyond 64k. */
c0d5a53d
L
9017 (*_bfd_error_handler)
9018 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9019 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9020 bfd_set_error (bfd_error_nonrepresentable_section);
9021 return FALSE;
9022 }
9023 return TRUE;
9024}
9025
c152c796
AM
9026/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9027 allowing an unsatisfied unversioned symbol in the DSO to match a
9028 versioned symbol that would normally require an explicit version.
9029 We also handle the case that a DSO references a hidden symbol
9030 which may be satisfied by a versioned symbol in another DSO. */
9031
9032static bfd_boolean
9033elf_link_check_versioned_symbol (struct bfd_link_info *info,
9034 const struct elf_backend_data *bed,
9035 struct elf_link_hash_entry *h)
9036{
9037 bfd *abfd;
9038 struct elf_link_loaded_list *loaded;
9039
9040 if (!is_elf_hash_table (info->hash))
9041 return FALSE;
9042
90c984fc
L
9043 /* Check indirect symbol. */
9044 while (h->root.type == bfd_link_hash_indirect)
9045 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9046
c152c796
AM
9047 switch (h->root.type)
9048 {
9049 default:
9050 abfd = NULL;
9051 break;
9052
9053 case bfd_link_hash_undefined:
9054 case bfd_link_hash_undefweak:
9055 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9056 if (abfd == NULL
9057 || (abfd->flags & DYNAMIC) == 0
e56f61be 9058 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9059 return FALSE;
9060 break;
9061
9062 case bfd_link_hash_defined:
9063 case bfd_link_hash_defweak:
9064 abfd = h->root.u.def.section->owner;
9065 break;
9066
9067 case bfd_link_hash_common:
9068 abfd = h->root.u.c.p->section->owner;
9069 break;
9070 }
9071 BFD_ASSERT (abfd != NULL);
9072
9073 for (loaded = elf_hash_table (info)->loaded;
9074 loaded != NULL;
9075 loaded = loaded->next)
9076 {
9077 bfd *input;
9078 Elf_Internal_Shdr *hdr;
ef53be89
AM
9079 size_t symcount;
9080 size_t extsymcount;
9081 size_t extsymoff;
c152c796
AM
9082 Elf_Internal_Shdr *versymhdr;
9083 Elf_Internal_Sym *isym;
9084 Elf_Internal_Sym *isymend;
9085 Elf_Internal_Sym *isymbuf;
9086 Elf_External_Versym *ever;
9087 Elf_External_Versym *extversym;
9088
9089 input = loaded->abfd;
9090
9091 /* We check each DSO for a possible hidden versioned definition. */
9092 if (input == abfd
9093 || (input->flags & DYNAMIC) == 0
9094 || elf_dynversym (input) == 0)
9095 continue;
9096
9097 hdr = &elf_tdata (input)->dynsymtab_hdr;
9098
9099 symcount = hdr->sh_size / bed->s->sizeof_sym;
9100 if (elf_bad_symtab (input))
9101 {
9102 extsymcount = symcount;
9103 extsymoff = 0;
9104 }
9105 else
9106 {
9107 extsymcount = symcount - hdr->sh_info;
9108 extsymoff = hdr->sh_info;
9109 }
9110
9111 if (extsymcount == 0)
9112 continue;
9113
9114 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9115 NULL, NULL, NULL);
9116 if (isymbuf == NULL)
9117 return FALSE;
9118
9119 /* Read in any version definitions. */
9120 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9121 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9122 if (extversym == NULL)
9123 goto error_ret;
9124
9125 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9126 || (bfd_bread (extversym, versymhdr->sh_size, input)
9127 != versymhdr->sh_size))
9128 {
9129 free (extversym);
9130 error_ret:
9131 free (isymbuf);
9132 return FALSE;
9133 }
9134
9135 ever = extversym + extsymoff;
9136 isymend = isymbuf + extsymcount;
9137 for (isym = isymbuf; isym < isymend; isym++, ever++)
9138 {
9139 const char *name;
9140 Elf_Internal_Versym iver;
9141 unsigned short version_index;
9142
9143 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9144 || isym->st_shndx == SHN_UNDEF)
9145 continue;
9146
9147 name = bfd_elf_string_from_elf_section (input,
9148 hdr->sh_link,
9149 isym->st_name);
9150 if (strcmp (name, h->root.root.string) != 0)
9151 continue;
9152
9153 _bfd_elf_swap_versym_in (input, ever, &iver);
9154
d023c380
L
9155 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9156 && !(h->def_regular
9157 && h->forced_local))
c152c796
AM
9158 {
9159 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9160 have provided a definition for the undefined sym unless
9161 it is defined in a non-shared object and forced local.
9162 */
c152c796
AM
9163 abort ();
9164 }
9165
9166 version_index = iver.vs_vers & VERSYM_VERSION;
9167 if (version_index == 1 || version_index == 2)
9168 {
9169 /* This is the base or first version. We can use it. */
9170 free (extversym);
9171 free (isymbuf);
9172 return TRUE;
9173 }
9174 }
9175
9176 free (extversym);
9177 free (isymbuf);
9178 }
9179
9180 return FALSE;
9181}
9182
b8871f35
L
9183/* Convert ELF common symbol TYPE. */
9184
9185static int
9186elf_link_convert_common_type (struct bfd_link_info *info, int type)
9187{
9188 /* Commom symbol can only appear in relocatable link. */
9189 if (!bfd_link_relocatable (info))
9190 abort ();
9191 switch (info->elf_stt_common)
9192 {
9193 case unchanged:
9194 break;
9195 case elf_stt_common:
9196 type = STT_COMMON;
9197 break;
9198 case no_elf_stt_common:
9199 type = STT_OBJECT;
9200 break;
9201 }
9202 return type;
9203}
9204
c152c796
AM
9205/* Add an external symbol to the symbol table. This is called from
9206 the hash table traversal routine. When generating a shared object,
9207 we go through the symbol table twice. The first time we output
9208 anything that might have been forced to local scope in a version
9209 script. The second time we output the symbols that are still
9210 global symbols. */
9211
9212static bfd_boolean
7686d77d 9213elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9214{
7686d77d 9215 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9216 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9217 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9218 bfd_boolean strip;
9219 Elf_Internal_Sym sym;
9220 asection *input_sec;
9221 const struct elf_backend_data *bed;
6e0b88f1
AM
9222 long indx;
9223 int ret;
b8871f35 9224 unsigned int type;
6e33951e
L
9225 /* A symbol is bound locally if it is forced local or it is locally
9226 defined, hidden versioned, not referenced by shared library and
9227 not exported when linking executable. */
9228 bfd_boolean local_bind = (h->forced_local
0e1862bb 9229 || (bfd_link_executable (flinfo->info)
6e33951e
L
9230 && !flinfo->info->export_dynamic
9231 && !h->dynamic
9232 && !h->ref_dynamic
9233 && h->def_regular
422f1182 9234 && h->versioned == versioned_hidden));
c152c796
AM
9235
9236 if (h->root.type == bfd_link_hash_warning)
9237 {
9238 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9239 if (h->root.type == bfd_link_hash_new)
9240 return TRUE;
9241 }
9242
9243 /* Decide whether to output this symbol in this pass. */
9244 if (eoinfo->localsyms)
9245 {
6e33951e 9246 if (!local_bind)
c152c796
AM
9247 return TRUE;
9248 }
9249 else
9250 {
6e33951e 9251 if (local_bind)
c152c796
AM
9252 return TRUE;
9253 }
9254
8b127cbc 9255 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9256
12ac1cf5 9257 if (h->root.type == bfd_link_hash_undefined)
c152c796 9258 {
12ac1cf5
NC
9259 /* If we have an undefined symbol reference here then it must have
9260 come from a shared library that is being linked in. (Undefined
98da7939
L
9261 references in regular files have already been handled unless
9262 they are in unreferenced sections which are removed by garbage
9263 collection). */
12ac1cf5
NC
9264 bfd_boolean ignore_undef = FALSE;
9265
9266 /* Some symbols may be special in that the fact that they're
9267 undefined can be safely ignored - let backend determine that. */
9268 if (bed->elf_backend_ignore_undef_symbol)
9269 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9270
9271 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9272 if (!ignore_undef
12ac1cf5 9273 && h->ref_dynamic
8b127cbc
AM
9274 && (!h->ref_regular || flinfo->info->gc_sections)
9275 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9276 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9277 (*flinfo->info->callbacks->undefined_symbol)
9278 (flinfo->info, h->root.root.string,
9279 h->ref_regular ? NULL : h->root.u.undef.abfd,
9280 NULL, 0,
9281 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9282
9283 /* Strip a global symbol defined in a discarded section. */
9284 if (h->indx == -3)
9285 return TRUE;
c152c796
AM
9286 }
9287
9288 /* We should also warn if a forced local symbol is referenced from
9289 shared libraries. */
0e1862bb 9290 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9291 && h->forced_local
9292 && h->ref_dynamic
371a5866 9293 && h->def_regular
f5385ebf 9294 && !h->dynamic_def
ee659f1f 9295 && h->ref_dynamic_nonweak
8b127cbc 9296 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9297 {
17d078c5
AM
9298 bfd *def_bfd;
9299 const char *msg;
90c984fc
L
9300 struct elf_link_hash_entry *hi = h;
9301
9302 /* Check indirect symbol. */
9303 while (hi->root.type == bfd_link_hash_indirect)
9304 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9305
9306 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9307 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9308 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9309 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9310 else
9311 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9312 def_bfd = flinfo->output_bfd;
90c984fc
L
9313 if (hi->root.u.def.section != bfd_abs_section_ptr)
9314 def_bfd = hi->root.u.def.section->owner;
8b127cbc 9315 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
17d078c5
AM
9316 h->root.root.string);
9317 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9318 eoinfo->failed = TRUE;
9319 return FALSE;
9320 }
9321
9322 /* We don't want to output symbols that have never been mentioned by
9323 a regular file, or that we have been told to strip. However, if
9324 h->indx is set to -2, the symbol is used by a reloc and we must
9325 output it. */
d983c8c5 9326 strip = FALSE;
c152c796 9327 if (h->indx == -2)
d983c8c5 9328 ;
f5385ebf 9329 else if ((h->def_dynamic
77cfaee6
AM
9330 || h->ref_dynamic
9331 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9332 && !h->def_regular
9333 && !h->ref_regular)
c152c796 9334 strip = TRUE;
8b127cbc 9335 else if (flinfo->info->strip == strip_all)
c152c796 9336 strip = TRUE;
8b127cbc
AM
9337 else if (flinfo->info->strip == strip_some
9338 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9339 h->root.root.string, FALSE, FALSE) == NULL)
9340 strip = TRUE;
d56d55e7
AM
9341 else if ((h->root.type == bfd_link_hash_defined
9342 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9343 && ((flinfo->info->strip_discarded
dbaa2011 9344 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9345 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9346 && h->root.u.def.section->owner != NULL
d56d55e7 9347 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9348 strip = TRUE;
9e2278f5
AM
9349 else if ((h->root.type == bfd_link_hash_undefined
9350 || h->root.type == bfd_link_hash_undefweak)
9351 && h->root.u.undef.abfd != NULL
9352 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9353 strip = TRUE;
c152c796 9354
b8871f35
L
9355 type = h->type;
9356
c152c796 9357 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9358 nothing else to do. However, if it is a forced local symbol or
9359 an ifunc symbol we need to give the backend finish_dynamic_symbol
9360 function a chance to make it dynamic. */
c152c796
AM
9361 if (strip
9362 && h->dynindx == -1
b8871f35 9363 && type != STT_GNU_IFUNC
f5385ebf 9364 && !h->forced_local)
c152c796
AM
9365 return TRUE;
9366
9367 sym.st_value = 0;
9368 sym.st_size = h->size;
9369 sym.st_other = h->other;
c152c796
AM
9370 switch (h->root.type)
9371 {
9372 default:
9373 case bfd_link_hash_new:
9374 case bfd_link_hash_warning:
9375 abort ();
9376 return FALSE;
9377
9378 case bfd_link_hash_undefined:
9379 case bfd_link_hash_undefweak:
9380 input_sec = bfd_und_section_ptr;
9381 sym.st_shndx = SHN_UNDEF;
9382 break;
9383
9384 case bfd_link_hash_defined:
9385 case bfd_link_hash_defweak:
9386 {
9387 input_sec = h->root.u.def.section;
9388 if (input_sec->output_section != NULL)
9389 {
9390 sym.st_shndx =
8b127cbc 9391 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9392 input_sec->output_section);
9393 if (sym.st_shndx == SHN_BAD)
9394 {
9395 (*_bfd_error_handler)
d003868e 9396 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9397 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9398 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9399 eoinfo->failed = TRUE;
9400 return FALSE;
9401 }
9402
9403 /* ELF symbols in relocatable files are section relative,
9404 but in nonrelocatable files they are virtual
9405 addresses. */
9406 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9407 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9408 {
9409 sym.st_value += input_sec->output_section->vma;
9410 if (h->type == STT_TLS)
9411 {
8b127cbc 9412 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9413 if (tls_sec != NULL)
9414 sym.st_value -= tls_sec->vma;
c152c796
AM
9415 }
9416 }
9417 }
9418 else
9419 {
9420 BFD_ASSERT (input_sec->owner == NULL
9421 || (input_sec->owner->flags & DYNAMIC) != 0);
9422 sym.st_shndx = SHN_UNDEF;
9423 input_sec = bfd_und_section_ptr;
9424 }
9425 }
9426 break;
9427
9428 case bfd_link_hash_common:
9429 input_sec = h->root.u.c.p->section;
a4d8e49b 9430 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9431 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9432 break;
9433
9434 case bfd_link_hash_indirect:
9435 /* These symbols are created by symbol versioning. They point
9436 to the decorated version of the name. For example, if the
9437 symbol foo@@GNU_1.2 is the default, which should be used when
9438 foo is used with no version, then we add an indirect symbol
9439 foo which points to foo@@GNU_1.2. We ignore these symbols,
9440 since the indirected symbol is already in the hash table. */
9441 return TRUE;
9442 }
9443
b8871f35
L
9444 if (type == STT_COMMON || type == STT_OBJECT)
9445 switch (h->root.type)
9446 {
9447 case bfd_link_hash_common:
9448 type = elf_link_convert_common_type (flinfo->info, type);
9449 break;
9450 case bfd_link_hash_defined:
9451 case bfd_link_hash_defweak:
9452 if (bed->common_definition (&sym))
9453 type = elf_link_convert_common_type (flinfo->info, type);
9454 else
9455 type = STT_OBJECT;
9456 break;
9457 case bfd_link_hash_undefined:
9458 case bfd_link_hash_undefweak:
9459 break;
9460 default:
9461 abort ();
9462 }
9463
9464 if (local_bind)
9465 {
9466 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9467 /* Turn off visibility on local symbol. */
9468 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9469 }
9470 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9471 else if (h->unique_global && h->def_regular)
9472 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9473 else if (h->root.type == bfd_link_hash_undefweak
9474 || h->root.type == bfd_link_hash_defweak)
9475 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9476 else
9477 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9478 sym.st_target_internal = h->target_internal;
9479
c152c796
AM
9480 /* Give the processor backend a chance to tweak the symbol value,
9481 and also to finish up anything that needs to be done for this
9482 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9483 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9484 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9485 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9486 && h->def_regular
0e1862bb 9487 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9488 || ((h->dynindx != -1
9489 || h->forced_local)
0e1862bb 9490 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9491 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9492 || h->root.type != bfd_link_hash_undefweak))
9493 || !h->forced_local)
8b127cbc 9494 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9495 {
9496 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9497 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9498 {
9499 eoinfo->failed = TRUE;
9500 return FALSE;
9501 }
9502 }
9503
9504 /* If we are marking the symbol as undefined, and there are no
9505 non-weak references to this symbol from a regular object, then
9506 mark the symbol as weak undefined; if there are non-weak
9507 references, mark the symbol as strong. We can't do this earlier,
9508 because it might not be marked as undefined until the
9509 finish_dynamic_symbol routine gets through with it. */
9510 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9511 && h->ref_regular
c152c796
AM
9512 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9513 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9514 {
9515 int bindtype;
b8871f35 9516 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9517
9518 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9519 if (type == STT_GNU_IFUNC)
9520 type = STT_FUNC;
c152c796 9521
f5385ebf 9522 if (h->ref_regular_nonweak)
c152c796
AM
9523 bindtype = STB_GLOBAL;
9524 else
9525 bindtype = STB_WEAK;
2955ec4c 9526 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9527 }
9528
bda987c2
CD
9529 /* If this is a symbol defined in a dynamic library, don't use the
9530 symbol size from the dynamic library. Relinking an executable
9531 against a new library may introduce gratuitous changes in the
9532 executable's symbols if we keep the size. */
9533 if (sym.st_shndx == SHN_UNDEF
9534 && !h->def_regular
9535 && h->def_dynamic)
9536 sym.st_size = 0;
9537
c152c796
AM
9538 /* If a non-weak symbol with non-default visibility is not defined
9539 locally, it is a fatal error. */
0e1862bb 9540 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9541 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9542 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9543 && h->root.type == bfd_link_hash_undefined
f5385ebf 9544 && !h->def_regular)
c152c796 9545 {
17d078c5
AM
9546 const char *msg;
9547
9548 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9549 msg = _("%B: protected symbol `%s' isn't defined");
9550 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9551 msg = _("%B: internal symbol `%s' isn't defined");
9552 else
9553 msg = _("%B: hidden symbol `%s' isn't defined");
8b127cbc 9554 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9555 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9556 eoinfo->failed = TRUE;
9557 return FALSE;
9558 }
9559
9560 /* If this symbol should be put in the .dynsym section, then put it
9561 there now. We already know the symbol index. We also fill in
9562 the entry in the .hash section. */
cae1fbbb 9563 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9564 && h->dynindx != -1
8b127cbc 9565 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9566 {
c152c796
AM
9567 bfd_byte *esym;
9568
90c984fc
L
9569 /* Since there is no version information in the dynamic string,
9570 if there is no version info in symbol version section, we will
1659f720 9571 have a run-time problem if not linking executable, referenced
6e33951e
L
9572 by shared library, not locally defined, or not bound locally.
9573 */
1659f720 9574 if (h->verinfo.verdef == NULL
6e33951e 9575 && !local_bind
0e1862bb 9576 && (!bfd_link_executable (flinfo->info)
1659f720
L
9577 || h->ref_dynamic
9578 || !h->def_regular))
90c984fc
L
9579 {
9580 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9581
9582 if (p && p [1] != '\0')
9583 {
9584 (*_bfd_error_handler)
9585 (_("%B: No symbol version section for versioned symbol `%s'"),
9586 flinfo->output_bfd, h->root.root.string);
9587 eoinfo->failed = TRUE;
9588 return FALSE;
9589 }
9590 }
9591
c152c796 9592 sym.st_name = h->dynstr_index;
cae1fbbb
L
9593 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9594 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9595 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9596 {
9597 eoinfo->failed = TRUE;
9598 return FALSE;
9599 }
8b127cbc 9600 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9601
8b127cbc 9602 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9603 {
9604 size_t hash_entry_size;
9605 bfd_byte *bucketpos;
9606 bfd_vma chain;
41198d0c
L
9607 size_t bucketcount;
9608 size_t bucket;
9609
8b127cbc 9610 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9611 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9612
9613 hash_entry_size
8b127cbc
AM
9614 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9615 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9616 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9617 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9618 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9619 bucketpos);
9620 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9621 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9622 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9623 }
c152c796 9624
8b127cbc 9625 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9626 {
9627 Elf_Internal_Versym iversym;
9628 Elf_External_Versym *eversym;
9629
f5385ebf 9630 if (!h->def_regular)
c152c796 9631 {
7b20f099
AM
9632 if (h->verinfo.verdef == NULL
9633 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9634 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9635 iversym.vs_vers = 0;
9636 else
9637 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9638 }
9639 else
9640 {
9641 if (h->verinfo.vertree == NULL)
9642 iversym.vs_vers = 1;
9643 else
9644 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9645 if (flinfo->info->create_default_symver)
3e3b46e5 9646 iversym.vs_vers++;
c152c796
AM
9647 }
9648
422f1182 9649 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9650 defined locally. */
422f1182 9651 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9652 iversym.vs_vers |= VERSYM_HIDDEN;
9653
8b127cbc 9654 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9655 eversym += h->dynindx;
8b127cbc 9656 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9657 }
9658 }
9659
d983c8c5
AM
9660 /* If the symbol is undefined, and we didn't output it to .dynsym,
9661 strip it from .symtab too. Obviously we can't do this for
9662 relocatable output or when needed for --emit-relocs. */
9663 else if (input_sec == bfd_und_section_ptr
9664 && h->indx != -2
0e1862bb 9665 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9666 return TRUE;
9667 /* Also strip others that we couldn't earlier due to dynamic symbol
9668 processing. */
9669 if (strip)
9670 return TRUE;
9671 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9672 return TRUE;
9673
2ec55de3
AM
9674 /* Output a FILE symbol so that following locals are not associated
9675 with the wrong input file. We need one for forced local symbols
9676 if we've seen more than one FILE symbol or when we have exactly
9677 one FILE symbol but global symbols are present in a file other
9678 than the one with the FILE symbol. We also need one if linker
9679 defined symbols are present. In practice these conditions are
9680 always met, so just emit the FILE symbol unconditionally. */
9681 if (eoinfo->localsyms
9682 && !eoinfo->file_sym_done
9683 && eoinfo->flinfo->filesym_count != 0)
9684 {
9685 Elf_Internal_Sym fsym;
9686
9687 memset (&fsym, 0, sizeof (fsym));
9688 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9689 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9690 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9691 bfd_und_section_ptr, NULL))
2ec55de3
AM
9692 return FALSE;
9693
9694 eoinfo->file_sym_done = TRUE;
9695 }
9696
8b127cbc 9697 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9698 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9699 input_sec, h);
6e0b88f1 9700 if (ret == 0)
c152c796
AM
9701 {
9702 eoinfo->failed = TRUE;
9703 return FALSE;
9704 }
6e0b88f1
AM
9705 else if (ret == 1)
9706 h->indx = indx;
9707 else if (h->indx == -2)
9708 abort();
c152c796
AM
9709
9710 return TRUE;
9711}
9712
cdd3575c
AM
9713/* Return TRUE if special handling is done for relocs in SEC against
9714 symbols defined in discarded sections. */
9715
c152c796
AM
9716static bfd_boolean
9717elf_section_ignore_discarded_relocs (asection *sec)
9718{
9719 const struct elf_backend_data *bed;
9720
cdd3575c
AM
9721 switch (sec->sec_info_type)
9722 {
dbaa2011
AM
9723 case SEC_INFO_TYPE_STABS:
9724 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9725 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9726 return TRUE;
9727 default:
9728 break;
9729 }
c152c796
AM
9730
9731 bed = get_elf_backend_data (sec->owner);
9732 if (bed->elf_backend_ignore_discarded_relocs != NULL
9733 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9734 return TRUE;
9735
9736 return FALSE;
9737}
9738
9e66c942
AM
9739/* Return a mask saying how ld should treat relocations in SEC against
9740 symbols defined in discarded sections. If this function returns
9741 COMPLAIN set, ld will issue a warning message. If this function
9742 returns PRETEND set, and the discarded section was link-once and the
9743 same size as the kept link-once section, ld will pretend that the
9744 symbol was actually defined in the kept section. Otherwise ld will
9745 zero the reloc (at least that is the intent, but some cooperation by
9746 the target dependent code is needed, particularly for REL targets). */
9747
8a696751
AM
9748unsigned int
9749_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9750{
9e66c942 9751 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9752 return PRETEND;
cdd3575c
AM
9753
9754 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9755 return 0;
cdd3575c
AM
9756
9757 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9758 return 0;
cdd3575c 9759
9e66c942 9760 return COMPLAIN | PRETEND;
cdd3575c
AM
9761}
9762
3d7f7666
L
9763/* Find a match between a section and a member of a section group. */
9764
9765static asection *
c0f00686
L
9766match_group_member (asection *sec, asection *group,
9767 struct bfd_link_info *info)
3d7f7666
L
9768{
9769 asection *first = elf_next_in_group (group);
9770 asection *s = first;
9771
9772 while (s != NULL)
9773 {
c0f00686 9774 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9775 return s;
9776
83180ade 9777 s = elf_next_in_group (s);
3d7f7666
L
9778 if (s == first)
9779 break;
9780 }
9781
9782 return NULL;
9783}
9784
01b3c8ab 9785/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9786 to replace it. Return the replacement if it is OK. Otherwise return
9787 NULL. */
01b3c8ab
L
9788
9789asection *
c0f00686 9790_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9791{
9792 asection *kept;
9793
9794 kept = sec->kept_section;
9795 if (kept != NULL)
9796 {
c2370991 9797 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9798 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9799 if (kept != NULL
9800 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9801 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9802 kept = NULL;
c2370991 9803 sec->kept_section = kept;
01b3c8ab
L
9804 }
9805 return kept;
9806}
9807
c152c796
AM
9808/* Link an input file into the linker output file. This function
9809 handles all the sections and relocations of the input file at once.
9810 This is so that we only have to read the local symbols once, and
9811 don't have to keep them in memory. */
9812
9813static bfd_boolean
8b127cbc 9814elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9815{
ece5ef60 9816 int (*relocate_section)
c152c796
AM
9817 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9818 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9819 bfd *output_bfd;
9820 Elf_Internal_Shdr *symtab_hdr;
9821 size_t locsymcount;
9822 size_t extsymoff;
9823 Elf_Internal_Sym *isymbuf;
9824 Elf_Internal_Sym *isym;
9825 Elf_Internal_Sym *isymend;
9826 long *pindex;
9827 asection **ppsection;
9828 asection *o;
9829 const struct elf_backend_data *bed;
c152c796 9830 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9831 bfd_size_type address_size;
9832 bfd_vma r_type_mask;
9833 int r_sym_shift;
ffbc01cc 9834 bfd_boolean have_file_sym = FALSE;
c152c796 9835
8b127cbc 9836 output_bfd = flinfo->output_bfd;
c152c796
AM
9837 bed = get_elf_backend_data (output_bfd);
9838 relocate_section = bed->elf_backend_relocate_section;
9839
9840 /* If this is a dynamic object, we don't want to do anything here:
9841 we don't want the local symbols, and we don't want the section
9842 contents. */
9843 if ((input_bfd->flags & DYNAMIC) != 0)
9844 return TRUE;
9845
c152c796
AM
9846 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9847 if (elf_bad_symtab (input_bfd))
9848 {
9849 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9850 extsymoff = 0;
9851 }
9852 else
9853 {
9854 locsymcount = symtab_hdr->sh_info;
9855 extsymoff = symtab_hdr->sh_info;
9856 }
9857
9858 /* Read the local symbols. */
9859 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9860 if (isymbuf == NULL && locsymcount != 0)
9861 {
9862 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9863 flinfo->internal_syms,
9864 flinfo->external_syms,
9865 flinfo->locsym_shndx);
c152c796
AM
9866 if (isymbuf == NULL)
9867 return FALSE;
9868 }
9869
9870 /* Find local symbol sections and adjust values of symbols in
9871 SEC_MERGE sections. Write out those local symbols we know are
9872 going into the output file. */
9873 isymend = isymbuf + locsymcount;
8b127cbc 9874 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9875 isym < isymend;
9876 isym++, pindex++, ppsection++)
9877 {
9878 asection *isec;
9879 const char *name;
9880 Elf_Internal_Sym osym;
6e0b88f1
AM
9881 long indx;
9882 int ret;
c152c796
AM
9883
9884 *pindex = -1;
9885
9886 if (elf_bad_symtab (input_bfd))
9887 {
9888 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9889 {
9890 *ppsection = NULL;
9891 continue;
9892 }
9893 }
9894
9895 if (isym->st_shndx == SHN_UNDEF)
9896 isec = bfd_und_section_ptr;
c152c796
AM
9897 else if (isym->st_shndx == SHN_ABS)
9898 isec = bfd_abs_section_ptr;
9899 else if (isym->st_shndx == SHN_COMMON)
9900 isec = bfd_com_section_ptr;
9901 else
9902 {
cb33740c
AM
9903 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9904 if (isec == NULL)
9905 {
9906 /* Don't attempt to output symbols with st_shnx in the
9907 reserved range other than SHN_ABS and SHN_COMMON. */
9908 *ppsection = NULL;
9909 continue;
9910 }
dbaa2011 9911 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9912 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9913 isym->st_value =
9914 _bfd_merged_section_offset (output_bfd, &isec,
9915 elf_section_data (isec)->sec_info,
9916 isym->st_value);
c152c796
AM
9917 }
9918
9919 *ppsection = isec;
9920
d983c8c5
AM
9921 /* Don't output the first, undefined, symbol. In fact, don't
9922 output any undefined local symbol. */
9923 if (isec == bfd_und_section_ptr)
c152c796
AM
9924 continue;
9925
9926 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9927 {
9928 /* We never output section symbols. Instead, we use the
9929 section symbol of the corresponding section in the output
9930 file. */
9931 continue;
9932 }
9933
9934 /* If we are stripping all symbols, we don't want to output this
9935 one. */
8b127cbc 9936 if (flinfo->info->strip == strip_all)
c152c796
AM
9937 continue;
9938
9939 /* If we are discarding all local symbols, we don't want to
9940 output this one. If we are generating a relocatable output
9941 file, then some of the local symbols may be required by
9942 relocs; we output them below as we discover that they are
9943 needed. */
8b127cbc 9944 if (flinfo->info->discard == discard_all)
c152c796
AM
9945 continue;
9946
9947 /* If this symbol is defined in a section which we are
f02571c5
AM
9948 discarding, we don't need to keep it. */
9949 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9950 && isym->st_shndx < SHN_LORESERVE
9951 && bfd_section_removed_from_list (output_bfd,
9952 isec->output_section))
e75a280b
L
9953 continue;
9954
c152c796
AM
9955 /* Get the name of the symbol. */
9956 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9957 isym->st_name);
9958 if (name == NULL)
9959 return FALSE;
9960
9961 /* See if we are discarding symbols with this name. */
8b127cbc
AM
9962 if ((flinfo->info->strip == strip_some
9963 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 9964 == NULL))
8b127cbc 9965 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
9966 && (isec->flags & SEC_MERGE)
9967 && !bfd_link_relocatable (flinfo->info))
8b127cbc 9968 || flinfo->info->discard == discard_l)
c152c796
AM
9969 && bfd_is_local_label_name (input_bfd, name)))
9970 continue;
9971
ffbc01cc
AM
9972 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9973 {
ce875075
AM
9974 if (input_bfd->lto_output)
9975 /* -flto puts a temp file name here. This means builds
9976 are not reproducible. Discard the symbol. */
9977 continue;
ffbc01cc
AM
9978 have_file_sym = TRUE;
9979 flinfo->filesym_count += 1;
9980 }
9981 if (!have_file_sym)
9982 {
9983 /* In the absence of debug info, bfd_find_nearest_line uses
9984 FILE symbols to determine the source file for local
9985 function symbols. Provide a FILE symbol here if input
9986 files lack such, so that their symbols won't be
9987 associated with a previous input file. It's not the
9988 source file, but the best we can do. */
9989 have_file_sym = TRUE;
9990 flinfo->filesym_count += 1;
9991 memset (&osym, 0, sizeof (osym));
9992 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9993 osym.st_shndx = SHN_ABS;
ef10c3ac
L
9994 if (!elf_link_output_symstrtab (flinfo,
9995 (input_bfd->lto_output ? NULL
9996 : input_bfd->filename),
9997 &osym, bfd_abs_section_ptr,
9998 NULL))
ffbc01cc
AM
9999 return FALSE;
10000 }
10001
c152c796
AM
10002 osym = *isym;
10003
10004 /* Adjust the section index for the output file. */
10005 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10006 isec->output_section);
10007 if (osym.st_shndx == SHN_BAD)
10008 return FALSE;
10009
c152c796
AM
10010 /* ELF symbols in relocatable files are section relative, but
10011 in executable files they are virtual addresses. Note that
10012 this code assumes that all ELF sections have an associated
10013 BFD section with a reasonable value for output_offset; below
10014 we assume that they also have a reasonable value for
10015 output_section. Any special sections must be set up to meet
10016 these requirements. */
10017 osym.st_value += isec->output_offset;
0e1862bb 10018 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10019 {
10020 osym.st_value += isec->output_section->vma;
10021 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10022 {
10023 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10024 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10025 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10026 }
10027 }
10028
6e0b88f1 10029 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10030 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10031 if (ret == 0)
c152c796 10032 return FALSE;
6e0b88f1
AM
10033 else if (ret == 1)
10034 *pindex = indx;
c152c796
AM
10035 }
10036
310fd250
L
10037 if (bed->s->arch_size == 32)
10038 {
10039 r_type_mask = 0xff;
10040 r_sym_shift = 8;
10041 address_size = 4;
10042 }
10043 else
10044 {
10045 r_type_mask = 0xffffffff;
10046 r_sym_shift = 32;
10047 address_size = 8;
10048 }
10049
c152c796
AM
10050 /* Relocate the contents of each section. */
10051 sym_hashes = elf_sym_hashes (input_bfd);
10052 for (o = input_bfd->sections; o != NULL; o = o->next)
10053 {
10054 bfd_byte *contents;
10055
10056 if (! o->linker_mark)
10057 {
10058 /* This section was omitted from the link. */
10059 continue;
10060 }
10061
0e1862bb 10062 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
10063 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10064 {
10065 /* Deal with the group signature symbol. */
10066 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10067 unsigned long symndx = sec_data->this_hdr.sh_info;
10068 asection *osec = o->output_section;
10069
10070 if (symndx >= locsymcount
10071 || (elf_bad_symtab (input_bfd)
8b127cbc 10072 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10073 {
10074 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10075 while (h->root.type == bfd_link_hash_indirect
10076 || h->root.type == bfd_link_hash_warning)
10077 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10078 /* Arrange for symbol to be output. */
10079 h->indx = -2;
10080 elf_section_data (osec)->this_hdr.sh_info = -2;
10081 }
10082 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10083 {
10084 /* We'll use the output section target_index. */
8b127cbc 10085 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10086 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10087 }
10088 else
10089 {
8b127cbc 10090 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10091 {
10092 /* Otherwise output the local symbol now. */
10093 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10094 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10095 const char *name;
6e0b88f1
AM
10096 long indx;
10097 int ret;
bcacc0f5
AM
10098
10099 name = bfd_elf_string_from_elf_section (input_bfd,
10100 symtab_hdr->sh_link,
10101 sym.st_name);
10102 if (name == NULL)
10103 return FALSE;
10104
10105 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10106 sec);
10107 if (sym.st_shndx == SHN_BAD)
10108 return FALSE;
10109
10110 sym.st_value += o->output_offset;
10111
6e0b88f1 10112 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10113 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10114 NULL);
6e0b88f1 10115 if (ret == 0)
bcacc0f5 10116 return FALSE;
6e0b88f1 10117 else if (ret == 1)
8b127cbc 10118 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10119 else
10120 abort ();
bcacc0f5
AM
10121 }
10122 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10123 = flinfo->indices[symndx];
bcacc0f5
AM
10124 }
10125 }
10126
c152c796 10127 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10128 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10129 continue;
10130
10131 if ((o->flags & SEC_LINKER_CREATED) != 0)
10132 {
10133 /* Section was created by _bfd_elf_link_create_dynamic_sections
10134 or somesuch. */
10135 continue;
10136 }
10137
10138 /* Get the contents of the section. They have been cached by a
10139 relaxation routine. Note that o is a section in an input
10140 file, so the contents field will not have been set by any of
10141 the routines which work on output files. */
10142 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10143 {
10144 contents = elf_section_data (o)->this_hdr.contents;
10145 if (bed->caches_rawsize
10146 && o->rawsize != 0
10147 && o->rawsize < o->size)
10148 {
10149 memcpy (flinfo->contents, contents, o->rawsize);
10150 contents = flinfo->contents;
10151 }
10152 }
c152c796
AM
10153 else
10154 {
8b127cbc 10155 contents = flinfo->contents;
4a114e3e 10156 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10157 return FALSE;
10158 }
10159
10160 if ((o->flags & SEC_RELOC) != 0)
10161 {
10162 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10163 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10164 int action_discarded;
ece5ef60 10165 int ret;
c152c796
AM
10166
10167 /* Get the swapped relocs. */
10168 internal_relocs
8b127cbc
AM
10169 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10170 flinfo->internal_relocs, FALSE);
c152c796
AM
10171 if (internal_relocs == NULL
10172 && o->reloc_count > 0)
10173 return FALSE;
10174
310fd250
L
10175 /* We need to reverse-copy input .ctors/.dtors sections if
10176 they are placed in .init_array/.finit_array for output. */
10177 if (o->size > address_size
10178 && ((strncmp (o->name, ".ctors", 6) == 0
10179 && strcmp (o->output_section->name,
10180 ".init_array") == 0)
10181 || (strncmp (o->name, ".dtors", 6) == 0
10182 && strcmp (o->output_section->name,
10183 ".fini_array") == 0))
10184 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10185 {
310fd250
L
10186 if (o->size != o->reloc_count * address_size)
10187 {
10188 (*_bfd_error_handler)
10189 (_("error: %B: size of section %A is not "
10190 "multiple of address size"),
10191 input_bfd, o);
10192 bfd_set_error (bfd_error_on_input);
10193 return FALSE;
10194 }
10195 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10196 }
10197
0f02bbd9 10198 action_discarded = -1;
c152c796 10199 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10200 action_discarded = (*bed->action_discarded) (o);
10201
10202 /* Run through the relocs evaluating complex reloc symbols and
10203 looking for relocs against symbols from discarded sections
10204 or section symbols from removed link-once sections.
10205 Complain about relocs against discarded sections. Zero
10206 relocs against removed link-once sections. */
10207
10208 rel = internal_relocs;
10209 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10210 for ( ; rel < relend; rel++)
c152c796 10211 {
0f02bbd9
AM
10212 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10213 unsigned int s_type;
10214 asection **ps, *sec;
10215 struct elf_link_hash_entry *h = NULL;
10216 const char *sym_name;
c152c796 10217
0f02bbd9
AM
10218 if (r_symndx == STN_UNDEF)
10219 continue;
c152c796 10220
0f02bbd9
AM
10221 if (r_symndx >= locsymcount
10222 || (elf_bad_symtab (input_bfd)
8b127cbc 10223 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10224 {
10225 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10226
0f02bbd9
AM
10227 /* Badly formatted input files can contain relocs that
10228 reference non-existant symbols. Check here so that
10229 we do not seg fault. */
10230 if (h == NULL)
c152c796 10231 {
0f02bbd9 10232 char buffer [32];
dce669a1 10233
0f02bbd9
AM
10234 sprintf_vma (buffer, rel->r_info);
10235 (*_bfd_error_handler)
10236 (_("error: %B contains a reloc (0x%s) for section %A "
10237 "that references a non-existent global symbol"),
10238 input_bfd, o, buffer);
10239 bfd_set_error (bfd_error_bad_value);
10240 return FALSE;
10241 }
3b36f7e6 10242
0f02bbd9
AM
10243 while (h->root.type == bfd_link_hash_indirect
10244 || h->root.type == bfd_link_hash_warning)
10245 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10246
0f02bbd9 10247 s_type = h->type;
cdd3575c 10248
9e2dec47 10249 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10250 mark the symbol as undefined. Note that the
10251 linker may attach linker created dynamic sections
10252 to the plugin bfd. Symbols defined in linker
10253 created sections are not plugin symbols. */
9e2dec47
L
10254 if (h->root.non_ir_ref
10255 && (h->root.type == bfd_link_hash_defined
10256 || h->root.type == bfd_link_hash_defweak)
10257 && (h->root.u.def.section->flags
10258 & SEC_LINKER_CREATED) == 0
10259 && h->root.u.def.section->owner != NULL
10260 && (h->root.u.def.section->owner->flags
10261 & BFD_PLUGIN) != 0)
10262 {
10263 h->root.type = bfd_link_hash_undefined;
10264 h->root.u.undef.abfd = h->root.u.def.section->owner;
10265 }
10266
0f02bbd9
AM
10267 ps = NULL;
10268 if (h->root.type == bfd_link_hash_defined
10269 || h->root.type == bfd_link_hash_defweak)
10270 ps = &h->root.u.def.section;
10271
10272 sym_name = h->root.root.string;
10273 }
10274 else
10275 {
10276 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10277
10278 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10279 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10280 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10281 sym, *ps);
10282 }
c152c796 10283
c301e700 10284 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10285 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10286 {
10287 bfd_vma val;
10288 bfd_vma dot = (rel->r_offset
10289 + o->output_offset + o->output_section->vma);
10290#ifdef DEBUG
10291 printf ("Encountered a complex symbol!");
10292 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10293 input_bfd->filename, o->name,
10294 (long) (rel - internal_relocs));
0f02bbd9
AM
10295 printf (" symbol: idx %8.8lx, name %s\n",
10296 r_symndx, sym_name);
10297 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10298 (unsigned long) rel->r_info,
10299 (unsigned long) rel->r_offset);
10300#endif
8b127cbc 10301 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10302 isymbuf, locsymcount, s_type == STT_SRELC))
10303 return FALSE;
10304
10305 /* Symbol evaluated OK. Update to absolute value. */
10306 set_symbol_value (input_bfd, isymbuf, locsymcount,
10307 r_symndx, val);
10308 continue;
10309 }
10310
10311 if (action_discarded != -1 && ps != NULL)
10312 {
cdd3575c
AM
10313 /* Complain if the definition comes from a
10314 discarded section. */
dbaa2011 10315 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10316 {
cf35638d 10317 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10318 if (action_discarded & COMPLAIN)
8b127cbc 10319 (*flinfo->info->callbacks->einfo)
e1fffbe6 10320 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10321 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10322 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10323
87e5235d 10324 /* Try to do the best we can to support buggy old
e0ae6d6f 10325 versions of gcc. Pretend that the symbol is
87e5235d
AM
10326 really defined in the kept linkonce section.
10327 FIXME: This is quite broken. Modifying the
10328 symbol here means we will be changing all later
e0ae6d6f 10329 uses of the symbol, not just in this section. */
0f02bbd9 10330 if (action_discarded & PRETEND)
87e5235d 10331 {
01b3c8ab
L
10332 asection *kept;
10333
c0f00686 10334 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10335 flinfo->info);
01b3c8ab 10336 if (kept != NULL)
87e5235d
AM
10337 {
10338 *ps = kept;
10339 continue;
10340 }
10341 }
c152c796
AM
10342 }
10343 }
10344 }
10345
10346 /* Relocate the section by invoking a back end routine.
10347
10348 The back end routine is responsible for adjusting the
10349 section contents as necessary, and (if using Rela relocs
10350 and generating a relocatable output file) adjusting the
10351 reloc addend as necessary.
10352
10353 The back end routine does not have to worry about setting
10354 the reloc address or the reloc symbol index.
10355
10356 The back end routine is given a pointer to the swapped in
10357 internal symbols, and can access the hash table entries
10358 for the external symbols via elf_sym_hashes (input_bfd).
10359
10360 When generating relocatable output, the back end routine
10361 must handle STB_LOCAL/STT_SECTION symbols specially. The
10362 output symbol is going to be a section symbol
10363 corresponding to the output section, which will require
10364 the addend to be adjusted. */
10365
8b127cbc 10366 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10367 input_bfd, o, contents,
10368 internal_relocs,
10369 isymbuf,
8b127cbc 10370 flinfo->sections);
ece5ef60 10371 if (!ret)
c152c796
AM
10372 return FALSE;
10373
ece5ef60 10374 if (ret == 2
0e1862bb 10375 || bfd_link_relocatable (flinfo->info)
8b127cbc 10376 || flinfo->info->emitrelocations)
c152c796
AM
10377 {
10378 Elf_Internal_Rela *irela;
d4730f92 10379 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10380 bfd_vma last_offset;
10381 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10382 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10383 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10384 unsigned int next_erel;
c152c796 10385 bfd_boolean rela_normal;
d4730f92 10386 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10387
d4730f92
BS
10388 esdi = elf_section_data (o);
10389 esdo = elf_section_data (o->output_section);
10390 rela_normal = FALSE;
c152c796
AM
10391
10392 /* Adjust the reloc addresses and symbol indices. */
10393
10394 irela = internal_relocs;
10395 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10396 rel_hash = esdo->rel.hashes + esdo->rel.count;
10397 /* We start processing the REL relocs, if any. When we reach
10398 IRELAMID in the loop, we switch to the RELA relocs. */
10399 irelamid = irela;
10400 if (esdi->rel.hdr != NULL)
10401 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10402 * bed->s->int_rels_per_ext_rel);
eac338cf 10403 rel_hash_list = rel_hash;
d4730f92 10404 rela_hash_list = NULL;
c152c796 10405 last_offset = o->output_offset;
0e1862bb 10406 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10407 last_offset += o->output_section->vma;
10408 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10409 {
10410 unsigned long r_symndx;
10411 asection *sec;
10412 Elf_Internal_Sym sym;
10413
10414 if (next_erel == bed->s->int_rels_per_ext_rel)
10415 {
10416 rel_hash++;
10417 next_erel = 0;
10418 }
10419
d4730f92
BS
10420 if (irela == irelamid)
10421 {
10422 rel_hash = esdo->rela.hashes + esdo->rela.count;
10423 rela_hash_list = rel_hash;
10424 rela_normal = bed->rela_normal;
10425 }
10426
c152c796 10427 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10428 flinfo->info, o,
c152c796
AM
10429 irela->r_offset);
10430 if (irela->r_offset >= (bfd_vma) -2)
10431 {
10432 /* This is a reloc for a deleted entry or somesuch.
10433 Turn it into an R_*_NONE reloc, at the same
10434 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10435 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10436 being ordered. */
10437 irela->r_offset = last_offset;
10438 irela->r_info = 0;
10439 irela->r_addend = 0;
10440 continue;
10441 }
10442
10443 irela->r_offset += o->output_offset;
10444
10445 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10446 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10447 irela->r_offset += o->output_section->vma;
10448
10449 last_offset = irela->r_offset;
10450
10451 r_symndx = irela->r_info >> r_sym_shift;
10452 if (r_symndx == STN_UNDEF)
10453 continue;
10454
10455 if (r_symndx >= locsymcount
10456 || (elf_bad_symtab (input_bfd)
8b127cbc 10457 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10458 {
10459 struct elf_link_hash_entry *rh;
10460 unsigned long indx;
10461
10462 /* This is a reloc against a global symbol. We
10463 have not yet output all the local symbols, so
10464 we do not know the symbol index of any global
10465 symbol. We set the rel_hash entry for this
10466 reloc to point to the global hash table entry
10467 for this symbol. The symbol index is then
ee75fd95 10468 set at the end of bfd_elf_final_link. */
c152c796
AM
10469 indx = r_symndx - extsymoff;
10470 rh = elf_sym_hashes (input_bfd)[indx];
10471 while (rh->root.type == bfd_link_hash_indirect
10472 || rh->root.type == bfd_link_hash_warning)
10473 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10474
10475 /* Setting the index to -2 tells
10476 elf_link_output_extsym that this symbol is
10477 used by a reloc. */
10478 BFD_ASSERT (rh->indx < 0);
10479 rh->indx = -2;
10480
10481 *rel_hash = rh;
10482
10483 continue;
10484 }
10485
10486 /* This is a reloc against a local symbol. */
10487
10488 *rel_hash = NULL;
10489 sym = isymbuf[r_symndx];
8b127cbc 10490 sec = flinfo->sections[r_symndx];
c152c796
AM
10491 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10492 {
10493 /* I suppose the backend ought to fill in the
10494 section of any STT_SECTION symbol against a
6a8d1586 10495 processor specific section. */
cf35638d 10496 r_symndx = STN_UNDEF;
6a8d1586
AM
10497 if (bfd_is_abs_section (sec))
10498 ;
c152c796
AM
10499 else if (sec == NULL || sec->owner == NULL)
10500 {
10501 bfd_set_error (bfd_error_bad_value);
10502 return FALSE;
10503 }
10504 else
10505 {
6a8d1586
AM
10506 asection *osec = sec->output_section;
10507
10508 /* If we have discarded a section, the output
10509 section will be the absolute section. In
ab96bf03
AM
10510 case of discarded SEC_MERGE sections, use
10511 the kept section. relocate_section should
10512 have already handled discarded linkonce
10513 sections. */
6a8d1586
AM
10514 if (bfd_is_abs_section (osec)
10515 && sec->kept_section != NULL
10516 && sec->kept_section->output_section != NULL)
10517 {
10518 osec = sec->kept_section->output_section;
10519 irela->r_addend -= osec->vma;
10520 }
10521
10522 if (!bfd_is_abs_section (osec))
10523 {
10524 r_symndx = osec->target_index;
cf35638d 10525 if (r_symndx == STN_UNDEF)
74541ad4 10526 {
051d833a
AM
10527 irela->r_addend += osec->vma;
10528 osec = _bfd_nearby_section (output_bfd, osec,
10529 osec->vma);
10530 irela->r_addend -= osec->vma;
10531 r_symndx = osec->target_index;
74541ad4 10532 }
6a8d1586 10533 }
c152c796
AM
10534 }
10535
10536 /* Adjust the addend according to where the
10537 section winds up in the output section. */
10538 if (rela_normal)
10539 irela->r_addend += sec->output_offset;
10540 }
10541 else
10542 {
8b127cbc 10543 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10544 {
10545 unsigned long shlink;
10546 const char *name;
10547 asection *osec;
6e0b88f1 10548 long indx;
c152c796 10549
8b127cbc 10550 if (flinfo->info->strip == strip_all)
c152c796
AM
10551 {
10552 /* You can't do ld -r -s. */
10553 bfd_set_error (bfd_error_invalid_operation);
10554 return FALSE;
10555 }
10556
10557 /* This symbol was skipped earlier, but
10558 since it is needed by a reloc, we
10559 must output it now. */
10560 shlink = symtab_hdr->sh_link;
10561 name = (bfd_elf_string_from_elf_section
10562 (input_bfd, shlink, sym.st_name));
10563 if (name == NULL)
10564 return FALSE;
10565
10566 osec = sec->output_section;
10567 sym.st_shndx =
10568 _bfd_elf_section_from_bfd_section (output_bfd,
10569 osec);
10570 if (sym.st_shndx == SHN_BAD)
10571 return FALSE;
10572
10573 sym.st_value += sec->output_offset;
0e1862bb 10574 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10575 {
10576 sym.st_value += osec->vma;
10577 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10578 {
10579 /* STT_TLS symbols are relative to PT_TLS
10580 segment base. */
8b127cbc 10581 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10582 ->tls_sec != NULL);
8b127cbc 10583 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10584 ->tls_sec->vma);
10585 }
10586 }
10587
6e0b88f1 10588 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10589 ret = elf_link_output_symstrtab (flinfo, name,
10590 &sym, sec,
10591 NULL);
6e0b88f1 10592 if (ret == 0)
c152c796 10593 return FALSE;
6e0b88f1 10594 else if (ret == 1)
8b127cbc 10595 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10596 else
10597 abort ();
c152c796
AM
10598 }
10599
8b127cbc 10600 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10601 }
10602
10603 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10604 | (irela->r_info & r_type_mask));
10605 }
10606
10607 /* Swap out the relocs. */
d4730f92
BS
10608 input_rel_hdr = esdi->rel.hdr;
10609 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10610 {
d4730f92
BS
10611 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10612 input_rel_hdr,
10613 internal_relocs,
10614 rel_hash_list))
10615 return FALSE;
c152c796
AM
10616 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10617 * bed->s->int_rels_per_ext_rel);
eac338cf 10618 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10619 }
10620
10621 input_rela_hdr = esdi->rela.hdr;
10622 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10623 {
eac338cf 10624 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10625 input_rela_hdr,
eac338cf 10626 internal_relocs,
d4730f92 10627 rela_hash_list))
c152c796
AM
10628 return FALSE;
10629 }
10630 }
10631 }
10632
10633 /* Write out the modified section contents. */
10634 if (bed->elf_backend_write_section
8b127cbc 10635 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10636 contents))
c152c796
AM
10637 {
10638 /* Section written out. */
10639 }
10640 else switch (o->sec_info_type)
10641 {
dbaa2011 10642 case SEC_INFO_TYPE_STABS:
c152c796
AM
10643 if (! (_bfd_write_section_stabs
10644 (output_bfd,
8b127cbc 10645 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10646 o, &elf_section_data (o)->sec_info, contents)))
10647 return FALSE;
10648 break;
dbaa2011 10649 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10650 if (! _bfd_write_merged_section (output_bfd, o,
10651 elf_section_data (o)->sec_info))
10652 return FALSE;
10653 break;
dbaa2011 10654 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10655 {
8b127cbc 10656 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10657 o, contents))
10658 return FALSE;
10659 }
10660 break;
2f0c68f2
CM
10661 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10662 {
10663 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10664 flinfo->info,
10665 o, contents))
10666 return FALSE;
10667 }
10668 break;
c152c796
AM
10669 default:
10670 {
310fd250
L
10671 if (! (o->flags & SEC_EXCLUDE))
10672 {
10673 file_ptr offset = (file_ptr) o->output_offset;
10674 bfd_size_type todo = o->size;
37b01f6a
DG
10675
10676 offset *= bfd_octets_per_byte (output_bfd);
10677
310fd250
L
10678 if ((o->flags & SEC_ELF_REVERSE_COPY))
10679 {
10680 /* Reverse-copy input section to output. */
10681 do
10682 {
10683 todo -= address_size;
10684 if (! bfd_set_section_contents (output_bfd,
10685 o->output_section,
10686 contents + todo,
10687 offset,
10688 address_size))
10689 return FALSE;
10690 if (todo == 0)
10691 break;
10692 offset += address_size;
10693 }
10694 while (1);
10695 }
10696 else if (! bfd_set_section_contents (output_bfd,
10697 o->output_section,
10698 contents,
10699 offset, todo))
10700 return FALSE;
10701 }
c152c796
AM
10702 }
10703 break;
10704 }
10705 }
10706
10707 return TRUE;
10708}
10709
10710/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10711 requested by the linker, and does not come from any input file. This
c152c796
AM
10712 is used to build constructor and destructor tables when linking
10713 with -Ur. */
10714
10715static bfd_boolean
10716elf_reloc_link_order (bfd *output_bfd,
10717 struct bfd_link_info *info,
10718 asection *output_section,
10719 struct bfd_link_order *link_order)
10720{
10721 reloc_howto_type *howto;
10722 long indx;
10723 bfd_vma offset;
10724 bfd_vma addend;
d4730f92 10725 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10726 struct elf_link_hash_entry **rel_hash_ptr;
10727 Elf_Internal_Shdr *rel_hdr;
10728 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10729 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10730 bfd_byte *erel;
10731 unsigned int i;
d4730f92 10732 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10733
10734 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10735 if (howto == NULL)
10736 {
10737 bfd_set_error (bfd_error_bad_value);
10738 return FALSE;
10739 }
10740
10741 addend = link_order->u.reloc.p->addend;
10742
d4730f92
BS
10743 if (esdo->rel.hdr)
10744 reldata = &esdo->rel;
10745 else if (esdo->rela.hdr)
10746 reldata = &esdo->rela;
10747 else
10748 {
10749 reldata = NULL;
10750 BFD_ASSERT (0);
10751 }
10752
c152c796 10753 /* Figure out the symbol index. */
d4730f92 10754 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10755 if (link_order->type == bfd_section_reloc_link_order)
10756 {
10757 indx = link_order->u.reloc.p->u.section->target_index;
10758 BFD_ASSERT (indx != 0);
10759 *rel_hash_ptr = NULL;
10760 }
10761 else
10762 {
10763 struct elf_link_hash_entry *h;
10764
10765 /* Treat a reloc against a defined symbol as though it were
10766 actually against the section. */
10767 h = ((struct elf_link_hash_entry *)
10768 bfd_wrapped_link_hash_lookup (output_bfd, info,
10769 link_order->u.reloc.p->u.name,
10770 FALSE, FALSE, TRUE));
10771 if (h != NULL
10772 && (h->root.type == bfd_link_hash_defined
10773 || h->root.type == bfd_link_hash_defweak))
10774 {
10775 asection *section;
10776
10777 section = h->root.u.def.section;
10778 indx = section->output_section->target_index;
10779 *rel_hash_ptr = NULL;
10780 /* It seems that we ought to add the symbol value to the
10781 addend here, but in practice it has already been added
10782 because it was passed to constructor_callback. */
10783 addend += section->output_section->vma + section->output_offset;
10784 }
10785 else if (h != NULL)
10786 {
10787 /* Setting the index to -2 tells elf_link_output_extsym that
10788 this symbol is used by a reloc. */
10789 h->indx = -2;
10790 *rel_hash_ptr = h;
10791 indx = 0;
10792 }
10793 else
10794 {
1a72702b
AM
10795 (*info->callbacks->unattached_reloc)
10796 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
10797 indx = 0;
10798 }
10799 }
10800
10801 /* If this is an inplace reloc, we must write the addend into the
10802 object file. */
10803 if (howto->partial_inplace && addend != 0)
10804 {
10805 bfd_size_type size;
10806 bfd_reloc_status_type rstat;
10807 bfd_byte *buf;
10808 bfd_boolean ok;
10809 const char *sym_name;
10810
a50b1753
NC
10811 size = (bfd_size_type) bfd_get_reloc_size (howto);
10812 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10813 if (buf == NULL && size != 0)
c152c796
AM
10814 return FALSE;
10815 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10816 switch (rstat)
10817 {
10818 case bfd_reloc_ok:
10819 break;
10820
10821 default:
10822 case bfd_reloc_outofrange:
10823 abort ();
10824
10825 case bfd_reloc_overflow:
10826 if (link_order->type == bfd_section_reloc_link_order)
10827 sym_name = bfd_section_name (output_bfd,
10828 link_order->u.reloc.p->u.section);
10829 else
10830 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
10831 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10832 howto->name, addend, NULL, NULL,
10833 (bfd_vma) 0);
c152c796
AM
10834 break;
10835 }
37b01f6a 10836
c152c796 10837 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
10838 link_order->offset
10839 * bfd_octets_per_byte (output_bfd),
10840 size);
c152c796
AM
10841 free (buf);
10842 if (! ok)
10843 return FALSE;
10844 }
10845
10846 /* The address of a reloc is relative to the section in a
10847 relocatable file, and is a virtual address in an executable
10848 file. */
10849 offset = link_order->offset;
0e1862bb 10850 if (! bfd_link_relocatable (info))
c152c796
AM
10851 offset += output_section->vma;
10852
10853 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10854 {
10855 irel[i].r_offset = offset;
10856 irel[i].r_info = 0;
10857 irel[i].r_addend = 0;
10858 }
10859 if (bed->s->arch_size == 32)
10860 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10861 else
10862 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10863
d4730f92 10864 rel_hdr = reldata->hdr;
c152c796
AM
10865 erel = rel_hdr->contents;
10866 if (rel_hdr->sh_type == SHT_REL)
10867 {
d4730f92 10868 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10869 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10870 }
10871 else
10872 {
10873 irel[0].r_addend = addend;
d4730f92 10874 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10875 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10876 }
10877
d4730f92 10878 ++reldata->count;
c152c796
AM
10879
10880 return TRUE;
10881}
10882
0b52efa6
PB
10883
10884/* Get the output vma of the section pointed to by the sh_link field. */
10885
10886static bfd_vma
10887elf_get_linked_section_vma (struct bfd_link_order *p)
10888{
10889 Elf_Internal_Shdr **elf_shdrp;
10890 asection *s;
10891 int elfsec;
10892
10893 s = p->u.indirect.section;
10894 elf_shdrp = elf_elfsections (s->owner);
10895 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10896 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10897 /* PR 290:
10898 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10899 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10900 sh_info fields. Hence we could get the situation
10901 where elfsec is 0. */
10902 if (elfsec == 0)
10903 {
10904 const struct elf_backend_data *bed
10905 = get_elf_backend_data (s->owner);
10906 if (bed->link_order_error_handler)
d003868e
AM
10907 bed->link_order_error_handler
10908 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10909 return 0;
10910 }
10911 else
10912 {
10913 s = elf_shdrp[elfsec]->bfd_section;
10914 return s->output_section->vma + s->output_offset;
10915 }
0b52efa6
PB
10916}
10917
10918
10919/* Compare two sections based on the locations of the sections they are
10920 linked to. Used by elf_fixup_link_order. */
10921
10922static int
10923compare_link_order (const void * a, const void * b)
10924{
10925 bfd_vma apos;
10926 bfd_vma bpos;
10927
10928 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10929 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10930 if (apos < bpos)
10931 return -1;
10932 return apos > bpos;
10933}
10934
10935
10936/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10937 order as their linked sections. Returns false if this could not be done
10938 because an output section includes both ordered and unordered
10939 sections. Ideally we'd do this in the linker proper. */
10940
10941static bfd_boolean
10942elf_fixup_link_order (bfd *abfd, asection *o)
10943{
10944 int seen_linkorder;
10945 int seen_other;
10946 int n;
10947 struct bfd_link_order *p;
10948 bfd *sub;
10949 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10950 unsigned elfsec;
0b52efa6 10951 struct bfd_link_order **sections;
d33cdfe3 10952 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10953 bfd_vma offset;
3b36f7e6 10954
d33cdfe3
L
10955 other_sec = NULL;
10956 linkorder_sec = NULL;
0b52efa6
PB
10957 seen_other = 0;
10958 seen_linkorder = 0;
8423293d 10959 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10960 {
d33cdfe3 10961 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10962 {
10963 s = p->u.indirect.section;
d33cdfe3
L
10964 sub = s->owner;
10965 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10966 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10967 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10968 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10969 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10970 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10971 {
10972 seen_linkorder++;
10973 linkorder_sec = s;
10974 }
0b52efa6 10975 else
d33cdfe3
L
10976 {
10977 seen_other++;
10978 other_sec = s;
10979 }
0b52efa6
PB
10980 }
10981 else
10982 seen_other++;
d33cdfe3
L
10983
10984 if (seen_other && seen_linkorder)
10985 {
10986 if (other_sec && linkorder_sec)
10987 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10988 o, linkorder_sec,
10989 linkorder_sec->owner, other_sec,
10990 other_sec->owner);
10991 else
10992 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10993 o);
10994 bfd_set_error (bfd_error_bad_value);
10995 return FALSE;
10996 }
0b52efa6
PB
10997 }
10998
10999 if (!seen_linkorder)
11000 return TRUE;
11001
0b52efa6 11002 sections = (struct bfd_link_order **)
14b1c01e
AM
11003 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11004 if (sections == NULL)
11005 return FALSE;
0b52efa6 11006 seen_linkorder = 0;
3b36f7e6 11007
8423293d 11008 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11009 {
11010 sections[seen_linkorder++] = p;
11011 }
11012 /* Sort the input sections in the order of their linked section. */
11013 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11014 compare_link_order);
11015
11016 /* Change the offsets of the sections. */
11017 offset = 0;
11018 for (n = 0; n < seen_linkorder; n++)
11019 {
11020 s = sections[n]->u.indirect.section;
461686a3 11021 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11022 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11023 sections[n]->offset = offset;
11024 offset += sections[n]->size;
11025 }
11026
4dd07732 11027 free (sections);
0b52efa6
PB
11028 return TRUE;
11029}
11030
9f7c3e5e
AM
11031static void
11032elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11033{
11034 asection *o;
11035
11036 if (flinfo->symstrtab != NULL)
ef10c3ac 11037 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11038 if (flinfo->contents != NULL)
11039 free (flinfo->contents);
11040 if (flinfo->external_relocs != NULL)
11041 free (flinfo->external_relocs);
11042 if (flinfo->internal_relocs != NULL)
11043 free (flinfo->internal_relocs);
11044 if (flinfo->external_syms != NULL)
11045 free (flinfo->external_syms);
11046 if (flinfo->locsym_shndx != NULL)
11047 free (flinfo->locsym_shndx);
11048 if (flinfo->internal_syms != NULL)
11049 free (flinfo->internal_syms);
11050 if (flinfo->indices != NULL)
11051 free (flinfo->indices);
11052 if (flinfo->sections != NULL)
11053 free (flinfo->sections);
9f7c3e5e
AM
11054 if (flinfo->symshndxbuf != NULL)
11055 free (flinfo->symshndxbuf);
11056 for (o = obfd->sections; o != NULL; o = o->next)
11057 {
11058 struct bfd_elf_section_data *esdo = elf_section_data (o);
11059 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11060 free (esdo->rel.hashes);
11061 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11062 free (esdo->rela.hashes);
11063 }
11064}
0b52efa6 11065
c152c796
AM
11066/* Do the final step of an ELF link. */
11067
11068bfd_boolean
11069bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11070{
11071 bfd_boolean dynamic;
11072 bfd_boolean emit_relocs;
11073 bfd *dynobj;
8b127cbc 11074 struct elf_final_link_info flinfo;
91d6fa6a
NC
11075 asection *o;
11076 struct bfd_link_order *p;
11077 bfd *sub;
c152c796
AM
11078 bfd_size_type max_contents_size;
11079 bfd_size_type max_external_reloc_size;
11080 bfd_size_type max_internal_reloc_count;
11081 bfd_size_type max_sym_count;
11082 bfd_size_type max_sym_shndx_count;
c152c796
AM
11083 Elf_Internal_Sym elfsym;
11084 unsigned int i;
11085 Elf_Internal_Shdr *symtab_hdr;
11086 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11087 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11088 struct elf_outext_info eoinfo;
11089 bfd_boolean merged;
11090 size_t relativecount = 0;
11091 asection *reldyn = 0;
11092 bfd_size_type amt;
104d59d1
JM
11093 asection *attr_section = NULL;
11094 bfd_vma attr_size = 0;
11095 const char *std_attrs_section;
c152c796
AM
11096
11097 if (! is_elf_hash_table (info->hash))
11098 return FALSE;
11099
0e1862bb 11100 if (bfd_link_pic (info))
c152c796
AM
11101 abfd->flags |= DYNAMIC;
11102
11103 dynamic = elf_hash_table (info)->dynamic_sections_created;
11104 dynobj = elf_hash_table (info)->dynobj;
11105
0e1862bb 11106 emit_relocs = (bfd_link_relocatable (info)
a4676736 11107 || info->emitrelocations);
c152c796 11108
8b127cbc
AM
11109 flinfo.info = info;
11110 flinfo.output_bfd = abfd;
ef10c3ac 11111 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11112 if (flinfo.symstrtab == NULL)
c152c796
AM
11113 return FALSE;
11114
11115 if (! dynamic)
11116 {
8b127cbc
AM
11117 flinfo.hash_sec = NULL;
11118 flinfo.symver_sec = NULL;
c152c796
AM
11119 }
11120 else
11121 {
3d4d4302 11122 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11123 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11124 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11125 /* Note that it is OK if symver_sec is NULL. */
11126 }
11127
8b127cbc
AM
11128 flinfo.contents = NULL;
11129 flinfo.external_relocs = NULL;
11130 flinfo.internal_relocs = NULL;
11131 flinfo.external_syms = NULL;
11132 flinfo.locsym_shndx = NULL;
11133 flinfo.internal_syms = NULL;
11134 flinfo.indices = NULL;
11135 flinfo.sections = NULL;
8b127cbc 11136 flinfo.symshndxbuf = NULL;
ffbc01cc 11137 flinfo.filesym_count = 0;
c152c796 11138
104d59d1
JM
11139 /* The object attributes have been merged. Remove the input
11140 sections from the link, and set the contents of the output
11141 secton. */
11142 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11143 for (o = abfd->sections; o != NULL; o = o->next)
11144 {
11145 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11146 || strcmp (o->name, ".gnu.attributes") == 0)
11147 {
11148 for (p = o->map_head.link_order; p != NULL; p = p->next)
11149 {
11150 asection *input_section;
11151
11152 if (p->type != bfd_indirect_link_order)
11153 continue;
11154 input_section = p->u.indirect.section;
11155 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11156 elf_link_input_bfd ignores this section. */
11157 input_section->flags &= ~SEC_HAS_CONTENTS;
11158 }
a0c8462f 11159
104d59d1
JM
11160 attr_size = bfd_elf_obj_attr_size (abfd);
11161 if (attr_size)
11162 {
11163 bfd_set_section_size (abfd, o, attr_size);
11164 attr_section = o;
11165 /* Skip this section later on. */
11166 o->map_head.link_order = NULL;
11167 }
11168 else
11169 o->flags |= SEC_EXCLUDE;
11170 }
11171 }
11172
c152c796
AM
11173 /* Count up the number of relocations we will output for each output
11174 section, so that we know the sizes of the reloc sections. We
11175 also figure out some maximum sizes. */
11176 max_contents_size = 0;
11177 max_external_reloc_size = 0;
11178 max_internal_reloc_count = 0;
11179 max_sym_count = 0;
11180 max_sym_shndx_count = 0;
11181 merged = FALSE;
11182 for (o = abfd->sections; o != NULL; o = o->next)
11183 {
11184 struct bfd_elf_section_data *esdo = elf_section_data (o);
11185 o->reloc_count = 0;
11186
8423293d 11187 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11188 {
11189 unsigned int reloc_count = 0;
491d01d3 11190 unsigned int additional_reloc_count = 0;
c152c796 11191 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11192
11193 if (p->type == bfd_section_reloc_link_order
11194 || p->type == bfd_symbol_reloc_link_order)
11195 reloc_count = 1;
11196 else if (p->type == bfd_indirect_link_order)
11197 {
11198 asection *sec;
11199
11200 sec = p->u.indirect.section;
11201 esdi = elf_section_data (sec);
11202
11203 /* Mark all sections which are to be included in the
11204 link. This will normally be every section. We need
11205 to do this so that we can identify any sections which
11206 the linker has decided to not include. */
11207 sec->linker_mark = TRUE;
11208
11209 if (sec->flags & SEC_MERGE)
11210 merged = TRUE;
11211
aed64b35
L
11212 if (esdo->this_hdr.sh_type == SHT_REL
11213 || esdo->this_hdr.sh_type == SHT_RELA)
11214 /* Some backends use reloc_count in relocation sections
11215 to count particular types of relocs. Of course,
11216 reloc sections themselves can't have relocations. */
11217 reloc_count = 0;
0e1862bb 11218 else if (emit_relocs)
491d01d3
YU
11219 {
11220 reloc_count = sec->reloc_count;
11221 if (bed->elf_backend_count_additional_relocs)
11222 {
11223 int c;
11224 c = (*bed->elf_backend_count_additional_relocs) (sec);
11225 additional_reloc_count += c;
11226 }
11227 }
c152c796 11228 else if (bed->elf_backend_count_relocs)
58217f29 11229 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 11230
eea6121a
AM
11231 if (sec->rawsize > max_contents_size)
11232 max_contents_size = sec->rawsize;
11233 if (sec->size > max_contents_size)
11234 max_contents_size = sec->size;
c152c796
AM
11235
11236 /* We are interested in just local symbols, not all
11237 symbols. */
11238 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11239 && (sec->owner->flags & DYNAMIC) == 0)
11240 {
11241 size_t sym_count;
11242
11243 if (elf_bad_symtab (sec->owner))
11244 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11245 / bed->s->sizeof_sym);
11246 else
11247 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11248
11249 if (sym_count > max_sym_count)
11250 max_sym_count = sym_count;
11251
11252 if (sym_count > max_sym_shndx_count
6a40cf0c 11253 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11254 max_sym_shndx_count = sym_count;
11255
11256 if ((sec->flags & SEC_RELOC) != 0)
11257 {
d4730f92 11258 size_t ext_size = 0;
c152c796 11259
d4730f92
BS
11260 if (esdi->rel.hdr != NULL)
11261 ext_size = esdi->rel.hdr->sh_size;
11262 if (esdi->rela.hdr != NULL)
11263 ext_size += esdi->rela.hdr->sh_size;
7326c758 11264
c152c796
AM
11265 if (ext_size > max_external_reloc_size)
11266 max_external_reloc_size = ext_size;
11267 if (sec->reloc_count > max_internal_reloc_count)
11268 max_internal_reloc_count = sec->reloc_count;
11269 }
11270 }
11271 }
11272
11273 if (reloc_count == 0)
11274 continue;
11275
491d01d3 11276 reloc_count += additional_reloc_count;
c152c796
AM
11277 o->reloc_count += reloc_count;
11278
0e1862bb 11279 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11280 {
d4730f92 11281 if (esdi->rel.hdr)
491d01d3
YU
11282 {
11283 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11284 esdo->rel.count += additional_reloc_count;
11285 }
d4730f92 11286 if (esdi->rela.hdr)
491d01d3
YU
11287 {
11288 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11289 esdo->rela.count += additional_reloc_count;
11290 }
d4730f92
BS
11291 }
11292 else
11293 {
11294 if (o->use_rela_p)
11295 esdo->rela.count += reloc_count;
2c2b4ed4 11296 else
d4730f92 11297 esdo->rel.count += reloc_count;
c152c796 11298 }
c152c796
AM
11299 }
11300
11301 if (o->reloc_count > 0)
11302 o->flags |= SEC_RELOC;
11303 else
11304 {
11305 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11306 set it (this is probably a bug) and if it is set
11307 assign_section_numbers will create a reloc section. */
11308 o->flags &=~ SEC_RELOC;
11309 }
11310
11311 /* If the SEC_ALLOC flag is not set, force the section VMA to
11312 zero. This is done in elf_fake_sections as well, but forcing
11313 the VMA to 0 here will ensure that relocs against these
11314 sections are handled correctly. */
11315 if ((o->flags & SEC_ALLOC) == 0
11316 && ! o->user_set_vma)
11317 o->vma = 0;
11318 }
11319
0e1862bb 11320 if (! bfd_link_relocatable (info) && merged)
c152c796
AM
11321 elf_link_hash_traverse (elf_hash_table (info),
11322 _bfd_elf_link_sec_merge_syms, abfd);
11323
11324 /* Figure out the file positions for everything but the symbol table
11325 and the relocs. We set symcount to force assign_section_numbers
11326 to create a symbol table. */
8539e4e8 11327 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11328 BFD_ASSERT (! abfd->output_has_begun);
11329 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11330 goto error_return;
11331
ee75fd95 11332 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11333 for (o = abfd->sections; o != NULL; o = o->next)
11334 {
d4730f92 11335 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11336 if ((o->flags & SEC_RELOC) != 0)
11337 {
d4730f92
BS
11338 if (esdo->rel.hdr
11339 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11340 goto error_return;
11341
d4730f92
BS
11342 if (esdo->rela.hdr
11343 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11344 goto error_return;
11345 }
11346
11347 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11348 to count upwards while actually outputting the relocations. */
d4730f92
BS
11349 esdo->rel.count = 0;
11350 esdo->rela.count = 0;
0ce398f1
L
11351
11352 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11353 {
11354 /* Cache the section contents so that they can be compressed
11355 later. Use bfd_malloc since it will be freed by
11356 bfd_compress_section_contents. */
11357 unsigned char *contents = esdo->this_hdr.contents;
11358 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11359 abort ();
11360 contents
11361 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11362 if (contents == NULL)
11363 goto error_return;
11364 esdo->this_hdr.contents = contents;
11365 }
c152c796
AM
11366 }
11367
c152c796 11368 /* We have now assigned file positions for all the sections except
a485e98e
AM
11369 .symtab, .strtab, and non-loaded reloc sections. We start the
11370 .symtab section at the current file position, and write directly
11371 to it. We build the .strtab section in memory. */
c152c796
AM
11372 bfd_get_symcount (abfd) = 0;
11373 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11374 /* sh_name is set in prep_headers. */
11375 symtab_hdr->sh_type = SHT_SYMTAB;
11376 /* sh_flags, sh_addr and sh_size all start off zero. */
11377 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11378 /* sh_link is set in assign_section_numbers. */
11379 /* sh_info is set below. */
11380 /* sh_offset is set just below. */
72de5009 11381 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11382
ef10c3ac
L
11383 if (max_sym_count < 20)
11384 max_sym_count = 20;
11385 elf_hash_table (info)->strtabsize = max_sym_count;
11386 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11387 elf_hash_table (info)->strtab
11388 = (struct elf_sym_strtab *) bfd_malloc (amt);
11389 if (elf_hash_table (info)->strtab == NULL)
c152c796 11390 goto error_return;
ef10c3ac
L
11391 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11392 flinfo.symshndxbuf
11393 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11394 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11395
8539e4e8 11396 if (info->strip != strip_all || emit_relocs)
c152c796 11397 {
8539e4e8
AM
11398 file_ptr off = elf_next_file_pos (abfd);
11399
11400 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11401
11402 /* Note that at this point elf_next_file_pos (abfd) is
11403 incorrect. We do not yet know the size of the .symtab section.
11404 We correct next_file_pos below, after we do know the size. */
11405
11406 /* Start writing out the symbol table. The first symbol is always a
11407 dummy symbol. */
c152c796
AM
11408 elfsym.st_value = 0;
11409 elfsym.st_size = 0;
11410 elfsym.st_info = 0;
11411 elfsym.st_other = 0;
11412 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11413 elfsym.st_target_internal = 0;
ef10c3ac
L
11414 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11415 bfd_und_section_ptr, NULL) != 1)
c152c796 11416 goto error_return;
c152c796 11417
8539e4e8
AM
11418 /* Output a symbol for each section. We output these even if we are
11419 discarding local symbols, since they are used for relocs. These
11420 symbols have no names. We store the index of each one in the
11421 index field of the section, so that we can find it again when
11422 outputting relocs. */
11423
c152c796
AM
11424 elfsym.st_size = 0;
11425 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11426 elfsym.st_other = 0;
f0b5bb34 11427 elfsym.st_value = 0;
35fc36a8 11428 elfsym.st_target_internal = 0;
c152c796
AM
11429 for (i = 1; i < elf_numsections (abfd); i++)
11430 {
11431 o = bfd_section_from_elf_index (abfd, i);
11432 if (o != NULL)
f0b5bb34
AM
11433 {
11434 o->target_index = bfd_get_symcount (abfd);
11435 elfsym.st_shndx = i;
0e1862bb 11436 if (!bfd_link_relocatable (info))
f0b5bb34 11437 elfsym.st_value = o->vma;
ef10c3ac
L
11438 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11439 NULL) != 1)
f0b5bb34
AM
11440 goto error_return;
11441 }
c152c796
AM
11442 }
11443 }
11444
11445 /* Allocate some memory to hold information read in from the input
11446 files. */
11447 if (max_contents_size != 0)
11448 {
8b127cbc
AM
11449 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11450 if (flinfo.contents == NULL)
c152c796
AM
11451 goto error_return;
11452 }
11453
11454 if (max_external_reloc_size != 0)
11455 {
8b127cbc
AM
11456 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11457 if (flinfo.external_relocs == NULL)
c152c796
AM
11458 goto error_return;
11459 }
11460
11461 if (max_internal_reloc_count != 0)
11462 {
11463 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11464 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11465 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11466 if (flinfo.internal_relocs == NULL)
c152c796
AM
11467 goto error_return;
11468 }
11469
11470 if (max_sym_count != 0)
11471 {
11472 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11473 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11474 if (flinfo.external_syms == NULL)
c152c796
AM
11475 goto error_return;
11476
11477 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11478 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11479 if (flinfo.internal_syms == NULL)
c152c796
AM
11480 goto error_return;
11481
11482 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11483 flinfo.indices = (long int *) bfd_malloc (amt);
11484 if (flinfo.indices == NULL)
c152c796
AM
11485 goto error_return;
11486
11487 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11488 flinfo.sections = (asection **) bfd_malloc (amt);
11489 if (flinfo.sections == NULL)
c152c796
AM
11490 goto error_return;
11491 }
11492
11493 if (max_sym_shndx_count != 0)
11494 {
11495 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11496 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11497 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11498 goto error_return;
11499 }
11500
11501 if (elf_hash_table (info)->tls_sec)
11502 {
11503 bfd_vma base, end = 0;
11504 asection *sec;
11505
11506 for (sec = elf_hash_table (info)->tls_sec;
11507 sec && (sec->flags & SEC_THREAD_LOCAL);
11508 sec = sec->next)
11509 {
3a800eb9 11510 bfd_size_type size = sec->size;
c152c796 11511
3a800eb9
AM
11512 if (size == 0
11513 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11514 {
91d6fa6a
NC
11515 struct bfd_link_order *ord = sec->map_tail.link_order;
11516
11517 if (ord != NULL)
11518 size = ord->offset + ord->size;
c152c796
AM
11519 }
11520 end = sec->vma + size;
11521 }
11522 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
11523 /* Only align end of TLS section if static TLS doesn't have special
11524 alignment requirements. */
11525 if (bed->static_tls_alignment == 1)
11526 end = align_power (end,
11527 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
11528 elf_hash_table (info)->tls_size = end - base;
11529 }
11530
0b52efa6
PB
11531 /* Reorder SHF_LINK_ORDER sections. */
11532 for (o = abfd->sections; o != NULL; o = o->next)
11533 {
11534 if (!elf_fixup_link_order (abfd, o))
11535 return FALSE;
11536 }
11537
2f0c68f2
CM
11538 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11539 return FALSE;
11540
c152c796
AM
11541 /* Since ELF permits relocations to be against local symbols, we
11542 must have the local symbols available when we do the relocations.
11543 Since we would rather only read the local symbols once, and we
11544 would rather not keep them in memory, we handle all the
11545 relocations for a single input file at the same time.
11546
11547 Unfortunately, there is no way to know the total number of local
11548 symbols until we have seen all of them, and the local symbol
11549 indices precede the global symbol indices. This means that when
11550 we are generating relocatable output, and we see a reloc against
11551 a global symbol, we can not know the symbol index until we have
11552 finished examining all the local symbols to see which ones we are
11553 going to output. To deal with this, we keep the relocations in
11554 memory, and don't output them until the end of the link. This is
11555 an unfortunate waste of memory, but I don't see a good way around
11556 it. Fortunately, it only happens when performing a relocatable
11557 link, which is not the common case. FIXME: If keep_memory is set
11558 we could write the relocs out and then read them again; I don't
11559 know how bad the memory loss will be. */
11560
c72f2fb2 11561 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11562 sub->output_has_begun = FALSE;
11563 for (o = abfd->sections; o != NULL; o = o->next)
11564 {
8423293d 11565 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11566 {
11567 if (p->type == bfd_indirect_link_order
11568 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11569 == bfd_target_elf_flavour)
11570 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11571 {
11572 if (! sub->output_has_begun)
11573 {
8b127cbc 11574 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11575 goto error_return;
11576 sub->output_has_begun = TRUE;
11577 }
11578 }
11579 else if (p->type == bfd_section_reloc_link_order
11580 || p->type == bfd_symbol_reloc_link_order)
11581 {
11582 if (! elf_reloc_link_order (abfd, info, o, p))
11583 goto error_return;
11584 }
11585 else
11586 {
11587 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11588 {
11589 if (p->type == bfd_indirect_link_order
11590 && (bfd_get_flavour (sub)
11591 == bfd_target_elf_flavour)
11592 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11593 != bed->s->elfclass))
11594 {
11595 const char *iclass, *oclass;
11596
aebf9be7 11597 switch (bed->s->elfclass)
351f65ca 11598 {
aebf9be7
NC
11599 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11600 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11601 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11602 default: abort ();
351f65ca 11603 }
aebf9be7
NC
11604
11605 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11606 {
aebf9be7
NC
11607 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11608 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11609 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11610 default: abort ();
351f65ca
L
11611 }
11612
11613 bfd_set_error (bfd_error_wrong_format);
11614 (*_bfd_error_handler)
11615 (_("%B: file class %s incompatible with %s"),
11616 sub, iclass, oclass);
11617 }
11618
11619 goto error_return;
11620 }
c152c796
AM
11621 }
11622 }
11623 }
11624
c0f00686
L
11625 /* Free symbol buffer if needed. */
11626 if (!info->reduce_memory_overheads)
11627 {
c72f2fb2 11628 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11629 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11630 && elf_tdata (sub)->symbuf)
c0f00686
L
11631 {
11632 free (elf_tdata (sub)->symbuf);
11633 elf_tdata (sub)->symbuf = NULL;
11634 }
11635 }
11636
c152c796
AM
11637 /* Output any global symbols that got converted to local in a
11638 version script or due to symbol visibility. We do this in a
11639 separate step since ELF requires all local symbols to appear
11640 prior to any global symbols. FIXME: We should only do this if
11641 some global symbols were, in fact, converted to become local.
11642 FIXME: Will this work correctly with the Irix 5 linker? */
11643 eoinfo.failed = FALSE;
8b127cbc 11644 eoinfo.flinfo = &flinfo;
c152c796 11645 eoinfo.localsyms = TRUE;
34a79995 11646 eoinfo.file_sym_done = FALSE;
7686d77d 11647 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11648 if (eoinfo.failed)
11649 return FALSE;
11650
4e617b1e
PB
11651 /* If backend needs to output some local symbols not present in the hash
11652 table, do it now. */
8539e4e8
AM
11653 if (bed->elf_backend_output_arch_local_syms
11654 && (info->strip != strip_all || emit_relocs))
4e617b1e 11655 {
6e0b88f1 11656 typedef int (*out_sym_func)
4e617b1e
PB
11657 (void *, const char *, Elf_Internal_Sym *, asection *,
11658 struct elf_link_hash_entry *);
11659
11660 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11661 (abfd, info, &flinfo,
11662 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11663 return FALSE;
11664 }
11665
c152c796
AM
11666 /* That wrote out all the local symbols. Finish up the symbol table
11667 with the global symbols. Even if we want to strip everything we
11668 can, we still need to deal with those global symbols that got
11669 converted to local in a version script. */
11670
11671 /* The sh_info field records the index of the first non local symbol. */
11672 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11673
11674 if (dynamic
cae1fbbb
L
11675 && elf_hash_table (info)->dynsym != NULL
11676 && (elf_hash_table (info)->dynsym->output_section
11677 != bfd_abs_section_ptr))
c152c796
AM
11678 {
11679 Elf_Internal_Sym sym;
cae1fbbb 11680 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
c152c796
AM
11681 long last_local = 0;
11682
11683 /* Write out the section symbols for the output sections. */
0e1862bb
L
11684 if (bfd_link_pic (info)
11685 || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
11686 {
11687 asection *s;
11688
11689 sym.st_size = 0;
11690 sym.st_name = 0;
11691 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11692 sym.st_other = 0;
35fc36a8 11693 sym.st_target_internal = 0;
c152c796
AM
11694
11695 for (s = abfd->sections; s != NULL; s = s->next)
11696 {
11697 int indx;
11698 bfd_byte *dest;
11699 long dynindx;
11700
c152c796 11701 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11702 if (dynindx <= 0)
11703 continue;
11704 indx = elf_section_data (s)->this_idx;
c152c796
AM
11705 BFD_ASSERT (indx > 0);
11706 sym.st_shndx = indx;
c0d5a53d
L
11707 if (! check_dynsym (abfd, &sym))
11708 return FALSE;
c152c796
AM
11709 sym.st_value = s->vma;
11710 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
11711 if (last_local < dynindx)
11712 last_local = dynindx;
c152c796
AM
11713 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11714 }
c152c796
AM
11715 }
11716
11717 /* Write out the local dynsyms. */
11718 if (elf_hash_table (info)->dynlocal)
11719 {
11720 struct elf_link_local_dynamic_entry *e;
11721 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11722 {
11723 asection *s;
11724 bfd_byte *dest;
11725
935bd1e0 11726 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11727 Note that we saved a word of storage and overwrote
11728 the original st_name with the dynstr_index. */
11729 sym = e->isym;
935bd1e0 11730 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11731
cb33740c
AM
11732 s = bfd_section_from_elf_index (e->input_bfd,
11733 e->isym.st_shndx);
11734 if (s != NULL)
c152c796 11735 {
c152c796
AM
11736 sym.st_shndx =
11737 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11738 if (! check_dynsym (abfd, &sym))
11739 return FALSE;
c152c796
AM
11740 sym.st_value = (s->output_section->vma
11741 + s->output_offset
11742 + e->isym.st_value);
11743 }
11744
11745 if (last_local < e->dynindx)
11746 last_local = e->dynindx;
11747
11748 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11749 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11750 }
11751 }
11752
cae1fbbb 11753 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
c152c796
AM
11754 last_local + 1;
11755 }
11756
11757 /* We get the global symbols from the hash table. */
11758 eoinfo.failed = FALSE;
11759 eoinfo.localsyms = FALSE;
8b127cbc 11760 eoinfo.flinfo = &flinfo;
7686d77d 11761 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11762 if (eoinfo.failed)
11763 return FALSE;
11764
11765 /* If backend needs to output some symbols not present in the hash
11766 table, do it now. */
8539e4e8
AM
11767 if (bed->elf_backend_output_arch_syms
11768 && (info->strip != strip_all || emit_relocs))
c152c796 11769 {
6e0b88f1 11770 typedef int (*out_sym_func)
c152c796
AM
11771 (void *, const char *, Elf_Internal_Sym *, asection *,
11772 struct elf_link_hash_entry *);
11773
11774 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
11775 (abfd, info, &flinfo,
11776 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
11777 return FALSE;
11778 }
11779
ef10c3ac
L
11780 /* Finalize the .strtab section. */
11781 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11782
11783 /* Swap out the .strtab section. */
11784 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
11785 return FALSE;
11786
11787 /* Now we know the size of the symtab section. */
c152c796
AM
11788 if (bfd_get_symcount (abfd) > 0)
11789 {
ee3b52e9
L
11790 /* Finish up and write out the symbol string table (.strtab)
11791 section. */
11792 Elf_Internal_Shdr *symstrtab_hdr;
8539e4e8
AM
11793 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11794
6a40cf0c
NC
11795 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11796 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
8539e4e8
AM
11797 {
11798 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11799 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11800 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11801 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11802 symtab_shndx_hdr->sh_size = amt;
11803
11804 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11805 off, TRUE);
11806
11807 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11808 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11809 return FALSE;
11810 }
ee3b52e9
L
11811
11812 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11813 /* sh_name was set in prep_headers. */
11814 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 11815 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 11816 symstrtab_hdr->sh_addr = 0;
ef10c3ac 11817 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
11818 symstrtab_hdr->sh_entsize = 0;
11819 symstrtab_hdr->sh_link = 0;
11820 symstrtab_hdr->sh_info = 0;
11821 /* sh_offset is set just below. */
11822 symstrtab_hdr->sh_addralign = 1;
11823
11824 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11825 off, TRUE);
11826 elf_next_file_pos (abfd) = off;
11827
c152c796 11828 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 11829 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
11830 return FALSE;
11831 }
11832
11833 /* Adjust the relocs to have the correct symbol indices. */
11834 for (o = abfd->sections; o != NULL; o = o->next)
11835 {
d4730f92 11836 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 11837 bfd_boolean sort;
c152c796
AM
11838 if ((o->flags & SEC_RELOC) == 0)
11839 continue;
11840
28dbcedc 11841 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3
AM
11842 if (esdo->rel.hdr != NULL
11843 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11844 return FALSE;
11845 if (esdo->rela.hdr != NULL
11846 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11847 return FALSE;
c152c796
AM
11848
11849 /* Set the reloc_count field to 0 to prevent write_relocs from
11850 trying to swap the relocs out itself. */
11851 o->reloc_count = 0;
11852 }
11853
11854 if (dynamic && info->combreloc && dynobj != NULL)
11855 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11856
11857 /* If we are linking against a dynamic object, or generating a
11858 shared library, finish up the dynamic linking information. */
11859 if (dynamic)
11860 {
11861 bfd_byte *dyncon, *dynconend;
11862
11863 /* Fix up .dynamic entries. */
3d4d4302 11864 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
11865 BFD_ASSERT (o != NULL);
11866
11867 dyncon = o->contents;
eea6121a 11868 dynconend = o->contents + o->size;
c152c796
AM
11869 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11870 {
11871 Elf_Internal_Dyn dyn;
11872 const char *name;
11873 unsigned int type;
11874
11875 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11876
11877 switch (dyn.d_tag)
11878 {
11879 default:
11880 continue;
11881 case DT_NULL:
11882 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11883 {
11884 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11885 {
11886 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11887 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11888 default: continue;
11889 }
11890 dyn.d_un.d_val = relativecount;
11891 relativecount = 0;
11892 break;
11893 }
11894 continue;
11895
11896 case DT_INIT:
11897 name = info->init_function;
11898 goto get_sym;
11899 case DT_FINI:
11900 name = info->fini_function;
11901 get_sym:
11902 {
11903 struct elf_link_hash_entry *h;
11904
11905 h = elf_link_hash_lookup (elf_hash_table (info), name,
11906 FALSE, FALSE, TRUE);
11907 if (h != NULL
11908 && (h->root.type == bfd_link_hash_defined
11909 || h->root.type == bfd_link_hash_defweak))
11910 {
bef26483 11911 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11912 o = h->root.u.def.section;
11913 if (o->output_section != NULL)
bef26483 11914 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11915 + o->output_offset);
11916 else
11917 {
11918 /* The symbol is imported from another shared
11919 library and does not apply to this one. */
bef26483 11920 dyn.d_un.d_ptr = 0;
c152c796
AM
11921 }
11922 break;
11923 }
11924 }
11925 continue;
11926
11927 case DT_PREINIT_ARRAYSZ:
11928 name = ".preinit_array";
4ade44b7 11929 goto get_out_size;
c152c796
AM
11930 case DT_INIT_ARRAYSZ:
11931 name = ".init_array";
4ade44b7 11932 goto get_out_size;
c152c796
AM
11933 case DT_FINI_ARRAYSZ:
11934 name = ".fini_array";
4ade44b7 11935 get_out_size:
c152c796
AM
11936 o = bfd_get_section_by_name (abfd, name);
11937 if (o == NULL)
11938 {
11939 (*_bfd_error_handler)
4ade44b7 11940 (_("could not find section %s"), name);
c152c796
AM
11941 goto error_return;
11942 }
eea6121a 11943 if (o->size == 0)
c152c796
AM
11944 (*_bfd_error_handler)
11945 (_("warning: %s section has zero size"), name);
eea6121a 11946 dyn.d_un.d_val = o->size;
c152c796
AM
11947 break;
11948
11949 case DT_PREINIT_ARRAY:
11950 name = ".preinit_array";
4ade44b7 11951 goto get_out_vma;
c152c796
AM
11952 case DT_INIT_ARRAY:
11953 name = ".init_array";
4ade44b7 11954 goto get_out_vma;
c152c796
AM
11955 case DT_FINI_ARRAY:
11956 name = ".fini_array";
4ade44b7
AM
11957 get_out_vma:
11958 o = bfd_get_section_by_name (abfd, name);
11959 goto do_vma;
c152c796
AM
11960
11961 case DT_HASH:
11962 name = ".hash";
11963 goto get_vma;
fdc90cb4
JJ
11964 case DT_GNU_HASH:
11965 name = ".gnu.hash";
11966 goto get_vma;
c152c796
AM
11967 case DT_STRTAB:
11968 name = ".dynstr";
11969 goto get_vma;
11970 case DT_SYMTAB:
11971 name = ".dynsym";
11972 goto get_vma;
11973 case DT_VERDEF:
11974 name = ".gnu.version_d";
11975 goto get_vma;
11976 case DT_VERNEED:
11977 name = ".gnu.version_r";
11978 goto get_vma;
11979 case DT_VERSYM:
11980 name = ".gnu.version";
11981 get_vma:
4ade44b7
AM
11982 o = bfd_get_linker_section (dynobj, name);
11983 do_vma:
c152c796
AM
11984 if (o == NULL)
11985 {
11986 (*_bfd_error_handler)
4ade44b7 11987 (_("could not find section %s"), name);
c152c796
AM
11988 goto error_return;
11989 }
894891db
NC
11990 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11991 {
11992 (*_bfd_error_handler)
11993 (_("warning: section '%s' is being made into a note"), name);
11994 bfd_set_error (bfd_error_nonrepresentable_section);
11995 goto error_return;
11996 }
4ade44b7 11997 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
11998 break;
11999
12000 case DT_REL:
12001 case DT_RELA:
12002 case DT_RELSZ:
12003 case DT_RELASZ:
12004 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12005 type = SHT_REL;
12006 else
12007 type = SHT_RELA;
12008 dyn.d_un.d_val = 0;
bef26483 12009 dyn.d_un.d_ptr = 0;
c152c796
AM
12010 for (i = 1; i < elf_numsections (abfd); i++)
12011 {
12012 Elf_Internal_Shdr *hdr;
12013
12014 hdr = elf_elfsections (abfd)[i];
12015 if (hdr->sh_type == type
12016 && (hdr->sh_flags & SHF_ALLOC) != 0)
12017 {
12018 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12019 dyn.d_un.d_val += hdr->sh_size;
12020 else
12021 {
bef26483
AM
12022 if (dyn.d_un.d_ptr == 0
12023 || hdr->sh_addr < dyn.d_un.d_ptr)
12024 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
12025 }
12026 }
12027 }
12028 break;
12029 }
12030 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12031 }
12032 }
12033
12034 /* If we have created any dynamic sections, then output them. */
12035 if (dynobj != NULL)
12036 {
12037 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12038 goto error_return;
12039
943284cc 12040 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12041 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12042 || info->error_textrel)
3d4d4302 12043 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12044 {
12045 bfd_byte *dyncon, *dynconend;
12046
943284cc
DJ
12047 dyncon = o->contents;
12048 dynconend = o->contents + o->size;
12049 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12050 {
12051 Elf_Internal_Dyn dyn;
12052
12053 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12054
12055 if (dyn.d_tag == DT_TEXTREL)
12056 {
c192a133
AM
12057 if (info->error_textrel)
12058 info->callbacks->einfo
12059 (_("%P%X: read-only segment has dynamic relocations.\n"));
12060 else
12061 info->callbacks->einfo
12062 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12063 break;
12064 }
12065 }
12066 }
12067
c152c796
AM
12068 for (o = dynobj->sections; o != NULL; o = o->next)
12069 {
12070 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12071 || o->size == 0
c152c796
AM
12072 || o->output_section == bfd_abs_section_ptr)
12073 continue;
12074 if ((o->flags & SEC_LINKER_CREATED) == 0)
12075 {
12076 /* At this point, we are only interested in sections
12077 created by _bfd_elf_link_create_dynamic_sections. */
12078 continue;
12079 }
3722b82f
AM
12080 if (elf_hash_table (info)->stab_info.stabstr == o)
12081 continue;
eea6121a
AM
12082 if (elf_hash_table (info)->eh_info.hdr_sec == o)
12083 continue;
3d4d4302 12084 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12085 {
12086 if (! bfd_set_section_contents (abfd, o->output_section,
12087 o->contents,
37b01f6a
DG
12088 (file_ptr) o->output_offset
12089 * bfd_octets_per_byte (abfd),
eea6121a 12090 o->size))
c152c796
AM
12091 goto error_return;
12092 }
12093 else
12094 {
12095 /* The contents of the .dynstr section are actually in a
12096 stringtab. */
8539e4e8
AM
12097 file_ptr off;
12098
c152c796
AM
12099 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12100 if (bfd_seek (abfd, off, SEEK_SET) != 0
12101 || ! _bfd_elf_strtab_emit (abfd,
12102 elf_hash_table (info)->dynstr))
12103 goto error_return;
12104 }
12105 }
12106 }
12107
0e1862bb 12108 if (bfd_link_relocatable (info))
c152c796
AM
12109 {
12110 bfd_boolean failed = FALSE;
12111
12112 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12113 if (failed)
12114 goto error_return;
12115 }
12116
12117 /* If we have optimized stabs strings, output them. */
3722b82f 12118 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
12119 {
12120 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
12121 goto error_return;
12122 }
12123
9f7c3e5e
AM
12124 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12125 goto error_return;
c152c796 12126
9f7c3e5e 12127 elf_final_link_free (abfd, &flinfo);
c152c796 12128
12bd6957 12129 elf_linker (abfd) = TRUE;
c152c796 12130
104d59d1
JM
12131 if (attr_section)
12132 {
a50b1753 12133 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12134 if (contents == NULL)
d0f16d5e 12135 return FALSE; /* Bail out and fail. */
104d59d1
JM
12136 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12137 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12138 free (contents);
12139 }
12140
c152c796
AM
12141 return TRUE;
12142
12143 error_return:
9f7c3e5e 12144 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12145 return FALSE;
12146}
12147\f
5241d853
RS
12148/* Initialize COOKIE for input bfd ABFD. */
12149
12150static bfd_boolean
12151init_reloc_cookie (struct elf_reloc_cookie *cookie,
12152 struct bfd_link_info *info, bfd *abfd)
12153{
12154 Elf_Internal_Shdr *symtab_hdr;
12155 const struct elf_backend_data *bed;
12156
12157 bed = get_elf_backend_data (abfd);
12158 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12159
12160 cookie->abfd = abfd;
12161 cookie->sym_hashes = elf_sym_hashes (abfd);
12162 cookie->bad_symtab = elf_bad_symtab (abfd);
12163 if (cookie->bad_symtab)
12164 {
12165 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12166 cookie->extsymoff = 0;
12167 }
12168 else
12169 {
12170 cookie->locsymcount = symtab_hdr->sh_info;
12171 cookie->extsymoff = symtab_hdr->sh_info;
12172 }
12173
12174 if (bed->s->arch_size == 32)
12175 cookie->r_sym_shift = 8;
12176 else
12177 cookie->r_sym_shift = 32;
12178
12179 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12180 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12181 {
12182 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12183 cookie->locsymcount, 0,
12184 NULL, NULL, NULL);
12185 if (cookie->locsyms == NULL)
12186 {
12187 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12188 return FALSE;
12189 }
12190 if (info->keep_memory)
12191 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12192 }
12193 return TRUE;
12194}
12195
12196/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12197
12198static void
12199fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12200{
12201 Elf_Internal_Shdr *symtab_hdr;
12202
12203 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12204 if (cookie->locsyms != NULL
12205 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12206 free (cookie->locsyms);
12207}
12208
12209/* Initialize the relocation information in COOKIE for input section SEC
12210 of input bfd ABFD. */
12211
12212static bfd_boolean
12213init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12214 struct bfd_link_info *info, bfd *abfd,
12215 asection *sec)
12216{
12217 const struct elf_backend_data *bed;
12218
12219 if (sec->reloc_count == 0)
12220 {
12221 cookie->rels = NULL;
12222 cookie->relend = NULL;
12223 }
12224 else
12225 {
12226 bed = get_elf_backend_data (abfd);
12227
12228 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12229 info->keep_memory);
12230 if (cookie->rels == NULL)
12231 return FALSE;
12232 cookie->rel = cookie->rels;
12233 cookie->relend = (cookie->rels
12234 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12235 }
12236 cookie->rel = cookie->rels;
12237 return TRUE;
12238}
12239
12240/* Free the memory allocated by init_reloc_cookie_rels,
12241 if appropriate. */
12242
12243static void
12244fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12245 asection *sec)
12246{
12247 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12248 free (cookie->rels);
12249}
12250
12251/* Initialize the whole of COOKIE for input section SEC. */
12252
12253static bfd_boolean
12254init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12255 struct bfd_link_info *info,
12256 asection *sec)
12257{
12258 if (!init_reloc_cookie (cookie, info, sec->owner))
12259 goto error1;
12260 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12261 goto error2;
12262 return TRUE;
12263
12264 error2:
12265 fini_reloc_cookie (cookie, sec->owner);
12266 error1:
12267 return FALSE;
12268}
12269
12270/* Free the memory allocated by init_reloc_cookie_for_section,
12271 if appropriate. */
12272
12273static void
12274fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12275 asection *sec)
12276{
12277 fini_reloc_cookie_rels (cookie, sec);
12278 fini_reloc_cookie (cookie, sec->owner);
12279}
12280\f
c152c796
AM
12281/* Garbage collect unused sections. */
12282
07adf181
AM
12283/* Default gc_mark_hook. */
12284
12285asection *
12286_bfd_elf_gc_mark_hook (asection *sec,
12287 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12288 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12289 struct elf_link_hash_entry *h,
12290 Elf_Internal_Sym *sym)
12291{
12292 if (h != NULL)
12293 {
12294 switch (h->root.type)
12295 {
12296 case bfd_link_hash_defined:
12297 case bfd_link_hash_defweak:
12298 return h->root.u.def.section;
12299
12300 case bfd_link_hash_common:
12301 return h->root.u.c.p->section;
12302
12303 default:
12304 break;
12305 }
12306 }
12307 else
12308 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12309
12310 return NULL;
12311}
12312
a6a4679f
AM
12313/* For undefined __start_<name> and __stop_<name> symbols, return the
12314 first input section matching <name>. Return NULL otherwise. */
12315
12316asection *
12317_bfd_elf_is_start_stop (const struct bfd_link_info *info,
12318 struct elf_link_hash_entry *h)
12319{
12320 asection *s;
12321 const char *sec_name;
12322
12323 if (h->root.type != bfd_link_hash_undefined
12324 && h->root.type != bfd_link_hash_undefweak)
12325 return NULL;
12326
12327 s = h->root.u.undef.section;
12328 if (s != NULL)
12329 {
12330 if (s == (asection *) 0 - 1)
12331 return NULL;
12332 return s;
12333 }
12334
12335 sec_name = NULL;
12336 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12337 sec_name = h->root.root.string + 8;
12338 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12339 sec_name = h->root.root.string + 7;
12340
12341 if (sec_name != NULL && *sec_name != '\0')
12342 {
12343 bfd *i;
12344
12345 for (i = info->input_bfds; i != NULL; i = i->link.next)
12346 {
12347 s = bfd_get_section_by_name (i, sec_name);
12348 if (s != NULL)
12349 {
12350 h->root.u.undef.section = s;
12351 break;
12352 }
12353 }
12354 }
12355
12356 if (s == NULL)
12357 h->root.u.undef.section = (asection *) 0 - 1;
12358
12359 return s;
12360}
12361
5241d853
RS
12362/* COOKIE->rel describes a relocation against section SEC, which is
12363 a section we've decided to keep. Return the section that contains
12364 the relocation symbol, or NULL if no section contains it. */
12365
12366asection *
12367_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12368 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12369 struct elf_reloc_cookie *cookie,
12370 bfd_boolean *start_stop)
5241d853
RS
12371{
12372 unsigned long r_symndx;
12373 struct elf_link_hash_entry *h;
12374
12375 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12376 if (r_symndx == STN_UNDEF)
5241d853
RS
12377 return NULL;
12378
12379 if (r_symndx >= cookie->locsymcount
12380 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12381 {
12382 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12383 if (h == NULL)
12384 {
12385 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12386 sec->owner);
12387 return NULL;
12388 }
5241d853
RS
12389 while (h->root.type == bfd_link_hash_indirect
12390 || h->root.type == bfd_link_hash_warning)
12391 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12392 h->mark = 1;
4e6b54a6
AM
12393 /* If this symbol is weak and there is a non-weak definition, we
12394 keep the non-weak definition because many backends put
12395 dynamic reloc info on the non-weak definition for code
12396 handling copy relocs. */
12397 if (h->u.weakdef != NULL)
12398 h->u.weakdef->mark = 1;
1cce69b9 12399
a6a4679f 12400 if (start_stop != NULL)
1cce69b9
AM
12401 {
12402 /* To work around a glibc bug, mark all XXX input sections
12403 when there is an as yet undefined reference to __start_XXX
12404 or __stop_XXX symbols. The linker will later define such
12405 symbols for orphan input sections that have a name
12406 representable as a C identifier. */
a6a4679f 12407 asection *s = _bfd_elf_is_start_stop (info, h);
1cce69b9 12408
a6a4679f 12409 if (s != NULL)
1cce69b9 12410 {
a6a4679f
AM
12411 *start_stop = !s->gc_mark;
12412 return s;
1cce69b9
AM
12413 }
12414 }
12415
5241d853
RS
12416 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12417 }
12418
12419 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12420 &cookie->locsyms[r_symndx]);
12421}
12422
12423/* COOKIE->rel describes a relocation against section SEC, which is
12424 a section we've decided to keep. Mark the section that contains
9d0a14d3 12425 the relocation symbol. */
5241d853
RS
12426
12427bfd_boolean
12428_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12429 asection *sec,
12430 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12431 struct elf_reloc_cookie *cookie)
5241d853
RS
12432{
12433 asection *rsec;
1cce69b9 12434 bfd_boolean start_stop = FALSE;
5241d853 12435
1cce69b9
AM
12436 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12437 while (rsec != NULL)
5241d853 12438 {
1cce69b9
AM
12439 if (!rsec->gc_mark)
12440 {
12441 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12442 || (rsec->owner->flags & DYNAMIC) != 0)
12443 rsec->gc_mark = 1;
12444 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12445 return FALSE;
12446 }
12447 if (!start_stop)
12448 break;
199af150 12449 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12450 }
12451 return TRUE;
12452}
12453
07adf181
AM
12454/* The mark phase of garbage collection. For a given section, mark
12455 it and any sections in this section's group, and all the sections
12456 which define symbols to which it refers. */
12457
ccfa59ea
AM
12458bfd_boolean
12459_bfd_elf_gc_mark (struct bfd_link_info *info,
12460 asection *sec,
6a5bb875 12461 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12462{
12463 bfd_boolean ret;
9d0a14d3 12464 asection *group_sec, *eh_frame;
c152c796
AM
12465
12466 sec->gc_mark = 1;
12467
12468 /* Mark all the sections in the group. */
12469 group_sec = elf_section_data (sec)->next_in_group;
12470 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12471 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12472 return FALSE;
12473
12474 /* Look through the section relocs. */
12475 ret = TRUE;
9d0a14d3
RS
12476 eh_frame = elf_eh_frame_section (sec->owner);
12477 if ((sec->flags & SEC_RELOC) != 0
12478 && sec->reloc_count > 0
12479 && sec != eh_frame)
c152c796 12480 {
5241d853 12481 struct elf_reloc_cookie cookie;
c152c796 12482
5241d853
RS
12483 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12484 ret = FALSE;
c152c796 12485 else
c152c796 12486 {
5241d853 12487 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12488 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12489 {
12490 ret = FALSE;
12491 break;
12492 }
12493 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12494 }
12495 }
9d0a14d3
RS
12496
12497 if (ret && eh_frame && elf_fde_list (sec))
12498 {
12499 struct elf_reloc_cookie cookie;
12500
12501 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12502 ret = FALSE;
12503 else
12504 {
12505 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12506 gc_mark_hook, &cookie))
12507 ret = FALSE;
12508 fini_reloc_cookie_for_section (&cookie, eh_frame);
12509 }
12510 }
12511
2f0c68f2
CM
12512 eh_frame = elf_section_eh_frame_entry (sec);
12513 if (ret && eh_frame && !eh_frame->gc_mark)
12514 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12515 ret = FALSE;
12516
c152c796
AM
12517 return ret;
12518}
12519
3c758495
TG
12520/* Scan and mark sections in a special or debug section group. */
12521
12522static void
12523_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12524{
12525 /* Point to first section of section group. */
12526 asection *ssec;
12527 /* Used to iterate the section group. */
12528 asection *msec;
12529
12530 bfd_boolean is_special_grp = TRUE;
12531 bfd_boolean is_debug_grp = TRUE;
12532
12533 /* First scan to see if group contains any section other than debug
12534 and special section. */
12535 ssec = msec = elf_next_in_group (grp);
12536 do
12537 {
12538 if ((msec->flags & SEC_DEBUGGING) == 0)
12539 is_debug_grp = FALSE;
12540
12541 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12542 is_special_grp = FALSE;
12543
12544 msec = elf_next_in_group (msec);
12545 }
12546 while (msec != ssec);
12547
12548 /* If this is a pure debug section group or pure special section group,
12549 keep all sections in this group. */
12550 if (is_debug_grp || is_special_grp)
12551 {
12552 do
12553 {
12554 msec->gc_mark = 1;
12555 msec = elf_next_in_group (msec);
12556 }
12557 while (msec != ssec);
12558 }
12559}
12560
7f6ab9f8
AM
12561/* Keep debug and special sections. */
12562
12563bfd_boolean
12564_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12565 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12566{
12567 bfd *ibfd;
12568
c72f2fb2 12569 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12570 {
12571 asection *isec;
12572 bfd_boolean some_kept;
b40bf0a2 12573 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12574
12575 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12576 continue;
12577
b40bf0a2
NC
12578 /* Ensure all linker created sections are kept,
12579 see if any other section is already marked,
12580 and note if we have any fragmented debug sections. */
12581 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12582 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12583 {
12584 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12585 isec->gc_mark = 1;
12586 else if (isec->gc_mark)
12587 some_kept = TRUE;
b40bf0a2
NC
12588
12589 if (debug_frag_seen == FALSE
12590 && (isec->flags & SEC_DEBUGGING)
12591 && CONST_STRNEQ (isec->name, ".debug_line."))
12592 debug_frag_seen = TRUE;
7f6ab9f8
AM
12593 }
12594
12595 /* If no section in this file will be kept, then we can
b40bf0a2 12596 toss out the debug and special sections. */
7f6ab9f8
AM
12597 if (!some_kept)
12598 continue;
12599
12600 /* Keep debug and special sections like .comment when they are
3c758495
TG
12601 not part of a group. Also keep section groups that contain
12602 just debug sections or special sections. */
7f6ab9f8 12603 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12604 {
12605 if ((isec->flags & SEC_GROUP) != 0)
12606 _bfd_elf_gc_mark_debug_special_section_group (isec);
12607 else if (((isec->flags & SEC_DEBUGGING) != 0
12608 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12609 && elf_next_in_group (isec) == NULL)
12610 isec->gc_mark = 1;
12611 }
b40bf0a2
NC
12612
12613 if (! debug_frag_seen)
12614 continue;
12615
12616 /* Look for CODE sections which are going to be discarded,
12617 and find and discard any fragmented debug sections which
12618 are associated with that code section. */
12619 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12620 if ((isec->flags & SEC_CODE) != 0
12621 && isec->gc_mark == 0)
12622 {
12623 unsigned int ilen;
12624 asection *dsec;
12625
12626 ilen = strlen (isec->name);
12627
12628 /* Association is determined by the name of the debug section
12629 containing the name of the code section as a suffix. For
12630 example .debug_line.text.foo is a debug section associated
12631 with .text.foo. */
12632 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12633 {
12634 unsigned int dlen;
12635
12636 if (dsec->gc_mark == 0
12637 || (dsec->flags & SEC_DEBUGGING) == 0)
12638 continue;
12639
12640 dlen = strlen (dsec->name);
12641
12642 if (dlen > ilen
12643 && strncmp (dsec->name + (dlen - ilen),
12644 isec->name, ilen) == 0)
12645 {
12646 dsec->gc_mark = 0;
b40bf0a2
NC
12647 }
12648 }
12649 }
7f6ab9f8
AM
12650 }
12651 return TRUE;
12652}
12653
c152c796
AM
12654/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12655
c17d87de
NC
12656struct elf_gc_sweep_symbol_info
12657{
ccabcbe5
AM
12658 struct bfd_link_info *info;
12659 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12660 bfd_boolean);
12661};
12662
c152c796 12663static bfd_boolean
ccabcbe5 12664elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 12665{
1d5316ab
AM
12666 if (!h->mark
12667 && (((h->root.type == bfd_link_hash_defined
12668 || h->root.type == bfd_link_hash_defweak)
c4621b33 12669 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6673f753 12670 && h->root.u.def.section->gc_mark))
1d5316ab
AM
12671 || h->root.type == bfd_link_hash_undefined
12672 || h->root.type == bfd_link_hash_undefweak))
12673 {
12674 struct elf_gc_sweep_symbol_info *inf;
12675
12676 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 12677 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
12678 h->def_regular = 0;
12679 h->ref_regular = 0;
12680 h->ref_regular_nonweak = 0;
ccabcbe5 12681 }
c152c796
AM
12682
12683 return TRUE;
12684}
12685
12686/* The sweep phase of garbage collection. Remove all garbage sections. */
12687
12688typedef bfd_boolean (*gc_sweep_hook_fn)
12689 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12690
12691static bfd_boolean
ccabcbe5 12692elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12693{
12694 bfd *sub;
ccabcbe5
AM
12695 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12696 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12697 unsigned long section_sym_count;
12698 struct elf_gc_sweep_symbol_info sweep_info;
c152c796 12699
c72f2fb2 12700 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12701 {
12702 asection *o;
12703
b19a8f85
L
12704 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12705 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12706 continue;
12707
12708 for (o = sub->sections; o != NULL; o = o->next)
12709 {
a33dafc3
L
12710 /* When any section in a section group is kept, we keep all
12711 sections in the section group. If the first member of
12712 the section group is excluded, we will also exclude the
12713 group section. */
12714 if (o->flags & SEC_GROUP)
12715 {
12716 asection *first = elf_next_in_group (o);
12717 o->gc_mark = first->gc_mark;
12718 }
c152c796 12719
1e7eae0d 12720 if (o->gc_mark)
c152c796
AM
12721 continue;
12722
12723 /* Skip sweeping sections already excluded. */
12724 if (o->flags & SEC_EXCLUDE)
12725 continue;
12726
12727 /* Since this is early in the link process, it is simple
12728 to remove a section from the output. */
12729 o->flags |= SEC_EXCLUDE;
12730
c55fe096 12731 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
12732 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12733
c152c796
AM
12734 /* But we also have to update some of the relocation
12735 info we collected before. */
12736 if (gc_sweep_hook
e8aaee2a 12737 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12738 && o->reloc_count != 0
12739 && !((info->strip == strip_all || info->strip == strip_debugger)
12740 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12741 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12742 {
12743 Elf_Internal_Rela *internal_relocs;
12744 bfd_boolean r;
12745
12746 internal_relocs
12747 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12748 info->keep_memory);
12749 if (internal_relocs == NULL)
12750 return FALSE;
12751
12752 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12753
12754 if (elf_section_data (o)->relocs != internal_relocs)
12755 free (internal_relocs);
12756
12757 if (!r)
12758 return FALSE;
12759 }
12760 }
12761 }
12762
12763 /* Remove the symbols that were in the swept sections from the dynamic
12764 symbol table. GCFIXME: Anyone know how to get them out of the
12765 static symbol table as well? */
ccabcbe5
AM
12766 sweep_info.info = info;
12767 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12768 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12769 &sweep_info);
c152c796 12770
ccabcbe5 12771 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
12772 return TRUE;
12773}
12774
12775/* Propagate collected vtable information. This is called through
12776 elf_link_hash_traverse. */
12777
12778static bfd_boolean
12779elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12780{
c152c796 12781 /* Those that are not vtables. */
f6e332e6 12782 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12783 return TRUE;
12784
12785 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12786 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12787 return TRUE;
12788
12789 /* If we've already been done, exit. */
f6e332e6 12790 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12791 return TRUE;
12792
12793 /* Make sure the parent's table is up to date. */
f6e332e6 12794 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 12795
f6e332e6 12796 if (h->vtable->used == NULL)
c152c796
AM
12797 {
12798 /* None of this table's entries were referenced. Re-use the
12799 parent's table. */
f6e332e6
AM
12800 h->vtable->used = h->vtable->parent->vtable->used;
12801 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
12802 }
12803 else
12804 {
12805 size_t n;
12806 bfd_boolean *cu, *pu;
12807
12808 /* Or the parent's entries into ours. */
f6e332e6 12809 cu = h->vtable->used;
c152c796 12810 cu[-1] = TRUE;
f6e332e6 12811 pu = h->vtable->parent->vtable->used;
c152c796
AM
12812 if (pu != NULL)
12813 {
12814 const struct elf_backend_data *bed;
12815 unsigned int log_file_align;
12816
12817 bed = get_elf_backend_data (h->root.u.def.section->owner);
12818 log_file_align = bed->s->log_file_align;
f6e332e6 12819 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
12820 while (n--)
12821 {
12822 if (*pu)
12823 *cu = TRUE;
12824 pu++;
12825 cu++;
12826 }
12827 }
12828 }
12829
12830 return TRUE;
12831}
12832
12833static bfd_boolean
12834elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12835{
12836 asection *sec;
12837 bfd_vma hstart, hend;
12838 Elf_Internal_Rela *relstart, *relend, *rel;
12839 const struct elf_backend_data *bed;
12840 unsigned int log_file_align;
12841
c152c796
AM
12842 /* Take care of both those symbols that do not describe vtables as
12843 well as those that are not loaded. */
f6e332e6 12844 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12845 return TRUE;
12846
12847 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12848 || h->root.type == bfd_link_hash_defweak);
12849
12850 sec = h->root.u.def.section;
12851 hstart = h->root.u.def.value;
12852 hend = hstart + h->size;
12853
12854 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12855 if (!relstart)
12856 return *(bfd_boolean *) okp = FALSE;
12857 bed = get_elf_backend_data (sec->owner);
12858 log_file_align = bed->s->log_file_align;
12859
12860 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12861
12862 for (rel = relstart; rel < relend; ++rel)
12863 if (rel->r_offset >= hstart && rel->r_offset < hend)
12864 {
12865 /* If the entry is in use, do nothing. */
f6e332e6
AM
12866 if (h->vtable->used
12867 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
12868 {
12869 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 12870 if (h->vtable->used[entry])
c152c796
AM
12871 continue;
12872 }
12873 /* Otherwise, kill it. */
12874 rel->r_offset = rel->r_info = rel->r_addend = 0;
12875 }
12876
12877 return TRUE;
12878}
12879
87538722
AM
12880/* Mark sections containing dynamically referenced symbols. When
12881 building shared libraries, we must assume that any visible symbol is
12882 referenced. */
715df9b8 12883
64d03ab5
AM
12884bfd_boolean
12885bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 12886{
87538722 12887 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 12888 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 12889
715df9b8
EB
12890 if ((h->root.type == bfd_link_hash_defined
12891 || h->root.type == bfd_link_hash_defweak)
87538722 12892 && (h->ref_dynamic
c4621b33 12893 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 12894 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 12895 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 12896 && (!bfd_link_executable (info)
b407645f
AM
12897 || info->export_dynamic
12898 || (h->dynamic
12899 && d != NULL
12900 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 12901 && (h->versioned >= versioned
54e8959c
L
12902 || !bfd_hide_sym_by_version (info->version_info,
12903 h->root.root.string)))))
715df9b8
EB
12904 h->root.u.def.section->flags |= SEC_KEEP;
12905
12906 return TRUE;
12907}
3b36f7e6 12908
74f0fb50
AM
12909/* Keep all sections containing symbols undefined on the command-line,
12910 and the section containing the entry symbol. */
12911
12912void
12913_bfd_elf_gc_keep (struct bfd_link_info *info)
12914{
12915 struct bfd_sym_chain *sym;
12916
12917 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12918 {
12919 struct elf_link_hash_entry *h;
12920
12921 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12922 FALSE, FALSE, FALSE);
12923
12924 if (h != NULL
12925 && (h->root.type == bfd_link_hash_defined
12926 || h->root.type == bfd_link_hash_defweak)
12927 && !bfd_is_abs_section (h->root.u.def.section))
12928 h->root.u.def.section->flags |= SEC_KEEP;
12929 }
12930}
12931
2f0c68f2
CM
12932bfd_boolean
12933bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12934 struct bfd_link_info *info)
12935{
12936 bfd *ibfd = info->input_bfds;
12937
12938 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12939 {
12940 asection *sec;
12941 struct elf_reloc_cookie cookie;
12942
12943 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12944 continue;
12945
12946 if (!init_reloc_cookie (&cookie, info, ibfd))
12947 return FALSE;
12948
12949 for (sec = ibfd->sections; sec; sec = sec->next)
12950 {
12951 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12952 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12953 {
12954 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12955 fini_reloc_cookie_rels (&cookie, sec);
12956 }
12957 }
12958 }
12959 return TRUE;
12960}
12961
c152c796
AM
12962/* Do mark and sweep of unused sections. */
12963
12964bfd_boolean
12965bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12966{
12967 bfd_boolean ok = TRUE;
12968 bfd *sub;
6a5bb875 12969 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 12970 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 12971 struct elf_link_hash_table *htab;
c152c796 12972
64d03ab5 12973 if (!bed->can_gc_sections
715df9b8 12974 || !is_elf_hash_table (info->hash))
c152c796
AM
12975 {
12976 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12977 return TRUE;
12978 }
12979
74f0fb50 12980 bed->gc_keep (info);
da44f4e5 12981 htab = elf_hash_table (info);
74f0fb50 12982
9d0a14d3
RS
12983 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12984 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
12985 for (sub = info->input_bfds;
12986 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12987 sub = sub->link.next)
9d0a14d3
RS
12988 {
12989 asection *sec;
12990 struct elf_reloc_cookie cookie;
12991
12992 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 12993 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
12994 {
12995 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
12996 if (elf_section_data (sec)->sec_info
12997 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
12998 elf_eh_frame_section (sub) = sec;
12999 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13000 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13001 }
13002 }
9d0a14d3 13003
c152c796 13004 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13005 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13006 if (!ok)
13007 return FALSE;
13008
13009 /* Kill the vtable relocations that were not used. */
da44f4e5 13010 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13011 if (!ok)
13012 return FALSE;
13013
715df9b8 13014 /* Mark dynamically referenced symbols. */
da44f4e5
AM
13015 if (htab->dynamic_sections_created)
13016 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13017
715df9b8 13018 /* Grovel through relocs to find out who stays ... */
64d03ab5 13019 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13020 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13021 {
13022 asection *o;
13023
b19a8f85
L
13024 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13025 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13026 continue;
13027
7f6ab9f8
AM
13028 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13029 Also treat note sections as a root, if the section is not part
13030 of a group. */
c152c796 13031 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13032 if (!o->gc_mark
13033 && (o->flags & SEC_EXCLUDE) == 0
24007750 13034 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13035 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13036 && elf_next_in_group (o) == NULL )))
13037 {
13038 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13039 return FALSE;
13040 }
c152c796
AM
13041 }
13042
6a5bb875 13043 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13044 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13045
c152c796 13046 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13047 return elf_gc_sweep (abfd, info);
c152c796
AM
13048}
13049\f
13050/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13051
13052bfd_boolean
13053bfd_elf_gc_record_vtinherit (bfd *abfd,
13054 asection *sec,
13055 struct elf_link_hash_entry *h,
13056 bfd_vma offset)
13057{
13058 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13059 struct elf_link_hash_entry **search, *child;
ef53be89 13060 size_t extsymcount;
c152c796
AM
13061 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13062
13063 /* The sh_info field of the symtab header tells us where the
13064 external symbols start. We don't care about the local symbols at
13065 this point. */
13066 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13067 if (!elf_bad_symtab (abfd))
13068 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13069
13070 sym_hashes = elf_sym_hashes (abfd);
13071 sym_hashes_end = sym_hashes + extsymcount;
13072
13073 /* Hunt down the child symbol, which is in this section at the same
13074 offset as the relocation. */
13075 for (search = sym_hashes; search != sym_hashes_end; ++search)
13076 {
13077 if ((child = *search) != NULL
13078 && (child->root.type == bfd_link_hash_defined
13079 || child->root.type == bfd_link_hash_defweak)
13080 && child->root.u.def.section == sec
13081 && child->root.u.def.value == offset)
13082 goto win;
13083 }
13084
d003868e
AM
13085 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
13086 abfd, sec, (unsigned long) offset);
c152c796
AM
13087 bfd_set_error (bfd_error_invalid_operation);
13088 return FALSE;
13089
13090 win:
f6e332e6
AM
13091 if (!child->vtable)
13092 {
ca4be51c
AM
13093 child->vtable = ((struct elf_link_virtual_table_entry *)
13094 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
13095 if (!child->vtable)
13096 return FALSE;
13097 }
c152c796
AM
13098 if (!h)
13099 {
13100 /* This *should* only be the absolute section. It could potentially
13101 be that someone has defined a non-global vtable though, which
13102 would be bad. It isn't worth paging in the local symbols to be
13103 sure though; that case should simply be handled by the assembler. */
13104
f6e332e6 13105 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13106 }
13107 else
f6e332e6 13108 child->vtable->parent = h;
c152c796
AM
13109
13110 return TRUE;
13111}
13112
13113/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13114
13115bfd_boolean
13116bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13117 asection *sec ATTRIBUTE_UNUSED,
13118 struct elf_link_hash_entry *h,
13119 bfd_vma addend)
13120{
13121 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13122 unsigned int log_file_align = bed->s->log_file_align;
13123
f6e332e6
AM
13124 if (!h->vtable)
13125 {
ca4be51c
AM
13126 h->vtable = ((struct elf_link_virtual_table_entry *)
13127 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
13128 if (!h->vtable)
13129 return FALSE;
13130 }
13131
13132 if (addend >= h->vtable->size)
c152c796
AM
13133 {
13134 size_t size, bytes, file_align;
f6e332e6 13135 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
13136
13137 /* While the symbol is undefined, we have to be prepared to handle
13138 a zero size. */
13139 file_align = 1 << log_file_align;
13140 if (h->root.type == bfd_link_hash_undefined)
13141 size = addend + file_align;
13142 else
13143 {
13144 size = h->size;
13145 if (addend >= size)
13146 {
13147 /* Oops! We've got a reference past the defined end of
13148 the table. This is probably a bug -- shall we warn? */
13149 size = addend + file_align;
13150 }
13151 }
13152 size = (size + file_align - 1) & -file_align;
13153
13154 /* Allocate one extra entry for use as a "done" flag for the
13155 consolidation pass. */
13156 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13157
13158 if (ptr)
13159 {
a50b1753 13160 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13161
13162 if (ptr != NULL)
13163 {
13164 size_t oldbytes;
13165
f6e332e6 13166 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
13167 * sizeof (bfd_boolean));
13168 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13169 }
13170 }
13171 else
a50b1753 13172 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13173
13174 if (ptr == NULL)
13175 return FALSE;
13176
13177 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
13178 h->vtable->used = ptr + 1;
13179 h->vtable->size = size;
c152c796
AM
13180 }
13181
f6e332e6 13182 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13183
13184 return TRUE;
13185}
13186
ae17ab41
CM
13187/* Map an ELF section header flag to its corresponding string. */
13188typedef struct
13189{
13190 char *flag_name;
13191 flagword flag_value;
13192} elf_flags_to_name_table;
13193
13194static elf_flags_to_name_table elf_flags_to_names [] =
13195{
13196 { "SHF_WRITE", SHF_WRITE },
13197 { "SHF_ALLOC", SHF_ALLOC },
13198 { "SHF_EXECINSTR", SHF_EXECINSTR },
13199 { "SHF_MERGE", SHF_MERGE },
13200 { "SHF_STRINGS", SHF_STRINGS },
13201 { "SHF_INFO_LINK", SHF_INFO_LINK},
13202 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13203 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13204 { "SHF_GROUP", SHF_GROUP },
13205 { "SHF_TLS", SHF_TLS },
13206 { "SHF_MASKOS", SHF_MASKOS },
13207 { "SHF_EXCLUDE", SHF_EXCLUDE },
13208};
13209
b9c361e0
JL
13210/* Returns TRUE if the section is to be included, otherwise FALSE. */
13211bfd_boolean
ae17ab41 13212bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13213 struct flag_info *flaginfo,
b9c361e0 13214 asection *section)
ae17ab41 13215{
8b127cbc 13216 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13217
8b127cbc 13218 if (!flaginfo->flags_initialized)
ae17ab41 13219 {
8b127cbc
AM
13220 bfd *obfd = info->output_bfd;
13221 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13222 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13223 int with_hex = 0;
13224 int without_hex = 0;
13225
8b127cbc 13226 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13227 {
b9c361e0 13228 unsigned i;
8b127cbc 13229 flagword (*lookup) (char *);
ae17ab41 13230
8b127cbc
AM
13231 lookup = bed->elf_backend_lookup_section_flags_hook;
13232 if (lookup != NULL)
ae17ab41 13233 {
8b127cbc 13234 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13235
13236 if (hexval != 0)
13237 {
13238 if (tf->with == with_flags)
13239 with_hex |= hexval;
13240 else if (tf->with == without_flags)
13241 without_hex |= hexval;
13242 tf->valid = TRUE;
13243 continue;
13244 }
ae17ab41 13245 }
8b127cbc 13246 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13247 {
8b127cbc 13248 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13249 {
13250 if (tf->with == with_flags)
13251 with_hex |= elf_flags_to_names[i].flag_value;
13252 else if (tf->with == without_flags)
13253 without_hex |= elf_flags_to_names[i].flag_value;
13254 tf->valid = TRUE;
13255 break;
13256 }
13257 }
8b127cbc 13258 if (!tf->valid)
b9c361e0 13259 {
68ffbac6 13260 info->callbacks->einfo
8b127cbc 13261 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13262 return FALSE;
ae17ab41
CM
13263 }
13264 }
8b127cbc
AM
13265 flaginfo->flags_initialized = TRUE;
13266 flaginfo->only_with_flags |= with_hex;
13267 flaginfo->not_with_flags |= without_hex;
ae17ab41 13268 }
ae17ab41 13269
8b127cbc 13270 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13271 return FALSE;
13272
8b127cbc 13273 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13274 return FALSE;
13275
13276 return TRUE;
ae17ab41
CM
13277}
13278
c152c796
AM
13279struct alloc_got_off_arg {
13280 bfd_vma gotoff;
10455f89 13281 struct bfd_link_info *info;
c152c796
AM
13282};
13283
13284/* We need a special top-level link routine to convert got reference counts
13285 to real got offsets. */
13286
13287static bfd_boolean
13288elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13289{
a50b1753 13290 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13291 bfd *obfd = gofarg->info->output_bfd;
13292 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13293
c152c796
AM
13294 if (h->got.refcount > 0)
13295 {
13296 h->got.offset = gofarg->gotoff;
10455f89 13297 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13298 }
13299 else
13300 h->got.offset = (bfd_vma) -1;
13301
13302 return TRUE;
13303}
13304
13305/* And an accompanying bit to work out final got entry offsets once
13306 we're done. Should be called from final_link. */
13307
13308bfd_boolean
13309bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13310 struct bfd_link_info *info)
13311{
13312 bfd *i;
13313 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13314 bfd_vma gotoff;
c152c796
AM
13315 struct alloc_got_off_arg gofarg;
13316
10455f89
HPN
13317 BFD_ASSERT (abfd == info->output_bfd);
13318
c152c796
AM
13319 if (! is_elf_hash_table (info->hash))
13320 return FALSE;
13321
13322 /* The GOT offset is relative to the .got section, but the GOT header is
13323 put into the .got.plt section, if the backend uses it. */
13324 if (bed->want_got_plt)
13325 gotoff = 0;
13326 else
13327 gotoff = bed->got_header_size;
13328
13329 /* Do the local .got entries first. */
c72f2fb2 13330 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13331 {
13332 bfd_signed_vma *local_got;
ef53be89 13333 size_t j, locsymcount;
c152c796
AM
13334 Elf_Internal_Shdr *symtab_hdr;
13335
13336 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13337 continue;
13338
13339 local_got = elf_local_got_refcounts (i);
13340 if (!local_got)
13341 continue;
13342
13343 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13344 if (elf_bad_symtab (i))
13345 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13346 else
13347 locsymcount = symtab_hdr->sh_info;
13348
13349 for (j = 0; j < locsymcount; ++j)
13350 {
13351 if (local_got[j] > 0)
13352 {
13353 local_got[j] = gotoff;
10455f89 13354 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13355 }
13356 else
13357 local_got[j] = (bfd_vma) -1;
13358 }
13359 }
13360
13361 /* Then the global .got entries. .plt refcounts are handled by
13362 adjust_dynamic_symbol */
13363 gofarg.gotoff = gotoff;
10455f89 13364 gofarg.info = info;
c152c796
AM
13365 elf_link_hash_traverse (elf_hash_table (info),
13366 elf_gc_allocate_got_offsets,
13367 &gofarg);
13368 return TRUE;
13369}
13370
13371/* Many folk need no more in the way of final link than this, once
13372 got entry reference counting is enabled. */
13373
13374bfd_boolean
13375bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13376{
13377 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13378 return FALSE;
13379
13380 /* Invoke the regular ELF backend linker to do all the work. */
13381 return bfd_elf_final_link (abfd, info);
13382}
13383
13384bfd_boolean
13385bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13386{
a50b1753 13387 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13388
13389 if (rcookie->bad_symtab)
13390 rcookie->rel = rcookie->rels;
13391
13392 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13393 {
13394 unsigned long r_symndx;
13395
13396 if (! rcookie->bad_symtab)
13397 if (rcookie->rel->r_offset > offset)
13398 return FALSE;
13399 if (rcookie->rel->r_offset != offset)
13400 continue;
13401
13402 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13403 if (r_symndx == STN_UNDEF)
c152c796
AM
13404 return TRUE;
13405
13406 if (r_symndx >= rcookie->locsymcount
13407 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13408 {
13409 struct elf_link_hash_entry *h;
13410
13411 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13412
13413 while (h->root.type == bfd_link_hash_indirect
13414 || h->root.type == bfd_link_hash_warning)
13415 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13416
13417 if ((h->root.type == bfd_link_hash_defined
13418 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13419 && (h->root.u.def.section->owner != rcookie->abfd
13420 || h->root.u.def.section->kept_section != NULL
13421 || discarded_section (h->root.u.def.section)))
c152c796 13422 return TRUE;
c152c796
AM
13423 }
13424 else
13425 {
13426 /* It's not a relocation against a global symbol,
13427 but it could be a relocation against a local
13428 symbol for a discarded section. */
13429 asection *isec;
13430 Elf_Internal_Sym *isym;
13431
13432 /* Need to: get the symbol; get the section. */
13433 isym = &rcookie->locsyms[r_symndx];
cb33740c 13434 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13435 if (isec != NULL
13436 && (isec->kept_section != NULL
13437 || discarded_section (isec)))
cb33740c 13438 return TRUE;
c152c796
AM
13439 }
13440 return FALSE;
13441 }
13442 return FALSE;
13443}
13444
13445/* Discard unneeded references to discarded sections.
75938853
AM
13446 Returns -1 on error, 1 if any section's size was changed, 0 if
13447 nothing changed. This function assumes that the relocations are in
13448 sorted order, which is true for all known assemblers. */
c152c796 13449
75938853 13450int
c152c796
AM
13451bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13452{
13453 struct elf_reloc_cookie cookie;
18cd5bce 13454 asection *o;
c152c796 13455 bfd *abfd;
75938853 13456 int changed = 0;
c152c796
AM
13457
13458 if (info->traditional_format
13459 || !is_elf_hash_table (info->hash))
75938853 13460 return 0;
c152c796 13461
18cd5bce
AM
13462 o = bfd_get_section_by_name (output_bfd, ".stab");
13463 if (o != NULL)
c152c796 13464 {
18cd5bce 13465 asection *i;
c152c796 13466
18cd5bce 13467 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13468 {
18cd5bce
AM
13469 if (i->size == 0
13470 || i->reloc_count == 0
13471 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13472 continue;
c152c796 13473
18cd5bce
AM
13474 abfd = i->owner;
13475 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13476 continue;
c152c796 13477
18cd5bce 13478 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13479 return -1;
c152c796 13480
18cd5bce
AM
13481 if (_bfd_discard_section_stabs (abfd, i,
13482 elf_section_data (i)->sec_info,
5241d853
RS
13483 bfd_elf_reloc_symbol_deleted_p,
13484 &cookie))
75938853 13485 changed = 1;
18cd5bce
AM
13486
13487 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13488 }
18cd5bce
AM
13489 }
13490
2f0c68f2
CM
13491 o = NULL;
13492 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13493 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13494 if (o != NULL)
13495 {
13496 asection *i;
c152c796 13497
18cd5bce 13498 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13499 {
18cd5bce
AM
13500 if (i->size == 0)
13501 continue;
13502
13503 abfd = i->owner;
13504 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13505 continue;
13506
13507 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13508 return -1;
18cd5bce
AM
13509
13510 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13511 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13512 bfd_elf_reloc_symbol_deleted_p,
13513 &cookie))
75938853 13514 changed = 1;
18cd5bce
AM
13515
13516 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13517 }
18cd5bce 13518 }
c152c796 13519
18cd5bce
AM
13520 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13521 {
13522 const struct elf_backend_data *bed;
c152c796 13523
18cd5bce
AM
13524 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13525 continue;
13526
13527 bed = get_elf_backend_data (abfd);
13528
13529 if (bed->elf_backend_discard_info != NULL)
13530 {
13531 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13532 return -1;
18cd5bce
AM
13533
13534 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13535 changed = 1;
18cd5bce
AM
13536
13537 fini_reloc_cookie (&cookie, abfd);
13538 }
c152c796
AM
13539 }
13540
2f0c68f2
CM
13541 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13542 _bfd_elf_end_eh_frame_parsing (info);
13543
13544 if (info->eh_frame_hdr_type
0e1862bb 13545 && !bfd_link_relocatable (info)
c152c796 13546 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13547 changed = 1;
c152c796 13548
75938853 13549 return changed;
c152c796 13550}
082b7297 13551
43e1669b 13552bfd_boolean
0c511000 13553_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13554 asection *sec,
c0f00686 13555 struct bfd_link_info *info)
082b7297
L
13556{
13557 flagword flags;
c77ec726 13558 const char *name, *key;
082b7297
L
13559 struct bfd_section_already_linked *l;
13560 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13561
c77ec726
AM
13562 if (sec->output_section == bfd_abs_section_ptr)
13563 return FALSE;
0c511000 13564
c77ec726 13565 flags = sec->flags;
0c511000 13566
c77ec726
AM
13567 /* Return if it isn't a linkonce section. A comdat group section
13568 also has SEC_LINK_ONCE set. */
13569 if ((flags & SEC_LINK_ONCE) == 0)
13570 return FALSE;
0c511000 13571
c77ec726
AM
13572 /* Don't put group member sections on our list of already linked
13573 sections. They are handled as a group via their group section. */
13574 if (elf_sec_group (sec) != NULL)
13575 return FALSE;
0c511000 13576
c77ec726
AM
13577 /* For a SHT_GROUP section, use the group signature as the key. */
13578 name = sec->name;
13579 if ((flags & SEC_GROUP) != 0
13580 && elf_next_in_group (sec) != NULL
13581 && elf_group_name (elf_next_in_group (sec)) != NULL)
13582 key = elf_group_name (elf_next_in_group (sec));
13583 else
13584 {
13585 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13586 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13587 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13588 key++;
0c511000 13589 else
c77ec726
AM
13590 /* Must be a user linkonce section that doesn't follow gcc's
13591 naming convention. In this case we won't be matching
13592 single member groups. */
13593 key = name;
0c511000 13594 }
6d2cd210 13595
c77ec726 13596 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13597
13598 for (l = already_linked_list->entry; l != NULL; l = l->next)
13599 {
c2370991 13600 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13601 sections with a signature of <key> (<key> is some string),
13602 and linkonce sections named .gnu.linkonce.<type>.<key>.
13603 Match like sections. LTO plugin sections are an exception.
13604 They are always named .gnu.linkonce.t.<key> and match either
13605 type of section. */
13606 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13607 && ((flags & SEC_GROUP) != 0
13608 || strcmp (name, l->sec->name) == 0))
13609 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13610 {
13611 /* The section has already been linked. See if we should
6d2cd210 13612 issue a warning. */
c77ec726
AM
13613 if (!_bfd_handle_already_linked (sec, l, info))
13614 return FALSE;
082b7297 13615
c77ec726 13616 if (flags & SEC_GROUP)
3d7f7666 13617 {
c77ec726
AM
13618 asection *first = elf_next_in_group (sec);
13619 asection *s = first;
3d7f7666 13620
c77ec726 13621 while (s != NULL)
3d7f7666 13622 {
c77ec726
AM
13623 s->output_section = bfd_abs_section_ptr;
13624 /* Record which group discards it. */
13625 s->kept_section = l->sec;
13626 s = elf_next_in_group (s);
13627 /* These lists are circular. */
13628 if (s == first)
13629 break;
3d7f7666
L
13630 }
13631 }
082b7297 13632
43e1669b 13633 return TRUE;
082b7297
L
13634 }
13635 }
13636
c77ec726
AM
13637 /* A single member comdat group section may be discarded by a
13638 linkonce section and vice versa. */
13639 if ((flags & SEC_GROUP) != 0)
3d7f7666 13640 {
c77ec726 13641 asection *first = elf_next_in_group (sec);
c2370991 13642
c77ec726
AM
13643 if (first != NULL && elf_next_in_group (first) == first)
13644 /* Check this single member group against linkonce sections. */
13645 for (l = already_linked_list->entry; l != NULL; l = l->next)
13646 if ((l->sec->flags & SEC_GROUP) == 0
13647 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13648 {
13649 first->output_section = bfd_abs_section_ptr;
13650 first->kept_section = l->sec;
13651 sec->output_section = bfd_abs_section_ptr;
13652 break;
13653 }
13654 }
13655 else
13656 /* Check this linkonce section against single member groups. */
13657 for (l = already_linked_list->entry; l != NULL; l = l->next)
13658 if (l->sec->flags & SEC_GROUP)
6d2cd210 13659 {
c77ec726 13660 asection *first = elf_next_in_group (l->sec);
6d2cd210 13661
c77ec726
AM
13662 if (first != NULL
13663 && elf_next_in_group (first) == first
13664 && bfd_elf_match_symbols_in_sections (first, sec, info))
13665 {
13666 sec->output_section = bfd_abs_section_ptr;
13667 sec->kept_section = first;
13668 break;
13669 }
6d2cd210 13670 }
0c511000 13671
c77ec726
AM
13672 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13673 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13674 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13675 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13676 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13677 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13678 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13679 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13680 The reverse order cannot happen as there is never a bfd with only the
13681 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13682 matter as here were are looking only for cross-bfd sections. */
13683
13684 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13685 for (l = already_linked_list->entry; l != NULL; l = l->next)
13686 if ((l->sec->flags & SEC_GROUP) == 0
13687 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13688 {
13689 if (abfd != l->sec->owner)
13690 sec->output_section = bfd_abs_section_ptr;
13691 break;
13692 }
80c29487 13693
082b7297 13694 /* This is the first section with this name. Record it. */
c77ec726 13695 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13696 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13697 return sec->output_section == bfd_abs_section_ptr;
082b7297 13698}
81e1b023 13699
a4d8e49b
L
13700bfd_boolean
13701_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13702{
13703 return sym->st_shndx == SHN_COMMON;
13704}
13705
13706unsigned int
13707_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13708{
13709 return SHN_COMMON;
13710}
13711
13712asection *
13713_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13714{
13715 return bfd_com_section_ptr;
13716}
10455f89
HPN
13717
13718bfd_vma
13719_bfd_elf_default_got_elt_size (bfd *abfd,
13720 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13721 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13722 bfd *ibfd ATTRIBUTE_UNUSED,
13723 unsigned long symndx ATTRIBUTE_UNUSED)
13724{
13725 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13726 return bed->s->arch_size / 8;
13727}
83bac4b0
NC
13728
13729/* Routines to support the creation of dynamic relocs. */
13730
83bac4b0
NC
13731/* Returns the name of the dynamic reloc section associated with SEC. */
13732
13733static const char *
13734get_dynamic_reloc_section_name (bfd * abfd,
13735 asection * sec,
13736 bfd_boolean is_rela)
13737{
ddcf1fcf
BS
13738 char *name;
13739 const char *old_name = bfd_get_section_name (NULL, sec);
13740 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13741
ddcf1fcf 13742 if (old_name == NULL)
83bac4b0
NC
13743 return NULL;
13744
ddcf1fcf 13745 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13746 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13747
13748 return name;
13749}
13750
13751/* Returns the dynamic reloc section associated with SEC.
13752 If necessary compute the name of the dynamic reloc section based
13753 on SEC's name (looked up in ABFD's string table) and the setting
13754 of IS_RELA. */
13755
13756asection *
13757_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13758 asection * sec,
13759 bfd_boolean is_rela)
13760{
13761 asection * reloc_sec = elf_section_data (sec)->sreloc;
13762
13763 if (reloc_sec == NULL)
13764 {
13765 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13766
13767 if (name != NULL)
13768 {
3d4d4302 13769 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
13770
13771 if (reloc_sec != NULL)
13772 elf_section_data (sec)->sreloc = reloc_sec;
13773 }
13774 }
13775
13776 return reloc_sec;
13777}
13778
13779/* Returns the dynamic reloc section associated with SEC. If the
13780 section does not exist it is created and attached to the DYNOBJ
13781 bfd and stored in the SRELOC field of SEC's elf_section_data
13782 structure.
f8076f98 13783
83bac4b0
NC
13784 ALIGNMENT is the alignment for the newly created section and
13785 IS_RELA defines whether the name should be .rela.<SEC's name>
13786 or .rel.<SEC's name>. The section name is looked up in the
13787 string table associated with ABFD. */
13788
13789asection *
ca4be51c
AM
13790_bfd_elf_make_dynamic_reloc_section (asection *sec,
13791 bfd *dynobj,
13792 unsigned int alignment,
13793 bfd *abfd,
13794 bfd_boolean is_rela)
83bac4b0
NC
13795{
13796 asection * reloc_sec = elf_section_data (sec)->sreloc;
13797
13798 if (reloc_sec == NULL)
13799 {
13800 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13801
13802 if (name == NULL)
13803 return NULL;
13804
3d4d4302 13805 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
13806
13807 if (reloc_sec == NULL)
13808 {
3d4d4302
AM
13809 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13810 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
13811 if ((sec->flags & SEC_ALLOC) != 0)
13812 flags |= SEC_ALLOC | SEC_LOAD;
13813
3d4d4302 13814 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
13815 if (reloc_sec != NULL)
13816 {
8877b5e5
AM
13817 /* _bfd_elf_get_sec_type_attr chooses a section type by
13818 name. Override as it may be wrong, eg. for a user
13819 section named "auto" we'll get ".relauto" which is
13820 seen to be a .rela section. */
13821 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
13822 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13823 reloc_sec = NULL;
13824 }
13825 }
13826
13827 elf_section_data (sec)->sreloc = reloc_sec;
13828 }
13829
13830 return reloc_sec;
13831}
1338dd10 13832
bffebb6b
AM
13833/* Copy the ELF symbol type and other attributes for a linker script
13834 assignment from HSRC to HDEST. Generally this should be treated as
13835 if we found a strong non-dynamic definition for HDEST (except that
13836 ld ignores multiple definition errors). */
1338dd10 13837void
bffebb6b
AM
13838_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13839 struct bfd_link_hash_entry *hdest,
13840 struct bfd_link_hash_entry *hsrc)
1338dd10 13841{
bffebb6b
AM
13842 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13843 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13844 Elf_Internal_Sym isym;
1338dd10
PB
13845
13846 ehdest->type = ehsrc->type;
35fc36a8 13847 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
13848
13849 isym.st_other = ehsrc->other;
b8417128 13850 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 13851}
351f65ca
L
13852
13853/* Append a RELA relocation REL to section S in BFD. */
13854
13855void
13856elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13857{
13858 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13859 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13860 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13861 bed->s->swap_reloca_out (abfd, rel, loc);
13862}
13863
13864/* Append a REL relocation REL to section S in BFD. */
13865
13866void
13867elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13868{
13869 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13870 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13871 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 13872 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 13873}
This page took 2.958292 seconds and 4 git commands to generate.