PR20932, Internal error during record link assignment
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
6f2750fe 2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
08ce1d72 31#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 32#include "plugin-api.h"
7dc3990e
L
33#include "plugin.h"
34#endif
252b5132 35
28caa186
AM
36/* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39struct elf_info_failed
40{
41 struct bfd_link_info *info;
28caa186
AM
42 bfd_boolean failed;
43};
44
45/* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48struct elf_find_verdep_info
49{
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56};
57
58static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
2f0c68f2
CM
61asection *
62_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65{
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100}
101
d98685ac
AM
102/* Define a symbol in a dynamic linkage section. */
103
104struct elf_link_hash_entry *
105_bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109{
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
ccabcbe5 112 const struct elf_backend_data *bed;
d98685ac
AM
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
122 }
123
124 bh = &h->root;
cf18fda4 125 bed = get_elf_backend_data (abfd);
d98685ac 126 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 127 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
128 &bh))
129 return NULL;
130 h = (struct elf_link_hash_entry *) bh;
131 h->def_regular = 1;
e28df02b 132 h->non_elf = 0;
12b2843a 133 h->root.linker_def = 1;
d98685ac 134 h->type = STT_OBJECT;
00b7642b
AM
135 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
136 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 137
ccabcbe5 138 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
139 return h;
140}
141
b34976b6 142bfd_boolean
268b6b39 143_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
144{
145 flagword flags;
aad5d350 146 asection *s;
252b5132 147 struct elf_link_hash_entry *h;
9c5bfbb7 148 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 149 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
150
151 /* This function may be called more than once. */
ce558b89 152 if (htab->sgot != 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
8e2a4f11
AM
580 if (h->root.type == bfd_link_hash_warning)
581 h = (struct elf_link_hash_entry *) h->root.u.i.link;
582
0f550b3d
L
583 if (h->versioned == unknown)
584 {
585 /* Set versioned if symbol version is unknown. */
586 char *version = strrchr (name, ELF_VER_CHR);
587 if (version)
588 {
589 if (version > name && version[-1] != ELF_VER_CHR)
590 h->versioned = versioned_hidden;
591 else
592 h->versioned = versioned;
593 }
594 }
595
00cbee0a 596 switch (h->root.type)
77cfaee6 597 {
00cbee0a
L
598 case bfd_link_hash_defined:
599 case bfd_link_hash_defweak:
600 case bfd_link_hash_common:
601 break;
602 case bfd_link_hash_undefweak:
603 case bfd_link_hash_undefined:
604 /* Since we're defining the symbol, don't let it seem to have not
605 been defined. record_dynamic_symbol and size_dynamic_sections
606 may depend on this. */
4ea42fb7 607 h->root.type = bfd_link_hash_new;
77cfaee6
AM
608 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
609 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
610 break;
611 case bfd_link_hash_new:
40b36307 612 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 613 h->non_elf = 0;
00cbee0a
L
614 break;
615 case bfd_link_hash_indirect:
616 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 617 the versioned symbol point to this one. */
00cbee0a
L
618 bed = get_elf_backend_data (output_bfd);
619 hv = h;
620 while (hv->root.type == bfd_link_hash_indirect
621 || hv->root.type == bfd_link_hash_warning)
622 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
623 /* We don't need to update h->root.u since linker will set them
624 later. */
625 h->root.type = bfd_link_hash_undefined;
626 hv->root.type = bfd_link_hash_indirect;
627 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
628 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
629 break;
8e2a4f11
AM
630 default:
631 BFD_FAIL ();
c2596ca5 632 return FALSE;
55255dae 633 }
45d6a902
AM
634
635 /* If this symbol is being provided by the linker script, and it is
636 currently defined by a dynamic object, but not by a regular
637 object, then mark it as undefined so that the generic linker will
638 force the correct value. */
639 if (provide
f5385ebf
AM
640 && h->def_dynamic
641 && !h->def_regular)
45d6a902
AM
642 h->root.type = bfd_link_hash_undefined;
643
644 /* If this symbol is not being provided by the linker script, and it is
645 currently defined by a dynamic object, but not by a regular object,
646 then clear out any version information because the symbol will not be
647 associated with the dynamic object any more. */
648 if (!provide
f5385ebf
AM
649 && h->def_dynamic
650 && !h->def_regular)
45d6a902
AM
651 h->verinfo.verdef = NULL;
652
f5385ebf 653 h->def_regular = 1;
45d6a902 654
eb8476a6 655 if (hidden)
fe21a8fc 656 {
91d6fa6a 657 bed = get_elf_backend_data (output_bfd);
b8297068
AM
658 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
659 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
660 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
661 }
662
6fa3860b
PB
663 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
664 and executables. */
0e1862bb 665 if (!bfd_link_relocatable (info)
6fa3860b
PB
666 && h->dynindx != -1
667 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
668 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
669 h->forced_local = 1;
670
f5385ebf
AM
671 if ((h->def_dynamic
672 || h->ref_dynamic
6b3b0ab8
L
673 || bfd_link_dll (info)
674 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
675 && h->dynindx == -1)
676 {
c152c796 677 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
678 return FALSE;
679
680 /* If this is a weak defined symbol, and we know a corresponding
681 real symbol from the same dynamic object, make sure the real
682 symbol is also made into a dynamic symbol. */
f6e332e6
AM
683 if (h->u.weakdef != NULL
684 && h->u.weakdef->dynindx == -1)
45d6a902 685 {
f6e332e6 686 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
687 return FALSE;
688 }
689 }
690
691 return TRUE;
692}
42751cf3 693
8c58d23b
AM
694/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
695 success, and 2 on a failure caused by attempting to record a symbol
696 in a discarded section, eg. a discarded link-once section symbol. */
697
698int
c152c796
AM
699bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
700 bfd *input_bfd,
701 long input_indx)
8c58d23b
AM
702{
703 bfd_size_type amt;
704 struct elf_link_local_dynamic_entry *entry;
705 struct elf_link_hash_table *eht;
706 struct elf_strtab_hash *dynstr;
ef53be89 707 size_t dynstr_index;
8c58d23b
AM
708 char *name;
709 Elf_External_Sym_Shndx eshndx;
710 char esym[sizeof (Elf64_External_Sym)];
711
0eddce27 712 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
713 return 0;
714
715 /* See if the entry exists already. */
716 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
717 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
718 return 1;
719
720 amt = sizeof (*entry);
a50b1753 721 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
722 if (entry == NULL)
723 return 0;
724
725 /* Go find the symbol, so that we can find it's name. */
726 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 727 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
728 {
729 bfd_release (input_bfd, entry);
730 return 0;
731 }
732
733 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 734 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
735 {
736 asection *s;
737
738 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
739 if (s == NULL || bfd_is_abs_section (s->output_section))
740 {
741 /* We can still bfd_release here as nothing has done another
742 bfd_alloc. We can't do this later in this function. */
743 bfd_release (input_bfd, entry);
744 return 2;
745 }
746 }
747
748 name = (bfd_elf_string_from_elf_section
749 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
750 entry->isym.st_name));
751
752 dynstr = elf_hash_table (info)->dynstr;
753 if (dynstr == NULL)
754 {
755 /* Create a strtab to hold the dynamic symbol names. */
756 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
757 if (dynstr == NULL)
758 return 0;
759 }
760
b34976b6 761 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 762 if (dynstr_index == (size_t) -1)
8c58d23b
AM
763 return 0;
764 entry->isym.st_name = dynstr_index;
765
766 eht = elf_hash_table (info);
767
768 entry->next = eht->dynlocal;
769 eht->dynlocal = entry;
770 entry->input_bfd = input_bfd;
771 entry->input_indx = input_indx;
772 eht->dynsymcount++;
773
774 /* Whatever binding the symbol had before, it's now local. */
775 entry->isym.st_info
776 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
777
778 /* The dynindx will be set at the end of size_dynamic_sections. */
779
780 return 1;
781}
782
30b30c21 783/* Return the dynindex of a local dynamic symbol. */
42751cf3 784
30b30c21 785long
268b6b39
AM
786_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
787 bfd *input_bfd,
788 long input_indx)
30b30c21
RH
789{
790 struct elf_link_local_dynamic_entry *e;
791
792 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
793 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
794 return e->dynindx;
795 return -1;
796}
797
798/* This function is used to renumber the dynamic symbols, if some of
799 them are removed because they are marked as local. This is called
800 via elf_link_hash_traverse. */
801
b34976b6 802static bfd_boolean
268b6b39
AM
803elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
804 void *data)
42751cf3 805{
a50b1753 806 size_t *count = (size_t *) data;
30b30c21 807
6fa3860b
PB
808 if (h->forced_local)
809 return TRUE;
810
811 if (h->dynindx != -1)
812 h->dynindx = ++(*count);
813
814 return TRUE;
815}
816
817
818/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
819 STB_LOCAL binding. */
820
821static bfd_boolean
822elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
823 void *data)
824{
a50b1753 825 size_t *count = (size_t *) data;
6fa3860b 826
6fa3860b
PB
827 if (!h->forced_local)
828 return TRUE;
829
42751cf3 830 if (h->dynindx != -1)
30b30c21
RH
831 h->dynindx = ++(*count);
832
b34976b6 833 return TRUE;
42751cf3 834}
30b30c21 835
aee6f5b4
AO
836/* Return true if the dynamic symbol for a given section should be
837 omitted when creating a shared library. */
838bfd_boolean
839_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
840 struct bfd_link_info *info,
841 asection *p)
842{
74541ad4 843 struct elf_link_hash_table *htab;
ca55926c 844 asection *ip;
74541ad4 845
aee6f5b4
AO
846 switch (elf_section_data (p)->this_hdr.sh_type)
847 {
848 case SHT_PROGBITS:
849 case SHT_NOBITS:
850 /* If sh_type is yet undecided, assume it could be
851 SHT_PROGBITS/SHT_NOBITS. */
852 case SHT_NULL:
74541ad4
AM
853 htab = elf_hash_table (info);
854 if (p == htab->tls_sec)
855 return FALSE;
856
857 if (htab->text_index_section != NULL)
858 return p != htab->text_index_section && p != htab->data_index_section;
859
ca55926c 860 return (htab->dynobj != NULL
3d4d4302 861 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 862 && ip->output_section == p);
aee6f5b4
AO
863
864 /* There shouldn't be section relative relocations
865 against any other section. */
866 default:
867 return TRUE;
868 }
869}
870
062e2358 871/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
872 symbol for each output section, which come first. Next come symbols
873 which have been forced to local binding. Then all of the back-end
874 allocated local dynamic syms, followed by the rest of the global
875 symbols. */
30b30c21 876
554220db
AM
877static unsigned long
878_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
879 struct bfd_link_info *info,
880 unsigned long *section_sym_count)
30b30c21
RH
881{
882 unsigned long dynsymcount = 0;
883
0e1862bb
L
884 if (bfd_link_pic (info)
885 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 886 {
aee6f5b4 887 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
888 asection *p;
889 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 890 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
891 && (p->flags & SEC_ALLOC) != 0
892 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
893 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
894 else
895 elf_section_data (p)->dynindx = 0;
30b30c21 896 }
554220db 897 *section_sym_count = dynsymcount;
30b30c21 898
6fa3860b
PB
899 elf_link_hash_traverse (elf_hash_table (info),
900 elf_link_renumber_local_hash_table_dynsyms,
901 &dynsymcount);
902
30b30c21
RH
903 if (elf_hash_table (info)->dynlocal)
904 {
905 struct elf_link_local_dynamic_entry *p;
906 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
907 p->dynindx = ++dynsymcount;
908 }
90ac2420 909 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
910
911 elf_link_hash_traverse (elf_hash_table (info),
912 elf_link_renumber_hash_table_dynsyms,
913 &dynsymcount);
914
d5486c43
L
915 /* There is an unused NULL entry at the head of the table which we
916 must account for in our count even if the table is empty since it
917 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
918 .dynamic section. */
919 dynsymcount++;
30b30c21 920
ccabcbe5
AM
921 elf_hash_table (info)->dynsymcount = dynsymcount;
922 return dynsymcount;
30b30c21 923}
252b5132 924
54ac0771
L
925/* Merge st_other field. */
926
927static void
928elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 929 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 930 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
931{
932 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
933
934 /* If st_other has a processor-specific meaning, specific
cd3416da 935 code might be needed here. */
54ac0771
L
936 if (bed->elf_backend_merge_symbol_attribute)
937 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
938 dynamic);
939
cd3416da 940 if (!dynamic)
54ac0771 941 {
cd3416da
AM
942 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
943 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 944
cd3416da
AM
945 /* Keep the most constraining visibility. Leave the remainder
946 of the st_other field to elf_backend_merge_symbol_attribute. */
947 if (symvis - 1 < hvis - 1)
948 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 949 }
b8417128
AM
950 else if (definition
951 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
952 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 953 h->protected_def = 1;
54ac0771
L
954}
955
4f3fedcf
AM
956/* This function is called when we want to merge a new symbol with an
957 existing symbol. It handles the various cases which arise when we
958 find a definition in a dynamic object, or when there is already a
959 definition in a dynamic object. The new symbol is described by
960 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
961 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
962 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
963 of an old common symbol. We set OVERRIDE if the old symbol is
964 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
965 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
966 to change. By OK to change, we mean that we shouldn't warn if the
967 type or size does change. */
45d6a902 968
8a56bd02 969static bfd_boolean
268b6b39
AM
970_bfd_elf_merge_symbol (bfd *abfd,
971 struct bfd_link_info *info,
972 const char *name,
973 Elf_Internal_Sym *sym,
974 asection **psec,
975 bfd_vma *pvalue,
4f3fedcf
AM
976 struct elf_link_hash_entry **sym_hash,
977 bfd **poldbfd,
37a9e49a 978 bfd_boolean *pold_weak,
af44c138 979 unsigned int *pold_alignment,
268b6b39
AM
980 bfd_boolean *skip,
981 bfd_boolean *override,
982 bfd_boolean *type_change_ok,
6e33951e
L
983 bfd_boolean *size_change_ok,
984 bfd_boolean *matched)
252b5132 985{
7479dfd4 986 asection *sec, *oldsec;
45d6a902 987 struct elf_link_hash_entry *h;
90c984fc 988 struct elf_link_hash_entry *hi;
45d6a902
AM
989 struct elf_link_hash_entry *flip;
990 int bind;
991 bfd *oldbfd;
992 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 993 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 994 const struct elf_backend_data *bed;
6e33951e 995 char *new_version;
45d6a902
AM
996
997 *skip = FALSE;
998 *override = FALSE;
999
1000 sec = *psec;
1001 bind = ELF_ST_BIND (sym->st_info);
1002
1003 if (! bfd_is_und_section (sec))
1004 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1005 else
1006 h = ((struct elf_link_hash_entry *)
1007 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1008 if (h == NULL)
1009 return FALSE;
1010 *sym_hash = h;
252b5132 1011
88ba32a0
L
1012 bed = get_elf_backend_data (abfd);
1013
6e33951e 1014 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1015 if (h->versioned != unversioned)
6e33951e 1016 {
422f1182
L
1017 /* Symbol version is unknown or versioned. */
1018 new_version = strrchr (name, ELF_VER_CHR);
1019 if (new_version)
1020 {
1021 if (h->versioned == unknown)
1022 {
1023 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1024 h->versioned = versioned_hidden;
1025 else
1026 h->versioned = versioned;
1027 }
1028 new_version += 1;
1029 if (new_version[0] == '\0')
1030 new_version = NULL;
1031 }
1032 else
1033 h->versioned = unversioned;
6e33951e 1034 }
422f1182
L
1035 else
1036 new_version = NULL;
6e33951e 1037
90c984fc
L
1038 /* For merging, we only care about real symbols. But we need to make
1039 sure that indirect symbol dynamic flags are updated. */
1040 hi = h;
45d6a902
AM
1041 while (h->root.type == bfd_link_hash_indirect
1042 || h->root.type == bfd_link_hash_warning)
1043 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1044
6e33951e
L
1045 if (!*matched)
1046 {
1047 if (hi == h || h->root.type == bfd_link_hash_new)
1048 *matched = TRUE;
1049 else
1050 {
ae7683d2 1051 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1052 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1053 true if the new symbol is only visible to the symbol with
6e33951e 1054 the same symbol version. */
422f1182
L
1055 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1056 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1057 if (!old_hidden && !new_hidden)
1058 /* The new symbol matches the existing symbol if both
1059 aren't hidden. */
1060 *matched = TRUE;
1061 else
1062 {
1063 /* OLD_VERSION is the symbol version of the existing
1064 symbol. */
422f1182
L
1065 char *old_version;
1066
1067 if (h->versioned >= versioned)
1068 old_version = strrchr (h->root.root.string,
1069 ELF_VER_CHR) + 1;
1070 else
1071 old_version = NULL;
6e33951e
L
1072
1073 /* The new symbol matches the existing symbol if they
1074 have the same symbol version. */
1075 *matched = (old_version == new_version
1076 || (old_version != NULL
1077 && new_version != NULL
1078 && strcmp (old_version, new_version) == 0));
1079 }
1080 }
1081 }
1082
934bce08
AM
1083 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1084 existing symbol. */
1085
1086 oldbfd = NULL;
1087 oldsec = NULL;
1088 switch (h->root.type)
1089 {
1090 default:
1091 break;
1092
1093 case bfd_link_hash_undefined:
1094 case bfd_link_hash_undefweak:
1095 oldbfd = h->root.u.undef.abfd;
1096 break;
1097
1098 case bfd_link_hash_defined:
1099 case bfd_link_hash_defweak:
1100 oldbfd = h->root.u.def.section->owner;
1101 oldsec = h->root.u.def.section;
1102 break;
1103
1104 case bfd_link_hash_common:
1105 oldbfd = h->root.u.c.p->section->owner;
1106 oldsec = h->root.u.c.p->section;
1107 if (pold_alignment)
1108 *pold_alignment = h->root.u.c.p->alignment_power;
1109 break;
1110 }
1111 if (poldbfd && *poldbfd == NULL)
1112 *poldbfd = oldbfd;
1113
1114 /* Differentiate strong and weak symbols. */
1115 newweak = bind == STB_WEAK;
1116 oldweak = (h->root.type == bfd_link_hash_defweak
1117 || h->root.type == bfd_link_hash_undefweak);
1118 if (pold_weak)
1119 *pold_weak = oldweak;
1120
1121 /* This code is for coping with dynamic objects, and is only useful
1122 if we are doing an ELF link. */
1123 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1124 return TRUE;
1125
40b36307 1126 /* We have to check it for every instance since the first few may be
ee659f1f 1127 references and not all compilers emit symbol type for undefined
40b36307
L
1128 symbols. */
1129 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1130
ee659f1f
AM
1131 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1132 respectively, is from a dynamic object. */
1133
1134 newdyn = (abfd->flags & DYNAMIC) != 0;
1135
1136 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1137 syms and defined syms in dynamic libraries respectively.
1138 ref_dynamic on the other hand can be set for a symbol defined in
1139 a dynamic library, and def_dynamic may not be set; When the
1140 definition in a dynamic lib is overridden by a definition in the
1141 executable use of the symbol in the dynamic lib becomes a
1142 reference to the executable symbol. */
1143 if (newdyn)
1144 {
1145 if (bfd_is_und_section (sec))
1146 {
1147 if (bind != STB_WEAK)
1148 {
1149 h->ref_dynamic_nonweak = 1;
1150 hi->ref_dynamic_nonweak = 1;
1151 }
1152 }
1153 else
1154 {
6e33951e
L
1155 /* Update the existing symbol only if they match. */
1156 if (*matched)
1157 h->dynamic_def = 1;
ee659f1f
AM
1158 hi->dynamic_def = 1;
1159 }
1160 }
1161
45d6a902
AM
1162 /* If we just created the symbol, mark it as being an ELF symbol.
1163 Other than that, there is nothing to do--there is no merge issue
1164 with a newly defined symbol--so we just return. */
1165
1166 if (h->root.type == bfd_link_hash_new)
252b5132 1167 {
f5385ebf 1168 h->non_elf = 0;
45d6a902
AM
1169 return TRUE;
1170 }
252b5132 1171
45d6a902
AM
1172 /* In cases involving weak versioned symbols, we may wind up trying
1173 to merge a symbol with itself. Catch that here, to avoid the
1174 confusion that results if we try to override a symbol with
1175 itself. The additional tests catch cases like
1176 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1177 dynamic object, which we do want to handle here. */
1178 if (abfd == oldbfd
895fa45f 1179 && (newweak || oldweak)
45d6a902 1180 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1181 || !h->def_regular))
45d6a902
AM
1182 return TRUE;
1183
707bba77 1184 olddyn = FALSE;
45d6a902
AM
1185 if (oldbfd != NULL)
1186 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1187 else if (oldsec != NULL)
45d6a902 1188 {
707bba77 1189 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1190 indices used by MIPS ELF. */
707bba77 1191 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1192 }
252b5132 1193
45d6a902
AM
1194 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1195 respectively, appear to be a definition rather than reference. */
1196
707bba77 1197 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1198
707bba77
AM
1199 olddef = (h->root.type != bfd_link_hash_undefined
1200 && h->root.type != bfd_link_hash_undefweak
202ac193 1201 && h->root.type != bfd_link_hash_common);
45d6a902 1202
0a36a439
L
1203 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1204 respectively, appear to be a function. */
1205
1206 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1207 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1208
1209 oldfunc = (h->type != STT_NOTYPE
1210 && bed->is_function_type (h->type));
1211
5b677558
AM
1212 /* If creating a default indirect symbol ("foo" or "foo@") from a
1213 dynamic versioned definition ("foo@@") skip doing so if there is
1214 an existing regular definition with a different type. We don't
1215 want, for example, a "time" variable in the executable overriding
1216 a "time" function in a shared library. */
580a2b6e 1217 if (pold_alignment == NULL
580a2b6e
L
1218 && newdyn
1219 && newdef
1220 && !olddyn
5b677558
AM
1221 && (olddef || h->root.type == bfd_link_hash_common)
1222 && ELF_ST_TYPE (sym->st_info) != h->type
1223 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1224 && h->type != STT_NOTYPE
1225 && !(newfunc && oldfunc))
580a2b6e
L
1226 {
1227 *skip = TRUE;
1228 return TRUE;
1229 }
1230
4c34aff8
AM
1231 /* Check TLS symbols. We don't check undefined symbols introduced
1232 by "ld -u" which have no type (and oldbfd NULL), and we don't
1233 check symbols from plugins because they also have no type. */
1234 if (oldbfd != NULL
1235 && (oldbfd->flags & BFD_PLUGIN) == 0
1236 && (abfd->flags & BFD_PLUGIN) == 0
1237 && ELF_ST_TYPE (sym->st_info) != h->type
1238 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1239 {
1240 bfd *ntbfd, *tbfd;
1241 bfd_boolean ntdef, tdef;
1242 asection *ntsec, *tsec;
1243
1244 if (h->type == STT_TLS)
1245 {
3b36f7e6 1246 ntbfd = abfd;
7479dfd4
L
1247 ntsec = sec;
1248 ntdef = newdef;
1249 tbfd = oldbfd;
1250 tsec = oldsec;
1251 tdef = olddef;
1252 }
1253 else
1254 {
1255 ntbfd = oldbfd;
1256 ntsec = oldsec;
1257 ntdef = olddef;
1258 tbfd = abfd;
1259 tsec = sec;
1260 tdef = newdef;
1261 }
1262
1263 if (tdef && ntdef)
4eca0228 1264 _bfd_error_handler
695344c0 1265 /* xgettext:c-format */
191c0c42
AM
1266 (_("%s: TLS definition in %B section %A "
1267 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1268 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1269 else if (!tdef && !ntdef)
4eca0228 1270 _bfd_error_handler
695344c0 1271 /* xgettext:c-format */
191c0c42
AM
1272 (_("%s: TLS reference in %B "
1273 "mismatches non-TLS reference in %B"),
7479dfd4
L
1274 tbfd, ntbfd, h->root.root.string);
1275 else if (tdef)
4eca0228 1276 _bfd_error_handler
695344c0 1277 /* xgettext:c-format */
191c0c42
AM
1278 (_("%s: TLS definition in %B section %A "
1279 "mismatches non-TLS reference in %B"),
7479dfd4
L
1280 tbfd, tsec, ntbfd, h->root.root.string);
1281 else
4eca0228 1282 _bfd_error_handler
695344c0 1283 /* xgettext:c-format */
191c0c42
AM
1284 (_("%s: TLS reference in %B "
1285 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1286 tbfd, ntbfd, ntsec, h->root.root.string);
1287
1288 bfd_set_error (bfd_error_bad_value);
1289 return FALSE;
1290 }
1291
45d6a902
AM
1292 /* If the old symbol has non-default visibility, we ignore the new
1293 definition from a dynamic object. */
1294 if (newdyn
9c7a29a3 1295 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1296 && !bfd_is_und_section (sec))
1297 {
1298 *skip = TRUE;
1299 /* Make sure this symbol is dynamic. */
f5385ebf 1300 h->ref_dynamic = 1;
90c984fc 1301 hi->ref_dynamic = 1;
45d6a902
AM
1302 /* A protected symbol has external availability. Make sure it is
1303 recorded as dynamic.
1304
1305 FIXME: Should we check type and size for protected symbol? */
1306 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1307 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1308 else
1309 return TRUE;
1310 }
1311 else if (!newdyn
9c7a29a3 1312 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1313 && h->def_dynamic)
45d6a902
AM
1314 {
1315 /* If the new symbol with non-default visibility comes from a
1316 relocatable file and the old definition comes from a dynamic
1317 object, we remove the old definition. */
6c9b78e6 1318 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1319 {
1320 /* Handle the case where the old dynamic definition is
1321 default versioned. We need to copy the symbol info from
1322 the symbol with default version to the normal one if it
1323 was referenced before. */
1324 if (h->ref_regular)
1325 {
6c9b78e6 1326 hi->root.type = h->root.type;
d2dee3b2 1327 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1328 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1329
6c9b78e6 1330 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1331 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1332 {
aed81c4e
MR
1333 /* If the new symbol is hidden or internal, completely undo
1334 any dynamic link state. */
1335 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1336 h->forced_local = 0;
1337 h->ref_dynamic = 0;
d2dee3b2
L
1338 }
1339 else
aed81c4e
MR
1340 h->ref_dynamic = 1;
1341
1342 h->def_dynamic = 0;
aed81c4e
MR
1343 /* FIXME: Should we check type and size for protected symbol? */
1344 h->size = 0;
1345 h->type = 0;
1346
6c9b78e6 1347 h = hi;
d2dee3b2
L
1348 }
1349 else
6c9b78e6 1350 h = hi;
d2dee3b2 1351 }
1de1a317 1352
f5eda473
AM
1353 /* If the old symbol was undefined before, then it will still be
1354 on the undefs list. If the new symbol is undefined or
1355 common, we can't make it bfd_link_hash_new here, because new
1356 undefined or common symbols will be added to the undefs list
1357 by _bfd_generic_link_add_one_symbol. Symbols may not be
1358 added twice to the undefs list. Also, if the new symbol is
1359 undefweak then we don't want to lose the strong undef. */
1360 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1361 {
1de1a317 1362 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1363 h->root.u.undef.abfd = abfd;
1364 }
1365 else
1366 {
1367 h->root.type = bfd_link_hash_new;
1368 h->root.u.undef.abfd = NULL;
1369 }
1370
f5eda473 1371 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1372 {
f5eda473
AM
1373 /* If the new symbol is hidden or internal, completely undo
1374 any dynamic link state. */
1375 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1376 h->forced_local = 0;
1377 h->ref_dynamic = 0;
45d6a902 1378 }
f5eda473
AM
1379 else
1380 h->ref_dynamic = 1;
1381 h->def_dynamic = 0;
45d6a902
AM
1382 /* FIXME: Should we check type and size for protected symbol? */
1383 h->size = 0;
1384 h->type = 0;
1385 return TRUE;
1386 }
14a793b2 1387
15b43f48
AM
1388 /* If a new weak symbol definition comes from a regular file and the
1389 old symbol comes from a dynamic library, we treat the new one as
1390 strong. Similarly, an old weak symbol definition from a regular
1391 file is treated as strong when the new symbol comes from a dynamic
1392 library. Further, an old weak symbol from a dynamic library is
1393 treated as strong if the new symbol is from a dynamic library.
1394 This reflects the way glibc's ld.so works.
1395
1396 Do this before setting *type_change_ok or *size_change_ok so that
1397 we warn properly when dynamic library symbols are overridden. */
1398
1399 if (newdef && !newdyn && olddyn)
0f8a2703 1400 newweak = FALSE;
15b43f48 1401 if (olddef && newdyn)
0f8a2703
AM
1402 oldweak = FALSE;
1403
d334575b 1404 /* Allow changes between different types of function symbol. */
0a36a439 1405 if (newfunc && oldfunc)
fcb93ecf
PB
1406 *type_change_ok = TRUE;
1407
79349b09
AM
1408 /* It's OK to change the type if either the existing symbol or the
1409 new symbol is weak. A type change is also OK if the old symbol
1410 is undefined and the new symbol is defined. */
252b5132 1411
79349b09
AM
1412 if (oldweak
1413 || newweak
1414 || (newdef
1415 && h->root.type == bfd_link_hash_undefined))
1416 *type_change_ok = TRUE;
1417
1418 /* It's OK to change the size if either the existing symbol or the
1419 new symbol is weak, or if the old symbol is undefined. */
1420
1421 if (*type_change_ok
1422 || h->root.type == bfd_link_hash_undefined)
1423 *size_change_ok = TRUE;
45d6a902 1424
45d6a902
AM
1425 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1426 symbol, respectively, appears to be a common symbol in a dynamic
1427 object. If a symbol appears in an uninitialized section, and is
1428 not weak, and is not a function, then it may be a common symbol
1429 which was resolved when the dynamic object was created. We want
1430 to treat such symbols specially, because they raise special
1431 considerations when setting the symbol size: if the symbol
1432 appears as a common symbol in a regular object, and the size in
1433 the regular object is larger, we must make sure that we use the
1434 larger size. This problematic case can always be avoided in C,
1435 but it must be handled correctly when using Fortran shared
1436 libraries.
1437
1438 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1439 likewise for OLDDYNCOMMON and OLDDEF.
1440
1441 Note that this test is just a heuristic, and that it is quite
1442 possible to have an uninitialized symbol in a shared object which
1443 is really a definition, rather than a common symbol. This could
1444 lead to some minor confusion when the symbol really is a common
1445 symbol in some regular object. However, I think it will be
1446 harmless. */
1447
1448 if (newdyn
1449 && newdef
79349b09 1450 && !newweak
45d6a902
AM
1451 && (sec->flags & SEC_ALLOC) != 0
1452 && (sec->flags & SEC_LOAD) == 0
1453 && sym->st_size > 0
0a36a439 1454 && !newfunc)
45d6a902
AM
1455 newdyncommon = TRUE;
1456 else
1457 newdyncommon = FALSE;
1458
1459 if (olddyn
1460 && olddef
1461 && h->root.type == bfd_link_hash_defined
f5385ebf 1462 && h->def_dynamic
45d6a902
AM
1463 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1464 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1465 && h->size > 0
0a36a439 1466 && !oldfunc)
45d6a902
AM
1467 olddyncommon = TRUE;
1468 else
1469 olddyncommon = FALSE;
1470
a4d8e49b
L
1471 /* We now know everything about the old and new symbols. We ask the
1472 backend to check if we can merge them. */
5d13b3b3
AM
1473 if (bed->merge_symbol != NULL)
1474 {
1475 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1476 return FALSE;
1477 sec = *psec;
1478 }
a4d8e49b 1479
45d6a902
AM
1480 /* If both the old and the new symbols look like common symbols in a
1481 dynamic object, set the size of the symbol to the larger of the
1482 two. */
1483
1484 if (olddyncommon
1485 && newdyncommon
1486 && sym->st_size != h->size)
1487 {
1488 /* Since we think we have two common symbols, issue a multiple
1489 common warning if desired. Note that we only warn if the
1490 size is different. If the size is the same, we simply let
1491 the old symbol override the new one as normally happens with
1492 symbols defined in dynamic objects. */
1493
1a72702b
AM
1494 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1495 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1496 if (sym->st_size > h->size)
1497 h->size = sym->st_size;
252b5132 1498
45d6a902 1499 *size_change_ok = TRUE;
252b5132
RH
1500 }
1501
45d6a902
AM
1502 /* If we are looking at a dynamic object, and we have found a
1503 definition, we need to see if the symbol was already defined by
1504 some other object. If so, we want to use the existing
1505 definition, and we do not want to report a multiple symbol
1506 definition error; we do this by clobbering *PSEC to be
1507 bfd_und_section_ptr.
1508
1509 We treat a common symbol as a definition if the symbol in the
1510 shared library is a function, since common symbols always
1511 represent variables; this can cause confusion in principle, but
1512 any such confusion would seem to indicate an erroneous program or
1513 shared library. We also permit a common symbol in a regular
202ac193
L
1514 object to override a weak symbol in a shared object. A common
1515 symbol in executable also overrides a symbol in a shared object. */
45d6a902
AM
1516
1517 if (newdyn
1518 && newdef
77cfaee6 1519 && (olddef
45d6a902 1520 || (h->root.type == bfd_link_hash_common
202ac193
L
1521 && (newweak
1522 || newfunc
1523 || (!olddyn && bfd_link_executable (info))))))
45d6a902
AM
1524 {
1525 *override = TRUE;
1526 newdef = FALSE;
1527 newdyncommon = FALSE;
252b5132 1528
45d6a902
AM
1529 *psec = sec = bfd_und_section_ptr;
1530 *size_change_ok = TRUE;
252b5132 1531
45d6a902
AM
1532 /* If we get here when the old symbol is a common symbol, then
1533 we are explicitly letting it override a weak symbol or
1534 function in a dynamic object, and we don't want to warn about
1535 a type change. If the old symbol is a defined symbol, a type
1536 change warning may still be appropriate. */
252b5132 1537
45d6a902
AM
1538 if (h->root.type == bfd_link_hash_common)
1539 *type_change_ok = TRUE;
1540 }
1541
1542 /* Handle the special case of an old common symbol merging with a
1543 new symbol which looks like a common symbol in a shared object.
1544 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1545 common symbol, and let _bfd_generic_link_add_one_symbol do the
1546 right thing. */
45d6a902
AM
1547
1548 if (newdyncommon
1549 && h->root.type == bfd_link_hash_common)
1550 {
1551 *override = TRUE;
1552 newdef = FALSE;
1553 newdyncommon = FALSE;
1554 *pvalue = sym->st_size;
a4d8e49b 1555 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1556 *size_change_ok = TRUE;
1557 }
1558
c5e2cead 1559 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1560 if (newdef && olddef && newweak)
54ac0771 1561 {
35ed3f94 1562 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1563 if (!(oldbfd != NULL
1564 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1565 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1566 {
1567 newdef = FALSE;
1568 *skip = TRUE;
1569 }
54ac0771
L
1570
1571 /* Merge st_other. If the symbol already has a dynamic index,
1572 but visibility says it should not be visible, turn it into a
1573 local symbol. */
b8417128 1574 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1575 if (h->dynindx != -1)
1576 switch (ELF_ST_VISIBILITY (h->other))
1577 {
1578 case STV_INTERNAL:
1579 case STV_HIDDEN:
1580 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1581 break;
1582 }
1583 }
c5e2cead 1584
45d6a902
AM
1585 /* If the old symbol is from a dynamic object, and the new symbol is
1586 a definition which is not from a dynamic object, then the new
1587 symbol overrides the old symbol. Symbols from regular files
1588 always take precedence over symbols from dynamic objects, even if
1589 they are defined after the dynamic object in the link.
1590
1591 As above, we again permit a common symbol in a regular object to
1592 override a definition in a shared object if the shared object
0f8a2703 1593 symbol is a function or is weak. */
45d6a902
AM
1594
1595 flip = NULL;
77cfaee6 1596 if (!newdyn
45d6a902
AM
1597 && (newdef
1598 || (bfd_is_com_section (sec)
0a36a439 1599 && (oldweak || oldfunc)))
45d6a902
AM
1600 && olddyn
1601 && olddef
f5385ebf 1602 && h->def_dynamic)
45d6a902
AM
1603 {
1604 /* Change the hash table entry to undefined, and let
1605 _bfd_generic_link_add_one_symbol do the right thing with the
1606 new definition. */
1607
1608 h->root.type = bfd_link_hash_undefined;
1609 h->root.u.undef.abfd = h->root.u.def.section->owner;
1610 *size_change_ok = TRUE;
1611
1612 olddef = FALSE;
1613 olddyncommon = FALSE;
1614
1615 /* We again permit a type change when a common symbol may be
1616 overriding a function. */
1617
1618 if (bfd_is_com_section (sec))
0a36a439
L
1619 {
1620 if (oldfunc)
1621 {
1622 /* If a common symbol overrides a function, make sure
1623 that it isn't defined dynamically nor has type
1624 function. */
1625 h->def_dynamic = 0;
1626 h->type = STT_NOTYPE;
1627 }
1628 *type_change_ok = TRUE;
1629 }
45d6a902 1630
6c9b78e6
AM
1631 if (hi->root.type == bfd_link_hash_indirect)
1632 flip = hi;
45d6a902
AM
1633 else
1634 /* This union may have been set to be non-NULL when this symbol
1635 was seen in a dynamic object. We must force the union to be
1636 NULL, so that it is correct for a regular symbol. */
1637 h->verinfo.vertree = NULL;
1638 }
1639
1640 /* Handle the special case of a new common symbol merging with an
1641 old symbol that looks like it might be a common symbol defined in
1642 a shared object. Note that we have already handled the case in
1643 which a new common symbol should simply override the definition
1644 in the shared library. */
1645
1646 if (! newdyn
1647 && bfd_is_com_section (sec)
1648 && olddyncommon)
1649 {
1650 /* It would be best if we could set the hash table entry to a
1651 common symbol, but we don't know what to use for the section
1652 or the alignment. */
1a72702b
AM
1653 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1654 bfd_link_hash_common, sym->st_size);
45d6a902 1655
4cc11e76 1656 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1657 larger, pretend that the new symbol has its size. */
1658
1659 if (h->size > *pvalue)
1660 *pvalue = h->size;
1661
af44c138
L
1662 /* We need to remember the alignment required by the symbol
1663 in the dynamic object. */
1664 BFD_ASSERT (pold_alignment);
1665 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1666
1667 olddef = FALSE;
1668 olddyncommon = FALSE;
1669
1670 h->root.type = bfd_link_hash_undefined;
1671 h->root.u.undef.abfd = h->root.u.def.section->owner;
1672
1673 *size_change_ok = TRUE;
1674 *type_change_ok = TRUE;
1675
6c9b78e6
AM
1676 if (hi->root.type == bfd_link_hash_indirect)
1677 flip = hi;
45d6a902
AM
1678 else
1679 h->verinfo.vertree = NULL;
1680 }
1681
1682 if (flip != NULL)
1683 {
1684 /* Handle the case where we had a versioned symbol in a dynamic
1685 library and now find a definition in a normal object. In this
1686 case, we make the versioned symbol point to the normal one. */
45d6a902 1687 flip->root.type = h->root.type;
00cbee0a 1688 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1689 h->root.type = bfd_link_hash_indirect;
1690 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1691 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1692 if (h->def_dynamic)
45d6a902 1693 {
f5385ebf
AM
1694 h->def_dynamic = 0;
1695 flip->ref_dynamic = 1;
45d6a902
AM
1696 }
1697 }
1698
45d6a902
AM
1699 return TRUE;
1700}
1701
1702/* This function is called to create an indirect symbol from the
1703 default for the symbol with the default version if needed. The
4f3fedcf 1704 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1705 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1706
28caa186 1707static bfd_boolean
268b6b39
AM
1708_bfd_elf_add_default_symbol (bfd *abfd,
1709 struct bfd_link_info *info,
1710 struct elf_link_hash_entry *h,
1711 const char *name,
1712 Elf_Internal_Sym *sym,
4f3fedcf
AM
1713 asection *sec,
1714 bfd_vma value,
1715 bfd **poldbfd,
e3c9d234 1716 bfd_boolean *dynsym)
45d6a902
AM
1717{
1718 bfd_boolean type_change_ok;
1719 bfd_boolean size_change_ok;
1720 bfd_boolean skip;
1721 char *shortname;
1722 struct elf_link_hash_entry *hi;
1723 struct bfd_link_hash_entry *bh;
9c5bfbb7 1724 const struct elf_backend_data *bed;
45d6a902
AM
1725 bfd_boolean collect;
1726 bfd_boolean dynamic;
e3c9d234 1727 bfd_boolean override;
45d6a902
AM
1728 char *p;
1729 size_t len, shortlen;
ffd65175 1730 asection *tmp_sec;
6e33951e 1731 bfd_boolean matched;
45d6a902 1732
422f1182
L
1733 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1734 return TRUE;
1735
45d6a902
AM
1736 /* If this symbol has a version, and it is the default version, we
1737 create an indirect symbol from the default name to the fully
1738 decorated name. This will cause external references which do not
1739 specify a version to be bound to this version of the symbol. */
1740 p = strchr (name, ELF_VER_CHR);
422f1182
L
1741 if (h->versioned == unknown)
1742 {
1743 if (p == NULL)
1744 {
1745 h->versioned = unversioned;
1746 return TRUE;
1747 }
1748 else
1749 {
1750 if (p[1] != ELF_VER_CHR)
1751 {
1752 h->versioned = versioned_hidden;
1753 return TRUE;
1754 }
1755 else
1756 h->versioned = versioned;
1757 }
1758 }
4373f8af
L
1759 else
1760 {
1761 /* PR ld/19073: We may see an unversioned definition after the
1762 default version. */
1763 if (p == NULL)
1764 return TRUE;
1765 }
45d6a902 1766
45d6a902
AM
1767 bed = get_elf_backend_data (abfd);
1768 collect = bed->collect;
1769 dynamic = (abfd->flags & DYNAMIC) != 0;
1770
1771 shortlen = p - name;
a50b1753 1772 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1773 if (shortname == NULL)
1774 return FALSE;
1775 memcpy (shortname, name, shortlen);
1776 shortname[shortlen] = '\0';
1777
1778 /* We are going to create a new symbol. Merge it with any existing
1779 symbol with this name. For the purposes of the merge, act as
1780 though we were defining the symbol we just defined, although we
1781 actually going to define an indirect symbol. */
1782 type_change_ok = FALSE;
1783 size_change_ok = FALSE;
6e33951e 1784 matched = TRUE;
ffd65175
AM
1785 tmp_sec = sec;
1786 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1787 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1788 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1789 return FALSE;
1790
1791 if (skip)
1792 goto nondefault;
1793
5b677558
AM
1794 if (hi->def_regular)
1795 {
1796 /* If the undecorated symbol will have a version added by a
1797 script different to H, then don't indirect to/from the
1798 undecorated symbol. This isn't ideal because we may not yet
1799 have seen symbol versions, if given by a script on the
1800 command line rather than via --version-script. */
1801 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1802 {
1803 bfd_boolean hide;
1804
1805 hi->verinfo.vertree
1806 = bfd_find_version_for_sym (info->version_info,
1807 hi->root.root.string, &hide);
1808 if (hi->verinfo.vertree != NULL && hide)
1809 {
1810 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1811 goto nondefault;
1812 }
1813 }
1814 if (hi->verinfo.vertree != NULL
1815 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1816 goto nondefault;
1817 }
1818
45d6a902
AM
1819 if (! override)
1820 {
c6e8a9a8 1821 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1822 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1823 {
1824 bh = &hi->root;
1825 if (! (_bfd_generic_link_add_one_symbol
1826 (info, abfd, shortname, BSF_INDIRECT,
1827 bfd_ind_section_ptr,
1828 0, name, FALSE, collect, &bh)))
1829 return FALSE;
1830 hi = (struct elf_link_hash_entry *) bh;
1831 }
45d6a902
AM
1832 }
1833 else
1834 {
1835 /* In this case the symbol named SHORTNAME is overriding the
1836 indirect symbol we want to add. We were planning on making
1837 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1838 is the name without a version. NAME is the fully versioned
1839 name, and it is the default version.
1840
1841 Overriding means that we already saw a definition for the
1842 symbol SHORTNAME in a regular object, and it is overriding
1843 the symbol defined in the dynamic object.
1844
1845 When this happens, we actually want to change NAME, the
1846 symbol we just added, to refer to SHORTNAME. This will cause
1847 references to NAME in the shared object to become references
1848 to SHORTNAME in the regular object. This is what we expect
1849 when we override a function in a shared object: that the
1850 references in the shared object will be mapped to the
1851 definition in the regular object. */
1852
1853 while (hi->root.type == bfd_link_hash_indirect
1854 || hi->root.type == bfd_link_hash_warning)
1855 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1856
1857 h->root.type = bfd_link_hash_indirect;
1858 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1859 if (h->def_dynamic)
45d6a902 1860 {
f5385ebf
AM
1861 h->def_dynamic = 0;
1862 hi->ref_dynamic = 1;
1863 if (hi->ref_regular
1864 || hi->def_regular)
45d6a902 1865 {
c152c796 1866 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1867 return FALSE;
1868 }
1869 }
1870
1871 /* Now set HI to H, so that the following code will set the
1872 other fields correctly. */
1873 hi = h;
1874 }
1875
fab4a87f
L
1876 /* Check if HI is a warning symbol. */
1877 if (hi->root.type == bfd_link_hash_warning)
1878 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1879
45d6a902
AM
1880 /* If there is a duplicate definition somewhere, then HI may not
1881 point to an indirect symbol. We will have reported an error to
1882 the user in that case. */
1883
1884 if (hi->root.type == bfd_link_hash_indirect)
1885 {
1886 struct elf_link_hash_entry *ht;
1887
45d6a902 1888 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1889 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1890
68c88cd4
AM
1891 /* A reference to the SHORTNAME symbol from a dynamic library
1892 will be satisfied by the versioned symbol at runtime. In
1893 effect, we have a reference to the versioned symbol. */
1894 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1895 hi->dynamic_def |= ht->dynamic_def;
1896
45d6a902
AM
1897 /* See if the new flags lead us to realize that the symbol must
1898 be dynamic. */
1899 if (! *dynsym)
1900 {
1901 if (! dynamic)
1902 {
0e1862bb 1903 if (! bfd_link_executable (info)
90c984fc 1904 || hi->def_dynamic
f5385ebf 1905 || hi->ref_dynamic)
45d6a902
AM
1906 *dynsym = TRUE;
1907 }
1908 else
1909 {
f5385ebf 1910 if (hi->ref_regular)
45d6a902
AM
1911 *dynsym = TRUE;
1912 }
1913 }
1914 }
1915
1916 /* We also need to define an indirection from the nondefault version
1917 of the symbol. */
1918
1919nondefault:
1920 len = strlen (name);
a50b1753 1921 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1922 if (shortname == NULL)
1923 return FALSE;
1924 memcpy (shortname, name, shortlen);
1925 memcpy (shortname + shortlen, p + 1, len - shortlen);
1926
1927 /* Once again, merge with any existing symbol. */
1928 type_change_ok = FALSE;
1929 size_change_ok = FALSE;
ffd65175
AM
1930 tmp_sec = sec;
1931 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1932 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1933 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1934 return FALSE;
1935
1936 if (skip)
1937 return TRUE;
1938
1939 if (override)
1940 {
1941 /* Here SHORTNAME is a versioned name, so we don't expect to see
1942 the type of override we do in the case above unless it is
4cc11e76 1943 overridden by a versioned definition. */
45d6a902
AM
1944 if (hi->root.type != bfd_link_hash_defined
1945 && hi->root.type != bfd_link_hash_defweak)
4eca0228 1946 _bfd_error_handler
695344c0 1947 /* xgettext:c-format */
d003868e
AM
1948 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1949 abfd, shortname);
45d6a902
AM
1950 }
1951 else
1952 {
1953 bh = &hi->root;
1954 if (! (_bfd_generic_link_add_one_symbol
1955 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1956 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1957 return FALSE;
1958 hi = (struct elf_link_hash_entry *) bh;
1959
1960 /* If there is a duplicate definition somewhere, then HI may not
1961 point to an indirect symbol. We will have reported an error
1962 to the user in that case. */
1963
1964 if (hi->root.type == bfd_link_hash_indirect)
1965 {
fcfa13d2 1966 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
1967 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1968 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
1969
1970 /* See if the new flags lead us to realize that the symbol
1971 must be dynamic. */
1972 if (! *dynsym)
1973 {
1974 if (! dynamic)
1975 {
0e1862bb 1976 if (! bfd_link_executable (info)
f5385ebf 1977 || hi->ref_dynamic)
45d6a902
AM
1978 *dynsym = TRUE;
1979 }
1980 else
1981 {
f5385ebf 1982 if (hi->ref_regular)
45d6a902
AM
1983 *dynsym = TRUE;
1984 }
1985 }
1986 }
1987 }
1988
1989 return TRUE;
1990}
1991\f
1992/* This routine is used to export all defined symbols into the dynamic
1993 symbol table. It is called via elf_link_hash_traverse. */
1994
28caa186 1995static bfd_boolean
268b6b39 1996_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1997{
a50b1753 1998 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
1999
2000 /* Ignore indirect symbols. These are added by the versioning code. */
2001 if (h->root.type == bfd_link_hash_indirect)
2002 return TRUE;
2003
7686d77d
AM
2004 /* Ignore this if we won't export it. */
2005 if (!eif->info->export_dynamic && !h->dynamic)
2006 return TRUE;
45d6a902
AM
2007
2008 if (h->dynindx == -1
fd91d419
L
2009 && (h->def_regular || h->ref_regular)
2010 && ! bfd_hide_sym_by_version (eif->info->version_info,
2011 h->root.root.string))
45d6a902 2012 {
fd91d419 2013 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2014 {
fd91d419
L
2015 eif->failed = TRUE;
2016 return FALSE;
45d6a902
AM
2017 }
2018 }
2019
2020 return TRUE;
2021}
2022\f
2023/* Look through the symbols which are defined in other shared
2024 libraries and referenced here. Update the list of version
2025 dependencies. This will be put into the .gnu.version_r section.
2026 This function is called via elf_link_hash_traverse. */
2027
28caa186 2028static bfd_boolean
268b6b39
AM
2029_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2030 void *data)
45d6a902 2031{
a50b1753 2032 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2033 Elf_Internal_Verneed *t;
2034 Elf_Internal_Vernaux *a;
2035 bfd_size_type amt;
2036
45d6a902
AM
2037 /* We only care about symbols defined in shared objects with version
2038 information. */
f5385ebf
AM
2039 if (!h->def_dynamic
2040 || h->def_regular
45d6a902 2041 || h->dynindx == -1
7b20f099
AM
2042 || h->verinfo.verdef == NULL
2043 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2044 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2045 return TRUE;
2046
2047 /* See if we already know about this version. */
28caa186
AM
2048 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2049 t != NULL;
2050 t = t->vn_nextref)
45d6a902
AM
2051 {
2052 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2053 continue;
2054
2055 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2056 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2057 return TRUE;
2058
2059 break;
2060 }
2061
2062 /* This is a new version. Add it to tree we are building. */
2063
2064 if (t == NULL)
2065 {
2066 amt = sizeof *t;
a50b1753 2067 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2068 if (t == NULL)
2069 {
2070 rinfo->failed = TRUE;
2071 return FALSE;
2072 }
2073
2074 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2075 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2076 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2077 }
2078
2079 amt = sizeof *a;
a50b1753 2080 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2081 if (a == NULL)
2082 {
2083 rinfo->failed = TRUE;
2084 return FALSE;
2085 }
45d6a902
AM
2086
2087 /* Note that we are copying a string pointer here, and testing it
2088 above. If bfd_elf_string_from_elf_section is ever changed to
2089 discard the string data when low in memory, this will have to be
2090 fixed. */
2091 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2092
2093 a->vna_flags = h->verinfo.verdef->vd_flags;
2094 a->vna_nextptr = t->vn_auxptr;
2095
2096 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2097 ++rinfo->vers;
2098
2099 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2100
2101 t->vn_auxptr = a;
2102
2103 return TRUE;
2104}
2105
2106/* Figure out appropriate versions for all the symbols. We may not
2107 have the version number script until we have read all of the input
2108 files, so until that point we don't know which symbols should be
2109 local. This function is called via elf_link_hash_traverse. */
2110
28caa186 2111static bfd_boolean
268b6b39 2112_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2113{
28caa186 2114 struct elf_info_failed *sinfo;
45d6a902 2115 struct bfd_link_info *info;
9c5bfbb7 2116 const struct elf_backend_data *bed;
45d6a902
AM
2117 struct elf_info_failed eif;
2118 char *p;
45d6a902 2119
a50b1753 2120 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2121 info = sinfo->info;
2122
45d6a902
AM
2123 /* Fix the symbol flags. */
2124 eif.failed = FALSE;
2125 eif.info = info;
2126 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2127 {
2128 if (eif.failed)
2129 sinfo->failed = TRUE;
2130 return FALSE;
2131 }
2132
2133 /* We only need version numbers for symbols defined in regular
2134 objects. */
f5385ebf 2135 if (!h->def_regular)
45d6a902
AM
2136 return TRUE;
2137
28caa186 2138 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2139 p = strchr (h->root.root.string, ELF_VER_CHR);
2140 if (p != NULL && h->verinfo.vertree == NULL)
2141 {
2142 struct bfd_elf_version_tree *t;
45d6a902 2143
45d6a902
AM
2144 ++p;
2145 if (*p == ELF_VER_CHR)
6e33951e 2146 ++p;
45d6a902
AM
2147
2148 /* If there is no version string, we can just return out. */
2149 if (*p == '\0')
6e33951e 2150 return TRUE;
45d6a902
AM
2151
2152 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2153 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2154 {
2155 if (strcmp (t->name, p) == 0)
2156 {
2157 size_t len;
2158 char *alc;
2159 struct bfd_elf_version_expr *d;
2160
2161 len = p - h->root.root.string;
a50b1753 2162 alc = (char *) bfd_malloc (len);
45d6a902 2163 if (alc == NULL)
14b1c01e
AM
2164 {
2165 sinfo->failed = TRUE;
2166 return FALSE;
2167 }
45d6a902
AM
2168 memcpy (alc, h->root.root.string, len - 1);
2169 alc[len - 1] = '\0';
2170 if (alc[len - 2] == ELF_VER_CHR)
2171 alc[len - 2] = '\0';
2172
2173 h->verinfo.vertree = t;
2174 t->used = TRUE;
2175 d = NULL;
2176
108ba305
JJ
2177 if (t->globals.list != NULL)
2178 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2179
2180 /* See if there is anything to force this symbol to
2181 local scope. */
108ba305 2182 if (d == NULL && t->locals.list != NULL)
45d6a902 2183 {
108ba305
JJ
2184 d = (*t->match) (&t->locals, NULL, alc);
2185 if (d != NULL
2186 && h->dynindx != -1
108ba305
JJ
2187 && ! info->export_dynamic)
2188 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2189 }
2190
2191 free (alc);
2192 break;
2193 }
2194 }
2195
2196 /* If we are building an application, we need to create a
2197 version node for this version. */
0e1862bb 2198 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2199 {
2200 struct bfd_elf_version_tree **pp;
2201 int version_index;
2202
2203 /* If we aren't going to export this symbol, we don't need
2204 to worry about it. */
2205 if (h->dynindx == -1)
2206 return TRUE;
2207
ef53be89
AM
2208 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2209 sizeof *t);
45d6a902
AM
2210 if (t == NULL)
2211 {
2212 sinfo->failed = TRUE;
2213 return FALSE;
2214 }
2215
45d6a902 2216 t->name = p;
45d6a902
AM
2217 t->name_indx = (unsigned int) -1;
2218 t->used = TRUE;
2219
2220 version_index = 1;
2221 /* Don't count anonymous version tag. */
fd91d419
L
2222 if (sinfo->info->version_info != NULL
2223 && sinfo->info->version_info->vernum == 0)
45d6a902 2224 version_index = 0;
fd91d419
L
2225 for (pp = &sinfo->info->version_info;
2226 *pp != NULL;
2227 pp = &(*pp)->next)
45d6a902
AM
2228 ++version_index;
2229 t->vernum = version_index;
2230
2231 *pp = t;
2232
2233 h->verinfo.vertree = t;
2234 }
2235 else if (t == NULL)
2236 {
2237 /* We could not find the version for a symbol when
2238 generating a shared archive. Return an error. */
4eca0228 2239 _bfd_error_handler
695344c0 2240 /* xgettext:c-format */
c55fe096 2241 (_("%B: version node not found for symbol %s"),
28caa186 2242 info->output_bfd, h->root.root.string);
45d6a902
AM
2243 bfd_set_error (bfd_error_bad_value);
2244 sinfo->failed = TRUE;
2245 return FALSE;
2246 }
45d6a902
AM
2247 }
2248
2249 /* If we don't have a version for this symbol, see if we can find
2250 something. */
fd91d419 2251 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2252 {
1e8fa21e 2253 bfd_boolean hide;
ae5a3597 2254
fd91d419
L
2255 h->verinfo.vertree
2256 = bfd_find_version_for_sym (sinfo->info->version_info,
2257 h->root.root.string, &hide);
1e8fa21e
AM
2258 if (h->verinfo.vertree != NULL && hide)
2259 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2260 }
2261
2262 return TRUE;
2263}
2264\f
45d6a902
AM
2265/* Read and swap the relocs from the section indicated by SHDR. This
2266 may be either a REL or a RELA section. The relocations are
2267 translated into RELA relocations and stored in INTERNAL_RELOCS,
2268 which should have already been allocated to contain enough space.
2269 The EXTERNAL_RELOCS are a buffer where the external form of the
2270 relocations should be stored.
2271
2272 Returns FALSE if something goes wrong. */
2273
2274static bfd_boolean
268b6b39 2275elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2276 asection *sec,
268b6b39
AM
2277 Elf_Internal_Shdr *shdr,
2278 void *external_relocs,
2279 Elf_Internal_Rela *internal_relocs)
45d6a902 2280{
9c5bfbb7 2281 const struct elf_backend_data *bed;
268b6b39 2282 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2283 const bfd_byte *erela;
2284 const bfd_byte *erelaend;
2285 Elf_Internal_Rela *irela;
243ef1e0
L
2286 Elf_Internal_Shdr *symtab_hdr;
2287 size_t nsyms;
45d6a902 2288
45d6a902
AM
2289 /* Position ourselves at the start of the section. */
2290 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2291 return FALSE;
2292
2293 /* Read the relocations. */
2294 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2295 return FALSE;
2296
243ef1e0 2297 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2298 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2299
45d6a902
AM
2300 bed = get_elf_backend_data (abfd);
2301
2302 /* Convert the external relocations to the internal format. */
2303 if (shdr->sh_entsize == bed->s->sizeof_rel)
2304 swap_in = bed->s->swap_reloc_in;
2305 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2306 swap_in = bed->s->swap_reloca_in;
2307 else
2308 {
2309 bfd_set_error (bfd_error_wrong_format);
2310 return FALSE;
2311 }
2312
a50b1753 2313 erela = (const bfd_byte *) external_relocs;
51992aec 2314 erelaend = erela + shdr->sh_size;
45d6a902
AM
2315 irela = internal_relocs;
2316 while (erela < erelaend)
2317 {
243ef1e0
L
2318 bfd_vma r_symndx;
2319
45d6a902 2320 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2321 r_symndx = ELF32_R_SYM (irela->r_info);
2322 if (bed->s->arch_size == 64)
2323 r_symndx >>= 24;
ce98a316
NC
2324 if (nsyms > 0)
2325 {
2326 if ((size_t) r_symndx >= nsyms)
2327 {
4eca0228 2328 _bfd_error_handler
695344c0 2329 /* xgettext:c-format */
ce98a316
NC
2330 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2331 " for offset 0x%lx in section `%A'"),
2332 abfd, sec,
2333 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2334 bfd_set_error (bfd_error_bad_value);
2335 return FALSE;
2336 }
2337 }
cf35638d 2338 else if (r_symndx != STN_UNDEF)
243ef1e0 2339 {
4eca0228 2340 _bfd_error_handler
695344c0 2341 /* xgettext:c-format */
ce98a316
NC
2342 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2343 " when the object file has no symbol table"),
d003868e
AM
2344 abfd, sec,
2345 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2346 bfd_set_error (bfd_error_bad_value);
2347 return FALSE;
2348 }
45d6a902
AM
2349 irela += bed->s->int_rels_per_ext_rel;
2350 erela += shdr->sh_entsize;
2351 }
2352
2353 return TRUE;
2354}
2355
2356/* Read and swap the relocs for a section O. They may have been
2357 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2358 not NULL, they are used as buffers to read into. They are known to
2359 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2360 the return value is allocated using either malloc or bfd_alloc,
2361 according to the KEEP_MEMORY argument. If O has two relocation
2362 sections (both REL and RELA relocations), then the REL_HDR
2363 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2364 RELA_HDR relocations. */
45d6a902
AM
2365
2366Elf_Internal_Rela *
268b6b39
AM
2367_bfd_elf_link_read_relocs (bfd *abfd,
2368 asection *o,
2369 void *external_relocs,
2370 Elf_Internal_Rela *internal_relocs,
2371 bfd_boolean keep_memory)
45d6a902 2372{
268b6b39 2373 void *alloc1 = NULL;
45d6a902 2374 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2375 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2376 struct bfd_elf_section_data *esdo = elf_section_data (o);
2377 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2378
d4730f92
BS
2379 if (esdo->relocs != NULL)
2380 return esdo->relocs;
45d6a902
AM
2381
2382 if (o->reloc_count == 0)
2383 return NULL;
2384
45d6a902
AM
2385 if (internal_relocs == NULL)
2386 {
2387 bfd_size_type size;
2388
2389 size = o->reloc_count;
2390 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2391 if (keep_memory)
a50b1753 2392 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2393 else
a50b1753 2394 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2395 if (internal_relocs == NULL)
2396 goto error_return;
2397 }
2398
2399 if (external_relocs == NULL)
2400 {
d4730f92
BS
2401 bfd_size_type size = 0;
2402
2403 if (esdo->rel.hdr)
2404 size += esdo->rel.hdr->sh_size;
2405 if (esdo->rela.hdr)
2406 size += esdo->rela.hdr->sh_size;
45d6a902 2407
268b6b39 2408 alloc1 = bfd_malloc (size);
45d6a902
AM
2409 if (alloc1 == NULL)
2410 goto error_return;
2411 external_relocs = alloc1;
2412 }
2413
d4730f92
BS
2414 internal_rela_relocs = internal_relocs;
2415 if (esdo->rel.hdr)
2416 {
2417 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2418 external_relocs,
2419 internal_relocs))
2420 goto error_return;
2421 external_relocs = (((bfd_byte *) external_relocs)
2422 + esdo->rel.hdr->sh_size);
2423 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2424 * bed->s->int_rels_per_ext_rel);
2425 }
2426
2427 if (esdo->rela.hdr
2428 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2429 external_relocs,
2430 internal_rela_relocs)))
45d6a902
AM
2431 goto error_return;
2432
2433 /* Cache the results for next time, if we can. */
2434 if (keep_memory)
d4730f92 2435 esdo->relocs = internal_relocs;
45d6a902
AM
2436
2437 if (alloc1 != NULL)
2438 free (alloc1);
2439
2440 /* Don't free alloc2, since if it was allocated we are passing it
2441 back (under the name of internal_relocs). */
2442
2443 return internal_relocs;
2444
2445 error_return:
2446 if (alloc1 != NULL)
2447 free (alloc1);
2448 if (alloc2 != NULL)
4dd07732
AM
2449 {
2450 if (keep_memory)
2451 bfd_release (abfd, alloc2);
2452 else
2453 free (alloc2);
2454 }
45d6a902
AM
2455 return NULL;
2456}
2457
2458/* Compute the size of, and allocate space for, REL_HDR which is the
2459 section header for a section containing relocations for O. */
2460
28caa186 2461static bfd_boolean
9eaff861
AO
2462_bfd_elf_link_size_reloc_section (bfd *abfd,
2463 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2464{
9eaff861 2465 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2466
2467 /* That allows us to calculate the size of the section. */
9eaff861 2468 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2469
2470 /* The contents field must last into write_object_contents, so we
2471 allocate it with bfd_alloc rather than malloc. Also since we
2472 cannot be sure that the contents will actually be filled in,
2473 we zero the allocated space. */
a50b1753 2474 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2475 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2476 return FALSE;
2477
d4730f92 2478 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2479 {
2480 struct elf_link_hash_entry **p;
2481
ca4be51c
AM
2482 p = ((struct elf_link_hash_entry **)
2483 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2484 if (p == NULL)
2485 return FALSE;
2486
d4730f92 2487 reldata->hashes = p;
45d6a902
AM
2488 }
2489
2490 return TRUE;
2491}
2492
2493/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2494 originated from the section given by INPUT_REL_HDR) to the
2495 OUTPUT_BFD. */
2496
2497bfd_boolean
268b6b39
AM
2498_bfd_elf_link_output_relocs (bfd *output_bfd,
2499 asection *input_section,
2500 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2501 Elf_Internal_Rela *internal_relocs,
2502 struct elf_link_hash_entry **rel_hash
2503 ATTRIBUTE_UNUSED)
45d6a902
AM
2504{
2505 Elf_Internal_Rela *irela;
2506 Elf_Internal_Rela *irelaend;
2507 bfd_byte *erel;
d4730f92 2508 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2509 asection *output_section;
9c5bfbb7 2510 const struct elf_backend_data *bed;
268b6b39 2511 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2512 struct bfd_elf_section_data *esdo;
45d6a902
AM
2513
2514 output_section = input_section->output_section;
45d6a902 2515
d4730f92
BS
2516 bed = get_elf_backend_data (output_bfd);
2517 esdo = elf_section_data (output_section);
2518 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2519 {
d4730f92
BS
2520 output_reldata = &esdo->rel;
2521 swap_out = bed->s->swap_reloc_out;
45d6a902 2522 }
d4730f92
BS
2523 else if (esdo->rela.hdr
2524 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2525 {
d4730f92
BS
2526 output_reldata = &esdo->rela;
2527 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2528 }
2529 else
2530 {
4eca0228 2531 _bfd_error_handler
695344c0 2532 /* xgettext:c-format */
d003868e
AM
2533 (_("%B: relocation size mismatch in %B section %A"),
2534 output_bfd, input_section->owner, input_section);
297d8443 2535 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2536 return FALSE;
2537 }
2538
d4730f92
BS
2539 erel = output_reldata->hdr->contents;
2540 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2541 irela = internal_relocs;
2542 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2543 * bed->s->int_rels_per_ext_rel);
2544 while (irela < irelaend)
2545 {
2546 (*swap_out) (output_bfd, irela, erel);
2547 irela += bed->s->int_rels_per_ext_rel;
2548 erel += input_rel_hdr->sh_entsize;
2549 }
2550
2551 /* Bump the counter, so that we know where to add the next set of
2552 relocations. */
d4730f92 2553 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2554
2555 return TRUE;
2556}
2557\f
508c3946
L
2558/* Make weak undefined symbols in PIE dynamic. */
2559
2560bfd_boolean
2561_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2562 struct elf_link_hash_entry *h)
2563{
0e1862bb 2564 if (bfd_link_pie (info)
508c3946
L
2565 && h->dynindx == -1
2566 && h->root.type == bfd_link_hash_undefweak)
2567 return bfd_elf_link_record_dynamic_symbol (info, h);
2568
2569 return TRUE;
2570}
2571
45d6a902
AM
2572/* Fix up the flags for a symbol. This handles various cases which
2573 can only be fixed after all the input files are seen. This is
2574 currently called by both adjust_dynamic_symbol and
2575 assign_sym_version, which is unnecessary but perhaps more robust in
2576 the face of future changes. */
2577
28caa186 2578static bfd_boolean
268b6b39
AM
2579_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2580 struct elf_info_failed *eif)
45d6a902 2581{
33774f08 2582 const struct elf_backend_data *bed;
508c3946 2583
45d6a902
AM
2584 /* If this symbol was mentioned in a non-ELF file, try to set
2585 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2586 permit a non-ELF file to correctly refer to a symbol defined in
2587 an ELF dynamic object. */
f5385ebf 2588 if (h->non_elf)
45d6a902
AM
2589 {
2590 while (h->root.type == bfd_link_hash_indirect)
2591 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2592
2593 if (h->root.type != bfd_link_hash_defined
2594 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2595 {
2596 h->ref_regular = 1;
2597 h->ref_regular_nonweak = 1;
2598 }
45d6a902
AM
2599 else
2600 {
2601 if (h->root.u.def.section->owner != NULL
2602 && (bfd_get_flavour (h->root.u.def.section->owner)
2603 == bfd_target_elf_flavour))
f5385ebf
AM
2604 {
2605 h->ref_regular = 1;
2606 h->ref_regular_nonweak = 1;
2607 }
45d6a902 2608 else
f5385ebf 2609 h->def_regular = 1;
45d6a902
AM
2610 }
2611
2612 if (h->dynindx == -1
f5385ebf
AM
2613 && (h->def_dynamic
2614 || h->ref_dynamic))
45d6a902 2615 {
c152c796 2616 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2617 {
2618 eif->failed = TRUE;
2619 return FALSE;
2620 }
2621 }
2622 }
2623 else
2624 {
f5385ebf 2625 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2626 was first seen in a non-ELF file. Fortunately, if the symbol
2627 was first seen in an ELF file, we're probably OK unless the
2628 symbol was defined in a non-ELF file. Catch that case here.
2629 FIXME: We're still in trouble if the symbol was first seen in
2630 a dynamic object, and then later in a non-ELF regular object. */
2631 if ((h->root.type == bfd_link_hash_defined
2632 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2633 && !h->def_regular
45d6a902
AM
2634 && (h->root.u.def.section->owner != NULL
2635 ? (bfd_get_flavour (h->root.u.def.section->owner)
2636 != bfd_target_elf_flavour)
2637 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2638 && !h->def_dynamic)))
2639 h->def_regular = 1;
45d6a902
AM
2640 }
2641
508c3946 2642 /* Backend specific symbol fixup. */
33774f08
AM
2643 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2644 if (bed->elf_backend_fixup_symbol
2645 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2646 return FALSE;
508c3946 2647
45d6a902
AM
2648 /* If this is a final link, and the symbol was defined as a common
2649 symbol in a regular object file, and there was no definition in
2650 any dynamic object, then the linker will have allocated space for
f5385ebf 2651 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2652 flag will not have been set. */
2653 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2654 && !h->def_regular
2655 && h->ref_regular
2656 && !h->def_dynamic
96f29d96 2657 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2658 h->def_regular = 1;
45d6a902 2659
4deb8f71
L
2660 /* If a weak undefined symbol has non-default visibility, we also
2661 hide it from the dynamic linker. */
2662 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2663 && h->root.type == bfd_link_hash_undefweak)
2664 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2665
2666 /* A hidden versioned symbol in executable should be forced local if
2667 it is is locally defined, not referenced by shared library and not
2668 exported. */
2669 else if (bfd_link_executable (eif->info)
2670 && h->versioned == versioned_hidden
2671 && !eif->info->export_dynamic
2672 && !h->dynamic
2673 && !h->ref_dynamic
2674 && h->def_regular)
2675 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2676
45d6a902
AM
2677 /* If -Bsymbolic was used (which means to bind references to global
2678 symbols to the definition within the shared object), and this
2679 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2680 need a PLT entry. Likewise, if the symbol has non-default
2681 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2682 will force it local. */
4deb8f71
L
2683 else if (h->needs_plt
2684 && bfd_link_pic (eif->info)
2685 && is_elf_hash_table (eif->info->hash)
2686 && (SYMBOLIC_BIND (eif->info, h)
2687 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2688 && h->def_regular)
45d6a902 2689 {
45d6a902
AM
2690 bfd_boolean force_local;
2691
45d6a902
AM
2692 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2693 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2694 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2695 }
2696
45d6a902
AM
2697 /* If this is a weak defined symbol in a dynamic object, and we know
2698 the real definition in the dynamic object, copy interesting flags
2699 over to the real definition. */
f6e332e6 2700 if (h->u.weakdef != NULL)
45d6a902 2701 {
45d6a902
AM
2702 /* If the real definition is defined by a regular object file,
2703 don't do anything special. See the longer description in
2704 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2705 if (h->u.weakdef->def_regular)
f6e332e6 2706 h->u.weakdef = NULL;
45d6a902 2707 else
a26587ba 2708 {
4e6b54a6
AM
2709 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2710
2711 while (h->root.type == bfd_link_hash_indirect)
2712 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2713
2714 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2715 || h->root.type == bfd_link_hash_defweak);
2716 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2717 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2718 || weakdef->root.type == bfd_link_hash_defweak);
2719 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2720 }
45d6a902
AM
2721 }
2722
2723 return TRUE;
2724}
2725
2726/* Make the backend pick a good value for a dynamic symbol. This is
2727 called via elf_link_hash_traverse, and also calls itself
2728 recursively. */
2729
28caa186 2730static bfd_boolean
268b6b39 2731_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2732{
a50b1753 2733 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2734 bfd *dynobj;
9c5bfbb7 2735 const struct elf_backend_data *bed;
45d6a902 2736
0eddce27 2737 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2738 return FALSE;
2739
45d6a902
AM
2740 /* Ignore indirect symbols. These are added by the versioning code. */
2741 if (h->root.type == bfd_link_hash_indirect)
2742 return TRUE;
2743
2744 /* Fix the symbol flags. */
2745 if (! _bfd_elf_fix_symbol_flags (h, eif))
2746 return FALSE;
2747
2748 /* If this symbol does not require a PLT entry, and it is not
2749 defined by a dynamic object, or is not referenced by a regular
2750 object, ignore it. We do have to handle a weak defined symbol,
2751 even if no regular object refers to it, if we decided to add it
2752 to the dynamic symbol table. FIXME: Do we normally need to worry
2753 about symbols which are defined by one dynamic object and
2754 referenced by another one? */
f5385ebf 2755 if (!h->needs_plt
91e21fb7 2756 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2757 && (h->def_regular
2758 || !h->def_dynamic
2759 || (!h->ref_regular
f6e332e6 2760 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2761 {
a6aa5195 2762 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2763 return TRUE;
2764 }
2765
2766 /* If we've already adjusted this symbol, don't do it again. This
2767 can happen via a recursive call. */
f5385ebf 2768 if (h->dynamic_adjusted)
45d6a902
AM
2769 return TRUE;
2770
2771 /* Don't look at this symbol again. Note that we must set this
2772 after checking the above conditions, because we may look at a
2773 symbol once, decide not to do anything, and then get called
2774 recursively later after REF_REGULAR is set below. */
f5385ebf 2775 h->dynamic_adjusted = 1;
45d6a902
AM
2776
2777 /* If this is a weak definition, and we know a real definition, and
2778 the real symbol is not itself defined by a regular object file,
2779 then get a good value for the real definition. We handle the
2780 real symbol first, for the convenience of the backend routine.
2781
2782 Note that there is a confusing case here. If the real definition
2783 is defined by a regular object file, we don't get the real symbol
2784 from the dynamic object, but we do get the weak symbol. If the
2785 processor backend uses a COPY reloc, then if some routine in the
2786 dynamic object changes the real symbol, we will not see that
2787 change in the corresponding weak symbol. This is the way other
2788 ELF linkers work as well, and seems to be a result of the shared
2789 library model.
2790
2791 I will clarify this issue. Most SVR4 shared libraries define the
2792 variable _timezone and define timezone as a weak synonym. The
2793 tzset call changes _timezone. If you write
2794 extern int timezone;
2795 int _timezone = 5;
2796 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2797 you might expect that, since timezone is a synonym for _timezone,
2798 the same number will print both times. However, if the processor
2799 backend uses a COPY reloc, then actually timezone will be copied
2800 into your process image, and, since you define _timezone
2801 yourself, _timezone will not. Thus timezone and _timezone will
2802 wind up at different memory locations. The tzset call will set
2803 _timezone, leaving timezone unchanged. */
2804
f6e332e6 2805 if (h->u.weakdef != NULL)
45d6a902 2806 {
ec24dc88
AM
2807 /* If we get to this point, there is an implicit reference to
2808 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2809 h->u.weakdef->ref_regular = 1;
45d6a902 2810
ec24dc88
AM
2811 /* Ensure that the backend adjust_dynamic_symbol function sees
2812 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2813 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2814 return FALSE;
2815 }
2816
2817 /* If a symbol has no type and no size and does not require a PLT
2818 entry, then we are probably about to do the wrong thing here: we
2819 are probably going to create a COPY reloc for an empty object.
2820 This case can arise when a shared object is built with assembly
2821 code, and the assembly code fails to set the symbol type. */
2822 if (h->size == 0
2823 && h->type == STT_NOTYPE
f5385ebf 2824 && !h->needs_plt)
4eca0228 2825 _bfd_error_handler
45d6a902
AM
2826 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2827 h->root.root.string);
2828
2829 dynobj = elf_hash_table (eif->info)->dynobj;
2830 bed = get_elf_backend_data (dynobj);
e7c33416 2831
45d6a902
AM
2832 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2833 {
2834 eif->failed = TRUE;
2835 return FALSE;
2836 }
2837
2838 return TRUE;
2839}
2840
027297b7
L
2841/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2842 DYNBSS. */
2843
2844bfd_boolean
6cabe1ea
AM
2845_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2846 struct elf_link_hash_entry *h,
027297b7
L
2847 asection *dynbss)
2848{
91ac5911 2849 unsigned int power_of_two;
027297b7
L
2850 bfd_vma mask;
2851 asection *sec = h->root.u.def.section;
2852
2853 /* The section aligment of definition is the maximum alignment
91ac5911
L
2854 requirement of symbols defined in the section. Since we don't
2855 know the symbol alignment requirement, we start with the
2856 maximum alignment and check low bits of the symbol address
2857 for the minimum alignment. */
2858 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2859 mask = ((bfd_vma) 1 << power_of_two) - 1;
2860 while ((h->root.u.def.value & mask) != 0)
2861 {
2862 mask >>= 1;
2863 --power_of_two;
2864 }
027297b7 2865
91ac5911
L
2866 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2867 dynbss))
027297b7
L
2868 {
2869 /* Adjust the section alignment if needed. */
2870 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2871 power_of_two))
027297b7
L
2872 return FALSE;
2873 }
2874
91ac5911 2875 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2876 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2877
2878 /* Define the symbol as being at this point in DYNBSS. */
2879 h->root.u.def.section = dynbss;
2880 h->root.u.def.value = dynbss->size;
2881
2882 /* Increment the size of DYNBSS to make room for the symbol. */
2883 dynbss->size += h->size;
2884
f7483970
L
2885 /* No error if extern_protected_data is true. */
2886 if (h->protected_def
889c2a67
L
2887 && (!info->extern_protected_data
2888 || (info->extern_protected_data < 0
2889 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2890 info->callbacks->einfo
2891 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2892 h->root.root.string);
6cabe1ea 2893
027297b7
L
2894 return TRUE;
2895}
2896
45d6a902
AM
2897/* Adjust all external symbols pointing into SEC_MERGE sections
2898 to reflect the object merging within the sections. */
2899
28caa186 2900static bfd_boolean
268b6b39 2901_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2902{
2903 asection *sec;
2904
45d6a902
AM
2905 if ((h->root.type == bfd_link_hash_defined
2906 || h->root.type == bfd_link_hash_defweak)
2907 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2908 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2909 {
a50b1753 2910 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2911
2912 h->root.u.def.value =
2913 _bfd_merged_section_offset (output_bfd,
2914 &h->root.u.def.section,
2915 elf_section_data (sec)->sec_info,
753731ee 2916 h->root.u.def.value);
45d6a902
AM
2917 }
2918
2919 return TRUE;
2920}
986a241f
RH
2921
2922/* Returns false if the symbol referred to by H should be considered
2923 to resolve local to the current module, and true if it should be
2924 considered to bind dynamically. */
2925
2926bfd_boolean
268b6b39
AM
2927_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2928 struct bfd_link_info *info,
89a2ee5a 2929 bfd_boolean not_local_protected)
986a241f
RH
2930{
2931 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2932 const struct elf_backend_data *bed;
2933 struct elf_link_hash_table *hash_table;
986a241f
RH
2934
2935 if (h == NULL)
2936 return FALSE;
2937
2938 while (h->root.type == bfd_link_hash_indirect
2939 || h->root.type == bfd_link_hash_warning)
2940 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2941
2942 /* If it was forced local, then clearly it's not dynamic. */
2943 if (h->dynindx == -1)
2944 return FALSE;
f5385ebf 2945 if (h->forced_local)
986a241f
RH
2946 return FALSE;
2947
2948 /* Identify the cases where name binding rules say that a
2949 visible symbol resolves locally. */
0e1862bb
L
2950 binding_stays_local_p = (bfd_link_executable (info)
2951 || SYMBOLIC_BIND (info, h));
986a241f
RH
2952
2953 switch (ELF_ST_VISIBILITY (h->other))
2954 {
2955 case STV_INTERNAL:
2956 case STV_HIDDEN:
2957 return FALSE;
2958
2959 case STV_PROTECTED:
fcb93ecf
PB
2960 hash_table = elf_hash_table (info);
2961 if (!is_elf_hash_table (hash_table))
2962 return FALSE;
2963
2964 bed = get_elf_backend_data (hash_table->dynobj);
2965
986a241f
RH
2966 /* Proper resolution for function pointer equality may require
2967 that these symbols perhaps be resolved dynamically, even though
2968 we should be resolving them to the current module. */
89a2ee5a 2969 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2970 binding_stays_local_p = TRUE;
2971 break;
2972
2973 default:
986a241f
RH
2974 break;
2975 }
2976
aa37626c 2977 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2978 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2979 return TRUE;
2980
986a241f
RH
2981 /* Otherwise, the symbol is dynamic if binding rules don't tell
2982 us that it remains local. */
2983 return !binding_stays_local_p;
2984}
f6c52c13
AM
2985
2986/* Return true if the symbol referred to by H should be considered
2987 to resolve local to the current module, and false otherwise. Differs
2988 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2989 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2990 for the place where forced_local and dynindx == -1 are tested. If
2991 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2992 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2993 the symbol is local only for defined symbols.
2994 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2995 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2996 treatment of undefined weak symbols. For those that do not make
2997 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2998
2999bfd_boolean
268b6b39
AM
3000_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3001 struct bfd_link_info *info,
3002 bfd_boolean local_protected)
f6c52c13 3003{
fcb93ecf
PB
3004 const struct elf_backend_data *bed;
3005 struct elf_link_hash_table *hash_table;
3006
f6c52c13
AM
3007 /* If it's a local sym, of course we resolve locally. */
3008 if (h == NULL)
3009 return TRUE;
3010
d95edcac
L
3011 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3012 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3013 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3014 return TRUE;
3015
7e2294f9
AO
3016 /* Common symbols that become definitions don't get the DEF_REGULAR
3017 flag set, so test it first, and don't bail out. */
3018 if (ELF_COMMON_DEF_P (h))
3019 /* Do nothing. */;
f6c52c13 3020 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3021 resolve locally. The sym is either undefined or dynamic. */
3022 else if (!h->def_regular)
f6c52c13
AM
3023 return FALSE;
3024
3025 /* Forced local symbols resolve locally. */
f5385ebf 3026 if (h->forced_local)
f6c52c13
AM
3027 return TRUE;
3028
3029 /* As do non-dynamic symbols. */
3030 if (h->dynindx == -1)
3031 return TRUE;
3032
3033 /* At this point, we know the symbol is defined and dynamic. In an
3034 executable it must resolve locally, likewise when building symbolic
3035 shared libraries. */
0e1862bb 3036 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3037 return TRUE;
3038
3039 /* Now deal with defined dynamic symbols in shared libraries. Ones
3040 with default visibility might not resolve locally. */
3041 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3042 return FALSE;
3043
fcb93ecf
PB
3044 hash_table = elf_hash_table (info);
3045 if (!is_elf_hash_table (hash_table))
3046 return TRUE;
3047
3048 bed = get_elf_backend_data (hash_table->dynobj);
3049
f7483970
L
3050 /* If extern_protected_data is false, STV_PROTECTED non-function
3051 symbols are local. */
889c2a67
L
3052 if ((!info->extern_protected_data
3053 || (info->extern_protected_data < 0
3054 && !bed->extern_protected_data))
3055 && !bed->is_function_type (h->type))
1c16dfa5
L
3056 return TRUE;
3057
f6c52c13 3058 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3059 symbols be treated as dynamic symbols. If the address of a
3060 function not defined in an executable is set to that function's
3061 plt entry in the executable, then the address of the function in
3062 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3063 return local_protected;
3064}
e1918d23
AM
3065
3066/* Caches some TLS segment info, and ensures that the TLS segment vma is
3067 aligned. Returns the first TLS output section. */
3068
3069struct bfd_section *
3070_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3071{
3072 struct bfd_section *sec, *tls;
3073 unsigned int align = 0;
3074
3075 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3076 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3077 break;
3078 tls = sec;
3079
3080 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3081 if (sec->alignment_power > align)
3082 align = sec->alignment_power;
3083
3084 elf_hash_table (info)->tls_sec = tls;
3085
3086 /* Ensure the alignment of the first section is the largest alignment,
3087 so that the tls segment starts aligned. */
3088 if (tls != NULL)
3089 tls->alignment_power = align;
3090
3091 return tls;
3092}
0ad989f9
L
3093
3094/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3095static bfd_boolean
3096is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3097 Elf_Internal_Sym *sym)
3098{
a4d8e49b
L
3099 const struct elf_backend_data *bed;
3100
0ad989f9
L
3101 /* Local symbols do not count, but target specific ones might. */
3102 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3103 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3104 return FALSE;
3105
fcb93ecf 3106 bed = get_elf_backend_data (abfd);
0ad989f9 3107 /* Function symbols do not count. */
fcb93ecf 3108 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3109 return FALSE;
3110
3111 /* If the section is undefined, then so is the symbol. */
3112 if (sym->st_shndx == SHN_UNDEF)
3113 return FALSE;
3114
3115 /* If the symbol is defined in the common section, then
3116 it is a common definition and so does not count. */
a4d8e49b 3117 if (bed->common_definition (sym))
0ad989f9
L
3118 return FALSE;
3119
3120 /* If the symbol is in a target specific section then we
3121 must rely upon the backend to tell us what it is. */
3122 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3123 /* FIXME - this function is not coded yet:
3124
3125 return _bfd_is_global_symbol_definition (abfd, sym);
3126
3127 Instead for now assume that the definition is not global,
3128 Even if this is wrong, at least the linker will behave
3129 in the same way that it used to do. */
3130 return FALSE;
3131
3132 return TRUE;
3133}
3134
3135/* Search the symbol table of the archive element of the archive ABFD
3136 whose archive map contains a mention of SYMDEF, and determine if
3137 the symbol is defined in this element. */
3138static bfd_boolean
3139elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3140{
3141 Elf_Internal_Shdr * hdr;
ef53be89
AM
3142 size_t symcount;
3143 size_t extsymcount;
3144 size_t extsymoff;
0ad989f9
L
3145 Elf_Internal_Sym *isymbuf;
3146 Elf_Internal_Sym *isym;
3147 Elf_Internal_Sym *isymend;
3148 bfd_boolean result;
3149
3150 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3151 if (abfd == NULL)
3152 return FALSE;
3153
3154 if (! bfd_check_format (abfd, bfd_object))
3155 return FALSE;
3156
7dc3990e
L
3157 /* Select the appropriate symbol table. If we don't know if the
3158 object file is an IR object, give linker LTO plugin a chance to
3159 get the correct symbol table. */
3160 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3161#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3162 || (abfd->plugin_format == bfd_plugin_unknown
3163 && bfd_link_plugin_object_p (abfd))
3164#endif
3165 )
3166 {
3167 /* Use the IR symbol table if the object has been claimed by
3168 plugin. */
3169 abfd = abfd->plugin_dummy_bfd;
3170 hdr = &elf_tdata (abfd)->symtab_hdr;
3171 }
3172 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3173 hdr = &elf_tdata (abfd)->symtab_hdr;
3174 else
3175 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3176
3177 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3178
3179 /* The sh_info field of the symtab header tells us where the
3180 external symbols start. We don't care about the local symbols. */
3181 if (elf_bad_symtab (abfd))
3182 {
3183 extsymcount = symcount;
3184 extsymoff = 0;
3185 }
3186 else
3187 {
3188 extsymcount = symcount - hdr->sh_info;
3189 extsymoff = hdr->sh_info;
3190 }
3191
3192 if (extsymcount == 0)
3193 return FALSE;
3194
3195 /* Read in the symbol table. */
3196 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3197 NULL, NULL, NULL);
3198 if (isymbuf == NULL)
3199 return FALSE;
3200
3201 /* Scan the symbol table looking for SYMDEF. */
3202 result = FALSE;
3203 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3204 {
3205 const char *name;
3206
3207 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3208 isym->st_name);
3209 if (name == NULL)
3210 break;
3211
3212 if (strcmp (name, symdef->name) == 0)
3213 {
3214 result = is_global_data_symbol_definition (abfd, isym);
3215 break;
3216 }
3217 }
3218
3219 free (isymbuf);
3220
3221 return result;
3222}
3223\f
5a580b3a
AM
3224/* Add an entry to the .dynamic table. */
3225
3226bfd_boolean
3227_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3228 bfd_vma tag,
3229 bfd_vma val)
3230{
3231 struct elf_link_hash_table *hash_table;
3232 const struct elf_backend_data *bed;
3233 asection *s;
3234 bfd_size_type newsize;
3235 bfd_byte *newcontents;
3236 Elf_Internal_Dyn dyn;
3237
3238 hash_table = elf_hash_table (info);
3239 if (! is_elf_hash_table (hash_table))
3240 return FALSE;
3241
3242 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3243 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3244 BFD_ASSERT (s != NULL);
3245
eea6121a 3246 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3247 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3248 if (newcontents == NULL)
3249 return FALSE;
3250
3251 dyn.d_tag = tag;
3252 dyn.d_un.d_val = val;
eea6121a 3253 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3254
eea6121a 3255 s->size = newsize;
5a580b3a
AM
3256 s->contents = newcontents;
3257
3258 return TRUE;
3259}
3260
3261/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3262 otherwise just check whether one already exists. Returns -1 on error,
3263 1 if a DT_NEEDED tag already exists, and 0 on success. */
3264
4ad4eba5 3265static int
7e9f0867
AM
3266elf_add_dt_needed_tag (bfd *abfd,
3267 struct bfd_link_info *info,
4ad4eba5
AM
3268 const char *soname,
3269 bfd_boolean do_it)
5a580b3a
AM
3270{
3271 struct elf_link_hash_table *hash_table;
ef53be89 3272 size_t strindex;
5a580b3a 3273
7e9f0867
AM
3274 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3275 return -1;
3276
5a580b3a 3277 hash_table = elf_hash_table (info);
5a580b3a 3278 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3279 if (strindex == (size_t) -1)
5a580b3a
AM
3280 return -1;
3281
02be4619 3282 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3283 {
3284 asection *sdyn;
3285 const struct elf_backend_data *bed;
3286 bfd_byte *extdyn;
3287
3288 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3289 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3290 if (sdyn != NULL)
3291 for (extdyn = sdyn->contents;
3292 extdyn < sdyn->contents + sdyn->size;
3293 extdyn += bed->s->sizeof_dyn)
3294 {
3295 Elf_Internal_Dyn dyn;
5a580b3a 3296
7e9f0867
AM
3297 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3298 if (dyn.d_tag == DT_NEEDED
3299 && dyn.d_un.d_val == strindex)
3300 {
3301 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3302 return 1;
3303 }
3304 }
5a580b3a
AM
3305 }
3306
3307 if (do_it)
3308 {
7e9f0867
AM
3309 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3310 return -1;
3311
5a580b3a
AM
3312 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3313 return -1;
3314 }
3315 else
3316 /* We were just checking for existence of the tag. */
3317 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3318
3319 return 0;
3320}
3321
7b15fa7a
AM
3322/* Return true if SONAME is on the needed list between NEEDED and STOP
3323 (or the end of list if STOP is NULL), and needed by a library that
3324 will be loaded. */
3325
010e5ae2 3326static bfd_boolean
7b15fa7a
AM
3327on_needed_list (const char *soname,
3328 struct bfd_link_needed_list *needed,
3329 struct bfd_link_needed_list *stop)
010e5ae2 3330{
7b15fa7a
AM
3331 struct bfd_link_needed_list *look;
3332 for (look = needed; look != stop; look = look->next)
3333 if (strcmp (soname, look->name) == 0
3334 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3335 /* If needed by a library that itself is not directly
3336 needed, recursively check whether that library is
3337 indirectly needed. Since we add DT_NEEDED entries to
3338 the end of the list, library dependencies appear after
3339 the library. Therefore search prior to the current
3340 LOOK, preventing possible infinite recursion. */
3341 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3342 return TRUE;
3343
3344 return FALSE;
3345}
3346
14160578 3347/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3348static int
3349elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3350{
3351 const struct elf_link_hash_entry *h1;
3352 const struct elf_link_hash_entry *h2;
10b7e05b 3353 bfd_signed_vma vdiff;
5a580b3a
AM
3354
3355 h1 = *(const struct elf_link_hash_entry **) arg1;
3356 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3357 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3358 if (vdiff != 0)
3359 return vdiff > 0 ? 1 : -1;
3360 else
3361 {
d3435ae8 3362 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3363 if (sdiff != 0)
3364 return sdiff > 0 ? 1 : -1;
3365 }
14160578
AM
3366 vdiff = h1->size - h2->size;
3367 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3368}
4ad4eba5 3369
5a580b3a
AM
3370/* This function is used to adjust offsets into .dynstr for
3371 dynamic symbols. This is called via elf_link_hash_traverse. */
3372
3373static bfd_boolean
3374elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3375{
a50b1753 3376 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3377
5a580b3a
AM
3378 if (h->dynindx != -1)
3379 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3380 return TRUE;
3381}
3382
3383/* Assign string offsets in .dynstr, update all structures referencing
3384 them. */
3385
4ad4eba5
AM
3386static bfd_boolean
3387elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3388{
3389 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3390 struct elf_link_local_dynamic_entry *entry;
3391 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3392 bfd *dynobj = hash_table->dynobj;
3393 asection *sdyn;
3394 bfd_size_type size;
3395 const struct elf_backend_data *bed;
3396 bfd_byte *extdyn;
3397
3398 _bfd_elf_strtab_finalize (dynstr);
3399 size = _bfd_elf_strtab_size (dynstr);
3400
3401 bed = get_elf_backend_data (dynobj);
3d4d4302 3402 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3403 BFD_ASSERT (sdyn != NULL);
3404
3405 /* Update all .dynamic entries referencing .dynstr strings. */
3406 for (extdyn = sdyn->contents;
eea6121a 3407 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3408 extdyn += bed->s->sizeof_dyn)
3409 {
3410 Elf_Internal_Dyn dyn;
3411
3412 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3413 switch (dyn.d_tag)
3414 {
3415 case DT_STRSZ:
3416 dyn.d_un.d_val = size;
3417 break;
3418 case DT_NEEDED:
3419 case DT_SONAME:
3420 case DT_RPATH:
3421 case DT_RUNPATH:
3422 case DT_FILTER:
3423 case DT_AUXILIARY:
7ee314fa
AM
3424 case DT_AUDIT:
3425 case DT_DEPAUDIT:
5a580b3a
AM
3426 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3427 break;
3428 default:
3429 continue;
3430 }
3431 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3432 }
3433
3434 /* Now update local dynamic symbols. */
3435 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3436 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3437 entry->isym.st_name);
3438
3439 /* And the rest of dynamic symbols. */
3440 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3441
3442 /* Adjust version definitions. */
3443 if (elf_tdata (output_bfd)->cverdefs)
3444 {
3445 asection *s;
3446 bfd_byte *p;
ef53be89 3447 size_t i;
5a580b3a
AM
3448 Elf_Internal_Verdef def;
3449 Elf_Internal_Verdaux defaux;
3450
3d4d4302 3451 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3452 p = s->contents;
3453 do
3454 {
3455 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3456 &def);
3457 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3458 if (def.vd_aux != sizeof (Elf_External_Verdef))
3459 continue;
5a580b3a
AM
3460 for (i = 0; i < def.vd_cnt; ++i)
3461 {
3462 _bfd_elf_swap_verdaux_in (output_bfd,
3463 (Elf_External_Verdaux *) p, &defaux);
3464 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3465 defaux.vda_name);
3466 _bfd_elf_swap_verdaux_out (output_bfd,
3467 &defaux, (Elf_External_Verdaux *) p);
3468 p += sizeof (Elf_External_Verdaux);
3469 }
3470 }
3471 while (def.vd_next);
3472 }
3473
3474 /* Adjust version references. */
3475 if (elf_tdata (output_bfd)->verref)
3476 {
3477 asection *s;
3478 bfd_byte *p;
ef53be89 3479 size_t i;
5a580b3a
AM
3480 Elf_Internal_Verneed need;
3481 Elf_Internal_Vernaux needaux;
3482
3d4d4302 3483 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3484 p = s->contents;
3485 do
3486 {
3487 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3488 &need);
3489 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3490 _bfd_elf_swap_verneed_out (output_bfd, &need,
3491 (Elf_External_Verneed *) p);
3492 p += sizeof (Elf_External_Verneed);
3493 for (i = 0; i < need.vn_cnt; ++i)
3494 {
3495 _bfd_elf_swap_vernaux_in (output_bfd,
3496 (Elf_External_Vernaux *) p, &needaux);
3497 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3498 needaux.vna_name);
3499 _bfd_elf_swap_vernaux_out (output_bfd,
3500 &needaux,
3501 (Elf_External_Vernaux *) p);
3502 p += sizeof (Elf_External_Vernaux);
3503 }
3504 }
3505 while (need.vn_next);
3506 }
3507
3508 return TRUE;
3509}
3510\f
13285a1b
AM
3511/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3512 The default is to only match when the INPUT and OUTPUT are exactly
3513 the same target. */
3514
3515bfd_boolean
3516_bfd_elf_default_relocs_compatible (const bfd_target *input,
3517 const bfd_target *output)
3518{
3519 return input == output;
3520}
3521
3522/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3523 This version is used when different targets for the same architecture
3524 are virtually identical. */
3525
3526bfd_boolean
3527_bfd_elf_relocs_compatible (const bfd_target *input,
3528 const bfd_target *output)
3529{
3530 const struct elf_backend_data *obed, *ibed;
3531
3532 if (input == output)
3533 return TRUE;
3534
3535 ibed = xvec_get_elf_backend_data (input);
3536 obed = xvec_get_elf_backend_data (output);
3537
3538 if (ibed->arch != obed->arch)
3539 return FALSE;
3540
3541 /* If both backends are using this function, deem them compatible. */
3542 return ibed->relocs_compatible == obed->relocs_compatible;
3543}
3544
e5034e59
AM
3545/* Make a special call to the linker "notice" function to tell it that
3546 we are about to handle an as-needed lib, or have finished
1b786873 3547 processing the lib. */
e5034e59
AM
3548
3549bfd_boolean
3550_bfd_elf_notice_as_needed (bfd *ibfd,
3551 struct bfd_link_info *info,
3552 enum notice_asneeded_action act)
3553{
46135103 3554 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3555}
3556
d9689752
L
3557/* Check relocations an ELF object file. */
3558
3559bfd_boolean
3560_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3561{
3562 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3563 struct elf_link_hash_table *htab = elf_hash_table (info);
3564
3565 /* If this object is the same format as the output object, and it is
3566 not a shared library, then let the backend look through the
3567 relocs.
3568
3569 This is required to build global offset table entries and to
3570 arrange for dynamic relocs. It is not required for the
3571 particular common case of linking non PIC code, even when linking
3572 against shared libraries, but unfortunately there is no way of
3573 knowing whether an object file has been compiled PIC or not.
3574 Looking through the relocs is not particularly time consuming.
3575 The problem is that we must either (1) keep the relocs in memory,
3576 which causes the linker to require additional runtime memory or
3577 (2) read the relocs twice from the input file, which wastes time.
3578 This would be a good case for using mmap.
3579
3580 I have no idea how to handle linking PIC code into a file of a
3581 different format. It probably can't be done. */
3582 if ((abfd->flags & DYNAMIC) == 0
3583 && is_elf_hash_table (htab)
3584 && bed->check_relocs != NULL
3585 && elf_object_id (abfd) == elf_hash_table_id (htab)
3586 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3587 {
3588 asection *o;
3589
3590 for (o = abfd->sections; o != NULL; o = o->next)
3591 {
3592 Elf_Internal_Rela *internal_relocs;
3593 bfd_boolean ok;
3594
5ce03cea 3595 /* Don't check relocations in excluded sections. */
d9689752 3596 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3597 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3598 || o->reloc_count == 0
3599 || ((info->strip == strip_all || info->strip == strip_debugger)
3600 && (o->flags & SEC_DEBUGGING) != 0)
3601 || bfd_is_abs_section (o->output_section))
3602 continue;
3603
3604 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3605 info->keep_memory);
3606 if (internal_relocs == NULL)
3607 return FALSE;
3608
3609 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3610
3611 if (elf_section_data (o)->relocs != internal_relocs)
3612 free (internal_relocs);
3613
3614 if (! ok)
3615 return FALSE;
3616 }
3617 }
3618
3619 return TRUE;
3620}
3621
4ad4eba5
AM
3622/* Add symbols from an ELF object file to the linker hash table. */
3623
3624static bfd_boolean
3625elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3626{
a0c402a5 3627 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3628 Elf_Internal_Shdr *hdr;
ef53be89
AM
3629 size_t symcount;
3630 size_t extsymcount;
3631 size_t extsymoff;
4ad4eba5
AM
3632 struct elf_link_hash_entry **sym_hash;
3633 bfd_boolean dynamic;
3634 Elf_External_Versym *extversym = NULL;
3635 Elf_External_Versym *ever;
3636 struct elf_link_hash_entry *weaks;
3637 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3638 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3639 Elf_Internal_Sym *isymbuf = NULL;
3640 Elf_Internal_Sym *isym;
3641 Elf_Internal_Sym *isymend;
3642 const struct elf_backend_data *bed;
3643 bfd_boolean add_needed;
66eb6687 3644 struct elf_link_hash_table *htab;
4ad4eba5 3645 bfd_size_type amt;
66eb6687 3646 void *alloc_mark = NULL;
4f87808c
AM
3647 struct bfd_hash_entry **old_table = NULL;
3648 unsigned int old_size = 0;
3649 unsigned int old_count = 0;
66eb6687 3650 void *old_tab = NULL;
66eb6687
AM
3651 void *old_ent;
3652 struct bfd_link_hash_entry *old_undefs = NULL;
3653 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3654 void *old_strtab = NULL;
66eb6687 3655 size_t tabsize = 0;
db6a5d5f 3656 asection *s;
29a9f53e 3657 bfd_boolean just_syms;
4ad4eba5 3658
66eb6687 3659 htab = elf_hash_table (info);
4ad4eba5 3660 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3661
3662 if ((abfd->flags & DYNAMIC) == 0)
3663 dynamic = FALSE;
3664 else
3665 {
3666 dynamic = TRUE;
3667
3668 /* You can't use -r against a dynamic object. Also, there's no
3669 hope of using a dynamic object which does not exactly match
3670 the format of the output file. */
0e1862bb 3671 if (bfd_link_relocatable (info)
66eb6687 3672 || !is_elf_hash_table (htab)
f13a99db 3673 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3674 {
0e1862bb 3675 if (bfd_link_relocatable (info))
9a0789ec
NC
3676 bfd_set_error (bfd_error_invalid_operation);
3677 else
3678 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3679 goto error_return;
3680 }
3681 }
3682
a0c402a5
L
3683 ehdr = elf_elfheader (abfd);
3684 if (info->warn_alternate_em
3685 && bed->elf_machine_code != ehdr->e_machine
3686 && ((bed->elf_machine_alt1 != 0
3687 && ehdr->e_machine == bed->elf_machine_alt1)
3688 || (bed->elf_machine_alt2 != 0
3689 && ehdr->e_machine == bed->elf_machine_alt2)))
3690 info->callbacks->einfo
695344c0 3691 /* xgettext:c-format */
a0c402a5
L
3692 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3693 ehdr->e_machine, abfd, bed->elf_machine_code);
3694
4ad4eba5
AM
3695 /* As a GNU extension, any input sections which are named
3696 .gnu.warning.SYMBOL are treated as warning symbols for the given
3697 symbol. This differs from .gnu.warning sections, which generate
3698 warnings when they are included in an output file. */
dd98f8d2 3699 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3700 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3701 {
db6a5d5f 3702 const char *name;
4ad4eba5 3703
db6a5d5f
AM
3704 name = bfd_get_section_name (abfd, s);
3705 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3706 {
db6a5d5f
AM
3707 char *msg;
3708 bfd_size_type sz;
3709
3710 name += sizeof ".gnu.warning." - 1;
3711
3712 /* If this is a shared object, then look up the symbol
3713 in the hash table. If it is there, and it is already
3714 been defined, then we will not be using the entry
3715 from this shared object, so we don't need to warn.
3716 FIXME: If we see the definition in a regular object
3717 later on, we will warn, but we shouldn't. The only
3718 fix is to keep track of what warnings we are supposed
3719 to emit, and then handle them all at the end of the
3720 link. */
3721 if (dynamic)
4ad4eba5 3722 {
db6a5d5f
AM
3723 struct elf_link_hash_entry *h;
3724
3725 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3726
3727 /* FIXME: What about bfd_link_hash_common? */
3728 if (h != NULL
3729 && (h->root.type == bfd_link_hash_defined
3730 || h->root.type == bfd_link_hash_defweak))
3731 continue;
3732 }
4ad4eba5 3733
db6a5d5f
AM
3734 sz = s->size;
3735 msg = (char *) bfd_alloc (abfd, sz + 1);
3736 if (msg == NULL)
3737 goto error_return;
4ad4eba5 3738
db6a5d5f
AM
3739 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3740 goto error_return;
4ad4eba5 3741
db6a5d5f 3742 msg[sz] = '\0';
4ad4eba5 3743
db6a5d5f
AM
3744 if (! (_bfd_generic_link_add_one_symbol
3745 (info, abfd, name, BSF_WARNING, s, 0, msg,
3746 FALSE, bed->collect, NULL)))
3747 goto error_return;
4ad4eba5 3748
0e1862bb 3749 if (bfd_link_executable (info))
db6a5d5f
AM
3750 {
3751 /* Clobber the section size so that the warning does
3752 not get copied into the output file. */
3753 s->size = 0;
11d2f718 3754
db6a5d5f
AM
3755 /* Also set SEC_EXCLUDE, so that symbols defined in
3756 the warning section don't get copied to the output. */
3757 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3758 }
3759 }
3760 }
3761
29a9f53e
L
3762 just_syms = ((s = abfd->sections) != NULL
3763 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3764
4ad4eba5
AM
3765 add_needed = TRUE;
3766 if (! dynamic)
3767 {
3768 /* If we are creating a shared library, create all the dynamic
3769 sections immediately. We need to attach them to something,
3770 so we attach them to this BFD, provided it is the right
bf89386a
L
3771 format and is not from ld --just-symbols. Always create the
3772 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3773 are no input BFD's of the same format as the output, we can't
3774 make a shared library. */
3775 if (!just_syms
bf89386a 3776 && (bfd_link_pic (info)
9c1d7a08
L
3777 || (!bfd_link_relocatable (info)
3778 && (info->export_dynamic || info->dynamic)))
66eb6687 3779 && is_elf_hash_table (htab)
f13a99db 3780 && info->output_bfd->xvec == abfd->xvec
66eb6687 3781 && !htab->dynamic_sections_created)
4ad4eba5
AM
3782 {
3783 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3784 goto error_return;
3785 }
3786 }
66eb6687 3787 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3788 goto error_return;
3789 else
3790 {
4ad4eba5 3791 const char *soname = NULL;
7ee314fa 3792 char *audit = NULL;
4ad4eba5
AM
3793 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3794 int ret;
3795
3796 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3797 ld shouldn't allow it. */
29a9f53e 3798 if (just_syms)
92fd189d 3799 abort ();
4ad4eba5
AM
3800
3801 /* If this dynamic lib was specified on the command line with
3802 --as-needed in effect, then we don't want to add a DT_NEEDED
3803 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3804 in by another lib's DT_NEEDED. When --no-add-needed is used
3805 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3806 any dynamic library in DT_NEEDED tags in the dynamic lib at
3807 all. */
3808 add_needed = (elf_dyn_lib_class (abfd)
3809 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3810 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3811
3812 s = bfd_get_section_by_name (abfd, ".dynamic");
3813 if (s != NULL)
3814 {
3815 bfd_byte *dynbuf;
3816 bfd_byte *extdyn;
cb33740c 3817 unsigned int elfsec;
4ad4eba5
AM
3818 unsigned long shlink;
3819
eea6121a 3820 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3821 {
3822error_free_dyn:
3823 free (dynbuf);
3824 goto error_return;
3825 }
4ad4eba5
AM
3826
3827 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3828 if (elfsec == SHN_BAD)
4ad4eba5
AM
3829 goto error_free_dyn;
3830 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3831
3832 for (extdyn = dynbuf;
eea6121a 3833 extdyn < dynbuf + s->size;
4ad4eba5
AM
3834 extdyn += bed->s->sizeof_dyn)
3835 {
3836 Elf_Internal_Dyn dyn;
3837
3838 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3839 if (dyn.d_tag == DT_SONAME)
3840 {
3841 unsigned int tagv = dyn.d_un.d_val;
3842 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3843 if (soname == NULL)
3844 goto error_free_dyn;
3845 }
3846 if (dyn.d_tag == DT_NEEDED)
3847 {
3848 struct bfd_link_needed_list *n, **pn;
3849 char *fnm, *anm;
3850 unsigned int tagv = dyn.d_un.d_val;
3851
3852 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3853 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3854 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3855 if (n == NULL || fnm == NULL)
3856 goto error_free_dyn;
3857 amt = strlen (fnm) + 1;
a50b1753 3858 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3859 if (anm == NULL)
3860 goto error_free_dyn;
3861 memcpy (anm, fnm, amt);
3862 n->name = anm;
3863 n->by = abfd;
3864 n->next = NULL;
66eb6687 3865 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3866 ;
3867 *pn = n;
3868 }
3869 if (dyn.d_tag == DT_RUNPATH)
3870 {
3871 struct bfd_link_needed_list *n, **pn;
3872 char *fnm, *anm;
3873 unsigned int tagv = dyn.d_un.d_val;
3874
3875 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3876 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3877 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3878 if (n == NULL || fnm == NULL)
3879 goto error_free_dyn;
3880 amt = strlen (fnm) + 1;
a50b1753 3881 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3882 if (anm == NULL)
3883 goto error_free_dyn;
3884 memcpy (anm, fnm, amt);
3885 n->name = anm;
3886 n->by = abfd;
3887 n->next = NULL;
3888 for (pn = & runpath;
3889 *pn != NULL;
3890 pn = &(*pn)->next)
3891 ;
3892 *pn = n;
3893 }
3894 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3895 if (!runpath && dyn.d_tag == DT_RPATH)
3896 {
3897 struct bfd_link_needed_list *n, **pn;
3898 char *fnm, *anm;
3899 unsigned int tagv = dyn.d_un.d_val;
3900
3901 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3902 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3903 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3904 if (n == NULL || fnm == NULL)
3905 goto error_free_dyn;
3906 amt = strlen (fnm) + 1;
a50b1753 3907 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3908 if (anm == NULL)
f8703194 3909 goto error_free_dyn;
4ad4eba5
AM
3910 memcpy (anm, fnm, amt);
3911 n->name = anm;
3912 n->by = abfd;
3913 n->next = NULL;
3914 for (pn = & rpath;
3915 *pn != NULL;
3916 pn = &(*pn)->next)
3917 ;
3918 *pn = n;
3919 }
7ee314fa
AM
3920 if (dyn.d_tag == DT_AUDIT)
3921 {
3922 unsigned int tagv = dyn.d_un.d_val;
3923 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3924 }
4ad4eba5
AM
3925 }
3926
3927 free (dynbuf);
3928 }
3929
3930 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3931 frees all more recently bfd_alloc'd blocks as well. */
3932 if (runpath)
3933 rpath = runpath;
3934
3935 if (rpath)
3936 {
3937 struct bfd_link_needed_list **pn;
66eb6687 3938 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3939 ;
3940 *pn = rpath;
3941 }
3942
3943 /* We do not want to include any of the sections in a dynamic
3944 object in the output file. We hack by simply clobbering the
3945 list of sections in the BFD. This could be handled more
3946 cleanly by, say, a new section flag; the existing
3947 SEC_NEVER_LOAD flag is not the one we want, because that one
3948 still implies that the section takes up space in the output
3949 file. */
3950 bfd_section_list_clear (abfd);
3951
4ad4eba5
AM
3952 /* Find the name to use in a DT_NEEDED entry that refers to this
3953 object. If the object has a DT_SONAME entry, we use it.
3954 Otherwise, if the generic linker stuck something in
3955 elf_dt_name, we use that. Otherwise, we just use the file
3956 name. */
3957 if (soname == NULL || *soname == '\0')
3958 {
3959 soname = elf_dt_name (abfd);
3960 if (soname == NULL || *soname == '\0')
3961 soname = bfd_get_filename (abfd);
3962 }
3963
3964 /* Save the SONAME because sometimes the linker emulation code
3965 will need to know it. */
3966 elf_dt_name (abfd) = soname;
3967
7e9f0867 3968 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3969 if (ret < 0)
3970 goto error_return;
3971
3972 /* If we have already included this dynamic object in the
3973 link, just ignore it. There is no reason to include a
3974 particular dynamic object more than once. */
3975 if (ret > 0)
3976 return TRUE;
7ee314fa
AM
3977
3978 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 3979 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3980 }
3981
3982 /* If this is a dynamic object, we always link against the .dynsym
3983 symbol table, not the .symtab symbol table. The dynamic linker
3984 will only see the .dynsym symbol table, so there is no reason to
3985 look at .symtab for a dynamic object. */
3986
3987 if (! dynamic || elf_dynsymtab (abfd) == 0)
3988 hdr = &elf_tdata (abfd)->symtab_hdr;
3989 else
3990 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3991
3992 symcount = hdr->sh_size / bed->s->sizeof_sym;
3993
3994 /* The sh_info field of the symtab header tells us where the
3995 external symbols start. We don't care about the local symbols at
3996 this point. */
3997 if (elf_bad_symtab (abfd))
3998 {
3999 extsymcount = symcount;
4000 extsymoff = 0;
4001 }
4002 else
4003 {
4004 extsymcount = symcount - hdr->sh_info;
4005 extsymoff = hdr->sh_info;
4006 }
4007
f45794cb 4008 sym_hash = elf_sym_hashes (abfd);
012b2306 4009 if (extsymcount != 0)
4ad4eba5
AM
4010 {
4011 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4012 NULL, NULL, NULL);
4013 if (isymbuf == NULL)
4014 goto error_return;
4015
4ad4eba5 4016 if (sym_hash == NULL)
012b2306
AM
4017 {
4018 /* We store a pointer to the hash table entry for each
4019 external symbol. */
ef53be89
AM
4020 amt = extsymcount;
4021 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4022 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4023 if (sym_hash == NULL)
4024 goto error_free_sym;
4025 elf_sym_hashes (abfd) = sym_hash;
4026 }
4ad4eba5
AM
4027 }
4028
4029 if (dynamic)
4030 {
4031 /* Read in any version definitions. */
fc0e6df6
PB
4032 if (!_bfd_elf_slurp_version_tables (abfd,
4033 info->default_imported_symver))
4ad4eba5
AM
4034 goto error_free_sym;
4035
4036 /* Read in the symbol versions, but don't bother to convert them
4037 to internal format. */
4038 if (elf_dynversym (abfd) != 0)
4039 {
4040 Elf_Internal_Shdr *versymhdr;
4041
4042 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4043 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4044 if (extversym == NULL)
4045 goto error_free_sym;
4046 amt = versymhdr->sh_size;
4047 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4048 || bfd_bread (extversym, amt, abfd) != amt)
4049 goto error_free_vers;
4050 }
4051 }
4052
66eb6687
AM
4053 /* If we are loading an as-needed shared lib, save the symbol table
4054 state before we start adding symbols. If the lib turns out
4055 to be unneeded, restore the state. */
4056 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4057 {
4058 unsigned int i;
4059 size_t entsize;
4060
4061 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4062 {
4063 struct bfd_hash_entry *p;
2de92251 4064 struct elf_link_hash_entry *h;
66eb6687
AM
4065
4066 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4067 {
4068 h = (struct elf_link_hash_entry *) p;
4069 entsize += htab->root.table.entsize;
4070 if (h->root.type == bfd_link_hash_warning)
4071 entsize += htab->root.table.entsize;
4072 }
66eb6687
AM
4073 }
4074
4075 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4076 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4077 if (old_tab == NULL)
4078 goto error_free_vers;
4079
4080 /* Remember the current objalloc pointer, so that all mem for
4081 symbols added can later be reclaimed. */
4082 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4083 if (alloc_mark == NULL)
4084 goto error_free_vers;
4085
5061a885
AM
4086 /* Make a special call to the linker "notice" function to
4087 tell it that we are about to handle an as-needed lib. */
e5034e59 4088 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4089 goto error_free_vers;
5061a885 4090
f45794cb
AM
4091 /* Clone the symbol table. Remember some pointers into the
4092 symbol table, and dynamic symbol count. */
4093 old_ent = (char *) old_tab + tabsize;
66eb6687 4094 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4095 old_undefs = htab->root.undefs;
4096 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4097 old_table = htab->root.table.table;
4098 old_size = htab->root.table.size;
4099 old_count = htab->root.table.count;
5b677558
AM
4100 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4101 if (old_strtab == NULL)
4102 goto error_free_vers;
66eb6687
AM
4103
4104 for (i = 0; i < htab->root.table.size; i++)
4105 {
4106 struct bfd_hash_entry *p;
2de92251 4107 struct elf_link_hash_entry *h;
66eb6687
AM
4108
4109 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4110 {
4111 memcpy (old_ent, p, htab->root.table.entsize);
4112 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4113 h = (struct elf_link_hash_entry *) p;
4114 if (h->root.type == bfd_link_hash_warning)
4115 {
4116 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4117 old_ent = (char *) old_ent + htab->root.table.entsize;
4118 }
66eb6687
AM
4119 }
4120 }
4121 }
4ad4eba5 4122
66eb6687 4123 weaks = NULL;
4ad4eba5
AM
4124 ever = extversym != NULL ? extversym + extsymoff : NULL;
4125 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4126 isym < isymend;
4127 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4128 {
4129 int bind;
4130 bfd_vma value;
af44c138 4131 asection *sec, *new_sec;
4ad4eba5
AM
4132 flagword flags;
4133 const char *name;
4134 struct elf_link_hash_entry *h;
90c984fc 4135 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4136 bfd_boolean definition;
4137 bfd_boolean size_change_ok;
4138 bfd_boolean type_change_ok;
4139 bfd_boolean new_weakdef;
37a9e49a
L
4140 bfd_boolean new_weak;
4141 bfd_boolean old_weak;
4ad4eba5 4142 bfd_boolean override;
a4d8e49b 4143 bfd_boolean common;
97196564 4144 bfd_boolean discarded;
4ad4eba5
AM
4145 unsigned int old_alignment;
4146 bfd *old_bfd;
6e33951e 4147 bfd_boolean matched;
4ad4eba5
AM
4148
4149 override = FALSE;
4150
4151 flags = BSF_NO_FLAGS;
4152 sec = NULL;
4153 value = isym->st_value;
a4d8e49b 4154 common = bed->common_definition (isym);
97196564 4155 discarded = FALSE;
4ad4eba5
AM
4156
4157 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4158 switch (bind)
4ad4eba5 4159 {
3e7a7d11 4160 case STB_LOCAL:
4ad4eba5
AM
4161 /* This should be impossible, since ELF requires that all
4162 global symbols follow all local symbols, and that sh_info
4163 point to the first global symbol. Unfortunately, Irix 5
4164 screws this up. */
4165 continue;
3e7a7d11
NC
4166
4167 case STB_GLOBAL:
a4d8e49b 4168 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4169 flags = BSF_GLOBAL;
3e7a7d11
NC
4170 break;
4171
4172 case STB_WEAK:
4173 flags = BSF_WEAK;
4174 break;
4175
4176 case STB_GNU_UNIQUE:
4177 flags = BSF_GNU_UNIQUE;
4178 break;
4179
4180 default:
4ad4eba5 4181 /* Leave it up to the processor backend. */
3e7a7d11 4182 break;
4ad4eba5
AM
4183 }
4184
4185 if (isym->st_shndx == SHN_UNDEF)
4186 sec = bfd_und_section_ptr;
cb33740c
AM
4187 else if (isym->st_shndx == SHN_ABS)
4188 sec = bfd_abs_section_ptr;
4189 else if (isym->st_shndx == SHN_COMMON)
4190 {
4191 sec = bfd_com_section_ptr;
4192 /* What ELF calls the size we call the value. What ELF
4193 calls the value we call the alignment. */
4194 value = isym->st_size;
4195 }
4196 else
4ad4eba5
AM
4197 {
4198 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4199 if (sec == NULL)
4200 sec = bfd_abs_section_ptr;
dbaa2011 4201 else if (discarded_section (sec))
529fcb95 4202 {
e5d08002
L
4203 /* Symbols from discarded section are undefined. We keep
4204 its visibility. */
529fcb95 4205 sec = bfd_und_section_ptr;
97196564 4206 discarded = TRUE;
529fcb95
PB
4207 isym->st_shndx = SHN_UNDEF;
4208 }
4ad4eba5
AM
4209 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4210 value -= sec->vma;
4211 }
4ad4eba5
AM
4212
4213 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4214 isym->st_name);
4215 if (name == NULL)
4216 goto error_free_vers;
4217
4218 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4219 && (abfd->flags & BFD_PLUGIN) != 0)
4220 {
4221 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4222
4223 if (xc == NULL)
4224 {
4225 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4226 | SEC_EXCLUDE);
4227 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4228 if (xc == NULL)
4229 goto error_free_vers;
4230 }
4231 sec = xc;
4232 }
4233 else if (isym->st_shndx == SHN_COMMON
4234 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4235 && !bfd_link_relocatable (info))
4ad4eba5
AM
4236 {
4237 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4238
4239 if (tcomm == NULL)
4240 {
02d00247
AM
4241 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4242 | SEC_LINKER_CREATED);
4243 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4244 if (tcomm == NULL)
4ad4eba5
AM
4245 goto error_free_vers;
4246 }
4247 sec = tcomm;
4248 }
66eb6687 4249 else if (bed->elf_add_symbol_hook)
4ad4eba5 4250 {
66eb6687
AM
4251 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4252 &sec, &value))
4ad4eba5
AM
4253 goto error_free_vers;
4254
4255 /* The hook function sets the name to NULL if this symbol
4256 should be skipped for some reason. */
4257 if (name == NULL)
4258 continue;
4259 }
4260
4261 /* Sanity check that all possibilities were handled. */
4262 if (sec == NULL)
4263 {
4264 bfd_set_error (bfd_error_bad_value);
4265 goto error_free_vers;
4266 }
4267
191c0c42
AM
4268 /* Silently discard TLS symbols from --just-syms. There's
4269 no way to combine a static TLS block with a new TLS block
4270 for this executable. */
4271 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4272 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4273 continue;
4274
4ad4eba5
AM
4275 if (bfd_is_und_section (sec)
4276 || bfd_is_com_section (sec))
4277 definition = FALSE;
4278 else
4279 definition = TRUE;
4280
4281 size_change_ok = FALSE;
66eb6687 4282 type_change_ok = bed->type_change_ok;
37a9e49a 4283 old_weak = FALSE;
6e33951e 4284 matched = FALSE;
4ad4eba5
AM
4285 old_alignment = 0;
4286 old_bfd = NULL;
af44c138 4287 new_sec = sec;
4ad4eba5 4288
66eb6687 4289 if (is_elf_hash_table (htab))
4ad4eba5
AM
4290 {
4291 Elf_Internal_Versym iver;
4292 unsigned int vernum = 0;
4293 bfd_boolean skip;
4294
fc0e6df6 4295 if (ever == NULL)
4ad4eba5 4296 {
fc0e6df6
PB
4297 if (info->default_imported_symver)
4298 /* Use the default symbol version created earlier. */
4299 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4300 else
4301 iver.vs_vers = 0;
4302 }
4303 else
4304 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4305
4306 vernum = iver.vs_vers & VERSYM_VERSION;
4307
4308 /* If this is a hidden symbol, or if it is not version
4309 1, we append the version name to the symbol name.
cc86ff91
EB
4310 However, we do not modify a non-hidden absolute symbol
4311 if it is not a function, because it might be the version
4312 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4313 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4314 || (vernum > 1
4315 && (!bfd_is_abs_section (sec)
4316 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4317 {
4318 const char *verstr;
4319 size_t namelen, verlen, newlen;
4320 char *newname, *p;
4321
4322 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4323 {
fc0e6df6
PB
4324 if (vernum > elf_tdata (abfd)->cverdefs)
4325 verstr = NULL;
4326 else if (vernum > 1)
4327 verstr =
4328 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4329 else
4330 verstr = "";
4ad4eba5 4331
fc0e6df6 4332 if (verstr == NULL)
4ad4eba5 4333 {
4eca0228 4334 _bfd_error_handler
695344c0 4335 /* xgettext:c-format */
fc0e6df6
PB
4336 (_("%B: %s: invalid version %u (max %d)"),
4337 abfd, name, vernum,
4338 elf_tdata (abfd)->cverdefs);
4339 bfd_set_error (bfd_error_bad_value);
4340 goto error_free_vers;
4ad4eba5 4341 }
fc0e6df6
PB
4342 }
4343 else
4344 {
4345 /* We cannot simply test for the number of
4346 entries in the VERNEED section since the
4347 numbers for the needed versions do not start
4348 at 0. */
4349 Elf_Internal_Verneed *t;
4350
4351 verstr = NULL;
4352 for (t = elf_tdata (abfd)->verref;
4353 t != NULL;
4354 t = t->vn_nextref)
4ad4eba5 4355 {
fc0e6df6 4356 Elf_Internal_Vernaux *a;
4ad4eba5 4357
fc0e6df6
PB
4358 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4359 {
4360 if (a->vna_other == vernum)
4ad4eba5 4361 {
fc0e6df6
PB
4362 verstr = a->vna_nodename;
4363 break;
4ad4eba5 4364 }
4ad4eba5 4365 }
fc0e6df6
PB
4366 if (a != NULL)
4367 break;
4368 }
4369 if (verstr == NULL)
4370 {
4eca0228 4371 _bfd_error_handler
695344c0 4372 /* xgettext:c-format */
fc0e6df6
PB
4373 (_("%B: %s: invalid needed version %d"),
4374 abfd, name, vernum);
4375 bfd_set_error (bfd_error_bad_value);
4376 goto error_free_vers;
4ad4eba5 4377 }
4ad4eba5 4378 }
fc0e6df6
PB
4379
4380 namelen = strlen (name);
4381 verlen = strlen (verstr);
4382 newlen = namelen + verlen + 2;
4383 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4384 && isym->st_shndx != SHN_UNDEF)
4385 ++newlen;
4386
a50b1753 4387 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4388 if (newname == NULL)
4389 goto error_free_vers;
4390 memcpy (newname, name, namelen);
4391 p = newname + namelen;
4392 *p++ = ELF_VER_CHR;
4393 /* If this is a defined non-hidden version symbol,
4394 we add another @ to the name. This indicates the
4395 default version of the symbol. */
4396 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4397 && isym->st_shndx != SHN_UNDEF)
4398 *p++ = ELF_VER_CHR;
4399 memcpy (p, verstr, verlen + 1);
4400
4401 name = newname;
4ad4eba5
AM
4402 }
4403
cd3416da
AM
4404 /* If this symbol has default visibility and the user has
4405 requested we not re-export it, then mark it as hidden. */
a0d49154 4406 if (!bfd_is_und_section (sec)
cd3416da 4407 && !dynamic
ce875075 4408 && abfd->no_export
cd3416da
AM
4409 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4410 isym->st_other = (STV_HIDDEN
4411 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4412
4f3fedcf
AM
4413 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4414 sym_hash, &old_bfd, &old_weak,
4415 &old_alignment, &skip, &override,
6e33951e
L
4416 &type_change_ok, &size_change_ok,
4417 &matched))
4ad4eba5
AM
4418 goto error_free_vers;
4419
4420 if (skip)
4421 continue;
4422
6e33951e
L
4423 /* Override a definition only if the new symbol matches the
4424 existing one. */
4425 if (override && matched)
4ad4eba5
AM
4426 definition = FALSE;
4427
4428 h = *sym_hash;
4429 while (h->root.type == bfd_link_hash_indirect
4430 || h->root.type == bfd_link_hash_warning)
4431 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4432
4ad4eba5 4433 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4434 && vernum > 1
4435 && definition)
4436 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4437 }
4438
4439 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4440 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4441 (struct bfd_link_hash_entry **) sym_hash)))
4442 goto error_free_vers;
4443
a43942db
MR
4444 if ((flags & BSF_GNU_UNIQUE)
4445 && (abfd->flags & DYNAMIC) == 0
4446 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4447 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4448
4ad4eba5 4449 h = *sym_hash;
90c984fc
L
4450 /* We need to make sure that indirect symbol dynamic flags are
4451 updated. */
4452 hi = h;
4ad4eba5
AM
4453 while (h->root.type == bfd_link_hash_indirect
4454 || h->root.type == bfd_link_hash_warning)
4455 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4456
97196564
L
4457 /* Setting the index to -3 tells elf_link_output_extsym that
4458 this symbol is defined in a discarded section. */
4459 if (discarded)
4460 h->indx = -3;
4461
4ad4eba5
AM
4462 *sym_hash = h;
4463
37a9e49a 4464 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4465 new_weakdef = FALSE;
4466 if (dynamic
4467 && definition
37a9e49a 4468 && new_weak
fcb93ecf 4469 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4470 && is_elf_hash_table (htab)
f6e332e6 4471 && h->u.weakdef == NULL)
4ad4eba5
AM
4472 {
4473 /* Keep a list of all weak defined non function symbols from
4474 a dynamic object, using the weakdef field. Later in this
4475 function we will set the weakdef field to the correct
4476 value. We only put non-function symbols from dynamic
4477 objects on this list, because that happens to be the only
4478 time we need to know the normal symbol corresponding to a
4479 weak symbol, and the information is time consuming to
4480 figure out. If the weakdef field is not already NULL,
4481 then this symbol was already defined by some previous
4482 dynamic object, and we will be using that previous
4483 definition anyhow. */
4484
f6e332e6 4485 h->u.weakdef = weaks;
4ad4eba5
AM
4486 weaks = h;
4487 new_weakdef = TRUE;
4488 }
4489
4490 /* Set the alignment of a common symbol. */
a4d8e49b 4491 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4492 && h->root.type == bfd_link_hash_common)
4493 {
4494 unsigned int align;
4495
a4d8e49b 4496 if (common)
af44c138
L
4497 align = bfd_log2 (isym->st_value);
4498 else
4499 {
4500 /* The new symbol is a common symbol in a shared object.
4501 We need to get the alignment from the section. */
4502 align = new_sec->alignment_power;
4503 }
595213d4 4504 if (align > old_alignment)
4ad4eba5
AM
4505 h->root.u.c.p->alignment_power = align;
4506 else
4507 h->root.u.c.p->alignment_power = old_alignment;
4508 }
4509
66eb6687 4510 if (is_elf_hash_table (htab))
4ad4eba5 4511 {
4f3fedcf
AM
4512 /* Set a flag in the hash table entry indicating the type of
4513 reference or definition we just found. A dynamic symbol
4514 is one which is referenced or defined by both a regular
4515 object and a shared object. */
4516 bfd_boolean dynsym = FALSE;
4517
4518 /* Plugin symbols aren't normal. Don't set def_regular or
4519 ref_regular for them, or make them dynamic. */
4520 if ((abfd->flags & BFD_PLUGIN) != 0)
4521 ;
4522 else if (! dynamic)
4523 {
4524 if (! definition)
4525 {
4526 h->ref_regular = 1;
4527 if (bind != STB_WEAK)
4528 h->ref_regular_nonweak = 1;
4529 }
4530 else
4531 {
4532 h->def_regular = 1;
4533 if (h->def_dynamic)
4534 {
4535 h->def_dynamic = 0;
4536 h->ref_dynamic = 1;
4537 }
4538 }
4539
4540 /* If the indirect symbol has been forced local, don't
4541 make the real symbol dynamic. */
4542 if ((h == hi || !hi->forced_local)
0e1862bb 4543 && (bfd_link_dll (info)
4f3fedcf
AM
4544 || h->def_dynamic
4545 || h->ref_dynamic))
4546 dynsym = TRUE;
4547 }
4548 else
4549 {
4550 if (! definition)
4551 {
4552 h->ref_dynamic = 1;
4553 hi->ref_dynamic = 1;
4554 }
4555 else
4556 {
4557 h->def_dynamic = 1;
4558 hi->def_dynamic = 1;
4559 }
4560
4561 /* If the indirect symbol has been forced local, don't
4562 make the real symbol dynamic. */
4563 if ((h == hi || !hi->forced_local)
4564 && (h->def_regular
4565 || h->ref_regular
4566 || (h->u.weakdef != NULL
4567 && ! new_weakdef
4568 && h->u.weakdef->dynindx != -1)))
4569 dynsym = TRUE;
4570 }
4571
4572 /* Check to see if we need to add an indirect symbol for
4573 the default name. */
4574 if (definition
4575 || (!override && h->root.type == bfd_link_hash_common))
4576 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4577 sec, value, &old_bfd, &dynsym))
4578 goto error_free_vers;
4ad4eba5
AM
4579
4580 /* Check the alignment when a common symbol is involved. This
4581 can change when a common symbol is overridden by a normal
4582 definition or a common symbol is ignored due to the old
4583 normal definition. We need to make sure the maximum
4584 alignment is maintained. */
a4d8e49b 4585 if ((old_alignment || common)
4ad4eba5
AM
4586 && h->root.type != bfd_link_hash_common)
4587 {
4588 unsigned int common_align;
4589 unsigned int normal_align;
4590 unsigned int symbol_align;
4591 bfd *normal_bfd;
4592 bfd *common_bfd;
4593
3a81e825
AM
4594 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4595 || h->root.type == bfd_link_hash_defweak);
4596
4ad4eba5
AM
4597 symbol_align = ffs (h->root.u.def.value) - 1;
4598 if (h->root.u.def.section->owner != NULL
0616a280
AM
4599 && (h->root.u.def.section->owner->flags
4600 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4601 {
4602 normal_align = h->root.u.def.section->alignment_power;
4603 if (normal_align > symbol_align)
4604 normal_align = symbol_align;
4605 }
4606 else
4607 normal_align = symbol_align;
4608
4609 if (old_alignment)
4610 {
4611 common_align = old_alignment;
4612 common_bfd = old_bfd;
4613 normal_bfd = abfd;
4614 }
4615 else
4616 {
4617 common_align = bfd_log2 (isym->st_value);
4618 common_bfd = abfd;
4619 normal_bfd = old_bfd;
4620 }
4621
4622 if (normal_align < common_align)
d07676f8
NC
4623 {
4624 /* PR binutils/2735 */
4625 if (normal_bfd == NULL)
4eca0228 4626 _bfd_error_handler
695344c0 4627 /* xgettext:c-format */
4f3fedcf
AM
4628 (_("Warning: alignment %u of common symbol `%s' in %B is"
4629 " greater than the alignment (%u) of its section %A"),
d07676f8
NC
4630 common_bfd, h->root.u.def.section,
4631 1 << common_align, name, 1 << normal_align);
4632 else
4eca0228 4633 _bfd_error_handler
695344c0 4634 /* xgettext:c-format */
d07676f8
NC
4635 (_("Warning: alignment %u of symbol `%s' in %B"
4636 " is smaller than %u in %B"),
4637 normal_bfd, common_bfd,
4638 1 << normal_align, name, 1 << common_align);
4639 }
4ad4eba5
AM
4640 }
4641
83ad0046 4642 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4643 if (isym->st_size != 0
4644 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4645 && (definition || h->size == 0))
4646 {
83ad0046
L
4647 if (h->size != 0
4648 && h->size != isym->st_size
4649 && ! size_change_ok)
4eca0228 4650 _bfd_error_handler
695344c0 4651 /* xgettext:c-format */
d003868e
AM
4652 (_("Warning: size of symbol `%s' changed"
4653 " from %lu in %B to %lu in %B"),
4654 old_bfd, abfd,
4ad4eba5 4655 name, (unsigned long) h->size,
d003868e 4656 (unsigned long) isym->st_size);
4ad4eba5
AM
4657
4658 h->size = isym->st_size;
4659 }
4660
4661 /* If this is a common symbol, then we always want H->SIZE
4662 to be the size of the common symbol. The code just above
4663 won't fix the size if a common symbol becomes larger. We
4664 don't warn about a size change here, because that is
4f3fedcf 4665 covered by --warn-common. Allow changes between different
fcb93ecf 4666 function types. */
4ad4eba5
AM
4667 if (h->root.type == bfd_link_hash_common)
4668 h->size = h->root.u.c.size;
4669
4670 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4671 && ((definition && !new_weak)
4672 || (old_weak && h->root.type == bfd_link_hash_common)
4673 || h->type == STT_NOTYPE))
4ad4eba5 4674 {
2955ec4c
L
4675 unsigned int type = ELF_ST_TYPE (isym->st_info);
4676
4677 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4678 symbol. */
4679 if (type == STT_GNU_IFUNC
4680 && (abfd->flags & DYNAMIC) != 0)
4681 type = STT_FUNC;
4ad4eba5 4682
2955ec4c
L
4683 if (h->type != type)
4684 {
4685 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4686 /* xgettext:c-format */
4eca0228 4687 _bfd_error_handler
2955ec4c
L
4688 (_("Warning: type of symbol `%s' changed"
4689 " from %d to %d in %B"),
4690 abfd, name, h->type, type);
4691
4692 h->type = type;
4693 }
4ad4eba5
AM
4694 }
4695
54ac0771 4696 /* Merge st_other field. */
b8417128 4697 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4698
c3df8c14 4699 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4700 if (definition
4701 && (sec->flags & SEC_DEBUGGING)
4702 && !bfd_link_relocatable (info))
c3df8c14
AM
4703 dynsym = FALSE;
4704
4f3fedcf
AM
4705 /* Nor should we make plugin symbols dynamic. */
4706 if ((abfd->flags & BFD_PLUGIN) != 0)
4707 dynsym = FALSE;
4708
35fc36a8 4709 if (definition)
35399224
L
4710 {
4711 h->target_internal = isym->st_target_internal;
4712 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4713 }
35fc36a8 4714
4ad4eba5
AM
4715 if (definition && !dynamic)
4716 {
4717 char *p = strchr (name, ELF_VER_CHR);
4718 if (p != NULL && p[1] != ELF_VER_CHR)
4719 {
4720 /* Queue non-default versions so that .symver x, x@FOO
4721 aliases can be checked. */
66eb6687 4722 if (!nondeflt_vers)
4ad4eba5 4723 {
66eb6687
AM
4724 amt = ((isymend - isym + 1)
4725 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4726 nondeflt_vers
4727 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4728 if (!nondeflt_vers)
4729 goto error_free_vers;
4ad4eba5 4730 }
66eb6687 4731 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4732 }
4733 }
4734
4735 if (dynsym && h->dynindx == -1)
4736 {
c152c796 4737 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4738 goto error_free_vers;
f6e332e6 4739 if (h->u.weakdef != NULL
4ad4eba5 4740 && ! new_weakdef
f6e332e6 4741 && h->u.weakdef->dynindx == -1)
4ad4eba5 4742 {
66eb6687 4743 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4744 goto error_free_vers;
4745 }
4746 }
1f599d0e 4747 else if (h->dynindx != -1)
4ad4eba5
AM
4748 /* If the symbol already has a dynamic index, but
4749 visibility says it should not be visible, turn it into
4750 a local symbol. */
4751 switch (ELF_ST_VISIBILITY (h->other))
4752 {
4753 case STV_INTERNAL:
4754 case STV_HIDDEN:
4755 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4756 dynsym = FALSE;
4757 break;
4758 }
4759
aef28989
L
4760 /* Don't add DT_NEEDED for references from the dummy bfd nor
4761 for unmatched symbol. */
4ad4eba5 4762 if (!add_needed
aef28989 4763 && matched
4ad4eba5 4764 && definition
010e5ae2 4765 && ((dynsym
ffa9430d 4766 && h->ref_regular_nonweak
4f3fedcf
AM
4767 && (old_bfd == NULL
4768 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4769 || (h->ref_dynamic_nonweak
010e5ae2 4770 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4771 && !on_needed_list (elf_dt_name (abfd),
4772 htab->needed, NULL))))
4ad4eba5
AM
4773 {
4774 int ret;
4775 const char *soname = elf_dt_name (abfd);
4776
16e4ecc0
AM
4777 info->callbacks->minfo ("%!", soname, old_bfd,
4778 h->root.root.string);
4779
4ad4eba5
AM
4780 /* A symbol from a library loaded via DT_NEEDED of some
4781 other library is referenced by a regular object.
e56f61be 4782 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4783 --no-add-needed is used and the reference was not
4784 a weak one. */
4f3fedcf 4785 if (old_bfd != NULL
b918acf9 4786 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 4787 {
4eca0228 4788 _bfd_error_handler
695344c0 4789 /* xgettext:c-format */
3cbc5de0 4790 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4791 old_bfd, name);
ff5ac77b 4792 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4793 goto error_free_vers;
4794 }
4795
a50b1753 4796 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4797 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4798
4ad4eba5 4799 add_needed = TRUE;
7e9f0867 4800 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4801 if (ret < 0)
4802 goto error_free_vers;
4803
4804 BFD_ASSERT (ret == 0);
4805 }
4806 }
4807 }
4808
66eb6687
AM
4809 if (extversym != NULL)
4810 {
4811 free (extversym);
4812 extversym = NULL;
4813 }
4814
4815 if (isymbuf != NULL)
4816 {
4817 free (isymbuf);
4818 isymbuf = NULL;
4819 }
4820
4821 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4822 {
4823 unsigned int i;
4824
4825 /* Restore the symbol table. */
f45794cb
AM
4826 old_ent = (char *) old_tab + tabsize;
4827 memset (elf_sym_hashes (abfd), 0,
4828 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4829 htab->root.table.table = old_table;
4830 htab->root.table.size = old_size;
4831 htab->root.table.count = old_count;
66eb6687 4832 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4833 htab->root.undefs = old_undefs;
4834 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
4835 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4836 free (old_strtab);
4837 old_strtab = NULL;
66eb6687
AM
4838 for (i = 0; i < htab->root.table.size; i++)
4839 {
4840 struct bfd_hash_entry *p;
4841 struct elf_link_hash_entry *h;
3e0882af
L
4842 bfd_size_type size;
4843 unsigned int alignment_power;
66eb6687
AM
4844
4845 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4846 {
4847 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4848 if (h->root.type == bfd_link_hash_warning)
4849 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4850
3e0882af
L
4851 /* Preserve the maximum alignment and size for common
4852 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4853 since it can still be loaded at run time by another
3e0882af
L
4854 dynamic lib. */
4855 if (h->root.type == bfd_link_hash_common)
4856 {
4857 size = h->root.u.c.size;
4858 alignment_power = h->root.u.c.p->alignment_power;
4859 }
4860 else
4861 {
4862 size = 0;
4863 alignment_power = 0;
4864 }
66eb6687
AM
4865 memcpy (p, old_ent, htab->root.table.entsize);
4866 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4867 h = (struct elf_link_hash_entry *) p;
4868 if (h->root.type == bfd_link_hash_warning)
4869 {
4870 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4871 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4872 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4873 }
a4542f1b 4874 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4875 {
4876 if (size > h->root.u.c.size)
4877 h->root.u.c.size = size;
4878 if (alignment_power > h->root.u.c.p->alignment_power)
4879 h->root.u.c.p->alignment_power = alignment_power;
4880 }
66eb6687
AM
4881 }
4882 }
4883
5061a885
AM
4884 /* Make a special call to the linker "notice" function to
4885 tell it that symbols added for crefs may need to be removed. */
e5034e59 4886 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4887 goto error_free_vers;
5061a885 4888
66eb6687
AM
4889 free (old_tab);
4890 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4891 alloc_mark);
4892 if (nondeflt_vers != NULL)
4893 free (nondeflt_vers);
4894 return TRUE;
4895 }
2de92251 4896
66eb6687
AM
4897 if (old_tab != NULL)
4898 {
e5034e59 4899 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4900 goto error_free_vers;
66eb6687
AM
4901 free (old_tab);
4902 old_tab = NULL;
4903 }
4904
c6e8a9a8
L
4905 /* Now that all the symbols from this input file are created, if
4906 not performing a relocatable link, handle .symver foo, foo@BAR
4907 such that any relocs against foo become foo@BAR. */
0e1862bb 4908 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 4909 {
ef53be89 4910 size_t cnt, symidx;
4ad4eba5
AM
4911
4912 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4913 {
4914 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4915 char *shortname, *p;
4916
4917 p = strchr (h->root.root.string, ELF_VER_CHR);
4918 if (p == NULL
4919 || (h->root.type != bfd_link_hash_defined
4920 && h->root.type != bfd_link_hash_defweak))
4921 continue;
4922
4923 amt = p - h->root.root.string;
a50b1753 4924 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4925 if (!shortname)
4926 goto error_free_vers;
4ad4eba5
AM
4927 memcpy (shortname, h->root.root.string, amt);
4928 shortname[amt] = '\0';
4929
4930 hi = (struct elf_link_hash_entry *)
66eb6687 4931 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4932 FALSE, FALSE, FALSE);
4933 if (hi != NULL
4934 && hi->root.type == h->root.type
4935 && hi->root.u.def.value == h->root.u.def.value
4936 && hi->root.u.def.section == h->root.u.def.section)
4937 {
4938 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4939 hi->root.type = bfd_link_hash_indirect;
4940 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4941 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4942 sym_hash = elf_sym_hashes (abfd);
4943 if (sym_hash)
4944 for (symidx = 0; symidx < extsymcount; ++symidx)
4945 if (sym_hash[symidx] == hi)
4946 {
4947 sym_hash[symidx] = h;
4948 break;
4949 }
4950 }
4951 free (shortname);
4952 }
4953 free (nondeflt_vers);
4954 nondeflt_vers = NULL;
4955 }
4956
4ad4eba5
AM
4957 /* Now set the weakdefs field correctly for all the weak defined
4958 symbols we found. The only way to do this is to search all the
4959 symbols. Since we only need the information for non functions in
4960 dynamic objects, that's the only time we actually put anything on
4961 the list WEAKS. We need this information so that if a regular
4962 object refers to a symbol defined weakly in a dynamic object, the
4963 real symbol in the dynamic object is also put in the dynamic
4964 symbols; we also must arrange for both symbols to point to the
4965 same memory location. We could handle the general case of symbol
4966 aliasing, but a general symbol alias can only be generated in
4967 assembler code, handling it correctly would be very time
4968 consuming, and other ELF linkers don't handle general aliasing
4969 either. */
4970 if (weaks != NULL)
4971 {
4972 struct elf_link_hash_entry **hpp;
4973 struct elf_link_hash_entry **hppend;
4974 struct elf_link_hash_entry **sorted_sym_hash;
4975 struct elf_link_hash_entry *h;
4976 size_t sym_count;
4977
4978 /* Since we have to search the whole symbol list for each weak
4979 defined symbol, search time for N weak defined symbols will be
4980 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
4981 amt = extsymcount;
4982 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 4983 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4984 if (sorted_sym_hash == NULL)
4985 goto error_return;
4986 sym_hash = sorted_sym_hash;
4987 hpp = elf_sym_hashes (abfd);
4988 hppend = hpp + extsymcount;
4989 sym_count = 0;
4990 for (; hpp < hppend; hpp++)
4991 {
4992 h = *hpp;
4993 if (h != NULL
4994 && h->root.type == bfd_link_hash_defined
fcb93ecf 4995 && !bed->is_function_type (h->type))
4ad4eba5
AM
4996 {
4997 *sym_hash = h;
4998 sym_hash++;
4999 sym_count++;
5000 }
5001 }
5002
5003 qsort (sorted_sym_hash, sym_count,
5004 sizeof (struct elf_link_hash_entry *),
5005 elf_sort_symbol);
5006
5007 while (weaks != NULL)
5008 {
5009 struct elf_link_hash_entry *hlook;
5010 asection *slook;
5011 bfd_vma vlook;
ed54588d 5012 size_t i, j, idx = 0;
4ad4eba5
AM
5013
5014 hlook = weaks;
f6e332e6
AM
5015 weaks = hlook->u.weakdef;
5016 hlook->u.weakdef = NULL;
4ad4eba5
AM
5017
5018 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5019 || hlook->root.type == bfd_link_hash_defweak
5020 || hlook->root.type == bfd_link_hash_common
5021 || hlook->root.type == bfd_link_hash_indirect);
5022 slook = hlook->root.u.def.section;
5023 vlook = hlook->root.u.def.value;
5024
4ad4eba5
AM
5025 i = 0;
5026 j = sym_count;
14160578 5027 while (i != j)
4ad4eba5
AM
5028 {
5029 bfd_signed_vma vdiff;
5030 idx = (i + j) / 2;
14160578 5031 h = sorted_sym_hash[idx];
4ad4eba5
AM
5032 vdiff = vlook - h->root.u.def.value;
5033 if (vdiff < 0)
5034 j = idx;
5035 else if (vdiff > 0)
5036 i = idx + 1;
5037 else
5038 {
d3435ae8 5039 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5040 if (sdiff < 0)
5041 j = idx;
5042 else if (sdiff > 0)
5043 i = idx + 1;
5044 else
14160578 5045 break;
4ad4eba5
AM
5046 }
5047 }
5048
5049 /* We didn't find a value/section match. */
14160578 5050 if (i == j)
4ad4eba5
AM
5051 continue;
5052
14160578
AM
5053 /* With multiple aliases, or when the weak symbol is already
5054 strongly defined, we have multiple matching symbols and
5055 the binary search above may land on any of them. Step
5056 one past the matching symbol(s). */
5057 while (++idx != j)
5058 {
5059 h = sorted_sym_hash[idx];
5060 if (h->root.u.def.section != slook
5061 || h->root.u.def.value != vlook)
5062 break;
5063 }
5064
5065 /* Now look back over the aliases. Since we sorted by size
5066 as well as value and section, we'll choose the one with
5067 the largest size. */
5068 while (idx-- != i)
4ad4eba5 5069 {
14160578 5070 h = sorted_sym_hash[idx];
4ad4eba5
AM
5071
5072 /* Stop if value or section doesn't match. */
14160578
AM
5073 if (h->root.u.def.section != slook
5074 || h->root.u.def.value != vlook)
4ad4eba5
AM
5075 break;
5076 else if (h != hlook)
5077 {
f6e332e6 5078 hlook->u.weakdef = h;
4ad4eba5
AM
5079
5080 /* If the weak definition is in the list of dynamic
5081 symbols, make sure the real definition is put
5082 there as well. */
5083 if (hlook->dynindx != -1 && h->dynindx == -1)
5084 {
c152c796 5085 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5086 {
5087 err_free_sym_hash:
5088 free (sorted_sym_hash);
5089 goto error_return;
5090 }
4ad4eba5
AM
5091 }
5092
5093 /* If the real definition is in the list of dynamic
5094 symbols, make sure the weak definition is put
5095 there as well. If we don't do this, then the
5096 dynamic loader might not merge the entries for the
5097 real definition and the weak definition. */
5098 if (h->dynindx != -1 && hlook->dynindx == -1)
5099 {
c152c796 5100 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5101 goto err_free_sym_hash;
4ad4eba5
AM
5102 }
5103 break;
5104 }
5105 }
5106 }
5107
5108 free (sorted_sym_hash);
5109 }
5110
33177bb1
AM
5111 if (bed->check_directives
5112 && !(*bed->check_directives) (abfd, info))
5113 return FALSE;
85fbca6a 5114
d9689752
L
5115 if (!info->check_relocs_after_open_input
5116 && !_bfd_elf_link_check_relocs (abfd, info))
5117 return FALSE;
4ad4eba5
AM
5118
5119 /* If this is a non-traditional link, try to optimize the handling
5120 of the .stab/.stabstr sections. */
5121 if (! dynamic
5122 && ! info->traditional_format
66eb6687 5123 && is_elf_hash_table (htab)
4ad4eba5
AM
5124 && (info->strip != strip_all && info->strip != strip_debugger))
5125 {
5126 asection *stabstr;
5127
5128 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5129 if (stabstr != NULL)
5130 {
5131 bfd_size_type string_offset = 0;
5132 asection *stab;
5133
5134 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5135 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5136 && (!stab->name[5] ||
5137 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5138 && (stab->flags & SEC_MERGE) == 0
5139 && !bfd_is_abs_section (stab->output_section))
5140 {
5141 struct bfd_elf_section_data *secdata;
5142
5143 secdata = elf_section_data (stab);
66eb6687
AM
5144 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5145 stabstr, &secdata->sec_info,
4ad4eba5
AM
5146 &string_offset))
5147 goto error_return;
5148 if (secdata->sec_info)
dbaa2011 5149 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5150 }
5151 }
5152 }
5153
66eb6687 5154 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5155 {
5156 /* Add this bfd to the loaded list. */
5157 struct elf_link_loaded_list *n;
5158
ca4be51c 5159 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5160 if (n == NULL)
5161 goto error_return;
5162 n->abfd = abfd;
66eb6687
AM
5163 n->next = htab->loaded;
5164 htab->loaded = n;
4ad4eba5
AM
5165 }
5166
5167 return TRUE;
5168
5169 error_free_vers:
66eb6687
AM
5170 if (old_tab != NULL)
5171 free (old_tab);
5b677558
AM
5172 if (old_strtab != NULL)
5173 free (old_strtab);
4ad4eba5
AM
5174 if (nondeflt_vers != NULL)
5175 free (nondeflt_vers);
5176 if (extversym != NULL)
5177 free (extversym);
5178 error_free_sym:
5179 if (isymbuf != NULL)
5180 free (isymbuf);
5181 error_return:
5182 return FALSE;
5183}
5184
8387904d
AM
5185/* Return the linker hash table entry of a symbol that might be
5186 satisfied by an archive symbol. Return -1 on error. */
5187
5188struct elf_link_hash_entry *
5189_bfd_elf_archive_symbol_lookup (bfd *abfd,
5190 struct bfd_link_info *info,
5191 const char *name)
5192{
5193 struct elf_link_hash_entry *h;
5194 char *p, *copy;
5195 size_t len, first;
5196
2a41f396 5197 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5198 if (h != NULL)
5199 return h;
5200
5201 /* If this is a default version (the name contains @@), look up the
5202 symbol again with only one `@' as well as without the version.
5203 The effect is that references to the symbol with and without the
5204 version will be matched by the default symbol in the archive. */
5205
5206 p = strchr (name, ELF_VER_CHR);
5207 if (p == NULL || p[1] != ELF_VER_CHR)
5208 return h;
5209
5210 /* First check with only one `@'. */
5211 len = strlen (name);
a50b1753 5212 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5213 if (copy == NULL)
5214 return (struct elf_link_hash_entry *) 0 - 1;
5215
5216 first = p - name + 1;
5217 memcpy (copy, name, first);
5218 memcpy (copy + first, name + first + 1, len - first);
5219
2a41f396 5220 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5221 if (h == NULL)
5222 {
5223 /* We also need to check references to the symbol without the
5224 version. */
5225 copy[first - 1] = '\0';
5226 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5227 FALSE, FALSE, TRUE);
8387904d
AM
5228 }
5229
5230 bfd_release (abfd, copy);
5231 return h;
5232}
5233
0ad989f9 5234/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5235 don't use _bfd_generic_link_add_archive_symbols because we need to
5236 handle versioned symbols.
0ad989f9
L
5237
5238 Fortunately, ELF archive handling is simpler than that done by
5239 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5240 oddities. In ELF, if we find a symbol in the archive map, and the
5241 symbol is currently undefined, we know that we must pull in that
5242 object file.
5243
5244 Unfortunately, we do have to make multiple passes over the symbol
5245 table until nothing further is resolved. */
5246
4ad4eba5
AM
5247static bfd_boolean
5248elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5249{
5250 symindex c;
13e570f8 5251 unsigned char *included = NULL;
0ad989f9
L
5252 carsym *symdefs;
5253 bfd_boolean loop;
5254 bfd_size_type amt;
8387904d
AM
5255 const struct elf_backend_data *bed;
5256 struct elf_link_hash_entry * (*archive_symbol_lookup)
5257 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5258
5259 if (! bfd_has_map (abfd))
5260 {
5261 /* An empty archive is a special case. */
5262 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5263 return TRUE;
5264 bfd_set_error (bfd_error_no_armap);
5265 return FALSE;
5266 }
5267
5268 /* Keep track of all symbols we know to be already defined, and all
5269 files we know to be already included. This is to speed up the
5270 second and subsequent passes. */
5271 c = bfd_ardata (abfd)->symdef_count;
5272 if (c == 0)
5273 return TRUE;
5274 amt = c;
13e570f8
AM
5275 amt *= sizeof (*included);
5276 included = (unsigned char *) bfd_zmalloc (amt);
5277 if (included == NULL)
5278 return FALSE;
0ad989f9
L
5279
5280 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5281 bed = get_elf_backend_data (abfd);
5282 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5283
5284 do
5285 {
5286 file_ptr last;
5287 symindex i;
5288 carsym *symdef;
5289 carsym *symdefend;
5290
5291 loop = FALSE;
5292 last = -1;
5293
5294 symdef = symdefs;
5295 symdefend = symdef + c;
5296 for (i = 0; symdef < symdefend; symdef++, i++)
5297 {
5298 struct elf_link_hash_entry *h;
5299 bfd *element;
5300 struct bfd_link_hash_entry *undefs_tail;
5301 symindex mark;
5302
13e570f8 5303 if (included[i])
0ad989f9
L
5304 continue;
5305 if (symdef->file_offset == last)
5306 {
5307 included[i] = TRUE;
5308 continue;
5309 }
5310
8387904d
AM
5311 h = archive_symbol_lookup (abfd, info, symdef->name);
5312 if (h == (struct elf_link_hash_entry *) 0 - 1)
5313 goto error_return;
0ad989f9
L
5314
5315 if (h == NULL)
5316 continue;
5317
5318 if (h->root.type == bfd_link_hash_common)
5319 {
5320 /* We currently have a common symbol. The archive map contains
5321 a reference to this symbol, so we may want to include it. We
5322 only want to include it however, if this archive element
5323 contains a definition of the symbol, not just another common
5324 declaration of it.
5325
5326 Unfortunately some archivers (including GNU ar) will put
5327 declarations of common symbols into their archive maps, as
5328 well as real definitions, so we cannot just go by the archive
5329 map alone. Instead we must read in the element's symbol
5330 table and check that to see what kind of symbol definition
5331 this is. */
5332 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5333 continue;
5334 }
5335 else if (h->root.type != bfd_link_hash_undefined)
5336 {
5337 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5338 /* Symbol must be defined. Don't check it again. */
5339 included[i] = TRUE;
0ad989f9
L
5340 continue;
5341 }
5342
5343 /* We need to include this archive member. */
5344 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5345 if (element == NULL)
5346 goto error_return;
5347
5348 if (! bfd_check_format (element, bfd_object))
5349 goto error_return;
5350
0ad989f9
L
5351 undefs_tail = info->hash->undefs_tail;
5352
0e144ba7
AM
5353 if (!(*info->callbacks
5354 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5355 continue;
0e144ba7 5356 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5357 goto error_return;
5358
5359 /* If there are any new undefined symbols, we need to make
5360 another pass through the archive in order to see whether
5361 they can be defined. FIXME: This isn't perfect, because
5362 common symbols wind up on undefs_tail and because an
5363 undefined symbol which is defined later on in this pass
5364 does not require another pass. This isn't a bug, but it
5365 does make the code less efficient than it could be. */
5366 if (undefs_tail != info->hash->undefs_tail)
5367 loop = TRUE;
5368
5369 /* Look backward to mark all symbols from this object file
5370 which we have already seen in this pass. */
5371 mark = i;
5372 do
5373 {
5374 included[mark] = TRUE;
5375 if (mark == 0)
5376 break;
5377 --mark;
5378 }
5379 while (symdefs[mark].file_offset == symdef->file_offset);
5380
5381 /* We mark subsequent symbols from this object file as we go
5382 on through the loop. */
5383 last = symdef->file_offset;
5384 }
5385 }
5386 while (loop);
5387
0ad989f9
L
5388 free (included);
5389
5390 return TRUE;
5391
5392 error_return:
0ad989f9
L
5393 if (included != NULL)
5394 free (included);
5395 return FALSE;
5396}
4ad4eba5
AM
5397
5398/* Given an ELF BFD, add symbols to the global hash table as
5399 appropriate. */
5400
5401bfd_boolean
5402bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5403{
5404 switch (bfd_get_format (abfd))
5405 {
5406 case bfd_object:
5407 return elf_link_add_object_symbols (abfd, info);
5408 case bfd_archive:
5409 return elf_link_add_archive_symbols (abfd, info);
5410 default:
5411 bfd_set_error (bfd_error_wrong_format);
5412 return FALSE;
5413 }
5414}
5a580b3a 5415\f
14b1c01e
AM
5416struct hash_codes_info
5417{
5418 unsigned long *hashcodes;
5419 bfd_boolean error;
5420};
a0c8462f 5421
5a580b3a
AM
5422/* This function will be called though elf_link_hash_traverse to store
5423 all hash value of the exported symbols in an array. */
5424
5425static bfd_boolean
5426elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5427{
a50b1753 5428 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5429 const char *name;
5a580b3a
AM
5430 unsigned long ha;
5431 char *alc = NULL;
5432
5a580b3a
AM
5433 /* Ignore indirect symbols. These are added by the versioning code. */
5434 if (h->dynindx == -1)
5435 return TRUE;
5436
5437 name = h->root.root.string;
422f1182 5438 if (h->versioned >= versioned)
5a580b3a 5439 {
422f1182
L
5440 char *p = strchr (name, ELF_VER_CHR);
5441 if (p != NULL)
14b1c01e 5442 {
422f1182
L
5443 alc = (char *) bfd_malloc (p - name + 1);
5444 if (alc == NULL)
5445 {
5446 inf->error = TRUE;
5447 return FALSE;
5448 }
5449 memcpy (alc, name, p - name);
5450 alc[p - name] = '\0';
5451 name = alc;
14b1c01e 5452 }
5a580b3a
AM
5453 }
5454
5455 /* Compute the hash value. */
5456 ha = bfd_elf_hash (name);
5457
5458 /* Store the found hash value in the array given as the argument. */
14b1c01e 5459 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5460
5461 /* And store it in the struct so that we can put it in the hash table
5462 later. */
f6e332e6 5463 h->u.elf_hash_value = ha;
5a580b3a
AM
5464
5465 if (alc != NULL)
5466 free (alc);
5467
5468 return TRUE;
5469}
5470
fdc90cb4
JJ
5471struct collect_gnu_hash_codes
5472{
5473 bfd *output_bfd;
5474 const struct elf_backend_data *bed;
5475 unsigned long int nsyms;
5476 unsigned long int maskbits;
5477 unsigned long int *hashcodes;
5478 unsigned long int *hashval;
5479 unsigned long int *indx;
5480 unsigned long int *counts;
5481 bfd_vma *bitmask;
5482 bfd_byte *contents;
5483 long int min_dynindx;
5484 unsigned long int bucketcount;
5485 unsigned long int symindx;
5486 long int local_indx;
5487 long int shift1, shift2;
5488 unsigned long int mask;
14b1c01e 5489 bfd_boolean error;
fdc90cb4
JJ
5490};
5491
5492/* This function will be called though elf_link_hash_traverse to store
5493 all hash value of the exported symbols in an array. */
5494
5495static bfd_boolean
5496elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5497{
a50b1753 5498 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5499 const char *name;
fdc90cb4
JJ
5500 unsigned long ha;
5501 char *alc = NULL;
5502
fdc90cb4
JJ
5503 /* Ignore indirect symbols. These are added by the versioning code. */
5504 if (h->dynindx == -1)
5505 return TRUE;
5506
5507 /* Ignore also local symbols and undefined symbols. */
5508 if (! (*s->bed->elf_hash_symbol) (h))
5509 return TRUE;
5510
5511 name = h->root.root.string;
422f1182 5512 if (h->versioned >= versioned)
fdc90cb4 5513 {
422f1182
L
5514 char *p = strchr (name, ELF_VER_CHR);
5515 if (p != NULL)
14b1c01e 5516 {
422f1182
L
5517 alc = (char *) bfd_malloc (p - name + 1);
5518 if (alc == NULL)
5519 {
5520 s->error = TRUE;
5521 return FALSE;
5522 }
5523 memcpy (alc, name, p - name);
5524 alc[p - name] = '\0';
5525 name = alc;
14b1c01e 5526 }
fdc90cb4
JJ
5527 }
5528
5529 /* Compute the hash value. */
5530 ha = bfd_elf_gnu_hash (name);
5531
5532 /* Store the found hash value in the array for compute_bucket_count,
5533 and also for .dynsym reordering purposes. */
5534 s->hashcodes[s->nsyms] = ha;
5535 s->hashval[h->dynindx] = ha;
5536 ++s->nsyms;
5537 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5538 s->min_dynindx = h->dynindx;
5539
5540 if (alc != NULL)
5541 free (alc);
5542
5543 return TRUE;
5544}
5545
5546/* This function will be called though elf_link_hash_traverse to do
5547 final dynaminc symbol renumbering. */
5548
5549static bfd_boolean
5550elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5551{
a50b1753 5552 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5553 unsigned long int bucket;
5554 unsigned long int val;
5555
fdc90cb4
JJ
5556 /* Ignore indirect symbols. */
5557 if (h->dynindx == -1)
5558 return TRUE;
5559
5560 /* Ignore also local symbols and undefined symbols. */
5561 if (! (*s->bed->elf_hash_symbol) (h))
5562 {
5563 if (h->dynindx >= s->min_dynindx)
5564 h->dynindx = s->local_indx++;
5565 return TRUE;
5566 }
5567
5568 bucket = s->hashval[h->dynindx] % s->bucketcount;
5569 val = (s->hashval[h->dynindx] >> s->shift1)
5570 & ((s->maskbits >> s->shift1) - 1);
5571 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5572 s->bitmask[val]
5573 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5574 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5575 if (s->counts[bucket] == 1)
5576 /* Last element terminates the chain. */
5577 val |= 1;
5578 bfd_put_32 (s->output_bfd, val,
5579 s->contents + (s->indx[bucket] - s->symindx) * 4);
5580 --s->counts[bucket];
5581 h->dynindx = s->indx[bucket]++;
5582 return TRUE;
5583}
5584
5585/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5586
5587bfd_boolean
5588_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5589{
5590 return !(h->forced_local
5591 || h->root.type == bfd_link_hash_undefined
5592 || h->root.type == bfd_link_hash_undefweak
5593 || ((h->root.type == bfd_link_hash_defined
5594 || h->root.type == bfd_link_hash_defweak)
5595 && h->root.u.def.section->output_section == NULL));
5596}
5597
5a580b3a
AM
5598/* Array used to determine the number of hash table buckets to use
5599 based on the number of symbols there are. If there are fewer than
5600 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5601 fewer than 37 we use 17 buckets, and so forth. We never use more
5602 than 32771 buckets. */
5603
5604static const size_t elf_buckets[] =
5605{
5606 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5607 16411, 32771, 0
5608};
5609
5610/* Compute bucket count for hashing table. We do not use a static set
5611 of possible tables sizes anymore. Instead we determine for all
5612 possible reasonable sizes of the table the outcome (i.e., the
5613 number of collisions etc) and choose the best solution. The
5614 weighting functions are not too simple to allow the table to grow
5615 without bounds. Instead one of the weighting factors is the size.
5616 Therefore the result is always a good payoff between few collisions
5617 (= short chain lengths) and table size. */
5618static size_t
b20dd2ce 5619compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5620 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5621 unsigned long int nsyms,
5622 int gnu_hash)
5a580b3a 5623{
5a580b3a 5624 size_t best_size = 0;
5a580b3a 5625 unsigned long int i;
5a580b3a 5626
5a580b3a
AM
5627 /* We have a problem here. The following code to optimize the table
5628 size requires an integer type with more the 32 bits. If
5629 BFD_HOST_U_64_BIT is set we know about such a type. */
5630#ifdef BFD_HOST_U_64_BIT
5631 if (info->optimize)
5632 {
5a580b3a
AM
5633 size_t minsize;
5634 size_t maxsize;
5635 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5636 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5637 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5638 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5639 unsigned long int *counts;
d40f3da9 5640 bfd_size_type amt;
0883b6e0 5641 unsigned int no_improvement_count = 0;
5a580b3a
AM
5642
5643 /* Possible optimization parameters: if we have NSYMS symbols we say
5644 that the hashing table must at least have NSYMS/4 and at most
5645 2*NSYMS buckets. */
5646 minsize = nsyms / 4;
5647 if (minsize == 0)
5648 minsize = 1;
5649 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5650 if (gnu_hash)
5651 {
5652 if (minsize < 2)
5653 minsize = 2;
5654 if ((best_size & 31) == 0)
5655 ++best_size;
5656 }
5a580b3a
AM
5657
5658 /* Create array where we count the collisions in. We must use bfd_malloc
5659 since the size could be large. */
5660 amt = maxsize;
5661 amt *= sizeof (unsigned long int);
a50b1753 5662 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5663 if (counts == NULL)
fdc90cb4 5664 return 0;
5a580b3a
AM
5665
5666 /* Compute the "optimal" size for the hash table. The criteria is a
5667 minimal chain length. The minor criteria is (of course) the size
5668 of the table. */
5669 for (i = minsize; i < maxsize; ++i)
5670 {
5671 /* Walk through the array of hashcodes and count the collisions. */
5672 BFD_HOST_U_64_BIT max;
5673 unsigned long int j;
5674 unsigned long int fact;
5675
fdc90cb4
JJ
5676 if (gnu_hash && (i & 31) == 0)
5677 continue;
5678
5a580b3a
AM
5679 memset (counts, '\0', i * sizeof (unsigned long int));
5680
5681 /* Determine how often each hash bucket is used. */
5682 for (j = 0; j < nsyms; ++j)
5683 ++counts[hashcodes[j] % i];
5684
5685 /* For the weight function we need some information about the
5686 pagesize on the target. This is information need not be 100%
5687 accurate. Since this information is not available (so far) we
5688 define it here to a reasonable default value. If it is crucial
5689 to have a better value some day simply define this value. */
5690# ifndef BFD_TARGET_PAGESIZE
5691# define BFD_TARGET_PAGESIZE (4096)
5692# endif
5693
fdc90cb4
JJ
5694 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5695 and the chains. */
5696 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5697
5698# if 1
5699 /* Variant 1: optimize for short chains. We add the squares
5700 of all the chain lengths (which favors many small chain
5701 over a few long chains). */
5702 for (j = 0; j < i; ++j)
5703 max += counts[j] * counts[j];
5704
5705 /* This adds penalties for the overall size of the table. */
fdc90cb4 5706 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5707 max *= fact * fact;
5708# else
5709 /* Variant 2: Optimize a lot more for small table. Here we
5710 also add squares of the size but we also add penalties for
5711 empty slots (the +1 term). */
5712 for (j = 0; j < i; ++j)
5713 max += (1 + counts[j]) * (1 + counts[j]);
5714
5715 /* The overall size of the table is considered, but not as
5716 strong as in variant 1, where it is squared. */
fdc90cb4 5717 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5718 max *= fact;
5719# endif
5720
5721 /* Compare with current best results. */
5722 if (max < best_chlen)
5723 {
5724 best_chlen = max;
5725 best_size = i;
ca4be51c 5726 no_improvement_count = 0;
5a580b3a 5727 }
0883b6e0
NC
5728 /* PR 11843: Avoid futile long searches for the best bucket size
5729 when there are a large number of symbols. */
5730 else if (++no_improvement_count == 100)
5731 break;
5a580b3a
AM
5732 }
5733
5734 free (counts);
5735 }
5736 else
5737#endif /* defined (BFD_HOST_U_64_BIT) */
5738 {
5739 /* This is the fallback solution if no 64bit type is available or if we
5740 are not supposed to spend much time on optimizations. We select the
5741 bucket count using a fixed set of numbers. */
5742 for (i = 0; elf_buckets[i] != 0; i++)
5743 {
5744 best_size = elf_buckets[i];
fdc90cb4 5745 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5746 break;
5747 }
fdc90cb4
JJ
5748 if (gnu_hash && best_size < 2)
5749 best_size = 2;
5a580b3a
AM
5750 }
5751
5a580b3a
AM
5752 return best_size;
5753}
5754
d0bf826b
AM
5755/* Size any SHT_GROUP section for ld -r. */
5756
5757bfd_boolean
5758_bfd_elf_size_group_sections (struct bfd_link_info *info)
5759{
5760 bfd *ibfd;
5761
c72f2fb2 5762 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5763 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5764 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5765 return FALSE;
5766 return TRUE;
5767}
5768
04c3a755
NS
5769/* Set a default stack segment size. The value in INFO wins. If it
5770 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5771 undefined it is initialized. */
5772
5773bfd_boolean
5774bfd_elf_stack_segment_size (bfd *output_bfd,
5775 struct bfd_link_info *info,
5776 const char *legacy_symbol,
5777 bfd_vma default_size)
5778{
5779 struct elf_link_hash_entry *h = NULL;
5780
5781 /* Look for legacy symbol. */
5782 if (legacy_symbol)
5783 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5784 FALSE, FALSE, FALSE);
5785 if (h && (h->root.type == bfd_link_hash_defined
5786 || h->root.type == bfd_link_hash_defweak)
5787 && h->def_regular
5788 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5789 {
5790 /* The symbol has no type if specified on the command line. */
5791 h->type = STT_OBJECT;
5792 if (info->stacksize)
695344c0 5793 /* xgettext:c-format */
4eca0228
AM
5794 _bfd_error_handler (_("%B: stack size specified and %s set"),
5795 output_bfd, legacy_symbol);
04c3a755 5796 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 5797 /* xgettext:c-format */
4eca0228
AM
5798 _bfd_error_handler (_("%B: %s not absolute"),
5799 output_bfd, legacy_symbol);
04c3a755
NS
5800 else
5801 info->stacksize = h->root.u.def.value;
5802 }
5803
5804 if (!info->stacksize)
5805 /* If the user didn't set a size, or explicitly inhibit the
5806 size, set it now. */
5807 info->stacksize = default_size;
5808
5809 /* Provide the legacy symbol, if it is referenced. */
5810 if (h && (h->root.type == bfd_link_hash_undefined
5811 || h->root.type == bfd_link_hash_undefweak))
5812 {
5813 struct bfd_link_hash_entry *bh = NULL;
5814
5815 if (!(_bfd_generic_link_add_one_symbol
5816 (info, output_bfd, legacy_symbol,
5817 BSF_GLOBAL, bfd_abs_section_ptr,
5818 info->stacksize >= 0 ? info->stacksize : 0,
5819 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5820 return FALSE;
5821
5822 h = (struct elf_link_hash_entry *) bh;
5823 h->def_regular = 1;
5824 h->type = STT_OBJECT;
5825 }
5826
5827 return TRUE;
5828}
5829
5a580b3a
AM
5830/* Set up the sizes and contents of the ELF dynamic sections. This is
5831 called by the ELF linker emulation before_allocation routine. We
5832 must set the sizes of the sections before the linker sets the
5833 addresses of the various sections. */
5834
5835bfd_boolean
5836bfd_elf_size_dynamic_sections (bfd *output_bfd,
5837 const char *soname,
5838 const char *rpath,
5839 const char *filter_shlib,
7ee314fa
AM
5840 const char *audit,
5841 const char *depaudit,
5a580b3a
AM
5842 const char * const *auxiliary_filters,
5843 struct bfd_link_info *info,
fd91d419 5844 asection **sinterpptr)
5a580b3a 5845{
ef53be89 5846 size_t soname_indx;
5a580b3a
AM
5847 bfd *dynobj;
5848 const struct elf_backend_data *bed;
28caa186 5849 struct elf_info_failed asvinfo;
5a580b3a
AM
5850
5851 *sinterpptr = NULL;
5852
ef53be89 5853 soname_indx = (size_t) -1;
5a580b3a
AM
5854
5855 if (!is_elf_hash_table (info->hash))
5856 return TRUE;
5857
6bfdb61b 5858 bed = get_elf_backend_data (output_bfd);
04c3a755
NS
5859
5860 /* Any syms created from now on start with -1 in
5861 got.refcount/offset and plt.refcount/offset. */
5862 elf_hash_table (info)->init_got_refcount
5863 = elf_hash_table (info)->init_got_offset;
5864 elf_hash_table (info)->init_plt_refcount
5865 = elf_hash_table (info)->init_plt_offset;
5866
0e1862bb 5867 if (bfd_link_relocatable (info)
04c3a755
NS
5868 && !_bfd_elf_size_group_sections (info))
5869 return FALSE;
5870
5871 /* The backend may have to create some sections regardless of whether
5872 we're dynamic or not. */
5873 if (bed->elf_backend_always_size_sections
5874 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5875 return FALSE;
5876
5877 /* Determine any GNU_STACK segment requirements, after the backend
5878 has had a chance to set a default segment size. */
5a580b3a 5879 if (info->execstack)
12bd6957 5880 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5a580b3a 5881 else if (info->noexecstack)
12bd6957 5882 elf_stack_flags (output_bfd) = PF_R | PF_W;
5a580b3a
AM
5883 else
5884 {
5885 bfd *inputobj;
5886 asection *notesec = NULL;
5887 int exec = 0;
5888
5889 for (inputobj = info->input_bfds;
5890 inputobj;
c72f2fb2 5891 inputobj = inputobj->link.next)
5a580b3a
AM
5892 {
5893 asection *s;
5894
a92c088a
L
5895 if (inputobj->flags
5896 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5897 continue;
5898 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5899 if (s)
5900 {
5901 if (s->flags & SEC_CODE)
5902 exec = PF_X;
5903 notesec = s;
5904 }
6bfdb61b 5905 else if (bed->default_execstack)
5a580b3a
AM
5906 exec = PF_X;
5907 }
04c3a755 5908 if (notesec || info->stacksize > 0)
12bd6957 5909 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
0e1862bb 5910 if (notesec && exec && bfd_link_relocatable (info)
04c3a755
NS
5911 && notesec->output_section != bfd_abs_section_ptr)
5912 notesec->output_section->flags |= SEC_CODE;
5a580b3a
AM
5913 }
5914
5a580b3a
AM
5915 dynobj = elf_hash_table (info)->dynobj;
5916
9a2a56cc 5917 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
5918 {
5919 struct elf_info_failed eif;
5920 struct elf_link_hash_entry *h;
5921 asection *dynstr;
5922 struct bfd_elf_version_tree *t;
5923 struct bfd_elf_version_expr *d;
046183de 5924 asection *s;
5a580b3a
AM
5925 bfd_boolean all_defined;
5926
3d4d4302 5927 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
9b8b325a 5928 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5a580b3a
AM
5929
5930 if (soname != NULL)
5931 {
5932 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5933 soname, TRUE);
ef53be89 5934 if (soname_indx == (size_t) -1
5a580b3a
AM
5935 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5936 return FALSE;
5937 }
5938
5939 if (info->symbolic)
5940 {
5941 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5942 return FALSE;
5943 info->flags |= DF_SYMBOLIC;
5944 }
5945
5946 if (rpath != NULL)
5947 {
ef53be89 5948 size_t indx;
b1b00fcc 5949 bfd_vma tag;
5a580b3a
AM
5950
5951 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5952 TRUE);
ef53be89 5953 if (indx == (size_t) -1)
5a580b3a
AM
5954 return FALSE;
5955
b1b00fcc
MF
5956 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5957 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5958 return FALSE;
5a580b3a
AM
5959 }
5960
5961 if (filter_shlib != NULL)
5962 {
ef53be89 5963 size_t indx;
5a580b3a
AM
5964
5965 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5966 filter_shlib, TRUE);
ef53be89 5967 if (indx == (size_t) -1
5a580b3a
AM
5968 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5969 return FALSE;
5970 }
5971
5972 if (auxiliary_filters != NULL)
5973 {
5974 const char * const *p;
5975
5976 for (p = auxiliary_filters; *p != NULL; p++)
5977 {
ef53be89 5978 size_t indx;
5a580b3a
AM
5979
5980 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5981 *p, TRUE);
ef53be89 5982 if (indx == (size_t) -1
5a580b3a
AM
5983 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5984 return FALSE;
5985 }
5986 }
5987
7ee314fa
AM
5988 if (audit != NULL)
5989 {
ef53be89 5990 size_t indx;
7ee314fa
AM
5991
5992 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5993 TRUE);
ef53be89 5994 if (indx == (size_t) -1
7ee314fa
AM
5995 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5996 return FALSE;
5997 }
5998
5999 if (depaudit != NULL)
6000 {
ef53be89 6001 size_t indx;
7ee314fa
AM
6002
6003 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6004 TRUE);
ef53be89 6005 if (indx == (size_t) -1
7ee314fa
AM
6006 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6007 return FALSE;
6008 }
6009
5a580b3a 6010 eif.info = info;
5a580b3a
AM
6011 eif.failed = FALSE;
6012
6013 /* If we are supposed to export all symbols into the dynamic symbol
6014 table (this is not the normal case), then do so. */
55255dae 6015 if (info->export_dynamic
0e1862bb 6016 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
6017 {
6018 elf_link_hash_traverse (elf_hash_table (info),
6019 _bfd_elf_export_symbol,
6020 &eif);
6021 if (eif.failed)
6022 return FALSE;
6023 }
6024
6025 /* Make all global versions with definition. */
fd91d419 6026 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6027 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6028 if (!d->symver && d->literal)
5a580b3a
AM
6029 {
6030 const char *verstr, *name;
6031 size_t namelen, verlen, newlen;
93252b1c 6032 char *newname, *p, leading_char;
5a580b3a
AM
6033 struct elf_link_hash_entry *newh;
6034
93252b1c 6035 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6036 name = d->pattern;
93252b1c 6037 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6038 verstr = t->name;
6039 verlen = strlen (verstr);
6040 newlen = namelen + verlen + 3;
6041
a50b1753 6042 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6043 if (newname == NULL)
6044 return FALSE;
93252b1c
MF
6045 newname[0] = leading_char;
6046 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6047
6048 /* Check the hidden versioned definition. */
6049 p = newname + namelen;
6050 *p++ = ELF_VER_CHR;
6051 memcpy (p, verstr, verlen + 1);
6052 newh = elf_link_hash_lookup (elf_hash_table (info),
6053 newname, FALSE, FALSE,
6054 FALSE);
6055 if (newh == NULL
6056 || (newh->root.type != bfd_link_hash_defined
6057 && newh->root.type != bfd_link_hash_defweak))
6058 {
6059 /* Check the default versioned definition. */
6060 *p++ = ELF_VER_CHR;
6061 memcpy (p, verstr, verlen + 1);
6062 newh = elf_link_hash_lookup (elf_hash_table (info),
6063 newname, FALSE, FALSE,
6064 FALSE);
6065 }
6066 free (newname);
6067
6068 /* Mark this version if there is a definition and it is
6069 not defined in a shared object. */
6070 if (newh != NULL
f5385ebf 6071 && !newh->def_dynamic
5a580b3a
AM
6072 && (newh->root.type == bfd_link_hash_defined
6073 || newh->root.type == bfd_link_hash_defweak))
6074 d->symver = 1;
6075 }
6076
6077 /* Attach all the symbols to their version information. */
5a580b3a 6078 asvinfo.info = info;
5a580b3a
AM
6079 asvinfo.failed = FALSE;
6080
6081 elf_link_hash_traverse (elf_hash_table (info),
6082 _bfd_elf_link_assign_sym_version,
6083 &asvinfo);
6084 if (asvinfo.failed)
6085 return FALSE;
6086
6087 if (!info->allow_undefined_version)
6088 {
6089 /* Check if all global versions have a definition. */
6090 all_defined = TRUE;
fd91d419 6091 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6092 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6093 if (d->literal && !d->symver && !d->script)
5a580b3a 6094 {
4eca0228 6095 _bfd_error_handler
5a580b3a
AM
6096 (_("%s: undefined version: %s"),
6097 d->pattern, t->name);
6098 all_defined = FALSE;
6099 }
6100
6101 if (!all_defined)
6102 {
6103 bfd_set_error (bfd_error_bad_value);
6104 return FALSE;
6105 }
6106 }
6107
6108 /* Find all symbols which were defined in a dynamic object and make
6109 the backend pick a reasonable value for them. */
6110 elf_link_hash_traverse (elf_hash_table (info),
6111 _bfd_elf_adjust_dynamic_symbol,
6112 &eif);
6113 if (eif.failed)
6114 return FALSE;
6115
6116 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 6117 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
6118 now so that we know the final size of the .dynamic section. */
6119
6120 /* If there are initialization and/or finalization functions to
6121 call then add the corresponding DT_INIT/DT_FINI entries. */
6122 h = (info->init_function
6123 ? elf_link_hash_lookup (elf_hash_table (info),
6124 info->init_function, FALSE,
6125 FALSE, FALSE)
6126 : NULL);
6127 if (h != NULL
f5385ebf
AM
6128 && (h->ref_regular
6129 || h->def_regular))
5a580b3a
AM
6130 {
6131 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6132 return FALSE;
6133 }
6134 h = (info->fini_function
6135 ? elf_link_hash_lookup (elf_hash_table (info),
6136 info->fini_function, FALSE,
6137 FALSE, FALSE)
6138 : NULL);
6139 if (h != NULL
f5385ebf
AM
6140 && (h->ref_regular
6141 || h->def_regular))
5a580b3a
AM
6142 {
6143 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6144 return FALSE;
6145 }
6146
046183de
AM
6147 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6148 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6149 {
6150 /* DT_PREINIT_ARRAY is not allowed in shared library. */
0e1862bb 6151 if (! bfd_link_executable (info))
5a580b3a
AM
6152 {
6153 bfd *sub;
6154 asection *o;
6155
6156 for (sub = info->input_bfds; sub != NULL;
c72f2fb2 6157 sub = sub->link.next)
3fcd97f1
JJ
6158 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6159 for (o = sub->sections; o != NULL; o = o->next)
6160 if (elf_section_data (o)->this_hdr.sh_type
6161 == SHT_PREINIT_ARRAY)
6162 {
4eca0228 6163 _bfd_error_handler
3fcd97f1
JJ
6164 (_("%B: .preinit_array section is not allowed in DSO"),
6165 sub);
6166 break;
6167 }
5a580b3a
AM
6168
6169 bfd_set_error (bfd_error_nonrepresentable_section);
6170 return FALSE;
6171 }
6172
6173 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6174 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6175 return FALSE;
6176 }
046183de
AM
6177 s = bfd_get_section_by_name (output_bfd, ".init_array");
6178 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6179 {
6180 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6181 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6182 return FALSE;
6183 }
046183de
AM
6184 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6185 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6186 {
6187 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6188 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6189 return FALSE;
6190 }
6191
3d4d4302 6192 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6193 /* If .dynstr is excluded from the link, we don't want any of
6194 these tags. Strictly, we should be checking each section
6195 individually; This quick check covers for the case where
6196 someone does a /DISCARD/ : { *(*) }. */
6197 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6198 {
6199 bfd_size_type strsize;
6200
6201 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
6202 if ((info->emit_hash
6203 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6204 || (info->emit_gnu_hash
6205 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
6206 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6207 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6208 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6209 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6210 bed->s->sizeof_sym))
6211 return FALSE;
6212 }
6213 }
6214
de231f20
CM
6215 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6216 return FALSE;
6217
5a580b3a
AM
6218 /* The backend must work out the sizes of all the other dynamic
6219 sections. */
9a2a56cc
AM
6220 if (dynobj != NULL
6221 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
6222 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6223 return FALSE;
6224
9a2a56cc 6225 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6226 {
554220db 6227 unsigned long section_sym_count;
fd91d419 6228 struct bfd_elf_version_tree *verdefs;
5a580b3a 6229 asection *s;
5a580b3a
AM
6230
6231 /* Set up the version definition section. */
3d4d4302 6232 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
6233 BFD_ASSERT (s != NULL);
6234
6235 /* We may have created additional version definitions if we are
6236 just linking a regular application. */
fd91d419 6237 verdefs = info->version_info;
5a580b3a
AM
6238
6239 /* Skip anonymous version tag. */
6240 if (verdefs != NULL && verdefs->vernum == 0)
6241 verdefs = verdefs->next;
6242
3e3b46e5 6243 if (verdefs == NULL && !info->create_default_symver)
8423293d 6244 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6245 else
6246 {
6247 unsigned int cdefs;
6248 bfd_size_type size;
6249 struct bfd_elf_version_tree *t;
6250 bfd_byte *p;
6251 Elf_Internal_Verdef def;
6252 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
6253 struct bfd_link_hash_entry *bh;
6254 struct elf_link_hash_entry *h;
6255 const char *name;
5a580b3a
AM
6256
6257 cdefs = 0;
6258 size = 0;
6259
6260 /* Make space for the base version. */
6261 size += sizeof (Elf_External_Verdef);
6262 size += sizeof (Elf_External_Verdaux);
6263 ++cdefs;
6264
3e3b46e5
PB
6265 /* Make space for the default version. */
6266 if (info->create_default_symver)
6267 {
6268 size += sizeof (Elf_External_Verdef);
6269 ++cdefs;
6270 }
6271
5a580b3a
AM
6272 for (t = verdefs; t != NULL; t = t->next)
6273 {
6274 struct bfd_elf_version_deps *n;
6275
a6cc6b3b
RO
6276 /* Don't emit base version twice. */
6277 if (t->vernum == 0)
6278 continue;
6279
5a580b3a
AM
6280 size += sizeof (Elf_External_Verdef);
6281 size += sizeof (Elf_External_Verdaux);
6282 ++cdefs;
6283
6284 for (n = t->deps; n != NULL; n = n->next)
6285 size += sizeof (Elf_External_Verdaux);
6286 }
6287
eea6121a 6288 s->size = size;
a50b1753 6289 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6290 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6291 return FALSE;
6292
6293 /* Fill in the version definition section. */
6294
6295 p = s->contents;
6296
6297 def.vd_version = VER_DEF_CURRENT;
6298 def.vd_flags = VER_FLG_BASE;
6299 def.vd_ndx = 1;
6300 def.vd_cnt = 1;
3e3b46e5
PB
6301 if (info->create_default_symver)
6302 {
6303 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6304 def.vd_next = sizeof (Elf_External_Verdef);
6305 }
6306 else
6307 {
6308 def.vd_aux = sizeof (Elf_External_Verdef);
6309 def.vd_next = (sizeof (Elf_External_Verdef)
6310 + sizeof (Elf_External_Verdaux));
6311 }
5a580b3a 6312
ef53be89 6313 if (soname_indx != (size_t) -1)
5a580b3a
AM
6314 {
6315 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6316 soname_indx);
6317 def.vd_hash = bfd_elf_hash (soname);
6318 defaux.vda_name = soname_indx;
3e3b46e5 6319 name = soname;
5a580b3a
AM
6320 }
6321 else
6322 {
ef53be89 6323 size_t indx;
5a580b3a 6324
06084812 6325 name = lbasename (output_bfd->filename);
5a580b3a
AM
6326 def.vd_hash = bfd_elf_hash (name);
6327 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6328 name, FALSE);
ef53be89 6329 if (indx == (size_t) -1)
5a580b3a
AM
6330 return FALSE;
6331 defaux.vda_name = indx;
6332 }
6333 defaux.vda_next = 0;
6334
6335 _bfd_elf_swap_verdef_out (output_bfd, &def,
6336 (Elf_External_Verdef *) p);
6337 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6338 if (info->create_default_symver)
6339 {
6340 /* Add a symbol representing this version. */
6341 bh = NULL;
6342 if (! (_bfd_generic_link_add_one_symbol
6343 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6344 0, NULL, FALSE,
6345 get_elf_backend_data (dynobj)->collect, &bh)))
6346 return FALSE;
6347 h = (struct elf_link_hash_entry *) bh;
6348 h->non_elf = 0;
6349 h->def_regular = 1;
6350 h->type = STT_OBJECT;
6351 h->verinfo.vertree = NULL;
6352
6353 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6354 return FALSE;
6355
6356 /* Create a duplicate of the base version with the same
6357 aux block, but different flags. */
6358 def.vd_flags = 0;
6359 def.vd_ndx = 2;
6360 def.vd_aux = sizeof (Elf_External_Verdef);
6361 if (verdefs)
6362 def.vd_next = (sizeof (Elf_External_Verdef)
6363 + sizeof (Elf_External_Verdaux));
6364 else
6365 def.vd_next = 0;
6366 _bfd_elf_swap_verdef_out (output_bfd, &def,
6367 (Elf_External_Verdef *) p);
6368 p += sizeof (Elf_External_Verdef);
6369 }
5a580b3a
AM
6370 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6371 (Elf_External_Verdaux *) p);
6372 p += sizeof (Elf_External_Verdaux);
6373
6374 for (t = verdefs; t != NULL; t = t->next)
6375 {
6376 unsigned int cdeps;
6377 struct bfd_elf_version_deps *n;
5a580b3a 6378
a6cc6b3b
RO
6379 /* Don't emit the base version twice. */
6380 if (t->vernum == 0)
6381 continue;
6382
5a580b3a
AM
6383 cdeps = 0;
6384 for (n = t->deps; n != NULL; n = n->next)
6385 ++cdeps;
6386
6387 /* Add a symbol representing this version. */
6388 bh = NULL;
6389 if (! (_bfd_generic_link_add_one_symbol
6390 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6391 0, NULL, FALSE,
6392 get_elf_backend_data (dynobj)->collect, &bh)))
6393 return FALSE;
6394 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6395 h->non_elf = 0;
6396 h->def_regular = 1;
5a580b3a
AM
6397 h->type = STT_OBJECT;
6398 h->verinfo.vertree = t;
6399
c152c796 6400 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6401 return FALSE;
6402
6403 def.vd_version = VER_DEF_CURRENT;
6404 def.vd_flags = 0;
6405 if (t->globals.list == NULL
6406 && t->locals.list == NULL
6407 && ! t->used)
6408 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6409 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6410 def.vd_cnt = cdeps + 1;
6411 def.vd_hash = bfd_elf_hash (t->name);
6412 def.vd_aux = sizeof (Elf_External_Verdef);
6413 def.vd_next = 0;
a6cc6b3b
RO
6414
6415 /* If a basever node is next, it *must* be the last node in
6416 the chain, otherwise Verdef construction breaks. */
6417 if (t->next != NULL && t->next->vernum == 0)
6418 BFD_ASSERT (t->next->next == NULL);
6419
6420 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6421 def.vd_next = (sizeof (Elf_External_Verdef)
6422 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6423
6424 _bfd_elf_swap_verdef_out (output_bfd, &def,
6425 (Elf_External_Verdef *) p);
6426 p += sizeof (Elf_External_Verdef);
6427
6428 defaux.vda_name = h->dynstr_index;
6429 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6430 h->dynstr_index);
6431 defaux.vda_next = 0;
6432 if (t->deps != NULL)
6433 defaux.vda_next = sizeof (Elf_External_Verdaux);
6434 t->name_indx = defaux.vda_name;
6435
6436 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6437 (Elf_External_Verdaux *) p);
6438 p += sizeof (Elf_External_Verdaux);
6439
6440 for (n = t->deps; n != NULL; n = n->next)
6441 {
6442 if (n->version_needed == NULL)
6443 {
6444 /* This can happen if there was an error in the
6445 version script. */
6446 defaux.vda_name = 0;
6447 }
6448 else
6449 {
6450 defaux.vda_name = n->version_needed->name_indx;
6451 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6452 defaux.vda_name);
6453 }
6454 if (n->next == NULL)
6455 defaux.vda_next = 0;
6456 else
6457 defaux.vda_next = sizeof (Elf_External_Verdaux);
6458
6459 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6460 (Elf_External_Verdaux *) p);
6461 p += sizeof (Elf_External_Verdaux);
6462 }
6463 }
6464
6465 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6466 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6467 return FALSE;
6468
6469 elf_tdata (output_bfd)->cverdefs = cdefs;
6470 }
6471
6472 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6473 {
6474 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6475 return FALSE;
6476 }
6477 else if (info->flags & DF_BIND_NOW)
6478 {
6479 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6480 return FALSE;
6481 }
6482
6483 if (info->flags_1)
6484 {
0e1862bb 6485 if (bfd_link_executable (info))
5a580b3a
AM
6486 info->flags_1 &= ~ (DF_1_INITFIRST
6487 | DF_1_NODELETE
6488 | DF_1_NOOPEN);
6489 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6490 return FALSE;
6491 }
6492
6493 /* Work out the size of the version reference section. */
6494
3d4d4302 6495 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6496 BFD_ASSERT (s != NULL);
6497 {
6498 struct elf_find_verdep_info sinfo;
6499
5a580b3a
AM
6500 sinfo.info = info;
6501 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6502 if (sinfo.vers == 0)
6503 sinfo.vers = 1;
6504 sinfo.failed = FALSE;
6505
6506 elf_link_hash_traverse (elf_hash_table (info),
6507 _bfd_elf_link_find_version_dependencies,
6508 &sinfo);
14b1c01e
AM
6509 if (sinfo.failed)
6510 return FALSE;
5a580b3a
AM
6511
6512 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6513 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6514 else
6515 {
6516 Elf_Internal_Verneed *t;
6517 unsigned int size;
6518 unsigned int crefs;
6519 bfd_byte *p;
6520
a6cc6b3b 6521 /* Build the version dependency section. */
5a580b3a
AM
6522 size = 0;
6523 crefs = 0;
6524 for (t = elf_tdata (output_bfd)->verref;
6525 t != NULL;
6526 t = t->vn_nextref)
6527 {
6528 Elf_Internal_Vernaux *a;
6529
6530 size += sizeof (Elf_External_Verneed);
6531 ++crefs;
6532 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6533 size += sizeof (Elf_External_Vernaux);
6534 }
6535
eea6121a 6536 s->size = size;
a50b1753 6537 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6538 if (s->contents == NULL)
6539 return FALSE;
6540
6541 p = s->contents;
6542 for (t = elf_tdata (output_bfd)->verref;
6543 t != NULL;
6544 t = t->vn_nextref)
6545 {
6546 unsigned int caux;
6547 Elf_Internal_Vernaux *a;
ef53be89 6548 size_t indx;
5a580b3a
AM
6549
6550 caux = 0;
6551 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6552 ++caux;
6553
6554 t->vn_version = VER_NEED_CURRENT;
6555 t->vn_cnt = caux;
6556 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6557 elf_dt_name (t->vn_bfd) != NULL
6558 ? elf_dt_name (t->vn_bfd)
06084812 6559 : lbasename (t->vn_bfd->filename),
5a580b3a 6560 FALSE);
ef53be89 6561 if (indx == (size_t) -1)
5a580b3a
AM
6562 return FALSE;
6563 t->vn_file = indx;
6564 t->vn_aux = sizeof (Elf_External_Verneed);
6565 if (t->vn_nextref == NULL)
6566 t->vn_next = 0;
6567 else
6568 t->vn_next = (sizeof (Elf_External_Verneed)
6569 + caux * sizeof (Elf_External_Vernaux));
6570
6571 _bfd_elf_swap_verneed_out (output_bfd, t,
6572 (Elf_External_Verneed *) p);
6573 p += sizeof (Elf_External_Verneed);
6574
6575 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6576 {
6577 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6578 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6579 a->vna_nodename, FALSE);
ef53be89 6580 if (indx == (size_t) -1)
5a580b3a
AM
6581 return FALSE;
6582 a->vna_name = indx;
6583 if (a->vna_nextptr == NULL)
6584 a->vna_next = 0;
6585 else
6586 a->vna_next = sizeof (Elf_External_Vernaux);
6587
6588 _bfd_elf_swap_vernaux_out (output_bfd, a,
6589 (Elf_External_Vernaux *) p);
6590 p += sizeof (Elf_External_Vernaux);
6591 }
6592 }
6593
6594 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6595 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6596 return FALSE;
6597
6598 elf_tdata (output_bfd)->cverrefs = crefs;
6599 }
6600 }
6601
8423293d
AM
6602 if ((elf_tdata (output_bfd)->cverrefs == 0
6603 && elf_tdata (output_bfd)->cverdefs == 0)
6604 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6605 &section_sym_count) == 0)
6606 {
3d4d4302 6607 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6608 s->flags |= SEC_EXCLUDE;
6609 }
6610 }
6611 return TRUE;
6612}
6613
74541ad4
AM
6614/* Find the first non-excluded output section. We'll use its
6615 section symbol for some emitted relocs. */
6616void
6617_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6618{
6619 asection *s;
6620
6621 for (s = output_bfd->sections; s != NULL; s = s->next)
6622 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6623 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6624 {
6625 elf_hash_table (info)->text_index_section = s;
6626 break;
6627 }
6628}
6629
6630/* Find two non-excluded output sections, one for code, one for data.
6631 We'll use their section symbols for some emitted relocs. */
6632void
6633_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6634{
6635 asection *s;
6636
266b05cf
DJ
6637 /* Data first, since setting text_index_section changes
6638 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6639 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6640 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6641 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6642 {
266b05cf 6643 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6644 break;
6645 }
6646
6647 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6648 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6649 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6650 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6651 {
266b05cf 6652 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6653 break;
6654 }
6655
6656 if (elf_hash_table (info)->text_index_section == NULL)
6657 elf_hash_table (info)->text_index_section
6658 = elf_hash_table (info)->data_index_section;
6659}
6660
8423293d
AM
6661bfd_boolean
6662bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6663{
74541ad4
AM
6664 const struct elf_backend_data *bed;
6665
8423293d
AM
6666 if (!is_elf_hash_table (info->hash))
6667 return TRUE;
6668
74541ad4
AM
6669 bed = get_elf_backend_data (output_bfd);
6670 (*bed->elf_backend_init_index_section) (output_bfd, info);
6671
8423293d
AM
6672 if (elf_hash_table (info)->dynamic_sections_created)
6673 {
6674 bfd *dynobj;
8423293d
AM
6675 asection *s;
6676 bfd_size_type dynsymcount;
6677 unsigned long section_sym_count;
8423293d
AM
6678 unsigned int dtagcount;
6679
6680 dynobj = elf_hash_table (info)->dynobj;
6681
5a580b3a
AM
6682 /* Assign dynsym indicies. In a shared library we generate a
6683 section symbol for each output section, which come first.
6684 Next come all of the back-end allocated local dynamic syms,
6685 followed by the rest of the global symbols. */
6686
554220db
AM
6687 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6688 &section_sym_count);
5a580b3a
AM
6689
6690 /* Work out the size of the symbol version section. */
3d4d4302 6691 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6692 BFD_ASSERT (s != NULL);
d5486c43 6693 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6694 {
eea6121a 6695 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6696 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6697 if (s->contents == NULL)
6698 return FALSE;
6699
6700 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6701 return FALSE;
6702 }
6703
6704 /* Set the size of the .dynsym and .hash sections. We counted
6705 the number of dynamic symbols in elf_link_add_object_symbols.
6706 We will build the contents of .dynsym and .hash when we build
6707 the final symbol table, because until then we do not know the
6708 correct value to give the symbols. We built the .dynstr
6709 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6710 s = elf_hash_table (info)->dynsym;
5a580b3a 6711 BFD_ASSERT (s != NULL);
eea6121a 6712 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6713
d5486c43
L
6714 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6715 if (s->contents == NULL)
6716 return FALSE;
5a580b3a 6717
d5486c43
L
6718 /* The first entry in .dynsym is a dummy symbol. Clear all the
6719 section syms, in case we don't output them all. */
6720 ++section_sym_count;
6721 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6722
fdc90cb4
JJ
6723 elf_hash_table (info)->bucketcount = 0;
6724
5a580b3a
AM
6725 /* Compute the size of the hashing table. As a side effect this
6726 computes the hash values for all the names we export. */
fdc90cb4
JJ
6727 if (info->emit_hash)
6728 {
6729 unsigned long int *hashcodes;
14b1c01e 6730 struct hash_codes_info hashinf;
fdc90cb4
JJ
6731 bfd_size_type amt;
6732 unsigned long int nsyms;
6733 size_t bucketcount;
6734 size_t hash_entry_size;
6735
6736 /* Compute the hash values for all exported symbols. At the same
6737 time store the values in an array so that we could use them for
6738 optimizations. */
6739 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6740 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6741 if (hashcodes == NULL)
6742 return FALSE;
14b1c01e
AM
6743 hashinf.hashcodes = hashcodes;
6744 hashinf.error = FALSE;
5a580b3a 6745
fdc90cb4
JJ
6746 /* Put all hash values in HASHCODES. */
6747 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6748 elf_collect_hash_codes, &hashinf);
6749 if (hashinf.error)
4dd07732
AM
6750 {
6751 free (hashcodes);
6752 return FALSE;
6753 }
5a580b3a 6754
14b1c01e 6755 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6756 bucketcount
6757 = compute_bucket_count (info, hashcodes, nsyms, 0);
6758 free (hashcodes);
6759
6760 if (bucketcount == 0)
6761 return FALSE;
5a580b3a 6762
fdc90cb4
JJ
6763 elf_hash_table (info)->bucketcount = bucketcount;
6764
3d4d4302 6765 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6766 BFD_ASSERT (s != NULL);
6767 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6768 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6769 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6770 if (s->contents == NULL)
6771 return FALSE;
6772
6773 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6774 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6775 s->contents + hash_entry_size);
6776 }
6777
6778 if (info->emit_gnu_hash)
6779 {
6780 size_t i, cnt;
6781 unsigned char *contents;
6782 struct collect_gnu_hash_codes cinfo;
6783 bfd_size_type amt;
6784 size_t bucketcount;
6785
6786 memset (&cinfo, 0, sizeof (cinfo));
6787
6788 /* Compute the hash values for all exported symbols. At the same
6789 time store the values in an array so that we could use them for
6790 optimizations. */
6791 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6792 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6793 if (cinfo.hashcodes == NULL)
6794 return FALSE;
6795
6796 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6797 cinfo.min_dynindx = -1;
6798 cinfo.output_bfd = output_bfd;
6799 cinfo.bed = bed;
6800
6801 /* Put all hash values in HASHCODES. */
6802 elf_link_hash_traverse (elf_hash_table (info),
6803 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6804 if (cinfo.error)
4dd07732
AM
6805 {
6806 free (cinfo.hashcodes);
6807 return FALSE;
6808 }
fdc90cb4
JJ
6809
6810 bucketcount
6811 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6812
6813 if (bucketcount == 0)
6814 {
6815 free (cinfo.hashcodes);
6816 return FALSE;
6817 }
6818
3d4d4302 6819 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6820 BFD_ASSERT (s != NULL);
6821
6822 if (cinfo.nsyms == 0)
6823 {
6824 /* Empty .gnu.hash section is special. */
6825 BFD_ASSERT (cinfo.min_dynindx == -1);
6826 free (cinfo.hashcodes);
6827 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6828 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6829 if (contents == NULL)
6830 return FALSE;
6831 s->contents = contents;
6832 /* 1 empty bucket. */
6833 bfd_put_32 (output_bfd, 1, contents);
6834 /* SYMIDX above the special symbol 0. */
6835 bfd_put_32 (output_bfd, 1, contents + 4);
6836 /* Just one word for bitmask. */
6837 bfd_put_32 (output_bfd, 1, contents + 8);
6838 /* Only hash fn bloom filter. */
6839 bfd_put_32 (output_bfd, 0, contents + 12);
6840 /* No hashes are valid - empty bitmask. */
6841 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6842 /* No hashes in the only bucket. */
6843 bfd_put_32 (output_bfd, 0,
6844 contents + 16 + bed->s->arch_size / 8);
6845 }
6846 else
6847 {
9e6619e2 6848 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6849 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6850
9e6619e2
AM
6851 x = cinfo.nsyms;
6852 maskbitslog2 = 1;
6853 while ((x >>= 1) != 0)
6854 ++maskbitslog2;
fdc90cb4
JJ
6855 if (maskbitslog2 < 3)
6856 maskbitslog2 = 5;
6857 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6858 maskbitslog2 = maskbitslog2 + 3;
6859 else
6860 maskbitslog2 = maskbitslog2 + 2;
6861 if (bed->s->arch_size == 64)
6862 {
6863 if (maskbitslog2 == 5)
6864 maskbitslog2 = 6;
6865 cinfo.shift1 = 6;
6866 }
6867 else
6868 cinfo.shift1 = 5;
6869 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6870 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6871 cinfo.maskbits = 1 << maskbitslog2;
6872 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6873 amt = bucketcount * sizeof (unsigned long int) * 2;
6874 amt += maskwords * sizeof (bfd_vma);
a50b1753 6875 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6876 if (cinfo.bitmask == NULL)
6877 {
6878 free (cinfo.hashcodes);
6879 return FALSE;
6880 }
6881
a50b1753 6882 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6883 cinfo.indx = cinfo.counts + bucketcount;
6884 cinfo.symindx = dynsymcount - cinfo.nsyms;
6885 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6886
6887 /* Determine how often each hash bucket is used. */
6888 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6889 for (i = 0; i < cinfo.nsyms; ++i)
6890 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6891
6892 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6893 if (cinfo.counts[i] != 0)
6894 {
6895 cinfo.indx[i] = cnt;
6896 cnt += cinfo.counts[i];
6897 }
6898 BFD_ASSERT (cnt == dynsymcount);
6899 cinfo.bucketcount = bucketcount;
6900 cinfo.local_indx = cinfo.min_dynindx;
6901
6902 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6903 s->size += cinfo.maskbits / 8;
a50b1753 6904 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6905 if (contents == NULL)
6906 {
6907 free (cinfo.bitmask);
6908 free (cinfo.hashcodes);
6909 return FALSE;
6910 }
6911
6912 s->contents = contents;
6913 bfd_put_32 (output_bfd, bucketcount, contents);
6914 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6915 bfd_put_32 (output_bfd, maskwords, contents + 8);
6916 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6917 contents += 16 + cinfo.maskbits / 8;
6918
6919 for (i = 0; i < bucketcount; ++i)
6920 {
6921 if (cinfo.counts[i] == 0)
6922 bfd_put_32 (output_bfd, 0, contents);
6923 else
6924 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6925 contents += 4;
6926 }
6927
6928 cinfo.contents = contents;
6929
6930 /* Renumber dynamic symbols, populate .gnu.hash section. */
6931 elf_link_hash_traverse (elf_hash_table (info),
6932 elf_renumber_gnu_hash_syms, &cinfo);
6933
6934 contents = s->contents + 16;
6935 for (i = 0; i < maskwords; ++i)
6936 {
6937 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6938 contents);
6939 contents += bed->s->arch_size / 8;
6940 }
6941
6942 free (cinfo.bitmask);
6943 free (cinfo.hashcodes);
6944 }
6945 }
5a580b3a 6946
3d4d4302 6947 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6948 BFD_ASSERT (s != NULL);
6949
4ad4eba5 6950 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6951
eea6121a 6952 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6953
6954 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6955 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6956 return FALSE;
6957 }
6958
6959 return TRUE;
6960}
4d269e42 6961\f
4d269e42
AM
6962/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6963
6964static void
6965merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6966 asection *sec)
6967{
dbaa2011
AM
6968 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6969 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
6970}
6971
6972/* Finish SHF_MERGE section merging. */
6973
6974bfd_boolean
630993ec 6975_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
6976{
6977 bfd *ibfd;
6978 asection *sec;
6979
6980 if (!is_elf_hash_table (info->hash))
6981 return FALSE;
6982
c72f2fb2 6983 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
6984 if ((ibfd->flags & DYNAMIC) == 0
6985 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
6986 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6987 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
6988 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6989 if ((sec->flags & SEC_MERGE) != 0
6990 && !bfd_is_abs_section (sec->output_section))
6991 {
6992 struct bfd_elf_section_data *secdata;
6993
6994 secdata = elf_section_data (sec);
630993ec 6995 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
6996 &elf_hash_table (info)->merge_info,
6997 sec, &secdata->sec_info))
6998 return FALSE;
6999 else if (secdata->sec_info)
dbaa2011 7000 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7001 }
7002
7003 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7004 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7005 merge_sections_remove_hook);
7006 return TRUE;
7007}
7008
7009/* Create an entry in an ELF linker hash table. */
7010
7011struct bfd_hash_entry *
7012_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7013 struct bfd_hash_table *table,
7014 const char *string)
7015{
7016 /* Allocate the structure if it has not already been allocated by a
7017 subclass. */
7018 if (entry == NULL)
7019 {
a50b1753 7020 entry = (struct bfd_hash_entry *)
ca4be51c 7021 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7022 if (entry == NULL)
7023 return entry;
7024 }
7025
7026 /* Call the allocation method of the superclass. */
7027 entry = _bfd_link_hash_newfunc (entry, table, string);
7028 if (entry != NULL)
7029 {
7030 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7031 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7032
7033 /* Set local fields. */
7034 ret->indx = -1;
7035 ret->dynindx = -1;
7036 ret->got = htab->init_got_refcount;
7037 ret->plt = htab->init_plt_refcount;
7038 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7039 - offsetof (struct elf_link_hash_entry, size)));
7040 /* Assume that we have been called by a non-ELF symbol reader.
7041 This flag is then reset by the code which reads an ELF input
7042 file. This ensures that a symbol created by a non-ELF symbol
7043 reader will have the flag set correctly. */
7044 ret->non_elf = 1;
7045 }
7046
7047 return entry;
7048}
7049
7050/* Copy data from an indirect symbol to its direct symbol, hiding the
7051 old indirect symbol. Also used for copying flags to a weakdef. */
7052
7053void
7054_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7055 struct elf_link_hash_entry *dir,
7056 struct elf_link_hash_entry *ind)
7057{
7058 struct elf_link_hash_table *htab;
7059
7060 /* Copy down any references that we may have already seen to the
6e33951e
L
7061 symbol which just became indirect if DIR isn't a hidden versioned
7062 symbol. */
4d269e42 7063
422f1182 7064 if (dir->versioned != versioned_hidden)
6e33951e
L
7065 {
7066 dir->ref_dynamic |= ind->ref_dynamic;
7067 dir->ref_regular |= ind->ref_regular;
7068 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7069 dir->non_got_ref |= ind->non_got_ref;
7070 dir->needs_plt |= ind->needs_plt;
7071 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7072 }
4d269e42
AM
7073
7074 if (ind->root.type != bfd_link_hash_indirect)
7075 return;
7076
7077 /* Copy over the global and procedure linkage table refcount entries.
7078 These may have been already set up by a check_relocs routine. */
7079 htab = elf_hash_table (info);
7080 if (ind->got.refcount > htab->init_got_refcount.refcount)
7081 {
7082 if (dir->got.refcount < 0)
7083 dir->got.refcount = 0;
7084 dir->got.refcount += ind->got.refcount;
7085 ind->got.refcount = htab->init_got_refcount.refcount;
7086 }
7087
7088 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7089 {
7090 if (dir->plt.refcount < 0)
7091 dir->plt.refcount = 0;
7092 dir->plt.refcount += ind->plt.refcount;
7093 ind->plt.refcount = htab->init_plt_refcount.refcount;
7094 }
7095
7096 if (ind->dynindx != -1)
7097 {
7098 if (dir->dynindx != -1)
7099 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7100 dir->dynindx = ind->dynindx;
7101 dir->dynstr_index = ind->dynstr_index;
7102 ind->dynindx = -1;
7103 ind->dynstr_index = 0;
7104 }
7105}
7106
7107void
7108_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7109 struct elf_link_hash_entry *h,
7110 bfd_boolean force_local)
7111{
3aa14d16
L
7112 /* STT_GNU_IFUNC symbol must go through PLT. */
7113 if (h->type != STT_GNU_IFUNC)
7114 {
7115 h->plt = elf_hash_table (info)->init_plt_offset;
7116 h->needs_plt = 0;
7117 }
4d269e42
AM
7118 if (force_local)
7119 {
7120 h->forced_local = 1;
7121 if (h->dynindx != -1)
7122 {
7123 h->dynindx = -1;
7124 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7125 h->dynstr_index);
7126 }
7127 }
7128}
7129
7bf52ea2
AM
7130/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7131 caller. */
4d269e42
AM
7132
7133bfd_boolean
7134_bfd_elf_link_hash_table_init
7135 (struct elf_link_hash_table *table,
7136 bfd *abfd,
7137 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7138 struct bfd_hash_table *,
7139 const char *),
4dfe6ac6
NC
7140 unsigned int entsize,
7141 enum elf_target_id target_id)
4d269e42
AM
7142{
7143 bfd_boolean ret;
7144 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7145
4d269e42
AM
7146 table->init_got_refcount.refcount = can_refcount - 1;
7147 table->init_plt_refcount.refcount = can_refcount - 1;
7148 table->init_got_offset.offset = -(bfd_vma) 1;
7149 table->init_plt_offset.offset = -(bfd_vma) 1;
7150 /* The first dynamic symbol is a dummy. */
7151 table->dynsymcount = 1;
7152
7153 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7154
4d269e42 7155 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7156 table->hash_table_id = target_id;
4d269e42
AM
7157
7158 return ret;
7159}
7160
7161/* Create an ELF linker hash table. */
7162
7163struct bfd_link_hash_table *
7164_bfd_elf_link_hash_table_create (bfd *abfd)
7165{
7166 struct elf_link_hash_table *ret;
7167 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7168
7bf52ea2 7169 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7170 if (ret == NULL)
7171 return NULL;
7172
7173 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7174 sizeof (struct elf_link_hash_entry),
7175 GENERIC_ELF_DATA))
4d269e42
AM
7176 {
7177 free (ret);
7178 return NULL;
7179 }
d495ab0d 7180 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7181
7182 return &ret->root;
7183}
7184
9f7c3e5e
AM
7185/* Destroy an ELF linker hash table. */
7186
7187void
d495ab0d 7188_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7189{
d495ab0d
AM
7190 struct elf_link_hash_table *htab;
7191
7192 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7193 if (htab->dynstr != NULL)
7194 _bfd_elf_strtab_free (htab->dynstr);
7195 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7196 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7197}
7198
4d269e42
AM
7199/* This is a hook for the ELF emulation code in the generic linker to
7200 tell the backend linker what file name to use for the DT_NEEDED
7201 entry for a dynamic object. */
7202
7203void
7204bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7205{
7206 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7207 && bfd_get_format (abfd) == bfd_object)
7208 elf_dt_name (abfd) = name;
7209}
7210
7211int
7212bfd_elf_get_dyn_lib_class (bfd *abfd)
7213{
7214 int lib_class;
7215 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7216 && bfd_get_format (abfd) == bfd_object)
7217 lib_class = elf_dyn_lib_class (abfd);
7218 else
7219 lib_class = 0;
7220 return lib_class;
7221}
7222
7223void
7224bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7225{
7226 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7227 && bfd_get_format (abfd) == bfd_object)
7228 elf_dyn_lib_class (abfd) = lib_class;
7229}
7230
7231/* Get the list of DT_NEEDED entries for a link. This is a hook for
7232 the linker ELF emulation code. */
7233
7234struct bfd_link_needed_list *
7235bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7236 struct bfd_link_info *info)
7237{
7238 if (! is_elf_hash_table (info->hash))
7239 return NULL;
7240 return elf_hash_table (info)->needed;
7241}
7242
7243/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7244 hook for the linker ELF emulation code. */
7245
7246struct bfd_link_needed_list *
7247bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7248 struct bfd_link_info *info)
7249{
7250 if (! is_elf_hash_table (info->hash))
7251 return NULL;
7252 return elf_hash_table (info)->runpath;
7253}
7254
7255/* Get the name actually used for a dynamic object for a link. This
7256 is the SONAME entry if there is one. Otherwise, it is the string
7257 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7258
7259const char *
7260bfd_elf_get_dt_soname (bfd *abfd)
7261{
7262 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7263 && bfd_get_format (abfd) == bfd_object)
7264 return elf_dt_name (abfd);
7265 return NULL;
7266}
7267
7268/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7269 the ELF linker emulation code. */
7270
7271bfd_boolean
7272bfd_elf_get_bfd_needed_list (bfd *abfd,
7273 struct bfd_link_needed_list **pneeded)
7274{
7275 asection *s;
7276 bfd_byte *dynbuf = NULL;
cb33740c 7277 unsigned int elfsec;
4d269e42
AM
7278 unsigned long shlink;
7279 bfd_byte *extdyn, *extdynend;
7280 size_t extdynsize;
7281 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7282
7283 *pneeded = NULL;
7284
7285 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7286 || bfd_get_format (abfd) != bfd_object)
7287 return TRUE;
7288
7289 s = bfd_get_section_by_name (abfd, ".dynamic");
7290 if (s == NULL || s->size == 0)
7291 return TRUE;
7292
7293 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7294 goto error_return;
7295
7296 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7297 if (elfsec == SHN_BAD)
4d269e42
AM
7298 goto error_return;
7299
7300 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7301
4d269e42
AM
7302 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7303 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7304
7305 extdyn = dynbuf;
7306 extdynend = extdyn + s->size;
7307 for (; extdyn < extdynend; extdyn += extdynsize)
7308 {
7309 Elf_Internal_Dyn dyn;
7310
7311 (*swap_dyn_in) (abfd, extdyn, &dyn);
7312
7313 if (dyn.d_tag == DT_NULL)
7314 break;
7315
7316 if (dyn.d_tag == DT_NEEDED)
7317 {
7318 const char *string;
7319 struct bfd_link_needed_list *l;
7320 unsigned int tagv = dyn.d_un.d_val;
7321 bfd_size_type amt;
7322
7323 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7324 if (string == NULL)
7325 goto error_return;
7326
7327 amt = sizeof *l;
a50b1753 7328 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7329 if (l == NULL)
7330 goto error_return;
7331
7332 l->by = abfd;
7333 l->name = string;
7334 l->next = *pneeded;
7335 *pneeded = l;
7336 }
7337 }
7338
7339 free (dynbuf);
7340
7341 return TRUE;
7342
7343 error_return:
7344 if (dynbuf != NULL)
7345 free (dynbuf);
7346 return FALSE;
7347}
7348
7349struct elf_symbuf_symbol
7350{
7351 unsigned long st_name; /* Symbol name, index in string tbl */
7352 unsigned char st_info; /* Type and binding attributes */
7353 unsigned char st_other; /* Visibilty, and target specific */
7354};
7355
7356struct elf_symbuf_head
7357{
7358 struct elf_symbuf_symbol *ssym;
ef53be89 7359 size_t count;
4d269e42
AM
7360 unsigned int st_shndx;
7361};
7362
7363struct elf_symbol
7364{
7365 union
7366 {
7367 Elf_Internal_Sym *isym;
7368 struct elf_symbuf_symbol *ssym;
7369 } u;
7370 const char *name;
7371};
7372
7373/* Sort references to symbols by ascending section number. */
7374
7375static int
7376elf_sort_elf_symbol (const void *arg1, const void *arg2)
7377{
7378 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7379 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7380
7381 return s1->st_shndx - s2->st_shndx;
7382}
7383
7384static int
7385elf_sym_name_compare (const void *arg1, const void *arg2)
7386{
7387 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7388 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7389 return strcmp (s1->name, s2->name);
7390}
7391
7392static struct elf_symbuf_head *
ef53be89 7393elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7394{
14b1c01e 7395 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7396 struct elf_symbuf_symbol *ssym;
7397 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7398 size_t i, shndx_count, total_size;
4d269e42 7399
a50b1753 7400 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7401 if (indbuf == NULL)
7402 return NULL;
7403
7404 for (ind = indbuf, i = 0; i < symcount; i++)
7405 if (isymbuf[i].st_shndx != SHN_UNDEF)
7406 *ind++ = &isymbuf[i];
7407 indbufend = ind;
7408
7409 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7410 elf_sort_elf_symbol);
7411
7412 shndx_count = 0;
7413 if (indbufend > indbuf)
7414 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7415 if (ind[0]->st_shndx != ind[1]->st_shndx)
7416 shndx_count++;
7417
3ae181ee
L
7418 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7419 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7420 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7421 if (ssymbuf == NULL)
7422 {
7423 free (indbuf);
7424 return NULL;
7425 }
7426
3ae181ee 7427 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7428 ssymbuf->ssym = NULL;
7429 ssymbuf->count = shndx_count;
7430 ssymbuf->st_shndx = 0;
7431 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7432 {
7433 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7434 {
7435 ssymhead++;
7436 ssymhead->ssym = ssym;
7437 ssymhead->count = 0;
7438 ssymhead->st_shndx = (*ind)->st_shndx;
7439 }
7440 ssym->st_name = (*ind)->st_name;
7441 ssym->st_info = (*ind)->st_info;
7442 ssym->st_other = (*ind)->st_other;
7443 ssymhead->count++;
7444 }
ef53be89 7445 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7446 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7447 == total_size));
4d269e42
AM
7448
7449 free (indbuf);
7450 return ssymbuf;
7451}
7452
7453/* Check if 2 sections define the same set of local and global
7454 symbols. */
7455
8f317e31 7456static bfd_boolean
4d269e42
AM
7457bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7458 struct bfd_link_info *info)
7459{
7460 bfd *bfd1, *bfd2;
7461 const struct elf_backend_data *bed1, *bed2;
7462 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7463 size_t symcount1, symcount2;
4d269e42
AM
7464 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7465 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7466 Elf_Internal_Sym *isym, *isymend;
7467 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7468 size_t count1, count2, i;
cb33740c 7469 unsigned int shndx1, shndx2;
4d269e42
AM
7470 bfd_boolean result;
7471
7472 bfd1 = sec1->owner;
7473 bfd2 = sec2->owner;
7474
4d269e42
AM
7475 /* Both sections have to be in ELF. */
7476 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7477 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7478 return FALSE;
7479
7480 if (elf_section_type (sec1) != elf_section_type (sec2))
7481 return FALSE;
7482
4d269e42
AM
7483 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7484 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7485 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7486 return FALSE;
7487
7488 bed1 = get_elf_backend_data (bfd1);
7489 bed2 = get_elf_backend_data (bfd2);
7490 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7491 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7492 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7493 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7494
7495 if (symcount1 == 0 || symcount2 == 0)
7496 return FALSE;
7497
7498 result = FALSE;
7499 isymbuf1 = NULL;
7500 isymbuf2 = NULL;
a50b1753
NC
7501 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7502 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7503
7504 if (ssymbuf1 == NULL)
7505 {
7506 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7507 NULL, NULL, NULL);
7508 if (isymbuf1 == NULL)
7509 goto done;
7510
7511 if (!info->reduce_memory_overheads)
7512 elf_tdata (bfd1)->symbuf = ssymbuf1
7513 = elf_create_symbuf (symcount1, isymbuf1);
7514 }
7515
7516 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7517 {
7518 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7519 NULL, NULL, NULL);
7520 if (isymbuf2 == NULL)
7521 goto done;
7522
7523 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7524 elf_tdata (bfd2)->symbuf = ssymbuf2
7525 = elf_create_symbuf (symcount2, isymbuf2);
7526 }
7527
7528 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7529 {
7530 /* Optimized faster version. */
ef53be89 7531 size_t lo, hi, mid;
4d269e42
AM
7532 struct elf_symbol *symp;
7533 struct elf_symbuf_symbol *ssym, *ssymend;
7534
7535 lo = 0;
7536 hi = ssymbuf1->count;
7537 ssymbuf1++;
7538 count1 = 0;
7539 while (lo < hi)
7540 {
7541 mid = (lo + hi) / 2;
cb33740c 7542 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7543 hi = mid;
cb33740c 7544 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7545 lo = mid + 1;
7546 else
7547 {
7548 count1 = ssymbuf1[mid].count;
7549 ssymbuf1 += mid;
7550 break;
7551 }
7552 }
7553
7554 lo = 0;
7555 hi = ssymbuf2->count;
7556 ssymbuf2++;
7557 count2 = 0;
7558 while (lo < hi)
7559 {
7560 mid = (lo + hi) / 2;
cb33740c 7561 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7562 hi = mid;
cb33740c 7563 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7564 lo = mid + 1;
7565 else
7566 {
7567 count2 = ssymbuf2[mid].count;
7568 ssymbuf2 += mid;
7569 break;
7570 }
7571 }
7572
7573 if (count1 == 0 || count2 == 0 || count1 != count2)
7574 goto done;
7575
ca4be51c
AM
7576 symtable1
7577 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7578 symtable2
7579 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7580 if (symtable1 == NULL || symtable2 == NULL)
7581 goto done;
7582
7583 symp = symtable1;
7584 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7585 ssym < ssymend; ssym++, symp++)
7586 {
7587 symp->u.ssym = ssym;
7588 symp->name = bfd_elf_string_from_elf_section (bfd1,
7589 hdr1->sh_link,
7590 ssym->st_name);
7591 }
7592
7593 symp = symtable2;
7594 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7595 ssym < ssymend; ssym++, symp++)
7596 {
7597 symp->u.ssym = ssym;
7598 symp->name = bfd_elf_string_from_elf_section (bfd2,
7599 hdr2->sh_link,
7600 ssym->st_name);
7601 }
7602
7603 /* Sort symbol by name. */
7604 qsort (symtable1, count1, sizeof (struct elf_symbol),
7605 elf_sym_name_compare);
7606 qsort (symtable2, count1, sizeof (struct elf_symbol),
7607 elf_sym_name_compare);
7608
7609 for (i = 0; i < count1; i++)
7610 /* Two symbols must have the same binding, type and name. */
7611 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7612 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7613 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7614 goto done;
7615
7616 result = TRUE;
7617 goto done;
7618 }
7619
a50b1753
NC
7620 symtable1 = (struct elf_symbol *)
7621 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7622 symtable2 = (struct elf_symbol *)
7623 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7624 if (symtable1 == NULL || symtable2 == NULL)
7625 goto done;
7626
7627 /* Count definitions in the section. */
7628 count1 = 0;
7629 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7630 if (isym->st_shndx == shndx1)
4d269e42
AM
7631 symtable1[count1++].u.isym = isym;
7632
7633 count2 = 0;
7634 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7635 if (isym->st_shndx == shndx2)
4d269e42
AM
7636 symtable2[count2++].u.isym = isym;
7637
7638 if (count1 == 0 || count2 == 0 || count1 != count2)
7639 goto done;
7640
7641 for (i = 0; i < count1; i++)
7642 symtable1[i].name
7643 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7644 symtable1[i].u.isym->st_name);
7645
7646 for (i = 0; i < count2; i++)
7647 symtable2[i].name
7648 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7649 symtable2[i].u.isym->st_name);
7650
7651 /* Sort symbol by name. */
7652 qsort (symtable1, count1, sizeof (struct elf_symbol),
7653 elf_sym_name_compare);
7654 qsort (symtable2, count1, sizeof (struct elf_symbol),
7655 elf_sym_name_compare);
7656
7657 for (i = 0; i < count1; i++)
7658 /* Two symbols must have the same binding, type and name. */
7659 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7660 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7661 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7662 goto done;
7663
7664 result = TRUE;
7665
7666done:
7667 if (symtable1)
7668 free (symtable1);
7669 if (symtable2)
7670 free (symtable2);
7671 if (isymbuf1)
7672 free (isymbuf1);
7673 if (isymbuf2)
7674 free (isymbuf2);
7675
7676 return result;
7677}
7678
7679/* Return TRUE if 2 section types are compatible. */
7680
7681bfd_boolean
7682_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7683 bfd *bbfd, const asection *bsec)
7684{
7685 if (asec == NULL
7686 || bsec == NULL
7687 || abfd->xvec->flavour != bfd_target_elf_flavour
7688 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7689 return TRUE;
7690
7691 return elf_section_type (asec) == elf_section_type (bsec);
7692}
7693\f
c152c796
AM
7694/* Final phase of ELF linker. */
7695
7696/* A structure we use to avoid passing large numbers of arguments. */
7697
7698struct elf_final_link_info
7699{
7700 /* General link information. */
7701 struct bfd_link_info *info;
7702 /* Output BFD. */
7703 bfd *output_bfd;
7704 /* Symbol string table. */
ef10c3ac 7705 struct elf_strtab_hash *symstrtab;
c152c796
AM
7706 /* .hash section. */
7707 asection *hash_sec;
7708 /* symbol version section (.gnu.version). */
7709 asection *symver_sec;
7710 /* Buffer large enough to hold contents of any section. */
7711 bfd_byte *contents;
7712 /* Buffer large enough to hold external relocs of any section. */
7713 void *external_relocs;
7714 /* Buffer large enough to hold internal relocs of any section. */
7715 Elf_Internal_Rela *internal_relocs;
7716 /* Buffer large enough to hold external local symbols of any input
7717 BFD. */
7718 bfd_byte *external_syms;
7719 /* And a buffer for symbol section indices. */
7720 Elf_External_Sym_Shndx *locsym_shndx;
7721 /* Buffer large enough to hold internal local symbols of any input
7722 BFD. */
7723 Elf_Internal_Sym *internal_syms;
7724 /* Array large enough to hold a symbol index for each local symbol
7725 of any input BFD. */
7726 long *indices;
7727 /* Array large enough to hold a section pointer for each local
7728 symbol of any input BFD. */
7729 asection **sections;
ef10c3ac 7730 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7731 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7732 /* Number of STT_FILE syms seen. */
7733 size_t filesym_count;
c152c796
AM
7734};
7735
7736/* This struct is used to pass information to elf_link_output_extsym. */
7737
7738struct elf_outext_info
7739{
7740 bfd_boolean failed;
7741 bfd_boolean localsyms;
34a79995 7742 bfd_boolean file_sym_done;
8b127cbc 7743 struct elf_final_link_info *flinfo;
c152c796
AM
7744};
7745
d9352518
DB
7746
7747/* Support for evaluating a complex relocation.
7748
7749 Complex relocations are generalized, self-describing relocations. The
7750 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7751 relocations themselves.
d9352518
DB
7752
7753 The relocations are use a reserved elf-wide relocation type code (R_RELC
7754 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7755 information (start bit, end bit, word width, etc) into the addend. This
7756 information is extracted from CGEN-generated operand tables within gas.
7757
7758 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7759 internal) representing prefix-notation expressions, including but not
7760 limited to those sorts of expressions normally encoded as addends in the
7761 addend field. The symbol mangling format is:
7762
7763 <node> := <literal>
7764 | <unary-operator> ':' <node>
7765 | <binary-operator> ':' <node> ':' <node>
7766 ;
7767
7768 <literal> := 's' <digits=N> ':' <N character symbol name>
7769 | 'S' <digits=N> ':' <N character section name>
7770 | '#' <hexdigits>
7771 ;
7772
7773 <binary-operator> := as in C
7774 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7775
7776static void
a0c8462f
AM
7777set_symbol_value (bfd *bfd_with_globals,
7778 Elf_Internal_Sym *isymbuf,
7779 size_t locsymcount,
7780 size_t symidx,
7781 bfd_vma val)
d9352518 7782{
8977835c
AM
7783 struct elf_link_hash_entry **sym_hashes;
7784 struct elf_link_hash_entry *h;
7785 size_t extsymoff = locsymcount;
d9352518 7786
8977835c 7787 if (symidx < locsymcount)
d9352518 7788 {
8977835c
AM
7789 Elf_Internal_Sym *sym;
7790
7791 sym = isymbuf + symidx;
7792 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7793 {
7794 /* It is a local symbol: move it to the
7795 "absolute" section and give it a value. */
7796 sym->st_shndx = SHN_ABS;
7797 sym->st_value = val;
7798 return;
7799 }
7800 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7801 extsymoff = 0;
d9352518 7802 }
8977835c
AM
7803
7804 /* It is a global symbol: set its link type
7805 to "defined" and give it a value. */
7806
7807 sym_hashes = elf_sym_hashes (bfd_with_globals);
7808 h = sym_hashes [symidx - extsymoff];
7809 while (h->root.type == bfd_link_hash_indirect
7810 || h->root.type == bfd_link_hash_warning)
7811 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7812 h->root.type = bfd_link_hash_defined;
7813 h->root.u.def.value = val;
7814 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7815}
7816
a0c8462f
AM
7817static bfd_boolean
7818resolve_symbol (const char *name,
7819 bfd *input_bfd,
8b127cbc 7820 struct elf_final_link_info *flinfo,
a0c8462f
AM
7821 bfd_vma *result,
7822 Elf_Internal_Sym *isymbuf,
7823 size_t locsymcount)
d9352518 7824{
a0c8462f
AM
7825 Elf_Internal_Sym *sym;
7826 struct bfd_link_hash_entry *global_entry;
7827 const char *candidate = NULL;
7828 Elf_Internal_Shdr *symtab_hdr;
7829 size_t i;
7830
d9352518
DB
7831 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7832
7833 for (i = 0; i < locsymcount; ++ i)
7834 {
8977835c 7835 sym = isymbuf + i;
d9352518
DB
7836
7837 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7838 continue;
7839
7840 candidate = bfd_elf_string_from_elf_section (input_bfd,
7841 symtab_hdr->sh_link,
7842 sym->st_name);
7843#ifdef DEBUG
0f02bbd9
AM
7844 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7845 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7846#endif
7847 if (candidate && strcmp (candidate, name) == 0)
7848 {
8b127cbc 7849 asection *sec = flinfo->sections [i];
d9352518 7850
0f02bbd9
AM
7851 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7852 *result += sec->output_offset + sec->output_section->vma;
d9352518 7853#ifdef DEBUG
0f02bbd9
AM
7854 printf ("Found symbol with value %8.8lx\n",
7855 (unsigned long) *result);
d9352518
DB
7856#endif
7857 return TRUE;
7858 }
7859 }
7860
7861 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7862 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7863 FALSE, FALSE, TRUE);
d9352518
DB
7864 if (!global_entry)
7865 return FALSE;
a0c8462f 7866
d9352518
DB
7867 if (global_entry->type == bfd_link_hash_defined
7868 || global_entry->type == bfd_link_hash_defweak)
7869 {
a0c8462f
AM
7870 *result = (global_entry->u.def.value
7871 + global_entry->u.def.section->output_section->vma
7872 + global_entry->u.def.section->output_offset);
d9352518 7873#ifdef DEBUG
0f02bbd9
AM
7874 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7875 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7876#endif
7877 return TRUE;
a0c8462f 7878 }
d9352518 7879
d9352518
DB
7880 return FALSE;
7881}
7882
37b01f6a
DG
7883/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7884 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7885 names like "foo.end" which is the end address of section "foo". */
7886
d9352518 7887static bfd_boolean
a0c8462f
AM
7888resolve_section (const char *name,
7889 asection *sections,
37b01f6a
DG
7890 bfd_vma *result,
7891 bfd * abfd)
d9352518 7892{
a0c8462f
AM
7893 asection *curr;
7894 unsigned int len;
d9352518 7895
a0c8462f 7896 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7897 if (strcmp (curr->name, name) == 0)
7898 {
7899 *result = curr->vma;
7900 return TRUE;
7901 }
7902
7903 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 7904 /* FIXME: This could be coded more efficiently... */
a0c8462f 7905 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7906 {
7907 len = strlen (curr->name);
a0c8462f 7908 if (len > strlen (name))
d9352518
DB
7909 continue;
7910
7911 if (strncmp (curr->name, name, len) == 0)
7912 {
7913 if (strncmp (".end", name + len, 4) == 0)
7914 {
37b01f6a 7915 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
7916 return TRUE;
7917 }
7918
7919 /* Insert more pseudo-section names here, if you like. */
7920 }
7921 }
a0c8462f 7922
d9352518
DB
7923 return FALSE;
7924}
7925
7926static void
a0c8462f 7927undefined_reference (const char *reftype, const char *name)
d9352518 7928{
695344c0 7929 /* xgettext:c-format */
a0c8462f
AM
7930 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7931 reftype, name);
d9352518
DB
7932}
7933
7934static bfd_boolean
a0c8462f
AM
7935eval_symbol (bfd_vma *result,
7936 const char **symp,
7937 bfd *input_bfd,
8b127cbc 7938 struct elf_final_link_info *flinfo,
a0c8462f
AM
7939 bfd_vma dot,
7940 Elf_Internal_Sym *isymbuf,
7941 size_t locsymcount,
7942 int signed_p)
d9352518 7943{
4b93929b
NC
7944 size_t len;
7945 size_t symlen;
a0c8462f
AM
7946 bfd_vma a;
7947 bfd_vma b;
4b93929b 7948 char symbuf[4096];
0f02bbd9 7949 const char *sym = *symp;
a0c8462f
AM
7950 const char *symend;
7951 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7952
7953 len = strlen (sym);
7954 symend = sym + len;
7955
4b93929b 7956 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7957 {
7958 bfd_set_error (bfd_error_invalid_operation);
7959 return FALSE;
7960 }
a0c8462f 7961
d9352518
DB
7962 switch (* sym)
7963 {
7964 case '.':
0f02bbd9
AM
7965 *result = dot;
7966 *symp = sym + 1;
d9352518
DB
7967 return TRUE;
7968
7969 case '#':
0f02bbd9
AM
7970 ++sym;
7971 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7972 return TRUE;
7973
7974 case 'S':
7975 symbol_is_section = TRUE;
1a0670f3 7976 /* Fall through. */
a0c8462f 7977 case 's':
0f02bbd9
AM
7978 ++sym;
7979 symlen = strtol (sym, (char **) symp, 10);
7980 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7981
4b93929b 7982 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7983 {
7984 bfd_set_error (bfd_error_invalid_operation);
7985 return FALSE;
7986 }
7987
7988 memcpy (symbuf, sym, symlen);
a0c8462f 7989 symbuf[symlen] = '\0';
0f02bbd9 7990 *symp = sym + symlen;
a0c8462f
AM
7991
7992 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7993 the symbol as a section, or vice-versa. so we're pretty liberal in our
7994 interpretation here; section means "try section first", not "must be a
7995 section", and likewise with symbol. */
7996
a0c8462f 7997 if (symbol_is_section)
d9352518 7998 {
37b01f6a 7999 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8000 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8001 isymbuf, locsymcount))
d9352518
DB
8002 {
8003 undefined_reference ("section", symbuf);
8004 return FALSE;
8005 }
a0c8462f
AM
8006 }
8007 else
d9352518 8008 {
8b127cbc 8009 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8010 isymbuf, locsymcount)
8b127cbc 8011 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8012 result, input_bfd))
d9352518
DB
8013 {
8014 undefined_reference ("symbol", symbuf);
8015 return FALSE;
8016 }
8017 }
8018
8019 return TRUE;
a0c8462f 8020
d9352518
DB
8021 /* All that remains are operators. */
8022
8023#define UNARY_OP(op) \
8024 if (strncmp (sym, #op, strlen (#op)) == 0) \
8025 { \
8026 sym += strlen (#op); \
a0c8462f
AM
8027 if (*sym == ':') \
8028 ++sym; \
0f02bbd9 8029 *symp = sym; \
8b127cbc 8030 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8031 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8032 return FALSE; \
8033 if (signed_p) \
0f02bbd9 8034 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8035 else \
8036 *result = op a; \
d9352518
DB
8037 return TRUE; \
8038 }
8039
8040#define BINARY_OP(op) \
8041 if (strncmp (sym, #op, strlen (#op)) == 0) \
8042 { \
8043 sym += strlen (#op); \
a0c8462f
AM
8044 if (*sym == ':') \
8045 ++sym; \
0f02bbd9 8046 *symp = sym; \
8b127cbc 8047 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8048 isymbuf, locsymcount, signed_p)) \
a0c8462f 8049 return FALSE; \
0f02bbd9 8050 ++*symp; \
8b127cbc 8051 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8052 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8053 return FALSE; \
8054 if (signed_p) \
0f02bbd9 8055 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8056 else \
8057 *result = a op b; \
d9352518
DB
8058 return TRUE; \
8059 }
8060
8061 default:
8062 UNARY_OP (0-);
8063 BINARY_OP (<<);
8064 BINARY_OP (>>);
8065 BINARY_OP (==);
8066 BINARY_OP (!=);
8067 BINARY_OP (<=);
8068 BINARY_OP (>=);
8069 BINARY_OP (&&);
8070 BINARY_OP (||);
8071 UNARY_OP (~);
8072 UNARY_OP (!);
8073 BINARY_OP (*);
8074 BINARY_OP (/);
8075 BINARY_OP (%);
8076 BINARY_OP (^);
8077 BINARY_OP (|);
8078 BINARY_OP (&);
8079 BINARY_OP (+);
8080 BINARY_OP (-);
8081 BINARY_OP (<);
8082 BINARY_OP (>);
8083#undef UNARY_OP
8084#undef BINARY_OP
8085 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8086 bfd_set_error (bfd_error_invalid_operation);
8087 return FALSE;
8088 }
8089}
8090
d9352518 8091static void
a0c8462f
AM
8092put_value (bfd_vma size,
8093 unsigned long chunksz,
8094 bfd *input_bfd,
8095 bfd_vma x,
8096 bfd_byte *location)
d9352518
DB
8097{
8098 location += (size - chunksz);
8099
41cd1ad1 8100 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8101 {
8102 switch (chunksz)
8103 {
d9352518
DB
8104 case 1:
8105 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8106 x >>= 8;
d9352518
DB
8107 break;
8108 case 2:
8109 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8110 x >>= 16;
d9352518
DB
8111 break;
8112 case 4:
8113 bfd_put_32 (input_bfd, x, location);
65164438
NC
8114 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8115 x >>= 16;
8116 x >>= 16;
d9352518 8117 break;
d9352518 8118#ifdef BFD64
41cd1ad1 8119 case 8:
d9352518 8120 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8121 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8122 x >>= 32;
8123 x >>= 32;
8124 break;
d9352518 8125#endif
41cd1ad1
NC
8126 default:
8127 abort ();
d9352518
DB
8128 break;
8129 }
8130 }
8131}
8132
a0c8462f
AM
8133static bfd_vma
8134get_value (bfd_vma size,
8135 unsigned long chunksz,
8136 bfd *input_bfd,
8137 bfd_byte *location)
d9352518 8138{
9b239e0e 8139 int shift;
d9352518
DB
8140 bfd_vma x = 0;
8141
9b239e0e
NC
8142 /* Sanity checks. */
8143 BFD_ASSERT (chunksz <= sizeof (x)
8144 && size >= chunksz
8145 && chunksz != 0
8146 && (size % chunksz) == 0
8147 && input_bfd != NULL
8148 && location != NULL);
8149
8150 if (chunksz == sizeof (x))
8151 {
8152 BFD_ASSERT (size == chunksz);
8153
8154 /* Make sure that we do not perform an undefined shift operation.
8155 We know that size == chunksz so there will only be one iteration
8156 of the loop below. */
8157 shift = 0;
8158 }
8159 else
8160 shift = 8 * chunksz;
8161
a0c8462f 8162 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8163 {
8164 switch (chunksz)
8165 {
d9352518 8166 case 1:
9b239e0e 8167 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8168 break;
8169 case 2:
9b239e0e 8170 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8171 break;
8172 case 4:
9b239e0e 8173 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8174 break;
d9352518 8175#ifdef BFD64
9b239e0e
NC
8176 case 8:
8177 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8178 break;
9b239e0e
NC
8179#endif
8180 default:
8181 abort ();
d9352518
DB
8182 }
8183 }
8184 return x;
8185}
8186
a0c8462f
AM
8187static void
8188decode_complex_addend (unsigned long *start, /* in bits */
8189 unsigned long *oplen, /* in bits */
8190 unsigned long *len, /* in bits */
8191 unsigned long *wordsz, /* in bytes */
8192 unsigned long *chunksz, /* in bytes */
8193 unsigned long *lsb0_p,
8194 unsigned long *signed_p,
8195 unsigned long *trunc_p,
8196 unsigned long encoded)
d9352518
DB
8197{
8198 * start = encoded & 0x3F;
8199 * len = (encoded >> 6) & 0x3F;
8200 * oplen = (encoded >> 12) & 0x3F;
8201 * wordsz = (encoded >> 18) & 0xF;
8202 * chunksz = (encoded >> 22) & 0xF;
8203 * lsb0_p = (encoded >> 27) & 1;
8204 * signed_p = (encoded >> 28) & 1;
8205 * trunc_p = (encoded >> 29) & 1;
8206}
8207
cdfeee4f 8208bfd_reloc_status_type
0f02bbd9 8209bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8210 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8211 bfd_byte *contents,
8212 Elf_Internal_Rela *rel,
8213 bfd_vma relocation)
d9352518 8214{
0f02bbd9
AM
8215 bfd_vma shift, x, mask;
8216 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8217 bfd_reloc_status_type r;
d9352518
DB
8218
8219 /* Perform this reloc, since it is complex.
8220 (this is not to say that it necessarily refers to a complex
8221 symbol; merely that it is a self-describing CGEN based reloc.
8222 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8223 word size, etc) encoded within it.). */
d9352518 8224
a0c8462f
AM
8225 decode_complex_addend (&start, &oplen, &len, &wordsz,
8226 &chunksz, &lsb0_p, &signed_p,
8227 &trunc_p, rel->r_addend);
d9352518
DB
8228
8229 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8230
8231 if (lsb0_p)
8232 shift = (start + 1) - len;
8233 else
8234 shift = (8 * wordsz) - (start + len);
8235
37b01f6a
DG
8236 x = get_value (wordsz, chunksz, input_bfd,
8237 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8238
8239#ifdef DEBUG
8240 printf ("Doing complex reloc: "
8241 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8242 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8243 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8244 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8245 oplen, (unsigned long) x, (unsigned long) mask,
8246 (unsigned long) relocation);
d9352518
DB
8247#endif
8248
cdfeee4f 8249 r = bfd_reloc_ok;
d9352518 8250 if (! trunc_p)
cdfeee4f
AM
8251 /* Now do an overflow check. */
8252 r = bfd_check_overflow ((signed_p
8253 ? complain_overflow_signed
8254 : complain_overflow_unsigned),
8255 len, 0, (8 * wordsz),
8256 relocation);
a0c8462f 8257
d9352518
DB
8258 /* Do the deed. */
8259 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8260
8261#ifdef DEBUG
8262 printf (" relocation: %8.8lx\n"
8263 " shifted mask: %8.8lx\n"
8264 " shifted/masked reloc: %8.8lx\n"
8265 " result: %8.8lx\n",
9ccb8af9
AM
8266 (unsigned long) relocation, (unsigned long) (mask << shift),
8267 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8268#endif
37b01f6a
DG
8269 put_value (wordsz, chunksz, input_bfd, x,
8270 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8271 return r;
d9352518
DB
8272}
8273
0e287786
AM
8274/* Functions to read r_offset from external (target order) reloc
8275 entry. Faster than bfd_getl32 et al, because we let the compiler
8276 know the value is aligned. */
53df40a4 8277
0e287786
AM
8278static bfd_vma
8279ext32l_r_offset (const void *p)
53df40a4
AM
8280{
8281 union aligned32
8282 {
8283 uint32_t v;
8284 unsigned char c[4];
8285 };
8286 const union aligned32 *a
0e287786 8287 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8288
8289 uint32_t aval = ( (uint32_t) a->c[0]
8290 | (uint32_t) a->c[1] << 8
8291 | (uint32_t) a->c[2] << 16
8292 | (uint32_t) a->c[3] << 24);
0e287786 8293 return aval;
53df40a4
AM
8294}
8295
0e287786
AM
8296static bfd_vma
8297ext32b_r_offset (const void *p)
53df40a4
AM
8298{
8299 union aligned32
8300 {
8301 uint32_t v;
8302 unsigned char c[4];
8303 };
8304 const union aligned32 *a
0e287786 8305 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8306
8307 uint32_t aval = ( (uint32_t) a->c[0] << 24
8308 | (uint32_t) a->c[1] << 16
8309 | (uint32_t) a->c[2] << 8
8310 | (uint32_t) a->c[3]);
0e287786 8311 return aval;
53df40a4
AM
8312}
8313
8314#ifdef BFD_HOST_64_BIT
0e287786
AM
8315static bfd_vma
8316ext64l_r_offset (const void *p)
53df40a4
AM
8317{
8318 union aligned64
8319 {
8320 uint64_t v;
8321 unsigned char c[8];
8322 };
8323 const union aligned64 *a
0e287786 8324 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8325
8326 uint64_t aval = ( (uint64_t) a->c[0]
8327 | (uint64_t) a->c[1] << 8
8328 | (uint64_t) a->c[2] << 16
8329 | (uint64_t) a->c[3] << 24
8330 | (uint64_t) a->c[4] << 32
8331 | (uint64_t) a->c[5] << 40
8332 | (uint64_t) a->c[6] << 48
8333 | (uint64_t) a->c[7] << 56);
0e287786 8334 return aval;
53df40a4
AM
8335}
8336
0e287786
AM
8337static bfd_vma
8338ext64b_r_offset (const void *p)
53df40a4
AM
8339{
8340 union aligned64
8341 {
8342 uint64_t v;
8343 unsigned char c[8];
8344 };
8345 const union aligned64 *a
0e287786 8346 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8347
8348 uint64_t aval = ( (uint64_t) a->c[0] << 56
8349 | (uint64_t) a->c[1] << 48
8350 | (uint64_t) a->c[2] << 40
8351 | (uint64_t) a->c[3] << 32
8352 | (uint64_t) a->c[4] << 24
8353 | (uint64_t) a->c[5] << 16
8354 | (uint64_t) a->c[6] << 8
8355 | (uint64_t) a->c[7]);
0e287786 8356 return aval;
53df40a4
AM
8357}
8358#endif
8359
c152c796
AM
8360/* When performing a relocatable link, the input relocations are
8361 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8362 referenced must be updated. Update all the relocations found in
8363 RELDATA. */
c152c796 8364
bca6d0e3 8365static bfd_boolean
c152c796 8366elf_link_adjust_relocs (bfd *abfd,
9eaff861 8367 asection *sec,
28dbcedc
AM
8368 struct bfd_elf_section_reloc_data *reldata,
8369 bfd_boolean sort)
c152c796
AM
8370{
8371 unsigned int i;
8372 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8373 bfd_byte *erela;
8374 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8375 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8376 bfd_vma r_type_mask;
8377 int r_sym_shift;
d4730f92
BS
8378 unsigned int count = reldata->count;
8379 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8380
d4730f92 8381 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8382 {
8383 swap_in = bed->s->swap_reloc_in;
8384 swap_out = bed->s->swap_reloc_out;
8385 }
d4730f92 8386 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8387 {
8388 swap_in = bed->s->swap_reloca_in;
8389 swap_out = bed->s->swap_reloca_out;
8390 }
8391 else
8392 abort ();
8393
8394 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8395 abort ();
8396
8397 if (bed->s->arch_size == 32)
8398 {
8399 r_type_mask = 0xff;
8400 r_sym_shift = 8;
8401 }
8402 else
8403 {
8404 r_type_mask = 0xffffffff;
8405 r_sym_shift = 32;
8406 }
8407
d4730f92
BS
8408 erela = reldata->hdr->contents;
8409 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8410 {
8411 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8412 unsigned int j;
8413
8414 if (*rel_hash == NULL)
8415 continue;
8416
8417 BFD_ASSERT ((*rel_hash)->indx >= 0);
8418
8419 (*swap_in) (abfd, erela, irela);
8420 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8421 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8422 | (irela[j].r_info & r_type_mask));
8423 (*swap_out) (abfd, irela, erela);
8424 }
53df40a4 8425
9eaff861
AO
8426 if (bed->elf_backend_update_relocs)
8427 (*bed->elf_backend_update_relocs) (sec, reldata);
8428
0e287786 8429 if (sort && count != 0)
53df40a4 8430 {
0e287786
AM
8431 bfd_vma (*ext_r_off) (const void *);
8432 bfd_vma r_off;
8433 size_t elt_size;
8434 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8435 bfd_byte *buf = NULL;
28dbcedc
AM
8436
8437 if (bed->s->arch_size == 32)
8438 {
8439 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8440 ext_r_off = ext32l_r_offset;
28dbcedc 8441 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8442 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8443 else
8444 abort ();
8445 }
53df40a4 8446 else
28dbcedc 8447 {
53df40a4 8448#ifdef BFD_HOST_64_BIT
28dbcedc 8449 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8450 ext_r_off = ext64l_r_offset;
28dbcedc 8451 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8452 ext_r_off = ext64b_r_offset;
28dbcedc 8453 else
53df40a4 8454#endif
28dbcedc
AM
8455 abort ();
8456 }
0e287786 8457
bca6d0e3
AM
8458 /* Must use a stable sort here. A modified insertion sort,
8459 since the relocs are mostly sorted already. */
0e287786
AM
8460 elt_size = reldata->hdr->sh_entsize;
8461 base = reldata->hdr->contents;
8462 end = base + count * elt_size;
bca6d0e3 8463 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8464 abort ();
8465
8466 /* Ensure the first element is lowest. This acts as a sentinel,
8467 speeding the main loop below. */
8468 r_off = (*ext_r_off) (base);
8469 for (p = loc = base; (p += elt_size) < end; )
8470 {
8471 bfd_vma r_off2 = (*ext_r_off) (p);
8472 if (r_off > r_off2)
8473 {
8474 r_off = r_off2;
8475 loc = p;
8476 }
8477 }
8478 if (loc != base)
8479 {
8480 /* Don't just swap *base and *loc as that changes the order
8481 of the original base[0] and base[1] if they happen to
8482 have the same r_offset. */
bca6d0e3
AM
8483 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8484 memcpy (onebuf, loc, elt_size);
0e287786 8485 memmove (base + elt_size, base, loc - base);
bca6d0e3 8486 memcpy (base, onebuf, elt_size);
0e287786
AM
8487 }
8488
b29b8669 8489 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8490 {
8491 /* base to p is sorted, *p is next to insert. */
8492 r_off = (*ext_r_off) (p);
8493 /* Search the sorted region for location to insert. */
8494 loc = p - elt_size;
8495 while (r_off < (*ext_r_off) (loc))
8496 loc -= elt_size;
8497 loc += elt_size;
8498 if (loc != p)
8499 {
bca6d0e3
AM
8500 /* Chances are there is a run of relocs to insert here,
8501 from one of more input files. Files are not always
8502 linked in order due to the way elf_link_input_bfd is
8503 called. See pr17666. */
8504 size_t sortlen = p - loc;
8505 bfd_vma r_off2 = (*ext_r_off) (loc);
8506 size_t runlen = elt_size;
8507 size_t buf_size = 96 * 1024;
8508 while (p + runlen < end
8509 && (sortlen <= buf_size
8510 || runlen + elt_size <= buf_size)
8511 && r_off2 > (*ext_r_off) (p + runlen))
8512 runlen += elt_size;
8513 if (buf == NULL)
8514 {
8515 buf = bfd_malloc (buf_size);
8516 if (buf == NULL)
8517 return FALSE;
8518 }
8519 if (runlen < sortlen)
8520 {
8521 memcpy (buf, p, runlen);
8522 memmove (loc + runlen, loc, sortlen);
8523 memcpy (loc, buf, runlen);
8524 }
8525 else
8526 {
8527 memcpy (buf, loc, sortlen);
8528 memmove (loc, p, runlen);
8529 memcpy (loc + runlen, buf, sortlen);
8530 }
b29b8669 8531 p += runlen - elt_size;
0e287786
AM
8532 }
8533 }
8534 /* Hashes are no longer valid. */
28dbcedc
AM
8535 free (reldata->hashes);
8536 reldata->hashes = NULL;
bca6d0e3 8537 free (buf);
53df40a4 8538 }
bca6d0e3 8539 return TRUE;
c152c796
AM
8540}
8541
8542struct elf_link_sort_rela
8543{
8544 union {
8545 bfd_vma offset;
8546 bfd_vma sym_mask;
8547 } u;
8548 enum elf_reloc_type_class type;
8549 /* We use this as an array of size int_rels_per_ext_rel. */
8550 Elf_Internal_Rela rela[1];
8551};
8552
8553static int
8554elf_link_sort_cmp1 (const void *A, const void *B)
8555{
a50b1753
NC
8556 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8557 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8558 int relativea, relativeb;
8559
8560 relativea = a->type == reloc_class_relative;
8561 relativeb = b->type == reloc_class_relative;
8562
8563 if (relativea < relativeb)
8564 return 1;
8565 if (relativea > relativeb)
8566 return -1;
8567 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8568 return -1;
8569 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8570 return 1;
8571 if (a->rela->r_offset < b->rela->r_offset)
8572 return -1;
8573 if (a->rela->r_offset > b->rela->r_offset)
8574 return 1;
8575 return 0;
8576}
8577
8578static int
8579elf_link_sort_cmp2 (const void *A, const void *B)
8580{
a50b1753
NC
8581 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8582 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8583
7e612e98 8584 if (a->type < b->type)
c152c796 8585 return -1;
7e612e98 8586 if (a->type > b->type)
c152c796 8587 return 1;
7e612e98 8588 if (a->u.offset < b->u.offset)
c152c796 8589 return -1;
7e612e98 8590 if (a->u.offset > b->u.offset)
c152c796
AM
8591 return 1;
8592 if (a->rela->r_offset < b->rela->r_offset)
8593 return -1;
8594 if (a->rela->r_offset > b->rela->r_offset)
8595 return 1;
8596 return 0;
8597}
8598
8599static size_t
8600elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8601{
3410fea8 8602 asection *dynamic_relocs;
fc66a176
L
8603 asection *rela_dyn;
8604 asection *rel_dyn;
c152c796
AM
8605 bfd_size_type count, size;
8606 size_t i, ret, sort_elt, ext_size;
8607 bfd_byte *sort, *s_non_relative, *p;
8608 struct elf_link_sort_rela *sq;
8609 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8610 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8611 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8612 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8613 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8614 struct bfd_link_order *lo;
8615 bfd_vma r_sym_mask;
3410fea8 8616 bfd_boolean use_rela;
c152c796 8617
3410fea8
NC
8618 /* Find a dynamic reloc section. */
8619 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8620 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8621 if (rela_dyn != NULL && rela_dyn->size > 0
8622 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8623 {
3410fea8
NC
8624 bfd_boolean use_rela_initialised = FALSE;
8625
8626 /* This is just here to stop gcc from complaining.
c8e44c6d 8627 Its initialization checking code is not perfect. */
3410fea8
NC
8628 use_rela = TRUE;
8629
8630 /* Both sections are present. Examine the sizes
8631 of the indirect sections to help us choose. */
8632 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8633 if (lo->type == bfd_indirect_link_order)
8634 {
8635 asection *o = lo->u.indirect.section;
8636
8637 if ((o->size % bed->s->sizeof_rela) == 0)
8638 {
8639 if ((o->size % bed->s->sizeof_rel) == 0)
8640 /* Section size is divisible by both rel and rela sizes.
8641 It is of no help to us. */
8642 ;
8643 else
8644 {
8645 /* Section size is only divisible by rela. */
8646 if (use_rela_initialised && (use_rela == FALSE))
8647 {
c8e44c6d
AM
8648 _bfd_error_handler (_("%B: Unable to sort relocs - "
8649 "they are in more than one size"),
8650 abfd);
3410fea8
NC
8651 bfd_set_error (bfd_error_invalid_operation);
8652 return 0;
8653 }
8654 else
8655 {
8656 use_rela = TRUE;
8657 use_rela_initialised = TRUE;
8658 }
8659 }
8660 }
8661 else if ((o->size % bed->s->sizeof_rel) == 0)
8662 {
8663 /* Section size is only divisible by rel. */
8664 if (use_rela_initialised && (use_rela == TRUE))
8665 {
c8e44c6d
AM
8666 _bfd_error_handler (_("%B: Unable to sort relocs - "
8667 "they are in more than one size"),
8668 abfd);
3410fea8
NC
8669 bfd_set_error (bfd_error_invalid_operation);
8670 return 0;
8671 }
8672 else
8673 {
8674 use_rela = FALSE;
8675 use_rela_initialised = TRUE;
8676 }
8677 }
8678 else
8679 {
c8e44c6d
AM
8680 /* The section size is not divisible by either -
8681 something is wrong. */
8682 _bfd_error_handler (_("%B: Unable to sort relocs - "
8683 "they are of an unknown size"), abfd);
3410fea8
NC
8684 bfd_set_error (bfd_error_invalid_operation);
8685 return 0;
8686 }
8687 }
8688
8689 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8690 if (lo->type == bfd_indirect_link_order)
8691 {
8692 asection *o = lo->u.indirect.section;
8693
8694 if ((o->size % bed->s->sizeof_rela) == 0)
8695 {
8696 if ((o->size % bed->s->sizeof_rel) == 0)
8697 /* Section size is divisible by both rel and rela sizes.
8698 It is of no help to us. */
8699 ;
8700 else
8701 {
8702 /* Section size is only divisible by rela. */
8703 if (use_rela_initialised && (use_rela == FALSE))
8704 {
c8e44c6d
AM
8705 _bfd_error_handler (_("%B: Unable to sort relocs - "
8706 "they are in more than one size"),
8707 abfd);
3410fea8
NC
8708 bfd_set_error (bfd_error_invalid_operation);
8709 return 0;
8710 }
8711 else
8712 {
8713 use_rela = TRUE;
8714 use_rela_initialised = TRUE;
8715 }
8716 }
8717 }
8718 else if ((o->size % bed->s->sizeof_rel) == 0)
8719 {
8720 /* Section size is only divisible by rel. */
8721 if (use_rela_initialised && (use_rela == TRUE))
8722 {
c8e44c6d
AM
8723 _bfd_error_handler (_("%B: Unable to sort relocs - "
8724 "they are in more than one size"),
8725 abfd);
3410fea8
NC
8726 bfd_set_error (bfd_error_invalid_operation);
8727 return 0;
8728 }
8729 else
8730 {
8731 use_rela = FALSE;
8732 use_rela_initialised = TRUE;
8733 }
8734 }
8735 else
8736 {
c8e44c6d
AM
8737 /* The section size is not divisible by either -
8738 something is wrong. */
8739 _bfd_error_handler (_("%B: Unable to sort relocs - "
8740 "they are of an unknown size"), abfd);
3410fea8
NC
8741 bfd_set_error (bfd_error_invalid_operation);
8742 return 0;
8743 }
8744 }
8745
8746 if (! use_rela_initialised)
8747 /* Make a guess. */
8748 use_rela = TRUE;
c152c796 8749 }
fc66a176
L
8750 else if (rela_dyn != NULL && rela_dyn->size > 0)
8751 use_rela = TRUE;
8752 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8753 use_rela = FALSE;
c152c796 8754 else
fc66a176 8755 return 0;
3410fea8
NC
8756
8757 if (use_rela)
c152c796 8758 {
3410fea8 8759 dynamic_relocs = rela_dyn;
c152c796
AM
8760 ext_size = bed->s->sizeof_rela;
8761 swap_in = bed->s->swap_reloca_in;
8762 swap_out = bed->s->swap_reloca_out;
8763 }
3410fea8
NC
8764 else
8765 {
8766 dynamic_relocs = rel_dyn;
8767 ext_size = bed->s->sizeof_rel;
8768 swap_in = bed->s->swap_reloc_in;
8769 swap_out = bed->s->swap_reloc_out;
8770 }
c152c796
AM
8771
8772 size = 0;
3410fea8 8773 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8774 if (lo->type == bfd_indirect_link_order)
3410fea8 8775 size += lo->u.indirect.section->size;
c152c796 8776
3410fea8 8777 if (size != dynamic_relocs->size)
c152c796
AM
8778 return 0;
8779
8780 sort_elt = (sizeof (struct elf_link_sort_rela)
8781 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8782
8783 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8784 if (count == 0)
8785 return 0;
a50b1753 8786 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8787
c152c796
AM
8788 if (sort == NULL)
8789 {
8790 (*info->callbacks->warning)
8791 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8792 return 0;
8793 }
8794
8795 if (bed->s->arch_size == 32)
8796 r_sym_mask = ~(bfd_vma) 0xff;
8797 else
8798 r_sym_mask = ~(bfd_vma) 0xffffffff;
8799
3410fea8 8800 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8801 if (lo->type == bfd_indirect_link_order)
8802 {
8803 bfd_byte *erel, *erelend;
8804 asection *o = lo->u.indirect.section;
8805
1da212d6
AM
8806 if (o->contents == NULL && o->size != 0)
8807 {
8808 /* This is a reloc section that is being handled as a normal
8809 section. See bfd_section_from_shdr. We can't combine
8810 relocs in this case. */
8811 free (sort);
8812 return 0;
8813 }
c152c796 8814 erel = o->contents;
eea6121a 8815 erelend = o->contents + o->size;
c8e44c6d 8816 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 8817
c152c796
AM
8818 while (erel < erelend)
8819 {
8820 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8821
c152c796 8822 (*swap_in) (abfd, erel, s->rela);
7e612e98 8823 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8824 s->u.sym_mask = r_sym_mask;
8825 p += sort_elt;
8826 erel += ext_size;
8827 }
8828 }
8829
8830 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8831
8832 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8833 {
8834 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8835 if (s->type != reloc_class_relative)
8836 break;
8837 }
8838 ret = i;
8839 s_non_relative = p;
8840
8841 sq = (struct elf_link_sort_rela *) s_non_relative;
8842 for (; i < count; i++, p += sort_elt)
8843 {
8844 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8845 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8846 sq = sp;
8847 sp->u.offset = sq->rela->r_offset;
8848 }
8849
8850 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8851
c8e44c6d
AM
8852 struct elf_link_hash_table *htab = elf_hash_table (info);
8853 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8854 {
8855 /* We have plt relocs in .rela.dyn. */
8856 sq = (struct elf_link_sort_rela *) sort;
8857 for (i = 0; i < count; i++)
8858 if (sq[count - i - 1].type != reloc_class_plt)
8859 break;
8860 if (i != 0 && htab->srelplt->size == i * ext_size)
8861 {
8862 struct bfd_link_order **plo;
8863 /* Put srelplt link_order last. This is so the output_offset
8864 set in the next loop is correct for DT_JMPREL. */
8865 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8866 if ((*plo)->type == bfd_indirect_link_order
8867 && (*plo)->u.indirect.section == htab->srelplt)
8868 {
8869 lo = *plo;
8870 *plo = lo->next;
8871 }
8872 else
8873 plo = &(*plo)->next;
8874 *plo = lo;
8875 lo->next = NULL;
8876 dynamic_relocs->map_tail.link_order = lo;
8877 }
8878 }
8879
8880 p = sort;
3410fea8 8881 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8882 if (lo->type == bfd_indirect_link_order)
8883 {
8884 bfd_byte *erel, *erelend;
8885 asection *o = lo->u.indirect.section;
8886
8887 erel = o->contents;
eea6121a 8888 erelend = o->contents + o->size;
c8e44c6d 8889 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
8890 while (erel < erelend)
8891 {
8892 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8893 (*swap_out) (abfd, s->rela, erel);
8894 p += sort_elt;
8895 erel += ext_size;
8896 }
8897 }
8898
8899 free (sort);
3410fea8 8900 *psec = dynamic_relocs;
c152c796
AM
8901 return ret;
8902}
8903
ef10c3ac 8904/* Add a symbol to the output symbol string table. */
c152c796 8905
6e0b88f1 8906static int
ef10c3ac
L
8907elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8908 const char *name,
8909 Elf_Internal_Sym *elfsym,
8910 asection *input_sec,
8911 struct elf_link_hash_entry *h)
c152c796 8912{
6e0b88f1 8913 int (*output_symbol_hook)
c152c796
AM
8914 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8915 struct elf_link_hash_entry *);
ef10c3ac 8916 struct elf_link_hash_table *hash_table;
c152c796 8917 const struct elf_backend_data *bed;
ef10c3ac 8918 bfd_size_type strtabsize;
c152c796 8919
8539e4e8
AM
8920 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8921
8b127cbc 8922 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
8923 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8924 if (output_symbol_hook != NULL)
8925 {
8b127cbc 8926 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
8927 if (ret != 1)
8928 return ret;
c152c796
AM
8929 }
8930
ef10c3ac
L
8931 if (name == NULL
8932 || *name == '\0'
8933 || (input_sec->flags & SEC_EXCLUDE))
8934 elfsym->st_name = (unsigned long) -1;
c152c796
AM
8935 else
8936 {
ef10c3ac
L
8937 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8938 to get the final offset for st_name. */
8939 elfsym->st_name
8940 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8941 name, FALSE);
c152c796 8942 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8943 return 0;
c152c796
AM
8944 }
8945
ef10c3ac
L
8946 hash_table = elf_hash_table (flinfo->info);
8947 strtabsize = hash_table->strtabsize;
8948 if (strtabsize <= hash_table->strtabcount)
c152c796 8949 {
ef10c3ac
L
8950 strtabsize += strtabsize;
8951 hash_table->strtabsize = strtabsize;
8952 strtabsize *= sizeof (*hash_table->strtab);
8953 hash_table->strtab
8954 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8955 strtabsize);
8956 if (hash_table->strtab == NULL)
6e0b88f1 8957 return 0;
c152c796 8958 }
ef10c3ac
L
8959 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8960 hash_table->strtab[hash_table->strtabcount].dest_index
8961 = hash_table->strtabcount;
8962 hash_table->strtab[hash_table->strtabcount].destshndx_index
8963 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8964
8965 bfd_get_symcount (flinfo->output_bfd) += 1;
8966 hash_table->strtabcount += 1;
8967
8968 return 1;
8969}
8970
8971/* Swap symbols out to the symbol table and flush the output symbols to
8972 the file. */
8973
8974static bfd_boolean
8975elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8976{
8977 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
8978 bfd_size_type amt;
8979 size_t i;
ef10c3ac
L
8980 const struct elf_backend_data *bed;
8981 bfd_byte *symbuf;
8982 Elf_Internal_Shdr *hdr;
8983 file_ptr pos;
8984 bfd_boolean ret;
8985
8986 if (!hash_table->strtabcount)
8987 return TRUE;
8988
8989 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8990
8991 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 8992
ef10c3ac
L
8993 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8994 symbuf = (bfd_byte *) bfd_malloc (amt);
8995 if (symbuf == NULL)
8996 return FALSE;
1b786873 8997
ef10c3ac 8998 if (flinfo->symshndxbuf)
c152c796 8999 {
ef53be89
AM
9000 amt = sizeof (Elf_External_Sym_Shndx);
9001 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9002 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9003 if (flinfo->symshndxbuf == NULL)
c152c796 9004 {
ef10c3ac
L
9005 free (symbuf);
9006 return FALSE;
c152c796 9007 }
c152c796
AM
9008 }
9009
ef10c3ac
L
9010 for (i = 0; i < hash_table->strtabcount; i++)
9011 {
9012 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9013 if (elfsym->sym.st_name == (unsigned long) -1)
9014 elfsym->sym.st_name = 0;
9015 else
9016 elfsym->sym.st_name
9017 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9018 elfsym->sym.st_name);
9019 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9020 ((bfd_byte *) symbuf
9021 + (elfsym->dest_index
9022 * bed->s->sizeof_sym)),
9023 (flinfo->symshndxbuf
9024 + elfsym->destshndx_index));
9025 }
9026
9027 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9028 pos = hdr->sh_offset + hdr->sh_size;
9029 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9030 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9031 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9032 {
9033 hdr->sh_size += amt;
9034 ret = TRUE;
9035 }
9036 else
9037 ret = FALSE;
c152c796 9038
ef10c3ac
L
9039 free (symbuf);
9040
9041 free (hash_table->strtab);
9042 hash_table->strtab = NULL;
9043
9044 return ret;
c152c796
AM
9045}
9046
c0d5a53d
L
9047/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9048
9049static bfd_boolean
9050check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9051{
4fbb74a6
AM
9052 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9053 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9054 {
9055 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9056 beyond 64k. */
4eca0228 9057 _bfd_error_handler
695344c0 9058 /* xgettext:c-format */
c0d5a53d 9059 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9060 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9061 bfd_set_error (bfd_error_nonrepresentable_section);
9062 return FALSE;
9063 }
9064 return TRUE;
9065}
9066
c152c796
AM
9067/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9068 allowing an unsatisfied unversioned symbol in the DSO to match a
9069 versioned symbol that would normally require an explicit version.
9070 We also handle the case that a DSO references a hidden symbol
9071 which may be satisfied by a versioned symbol in another DSO. */
9072
9073static bfd_boolean
9074elf_link_check_versioned_symbol (struct bfd_link_info *info,
9075 const struct elf_backend_data *bed,
9076 struct elf_link_hash_entry *h)
9077{
9078 bfd *abfd;
9079 struct elf_link_loaded_list *loaded;
9080
9081 if (!is_elf_hash_table (info->hash))
9082 return FALSE;
9083
90c984fc
L
9084 /* Check indirect symbol. */
9085 while (h->root.type == bfd_link_hash_indirect)
9086 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9087
c152c796
AM
9088 switch (h->root.type)
9089 {
9090 default:
9091 abfd = NULL;
9092 break;
9093
9094 case bfd_link_hash_undefined:
9095 case bfd_link_hash_undefweak:
9096 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9097 if (abfd == NULL
9098 || (abfd->flags & DYNAMIC) == 0
e56f61be 9099 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9100 return FALSE;
9101 break;
9102
9103 case bfd_link_hash_defined:
9104 case bfd_link_hash_defweak:
9105 abfd = h->root.u.def.section->owner;
9106 break;
9107
9108 case bfd_link_hash_common:
9109 abfd = h->root.u.c.p->section->owner;
9110 break;
9111 }
9112 BFD_ASSERT (abfd != NULL);
9113
9114 for (loaded = elf_hash_table (info)->loaded;
9115 loaded != NULL;
9116 loaded = loaded->next)
9117 {
9118 bfd *input;
9119 Elf_Internal_Shdr *hdr;
ef53be89
AM
9120 size_t symcount;
9121 size_t extsymcount;
9122 size_t extsymoff;
c152c796
AM
9123 Elf_Internal_Shdr *versymhdr;
9124 Elf_Internal_Sym *isym;
9125 Elf_Internal_Sym *isymend;
9126 Elf_Internal_Sym *isymbuf;
9127 Elf_External_Versym *ever;
9128 Elf_External_Versym *extversym;
9129
9130 input = loaded->abfd;
9131
9132 /* We check each DSO for a possible hidden versioned definition. */
9133 if (input == abfd
9134 || (input->flags & DYNAMIC) == 0
9135 || elf_dynversym (input) == 0)
9136 continue;
9137
9138 hdr = &elf_tdata (input)->dynsymtab_hdr;
9139
9140 symcount = hdr->sh_size / bed->s->sizeof_sym;
9141 if (elf_bad_symtab (input))
9142 {
9143 extsymcount = symcount;
9144 extsymoff = 0;
9145 }
9146 else
9147 {
9148 extsymcount = symcount - hdr->sh_info;
9149 extsymoff = hdr->sh_info;
9150 }
9151
9152 if (extsymcount == 0)
9153 continue;
9154
9155 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9156 NULL, NULL, NULL);
9157 if (isymbuf == NULL)
9158 return FALSE;
9159
9160 /* Read in any version definitions. */
9161 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9162 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9163 if (extversym == NULL)
9164 goto error_ret;
9165
9166 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9167 || (bfd_bread (extversym, versymhdr->sh_size, input)
9168 != versymhdr->sh_size))
9169 {
9170 free (extversym);
9171 error_ret:
9172 free (isymbuf);
9173 return FALSE;
9174 }
9175
9176 ever = extversym + extsymoff;
9177 isymend = isymbuf + extsymcount;
9178 for (isym = isymbuf; isym < isymend; isym++, ever++)
9179 {
9180 const char *name;
9181 Elf_Internal_Versym iver;
9182 unsigned short version_index;
9183
9184 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9185 || isym->st_shndx == SHN_UNDEF)
9186 continue;
9187
9188 name = bfd_elf_string_from_elf_section (input,
9189 hdr->sh_link,
9190 isym->st_name);
9191 if (strcmp (name, h->root.root.string) != 0)
9192 continue;
9193
9194 _bfd_elf_swap_versym_in (input, ever, &iver);
9195
d023c380
L
9196 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9197 && !(h->def_regular
9198 && h->forced_local))
c152c796
AM
9199 {
9200 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9201 have provided a definition for the undefined sym unless
9202 it is defined in a non-shared object and forced local.
9203 */
c152c796
AM
9204 abort ();
9205 }
9206
9207 version_index = iver.vs_vers & VERSYM_VERSION;
9208 if (version_index == 1 || version_index == 2)
9209 {
9210 /* This is the base or first version. We can use it. */
9211 free (extversym);
9212 free (isymbuf);
9213 return TRUE;
9214 }
9215 }
9216
9217 free (extversym);
9218 free (isymbuf);
9219 }
9220
9221 return FALSE;
9222}
9223
b8871f35
L
9224/* Convert ELF common symbol TYPE. */
9225
9226static int
9227elf_link_convert_common_type (struct bfd_link_info *info, int type)
9228{
9229 /* Commom symbol can only appear in relocatable link. */
9230 if (!bfd_link_relocatable (info))
9231 abort ();
9232 switch (info->elf_stt_common)
9233 {
9234 case unchanged:
9235 break;
9236 case elf_stt_common:
9237 type = STT_COMMON;
9238 break;
9239 case no_elf_stt_common:
9240 type = STT_OBJECT;
9241 break;
9242 }
9243 return type;
9244}
9245
c152c796
AM
9246/* Add an external symbol to the symbol table. This is called from
9247 the hash table traversal routine. When generating a shared object,
9248 we go through the symbol table twice. The first time we output
9249 anything that might have been forced to local scope in a version
9250 script. The second time we output the symbols that are still
9251 global symbols. */
9252
9253static bfd_boolean
7686d77d 9254elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9255{
7686d77d 9256 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9257 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9258 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9259 bfd_boolean strip;
9260 Elf_Internal_Sym sym;
9261 asection *input_sec;
9262 const struct elf_backend_data *bed;
6e0b88f1
AM
9263 long indx;
9264 int ret;
b8871f35 9265 unsigned int type;
c152c796
AM
9266
9267 if (h->root.type == bfd_link_hash_warning)
9268 {
9269 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9270 if (h->root.type == bfd_link_hash_new)
9271 return TRUE;
9272 }
9273
9274 /* Decide whether to output this symbol in this pass. */
9275 if (eoinfo->localsyms)
9276 {
4deb8f71 9277 if (!h->forced_local)
c152c796
AM
9278 return TRUE;
9279 }
9280 else
9281 {
4deb8f71 9282 if (h->forced_local)
c152c796
AM
9283 return TRUE;
9284 }
9285
8b127cbc 9286 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9287
12ac1cf5 9288 if (h->root.type == bfd_link_hash_undefined)
c152c796 9289 {
12ac1cf5
NC
9290 /* If we have an undefined symbol reference here then it must have
9291 come from a shared library that is being linked in. (Undefined
98da7939
L
9292 references in regular files have already been handled unless
9293 they are in unreferenced sections which are removed by garbage
9294 collection). */
12ac1cf5
NC
9295 bfd_boolean ignore_undef = FALSE;
9296
9297 /* Some symbols may be special in that the fact that they're
9298 undefined can be safely ignored - let backend determine that. */
9299 if (bed->elf_backend_ignore_undef_symbol)
9300 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9301
9302 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9303 if (!ignore_undef
12ac1cf5 9304 && h->ref_dynamic
8b127cbc
AM
9305 && (!h->ref_regular || flinfo->info->gc_sections)
9306 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9307 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9308 (*flinfo->info->callbacks->undefined_symbol)
9309 (flinfo->info, h->root.root.string,
9310 h->ref_regular ? NULL : h->root.u.undef.abfd,
9311 NULL, 0,
9312 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9313
9314 /* Strip a global symbol defined in a discarded section. */
9315 if (h->indx == -3)
9316 return TRUE;
c152c796
AM
9317 }
9318
9319 /* We should also warn if a forced local symbol is referenced from
9320 shared libraries. */
0e1862bb 9321 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9322 && h->forced_local
9323 && h->ref_dynamic
371a5866 9324 && h->def_regular
f5385ebf 9325 && !h->dynamic_def
ee659f1f 9326 && h->ref_dynamic_nonweak
8b127cbc 9327 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9328 {
17d078c5
AM
9329 bfd *def_bfd;
9330 const char *msg;
90c984fc
L
9331 struct elf_link_hash_entry *hi = h;
9332
9333 /* Check indirect symbol. */
9334 while (hi->root.type == bfd_link_hash_indirect)
9335 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9336
9337 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9338 /* xgettext:c-format */
17d078c5
AM
9339 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9340 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9341 /* xgettext:c-format */
17d078c5
AM
9342 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9343 else
695344c0 9344 /* xgettext:c-format */
17d078c5 9345 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9346 def_bfd = flinfo->output_bfd;
90c984fc
L
9347 if (hi->root.u.def.section != bfd_abs_section_ptr)
9348 def_bfd = hi->root.u.def.section->owner;
4eca0228
AM
9349 _bfd_error_handler (msg, flinfo->output_bfd, def_bfd,
9350 h->root.root.string);
17d078c5 9351 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9352 eoinfo->failed = TRUE;
9353 return FALSE;
9354 }
9355
9356 /* We don't want to output symbols that have never been mentioned by
9357 a regular file, or that we have been told to strip. However, if
9358 h->indx is set to -2, the symbol is used by a reloc and we must
9359 output it. */
d983c8c5 9360 strip = FALSE;
c152c796 9361 if (h->indx == -2)
d983c8c5 9362 ;
f5385ebf 9363 else if ((h->def_dynamic
77cfaee6
AM
9364 || h->ref_dynamic
9365 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9366 && !h->def_regular
9367 && !h->ref_regular)
c152c796 9368 strip = TRUE;
8b127cbc 9369 else if (flinfo->info->strip == strip_all)
c152c796 9370 strip = TRUE;
8b127cbc
AM
9371 else if (flinfo->info->strip == strip_some
9372 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9373 h->root.root.string, FALSE, FALSE) == NULL)
9374 strip = TRUE;
d56d55e7
AM
9375 else if ((h->root.type == bfd_link_hash_defined
9376 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9377 && ((flinfo->info->strip_discarded
dbaa2011 9378 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9379 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9380 && h->root.u.def.section->owner != NULL
d56d55e7 9381 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9382 strip = TRUE;
9e2278f5
AM
9383 else if ((h->root.type == bfd_link_hash_undefined
9384 || h->root.type == bfd_link_hash_undefweak)
9385 && h->root.u.undef.abfd != NULL
9386 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9387 strip = TRUE;
c152c796 9388
b8871f35
L
9389 type = h->type;
9390
c152c796 9391 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9392 nothing else to do. However, if it is a forced local symbol or
9393 an ifunc symbol we need to give the backend finish_dynamic_symbol
9394 function a chance to make it dynamic. */
c152c796
AM
9395 if (strip
9396 && h->dynindx == -1
b8871f35 9397 && type != STT_GNU_IFUNC
f5385ebf 9398 && !h->forced_local)
c152c796
AM
9399 return TRUE;
9400
9401 sym.st_value = 0;
9402 sym.st_size = h->size;
9403 sym.st_other = h->other;
c152c796
AM
9404 switch (h->root.type)
9405 {
9406 default:
9407 case bfd_link_hash_new:
9408 case bfd_link_hash_warning:
9409 abort ();
9410 return FALSE;
9411
9412 case bfd_link_hash_undefined:
9413 case bfd_link_hash_undefweak:
9414 input_sec = bfd_und_section_ptr;
9415 sym.st_shndx = SHN_UNDEF;
9416 break;
9417
9418 case bfd_link_hash_defined:
9419 case bfd_link_hash_defweak:
9420 {
9421 input_sec = h->root.u.def.section;
9422 if (input_sec->output_section != NULL)
9423 {
9424 sym.st_shndx =
8b127cbc 9425 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9426 input_sec->output_section);
9427 if (sym.st_shndx == SHN_BAD)
9428 {
4eca0228 9429 _bfd_error_handler
695344c0 9430 /* xgettext:c-format */
d003868e 9431 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9432 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9433 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9434 eoinfo->failed = TRUE;
9435 return FALSE;
9436 }
9437
9438 /* ELF symbols in relocatable files are section relative,
9439 but in nonrelocatable files they are virtual
9440 addresses. */
9441 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9442 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9443 {
9444 sym.st_value += input_sec->output_section->vma;
9445 if (h->type == STT_TLS)
9446 {
8b127cbc 9447 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9448 if (tls_sec != NULL)
9449 sym.st_value -= tls_sec->vma;
c152c796
AM
9450 }
9451 }
9452 }
9453 else
9454 {
9455 BFD_ASSERT (input_sec->owner == NULL
9456 || (input_sec->owner->flags & DYNAMIC) != 0);
9457 sym.st_shndx = SHN_UNDEF;
9458 input_sec = bfd_und_section_ptr;
9459 }
9460 }
9461 break;
9462
9463 case bfd_link_hash_common:
9464 input_sec = h->root.u.c.p->section;
a4d8e49b 9465 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9466 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9467 break;
9468
9469 case bfd_link_hash_indirect:
9470 /* These symbols are created by symbol versioning. They point
9471 to the decorated version of the name. For example, if the
9472 symbol foo@@GNU_1.2 is the default, which should be used when
9473 foo is used with no version, then we add an indirect symbol
9474 foo which points to foo@@GNU_1.2. We ignore these symbols,
9475 since the indirected symbol is already in the hash table. */
9476 return TRUE;
9477 }
9478
b8871f35
L
9479 if (type == STT_COMMON || type == STT_OBJECT)
9480 switch (h->root.type)
9481 {
9482 case bfd_link_hash_common:
9483 type = elf_link_convert_common_type (flinfo->info, type);
9484 break;
9485 case bfd_link_hash_defined:
9486 case bfd_link_hash_defweak:
9487 if (bed->common_definition (&sym))
9488 type = elf_link_convert_common_type (flinfo->info, type);
9489 else
9490 type = STT_OBJECT;
9491 break;
9492 case bfd_link_hash_undefined:
9493 case bfd_link_hash_undefweak:
9494 break;
9495 default:
9496 abort ();
9497 }
9498
4deb8f71 9499 if (h->forced_local)
b8871f35
L
9500 {
9501 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9502 /* Turn off visibility on local symbol. */
9503 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9504 }
9505 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9506 else if (h->unique_global && h->def_regular)
9507 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9508 else if (h->root.type == bfd_link_hash_undefweak
9509 || h->root.type == bfd_link_hash_defweak)
9510 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9511 else
9512 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9513 sym.st_target_internal = h->target_internal;
9514
c152c796
AM
9515 /* Give the processor backend a chance to tweak the symbol value,
9516 and also to finish up anything that needs to be done for this
9517 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9518 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9519 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9520 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9521 && h->def_regular
0e1862bb 9522 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9523 || ((h->dynindx != -1
9524 || h->forced_local)
0e1862bb 9525 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9526 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9527 || h->root.type != bfd_link_hash_undefweak))
9528 || !h->forced_local)
8b127cbc 9529 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9530 {
9531 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9532 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9533 {
9534 eoinfo->failed = TRUE;
9535 return FALSE;
9536 }
9537 }
9538
9539 /* If we are marking the symbol as undefined, and there are no
9540 non-weak references to this symbol from a regular object, then
9541 mark the symbol as weak undefined; if there are non-weak
9542 references, mark the symbol as strong. We can't do this earlier,
9543 because it might not be marked as undefined until the
9544 finish_dynamic_symbol routine gets through with it. */
9545 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9546 && h->ref_regular
c152c796
AM
9547 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9548 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9549 {
9550 int bindtype;
b8871f35 9551 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9552
9553 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9554 if (type == STT_GNU_IFUNC)
9555 type = STT_FUNC;
c152c796 9556
f5385ebf 9557 if (h->ref_regular_nonweak)
c152c796
AM
9558 bindtype = STB_GLOBAL;
9559 else
9560 bindtype = STB_WEAK;
2955ec4c 9561 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9562 }
9563
bda987c2
CD
9564 /* If this is a symbol defined in a dynamic library, don't use the
9565 symbol size from the dynamic library. Relinking an executable
9566 against a new library may introduce gratuitous changes in the
9567 executable's symbols if we keep the size. */
9568 if (sym.st_shndx == SHN_UNDEF
9569 && !h->def_regular
9570 && h->def_dynamic)
9571 sym.st_size = 0;
9572
c152c796
AM
9573 /* If a non-weak symbol with non-default visibility is not defined
9574 locally, it is a fatal error. */
0e1862bb 9575 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9576 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9577 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9578 && h->root.type == bfd_link_hash_undefined
f5385ebf 9579 && !h->def_regular)
c152c796 9580 {
17d078c5
AM
9581 const char *msg;
9582
9583 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 9584 /* xgettext:c-format */
17d078c5
AM
9585 msg = _("%B: protected symbol `%s' isn't defined");
9586 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 9587 /* xgettext:c-format */
17d078c5
AM
9588 msg = _("%B: internal symbol `%s' isn't defined");
9589 else
695344c0 9590 /* xgettext:c-format */
17d078c5 9591 msg = _("%B: hidden symbol `%s' isn't defined");
4eca0228 9592 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9593 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9594 eoinfo->failed = TRUE;
9595 return FALSE;
9596 }
9597
9598 /* If this symbol should be put in the .dynsym section, then put it
9599 there now. We already know the symbol index. We also fill in
9600 the entry in the .hash section. */
cae1fbbb 9601 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9602 && h->dynindx != -1
8b127cbc 9603 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9604 {
c152c796
AM
9605 bfd_byte *esym;
9606
90c984fc
L
9607 /* Since there is no version information in the dynamic string,
9608 if there is no version info in symbol version section, we will
1659f720 9609 have a run-time problem if not linking executable, referenced
4deb8f71 9610 by shared library, or not bound locally. */
1659f720 9611 if (h->verinfo.verdef == NULL
0e1862bb 9612 && (!bfd_link_executable (flinfo->info)
1659f720
L
9613 || h->ref_dynamic
9614 || !h->def_regular))
90c984fc
L
9615 {
9616 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9617
9618 if (p && p [1] != '\0')
9619 {
4eca0228 9620 _bfd_error_handler
695344c0 9621 /* xgettext:c-format */
90c984fc
L
9622 (_("%B: No symbol version section for versioned symbol `%s'"),
9623 flinfo->output_bfd, h->root.root.string);
9624 eoinfo->failed = TRUE;
9625 return FALSE;
9626 }
9627 }
9628
c152c796 9629 sym.st_name = h->dynstr_index;
cae1fbbb
L
9630 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9631 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9632 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9633 {
9634 eoinfo->failed = TRUE;
9635 return FALSE;
9636 }
8b127cbc 9637 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9638
8b127cbc 9639 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9640 {
9641 size_t hash_entry_size;
9642 bfd_byte *bucketpos;
9643 bfd_vma chain;
41198d0c
L
9644 size_t bucketcount;
9645 size_t bucket;
9646
8b127cbc 9647 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9648 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9649
9650 hash_entry_size
8b127cbc
AM
9651 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9652 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9653 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9654 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9655 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9656 bucketpos);
9657 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9658 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9659 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9660 }
c152c796 9661
8b127cbc 9662 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9663 {
9664 Elf_Internal_Versym iversym;
9665 Elf_External_Versym *eversym;
9666
f5385ebf 9667 if (!h->def_regular)
c152c796 9668 {
7b20f099
AM
9669 if (h->verinfo.verdef == NULL
9670 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9671 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9672 iversym.vs_vers = 0;
9673 else
9674 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9675 }
9676 else
9677 {
9678 if (h->verinfo.vertree == NULL)
9679 iversym.vs_vers = 1;
9680 else
9681 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9682 if (flinfo->info->create_default_symver)
3e3b46e5 9683 iversym.vs_vers++;
c152c796
AM
9684 }
9685
422f1182 9686 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9687 defined locally. */
422f1182 9688 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9689 iversym.vs_vers |= VERSYM_HIDDEN;
9690
8b127cbc 9691 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9692 eversym += h->dynindx;
8b127cbc 9693 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9694 }
9695 }
9696
d983c8c5
AM
9697 /* If the symbol is undefined, and we didn't output it to .dynsym,
9698 strip it from .symtab too. Obviously we can't do this for
9699 relocatable output or when needed for --emit-relocs. */
9700 else if (input_sec == bfd_und_section_ptr
9701 && h->indx != -2
0e1862bb 9702 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9703 return TRUE;
9704 /* Also strip others that we couldn't earlier due to dynamic symbol
9705 processing. */
9706 if (strip)
9707 return TRUE;
9708 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9709 return TRUE;
9710
2ec55de3
AM
9711 /* Output a FILE symbol so that following locals are not associated
9712 with the wrong input file. We need one for forced local symbols
9713 if we've seen more than one FILE symbol or when we have exactly
9714 one FILE symbol but global symbols are present in a file other
9715 than the one with the FILE symbol. We also need one if linker
9716 defined symbols are present. In practice these conditions are
9717 always met, so just emit the FILE symbol unconditionally. */
9718 if (eoinfo->localsyms
9719 && !eoinfo->file_sym_done
9720 && eoinfo->flinfo->filesym_count != 0)
9721 {
9722 Elf_Internal_Sym fsym;
9723
9724 memset (&fsym, 0, sizeof (fsym));
9725 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9726 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9727 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9728 bfd_und_section_ptr, NULL))
2ec55de3
AM
9729 return FALSE;
9730
9731 eoinfo->file_sym_done = TRUE;
9732 }
9733
8b127cbc 9734 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9735 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9736 input_sec, h);
6e0b88f1 9737 if (ret == 0)
c152c796
AM
9738 {
9739 eoinfo->failed = TRUE;
9740 return FALSE;
9741 }
6e0b88f1
AM
9742 else if (ret == 1)
9743 h->indx = indx;
9744 else if (h->indx == -2)
9745 abort();
c152c796
AM
9746
9747 return TRUE;
9748}
9749
cdd3575c
AM
9750/* Return TRUE if special handling is done for relocs in SEC against
9751 symbols defined in discarded sections. */
9752
c152c796
AM
9753static bfd_boolean
9754elf_section_ignore_discarded_relocs (asection *sec)
9755{
9756 const struct elf_backend_data *bed;
9757
cdd3575c
AM
9758 switch (sec->sec_info_type)
9759 {
dbaa2011
AM
9760 case SEC_INFO_TYPE_STABS:
9761 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9762 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9763 return TRUE;
9764 default:
9765 break;
9766 }
c152c796
AM
9767
9768 bed = get_elf_backend_data (sec->owner);
9769 if (bed->elf_backend_ignore_discarded_relocs != NULL
9770 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9771 return TRUE;
9772
9773 return FALSE;
9774}
9775
9e66c942
AM
9776/* Return a mask saying how ld should treat relocations in SEC against
9777 symbols defined in discarded sections. If this function returns
9778 COMPLAIN set, ld will issue a warning message. If this function
9779 returns PRETEND set, and the discarded section was link-once and the
9780 same size as the kept link-once section, ld will pretend that the
9781 symbol was actually defined in the kept section. Otherwise ld will
9782 zero the reloc (at least that is the intent, but some cooperation by
9783 the target dependent code is needed, particularly for REL targets). */
9784
8a696751
AM
9785unsigned int
9786_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9787{
9e66c942 9788 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9789 return PRETEND;
cdd3575c
AM
9790
9791 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9792 return 0;
cdd3575c
AM
9793
9794 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9795 return 0;
cdd3575c 9796
9e66c942 9797 return COMPLAIN | PRETEND;
cdd3575c
AM
9798}
9799
3d7f7666
L
9800/* Find a match between a section and a member of a section group. */
9801
9802static asection *
c0f00686
L
9803match_group_member (asection *sec, asection *group,
9804 struct bfd_link_info *info)
3d7f7666
L
9805{
9806 asection *first = elf_next_in_group (group);
9807 asection *s = first;
9808
9809 while (s != NULL)
9810 {
c0f00686 9811 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9812 return s;
9813
83180ade 9814 s = elf_next_in_group (s);
3d7f7666
L
9815 if (s == first)
9816 break;
9817 }
9818
9819 return NULL;
9820}
9821
01b3c8ab 9822/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9823 to replace it. Return the replacement if it is OK. Otherwise return
9824 NULL. */
01b3c8ab
L
9825
9826asection *
c0f00686 9827_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9828{
9829 asection *kept;
9830
9831 kept = sec->kept_section;
9832 if (kept != NULL)
9833 {
c2370991 9834 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9835 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9836 if (kept != NULL
9837 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9838 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9839 kept = NULL;
c2370991 9840 sec->kept_section = kept;
01b3c8ab
L
9841 }
9842 return kept;
9843}
9844
c152c796
AM
9845/* Link an input file into the linker output file. This function
9846 handles all the sections and relocations of the input file at once.
9847 This is so that we only have to read the local symbols once, and
9848 don't have to keep them in memory. */
9849
9850static bfd_boolean
8b127cbc 9851elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9852{
ece5ef60 9853 int (*relocate_section)
c152c796
AM
9854 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9855 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9856 bfd *output_bfd;
9857 Elf_Internal_Shdr *symtab_hdr;
9858 size_t locsymcount;
9859 size_t extsymoff;
9860 Elf_Internal_Sym *isymbuf;
9861 Elf_Internal_Sym *isym;
9862 Elf_Internal_Sym *isymend;
9863 long *pindex;
9864 asection **ppsection;
9865 asection *o;
9866 const struct elf_backend_data *bed;
c152c796 9867 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9868 bfd_size_type address_size;
9869 bfd_vma r_type_mask;
9870 int r_sym_shift;
ffbc01cc 9871 bfd_boolean have_file_sym = FALSE;
c152c796 9872
8b127cbc 9873 output_bfd = flinfo->output_bfd;
c152c796
AM
9874 bed = get_elf_backend_data (output_bfd);
9875 relocate_section = bed->elf_backend_relocate_section;
9876
9877 /* If this is a dynamic object, we don't want to do anything here:
9878 we don't want the local symbols, and we don't want the section
9879 contents. */
9880 if ((input_bfd->flags & DYNAMIC) != 0)
9881 return TRUE;
9882
c152c796
AM
9883 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9884 if (elf_bad_symtab (input_bfd))
9885 {
9886 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9887 extsymoff = 0;
9888 }
9889 else
9890 {
9891 locsymcount = symtab_hdr->sh_info;
9892 extsymoff = symtab_hdr->sh_info;
9893 }
9894
9895 /* Read the local symbols. */
9896 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9897 if (isymbuf == NULL && locsymcount != 0)
9898 {
9899 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9900 flinfo->internal_syms,
9901 flinfo->external_syms,
9902 flinfo->locsym_shndx);
c152c796
AM
9903 if (isymbuf == NULL)
9904 return FALSE;
9905 }
9906
9907 /* Find local symbol sections and adjust values of symbols in
9908 SEC_MERGE sections. Write out those local symbols we know are
9909 going into the output file. */
9910 isymend = isymbuf + locsymcount;
8b127cbc 9911 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9912 isym < isymend;
9913 isym++, pindex++, ppsection++)
9914 {
9915 asection *isec;
9916 const char *name;
9917 Elf_Internal_Sym osym;
6e0b88f1
AM
9918 long indx;
9919 int ret;
c152c796
AM
9920
9921 *pindex = -1;
9922
9923 if (elf_bad_symtab (input_bfd))
9924 {
9925 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9926 {
9927 *ppsection = NULL;
9928 continue;
9929 }
9930 }
9931
9932 if (isym->st_shndx == SHN_UNDEF)
9933 isec = bfd_und_section_ptr;
c152c796
AM
9934 else if (isym->st_shndx == SHN_ABS)
9935 isec = bfd_abs_section_ptr;
9936 else if (isym->st_shndx == SHN_COMMON)
9937 isec = bfd_com_section_ptr;
9938 else
9939 {
cb33740c
AM
9940 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9941 if (isec == NULL)
9942 {
9943 /* Don't attempt to output symbols with st_shnx in the
9944 reserved range other than SHN_ABS and SHN_COMMON. */
9945 *ppsection = NULL;
9946 continue;
9947 }
dbaa2011 9948 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9949 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9950 isym->st_value =
9951 _bfd_merged_section_offset (output_bfd, &isec,
9952 elf_section_data (isec)->sec_info,
9953 isym->st_value);
c152c796
AM
9954 }
9955
9956 *ppsection = isec;
9957
d983c8c5
AM
9958 /* Don't output the first, undefined, symbol. In fact, don't
9959 output any undefined local symbol. */
9960 if (isec == bfd_und_section_ptr)
c152c796
AM
9961 continue;
9962
9963 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9964 {
9965 /* We never output section symbols. Instead, we use the
9966 section symbol of the corresponding section in the output
9967 file. */
9968 continue;
9969 }
9970
9971 /* If we are stripping all symbols, we don't want to output this
9972 one. */
8b127cbc 9973 if (flinfo->info->strip == strip_all)
c152c796
AM
9974 continue;
9975
9976 /* If we are discarding all local symbols, we don't want to
9977 output this one. If we are generating a relocatable output
9978 file, then some of the local symbols may be required by
9979 relocs; we output them below as we discover that they are
9980 needed. */
8b127cbc 9981 if (flinfo->info->discard == discard_all)
c152c796
AM
9982 continue;
9983
9984 /* If this symbol is defined in a section which we are
f02571c5
AM
9985 discarding, we don't need to keep it. */
9986 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9987 && isym->st_shndx < SHN_LORESERVE
9988 && bfd_section_removed_from_list (output_bfd,
9989 isec->output_section))
e75a280b
L
9990 continue;
9991
c152c796
AM
9992 /* Get the name of the symbol. */
9993 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9994 isym->st_name);
9995 if (name == NULL)
9996 return FALSE;
9997
9998 /* See if we are discarding symbols with this name. */
8b127cbc
AM
9999 if ((flinfo->info->strip == strip_some
10000 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10001 == NULL))
8b127cbc 10002 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10003 && (isec->flags & SEC_MERGE)
10004 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10005 || flinfo->info->discard == discard_l)
c152c796
AM
10006 && bfd_is_local_label_name (input_bfd, name)))
10007 continue;
10008
ffbc01cc
AM
10009 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10010 {
ce875075
AM
10011 if (input_bfd->lto_output)
10012 /* -flto puts a temp file name here. This means builds
10013 are not reproducible. Discard the symbol. */
10014 continue;
ffbc01cc
AM
10015 have_file_sym = TRUE;
10016 flinfo->filesym_count += 1;
10017 }
10018 if (!have_file_sym)
10019 {
10020 /* In the absence of debug info, bfd_find_nearest_line uses
10021 FILE symbols to determine the source file for local
10022 function symbols. Provide a FILE symbol here if input
10023 files lack such, so that their symbols won't be
10024 associated with a previous input file. It's not the
10025 source file, but the best we can do. */
10026 have_file_sym = TRUE;
10027 flinfo->filesym_count += 1;
10028 memset (&osym, 0, sizeof (osym));
10029 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10030 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10031 if (!elf_link_output_symstrtab (flinfo,
10032 (input_bfd->lto_output ? NULL
10033 : input_bfd->filename),
10034 &osym, bfd_abs_section_ptr,
10035 NULL))
ffbc01cc
AM
10036 return FALSE;
10037 }
10038
c152c796
AM
10039 osym = *isym;
10040
10041 /* Adjust the section index for the output file. */
10042 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10043 isec->output_section);
10044 if (osym.st_shndx == SHN_BAD)
10045 return FALSE;
10046
c152c796
AM
10047 /* ELF symbols in relocatable files are section relative, but
10048 in executable files they are virtual addresses. Note that
10049 this code assumes that all ELF sections have an associated
10050 BFD section with a reasonable value for output_offset; below
10051 we assume that they also have a reasonable value for
10052 output_section. Any special sections must be set up to meet
10053 these requirements. */
10054 osym.st_value += isec->output_offset;
0e1862bb 10055 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10056 {
10057 osym.st_value += isec->output_section->vma;
10058 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10059 {
10060 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10061 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10062 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10063 }
10064 }
10065
6e0b88f1 10066 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10067 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10068 if (ret == 0)
c152c796 10069 return FALSE;
6e0b88f1
AM
10070 else if (ret == 1)
10071 *pindex = indx;
c152c796
AM
10072 }
10073
310fd250
L
10074 if (bed->s->arch_size == 32)
10075 {
10076 r_type_mask = 0xff;
10077 r_sym_shift = 8;
10078 address_size = 4;
10079 }
10080 else
10081 {
10082 r_type_mask = 0xffffffff;
10083 r_sym_shift = 32;
10084 address_size = 8;
10085 }
10086
c152c796
AM
10087 /* Relocate the contents of each section. */
10088 sym_hashes = elf_sym_hashes (input_bfd);
10089 for (o = input_bfd->sections; o != NULL; o = o->next)
10090 {
10091 bfd_byte *contents;
10092
10093 if (! o->linker_mark)
10094 {
10095 /* This section was omitted from the link. */
10096 continue;
10097 }
10098
0e1862bb 10099 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
10100 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10101 {
10102 /* Deal with the group signature symbol. */
10103 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10104 unsigned long symndx = sec_data->this_hdr.sh_info;
10105 asection *osec = o->output_section;
10106
10107 if (symndx >= locsymcount
10108 || (elf_bad_symtab (input_bfd)
8b127cbc 10109 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10110 {
10111 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10112 while (h->root.type == bfd_link_hash_indirect
10113 || h->root.type == bfd_link_hash_warning)
10114 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10115 /* Arrange for symbol to be output. */
10116 h->indx = -2;
10117 elf_section_data (osec)->this_hdr.sh_info = -2;
10118 }
10119 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10120 {
10121 /* We'll use the output section target_index. */
8b127cbc 10122 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10123 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10124 }
10125 else
10126 {
8b127cbc 10127 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10128 {
10129 /* Otherwise output the local symbol now. */
10130 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10131 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10132 const char *name;
6e0b88f1
AM
10133 long indx;
10134 int ret;
bcacc0f5
AM
10135
10136 name = bfd_elf_string_from_elf_section (input_bfd,
10137 symtab_hdr->sh_link,
10138 sym.st_name);
10139 if (name == NULL)
10140 return FALSE;
10141
10142 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10143 sec);
10144 if (sym.st_shndx == SHN_BAD)
10145 return FALSE;
10146
10147 sym.st_value += o->output_offset;
10148
6e0b88f1 10149 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10150 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10151 NULL);
6e0b88f1 10152 if (ret == 0)
bcacc0f5 10153 return FALSE;
6e0b88f1 10154 else if (ret == 1)
8b127cbc 10155 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10156 else
10157 abort ();
bcacc0f5
AM
10158 }
10159 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10160 = flinfo->indices[symndx];
bcacc0f5
AM
10161 }
10162 }
10163
c152c796 10164 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10165 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10166 continue;
10167
10168 if ((o->flags & SEC_LINKER_CREATED) != 0)
10169 {
10170 /* Section was created by _bfd_elf_link_create_dynamic_sections
10171 or somesuch. */
10172 continue;
10173 }
10174
10175 /* Get the contents of the section. They have been cached by a
10176 relaxation routine. Note that o is a section in an input
10177 file, so the contents field will not have been set by any of
10178 the routines which work on output files. */
10179 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10180 {
10181 contents = elf_section_data (o)->this_hdr.contents;
10182 if (bed->caches_rawsize
10183 && o->rawsize != 0
10184 && o->rawsize < o->size)
10185 {
10186 memcpy (flinfo->contents, contents, o->rawsize);
10187 contents = flinfo->contents;
10188 }
10189 }
c152c796
AM
10190 else
10191 {
8b127cbc 10192 contents = flinfo->contents;
4a114e3e 10193 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10194 return FALSE;
10195 }
10196
10197 if ((o->flags & SEC_RELOC) != 0)
10198 {
10199 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10200 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10201 int action_discarded;
ece5ef60 10202 int ret;
c152c796
AM
10203
10204 /* Get the swapped relocs. */
10205 internal_relocs
8b127cbc
AM
10206 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10207 flinfo->internal_relocs, FALSE);
c152c796
AM
10208 if (internal_relocs == NULL
10209 && o->reloc_count > 0)
10210 return FALSE;
10211
310fd250
L
10212 /* We need to reverse-copy input .ctors/.dtors sections if
10213 they are placed in .init_array/.finit_array for output. */
10214 if (o->size > address_size
10215 && ((strncmp (o->name, ".ctors", 6) == 0
10216 && strcmp (o->output_section->name,
10217 ".init_array") == 0)
10218 || (strncmp (o->name, ".dtors", 6) == 0
10219 && strcmp (o->output_section->name,
10220 ".fini_array") == 0))
10221 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10222 {
310fd250
L
10223 if (o->size != o->reloc_count * address_size)
10224 {
4eca0228 10225 _bfd_error_handler
695344c0 10226 /* xgettext:c-format */
310fd250
L
10227 (_("error: %B: size of section %A is not "
10228 "multiple of address size"),
10229 input_bfd, o);
10230 bfd_set_error (bfd_error_on_input);
10231 return FALSE;
10232 }
10233 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10234 }
10235
0f02bbd9 10236 action_discarded = -1;
c152c796 10237 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10238 action_discarded = (*bed->action_discarded) (o);
10239
10240 /* Run through the relocs evaluating complex reloc symbols and
10241 looking for relocs against symbols from discarded sections
10242 or section symbols from removed link-once sections.
10243 Complain about relocs against discarded sections. Zero
10244 relocs against removed link-once sections. */
10245
10246 rel = internal_relocs;
10247 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10248 for ( ; rel < relend; rel++)
c152c796 10249 {
0f02bbd9
AM
10250 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10251 unsigned int s_type;
10252 asection **ps, *sec;
10253 struct elf_link_hash_entry *h = NULL;
10254 const char *sym_name;
c152c796 10255
0f02bbd9
AM
10256 if (r_symndx == STN_UNDEF)
10257 continue;
c152c796 10258
0f02bbd9
AM
10259 if (r_symndx >= locsymcount
10260 || (elf_bad_symtab (input_bfd)
8b127cbc 10261 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10262 {
10263 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10264
0f02bbd9
AM
10265 /* Badly formatted input files can contain relocs that
10266 reference non-existant symbols. Check here so that
10267 we do not seg fault. */
10268 if (h == NULL)
c152c796 10269 {
0f02bbd9 10270 char buffer [32];
dce669a1 10271
0f02bbd9 10272 sprintf_vma (buffer, rel->r_info);
4eca0228 10273 _bfd_error_handler
695344c0 10274 /* xgettext:c-format */
0f02bbd9
AM
10275 (_("error: %B contains a reloc (0x%s) for section %A "
10276 "that references a non-existent global symbol"),
10277 input_bfd, o, buffer);
10278 bfd_set_error (bfd_error_bad_value);
10279 return FALSE;
10280 }
3b36f7e6 10281
0f02bbd9
AM
10282 while (h->root.type == bfd_link_hash_indirect
10283 || h->root.type == bfd_link_hash_warning)
10284 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10285
0f02bbd9 10286 s_type = h->type;
cdd3575c 10287
9e2dec47 10288 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10289 mark the symbol as undefined. Note that the
10290 linker may attach linker created dynamic sections
10291 to the plugin bfd. Symbols defined in linker
10292 created sections are not plugin symbols. */
9e2dec47
L
10293 if (h->root.non_ir_ref
10294 && (h->root.type == bfd_link_hash_defined
10295 || h->root.type == bfd_link_hash_defweak)
10296 && (h->root.u.def.section->flags
10297 & SEC_LINKER_CREATED) == 0
10298 && h->root.u.def.section->owner != NULL
10299 && (h->root.u.def.section->owner->flags
10300 & BFD_PLUGIN) != 0)
10301 {
10302 h->root.type = bfd_link_hash_undefined;
10303 h->root.u.undef.abfd = h->root.u.def.section->owner;
10304 }
10305
0f02bbd9
AM
10306 ps = NULL;
10307 if (h->root.type == bfd_link_hash_defined
10308 || h->root.type == bfd_link_hash_defweak)
10309 ps = &h->root.u.def.section;
10310
10311 sym_name = h->root.root.string;
10312 }
10313 else
10314 {
10315 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10316
10317 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10318 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10319 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10320 sym, *ps);
10321 }
c152c796 10322
c301e700 10323 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10324 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10325 {
10326 bfd_vma val;
10327 bfd_vma dot = (rel->r_offset
10328 + o->output_offset + o->output_section->vma);
10329#ifdef DEBUG
10330 printf ("Encountered a complex symbol!");
10331 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10332 input_bfd->filename, o->name,
10333 (long) (rel - internal_relocs));
0f02bbd9
AM
10334 printf (" symbol: idx %8.8lx, name %s\n",
10335 r_symndx, sym_name);
10336 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10337 (unsigned long) rel->r_info,
10338 (unsigned long) rel->r_offset);
10339#endif
8b127cbc 10340 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10341 isymbuf, locsymcount, s_type == STT_SRELC))
10342 return FALSE;
10343
10344 /* Symbol evaluated OK. Update to absolute value. */
10345 set_symbol_value (input_bfd, isymbuf, locsymcount,
10346 r_symndx, val);
10347 continue;
10348 }
10349
10350 if (action_discarded != -1 && ps != NULL)
10351 {
cdd3575c
AM
10352 /* Complain if the definition comes from a
10353 discarded section. */
dbaa2011 10354 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10355 {
cf35638d 10356 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10357 if (action_discarded & COMPLAIN)
8b127cbc 10358 (*flinfo->info->callbacks->einfo)
695344c0 10359 /* xgettext:c-format */
e1fffbe6 10360 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10361 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10362 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10363
87e5235d 10364 /* Try to do the best we can to support buggy old
e0ae6d6f 10365 versions of gcc. Pretend that the symbol is
87e5235d
AM
10366 really defined in the kept linkonce section.
10367 FIXME: This is quite broken. Modifying the
10368 symbol here means we will be changing all later
e0ae6d6f 10369 uses of the symbol, not just in this section. */
0f02bbd9 10370 if (action_discarded & PRETEND)
87e5235d 10371 {
01b3c8ab
L
10372 asection *kept;
10373
c0f00686 10374 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10375 flinfo->info);
01b3c8ab 10376 if (kept != NULL)
87e5235d
AM
10377 {
10378 *ps = kept;
10379 continue;
10380 }
10381 }
c152c796
AM
10382 }
10383 }
10384 }
10385
10386 /* Relocate the section by invoking a back end routine.
10387
10388 The back end routine is responsible for adjusting the
10389 section contents as necessary, and (if using Rela relocs
10390 and generating a relocatable output file) adjusting the
10391 reloc addend as necessary.
10392
10393 The back end routine does not have to worry about setting
10394 the reloc address or the reloc symbol index.
10395
10396 The back end routine is given a pointer to the swapped in
10397 internal symbols, and can access the hash table entries
10398 for the external symbols via elf_sym_hashes (input_bfd).
10399
10400 When generating relocatable output, the back end routine
10401 must handle STB_LOCAL/STT_SECTION symbols specially. The
10402 output symbol is going to be a section symbol
10403 corresponding to the output section, which will require
10404 the addend to be adjusted. */
10405
8b127cbc 10406 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10407 input_bfd, o, contents,
10408 internal_relocs,
10409 isymbuf,
8b127cbc 10410 flinfo->sections);
ece5ef60 10411 if (!ret)
c152c796
AM
10412 return FALSE;
10413
ece5ef60 10414 if (ret == 2
0e1862bb 10415 || bfd_link_relocatable (flinfo->info)
8b127cbc 10416 || flinfo->info->emitrelocations)
c152c796
AM
10417 {
10418 Elf_Internal_Rela *irela;
d4730f92 10419 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10420 bfd_vma last_offset;
10421 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10422 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10423 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10424 unsigned int next_erel;
c152c796 10425 bfd_boolean rela_normal;
d4730f92 10426 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10427
d4730f92
BS
10428 esdi = elf_section_data (o);
10429 esdo = elf_section_data (o->output_section);
10430 rela_normal = FALSE;
c152c796
AM
10431
10432 /* Adjust the reloc addresses and symbol indices. */
10433
10434 irela = internal_relocs;
10435 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10436 rel_hash = esdo->rel.hashes + esdo->rel.count;
10437 /* We start processing the REL relocs, if any. When we reach
10438 IRELAMID in the loop, we switch to the RELA relocs. */
10439 irelamid = irela;
10440 if (esdi->rel.hdr != NULL)
10441 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10442 * bed->s->int_rels_per_ext_rel);
eac338cf 10443 rel_hash_list = rel_hash;
d4730f92 10444 rela_hash_list = NULL;
c152c796 10445 last_offset = o->output_offset;
0e1862bb 10446 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10447 last_offset += o->output_section->vma;
10448 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10449 {
10450 unsigned long r_symndx;
10451 asection *sec;
10452 Elf_Internal_Sym sym;
10453
10454 if (next_erel == bed->s->int_rels_per_ext_rel)
10455 {
10456 rel_hash++;
10457 next_erel = 0;
10458 }
10459
d4730f92
BS
10460 if (irela == irelamid)
10461 {
10462 rel_hash = esdo->rela.hashes + esdo->rela.count;
10463 rela_hash_list = rel_hash;
10464 rela_normal = bed->rela_normal;
10465 }
10466
c152c796 10467 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10468 flinfo->info, o,
c152c796
AM
10469 irela->r_offset);
10470 if (irela->r_offset >= (bfd_vma) -2)
10471 {
10472 /* This is a reloc for a deleted entry or somesuch.
10473 Turn it into an R_*_NONE reloc, at the same
10474 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10475 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10476 being ordered. */
10477 irela->r_offset = last_offset;
10478 irela->r_info = 0;
10479 irela->r_addend = 0;
10480 continue;
10481 }
10482
10483 irela->r_offset += o->output_offset;
10484
10485 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10486 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10487 irela->r_offset += o->output_section->vma;
10488
10489 last_offset = irela->r_offset;
10490
10491 r_symndx = irela->r_info >> r_sym_shift;
10492 if (r_symndx == STN_UNDEF)
10493 continue;
10494
10495 if (r_symndx >= locsymcount
10496 || (elf_bad_symtab (input_bfd)
8b127cbc 10497 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10498 {
10499 struct elf_link_hash_entry *rh;
10500 unsigned long indx;
10501
10502 /* This is a reloc against a global symbol. We
10503 have not yet output all the local symbols, so
10504 we do not know the symbol index of any global
10505 symbol. We set the rel_hash entry for this
10506 reloc to point to the global hash table entry
10507 for this symbol. The symbol index is then
ee75fd95 10508 set at the end of bfd_elf_final_link. */
c152c796
AM
10509 indx = r_symndx - extsymoff;
10510 rh = elf_sym_hashes (input_bfd)[indx];
10511 while (rh->root.type == bfd_link_hash_indirect
10512 || rh->root.type == bfd_link_hash_warning)
10513 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10514
10515 /* Setting the index to -2 tells
10516 elf_link_output_extsym that this symbol is
10517 used by a reloc. */
10518 BFD_ASSERT (rh->indx < 0);
10519 rh->indx = -2;
10520
10521 *rel_hash = rh;
10522
10523 continue;
10524 }
10525
10526 /* This is a reloc against a local symbol. */
10527
10528 *rel_hash = NULL;
10529 sym = isymbuf[r_symndx];
8b127cbc 10530 sec = flinfo->sections[r_symndx];
c152c796
AM
10531 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10532 {
10533 /* I suppose the backend ought to fill in the
10534 section of any STT_SECTION symbol against a
6a8d1586 10535 processor specific section. */
cf35638d 10536 r_symndx = STN_UNDEF;
6a8d1586
AM
10537 if (bfd_is_abs_section (sec))
10538 ;
c152c796
AM
10539 else if (sec == NULL || sec->owner == NULL)
10540 {
10541 bfd_set_error (bfd_error_bad_value);
10542 return FALSE;
10543 }
10544 else
10545 {
6a8d1586
AM
10546 asection *osec = sec->output_section;
10547
10548 /* If we have discarded a section, the output
10549 section will be the absolute section. In
ab96bf03
AM
10550 case of discarded SEC_MERGE sections, use
10551 the kept section. relocate_section should
10552 have already handled discarded linkonce
10553 sections. */
6a8d1586
AM
10554 if (bfd_is_abs_section (osec)
10555 && sec->kept_section != NULL
10556 && sec->kept_section->output_section != NULL)
10557 {
10558 osec = sec->kept_section->output_section;
10559 irela->r_addend -= osec->vma;
10560 }
10561
10562 if (!bfd_is_abs_section (osec))
10563 {
10564 r_symndx = osec->target_index;
cf35638d 10565 if (r_symndx == STN_UNDEF)
74541ad4 10566 {
051d833a
AM
10567 irela->r_addend += osec->vma;
10568 osec = _bfd_nearby_section (output_bfd, osec,
10569 osec->vma);
10570 irela->r_addend -= osec->vma;
10571 r_symndx = osec->target_index;
74541ad4 10572 }
6a8d1586 10573 }
c152c796
AM
10574 }
10575
10576 /* Adjust the addend according to where the
10577 section winds up in the output section. */
10578 if (rela_normal)
10579 irela->r_addend += sec->output_offset;
10580 }
10581 else
10582 {
8b127cbc 10583 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10584 {
10585 unsigned long shlink;
10586 const char *name;
10587 asection *osec;
6e0b88f1 10588 long indx;
c152c796 10589
8b127cbc 10590 if (flinfo->info->strip == strip_all)
c152c796
AM
10591 {
10592 /* You can't do ld -r -s. */
10593 bfd_set_error (bfd_error_invalid_operation);
10594 return FALSE;
10595 }
10596
10597 /* This symbol was skipped earlier, but
10598 since it is needed by a reloc, we
10599 must output it now. */
10600 shlink = symtab_hdr->sh_link;
10601 name = (bfd_elf_string_from_elf_section
10602 (input_bfd, shlink, sym.st_name));
10603 if (name == NULL)
10604 return FALSE;
10605
10606 osec = sec->output_section;
10607 sym.st_shndx =
10608 _bfd_elf_section_from_bfd_section (output_bfd,
10609 osec);
10610 if (sym.st_shndx == SHN_BAD)
10611 return FALSE;
10612
10613 sym.st_value += sec->output_offset;
0e1862bb 10614 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10615 {
10616 sym.st_value += osec->vma;
10617 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10618 {
10619 /* STT_TLS symbols are relative to PT_TLS
10620 segment base. */
8b127cbc 10621 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10622 ->tls_sec != NULL);
8b127cbc 10623 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10624 ->tls_sec->vma);
10625 }
10626 }
10627
6e0b88f1 10628 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10629 ret = elf_link_output_symstrtab (flinfo, name,
10630 &sym, sec,
10631 NULL);
6e0b88f1 10632 if (ret == 0)
c152c796 10633 return FALSE;
6e0b88f1 10634 else if (ret == 1)
8b127cbc 10635 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10636 else
10637 abort ();
c152c796
AM
10638 }
10639
8b127cbc 10640 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10641 }
10642
10643 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10644 | (irela->r_info & r_type_mask));
10645 }
10646
10647 /* Swap out the relocs. */
d4730f92
BS
10648 input_rel_hdr = esdi->rel.hdr;
10649 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10650 {
d4730f92
BS
10651 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10652 input_rel_hdr,
10653 internal_relocs,
10654 rel_hash_list))
10655 return FALSE;
c152c796
AM
10656 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10657 * bed->s->int_rels_per_ext_rel);
eac338cf 10658 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10659 }
10660
10661 input_rela_hdr = esdi->rela.hdr;
10662 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10663 {
eac338cf 10664 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10665 input_rela_hdr,
eac338cf 10666 internal_relocs,
d4730f92 10667 rela_hash_list))
c152c796
AM
10668 return FALSE;
10669 }
10670 }
10671 }
10672
10673 /* Write out the modified section contents. */
10674 if (bed->elf_backend_write_section
8b127cbc 10675 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10676 contents))
c152c796
AM
10677 {
10678 /* Section written out. */
10679 }
10680 else switch (o->sec_info_type)
10681 {
dbaa2011 10682 case SEC_INFO_TYPE_STABS:
c152c796
AM
10683 if (! (_bfd_write_section_stabs
10684 (output_bfd,
8b127cbc 10685 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10686 o, &elf_section_data (o)->sec_info, contents)))
10687 return FALSE;
10688 break;
dbaa2011 10689 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10690 if (! _bfd_write_merged_section (output_bfd, o,
10691 elf_section_data (o)->sec_info))
10692 return FALSE;
10693 break;
dbaa2011 10694 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10695 {
8b127cbc 10696 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10697 o, contents))
10698 return FALSE;
10699 }
10700 break;
2f0c68f2
CM
10701 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10702 {
10703 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10704 flinfo->info,
10705 o, contents))
10706 return FALSE;
10707 }
10708 break;
c152c796
AM
10709 default:
10710 {
310fd250
L
10711 if (! (o->flags & SEC_EXCLUDE))
10712 {
10713 file_ptr offset = (file_ptr) o->output_offset;
10714 bfd_size_type todo = o->size;
37b01f6a
DG
10715
10716 offset *= bfd_octets_per_byte (output_bfd);
10717
310fd250
L
10718 if ((o->flags & SEC_ELF_REVERSE_COPY))
10719 {
10720 /* Reverse-copy input section to output. */
10721 do
10722 {
10723 todo -= address_size;
10724 if (! bfd_set_section_contents (output_bfd,
10725 o->output_section,
10726 contents + todo,
10727 offset,
10728 address_size))
10729 return FALSE;
10730 if (todo == 0)
10731 break;
10732 offset += address_size;
10733 }
10734 while (1);
10735 }
10736 else if (! bfd_set_section_contents (output_bfd,
10737 o->output_section,
10738 contents,
10739 offset, todo))
10740 return FALSE;
10741 }
c152c796
AM
10742 }
10743 break;
10744 }
10745 }
10746
10747 return TRUE;
10748}
10749
10750/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10751 requested by the linker, and does not come from any input file. This
c152c796
AM
10752 is used to build constructor and destructor tables when linking
10753 with -Ur. */
10754
10755static bfd_boolean
10756elf_reloc_link_order (bfd *output_bfd,
10757 struct bfd_link_info *info,
10758 asection *output_section,
10759 struct bfd_link_order *link_order)
10760{
10761 reloc_howto_type *howto;
10762 long indx;
10763 bfd_vma offset;
10764 bfd_vma addend;
d4730f92 10765 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10766 struct elf_link_hash_entry **rel_hash_ptr;
10767 Elf_Internal_Shdr *rel_hdr;
10768 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10769 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10770 bfd_byte *erel;
10771 unsigned int i;
d4730f92 10772 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10773
10774 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10775 if (howto == NULL)
10776 {
10777 bfd_set_error (bfd_error_bad_value);
10778 return FALSE;
10779 }
10780
10781 addend = link_order->u.reloc.p->addend;
10782
d4730f92
BS
10783 if (esdo->rel.hdr)
10784 reldata = &esdo->rel;
10785 else if (esdo->rela.hdr)
10786 reldata = &esdo->rela;
10787 else
10788 {
10789 reldata = NULL;
10790 BFD_ASSERT (0);
10791 }
10792
c152c796 10793 /* Figure out the symbol index. */
d4730f92 10794 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10795 if (link_order->type == bfd_section_reloc_link_order)
10796 {
10797 indx = link_order->u.reloc.p->u.section->target_index;
10798 BFD_ASSERT (indx != 0);
10799 *rel_hash_ptr = NULL;
10800 }
10801 else
10802 {
10803 struct elf_link_hash_entry *h;
10804
10805 /* Treat a reloc against a defined symbol as though it were
10806 actually against the section. */
10807 h = ((struct elf_link_hash_entry *)
10808 bfd_wrapped_link_hash_lookup (output_bfd, info,
10809 link_order->u.reloc.p->u.name,
10810 FALSE, FALSE, TRUE));
10811 if (h != NULL
10812 && (h->root.type == bfd_link_hash_defined
10813 || h->root.type == bfd_link_hash_defweak))
10814 {
10815 asection *section;
10816
10817 section = h->root.u.def.section;
10818 indx = section->output_section->target_index;
10819 *rel_hash_ptr = NULL;
10820 /* It seems that we ought to add the symbol value to the
10821 addend here, but in practice it has already been added
10822 because it was passed to constructor_callback. */
10823 addend += section->output_section->vma + section->output_offset;
10824 }
10825 else if (h != NULL)
10826 {
10827 /* Setting the index to -2 tells elf_link_output_extsym that
10828 this symbol is used by a reloc. */
10829 h->indx = -2;
10830 *rel_hash_ptr = h;
10831 indx = 0;
10832 }
10833 else
10834 {
1a72702b
AM
10835 (*info->callbacks->unattached_reloc)
10836 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
10837 indx = 0;
10838 }
10839 }
10840
10841 /* If this is an inplace reloc, we must write the addend into the
10842 object file. */
10843 if (howto->partial_inplace && addend != 0)
10844 {
10845 bfd_size_type size;
10846 bfd_reloc_status_type rstat;
10847 bfd_byte *buf;
10848 bfd_boolean ok;
10849 const char *sym_name;
10850
a50b1753
NC
10851 size = (bfd_size_type) bfd_get_reloc_size (howto);
10852 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10853 if (buf == NULL && size != 0)
c152c796
AM
10854 return FALSE;
10855 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10856 switch (rstat)
10857 {
10858 case bfd_reloc_ok:
10859 break;
10860
10861 default:
10862 case bfd_reloc_outofrange:
10863 abort ();
10864
10865 case bfd_reloc_overflow:
10866 if (link_order->type == bfd_section_reloc_link_order)
10867 sym_name = bfd_section_name (output_bfd,
10868 link_order->u.reloc.p->u.section);
10869 else
10870 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
10871 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10872 howto->name, addend, NULL, NULL,
10873 (bfd_vma) 0);
c152c796
AM
10874 break;
10875 }
37b01f6a 10876
c152c796 10877 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
10878 link_order->offset
10879 * bfd_octets_per_byte (output_bfd),
10880 size);
c152c796
AM
10881 free (buf);
10882 if (! ok)
10883 return FALSE;
10884 }
10885
10886 /* The address of a reloc is relative to the section in a
10887 relocatable file, and is a virtual address in an executable
10888 file. */
10889 offset = link_order->offset;
0e1862bb 10890 if (! bfd_link_relocatable (info))
c152c796
AM
10891 offset += output_section->vma;
10892
10893 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10894 {
10895 irel[i].r_offset = offset;
10896 irel[i].r_info = 0;
10897 irel[i].r_addend = 0;
10898 }
10899 if (bed->s->arch_size == 32)
10900 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10901 else
10902 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10903
d4730f92 10904 rel_hdr = reldata->hdr;
c152c796
AM
10905 erel = rel_hdr->contents;
10906 if (rel_hdr->sh_type == SHT_REL)
10907 {
d4730f92 10908 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10909 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10910 }
10911 else
10912 {
10913 irel[0].r_addend = addend;
d4730f92 10914 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10915 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10916 }
10917
d4730f92 10918 ++reldata->count;
c152c796
AM
10919
10920 return TRUE;
10921}
10922
0b52efa6
PB
10923
10924/* Get the output vma of the section pointed to by the sh_link field. */
10925
10926static bfd_vma
10927elf_get_linked_section_vma (struct bfd_link_order *p)
10928{
10929 Elf_Internal_Shdr **elf_shdrp;
10930 asection *s;
10931 int elfsec;
10932
10933 s = p->u.indirect.section;
10934 elf_shdrp = elf_elfsections (s->owner);
10935 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10936 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10937 /* PR 290:
10938 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10939 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10940 sh_info fields. Hence we could get the situation
10941 where elfsec is 0. */
10942 if (elfsec == 0)
10943 {
10944 const struct elf_backend_data *bed
10945 = get_elf_backend_data (s->owner);
10946 if (bed->link_order_error_handler)
d003868e 10947 bed->link_order_error_handler
695344c0 10948 /* xgettext:c-format */
d003868e 10949 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10950 return 0;
10951 }
10952 else
10953 {
10954 s = elf_shdrp[elfsec]->bfd_section;
10955 return s->output_section->vma + s->output_offset;
10956 }
0b52efa6
PB
10957}
10958
10959
10960/* Compare two sections based on the locations of the sections they are
10961 linked to. Used by elf_fixup_link_order. */
10962
10963static int
10964compare_link_order (const void * a, const void * b)
10965{
10966 bfd_vma apos;
10967 bfd_vma bpos;
10968
10969 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10970 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10971 if (apos < bpos)
10972 return -1;
10973 return apos > bpos;
10974}
10975
10976
10977/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10978 order as their linked sections. Returns false if this could not be done
10979 because an output section includes both ordered and unordered
10980 sections. Ideally we'd do this in the linker proper. */
10981
10982static bfd_boolean
10983elf_fixup_link_order (bfd *abfd, asection *o)
10984{
10985 int seen_linkorder;
10986 int seen_other;
10987 int n;
10988 struct bfd_link_order *p;
10989 bfd *sub;
10990 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10991 unsigned elfsec;
0b52efa6 10992 struct bfd_link_order **sections;
d33cdfe3 10993 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10994 bfd_vma offset;
3b36f7e6 10995
d33cdfe3
L
10996 other_sec = NULL;
10997 linkorder_sec = NULL;
0b52efa6
PB
10998 seen_other = 0;
10999 seen_linkorder = 0;
8423293d 11000 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11001 {
d33cdfe3 11002 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11003 {
11004 s = p->u.indirect.section;
d33cdfe3
L
11005 sub = s->owner;
11006 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11007 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11008 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11009 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11010 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11011 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11012 {
11013 seen_linkorder++;
11014 linkorder_sec = s;
11015 }
0b52efa6 11016 else
d33cdfe3
L
11017 {
11018 seen_other++;
11019 other_sec = s;
11020 }
0b52efa6
PB
11021 }
11022 else
11023 seen_other++;
d33cdfe3
L
11024
11025 if (seen_other && seen_linkorder)
11026 {
11027 if (other_sec && linkorder_sec)
4eca0228 11028 _bfd_error_handler
695344c0 11029 /* xgettext:c-format */
4eca0228
AM
11030 (_("%A has both ordered [`%A' in %B] "
11031 "and unordered [`%A' in %B] sections"),
11032 o, linkorder_sec,
11033 linkorder_sec->owner, other_sec,
11034 other_sec->owner);
d33cdfe3 11035 else
4eca0228
AM
11036 _bfd_error_handler
11037 (_("%A has both ordered and unordered sections"), o);
d33cdfe3
L
11038 bfd_set_error (bfd_error_bad_value);
11039 return FALSE;
11040 }
0b52efa6
PB
11041 }
11042
11043 if (!seen_linkorder)
11044 return TRUE;
11045
0b52efa6 11046 sections = (struct bfd_link_order **)
14b1c01e
AM
11047 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11048 if (sections == NULL)
11049 return FALSE;
0b52efa6 11050 seen_linkorder = 0;
3b36f7e6 11051
8423293d 11052 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11053 {
11054 sections[seen_linkorder++] = p;
11055 }
11056 /* Sort the input sections in the order of their linked section. */
11057 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11058 compare_link_order);
11059
11060 /* Change the offsets of the sections. */
11061 offset = 0;
11062 for (n = 0; n < seen_linkorder; n++)
11063 {
11064 s = sections[n]->u.indirect.section;
461686a3 11065 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11066 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11067 sections[n]->offset = offset;
11068 offset += sections[n]->size;
11069 }
11070
4dd07732 11071 free (sections);
0b52efa6
PB
11072 return TRUE;
11073}
11074
76359541
TP
11075/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11076 Returns TRUE upon success, FALSE otherwise. */
11077
11078static bfd_boolean
11079elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11080{
11081 bfd_boolean ret = FALSE;
11082 bfd *implib_bfd;
11083 const struct elf_backend_data *bed;
11084 flagword flags;
11085 enum bfd_architecture arch;
11086 unsigned int mach;
11087 asymbol **sympp = NULL;
11088 long symsize;
11089 long symcount;
11090 long src_count;
11091 elf_symbol_type *osymbuf;
11092
11093 implib_bfd = info->out_implib_bfd;
11094 bed = get_elf_backend_data (abfd);
11095
11096 if (!bfd_set_format (implib_bfd, bfd_object))
11097 return FALSE;
11098
11099 flags = bfd_get_file_flags (abfd);
11100 flags &= ~HAS_RELOC;
11101 if (!bfd_set_start_address (implib_bfd, 0)
11102 || !bfd_set_file_flags (implib_bfd, flags))
11103 return FALSE;
11104
11105 /* Copy architecture of output file to import library file. */
11106 arch = bfd_get_arch (abfd);
11107 mach = bfd_get_mach (abfd);
11108 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11109 && (abfd->target_defaulted
11110 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11111 return FALSE;
11112
11113 /* Get symbol table size. */
11114 symsize = bfd_get_symtab_upper_bound (abfd);
11115 if (symsize < 0)
11116 return FALSE;
11117
11118 /* Read in the symbol table. */
11119 sympp = (asymbol **) xmalloc (symsize);
11120 symcount = bfd_canonicalize_symtab (abfd, sympp);
11121 if (symcount < 0)
11122 goto free_sym_buf;
11123
11124 /* Allow the BFD backend to copy any private header data it
11125 understands from the output BFD to the import library BFD. */
11126 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11127 goto free_sym_buf;
11128
11129 /* Filter symbols to appear in the import library. */
11130 if (bed->elf_backend_filter_implib_symbols)
11131 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11132 symcount);
11133 else
11134 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11135 if (symcount == 0)
11136 {
5df1bc57 11137 bfd_set_error (bfd_error_no_symbols);
4eca0228
AM
11138 _bfd_error_handler (_("%B: no symbol found for import library"),
11139 implib_bfd);
76359541
TP
11140 goto free_sym_buf;
11141 }
11142
11143
11144 /* Make symbols absolute. */
11145 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11146 sizeof (*osymbuf));
11147 for (src_count = 0; src_count < symcount; src_count++)
11148 {
11149 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11150 sizeof (*osymbuf));
11151 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11152 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11153 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11154 osymbuf[src_count].internal_elf_sym.st_value =
11155 osymbuf[src_count].symbol.value;
11156 sympp[src_count] = &osymbuf[src_count].symbol;
11157 }
11158
11159 bfd_set_symtab (implib_bfd, sympp, symcount);
11160
11161 /* Allow the BFD backend to copy any private data it understands
11162 from the output BFD to the import library BFD. This is done last
11163 to permit the routine to look at the filtered symbol table. */
11164 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11165 goto free_sym_buf;
11166
11167 if (!bfd_close (implib_bfd))
11168 goto free_sym_buf;
11169
11170 ret = TRUE;
11171
11172free_sym_buf:
11173 free (sympp);
11174 return ret;
11175}
11176
9f7c3e5e
AM
11177static void
11178elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11179{
11180 asection *o;
11181
11182 if (flinfo->symstrtab != NULL)
ef10c3ac 11183 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11184 if (flinfo->contents != NULL)
11185 free (flinfo->contents);
11186 if (flinfo->external_relocs != NULL)
11187 free (flinfo->external_relocs);
11188 if (flinfo->internal_relocs != NULL)
11189 free (flinfo->internal_relocs);
11190 if (flinfo->external_syms != NULL)
11191 free (flinfo->external_syms);
11192 if (flinfo->locsym_shndx != NULL)
11193 free (flinfo->locsym_shndx);
11194 if (flinfo->internal_syms != NULL)
11195 free (flinfo->internal_syms);
11196 if (flinfo->indices != NULL)
11197 free (flinfo->indices);
11198 if (flinfo->sections != NULL)
11199 free (flinfo->sections);
9f7c3e5e
AM
11200 if (flinfo->symshndxbuf != NULL)
11201 free (flinfo->symshndxbuf);
11202 for (o = obfd->sections; o != NULL; o = o->next)
11203 {
11204 struct bfd_elf_section_data *esdo = elf_section_data (o);
11205 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11206 free (esdo->rel.hashes);
11207 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11208 free (esdo->rela.hashes);
11209 }
11210}
0b52efa6 11211
c152c796
AM
11212/* Do the final step of an ELF link. */
11213
11214bfd_boolean
11215bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11216{
11217 bfd_boolean dynamic;
11218 bfd_boolean emit_relocs;
11219 bfd *dynobj;
8b127cbc 11220 struct elf_final_link_info flinfo;
91d6fa6a
NC
11221 asection *o;
11222 struct bfd_link_order *p;
11223 bfd *sub;
c152c796
AM
11224 bfd_size_type max_contents_size;
11225 bfd_size_type max_external_reloc_size;
11226 bfd_size_type max_internal_reloc_count;
11227 bfd_size_type max_sym_count;
11228 bfd_size_type max_sym_shndx_count;
c152c796
AM
11229 Elf_Internal_Sym elfsym;
11230 unsigned int i;
11231 Elf_Internal_Shdr *symtab_hdr;
11232 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11233 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11234 struct elf_outext_info eoinfo;
11235 bfd_boolean merged;
11236 size_t relativecount = 0;
11237 asection *reldyn = 0;
11238 bfd_size_type amt;
104d59d1
JM
11239 asection *attr_section = NULL;
11240 bfd_vma attr_size = 0;
11241 const char *std_attrs_section;
64f52338 11242 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11243
64f52338 11244 if (!is_elf_hash_table (htab))
c152c796
AM
11245 return FALSE;
11246
0e1862bb 11247 if (bfd_link_pic (info))
c152c796
AM
11248 abfd->flags |= DYNAMIC;
11249
64f52338
AM
11250 dynamic = htab->dynamic_sections_created;
11251 dynobj = htab->dynobj;
c152c796 11252
0e1862bb 11253 emit_relocs = (bfd_link_relocatable (info)
a4676736 11254 || info->emitrelocations);
c152c796 11255
8b127cbc
AM
11256 flinfo.info = info;
11257 flinfo.output_bfd = abfd;
ef10c3ac 11258 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11259 if (flinfo.symstrtab == NULL)
c152c796
AM
11260 return FALSE;
11261
11262 if (! dynamic)
11263 {
8b127cbc
AM
11264 flinfo.hash_sec = NULL;
11265 flinfo.symver_sec = NULL;
c152c796
AM
11266 }
11267 else
11268 {
3d4d4302 11269 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11270 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11271 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11272 /* Note that it is OK if symver_sec is NULL. */
11273 }
11274
8b127cbc
AM
11275 flinfo.contents = NULL;
11276 flinfo.external_relocs = NULL;
11277 flinfo.internal_relocs = NULL;
11278 flinfo.external_syms = NULL;
11279 flinfo.locsym_shndx = NULL;
11280 flinfo.internal_syms = NULL;
11281 flinfo.indices = NULL;
11282 flinfo.sections = NULL;
8b127cbc 11283 flinfo.symshndxbuf = NULL;
ffbc01cc 11284 flinfo.filesym_count = 0;
c152c796 11285
104d59d1
JM
11286 /* The object attributes have been merged. Remove the input
11287 sections from the link, and set the contents of the output
11288 secton. */
11289 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11290 for (o = abfd->sections; o != NULL; o = o->next)
11291 {
11292 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11293 || strcmp (o->name, ".gnu.attributes") == 0)
11294 {
11295 for (p = o->map_head.link_order; p != NULL; p = p->next)
11296 {
11297 asection *input_section;
11298
11299 if (p->type != bfd_indirect_link_order)
11300 continue;
11301 input_section = p->u.indirect.section;
11302 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11303 elf_link_input_bfd ignores this section. */
11304 input_section->flags &= ~SEC_HAS_CONTENTS;
11305 }
a0c8462f 11306
104d59d1
JM
11307 attr_size = bfd_elf_obj_attr_size (abfd);
11308 if (attr_size)
11309 {
11310 bfd_set_section_size (abfd, o, attr_size);
11311 attr_section = o;
11312 /* Skip this section later on. */
11313 o->map_head.link_order = NULL;
11314 }
11315 else
11316 o->flags |= SEC_EXCLUDE;
11317 }
11318 }
11319
c152c796
AM
11320 /* Count up the number of relocations we will output for each output
11321 section, so that we know the sizes of the reloc sections. We
11322 also figure out some maximum sizes. */
11323 max_contents_size = 0;
11324 max_external_reloc_size = 0;
11325 max_internal_reloc_count = 0;
11326 max_sym_count = 0;
11327 max_sym_shndx_count = 0;
11328 merged = FALSE;
11329 for (o = abfd->sections; o != NULL; o = o->next)
11330 {
11331 struct bfd_elf_section_data *esdo = elf_section_data (o);
11332 o->reloc_count = 0;
11333
8423293d 11334 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11335 {
11336 unsigned int reloc_count = 0;
9eaff861 11337 unsigned int additional_reloc_count = 0;
c152c796 11338 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11339
11340 if (p->type == bfd_section_reloc_link_order
11341 || p->type == bfd_symbol_reloc_link_order)
11342 reloc_count = 1;
11343 else if (p->type == bfd_indirect_link_order)
11344 {
11345 asection *sec;
11346
11347 sec = p->u.indirect.section;
d7f399a8
NC
11348 /* See PR 20908 for a reproducer. */
11349 if (bfd_get_flavour (sec->owner) != bfd_target_elf_flavour)
11350 {
11351 _bfd_error_handler (_("%B: not in ELF format"), sec->owner);
11352 goto error_return;
11353 }
c152c796
AM
11354 esdi = elf_section_data (sec);
11355
11356 /* Mark all sections which are to be included in the
11357 link. This will normally be every section. We need
11358 to do this so that we can identify any sections which
11359 the linker has decided to not include. */
11360 sec->linker_mark = TRUE;
11361
11362 if (sec->flags & SEC_MERGE)
11363 merged = TRUE;
11364
aed64b35
L
11365 if (esdo->this_hdr.sh_type == SHT_REL
11366 || esdo->this_hdr.sh_type == SHT_RELA)
11367 /* Some backends use reloc_count in relocation sections
11368 to count particular types of relocs. Of course,
11369 reloc sections themselves can't have relocations. */
11370 reloc_count = 0;
0e1862bb 11371 else if (emit_relocs)
491d01d3
YU
11372 {
11373 reloc_count = sec->reloc_count;
11374 if (bed->elf_backend_count_additional_relocs)
11375 {
11376 int c;
11377 c = (*bed->elf_backend_count_additional_relocs) (sec);
11378 additional_reloc_count += c;
11379 }
11380 }
c152c796 11381 else if (bed->elf_backend_count_relocs)
58217f29 11382 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 11383
eea6121a
AM
11384 if (sec->rawsize > max_contents_size)
11385 max_contents_size = sec->rawsize;
11386 if (sec->size > max_contents_size)
11387 max_contents_size = sec->size;
c152c796
AM
11388
11389 /* We are interested in just local symbols, not all
11390 symbols. */
11391 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11392 && (sec->owner->flags & DYNAMIC) == 0)
11393 {
11394 size_t sym_count;
11395
11396 if (elf_bad_symtab (sec->owner))
11397 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11398 / bed->s->sizeof_sym);
11399 else
11400 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11401
11402 if (sym_count > max_sym_count)
11403 max_sym_count = sym_count;
11404
11405 if (sym_count > max_sym_shndx_count
6a40cf0c 11406 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11407 max_sym_shndx_count = sym_count;
11408
11409 if ((sec->flags & SEC_RELOC) != 0)
11410 {
d4730f92 11411 size_t ext_size = 0;
c152c796 11412
d4730f92
BS
11413 if (esdi->rel.hdr != NULL)
11414 ext_size = esdi->rel.hdr->sh_size;
11415 if (esdi->rela.hdr != NULL)
11416 ext_size += esdi->rela.hdr->sh_size;
7326c758 11417
c152c796
AM
11418 if (ext_size > max_external_reloc_size)
11419 max_external_reloc_size = ext_size;
11420 if (sec->reloc_count > max_internal_reloc_count)
11421 max_internal_reloc_count = sec->reloc_count;
11422 }
11423 }
11424 }
11425
11426 if (reloc_count == 0)
11427 continue;
11428
9eaff861 11429 reloc_count += additional_reloc_count;
c152c796
AM
11430 o->reloc_count += reloc_count;
11431
0e1862bb 11432 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11433 {
d4730f92 11434 if (esdi->rel.hdr)
9eaff861 11435 {
491d01d3 11436 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11437 esdo->rel.count += additional_reloc_count;
11438 }
d4730f92 11439 if (esdi->rela.hdr)
9eaff861 11440 {
491d01d3 11441 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11442 esdo->rela.count += additional_reloc_count;
11443 }
d4730f92
BS
11444 }
11445 else
11446 {
11447 if (o->use_rela_p)
11448 esdo->rela.count += reloc_count;
2c2b4ed4 11449 else
d4730f92 11450 esdo->rel.count += reloc_count;
c152c796 11451 }
c152c796
AM
11452 }
11453
9eaff861 11454 if (o->reloc_count > 0)
c152c796
AM
11455 o->flags |= SEC_RELOC;
11456 else
11457 {
11458 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11459 set it (this is probably a bug) and if it is set
11460 assign_section_numbers will create a reloc section. */
11461 o->flags &=~ SEC_RELOC;
11462 }
11463
11464 /* If the SEC_ALLOC flag is not set, force the section VMA to
11465 zero. This is done in elf_fake_sections as well, but forcing
11466 the VMA to 0 here will ensure that relocs against these
11467 sections are handled correctly. */
11468 if ((o->flags & SEC_ALLOC) == 0
11469 && ! o->user_set_vma)
11470 o->vma = 0;
11471 }
11472
0e1862bb 11473 if (! bfd_link_relocatable (info) && merged)
64f52338 11474 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11475
11476 /* Figure out the file positions for everything but the symbol table
11477 and the relocs. We set symcount to force assign_section_numbers
11478 to create a symbol table. */
8539e4e8 11479 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11480 BFD_ASSERT (! abfd->output_has_begun);
11481 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11482 goto error_return;
11483
ee75fd95 11484 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11485 for (o = abfd->sections; o != NULL; o = o->next)
11486 {
d4730f92 11487 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11488 if ((o->flags & SEC_RELOC) != 0)
11489 {
d4730f92 11490 if (esdo->rel.hdr
9eaff861 11491 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11492 goto error_return;
11493
d4730f92 11494 if (esdo->rela.hdr
9eaff861 11495 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11496 goto error_return;
11497 }
11498
11499 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11500 to count upwards while actually outputting the relocations. */
d4730f92
BS
11501 esdo->rel.count = 0;
11502 esdo->rela.count = 0;
0ce398f1
L
11503
11504 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11505 {
11506 /* Cache the section contents so that they can be compressed
11507 later. Use bfd_malloc since it will be freed by
11508 bfd_compress_section_contents. */
11509 unsigned char *contents = esdo->this_hdr.contents;
11510 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11511 abort ();
11512 contents
11513 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11514 if (contents == NULL)
11515 goto error_return;
11516 esdo->this_hdr.contents = contents;
11517 }
c152c796
AM
11518 }
11519
c152c796 11520 /* We have now assigned file positions for all the sections except
a485e98e
AM
11521 .symtab, .strtab, and non-loaded reloc sections. We start the
11522 .symtab section at the current file position, and write directly
11523 to it. We build the .strtab section in memory. */
c152c796
AM
11524 bfd_get_symcount (abfd) = 0;
11525 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11526 /* sh_name is set in prep_headers. */
11527 symtab_hdr->sh_type = SHT_SYMTAB;
11528 /* sh_flags, sh_addr and sh_size all start off zero. */
11529 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11530 /* sh_link is set in assign_section_numbers. */
11531 /* sh_info is set below. */
11532 /* sh_offset is set just below. */
72de5009 11533 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11534
ef10c3ac
L
11535 if (max_sym_count < 20)
11536 max_sym_count = 20;
64f52338 11537 htab->strtabsize = max_sym_count;
ef10c3ac 11538 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11539 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11540 if (htab->strtab == NULL)
c152c796 11541 goto error_return;
ef10c3ac
L
11542 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11543 flinfo.symshndxbuf
11544 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11545 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11546
8539e4e8 11547 if (info->strip != strip_all || emit_relocs)
c152c796 11548 {
8539e4e8
AM
11549 file_ptr off = elf_next_file_pos (abfd);
11550
11551 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11552
11553 /* Note that at this point elf_next_file_pos (abfd) is
11554 incorrect. We do not yet know the size of the .symtab section.
11555 We correct next_file_pos below, after we do know the size. */
11556
11557 /* Start writing out the symbol table. The first symbol is always a
11558 dummy symbol. */
c152c796
AM
11559 elfsym.st_value = 0;
11560 elfsym.st_size = 0;
11561 elfsym.st_info = 0;
11562 elfsym.st_other = 0;
11563 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11564 elfsym.st_target_internal = 0;
ef10c3ac
L
11565 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11566 bfd_und_section_ptr, NULL) != 1)
c152c796 11567 goto error_return;
c152c796 11568
8539e4e8
AM
11569 /* Output a symbol for each section. We output these even if we are
11570 discarding local symbols, since they are used for relocs. These
11571 symbols have no names. We store the index of each one in the
11572 index field of the section, so that we can find it again when
11573 outputting relocs. */
11574
c152c796
AM
11575 elfsym.st_size = 0;
11576 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11577 elfsym.st_other = 0;
f0b5bb34 11578 elfsym.st_value = 0;
35fc36a8 11579 elfsym.st_target_internal = 0;
c152c796
AM
11580 for (i = 1; i < elf_numsections (abfd); i++)
11581 {
11582 o = bfd_section_from_elf_index (abfd, i);
11583 if (o != NULL)
f0b5bb34
AM
11584 {
11585 o->target_index = bfd_get_symcount (abfd);
11586 elfsym.st_shndx = i;
0e1862bb 11587 if (!bfd_link_relocatable (info))
f0b5bb34 11588 elfsym.st_value = o->vma;
ef10c3ac
L
11589 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11590 NULL) != 1)
f0b5bb34
AM
11591 goto error_return;
11592 }
c152c796
AM
11593 }
11594 }
11595
11596 /* Allocate some memory to hold information read in from the input
11597 files. */
11598 if (max_contents_size != 0)
11599 {
8b127cbc
AM
11600 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11601 if (flinfo.contents == NULL)
c152c796
AM
11602 goto error_return;
11603 }
11604
11605 if (max_external_reloc_size != 0)
11606 {
8b127cbc
AM
11607 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11608 if (flinfo.external_relocs == NULL)
c152c796
AM
11609 goto error_return;
11610 }
11611
11612 if (max_internal_reloc_count != 0)
11613 {
11614 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11615 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11616 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11617 if (flinfo.internal_relocs == NULL)
c152c796
AM
11618 goto error_return;
11619 }
11620
11621 if (max_sym_count != 0)
11622 {
11623 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11624 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11625 if (flinfo.external_syms == NULL)
c152c796
AM
11626 goto error_return;
11627
11628 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11629 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11630 if (flinfo.internal_syms == NULL)
c152c796
AM
11631 goto error_return;
11632
11633 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11634 flinfo.indices = (long int *) bfd_malloc (amt);
11635 if (flinfo.indices == NULL)
c152c796
AM
11636 goto error_return;
11637
11638 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11639 flinfo.sections = (asection **) bfd_malloc (amt);
11640 if (flinfo.sections == NULL)
c152c796
AM
11641 goto error_return;
11642 }
11643
11644 if (max_sym_shndx_count != 0)
11645 {
11646 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11647 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11648 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11649 goto error_return;
11650 }
11651
64f52338 11652 if (htab->tls_sec)
c152c796
AM
11653 {
11654 bfd_vma base, end = 0;
11655 asection *sec;
11656
64f52338 11657 for (sec = htab->tls_sec;
c152c796
AM
11658 sec && (sec->flags & SEC_THREAD_LOCAL);
11659 sec = sec->next)
11660 {
3a800eb9 11661 bfd_size_type size = sec->size;
c152c796 11662
3a800eb9
AM
11663 if (size == 0
11664 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11665 {
91d6fa6a
NC
11666 struct bfd_link_order *ord = sec->map_tail.link_order;
11667
11668 if (ord != NULL)
11669 size = ord->offset + ord->size;
c152c796
AM
11670 }
11671 end = sec->vma + size;
11672 }
64f52338 11673 base = htab->tls_sec->vma;
7dc98aea
RO
11674 /* Only align end of TLS section if static TLS doesn't have special
11675 alignment requirements. */
11676 if (bed->static_tls_alignment == 1)
64f52338
AM
11677 end = align_power (end, htab->tls_sec->alignment_power);
11678 htab->tls_size = end - base;
c152c796
AM
11679 }
11680
0b52efa6
PB
11681 /* Reorder SHF_LINK_ORDER sections. */
11682 for (o = abfd->sections; o != NULL; o = o->next)
11683 {
11684 if (!elf_fixup_link_order (abfd, o))
11685 return FALSE;
11686 }
11687
2f0c68f2
CM
11688 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11689 return FALSE;
11690
c152c796
AM
11691 /* Since ELF permits relocations to be against local symbols, we
11692 must have the local symbols available when we do the relocations.
11693 Since we would rather only read the local symbols once, and we
11694 would rather not keep them in memory, we handle all the
11695 relocations for a single input file at the same time.
11696
11697 Unfortunately, there is no way to know the total number of local
11698 symbols until we have seen all of them, and the local symbol
11699 indices precede the global symbol indices. This means that when
11700 we are generating relocatable output, and we see a reloc against
11701 a global symbol, we can not know the symbol index until we have
11702 finished examining all the local symbols to see which ones we are
11703 going to output. To deal with this, we keep the relocations in
11704 memory, and don't output them until the end of the link. This is
11705 an unfortunate waste of memory, but I don't see a good way around
11706 it. Fortunately, it only happens when performing a relocatable
11707 link, which is not the common case. FIXME: If keep_memory is set
11708 we could write the relocs out and then read them again; I don't
11709 know how bad the memory loss will be. */
11710
c72f2fb2 11711 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11712 sub->output_has_begun = FALSE;
11713 for (o = abfd->sections; o != NULL; o = o->next)
11714 {
8423293d 11715 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11716 {
11717 if (p->type == bfd_indirect_link_order
11718 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11719 == bfd_target_elf_flavour)
11720 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11721 {
11722 if (! sub->output_has_begun)
11723 {
8b127cbc 11724 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11725 goto error_return;
11726 sub->output_has_begun = TRUE;
11727 }
11728 }
11729 else if (p->type == bfd_section_reloc_link_order
11730 || p->type == bfd_symbol_reloc_link_order)
11731 {
11732 if (! elf_reloc_link_order (abfd, info, o, p))
11733 goto error_return;
11734 }
11735 else
11736 {
11737 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11738 {
11739 if (p->type == bfd_indirect_link_order
11740 && (bfd_get_flavour (sub)
11741 == bfd_target_elf_flavour)
11742 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11743 != bed->s->elfclass))
11744 {
11745 const char *iclass, *oclass;
11746
aebf9be7 11747 switch (bed->s->elfclass)
351f65ca 11748 {
aebf9be7
NC
11749 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11750 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11751 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11752 default: abort ();
351f65ca 11753 }
aebf9be7
NC
11754
11755 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11756 {
aebf9be7
NC
11757 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11758 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11759 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11760 default: abort ();
351f65ca
L
11761 }
11762
11763 bfd_set_error (bfd_error_wrong_format);
4eca0228 11764 _bfd_error_handler
695344c0 11765 /* xgettext:c-format */
351f65ca
L
11766 (_("%B: file class %s incompatible with %s"),
11767 sub, iclass, oclass);
11768 }
11769
11770 goto error_return;
11771 }
c152c796
AM
11772 }
11773 }
11774 }
11775
c0f00686
L
11776 /* Free symbol buffer if needed. */
11777 if (!info->reduce_memory_overheads)
11778 {
c72f2fb2 11779 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11780 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11781 && elf_tdata (sub)->symbuf)
c0f00686
L
11782 {
11783 free (elf_tdata (sub)->symbuf);
11784 elf_tdata (sub)->symbuf = NULL;
11785 }
11786 }
11787
c152c796
AM
11788 /* Output any global symbols that got converted to local in a
11789 version script or due to symbol visibility. We do this in a
11790 separate step since ELF requires all local symbols to appear
11791 prior to any global symbols. FIXME: We should only do this if
11792 some global symbols were, in fact, converted to become local.
11793 FIXME: Will this work correctly with the Irix 5 linker? */
11794 eoinfo.failed = FALSE;
8b127cbc 11795 eoinfo.flinfo = &flinfo;
c152c796 11796 eoinfo.localsyms = TRUE;
34a79995 11797 eoinfo.file_sym_done = FALSE;
7686d77d 11798 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11799 if (eoinfo.failed)
11800 return FALSE;
11801
4e617b1e
PB
11802 /* If backend needs to output some local symbols not present in the hash
11803 table, do it now. */
8539e4e8
AM
11804 if (bed->elf_backend_output_arch_local_syms
11805 && (info->strip != strip_all || emit_relocs))
4e617b1e 11806 {
6e0b88f1 11807 typedef int (*out_sym_func)
4e617b1e
PB
11808 (void *, const char *, Elf_Internal_Sym *, asection *,
11809 struct elf_link_hash_entry *);
11810
11811 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11812 (abfd, info, &flinfo,
11813 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11814 return FALSE;
11815 }
11816
c152c796
AM
11817 /* That wrote out all the local symbols. Finish up the symbol table
11818 with the global symbols. Even if we want to strip everything we
11819 can, we still need to deal with those global symbols that got
11820 converted to local in a version script. */
11821
11822 /* The sh_info field records the index of the first non local symbol. */
11823 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11824
11825 if (dynamic
64f52338
AM
11826 && htab->dynsym != NULL
11827 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
11828 {
11829 Elf_Internal_Sym sym;
64f52338 11830 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 11831
64f52338
AM
11832 o = htab->dynsym->output_section;
11833 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
11834
11835 /* Write out the section symbols for the output sections. */
0e1862bb 11836 if (bfd_link_pic (info)
64f52338 11837 || htab->is_relocatable_executable)
c152c796
AM
11838 {
11839 asection *s;
11840
11841 sym.st_size = 0;
11842 sym.st_name = 0;
11843 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11844 sym.st_other = 0;
35fc36a8 11845 sym.st_target_internal = 0;
c152c796
AM
11846
11847 for (s = abfd->sections; s != NULL; s = s->next)
11848 {
11849 int indx;
11850 bfd_byte *dest;
11851 long dynindx;
11852
c152c796 11853 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11854 if (dynindx <= 0)
11855 continue;
11856 indx = elf_section_data (s)->this_idx;
c152c796
AM
11857 BFD_ASSERT (indx > 0);
11858 sym.st_shndx = indx;
c0d5a53d
L
11859 if (! check_dynsym (abfd, &sym))
11860 return FALSE;
c152c796
AM
11861 sym.st_value = s->vma;
11862 dest = dynsym + dynindx * bed->s->sizeof_sym;
11863 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11864 }
c152c796
AM
11865 }
11866
11867 /* Write out the local dynsyms. */
64f52338 11868 if (htab->dynlocal)
c152c796
AM
11869 {
11870 struct elf_link_local_dynamic_entry *e;
64f52338 11871 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
11872 {
11873 asection *s;
11874 bfd_byte *dest;
11875
935bd1e0 11876 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11877 Note that we saved a word of storage and overwrote
11878 the original st_name with the dynstr_index. */
11879 sym = e->isym;
935bd1e0 11880 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11881
cb33740c
AM
11882 s = bfd_section_from_elf_index (e->input_bfd,
11883 e->isym.st_shndx);
11884 if (s != NULL)
c152c796 11885 {
c152c796
AM
11886 sym.st_shndx =
11887 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11888 if (! check_dynsym (abfd, &sym))
11889 return FALSE;
c152c796
AM
11890 sym.st_value = (s->output_section->vma
11891 + s->output_offset
11892 + e->isym.st_value);
11893 }
11894
c152c796
AM
11895 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11896 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11897 }
11898 }
c152c796
AM
11899 }
11900
11901 /* We get the global symbols from the hash table. */
11902 eoinfo.failed = FALSE;
11903 eoinfo.localsyms = FALSE;
8b127cbc 11904 eoinfo.flinfo = &flinfo;
7686d77d 11905 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11906 if (eoinfo.failed)
11907 return FALSE;
11908
11909 /* If backend needs to output some symbols not present in the hash
11910 table, do it now. */
8539e4e8
AM
11911 if (bed->elf_backend_output_arch_syms
11912 && (info->strip != strip_all || emit_relocs))
c152c796 11913 {
6e0b88f1 11914 typedef int (*out_sym_func)
c152c796
AM
11915 (void *, const char *, Elf_Internal_Sym *, asection *,
11916 struct elf_link_hash_entry *);
11917
11918 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
11919 (abfd, info, &flinfo,
11920 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
11921 return FALSE;
11922 }
11923
ef10c3ac
L
11924 /* Finalize the .strtab section. */
11925 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11926
11927 /* Swap out the .strtab section. */
11928 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
11929 return FALSE;
11930
11931 /* Now we know the size of the symtab section. */
c152c796
AM
11932 if (bfd_get_symcount (abfd) > 0)
11933 {
ee3b52e9
L
11934 /* Finish up and write out the symbol string table (.strtab)
11935 section. */
11936 Elf_Internal_Shdr *symstrtab_hdr;
8539e4e8
AM
11937 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11938
6a40cf0c
NC
11939 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11940 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
8539e4e8
AM
11941 {
11942 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11943 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11944 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11945 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11946 symtab_shndx_hdr->sh_size = amt;
11947
11948 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11949 off, TRUE);
11950
11951 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11952 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11953 return FALSE;
11954 }
ee3b52e9
L
11955
11956 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11957 /* sh_name was set in prep_headers. */
11958 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 11959 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 11960 symstrtab_hdr->sh_addr = 0;
ef10c3ac 11961 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
11962 symstrtab_hdr->sh_entsize = 0;
11963 symstrtab_hdr->sh_link = 0;
11964 symstrtab_hdr->sh_info = 0;
11965 /* sh_offset is set just below. */
11966 symstrtab_hdr->sh_addralign = 1;
11967
11968 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11969 off, TRUE);
11970 elf_next_file_pos (abfd) = off;
11971
c152c796 11972 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 11973 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
11974 return FALSE;
11975 }
11976
76359541
TP
11977 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
11978 {
4eca0228
AM
11979 _bfd_error_handler (_("%B: failed to generate import library"),
11980 info->out_implib_bfd);
76359541
TP
11981 return FALSE;
11982 }
11983
c152c796
AM
11984 /* Adjust the relocs to have the correct symbol indices. */
11985 for (o = abfd->sections; o != NULL; o = o->next)
11986 {
d4730f92 11987 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 11988 bfd_boolean sort;
c152c796
AM
11989 if ((o->flags & SEC_RELOC) == 0)
11990 continue;
11991
28dbcedc 11992 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 11993 if (esdo->rel.hdr != NULL
9eaff861 11994 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort))
bca6d0e3
AM
11995 return FALSE;
11996 if (esdo->rela.hdr != NULL
9eaff861 11997 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort))
bca6d0e3 11998 return FALSE;
c152c796
AM
11999
12000 /* Set the reloc_count field to 0 to prevent write_relocs from
12001 trying to swap the relocs out itself. */
12002 o->reloc_count = 0;
12003 }
12004
12005 if (dynamic && info->combreloc && dynobj != NULL)
12006 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12007
12008 /* If we are linking against a dynamic object, or generating a
12009 shared library, finish up the dynamic linking information. */
12010 if (dynamic)
12011 {
12012 bfd_byte *dyncon, *dynconend;
12013
12014 /* Fix up .dynamic entries. */
3d4d4302 12015 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12016 BFD_ASSERT (o != NULL);
12017
12018 dyncon = o->contents;
eea6121a 12019 dynconend = o->contents + o->size;
c152c796
AM
12020 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12021 {
12022 Elf_Internal_Dyn dyn;
12023 const char *name;
12024 unsigned int type;
12025
12026 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12027
12028 switch (dyn.d_tag)
12029 {
12030 default:
12031 continue;
12032 case DT_NULL:
12033 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12034 {
12035 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12036 {
12037 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12038 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12039 default: continue;
12040 }
12041 dyn.d_un.d_val = relativecount;
12042 relativecount = 0;
12043 break;
12044 }
12045 continue;
12046
12047 case DT_INIT:
12048 name = info->init_function;
12049 goto get_sym;
12050 case DT_FINI:
12051 name = info->fini_function;
12052 get_sym:
12053 {
12054 struct elf_link_hash_entry *h;
12055
64f52338 12056 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12057 if (h != NULL
12058 && (h->root.type == bfd_link_hash_defined
12059 || h->root.type == bfd_link_hash_defweak))
12060 {
bef26483 12061 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12062 o = h->root.u.def.section;
12063 if (o->output_section != NULL)
bef26483 12064 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12065 + o->output_offset);
12066 else
12067 {
12068 /* The symbol is imported from another shared
12069 library and does not apply to this one. */
bef26483 12070 dyn.d_un.d_ptr = 0;
c152c796
AM
12071 }
12072 break;
12073 }
12074 }
12075 continue;
12076
12077 case DT_PREINIT_ARRAYSZ:
12078 name = ".preinit_array";
4ade44b7 12079 goto get_out_size;
c152c796
AM
12080 case DT_INIT_ARRAYSZ:
12081 name = ".init_array";
4ade44b7 12082 goto get_out_size;
c152c796
AM
12083 case DT_FINI_ARRAYSZ:
12084 name = ".fini_array";
4ade44b7 12085 get_out_size:
c152c796
AM
12086 o = bfd_get_section_by_name (abfd, name);
12087 if (o == NULL)
12088 {
4eca0228 12089 _bfd_error_handler
4ade44b7 12090 (_("could not find section %s"), name);
c152c796
AM
12091 goto error_return;
12092 }
eea6121a 12093 if (o->size == 0)
4eca0228 12094 _bfd_error_handler
c152c796 12095 (_("warning: %s section has zero size"), name);
eea6121a 12096 dyn.d_un.d_val = o->size;
c152c796
AM
12097 break;
12098
12099 case DT_PREINIT_ARRAY:
12100 name = ".preinit_array";
4ade44b7 12101 goto get_out_vma;
c152c796
AM
12102 case DT_INIT_ARRAY:
12103 name = ".init_array";
4ade44b7 12104 goto get_out_vma;
c152c796
AM
12105 case DT_FINI_ARRAY:
12106 name = ".fini_array";
4ade44b7
AM
12107 get_out_vma:
12108 o = bfd_get_section_by_name (abfd, name);
12109 goto do_vma;
c152c796
AM
12110
12111 case DT_HASH:
12112 name = ".hash";
12113 goto get_vma;
fdc90cb4
JJ
12114 case DT_GNU_HASH:
12115 name = ".gnu.hash";
12116 goto get_vma;
c152c796
AM
12117 case DT_STRTAB:
12118 name = ".dynstr";
12119 goto get_vma;
12120 case DT_SYMTAB:
12121 name = ".dynsym";
12122 goto get_vma;
12123 case DT_VERDEF:
12124 name = ".gnu.version_d";
12125 goto get_vma;
12126 case DT_VERNEED:
12127 name = ".gnu.version_r";
12128 goto get_vma;
12129 case DT_VERSYM:
12130 name = ".gnu.version";
12131 get_vma:
4ade44b7
AM
12132 o = bfd_get_linker_section (dynobj, name);
12133 do_vma:
c152c796
AM
12134 if (o == NULL)
12135 {
4eca0228 12136 _bfd_error_handler
4ade44b7 12137 (_("could not find section %s"), name);
c152c796
AM
12138 goto error_return;
12139 }
894891db
NC
12140 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12141 {
4eca0228 12142 _bfd_error_handler
894891db
NC
12143 (_("warning: section '%s' is being made into a note"), name);
12144 bfd_set_error (bfd_error_nonrepresentable_section);
12145 goto error_return;
12146 }
4ade44b7 12147 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12148 break;
12149
12150 case DT_REL:
12151 case DT_RELA:
12152 case DT_RELSZ:
12153 case DT_RELASZ:
12154 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12155 type = SHT_REL;
12156 else
12157 type = SHT_RELA;
12158 dyn.d_un.d_val = 0;
bef26483 12159 dyn.d_un.d_ptr = 0;
c152c796
AM
12160 for (i = 1; i < elf_numsections (abfd); i++)
12161 {
12162 Elf_Internal_Shdr *hdr;
12163
12164 hdr = elf_elfsections (abfd)[i];
12165 if (hdr->sh_type == type
12166 && (hdr->sh_flags & SHF_ALLOC) != 0)
12167 {
12168 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12169 dyn.d_un.d_val += hdr->sh_size;
12170 else
12171 {
bef26483
AM
12172 if (dyn.d_un.d_ptr == 0
12173 || hdr->sh_addr < dyn.d_un.d_ptr)
12174 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
12175 }
12176 }
12177 }
64f52338
AM
12178 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12179 {
12180 /* Don't count procedure linkage table relocs in the
12181 overall reloc count. */
12182 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12183 dyn.d_un.d_val -= htab->srelplt->size;
12184 /* If .rela.plt is the first .rela section, exclude
12185 it from DT_RELA. */
12186 else if (dyn.d_un.d_ptr == (htab->srelplt->output_section->vma
12187 + htab->srelplt->output_offset))
12188 dyn.d_un.d_ptr += htab->srelplt->size;
12189 }
c152c796
AM
12190 break;
12191 }
12192 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12193 }
12194 }
12195
12196 /* If we have created any dynamic sections, then output them. */
12197 if (dynobj != NULL)
12198 {
12199 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12200 goto error_return;
12201
943284cc 12202 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12203 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12204 || info->error_textrel)
3d4d4302 12205 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12206 {
12207 bfd_byte *dyncon, *dynconend;
12208
943284cc
DJ
12209 dyncon = o->contents;
12210 dynconend = o->contents + o->size;
12211 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12212 {
12213 Elf_Internal_Dyn dyn;
12214
12215 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12216
12217 if (dyn.d_tag == DT_TEXTREL)
12218 {
c192a133
AM
12219 if (info->error_textrel)
12220 info->callbacks->einfo
12221 (_("%P%X: read-only segment has dynamic relocations.\n"));
12222 else
12223 info->callbacks->einfo
12224 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12225 break;
12226 }
12227 }
12228 }
12229
c152c796
AM
12230 for (o = dynobj->sections; o != NULL; o = o->next)
12231 {
12232 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12233 || o->size == 0
c152c796
AM
12234 || o->output_section == bfd_abs_section_ptr)
12235 continue;
12236 if ((o->flags & SEC_LINKER_CREATED) == 0)
12237 {
12238 /* At this point, we are only interested in sections
12239 created by _bfd_elf_link_create_dynamic_sections. */
12240 continue;
12241 }
64f52338 12242 if (htab->stab_info.stabstr == o)
3722b82f 12243 continue;
64f52338 12244 if (htab->eh_info.hdr_sec == o)
eea6121a 12245 continue;
3d4d4302 12246 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12247 {
12248 if (! bfd_set_section_contents (abfd, o->output_section,
12249 o->contents,
37b01f6a
DG
12250 (file_ptr) o->output_offset
12251 * bfd_octets_per_byte (abfd),
eea6121a 12252 o->size))
c152c796
AM
12253 goto error_return;
12254 }
12255 else
12256 {
12257 /* The contents of the .dynstr section are actually in a
12258 stringtab. */
8539e4e8
AM
12259 file_ptr off;
12260
c152c796
AM
12261 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12262 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12263 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12264 goto error_return;
12265 }
12266 }
12267 }
12268
0e1862bb 12269 if (bfd_link_relocatable (info))
c152c796
AM
12270 {
12271 bfd_boolean failed = FALSE;
12272
12273 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12274 if (failed)
12275 goto error_return;
12276 }
12277
12278 /* If we have optimized stabs strings, output them. */
64f52338 12279 if (htab->stab_info.stabstr != NULL)
c152c796 12280 {
64f52338 12281 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12282 goto error_return;
12283 }
12284
9f7c3e5e
AM
12285 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12286 goto error_return;
c152c796 12287
9f7c3e5e 12288 elf_final_link_free (abfd, &flinfo);
c152c796 12289
12bd6957 12290 elf_linker (abfd) = TRUE;
c152c796 12291
104d59d1
JM
12292 if (attr_section)
12293 {
a50b1753 12294 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12295 if (contents == NULL)
d0f16d5e 12296 return FALSE; /* Bail out and fail. */
104d59d1
JM
12297 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12298 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12299 free (contents);
12300 }
12301
c152c796
AM
12302 return TRUE;
12303
12304 error_return:
9f7c3e5e 12305 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12306 return FALSE;
12307}
12308\f
5241d853
RS
12309/* Initialize COOKIE for input bfd ABFD. */
12310
12311static bfd_boolean
12312init_reloc_cookie (struct elf_reloc_cookie *cookie,
12313 struct bfd_link_info *info, bfd *abfd)
12314{
12315 Elf_Internal_Shdr *symtab_hdr;
12316 const struct elf_backend_data *bed;
12317
12318 bed = get_elf_backend_data (abfd);
12319 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12320
12321 cookie->abfd = abfd;
12322 cookie->sym_hashes = elf_sym_hashes (abfd);
12323 cookie->bad_symtab = elf_bad_symtab (abfd);
12324 if (cookie->bad_symtab)
12325 {
12326 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12327 cookie->extsymoff = 0;
12328 }
12329 else
12330 {
12331 cookie->locsymcount = symtab_hdr->sh_info;
12332 cookie->extsymoff = symtab_hdr->sh_info;
12333 }
12334
12335 if (bed->s->arch_size == 32)
12336 cookie->r_sym_shift = 8;
12337 else
12338 cookie->r_sym_shift = 32;
12339
12340 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12341 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12342 {
12343 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12344 cookie->locsymcount, 0,
12345 NULL, NULL, NULL);
12346 if (cookie->locsyms == NULL)
12347 {
12348 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12349 return FALSE;
12350 }
12351 if (info->keep_memory)
12352 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12353 }
12354 return TRUE;
12355}
12356
12357/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12358
12359static void
12360fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12361{
12362 Elf_Internal_Shdr *symtab_hdr;
12363
12364 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12365 if (cookie->locsyms != NULL
12366 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12367 free (cookie->locsyms);
12368}
12369
12370/* Initialize the relocation information in COOKIE for input section SEC
12371 of input bfd ABFD. */
12372
12373static bfd_boolean
12374init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12375 struct bfd_link_info *info, bfd *abfd,
12376 asection *sec)
12377{
12378 const struct elf_backend_data *bed;
12379
12380 if (sec->reloc_count == 0)
12381 {
12382 cookie->rels = NULL;
12383 cookie->relend = NULL;
12384 }
12385 else
12386 {
12387 bed = get_elf_backend_data (abfd);
12388
12389 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12390 info->keep_memory);
12391 if (cookie->rels == NULL)
12392 return FALSE;
12393 cookie->rel = cookie->rels;
12394 cookie->relend = (cookie->rels
12395 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12396 }
12397 cookie->rel = cookie->rels;
12398 return TRUE;
12399}
12400
12401/* Free the memory allocated by init_reloc_cookie_rels,
12402 if appropriate. */
12403
12404static void
12405fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12406 asection *sec)
12407{
12408 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12409 free (cookie->rels);
12410}
12411
12412/* Initialize the whole of COOKIE for input section SEC. */
12413
12414static bfd_boolean
12415init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12416 struct bfd_link_info *info,
12417 asection *sec)
12418{
12419 if (!init_reloc_cookie (cookie, info, sec->owner))
12420 goto error1;
12421 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12422 goto error2;
12423 return TRUE;
12424
12425 error2:
12426 fini_reloc_cookie (cookie, sec->owner);
12427 error1:
12428 return FALSE;
12429}
12430
12431/* Free the memory allocated by init_reloc_cookie_for_section,
12432 if appropriate. */
12433
12434static void
12435fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12436 asection *sec)
12437{
12438 fini_reloc_cookie_rels (cookie, sec);
12439 fini_reloc_cookie (cookie, sec->owner);
12440}
12441\f
c152c796
AM
12442/* Garbage collect unused sections. */
12443
07adf181
AM
12444/* Default gc_mark_hook. */
12445
12446asection *
12447_bfd_elf_gc_mark_hook (asection *sec,
12448 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12449 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12450 struct elf_link_hash_entry *h,
12451 Elf_Internal_Sym *sym)
12452{
12453 if (h != NULL)
12454 {
12455 switch (h->root.type)
12456 {
12457 case bfd_link_hash_defined:
12458 case bfd_link_hash_defweak:
12459 return h->root.u.def.section;
12460
12461 case bfd_link_hash_common:
12462 return h->root.u.c.p->section;
12463
12464 default:
12465 break;
12466 }
12467 }
12468 else
12469 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12470
12471 return NULL;
12472}
12473
a6a4679f
AM
12474/* For undefined __start_<name> and __stop_<name> symbols, return the
12475 first input section matching <name>. Return NULL otherwise. */
12476
12477asection *
12478_bfd_elf_is_start_stop (const struct bfd_link_info *info,
12479 struct elf_link_hash_entry *h)
12480{
12481 asection *s;
12482 const char *sec_name;
12483
12484 if (h->root.type != bfd_link_hash_undefined
12485 && h->root.type != bfd_link_hash_undefweak)
12486 return NULL;
12487
12488 s = h->root.u.undef.section;
12489 if (s != NULL)
12490 {
12491 if (s == (asection *) 0 - 1)
12492 return NULL;
12493 return s;
12494 }
12495
12496 sec_name = NULL;
12497 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12498 sec_name = h->root.root.string + 8;
12499 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12500 sec_name = h->root.root.string + 7;
12501
12502 if (sec_name != NULL && *sec_name != '\0')
12503 {
12504 bfd *i;
12505
12506 for (i = info->input_bfds; i != NULL; i = i->link.next)
12507 {
12508 s = bfd_get_section_by_name (i, sec_name);
12509 if (s != NULL)
12510 {
12511 h->root.u.undef.section = s;
12512 break;
12513 }
12514 }
12515 }
12516
12517 if (s == NULL)
12518 h->root.u.undef.section = (asection *) 0 - 1;
12519
12520 return s;
12521}
12522
5241d853
RS
12523/* COOKIE->rel describes a relocation against section SEC, which is
12524 a section we've decided to keep. Return the section that contains
12525 the relocation symbol, or NULL if no section contains it. */
12526
12527asection *
12528_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12529 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12530 struct elf_reloc_cookie *cookie,
12531 bfd_boolean *start_stop)
5241d853
RS
12532{
12533 unsigned long r_symndx;
12534 struct elf_link_hash_entry *h;
12535
12536 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12537 if (r_symndx == STN_UNDEF)
5241d853
RS
12538 return NULL;
12539
12540 if (r_symndx >= cookie->locsymcount
12541 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12542 {
12543 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12544 if (h == NULL)
12545 {
12546 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12547 sec->owner);
12548 return NULL;
12549 }
5241d853
RS
12550 while (h->root.type == bfd_link_hash_indirect
12551 || h->root.type == bfd_link_hash_warning)
12552 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12553 h->mark = 1;
4e6b54a6
AM
12554 /* If this symbol is weak and there is a non-weak definition, we
12555 keep the non-weak definition because many backends put
12556 dynamic reloc info on the non-weak definition for code
12557 handling copy relocs. */
12558 if (h->u.weakdef != NULL)
12559 h->u.weakdef->mark = 1;
1cce69b9 12560
a6a4679f 12561 if (start_stop != NULL)
1cce69b9
AM
12562 {
12563 /* To work around a glibc bug, mark all XXX input sections
12564 when there is an as yet undefined reference to __start_XXX
12565 or __stop_XXX symbols. The linker will later define such
12566 symbols for orphan input sections that have a name
12567 representable as a C identifier. */
a6a4679f 12568 asection *s = _bfd_elf_is_start_stop (info, h);
1cce69b9 12569
a6a4679f 12570 if (s != NULL)
1cce69b9 12571 {
a6a4679f
AM
12572 *start_stop = !s->gc_mark;
12573 return s;
1cce69b9
AM
12574 }
12575 }
12576
5241d853
RS
12577 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12578 }
12579
12580 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12581 &cookie->locsyms[r_symndx]);
12582}
12583
12584/* COOKIE->rel describes a relocation against section SEC, which is
12585 a section we've decided to keep. Mark the section that contains
9d0a14d3 12586 the relocation symbol. */
5241d853
RS
12587
12588bfd_boolean
12589_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12590 asection *sec,
12591 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12592 struct elf_reloc_cookie *cookie)
5241d853
RS
12593{
12594 asection *rsec;
1cce69b9 12595 bfd_boolean start_stop = FALSE;
5241d853 12596
1cce69b9
AM
12597 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12598 while (rsec != NULL)
5241d853 12599 {
1cce69b9
AM
12600 if (!rsec->gc_mark)
12601 {
12602 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12603 || (rsec->owner->flags & DYNAMIC) != 0)
12604 rsec->gc_mark = 1;
12605 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12606 return FALSE;
12607 }
12608 if (!start_stop)
12609 break;
199af150 12610 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12611 }
12612 return TRUE;
12613}
12614
07adf181
AM
12615/* The mark phase of garbage collection. For a given section, mark
12616 it and any sections in this section's group, and all the sections
12617 which define symbols to which it refers. */
12618
ccfa59ea
AM
12619bfd_boolean
12620_bfd_elf_gc_mark (struct bfd_link_info *info,
12621 asection *sec,
6a5bb875 12622 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12623{
12624 bfd_boolean ret;
9d0a14d3 12625 asection *group_sec, *eh_frame;
c152c796
AM
12626
12627 sec->gc_mark = 1;
12628
12629 /* Mark all the sections in the group. */
12630 group_sec = elf_section_data (sec)->next_in_group;
12631 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12632 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12633 return FALSE;
12634
12635 /* Look through the section relocs. */
12636 ret = TRUE;
9d0a14d3
RS
12637 eh_frame = elf_eh_frame_section (sec->owner);
12638 if ((sec->flags & SEC_RELOC) != 0
12639 && sec->reloc_count > 0
12640 && sec != eh_frame)
c152c796 12641 {
5241d853 12642 struct elf_reloc_cookie cookie;
c152c796 12643
5241d853
RS
12644 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12645 ret = FALSE;
c152c796 12646 else
c152c796 12647 {
5241d853 12648 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12649 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12650 {
12651 ret = FALSE;
12652 break;
12653 }
12654 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12655 }
12656 }
9d0a14d3
RS
12657
12658 if (ret && eh_frame && elf_fde_list (sec))
12659 {
12660 struct elf_reloc_cookie cookie;
12661
12662 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12663 ret = FALSE;
12664 else
12665 {
12666 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12667 gc_mark_hook, &cookie))
12668 ret = FALSE;
12669 fini_reloc_cookie_for_section (&cookie, eh_frame);
12670 }
12671 }
12672
2f0c68f2
CM
12673 eh_frame = elf_section_eh_frame_entry (sec);
12674 if (ret && eh_frame && !eh_frame->gc_mark)
12675 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12676 ret = FALSE;
12677
c152c796
AM
12678 return ret;
12679}
12680
3c758495
TG
12681/* Scan and mark sections in a special or debug section group. */
12682
12683static void
12684_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12685{
12686 /* Point to first section of section group. */
12687 asection *ssec;
12688 /* Used to iterate the section group. */
12689 asection *msec;
12690
12691 bfd_boolean is_special_grp = TRUE;
12692 bfd_boolean is_debug_grp = TRUE;
12693
12694 /* First scan to see if group contains any section other than debug
12695 and special section. */
12696 ssec = msec = elf_next_in_group (grp);
12697 do
12698 {
12699 if ((msec->flags & SEC_DEBUGGING) == 0)
12700 is_debug_grp = FALSE;
12701
12702 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12703 is_special_grp = FALSE;
12704
12705 msec = elf_next_in_group (msec);
12706 }
12707 while (msec != ssec);
12708
12709 /* If this is a pure debug section group or pure special section group,
12710 keep all sections in this group. */
12711 if (is_debug_grp || is_special_grp)
12712 {
12713 do
12714 {
12715 msec->gc_mark = 1;
12716 msec = elf_next_in_group (msec);
12717 }
12718 while (msec != ssec);
12719 }
12720}
12721
7f6ab9f8
AM
12722/* Keep debug and special sections. */
12723
12724bfd_boolean
12725_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12726 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12727{
12728 bfd *ibfd;
12729
c72f2fb2 12730 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12731 {
12732 asection *isec;
12733 bfd_boolean some_kept;
b40bf0a2 12734 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12735
12736 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12737 continue;
12738
b40bf0a2
NC
12739 /* Ensure all linker created sections are kept,
12740 see if any other section is already marked,
12741 and note if we have any fragmented debug sections. */
12742 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12743 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12744 {
12745 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12746 isec->gc_mark = 1;
12747 else if (isec->gc_mark)
12748 some_kept = TRUE;
b40bf0a2
NC
12749
12750 if (debug_frag_seen == FALSE
12751 && (isec->flags & SEC_DEBUGGING)
12752 && CONST_STRNEQ (isec->name, ".debug_line."))
12753 debug_frag_seen = TRUE;
7f6ab9f8
AM
12754 }
12755
12756 /* If no section in this file will be kept, then we can
b40bf0a2 12757 toss out the debug and special sections. */
7f6ab9f8
AM
12758 if (!some_kept)
12759 continue;
12760
12761 /* Keep debug and special sections like .comment when they are
3c758495
TG
12762 not part of a group. Also keep section groups that contain
12763 just debug sections or special sections. */
7f6ab9f8 12764 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12765 {
12766 if ((isec->flags & SEC_GROUP) != 0)
12767 _bfd_elf_gc_mark_debug_special_section_group (isec);
12768 else if (((isec->flags & SEC_DEBUGGING) != 0
12769 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12770 && elf_next_in_group (isec) == NULL)
12771 isec->gc_mark = 1;
12772 }
b40bf0a2
NC
12773
12774 if (! debug_frag_seen)
12775 continue;
12776
12777 /* Look for CODE sections which are going to be discarded,
12778 and find and discard any fragmented debug sections which
12779 are associated with that code section. */
12780 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12781 if ((isec->flags & SEC_CODE) != 0
12782 && isec->gc_mark == 0)
12783 {
12784 unsigned int ilen;
12785 asection *dsec;
12786
12787 ilen = strlen (isec->name);
12788
12789 /* Association is determined by the name of the debug section
12790 containing the name of the code section as a suffix. For
12791 example .debug_line.text.foo is a debug section associated
12792 with .text.foo. */
12793 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12794 {
12795 unsigned int dlen;
12796
12797 if (dsec->gc_mark == 0
12798 || (dsec->flags & SEC_DEBUGGING) == 0)
12799 continue;
12800
12801 dlen = strlen (dsec->name);
12802
12803 if (dlen > ilen
12804 && strncmp (dsec->name + (dlen - ilen),
12805 isec->name, ilen) == 0)
12806 {
12807 dsec->gc_mark = 0;
b40bf0a2
NC
12808 }
12809 }
12810 }
7f6ab9f8
AM
12811 }
12812 return TRUE;
12813}
12814
c152c796
AM
12815/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12816
c17d87de
NC
12817struct elf_gc_sweep_symbol_info
12818{
ccabcbe5
AM
12819 struct bfd_link_info *info;
12820 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12821 bfd_boolean);
12822};
12823
c152c796 12824static bfd_boolean
ccabcbe5 12825elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 12826{
1d5316ab
AM
12827 if (!h->mark
12828 && (((h->root.type == bfd_link_hash_defined
12829 || h->root.type == bfd_link_hash_defweak)
c4621b33 12830 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6673f753 12831 && h->root.u.def.section->gc_mark))
1d5316ab
AM
12832 || h->root.type == bfd_link_hash_undefined
12833 || h->root.type == bfd_link_hash_undefweak))
12834 {
12835 struct elf_gc_sweep_symbol_info *inf;
12836
12837 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 12838 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
12839 h->def_regular = 0;
12840 h->ref_regular = 0;
12841 h->ref_regular_nonweak = 0;
ccabcbe5 12842 }
c152c796
AM
12843
12844 return TRUE;
12845}
12846
12847/* The sweep phase of garbage collection. Remove all garbage sections. */
12848
12849typedef bfd_boolean (*gc_sweep_hook_fn)
12850 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12851
12852static bfd_boolean
ccabcbe5 12853elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12854{
12855 bfd *sub;
ccabcbe5
AM
12856 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12857 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12858 unsigned long section_sym_count;
12859 struct elf_gc_sweep_symbol_info sweep_info;
c152c796 12860
c72f2fb2 12861 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12862 {
12863 asection *o;
12864
b19a8f85
L
12865 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12866 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12867 continue;
12868
12869 for (o = sub->sections; o != NULL; o = o->next)
12870 {
a33dafc3
L
12871 /* When any section in a section group is kept, we keep all
12872 sections in the section group. If the first member of
12873 the section group is excluded, we will also exclude the
12874 group section. */
12875 if (o->flags & SEC_GROUP)
12876 {
12877 asection *first = elf_next_in_group (o);
12878 o->gc_mark = first->gc_mark;
12879 }
c152c796 12880
1e7eae0d 12881 if (o->gc_mark)
c152c796
AM
12882 continue;
12883
12884 /* Skip sweeping sections already excluded. */
12885 if (o->flags & SEC_EXCLUDE)
12886 continue;
12887
12888 /* Since this is early in the link process, it is simple
12889 to remove a section from the output. */
12890 o->flags |= SEC_EXCLUDE;
12891
c55fe096 12892 if (info->print_gc_sections && o->size != 0)
695344c0 12893 /* xgettext:c-format */
c17d87de
NC
12894 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12895
c152c796
AM
12896 /* But we also have to update some of the relocation
12897 info we collected before. */
12898 if (gc_sweep_hook
e8aaee2a 12899 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12900 && o->reloc_count != 0
12901 && !((info->strip == strip_all || info->strip == strip_debugger)
12902 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12903 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12904 {
12905 Elf_Internal_Rela *internal_relocs;
12906 bfd_boolean r;
12907
12908 internal_relocs
12909 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12910 info->keep_memory);
12911 if (internal_relocs == NULL)
12912 return FALSE;
12913
12914 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12915
12916 if (elf_section_data (o)->relocs != internal_relocs)
12917 free (internal_relocs);
12918
12919 if (!r)
12920 return FALSE;
12921 }
12922 }
12923 }
12924
12925 /* Remove the symbols that were in the swept sections from the dynamic
12926 symbol table. GCFIXME: Anyone know how to get them out of the
12927 static symbol table as well? */
ccabcbe5
AM
12928 sweep_info.info = info;
12929 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12930 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12931 &sweep_info);
c152c796 12932
ccabcbe5 12933 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
12934 return TRUE;
12935}
12936
12937/* Propagate collected vtable information. This is called through
12938 elf_link_hash_traverse. */
12939
12940static bfd_boolean
12941elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12942{
c152c796 12943 /* Those that are not vtables. */
f6e332e6 12944 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12945 return TRUE;
12946
12947 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12948 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12949 return TRUE;
12950
12951 /* If we've already been done, exit. */
f6e332e6 12952 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12953 return TRUE;
12954
12955 /* Make sure the parent's table is up to date. */
f6e332e6 12956 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 12957
f6e332e6 12958 if (h->vtable->used == NULL)
c152c796
AM
12959 {
12960 /* None of this table's entries were referenced. Re-use the
12961 parent's table. */
f6e332e6
AM
12962 h->vtable->used = h->vtable->parent->vtable->used;
12963 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
12964 }
12965 else
12966 {
12967 size_t n;
12968 bfd_boolean *cu, *pu;
12969
12970 /* Or the parent's entries into ours. */
f6e332e6 12971 cu = h->vtable->used;
c152c796 12972 cu[-1] = TRUE;
f6e332e6 12973 pu = h->vtable->parent->vtable->used;
c152c796
AM
12974 if (pu != NULL)
12975 {
12976 const struct elf_backend_data *bed;
12977 unsigned int log_file_align;
12978
12979 bed = get_elf_backend_data (h->root.u.def.section->owner);
12980 log_file_align = bed->s->log_file_align;
f6e332e6 12981 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
12982 while (n--)
12983 {
12984 if (*pu)
12985 *cu = TRUE;
12986 pu++;
12987 cu++;
12988 }
12989 }
12990 }
12991
12992 return TRUE;
12993}
12994
12995static bfd_boolean
12996elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12997{
12998 asection *sec;
12999 bfd_vma hstart, hend;
13000 Elf_Internal_Rela *relstart, *relend, *rel;
13001 const struct elf_backend_data *bed;
13002 unsigned int log_file_align;
13003
c152c796
AM
13004 /* Take care of both those symbols that do not describe vtables as
13005 well as those that are not loaded. */
f6e332e6 13006 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
13007 return TRUE;
13008
13009 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13010 || h->root.type == bfd_link_hash_defweak);
13011
13012 sec = h->root.u.def.section;
13013 hstart = h->root.u.def.value;
13014 hend = hstart + h->size;
13015
13016 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13017 if (!relstart)
13018 return *(bfd_boolean *) okp = FALSE;
13019 bed = get_elf_backend_data (sec->owner);
13020 log_file_align = bed->s->log_file_align;
13021
13022 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
13023
13024 for (rel = relstart; rel < relend; ++rel)
13025 if (rel->r_offset >= hstart && rel->r_offset < hend)
13026 {
13027 /* If the entry is in use, do nothing. */
f6e332e6
AM
13028 if (h->vtable->used
13029 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
13030 {
13031 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 13032 if (h->vtable->used[entry])
c152c796
AM
13033 continue;
13034 }
13035 /* Otherwise, kill it. */
13036 rel->r_offset = rel->r_info = rel->r_addend = 0;
13037 }
13038
13039 return TRUE;
13040}
13041
87538722
AM
13042/* Mark sections containing dynamically referenced symbols. When
13043 building shared libraries, we must assume that any visible symbol is
13044 referenced. */
715df9b8 13045
64d03ab5
AM
13046bfd_boolean
13047bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13048{
87538722 13049 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13050 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13051
715df9b8
EB
13052 if ((h->root.type == bfd_link_hash_defined
13053 || h->root.type == bfd_link_hash_defweak)
87538722 13054 && (h->ref_dynamic
c4621b33 13055 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13056 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13057 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13058 && (!bfd_link_executable (info)
b407645f
AM
13059 || info->export_dynamic
13060 || (h->dynamic
13061 && d != NULL
13062 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13063 && (h->versioned >= versioned
54e8959c
L
13064 || !bfd_hide_sym_by_version (info->version_info,
13065 h->root.root.string)))))
715df9b8
EB
13066 h->root.u.def.section->flags |= SEC_KEEP;
13067
13068 return TRUE;
13069}
3b36f7e6 13070
74f0fb50
AM
13071/* Keep all sections containing symbols undefined on the command-line,
13072 and the section containing the entry symbol. */
13073
13074void
13075_bfd_elf_gc_keep (struct bfd_link_info *info)
13076{
13077 struct bfd_sym_chain *sym;
13078
13079 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13080 {
13081 struct elf_link_hash_entry *h;
13082
13083 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13084 FALSE, FALSE, FALSE);
13085
13086 if (h != NULL
13087 && (h->root.type == bfd_link_hash_defined
13088 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13089 && !bfd_is_abs_section (h->root.u.def.section)
13090 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13091 h->root.u.def.section->flags |= SEC_KEEP;
13092 }
13093}
13094
2f0c68f2
CM
13095bfd_boolean
13096bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13097 struct bfd_link_info *info)
13098{
13099 bfd *ibfd = info->input_bfds;
13100
13101 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13102 {
13103 asection *sec;
13104 struct elf_reloc_cookie cookie;
13105
13106 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13107 continue;
13108
13109 if (!init_reloc_cookie (&cookie, info, ibfd))
13110 return FALSE;
13111
13112 for (sec = ibfd->sections; sec; sec = sec->next)
13113 {
13114 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13115 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13116 {
13117 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13118 fini_reloc_cookie_rels (&cookie, sec);
13119 }
13120 }
13121 }
13122 return TRUE;
13123}
13124
c152c796
AM
13125/* Do mark and sweep of unused sections. */
13126
13127bfd_boolean
13128bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13129{
13130 bfd_boolean ok = TRUE;
13131 bfd *sub;
6a5bb875 13132 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13133 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13134 struct elf_link_hash_table *htab;
c152c796 13135
64d03ab5 13136 if (!bed->can_gc_sections
715df9b8 13137 || !is_elf_hash_table (info->hash))
c152c796 13138 {
4eca0228 13139 _bfd_error_handler(_("Warning: gc-sections option ignored"));
c152c796
AM
13140 return TRUE;
13141 }
13142
74f0fb50 13143 bed->gc_keep (info);
da44f4e5 13144 htab = elf_hash_table (info);
74f0fb50 13145
9d0a14d3
RS
13146 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13147 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13148 for (sub = info->input_bfds;
13149 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13150 sub = sub->link.next)
9d0a14d3
RS
13151 {
13152 asection *sec;
13153 struct elf_reloc_cookie cookie;
13154
13155 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13156 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13157 {
13158 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13159 if (elf_section_data (sec)->sec_info
13160 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13161 elf_eh_frame_section (sub) = sec;
13162 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13163 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13164 }
13165 }
9d0a14d3 13166
c152c796 13167 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13168 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13169 if (!ok)
13170 return FALSE;
13171
13172 /* Kill the vtable relocations that were not used. */
da44f4e5 13173 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13174 if (!ok)
13175 return FALSE;
13176
715df9b8 13177 /* Mark dynamically referenced symbols. */
da44f4e5
AM
13178 if (htab->dynamic_sections_created)
13179 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13180
715df9b8 13181 /* Grovel through relocs to find out who stays ... */
64d03ab5 13182 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13183 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13184 {
13185 asection *o;
13186
b19a8f85
L
13187 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13188 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13189 continue;
13190
7f6ab9f8
AM
13191 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13192 Also treat note sections as a root, if the section is not part
13193 of a group. */
c152c796 13194 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13195 if (!o->gc_mark
13196 && (o->flags & SEC_EXCLUDE) == 0
24007750 13197 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13198 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13199 && elf_next_in_group (o) == NULL )))
13200 {
13201 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13202 return FALSE;
13203 }
c152c796
AM
13204 }
13205
6a5bb875 13206 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13207 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13208
c152c796 13209 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13210 return elf_gc_sweep (abfd, info);
c152c796
AM
13211}
13212\f
13213/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13214
13215bfd_boolean
13216bfd_elf_gc_record_vtinherit (bfd *abfd,
13217 asection *sec,
13218 struct elf_link_hash_entry *h,
13219 bfd_vma offset)
13220{
13221 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13222 struct elf_link_hash_entry **search, *child;
ef53be89 13223 size_t extsymcount;
c152c796
AM
13224 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13225
13226 /* The sh_info field of the symtab header tells us where the
13227 external symbols start. We don't care about the local symbols at
13228 this point. */
13229 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13230 if (!elf_bad_symtab (abfd))
13231 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13232
13233 sym_hashes = elf_sym_hashes (abfd);
13234 sym_hashes_end = sym_hashes + extsymcount;
13235
13236 /* Hunt down the child symbol, which is in this section at the same
13237 offset as the relocation. */
13238 for (search = sym_hashes; search != sym_hashes_end; ++search)
13239 {
13240 if ((child = *search) != NULL
13241 && (child->root.type == bfd_link_hash_defined
13242 || child->root.type == bfd_link_hash_defweak)
13243 && child->root.u.def.section == sec
13244 && child->root.u.def.value == offset)
13245 goto win;
13246 }
13247
695344c0
NC
13248 /* xgettext:c-format */
13249 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
4eca0228 13250 abfd, sec, (unsigned long) offset);
c152c796
AM
13251 bfd_set_error (bfd_error_invalid_operation);
13252 return FALSE;
13253
13254 win:
f6e332e6
AM
13255 if (!child->vtable)
13256 {
ca4be51c
AM
13257 child->vtable = ((struct elf_link_virtual_table_entry *)
13258 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
13259 if (!child->vtable)
13260 return FALSE;
13261 }
c152c796
AM
13262 if (!h)
13263 {
13264 /* This *should* only be the absolute section. It could potentially
13265 be that someone has defined a non-global vtable though, which
13266 would be bad. It isn't worth paging in the local symbols to be
13267 sure though; that case should simply be handled by the assembler. */
13268
f6e332e6 13269 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13270 }
13271 else
f6e332e6 13272 child->vtable->parent = h;
c152c796
AM
13273
13274 return TRUE;
13275}
13276
13277/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13278
13279bfd_boolean
13280bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13281 asection *sec ATTRIBUTE_UNUSED,
13282 struct elf_link_hash_entry *h,
13283 bfd_vma addend)
13284{
13285 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13286 unsigned int log_file_align = bed->s->log_file_align;
13287
f6e332e6
AM
13288 if (!h->vtable)
13289 {
ca4be51c
AM
13290 h->vtable = ((struct elf_link_virtual_table_entry *)
13291 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
13292 if (!h->vtable)
13293 return FALSE;
13294 }
13295
13296 if (addend >= h->vtable->size)
c152c796
AM
13297 {
13298 size_t size, bytes, file_align;
f6e332e6 13299 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
13300
13301 /* While the symbol is undefined, we have to be prepared to handle
13302 a zero size. */
13303 file_align = 1 << log_file_align;
13304 if (h->root.type == bfd_link_hash_undefined)
13305 size = addend + file_align;
13306 else
13307 {
13308 size = h->size;
13309 if (addend >= size)
13310 {
13311 /* Oops! We've got a reference past the defined end of
13312 the table. This is probably a bug -- shall we warn? */
13313 size = addend + file_align;
13314 }
13315 }
13316 size = (size + file_align - 1) & -file_align;
13317
13318 /* Allocate one extra entry for use as a "done" flag for the
13319 consolidation pass. */
13320 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13321
13322 if (ptr)
13323 {
a50b1753 13324 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13325
13326 if (ptr != NULL)
13327 {
13328 size_t oldbytes;
13329
f6e332e6 13330 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
13331 * sizeof (bfd_boolean));
13332 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13333 }
13334 }
13335 else
a50b1753 13336 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13337
13338 if (ptr == NULL)
13339 return FALSE;
13340
13341 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
13342 h->vtable->used = ptr + 1;
13343 h->vtable->size = size;
c152c796
AM
13344 }
13345
f6e332e6 13346 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13347
13348 return TRUE;
13349}
13350
ae17ab41
CM
13351/* Map an ELF section header flag to its corresponding string. */
13352typedef struct
13353{
13354 char *flag_name;
13355 flagword flag_value;
13356} elf_flags_to_name_table;
13357
13358static elf_flags_to_name_table elf_flags_to_names [] =
13359{
13360 { "SHF_WRITE", SHF_WRITE },
13361 { "SHF_ALLOC", SHF_ALLOC },
13362 { "SHF_EXECINSTR", SHF_EXECINSTR },
13363 { "SHF_MERGE", SHF_MERGE },
13364 { "SHF_STRINGS", SHF_STRINGS },
13365 { "SHF_INFO_LINK", SHF_INFO_LINK},
13366 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13367 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13368 { "SHF_GROUP", SHF_GROUP },
13369 { "SHF_TLS", SHF_TLS },
13370 { "SHF_MASKOS", SHF_MASKOS },
13371 { "SHF_EXCLUDE", SHF_EXCLUDE },
13372};
13373
b9c361e0
JL
13374/* Returns TRUE if the section is to be included, otherwise FALSE. */
13375bfd_boolean
ae17ab41 13376bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13377 struct flag_info *flaginfo,
b9c361e0 13378 asection *section)
ae17ab41 13379{
8b127cbc 13380 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13381
8b127cbc 13382 if (!flaginfo->flags_initialized)
ae17ab41 13383 {
8b127cbc
AM
13384 bfd *obfd = info->output_bfd;
13385 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13386 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13387 int with_hex = 0;
13388 int without_hex = 0;
13389
8b127cbc 13390 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13391 {
b9c361e0 13392 unsigned i;
8b127cbc 13393 flagword (*lookup) (char *);
ae17ab41 13394
8b127cbc
AM
13395 lookup = bed->elf_backend_lookup_section_flags_hook;
13396 if (lookup != NULL)
ae17ab41 13397 {
8b127cbc 13398 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13399
13400 if (hexval != 0)
13401 {
13402 if (tf->with == with_flags)
13403 with_hex |= hexval;
13404 else if (tf->with == without_flags)
13405 without_hex |= hexval;
13406 tf->valid = TRUE;
13407 continue;
13408 }
ae17ab41 13409 }
8b127cbc 13410 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13411 {
8b127cbc 13412 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13413 {
13414 if (tf->with == with_flags)
13415 with_hex |= elf_flags_to_names[i].flag_value;
13416 else if (tf->with == without_flags)
13417 without_hex |= elf_flags_to_names[i].flag_value;
13418 tf->valid = TRUE;
13419 break;
13420 }
13421 }
8b127cbc 13422 if (!tf->valid)
b9c361e0 13423 {
68ffbac6 13424 info->callbacks->einfo
8b127cbc 13425 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13426 return FALSE;
ae17ab41
CM
13427 }
13428 }
8b127cbc
AM
13429 flaginfo->flags_initialized = TRUE;
13430 flaginfo->only_with_flags |= with_hex;
13431 flaginfo->not_with_flags |= without_hex;
ae17ab41 13432 }
ae17ab41 13433
8b127cbc 13434 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13435 return FALSE;
13436
8b127cbc 13437 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13438 return FALSE;
13439
13440 return TRUE;
ae17ab41
CM
13441}
13442
c152c796
AM
13443struct alloc_got_off_arg {
13444 bfd_vma gotoff;
10455f89 13445 struct bfd_link_info *info;
c152c796
AM
13446};
13447
13448/* We need a special top-level link routine to convert got reference counts
13449 to real got offsets. */
13450
13451static bfd_boolean
13452elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13453{
a50b1753 13454 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13455 bfd *obfd = gofarg->info->output_bfd;
13456 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13457
c152c796
AM
13458 if (h->got.refcount > 0)
13459 {
13460 h->got.offset = gofarg->gotoff;
10455f89 13461 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13462 }
13463 else
13464 h->got.offset = (bfd_vma) -1;
13465
13466 return TRUE;
13467}
13468
13469/* And an accompanying bit to work out final got entry offsets once
13470 we're done. Should be called from final_link. */
13471
13472bfd_boolean
13473bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13474 struct bfd_link_info *info)
13475{
13476 bfd *i;
13477 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13478 bfd_vma gotoff;
c152c796
AM
13479 struct alloc_got_off_arg gofarg;
13480
10455f89
HPN
13481 BFD_ASSERT (abfd == info->output_bfd);
13482
c152c796
AM
13483 if (! is_elf_hash_table (info->hash))
13484 return FALSE;
13485
13486 /* The GOT offset is relative to the .got section, but the GOT header is
13487 put into the .got.plt section, if the backend uses it. */
13488 if (bed->want_got_plt)
13489 gotoff = 0;
13490 else
13491 gotoff = bed->got_header_size;
13492
13493 /* Do the local .got entries first. */
c72f2fb2 13494 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13495 {
13496 bfd_signed_vma *local_got;
ef53be89 13497 size_t j, locsymcount;
c152c796
AM
13498 Elf_Internal_Shdr *symtab_hdr;
13499
13500 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13501 continue;
13502
13503 local_got = elf_local_got_refcounts (i);
13504 if (!local_got)
13505 continue;
13506
13507 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13508 if (elf_bad_symtab (i))
13509 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13510 else
13511 locsymcount = symtab_hdr->sh_info;
13512
13513 for (j = 0; j < locsymcount; ++j)
13514 {
13515 if (local_got[j] > 0)
13516 {
13517 local_got[j] = gotoff;
10455f89 13518 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13519 }
13520 else
13521 local_got[j] = (bfd_vma) -1;
13522 }
13523 }
13524
13525 /* Then the global .got entries. .plt refcounts are handled by
13526 adjust_dynamic_symbol */
13527 gofarg.gotoff = gotoff;
10455f89 13528 gofarg.info = info;
c152c796
AM
13529 elf_link_hash_traverse (elf_hash_table (info),
13530 elf_gc_allocate_got_offsets,
13531 &gofarg);
13532 return TRUE;
13533}
13534
13535/* Many folk need no more in the way of final link than this, once
13536 got entry reference counting is enabled. */
13537
13538bfd_boolean
13539bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13540{
13541 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13542 return FALSE;
13543
13544 /* Invoke the regular ELF backend linker to do all the work. */
13545 return bfd_elf_final_link (abfd, info);
13546}
13547
13548bfd_boolean
13549bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13550{
a50b1753 13551 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13552
13553 if (rcookie->bad_symtab)
13554 rcookie->rel = rcookie->rels;
13555
13556 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13557 {
13558 unsigned long r_symndx;
13559
13560 if (! rcookie->bad_symtab)
13561 if (rcookie->rel->r_offset > offset)
13562 return FALSE;
13563 if (rcookie->rel->r_offset != offset)
13564 continue;
13565
13566 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13567 if (r_symndx == STN_UNDEF)
c152c796
AM
13568 return TRUE;
13569
13570 if (r_symndx >= rcookie->locsymcount
13571 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13572 {
13573 struct elf_link_hash_entry *h;
13574
13575 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13576
13577 while (h->root.type == bfd_link_hash_indirect
13578 || h->root.type == bfd_link_hash_warning)
13579 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13580
13581 if ((h->root.type == bfd_link_hash_defined
13582 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13583 && (h->root.u.def.section->owner != rcookie->abfd
13584 || h->root.u.def.section->kept_section != NULL
13585 || discarded_section (h->root.u.def.section)))
c152c796 13586 return TRUE;
c152c796
AM
13587 }
13588 else
13589 {
13590 /* It's not a relocation against a global symbol,
13591 but it could be a relocation against a local
13592 symbol for a discarded section. */
13593 asection *isec;
13594 Elf_Internal_Sym *isym;
13595
13596 /* Need to: get the symbol; get the section. */
13597 isym = &rcookie->locsyms[r_symndx];
cb33740c 13598 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13599 if (isec != NULL
13600 && (isec->kept_section != NULL
13601 || discarded_section (isec)))
cb33740c 13602 return TRUE;
c152c796
AM
13603 }
13604 return FALSE;
13605 }
13606 return FALSE;
13607}
13608
13609/* Discard unneeded references to discarded sections.
75938853
AM
13610 Returns -1 on error, 1 if any section's size was changed, 0 if
13611 nothing changed. This function assumes that the relocations are in
13612 sorted order, which is true for all known assemblers. */
c152c796 13613
75938853 13614int
c152c796
AM
13615bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13616{
13617 struct elf_reloc_cookie cookie;
18cd5bce 13618 asection *o;
c152c796 13619 bfd *abfd;
75938853 13620 int changed = 0;
c152c796
AM
13621
13622 if (info->traditional_format
13623 || !is_elf_hash_table (info->hash))
75938853 13624 return 0;
c152c796 13625
18cd5bce
AM
13626 o = bfd_get_section_by_name (output_bfd, ".stab");
13627 if (o != NULL)
c152c796 13628 {
18cd5bce 13629 asection *i;
c152c796 13630
18cd5bce 13631 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13632 {
18cd5bce
AM
13633 if (i->size == 0
13634 || i->reloc_count == 0
13635 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13636 continue;
c152c796 13637
18cd5bce
AM
13638 abfd = i->owner;
13639 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13640 continue;
c152c796 13641
18cd5bce 13642 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13643 return -1;
c152c796 13644
18cd5bce
AM
13645 if (_bfd_discard_section_stabs (abfd, i,
13646 elf_section_data (i)->sec_info,
5241d853
RS
13647 bfd_elf_reloc_symbol_deleted_p,
13648 &cookie))
75938853 13649 changed = 1;
18cd5bce
AM
13650
13651 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13652 }
18cd5bce
AM
13653 }
13654
2f0c68f2
CM
13655 o = NULL;
13656 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13657 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13658 if (o != NULL)
13659 {
13660 asection *i;
c152c796 13661
18cd5bce 13662 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13663 {
18cd5bce
AM
13664 if (i->size == 0)
13665 continue;
13666
13667 abfd = i->owner;
13668 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13669 continue;
13670
13671 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13672 return -1;
18cd5bce
AM
13673
13674 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13675 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13676 bfd_elf_reloc_symbol_deleted_p,
13677 &cookie))
75938853 13678 changed = 1;
18cd5bce
AM
13679
13680 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13681 }
18cd5bce 13682 }
c152c796 13683
18cd5bce
AM
13684 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13685 {
13686 const struct elf_backend_data *bed;
c152c796 13687
18cd5bce
AM
13688 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13689 continue;
13690
13691 bed = get_elf_backend_data (abfd);
13692
13693 if (bed->elf_backend_discard_info != NULL)
13694 {
13695 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13696 return -1;
18cd5bce
AM
13697
13698 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13699 changed = 1;
18cd5bce
AM
13700
13701 fini_reloc_cookie (&cookie, abfd);
13702 }
c152c796
AM
13703 }
13704
2f0c68f2
CM
13705 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13706 _bfd_elf_end_eh_frame_parsing (info);
13707
13708 if (info->eh_frame_hdr_type
0e1862bb 13709 && !bfd_link_relocatable (info)
c152c796 13710 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13711 changed = 1;
c152c796 13712
75938853 13713 return changed;
c152c796 13714}
082b7297 13715
43e1669b 13716bfd_boolean
0c511000 13717_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13718 asection *sec,
c0f00686 13719 struct bfd_link_info *info)
082b7297
L
13720{
13721 flagword flags;
c77ec726 13722 const char *name, *key;
082b7297
L
13723 struct bfd_section_already_linked *l;
13724 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13725
c77ec726
AM
13726 if (sec->output_section == bfd_abs_section_ptr)
13727 return FALSE;
0c511000 13728
c77ec726 13729 flags = sec->flags;
0c511000 13730
c77ec726
AM
13731 /* Return if it isn't a linkonce section. A comdat group section
13732 also has SEC_LINK_ONCE set. */
13733 if ((flags & SEC_LINK_ONCE) == 0)
13734 return FALSE;
0c511000 13735
c77ec726
AM
13736 /* Don't put group member sections on our list of already linked
13737 sections. They are handled as a group via their group section. */
13738 if (elf_sec_group (sec) != NULL)
13739 return FALSE;
0c511000 13740
c77ec726
AM
13741 /* For a SHT_GROUP section, use the group signature as the key. */
13742 name = sec->name;
13743 if ((flags & SEC_GROUP) != 0
13744 && elf_next_in_group (sec) != NULL
13745 && elf_group_name (elf_next_in_group (sec)) != NULL)
13746 key = elf_group_name (elf_next_in_group (sec));
13747 else
13748 {
13749 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13750 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13751 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13752 key++;
0c511000 13753 else
c77ec726
AM
13754 /* Must be a user linkonce section that doesn't follow gcc's
13755 naming convention. In this case we won't be matching
13756 single member groups. */
13757 key = name;
0c511000 13758 }
6d2cd210 13759
c77ec726 13760 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13761
13762 for (l = already_linked_list->entry; l != NULL; l = l->next)
13763 {
c2370991 13764 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13765 sections with a signature of <key> (<key> is some string),
13766 and linkonce sections named .gnu.linkonce.<type>.<key>.
13767 Match like sections. LTO plugin sections are an exception.
13768 They are always named .gnu.linkonce.t.<key> and match either
13769 type of section. */
13770 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13771 && ((flags & SEC_GROUP) != 0
13772 || strcmp (name, l->sec->name) == 0))
13773 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13774 {
13775 /* The section has already been linked. See if we should
6d2cd210 13776 issue a warning. */
c77ec726
AM
13777 if (!_bfd_handle_already_linked (sec, l, info))
13778 return FALSE;
082b7297 13779
c77ec726 13780 if (flags & SEC_GROUP)
3d7f7666 13781 {
c77ec726
AM
13782 asection *first = elf_next_in_group (sec);
13783 asection *s = first;
3d7f7666 13784
c77ec726 13785 while (s != NULL)
3d7f7666 13786 {
c77ec726
AM
13787 s->output_section = bfd_abs_section_ptr;
13788 /* Record which group discards it. */
13789 s->kept_section = l->sec;
13790 s = elf_next_in_group (s);
13791 /* These lists are circular. */
13792 if (s == first)
13793 break;
3d7f7666
L
13794 }
13795 }
082b7297 13796
43e1669b 13797 return TRUE;
082b7297
L
13798 }
13799 }
13800
c77ec726
AM
13801 /* A single member comdat group section may be discarded by a
13802 linkonce section and vice versa. */
13803 if ((flags & SEC_GROUP) != 0)
3d7f7666 13804 {
c77ec726 13805 asection *first = elf_next_in_group (sec);
c2370991 13806
c77ec726
AM
13807 if (first != NULL && elf_next_in_group (first) == first)
13808 /* Check this single member group against linkonce sections. */
13809 for (l = already_linked_list->entry; l != NULL; l = l->next)
13810 if ((l->sec->flags & SEC_GROUP) == 0
13811 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13812 {
13813 first->output_section = bfd_abs_section_ptr;
13814 first->kept_section = l->sec;
13815 sec->output_section = bfd_abs_section_ptr;
13816 break;
13817 }
13818 }
13819 else
13820 /* Check this linkonce section against single member groups. */
13821 for (l = already_linked_list->entry; l != NULL; l = l->next)
13822 if (l->sec->flags & SEC_GROUP)
6d2cd210 13823 {
c77ec726 13824 asection *first = elf_next_in_group (l->sec);
6d2cd210 13825
c77ec726
AM
13826 if (first != NULL
13827 && elf_next_in_group (first) == first
13828 && bfd_elf_match_symbols_in_sections (first, sec, info))
13829 {
13830 sec->output_section = bfd_abs_section_ptr;
13831 sec->kept_section = first;
13832 break;
13833 }
6d2cd210 13834 }
0c511000 13835
c77ec726
AM
13836 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13837 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13838 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13839 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13840 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13841 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13842 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13843 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13844 The reverse order cannot happen as there is never a bfd with only the
13845 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13846 matter as here were are looking only for cross-bfd sections. */
13847
13848 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13849 for (l = already_linked_list->entry; l != NULL; l = l->next)
13850 if ((l->sec->flags & SEC_GROUP) == 0
13851 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13852 {
13853 if (abfd != l->sec->owner)
13854 sec->output_section = bfd_abs_section_ptr;
13855 break;
13856 }
80c29487 13857
082b7297 13858 /* This is the first section with this name. Record it. */
c77ec726 13859 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13860 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13861 return sec->output_section == bfd_abs_section_ptr;
082b7297 13862}
81e1b023 13863
a4d8e49b
L
13864bfd_boolean
13865_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13866{
13867 return sym->st_shndx == SHN_COMMON;
13868}
13869
13870unsigned int
13871_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13872{
13873 return SHN_COMMON;
13874}
13875
13876asection *
13877_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13878{
13879 return bfd_com_section_ptr;
13880}
10455f89
HPN
13881
13882bfd_vma
13883_bfd_elf_default_got_elt_size (bfd *abfd,
13884 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13885 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13886 bfd *ibfd ATTRIBUTE_UNUSED,
13887 unsigned long symndx ATTRIBUTE_UNUSED)
13888{
13889 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13890 return bed->s->arch_size / 8;
13891}
83bac4b0
NC
13892
13893/* Routines to support the creation of dynamic relocs. */
13894
83bac4b0
NC
13895/* Returns the name of the dynamic reloc section associated with SEC. */
13896
13897static const char *
13898get_dynamic_reloc_section_name (bfd * abfd,
13899 asection * sec,
13900 bfd_boolean is_rela)
13901{
ddcf1fcf
BS
13902 char *name;
13903 const char *old_name = bfd_get_section_name (NULL, sec);
13904 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13905
ddcf1fcf 13906 if (old_name == NULL)
83bac4b0
NC
13907 return NULL;
13908
ddcf1fcf 13909 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13910 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13911
13912 return name;
13913}
13914
13915/* Returns the dynamic reloc section associated with SEC.
13916 If necessary compute the name of the dynamic reloc section based
13917 on SEC's name (looked up in ABFD's string table) and the setting
13918 of IS_RELA. */
13919
13920asection *
13921_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13922 asection * sec,
13923 bfd_boolean is_rela)
13924{
13925 asection * reloc_sec = elf_section_data (sec)->sreloc;
13926
13927 if (reloc_sec == NULL)
13928 {
13929 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13930
13931 if (name != NULL)
13932 {
3d4d4302 13933 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
13934
13935 if (reloc_sec != NULL)
13936 elf_section_data (sec)->sreloc = reloc_sec;
13937 }
13938 }
13939
13940 return reloc_sec;
13941}
13942
13943/* Returns the dynamic reloc section associated with SEC. If the
13944 section does not exist it is created and attached to the DYNOBJ
13945 bfd and stored in the SRELOC field of SEC's elf_section_data
13946 structure.
f8076f98 13947
83bac4b0
NC
13948 ALIGNMENT is the alignment for the newly created section and
13949 IS_RELA defines whether the name should be .rela.<SEC's name>
13950 or .rel.<SEC's name>. The section name is looked up in the
13951 string table associated with ABFD. */
13952
13953asection *
ca4be51c
AM
13954_bfd_elf_make_dynamic_reloc_section (asection *sec,
13955 bfd *dynobj,
13956 unsigned int alignment,
13957 bfd *abfd,
13958 bfd_boolean is_rela)
83bac4b0
NC
13959{
13960 asection * reloc_sec = elf_section_data (sec)->sreloc;
13961
13962 if (reloc_sec == NULL)
13963 {
13964 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13965
13966 if (name == NULL)
13967 return NULL;
13968
3d4d4302 13969 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
13970
13971 if (reloc_sec == NULL)
13972 {
3d4d4302
AM
13973 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13974 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
13975 if ((sec->flags & SEC_ALLOC) != 0)
13976 flags |= SEC_ALLOC | SEC_LOAD;
13977
3d4d4302 13978 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
13979 if (reloc_sec != NULL)
13980 {
8877b5e5
AM
13981 /* _bfd_elf_get_sec_type_attr chooses a section type by
13982 name. Override as it may be wrong, eg. for a user
13983 section named "auto" we'll get ".relauto" which is
13984 seen to be a .rela section. */
13985 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
13986 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13987 reloc_sec = NULL;
13988 }
13989 }
13990
13991 elf_section_data (sec)->sreloc = reloc_sec;
13992 }
13993
13994 return reloc_sec;
13995}
1338dd10 13996
bffebb6b
AM
13997/* Copy the ELF symbol type and other attributes for a linker script
13998 assignment from HSRC to HDEST. Generally this should be treated as
13999 if we found a strong non-dynamic definition for HDEST (except that
14000 ld ignores multiple definition errors). */
1338dd10 14001void
bffebb6b
AM
14002_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14003 struct bfd_link_hash_entry *hdest,
14004 struct bfd_link_hash_entry *hsrc)
1338dd10 14005{
bffebb6b
AM
14006 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14007 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14008 Elf_Internal_Sym isym;
1338dd10
PB
14009
14010 ehdest->type = ehsrc->type;
35fc36a8 14011 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14012
14013 isym.st_other = ehsrc->other;
b8417128 14014 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14015}
351f65ca
L
14016
14017/* Append a RELA relocation REL to section S in BFD. */
14018
14019void
14020elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14021{
14022 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14023 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14024 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14025 bed->s->swap_reloca_out (abfd, rel, loc);
14026}
14027
14028/* Append a REL relocation REL to section S in BFD. */
14029
14030void
14031elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14032{
14033 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14034 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14035 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14036 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14037}
This page took 2.514644 seconds and 4 git commands to generate.