MIPS/BFD: Don't stop processing on a cross-mode jump conversion error
[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"
7dc3990e
L
31#ifdef BFD_SUPPORTS_PLUGINS
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
3137#ifdef BFD_SUPPORTS_PLUGINS
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;
9056 if ((abfd->flags & DYNAMIC) == 0
e56f61be 9057 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9058 return FALSE;
9059 break;
9060
9061 case bfd_link_hash_defined:
9062 case bfd_link_hash_defweak:
9063 abfd = h->root.u.def.section->owner;
9064 break;
9065
9066 case bfd_link_hash_common:
9067 abfd = h->root.u.c.p->section->owner;
9068 break;
9069 }
9070 BFD_ASSERT (abfd != NULL);
9071
9072 for (loaded = elf_hash_table (info)->loaded;
9073 loaded != NULL;
9074 loaded = loaded->next)
9075 {
9076 bfd *input;
9077 Elf_Internal_Shdr *hdr;
ef53be89
AM
9078 size_t symcount;
9079 size_t extsymcount;
9080 size_t extsymoff;
c152c796
AM
9081 Elf_Internal_Shdr *versymhdr;
9082 Elf_Internal_Sym *isym;
9083 Elf_Internal_Sym *isymend;
9084 Elf_Internal_Sym *isymbuf;
9085 Elf_External_Versym *ever;
9086 Elf_External_Versym *extversym;
9087
9088 input = loaded->abfd;
9089
9090 /* We check each DSO for a possible hidden versioned definition. */
9091 if (input == abfd
9092 || (input->flags & DYNAMIC) == 0
9093 || elf_dynversym (input) == 0)
9094 continue;
9095
9096 hdr = &elf_tdata (input)->dynsymtab_hdr;
9097
9098 symcount = hdr->sh_size / bed->s->sizeof_sym;
9099 if (elf_bad_symtab (input))
9100 {
9101 extsymcount = symcount;
9102 extsymoff = 0;
9103 }
9104 else
9105 {
9106 extsymcount = symcount - hdr->sh_info;
9107 extsymoff = hdr->sh_info;
9108 }
9109
9110 if (extsymcount == 0)
9111 continue;
9112
9113 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9114 NULL, NULL, NULL);
9115 if (isymbuf == NULL)
9116 return FALSE;
9117
9118 /* Read in any version definitions. */
9119 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9120 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9121 if (extversym == NULL)
9122 goto error_ret;
9123
9124 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9125 || (bfd_bread (extversym, versymhdr->sh_size, input)
9126 != versymhdr->sh_size))
9127 {
9128 free (extversym);
9129 error_ret:
9130 free (isymbuf);
9131 return FALSE;
9132 }
9133
9134 ever = extversym + extsymoff;
9135 isymend = isymbuf + extsymcount;
9136 for (isym = isymbuf; isym < isymend; isym++, ever++)
9137 {
9138 const char *name;
9139 Elf_Internal_Versym iver;
9140 unsigned short version_index;
9141
9142 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9143 || isym->st_shndx == SHN_UNDEF)
9144 continue;
9145
9146 name = bfd_elf_string_from_elf_section (input,
9147 hdr->sh_link,
9148 isym->st_name);
9149 if (strcmp (name, h->root.root.string) != 0)
9150 continue;
9151
9152 _bfd_elf_swap_versym_in (input, ever, &iver);
9153
d023c380
L
9154 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9155 && !(h->def_regular
9156 && h->forced_local))
c152c796
AM
9157 {
9158 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9159 have provided a definition for the undefined sym unless
9160 it is defined in a non-shared object and forced local.
9161 */
c152c796
AM
9162 abort ();
9163 }
9164
9165 version_index = iver.vs_vers & VERSYM_VERSION;
9166 if (version_index == 1 || version_index == 2)
9167 {
9168 /* This is the base or first version. We can use it. */
9169 free (extversym);
9170 free (isymbuf);
9171 return TRUE;
9172 }
9173 }
9174
9175 free (extversym);
9176 free (isymbuf);
9177 }
9178
9179 return FALSE;
9180}
9181
b8871f35
L
9182/* Convert ELF common symbol TYPE. */
9183
9184static int
9185elf_link_convert_common_type (struct bfd_link_info *info, int type)
9186{
9187 /* Commom symbol can only appear in relocatable link. */
9188 if (!bfd_link_relocatable (info))
9189 abort ();
9190 switch (info->elf_stt_common)
9191 {
9192 case unchanged:
9193 break;
9194 case elf_stt_common:
9195 type = STT_COMMON;
9196 break;
9197 case no_elf_stt_common:
9198 type = STT_OBJECT;
9199 break;
9200 }
9201 return type;
9202}
9203
c152c796
AM
9204/* Add an external symbol to the symbol table. This is called from
9205 the hash table traversal routine. When generating a shared object,
9206 we go through the symbol table twice. The first time we output
9207 anything that might have been forced to local scope in a version
9208 script. The second time we output the symbols that are still
9209 global symbols. */
9210
9211static bfd_boolean
7686d77d 9212elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9213{
7686d77d 9214 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9215 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9216 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9217 bfd_boolean strip;
9218 Elf_Internal_Sym sym;
9219 asection *input_sec;
9220 const struct elf_backend_data *bed;
6e0b88f1
AM
9221 long indx;
9222 int ret;
b8871f35 9223 unsigned int type;
6e33951e
L
9224 /* A symbol is bound locally if it is forced local or it is locally
9225 defined, hidden versioned, not referenced by shared library and
9226 not exported when linking executable. */
9227 bfd_boolean local_bind = (h->forced_local
0e1862bb 9228 || (bfd_link_executable (flinfo->info)
6e33951e
L
9229 && !flinfo->info->export_dynamic
9230 && !h->dynamic
9231 && !h->ref_dynamic
9232 && h->def_regular
422f1182 9233 && h->versioned == versioned_hidden));
c152c796
AM
9234
9235 if (h->root.type == bfd_link_hash_warning)
9236 {
9237 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9238 if (h->root.type == bfd_link_hash_new)
9239 return TRUE;
9240 }
9241
9242 /* Decide whether to output this symbol in this pass. */
9243 if (eoinfo->localsyms)
9244 {
6e33951e 9245 if (!local_bind)
c152c796
AM
9246 return TRUE;
9247 }
9248 else
9249 {
6e33951e 9250 if (local_bind)
c152c796
AM
9251 return TRUE;
9252 }
9253
8b127cbc 9254 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9255
12ac1cf5 9256 if (h->root.type == bfd_link_hash_undefined)
c152c796 9257 {
12ac1cf5
NC
9258 /* If we have an undefined symbol reference here then it must have
9259 come from a shared library that is being linked in. (Undefined
98da7939
L
9260 references in regular files have already been handled unless
9261 they are in unreferenced sections which are removed by garbage
9262 collection). */
12ac1cf5
NC
9263 bfd_boolean ignore_undef = FALSE;
9264
9265 /* Some symbols may be special in that the fact that they're
9266 undefined can be safely ignored - let backend determine that. */
9267 if (bed->elf_backend_ignore_undef_symbol)
9268 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9269
9270 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9271 if (!ignore_undef
12ac1cf5 9272 && h->ref_dynamic
8b127cbc
AM
9273 && (!h->ref_regular || flinfo->info->gc_sections)
9274 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9275 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9276 (*flinfo->info->callbacks->undefined_symbol)
9277 (flinfo->info, h->root.root.string,
9278 h->ref_regular ? NULL : h->root.u.undef.abfd,
9279 NULL, 0,
9280 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9281
9282 /* Strip a global symbol defined in a discarded section. */
9283 if (h->indx == -3)
9284 return TRUE;
c152c796
AM
9285 }
9286
9287 /* We should also warn if a forced local symbol is referenced from
9288 shared libraries. */
0e1862bb 9289 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9290 && h->forced_local
9291 && h->ref_dynamic
371a5866 9292 && h->def_regular
f5385ebf 9293 && !h->dynamic_def
ee659f1f 9294 && h->ref_dynamic_nonweak
8b127cbc 9295 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9296 {
17d078c5
AM
9297 bfd *def_bfd;
9298 const char *msg;
90c984fc
L
9299 struct elf_link_hash_entry *hi = h;
9300
9301 /* Check indirect symbol. */
9302 while (hi->root.type == bfd_link_hash_indirect)
9303 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9304
9305 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9306 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9307 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9308 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9309 else
9310 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9311 def_bfd = flinfo->output_bfd;
90c984fc
L
9312 if (hi->root.u.def.section != bfd_abs_section_ptr)
9313 def_bfd = hi->root.u.def.section->owner;
8b127cbc 9314 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
17d078c5
AM
9315 h->root.root.string);
9316 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9317 eoinfo->failed = TRUE;
9318 return FALSE;
9319 }
9320
9321 /* We don't want to output symbols that have never been mentioned by
9322 a regular file, or that we have been told to strip. However, if
9323 h->indx is set to -2, the symbol is used by a reloc and we must
9324 output it. */
d983c8c5 9325 strip = FALSE;
c152c796 9326 if (h->indx == -2)
d983c8c5 9327 ;
f5385ebf 9328 else if ((h->def_dynamic
77cfaee6
AM
9329 || h->ref_dynamic
9330 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9331 && !h->def_regular
9332 && !h->ref_regular)
c152c796 9333 strip = TRUE;
8b127cbc 9334 else if (flinfo->info->strip == strip_all)
c152c796 9335 strip = TRUE;
8b127cbc
AM
9336 else if (flinfo->info->strip == strip_some
9337 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9338 h->root.root.string, FALSE, FALSE) == NULL)
9339 strip = TRUE;
d56d55e7
AM
9340 else if ((h->root.type == bfd_link_hash_defined
9341 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9342 && ((flinfo->info->strip_discarded
dbaa2011 9343 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9344 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9345 && h->root.u.def.section->owner != NULL
d56d55e7 9346 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9347 strip = TRUE;
9e2278f5
AM
9348 else if ((h->root.type == bfd_link_hash_undefined
9349 || h->root.type == bfd_link_hash_undefweak)
9350 && h->root.u.undef.abfd != NULL
9351 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9352 strip = TRUE;
c152c796 9353
b8871f35
L
9354 type = h->type;
9355
c152c796 9356 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9357 nothing else to do. However, if it is a forced local symbol or
9358 an ifunc symbol we need to give the backend finish_dynamic_symbol
9359 function a chance to make it dynamic. */
c152c796
AM
9360 if (strip
9361 && h->dynindx == -1
b8871f35 9362 && type != STT_GNU_IFUNC
f5385ebf 9363 && !h->forced_local)
c152c796
AM
9364 return TRUE;
9365
9366 sym.st_value = 0;
9367 sym.st_size = h->size;
9368 sym.st_other = h->other;
c152c796
AM
9369 switch (h->root.type)
9370 {
9371 default:
9372 case bfd_link_hash_new:
9373 case bfd_link_hash_warning:
9374 abort ();
9375 return FALSE;
9376
9377 case bfd_link_hash_undefined:
9378 case bfd_link_hash_undefweak:
9379 input_sec = bfd_und_section_ptr;
9380 sym.st_shndx = SHN_UNDEF;
9381 break;
9382
9383 case bfd_link_hash_defined:
9384 case bfd_link_hash_defweak:
9385 {
9386 input_sec = h->root.u.def.section;
9387 if (input_sec->output_section != NULL)
9388 {
9389 sym.st_shndx =
8b127cbc 9390 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9391 input_sec->output_section);
9392 if (sym.st_shndx == SHN_BAD)
9393 {
9394 (*_bfd_error_handler)
d003868e 9395 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9396 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9397 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9398 eoinfo->failed = TRUE;
9399 return FALSE;
9400 }
9401
9402 /* ELF symbols in relocatable files are section relative,
9403 but in nonrelocatable files they are virtual
9404 addresses. */
9405 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9406 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9407 {
9408 sym.st_value += input_sec->output_section->vma;
9409 if (h->type == STT_TLS)
9410 {
8b127cbc 9411 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9412 if (tls_sec != NULL)
9413 sym.st_value -= tls_sec->vma;
c152c796
AM
9414 }
9415 }
9416 }
9417 else
9418 {
9419 BFD_ASSERT (input_sec->owner == NULL
9420 || (input_sec->owner->flags & DYNAMIC) != 0);
9421 sym.st_shndx = SHN_UNDEF;
9422 input_sec = bfd_und_section_ptr;
9423 }
9424 }
9425 break;
9426
9427 case bfd_link_hash_common:
9428 input_sec = h->root.u.c.p->section;
a4d8e49b 9429 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9430 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9431 break;
9432
9433 case bfd_link_hash_indirect:
9434 /* These symbols are created by symbol versioning. They point
9435 to the decorated version of the name. For example, if the
9436 symbol foo@@GNU_1.2 is the default, which should be used when
9437 foo is used with no version, then we add an indirect symbol
9438 foo which points to foo@@GNU_1.2. We ignore these symbols,
9439 since the indirected symbol is already in the hash table. */
9440 return TRUE;
9441 }
9442
b8871f35
L
9443 if (type == STT_COMMON || type == STT_OBJECT)
9444 switch (h->root.type)
9445 {
9446 case bfd_link_hash_common:
9447 type = elf_link_convert_common_type (flinfo->info, type);
9448 break;
9449 case bfd_link_hash_defined:
9450 case bfd_link_hash_defweak:
9451 if (bed->common_definition (&sym))
9452 type = elf_link_convert_common_type (flinfo->info, type);
9453 else
9454 type = STT_OBJECT;
9455 break;
9456 case bfd_link_hash_undefined:
9457 case bfd_link_hash_undefweak:
9458 break;
9459 default:
9460 abort ();
9461 }
9462
9463 if (local_bind)
9464 {
9465 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9466 /* Turn off visibility on local symbol. */
9467 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9468 }
9469 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9470 else if (h->unique_global && h->def_regular)
9471 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9472 else if (h->root.type == bfd_link_hash_undefweak
9473 || h->root.type == bfd_link_hash_defweak)
9474 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9475 else
9476 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9477 sym.st_target_internal = h->target_internal;
9478
c152c796
AM
9479 /* Give the processor backend a chance to tweak the symbol value,
9480 and also to finish up anything that needs to be done for this
9481 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9482 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9483 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9484 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9485 && h->def_regular
0e1862bb 9486 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9487 || ((h->dynindx != -1
9488 || h->forced_local)
0e1862bb 9489 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9490 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9491 || h->root.type != bfd_link_hash_undefweak))
9492 || !h->forced_local)
8b127cbc 9493 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9494 {
9495 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9496 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9497 {
9498 eoinfo->failed = TRUE;
9499 return FALSE;
9500 }
9501 }
9502
9503 /* If we are marking the symbol as undefined, and there are no
9504 non-weak references to this symbol from a regular object, then
9505 mark the symbol as weak undefined; if there are non-weak
9506 references, mark the symbol as strong. We can't do this earlier,
9507 because it might not be marked as undefined until the
9508 finish_dynamic_symbol routine gets through with it. */
9509 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9510 && h->ref_regular
c152c796
AM
9511 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9512 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9513 {
9514 int bindtype;
b8871f35 9515 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9516
9517 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9518 if (type == STT_GNU_IFUNC)
9519 type = STT_FUNC;
c152c796 9520
f5385ebf 9521 if (h->ref_regular_nonweak)
c152c796
AM
9522 bindtype = STB_GLOBAL;
9523 else
9524 bindtype = STB_WEAK;
2955ec4c 9525 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9526 }
9527
bda987c2
CD
9528 /* If this is a symbol defined in a dynamic library, don't use the
9529 symbol size from the dynamic library. Relinking an executable
9530 against a new library may introduce gratuitous changes in the
9531 executable's symbols if we keep the size. */
9532 if (sym.st_shndx == SHN_UNDEF
9533 && !h->def_regular
9534 && h->def_dynamic)
9535 sym.st_size = 0;
9536
c152c796
AM
9537 /* If a non-weak symbol with non-default visibility is not defined
9538 locally, it is a fatal error. */
0e1862bb 9539 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9540 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9541 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9542 && h->root.type == bfd_link_hash_undefined
f5385ebf 9543 && !h->def_regular)
c152c796 9544 {
17d078c5
AM
9545 const char *msg;
9546
9547 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9548 msg = _("%B: protected symbol `%s' isn't defined");
9549 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9550 msg = _("%B: internal symbol `%s' isn't defined");
9551 else
9552 msg = _("%B: hidden symbol `%s' isn't defined");
8b127cbc 9553 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9554 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9555 eoinfo->failed = TRUE;
9556 return FALSE;
9557 }
9558
9559 /* If this symbol should be put in the .dynsym section, then put it
9560 there now. We already know the symbol index. We also fill in
9561 the entry in the .hash section. */
cae1fbbb 9562 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9563 && h->dynindx != -1
8b127cbc 9564 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9565 {
c152c796
AM
9566 bfd_byte *esym;
9567
90c984fc
L
9568 /* Since there is no version information in the dynamic string,
9569 if there is no version info in symbol version section, we will
1659f720 9570 have a run-time problem if not linking executable, referenced
6e33951e
L
9571 by shared library, not locally defined, or not bound locally.
9572 */
1659f720 9573 if (h->verinfo.verdef == NULL
6e33951e 9574 && !local_bind
0e1862bb 9575 && (!bfd_link_executable (flinfo->info)
1659f720
L
9576 || h->ref_dynamic
9577 || !h->def_regular))
90c984fc
L
9578 {
9579 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9580
9581 if (p && p [1] != '\0')
9582 {
9583 (*_bfd_error_handler)
9584 (_("%B: No symbol version section for versioned symbol `%s'"),
9585 flinfo->output_bfd, h->root.root.string);
9586 eoinfo->failed = TRUE;
9587 return FALSE;
9588 }
9589 }
9590
c152c796 9591 sym.st_name = h->dynstr_index;
cae1fbbb
L
9592 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9593 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9594 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9595 {
9596 eoinfo->failed = TRUE;
9597 return FALSE;
9598 }
8b127cbc 9599 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9600
8b127cbc 9601 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9602 {
9603 size_t hash_entry_size;
9604 bfd_byte *bucketpos;
9605 bfd_vma chain;
41198d0c
L
9606 size_t bucketcount;
9607 size_t bucket;
9608
8b127cbc 9609 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9610 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9611
9612 hash_entry_size
8b127cbc
AM
9613 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9614 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9615 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9616 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9617 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9618 bucketpos);
9619 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9620 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9621 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9622 }
c152c796 9623
8b127cbc 9624 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9625 {
9626 Elf_Internal_Versym iversym;
9627 Elf_External_Versym *eversym;
9628
f5385ebf 9629 if (!h->def_regular)
c152c796 9630 {
7b20f099
AM
9631 if (h->verinfo.verdef == NULL
9632 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9633 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9634 iversym.vs_vers = 0;
9635 else
9636 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9637 }
9638 else
9639 {
9640 if (h->verinfo.vertree == NULL)
9641 iversym.vs_vers = 1;
9642 else
9643 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9644 if (flinfo->info->create_default_symver)
3e3b46e5 9645 iversym.vs_vers++;
c152c796
AM
9646 }
9647
422f1182 9648 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9649 defined locally. */
422f1182 9650 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9651 iversym.vs_vers |= VERSYM_HIDDEN;
9652
8b127cbc 9653 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9654 eversym += h->dynindx;
8b127cbc 9655 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9656 }
9657 }
9658
d983c8c5
AM
9659 /* If the symbol is undefined, and we didn't output it to .dynsym,
9660 strip it from .symtab too. Obviously we can't do this for
9661 relocatable output or when needed for --emit-relocs. */
9662 else if (input_sec == bfd_und_section_ptr
9663 && h->indx != -2
0e1862bb 9664 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9665 return TRUE;
9666 /* Also strip others that we couldn't earlier due to dynamic symbol
9667 processing. */
9668 if (strip)
9669 return TRUE;
9670 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9671 return TRUE;
9672
2ec55de3
AM
9673 /* Output a FILE symbol so that following locals are not associated
9674 with the wrong input file. We need one for forced local symbols
9675 if we've seen more than one FILE symbol or when we have exactly
9676 one FILE symbol but global symbols are present in a file other
9677 than the one with the FILE symbol. We also need one if linker
9678 defined symbols are present. In practice these conditions are
9679 always met, so just emit the FILE symbol unconditionally. */
9680 if (eoinfo->localsyms
9681 && !eoinfo->file_sym_done
9682 && eoinfo->flinfo->filesym_count != 0)
9683 {
9684 Elf_Internal_Sym fsym;
9685
9686 memset (&fsym, 0, sizeof (fsym));
9687 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9688 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9689 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9690 bfd_und_section_ptr, NULL))
2ec55de3
AM
9691 return FALSE;
9692
9693 eoinfo->file_sym_done = TRUE;
9694 }
9695
8b127cbc 9696 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9697 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9698 input_sec, h);
6e0b88f1 9699 if (ret == 0)
c152c796
AM
9700 {
9701 eoinfo->failed = TRUE;
9702 return FALSE;
9703 }
6e0b88f1
AM
9704 else if (ret == 1)
9705 h->indx = indx;
9706 else if (h->indx == -2)
9707 abort();
c152c796
AM
9708
9709 return TRUE;
9710}
9711
cdd3575c
AM
9712/* Return TRUE if special handling is done for relocs in SEC against
9713 symbols defined in discarded sections. */
9714
c152c796
AM
9715static bfd_boolean
9716elf_section_ignore_discarded_relocs (asection *sec)
9717{
9718 const struct elf_backend_data *bed;
9719
cdd3575c
AM
9720 switch (sec->sec_info_type)
9721 {
dbaa2011
AM
9722 case SEC_INFO_TYPE_STABS:
9723 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9724 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9725 return TRUE;
9726 default:
9727 break;
9728 }
c152c796
AM
9729
9730 bed = get_elf_backend_data (sec->owner);
9731 if (bed->elf_backend_ignore_discarded_relocs != NULL
9732 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9733 return TRUE;
9734
9735 return FALSE;
9736}
9737
9e66c942
AM
9738/* Return a mask saying how ld should treat relocations in SEC against
9739 symbols defined in discarded sections. If this function returns
9740 COMPLAIN set, ld will issue a warning message. If this function
9741 returns PRETEND set, and the discarded section was link-once and the
9742 same size as the kept link-once section, ld will pretend that the
9743 symbol was actually defined in the kept section. Otherwise ld will
9744 zero the reloc (at least that is the intent, but some cooperation by
9745 the target dependent code is needed, particularly for REL targets). */
9746
8a696751
AM
9747unsigned int
9748_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9749{
9e66c942 9750 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9751 return PRETEND;
cdd3575c
AM
9752
9753 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9754 return 0;
cdd3575c
AM
9755
9756 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9757 return 0;
cdd3575c 9758
9e66c942 9759 return COMPLAIN | PRETEND;
cdd3575c
AM
9760}
9761
3d7f7666
L
9762/* Find a match between a section and a member of a section group. */
9763
9764static asection *
c0f00686
L
9765match_group_member (asection *sec, asection *group,
9766 struct bfd_link_info *info)
3d7f7666
L
9767{
9768 asection *first = elf_next_in_group (group);
9769 asection *s = first;
9770
9771 while (s != NULL)
9772 {
c0f00686 9773 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9774 return s;
9775
83180ade 9776 s = elf_next_in_group (s);
3d7f7666
L
9777 if (s == first)
9778 break;
9779 }
9780
9781 return NULL;
9782}
9783
01b3c8ab 9784/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9785 to replace it. Return the replacement if it is OK. Otherwise return
9786 NULL. */
01b3c8ab
L
9787
9788asection *
c0f00686 9789_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9790{
9791 asection *kept;
9792
9793 kept = sec->kept_section;
9794 if (kept != NULL)
9795 {
c2370991 9796 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9797 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9798 if (kept != NULL
9799 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9800 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9801 kept = NULL;
c2370991 9802 sec->kept_section = kept;
01b3c8ab
L
9803 }
9804 return kept;
9805}
9806
c152c796
AM
9807/* Link an input file into the linker output file. This function
9808 handles all the sections and relocations of the input file at once.
9809 This is so that we only have to read the local symbols once, and
9810 don't have to keep them in memory. */
9811
9812static bfd_boolean
8b127cbc 9813elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9814{
ece5ef60 9815 int (*relocate_section)
c152c796
AM
9816 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9817 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9818 bfd *output_bfd;
9819 Elf_Internal_Shdr *symtab_hdr;
9820 size_t locsymcount;
9821 size_t extsymoff;
9822 Elf_Internal_Sym *isymbuf;
9823 Elf_Internal_Sym *isym;
9824 Elf_Internal_Sym *isymend;
9825 long *pindex;
9826 asection **ppsection;
9827 asection *o;
9828 const struct elf_backend_data *bed;
c152c796 9829 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9830 bfd_size_type address_size;
9831 bfd_vma r_type_mask;
9832 int r_sym_shift;
ffbc01cc 9833 bfd_boolean have_file_sym = FALSE;
c152c796 9834
8b127cbc 9835 output_bfd = flinfo->output_bfd;
c152c796
AM
9836 bed = get_elf_backend_data (output_bfd);
9837 relocate_section = bed->elf_backend_relocate_section;
9838
9839 /* If this is a dynamic object, we don't want to do anything here:
9840 we don't want the local symbols, and we don't want the section
9841 contents. */
9842 if ((input_bfd->flags & DYNAMIC) != 0)
9843 return TRUE;
9844
c152c796
AM
9845 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9846 if (elf_bad_symtab (input_bfd))
9847 {
9848 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9849 extsymoff = 0;
9850 }
9851 else
9852 {
9853 locsymcount = symtab_hdr->sh_info;
9854 extsymoff = symtab_hdr->sh_info;
9855 }
9856
9857 /* Read the local symbols. */
9858 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9859 if (isymbuf == NULL && locsymcount != 0)
9860 {
9861 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9862 flinfo->internal_syms,
9863 flinfo->external_syms,
9864 flinfo->locsym_shndx);
c152c796
AM
9865 if (isymbuf == NULL)
9866 return FALSE;
9867 }
9868
9869 /* Find local symbol sections and adjust values of symbols in
9870 SEC_MERGE sections. Write out those local symbols we know are
9871 going into the output file. */
9872 isymend = isymbuf + locsymcount;
8b127cbc 9873 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9874 isym < isymend;
9875 isym++, pindex++, ppsection++)
9876 {
9877 asection *isec;
9878 const char *name;
9879 Elf_Internal_Sym osym;
6e0b88f1
AM
9880 long indx;
9881 int ret;
c152c796
AM
9882
9883 *pindex = -1;
9884
9885 if (elf_bad_symtab (input_bfd))
9886 {
9887 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9888 {
9889 *ppsection = NULL;
9890 continue;
9891 }
9892 }
9893
9894 if (isym->st_shndx == SHN_UNDEF)
9895 isec = bfd_und_section_ptr;
c152c796
AM
9896 else if (isym->st_shndx == SHN_ABS)
9897 isec = bfd_abs_section_ptr;
9898 else if (isym->st_shndx == SHN_COMMON)
9899 isec = bfd_com_section_ptr;
9900 else
9901 {
cb33740c
AM
9902 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9903 if (isec == NULL)
9904 {
9905 /* Don't attempt to output symbols with st_shnx in the
9906 reserved range other than SHN_ABS and SHN_COMMON. */
9907 *ppsection = NULL;
9908 continue;
9909 }
dbaa2011 9910 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9911 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9912 isym->st_value =
9913 _bfd_merged_section_offset (output_bfd, &isec,
9914 elf_section_data (isec)->sec_info,
9915 isym->st_value);
c152c796
AM
9916 }
9917
9918 *ppsection = isec;
9919
d983c8c5
AM
9920 /* Don't output the first, undefined, symbol. In fact, don't
9921 output any undefined local symbol. */
9922 if (isec == bfd_und_section_ptr)
c152c796
AM
9923 continue;
9924
9925 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9926 {
9927 /* We never output section symbols. Instead, we use the
9928 section symbol of the corresponding section in the output
9929 file. */
9930 continue;
9931 }
9932
9933 /* If we are stripping all symbols, we don't want to output this
9934 one. */
8b127cbc 9935 if (flinfo->info->strip == strip_all)
c152c796
AM
9936 continue;
9937
9938 /* If we are discarding all local symbols, we don't want to
9939 output this one. If we are generating a relocatable output
9940 file, then some of the local symbols may be required by
9941 relocs; we output them below as we discover that they are
9942 needed. */
8b127cbc 9943 if (flinfo->info->discard == discard_all)
c152c796
AM
9944 continue;
9945
9946 /* If this symbol is defined in a section which we are
f02571c5
AM
9947 discarding, we don't need to keep it. */
9948 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9949 && isym->st_shndx < SHN_LORESERVE
9950 && bfd_section_removed_from_list (output_bfd,
9951 isec->output_section))
e75a280b
L
9952 continue;
9953
c152c796
AM
9954 /* Get the name of the symbol. */
9955 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9956 isym->st_name);
9957 if (name == NULL)
9958 return FALSE;
9959
9960 /* See if we are discarding symbols with this name. */
8b127cbc
AM
9961 if ((flinfo->info->strip == strip_some
9962 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 9963 == NULL))
8b127cbc 9964 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
9965 && (isec->flags & SEC_MERGE)
9966 && !bfd_link_relocatable (flinfo->info))
8b127cbc 9967 || flinfo->info->discard == discard_l)
c152c796
AM
9968 && bfd_is_local_label_name (input_bfd, name)))
9969 continue;
9970
ffbc01cc
AM
9971 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9972 {
ce875075
AM
9973 if (input_bfd->lto_output)
9974 /* -flto puts a temp file name here. This means builds
9975 are not reproducible. Discard the symbol. */
9976 continue;
ffbc01cc
AM
9977 have_file_sym = TRUE;
9978 flinfo->filesym_count += 1;
9979 }
9980 if (!have_file_sym)
9981 {
9982 /* In the absence of debug info, bfd_find_nearest_line uses
9983 FILE symbols to determine the source file for local
9984 function symbols. Provide a FILE symbol here if input
9985 files lack such, so that their symbols won't be
9986 associated with a previous input file. It's not the
9987 source file, but the best we can do. */
9988 have_file_sym = TRUE;
9989 flinfo->filesym_count += 1;
9990 memset (&osym, 0, sizeof (osym));
9991 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9992 osym.st_shndx = SHN_ABS;
ef10c3ac
L
9993 if (!elf_link_output_symstrtab (flinfo,
9994 (input_bfd->lto_output ? NULL
9995 : input_bfd->filename),
9996 &osym, bfd_abs_section_ptr,
9997 NULL))
ffbc01cc
AM
9998 return FALSE;
9999 }
10000
c152c796
AM
10001 osym = *isym;
10002
10003 /* Adjust the section index for the output file. */
10004 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10005 isec->output_section);
10006 if (osym.st_shndx == SHN_BAD)
10007 return FALSE;
10008
c152c796
AM
10009 /* ELF symbols in relocatable files are section relative, but
10010 in executable files they are virtual addresses. Note that
10011 this code assumes that all ELF sections have an associated
10012 BFD section with a reasonable value for output_offset; below
10013 we assume that they also have a reasonable value for
10014 output_section. Any special sections must be set up to meet
10015 these requirements. */
10016 osym.st_value += isec->output_offset;
0e1862bb 10017 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10018 {
10019 osym.st_value += isec->output_section->vma;
10020 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10021 {
10022 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10023 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10024 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10025 }
10026 }
10027
6e0b88f1 10028 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10029 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10030 if (ret == 0)
c152c796 10031 return FALSE;
6e0b88f1
AM
10032 else if (ret == 1)
10033 *pindex = indx;
c152c796
AM
10034 }
10035
310fd250
L
10036 if (bed->s->arch_size == 32)
10037 {
10038 r_type_mask = 0xff;
10039 r_sym_shift = 8;
10040 address_size = 4;
10041 }
10042 else
10043 {
10044 r_type_mask = 0xffffffff;
10045 r_sym_shift = 32;
10046 address_size = 8;
10047 }
10048
c152c796
AM
10049 /* Relocate the contents of each section. */
10050 sym_hashes = elf_sym_hashes (input_bfd);
10051 for (o = input_bfd->sections; o != NULL; o = o->next)
10052 {
10053 bfd_byte *contents;
10054
10055 if (! o->linker_mark)
10056 {
10057 /* This section was omitted from the link. */
10058 continue;
10059 }
10060
0e1862bb 10061 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
10062 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10063 {
10064 /* Deal with the group signature symbol. */
10065 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10066 unsigned long symndx = sec_data->this_hdr.sh_info;
10067 asection *osec = o->output_section;
10068
10069 if (symndx >= locsymcount
10070 || (elf_bad_symtab (input_bfd)
8b127cbc 10071 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10072 {
10073 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10074 while (h->root.type == bfd_link_hash_indirect
10075 || h->root.type == bfd_link_hash_warning)
10076 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10077 /* Arrange for symbol to be output. */
10078 h->indx = -2;
10079 elf_section_data (osec)->this_hdr.sh_info = -2;
10080 }
10081 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10082 {
10083 /* We'll use the output section target_index. */
8b127cbc 10084 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10085 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10086 }
10087 else
10088 {
8b127cbc 10089 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10090 {
10091 /* Otherwise output the local symbol now. */
10092 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10093 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10094 const char *name;
6e0b88f1
AM
10095 long indx;
10096 int ret;
bcacc0f5
AM
10097
10098 name = bfd_elf_string_from_elf_section (input_bfd,
10099 symtab_hdr->sh_link,
10100 sym.st_name);
10101 if (name == NULL)
10102 return FALSE;
10103
10104 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10105 sec);
10106 if (sym.st_shndx == SHN_BAD)
10107 return FALSE;
10108
10109 sym.st_value += o->output_offset;
10110
6e0b88f1 10111 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10112 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10113 NULL);
6e0b88f1 10114 if (ret == 0)
bcacc0f5 10115 return FALSE;
6e0b88f1 10116 else if (ret == 1)
8b127cbc 10117 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10118 else
10119 abort ();
bcacc0f5
AM
10120 }
10121 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10122 = flinfo->indices[symndx];
bcacc0f5
AM
10123 }
10124 }
10125
c152c796 10126 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10127 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10128 continue;
10129
10130 if ((o->flags & SEC_LINKER_CREATED) != 0)
10131 {
10132 /* Section was created by _bfd_elf_link_create_dynamic_sections
10133 or somesuch. */
10134 continue;
10135 }
10136
10137 /* Get the contents of the section. They have been cached by a
10138 relaxation routine. Note that o is a section in an input
10139 file, so the contents field will not have been set by any of
10140 the routines which work on output files. */
10141 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10142 {
10143 contents = elf_section_data (o)->this_hdr.contents;
10144 if (bed->caches_rawsize
10145 && o->rawsize != 0
10146 && o->rawsize < o->size)
10147 {
10148 memcpy (flinfo->contents, contents, o->rawsize);
10149 contents = flinfo->contents;
10150 }
10151 }
c152c796
AM
10152 else
10153 {
8b127cbc 10154 contents = flinfo->contents;
4a114e3e 10155 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10156 return FALSE;
10157 }
10158
10159 if ((o->flags & SEC_RELOC) != 0)
10160 {
10161 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10162 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10163 int action_discarded;
ece5ef60 10164 int ret;
c152c796
AM
10165
10166 /* Get the swapped relocs. */
10167 internal_relocs
8b127cbc
AM
10168 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10169 flinfo->internal_relocs, FALSE);
c152c796
AM
10170 if (internal_relocs == NULL
10171 && o->reloc_count > 0)
10172 return FALSE;
10173
310fd250
L
10174 /* We need to reverse-copy input .ctors/.dtors sections if
10175 they are placed in .init_array/.finit_array for output. */
10176 if (o->size > address_size
10177 && ((strncmp (o->name, ".ctors", 6) == 0
10178 && strcmp (o->output_section->name,
10179 ".init_array") == 0)
10180 || (strncmp (o->name, ".dtors", 6) == 0
10181 && strcmp (o->output_section->name,
10182 ".fini_array") == 0))
10183 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10184 {
310fd250
L
10185 if (o->size != o->reloc_count * address_size)
10186 {
10187 (*_bfd_error_handler)
10188 (_("error: %B: size of section %A is not "
10189 "multiple of address size"),
10190 input_bfd, o);
10191 bfd_set_error (bfd_error_on_input);
10192 return FALSE;
10193 }
10194 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10195 }
10196
0f02bbd9 10197 action_discarded = -1;
c152c796 10198 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10199 action_discarded = (*bed->action_discarded) (o);
10200
10201 /* Run through the relocs evaluating complex reloc symbols and
10202 looking for relocs against symbols from discarded sections
10203 or section symbols from removed link-once sections.
10204 Complain about relocs against discarded sections. Zero
10205 relocs against removed link-once sections. */
10206
10207 rel = internal_relocs;
10208 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10209 for ( ; rel < relend; rel++)
c152c796 10210 {
0f02bbd9
AM
10211 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10212 unsigned int s_type;
10213 asection **ps, *sec;
10214 struct elf_link_hash_entry *h = NULL;
10215 const char *sym_name;
c152c796 10216
0f02bbd9
AM
10217 if (r_symndx == STN_UNDEF)
10218 continue;
c152c796 10219
0f02bbd9
AM
10220 if (r_symndx >= locsymcount
10221 || (elf_bad_symtab (input_bfd)
8b127cbc 10222 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10223 {
10224 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10225
0f02bbd9
AM
10226 /* Badly formatted input files can contain relocs that
10227 reference non-existant symbols. Check here so that
10228 we do not seg fault. */
10229 if (h == NULL)
c152c796 10230 {
0f02bbd9 10231 char buffer [32];
dce669a1 10232
0f02bbd9
AM
10233 sprintf_vma (buffer, rel->r_info);
10234 (*_bfd_error_handler)
10235 (_("error: %B contains a reloc (0x%s) for section %A "
10236 "that references a non-existent global symbol"),
10237 input_bfd, o, buffer);
10238 bfd_set_error (bfd_error_bad_value);
10239 return FALSE;
10240 }
3b36f7e6 10241
0f02bbd9
AM
10242 while (h->root.type == bfd_link_hash_indirect
10243 || h->root.type == bfd_link_hash_warning)
10244 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10245
0f02bbd9 10246 s_type = h->type;
cdd3575c 10247
9e2dec47 10248 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10249 mark the symbol as undefined. Note that the
10250 linker may attach linker created dynamic sections
10251 to the plugin bfd. Symbols defined in linker
10252 created sections are not plugin symbols. */
9e2dec47
L
10253 if (h->root.non_ir_ref
10254 && (h->root.type == bfd_link_hash_defined
10255 || h->root.type == bfd_link_hash_defweak)
10256 && (h->root.u.def.section->flags
10257 & SEC_LINKER_CREATED) == 0
10258 && h->root.u.def.section->owner != NULL
10259 && (h->root.u.def.section->owner->flags
10260 & BFD_PLUGIN) != 0)
10261 {
10262 h->root.type = bfd_link_hash_undefined;
10263 h->root.u.undef.abfd = h->root.u.def.section->owner;
10264 }
10265
0f02bbd9
AM
10266 ps = NULL;
10267 if (h->root.type == bfd_link_hash_defined
10268 || h->root.type == bfd_link_hash_defweak)
10269 ps = &h->root.u.def.section;
10270
10271 sym_name = h->root.root.string;
10272 }
10273 else
10274 {
10275 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10276
10277 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10278 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10279 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10280 sym, *ps);
10281 }
c152c796 10282
c301e700 10283 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10284 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10285 {
10286 bfd_vma val;
10287 bfd_vma dot = (rel->r_offset
10288 + o->output_offset + o->output_section->vma);
10289#ifdef DEBUG
10290 printf ("Encountered a complex symbol!");
10291 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10292 input_bfd->filename, o->name,
10293 (long) (rel - internal_relocs));
0f02bbd9
AM
10294 printf (" symbol: idx %8.8lx, name %s\n",
10295 r_symndx, sym_name);
10296 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10297 (unsigned long) rel->r_info,
10298 (unsigned long) rel->r_offset);
10299#endif
8b127cbc 10300 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10301 isymbuf, locsymcount, s_type == STT_SRELC))
10302 return FALSE;
10303
10304 /* Symbol evaluated OK. Update to absolute value. */
10305 set_symbol_value (input_bfd, isymbuf, locsymcount,
10306 r_symndx, val);
10307 continue;
10308 }
10309
10310 if (action_discarded != -1 && ps != NULL)
10311 {
cdd3575c
AM
10312 /* Complain if the definition comes from a
10313 discarded section. */
dbaa2011 10314 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10315 {
cf35638d 10316 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10317 if (action_discarded & COMPLAIN)
8b127cbc 10318 (*flinfo->info->callbacks->einfo)
e1fffbe6 10319 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10320 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10321 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10322
87e5235d 10323 /* Try to do the best we can to support buggy old
e0ae6d6f 10324 versions of gcc. Pretend that the symbol is
87e5235d
AM
10325 really defined in the kept linkonce section.
10326 FIXME: This is quite broken. Modifying the
10327 symbol here means we will be changing all later
e0ae6d6f 10328 uses of the symbol, not just in this section. */
0f02bbd9 10329 if (action_discarded & PRETEND)
87e5235d 10330 {
01b3c8ab
L
10331 asection *kept;
10332
c0f00686 10333 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10334 flinfo->info);
01b3c8ab 10335 if (kept != NULL)
87e5235d
AM
10336 {
10337 *ps = kept;
10338 continue;
10339 }
10340 }
c152c796
AM
10341 }
10342 }
10343 }
10344
10345 /* Relocate the section by invoking a back end routine.
10346
10347 The back end routine is responsible for adjusting the
10348 section contents as necessary, and (if using Rela relocs
10349 and generating a relocatable output file) adjusting the
10350 reloc addend as necessary.
10351
10352 The back end routine does not have to worry about setting
10353 the reloc address or the reloc symbol index.
10354
10355 The back end routine is given a pointer to the swapped in
10356 internal symbols, and can access the hash table entries
10357 for the external symbols via elf_sym_hashes (input_bfd).
10358
10359 When generating relocatable output, the back end routine
10360 must handle STB_LOCAL/STT_SECTION symbols specially. The
10361 output symbol is going to be a section symbol
10362 corresponding to the output section, which will require
10363 the addend to be adjusted. */
10364
8b127cbc 10365 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10366 input_bfd, o, contents,
10367 internal_relocs,
10368 isymbuf,
8b127cbc 10369 flinfo->sections);
ece5ef60 10370 if (!ret)
c152c796
AM
10371 return FALSE;
10372
ece5ef60 10373 if (ret == 2
0e1862bb 10374 || bfd_link_relocatable (flinfo->info)
8b127cbc 10375 || flinfo->info->emitrelocations)
c152c796
AM
10376 {
10377 Elf_Internal_Rela *irela;
d4730f92 10378 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10379 bfd_vma last_offset;
10380 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10381 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10382 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10383 unsigned int next_erel;
c152c796 10384 bfd_boolean rela_normal;
d4730f92 10385 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10386
d4730f92
BS
10387 esdi = elf_section_data (o);
10388 esdo = elf_section_data (o->output_section);
10389 rela_normal = FALSE;
c152c796
AM
10390
10391 /* Adjust the reloc addresses and symbol indices. */
10392
10393 irela = internal_relocs;
10394 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10395 rel_hash = esdo->rel.hashes + esdo->rel.count;
10396 /* We start processing the REL relocs, if any. When we reach
10397 IRELAMID in the loop, we switch to the RELA relocs. */
10398 irelamid = irela;
10399 if (esdi->rel.hdr != NULL)
10400 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10401 * bed->s->int_rels_per_ext_rel);
eac338cf 10402 rel_hash_list = rel_hash;
d4730f92 10403 rela_hash_list = NULL;
c152c796 10404 last_offset = o->output_offset;
0e1862bb 10405 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10406 last_offset += o->output_section->vma;
10407 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10408 {
10409 unsigned long r_symndx;
10410 asection *sec;
10411 Elf_Internal_Sym sym;
10412
10413 if (next_erel == bed->s->int_rels_per_ext_rel)
10414 {
10415 rel_hash++;
10416 next_erel = 0;
10417 }
10418
d4730f92
BS
10419 if (irela == irelamid)
10420 {
10421 rel_hash = esdo->rela.hashes + esdo->rela.count;
10422 rela_hash_list = rel_hash;
10423 rela_normal = bed->rela_normal;
10424 }
10425
c152c796 10426 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10427 flinfo->info, o,
c152c796
AM
10428 irela->r_offset);
10429 if (irela->r_offset >= (bfd_vma) -2)
10430 {
10431 /* This is a reloc for a deleted entry or somesuch.
10432 Turn it into an R_*_NONE reloc, at the same
10433 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10434 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10435 being ordered. */
10436 irela->r_offset = last_offset;
10437 irela->r_info = 0;
10438 irela->r_addend = 0;
10439 continue;
10440 }
10441
10442 irela->r_offset += o->output_offset;
10443
10444 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10445 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10446 irela->r_offset += o->output_section->vma;
10447
10448 last_offset = irela->r_offset;
10449
10450 r_symndx = irela->r_info >> r_sym_shift;
10451 if (r_symndx == STN_UNDEF)
10452 continue;
10453
10454 if (r_symndx >= locsymcount
10455 || (elf_bad_symtab (input_bfd)
8b127cbc 10456 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10457 {
10458 struct elf_link_hash_entry *rh;
10459 unsigned long indx;
10460
10461 /* This is a reloc against a global symbol. We
10462 have not yet output all the local symbols, so
10463 we do not know the symbol index of any global
10464 symbol. We set the rel_hash entry for this
10465 reloc to point to the global hash table entry
10466 for this symbol. The symbol index is then
ee75fd95 10467 set at the end of bfd_elf_final_link. */
c152c796
AM
10468 indx = r_symndx - extsymoff;
10469 rh = elf_sym_hashes (input_bfd)[indx];
10470 while (rh->root.type == bfd_link_hash_indirect
10471 || rh->root.type == bfd_link_hash_warning)
10472 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10473
10474 /* Setting the index to -2 tells
10475 elf_link_output_extsym that this symbol is
10476 used by a reloc. */
10477 BFD_ASSERT (rh->indx < 0);
10478 rh->indx = -2;
10479
10480 *rel_hash = rh;
10481
10482 continue;
10483 }
10484
10485 /* This is a reloc against a local symbol. */
10486
10487 *rel_hash = NULL;
10488 sym = isymbuf[r_symndx];
8b127cbc 10489 sec = flinfo->sections[r_symndx];
c152c796
AM
10490 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10491 {
10492 /* I suppose the backend ought to fill in the
10493 section of any STT_SECTION symbol against a
6a8d1586 10494 processor specific section. */
cf35638d 10495 r_symndx = STN_UNDEF;
6a8d1586
AM
10496 if (bfd_is_abs_section (sec))
10497 ;
c152c796
AM
10498 else if (sec == NULL || sec->owner == NULL)
10499 {
10500 bfd_set_error (bfd_error_bad_value);
10501 return FALSE;
10502 }
10503 else
10504 {
6a8d1586
AM
10505 asection *osec = sec->output_section;
10506
10507 /* If we have discarded a section, the output
10508 section will be the absolute section. In
ab96bf03
AM
10509 case of discarded SEC_MERGE sections, use
10510 the kept section. relocate_section should
10511 have already handled discarded linkonce
10512 sections. */
6a8d1586
AM
10513 if (bfd_is_abs_section (osec)
10514 && sec->kept_section != NULL
10515 && sec->kept_section->output_section != NULL)
10516 {
10517 osec = sec->kept_section->output_section;
10518 irela->r_addend -= osec->vma;
10519 }
10520
10521 if (!bfd_is_abs_section (osec))
10522 {
10523 r_symndx = osec->target_index;
cf35638d 10524 if (r_symndx == STN_UNDEF)
74541ad4 10525 {
051d833a
AM
10526 irela->r_addend += osec->vma;
10527 osec = _bfd_nearby_section (output_bfd, osec,
10528 osec->vma);
10529 irela->r_addend -= osec->vma;
10530 r_symndx = osec->target_index;
74541ad4 10531 }
6a8d1586 10532 }
c152c796
AM
10533 }
10534
10535 /* Adjust the addend according to where the
10536 section winds up in the output section. */
10537 if (rela_normal)
10538 irela->r_addend += sec->output_offset;
10539 }
10540 else
10541 {
8b127cbc 10542 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10543 {
10544 unsigned long shlink;
10545 const char *name;
10546 asection *osec;
6e0b88f1 10547 long indx;
c152c796 10548
8b127cbc 10549 if (flinfo->info->strip == strip_all)
c152c796
AM
10550 {
10551 /* You can't do ld -r -s. */
10552 bfd_set_error (bfd_error_invalid_operation);
10553 return FALSE;
10554 }
10555
10556 /* This symbol was skipped earlier, but
10557 since it is needed by a reloc, we
10558 must output it now. */
10559 shlink = symtab_hdr->sh_link;
10560 name = (bfd_elf_string_from_elf_section
10561 (input_bfd, shlink, sym.st_name));
10562 if (name == NULL)
10563 return FALSE;
10564
10565 osec = sec->output_section;
10566 sym.st_shndx =
10567 _bfd_elf_section_from_bfd_section (output_bfd,
10568 osec);
10569 if (sym.st_shndx == SHN_BAD)
10570 return FALSE;
10571
10572 sym.st_value += sec->output_offset;
0e1862bb 10573 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10574 {
10575 sym.st_value += osec->vma;
10576 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10577 {
10578 /* STT_TLS symbols are relative to PT_TLS
10579 segment base. */
8b127cbc 10580 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10581 ->tls_sec != NULL);
8b127cbc 10582 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10583 ->tls_sec->vma);
10584 }
10585 }
10586
6e0b88f1 10587 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10588 ret = elf_link_output_symstrtab (flinfo, name,
10589 &sym, sec,
10590 NULL);
6e0b88f1 10591 if (ret == 0)
c152c796 10592 return FALSE;
6e0b88f1 10593 else if (ret == 1)
8b127cbc 10594 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10595 else
10596 abort ();
c152c796
AM
10597 }
10598
8b127cbc 10599 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10600 }
10601
10602 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10603 | (irela->r_info & r_type_mask));
10604 }
10605
10606 /* Swap out the relocs. */
d4730f92
BS
10607 input_rel_hdr = esdi->rel.hdr;
10608 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10609 {
d4730f92
BS
10610 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10611 input_rel_hdr,
10612 internal_relocs,
10613 rel_hash_list))
10614 return FALSE;
c152c796
AM
10615 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10616 * bed->s->int_rels_per_ext_rel);
eac338cf 10617 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10618 }
10619
10620 input_rela_hdr = esdi->rela.hdr;
10621 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10622 {
eac338cf 10623 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10624 input_rela_hdr,
eac338cf 10625 internal_relocs,
d4730f92 10626 rela_hash_list))
c152c796
AM
10627 return FALSE;
10628 }
10629 }
10630 }
10631
10632 /* Write out the modified section contents. */
10633 if (bed->elf_backend_write_section
8b127cbc 10634 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10635 contents))
c152c796
AM
10636 {
10637 /* Section written out. */
10638 }
10639 else switch (o->sec_info_type)
10640 {
dbaa2011 10641 case SEC_INFO_TYPE_STABS:
c152c796
AM
10642 if (! (_bfd_write_section_stabs
10643 (output_bfd,
8b127cbc 10644 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10645 o, &elf_section_data (o)->sec_info, contents)))
10646 return FALSE;
10647 break;
dbaa2011 10648 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10649 if (! _bfd_write_merged_section (output_bfd, o,
10650 elf_section_data (o)->sec_info))
10651 return FALSE;
10652 break;
dbaa2011 10653 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10654 {
8b127cbc 10655 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10656 o, contents))
10657 return FALSE;
10658 }
10659 break;
2f0c68f2
CM
10660 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10661 {
10662 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10663 flinfo->info,
10664 o, contents))
10665 return FALSE;
10666 }
10667 break;
c152c796
AM
10668 default:
10669 {
310fd250
L
10670 if (! (o->flags & SEC_EXCLUDE))
10671 {
10672 file_ptr offset = (file_ptr) o->output_offset;
10673 bfd_size_type todo = o->size;
37b01f6a
DG
10674
10675 offset *= bfd_octets_per_byte (output_bfd);
10676
310fd250
L
10677 if ((o->flags & SEC_ELF_REVERSE_COPY))
10678 {
10679 /* Reverse-copy input section to output. */
10680 do
10681 {
10682 todo -= address_size;
10683 if (! bfd_set_section_contents (output_bfd,
10684 o->output_section,
10685 contents + todo,
10686 offset,
10687 address_size))
10688 return FALSE;
10689 if (todo == 0)
10690 break;
10691 offset += address_size;
10692 }
10693 while (1);
10694 }
10695 else if (! bfd_set_section_contents (output_bfd,
10696 o->output_section,
10697 contents,
10698 offset, todo))
10699 return FALSE;
10700 }
c152c796
AM
10701 }
10702 break;
10703 }
10704 }
10705
10706 return TRUE;
10707}
10708
10709/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10710 requested by the linker, and does not come from any input file. This
c152c796
AM
10711 is used to build constructor and destructor tables when linking
10712 with -Ur. */
10713
10714static bfd_boolean
10715elf_reloc_link_order (bfd *output_bfd,
10716 struct bfd_link_info *info,
10717 asection *output_section,
10718 struct bfd_link_order *link_order)
10719{
10720 reloc_howto_type *howto;
10721 long indx;
10722 bfd_vma offset;
10723 bfd_vma addend;
d4730f92 10724 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10725 struct elf_link_hash_entry **rel_hash_ptr;
10726 Elf_Internal_Shdr *rel_hdr;
10727 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10728 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10729 bfd_byte *erel;
10730 unsigned int i;
d4730f92 10731 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10732
10733 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10734 if (howto == NULL)
10735 {
10736 bfd_set_error (bfd_error_bad_value);
10737 return FALSE;
10738 }
10739
10740 addend = link_order->u.reloc.p->addend;
10741
d4730f92
BS
10742 if (esdo->rel.hdr)
10743 reldata = &esdo->rel;
10744 else if (esdo->rela.hdr)
10745 reldata = &esdo->rela;
10746 else
10747 {
10748 reldata = NULL;
10749 BFD_ASSERT (0);
10750 }
10751
c152c796 10752 /* Figure out the symbol index. */
d4730f92 10753 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10754 if (link_order->type == bfd_section_reloc_link_order)
10755 {
10756 indx = link_order->u.reloc.p->u.section->target_index;
10757 BFD_ASSERT (indx != 0);
10758 *rel_hash_ptr = NULL;
10759 }
10760 else
10761 {
10762 struct elf_link_hash_entry *h;
10763
10764 /* Treat a reloc against a defined symbol as though it were
10765 actually against the section. */
10766 h = ((struct elf_link_hash_entry *)
10767 bfd_wrapped_link_hash_lookup (output_bfd, info,
10768 link_order->u.reloc.p->u.name,
10769 FALSE, FALSE, TRUE));
10770 if (h != NULL
10771 && (h->root.type == bfd_link_hash_defined
10772 || h->root.type == bfd_link_hash_defweak))
10773 {
10774 asection *section;
10775
10776 section = h->root.u.def.section;
10777 indx = section->output_section->target_index;
10778 *rel_hash_ptr = NULL;
10779 /* It seems that we ought to add the symbol value to the
10780 addend here, but in practice it has already been added
10781 because it was passed to constructor_callback. */
10782 addend += section->output_section->vma + section->output_offset;
10783 }
10784 else if (h != NULL)
10785 {
10786 /* Setting the index to -2 tells elf_link_output_extsym that
10787 this symbol is used by a reloc. */
10788 h->indx = -2;
10789 *rel_hash_ptr = h;
10790 indx = 0;
10791 }
10792 else
10793 {
1a72702b
AM
10794 (*info->callbacks->unattached_reloc)
10795 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
10796 indx = 0;
10797 }
10798 }
10799
10800 /* If this is an inplace reloc, we must write the addend into the
10801 object file. */
10802 if (howto->partial_inplace && addend != 0)
10803 {
10804 bfd_size_type size;
10805 bfd_reloc_status_type rstat;
10806 bfd_byte *buf;
10807 bfd_boolean ok;
10808 const char *sym_name;
10809
a50b1753
NC
10810 size = (bfd_size_type) bfd_get_reloc_size (howto);
10811 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10812 if (buf == NULL && size != 0)
c152c796
AM
10813 return FALSE;
10814 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10815 switch (rstat)
10816 {
10817 case bfd_reloc_ok:
10818 break;
10819
10820 default:
10821 case bfd_reloc_outofrange:
10822 abort ();
10823
10824 case bfd_reloc_overflow:
10825 if (link_order->type == bfd_section_reloc_link_order)
10826 sym_name = bfd_section_name (output_bfd,
10827 link_order->u.reloc.p->u.section);
10828 else
10829 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
10830 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10831 howto->name, addend, NULL, NULL,
10832 (bfd_vma) 0);
c152c796
AM
10833 break;
10834 }
37b01f6a 10835
c152c796 10836 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
10837 link_order->offset
10838 * bfd_octets_per_byte (output_bfd),
10839 size);
c152c796
AM
10840 free (buf);
10841 if (! ok)
10842 return FALSE;
10843 }
10844
10845 /* The address of a reloc is relative to the section in a
10846 relocatable file, and is a virtual address in an executable
10847 file. */
10848 offset = link_order->offset;
0e1862bb 10849 if (! bfd_link_relocatable (info))
c152c796
AM
10850 offset += output_section->vma;
10851
10852 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10853 {
10854 irel[i].r_offset = offset;
10855 irel[i].r_info = 0;
10856 irel[i].r_addend = 0;
10857 }
10858 if (bed->s->arch_size == 32)
10859 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10860 else
10861 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10862
d4730f92 10863 rel_hdr = reldata->hdr;
c152c796
AM
10864 erel = rel_hdr->contents;
10865 if (rel_hdr->sh_type == SHT_REL)
10866 {
d4730f92 10867 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10868 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10869 }
10870 else
10871 {
10872 irel[0].r_addend = addend;
d4730f92 10873 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10874 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10875 }
10876
d4730f92 10877 ++reldata->count;
c152c796
AM
10878
10879 return TRUE;
10880}
10881
0b52efa6
PB
10882
10883/* Get the output vma of the section pointed to by the sh_link field. */
10884
10885static bfd_vma
10886elf_get_linked_section_vma (struct bfd_link_order *p)
10887{
10888 Elf_Internal_Shdr **elf_shdrp;
10889 asection *s;
10890 int elfsec;
10891
10892 s = p->u.indirect.section;
10893 elf_shdrp = elf_elfsections (s->owner);
10894 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10895 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10896 /* PR 290:
10897 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10898 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10899 sh_info fields. Hence we could get the situation
10900 where elfsec is 0. */
10901 if (elfsec == 0)
10902 {
10903 const struct elf_backend_data *bed
10904 = get_elf_backend_data (s->owner);
10905 if (bed->link_order_error_handler)
d003868e
AM
10906 bed->link_order_error_handler
10907 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10908 return 0;
10909 }
10910 else
10911 {
10912 s = elf_shdrp[elfsec]->bfd_section;
10913 return s->output_section->vma + s->output_offset;
10914 }
0b52efa6
PB
10915}
10916
10917
10918/* Compare two sections based on the locations of the sections they are
10919 linked to. Used by elf_fixup_link_order. */
10920
10921static int
10922compare_link_order (const void * a, const void * b)
10923{
10924 bfd_vma apos;
10925 bfd_vma bpos;
10926
10927 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10928 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10929 if (apos < bpos)
10930 return -1;
10931 return apos > bpos;
10932}
10933
10934
10935/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10936 order as their linked sections. Returns false if this could not be done
10937 because an output section includes both ordered and unordered
10938 sections. Ideally we'd do this in the linker proper. */
10939
10940static bfd_boolean
10941elf_fixup_link_order (bfd *abfd, asection *o)
10942{
10943 int seen_linkorder;
10944 int seen_other;
10945 int n;
10946 struct bfd_link_order *p;
10947 bfd *sub;
10948 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10949 unsigned elfsec;
0b52efa6 10950 struct bfd_link_order **sections;
d33cdfe3 10951 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10952 bfd_vma offset;
3b36f7e6 10953
d33cdfe3
L
10954 other_sec = NULL;
10955 linkorder_sec = NULL;
0b52efa6
PB
10956 seen_other = 0;
10957 seen_linkorder = 0;
8423293d 10958 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10959 {
d33cdfe3 10960 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10961 {
10962 s = p->u.indirect.section;
d33cdfe3
L
10963 sub = s->owner;
10964 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10965 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10966 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10967 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10968 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10969 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10970 {
10971 seen_linkorder++;
10972 linkorder_sec = s;
10973 }
0b52efa6 10974 else
d33cdfe3
L
10975 {
10976 seen_other++;
10977 other_sec = s;
10978 }
0b52efa6
PB
10979 }
10980 else
10981 seen_other++;
d33cdfe3
L
10982
10983 if (seen_other && seen_linkorder)
10984 {
10985 if (other_sec && linkorder_sec)
10986 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10987 o, linkorder_sec,
10988 linkorder_sec->owner, other_sec,
10989 other_sec->owner);
10990 else
10991 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10992 o);
10993 bfd_set_error (bfd_error_bad_value);
10994 return FALSE;
10995 }
0b52efa6
PB
10996 }
10997
10998 if (!seen_linkorder)
10999 return TRUE;
11000
0b52efa6 11001 sections = (struct bfd_link_order **)
14b1c01e
AM
11002 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11003 if (sections == NULL)
11004 return FALSE;
0b52efa6 11005 seen_linkorder = 0;
3b36f7e6 11006
8423293d 11007 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11008 {
11009 sections[seen_linkorder++] = p;
11010 }
11011 /* Sort the input sections in the order of their linked section. */
11012 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11013 compare_link_order);
11014
11015 /* Change the offsets of the sections. */
11016 offset = 0;
11017 for (n = 0; n < seen_linkorder; n++)
11018 {
11019 s = sections[n]->u.indirect.section;
461686a3 11020 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11021 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11022 sections[n]->offset = offset;
11023 offset += sections[n]->size;
11024 }
11025
4dd07732 11026 free (sections);
0b52efa6
PB
11027 return TRUE;
11028}
11029
9f7c3e5e
AM
11030static void
11031elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11032{
11033 asection *o;
11034
11035 if (flinfo->symstrtab != NULL)
ef10c3ac 11036 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11037 if (flinfo->contents != NULL)
11038 free (flinfo->contents);
11039 if (flinfo->external_relocs != NULL)
11040 free (flinfo->external_relocs);
11041 if (flinfo->internal_relocs != NULL)
11042 free (flinfo->internal_relocs);
11043 if (flinfo->external_syms != NULL)
11044 free (flinfo->external_syms);
11045 if (flinfo->locsym_shndx != NULL)
11046 free (flinfo->locsym_shndx);
11047 if (flinfo->internal_syms != NULL)
11048 free (flinfo->internal_syms);
11049 if (flinfo->indices != NULL)
11050 free (flinfo->indices);
11051 if (flinfo->sections != NULL)
11052 free (flinfo->sections);
9f7c3e5e
AM
11053 if (flinfo->symshndxbuf != NULL)
11054 free (flinfo->symshndxbuf);
11055 for (o = obfd->sections; o != NULL; o = o->next)
11056 {
11057 struct bfd_elf_section_data *esdo = elf_section_data (o);
11058 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11059 free (esdo->rel.hashes);
11060 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11061 free (esdo->rela.hashes);
11062 }
11063}
0b52efa6 11064
c152c796
AM
11065/* Do the final step of an ELF link. */
11066
11067bfd_boolean
11068bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11069{
11070 bfd_boolean dynamic;
11071 bfd_boolean emit_relocs;
11072 bfd *dynobj;
8b127cbc 11073 struct elf_final_link_info flinfo;
91d6fa6a
NC
11074 asection *o;
11075 struct bfd_link_order *p;
11076 bfd *sub;
c152c796
AM
11077 bfd_size_type max_contents_size;
11078 bfd_size_type max_external_reloc_size;
11079 bfd_size_type max_internal_reloc_count;
11080 bfd_size_type max_sym_count;
11081 bfd_size_type max_sym_shndx_count;
c152c796
AM
11082 Elf_Internal_Sym elfsym;
11083 unsigned int i;
11084 Elf_Internal_Shdr *symtab_hdr;
11085 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11086 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11087 struct elf_outext_info eoinfo;
11088 bfd_boolean merged;
11089 size_t relativecount = 0;
11090 asection *reldyn = 0;
11091 bfd_size_type amt;
104d59d1
JM
11092 asection *attr_section = NULL;
11093 bfd_vma attr_size = 0;
11094 const char *std_attrs_section;
c152c796
AM
11095
11096 if (! is_elf_hash_table (info->hash))
11097 return FALSE;
11098
0e1862bb 11099 if (bfd_link_pic (info))
c152c796
AM
11100 abfd->flags |= DYNAMIC;
11101
11102 dynamic = elf_hash_table (info)->dynamic_sections_created;
11103 dynobj = elf_hash_table (info)->dynobj;
11104
0e1862bb 11105 emit_relocs = (bfd_link_relocatable (info)
a4676736 11106 || info->emitrelocations);
c152c796 11107
8b127cbc
AM
11108 flinfo.info = info;
11109 flinfo.output_bfd = abfd;
ef10c3ac 11110 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11111 if (flinfo.symstrtab == NULL)
c152c796
AM
11112 return FALSE;
11113
11114 if (! dynamic)
11115 {
8b127cbc
AM
11116 flinfo.hash_sec = NULL;
11117 flinfo.symver_sec = NULL;
c152c796
AM
11118 }
11119 else
11120 {
3d4d4302 11121 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11122 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11123 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11124 /* Note that it is OK if symver_sec is NULL. */
11125 }
11126
8b127cbc
AM
11127 flinfo.contents = NULL;
11128 flinfo.external_relocs = NULL;
11129 flinfo.internal_relocs = NULL;
11130 flinfo.external_syms = NULL;
11131 flinfo.locsym_shndx = NULL;
11132 flinfo.internal_syms = NULL;
11133 flinfo.indices = NULL;
11134 flinfo.sections = NULL;
8b127cbc 11135 flinfo.symshndxbuf = NULL;
ffbc01cc 11136 flinfo.filesym_count = 0;
c152c796 11137
104d59d1
JM
11138 /* The object attributes have been merged. Remove the input
11139 sections from the link, and set the contents of the output
11140 secton. */
11141 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11142 for (o = abfd->sections; o != NULL; o = o->next)
11143 {
11144 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11145 || strcmp (o->name, ".gnu.attributes") == 0)
11146 {
11147 for (p = o->map_head.link_order; p != NULL; p = p->next)
11148 {
11149 asection *input_section;
11150
11151 if (p->type != bfd_indirect_link_order)
11152 continue;
11153 input_section = p->u.indirect.section;
11154 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11155 elf_link_input_bfd ignores this section. */
11156 input_section->flags &= ~SEC_HAS_CONTENTS;
11157 }
a0c8462f 11158
104d59d1
JM
11159 attr_size = bfd_elf_obj_attr_size (abfd);
11160 if (attr_size)
11161 {
11162 bfd_set_section_size (abfd, o, attr_size);
11163 attr_section = o;
11164 /* Skip this section later on. */
11165 o->map_head.link_order = NULL;
11166 }
11167 else
11168 o->flags |= SEC_EXCLUDE;
11169 }
11170 }
11171
c152c796
AM
11172 /* Count up the number of relocations we will output for each output
11173 section, so that we know the sizes of the reloc sections. We
11174 also figure out some maximum sizes. */
11175 max_contents_size = 0;
11176 max_external_reloc_size = 0;
11177 max_internal_reloc_count = 0;
11178 max_sym_count = 0;
11179 max_sym_shndx_count = 0;
11180 merged = FALSE;
11181 for (o = abfd->sections; o != NULL; o = o->next)
11182 {
11183 struct bfd_elf_section_data *esdo = elf_section_data (o);
11184 o->reloc_count = 0;
11185
8423293d 11186 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11187 {
11188 unsigned int reloc_count = 0;
491d01d3 11189 unsigned int additional_reloc_count = 0;
c152c796 11190 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11191
11192 if (p->type == bfd_section_reloc_link_order
11193 || p->type == bfd_symbol_reloc_link_order)
11194 reloc_count = 1;
11195 else if (p->type == bfd_indirect_link_order)
11196 {
11197 asection *sec;
11198
11199 sec = p->u.indirect.section;
11200 esdi = elf_section_data (sec);
11201
11202 /* Mark all sections which are to be included in the
11203 link. This will normally be every section. We need
11204 to do this so that we can identify any sections which
11205 the linker has decided to not include. */
11206 sec->linker_mark = TRUE;
11207
11208 if (sec->flags & SEC_MERGE)
11209 merged = TRUE;
11210
aed64b35
L
11211 if (esdo->this_hdr.sh_type == SHT_REL
11212 || esdo->this_hdr.sh_type == SHT_RELA)
11213 /* Some backends use reloc_count in relocation sections
11214 to count particular types of relocs. Of course,
11215 reloc sections themselves can't have relocations. */
11216 reloc_count = 0;
0e1862bb 11217 else if (emit_relocs)
491d01d3
YU
11218 {
11219 reloc_count = sec->reloc_count;
11220 if (bed->elf_backend_count_additional_relocs)
11221 {
11222 int c;
11223 c = (*bed->elf_backend_count_additional_relocs) (sec);
11224 additional_reloc_count += c;
11225 }
11226 }
c152c796 11227 else if (bed->elf_backend_count_relocs)
58217f29 11228 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 11229
eea6121a
AM
11230 if (sec->rawsize > max_contents_size)
11231 max_contents_size = sec->rawsize;
11232 if (sec->size > max_contents_size)
11233 max_contents_size = sec->size;
c152c796
AM
11234
11235 /* We are interested in just local symbols, not all
11236 symbols. */
11237 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11238 && (sec->owner->flags & DYNAMIC) == 0)
11239 {
11240 size_t sym_count;
11241
11242 if (elf_bad_symtab (sec->owner))
11243 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11244 / bed->s->sizeof_sym);
11245 else
11246 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11247
11248 if (sym_count > max_sym_count)
11249 max_sym_count = sym_count;
11250
11251 if (sym_count > max_sym_shndx_count
6a40cf0c 11252 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11253 max_sym_shndx_count = sym_count;
11254
11255 if ((sec->flags & SEC_RELOC) != 0)
11256 {
d4730f92 11257 size_t ext_size = 0;
c152c796 11258
d4730f92
BS
11259 if (esdi->rel.hdr != NULL)
11260 ext_size = esdi->rel.hdr->sh_size;
11261 if (esdi->rela.hdr != NULL)
11262 ext_size += esdi->rela.hdr->sh_size;
7326c758 11263
c152c796
AM
11264 if (ext_size > max_external_reloc_size)
11265 max_external_reloc_size = ext_size;
11266 if (sec->reloc_count > max_internal_reloc_count)
11267 max_internal_reloc_count = sec->reloc_count;
11268 }
11269 }
11270 }
11271
11272 if (reloc_count == 0)
11273 continue;
11274
491d01d3 11275 reloc_count += additional_reloc_count;
c152c796
AM
11276 o->reloc_count += reloc_count;
11277
0e1862bb 11278 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11279 {
d4730f92 11280 if (esdi->rel.hdr)
491d01d3
YU
11281 {
11282 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11283 esdo->rel.count += additional_reloc_count;
11284 }
d4730f92 11285 if (esdi->rela.hdr)
491d01d3
YU
11286 {
11287 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11288 esdo->rela.count += additional_reloc_count;
11289 }
d4730f92
BS
11290 }
11291 else
11292 {
11293 if (o->use_rela_p)
11294 esdo->rela.count += reloc_count;
2c2b4ed4 11295 else
d4730f92 11296 esdo->rel.count += reloc_count;
c152c796 11297 }
c152c796
AM
11298 }
11299
11300 if (o->reloc_count > 0)
11301 o->flags |= SEC_RELOC;
11302 else
11303 {
11304 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11305 set it (this is probably a bug) and if it is set
11306 assign_section_numbers will create a reloc section. */
11307 o->flags &=~ SEC_RELOC;
11308 }
11309
11310 /* If the SEC_ALLOC flag is not set, force the section VMA to
11311 zero. This is done in elf_fake_sections as well, but forcing
11312 the VMA to 0 here will ensure that relocs against these
11313 sections are handled correctly. */
11314 if ((o->flags & SEC_ALLOC) == 0
11315 && ! o->user_set_vma)
11316 o->vma = 0;
11317 }
11318
0e1862bb 11319 if (! bfd_link_relocatable (info) && merged)
c152c796
AM
11320 elf_link_hash_traverse (elf_hash_table (info),
11321 _bfd_elf_link_sec_merge_syms, abfd);
11322
11323 /* Figure out the file positions for everything but the symbol table
11324 and the relocs. We set symcount to force assign_section_numbers
11325 to create a symbol table. */
8539e4e8 11326 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11327 BFD_ASSERT (! abfd->output_has_begun);
11328 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11329 goto error_return;
11330
ee75fd95 11331 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11332 for (o = abfd->sections; o != NULL; o = o->next)
11333 {
d4730f92 11334 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11335 if ((o->flags & SEC_RELOC) != 0)
11336 {
d4730f92
BS
11337 if (esdo->rel.hdr
11338 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11339 goto error_return;
11340
d4730f92
BS
11341 if (esdo->rela.hdr
11342 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11343 goto error_return;
11344 }
11345
11346 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11347 to count upwards while actually outputting the relocations. */
d4730f92
BS
11348 esdo->rel.count = 0;
11349 esdo->rela.count = 0;
0ce398f1
L
11350
11351 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11352 {
11353 /* Cache the section contents so that they can be compressed
11354 later. Use bfd_malloc since it will be freed by
11355 bfd_compress_section_contents. */
11356 unsigned char *contents = esdo->this_hdr.contents;
11357 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11358 abort ();
11359 contents
11360 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11361 if (contents == NULL)
11362 goto error_return;
11363 esdo->this_hdr.contents = contents;
11364 }
c152c796
AM
11365 }
11366
c152c796 11367 /* We have now assigned file positions for all the sections except
a485e98e
AM
11368 .symtab, .strtab, and non-loaded reloc sections. We start the
11369 .symtab section at the current file position, and write directly
11370 to it. We build the .strtab section in memory. */
c152c796
AM
11371 bfd_get_symcount (abfd) = 0;
11372 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11373 /* sh_name is set in prep_headers. */
11374 symtab_hdr->sh_type = SHT_SYMTAB;
11375 /* sh_flags, sh_addr and sh_size all start off zero. */
11376 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11377 /* sh_link is set in assign_section_numbers. */
11378 /* sh_info is set below. */
11379 /* sh_offset is set just below. */
72de5009 11380 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11381
ef10c3ac
L
11382 if (max_sym_count < 20)
11383 max_sym_count = 20;
11384 elf_hash_table (info)->strtabsize = max_sym_count;
11385 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11386 elf_hash_table (info)->strtab
11387 = (struct elf_sym_strtab *) bfd_malloc (amt);
11388 if (elf_hash_table (info)->strtab == NULL)
c152c796 11389 goto error_return;
ef10c3ac
L
11390 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11391 flinfo.symshndxbuf
11392 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11393 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11394
8539e4e8 11395 if (info->strip != strip_all || emit_relocs)
c152c796 11396 {
8539e4e8
AM
11397 file_ptr off = elf_next_file_pos (abfd);
11398
11399 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11400
11401 /* Note that at this point elf_next_file_pos (abfd) is
11402 incorrect. We do not yet know the size of the .symtab section.
11403 We correct next_file_pos below, after we do know the size. */
11404
11405 /* Start writing out the symbol table. The first symbol is always a
11406 dummy symbol. */
c152c796
AM
11407 elfsym.st_value = 0;
11408 elfsym.st_size = 0;
11409 elfsym.st_info = 0;
11410 elfsym.st_other = 0;
11411 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11412 elfsym.st_target_internal = 0;
ef10c3ac
L
11413 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11414 bfd_und_section_ptr, NULL) != 1)
c152c796 11415 goto error_return;
c152c796 11416
8539e4e8
AM
11417 /* Output a symbol for each section. We output these even if we are
11418 discarding local symbols, since they are used for relocs. These
11419 symbols have no names. We store the index of each one in the
11420 index field of the section, so that we can find it again when
11421 outputting relocs. */
11422
c152c796
AM
11423 elfsym.st_size = 0;
11424 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11425 elfsym.st_other = 0;
f0b5bb34 11426 elfsym.st_value = 0;
35fc36a8 11427 elfsym.st_target_internal = 0;
c152c796
AM
11428 for (i = 1; i < elf_numsections (abfd); i++)
11429 {
11430 o = bfd_section_from_elf_index (abfd, i);
11431 if (o != NULL)
f0b5bb34
AM
11432 {
11433 o->target_index = bfd_get_symcount (abfd);
11434 elfsym.st_shndx = i;
0e1862bb 11435 if (!bfd_link_relocatable (info))
f0b5bb34 11436 elfsym.st_value = o->vma;
ef10c3ac
L
11437 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11438 NULL) != 1)
f0b5bb34
AM
11439 goto error_return;
11440 }
c152c796
AM
11441 }
11442 }
11443
11444 /* Allocate some memory to hold information read in from the input
11445 files. */
11446 if (max_contents_size != 0)
11447 {
8b127cbc
AM
11448 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11449 if (flinfo.contents == NULL)
c152c796
AM
11450 goto error_return;
11451 }
11452
11453 if (max_external_reloc_size != 0)
11454 {
8b127cbc
AM
11455 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11456 if (flinfo.external_relocs == NULL)
c152c796
AM
11457 goto error_return;
11458 }
11459
11460 if (max_internal_reloc_count != 0)
11461 {
11462 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11463 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11464 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11465 if (flinfo.internal_relocs == NULL)
c152c796
AM
11466 goto error_return;
11467 }
11468
11469 if (max_sym_count != 0)
11470 {
11471 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11472 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11473 if (flinfo.external_syms == NULL)
c152c796
AM
11474 goto error_return;
11475
11476 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11477 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11478 if (flinfo.internal_syms == NULL)
c152c796
AM
11479 goto error_return;
11480
11481 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11482 flinfo.indices = (long int *) bfd_malloc (amt);
11483 if (flinfo.indices == NULL)
c152c796
AM
11484 goto error_return;
11485
11486 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11487 flinfo.sections = (asection **) bfd_malloc (amt);
11488 if (flinfo.sections == NULL)
c152c796
AM
11489 goto error_return;
11490 }
11491
11492 if (max_sym_shndx_count != 0)
11493 {
11494 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11495 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11496 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11497 goto error_return;
11498 }
11499
11500 if (elf_hash_table (info)->tls_sec)
11501 {
11502 bfd_vma base, end = 0;
11503 asection *sec;
11504
11505 for (sec = elf_hash_table (info)->tls_sec;
11506 sec && (sec->flags & SEC_THREAD_LOCAL);
11507 sec = sec->next)
11508 {
3a800eb9 11509 bfd_size_type size = sec->size;
c152c796 11510
3a800eb9
AM
11511 if (size == 0
11512 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11513 {
91d6fa6a
NC
11514 struct bfd_link_order *ord = sec->map_tail.link_order;
11515
11516 if (ord != NULL)
11517 size = ord->offset + ord->size;
c152c796
AM
11518 }
11519 end = sec->vma + size;
11520 }
11521 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
11522 /* Only align end of TLS section if static TLS doesn't have special
11523 alignment requirements. */
11524 if (bed->static_tls_alignment == 1)
11525 end = align_power (end,
11526 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
11527 elf_hash_table (info)->tls_size = end - base;
11528 }
11529
0b52efa6
PB
11530 /* Reorder SHF_LINK_ORDER sections. */
11531 for (o = abfd->sections; o != NULL; o = o->next)
11532 {
11533 if (!elf_fixup_link_order (abfd, o))
11534 return FALSE;
11535 }
11536
2f0c68f2
CM
11537 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11538 return FALSE;
11539
c152c796
AM
11540 /* Since ELF permits relocations to be against local symbols, we
11541 must have the local symbols available when we do the relocations.
11542 Since we would rather only read the local symbols once, and we
11543 would rather not keep them in memory, we handle all the
11544 relocations for a single input file at the same time.
11545
11546 Unfortunately, there is no way to know the total number of local
11547 symbols until we have seen all of them, and the local symbol
11548 indices precede the global symbol indices. This means that when
11549 we are generating relocatable output, and we see a reloc against
11550 a global symbol, we can not know the symbol index until we have
11551 finished examining all the local symbols to see which ones we are
11552 going to output. To deal with this, we keep the relocations in
11553 memory, and don't output them until the end of the link. This is
11554 an unfortunate waste of memory, but I don't see a good way around
11555 it. Fortunately, it only happens when performing a relocatable
11556 link, which is not the common case. FIXME: If keep_memory is set
11557 we could write the relocs out and then read them again; I don't
11558 know how bad the memory loss will be. */
11559
c72f2fb2 11560 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11561 sub->output_has_begun = FALSE;
11562 for (o = abfd->sections; o != NULL; o = o->next)
11563 {
8423293d 11564 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11565 {
11566 if (p->type == bfd_indirect_link_order
11567 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11568 == bfd_target_elf_flavour)
11569 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11570 {
11571 if (! sub->output_has_begun)
11572 {
8b127cbc 11573 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11574 goto error_return;
11575 sub->output_has_begun = TRUE;
11576 }
11577 }
11578 else if (p->type == bfd_section_reloc_link_order
11579 || p->type == bfd_symbol_reloc_link_order)
11580 {
11581 if (! elf_reloc_link_order (abfd, info, o, p))
11582 goto error_return;
11583 }
11584 else
11585 {
11586 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11587 {
11588 if (p->type == bfd_indirect_link_order
11589 && (bfd_get_flavour (sub)
11590 == bfd_target_elf_flavour)
11591 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11592 != bed->s->elfclass))
11593 {
11594 const char *iclass, *oclass;
11595
aebf9be7 11596 switch (bed->s->elfclass)
351f65ca 11597 {
aebf9be7
NC
11598 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11599 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11600 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11601 default: abort ();
351f65ca 11602 }
aebf9be7
NC
11603
11604 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11605 {
aebf9be7
NC
11606 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11607 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11608 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11609 default: abort ();
351f65ca
L
11610 }
11611
11612 bfd_set_error (bfd_error_wrong_format);
11613 (*_bfd_error_handler)
11614 (_("%B: file class %s incompatible with %s"),
11615 sub, iclass, oclass);
11616 }
11617
11618 goto error_return;
11619 }
c152c796
AM
11620 }
11621 }
11622 }
11623
c0f00686
L
11624 /* Free symbol buffer if needed. */
11625 if (!info->reduce_memory_overheads)
11626 {
c72f2fb2 11627 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11628 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11629 && elf_tdata (sub)->symbuf)
c0f00686
L
11630 {
11631 free (elf_tdata (sub)->symbuf);
11632 elf_tdata (sub)->symbuf = NULL;
11633 }
11634 }
11635
c152c796
AM
11636 /* Output any global symbols that got converted to local in a
11637 version script or due to symbol visibility. We do this in a
11638 separate step since ELF requires all local symbols to appear
11639 prior to any global symbols. FIXME: We should only do this if
11640 some global symbols were, in fact, converted to become local.
11641 FIXME: Will this work correctly with the Irix 5 linker? */
11642 eoinfo.failed = FALSE;
8b127cbc 11643 eoinfo.flinfo = &flinfo;
c152c796 11644 eoinfo.localsyms = TRUE;
34a79995 11645 eoinfo.file_sym_done = FALSE;
7686d77d 11646 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11647 if (eoinfo.failed)
11648 return FALSE;
11649
4e617b1e
PB
11650 /* If backend needs to output some local symbols not present in the hash
11651 table, do it now. */
8539e4e8
AM
11652 if (bed->elf_backend_output_arch_local_syms
11653 && (info->strip != strip_all || emit_relocs))
4e617b1e 11654 {
6e0b88f1 11655 typedef int (*out_sym_func)
4e617b1e
PB
11656 (void *, const char *, Elf_Internal_Sym *, asection *,
11657 struct elf_link_hash_entry *);
11658
11659 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11660 (abfd, info, &flinfo,
11661 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11662 return FALSE;
11663 }
11664
c152c796
AM
11665 /* That wrote out all the local symbols. Finish up the symbol table
11666 with the global symbols. Even if we want to strip everything we
11667 can, we still need to deal with those global symbols that got
11668 converted to local in a version script. */
11669
11670 /* The sh_info field records the index of the first non local symbol. */
11671 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11672
11673 if (dynamic
cae1fbbb
L
11674 && elf_hash_table (info)->dynsym != NULL
11675 && (elf_hash_table (info)->dynsym->output_section
11676 != bfd_abs_section_ptr))
c152c796
AM
11677 {
11678 Elf_Internal_Sym sym;
cae1fbbb 11679 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
c152c796
AM
11680 long last_local = 0;
11681
11682 /* Write out the section symbols for the output sections. */
0e1862bb
L
11683 if (bfd_link_pic (info)
11684 || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
11685 {
11686 asection *s;
11687
11688 sym.st_size = 0;
11689 sym.st_name = 0;
11690 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11691 sym.st_other = 0;
35fc36a8 11692 sym.st_target_internal = 0;
c152c796
AM
11693
11694 for (s = abfd->sections; s != NULL; s = s->next)
11695 {
11696 int indx;
11697 bfd_byte *dest;
11698 long dynindx;
11699
c152c796 11700 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11701 if (dynindx <= 0)
11702 continue;
11703 indx = elf_section_data (s)->this_idx;
c152c796
AM
11704 BFD_ASSERT (indx > 0);
11705 sym.st_shndx = indx;
c0d5a53d
L
11706 if (! check_dynsym (abfd, &sym))
11707 return FALSE;
c152c796
AM
11708 sym.st_value = s->vma;
11709 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
11710 if (last_local < dynindx)
11711 last_local = dynindx;
c152c796
AM
11712 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11713 }
c152c796
AM
11714 }
11715
11716 /* Write out the local dynsyms. */
11717 if (elf_hash_table (info)->dynlocal)
11718 {
11719 struct elf_link_local_dynamic_entry *e;
11720 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11721 {
11722 asection *s;
11723 bfd_byte *dest;
11724
935bd1e0 11725 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11726 Note that we saved a word of storage and overwrote
11727 the original st_name with the dynstr_index. */
11728 sym = e->isym;
935bd1e0 11729 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11730
cb33740c
AM
11731 s = bfd_section_from_elf_index (e->input_bfd,
11732 e->isym.st_shndx);
11733 if (s != NULL)
c152c796 11734 {
c152c796
AM
11735 sym.st_shndx =
11736 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11737 if (! check_dynsym (abfd, &sym))
11738 return FALSE;
c152c796
AM
11739 sym.st_value = (s->output_section->vma
11740 + s->output_offset
11741 + e->isym.st_value);
11742 }
11743
11744 if (last_local < e->dynindx)
11745 last_local = e->dynindx;
11746
11747 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11748 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11749 }
11750 }
11751
cae1fbbb 11752 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
c152c796
AM
11753 last_local + 1;
11754 }
11755
11756 /* We get the global symbols from the hash table. */
11757 eoinfo.failed = FALSE;
11758 eoinfo.localsyms = FALSE;
8b127cbc 11759 eoinfo.flinfo = &flinfo;
7686d77d 11760 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11761 if (eoinfo.failed)
11762 return FALSE;
11763
11764 /* If backend needs to output some symbols not present in the hash
11765 table, do it now. */
8539e4e8
AM
11766 if (bed->elf_backend_output_arch_syms
11767 && (info->strip != strip_all || emit_relocs))
c152c796 11768 {
6e0b88f1 11769 typedef int (*out_sym_func)
c152c796
AM
11770 (void *, const char *, Elf_Internal_Sym *, asection *,
11771 struct elf_link_hash_entry *);
11772
11773 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
11774 (abfd, info, &flinfo,
11775 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
11776 return FALSE;
11777 }
11778
ef10c3ac
L
11779 /* Finalize the .strtab section. */
11780 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11781
11782 /* Swap out the .strtab section. */
11783 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
11784 return FALSE;
11785
11786 /* Now we know the size of the symtab section. */
c152c796
AM
11787 if (bfd_get_symcount (abfd) > 0)
11788 {
ee3b52e9
L
11789 /* Finish up and write out the symbol string table (.strtab)
11790 section. */
11791 Elf_Internal_Shdr *symstrtab_hdr;
8539e4e8
AM
11792 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11793
6a40cf0c
NC
11794 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11795 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
8539e4e8
AM
11796 {
11797 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11798 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11799 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11800 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11801 symtab_shndx_hdr->sh_size = amt;
11802
11803 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11804 off, TRUE);
11805
11806 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11807 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11808 return FALSE;
11809 }
ee3b52e9
L
11810
11811 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11812 /* sh_name was set in prep_headers. */
11813 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 11814 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 11815 symstrtab_hdr->sh_addr = 0;
ef10c3ac 11816 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
11817 symstrtab_hdr->sh_entsize = 0;
11818 symstrtab_hdr->sh_link = 0;
11819 symstrtab_hdr->sh_info = 0;
11820 /* sh_offset is set just below. */
11821 symstrtab_hdr->sh_addralign = 1;
11822
11823 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11824 off, TRUE);
11825 elf_next_file_pos (abfd) = off;
11826
c152c796 11827 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 11828 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
11829 return FALSE;
11830 }
11831
11832 /* Adjust the relocs to have the correct symbol indices. */
11833 for (o = abfd->sections; o != NULL; o = o->next)
11834 {
d4730f92 11835 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 11836 bfd_boolean sort;
c152c796
AM
11837 if ((o->flags & SEC_RELOC) == 0)
11838 continue;
11839
28dbcedc 11840 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3
AM
11841 if (esdo->rel.hdr != NULL
11842 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11843 return FALSE;
11844 if (esdo->rela.hdr != NULL
11845 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11846 return FALSE;
c152c796
AM
11847
11848 /* Set the reloc_count field to 0 to prevent write_relocs from
11849 trying to swap the relocs out itself. */
11850 o->reloc_count = 0;
11851 }
11852
11853 if (dynamic && info->combreloc && dynobj != NULL)
11854 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11855
11856 /* If we are linking against a dynamic object, or generating a
11857 shared library, finish up the dynamic linking information. */
11858 if (dynamic)
11859 {
11860 bfd_byte *dyncon, *dynconend;
11861
11862 /* Fix up .dynamic entries. */
3d4d4302 11863 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
11864 BFD_ASSERT (o != NULL);
11865
11866 dyncon = o->contents;
eea6121a 11867 dynconend = o->contents + o->size;
c152c796
AM
11868 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11869 {
11870 Elf_Internal_Dyn dyn;
11871 const char *name;
11872 unsigned int type;
11873
11874 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11875
11876 switch (dyn.d_tag)
11877 {
11878 default:
11879 continue;
11880 case DT_NULL:
11881 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11882 {
11883 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11884 {
11885 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11886 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11887 default: continue;
11888 }
11889 dyn.d_un.d_val = relativecount;
11890 relativecount = 0;
11891 break;
11892 }
11893 continue;
11894
11895 case DT_INIT:
11896 name = info->init_function;
11897 goto get_sym;
11898 case DT_FINI:
11899 name = info->fini_function;
11900 get_sym:
11901 {
11902 struct elf_link_hash_entry *h;
11903
11904 h = elf_link_hash_lookup (elf_hash_table (info), name,
11905 FALSE, FALSE, TRUE);
11906 if (h != NULL
11907 && (h->root.type == bfd_link_hash_defined
11908 || h->root.type == bfd_link_hash_defweak))
11909 {
bef26483 11910 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11911 o = h->root.u.def.section;
11912 if (o->output_section != NULL)
bef26483 11913 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11914 + o->output_offset);
11915 else
11916 {
11917 /* The symbol is imported from another shared
11918 library and does not apply to this one. */
bef26483 11919 dyn.d_un.d_ptr = 0;
c152c796
AM
11920 }
11921 break;
11922 }
11923 }
11924 continue;
11925
11926 case DT_PREINIT_ARRAYSZ:
11927 name = ".preinit_array";
4ade44b7 11928 goto get_out_size;
c152c796
AM
11929 case DT_INIT_ARRAYSZ:
11930 name = ".init_array";
4ade44b7 11931 goto get_out_size;
c152c796
AM
11932 case DT_FINI_ARRAYSZ:
11933 name = ".fini_array";
4ade44b7 11934 get_out_size:
c152c796
AM
11935 o = bfd_get_section_by_name (abfd, name);
11936 if (o == NULL)
11937 {
11938 (*_bfd_error_handler)
4ade44b7 11939 (_("could not find section %s"), name);
c152c796
AM
11940 goto error_return;
11941 }
eea6121a 11942 if (o->size == 0)
c152c796
AM
11943 (*_bfd_error_handler)
11944 (_("warning: %s section has zero size"), name);
eea6121a 11945 dyn.d_un.d_val = o->size;
c152c796
AM
11946 break;
11947
11948 case DT_PREINIT_ARRAY:
11949 name = ".preinit_array";
4ade44b7 11950 goto get_out_vma;
c152c796
AM
11951 case DT_INIT_ARRAY:
11952 name = ".init_array";
4ade44b7 11953 goto get_out_vma;
c152c796
AM
11954 case DT_FINI_ARRAY:
11955 name = ".fini_array";
4ade44b7
AM
11956 get_out_vma:
11957 o = bfd_get_section_by_name (abfd, name);
11958 goto do_vma;
c152c796
AM
11959
11960 case DT_HASH:
11961 name = ".hash";
11962 goto get_vma;
fdc90cb4
JJ
11963 case DT_GNU_HASH:
11964 name = ".gnu.hash";
11965 goto get_vma;
c152c796
AM
11966 case DT_STRTAB:
11967 name = ".dynstr";
11968 goto get_vma;
11969 case DT_SYMTAB:
11970 name = ".dynsym";
11971 goto get_vma;
11972 case DT_VERDEF:
11973 name = ".gnu.version_d";
11974 goto get_vma;
11975 case DT_VERNEED:
11976 name = ".gnu.version_r";
11977 goto get_vma;
11978 case DT_VERSYM:
11979 name = ".gnu.version";
11980 get_vma:
4ade44b7
AM
11981 o = bfd_get_linker_section (dynobj, name);
11982 do_vma:
c152c796
AM
11983 if (o == NULL)
11984 {
11985 (*_bfd_error_handler)
4ade44b7 11986 (_("could not find section %s"), name);
c152c796
AM
11987 goto error_return;
11988 }
894891db
NC
11989 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11990 {
11991 (*_bfd_error_handler)
11992 (_("warning: section '%s' is being made into a note"), name);
11993 bfd_set_error (bfd_error_nonrepresentable_section);
11994 goto error_return;
11995 }
4ade44b7 11996 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
11997 break;
11998
11999 case DT_REL:
12000 case DT_RELA:
12001 case DT_RELSZ:
12002 case DT_RELASZ:
12003 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12004 type = SHT_REL;
12005 else
12006 type = SHT_RELA;
12007 dyn.d_un.d_val = 0;
bef26483 12008 dyn.d_un.d_ptr = 0;
c152c796
AM
12009 for (i = 1; i < elf_numsections (abfd); i++)
12010 {
12011 Elf_Internal_Shdr *hdr;
12012
12013 hdr = elf_elfsections (abfd)[i];
12014 if (hdr->sh_type == type
12015 && (hdr->sh_flags & SHF_ALLOC) != 0)
12016 {
12017 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12018 dyn.d_un.d_val += hdr->sh_size;
12019 else
12020 {
bef26483
AM
12021 if (dyn.d_un.d_ptr == 0
12022 || hdr->sh_addr < dyn.d_un.d_ptr)
12023 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
12024 }
12025 }
12026 }
12027 break;
12028 }
12029 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12030 }
12031 }
12032
12033 /* If we have created any dynamic sections, then output them. */
12034 if (dynobj != NULL)
12035 {
12036 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12037 goto error_return;
12038
943284cc 12039 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12040 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12041 || info->error_textrel)
3d4d4302 12042 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12043 {
12044 bfd_byte *dyncon, *dynconend;
12045
943284cc
DJ
12046 dyncon = o->contents;
12047 dynconend = o->contents + o->size;
12048 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12049 {
12050 Elf_Internal_Dyn dyn;
12051
12052 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12053
12054 if (dyn.d_tag == DT_TEXTREL)
12055 {
c192a133
AM
12056 if (info->error_textrel)
12057 info->callbacks->einfo
12058 (_("%P%X: read-only segment has dynamic relocations.\n"));
12059 else
12060 info->callbacks->einfo
12061 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12062 break;
12063 }
12064 }
12065 }
12066
c152c796
AM
12067 for (o = dynobj->sections; o != NULL; o = o->next)
12068 {
12069 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12070 || o->size == 0
c152c796
AM
12071 || o->output_section == bfd_abs_section_ptr)
12072 continue;
12073 if ((o->flags & SEC_LINKER_CREATED) == 0)
12074 {
12075 /* At this point, we are only interested in sections
12076 created by _bfd_elf_link_create_dynamic_sections. */
12077 continue;
12078 }
3722b82f
AM
12079 if (elf_hash_table (info)->stab_info.stabstr == o)
12080 continue;
eea6121a
AM
12081 if (elf_hash_table (info)->eh_info.hdr_sec == o)
12082 continue;
3d4d4302 12083 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12084 {
12085 if (! bfd_set_section_contents (abfd, o->output_section,
12086 o->contents,
37b01f6a
DG
12087 (file_ptr) o->output_offset
12088 * bfd_octets_per_byte (abfd),
eea6121a 12089 o->size))
c152c796
AM
12090 goto error_return;
12091 }
12092 else
12093 {
12094 /* The contents of the .dynstr section are actually in a
12095 stringtab. */
8539e4e8
AM
12096 file_ptr off;
12097
c152c796
AM
12098 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12099 if (bfd_seek (abfd, off, SEEK_SET) != 0
12100 || ! _bfd_elf_strtab_emit (abfd,
12101 elf_hash_table (info)->dynstr))
12102 goto error_return;
12103 }
12104 }
12105 }
12106
0e1862bb 12107 if (bfd_link_relocatable (info))
c152c796
AM
12108 {
12109 bfd_boolean failed = FALSE;
12110
12111 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12112 if (failed)
12113 goto error_return;
12114 }
12115
12116 /* If we have optimized stabs strings, output them. */
3722b82f 12117 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
12118 {
12119 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
12120 goto error_return;
12121 }
12122
9f7c3e5e
AM
12123 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12124 goto error_return;
c152c796 12125
9f7c3e5e 12126 elf_final_link_free (abfd, &flinfo);
c152c796 12127
12bd6957 12128 elf_linker (abfd) = TRUE;
c152c796 12129
104d59d1
JM
12130 if (attr_section)
12131 {
a50b1753 12132 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12133 if (contents == NULL)
d0f16d5e 12134 return FALSE; /* Bail out and fail. */
104d59d1
JM
12135 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12136 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12137 free (contents);
12138 }
12139
c152c796
AM
12140 return TRUE;
12141
12142 error_return:
9f7c3e5e 12143 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12144 return FALSE;
12145}
12146\f
5241d853
RS
12147/* Initialize COOKIE for input bfd ABFD. */
12148
12149static bfd_boolean
12150init_reloc_cookie (struct elf_reloc_cookie *cookie,
12151 struct bfd_link_info *info, bfd *abfd)
12152{
12153 Elf_Internal_Shdr *symtab_hdr;
12154 const struct elf_backend_data *bed;
12155
12156 bed = get_elf_backend_data (abfd);
12157 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12158
12159 cookie->abfd = abfd;
12160 cookie->sym_hashes = elf_sym_hashes (abfd);
12161 cookie->bad_symtab = elf_bad_symtab (abfd);
12162 if (cookie->bad_symtab)
12163 {
12164 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12165 cookie->extsymoff = 0;
12166 }
12167 else
12168 {
12169 cookie->locsymcount = symtab_hdr->sh_info;
12170 cookie->extsymoff = symtab_hdr->sh_info;
12171 }
12172
12173 if (bed->s->arch_size == 32)
12174 cookie->r_sym_shift = 8;
12175 else
12176 cookie->r_sym_shift = 32;
12177
12178 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12179 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12180 {
12181 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12182 cookie->locsymcount, 0,
12183 NULL, NULL, NULL);
12184 if (cookie->locsyms == NULL)
12185 {
12186 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12187 return FALSE;
12188 }
12189 if (info->keep_memory)
12190 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12191 }
12192 return TRUE;
12193}
12194
12195/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12196
12197static void
12198fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12199{
12200 Elf_Internal_Shdr *symtab_hdr;
12201
12202 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12203 if (cookie->locsyms != NULL
12204 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12205 free (cookie->locsyms);
12206}
12207
12208/* Initialize the relocation information in COOKIE for input section SEC
12209 of input bfd ABFD. */
12210
12211static bfd_boolean
12212init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12213 struct bfd_link_info *info, bfd *abfd,
12214 asection *sec)
12215{
12216 const struct elf_backend_data *bed;
12217
12218 if (sec->reloc_count == 0)
12219 {
12220 cookie->rels = NULL;
12221 cookie->relend = NULL;
12222 }
12223 else
12224 {
12225 bed = get_elf_backend_data (abfd);
12226
12227 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12228 info->keep_memory);
12229 if (cookie->rels == NULL)
12230 return FALSE;
12231 cookie->rel = cookie->rels;
12232 cookie->relend = (cookie->rels
12233 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12234 }
12235 cookie->rel = cookie->rels;
12236 return TRUE;
12237}
12238
12239/* Free the memory allocated by init_reloc_cookie_rels,
12240 if appropriate. */
12241
12242static void
12243fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12244 asection *sec)
12245{
12246 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12247 free (cookie->rels);
12248}
12249
12250/* Initialize the whole of COOKIE for input section SEC. */
12251
12252static bfd_boolean
12253init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12254 struct bfd_link_info *info,
12255 asection *sec)
12256{
12257 if (!init_reloc_cookie (cookie, info, sec->owner))
12258 goto error1;
12259 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12260 goto error2;
12261 return TRUE;
12262
12263 error2:
12264 fini_reloc_cookie (cookie, sec->owner);
12265 error1:
12266 return FALSE;
12267}
12268
12269/* Free the memory allocated by init_reloc_cookie_for_section,
12270 if appropriate. */
12271
12272static void
12273fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12274 asection *sec)
12275{
12276 fini_reloc_cookie_rels (cookie, sec);
12277 fini_reloc_cookie (cookie, sec->owner);
12278}
12279\f
c152c796
AM
12280/* Garbage collect unused sections. */
12281
07adf181
AM
12282/* Default gc_mark_hook. */
12283
12284asection *
12285_bfd_elf_gc_mark_hook (asection *sec,
12286 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12287 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12288 struct elf_link_hash_entry *h,
12289 Elf_Internal_Sym *sym)
12290{
12291 if (h != NULL)
12292 {
12293 switch (h->root.type)
12294 {
12295 case bfd_link_hash_defined:
12296 case bfd_link_hash_defweak:
12297 return h->root.u.def.section;
12298
12299 case bfd_link_hash_common:
12300 return h->root.u.c.p->section;
12301
12302 default:
12303 break;
12304 }
12305 }
12306 else
12307 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12308
12309 return NULL;
12310}
12311
a6a4679f
AM
12312/* For undefined __start_<name> and __stop_<name> symbols, return the
12313 first input section matching <name>. Return NULL otherwise. */
12314
12315asection *
12316_bfd_elf_is_start_stop (const struct bfd_link_info *info,
12317 struct elf_link_hash_entry *h)
12318{
12319 asection *s;
12320 const char *sec_name;
12321
12322 if (h->root.type != bfd_link_hash_undefined
12323 && h->root.type != bfd_link_hash_undefweak)
12324 return NULL;
12325
12326 s = h->root.u.undef.section;
12327 if (s != NULL)
12328 {
12329 if (s == (asection *) 0 - 1)
12330 return NULL;
12331 return s;
12332 }
12333
12334 sec_name = NULL;
12335 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12336 sec_name = h->root.root.string + 8;
12337 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12338 sec_name = h->root.root.string + 7;
12339
12340 if (sec_name != NULL && *sec_name != '\0')
12341 {
12342 bfd *i;
12343
12344 for (i = info->input_bfds; i != NULL; i = i->link.next)
12345 {
12346 s = bfd_get_section_by_name (i, sec_name);
12347 if (s != NULL)
12348 {
12349 h->root.u.undef.section = s;
12350 break;
12351 }
12352 }
12353 }
12354
12355 if (s == NULL)
12356 h->root.u.undef.section = (asection *) 0 - 1;
12357
12358 return s;
12359}
12360
5241d853
RS
12361/* COOKIE->rel describes a relocation against section SEC, which is
12362 a section we've decided to keep. Return the section that contains
12363 the relocation symbol, or NULL if no section contains it. */
12364
12365asection *
12366_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12367 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12368 struct elf_reloc_cookie *cookie,
12369 bfd_boolean *start_stop)
5241d853
RS
12370{
12371 unsigned long r_symndx;
12372 struct elf_link_hash_entry *h;
12373
12374 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12375 if (r_symndx == STN_UNDEF)
5241d853
RS
12376 return NULL;
12377
12378 if (r_symndx >= cookie->locsymcount
12379 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12380 {
12381 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12382 if (h == NULL)
12383 {
12384 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12385 sec->owner);
12386 return NULL;
12387 }
5241d853
RS
12388 while (h->root.type == bfd_link_hash_indirect
12389 || h->root.type == bfd_link_hash_warning)
12390 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12391 h->mark = 1;
4e6b54a6
AM
12392 /* If this symbol is weak and there is a non-weak definition, we
12393 keep the non-weak definition because many backends put
12394 dynamic reloc info on the non-weak definition for code
12395 handling copy relocs. */
12396 if (h->u.weakdef != NULL)
12397 h->u.weakdef->mark = 1;
1cce69b9 12398
a6a4679f 12399 if (start_stop != NULL)
1cce69b9
AM
12400 {
12401 /* To work around a glibc bug, mark all XXX input sections
12402 when there is an as yet undefined reference to __start_XXX
12403 or __stop_XXX symbols. The linker will later define such
12404 symbols for orphan input sections that have a name
12405 representable as a C identifier. */
a6a4679f 12406 asection *s = _bfd_elf_is_start_stop (info, h);
1cce69b9 12407
a6a4679f 12408 if (s != NULL)
1cce69b9 12409 {
a6a4679f
AM
12410 *start_stop = !s->gc_mark;
12411 return s;
1cce69b9
AM
12412 }
12413 }
12414
5241d853
RS
12415 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12416 }
12417
12418 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12419 &cookie->locsyms[r_symndx]);
12420}
12421
12422/* COOKIE->rel describes a relocation against section SEC, which is
12423 a section we've decided to keep. Mark the section that contains
9d0a14d3 12424 the relocation symbol. */
5241d853
RS
12425
12426bfd_boolean
12427_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12428 asection *sec,
12429 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12430 struct elf_reloc_cookie *cookie)
5241d853
RS
12431{
12432 asection *rsec;
1cce69b9 12433 bfd_boolean start_stop = FALSE;
5241d853 12434
1cce69b9
AM
12435 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12436 while (rsec != NULL)
5241d853 12437 {
1cce69b9
AM
12438 if (!rsec->gc_mark)
12439 {
12440 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12441 || (rsec->owner->flags & DYNAMIC) != 0)
12442 rsec->gc_mark = 1;
12443 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12444 return FALSE;
12445 }
12446 if (!start_stop)
12447 break;
199af150 12448 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12449 }
12450 return TRUE;
12451}
12452
07adf181
AM
12453/* The mark phase of garbage collection. For a given section, mark
12454 it and any sections in this section's group, and all the sections
12455 which define symbols to which it refers. */
12456
ccfa59ea
AM
12457bfd_boolean
12458_bfd_elf_gc_mark (struct bfd_link_info *info,
12459 asection *sec,
6a5bb875 12460 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12461{
12462 bfd_boolean ret;
9d0a14d3 12463 asection *group_sec, *eh_frame;
c152c796
AM
12464
12465 sec->gc_mark = 1;
12466
12467 /* Mark all the sections in the group. */
12468 group_sec = elf_section_data (sec)->next_in_group;
12469 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12470 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12471 return FALSE;
12472
12473 /* Look through the section relocs. */
12474 ret = TRUE;
9d0a14d3
RS
12475 eh_frame = elf_eh_frame_section (sec->owner);
12476 if ((sec->flags & SEC_RELOC) != 0
12477 && sec->reloc_count > 0
12478 && sec != eh_frame)
c152c796 12479 {
5241d853 12480 struct elf_reloc_cookie cookie;
c152c796 12481
5241d853
RS
12482 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12483 ret = FALSE;
c152c796 12484 else
c152c796 12485 {
5241d853 12486 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12487 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12488 {
12489 ret = FALSE;
12490 break;
12491 }
12492 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12493 }
12494 }
9d0a14d3
RS
12495
12496 if (ret && eh_frame && elf_fde_list (sec))
12497 {
12498 struct elf_reloc_cookie cookie;
12499
12500 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12501 ret = FALSE;
12502 else
12503 {
12504 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12505 gc_mark_hook, &cookie))
12506 ret = FALSE;
12507 fini_reloc_cookie_for_section (&cookie, eh_frame);
12508 }
12509 }
12510
2f0c68f2
CM
12511 eh_frame = elf_section_eh_frame_entry (sec);
12512 if (ret && eh_frame && !eh_frame->gc_mark)
12513 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12514 ret = FALSE;
12515
c152c796
AM
12516 return ret;
12517}
12518
3c758495
TG
12519/* Scan and mark sections in a special or debug section group. */
12520
12521static void
12522_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12523{
12524 /* Point to first section of section group. */
12525 asection *ssec;
12526 /* Used to iterate the section group. */
12527 asection *msec;
12528
12529 bfd_boolean is_special_grp = TRUE;
12530 bfd_boolean is_debug_grp = TRUE;
12531
12532 /* First scan to see if group contains any section other than debug
12533 and special section. */
12534 ssec = msec = elf_next_in_group (grp);
12535 do
12536 {
12537 if ((msec->flags & SEC_DEBUGGING) == 0)
12538 is_debug_grp = FALSE;
12539
12540 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12541 is_special_grp = FALSE;
12542
12543 msec = elf_next_in_group (msec);
12544 }
12545 while (msec != ssec);
12546
12547 /* If this is a pure debug section group or pure special section group,
12548 keep all sections in this group. */
12549 if (is_debug_grp || is_special_grp)
12550 {
12551 do
12552 {
12553 msec->gc_mark = 1;
12554 msec = elf_next_in_group (msec);
12555 }
12556 while (msec != ssec);
12557 }
12558}
12559
7f6ab9f8
AM
12560/* Keep debug and special sections. */
12561
12562bfd_boolean
12563_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12564 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12565{
12566 bfd *ibfd;
12567
c72f2fb2 12568 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12569 {
12570 asection *isec;
12571 bfd_boolean some_kept;
b40bf0a2 12572 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12573
12574 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12575 continue;
12576
b40bf0a2
NC
12577 /* Ensure all linker created sections are kept,
12578 see if any other section is already marked,
12579 and note if we have any fragmented debug sections. */
12580 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12581 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12582 {
12583 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12584 isec->gc_mark = 1;
12585 else if (isec->gc_mark)
12586 some_kept = TRUE;
b40bf0a2
NC
12587
12588 if (debug_frag_seen == FALSE
12589 && (isec->flags & SEC_DEBUGGING)
12590 && CONST_STRNEQ (isec->name, ".debug_line."))
12591 debug_frag_seen = TRUE;
7f6ab9f8
AM
12592 }
12593
12594 /* If no section in this file will be kept, then we can
b40bf0a2 12595 toss out the debug and special sections. */
7f6ab9f8
AM
12596 if (!some_kept)
12597 continue;
12598
12599 /* Keep debug and special sections like .comment when they are
3c758495
TG
12600 not part of a group. Also keep section groups that contain
12601 just debug sections or special sections. */
7f6ab9f8 12602 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12603 {
12604 if ((isec->flags & SEC_GROUP) != 0)
12605 _bfd_elf_gc_mark_debug_special_section_group (isec);
12606 else if (((isec->flags & SEC_DEBUGGING) != 0
12607 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12608 && elf_next_in_group (isec) == NULL)
12609 isec->gc_mark = 1;
12610 }
b40bf0a2
NC
12611
12612 if (! debug_frag_seen)
12613 continue;
12614
12615 /* Look for CODE sections which are going to be discarded,
12616 and find and discard any fragmented debug sections which
12617 are associated with that code section. */
12618 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12619 if ((isec->flags & SEC_CODE) != 0
12620 && isec->gc_mark == 0)
12621 {
12622 unsigned int ilen;
12623 asection *dsec;
12624
12625 ilen = strlen (isec->name);
12626
12627 /* Association is determined by the name of the debug section
12628 containing the name of the code section as a suffix. For
12629 example .debug_line.text.foo is a debug section associated
12630 with .text.foo. */
12631 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12632 {
12633 unsigned int dlen;
12634
12635 if (dsec->gc_mark == 0
12636 || (dsec->flags & SEC_DEBUGGING) == 0)
12637 continue;
12638
12639 dlen = strlen (dsec->name);
12640
12641 if (dlen > ilen
12642 && strncmp (dsec->name + (dlen - ilen),
12643 isec->name, ilen) == 0)
12644 {
12645 dsec->gc_mark = 0;
b40bf0a2
NC
12646 }
12647 }
12648 }
7f6ab9f8
AM
12649 }
12650 return TRUE;
12651}
12652
c152c796
AM
12653/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12654
c17d87de
NC
12655struct elf_gc_sweep_symbol_info
12656{
ccabcbe5
AM
12657 struct bfd_link_info *info;
12658 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12659 bfd_boolean);
12660};
12661
c152c796 12662static bfd_boolean
ccabcbe5 12663elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 12664{
1d5316ab
AM
12665 if (!h->mark
12666 && (((h->root.type == bfd_link_hash_defined
12667 || h->root.type == bfd_link_hash_defweak)
c4621b33 12668 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6673f753 12669 && h->root.u.def.section->gc_mark))
1d5316ab
AM
12670 || h->root.type == bfd_link_hash_undefined
12671 || h->root.type == bfd_link_hash_undefweak))
12672 {
12673 struct elf_gc_sweep_symbol_info *inf;
12674
12675 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 12676 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
12677 h->def_regular = 0;
12678 h->ref_regular = 0;
12679 h->ref_regular_nonweak = 0;
ccabcbe5 12680 }
c152c796
AM
12681
12682 return TRUE;
12683}
12684
12685/* The sweep phase of garbage collection. Remove all garbage sections. */
12686
12687typedef bfd_boolean (*gc_sweep_hook_fn)
12688 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12689
12690static bfd_boolean
ccabcbe5 12691elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12692{
12693 bfd *sub;
ccabcbe5
AM
12694 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12695 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12696 unsigned long section_sym_count;
12697 struct elf_gc_sweep_symbol_info sweep_info;
c152c796 12698
c72f2fb2 12699 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12700 {
12701 asection *o;
12702
b19a8f85
L
12703 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12704 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12705 continue;
12706
12707 for (o = sub->sections; o != NULL; o = o->next)
12708 {
a33dafc3
L
12709 /* When any section in a section group is kept, we keep all
12710 sections in the section group. If the first member of
12711 the section group is excluded, we will also exclude the
12712 group section. */
12713 if (o->flags & SEC_GROUP)
12714 {
12715 asection *first = elf_next_in_group (o);
12716 o->gc_mark = first->gc_mark;
12717 }
c152c796 12718
1e7eae0d 12719 if (o->gc_mark)
c152c796
AM
12720 continue;
12721
12722 /* Skip sweeping sections already excluded. */
12723 if (o->flags & SEC_EXCLUDE)
12724 continue;
12725
12726 /* Since this is early in the link process, it is simple
12727 to remove a section from the output. */
12728 o->flags |= SEC_EXCLUDE;
12729
c55fe096 12730 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
12731 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12732
c152c796
AM
12733 /* But we also have to update some of the relocation
12734 info we collected before. */
12735 if (gc_sweep_hook
e8aaee2a 12736 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12737 && o->reloc_count != 0
12738 && !((info->strip == strip_all || info->strip == strip_debugger)
12739 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12740 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12741 {
12742 Elf_Internal_Rela *internal_relocs;
12743 bfd_boolean r;
12744
12745 internal_relocs
12746 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12747 info->keep_memory);
12748 if (internal_relocs == NULL)
12749 return FALSE;
12750
12751 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12752
12753 if (elf_section_data (o)->relocs != internal_relocs)
12754 free (internal_relocs);
12755
12756 if (!r)
12757 return FALSE;
12758 }
12759 }
12760 }
12761
12762 /* Remove the symbols that were in the swept sections from the dynamic
12763 symbol table. GCFIXME: Anyone know how to get them out of the
12764 static symbol table as well? */
ccabcbe5
AM
12765 sweep_info.info = info;
12766 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12767 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12768 &sweep_info);
c152c796 12769
ccabcbe5 12770 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
12771 return TRUE;
12772}
12773
12774/* Propagate collected vtable information. This is called through
12775 elf_link_hash_traverse. */
12776
12777static bfd_boolean
12778elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12779{
c152c796 12780 /* Those that are not vtables. */
f6e332e6 12781 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12782 return TRUE;
12783
12784 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12785 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12786 return TRUE;
12787
12788 /* If we've already been done, exit. */
f6e332e6 12789 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12790 return TRUE;
12791
12792 /* Make sure the parent's table is up to date. */
f6e332e6 12793 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 12794
f6e332e6 12795 if (h->vtable->used == NULL)
c152c796
AM
12796 {
12797 /* None of this table's entries were referenced. Re-use the
12798 parent's table. */
f6e332e6
AM
12799 h->vtable->used = h->vtable->parent->vtable->used;
12800 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
12801 }
12802 else
12803 {
12804 size_t n;
12805 bfd_boolean *cu, *pu;
12806
12807 /* Or the parent's entries into ours. */
f6e332e6 12808 cu = h->vtable->used;
c152c796 12809 cu[-1] = TRUE;
f6e332e6 12810 pu = h->vtable->parent->vtable->used;
c152c796
AM
12811 if (pu != NULL)
12812 {
12813 const struct elf_backend_data *bed;
12814 unsigned int log_file_align;
12815
12816 bed = get_elf_backend_data (h->root.u.def.section->owner);
12817 log_file_align = bed->s->log_file_align;
f6e332e6 12818 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
12819 while (n--)
12820 {
12821 if (*pu)
12822 *cu = TRUE;
12823 pu++;
12824 cu++;
12825 }
12826 }
12827 }
12828
12829 return TRUE;
12830}
12831
12832static bfd_boolean
12833elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12834{
12835 asection *sec;
12836 bfd_vma hstart, hend;
12837 Elf_Internal_Rela *relstart, *relend, *rel;
12838 const struct elf_backend_data *bed;
12839 unsigned int log_file_align;
12840
c152c796
AM
12841 /* Take care of both those symbols that do not describe vtables as
12842 well as those that are not loaded. */
f6e332e6 12843 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12844 return TRUE;
12845
12846 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12847 || h->root.type == bfd_link_hash_defweak);
12848
12849 sec = h->root.u.def.section;
12850 hstart = h->root.u.def.value;
12851 hend = hstart + h->size;
12852
12853 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12854 if (!relstart)
12855 return *(bfd_boolean *) okp = FALSE;
12856 bed = get_elf_backend_data (sec->owner);
12857 log_file_align = bed->s->log_file_align;
12858
12859 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12860
12861 for (rel = relstart; rel < relend; ++rel)
12862 if (rel->r_offset >= hstart && rel->r_offset < hend)
12863 {
12864 /* If the entry is in use, do nothing. */
f6e332e6
AM
12865 if (h->vtable->used
12866 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
12867 {
12868 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 12869 if (h->vtable->used[entry])
c152c796
AM
12870 continue;
12871 }
12872 /* Otherwise, kill it. */
12873 rel->r_offset = rel->r_info = rel->r_addend = 0;
12874 }
12875
12876 return TRUE;
12877}
12878
87538722
AM
12879/* Mark sections containing dynamically referenced symbols. When
12880 building shared libraries, we must assume that any visible symbol is
12881 referenced. */
715df9b8 12882
64d03ab5
AM
12883bfd_boolean
12884bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 12885{
87538722 12886 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 12887 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 12888
715df9b8
EB
12889 if ((h->root.type == bfd_link_hash_defined
12890 || h->root.type == bfd_link_hash_defweak)
87538722 12891 && (h->ref_dynamic
c4621b33 12892 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 12893 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 12894 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 12895 && (!bfd_link_executable (info)
b407645f
AM
12896 || info->export_dynamic
12897 || (h->dynamic
12898 && d != NULL
12899 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 12900 && (h->versioned >= versioned
54e8959c
L
12901 || !bfd_hide_sym_by_version (info->version_info,
12902 h->root.root.string)))))
715df9b8
EB
12903 h->root.u.def.section->flags |= SEC_KEEP;
12904
12905 return TRUE;
12906}
3b36f7e6 12907
74f0fb50
AM
12908/* Keep all sections containing symbols undefined on the command-line,
12909 and the section containing the entry symbol. */
12910
12911void
12912_bfd_elf_gc_keep (struct bfd_link_info *info)
12913{
12914 struct bfd_sym_chain *sym;
12915
12916 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12917 {
12918 struct elf_link_hash_entry *h;
12919
12920 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12921 FALSE, FALSE, FALSE);
12922
12923 if (h != NULL
12924 && (h->root.type == bfd_link_hash_defined
12925 || h->root.type == bfd_link_hash_defweak)
12926 && !bfd_is_abs_section (h->root.u.def.section))
12927 h->root.u.def.section->flags |= SEC_KEEP;
12928 }
12929}
12930
2f0c68f2
CM
12931bfd_boolean
12932bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12933 struct bfd_link_info *info)
12934{
12935 bfd *ibfd = info->input_bfds;
12936
12937 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12938 {
12939 asection *sec;
12940 struct elf_reloc_cookie cookie;
12941
12942 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12943 continue;
12944
12945 if (!init_reloc_cookie (&cookie, info, ibfd))
12946 return FALSE;
12947
12948 for (sec = ibfd->sections; sec; sec = sec->next)
12949 {
12950 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12951 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12952 {
12953 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12954 fini_reloc_cookie_rels (&cookie, sec);
12955 }
12956 }
12957 }
12958 return TRUE;
12959}
12960
c152c796
AM
12961/* Do mark and sweep of unused sections. */
12962
12963bfd_boolean
12964bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12965{
12966 bfd_boolean ok = TRUE;
12967 bfd *sub;
6a5bb875 12968 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 12969 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 12970 struct elf_link_hash_table *htab;
c152c796 12971
64d03ab5 12972 if (!bed->can_gc_sections
715df9b8 12973 || !is_elf_hash_table (info->hash))
c152c796
AM
12974 {
12975 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12976 return TRUE;
12977 }
12978
74f0fb50 12979 bed->gc_keep (info);
da44f4e5 12980 htab = elf_hash_table (info);
74f0fb50 12981
9d0a14d3
RS
12982 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12983 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
12984 for (sub = info->input_bfds;
12985 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12986 sub = sub->link.next)
9d0a14d3
RS
12987 {
12988 asection *sec;
12989 struct elf_reloc_cookie cookie;
12990
12991 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 12992 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
12993 {
12994 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
12995 if (elf_section_data (sec)->sec_info
12996 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
12997 elf_eh_frame_section (sub) = sec;
12998 fini_reloc_cookie_for_section (&cookie, sec);
199af150 12999 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13000 }
13001 }
9d0a14d3 13002
c152c796 13003 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13004 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13005 if (!ok)
13006 return FALSE;
13007
13008 /* Kill the vtable relocations that were not used. */
da44f4e5 13009 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13010 if (!ok)
13011 return FALSE;
13012
715df9b8 13013 /* Mark dynamically referenced symbols. */
da44f4e5
AM
13014 if (htab->dynamic_sections_created)
13015 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13016
715df9b8 13017 /* Grovel through relocs to find out who stays ... */
64d03ab5 13018 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13019 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13020 {
13021 asection *o;
13022
b19a8f85
L
13023 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13024 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13025 continue;
13026
7f6ab9f8
AM
13027 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13028 Also treat note sections as a root, if the section is not part
13029 of a group. */
c152c796 13030 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13031 if (!o->gc_mark
13032 && (o->flags & SEC_EXCLUDE) == 0
24007750 13033 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13034 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13035 && elf_next_in_group (o) == NULL )))
13036 {
13037 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13038 return FALSE;
13039 }
c152c796
AM
13040 }
13041
6a5bb875 13042 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13043 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13044
c152c796 13045 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13046 return elf_gc_sweep (abfd, info);
c152c796
AM
13047}
13048\f
13049/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13050
13051bfd_boolean
13052bfd_elf_gc_record_vtinherit (bfd *abfd,
13053 asection *sec,
13054 struct elf_link_hash_entry *h,
13055 bfd_vma offset)
13056{
13057 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13058 struct elf_link_hash_entry **search, *child;
ef53be89 13059 size_t extsymcount;
c152c796
AM
13060 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13061
13062 /* The sh_info field of the symtab header tells us where the
13063 external symbols start. We don't care about the local symbols at
13064 this point. */
13065 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13066 if (!elf_bad_symtab (abfd))
13067 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13068
13069 sym_hashes = elf_sym_hashes (abfd);
13070 sym_hashes_end = sym_hashes + extsymcount;
13071
13072 /* Hunt down the child symbol, which is in this section at the same
13073 offset as the relocation. */
13074 for (search = sym_hashes; search != sym_hashes_end; ++search)
13075 {
13076 if ((child = *search) != NULL
13077 && (child->root.type == bfd_link_hash_defined
13078 || child->root.type == bfd_link_hash_defweak)
13079 && child->root.u.def.section == sec
13080 && child->root.u.def.value == offset)
13081 goto win;
13082 }
13083
d003868e
AM
13084 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
13085 abfd, sec, (unsigned long) offset);
c152c796
AM
13086 bfd_set_error (bfd_error_invalid_operation);
13087 return FALSE;
13088
13089 win:
f6e332e6
AM
13090 if (!child->vtable)
13091 {
ca4be51c
AM
13092 child->vtable = ((struct elf_link_virtual_table_entry *)
13093 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
13094 if (!child->vtable)
13095 return FALSE;
13096 }
c152c796
AM
13097 if (!h)
13098 {
13099 /* This *should* only be the absolute section. It could potentially
13100 be that someone has defined a non-global vtable though, which
13101 would be bad. It isn't worth paging in the local symbols to be
13102 sure though; that case should simply be handled by the assembler. */
13103
f6e332e6 13104 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13105 }
13106 else
f6e332e6 13107 child->vtable->parent = h;
c152c796
AM
13108
13109 return TRUE;
13110}
13111
13112/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13113
13114bfd_boolean
13115bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13116 asection *sec ATTRIBUTE_UNUSED,
13117 struct elf_link_hash_entry *h,
13118 bfd_vma addend)
13119{
13120 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13121 unsigned int log_file_align = bed->s->log_file_align;
13122
f6e332e6
AM
13123 if (!h->vtable)
13124 {
ca4be51c
AM
13125 h->vtable = ((struct elf_link_virtual_table_entry *)
13126 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
13127 if (!h->vtable)
13128 return FALSE;
13129 }
13130
13131 if (addend >= h->vtable->size)
c152c796
AM
13132 {
13133 size_t size, bytes, file_align;
f6e332e6 13134 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
13135
13136 /* While the symbol is undefined, we have to be prepared to handle
13137 a zero size. */
13138 file_align = 1 << log_file_align;
13139 if (h->root.type == bfd_link_hash_undefined)
13140 size = addend + file_align;
13141 else
13142 {
13143 size = h->size;
13144 if (addend >= size)
13145 {
13146 /* Oops! We've got a reference past the defined end of
13147 the table. This is probably a bug -- shall we warn? */
13148 size = addend + file_align;
13149 }
13150 }
13151 size = (size + file_align - 1) & -file_align;
13152
13153 /* Allocate one extra entry for use as a "done" flag for the
13154 consolidation pass. */
13155 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13156
13157 if (ptr)
13158 {
a50b1753 13159 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13160
13161 if (ptr != NULL)
13162 {
13163 size_t oldbytes;
13164
f6e332e6 13165 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
13166 * sizeof (bfd_boolean));
13167 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13168 }
13169 }
13170 else
a50b1753 13171 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13172
13173 if (ptr == NULL)
13174 return FALSE;
13175
13176 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
13177 h->vtable->used = ptr + 1;
13178 h->vtable->size = size;
c152c796
AM
13179 }
13180
f6e332e6 13181 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13182
13183 return TRUE;
13184}
13185
ae17ab41
CM
13186/* Map an ELF section header flag to its corresponding string. */
13187typedef struct
13188{
13189 char *flag_name;
13190 flagword flag_value;
13191} elf_flags_to_name_table;
13192
13193static elf_flags_to_name_table elf_flags_to_names [] =
13194{
13195 { "SHF_WRITE", SHF_WRITE },
13196 { "SHF_ALLOC", SHF_ALLOC },
13197 { "SHF_EXECINSTR", SHF_EXECINSTR },
13198 { "SHF_MERGE", SHF_MERGE },
13199 { "SHF_STRINGS", SHF_STRINGS },
13200 { "SHF_INFO_LINK", SHF_INFO_LINK},
13201 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13202 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13203 { "SHF_GROUP", SHF_GROUP },
13204 { "SHF_TLS", SHF_TLS },
13205 { "SHF_MASKOS", SHF_MASKOS },
13206 { "SHF_EXCLUDE", SHF_EXCLUDE },
13207};
13208
b9c361e0
JL
13209/* Returns TRUE if the section is to be included, otherwise FALSE. */
13210bfd_boolean
ae17ab41 13211bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13212 struct flag_info *flaginfo,
b9c361e0 13213 asection *section)
ae17ab41 13214{
8b127cbc 13215 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13216
8b127cbc 13217 if (!flaginfo->flags_initialized)
ae17ab41 13218 {
8b127cbc
AM
13219 bfd *obfd = info->output_bfd;
13220 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13221 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13222 int with_hex = 0;
13223 int without_hex = 0;
13224
8b127cbc 13225 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13226 {
b9c361e0 13227 unsigned i;
8b127cbc 13228 flagword (*lookup) (char *);
ae17ab41 13229
8b127cbc
AM
13230 lookup = bed->elf_backend_lookup_section_flags_hook;
13231 if (lookup != NULL)
ae17ab41 13232 {
8b127cbc 13233 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13234
13235 if (hexval != 0)
13236 {
13237 if (tf->with == with_flags)
13238 with_hex |= hexval;
13239 else if (tf->with == without_flags)
13240 without_hex |= hexval;
13241 tf->valid = TRUE;
13242 continue;
13243 }
ae17ab41 13244 }
8b127cbc 13245 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13246 {
8b127cbc 13247 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13248 {
13249 if (tf->with == with_flags)
13250 with_hex |= elf_flags_to_names[i].flag_value;
13251 else if (tf->with == without_flags)
13252 without_hex |= elf_flags_to_names[i].flag_value;
13253 tf->valid = TRUE;
13254 break;
13255 }
13256 }
8b127cbc 13257 if (!tf->valid)
b9c361e0 13258 {
68ffbac6 13259 info->callbacks->einfo
8b127cbc 13260 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13261 return FALSE;
ae17ab41
CM
13262 }
13263 }
8b127cbc
AM
13264 flaginfo->flags_initialized = TRUE;
13265 flaginfo->only_with_flags |= with_hex;
13266 flaginfo->not_with_flags |= without_hex;
ae17ab41 13267 }
ae17ab41 13268
8b127cbc 13269 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13270 return FALSE;
13271
8b127cbc 13272 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13273 return FALSE;
13274
13275 return TRUE;
ae17ab41
CM
13276}
13277
c152c796
AM
13278struct alloc_got_off_arg {
13279 bfd_vma gotoff;
10455f89 13280 struct bfd_link_info *info;
c152c796
AM
13281};
13282
13283/* We need a special top-level link routine to convert got reference counts
13284 to real got offsets. */
13285
13286static bfd_boolean
13287elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13288{
a50b1753 13289 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13290 bfd *obfd = gofarg->info->output_bfd;
13291 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13292
c152c796
AM
13293 if (h->got.refcount > 0)
13294 {
13295 h->got.offset = gofarg->gotoff;
10455f89 13296 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13297 }
13298 else
13299 h->got.offset = (bfd_vma) -1;
13300
13301 return TRUE;
13302}
13303
13304/* And an accompanying bit to work out final got entry offsets once
13305 we're done. Should be called from final_link. */
13306
13307bfd_boolean
13308bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13309 struct bfd_link_info *info)
13310{
13311 bfd *i;
13312 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13313 bfd_vma gotoff;
c152c796
AM
13314 struct alloc_got_off_arg gofarg;
13315
10455f89
HPN
13316 BFD_ASSERT (abfd == info->output_bfd);
13317
c152c796
AM
13318 if (! is_elf_hash_table (info->hash))
13319 return FALSE;
13320
13321 /* The GOT offset is relative to the .got section, but the GOT header is
13322 put into the .got.plt section, if the backend uses it. */
13323 if (bed->want_got_plt)
13324 gotoff = 0;
13325 else
13326 gotoff = bed->got_header_size;
13327
13328 /* Do the local .got entries first. */
c72f2fb2 13329 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13330 {
13331 bfd_signed_vma *local_got;
ef53be89 13332 size_t j, locsymcount;
c152c796
AM
13333 Elf_Internal_Shdr *symtab_hdr;
13334
13335 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13336 continue;
13337
13338 local_got = elf_local_got_refcounts (i);
13339 if (!local_got)
13340 continue;
13341
13342 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13343 if (elf_bad_symtab (i))
13344 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13345 else
13346 locsymcount = symtab_hdr->sh_info;
13347
13348 for (j = 0; j < locsymcount; ++j)
13349 {
13350 if (local_got[j] > 0)
13351 {
13352 local_got[j] = gotoff;
10455f89 13353 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13354 }
13355 else
13356 local_got[j] = (bfd_vma) -1;
13357 }
13358 }
13359
13360 /* Then the global .got entries. .plt refcounts are handled by
13361 adjust_dynamic_symbol */
13362 gofarg.gotoff = gotoff;
10455f89 13363 gofarg.info = info;
c152c796
AM
13364 elf_link_hash_traverse (elf_hash_table (info),
13365 elf_gc_allocate_got_offsets,
13366 &gofarg);
13367 return TRUE;
13368}
13369
13370/* Many folk need no more in the way of final link than this, once
13371 got entry reference counting is enabled. */
13372
13373bfd_boolean
13374bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13375{
13376 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13377 return FALSE;
13378
13379 /* Invoke the regular ELF backend linker to do all the work. */
13380 return bfd_elf_final_link (abfd, info);
13381}
13382
13383bfd_boolean
13384bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13385{
a50b1753 13386 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13387
13388 if (rcookie->bad_symtab)
13389 rcookie->rel = rcookie->rels;
13390
13391 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13392 {
13393 unsigned long r_symndx;
13394
13395 if (! rcookie->bad_symtab)
13396 if (rcookie->rel->r_offset > offset)
13397 return FALSE;
13398 if (rcookie->rel->r_offset != offset)
13399 continue;
13400
13401 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13402 if (r_symndx == STN_UNDEF)
c152c796
AM
13403 return TRUE;
13404
13405 if (r_symndx >= rcookie->locsymcount
13406 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13407 {
13408 struct elf_link_hash_entry *h;
13409
13410 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13411
13412 while (h->root.type == bfd_link_hash_indirect
13413 || h->root.type == bfd_link_hash_warning)
13414 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13415
13416 if ((h->root.type == bfd_link_hash_defined
13417 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13418 && (h->root.u.def.section->owner != rcookie->abfd
13419 || h->root.u.def.section->kept_section != NULL
13420 || discarded_section (h->root.u.def.section)))
c152c796 13421 return TRUE;
c152c796
AM
13422 }
13423 else
13424 {
13425 /* It's not a relocation against a global symbol,
13426 but it could be a relocation against a local
13427 symbol for a discarded section. */
13428 asection *isec;
13429 Elf_Internal_Sym *isym;
13430
13431 /* Need to: get the symbol; get the section. */
13432 isym = &rcookie->locsyms[r_symndx];
cb33740c 13433 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13434 if (isec != NULL
13435 && (isec->kept_section != NULL
13436 || discarded_section (isec)))
cb33740c 13437 return TRUE;
c152c796
AM
13438 }
13439 return FALSE;
13440 }
13441 return FALSE;
13442}
13443
13444/* Discard unneeded references to discarded sections.
75938853
AM
13445 Returns -1 on error, 1 if any section's size was changed, 0 if
13446 nothing changed. This function assumes that the relocations are in
13447 sorted order, which is true for all known assemblers. */
c152c796 13448
75938853 13449int
c152c796
AM
13450bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13451{
13452 struct elf_reloc_cookie cookie;
18cd5bce 13453 asection *o;
c152c796 13454 bfd *abfd;
75938853 13455 int changed = 0;
c152c796
AM
13456
13457 if (info->traditional_format
13458 || !is_elf_hash_table (info->hash))
75938853 13459 return 0;
c152c796 13460
18cd5bce
AM
13461 o = bfd_get_section_by_name (output_bfd, ".stab");
13462 if (o != NULL)
c152c796 13463 {
18cd5bce 13464 asection *i;
c152c796 13465
18cd5bce 13466 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13467 {
18cd5bce
AM
13468 if (i->size == 0
13469 || i->reloc_count == 0
13470 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13471 continue;
c152c796 13472
18cd5bce
AM
13473 abfd = i->owner;
13474 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13475 continue;
c152c796 13476
18cd5bce 13477 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13478 return -1;
c152c796 13479
18cd5bce
AM
13480 if (_bfd_discard_section_stabs (abfd, i,
13481 elf_section_data (i)->sec_info,
5241d853
RS
13482 bfd_elf_reloc_symbol_deleted_p,
13483 &cookie))
75938853 13484 changed = 1;
18cd5bce
AM
13485
13486 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13487 }
18cd5bce
AM
13488 }
13489
2f0c68f2
CM
13490 o = NULL;
13491 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13492 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13493 if (o != NULL)
13494 {
13495 asection *i;
c152c796 13496
18cd5bce 13497 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13498 {
18cd5bce
AM
13499 if (i->size == 0)
13500 continue;
13501
13502 abfd = i->owner;
13503 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13504 continue;
13505
13506 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13507 return -1;
18cd5bce
AM
13508
13509 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13510 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13511 bfd_elf_reloc_symbol_deleted_p,
13512 &cookie))
75938853 13513 changed = 1;
18cd5bce
AM
13514
13515 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13516 }
18cd5bce 13517 }
c152c796 13518
18cd5bce
AM
13519 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13520 {
13521 const struct elf_backend_data *bed;
c152c796 13522
18cd5bce
AM
13523 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13524 continue;
13525
13526 bed = get_elf_backend_data (abfd);
13527
13528 if (bed->elf_backend_discard_info != NULL)
13529 {
13530 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13531 return -1;
18cd5bce
AM
13532
13533 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13534 changed = 1;
18cd5bce
AM
13535
13536 fini_reloc_cookie (&cookie, abfd);
13537 }
c152c796
AM
13538 }
13539
2f0c68f2
CM
13540 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13541 _bfd_elf_end_eh_frame_parsing (info);
13542
13543 if (info->eh_frame_hdr_type
0e1862bb 13544 && !bfd_link_relocatable (info)
c152c796 13545 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13546 changed = 1;
c152c796 13547
75938853 13548 return changed;
c152c796 13549}
082b7297 13550
43e1669b 13551bfd_boolean
0c511000 13552_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13553 asection *sec,
c0f00686 13554 struct bfd_link_info *info)
082b7297
L
13555{
13556 flagword flags;
c77ec726 13557 const char *name, *key;
082b7297
L
13558 struct bfd_section_already_linked *l;
13559 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13560
c77ec726
AM
13561 if (sec->output_section == bfd_abs_section_ptr)
13562 return FALSE;
0c511000 13563
c77ec726 13564 flags = sec->flags;
0c511000 13565
c77ec726
AM
13566 /* Return if it isn't a linkonce section. A comdat group section
13567 also has SEC_LINK_ONCE set. */
13568 if ((flags & SEC_LINK_ONCE) == 0)
13569 return FALSE;
0c511000 13570
c77ec726
AM
13571 /* Don't put group member sections on our list of already linked
13572 sections. They are handled as a group via their group section. */
13573 if (elf_sec_group (sec) != NULL)
13574 return FALSE;
0c511000 13575
c77ec726
AM
13576 /* For a SHT_GROUP section, use the group signature as the key. */
13577 name = sec->name;
13578 if ((flags & SEC_GROUP) != 0
13579 && elf_next_in_group (sec) != NULL
13580 && elf_group_name (elf_next_in_group (sec)) != NULL)
13581 key = elf_group_name (elf_next_in_group (sec));
13582 else
13583 {
13584 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13585 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13586 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13587 key++;
0c511000 13588 else
c77ec726
AM
13589 /* Must be a user linkonce section that doesn't follow gcc's
13590 naming convention. In this case we won't be matching
13591 single member groups. */
13592 key = name;
0c511000 13593 }
6d2cd210 13594
c77ec726 13595 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13596
13597 for (l = already_linked_list->entry; l != NULL; l = l->next)
13598 {
c2370991 13599 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13600 sections with a signature of <key> (<key> is some string),
13601 and linkonce sections named .gnu.linkonce.<type>.<key>.
13602 Match like sections. LTO plugin sections are an exception.
13603 They are always named .gnu.linkonce.t.<key> and match either
13604 type of section. */
13605 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13606 && ((flags & SEC_GROUP) != 0
13607 || strcmp (name, l->sec->name) == 0))
13608 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13609 {
13610 /* The section has already been linked. See if we should
6d2cd210 13611 issue a warning. */
c77ec726
AM
13612 if (!_bfd_handle_already_linked (sec, l, info))
13613 return FALSE;
082b7297 13614
c77ec726 13615 if (flags & SEC_GROUP)
3d7f7666 13616 {
c77ec726
AM
13617 asection *first = elf_next_in_group (sec);
13618 asection *s = first;
3d7f7666 13619
c77ec726 13620 while (s != NULL)
3d7f7666 13621 {
c77ec726
AM
13622 s->output_section = bfd_abs_section_ptr;
13623 /* Record which group discards it. */
13624 s->kept_section = l->sec;
13625 s = elf_next_in_group (s);
13626 /* These lists are circular. */
13627 if (s == first)
13628 break;
3d7f7666
L
13629 }
13630 }
082b7297 13631
43e1669b 13632 return TRUE;
082b7297
L
13633 }
13634 }
13635
c77ec726
AM
13636 /* A single member comdat group section may be discarded by a
13637 linkonce section and vice versa. */
13638 if ((flags & SEC_GROUP) != 0)
3d7f7666 13639 {
c77ec726 13640 asection *first = elf_next_in_group (sec);
c2370991 13641
c77ec726
AM
13642 if (first != NULL && elf_next_in_group (first) == first)
13643 /* Check this single member group against linkonce sections. */
13644 for (l = already_linked_list->entry; l != NULL; l = l->next)
13645 if ((l->sec->flags & SEC_GROUP) == 0
13646 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13647 {
13648 first->output_section = bfd_abs_section_ptr;
13649 first->kept_section = l->sec;
13650 sec->output_section = bfd_abs_section_ptr;
13651 break;
13652 }
13653 }
13654 else
13655 /* Check this linkonce section against single member groups. */
13656 for (l = already_linked_list->entry; l != NULL; l = l->next)
13657 if (l->sec->flags & SEC_GROUP)
6d2cd210 13658 {
c77ec726 13659 asection *first = elf_next_in_group (l->sec);
6d2cd210 13660
c77ec726
AM
13661 if (first != NULL
13662 && elf_next_in_group (first) == first
13663 && bfd_elf_match_symbols_in_sections (first, sec, info))
13664 {
13665 sec->output_section = bfd_abs_section_ptr;
13666 sec->kept_section = first;
13667 break;
13668 }
6d2cd210 13669 }
0c511000 13670
c77ec726
AM
13671 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13672 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13673 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13674 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13675 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13676 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13677 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13678 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13679 The reverse order cannot happen as there is never a bfd with only the
13680 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13681 matter as here were are looking only for cross-bfd sections. */
13682
13683 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13684 for (l = already_linked_list->entry; l != NULL; l = l->next)
13685 if ((l->sec->flags & SEC_GROUP) == 0
13686 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13687 {
13688 if (abfd != l->sec->owner)
13689 sec->output_section = bfd_abs_section_ptr;
13690 break;
13691 }
80c29487 13692
082b7297 13693 /* This is the first section with this name. Record it. */
c77ec726 13694 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13695 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13696 return sec->output_section == bfd_abs_section_ptr;
082b7297 13697}
81e1b023 13698
a4d8e49b
L
13699bfd_boolean
13700_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13701{
13702 return sym->st_shndx == SHN_COMMON;
13703}
13704
13705unsigned int
13706_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13707{
13708 return SHN_COMMON;
13709}
13710
13711asection *
13712_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13713{
13714 return bfd_com_section_ptr;
13715}
10455f89
HPN
13716
13717bfd_vma
13718_bfd_elf_default_got_elt_size (bfd *abfd,
13719 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13720 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13721 bfd *ibfd ATTRIBUTE_UNUSED,
13722 unsigned long symndx ATTRIBUTE_UNUSED)
13723{
13724 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13725 return bed->s->arch_size / 8;
13726}
83bac4b0
NC
13727
13728/* Routines to support the creation of dynamic relocs. */
13729
83bac4b0
NC
13730/* Returns the name of the dynamic reloc section associated with SEC. */
13731
13732static const char *
13733get_dynamic_reloc_section_name (bfd * abfd,
13734 asection * sec,
13735 bfd_boolean is_rela)
13736{
ddcf1fcf
BS
13737 char *name;
13738 const char *old_name = bfd_get_section_name (NULL, sec);
13739 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13740
ddcf1fcf 13741 if (old_name == NULL)
83bac4b0
NC
13742 return NULL;
13743
ddcf1fcf 13744 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13745 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13746
13747 return name;
13748}
13749
13750/* Returns the dynamic reloc section associated with SEC.
13751 If necessary compute the name of the dynamic reloc section based
13752 on SEC's name (looked up in ABFD's string table) and the setting
13753 of IS_RELA. */
13754
13755asection *
13756_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13757 asection * sec,
13758 bfd_boolean is_rela)
13759{
13760 asection * reloc_sec = elf_section_data (sec)->sreloc;
13761
13762 if (reloc_sec == NULL)
13763 {
13764 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13765
13766 if (name != NULL)
13767 {
3d4d4302 13768 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
13769
13770 if (reloc_sec != NULL)
13771 elf_section_data (sec)->sreloc = reloc_sec;
13772 }
13773 }
13774
13775 return reloc_sec;
13776}
13777
13778/* Returns the dynamic reloc section associated with SEC. If the
13779 section does not exist it is created and attached to the DYNOBJ
13780 bfd and stored in the SRELOC field of SEC's elf_section_data
13781 structure.
f8076f98 13782
83bac4b0
NC
13783 ALIGNMENT is the alignment for the newly created section and
13784 IS_RELA defines whether the name should be .rela.<SEC's name>
13785 or .rel.<SEC's name>. The section name is looked up in the
13786 string table associated with ABFD. */
13787
13788asection *
ca4be51c
AM
13789_bfd_elf_make_dynamic_reloc_section (asection *sec,
13790 bfd *dynobj,
13791 unsigned int alignment,
13792 bfd *abfd,
13793 bfd_boolean is_rela)
83bac4b0
NC
13794{
13795 asection * reloc_sec = elf_section_data (sec)->sreloc;
13796
13797 if (reloc_sec == NULL)
13798 {
13799 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13800
13801 if (name == NULL)
13802 return NULL;
13803
3d4d4302 13804 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
13805
13806 if (reloc_sec == NULL)
13807 {
3d4d4302
AM
13808 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13809 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
13810 if ((sec->flags & SEC_ALLOC) != 0)
13811 flags |= SEC_ALLOC | SEC_LOAD;
13812
3d4d4302 13813 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
13814 if (reloc_sec != NULL)
13815 {
8877b5e5
AM
13816 /* _bfd_elf_get_sec_type_attr chooses a section type by
13817 name. Override as it may be wrong, eg. for a user
13818 section named "auto" we'll get ".relauto" which is
13819 seen to be a .rela section. */
13820 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
13821 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13822 reloc_sec = NULL;
13823 }
13824 }
13825
13826 elf_section_data (sec)->sreloc = reloc_sec;
13827 }
13828
13829 return reloc_sec;
13830}
1338dd10 13831
bffebb6b
AM
13832/* Copy the ELF symbol type and other attributes for a linker script
13833 assignment from HSRC to HDEST. Generally this should be treated as
13834 if we found a strong non-dynamic definition for HDEST (except that
13835 ld ignores multiple definition errors). */
1338dd10 13836void
bffebb6b
AM
13837_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13838 struct bfd_link_hash_entry *hdest,
13839 struct bfd_link_hash_entry *hsrc)
1338dd10 13840{
bffebb6b
AM
13841 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13842 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13843 Elf_Internal_Sym isym;
1338dd10
PB
13844
13845 ehdest->type = ehsrc->type;
35fc36a8 13846 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
13847
13848 isym.st_other = ehsrc->other;
b8417128 13849 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 13850}
351f65ca
L
13851
13852/* Append a RELA relocation REL to section S in BFD. */
13853
13854void
13855elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13856{
13857 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13858 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13859 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13860 bed->s->swap_reloca_out (abfd, rel, loc);
13861}
13862
13863/* Append a REL relocation REL to section S in BFD. */
13864
13865void
13866elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13867{
13868 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13869 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13870 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 13871 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 13872}
This page took 2.165595 seconds and 4 git commands to generate.