Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
6f2750fe 2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
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. */
3d4d4302
AM
152 s = bfd_get_linker_section (abfd, ".got");
153 if (s != NULL)
b34976b6 154 return TRUE;
252b5132 155
e5a52504 156 flags = bed->dynamic_sec_flags;
252b5132 157
14b2f831
AM
158 s = bfd_make_section_anyway_with_flags (abfd,
159 (bed->rela_plts_and_copies_p
160 ? ".rela.got" : ".rel.got"),
161 (bed->dynamic_sec_flags
162 | SEC_READONLY));
6de2ae4a
L
163 if (s == NULL
164 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
165 return FALSE;
166 htab->srelgot = s;
252b5132 167
14b2f831 168 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
169 if (s == NULL
170 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
171 return FALSE;
172 htab->sgot = s;
173
252b5132
RH
174 if (bed->want_got_plt)
175 {
14b2f831 176 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 177 if (s == NULL
6de2ae4a
L
178 || !bfd_set_section_alignment (abfd, s,
179 bed->s->log_file_align))
b34976b6 180 return FALSE;
6de2ae4a 181 htab->sgotplt = s;
252b5132
RH
182 }
183
64e77c6d
L
184 /* The first bit of the global offset table is the header. */
185 s->size += bed->got_header_size;
186
2517a57f
AM
187 if (bed->want_got_sym)
188 {
189 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
190 (or .got.plt) section. We don't do this in the linker script
191 because we don't want to define the symbol if we are not creating
192 a global offset table. */
6de2ae4a
L
193 h = _bfd_elf_define_linkage_sym (abfd, info, s,
194 "_GLOBAL_OFFSET_TABLE_");
2517a57f 195 elf_hash_table (info)->hgot = h;
d98685ac
AM
196 if (h == NULL)
197 return FALSE;
2517a57f 198 }
252b5132 199
b34976b6 200 return TRUE;
252b5132
RH
201}
202\f
7e9f0867
AM
203/* Create a strtab to hold the dynamic symbol names. */
204static bfd_boolean
205_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
206{
207 struct elf_link_hash_table *hash_table;
208
209 hash_table = elf_hash_table (info);
210 if (hash_table->dynobj == NULL)
6cd255ca
L
211 {
212 /* We may not set dynobj, an input file holding linker created
213 dynamic sections to abfd, which may be a dynamic object with
214 its own dynamic sections. We need to find a normal input file
215 to hold linker created sections if possible. */
216 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
217 {
218 bfd *ibfd;
219 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e
L
220 if ((ibfd->flags
221 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
6cd255ca
L
222 {
223 abfd = ibfd;
224 break;
225 }
226 }
227 hash_table->dynobj = abfd;
228 }
7e9f0867
AM
229
230 if (hash_table->dynstr == NULL)
231 {
232 hash_table->dynstr = _bfd_elf_strtab_init ();
233 if (hash_table->dynstr == NULL)
234 return FALSE;
235 }
236 return TRUE;
237}
238
45d6a902
AM
239/* Create some sections which will be filled in with dynamic linking
240 information. ABFD is an input file which requires dynamic sections
241 to be created. The dynamic sections take up virtual memory space
242 when the final executable is run, so we need to create them before
243 addresses are assigned to the output sections. We work out the
244 actual contents and size of these sections later. */
252b5132 245
b34976b6 246bfd_boolean
268b6b39 247_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 248{
45d6a902 249 flagword flags;
91d6fa6a 250 asection *s;
9c5bfbb7 251 const struct elf_backend_data *bed;
9637f6ef 252 struct elf_link_hash_entry *h;
252b5132 253
0eddce27 254 if (! is_elf_hash_table (info->hash))
45d6a902
AM
255 return FALSE;
256
257 if (elf_hash_table (info)->dynamic_sections_created)
258 return TRUE;
259
7e9f0867
AM
260 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
261 return FALSE;
45d6a902 262
7e9f0867 263 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
264 bed = get_elf_backend_data (abfd);
265
266 flags = bed->dynamic_sec_flags;
45d6a902
AM
267
268 /* A dynamically linked executable has a .interp section, but a
269 shared library does not. */
9b8b325a 270 if (bfd_link_executable (info) && !info->nointerp)
252b5132 271 {
14b2f831
AM
272 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
273 flags | SEC_READONLY);
3496cb2a 274 if (s == NULL)
45d6a902
AM
275 return FALSE;
276 }
bb0deeff 277
45d6a902
AM
278 /* Create sections to hold version informations. These are removed
279 if they are not needed. */
14b2f831
AM
280 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
281 flags | SEC_READONLY);
45d6a902 282 if (s == NULL
45d6a902
AM
283 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
284 return FALSE;
285
14b2f831
AM
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
287 flags | SEC_READONLY);
45d6a902 288 if (s == NULL
45d6a902
AM
289 || ! bfd_set_section_alignment (abfd, s, 1))
290 return FALSE;
291
14b2f831
AM
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
293 flags | SEC_READONLY);
45d6a902 294 if (s == NULL
45d6a902
AM
295 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
296 return FALSE;
297
14b2f831
AM
298 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
299 flags | SEC_READONLY);
45d6a902 300 if (s == NULL
45d6a902
AM
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
cae1fbbb 303 elf_hash_table (info)->dynsym = s;
45d6a902 304
14b2f831
AM
305 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
306 flags | SEC_READONLY);
3496cb2a 307 if (s == NULL)
45d6a902
AM
308 return FALSE;
309
14b2f831 310 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 311 if (s == NULL
45d6a902
AM
312 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
313 return FALSE;
314
315 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
316 .dynamic section. We could set _DYNAMIC in a linker script, but we
317 only want to define it if we are, in fact, creating a .dynamic
318 section. We don't want to define it if there is no .dynamic
319 section, since on some ELF platforms the start up code examines it
320 to decide how to initialize the process. */
9637f6ef
L
321 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
322 elf_hash_table (info)->hdynamic = h;
323 if (h == NULL)
45d6a902
AM
324 return FALSE;
325
fdc90cb4
JJ
326 if (info->emit_hash)
327 {
14b2f831
AM
328 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
329 flags | SEC_READONLY);
fdc90cb4
JJ
330 if (s == NULL
331 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
332 return FALSE;
333 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
334 }
335
336 if (info->emit_gnu_hash)
337 {
14b2f831
AM
338 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
339 flags | SEC_READONLY);
fdc90cb4
JJ
340 if (s == NULL
341 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
342 return FALSE;
343 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
344 4 32-bit words followed by variable count of 64-bit words, then
345 variable count of 32-bit words. */
346 if (bed->s->arch_size == 64)
347 elf_section_data (s)->this_hdr.sh_entsize = 0;
348 else
349 elf_section_data (s)->this_hdr.sh_entsize = 4;
350 }
45d6a902
AM
351
352 /* Let the backend create the rest of the sections. This lets the
353 backend set the right flags. The backend will normally create
354 the .got and .plt sections. */
894891db
NC
355 if (bed->elf_backend_create_dynamic_sections == NULL
356 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
357 return FALSE;
358
359 elf_hash_table (info)->dynamic_sections_created = TRUE;
360
361 return TRUE;
362}
363
364/* Create dynamic sections when linking against a dynamic object. */
365
366bfd_boolean
268b6b39 367_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
368{
369 flagword flags, pltflags;
7325306f 370 struct elf_link_hash_entry *h;
45d6a902 371 asection *s;
9c5bfbb7 372 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 373 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 374
252b5132
RH
375 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
376 .rel[a].bss sections. */
e5a52504 377 flags = bed->dynamic_sec_flags;
252b5132
RH
378
379 pltflags = flags;
252b5132 380 if (bed->plt_not_loaded)
6df4d94c
MM
381 /* We do not clear SEC_ALLOC here because we still want the OS to
382 allocate space for the section; it's just that there's nothing
383 to read in from the object file. */
5d1634d7 384 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
385 else
386 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
387 if (bed->plt_readonly)
388 pltflags |= SEC_READONLY;
389
14b2f831 390 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 391 if (s == NULL
252b5132 392 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 393 return FALSE;
6de2ae4a 394 htab->splt = s;
252b5132 395
d98685ac
AM
396 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
397 .plt section. */
7325306f
RS
398 if (bed->want_plt_sym)
399 {
400 h = _bfd_elf_define_linkage_sym (abfd, info, s,
401 "_PROCEDURE_LINKAGE_TABLE_");
402 elf_hash_table (info)->hplt = h;
403 if (h == NULL)
404 return FALSE;
405 }
252b5132 406
14b2f831
AM
407 s = bfd_make_section_anyway_with_flags (abfd,
408 (bed->rela_plts_and_copies_p
409 ? ".rela.plt" : ".rel.plt"),
410 flags | SEC_READONLY);
252b5132 411 if (s == NULL
45d6a902 412 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 413 return FALSE;
6de2ae4a 414 htab->srelplt = s;
252b5132
RH
415
416 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 417 return FALSE;
252b5132 418
3018b441
RH
419 if (bed->want_dynbss)
420 {
421 /* The .dynbss section is a place to put symbols which are defined
422 by dynamic objects, are referenced by regular objects, and are
423 not functions. We must allocate space for them in the process
424 image and use a R_*_COPY reloc to tell the dynamic linker to
425 initialize them at run time. The linker script puts the .dynbss
426 section into the .bss section of the final image. */
14b2f831
AM
427 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
428 (SEC_ALLOC | SEC_LINKER_CREATED));
3496cb2a 429 if (s == NULL)
b34976b6 430 return FALSE;
252b5132 431
3018b441 432 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
433 normally needed. We need to create it here, though, so that the
434 linker will map it to an output section. We can't just create it
435 only if we need it, because we will not know whether we need it
436 until we have seen all the input files, and the first time the
437 main linker code calls BFD after examining all the input files
438 (size_dynamic_sections) the input sections have already been
439 mapped to the output sections. If the section turns out not to
440 be needed, we can discard it later. We will never need this
441 section when generating a shared object, since they do not use
442 copy relocs. */
0e1862bb 443 if (! bfd_link_pic (info))
3018b441 444 {
14b2f831
AM
445 s = bfd_make_section_anyway_with_flags (abfd,
446 (bed->rela_plts_and_copies_p
447 ? ".rela.bss" : ".rel.bss"),
448 flags | SEC_READONLY);
3018b441 449 if (s == NULL
45d6a902 450 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 451 return FALSE;
3018b441 452 }
252b5132
RH
453 }
454
b34976b6 455 return TRUE;
252b5132
RH
456}
457\f
252b5132
RH
458/* Record a new dynamic symbol. We record the dynamic symbols as we
459 read the input files, since we need to have a list of all of them
460 before we can determine the final sizes of the output sections.
461 Note that we may actually call this function even though we are not
462 going to output any dynamic symbols; in some cases we know that a
463 symbol should be in the dynamic symbol table, but only if there is
464 one. */
465
b34976b6 466bfd_boolean
c152c796
AM
467bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
468 struct elf_link_hash_entry *h)
252b5132
RH
469{
470 if (h->dynindx == -1)
471 {
2b0f7ef9 472 struct elf_strtab_hash *dynstr;
68b6ddd0 473 char *p;
252b5132 474 const char *name;
ef53be89 475 size_t indx;
252b5132 476
7a13edea
NC
477 /* XXX: The ABI draft says the linker must turn hidden and
478 internal symbols into STB_LOCAL symbols when producing the
479 DSO. However, if ld.so honors st_other in the dynamic table,
480 this would not be necessary. */
481 switch (ELF_ST_VISIBILITY (h->other))
482 {
483 case STV_INTERNAL:
484 case STV_HIDDEN:
9d6eee78
L
485 if (h->root.type != bfd_link_hash_undefined
486 && h->root.type != bfd_link_hash_undefweak)
38048eb9 487 {
f5385ebf 488 h->forced_local = 1;
67687978
PB
489 if (!elf_hash_table (info)->is_relocatable_executable)
490 return TRUE;
7a13edea 491 }
0444bdd4 492
7a13edea
NC
493 default:
494 break;
495 }
496
252b5132
RH
497 h->dynindx = elf_hash_table (info)->dynsymcount;
498 ++elf_hash_table (info)->dynsymcount;
499
500 dynstr = elf_hash_table (info)->dynstr;
501 if (dynstr == NULL)
502 {
503 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 504 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 505 if (dynstr == NULL)
b34976b6 506 return FALSE;
252b5132
RH
507 }
508
509 /* We don't put any version information in the dynamic string
aad5d350 510 table. */
252b5132
RH
511 name = h->root.root.string;
512 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
513 if (p != NULL)
514 /* We know that the p points into writable memory. In fact,
515 there are only a few symbols that have read-only names, being
516 those like _GLOBAL_OFFSET_TABLE_ that are created specially
517 by the backends. Most symbols will have names pointing into
518 an ELF string table read from a file, or to objalloc memory. */
519 *p = 0;
520
521 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
522
523 if (p != NULL)
524 *p = ELF_VER_CHR;
252b5132 525
ef53be89 526 if (indx == (size_t) -1)
b34976b6 527 return FALSE;
252b5132
RH
528 h->dynstr_index = indx;
529 }
530
b34976b6 531 return TRUE;
252b5132 532}
45d6a902 533\f
55255dae
L
534/* Mark a symbol dynamic. */
535
28caa186 536static void
55255dae 537bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
538 struct elf_link_hash_entry *h,
539 Elf_Internal_Sym *sym)
55255dae 540{
40b36307 541 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 542
40b36307 543 /* It may be called more than once on the same H. */
0e1862bb 544 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
545 return;
546
40b36307
L
547 if ((info->dynamic_data
548 && (h->type == STT_OBJECT
b8871f35 549 || h->type == STT_COMMON
40b36307 550 || (sym != NULL
b8871f35
L
551 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
552 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 553 || (d != NULL
40b36307
L
554 && h->root.type == bfd_link_hash_new
555 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
556 h->dynamic = 1;
557}
558
45d6a902
AM
559/* Record an assignment to a symbol made by a linker script. We need
560 this in case some dynamic object refers to this symbol. */
561
562bfd_boolean
fe21a8fc
L
563bfd_elf_record_link_assignment (bfd *output_bfd,
564 struct bfd_link_info *info,
268b6b39 565 const char *name,
fe21a8fc
L
566 bfd_boolean provide,
567 bfd_boolean hidden)
45d6a902 568{
00cbee0a 569 struct elf_link_hash_entry *h, *hv;
4ea42fb7 570 struct elf_link_hash_table *htab;
00cbee0a 571 const struct elf_backend_data *bed;
45d6a902 572
0eddce27 573 if (!is_elf_hash_table (info->hash))
45d6a902
AM
574 return TRUE;
575
4ea42fb7
AM
576 htab = elf_hash_table (info);
577 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 578 if (h == NULL)
4ea42fb7 579 return provide;
45d6a902 580
0f550b3d
L
581 if (h->versioned == unknown)
582 {
583 /* Set versioned if symbol version is unknown. */
584 char *version = strrchr (name, ELF_VER_CHR);
585 if (version)
586 {
587 if (version > name && version[-1] != ELF_VER_CHR)
588 h->versioned = versioned_hidden;
589 else
590 h->versioned = versioned;
591 }
592 }
593
00cbee0a 594 switch (h->root.type)
77cfaee6 595 {
00cbee0a
L
596 case bfd_link_hash_defined:
597 case bfd_link_hash_defweak:
598 case bfd_link_hash_common:
599 break;
600 case bfd_link_hash_undefweak:
601 case bfd_link_hash_undefined:
602 /* Since we're defining the symbol, don't let it seem to have not
603 been defined. record_dynamic_symbol and size_dynamic_sections
604 may depend on this. */
4ea42fb7 605 h->root.type = bfd_link_hash_new;
77cfaee6
AM
606 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
607 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
608 break;
609 case bfd_link_hash_new:
40b36307 610 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 611 h->non_elf = 0;
00cbee0a
L
612 break;
613 case bfd_link_hash_indirect:
614 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 615 the versioned symbol point to this one. */
00cbee0a
L
616 bed = get_elf_backend_data (output_bfd);
617 hv = h;
618 while (hv->root.type == bfd_link_hash_indirect
619 || hv->root.type == bfd_link_hash_warning)
620 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
621 /* We don't need to update h->root.u since linker will set them
622 later. */
623 h->root.type = bfd_link_hash_undefined;
624 hv->root.type = bfd_link_hash_indirect;
625 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
626 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
627 break;
628 case bfd_link_hash_warning:
629 abort ();
630 break;
55255dae 631 }
45d6a902
AM
632
633 /* If this symbol is being provided by the linker script, and it is
634 currently defined by a dynamic object, but not by a regular
635 object, then mark it as undefined so that the generic linker will
636 force the correct value. */
637 if (provide
f5385ebf
AM
638 && h->def_dynamic
639 && !h->def_regular)
45d6a902
AM
640 h->root.type = bfd_link_hash_undefined;
641
642 /* If this symbol is not being provided by the linker script, and it is
643 currently defined by a dynamic object, but not by a regular object,
644 then clear out any version information because the symbol will not be
645 associated with the dynamic object any more. */
646 if (!provide
f5385ebf
AM
647 && h->def_dynamic
648 && !h->def_regular)
45d6a902
AM
649 h->verinfo.verdef = NULL;
650
f5385ebf 651 h->def_regular = 1;
45d6a902 652
eb8476a6 653 if (hidden)
fe21a8fc 654 {
91d6fa6a 655 bed = get_elf_backend_data (output_bfd);
b8297068
AM
656 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
657 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
658 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
659 }
660
6fa3860b
PB
661 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
662 and executables. */
0e1862bb 663 if (!bfd_link_relocatable (info)
6fa3860b
PB
664 && h->dynindx != -1
665 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
666 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
667 h->forced_local = 1;
668
f5385ebf
AM
669 if ((h->def_dynamic
670 || h->ref_dynamic
6b3b0ab8
L
671 || bfd_link_dll (info)
672 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
673 && h->dynindx == -1)
674 {
c152c796 675 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
676 return FALSE;
677
678 /* If this is a weak defined symbol, and we know a corresponding
679 real symbol from the same dynamic object, make sure the real
680 symbol is also made into a dynamic symbol. */
f6e332e6
AM
681 if (h->u.weakdef != NULL
682 && h->u.weakdef->dynindx == -1)
45d6a902 683 {
f6e332e6 684 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
685 return FALSE;
686 }
687 }
688
689 return TRUE;
690}
42751cf3 691
8c58d23b
AM
692/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
693 success, and 2 on a failure caused by attempting to record a symbol
694 in a discarded section, eg. a discarded link-once section symbol. */
695
696int
c152c796
AM
697bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
698 bfd *input_bfd,
699 long input_indx)
8c58d23b
AM
700{
701 bfd_size_type amt;
702 struct elf_link_local_dynamic_entry *entry;
703 struct elf_link_hash_table *eht;
704 struct elf_strtab_hash *dynstr;
ef53be89 705 size_t dynstr_index;
8c58d23b
AM
706 char *name;
707 Elf_External_Sym_Shndx eshndx;
708 char esym[sizeof (Elf64_External_Sym)];
709
0eddce27 710 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
711 return 0;
712
713 /* See if the entry exists already. */
714 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
715 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
716 return 1;
717
718 amt = sizeof (*entry);
a50b1753 719 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
720 if (entry == NULL)
721 return 0;
722
723 /* Go find the symbol, so that we can find it's name. */
724 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 725 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
726 {
727 bfd_release (input_bfd, entry);
728 return 0;
729 }
730
731 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 732 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
733 {
734 asection *s;
735
736 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
737 if (s == NULL || bfd_is_abs_section (s->output_section))
738 {
739 /* We can still bfd_release here as nothing has done another
740 bfd_alloc. We can't do this later in this function. */
741 bfd_release (input_bfd, entry);
742 return 2;
743 }
744 }
745
746 name = (bfd_elf_string_from_elf_section
747 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
748 entry->isym.st_name));
749
750 dynstr = elf_hash_table (info)->dynstr;
751 if (dynstr == NULL)
752 {
753 /* Create a strtab to hold the dynamic symbol names. */
754 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
755 if (dynstr == NULL)
756 return 0;
757 }
758
b34976b6 759 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 760 if (dynstr_index == (size_t) -1)
8c58d23b
AM
761 return 0;
762 entry->isym.st_name = dynstr_index;
763
764 eht = elf_hash_table (info);
765
766 entry->next = eht->dynlocal;
767 eht->dynlocal = entry;
768 entry->input_bfd = input_bfd;
769 entry->input_indx = input_indx;
770 eht->dynsymcount++;
771
772 /* Whatever binding the symbol had before, it's now local. */
773 entry->isym.st_info
774 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
775
776 /* The dynindx will be set at the end of size_dynamic_sections. */
777
778 return 1;
779}
780
30b30c21 781/* Return the dynindex of a local dynamic symbol. */
42751cf3 782
30b30c21 783long
268b6b39
AM
784_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
785 bfd *input_bfd,
786 long input_indx)
30b30c21
RH
787{
788 struct elf_link_local_dynamic_entry *e;
789
790 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
791 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
792 return e->dynindx;
793 return -1;
794}
795
796/* This function is used to renumber the dynamic symbols, if some of
797 them are removed because they are marked as local. This is called
798 via elf_link_hash_traverse. */
799
b34976b6 800static bfd_boolean
268b6b39
AM
801elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
802 void *data)
42751cf3 803{
a50b1753 804 size_t *count = (size_t *) data;
30b30c21 805
6fa3860b
PB
806 if (h->forced_local)
807 return TRUE;
808
809 if (h->dynindx != -1)
810 h->dynindx = ++(*count);
811
812 return TRUE;
813}
814
815
816/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
817 STB_LOCAL binding. */
818
819static bfd_boolean
820elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
821 void *data)
822{
a50b1753 823 size_t *count = (size_t *) data;
6fa3860b 824
6fa3860b
PB
825 if (!h->forced_local)
826 return TRUE;
827
42751cf3 828 if (h->dynindx != -1)
30b30c21
RH
829 h->dynindx = ++(*count);
830
b34976b6 831 return TRUE;
42751cf3 832}
30b30c21 833
aee6f5b4
AO
834/* Return true if the dynamic symbol for a given section should be
835 omitted when creating a shared library. */
836bfd_boolean
837_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
838 struct bfd_link_info *info,
839 asection *p)
840{
74541ad4 841 struct elf_link_hash_table *htab;
ca55926c 842 asection *ip;
74541ad4 843
aee6f5b4
AO
844 switch (elf_section_data (p)->this_hdr.sh_type)
845 {
846 case SHT_PROGBITS:
847 case SHT_NOBITS:
848 /* If sh_type is yet undecided, assume it could be
849 SHT_PROGBITS/SHT_NOBITS. */
850 case SHT_NULL:
74541ad4
AM
851 htab = elf_hash_table (info);
852 if (p == htab->tls_sec)
853 return FALSE;
854
855 if (htab->text_index_section != NULL)
856 return p != htab->text_index_section && p != htab->data_index_section;
857
ca55926c 858 return (htab->dynobj != NULL
3d4d4302 859 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 860 && ip->output_section == p);
aee6f5b4
AO
861
862 /* There shouldn't be section relative relocations
863 against any other section. */
864 default:
865 return TRUE;
866 }
867}
868
062e2358 869/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
870 symbol for each output section, which come first. Next come symbols
871 which have been forced to local binding. Then all of the back-end
872 allocated local dynamic syms, followed by the rest of the global
873 symbols. */
30b30c21 874
554220db
AM
875static unsigned long
876_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
877 struct bfd_link_info *info,
878 unsigned long *section_sym_count)
30b30c21
RH
879{
880 unsigned long dynsymcount = 0;
881
0e1862bb
L
882 if (bfd_link_pic (info)
883 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 884 {
aee6f5b4 885 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
886 asection *p;
887 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 888 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
889 && (p->flags & SEC_ALLOC) != 0
890 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
891 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
892 else
893 elf_section_data (p)->dynindx = 0;
30b30c21 894 }
554220db 895 *section_sym_count = dynsymcount;
30b30c21 896
6fa3860b
PB
897 elf_link_hash_traverse (elf_hash_table (info),
898 elf_link_renumber_local_hash_table_dynsyms,
899 &dynsymcount);
900
30b30c21
RH
901 if (elf_hash_table (info)->dynlocal)
902 {
903 struct elf_link_local_dynamic_entry *p;
904 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
905 p->dynindx = ++dynsymcount;
906 }
90ac2420 907 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
908
909 elf_link_hash_traverse (elf_hash_table (info),
910 elf_link_renumber_hash_table_dynsyms,
911 &dynsymcount);
912
d5486c43
L
913 /* There is an unused NULL entry at the head of the table which we
914 must account for in our count even if the table is empty since it
915 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
916 .dynamic section. */
917 dynsymcount++;
30b30c21 918
ccabcbe5
AM
919 elf_hash_table (info)->dynsymcount = dynsymcount;
920 return dynsymcount;
30b30c21 921}
252b5132 922
54ac0771
L
923/* Merge st_other field. */
924
925static void
926elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 927 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 928 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
929{
930 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
931
932 /* If st_other has a processor-specific meaning, specific
cd3416da 933 code might be needed here. */
54ac0771
L
934 if (bed->elf_backend_merge_symbol_attribute)
935 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
936 dynamic);
937
cd3416da 938 if (!dynamic)
54ac0771 939 {
cd3416da
AM
940 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
941 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 942
cd3416da
AM
943 /* Keep the most constraining visibility. Leave the remainder
944 of the st_other field to elf_backend_merge_symbol_attribute. */
945 if (symvis - 1 < hvis - 1)
946 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 947 }
b8417128
AM
948 else if (definition
949 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
950 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 951 h->protected_def = 1;
54ac0771
L
952}
953
4f3fedcf
AM
954/* This function is called when we want to merge a new symbol with an
955 existing symbol. It handles the various cases which arise when we
956 find a definition in a dynamic object, or when there is already a
957 definition in a dynamic object. The new symbol is described by
958 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
959 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
960 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
961 of an old common symbol. We set OVERRIDE if the old symbol is
962 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
963 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
964 to change. By OK to change, we mean that we shouldn't warn if the
965 type or size does change. */
45d6a902 966
8a56bd02 967static bfd_boolean
268b6b39
AM
968_bfd_elf_merge_symbol (bfd *abfd,
969 struct bfd_link_info *info,
970 const char *name,
971 Elf_Internal_Sym *sym,
972 asection **psec,
973 bfd_vma *pvalue,
4f3fedcf
AM
974 struct elf_link_hash_entry **sym_hash,
975 bfd **poldbfd,
37a9e49a 976 bfd_boolean *pold_weak,
af44c138 977 unsigned int *pold_alignment,
268b6b39
AM
978 bfd_boolean *skip,
979 bfd_boolean *override,
980 bfd_boolean *type_change_ok,
6e33951e
L
981 bfd_boolean *size_change_ok,
982 bfd_boolean *matched)
252b5132 983{
7479dfd4 984 asection *sec, *oldsec;
45d6a902 985 struct elf_link_hash_entry *h;
90c984fc 986 struct elf_link_hash_entry *hi;
45d6a902
AM
987 struct elf_link_hash_entry *flip;
988 int bind;
989 bfd *oldbfd;
990 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 991 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 992 const struct elf_backend_data *bed;
6e33951e 993 char *new_version;
45d6a902
AM
994
995 *skip = FALSE;
996 *override = FALSE;
997
998 sec = *psec;
999 bind = ELF_ST_BIND (sym->st_info);
1000
1001 if (! bfd_is_und_section (sec))
1002 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1003 else
1004 h = ((struct elf_link_hash_entry *)
1005 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1006 if (h == NULL)
1007 return FALSE;
1008 *sym_hash = h;
252b5132 1009
88ba32a0
L
1010 bed = get_elf_backend_data (abfd);
1011
6e33951e 1012 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1013 if (h->versioned != unversioned)
6e33951e 1014 {
422f1182
L
1015 /* Symbol version is unknown or versioned. */
1016 new_version = strrchr (name, ELF_VER_CHR);
1017 if (new_version)
1018 {
1019 if (h->versioned == unknown)
1020 {
1021 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1022 h->versioned = versioned_hidden;
1023 else
1024 h->versioned = versioned;
1025 }
1026 new_version += 1;
1027 if (new_version[0] == '\0')
1028 new_version = NULL;
1029 }
1030 else
1031 h->versioned = unversioned;
6e33951e 1032 }
422f1182
L
1033 else
1034 new_version = NULL;
6e33951e 1035
90c984fc
L
1036 /* For merging, we only care about real symbols. But we need to make
1037 sure that indirect symbol dynamic flags are updated. */
1038 hi = h;
45d6a902
AM
1039 while (h->root.type == bfd_link_hash_indirect
1040 || h->root.type == bfd_link_hash_warning)
1041 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1042
6e33951e
L
1043 if (!*matched)
1044 {
1045 if (hi == h || h->root.type == bfd_link_hash_new)
1046 *matched = TRUE;
1047 else
1048 {
ae7683d2 1049 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1050 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1051 true if the new symbol is only visible to the symbol with
6e33951e 1052 the same symbol version. */
422f1182
L
1053 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1054 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1055 if (!old_hidden && !new_hidden)
1056 /* The new symbol matches the existing symbol if both
1057 aren't hidden. */
1058 *matched = TRUE;
1059 else
1060 {
1061 /* OLD_VERSION is the symbol version of the existing
1062 symbol. */
422f1182
L
1063 char *old_version;
1064
1065 if (h->versioned >= versioned)
1066 old_version = strrchr (h->root.root.string,
1067 ELF_VER_CHR) + 1;
1068 else
1069 old_version = NULL;
6e33951e
L
1070
1071 /* The new symbol matches the existing symbol if they
1072 have the same symbol version. */
1073 *matched = (old_version == new_version
1074 || (old_version != NULL
1075 && new_version != NULL
1076 && strcmp (old_version, new_version) == 0));
1077 }
1078 }
1079 }
1080
934bce08
AM
1081 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1082 existing symbol. */
1083
1084 oldbfd = NULL;
1085 oldsec = NULL;
1086 switch (h->root.type)
1087 {
1088 default:
1089 break;
1090
1091 case bfd_link_hash_undefined:
1092 case bfd_link_hash_undefweak:
1093 oldbfd = h->root.u.undef.abfd;
1094 break;
1095
1096 case bfd_link_hash_defined:
1097 case bfd_link_hash_defweak:
1098 oldbfd = h->root.u.def.section->owner;
1099 oldsec = h->root.u.def.section;
1100 break;
1101
1102 case bfd_link_hash_common:
1103 oldbfd = h->root.u.c.p->section->owner;
1104 oldsec = h->root.u.c.p->section;
1105 if (pold_alignment)
1106 *pold_alignment = h->root.u.c.p->alignment_power;
1107 break;
1108 }
1109 if (poldbfd && *poldbfd == NULL)
1110 *poldbfd = oldbfd;
1111
1112 /* Differentiate strong and weak symbols. */
1113 newweak = bind == STB_WEAK;
1114 oldweak = (h->root.type == bfd_link_hash_defweak
1115 || h->root.type == bfd_link_hash_undefweak);
1116 if (pold_weak)
1117 *pold_weak = oldweak;
1118
1119 /* This code is for coping with dynamic objects, and is only useful
1120 if we are doing an ELF link. */
1121 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1122 return TRUE;
1123
40b36307 1124 /* We have to check it for every instance since the first few may be
ee659f1f 1125 references and not all compilers emit symbol type for undefined
40b36307
L
1126 symbols. */
1127 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1128
ee659f1f
AM
1129 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1130 respectively, is from a dynamic object. */
1131
1132 newdyn = (abfd->flags & DYNAMIC) != 0;
1133
1134 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1135 syms and defined syms in dynamic libraries respectively.
1136 ref_dynamic on the other hand can be set for a symbol defined in
1137 a dynamic library, and def_dynamic may not be set; When the
1138 definition in a dynamic lib is overridden by a definition in the
1139 executable use of the symbol in the dynamic lib becomes a
1140 reference to the executable symbol. */
1141 if (newdyn)
1142 {
1143 if (bfd_is_und_section (sec))
1144 {
1145 if (bind != STB_WEAK)
1146 {
1147 h->ref_dynamic_nonweak = 1;
1148 hi->ref_dynamic_nonweak = 1;
1149 }
1150 }
1151 else
1152 {
6e33951e
L
1153 /* Update the existing symbol only if they match. */
1154 if (*matched)
1155 h->dynamic_def = 1;
ee659f1f
AM
1156 hi->dynamic_def = 1;
1157 }
1158 }
1159
45d6a902
AM
1160 /* If we just created the symbol, mark it as being an ELF symbol.
1161 Other than that, there is nothing to do--there is no merge issue
1162 with a newly defined symbol--so we just return. */
1163
1164 if (h->root.type == bfd_link_hash_new)
252b5132 1165 {
f5385ebf 1166 h->non_elf = 0;
45d6a902
AM
1167 return TRUE;
1168 }
252b5132 1169
45d6a902
AM
1170 /* In cases involving weak versioned symbols, we may wind up trying
1171 to merge a symbol with itself. Catch that here, to avoid the
1172 confusion that results if we try to override a symbol with
1173 itself. The additional tests catch cases like
1174 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1175 dynamic object, which we do want to handle here. */
1176 if (abfd == oldbfd
895fa45f 1177 && (newweak || oldweak)
45d6a902 1178 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1179 || !h->def_regular))
45d6a902
AM
1180 return TRUE;
1181
707bba77 1182 olddyn = FALSE;
45d6a902
AM
1183 if (oldbfd != NULL)
1184 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1185 else if (oldsec != NULL)
45d6a902 1186 {
707bba77 1187 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1188 indices used by MIPS ELF. */
707bba77 1189 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1190 }
252b5132 1191
45d6a902
AM
1192 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1193 respectively, appear to be a definition rather than reference. */
1194
707bba77 1195 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1196
707bba77
AM
1197 olddef = (h->root.type != bfd_link_hash_undefined
1198 && h->root.type != bfd_link_hash_undefweak
202ac193 1199 && h->root.type != bfd_link_hash_common);
45d6a902 1200
0a36a439
L
1201 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1202 respectively, appear to be a function. */
1203
1204 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1205 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1206
1207 oldfunc = (h->type != STT_NOTYPE
1208 && bed->is_function_type (h->type));
1209
5b677558
AM
1210 /* If creating a default indirect symbol ("foo" or "foo@") from a
1211 dynamic versioned definition ("foo@@") skip doing so if there is
1212 an existing regular definition with a different type. We don't
1213 want, for example, a "time" variable in the executable overriding
1214 a "time" function in a shared library. */
580a2b6e 1215 if (pold_alignment == NULL
580a2b6e
L
1216 && newdyn
1217 && newdef
1218 && !olddyn
5b677558
AM
1219 && (olddef || h->root.type == bfd_link_hash_common)
1220 && ELF_ST_TYPE (sym->st_info) != h->type
1221 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1222 && h->type != STT_NOTYPE
1223 && !(newfunc && oldfunc))
580a2b6e
L
1224 {
1225 *skip = TRUE;
1226 return TRUE;
1227 }
1228
4c34aff8
AM
1229 /* Check TLS symbols. We don't check undefined symbols introduced
1230 by "ld -u" which have no type (and oldbfd NULL), and we don't
1231 check symbols from plugins because they also have no type. */
1232 if (oldbfd != NULL
1233 && (oldbfd->flags & BFD_PLUGIN) == 0
1234 && (abfd->flags & BFD_PLUGIN) == 0
1235 && ELF_ST_TYPE (sym->st_info) != h->type
1236 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1237 {
1238 bfd *ntbfd, *tbfd;
1239 bfd_boolean ntdef, tdef;
1240 asection *ntsec, *tsec;
1241
1242 if (h->type == STT_TLS)
1243 {
3b36f7e6 1244 ntbfd = abfd;
7479dfd4
L
1245 ntsec = sec;
1246 ntdef = newdef;
1247 tbfd = oldbfd;
1248 tsec = oldsec;
1249 tdef = olddef;
1250 }
1251 else
1252 {
1253 ntbfd = oldbfd;
1254 ntsec = oldsec;
1255 ntdef = olddef;
1256 tbfd = abfd;
1257 tsec = sec;
1258 tdef = newdef;
1259 }
1260
1261 if (tdef && ntdef)
1262 (*_bfd_error_handler)
191c0c42
AM
1263 (_("%s: TLS definition in %B section %A "
1264 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1265 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1266 else if (!tdef && !ntdef)
1267 (*_bfd_error_handler)
191c0c42
AM
1268 (_("%s: TLS reference in %B "
1269 "mismatches non-TLS reference in %B"),
7479dfd4
L
1270 tbfd, ntbfd, h->root.root.string);
1271 else if (tdef)
1272 (*_bfd_error_handler)
191c0c42
AM
1273 (_("%s: TLS definition in %B section %A "
1274 "mismatches non-TLS reference in %B"),
7479dfd4
L
1275 tbfd, tsec, ntbfd, h->root.root.string);
1276 else
1277 (*_bfd_error_handler)
191c0c42
AM
1278 (_("%s: TLS reference in %B "
1279 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1280 tbfd, ntbfd, ntsec, h->root.root.string);
1281
1282 bfd_set_error (bfd_error_bad_value);
1283 return FALSE;
1284 }
1285
45d6a902
AM
1286 /* If the old symbol has non-default visibility, we ignore the new
1287 definition from a dynamic object. */
1288 if (newdyn
9c7a29a3 1289 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1290 && !bfd_is_und_section (sec))
1291 {
1292 *skip = TRUE;
1293 /* Make sure this symbol is dynamic. */
f5385ebf 1294 h->ref_dynamic = 1;
90c984fc 1295 hi->ref_dynamic = 1;
45d6a902
AM
1296 /* A protected symbol has external availability. Make sure it is
1297 recorded as dynamic.
1298
1299 FIXME: Should we check type and size for protected symbol? */
1300 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1301 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1302 else
1303 return TRUE;
1304 }
1305 else if (!newdyn
9c7a29a3 1306 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1307 && h->def_dynamic)
45d6a902
AM
1308 {
1309 /* If the new symbol with non-default visibility comes from a
1310 relocatable file and the old definition comes from a dynamic
1311 object, we remove the old definition. */
6c9b78e6 1312 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1313 {
1314 /* Handle the case where the old dynamic definition is
1315 default versioned. We need to copy the symbol info from
1316 the symbol with default version to the normal one if it
1317 was referenced before. */
1318 if (h->ref_regular)
1319 {
6c9b78e6 1320 hi->root.type = h->root.type;
d2dee3b2 1321 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1322 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1323
6c9b78e6 1324 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1325 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1326 {
aed81c4e
MR
1327 /* If the new symbol is hidden or internal, completely undo
1328 any dynamic link state. */
1329 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1330 h->forced_local = 0;
1331 h->ref_dynamic = 0;
d2dee3b2
L
1332 }
1333 else
aed81c4e
MR
1334 h->ref_dynamic = 1;
1335
1336 h->def_dynamic = 0;
aed81c4e
MR
1337 /* FIXME: Should we check type and size for protected symbol? */
1338 h->size = 0;
1339 h->type = 0;
1340
6c9b78e6 1341 h = hi;
d2dee3b2
L
1342 }
1343 else
6c9b78e6 1344 h = hi;
d2dee3b2 1345 }
1de1a317 1346
f5eda473
AM
1347 /* If the old symbol was undefined before, then it will still be
1348 on the undefs list. If the new symbol is undefined or
1349 common, we can't make it bfd_link_hash_new here, because new
1350 undefined or common symbols will be added to the undefs list
1351 by _bfd_generic_link_add_one_symbol. Symbols may not be
1352 added twice to the undefs list. Also, if the new symbol is
1353 undefweak then we don't want to lose the strong undef. */
1354 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1355 {
1de1a317 1356 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1357 h->root.u.undef.abfd = abfd;
1358 }
1359 else
1360 {
1361 h->root.type = bfd_link_hash_new;
1362 h->root.u.undef.abfd = NULL;
1363 }
1364
f5eda473 1365 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1366 {
f5eda473
AM
1367 /* If the new symbol is hidden or internal, completely undo
1368 any dynamic link state. */
1369 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1370 h->forced_local = 0;
1371 h->ref_dynamic = 0;
45d6a902 1372 }
f5eda473
AM
1373 else
1374 h->ref_dynamic = 1;
1375 h->def_dynamic = 0;
45d6a902
AM
1376 /* FIXME: Should we check type and size for protected symbol? */
1377 h->size = 0;
1378 h->type = 0;
1379 return TRUE;
1380 }
14a793b2 1381
15b43f48
AM
1382 /* If a new weak symbol definition comes from a regular file and the
1383 old symbol comes from a dynamic library, we treat the new one as
1384 strong. Similarly, an old weak symbol definition from a regular
1385 file is treated as strong when the new symbol comes from a dynamic
1386 library. Further, an old weak symbol from a dynamic library is
1387 treated as strong if the new symbol is from a dynamic library.
1388 This reflects the way glibc's ld.so works.
1389
1390 Do this before setting *type_change_ok or *size_change_ok so that
1391 we warn properly when dynamic library symbols are overridden. */
1392
1393 if (newdef && !newdyn && olddyn)
0f8a2703 1394 newweak = FALSE;
15b43f48 1395 if (olddef && newdyn)
0f8a2703
AM
1396 oldweak = FALSE;
1397
d334575b 1398 /* Allow changes between different types of function symbol. */
0a36a439 1399 if (newfunc && oldfunc)
fcb93ecf
PB
1400 *type_change_ok = TRUE;
1401
79349b09
AM
1402 /* It's OK to change the type if either the existing symbol or the
1403 new symbol is weak. A type change is also OK if the old symbol
1404 is undefined and the new symbol is defined. */
252b5132 1405
79349b09
AM
1406 if (oldweak
1407 || newweak
1408 || (newdef
1409 && h->root.type == bfd_link_hash_undefined))
1410 *type_change_ok = TRUE;
1411
1412 /* It's OK to change the size if either the existing symbol or the
1413 new symbol is weak, or if the old symbol is undefined. */
1414
1415 if (*type_change_ok
1416 || h->root.type == bfd_link_hash_undefined)
1417 *size_change_ok = TRUE;
45d6a902 1418
45d6a902
AM
1419 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1420 symbol, respectively, appears to be a common symbol in a dynamic
1421 object. If a symbol appears in an uninitialized section, and is
1422 not weak, and is not a function, then it may be a common symbol
1423 which was resolved when the dynamic object was created. We want
1424 to treat such symbols specially, because they raise special
1425 considerations when setting the symbol size: if the symbol
1426 appears as a common symbol in a regular object, and the size in
1427 the regular object is larger, we must make sure that we use the
1428 larger size. This problematic case can always be avoided in C,
1429 but it must be handled correctly when using Fortran shared
1430 libraries.
1431
1432 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1433 likewise for OLDDYNCOMMON and OLDDEF.
1434
1435 Note that this test is just a heuristic, and that it is quite
1436 possible to have an uninitialized symbol in a shared object which
1437 is really a definition, rather than a common symbol. This could
1438 lead to some minor confusion when the symbol really is a common
1439 symbol in some regular object. However, I think it will be
1440 harmless. */
1441
1442 if (newdyn
1443 && newdef
79349b09 1444 && !newweak
45d6a902
AM
1445 && (sec->flags & SEC_ALLOC) != 0
1446 && (sec->flags & SEC_LOAD) == 0
1447 && sym->st_size > 0
0a36a439 1448 && !newfunc)
45d6a902
AM
1449 newdyncommon = TRUE;
1450 else
1451 newdyncommon = FALSE;
1452
1453 if (olddyn
1454 && olddef
1455 && h->root.type == bfd_link_hash_defined
f5385ebf 1456 && h->def_dynamic
45d6a902
AM
1457 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1458 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1459 && h->size > 0
0a36a439 1460 && !oldfunc)
45d6a902
AM
1461 olddyncommon = TRUE;
1462 else
1463 olddyncommon = FALSE;
1464
a4d8e49b
L
1465 /* We now know everything about the old and new symbols. We ask the
1466 backend to check if we can merge them. */
5d13b3b3
AM
1467 if (bed->merge_symbol != NULL)
1468 {
1469 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1470 return FALSE;
1471 sec = *psec;
1472 }
a4d8e49b 1473
45d6a902
AM
1474 /* If both the old and the new symbols look like common symbols in a
1475 dynamic object, set the size of the symbol to the larger of the
1476 two. */
1477
1478 if (olddyncommon
1479 && newdyncommon
1480 && sym->st_size != h->size)
1481 {
1482 /* Since we think we have two common symbols, issue a multiple
1483 common warning if desired. Note that we only warn if the
1484 size is different. If the size is the same, we simply let
1485 the old symbol override the new one as normally happens with
1486 symbols defined in dynamic objects. */
1487
1a72702b
AM
1488 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1489 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1490 if (sym->st_size > h->size)
1491 h->size = sym->st_size;
252b5132 1492
45d6a902 1493 *size_change_ok = TRUE;
252b5132
RH
1494 }
1495
45d6a902
AM
1496 /* If we are looking at a dynamic object, and we have found a
1497 definition, we need to see if the symbol was already defined by
1498 some other object. If so, we want to use the existing
1499 definition, and we do not want to report a multiple symbol
1500 definition error; we do this by clobbering *PSEC to be
1501 bfd_und_section_ptr.
1502
1503 We treat a common symbol as a definition if the symbol in the
1504 shared library is a function, since common symbols always
1505 represent variables; this can cause confusion in principle, but
1506 any such confusion would seem to indicate an erroneous program or
1507 shared library. We also permit a common symbol in a regular
202ac193
L
1508 object to override a weak symbol in a shared object. A common
1509 symbol in executable also overrides a symbol in a shared object. */
45d6a902
AM
1510
1511 if (newdyn
1512 && newdef
77cfaee6 1513 && (olddef
45d6a902 1514 || (h->root.type == bfd_link_hash_common
202ac193
L
1515 && (newweak
1516 || newfunc
1517 || (!olddyn && bfd_link_executable (info))))))
45d6a902
AM
1518 {
1519 *override = TRUE;
1520 newdef = FALSE;
1521 newdyncommon = FALSE;
252b5132 1522
45d6a902
AM
1523 *psec = sec = bfd_und_section_ptr;
1524 *size_change_ok = TRUE;
252b5132 1525
45d6a902
AM
1526 /* If we get here when the old symbol is a common symbol, then
1527 we are explicitly letting it override a weak symbol or
1528 function in a dynamic object, and we don't want to warn about
1529 a type change. If the old symbol is a defined symbol, a type
1530 change warning may still be appropriate. */
252b5132 1531
45d6a902
AM
1532 if (h->root.type == bfd_link_hash_common)
1533 *type_change_ok = TRUE;
1534 }
1535
1536 /* Handle the special case of an old common symbol merging with a
1537 new symbol which looks like a common symbol in a shared object.
1538 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1539 common symbol, and let _bfd_generic_link_add_one_symbol do the
1540 right thing. */
45d6a902
AM
1541
1542 if (newdyncommon
1543 && h->root.type == bfd_link_hash_common)
1544 {
1545 *override = TRUE;
1546 newdef = FALSE;
1547 newdyncommon = FALSE;
1548 *pvalue = sym->st_size;
a4d8e49b 1549 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1550 *size_change_ok = TRUE;
1551 }
1552
c5e2cead 1553 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1554 if (newdef && olddef && newweak)
54ac0771 1555 {
35ed3f94 1556 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1557 if (!(oldbfd != NULL
1558 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1559 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1560 {
1561 newdef = FALSE;
1562 *skip = TRUE;
1563 }
54ac0771
L
1564
1565 /* Merge st_other. If the symbol already has a dynamic index,
1566 but visibility says it should not be visible, turn it into a
1567 local symbol. */
b8417128 1568 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1569 if (h->dynindx != -1)
1570 switch (ELF_ST_VISIBILITY (h->other))
1571 {
1572 case STV_INTERNAL:
1573 case STV_HIDDEN:
1574 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1575 break;
1576 }
1577 }
c5e2cead 1578
45d6a902
AM
1579 /* If the old symbol is from a dynamic object, and the new symbol is
1580 a definition which is not from a dynamic object, then the new
1581 symbol overrides the old symbol. Symbols from regular files
1582 always take precedence over symbols from dynamic objects, even if
1583 they are defined after the dynamic object in the link.
1584
1585 As above, we again permit a common symbol in a regular object to
1586 override a definition in a shared object if the shared object
0f8a2703 1587 symbol is a function or is weak. */
45d6a902
AM
1588
1589 flip = NULL;
77cfaee6 1590 if (!newdyn
45d6a902
AM
1591 && (newdef
1592 || (bfd_is_com_section (sec)
0a36a439 1593 && (oldweak || oldfunc)))
45d6a902
AM
1594 && olddyn
1595 && olddef
f5385ebf 1596 && h->def_dynamic)
45d6a902
AM
1597 {
1598 /* Change the hash table entry to undefined, and let
1599 _bfd_generic_link_add_one_symbol do the right thing with the
1600 new definition. */
1601
1602 h->root.type = bfd_link_hash_undefined;
1603 h->root.u.undef.abfd = h->root.u.def.section->owner;
1604 *size_change_ok = TRUE;
1605
1606 olddef = FALSE;
1607 olddyncommon = FALSE;
1608
1609 /* We again permit a type change when a common symbol may be
1610 overriding a function. */
1611
1612 if (bfd_is_com_section (sec))
0a36a439
L
1613 {
1614 if (oldfunc)
1615 {
1616 /* If a common symbol overrides a function, make sure
1617 that it isn't defined dynamically nor has type
1618 function. */
1619 h->def_dynamic = 0;
1620 h->type = STT_NOTYPE;
1621 }
1622 *type_change_ok = TRUE;
1623 }
45d6a902 1624
6c9b78e6
AM
1625 if (hi->root.type == bfd_link_hash_indirect)
1626 flip = hi;
45d6a902
AM
1627 else
1628 /* This union may have been set to be non-NULL when this symbol
1629 was seen in a dynamic object. We must force the union to be
1630 NULL, so that it is correct for a regular symbol. */
1631 h->verinfo.vertree = NULL;
1632 }
1633
1634 /* Handle the special case of a new common symbol merging with an
1635 old symbol that looks like it might be a common symbol defined in
1636 a shared object. Note that we have already handled the case in
1637 which a new common symbol should simply override the definition
1638 in the shared library. */
1639
1640 if (! newdyn
1641 && bfd_is_com_section (sec)
1642 && olddyncommon)
1643 {
1644 /* It would be best if we could set the hash table entry to a
1645 common symbol, but we don't know what to use for the section
1646 or the alignment. */
1a72702b
AM
1647 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1648 bfd_link_hash_common, sym->st_size);
45d6a902 1649
4cc11e76 1650 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1651 larger, pretend that the new symbol has its size. */
1652
1653 if (h->size > *pvalue)
1654 *pvalue = h->size;
1655
af44c138
L
1656 /* We need to remember the alignment required by the symbol
1657 in the dynamic object. */
1658 BFD_ASSERT (pold_alignment);
1659 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1660
1661 olddef = FALSE;
1662 olddyncommon = FALSE;
1663
1664 h->root.type = bfd_link_hash_undefined;
1665 h->root.u.undef.abfd = h->root.u.def.section->owner;
1666
1667 *size_change_ok = TRUE;
1668 *type_change_ok = TRUE;
1669
6c9b78e6
AM
1670 if (hi->root.type == bfd_link_hash_indirect)
1671 flip = hi;
45d6a902
AM
1672 else
1673 h->verinfo.vertree = NULL;
1674 }
1675
1676 if (flip != NULL)
1677 {
1678 /* Handle the case where we had a versioned symbol in a dynamic
1679 library and now find a definition in a normal object. In this
1680 case, we make the versioned symbol point to the normal one. */
45d6a902 1681 flip->root.type = h->root.type;
00cbee0a 1682 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1683 h->root.type = bfd_link_hash_indirect;
1684 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1685 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1686 if (h->def_dynamic)
45d6a902 1687 {
f5385ebf
AM
1688 h->def_dynamic = 0;
1689 flip->ref_dynamic = 1;
45d6a902
AM
1690 }
1691 }
1692
45d6a902
AM
1693 return TRUE;
1694}
1695
1696/* This function is called to create an indirect symbol from the
1697 default for the symbol with the default version if needed. The
4f3fedcf 1698 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1699 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1700
28caa186 1701static bfd_boolean
268b6b39
AM
1702_bfd_elf_add_default_symbol (bfd *abfd,
1703 struct bfd_link_info *info,
1704 struct elf_link_hash_entry *h,
1705 const char *name,
1706 Elf_Internal_Sym *sym,
4f3fedcf
AM
1707 asection *sec,
1708 bfd_vma value,
1709 bfd **poldbfd,
e3c9d234 1710 bfd_boolean *dynsym)
45d6a902
AM
1711{
1712 bfd_boolean type_change_ok;
1713 bfd_boolean size_change_ok;
1714 bfd_boolean skip;
1715 char *shortname;
1716 struct elf_link_hash_entry *hi;
1717 struct bfd_link_hash_entry *bh;
9c5bfbb7 1718 const struct elf_backend_data *bed;
45d6a902
AM
1719 bfd_boolean collect;
1720 bfd_boolean dynamic;
e3c9d234 1721 bfd_boolean override;
45d6a902
AM
1722 char *p;
1723 size_t len, shortlen;
ffd65175 1724 asection *tmp_sec;
6e33951e 1725 bfd_boolean matched;
45d6a902 1726
422f1182
L
1727 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1728 return TRUE;
1729
45d6a902
AM
1730 /* If this symbol has a version, and it is the default version, we
1731 create an indirect symbol from the default name to the fully
1732 decorated name. This will cause external references which do not
1733 specify a version to be bound to this version of the symbol. */
1734 p = strchr (name, ELF_VER_CHR);
422f1182
L
1735 if (h->versioned == unknown)
1736 {
1737 if (p == NULL)
1738 {
1739 h->versioned = unversioned;
1740 return TRUE;
1741 }
1742 else
1743 {
1744 if (p[1] != ELF_VER_CHR)
1745 {
1746 h->versioned = versioned_hidden;
1747 return TRUE;
1748 }
1749 else
1750 h->versioned = versioned;
1751 }
1752 }
4373f8af
L
1753 else
1754 {
1755 /* PR ld/19073: We may see an unversioned definition after the
1756 default version. */
1757 if (p == NULL)
1758 return TRUE;
1759 }
45d6a902 1760
45d6a902
AM
1761 bed = get_elf_backend_data (abfd);
1762 collect = bed->collect;
1763 dynamic = (abfd->flags & DYNAMIC) != 0;
1764
1765 shortlen = p - name;
a50b1753 1766 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1767 if (shortname == NULL)
1768 return FALSE;
1769 memcpy (shortname, name, shortlen);
1770 shortname[shortlen] = '\0';
1771
1772 /* We are going to create a new symbol. Merge it with any existing
1773 symbol with this name. For the purposes of the merge, act as
1774 though we were defining the symbol we just defined, although we
1775 actually going to define an indirect symbol. */
1776 type_change_ok = FALSE;
1777 size_change_ok = FALSE;
6e33951e 1778 matched = TRUE;
ffd65175
AM
1779 tmp_sec = sec;
1780 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1781 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1782 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1783 return FALSE;
1784
1785 if (skip)
1786 goto nondefault;
1787
5b677558
AM
1788 if (hi->def_regular)
1789 {
1790 /* If the undecorated symbol will have a version added by a
1791 script different to H, then don't indirect to/from the
1792 undecorated symbol. This isn't ideal because we may not yet
1793 have seen symbol versions, if given by a script on the
1794 command line rather than via --version-script. */
1795 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1796 {
1797 bfd_boolean hide;
1798
1799 hi->verinfo.vertree
1800 = bfd_find_version_for_sym (info->version_info,
1801 hi->root.root.string, &hide);
1802 if (hi->verinfo.vertree != NULL && hide)
1803 {
1804 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1805 goto nondefault;
1806 }
1807 }
1808 if (hi->verinfo.vertree != NULL
1809 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1810 goto nondefault;
1811 }
1812
45d6a902
AM
1813 if (! override)
1814 {
c6e8a9a8 1815 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1816 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1817 {
1818 bh = &hi->root;
1819 if (! (_bfd_generic_link_add_one_symbol
1820 (info, abfd, shortname, BSF_INDIRECT,
1821 bfd_ind_section_ptr,
1822 0, name, FALSE, collect, &bh)))
1823 return FALSE;
1824 hi = (struct elf_link_hash_entry *) bh;
1825 }
45d6a902
AM
1826 }
1827 else
1828 {
1829 /* In this case the symbol named SHORTNAME is overriding the
1830 indirect symbol we want to add. We were planning on making
1831 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1832 is the name without a version. NAME is the fully versioned
1833 name, and it is the default version.
1834
1835 Overriding means that we already saw a definition for the
1836 symbol SHORTNAME in a regular object, and it is overriding
1837 the symbol defined in the dynamic object.
1838
1839 When this happens, we actually want to change NAME, the
1840 symbol we just added, to refer to SHORTNAME. This will cause
1841 references to NAME in the shared object to become references
1842 to SHORTNAME in the regular object. This is what we expect
1843 when we override a function in a shared object: that the
1844 references in the shared object will be mapped to the
1845 definition in the regular object. */
1846
1847 while (hi->root.type == bfd_link_hash_indirect
1848 || hi->root.type == bfd_link_hash_warning)
1849 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1850
1851 h->root.type = bfd_link_hash_indirect;
1852 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1853 if (h->def_dynamic)
45d6a902 1854 {
f5385ebf
AM
1855 h->def_dynamic = 0;
1856 hi->ref_dynamic = 1;
1857 if (hi->ref_regular
1858 || hi->def_regular)
45d6a902 1859 {
c152c796 1860 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1861 return FALSE;
1862 }
1863 }
1864
1865 /* Now set HI to H, so that the following code will set the
1866 other fields correctly. */
1867 hi = h;
1868 }
1869
fab4a87f
L
1870 /* Check if HI is a warning symbol. */
1871 if (hi->root.type == bfd_link_hash_warning)
1872 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1873
45d6a902
AM
1874 /* If there is a duplicate definition somewhere, then HI may not
1875 point to an indirect symbol. We will have reported an error to
1876 the user in that case. */
1877
1878 if (hi->root.type == bfd_link_hash_indirect)
1879 {
1880 struct elf_link_hash_entry *ht;
1881
45d6a902 1882 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1883 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1884
68c88cd4
AM
1885 /* A reference to the SHORTNAME symbol from a dynamic library
1886 will be satisfied by the versioned symbol at runtime. In
1887 effect, we have a reference to the versioned symbol. */
1888 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1889 hi->dynamic_def |= ht->dynamic_def;
1890
45d6a902
AM
1891 /* See if the new flags lead us to realize that the symbol must
1892 be dynamic. */
1893 if (! *dynsym)
1894 {
1895 if (! dynamic)
1896 {
0e1862bb 1897 if (! bfd_link_executable (info)
90c984fc 1898 || hi->def_dynamic
f5385ebf 1899 || hi->ref_dynamic)
45d6a902
AM
1900 *dynsym = TRUE;
1901 }
1902 else
1903 {
f5385ebf 1904 if (hi->ref_regular)
45d6a902
AM
1905 *dynsym = TRUE;
1906 }
1907 }
1908 }
1909
1910 /* We also need to define an indirection from the nondefault version
1911 of the symbol. */
1912
1913nondefault:
1914 len = strlen (name);
a50b1753 1915 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1916 if (shortname == NULL)
1917 return FALSE;
1918 memcpy (shortname, name, shortlen);
1919 memcpy (shortname + shortlen, p + 1, len - shortlen);
1920
1921 /* Once again, merge with any existing symbol. */
1922 type_change_ok = FALSE;
1923 size_change_ok = FALSE;
ffd65175
AM
1924 tmp_sec = sec;
1925 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1926 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1927 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1928 return FALSE;
1929
1930 if (skip)
1931 return TRUE;
1932
1933 if (override)
1934 {
1935 /* Here SHORTNAME is a versioned name, so we don't expect to see
1936 the type of override we do in the case above unless it is
4cc11e76 1937 overridden by a versioned definition. */
45d6a902
AM
1938 if (hi->root.type != bfd_link_hash_defined
1939 && hi->root.type != bfd_link_hash_defweak)
1940 (*_bfd_error_handler)
d003868e
AM
1941 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1942 abfd, shortname);
45d6a902
AM
1943 }
1944 else
1945 {
1946 bh = &hi->root;
1947 if (! (_bfd_generic_link_add_one_symbol
1948 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1949 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1950 return FALSE;
1951 hi = (struct elf_link_hash_entry *) bh;
1952
1953 /* If there is a duplicate definition somewhere, then HI may not
1954 point to an indirect symbol. We will have reported an error
1955 to the user in that case. */
1956
1957 if (hi->root.type == bfd_link_hash_indirect)
1958 {
fcfa13d2 1959 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
1960 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1961 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
1962
1963 /* See if the new flags lead us to realize that the symbol
1964 must be dynamic. */
1965 if (! *dynsym)
1966 {
1967 if (! dynamic)
1968 {
0e1862bb 1969 if (! bfd_link_executable (info)
f5385ebf 1970 || hi->ref_dynamic)
45d6a902
AM
1971 *dynsym = TRUE;
1972 }
1973 else
1974 {
f5385ebf 1975 if (hi->ref_regular)
45d6a902
AM
1976 *dynsym = TRUE;
1977 }
1978 }
1979 }
1980 }
1981
1982 return TRUE;
1983}
1984\f
1985/* This routine is used to export all defined symbols into the dynamic
1986 symbol table. It is called via elf_link_hash_traverse. */
1987
28caa186 1988static bfd_boolean
268b6b39 1989_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1990{
a50b1753 1991 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
1992
1993 /* Ignore indirect symbols. These are added by the versioning code. */
1994 if (h->root.type == bfd_link_hash_indirect)
1995 return TRUE;
1996
7686d77d
AM
1997 /* Ignore this if we won't export it. */
1998 if (!eif->info->export_dynamic && !h->dynamic)
1999 return TRUE;
45d6a902
AM
2000
2001 if (h->dynindx == -1
fd91d419
L
2002 && (h->def_regular || h->ref_regular)
2003 && ! bfd_hide_sym_by_version (eif->info->version_info,
2004 h->root.root.string))
45d6a902 2005 {
fd91d419 2006 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2007 {
fd91d419
L
2008 eif->failed = TRUE;
2009 return FALSE;
45d6a902
AM
2010 }
2011 }
2012
2013 return TRUE;
2014}
2015\f
2016/* Look through the symbols which are defined in other shared
2017 libraries and referenced here. Update the list of version
2018 dependencies. This will be put into the .gnu.version_r section.
2019 This function is called via elf_link_hash_traverse. */
2020
28caa186 2021static bfd_boolean
268b6b39
AM
2022_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2023 void *data)
45d6a902 2024{
a50b1753 2025 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2026 Elf_Internal_Verneed *t;
2027 Elf_Internal_Vernaux *a;
2028 bfd_size_type amt;
2029
45d6a902
AM
2030 /* We only care about symbols defined in shared objects with version
2031 information. */
f5385ebf
AM
2032 if (!h->def_dynamic
2033 || h->def_regular
45d6a902 2034 || h->dynindx == -1
7b20f099
AM
2035 || h->verinfo.verdef == NULL
2036 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2037 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2038 return TRUE;
2039
2040 /* See if we already know about this version. */
28caa186
AM
2041 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2042 t != NULL;
2043 t = t->vn_nextref)
45d6a902
AM
2044 {
2045 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2046 continue;
2047
2048 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2049 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2050 return TRUE;
2051
2052 break;
2053 }
2054
2055 /* This is a new version. Add it to tree we are building. */
2056
2057 if (t == NULL)
2058 {
2059 amt = sizeof *t;
a50b1753 2060 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2061 if (t == NULL)
2062 {
2063 rinfo->failed = TRUE;
2064 return FALSE;
2065 }
2066
2067 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2068 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2069 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2070 }
2071
2072 amt = sizeof *a;
a50b1753 2073 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2074 if (a == NULL)
2075 {
2076 rinfo->failed = TRUE;
2077 return FALSE;
2078 }
45d6a902
AM
2079
2080 /* Note that we are copying a string pointer here, and testing it
2081 above. If bfd_elf_string_from_elf_section is ever changed to
2082 discard the string data when low in memory, this will have to be
2083 fixed. */
2084 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2085
2086 a->vna_flags = h->verinfo.verdef->vd_flags;
2087 a->vna_nextptr = t->vn_auxptr;
2088
2089 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2090 ++rinfo->vers;
2091
2092 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2093
2094 t->vn_auxptr = a;
2095
2096 return TRUE;
2097}
2098
2099/* Figure out appropriate versions for all the symbols. We may not
2100 have the version number script until we have read all of the input
2101 files, so until that point we don't know which symbols should be
2102 local. This function is called via elf_link_hash_traverse. */
2103
28caa186 2104static bfd_boolean
268b6b39 2105_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2106{
28caa186 2107 struct elf_info_failed *sinfo;
45d6a902 2108 struct bfd_link_info *info;
9c5bfbb7 2109 const struct elf_backend_data *bed;
45d6a902
AM
2110 struct elf_info_failed eif;
2111 char *p;
45d6a902 2112
a50b1753 2113 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2114 info = sinfo->info;
2115
45d6a902
AM
2116 /* Fix the symbol flags. */
2117 eif.failed = FALSE;
2118 eif.info = info;
2119 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2120 {
2121 if (eif.failed)
2122 sinfo->failed = TRUE;
2123 return FALSE;
2124 }
2125
2126 /* We only need version numbers for symbols defined in regular
2127 objects. */
f5385ebf 2128 if (!h->def_regular)
45d6a902
AM
2129 return TRUE;
2130
28caa186 2131 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2132 p = strchr (h->root.root.string, ELF_VER_CHR);
2133 if (p != NULL && h->verinfo.vertree == NULL)
2134 {
2135 struct bfd_elf_version_tree *t;
45d6a902 2136
45d6a902
AM
2137 ++p;
2138 if (*p == ELF_VER_CHR)
6e33951e 2139 ++p;
45d6a902
AM
2140
2141 /* If there is no version string, we can just return out. */
2142 if (*p == '\0')
6e33951e 2143 return TRUE;
45d6a902
AM
2144
2145 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2146 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2147 {
2148 if (strcmp (t->name, p) == 0)
2149 {
2150 size_t len;
2151 char *alc;
2152 struct bfd_elf_version_expr *d;
2153
2154 len = p - h->root.root.string;
a50b1753 2155 alc = (char *) bfd_malloc (len);
45d6a902 2156 if (alc == NULL)
14b1c01e
AM
2157 {
2158 sinfo->failed = TRUE;
2159 return FALSE;
2160 }
45d6a902
AM
2161 memcpy (alc, h->root.root.string, len - 1);
2162 alc[len - 1] = '\0';
2163 if (alc[len - 2] == ELF_VER_CHR)
2164 alc[len - 2] = '\0';
2165
2166 h->verinfo.vertree = t;
2167 t->used = TRUE;
2168 d = NULL;
2169
108ba305
JJ
2170 if (t->globals.list != NULL)
2171 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2172
2173 /* See if there is anything to force this symbol to
2174 local scope. */
108ba305 2175 if (d == NULL && t->locals.list != NULL)
45d6a902 2176 {
108ba305
JJ
2177 d = (*t->match) (&t->locals, NULL, alc);
2178 if (d != NULL
2179 && h->dynindx != -1
108ba305
JJ
2180 && ! info->export_dynamic)
2181 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2182 }
2183
2184 free (alc);
2185 break;
2186 }
2187 }
2188
2189 /* If we are building an application, we need to create a
2190 version node for this version. */
0e1862bb 2191 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2192 {
2193 struct bfd_elf_version_tree **pp;
2194 int version_index;
2195
2196 /* If we aren't going to export this symbol, we don't need
2197 to worry about it. */
2198 if (h->dynindx == -1)
2199 return TRUE;
2200
ef53be89
AM
2201 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2202 sizeof *t);
45d6a902
AM
2203 if (t == NULL)
2204 {
2205 sinfo->failed = TRUE;
2206 return FALSE;
2207 }
2208
45d6a902 2209 t->name = p;
45d6a902
AM
2210 t->name_indx = (unsigned int) -1;
2211 t->used = TRUE;
2212
2213 version_index = 1;
2214 /* Don't count anonymous version tag. */
fd91d419
L
2215 if (sinfo->info->version_info != NULL
2216 && sinfo->info->version_info->vernum == 0)
45d6a902 2217 version_index = 0;
fd91d419
L
2218 for (pp = &sinfo->info->version_info;
2219 *pp != NULL;
2220 pp = &(*pp)->next)
45d6a902
AM
2221 ++version_index;
2222 t->vernum = version_index;
2223
2224 *pp = t;
2225
2226 h->verinfo.vertree = t;
2227 }
2228 else if (t == NULL)
2229 {
2230 /* We could not find the version for a symbol when
2231 generating a shared archive. Return an error. */
2232 (*_bfd_error_handler)
c55fe096 2233 (_("%B: version node not found for symbol %s"),
28caa186 2234 info->output_bfd, h->root.root.string);
45d6a902
AM
2235 bfd_set_error (bfd_error_bad_value);
2236 sinfo->failed = TRUE;
2237 return FALSE;
2238 }
45d6a902
AM
2239 }
2240
2241 /* If we don't have a version for this symbol, see if we can find
2242 something. */
fd91d419 2243 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2244 {
1e8fa21e 2245 bfd_boolean hide;
ae5a3597 2246
fd91d419
L
2247 h->verinfo.vertree
2248 = bfd_find_version_for_sym (sinfo->info->version_info,
2249 h->root.root.string, &hide);
1e8fa21e
AM
2250 if (h->verinfo.vertree != NULL && hide)
2251 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2252 }
2253
2254 return TRUE;
2255}
2256\f
45d6a902
AM
2257/* Read and swap the relocs from the section indicated by SHDR. This
2258 may be either a REL or a RELA section. The relocations are
2259 translated into RELA relocations and stored in INTERNAL_RELOCS,
2260 which should have already been allocated to contain enough space.
2261 The EXTERNAL_RELOCS are a buffer where the external form of the
2262 relocations should be stored.
2263
2264 Returns FALSE if something goes wrong. */
2265
2266static bfd_boolean
268b6b39 2267elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2268 asection *sec,
268b6b39
AM
2269 Elf_Internal_Shdr *shdr,
2270 void *external_relocs,
2271 Elf_Internal_Rela *internal_relocs)
45d6a902 2272{
9c5bfbb7 2273 const struct elf_backend_data *bed;
268b6b39 2274 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2275 const bfd_byte *erela;
2276 const bfd_byte *erelaend;
2277 Elf_Internal_Rela *irela;
243ef1e0
L
2278 Elf_Internal_Shdr *symtab_hdr;
2279 size_t nsyms;
45d6a902 2280
45d6a902
AM
2281 /* Position ourselves at the start of the section. */
2282 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2283 return FALSE;
2284
2285 /* Read the relocations. */
2286 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2287 return FALSE;
2288
243ef1e0 2289 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2290 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2291
45d6a902
AM
2292 bed = get_elf_backend_data (abfd);
2293
2294 /* Convert the external relocations to the internal format. */
2295 if (shdr->sh_entsize == bed->s->sizeof_rel)
2296 swap_in = bed->s->swap_reloc_in;
2297 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2298 swap_in = bed->s->swap_reloca_in;
2299 else
2300 {
2301 bfd_set_error (bfd_error_wrong_format);
2302 return FALSE;
2303 }
2304
a50b1753 2305 erela = (const bfd_byte *) external_relocs;
51992aec 2306 erelaend = erela + shdr->sh_size;
45d6a902
AM
2307 irela = internal_relocs;
2308 while (erela < erelaend)
2309 {
243ef1e0
L
2310 bfd_vma r_symndx;
2311
45d6a902 2312 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2313 r_symndx = ELF32_R_SYM (irela->r_info);
2314 if (bed->s->arch_size == 64)
2315 r_symndx >>= 24;
ce98a316
NC
2316 if (nsyms > 0)
2317 {
2318 if ((size_t) r_symndx >= nsyms)
2319 {
2320 (*_bfd_error_handler)
2321 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2322 " for offset 0x%lx in section `%A'"),
2323 abfd, sec,
2324 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2325 bfd_set_error (bfd_error_bad_value);
2326 return FALSE;
2327 }
2328 }
cf35638d 2329 else if (r_symndx != STN_UNDEF)
243ef1e0
L
2330 {
2331 (*_bfd_error_handler)
ce98a316
NC
2332 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2333 " when the object file has no symbol table"),
d003868e
AM
2334 abfd, sec,
2335 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2336 bfd_set_error (bfd_error_bad_value);
2337 return FALSE;
2338 }
45d6a902
AM
2339 irela += bed->s->int_rels_per_ext_rel;
2340 erela += shdr->sh_entsize;
2341 }
2342
2343 return TRUE;
2344}
2345
2346/* Read and swap the relocs for a section O. They may have been
2347 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2348 not NULL, they are used as buffers to read into. They are known to
2349 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2350 the return value is allocated using either malloc or bfd_alloc,
2351 according to the KEEP_MEMORY argument. If O has two relocation
2352 sections (both REL and RELA relocations), then the REL_HDR
2353 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2354 RELA_HDR relocations. */
45d6a902
AM
2355
2356Elf_Internal_Rela *
268b6b39
AM
2357_bfd_elf_link_read_relocs (bfd *abfd,
2358 asection *o,
2359 void *external_relocs,
2360 Elf_Internal_Rela *internal_relocs,
2361 bfd_boolean keep_memory)
45d6a902 2362{
268b6b39 2363 void *alloc1 = NULL;
45d6a902 2364 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2365 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2366 struct bfd_elf_section_data *esdo = elf_section_data (o);
2367 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2368
d4730f92
BS
2369 if (esdo->relocs != NULL)
2370 return esdo->relocs;
45d6a902
AM
2371
2372 if (o->reloc_count == 0)
2373 return NULL;
2374
45d6a902
AM
2375 if (internal_relocs == NULL)
2376 {
2377 bfd_size_type size;
2378
2379 size = o->reloc_count;
2380 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2381 if (keep_memory)
a50b1753 2382 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2383 else
a50b1753 2384 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2385 if (internal_relocs == NULL)
2386 goto error_return;
2387 }
2388
2389 if (external_relocs == NULL)
2390 {
d4730f92
BS
2391 bfd_size_type size = 0;
2392
2393 if (esdo->rel.hdr)
2394 size += esdo->rel.hdr->sh_size;
2395 if (esdo->rela.hdr)
2396 size += esdo->rela.hdr->sh_size;
45d6a902 2397
268b6b39 2398 alloc1 = bfd_malloc (size);
45d6a902
AM
2399 if (alloc1 == NULL)
2400 goto error_return;
2401 external_relocs = alloc1;
2402 }
2403
d4730f92
BS
2404 internal_rela_relocs = internal_relocs;
2405 if (esdo->rel.hdr)
2406 {
2407 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2408 external_relocs,
2409 internal_relocs))
2410 goto error_return;
2411 external_relocs = (((bfd_byte *) external_relocs)
2412 + esdo->rel.hdr->sh_size);
2413 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2414 * bed->s->int_rels_per_ext_rel);
2415 }
2416
2417 if (esdo->rela.hdr
2418 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2419 external_relocs,
2420 internal_rela_relocs)))
45d6a902
AM
2421 goto error_return;
2422
2423 /* Cache the results for next time, if we can. */
2424 if (keep_memory)
d4730f92 2425 esdo->relocs = internal_relocs;
45d6a902
AM
2426
2427 if (alloc1 != NULL)
2428 free (alloc1);
2429
2430 /* Don't free alloc2, since if it was allocated we are passing it
2431 back (under the name of internal_relocs). */
2432
2433 return internal_relocs;
2434
2435 error_return:
2436 if (alloc1 != NULL)
2437 free (alloc1);
2438 if (alloc2 != NULL)
4dd07732
AM
2439 {
2440 if (keep_memory)
2441 bfd_release (abfd, alloc2);
2442 else
2443 free (alloc2);
2444 }
45d6a902
AM
2445 return NULL;
2446}
2447
2448/* Compute the size of, and allocate space for, REL_HDR which is the
2449 section header for a section containing relocations for O. */
2450
28caa186 2451static bfd_boolean
268b6b39 2452_bfd_elf_link_size_reloc_section (bfd *abfd,
d4730f92 2453 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2454{
d4730f92 2455 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2456
2457 /* That allows us to calculate the size of the section. */
d4730f92 2458 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2459
2460 /* The contents field must last into write_object_contents, so we
2461 allocate it with bfd_alloc rather than malloc. Also since we
2462 cannot be sure that the contents will actually be filled in,
2463 we zero the allocated space. */
a50b1753 2464 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2465 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2466 return FALSE;
2467
d4730f92 2468 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2469 {
2470 struct elf_link_hash_entry **p;
2471
ca4be51c
AM
2472 p = ((struct elf_link_hash_entry **)
2473 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2474 if (p == NULL)
2475 return FALSE;
2476
d4730f92 2477 reldata->hashes = p;
45d6a902
AM
2478 }
2479
2480 return TRUE;
2481}
2482
2483/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2484 originated from the section given by INPUT_REL_HDR) to the
2485 OUTPUT_BFD. */
2486
2487bfd_boolean
268b6b39
AM
2488_bfd_elf_link_output_relocs (bfd *output_bfd,
2489 asection *input_section,
2490 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2491 Elf_Internal_Rela *internal_relocs,
2492 struct elf_link_hash_entry **rel_hash
2493 ATTRIBUTE_UNUSED)
45d6a902
AM
2494{
2495 Elf_Internal_Rela *irela;
2496 Elf_Internal_Rela *irelaend;
2497 bfd_byte *erel;
d4730f92 2498 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2499 asection *output_section;
9c5bfbb7 2500 const struct elf_backend_data *bed;
268b6b39 2501 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2502 struct bfd_elf_section_data *esdo;
45d6a902
AM
2503
2504 output_section = input_section->output_section;
45d6a902 2505
d4730f92
BS
2506 bed = get_elf_backend_data (output_bfd);
2507 esdo = elf_section_data (output_section);
2508 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2509 {
d4730f92
BS
2510 output_reldata = &esdo->rel;
2511 swap_out = bed->s->swap_reloc_out;
45d6a902 2512 }
d4730f92
BS
2513 else if (esdo->rela.hdr
2514 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2515 {
d4730f92
BS
2516 output_reldata = &esdo->rela;
2517 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2518 }
2519 else
2520 {
2521 (*_bfd_error_handler)
d003868e
AM
2522 (_("%B: relocation size mismatch in %B section %A"),
2523 output_bfd, input_section->owner, input_section);
297d8443 2524 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2525 return FALSE;
2526 }
2527
d4730f92
BS
2528 erel = output_reldata->hdr->contents;
2529 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2530 irela = internal_relocs;
2531 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2532 * bed->s->int_rels_per_ext_rel);
2533 while (irela < irelaend)
2534 {
2535 (*swap_out) (output_bfd, irela, erel);
2536 irela += bed->s->int_rels_per_ext_rel;
2537 erel += input_rel_hdr->sh_entsize;
2538 }
2539
2540 /* Bump the counter, so that we know where to add the next set of
2541 relocations. */
d4730f92 2542 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2543
2544 return TRUE;
2545}
2546\f
508c3946
L
2547/* Make weak undefined symbols in PIE dynamic. */
2548
2549bfd_boolean
2550_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2551 struct elf_link_hash_entry *h)
2552{
0e1862bb 2553 if (bfd_link_pie (info)
508c3946
L
2554 && h->dynindx == -1
2555 && h->root.type == bfd_link_hash_undefweak)
2556 return bfd_elf_link_record_dynamic_symbol (info, h);
2557
2558 return TRUE;
2559}
2560
45d6a902
AM
2561/* Fix up the flags for a symbol. This handles various cases which
2562 can only be fixed after all the input files are seen. This is
2563 currently called by both adjust_dynamic_symbol and
2564 assign_sym_version, which is unnecessary but perhaps more robust in
2565 the face of future changes. */
2566
28caa186 2567static bfd_boolean
268b6b39
AM
2568_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2569 struct elf_info_failed *eif)
45d6a902 2570{
33774f08 2571 const struct elf_backend_data *bed;
508c3946 2572
45d6a902
AM
2573 /* If this symbol was mentioned in a non-ELF file, try to set
2574 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2575 permit a non-ELF file to correctly refer to a symbol defined in
2576 an ELF dynamic object. */
f5385ebf 2577 if (h->non_elf)
45d6a902
AM
2578 {
2579 while (h->root.type == bfd_link_hash_indirect)
2580 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2581
2582 if (h->root.type != bfd_link_hash_defined
2583 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2584 {
2585 h->ref_regular = 1;
2586 h->ref_regular_nonweak = 1;
2587 }
45d6a902
AM
2588 else
2589 {
2590 if (h->root.u.def.section->owner != NULL
2591 && (bfd_get_flavour (h->root.u.def.section->owner)
2592 == bfd_target_elf_flavour))
f5385ebf
AM
2593 {
2594 h->ref_regular = 1;
2595 h->ref_regular_nonweak = 1;
2596 }
45d6a902 2597 else
f5385ebf 2598 h->def_regular = 1;
45d6a902
AM
2599 }
2600
2601 if (h->dynindx == -1
f5385ebf
AM
2602 && (h->def_dynamic
2603 || h->ref_dynamic))
45d6a902 2604 {
c152c796 2605 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2606 {
2607 eif->failed = TRUE;
2608 return FALSE;
2609 }
2610 }
2611 }
2612 else
2613 {
f5385ebf 2614 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2615 was first seen in a non-ELF file. Fortunately, if the symbol
2616 was first seen in an ELF file, we're probably OK unless the
2617 symbol was defined in a non-ELF file. Catch that case here.
2618 FIXME: We're still in trouble if the symbol was first seen in
2619 a dynamic object, and then later in a non-ELF regular object. */
2620 if ((h->root.type == bfd_link_hash_defined
2621 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2622 && !h->def_regular
45d6a902
AM
2623 && (h->root.u.def.section->owner != NULL
2624 ? (bfd_get_flavour (h->root.u.def.section->owner)
2625 != bfd_target_elf_flavour)
2626 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2627 && !h->def_dynamic)))
2628 h->def_regular = 1;
45d6a902
AM
2629 }
2630
508c3946 2631 /* Backend specific symbol fixup. */
33774f08
AM
2632 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2633 if (bed->elf_backend_fixup_symbol
2634 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2635 return FALSE;
508c3946 2636
45d6a902
AM
2637 /* If this is a final link, and the symbol was defined as a common
2638 symbol in a regular object file, and there was no definition in
2639 any dynamic object, then the linker will have allocated space for
f5385ebf 2640 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2641 flag will not have been set. */
2642 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2643 && !h->def_regular
2644 && h->ref_regular
2645 && !h->def_dynamic
96f29d96 2646 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2647 h->def_regular = 1;
45d6a902
AM
2648
2649 /* If -Bsymbolic was used (which means to bind references to global
2650 symbols to the definition within the shared object), and this
2651 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2652 need a PLT entry. Likewise, if the symbol has non-default
2653 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2654 will force it local. */
f5385ebf 2655 if (h->needs_plt
0e1862bb 2656 && bfd_link_pic (eif->info)
0eddce27 2657 && is_elf_hash_table (eif->info->hash)
55255dae 2658 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2659 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2660 && h->def_regular)
45d6a902 2661 {
45d6a902
AM
2662 bfd_boolean force_local;
2663
45d6a902
AM
2664 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2665 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2666 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2667 }
2668
2669 /* If a weak undefined symbol has non-default visibility, we also
2670 hide it from the dynamic linker. */
9c7a29a3 2671 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2672 && h->root.type == bfd_link_hash_undefweak)
33774f08 2673 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2674
2675 /* If this is a weak defined symbol in a dynamic object, and we know
2676 the real definition in the dynamic object, copy interesting flags
2677 over to the real definition. */
f6e332e6 2678 if (h->u.weakdef != NULL)
45d6a902 2679 {
45d6a902
AM
2680 /* If the real definition is defined by a regular object file,
2681 don't do anything special. See the longer description in
2682 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2683 if (h->u.weakdef->def_regular)
f6e332e6 2684 h->u.weakdef = NULL;
45d6a902 2685 else
a26587ba 2686 {
4e6b54a6
AM
2687 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2688
2689 while (h->root.type == bfd_link_hash_indirect)
2690 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2691
2692 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2693 || h->root.type == bfd_link_hash_defweak);
2694 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2695 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2696 || weakdef->root.type == bfd_link_hash_defweak);
2697 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2698 }
45d6a902
AM
2699 }
2700
2701 return TRUE;
2702}
2703
2704/* Make the backend pick a good value for a dynamic symbol. This is
2705 called via elf_link_hash_traverse, and also calls itself
2706 recursively. */
2707
28caa186 2708static bfd_boolean
268b6b39 2709_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2710{
a50b1753 2711 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2712 bfd *dynobj;
9c5bfbb7 2713 const struct elf_backend_data *bed;
45d6a902 2714
0eddce27 2715 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2716 return FALSE;
2717
45d6a902
AM
2718 /* Ignore indirect symbols. These are added by the versioning code. */
2719 if (h->root.type == bfd_link_hash_indirect)
2720 return TRUE;
2721
2722 /* Fix the symbol flags. */
2723 if (! _bfd_elf_fix_symbol_flags (h, eif))
2724 return FALSE;
2725
2726 /* If this symbol does not require a PLT entry, and it is not
2727 defined by a dynamic object, or is not referenced by a regular
2728 object, ignore it. We do have to handle a weak defined symbol,
2729 even if no regular object refers to it, if we decided to add it
2730 to the dynamic symbol table. FIXME: Do we normally need to worry
2731 about symbols which are defined by one dynamic object and
2732 referenced by another one? */
f5385ebf 2733 if (!h->needs_plt
91e21fb7 2734 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2735 && (h->def_regular
2736 || !h->def_dynamic
2737 || (!h->ref_regular
f6e332e6 2738 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2739 {
a6aa5195 2740 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2741 return TRUE;
2742 }
2743
2744 /* If we've already adjusted this symbol, don't do it again. This
2745 can happen via a recursive call. */
f5385ebf 2746 if (h->dynamic_adjusted)
45d6a902
AM
2747 return TRUE;
2748
2749 /* Don't look at this symbol again. Note that we must set this
2750 after checking the above conditions, because we may look at a
2751 symbol once, decide not to do anything, and then get called
2752 recursively later after REF_REGULAR is set below. */
f5385ebf 2753 h->dynamic_adjusted = 1;
45d6a902
AM
2754
2755 /* If this is a weak definition, and we know a real definition, and
2756 the real symbol is not itself defined by a regular object file,
2757 then get a good value for the real definition. We handle the
2758 real symbol first, for the convenience of the backend routine.
2759
2760 Note that there is a confusing case here. If the real definition
2761 is defined by a regular object file, we don't get the real symbol
2762 from the dynamic object, but we do get the weak symbol. If the
2763 processor backend uses a COPY reloc, then if some routine in the
2764 dynamic object changes the real symbol, we will not see that
2765 change in the corresponding weak symbol. This is the way other
2766 ELF linkers work as well, and seems to be a result of the shared
2767 library model.
2768
2769 I will clarify this issue. Most SVR4 shared libraries define the
2770 variable _timezone and define timezone as a weak synonym. The
2771 tzset call changes _timezone. If you write
2772 extern int timezone;
2773 int _timezone = 5;
2774 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2775 you might expect that, since timezone is a synonym for _timezone,
2776 the same number will print both times. However, if the processor
2777 backend uses a COPY reloc, then actually timezone will be copied
2778 into your process image, and, since you define _timezone
2779 yourself, _timezone will not. Thus timezone and _timezone will
2780 wind up at different memory locations. The tzset call will set
2781 _timezone, leaving timezone unchanged. */
2782
f6e332e6 2783 if (h->u.weakdef != NULL)
45d6a902 2784 {
ec24dc88
AM
2785 /* If we get to this point, there is an implicit reference to
2786 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2787 h->u.weakdef->ref_regular = 1;
45d6a902 2788
ec24dc88
AM
2789 /* Ensure that the backend adjust_dynamic_symbol function sees
2790 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2791 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2792 return FALSE;
2793 }
2794
2795 /* If a symbol has no type and no size and does not require a PLT
2796 entry, then we are probably about to do the wrong thing here: we
2797 are probably going to create a COPY reloc for an empty object.
2798 This case can arise when a shared object is built with assembly
2799 code, and the assembly code fails to set the symbol type. */
2800 if (h->size == 0
2801 && h->type == STT_NOTYPE
f5385ebf 2802 && !h->needs_plt)
45d6a902
AM
2803 (*_bfd_error_handler)
2804 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2805 h->root.root.string);
2806
2807 dynobj = elf_hash_table (eif->info)->dynobj;
2808 bed = get_elf_backend_data (dynobj);
e7c33416 2809
45d6a902
AM
2810 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2811 {
2812 eif->failed = TRUE;
2813 return FALSE;
2814 }
2815
2816 return TRUE;
2817}
2818
027297b7
L
2819/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2820 DYNBSS. */
2821
2822bfd_boolean
6cabe1ea
AM
2823_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2824 struct elf_link_hash_entry *h,
027297b7
L
2825 asection *dynbss)
2826{
91ac5911 2827 unsigned int power_of_two;
027297b7
L
2828 bfd_vma mask;
2829 asection *sec = h->root.u.def.section;
2830
2831 /* The section aligment of definition is the maximum alignment
91ac5911
L
2832 requirement of symbols defined in the section. Since we don't
2833 know the symbol alignment requirement, we start with the
2834 maximum alignment and check low bits of the symbol address
2835 for the minimum alignment. */
2836 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2837 mask = ((bfd_vma) 1 << power_of_two) - 1;
2838 while ((h->root.u.def.value & mask) != 0)
2839 {
2840 mask >>= 1;
2841 --power_of_two;
2842 }
027297b7 2843
91ac5911
L
2844 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2845 dynbss))
027297b7
L
2846 {
2847 /* Adjust the section alignment if needed. */
2848 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2849 power_of_two))
027297b7
L
2850 return FALSE;
2851 }
2852
91ac5911 2853 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2854 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2855
2856 /* Define the symbol as being at this point in DYNBSS. */
2857 h->root.u.def.section = dynbss;
2858 h->root.u.def.value = dynbss->size;
2859
2860 /* Increment the size of DYNBSS to make room for the symbol. */
2861 dynbss->size += h->size;
2862
f7483970
L
2863 /* No error if extern_protected_data is true. */
2864 if (h->protected_def
889c2a67
L
2865 && (!info->extern_protected_data
2866 || (info->extern_protected_data < 0
2867 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2868 info->callbacks->einfo
2869 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2870 h->root.root.string);
6cabe1ea 2871
027297b7
L
2872 return TRUE;
2873}
2874
45d6a902
AM
2875/* Adjust all external symbols pointing into SEC_MERGE sections
2876 to reflect the object merging within the sections. */
2877
28caa186 2878static bfd_boolean
268b6b39 2879_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2880{
2881 asection *sec;
2882
45d6a902
AM
2883 if ((h->root.type == bfd_link_hash_defined
2884 || h->root.type == bfd_link_hash_defweak)
2885 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2886 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2887 {
a50b1753 2888 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2889
2890 h->root.u.def.value =
2891 _bfd_merged_section_offset (output_bfd,
2892 &h->root.u.def.section,
2893 elf_section_data (sec)->sec_info,
753731ee 2894 h->root.u.def.value);
45d6a902
AM
2895 }
2896
2897 return TRUE;
2898}
986a241f
RH
2899
2900/* Returns false if the symbol referred to by H should be considered
2901 to resolve local to the current module, and true if it should be
2902 considered to bind dynamically. */
2903
2904bfd_boolean
268b6b39
AM
2905_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2906 struct bfd_link_info *info,
89a2ee5a 2907 bfd_boolean not_local_protected)
986a241f
RH
2908{
2909 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2910 const struct elf_backend_data *bed;
2911 struct elf_link_hash_table *hash_table;
986a241f
RH
2912
2913 if (h == NULL)
2914 return FALSE;
2915
2916 while (h->root.type == bfd_link_hash_indirect
2917 || h->root.type == bfd_link_hash_warning)
2918 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2919
2920 /* If it was forced local, then clearly it's not dynamic. */
2921 if (h->dynindx == -1)
2922 return FALSE;
f5385ebf 2923 if (h->forced_local)
986a241f
RH
2924 return FALSE;
2925
2926 /* Identify the cases where name binding rules say that a
2927 visible symbol resolves locally. */
0e1862bb
L
2928 binding_stays_local_p = (bfd_link_executable (info)
2929 || SYMBOLIC_BIND (info, h));
986a241f
RH
2930
2931 switch (ELF_ST_VISIBILITY (h->other))
2932 {
2933 case STV_INTERNAL:
2934 case STV_HIDDEN:
2935 return FALSE;
2936
2937 case STV_PROTECTED:
fcb93ecf
PB
2938 hash_table = elf_hash_table (info);
2939 if (!is_elf_hash_table (hash_table))
2940 return FALSE;
2941
2942 bed = get_elf_backend_data (hash_table->dynobj);
2943
986a241f
RH
2944 /* Proper resolution for function pointer equality may require
2945 that these symbols perhaps be resolved dynamically, even though
2946 we should be resolving them to the current module. */
89a2ee5a 2947 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2948 binding_stays_local_p = TRUE;
2949 break;
2950
2951 default:
986a241f
RH
2952 break;
2953 }
2954
aa37626c 2955 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2956 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2957 return TRUE;
2958
986a241f
RH
2959 /* Otherwise, the symbol is dynamic if binding rules don't tell
2960 us that it remains local. */
2961 return !binding_stays_local_p;
2962}
f6c52c13
AM
2963
2964/* Return true if the symbol referred to by H should be considered
2965 to resolve local to the current module, and false otherwise. Differs
2966 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2967 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2968 for the place where forced_local and dynindx == -1 are tested. If
2969 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2970 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2971 the symbol is local only for defined symbols.
2972 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2973 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2974 treatment of undefined weak symbols. For those that do not make
2975 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2976
2977bfd_boolean
268b6b39
AM
2978_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2979 struct bfd_link_info *info,
2980 bfd_boolean local_protected)
f6c52c13 2981{
fcb93ecf
PB
2982 const struct elf_backend_data *bed;
2983 struct elf_link_hash_table *hash_table;
2984
f6c52c13
AM
2985 /* If it's a local sym, of course we resolve locally. */
2986 if (h == NULL)
2987 return TRUE;
2988
d95edcac
L
2989 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2990 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2991 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2992 return TRUE;
2993
7e2294f9
AO
2994 /* Common symbols that become definitions don't get the DEF_REGULAR
2995 flag set, so test it first, and don't bail out. */
2996 if (ELF_COMMON_DEF_P (h))
2997 /* Do nothing. */;
f6c52c13 2998 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2999 resolve locally. The sym is either undefined or dynamic. */
3000 else if (!h->def_regular)
f6c52c13
AM
3001 return FALSE;
3002
3003 /* Forced local symbols resolve locally. */
f5385ebf 3004 if (h->forced_local)
f6c52c13
AM
3005 return TRUE;
3006
3007 /* As do non-dynamic symbols. */
3008 if (h->dynindx == -1)
3009 return TRUE;
3010
3011 /* At this point, we know the symbol is defined and dynamic. In an
3012 executable it must resolve locally, likewise when building symbolic
3013 shared libraries. */
0e1862bb 3014 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3015 return TRUE;
3016
3017 /* Now deal with defined dynamic symbols in shared libraries. Ones
3018 with default visibility might not resolve locally. */
3019 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3020 return FALSE;
3021
fcb93ecf
PB
3022 hash_table = elf_hash_table (info);
3023 if (!is_elf_hash_table (hash_table))
3024 return TRUE;
3025
3026 bed = get_elf_backend_data (hash_table->dynobj);
3027
f7483970
L
3028 /* If extern_protected_data is false, STV_PROTECTED non-function
3029 symbols are local. */
889c2a67
L
3030 if ((!info->extern_protected_data
3031 || (info->extern_protected_data < 0
3032 && !bed->extern_protected_data))
3033 && !bed->is_function_type (h->type))
1c16dfa5
L
3034 return TRUE;
3035
f6c52c13 3036 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3037 symbols be treated as dynamic symbols. If the address of a
3038 function not defined in an executable is set to that function's
3039 plt entry in the executable, then the address of the function in
3040 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3041 return local_protected;
3042}
e1918d23
AM
3043
3044/* Caches some TLS segment info, and ensures that the TLS segment vma is
3045 aligned. Returns the first TLS output section. */
3046
3047struct bfd_section *
3048_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3049{
3050 struct bfd_section *sec, *tls;
3051 unsigned int align = 0;
3052
3053 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3054 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3055 break;
3056 tls = sec;
3057
3058 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3059 if (sec->alignment_power > align)
3060 align = sec->alignment_power;
3061
3062 elf_hash_table (info)->tls_sec = tls;
3063
3064 /* Ensure the alignment of the first section is the largest alignment,
3065 so that the tls segment starts aligned. */
3066 if (tls != NULL)
3067 tls->alignment_power = align;
3068
3069 return tls;
3070}
0ad989f9
L
3071
3072/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3073static bfd_boolean
3074is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3075 Elf_Internal_Sym *sym)
3076{
a4d8e49b
L
3077 const struct elf_backend_data *bed;
3078
0ad989f9
L
3079 /* Local symbols do not count, but target specific ones might. */
3080 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3081 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3082 return FALSE;
3083
fcb93ecf 3084 bed = get_elf_backend_data (abfd);
0ad989f9 3085 /* Function symbols do not count. */
fcb93ecf 3086 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3087 return FALSE;
3088
3089 /* If the section is undefined, then so is the symbol. */
3090 if (sym->st_shndx == SHN_UNDEF)
3091 return FALSE;
3092
3093 /* If the symbol is defined in the common section, then
3094 it is a common definition and so does not count. */
a4d8e49b 3095 if (bed->common_definition (sym))
0ad989f9
L
3096 return FALSE;
3097
3098 /* If the symbol is in a target specific section then we
3099 must rely upon the backend to tell us what it is. */
3100 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3101 /* FIXME - this function is not coded yet:
3102
3103 return _bfd_is_global_symbol_definition (abfd, sym);
3104
3105 Instead for now assume that the definition is not global,
3106 Even if this is wrong, at least the linker will behave
3107 in the same way that it used to do. */
3108 return FALSE;
3109
3110 return TRUE;
3111}
3112
3113/* Search the symbol table of the archive element of the archive ABFD
3114 whose archive map contains a mention of SYMDEF, and determine if
3115 the symbol is defined in this element. */
3116static bfd_boolean
3117elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3118{
3119 Elf_Internal_Shdr * hdr;
ef53be89
AM
3120 size_t symcount;
3121 size_t extsymcount;
3122 size_t extsymoff;
0ad989f9
L
3123 Elf_Internal_Sym *isymbuf;
3124 Elf_Internal_Sym *isym;
3125 Elf_Internal_Sym *isymend;
3126 bfd_boolean result;
3127
3128 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3129 if (abfd == NULL)
3130 return FALSE;
3131
3132 if (! bfd_check_format (abfd, bfd_object))
3133 return FALSE;
3134
7dc3990e
L
3135 /* Select the appropriate symbol table. If we don't know if the
3136 object file is an IR object, give linker LTO plugin a chance to
3137 get the correct symbol table. */
3138 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3139#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3140 || (abfd->plugin_format == bfd_plugin_unknown
3141 && bfd_link_plugin_object_p (abfd))
3142#endif
3143 )
3144 {
3145 /* Use the IR symbol table if the object has been claimed by
3146 plugin. */
3147 abfd = abfd->plugin_dummy_bfd;
3148 hdr = &elf_tdata (abfd)->symtab_hdr;
3149 }
3150 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3151 hdr = &elf_tdata (abfd)->symtab_hdr;
3152 else
3153 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3154
3155 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3156
3157 /* The sh_info field of the symtab header tells us where the
3158 external symbols start. We don't care about the local symbols. */
3159 if (elf_bad_symtab (abfd))
3160 {
3161 extsymcount = symcount;
3162 extsymoff = 0;
3163 }
3164 else
3165 {
3166 extsymcount = symcount - hdr->sh_info;
3167 extsymoff = hdr->sh_info;
3168 }
3169
3170 if (extsymcount == 0)
3171 return FALSE;
3172
3173 /* Read in the symbol table. */
3174 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3175 NULL, NULL, NULL);
3176 if (isymbuf == NULL)
3177 return FALSE;
3178
3179 /* Scan the symbol table looking for SYMDEF. */
3180 result = FALSE;
3181 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3182 {
3183 const char *name;
3184
3185 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3186 isym->st_name);
3187 if (name == NULL)
3188 break;
3189
3190 if (strcmp (name, symdef->name) == 0)
3191 {
3192 result = is_global_data_symbol_definition (abfd, isym);
3193 break;
3194 }
3195 }
3196
3197 free (isymbuf);
3198
3199 return result;
3200}
3201\f
5a580b3a
AM
3202/* Add an entry to the .dynamic table. */
3203
3204bfd_boolean
3205_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3206 bfd_vma tag,
3207 bfd_vma val)
3208{
3209 struct elf_link_hash_table *hash_table;
3210 const struct elf_backend_data *bed;
3211 asection *s;
3212 bfd_size_type newsize;
3213 bfd_byte *newcontents;
3214 Elf_Internal_Dyn dyn;
3215
3216 hash_table = elf_hash_table (info);
3217 if (! is_elf_hash_table (hash_table))
3218 return FALSE;
3219
3220 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3221 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3222 BFD_ASSERT (s != NULL);
3223
eea6121a 3224 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3225 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3226 if (newcontents == NULL)
3227 return FALSE;
3228
3229 dyn.d_tag = tag;
3230 dyn.d_un.d_val = val;
eea6121a 3231 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3232
eea6121a 3233 s->size = newsize;
5a580b3a
AM
3234 s->contents = newcontents;
3235
3236 return TRUE;
3237}
3238
3239/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3240 otherwise just check whether one already exists. Returns -1 on error,
3241 1 if a DT_NEEDED tag already exists, and 0 on success. */
3242
4ad4eba5 3243static int
7e9f0867
AM
3244elf_add_dt_needed_tag (bfd *abfd,
3245 struct bfd_link_info *info,
4ad4eba5
AM
3246 const char *soname,
3247 bfd_boolean do_it)
5a580b3a
AM
3248{
3249 struct elf_link_hash_table *hash_table;
ef53be89 3250 size_t strindex;
5a580b3a 3251
7e9f0867
AM
3252 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3253 return -1;
3254
5a580b3a 3255 hash_table = elf_hash_table (info);
5a580b3a 3256 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3257 if (strindex == (size_t) -1)
5a580b3a
AM
3258 return -1;
3259
02be4619 3260 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3261 {
3262 asection *sdyn;
3263 const struct elf_backend_data *bed;
3264 bfd_byte *extdyn;
3265
3266 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3267 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3268 if (sdyn != NULL)
3269 for (extdyn = sdyn->contents;
3270 extdyn < sdyn->contents + sdyn->size;
3271 extdyn += bed->s->sizeof_dyn)
3272 {
3273 Elf_Internal_Dyn dyn;
5a580b3a 3274
7e9f0867
AM
3275 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3276 if (dyn.d_tag == DT_NEEDED
3277 && dyn.d_un.d_val == strindex)
3278 {
3279 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3280 return 1;
3281 }
3282 }
5a580b3a
AM
3283 }
3284
3285 if (do_it)
3286 {
7e9f0867
AM
3287 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3288 return -1;
3289
5a580b3a
AM
3290 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3291 return -1;
3292 }
3293 else
3294 /* We were just checking for existence of the tag. */
3295 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3296
3297 return 0;
3298}
3299
7b15fa7a
AM
3300/* Return true if SONAME is on the needed list between NEEDED and STOP
3301 (or the end of list if STOP is NULL), and needed by a library that
3302 will be loaded. */
3303
010e5ae2 3304static bfd_boolean
7b15fa7a
AM
3305on_needed_list (const char *soname,
3306 struct bfd_link_needed_list *needed,
3307 struct bfd_link_needed_list *stop)
010e5ae2 3308{
7b15fa7a
AM
3309 struct bfd_link_needed_list *look;
3310 for (look = needed; look != stop; look = look->next)
3311 if (strcmp (soname, look->name) == 0
3312 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3313 /* If needed by a library that itself is not directly
3314 needed, recursively check whether that library is
3315 indirectly needed. Since we add DT_NEEDED entries to
3316 the end of the list, library dependencies appear after
3317 the library. Therefore search prior to the current
3318 LOOK, preventing possible infinite recursion. */
3319 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3320 return TRUE;
3321
3322 return FALSE;
3323}
3324
14160578 3325/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3326static int
3327elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3328{
3329 const struct elf_link_hash_entry *h1;
3330 const struct elf_link_hash_entry *h2;
10b7e05b 3331 bfd_signed_vma vdiff;
5a580b3a
AM
3332
3333 h1 = *(const struct elf_link_hash_entry **) arg1;
3334 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3335 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3336 if (vdiff != 0)
3337 return vdiff > 0 ? 1 : -1;
3338 else
3339 {
d3435ae8 3340 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3341 if (sdiff != 0)
3342 return sdiff > 0 ? 1 : -1;
3343 }
14160578
AM
3344 vdiff = h1->size - h2->size;
3345 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3346}
4ad4eba5 3347
5a580b3a
AM
3348/* This function is used to adjust offsets into .dynstr for
3349 dynamic symbols. This is called via elf_link_hash_traverse. */
3350
3351static bfd_boolean
3352elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3353{
a50b1753 3354 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3355
5a580b3a
AM
3356 if (h->dynindx != -1)
3357 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3358 return TRUE;
3359}
3360
3361/* Assign string offsets in .dynstr, update all structures referencing
3362 them. */
3363
4ad4eba5
AM
3364static bfd_boolean
3365elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3366{
3367 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3368 struct elf_link_local_dynamic_entry *entry;
3369 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3370 bfd *dynobj = hash_table->dynobj;
3371 asection *sdyn;
3372 bfd_size_type size;
3373 const struct elf_backend_data *bed;
3374 bfd_byte *extdyn;
3375
3376 _bfd_elf_strtab_finalize (dynstr);
3377 size = _bfd_elf_strtab_size (dynstr);
3378
3379 bed = get_elf_backend_data (dynobj);
3d4d4302 3380 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3381 BFD_ASSERT (sdyn != NULL);
3382
3383 /* Update all .dynamic entries referencing .dynstr strings. */
3384 for (extdyn = sdyn->contents;
eea6121a 3385 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3386 extdyn += bed->s->sizeof_dyn)
3387 {
3388 Elf_Internal_Dyn dyn;
3389
3390 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3391 switch (dyn.d_tag)
3392 {
3393 case DT_STRSZ:
3394 dyn.d_un.d_val = size;
3395 break;
3396 case DT_NEEDED:
3397 case DT_SONAME:
3398 case DT_RPATH:
3399 case DT_RUNPATH:
3400 case DT_FILTER:
3401 case DT_AUXILIARY:
7ee314fa
AM
3402 case DT_AUDIT:
3403 case DT_DEPAUDIT:
5a580b3a
AM
3404 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3405 break;
3406 default:
3407 continue;
3408 }
3409 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3410 }
3411
3412 /* Now update local dynamic symbols. */
3413 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3414 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3415 entry->isym.st_name);
3416
3417 /* And the rest of dynamic symbols. */
3418 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3419
3420 /* Adjust version definitions. */
3421 if (elf_tdata (output_bfd)->cverdefs)
3422 {
3423 asection *s;
3424 bfd_byte *p;
ef53be89 3425 size_t i;
5a580b3a
AM
3426 Elf_Internal_Verdef def;
3427 Elf_Internal_Verdaux defaux;
3428
3d4d4302 3429 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3430 p = s->contents;
3431 do
3432 {
3433 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3434 &def);
3435 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3436 if (def.vd_aux != sizeof (Elf_External_Verdef))
3437 continue;
5a580b3a
AM
3438 for (i = 0; i < def.vd_cnt; ++i)
3439 {
3440 _bfd_elf_swap_verdaux_in (output_bfd,
3441 (Elf_External_Verdaux *) p, &defaux);
3442 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3443 defaux.vda_name);
3444 _bfd_elf_swap_verdaux_out (output_bfd,
3445 &defaux, (Elf_External_Verdaux *) p);
3446 p += sizeof (Elf_External_Verdaux);
3447 }
3448 }
3449 while (def.vd_next);
3450 }
3451
3452 /* Adjust version references. */
3453 if (elf_tdata (output_bfd)->verref)
3454 {
3455 asection *s;
3456 bfd_byte *p;
ef53be89 3457 size_t i;
5a580b3a
AM
3458 Elf_Internal_Verneed need;
3459 Elf_Internal_Vernaux needaux;
3460
3d4d4302 3461 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3462 p = s->contents;
3463 do
3464 {
3465 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3466 &need);
3467 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3468 _bfd_elf_swap_verneed_out (output_bfd, &need,
3469 (Elf_External_Verneed *) p);
3470 p += sizeof (Elf_External_Verneed);
3471 for (i = 0; i < need.vn_cnt; ++i)
3472 {
3473 _bfd_elf_swap_vernaux_in (output_bfd,
3474 (Elf_External_Vernaux *) p, &needaux);
3475 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3476 needaux.vna_name);
3477 _bfd_elf_swap_vernaux_out (output_bfd,
3478 &needaux,
3479 (Elf_External_Vernaux *) p);
3480 p += sizeof (Elf_External_Vernaux);
3481 }
3482 }
3483 while (need.vn_next);
3484 }
3485
3486 return TRUE;
3487}
3488\f
13285a1b
AM
3489/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3490 The default is to only match when the INPUT and OUTPUT are exactly
3491 the same target. */
3492
3493bfd_boolean
3494_bfd_elf_default_relocs_compatible (const bfd_target *input,
3495 const bfd_target *output)
3496{
3497 return input == output;
3498}
3499
3500/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3501 This version is used when different targets for the same architecture
3502 are virtually identical. */
3503
3504bfd_boolean
3505_bfd_elf_relocs_compatible (const bfd_target *input,
3506 const bfd_target *output)
3507{
3508 const struct elf_backend_data *obed, *ibed;
3509
3510 if (input == output)
3511 return TRUE;
3512
3513 ibed = xvec_get_elf_backend_data (input);
3514 obed = xvec_get_elf_backend_data (output);
3515
3516 if (ibed->arch != obed->arch)
3517 return FALSE;
3518
3519 /* If both backends are using this function, deem them compatible. */
3520 return ibed->relocs_compatible == obed->relocs_compatible;
3521}
3522
e5034e59
AM
3523/* Make a special call to the linker "notice" function to tell it that
3524 we are about to handle an as-needed lib, or have finished
1b786873 3525 processing the lib. */
e5034e59
AM
3526
3527bfd_boolean
3528_bfd_elf_notice_as_needed (bfd *ibfd,
3529 struct bfd_link_info *info,
3530 enum notice_asneeded_action act)
3531{
46135103 3532 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3533}
3534
d9689752
L
3535/* Check relocations an ELF object file. */
3536
3537bfd_boolean
3538_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3539{
3540 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3541 struct elf_link_hash_table *htab = elf_hash_table (info);
3542
3543 /* If this object is the same format as the output object, and it is
3544 not a shared library, then let the backend look through the
3545 relocs.
3546
3547 This is required to build global offset table entries and to
3548 arrange for dynamic relocs. It is not required for the
3549 particular common case of linking non PIC code, even when linking
3550 against shared libraries, but unfortunately there is no way of
3551 knowing whether an object file has been compiled PIC or not.
3552 Looking through the relocs is not particularly time consuming.
3553 The problem is that we must either (1) keep the relocs in memory,
3554 which causes the linker to require additional runtime memory or
3555 (2) read the relocs twice from the input file, which wastes time.
3556 This would be a good case for using mmap.
3557
3558 I have no idea how to handle linking PIC code into a file of a
3559 different format. It probably can't be done. */
3560 if ((abfd->flags & DYNAMIC) == 0
3561 && is_elf_hash_table (htab)
3562 && bed->check_relocs != NULL
3563 && elf_object_id (abfd) == elf_hash_table_id (htab)
3564 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3565 {
3566 asection *o;
3567
3568 for (o = abfd->sections; o != NULL; o = o->next)
3569 {
3570 Elf_Internal_Rela *internal_relocs;
3571 bfd_boolean ok;
3572
5ce03cea 3573 /* Don't check relocations in excluded sections. */
d9689752 3574 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3575 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3576 || o->reloc_count == 0
3577 || ((info->strip == strip_all || info->strip == strip_debugger)
3578 && (o->flags & SEC_DEBUGGING) != 0)
3579 || bfd_is_abs_section (o->output_section))
3580 continue;
3581
3582 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3583 info->keep_memory);
3584 if (internal_relocs == NULL)
3585 return FALSE;
3586
3587 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3588
3589 if (elf_section_data (o)->relocs != internal_relocs)
3590 free (internal_relocs);
3591
3592 if (! ok)
3593 return FALSE;
3594 }
3595 }
3596
3597 return TRUE;
3598}
3599
4ad4eba5
AM
3600/* Add symbols from an ELF object file to the linker hash table. */
3601
3602static bfd_boolean
3603elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3604{
a0c402a5 3605 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3606 Elf_Internal_Shdr *hdr;
ef53be89
AM
3607 size_t symcount;
3608 size_t extsymcount;
3609 size_t extsymoff;
4ad4eba5
AM
3610 struct elf_link_hash_entry **sym_hash;
3611 bfd_boolean dynamic;
3612 Elf_External_Versym *extversym = NULL;
3613 Elf_External_Versym *ever;
3614 struct elf_link_hash_entry *weaks;
3615 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3616 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3617 Elf_Internal_Sym *isymbuf = NULL;
3618 Elf_Internal_Sym *isym;
3619 Elf_Internal_Sym *isymend;
3620 const struct elf_backend_data *bed;
3621 bfd_boolean add_needed;
66eb6687 3622 struct elf_link_hash_table *htab;
4ad4eba5 3623 bfd_size_type amt;
66eb6687 3624 void *alloc_mark = NULL;
4f87808c
AM
3625 struct bfd_hash_entry **old_table = NULL;
3626 unsigned int old_size = 0;
3627 unsigned int old_count = 0;
66eb6687 3628 void *old_tab = NULL;
66eb6687
AM
3629 void *old_ent;
3630 struct bfd_link_hash_entry *old_undefs = NULL;
3631 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3632 void *old_strtab = NULL;
66eb6687 3633 size_t tabsize = 0;
db6a5d5f 3634 asection *s;
29a9f53e 3635 bfd_boolean just_syms;
4ad4eba5 3636
66eb6687 3637 htab = elf_hash_table (info);
4ad4eba5 3638 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3639
3640 if ((abfd->flags & DYNAMIC) == 0)
3641 dynamic = FALSE;
3642 else
3643 {
3644 dynamic = TRUE;
3645
3646 /* You can't use -r against a dynamic object. Also, there's no
3647 hope of using a dynamic object which does not exactly match
3648 the format of the output file. */
0e1862bb 3649 if (bfd_link_relocatable (info)
66eb6687 3650 || !is_elf_hash_table (htab)
f13a99db 3651 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3652 {
0e1862bb 3653 if (bfd_link_relocatable (info))
9a0789ec
NC
3654 bfd_set_error (bfd_error_invalid_operation);
3655 else
3656 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3657 goto error_return;
3658 }
3659 }
3660
a0c402a5
L
3661 ehdr = elf_elfheader (abfd);
3662 if (info->warn_alternate_em
3663 && bed->elf_machine_code != ehdr->e_machine
3664 && ((bed->elf_machine_alt1 != 0
3665 && ehdr->e_machine == bed->elf_machine_alt1)
3666 || (bed->elf_machine_alt2 != 0
3667 && ehdr->e_machine == bed->elf_machine_alt2)))
3668 info->callbacks->einfo
3669 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3670 ehdr->e_machine, abfd, bed->elf_machine_code);
3671
4ad4eba5
AM
3672 /* As a GNU extension, any input sections which are named
3673 .gnu.warning.SYMBOL are treated as warning symbols for the given
3674 symbol. This differs from .gnu.warning sections, which generate
3675 warnings when they are included in an output file. */
dd98f8d2 3676 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3677 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3678 {
db6a5d5f 3679 const char *name;
4ad4eba5 3680
db6a5d5f
AM
3681 name = bfd_get_section_name (abfd, s);
3682 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3683 {
db6a5d5f
AM
3684 char *msg;
3685 bfd_size_type sz;
3686
3687 name += sizeof ".gnu.warning." - 1;
3688
3689 /* If this is a shared object, then look up the symbol
3690 in the hash table. If it is there, and it is already
3691 been defined, then we will not be using the entry
3692 from this shared object, so we don't need to warn.
3693 FIXME: If we see the definition in a regular object
3694 later on, we will warn, but we shouldn't. The only
3695 fix is to keep track of what warnings we are supposed
3696 to emit, and then handle them all at the end of the
3697 link. */
3698 if (dynamic)
4ad4eba5 3699 {
db6a5d5f
AM
3700 struct elf_link_hash_entry *h;
3701
3702 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3703
3704 /* FIXME: What about bfd_link_hash_common? */
3705 if (h != NULL
3706 && (h->root.type == bfd_link_hash_defined
3707 || h->root.type == bfd_link_hash_defweak))
3708 continue;
3709 }
4ad4eba5 3710
db6a5d5f
AM
3711 sz = s->size;
3712 msg = (char *) bfd_alloc (abfd, sz + 1);
3713 if (msg == NULL)
3714 goto error_return;
4ad4eba5 3715
db6a5d5f
AM
3716 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3717 goto error_return;
4ad4eba5 3718
db6a5d5f 3719 msg[sz] = '\0';
4ad4eba5 3720
db6a5d5f
AM
3721 if (! (_bfd_generic_link_add_one_symbol
3722 (info, abfd, name, BSF_WARNING, s, 0, msg,
3723 FALSE, bed->collect, NULL)))
3724 goto error_return;
4ad4eba5 3725
0e1862bb 3726 if (bfd_link_executable (info))
db6a5d5f
AM
3727 {
3728 /* Clobber the section size so that the warning does
3729 not get copied into the output file. */
3730 s->size = 0;
11d2f718 3731
db6a5d5f
AM
3732 /* Also set SEC_EXCLUDE, so that symbols defined in
3733 the warning section don't get copied to the output. */
3734 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3735 }
3736 }
3737 }
3738
29a9f53e
L
3739 just_syms = ((s = abfd->sections) != NULL
3740 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3741
4ad4eba5
AM
3742 add_needed = TRUE;
3743 if (! dynamic)
3744 {
3745 /* If we are creating a shared library, create all the dynamic
3746 sections immediately. We need to attach them to something,
3747 so we attach them to this BFD, provided it is the right
bf89386a
L
3748 format and is not from ld --just-symbols. Always create the
3749 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3750 are no input BFD's of the same format as the output, we can't
3751 make a shared library. */
3752 if (!just_syms
bf89386a 3753 && (bfd_link_pic (info)
9c1d7a08
L
3754 || (!bfd_link_relocatable (info)
3755 && (info->export_dynamic || info->dynamic)))
66eb6687 3756 && is_elf_hash_table (htab)
f13a99db 3757 && info->output_bfd->xvec == abfd->xvec
66eb6687 3758 && !htab->dynamic_sections_created)
4ad4eba5
AM
3759 {
3760 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3761 goto error_return;
3762 }
3763 }
66eb6687 3764 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3765 goto error_return;
3766 else
3767 {
4ad4eba5 3768 const char *soname = NULL;
7ee314fa 3769 char *audit = NULL;
4ad4eba5
AM
3770 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3771 int ret;
3772
3773 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3774 ld shouldn't allow it. */
29a9f53e 3775 if (just_syms)
92fd189d 3776 abort ();
4ad4eba5
AM
3777
3778 /* If this dynamic lib was specified on the command line with
3779 --as-needed in effect, then we don't want to add a DT_NEEDED
3780 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3781 in by another lib's DT_NEEDED. When --no-add-needed is used
3782 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3783 any dynamic library in DT_NEEDED tags in the dynamic lib at
3784 all. */
3785 add_needed = (elf_dyn_lib_class (abfd)
3786 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3787 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3788
3789 s = bfd_get_section_by_name (abfd, ".dynamic");
3790 if (s != NULL)
3791 {
3792 bfd_byte *dynbuf;
3793 bfd_byte *extdyn;
cb33740c 3794 unsigned int elfsec;
4ad4eba5
AM
3795 unsigned long shlink;
3796
eea6121a 3797 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3798 {
3799error_free_dyn:
3800 free (dynbuf);
3801 goto error_return;
3802 }
4ad4eba5
AM
3803
3804 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3805 if (elfsec == SHN_BAD)
4ad4eba5
AM
3806 goto error_free_dyn;
3807 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3808
3809 for (extdyn = dynbuf;
eea6121a 3810 extdyn < dynbuf + s->size;
4ad4eba5
AM
3811 extdyn += bed->s->sizeof_dyn)
3812 {
3813 Elf_Internal_Dyn dyn;
3814
3815 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3816 if (dyn.d_tag == DT_SONAME)
3817 {
3818 unsigned int tagv = dyn.d_un.d_val;
3819 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3820 if (soname == NULL)
3821 goto error_free_dyn;
3822 }
3823 if (dyn.d_tag == DT_NEEDED)
3824 {
3825 struct bfd_link_needed_list *n, **pn;
3826 char *fnm, *anm;
3827 unsigned int tagv = dyn.d_un.d_val;
3828
3829 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3830 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3831 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3832 if (n == NULL || fnm == NULL)
3833 goto error_free_dyn;
3834 amt = strlen (fnm) + 1;
a50b1753 3835 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3836 if (anm == NULL)
3837 goto error_free_dyn;
3838 memcpy (anm, fnm, amt);
3839 n->name = anm;
3840 n->by = abfd;
3841 n->next = NULL;
66eb6687 3842 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3843 ;
3844 *pn = n;
3845 }
3846 if (dyn.d_tag == DT_RUNPATH)
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;
3865 for (pn = & runpath;
3866 *pn != NULL;
3867 pn = &(*pn)->next)
3868 ;
3869 *pn = n;
3870 }
3871 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3872 if (!runpath && dyn.d_tag == DT_RPATH)
3873 {
3874 struct bfd_link_needed_list *n, **pn;
3875 char *fnm, *anm;
3876 unsigned int tagv = dyn.d_un.d_val;
3877
3878 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3879 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3880 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3881 if (n == NULL || fnm == NULL)
3882 goto error_free_dyn;
3883 amt = strlen (fnm) + 1;
a50b1753 3884 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3885 if (anm == NULL)
f8703194 3886 goto error_free_dyn;
4ad4eba5
AM
3887 memcpy (anm, fnm, amt);
3888 n->name = anm;
3889 n->by = abfd;
3890 n->next = NULL;
3891 for (pn = & rpath;
3892 *pn != NULL;
3893 pn = &(*pn)->next)
3894 ;
3895 *pn = n;
3896 }
7ee314fa
AM
3897 if (dyn.d_tag == DT_AUDIT)
3898 {
3899 unsigned int tagv = dyn.d_un.d_val;
3900 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3901 }
4ad4eba5
AM
3902 }
3903
3904 free (dynbuf);
3905 }
3906
3907 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3908 frees all more recently bfd_alloc'd blocks as well. */
3909 if (runpath)
3910 rpath = runpath;
3911
3912 if (rpath)
3913 {
3914 struct bfd_link_needed_list **pn;
66eb6687 3915 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3916 ;
3917 *pn = rpath;
3918 }
3919
3920 /* We do not want to include any of the sections in a dynamic
3921 object in the output file. We hack by simply clobbering the
3922 list of sections in the BFD. This could be handled more
3923 cleanly by, say, a new section flag; the existing
3924 SEC_NEVER_LOAD flag is not the one we want, because that one
3925 still implies that the section takes up space in the output
3926 file. */
3927 bfd_section_list_clear (abfd);
3928
4ad4eba5
AM
3929 /* Find the name to use in a DT_NEEDED entry that refers to this
3930 object. If the object has a DT_SONAME entry, we use it.
3931 Otherwise, if the generic linker stuck something in
3932 elf_dt_name, we use that. Otherwise, we just use the file
3933 name. */
3934 if (soname == NULL || *soname == '\0')
3935 {
3936 soname = elf_dt_name (abfd);
3937 if (soname == NULL || *soname == '\0')
3938 soname = bfd_get_filename (abfd);
3939 }
3940
3941 /* Save the SONAME because sometimes the linker emulation code
3942 will need to know it. */
3943 elf_dt_name (abfd) = soname;
3944
7e9f0867 3945 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3946 if (ret < 0)
3947 goto error_return;
3948
3949 /* If we have already included this dynamic object in the
3950 link, just ignore it. There is no reason to include a
3951 particular dynamic object more than once. */
3952 if (ret > 0)
3953 return TRUE;
7ee314fa
AM
3954
3955 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 3956 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3957 }
3958
3959 /* If this is a dynamic object, we always link against the .dynsym
3960 symbol table, not the .symtab symbol table. The dynamic linker
3961 will only see the .dynsym symbol table, so there is no reason to
3962 look at .symtab for a dynamic object. */
3963
3964 if (! dynamic || elf_dynsymtab (abfd) == 0)
3965 hdr = &elf_tdata (abfd)->symtab_hdr;
3966 else
3967 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3968
3969 symcount = hdr->sh_size / bed->s->sizeof_sym;
3970
3971 /* The sh_info field of the symtab header tells us where the
3972 external symbols start. We don't care about the local symbols at
3973 this point. */
3974 if (elf_bad_symtab (abfd))
3975 {
3976 extsymcount = symcount;
3977 extsymoff = 0;
3978 }
3979 else
3980 {
3981 extsymcount = symcount - hdr->sh_info;
3982 extsymoff = hdr->sh_info;
3983 }
3984
f45794cb 3985 sym_hash = elf_sym_hashes (abfd);
012b2306 3986 if (extsymcount != 0)
4ad4eba5
AM
3987 {
3988 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3989 NULL, NULL, NULL);
3990 if (isymbuf == NULL)
3991 goto error_return;
3992
4ad4eba5 3993 if (sym_hash == NULL)
012b2306
AM
3994 {
3995 /* We store a pointer to the hash table entry for each
3996 external symbol. */
ef53be89
AM
3997 amt = extsymcount;
3998 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
3999 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4000 if (sym_hash == NULL)
4001 goto error_free_sym;
4002 elf_sym_hashes (abfd) = sym_hash;
4003 }
4ad4eba5
AM
4004 }
4005
4006 if (dynamic)
4007 {
4008 /* Read in any version definitions. */
fc0e6df6
PB
4009 if (!_bfd_elf_slurp_version_tables (abfd,
4010 info->default_imported_symver))
4ad4eba5
AM
4011 goto error_free_sym;
4012
4013 /* Read in the symbol versions, but don't bother to convert them
4014 to internal format. */
4015 if (elf_dynversym (abfd) != 0)
4016 {
4017 Elf_Internal_Shdr *versymhdr;
4018
4019 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4020 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4021 if (extversym == NULL)
4022 goto error_free_sym;
4023 amt = versymhdr->sh_size;
4024 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4025 || bfd_bread (extversym, amt, abfd) != amt)
4026 goto error_free_vers;
4027 }
4028 }
4029
66eb6687
AM
4030 /* If we are loading an as-needed shared lib, save the symbol table
4031 state before we start adding symbols. If the lib turns out
4032 to be unneeded, restore the state. */
4033 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4034 {
4035 unsigned int i;
4036 size_t entsize;
4037
4038 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4039 {
4040 struct bfd_hash_entry *p;
2de92251 4041 struct elf_link_hash_entry *h;
66eb6687
AM
4042
4043 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4044 {
4045 h = (struct elf_link_hash_entry *) p;
4046 entsize += htab->root.table.entsize;
4047 if (h->root.type == bfd_link_hash_warning)
4048 entsize += htab->root.table.entsize;
4049 }
66eb6687
AM
4050 }
4051
4052 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4053 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4054 if (old_tab == NULL)
4055 goto error_free_vers;
4056
4057 /* Remember the current objalloc pointer, so that all mem for
4058 symbols added can later be reclaimed. */
4059 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4060 if (alloc_mark == NULL)
4061 goto error_free_vers;
4062
5061a885
AM
4063 /* Make a special call to the linker "notice" function to
4064 tell it that we are about to handle an as-needed lib. */
e5034e59 4065 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4066 goto error_free_vers;
5061a885 4067
f45794cb
AM
4068 /* Clone the symbol table. Remember some pointers into the
4069 symbol table, and dynamic symbol count. */
4070 old_ent = (char *) old_tab + tabsize;
66eb6687 4071 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4072 old_undefs = htab->root.undefs;
4073 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4074 old_table = htab->root.table.table;
4075 old_size = htab->root.table.size;
4076 old_count = htab->root.table.count;
5b677558
AM
4077 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4078 if (old_strtab == NULL)
4079 goto error_free_vers;
66eb6687
AM
4080
4081 for (i = 0; i < htab->root.table.size; i++)
4082 {
4083 struct bfd_hash_entry *p;
2de92251 4084 struct elf_link_hash_entry *h;
66eb6687
AM
4085
4086 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4087 {
4088 memcpy (old_ent, p, htab->root.table.entsize);
4089 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4090 h = (struct elf_link_hash_entry *) p;
4091 if (h->root.type == bfd_link_hash_warning)
4092 {
4093 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4094 old_ent = (char *) old_ent + htab->root.table.entsize;
4095 }
66eb6687
AM
4096 }
4097 }
4098 }
4ad4eba5 4099
66eb6687 4100 weaks = NULL;
4ad4eba5
AM
4101 ever = extversym != NULL ? extversym + extsymoff : NULL;
4102 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4103 isym < isymend;
4104 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4105 {
4106 int bind;
4107 bfd_vma value;
af44c138 4108 asection *sec, *new_sec;
4ad4eba5
AM
4109 flagword flags;
4110 const char *name;
4111 struct elf_link_hash_entry *h;
90c984fc 4112 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4113 bfd_boolean definition;
4114 bfd_boolean size_change_ok;
4115 bfd_boolean type_change_ok;
4116 bfd_boolean new_weakdef;
37a9e49a
L
4117 bfd_boolean new_weak;
4118 bfd_boolean old_weak;
4ad4eba5 4119 bfd_boolean override;
a4d8e49b 4120 bfd_boolean common;
97196564 4121 bfd_boolean discarded;
4ad4eba5
AM
4122 unsigned int old_alignment;
4123 bfd *old_bfd;
6e33951e 4124 bfd_boolean matched;
4ad4eba5
AM
4125
4126 override = FALSE;
4127
4128 flags = BSF_NO_FLAGS;
4129 sec = NULL;
4130 value = isym->st_value;
a4d8e49b 4131 common = bed->common_definition (isym);
97196564 4132 discarded = FALSE;
4ad4eba5
AM
4133
4134 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4135 switch (bind)
4ad4eba5 4136 {
3e7a7d11 4137 case STB_LOCAL:
4ad4eba5
AM
4138 /* This should be impossible, since ELF requires that all
4139 global symbols follow all local symbols, and that sh_info
4140 point to the first global symbol. Unfortunately, Irix 5
4141 screws this up. */
4142 continue;
3e7a7d11
NC
4143
4144 case STB_GLOBAL:
a4d8e49b 4145 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4146 flags = BSF_GLOBAL;
3e7a7d11
NC
4147 break;
4148
4149 case STB_WEAK:
4150 flags = BSF_WEAK;
4151 break;
4152
4153 case STB_GNU_UNIQUE:
4154 flags = BSF_GNU_UNIQUE;
4155 break;
4156
4157 default:
4ad4eba5 4158 /* Leave it up to the processor backend. */
3e7a7d11 4159 break;
4ad4eba5
AM
4160 }
4161
4162 if (isym->st_shndx == SHN_UNDEF)
4163 sec = bfd_und_section_ptr;
cb33740c
AM
4164 else if (isym->st_shndx == SHN_ABS)
4165 sec = bfd_abs_section_ptr;
4166 else if (isym->st_shndx == SHN_COMMON)
4167 {
4168 sec = bfd_com_section_ptr;
4169 /* What ELF calls the size we call the value. What ELF
4170 calls the value we call the alignment. */
4171 value = isym->st_size;
4172 }
4173 else
4ad4eba5
AM
4174 {
4175 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4176 if (sec == NULL)
4177 sec = bfd_abs_section_ptr;
dbaa2011 4178 else if (discarded_section (sec))
529fcb95 4179 {
e5d08002
L
4180 /* Symbols from discarded section are undefined. We keep
4181 its visibility. */
529fcb95 4182 sec = bfd_und_section_ptr;
97196564 4183 discarded = TRUE;
529fcb95
PB
4184 isym->st_shndx = SHN_UNDEF;
4185 }
4ad4eba5
AM
4186 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4187 value -= sec->vma;
4188 }
4ad4eba5
AM
4189
4190 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4191 isym->st_name);
4192 if (name == NULL)
4193 goto error_free_vers;
4194
4195 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4196 && (abfd->flags & BFD_PLUGIN) != 0)
4197 {
4198 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4199
4200 if (xc == NULL)
4201 {
4202 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4203 | SEC_EXCLUDE);
4204 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4205 if (xc == NULL)
4206 goto error_free_vers;
4207 }
4208 sec = xc;
4209 }
4210 else if (isym->st_shndx == SHN_COMMON
4211 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4212 && !bfd_link_relocatable (info))
4ad4eba5
AM
4213 {
4214 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4215
4216 if (tcomm == NULL)
4217 {
02d00247
AM
4218 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4219 | SEC_LINKER_CREATED);
4220 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4221 if (tcomm == NULL)
4ad4eba5
AM
4222 goto error_free_vers;
4223 }
4224 sec = tcomm;
4225 }
66eb6687 4226 else if (bed->elf_add_symbol_hook)
4ad4eba5 4227 {
66eb6687
AM
4228 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4229 &sec, &value))
4ad4eba5
AM
4230 goto error_free_vers;
4231
4232 /* The hook function sets the name to NULL if this symbol
4233 should be skipped for some reason. */
4234 if (name == NULL)
4235 continue;
4236 }
4237
4238 /* Sanity check that all possibilities were handled. */
4239 if (sec == NULL)
4240 {
4241 bfd_set_error (bfd_error_bad_value);
4242 goto error_free_vers;
4243 }
4244
191c0c42
AM
4245 /* Silently discard TLS symbols from --just-syms. There's
4246 no way to combine a static TLS block with a new TLS block
4247 for this executable. */
4248 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4249 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4250 continue;
4251
4ad4eba5
AM
4252 if (bfd_is_und_section (sec)
4253 || bfd_is_com_section (sec))
4254 definition = FALSE;
4255 else
4256 definition = TRUE;
4257
4258 size_change_ok = FALSE;
66eb6687 4259 type_change_ok = bed->type_change_ok;
37a9e49a 4260 old_weak = FALSE;
6e33951e 4261 matched = FALSE;
4ad4eba5
AM
4262 old_alignment = 0;
4263 old_bfd = NULL;
af44c138 4264 new_sec = sec;
4ad4eba5 4265
66eb6687 4266 if (is_elf_hash_table (htab))
4ad4eba5
AM
4267 {
4268 Elf_Internal_Versym iver;
4269 unsigned int vernum = 0;
4270 bfd_boolean skip;
4271
fc0e6df6 4272 if (ever == NULL)
4ad4eba5 4273 {
fc0e6df6
PB
4274 if (info->default_imported_symver)
4275 /* Use the default symbol version created earlier. */
4276 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4277 else
4278 iver.vs_vers = 0;
4279 }
4280 else
4281 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4282
4283 vernum = iver.vs_vers & VERSYM_VERSION;
4284
4285 /* If this is a hidden symbol, or if it is not version
4286 1, we append the version name to the symbol name.
cc86ff91
EB
4287 However, we do not modify a non-hidden absolute symbol
4288 if it is not a function, because it might be the version
4289 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4290 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4291 || (vernum > 1
4292 && (!bfd_is_abs_section (sec)
4293 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4294 {
4295 const char *verstr;
4296 size_t namelen, verlen, newlen;
4297 char *newname, *p;
4298
4299 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4300 {
fc0e6df6
PB
4301 if (vernum > elf_tdata (abfd)->cverdefs)
4302 verstr = NULL;
4303 else if (vernum > 1)
4304 verstr =
4305 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4306 else
4307 verstr = "";
4ad4eba5 4308
fc0e6df6 4309 if (verstr == NULL)
4ad4eba5 4310 {
fc0e6df6
PB
4311 (*_bfd_error_handler)
4312 (_("%B: %s: invalid version %u (max %d)"),
4313 abfd, name, vernum,
4314 elf_tdata (abfd)->cverdefs);
4315 bfd_set_error (bfd_error_bad_value);
4316 goto error_free_vers;
4ad4eba5 4317 }
fc0e6df6
PB
4318 }
4319 else
4320 {
4321 /* We cannot simply test for the number of
4322 entries in the VERNEED section since the
4323 numbers for the needed versions do not start
4324 at 0. */
4325 Elf_Internal_Verneed *t;
4326
4327 verstr = NULL;
4328 for (t = elf_tdata (abfd)->verref;
4329 t != NULL;
4330 t = t->vn_nextref)
4ad4eba5 4331 {
fc0e6df6 4332 Elf_Internal_Vernaux *a;
4ad4eba5 4333
fc0e6df6
PB
4334 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4335 {
4336 if (a->vna_other == vernum)
4ad4eba5 4337 {
fc0e6df6
PB
4338 verstr = a->vna_nodename;
4339 break;
4ad4eba5 4340 }
4ad4eba5 4341 }
fc0e6df6
PB
4342 if (a != NULL)
4343 break;
4344 }
4345 if (verstr == NULL)
4346 {
4347 (*_bfd_error_handler)
4348 (_("%B: %s: invalid needed version %d"),
4349 abfd, name, vernum);
4350 bfd_set_error (bfd_error_bad_value);
4351 goto error_free_vers;
4ad4eba5 4352 }
4ad4eba5 4353 }
fc0e6df6
PB
4354
4355 namelen = strlen (name);
4356 verlen = strlen (verstr);
4357 newlen = namelen + verlen + 2;
4358 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4359 && isym->st_shndx != SHN_UNDEF)
4360 ++newlen;
4361
a50b1753 4362 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4363 if (newname == NULL)
4364 goto error_free_vers;
4365 memcpy (newname, name, namelen);
4366 p = newname + namelen;
4367 *p++ = ELF_VER_CHR;
4368 /* If this is a defined non-hidden version symbol,
4369 we add another @ to the name. This indicates the
4370 default version of the symbol. */
4371 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4372 && isym->st_shndx != SHN_UNDEF)
4373 *p++ = ELF_VER_CHR;
4374 memcpy (p, verstr, verlen + 1);
4375
4376 name = newname;
4ad4eba5
AM
4377 }
4378
cd3416da
AM
4379 /* If this symbol has default visibility and the user has
4380 requested we not re-export it, then mark it as hidden. */
a0d49154 4381 if (!bfd_is_und_section (sec)
cd3416da 4382 && !dynamic
ce875075 4383 && abfd->no_export
cd3416da
AM
4384 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4385 isym->st_other = (STV_HIDDEN
4386 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4387
4f3fedcf
AM
4388 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4389 sym_hash, &old_bfd, &old_weak,
4390 &old_alignment, &skip, &override,
6e33951e
L
4391 &type_change_ok, &size_change_ok,
4392 &matched))
4ad4eba5
AM
4393 goto error_free_vers;
4394
4395 if (skip)
4396 continue;
4397
6e33951e
L
4398 /* Override a definition only if the new symbol matches the
4399 existing one. */
4400 if (override && matched)
4ad4eba5
AM
4401 definition = FALSE;
4402
4403 h = *sym_hash;
4404 while (h->root.type == bfd_link_hash_indirect
4405 || h->root.type == bfd_link_hash_warning)
4406 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4407
4ad4eba5 4408 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4409 && vernum > 1
4410 && definition)
4411 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4412 }
4413
4414 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4415 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4416 (struct bfd_link_hash_entry **) sym_hash)))
4417 goto error_free_vers;
4418
a43942db
MR
4419 if ((flags & BSF_GNU_UNIQUE)
4420 && (abfd->flags & DYNAMIC) == 0
4421 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4422 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4423
4ad4eba5 4424 h = *sym_hash;
90c984fc
L
4425 /* We need to make sure that indirect symbol dynamic flags are
4426 updated. */
4427 hi = h;
4ad4eba5
AM
4428 while (h->root.type == bfd_link_hash_indirect
4429 || h->root.type == bfd_link_hash_warning)
4430 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4431
97196564
L
4432 /* Setting the index to -3 tells elf_link_output_extsym that
4433 this symbol is defined in a discarded section. */
4434 if (discarded)
4435 h->indx = -3;
4436
4ad4eba5
AM
4437 *sym_hash = h;
4438
37a9e49a 4439 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4440 new_weakdef = FALSE;
4441 if (dynamic
4442 && definition
37a9e49a 4443 && new_weak
fcb93ecf 4444 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4445 && is_elf_hash_table (htab)
f6e332e6 4446 && h->u.weakdef == NULL)
4ad4eba5
AM
4447 {
4448 /* Keep a list of all weak defined non function symbols from
4449 a dynamic object, using the weakdef field. Later in this
4450 function we will set the weakdef field to the correct
4451 value. We only put non-function symbols from dynamic
4452 objects on this list, because that happens to be the only
4453 time we need to know the normal symbol corresponding to a
4454 weak symbol, and the information is time consuming to
4455 figure out. If the weakdef field is not already NULL,
4456 then this symbol was already defined by some previous
4457 dynamic object, and we will be using that previous
4458 definition anyhow. */
4459
f6e332e6 4460 h->u.weakdef = weaks;
4ad4eba5
AM
4461 weaks = h;
4462 new_weakdef = TRUE;
4463 }
4464
4465 /* Set the alignment of a common symbol. */
a4d8e49b 4466 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4467 && h->root.type == bfd_link_hash_common)
4468 {
4469 unsigned int align;
4470
a4d8e49b 4471 if (common)
af44c138
L
4472 align = bfd_log2 (isym->st_value);
4473 else
4474 {
4475 /* The new symbol is a common symbol in a shared object.
4476 We need to get the alignment from the section. */
4477 align = new_sec->alignment_power;
4478 }
595213d4 4479 if (align > old_alignment)
4ad4eba5
AM
4480 h->root.u.c.p->alignment_power = align;
4481 else
4482 h->root.u.c.p->alignment_power = old_alignment;
4483 }
4484
66eb6687 4485 if (is_elf_hash_table (htab))
4ad4eba5 4486 {
4f3fedcf
AM
4487 /* Set a flag in the hash table entry indicating the type of
4488 reference or definition we just found. A dynamic symbol
4489 is one which is referenced or defined by both a regular
4490 object and a shared object. */
4491 bfd_boolean dynsym = FALSE;
4492
4493 /* Plugin symbols aren't normal. Don't set def_regular or
4494 ref_regular for them, or make them dynamic. */
4495 if ((abfd->flags & BFD_PLUGIN) != 0)
4496 ;
4497 else if (! dynamic)
4498 {
4499 if (! definition)
4500 {
4501 h->ref_regular = 1;
4502 if (bind != STB_WEAK)
4503 h->ref_regular_nonweak = 1;
4504 }
4505 else
4506 {
4507 h->def_regular = 1;
4508 if (h->def_dynamic)
4509 {
4510 h->def_dynamic = 0;
4511 h->ref_dynamic = 1;
4512 }
4513 }
4514
4515 /* If the indirect symbol has been forced local, don't
4516 make the real symbol dynamic. */
4517 if ((h == hi || !hi->forced_local)
0e1862bb 4518 && (bfd_link_dll (info)
4f3fedcf
AM
4519 || h->def_dynamic
4520 || h->ref_dynamic))
4521 dynsym = TRUE;
4522 }
4523 else
4524 {
4525 if (! definition)
4526 {
4527 h->ref_dynamic = 1;
4528 hi->ref_dynamic = 1;
4529 }
4530 else
4531 {
4532 h->def_dynamic = 1;
4533 hi->def_dynamic = 1;
4534 }
4535
4536 /* If the indirect symbol has been forced local, don't
4537 make the real symbol dynamic. */
4538 if ((h == hi || !hi->forced_local)
4539 && (h->def_regular
4540 || h->ref_regular
4541 || (h->u.weakdef != NULL
4542 && ! new_weakdef
4543 && h->u.weakdef->dynindx != -1)))
4544 dynsym = TRUE;
4545 }
4546
4547 /* Check to see if we need to add an indirect symbol for
4548 the default name. */
4549 if (definition
4550 || (!override && h->root.type == bfd_link_hash_common))
4551 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4552 sec, value, &old_bfd, &dynsym))
4553 goto error_free_vers;
4ad4eba5
AM
4554
4555 /* Check the alignment when a common symbol is involved. This
4556 can change when a common symbol is overridden by a normal
4557 definition or a common symbol is ignored due to the old
4558 normal definition. We need to make sure the maximum
4559 alignment is maintained. */
a4d8e49b 4560 if ((old_alignment || common)
4ad4eba5
AM
4561 && h->root.type != bfd_link_hash_common)
4562 {
4563 unsigned int common_align;
4564 unsigned int normal_align;
4565 unsigned int symbol_align;
4566 bfd *normal_bfd;
4567 bfd *common_bfd;
4568
3a81e825
AM
4569 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4570 || h->root.type == bfd_link_hash_defweak);
4571
4ad4eba5
AM
4572 symbol_align = ffs (h->root.u.def.value) - 1;
4573 if (h->root.u.def.section->owner != NULL
0616a280
AM
4574 && (h->root.u.def.section->owner->flags
4575 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4576 {
4577 normal_align = h->root.u.def.section->alignment_power;
4578 if (normal_align > symbol_align)
4579 normal_align = symbol_align;
4580 }
4581 else
4582 normal_align = symbol_align;
4583
4584 if (old_alignment)
4585 {
4586 common_align = old_alignment;
4587 common_bfd = old_bfd;
4588 normal_bfd = abfd;
4589 }
4590 else
4591 {
4592 common_align = bfd_log2 (isym->st_value);
4593 common_bfd = abfd;
4594 normal_bfd = old_bfd;
4595 }
4596
4597 if (normal_align < common_align)
d07676f8
NC
4598 {
4599 /* PR binutils/2735 */
4600 if (normal_bfd == NULL)
4601 (*_bfd_error_handler)
4f3fedcf
AM
4602 (_("Warning: alignment %u of common symbol `%s' in %B is"
4603 " greater than the alignment (%u) of its section %A"),
d07676f8
NC
4604 common_bfd, h->root.u.def.section,
4605 1 << common_align, name, 1 << normal_align);
4606 else
4607 (*_bfd_error_handler)
4608 (_("Warning: alignment %u of symbol `%s' in %B"
4609 " is smaller than %u in %B"),
4610 normal_bfd, common_bfd,
4611 1 << normal_align, name, 1 << common_align);
4612 }
4ad4eba5
AM
4613 }
4614
83ad0046 4615 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4616 if (isym->st_size != 0
4617 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4618 && (definition || h->size == 0))
4619 {
83ad0046
L
4620 if (h->size != 0
4621 && h->size != isym->st_size
4622 && ! size_change_ok)
4ad4eba5 4623 (*_bfd_error_handler)
d003868e
AM
4624 (_("Warning: size of symbol `%s' changed"
4625 " from %lu in %B to %lu in %B"),
4626 old_bfd, abfd,
4ad4eba5 4627 name, (unsigned long) h->size,
d003868e 4628 (unsigned long) isym->st_size);
4ad4eba5
AM
4629
4630 h->size = isym->st_size;
4631 }
4632
4633 /* If this is a common symbol, then we always want H->SIZE
4634 to be the size of the common symbol. The code just above
4635 won't fix the size if a common symbol becomes larger. We
4636 don't warn about a size change here, because that is
4f3fedcf 4637 covered by --warn-common. Allow changes between different
fcb93ecf 4638 function types. */
4ad4eba5
AM
4639 if (h->root.type == bfd_link_hash_common)
4640 h->size = h->root.u.c.size;
4641
4642 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4643 && ((definition && !new_weak)
4644 || (old_weak && h->root.type == bfd_link_hash_common)
4645 || h->type == STT_NOTYPE))
4ad4eba5 4646 {
2955ec4c
L
4647 unsigned int type = ELF_ST_TYPE (isym->st_info);
4648
4649 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4650 symbol. */
4651 if (type == STT_GNU_IFUNC
4652 && (abfd->flags & DYNAMIC) != 0)
4653 type = STT_FUNC;
4ad4eba5 4654
2955ec4c
L
4655 if (h->type != type)
4656 {
4657 if (h->type != STT_NOTYPE && ! type_change_ok)
4658 (*_bfd_error_handler)
4659 (_("Warning: type of symbol `%s' changed"
4660 " from %d to %d in %B"),
4661 abfd, name, h->type, type);
4662
4663 h->type = type;
4664 }
4ad4eba5
AM
4665 }
4666
54ac0771 4667 /* Merge st_other field. */
b8417128 4668 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4669
c3df8c14 4670 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4671 if (definition
4672 && (sec->flags & SEC_DEBUGGING)
4673 && !bfd_link_relocatable (info))
c3df8c14
AM
4674 dynsym = FALSE;
4675
4f3fedcf
AM
4676 /* Nor should we make plugin symbols dynamic. */
4677 if ((abfd->flags & BFD_PLUGIN) != 0)
4678 dynsym = FALSE;
4679
35fc36a8 4680 if (definition)
35399224
L
4681 {
4682 h->target_internal = isym->st_target_internal;
4683 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4684 }
35fc36a8 4685
4ad4eba5
AM
4686 if (definition && !dynamic)
4687 {
4688 char *p = strchr (name, ELF_VER_CHR);
4689 if (p != NULL && p[1] != ELF_VER_CHR)
4690 {
4691 /* Queue non-default versions so that .symver x, x@FOO
4692 aliases can be checked. */
66eb6687 4693 if (!nondeflt_vers)
4ad4eba5 4694 {
66eb6687
AM
4695 amt = ((isymend - isym + 1)
4696 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4697 nondeflt_vers
4698 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4699 if (!nondeflt_vers)
4700 goto error_free_vers;
4ad4eba5 4701 }
66eb6687 4702 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4703 }
4704 }
4705
4706 if (dynsym && h->dynindx == -1)
4707 {
c152c796 4708 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4709 goto error_free_vers;
f6e332e6 4710 if (h->u.weakdef != NULL
4ad4eba5 4711 && ! new_weakdef
f6e332e6 4712 && h->u.weakdef->dynindx == -1)
4ad4eba5 4713 {
66eb6687 4714 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4715 goto error_free_vers;
4716 }
4717 }
1f599d0e 4718 else if (h->dynindx != -1)
4ad4eba5
AM
4719 /* If the symbol already has a dynamic index, but
4720 visibility says it should not be visible, turn it into
4721 a local symbol. */
4722 switch (ELF_ST_VISIBILITY (h->other))
4723 {
4724 case STV_INTERNAL:
4725 case STV_HIDDEN:
4726 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4727 dynsym = FALSE;
4728 break;
4729 }
4730
aef28989
L
4731 /* Don't add DT_NEEDED for references from the dummy bfd nor
4732 for unmatched symbol. */
4ad4eba5 4733 if (!add_needed
aef28989 4734 && matched
4ad4eba5 4735 && definition
010e5ae2 4736 && ((dynsym
ffa9430d 4737 && h->ref_regular_nonweak
4f3fedcf
AM
4738 && (old_bfd == NULL
4739 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4740 || (h->ref_dynamic_nonweak
010e5ae2 4741 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4742 && !on_needed_list (elf_dt_name (abfd),
4743 htab->needed, NULL))))
4ad4eba5
AM
4744 {
4745 int ret;
4746 const char *soname = elf_dt_name (abfd);
4747
16e4ecc0
AM
4748 info->callbacks->minfo ("%!", soname, old_bfd,
4749 h->root.root.string);
4750
4ad4eba5
AM
4751 /* A symbol from a library loaded via DT_NEEDED of some
4752 other library is referenced by a regular object.
e56f61be 4753 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4754 --no-add-needed is used and the reference was not
4755 a weak one. */
4f3fedcf 4756 if (old_bfd != NULL
b918acf9 4757 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4758 {
4759 (*_bfd_error_handler)
3cbc5de0 4760 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4761 old_bfd, name);
ff5ac77b 4762 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4763 goto error_free_vers;
4764 }
4765
a50b1753 4766 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4767 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4768
4ad4eba5 4769 add_needed = TRUE;
7e9f0867 4770 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4771 if (ret < 0)
4772 goto error_free_vers;
4773
4774 BFD_ASSERT (ret == 0);
4775 }
4776 }
4777 }
4778
66eb6687
AM
4779 if (extversym != NULL)
4780 {
4781 free (extversym);
4782 extversym = NULL;
4783 }
4784
4785 if (isymbuf != NULL)
4786 {
4787 free (isymbuf);
4788 isymbuf = NULL;
4789 }
4790
4791 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4792 {
4793 unsigned int i;
4794
4795 /* Restore the symbol table. */
f45794cb
AM
4796 old_ent = (char *) old_tab + tabsize;
4797 memset (elf_sym_hashes (abfd), 0,
4798 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4799 htab->root.table.table = old_table;
4800 htab->root.table.size = old_size;
4801 htab->root.table.count = old_count;
66eb6687 4802 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4803 htab->root.undefs = old_undefs;
4804 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
4805 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4806 free (old_strtab);
4807 old_strtab = NULL;
66eb6687
AM
4808 for (i = 0; i < htab->root.table.size; i++)
4809 {
4810 struct bfd_hash_entry *p;
4811 struct elf_link_hash_entry *h;
3e0882af
L
4812 bfd_size_type size;
4813 unsigned int alignment_power;
66eb6687
AM
4814
4815 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4816 {
4817 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4818 if (h->root.type == bfd_link_hash_warning)
4819 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4820
3e0882af
L
4821 /* Preserve the maximum alignment and size for common
4822 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4823 since it can still be loaded at run time by another
3e0882af
L
4824 dynamic lib. */
4825 if (h->root.type == bfd_link_hash_common)
4826 {
4827 size = h->root.u.c.size;
4828 alignment_power = h->root.u.c.p->alignment_power;
4829 }
4830 else
4831 {
4832 size = 0;
4833 alignment_power = 0;
4834 }
66eb6687
AM
4835 memcpy (p, old_ent, htab->root.table.entsize);
4836 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4837 h = (struct elf_link_hash_entry *) p;
4838 if (h->root.type == bfd_link_hash_warning)
4839 {
4840 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4841 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4842 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4843 }
a4542f1b 4844 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4845 {
4846 if (size > h->root.u.c.size)
4847 h->root.u.c.size = size;
4848 if (alignment_power > h->root.u.c.p->alignment_power)
4849 h->root.u.c.p->alignment_power = alignment_power;
4850 }
66eb6687
AM
4851 }
4852 }
4853
5061a885
AM
4854 /* Make a special call to the linker "notice" function to
4855 tell it that symbols added for crefs may need to be removed. */
e5034e59 4856 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4857 goto error_free_vers;
5061a885 4858
66eb6687
AM
4859 free (old_tab);
4860 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4861 alloc_mark);
4862 if (nondeflt_vers != NULL)
4863 free (nondeflt_vers);
4864 return TRUE;
4865 }
2de92251 4866
66eb6687
AM
4867 if (old_tab != NULL)
4868 {
e5034e59 4869 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4870 goto error_free_vers;
66eb6687
AM
4871 free (old_tab);
4872 old_tab = NULL;
4873 }
4874
c6e8a9a8
L
4875 /* Now that all the symbols from this input file are created, if
4876 not performing a relocatable link, handle .symver foo, foo@BAR
4877 such that any relocs against foo become foo@BAR. */
0e1862bb 4878 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 4879 {
ef53be89 4880 size_t cnt, symidx;
4ad4eba5
AM
4881
4882 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4883 {
4884 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4885 char *shortname, *p;
4886
4887 p = strchr (h->root.root.string, ELF_VER_CHR);
4888 if (p == NULL
4889 || (h->root.type != bfd_link_hash_defined
4890 && h->root.type != bfd_link_hash_defweak))
4891 continue;
4892
4893 amt = p - h->root.root.string;
a50b1753 4894 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4895 if (!shortname)
4896 goto error_free_vers;
4ad4eba5
AM
4897 memcpy (shortname, h->root.root.string, amt);
4898 shortname[amt] = '\0';
4899
4900 hi = (struct elf_link_hash_entry *)
66eb6687 4901 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4902 FALSE, FALSE, FALSE);
4903 if (hi != NULL
4904 && hi->root.type == h->root.type
4905 && hi->root.u.def.value == h->root.u.def.value
4906 && hi->root.u.def.section == h->root.u.def.section)
4907 {
4908 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4909 hi->root.type = bfd_link_hash_indirect;
4910 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4911 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4912 sym_hash = elf_sym_hashes (abfd);
4913 if (sym_hash)
4914 for (symidx = 0; symidx < extsymcount; ++symidx)
4915 if (sym_hash[symidx] == hi)
4916 {
4917 sym_hash[symidx] = h;
4918 break;
4919 }
4920 }
4921 free (shortname);
4922 }
4923 free (nondeflt_vers);
4924 nondeflt_vers = NULL;
4925 }
4926
4ad4eba5
AM
4927 /* Now set the weakdefs field correctly for all the weak defined
4928 symbols we found. The only way to do this is to search all the
4929 symbols. Since we only need the information for non functions in
4930 dynamic objects, that's the only time we actually put anything on
4931 the list WEAKS. We need this information so that if a regular
4932 object refers to a symbol defined weakly in a dynamic object, the
4933 real symbol in the dynamic object is also put in the dynamic
4934 symbols; we also must arrange for both symbols to point to the
4935 same memory location. We could handle the general case of symbol
4936 aliasing, but a general symbol alias can only be generated in
4937 assembler code, handling it correctly would be very time
4938 consuming, and other ELF linkers don't handle general aliasing
4939 either. */
4940 if (weaks != NULL)
4941 {
4942 struct elf_link_hash_entry **hpp;
4943 struct elf_link_hash_entry **hppend;
4944 struct elf_link_hash_entry **sorted_sym_hash;
4945 struct elf_link_hash_entry *h;
4946 size_t sym_count;
4947
4948 /* Since we have to search the whole symbol list for each weak
4949 defined symbol, search time for N weak defined symbols will be
4950 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
4951 amt = extsymcount;
4952 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 4953 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4954 if (sorted_sym_hash == NULL)
4955 goto error_return;
4956 sym_hash = sorted_sym_hash;
4957 hpp = elf_sym_hashes (abfd);
4958 hppend = hpp + extsymcount;
4959 sym_count = 0;
4960 for (; hpp < hppend; hpp++)
4961 {
4962 h = *hpp;
4963 if (h != NULL
4964 && h->root.type == bfd_link_hash_defined
fcb93ecf 4965 && !bed->is_function_type (h->type))
4ad4eba5
AM
4966 {
4967 *sym_hash = h;
4968 sym_hash++;
4969 sym_count++;
4970 }
4971 }
4972
4973 qsort (sorted_sym_hash, sym_count,
4974 sizeof (struct elf_link_hash_entry *),
4975 elf_sort_symbol);
4976
4977 while (weaks != NULL)
4978 {
4979 struct elf_link_hash_entry *hlook;
4980 asection *slook;
4981 bfd_vma vlook;
ed54588d 4982 size_t i, j, idx = 0;
4ad4eba5
AM
4983
4984 hlook = weaks;
f6e332e6
AM
4985 weaks = hlook->u.weakdef;
4986 hlook->u.weakdef = NULL;
4ad4eba5
AM
4987
4988 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4989 || hlook->root.type == bfd_link_hash_defweak
4990 || hlook->root.type == bfd_link_hash_common
4991 || hlook->root.type == bfd_link_hash_indirect);
4992 slook = hlook->root.u.def.section;
4993 vlook = hlook->root.u.def.value;
4994
4ad4eba5
AM
4995 i = 0;
4996 j = sym_count;
14160578 4997 while (i != j)
4ad4eba5
AM
4998 {
4999 bfd_signed_vma vdiff;
5000 idx = (i + j) / 2;
14160578 5001 h = sorted_sym_hash[idx];
4ad4eba5
AM
5002 vdiff = vlook - h->root.u.def.value;
5003 if (vdiff < 0)
5004 j = idx;
5005 else if (vdiff > 0)
5006 i = idx + 1;
5007 else
5008 {
d3435ae8 5009 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5010 if (sdiff < 0)
5011 j = idx;
5012 else if (sdiff > 0)
5013 i = idx + 1;
5014 else
14160578 5015 break;
4ad4eba5
AM
5016 }
5017 }
5018
5019 /* We didn't find a value/section match. */
14160578 5020 if (i == j)
4ad4eba5
AM
5021 continue;
5022
14160578
AM
5023 /* With multiple aliases, or when the weak symbol is already
5024 strongly defined, we have multiple matching symbols and
5025 the binary search above may land on any of them. Step
5026 one past the matching symbol(s). */
5027 while (++idx != j)
5028 {
5029 h = sorted_sym_hash[idx];
5030 if (h->root.u.def.section != slook
5031 || h->root.u.def.value != vlook)
5032 break;
5033 }
5034
5035 /* Now look back over the aliases. Since we sorted by size
5036 as well as value and section, we'll choose the one with
5037 the largest size. */
5038 while (idx-- != i)
4ad4eba5 5039 {
14160578 5040 h = sorted_sym_hash[idx];
4ad4eba5
AM
5041
5042 /* Stop if value or section doesn't match. */
14160578
AM
5043 if (h->root.u.def.section != slook
5044 || h->root.u.def.value != vlook)
4ad4eba5
AM
5045 break;
5046 else if (h != hlook)
5047 {
f6e332e6 5048 hlook->u.weakdef = h;
4ad4eba5
AM
5049
5050 /* If the weak definition is in the list of dynamic
5051 symbols, make sure the real definition is put
5052 there as well. */
5053 if (hlook->dynindx != -1 && h->dynindx == -1)
5054 {
c152c796 5055 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5056 {
5057 err_free_sym_hash:
5058 free (sorted_sym_hash);
5059 goto error_return;
5060 }
4ad4eba5
AM
5061 }
5062
5063 /* If the real definition is in the list of dynamic
5064 symbols, make sure the weak definition is put
5065 there as well. If we don't do this, then the
5066 dynamic loader might not merge the entries for the
5067 real definition and the weak definition. */
5068 if (h->dynindx != -1 && hlook->dynindx == -1)
5069 {
c152c796 5070 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5071 goto err_free_sym_hash;
4ad4eba5
AM
5072 }
5073 break;
5074 }
5075 }
5076 }
5077
5078 free (sorted_sym_hash);
5079 }
5080
33177bb1
AM
5081 if (bed->check_directives
5082 && !(*bed->check_directives) (abfd, info))
5083 return FALSE;
85fbca6a 5084
d9689752
L
5085 if (!info->check_relocs_after_open_input
5086 && !_bfd_elf_link_check_relocs (abfd, info))
5087 return FALSE;
4ad4eba5
AM
5088
5089 /* If this is a non-traditional link, try to optimize the handling
5090 of the .stab/.stabstr sections. */
5091 if (! dynamic
5092 && ! info->traditional_format
66eb6687 5093 && is_elf_hash_table (htab)
4ad4eba5
AM
5094 && (info->strip != strip_all && info->strip != strip_debugger))
5095 {
5096 asection *stabstr;
5097
5098 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5099 if (stabstr != NULL)
5100 {
5101 bfd_size_type string_offset = 0;
5102 asection *stab;
5103
5104 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5105 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5106 && (!stab->name[5] ||
5107 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5108 && (stab->flags & SEC_MERGE) == 0
5109 && !bfd_is_abs_section (stab->output_section))
5110 {
5111 struct bfd_elf_section_data *secdata;
5112
5113 secdata = elf_section_data (stab);
66eb6687
AM
5114 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5115 stabstr, &secdata->sec_info,
4ad4eba5
AM
5116 &string_offset))
5117 goto error_return;
5118 if (secdata->sec_info)
dbaa2011 5119 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5120 }
5121 }
5122 }
5123
66eb6687 5124 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5125 {
5126 /* Add this bfd to the loaded list. */
5127 struct elf_link_loaded_list *n;
5128
ca4be51c 5129 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5130 if (n == NULL)
5131 goto error_return;
5132 n->abfd = abfd;
66eb6687
AM
5133 n->next = htab->loaded;
5134 htab->loaded = n;
4ad4eba5
AM
5135 }
5136
5137 return TRUE;
5138
5139 error_free_vers:
66eb6687
AM
5140 if (old_tab != NULL)
5141 free (old_tab);
5b677558
AM
5142 if (old_strtab != NULL)
5143 free (old_strtab);
4ad4eba5
AM
5144 if (nondeflt_vers != NULL)
5145 free (nondeflt_vers);
5146 if (extversym != NULL)
5147 free (extversym);
5148 error_free_sym:
5149 if (isymbuf != NULL)
5150 free (isymbuf);
5151 error_return:
5152 return FALSE;
5153}
5154
8387904d
AM
5155/* Return the linker hash table entry of a symbol that might be
5156 satisfied by an archive symbol. Return -1 on error. */
5157
5158struct elf_link_hash_entry *
5159_bfd_elf_archive_symbol_lookup (bfd *abfd,
5160 struct bfd_link_info *info,
5161 const char *name)
5162{
5163 struct elf_link_hash_entry *h;
5164 char *p, *copy;
5165 size_t len, first;
5166
2a41f396 5167 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5168 if (h != NULL)
5169 return h;
5170
5171 /* If this is a default version (the name contains @@), look up the
5172 symbol again with only one `@' as well as without the version.
5173 The effect is that references to the symbol with and without the
5174 version will be matched by the default symbol in the archive. */
5175
5176 p = strchr (name, ELF_VER_CHR);
5177 if (p == NULL || p[1] != ELF_VER_CHR)
5178 return h;
5179
5180 /* First check with only one `@'. */
5181 len = strlen (name);
a50b1753 5182 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5183 if (copy == NULL)
5184 return (struct elf_link_hash_entry *) 0 - 1;
5185
5186 first = p - name + 1;
5187 memcpy (copy, name, first);
5188 memcpy (copy + first, name + first + 1, len - first);
5189
2a41f396 5190 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5191 if (h == NULL)
5192 {
5193 /* We also need to check references to the symbol without the
5194 version. */
5195 copy[first - 1] = '\0';
5196 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5197 FALSE, FALSE, TRUE);
8387904d
AM
5198 }
5199
5200 bfd_release (abfd, copy);
5201 return h;
5202}
5203
0ad989f9 5204/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5205 don't use _bfd_generic_link_add_archive_symbols because we need to
5206 handle versioned symbols.
0ad989f9
L
5207
5208 Fortunately, ELF archive handling is simpler than that done by
5209 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5210 oddities. In ELF, if we find a symbol in the archive map, and the
5211 symbol is currently undefined, we know that we must pull in that
5212 object file.
5213
5214 Unfortunately, we do have to make multiple passes over the symbol
5215 table until nothing further is resolved. */
5216
4ad4eba5
AM
5217static bfd_boolean
5218elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5219{
5220 symindex c;
13e570f8 5221 unsigned char *included = NULL;
0ad989f9
L
5222 carsym *symdefs;
5223 bfd_boolean loop;
5224 bfd_size_type amt;
8387904d
AM
5225 const struct elf_backend_data *bed;
5226 struct elf_link_hash_entry * (*archive_symbol_lookup)
5227 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5228
5229 if (! bfd_has_map (abfd))
5230 {
5231 /* An empty archive is a special case. */
5232 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5233 return TRUE;
5234 bfd_set_error (bfd_error_no_armap);
5235 return FALSE;
5236 }
5237
5238 /* Keep track of all symbols we know to be already defined, and all
5239 files we know to be already included. This is to speed up the
5240 second and subsequent passes. */
5241 c = bfd_ardata (abfd)->symdef_count;
5242 if (c == 0)
5243 return TRUE;
5244 amt = c;
13e570f8
AM
5245 amt *= sizeof (*included);
5246 included = (unsigned char *) bfd_zmalloc (amt);
5247 if (included == NULL)
5248 return FALSE;
0ad989f9
L
5249
5250 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5251 bed = get_elf_backend_data (abfd);
5252 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5253
5254 do
5255 {
5256 file_ptr last;
5257 symindex i;
5258 carsym *symdef;
5259 carsym *symdefend;
5260
5261 loop = FALSE;
5262 last = -1;
5263
5264 symdef = symdefs;
5265 symdefend = symdef + c;
5266 for (i = 0; symdef < symdefend; symdef++, i++)
5267 {
5268 struct elf_link_hash_entry *h;
5269 bfd *element;
5270 struct bfd_link_hash_entry *undefs_tail;
5271 symindex mark;
5272
13e570f8 5273 if (included[i])
0ad989f9
L
5274 continue;
5275 if (symdef->file_offset == last)
5276 {
5277 included[i] = TRUE;
5278 continue;
5279 }
5280
8387904d
AM
5281 h = archive_symbol_lookup (abfd, info, symdef->name);
5282 if (h == (struct elf_link_hash_entry *) 0 - 1)
5283 goto error_return;
0ad989f9
L
5284
5285 if (h == NULL)
5286 continue;
5287
5288 if (h->root.type == bfd_link_hash_common)
5289 {
5290 /* We currently have a common symbol. The archive map contains
5291 a reference to this symbol, so we may want to include it. We
5292 only want to include it however, if this archive element
5293 contains a definition of the symbol, not just another common
5294 declaration of it.
5295
5296 Unfortunately some archivers (including GNU ar) will put
5297 declarations of common symbols into their archive maps, as
5298 well as real definitions, so we cannot just go by the archive
5299 map alone. Instead we must read in the element's symbol
5300 table and check that to see what kind of symbol definition
5301 this is. */
5302 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5303 continue;
5304 }
5305 else if (h->root.type != bfd_link_hash_undefined)
5306 {
5307 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5308 /* Symbol must be defined. Don't check it again. */
5309 included[i] = TRUE;
0ad989f9
L
5310 continue;
5311 }
5312
5313 /* We need to include this archive member. */
5314 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5315 if (element == NULL)
5316 goto error_return;
5317
5318 if (! bfd_check_format (element, bfd_object))
5319 goto error_return;
5320
0ad989f9
L
5321 undefs_tail = info->hash->undefs_tail;
5322
0e144ba7
AM
5323 if (!(*info->callbacks
5324 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5325 continue;
0e144ba7 5326 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5327 goto error_return;
5328
5329 /* If there are any new undefined symbols, we need to make
5330 another pass through the archive in order to see whether
5331 they can be defined. FIXME: This isn't perfect, because
5332 common symbols wind up on undefs_tail and because an
5333 undefined symbol which is defined later on in this pass
5334 does not require another pass. This isn't a bug, but it
5335 does make the code less efficient than it could be. */
5336 if (undefs_tail != info->hash->undefs_tail)
5337 loop = TRUE;
5338
5339 /* Look backward to mark all symbols from this object file
5340 which we have already seen in this pass. */
5341 mark = i;
5342 do
5343 {
5344 included[mark] = TRUE;
5345 if (mark == 0)
5346 break;
5347 --mark;
5348 }
5349 while (symdefs[mark].file_offset == symdef->file_offset);
5350
5351 /* We mark subsequent symbols from this object file as we go
5352 on through the loop. */
5353 last = symdef->file_offset;
5354 }
5355 }
5356 while (loop);
5357
0ad989f9
L
5358 free (included);
5359
5360 return TRUE;
5361
5362 error_return:
0ad989f9
L
5363 if (included != NULL)
5364 free (included);
5365 return FALSE;
5366}
4ad4eba5
AM
5367
5368/* Given an ELF BFD, add symbols to the global hash table as
5369 appropriate. */
5370
5371bfd_boolean
5372bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5373{
5374 switch (bfd_get_format (abfd))
5375 {
5376 case bfd_object:
5377 return elf_link_add_object_symbols (abfd, info);
5378 case bfd_archive:
5379 return elf_link_add_archive_symbols (abfd, info);
5380 default:
5381 bfd_set_error (bfd_error_wrong_format);
5382 return FALSE;
5383 }
5384}
5a580b3a 5385\f
14b1c01e
AM
5386struct hash_codes_info
5387{
5388 unsigned long *hashcodes;
5389 bfd_boolean error;
5390};
a0c8462f 5391
5a580b3a
AM
5392/* This function will be called though elf_link_hash_traverse to store
5393 all hash value of the exported symbols in an array. */
5394
5395static bfd_boolean
5396elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5397{
a50b1753 5398 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5399 const char *name;
5a580b3a
AM
5400 unsigned long ha;
5401 char *alc = NULL;
5402
5a580b3a
AM
5403 /* Ignore indirect symbols. These are added by the versioning code. */
5404 if (h->dynindx == -1)
5405 return TRUE;
5406
5407 name = h->root.root.string;
422f1182 5408 if (h->versioned >= versioned)
5a580b3a 5409 {
422f1182
L
5410 char *p = strchr (name, ELF_VER_CHR);
5411 if (p != NULL)
14b1c01e 5412 {
422f1182
L
5413 alc = (char *) bfd_malloc (p - name + 1);
5414 if (alc == NULL)
5415 {
5416 inf->error = TRUE;
5417 return FALSE;
5418 }
5419 memcpy (alc, name, p - name);
5420 alc[p - name] = '\0';
5421 name = alc;
14b1c01e 5422 }
5a580b3a
AM
5423 }
5424
5425 /* Compute the hash value. */
5426 ha = bfd_elf_hash (name);
5427
5428 /* Store the found hash value in the array given as the argument. */
14b1c01e 5429 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5430
5431 /* And store it in the struct so that we can put it in the hash table
5432 later. */
f6e332e6 5433 h->u.elf_hash_value = ha;
5a580b3a
AM
5434
5435 if (alc != NULL)
5436 free (alc);
5437
5438 return TRUE;
5439}
5440
fdc90cb4
JJ
5441struct collect_gnu_hash_codes
5442{
5443 bfd *output_bfd;
5444 const struct elf_backend_data *bed;
5445 unsigned long int nsyms;
5446 unsigned long int maskbits;
5447 unsigned long int *hashcodes;
5448 unsigned long int *hashval;
5449 unsigned long int *indx;
5450 unsigned long int *counts;
5451 bfd_vma *bitmask;
5452 bfd_byte *contents;
5453 long int min_dynindx;
5454 unsigned long int bucketcount;
5455 unsigned long int symindx;
5456 long int local_indx;
5457 long int shift1, shift2;
5458 unsigned long int mask;
14b1c01e 5459 bfd_boolean error;
fdc90cb4
JJ
5460};
5461
5462/* This function will be called though elf_link_hash_traverse to store
5463 all hash value of the exported symbols in an array. */
5464
5465static bfd_boolean
5466elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5467{
a50b1753 5468 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5469 const char *name;
fdc90cb4
JJ
5470 unsigned long ha;
5471 char *alc = NULL;
5472
fdc90cb4
JJ
5473 /* Ignore indirect symbols. These are added by the versioning code. */
5474 if (h->dynindx == -1)
5475 return TRUE;
5476
5477 /* Ignore also local symbols and undefined symbols. */
5478 if (! (*s->bed->elf_hash_symbol) (h))
5479 return TRUE;
5480
5481 name = h->root.root.string;
422f1182 5482 if (h->versioned >= versioned)
fdc90cb4 5483 {
422f1182
L
5484 char *p = strchr (name, ELF_VER_CHR);
5485 if (p != NULL)
14b1c01e 5486 {
422f1182
L
5487 alc = (char *) bfd_malloc (p - name + 1);
5488 if (alc == NULL)
5489 {
5490 s->error = TRUE;
5491 return FALSE;
5492 }
5493 memcpy (alc, name, p - name);
5494 alc[p - name] = '\0';
5495 name = alc;
14b1c01e 5496 }
fdc90cb4
JJ
5497 }
5498
5499 /* Compute the hash value. */
5500 ha = bfd_elf_gnu_hash (name);
5501
5502 /* Store the found hash value in the array for compute_bucket_count,
5503 and also for .dynsym reordering purposes. */
5504 s->hashcodes[s->nsyms] = ha;
5505 s->hashval[h->dynindx] = ha;
5506 ++s->nsyms;
5507 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5508 s->min_dynindx = h->dynindx;
5509
5510 if (alc != NULL)
5511 free (alc);
5512
5513 return TRUE;
5514}
5515
5516/* This function will be called though elf_link_hash_traverse to do
5517 final dynaminc symbol renumbering. */
5518
5519static bfd_boolean
5520elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5521{
a50b1753 5522 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5523 unsigned long int bucket;
5524 unsigned long int val;
5525
fdc90cb4
JJ
5526 /* Ignore indirect symbols. */
5527 if (h->dynindx == -1)
5528 return TRUE;
5529
5530 /* Ignore also local symbols and undefined symbols. */
5531 if (! (*s->bed->elf_hash_symbol) (h))
5532 {
5533 if (h->dynindx >= s->min_dynindx)
5534 h->dynindx = s->local_indx++;
5535 return TRUE;
5536 }
5537
5538 bucket = s->hashval[h->dynindx] % s->bucketcount;
5539 val = (s->hashval[h->dynindx] >> s->shift1)
5540 & ((s->maskbits >> s->shift1) - 1);
5541 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5542 s->bitmask[val]
5543 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5544 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5545 if (s->counts[bucket] == 1)
5546 /* Last element terminates the chain. */
5547 val |= 1;
5548 bfd_put_32 (s->output_bfd, val,
5549 s->contents + (s->indx[bucket] - s->symindx) * 4);
5550 --s->counts[bucket];
5551 h->dynindx = s->indx[bucket]++;
5552 return TRUE;
5553}
5554
5555/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5556
5557bfd_boolean
5558_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5559{
5560 return !(h->forced_local
5561 || h->root.type == bfd_link_hash_undefined
5562 || h->root.type == bfd_link_hash_undefweak
5563 || ((h->root.type == bfd_link_hash_defined
5564 || h->root.type == bfd_link_hash_defweak)
5565 && h->root.u.def.section->output_section == NULL));
5566}
5567
5a580b3a
AM
5568/* Array used to determine the number of hash table buckets to use
5569 based on the number of symbols there are. If there are fewer than
5570 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5571 fewer than 37 we use 17 buckets, and so forth. We never use more
5572 than 32771 buckets. */
5573
5574static const size_t elf_buckets[] =
5575{
5576 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5577 16411, 32771, 0
5578};
5579
5580/* Compute bucket count for hashing table. We do not use a static set
5581 of possible tables sizes anymore. Instead we determine for all
5582 possible reasonable sizes of the table the outcome (i.e., the
5583 number of collisions etc) and choose the best solution. The
5584 weighting functions are not too simple to allow the table to grow
5585 without bounds. Instead one of the weighting factors is the size.
5586 Therefore the result is always a good payoff between few collisions
5587 (= short chain lengths) and table size. */
5588static size_t
b20dd2ce 5589compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5590 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5591 unsigned long int nsyms,
5592 int gnu_hash)
5a580b3a 5593{
5a580b3a 5594 size_t best_size = 0;
5a580b3a 5595 unsigned long int i;
5a580b3a 5596
5a580b3a
AM
5597 /* We have a problem here. The following code to optimize the table
5598 size requires an integer type with more the 32 bits. If
5599 BFD_HOST_U_64_BIT is set we know about such a type. */
5600#ifdef BFD_HOST_U_64_BIT
5601 if (info->optimize)
5602 {
5a580b3a
AM
5603 size_t minsize;
5604 size_t maxsize;
5605 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5606 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5607 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5608 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5609 unsigned long int *counts;
d40f3da9 5610 bfd_size_type amt;
0883b6e0 5611 unsigned int no_improvement_count = 0;
5a580b3a
AM
5612
5613 /* Possible optimization parameters: if we have NSYMS symbols we say
5614 that the hashing table must at least have NSYMS/4 and at most
5615 2*NSYMS buckets. */
5616 minsize = nsyms / 4;
5617 if (minsize == 0)
5618 minsize = 1;
5619 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5620 if (gnu_hash)
5621 {
5622 if (minsize < 2)
5623 minsize = 2;
5624 if ((best_size & 31) == 0)
5625 ++best_size;
5626 }
5a580b3a
AM
5627
5628 /* Create array where we count the collisions in. We must use bfd_malloc
5629 since the size could be large. */
5630 amt = maxsize;
5631 amt *= sizeof (unsigned long int);
a50b1753 5632 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5633 if (counts == NULL)
fdc90cb4 5634 return 0;
5a580b3a
AM
5635
5636 /* Compute the "optimal" size for the hash table. The criteria is a
5637 minimal chain length. The minor criteria is (of course) the size
5638 of the table. */
5639 for (i = minsize; i < maxsize; ++i)
5640 {
5641 /* Walk through the array of hashcodes and count the collisions. */
5642 BFD_HOST_U_64_BIT max;
5643 unsigned long int j;
5644 unsigned long int fact;
5645
fdc90cb4
JJ
5646 if (gnu_hash && (i & 31) == 0)
5647 continue;
5648
5a580b3a
AM
5649 memset (counts, '\0', i * sizeof (unsigned long int));
5650
5651 /* Determine how often each hash bucket is used. */
5652 for (j = 0; j < nsyms; ++j)
5653 ++counts[hashcodes[j] % i];
5654
5655 /* For the weight function we need some information about the
5656 pagesize on the target. This is information need not be 100%
5657 accurate. Since this information is not available (so far) we
5658 define it here to a reasonable default value. If it is crucial
5659 to have a better value some day simply define this value. */
5660# ifndef BFD_TARGET_PAGESIZE
5661# define BFD_TARGET_PAGESIZE (4096)
5662# endif
5663
fdc90cb4
JJ
5664 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5665 and the chains. */
5666 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5667
5668# if 1
5669 /* Variant 1: optimize for short chains. We add the squares
5670 of all the chain lengths (which favors many small chain
5671 over a few long chains). */
5672 for (j = 0; j < i; ++j)
5673 max += counts[j] * counts[j];
5674
5675 /* This adds penalties for the overall size of the table. */
fdc90cb4 5676 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5677 max *= fact * fact;
5678# else
5679 /* Variant 2: Optimize a lot more for small table. Here we
5680 also add squares of the size but we also add penalties for
5681 empty slots (the +1 term). */
5682 for (j = 0; j < i; ++j)
5683 max += (1 + counts[j]) * (1 + counts[j]);
5684
5685 /* The overall size of the table is considered, but not as
5686 strong as in variant 1, where it is squared. */
fdc90cb4 5687 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5688 max *= fact;
5689# endif
5690
5691 /* Compare with current best results. */
5692 if (max < best_chlen)
5693 {
5694 best_chlen = max;
5695 best_size = i;
ca4be51c 5696 no_improvement_count = 0;
5a580b3a 5697 }
0883b6e0
NC
5698 /* PR 11843: Avoid futile long searches for the best bucket size
5699 when there are a large number of symbols. */
5700 else if (++no_improvement_count == 100)
5701 break;
5a580b3a
AM
5702 }
5703
5704 free (counts);
5705 }
5706 else
5707#endif /* defined (BFD_HOST_U_64_BIT) */
5708 {
5709 /* This is the fallback solution if no 64bit type is available or if we
5710 are not supposed to spend much time on optimizations. We select the
5711 bucket count using a fixed set of numbers. */
5712 for (i = 0; elf_buckets[i] != 0; i++)
5713 {
5714 best_size = elf_buckets[i];
fdc90cb4 5715 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5716 break;
5717 }
fdc90cb4
JJ
5718 if (gnu_hash && best_size < 2)
5719 best_size = 2;
5a580b3a
AM
5720 }
5721
5a580b3a
AM
5722 return best_size;
5723}
5724
d0bf826b
AM
5725/* Size any SHT_GROUP section for ld -r. */
5726
5727bfd_boolean
5728_bfd_elf_size_group_sections (struct bfd_link_info *info)
5729{
5730 bfd *ibfd;
5731
c72f2fb2 5732 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5733 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5734 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5735 return FALSE;
5736 return TRUE;
5737}
5738
04c3a755
NS
5739/* Set a default stack segment size. The value in INFO wins. If it
5740 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5741 undefined it is initialized. */
5742
5743bfd_boolean
5744bfd_elf_stack_segment_size (bfd *output_bfd,
5745 struct bfd_link_info *info,
5746 const char *legacy_symbol,
5747 bfd_vma default_size)
5748{
5749 struct elf_link_hash_entry *h = NULL;
5750
5751 /* Look for legacy symbol. */
5752 if (legacy_symbol)
5753 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5754 FALSE, FALSE, FALSE);
5755 if (h && (h->root.type == bfd_link_hash_defined
5756 || h->root.type == bfd_link_hash_defweak)
5757 && h->def_regular
5758 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5759 {
5760 /* The symbol has no type if specified on the command line. */
5761 h->type = STT_OBJECT;
5762 if (info->stacksize)
5763 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5764 output_bfd, legacy_symbol);
5765 else if (h->root.u.def.section != bfd_abs_section_ptr)
5766 (*_bfd_error_handler) (_("%B: %s not absolute"),
5767 output_bfd, legacy_symbol);
5768 else
5769 info->stacksize = h->root.u.def.value;
5770 }
5771
5772 if (!info->stacksize)
5773 /* If the user didn't set a size, or explicitly inhibit the
5774 size, set it now. */
5775 info->stacksize = default_size;
5776
5777 /* Provide the legacy symbol, if it is referenced. */
5778 if (h && (h->root.type == bfd_link_hash_undefined
5779 || h->root.type == bfd_link_hash_undefweak))
5780 {
5781 struct bfd_link_hash_entry *bh = NULL;
5782
5783 if (!(_bfd_generic_link_add_one_symbol
5784 (info, output_bfd, legacy_symbol,
5785 BSF_GLOBAL, bfd_abs_section_ptr,
5786 info->stacksize >= 0 ? info->stacksize : 0,
5787 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5788 return FALSE;
5789
5790 h = (struct elf_link_hash_entry *) bh;
5791 h->def_regular = 1;
5792 h->type = STT_OBJECT;
5793 }
5794
5795 return TRUE;
5796}
5797
5a580b3a
AM
5798/* Set up the sizes and contents of the ELF dynamic sections. This is
5799 called by the ELF linker emulation before_allocation routine. We
5800 must set the sizes of the sections before the linker sets the
5801 addresses of the various sections. */
5802
5803bfd_boolean
5804bfd_elf_size_dynamic_sections (bfd *output_bfd,
5805 const char *soname,
5806 const char *rpath,
5807 const char *filter_shlib,
7ee314fa
AM
5808 const char *audit,
5809 const char *depaudit,
5a580b3a
AM
5810 const char * const *auxiliary_filters,
5811 struct bfd_link_info *info,
fd91d419 5812 asection **sinterpptr)
5a580b3a 5813{
ef53be89 5814 size_t soname_indx;
5a580b3a
AM
5815 bfd *dynobj;
5816 const struct elf_backend_data *bed;
28caa186 5817 struct elf_info_failed asvinfo;
5a580b3a
AM
5818
5819 *sinterpptr = NULL;
5820
ef53be89 5821 soname_indx = (size_t) -1;
5a580b3a
AM
5822
5823 if (!is_elf_hash_table (info->hash))
5824 return TRUE;
5825
6bfdb61b 5826 bed = get_elf_backend_data (output_bfd);
04c3a755
NS
5827
5828 /* Any syms created from now on start with -1 in
5829 got.refcount/offset and plt.refcount/offset. */
5830 elf_hash_table (info)->init_got_refcount
5831 = elf_hash_table (info)->init_got_offset;
5832 elf_hash_table (info)->init_plt_refcount
5833 = elf_hash_table (info)->init_plt_offset;
5834
0e1862bb 5835 if (bfd_link_relocatable (info)
04c3a755
NS
5836 && !_bfd_elf_size_group_sections (info))
5837 return FALSE;
5838
5839 /* The backend may have to create some sections regardless of whether
5840 we're dynamic or not. */
5841 if (bed->elf_backend_always_size_sections
5842 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5843 return FALSE;
5844
5845 /* Determine any GNU_STACK segment requirements, after the backend
5846 has had a chance to set a default segment size. */
5a580b3a 5847 if (info->execstack)
12bd6957 5848 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5a580b3a 5849 else if (info->noexecstack)
12bd6957 5850 elf_stack_flags (output_bfd) = PF_R | PF_W;
5a580b3a
AM
5851 else
5852 {
5853 bfd *inputobj;
5854 asection *notesec = NULL;
5855 int exec = 0;
5856
5857 for (inputobj = info->input_bfds;
5858 inputobj;
c72f2fb2 5859 inputobj = inputobj->link.next)
5a580b3a
AM
5860 {
5861 asection *s;
5862
a92c088a
L
5863 if (inputobj->flags
5864 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5865 continue;
5866 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5867 if (s)
5868 {
5869 if (s->flags & SEC_CODE)
5870 exec = PF_X;
5871 notesec = s;
5872 }
6bfdb61b 5873 else if (bed->default_execstack)
5a580b3a
AM
5874 exec = PF_X;
5875 }
04c3a755 5876 if (notesec || info->stacksize > 0)
12bd6957 5877 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
0e1862bb 5878 if (notesec && exec && bfd_link_relocatable (info)
04c3a755
NS
5879 && notesec->output_section != bfd_abs_section_ptr)
5880 notesec->output_section->flags |= SEC_CODE;
5a580b3a
AM
5881 }
5882
5a580b3a
AM
5883 dynobj = elf_hash_table (info)->dynobj;
5884
9a2a56cc 5885 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
5886 {
5887 struct elf_info_failed eif;
5888 struct elf_link_hash_entry *h;
5889 asection *dynstr;
5890 struct bfd_elf_version_tree *t;
5891 struct bfd_elf_version_expr *d;
046183de 5892 asection *s;
5a580b3a
AM
5893 bfd_boolean all_defined;
5894
3d4d4302 5895 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
9b8b325a 5896 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5a580b3a
AM
5897
5898 if (soname != NULL)
5899 {
5900 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5901 soname, TRUE);
ef53be89 5902 if (soname_indx == (size_t) -1
5a580b3a
AM
5903 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5904 return FALSE;
5905 }
5906
5907 if (info->symbolic)
5908 {
5909 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5910 return FALSE;
5911 info->flags |= DF_SYMBOLIC;
5912 }
5913
5914 if (rpath != NULL)
5915 {
ef53be89 5916 size_t indx;
b1b00fcc 5917 bfd_vma tag;
5a580b3a
AM
5918
5919 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5920 TRUE);
ef53be89 5921 if (indx == (size_t) -1)
5a580b3a
AM
5922 return FALSE;
5923
b1b00fcc
MF
5924 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5925 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5926 return FALSE;
5a580b3a
AM
5927 }
5928
5929 if (filter_shlib != NULL)
5930 {
ef53be89 5931 size_t indx;
5a580b3a
AM
5932
5933 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5934 filter_shlib, TRUE);
ef53be89 5935 if (indx == (size_t) -1
5a580b3a
AM
5936 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5937 return FALSE;
5938 }
5939
5940 if (auxiliary_filters != NULL)
5941 {
5942 const char * const *p;
5943
5944 for (p = auxiliary_filters; *p != NULL; p++)
5945 {
ef53be89 5946 size_t indx;
5a580b3a
AM
5947
5948 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5949 *p, TRUE);
ef53be89 5950 if (indx == (size_t) -1
5a580b3a
AM
5951 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5952 return FALSE;
5953 }
5954 }
5955
7ee314fa
AM
5956 if (audit != NULL)
5957 {
ef53be89 5958 size_t indx;
7ee314fa
AM
5959
5960 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5961 TRUE);
ef53be89 5962 if (indx == (size_t) -1
7ee314fa
AM
5963 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5964 return FALSE;
5965 }
5966
5967 if (depaudit != NULL)
5968 {
ef53be89 5969 size_t indx;
7ee314fa
AM
5970
5971 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5972 TRUE);
ef53be89 5973 if (indx == (size_t) -1
7ee314fa
AM
5974 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5975 return FALSE;
5976 }
5977
5a580b3a 5978 eif.info = info;
5a580b3a
AM
5979 eif.failed = FALSE;
5980
5981 /* If we are supposed to export all symbols into the dynamic symbol
5982 table (this is not the normal case), then do so. */
55255dae 5983 if (info->export_dynamic
0e1862bb 5984 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
5985 {
5986 elf_link_hash_traverse (elf_hash_table (info),
5987 _bfd_elf_export_symbol,
5988 &eif);
5989 if (eif.failed)
5990 return FALSE;
5991 }
5992
5993 /* Make all global versions with definition. */
fd91d419 5994 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5995 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5996 if (!d->symver && d->literal)
5a580b3a
AM
5997 {
5998 const char *verstr, *name;
5999 size_t namelen, verlen, newlen;
93252b1c 6000 char *newname, *p, leading_char;
5a580b3a
AM
6001 struct elf_link_hash_entry *newh;
6002
93252b1c 6003 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6004 name = d->pattern;
93252b1c 6005 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6006 verstr = t->name;
6007 verlen = strlen (verstr);
6008 newlen = namelen + verlen + 3;
6009
a50b1753 6010 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6011 if (newname == NULL)
6012 return FALSE;
93252b1c
MF
6013 newname[0] = leading_char;
6014 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6015
6016 /* Check the hidden versioned definition. */
6017 p = newname + namelen;
6018 *p++ = ELF_VER_CHR;
6019 memcpy (p, verstr, verlen + 1);
6020 newh = elf_link_hash_lookup (elf_hash_table (info),
6021 newname, FALSE, FALSE,
6022 FALSE);
6023 if (newh == NULL
6024 || (newh->root.type != bfd_link_hash_defined
6025 && newh->root.type != bfd_link_hash_defweak))
6026 {
6027 /* Check the default versioned definition. */
6028 *p++ = ELF_VER_CHR;
6029 memcpy (p, verstr, verlen + 1);
6030 newh = elf_link_hash_lookup (elf_hash_table (info),
6031 newname, FALSE, FALSE,
6032 FALSE);
6033 }
6034 free (newname);
6035
6036 /* Mark this version if there is a definition and it is
6037 not defined in a shared object. */
6038 if (newh != NULL
f5385ebf 6039 && !newh->def_dynamic
5a580b3a
AM
6040 && (newh->root.type == bfd_link_hash_defined
6041 || newh->root.type == bfd_link_hash_defweak))
6042 d->symver = 1;
6043 }
6044
6045 /* Attach all the symbols to their version information. */
5a580b3a 6046 asvinfo.info = info;
5a580b3a
AM
6047 asvinfo.failed = FALSE;
6048
6049 elf_link_hash_traverse (elf_hash_table (info),
6050 _bfd_elf_link_assign_sym_version,
6051 &asvinfo);
6052 if (asvinfo.failed)
6053 return FALSE;
6054
6055 if (!info->allow_undefined_version)
6056 {
6057 /* Check if all global versions have a definition. */
6058 all_defined = TRUE;
fd91d419 6059 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6060 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6061 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
6062 {
6063 (*_bfd_error_handler)
6064 (_("%s: undefined version: %s"),
6065 d->pattern, t->name);
6066 all_defined = FALSE;
6067 }
6068
6069 if (!all_defined)
6070 {
6071 bfd_set_error (bfd_error_bad_value);
6072 return FALSE;
6073 }
6074 }
6075
6076 /* Find all symbols which were defined in a dynamic object and make
6077 the backend pick a reasonable value for them. */
6078 elf_link_hash_traverse (elf_hash_table (info),
6079 _bfd_elf_adjust_dynamic_symbol,
6080 &eif);
6081 if (eif.failed)
6082 return FALSE;
6083
6084 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 6085 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
6086 now so that we know the final size of the .dynamic section. */
6087
6088 /* If there are initialization and/or finalization functions to
6089 call then add the corresponding DT_INIT/DT_FINI entries. */
6090 h = (info->init_function
6091 ? elf_link_hash_lookup (elf_hash_table (info),
6092 info->init_function, FALSE,
6093 FALSE, FALSE)
6094 : NULL);
6095 if (h != NULL
f5385ebf
AM
6096 && (h->ref_regular
6097 || h->def_regular))
5a580b3a
AM
6098 {
6099 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6100 return FALSE;
6101 }
6102 h = (info->fini_function
6103 ? elf_link_hash_lookup (elf_hash_table (info),
6104 info->fini_function, FALSE,
6105 FALSE, FALSE)
6106 : NULL);
6107 if (h != NULL
f5385ebf
AM
6108 && (h->ref_regular
6109 || h->def_regular))
5a580b3a
AM
6110 {
6111 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6112 return FALSE;
6113 }
6114
046183de
AM
6115 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6116 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6117 {
6118 /* DT_PREINIT_ARRAY is not allowed in shared library. */
0e1862bb 6119 if (! bfd_link_executable (info))
5a580b3a
AM
6120 {
6121 bfd *sub;
6122 asection *o;
6123
6124 for (sub = info->input_bfds; sub != NULL;
c72f2fb2 6125 sub = sub->link.next)
3fcd97f1
JJ
6126 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6127 for (o = sub->sections; o != NULL; o = o->next)
6128 if (elf_section_data (o)->this_hdr.sh_type
6129 == SHT_PREINIT_ARRAY)
6130 {
6131 (*_bfd_error_handler)
6132 (_("%B: .preinit_array section is not allowed in DSO"),
6133 sub);
6134 break;
6135 }
5a580b3a
AM
6136
6137 bfd_set_error (bfd_error_nonrepresentable_section);
6138 return FALSE;
6139 }
6140
6141 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6142 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6143 return FALSE;
6144 }
046183de
AM
6145 s = bfd_get_section_by_name (output_bfd, ".init_array");
6146 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6147 {
6148 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6149 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6150 return FALSE;
6151 }
046183de
AM
6152 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6153 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6154 {
6155 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6156 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6157 return FALSE;
6158 }
6159
3d4d4302 6160 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6161 /* If .dynstr is excluded from the link, we don't want any of
6162 these tags. Strictly, we should be checking each section
6163 individually; This quick check covers for the case where
6164 someone does a /DISCARD/ : { *(*) }. */
6165 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6166 {
6167 bfd_size_type strsize;
6168
6169 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
6170 if ((info->emit_hash
6171 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6172 || (info->emit_gnu_hash
6173 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
6174 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6175 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6176 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6177 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6178 bed->s->sizeof_sym))
6179 return FALSE;
6180 }
6181 }
6182
de231f20
CM
6183 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6184 return FALSE;
6185
5a580b3a
AM
6186 /* The backend must work out the sizes of all the other dynamic
6187 sections. */
9a2a56cc
AM
6188 if (dynobj != NULL
6189 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
6190 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6191 return FALSE;
6192
9a2a56cc 6193 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6194 {
554220db 6195 unsigned long section_sym_count;
fd91d419 6196 struct bfd_elf_version_tree *verdefs;
5a580b3a 6197 asection *s;
5a580b3a
AM
6198
6199 /* Set up the version definition section. */
3d4d4302 6200 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
6201 BFD_ASSERT (s != NULL);
6202
6203 /* We may have created additional version definitions if we are
6204 just linking a regular application. */
fd91d419 6205 verdefs = info->version_info;
5a580b3a
AM
6206
6207 /* Skip anonymous version tag. */
6208 if (verdefs != NULL && verdefs->vernum == 0)
6209 verdefs = verdefs->next;
6210
3e3b46e5 6211 if (verdefs == NULL && !info->create_default_symver)
8423293d 6212 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6213 else
6214 {
6215 unsigned int cdefs;
6216 bfd_size_type size;
6217 struct bfd_elf_version_tree *t;
6218 bfd_byte *p;
6219 Elf_Internal_Verdef def;
6220 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
6221 struct bfd_link_hash_entry *bh;
6222 struct elf_link_hash_entry *h;
6223 const char *name;
5a580b3a
AM
6224
6225 cdefs = 0;
6226 size = 0;
6227
6228 /* Make space for the base version. */
6229 size += sizeof (Elf_External_Verdef);
6230 size += sizeof (Elf_External_Verdaux);
6231 ++cdefs;
6232
3e3b46e5
PB
6233 /* Make space for the default version. */
6234 if (info->create_default_symver)
6235 {
6236 size += sizeof (Elf_External_Verdef);
6237 ++cdefs;
6238 }
6239
5a580b3a
AM
6240 for (t = verdefs; t != NULL; t = t->next)
6241 {
6242 struct bfd_elf_version_deps *n;
6243
a6cc6b3b
RO
6244 /* Don't emit base version twice. */
6245 if (t->vernum == 0)
6246 continue;
6247
5a580b3a
AM
6248 size += sizeof (Elf_External_Verdef);
6249 size += sizeof (Elf_External_Verdaux);
6250 ++cdefs;
6251
6252 for (n = t->deps; n != NULL; n = n->next)
6253 size += sizeof (Elf_External_Verdaux);
6254 }
6255
eea6121a 6256 s->size = size;
a50b1753 6257 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6258 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6259 return FALSE;
6260
6261 /* Fill in the version definition section. */
6262
6263 p = s->contents;
6264
6265 def.vd_version = VER_DEF_CURRENT;
6266 def.vd_flags = VER_FLG_BASE;
6267 def.vd_ndx = 1;
6268 def.vd_cnt = 1;
3e3b46e5
PB
6269 if (info->create_default_symver)
6270 {
6271 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6272 def.vd_next = sizeof (Elf_External_Verdef);
6273 }
6274 else
6275 {
6276 def.vd_aux = sizeof (Elf_External_Verdef);
6277 def.vd_next = (sizeof (Elf_External_Verdef)
6278 + sizeof (Elf_External_Verdaux));
6279 }
5a580b3a 6280
ef53be89 6281 if (soname_indx != (size_t) -1)
5a580b3a
AM
6282 {
6283 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6284 soname_indx);
6285 def.vd_hash = bfd_elf_hash (soname);
6286 defaux.vda_name = soname_indx;
3e3b46e5 6287 name = soname;
5a580b3a
AM
6288 }
6289 else
6290 {
ef53be89 6291 size_t indx;
5a580b3a 6292
06084812 6293 name = lbasename (output_bfd->filename);
5a580b3a
AM
6294 def.vd_hash = bfd_elf_hash (name);
6295 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6296 name, FALSE);
ef53be89 6297 if (indx == (size_t) -1)
5a580b3a
AM
6298 return FALSE;
6299 defaux.vda_name = indx;
6300 }
6301 defaux.vda_next = 0;
6302
6303 _bfd_elf_swap_verdef_out (output_bfd, &def,
6304 (Elf_External_Verdef *) p);
6305 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6306 if (info->create_default_symver)
6307 {
6308 /* Add a symbol representing this version. */
6309 bh = NULL;
6310 if (! (_bfd_generic_link_add_one_symbol
6311 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6312 0, NULL, FALSE,
6313 get_elf_backend_data (dynobj)->collect, &bh)))
6314 return FALSE;
6315 h = (struct elf_link_hash_entry *) bh;
6316 h->non_elf = 0;
6317 h->def_regular = 1;
6318 h->type = STT_OBJECT;
6319 h->verinfo.vertree = NULL;
6320
6321 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6322 return FALSE;
6323
6324 /* Create a duplicate of the base version with the same
6325 aux block, but different flags. */
6326 def.vd_flags = 0;
6327 def.vd_ndx = 2;
6328 def.vd_aux = sizeof (Elf_External_Verdef);
6329 if (verdefs)
6330 def.vd_next = (sizeof (Elf_External_Verdef)
6331 + sizeof (Elf_External_Verdaux));
6332 else
6333 def.vd_next = 0;
6334 _bfd_elf_swap_verdef_out (output_bfd, &def,
6335 (Elf_External_Verdef *) p);
6336 p += sizeof (Elf_External_Verdef);
6337 }
5a580b3a
AM
6338 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6339 (Elf_External_Verdaux *) p);
6340 p += sizeof (Elf_External_Verdaux);
6341
6342 for (t = verdefs; t != NULL; t = t->next)
6343 {
6344 unsigned int cdeps;
6345 struct bfd_elf_version_deps *n;
5a580b3a 6346
a6cc6b3b
RO
6347 /* Don't emit the base version twice. */
6348 if (t->vernum == 0)
6349 continue;
6350
5a580b3a
AM
6351 cdeps = 0;
6352 for (n = t->deps; n != NULL; n = n->next)
6353 ++cdeps;
6354
6355 /* Add a symbol representing this version. */
6356 bh = NULL;
6357 if (! (_bfd_generic_link_add_one_symbol
6358 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6359 0, NULL, FALSE,
6360 get_elf_backend_data (dynobj)->collect, &bh)))
6361 return FALSE;
6362 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6363 h->non_elf = 0;
6364 h->def_regular = 1;
5a580b3a
AM
6365 h->type = STT_OBJECT;
6366 h->verinfo.vertree = t;
6367
c152c796 6368 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6369 return FALSE;
6370
6371 def.vd_version = VER_DEF_CURRENT;
6372 def.vd_flags = 0;
6373 if (t->globals.list == NULL
6374 && t->locals.list == NULL
6375 && ! t->used)
6376 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6377 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6378 def.vd_cnt = cdeps + 1;
6379 def.vd_hash = bfd_elf_hash (t->name);
6380 def.vd_aux = sizeof (Elf_External_Verdef);
6381 def.vd_next = 0;
a6cc6b3b
RO
6382
6383 /* If a basever node is next, it *must* be the last node in
6384 the chain, otherwise Verdef construction breaks. */
6385 if (t->next != NULL && t->next->vernum == 0)
6386 BFD_ASSERT (t->next->next == NULL);
6387
6388 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6389 def.vd_next = (sizeof (Elf_External_Verdef)
6390 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6391
6392 _bfd_elf_swap_verdef_out (output_bfd, &def,
6393 (Elf_External_Verdef *) p);
6394 p += sizeof (Elf_External_Verdef);
6395
6396 defaux.vda_name = h->dynstr_index;
6397 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6398 h->dynstr_index);
6399 defaux.vda_next = 0;
6400 if (t->deps != NULL)
6401 defaux.vda_next = sizeof (Elf_External_Verdaux);
6402 t->name_indx = defaux.vda_name;
6403
6404 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6405 (Elf_External_Verdaux *) p);
6406 p += sizeof (Elf_External_Verdaux);
6407
6408 for (n = t->deps; n != NULL; n = n->next)
6409 {
6410 if (n->version_needed == NULL)
6411 {
6412 /* This can happen if there was an error in the
6413 version script. */
6414 defaux.vda_name = 0;
6415 }
6416 else
6417 {
6418 defaux.vda_name = n->version_needed->name_indx;
6419 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6420 defaux.vda_name);
6421 }
6422 if (n->next == NULL)
6423 defaux.vda_next = 0;
6424 else
6425 defaux.vda_next = sizeof (Elf_External_Verdaux);
6426
6427 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6428 (Elf_External_Verdaux *) p);
6429 p += sizeof (Elf_External_Verdaux);
6430 }
6431 }
6432
6433 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6434 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6435 return FALSE;
6436
6437 elf_tdata (output_bfd)->cverdefs = cdefs;
6438 }
6439
6440 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6441 {
6442 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6443 return FALSE;
6444 }
6445 else if (info->flags & DF_BIND_NOW)
6446 {
6447 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6448 return FALSE;
6449 }
6450
6451 if (info->flags_1)
6452 {
0e1862bb 6453 if (bfd_link_executable (info))
5a580b3a
AM
6454 info->flags_1 &= ~ (DF_1_INITFIRST
6455 | DF_1_NODELETE
6456 | DF_1_NOOPEN);
6457 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6458 return FALSE;
6459 }
6460
6461 /* Work out the size of the version reference section. */
6462
3d4d4302 6463 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6464 BFD_ASSERT (s != NULL);
6465 {
6466 struct elf_find_verdep_info sinfo;
6467
5a580b3a
AM
6468 sinfo.info = info;
6469 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6470 if (sinfo.vers == 0)
6471 sinfo.vers = 1;
6472 sinfo.failed = FALSE;
6473
6474 elf_link_hash_traverse (elf_hash_table (info),
6475 _bfd_elf_link_find_version_dependencies,
6476 &sinfo);
14b1c01e
AM
6477 if (sinfo.failed)
6478 return FALSE;
5a580b3a
AM
6479
6480 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6481 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6482 else
6483 {
6484 Elf_Internal_Verneed *t;
6485 unsigned int size;
6486 unsigned int crefs;
6487 bfd_byte *p;
6488
a6cc6b3b 6489 /* Build the version dependency section. */
5a580b3a
AM
6490 size = 0;
6491 crefs = 0;
6492 for (t = elf_tdata (output_bfd)->verref;
6493 t != NULL;
6494 t = t->vn_nextref)
6495 {
6496 Elf_Internal_Vernaux *a;
6497
6498 size += sizeof (Elf_External_Verneed);
6499 ++crefs;
6500 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6501 size += sizeof (Elf_External_Vernaux);
6502 }
6503
eea6121a 6504 s->size = size;
a50b1753 6505 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6506 if (s->contents == NULL)
6507 return FALSE;
6508
6509 p = s->contents;
6510 for (t = elf_tdata (output_bfd)->verref;
6511 t != NULL;
6512 t = t->vn_nextref)
6513 {
6514 unsigned int caux;
6515 Elf_Internal_Vernaux *a;
ef53be89 6516 size_t indx;
5a580b3a
AM
6517
6518 caux = 0;
6519 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6520 ++caux;
6521
6522 t->vn_version = VER_NEED_CURRENT;
6523 t->vn_cnt = caux;
6524 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6525 elf_dt_name (t->vn_bfd) != NULL
6526 ? elf_dt_name (t->vn_bfd)
06084812 6527 : lbasename (t->vn_bfd->filename),
5a580b3a 6528 FALSE);
ef53be89 6529 if (indx == (size_t) -1)
5a580b3a
AM
6530 return FALSE;
6531 t->vn_file = indx;
6532 t->vn_aux = sizeof (Elf_External_Verneed);
6533 if (t->vn_nextref == NULL)
6534 t->vn_next = 0;
6535 else
6536 t->vn_next = (sizeof (Elf_External_Verneed)
6537 + caux * sizeof (Elf_External_Vernaux));
6538
6539 _bfd_elf_swap_verneed_out (output_bfd, t,
6540 (Elf_External_Verneed *) p);
6541 p += sizeof (Elf_External_Verneed);
6542
6543 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6544 {
6545 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6546 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6547 a->vna_nodename, FALSE);
ef53be89 6548 if (indx == (size_t) -1)
5a580b3a
AM
6549 return FALSE;
6550 a->vna_name = indx;
6551 if (a->vna_nextptr == NULL)
6552 a->vna_next = 0;
6553 else
6554 a->vna_next = sizeof (Elf_External_Vernaux);
6555
6556 _bfd_elf_swap_vernaux_out (output_bfd, a,
6557 (Elf_External_Vernaux *) p);
6558 p += sizeof (Elf_External_Vernaux);
6559 }
6560 }
6561
6562 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6563 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6564 return FALSE;
6565
6566 elf_tdata (output_bfd)->cverrefs = crefs;
6567 }
6568 }
6569
8423293d
AM
6570 if ((elf_tdata (output_bfd)->cverrefs == 0
6571 && elf_tdata (output_bfd)->cverdefs == 0)
6572 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6573 &section_sym_count) == 0)
6574 {
3d4d4302 6575 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6576 s->flags |= SEC_EXCLUDE;
6577 }
6578 }
6579 return TRUE;
6580}
6581
74541ad4
AM
6582/* Find the first non-excluded output section. We'll use its
6583 section symbol for some emitted relocs. */
6584void
6585_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6586{
6587 asection *s;
6588
6589 for (s = output_bfd->sections; s != NULL; s = s->next)
6590 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6591 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6592 {
6593 elf_hash_table (info)->text_index_section = s;
6594 break;
6595 }
6596}
6597
6598/* Find two non-excluded output sections, one for code, one for data.
6599 We'll use their section symbols for some emitted relocs. */
6600void
6601_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6602{
6603 asection *s;
6604
266b05cf
DJ
6605 /* Data first, since setting text_index_section changes
6606 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6607 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6608 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6609 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6610 {
266b05cf 6611 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6612 break;
6613 }
6614
6615 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6616 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6617 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6618 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6619 {
266b05cf 6620 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6621 break;
6622 }
6623
6624 if (elf_hash_table (info)->text_index_section == NULL)
6625 elf_hash_table (info)->text_index_section
6626 = elf_hash_table (info)->data_index_section;
6627}
6628
8423293d
AM
6629bfd_boolean
6630bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6631{
74541ad4
AM
6632 const struct elf_backend_data *bed;
6633
8423293d
AM
6634 if (!is_elf_hash_table (info->hash))
6635 return TRUE;
6636
74541ad4
AM
6637 bed = get_elf_backend_data (output_bfd);
6638 (*bed->elf_backend_init_index_section) (output_bfd, info);
6639
8423293d
AM
6640 if (elf_hash_table (info)->dynamic_sections_created)
6641 {
6642 bfd *dynobj;
8423293d
AM
6643 asection *s;
6644 bfd_size_type dynsymcount;
6645 unsigned long section_sym_count;
8423293d
AM
6646 unsigned int dtagcount;
6647
6648 dynobj = elf_hash_table (info)->dynobj;
6649
5a580b3a
AM
6650 /* Assign dynsym indicies. In a shared library we generate a
6651 section symbol for each output section, which come first.
6652 Next come all of the back-end allocated local dynamic syms,
6653 followed by the rest of the global symbols. */
6654
554220db
AM
6655 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6656 &section_sym_count);
5a580b3a
AM
6657
6658 /* Work out the size of the symbol version section. */
3d4d4302 6659 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6660 BFD_ASSERT (s != NULL);
d5486c43 6661 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6662 {
eea6121a 6663 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6664 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6665 if (s->contents == NULL)
6666 return FALSE;
6667
6668 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6669 return FALSE;
6670 }
6671
6672 /* Set the size of the .dynsym and .hash sections. We counted
6673 the number of dynamic symbols in elf_link_add_object_symbols.
6674 We will build the contents of .dynsym and .hash when we build
6675 the final symbol table, because until then we do not know the
6676 correct value to give the symbols. We built the .dynstr
6677 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6678 s = elf_hash_table (info)->dynsym;
5a580b3a 6679 BFD_ASSERT (s != NULL);
eea6121a 6680 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6681
d5486c43
L
6682 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6683 if (s->contents == NULL)
6684 return FALSE;
5a580b3a 6685
d5486c43
L
6686 /* The first entry in .dynsym is a dummy symbol. Clear all the
6687 section syms, in case we don't output them all. */
6688 ++section_sym_count;
6689 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6690
fdc90cb4
JJ
6691 elf_hash_table (info)->bucketcount = 0;
6692
5a580b3a
AM
6693 /* Compute the size of the hashing table. As a side effect this
6694 computes the hash values for all the names we export. */
fdc90cb4
JJ
6695 if (info->emit_hash)
6696 {
6697 unsigned long int *hashcodes;
14b1c01e 6698 struct hash_codes_info hashinf;
fdc90cb4
JJ
6699 bfd_size_type amt;
6700 unsigned long int nsyms;
6701 size_t bucketcount;
6702 size_t hash_entry_size;
6703
6704 /* Compute the hash values for all exported symbols. At the same
6705 time store the values in an array so that we could use them for
6706 optimizations. */
6707 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6708 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6709 if (hashcodes == NULL)
6710 return FALSE;
14b1c01e
AM
6711 hashinf.hashcodes = hashcodes;
6712 hashinf.error = FALSE;
5a580b3a 6713
fdc90cb4
JJ
6714 /* Put all hash values in HASHCODES. */
6715 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6716 elf_collect_hash_codes, &hashinf);
6717 if (hashinf.error)
4dd07732
AM
6718 {
6719 free (hashcodes);
6720 return FALSE;
6721 }
5a580b3a 6722
14b1c01e 6723 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6724 bucketcount
6725 = compute_bucket_count (info, hashcodes, nsyms, 0);
6726 free (hashcodes);
6727
6728 if (bucketcount == 0)
6729 return FALSE;
5a580b3a 6730
fdc90cb4
JJ
6731 elf_hash_table (info)->bucketcount = bucketcount;
6732
3d4d4302 6733 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6734 BFD_ASSERT (s != NULL);
6735 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6736 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6737 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6738 if (s->contents == NULL)
6739 return FALSE;
6740
6741 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6742 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6743 s->contents + hash_entry_size);
6744 }
6745
6746 if (info->emit_gnu_hash)
6747 {
6748 size_t i, cnt;
6749 unsigned char *contents;
6750 struct collect_gnu_hash_codes cinfo;
6751 bfd_size_type amt;
6752 size_t bucketcount;
6753
6754 memset (&cinfo, 0, sizeof (cinfo));
6755
6756 /* Compute the hash values for all exported symbols. At the same
6757 time store the values in an array so that we could use them for
6758 optimizations. */
6759 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6760 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6761 if (cinfo.hashcodes == NULL)
6762 return FALSE;
6763
6764 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6765 cinfo.min_dynindx = -1;
6766 cinfo.output_bfd = output_bfd;
6767 cinfo.bed = bed;
6768
6769 /* Put all hash values in HASHCODES. */
6770 elf_link_hash_traverse (elf_hash_table (info),
6771 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6772 if (cinfo.error)
4dd07732
AM
6773 {
6774 free (cinfo.hashcodes);
6775 return FALSE;
6776 }
fdc90cb4
JJ
6777
6778 bucketcount
6779 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6780
6781 if (bucketcount == 0)
6782 {
6783 free (cinfo.hashcodes);
6784 return FALSE;
6785 }
6786
3d4d4302 6787 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6788 BFD_ASSERT (s != NULL);
6789
6790 if (cinfo.nsyms == 0)
6791 {
6792 /* Empty .gnu.hash section is special. */
6793 BFD_ASSERT (cinfo.min_dynindx == -1);
6794 free (cinfo.hashcodes);
6795 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6796 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6797 if (contents == NULL)
6798 return FALSE;
6799 s->contents = contents;
6800 /* 1 empty bucket. */
6801 bfd_put_32 (output_bfd, 1, contents);
6802 /* SYMIDX above the special symbol 0. */
6803 bfd_put_32 (output_bfd, 1, contents + 4);
6804 /* Just one word for bitmask. */
6805 bfd_put_32 (output_bfd, 1, contents + 8);
6806 /* Only hash fn bloom filter. */
6807 bfd_put_32 (output_bfd, 0, contents + 12);
6808 /* No hashes are valid - empty bitmask. */
6809 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6810 /* No hashes in the only bucket. */
6811 bfd_put_32 (output_bfd, 0,
6812 contents + 16 + bed->s->arch_size / 8);
6813 }
6814 else
6815 {
9e6619e2 6816 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6817 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6818
9e6619e2
AM
6819 x = cinfo.nsyms;
6820 maskbitslog2 = 1;
6821 while ((x >>= 1) != 0)
6822 ++maskbitslog2;
fdc90cb4
JJ
6823 if (maskbitslog2 < 3)
6824 maskbitslog2 = 5;
6825 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6826 maskbitslog2 = maskbitslog2 + 3;
6827 else
6828 maskbitslog2 = maskbitslog2 + 2;
6829 if (bed->s->arch_size == 64)
6830 {
6831 if (maskbitslog2 == 5)
6832 maskbitslog2 = 6;
6833 cinfo.shift1 = 6;
6834 }
6835 else
6836 cinfo.shift1 = 5;
6837 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6838 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6839 cinfo.maskbits = 1 << maskbitslog2;
6840 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6841 amt = bucketcount * sizeof (unsigned long int) * 2;
6842 amt += maskwords * sizeof (bfd_vma);
a50b1753 6843 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6844 if (cinfo.bitmask == NULL)
6845 {
6846 free (cinfo.hashcodes);
6847 return FALSE;
6848 }
6849
a50b1753 6850 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6851 cinfo.indx = cinfo.counts + bucketcount;
6852 cinfo.symindx = dynsymcount - cinfo.nsyms;
6853 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6854
6855 /* Determine how often each hash bucket is used. */
6856 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6857 for (i = 0; i < cinfo.nsyms; ++i)
6858 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6859
6860 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6861 if (cinfo.counts[i] != 0)
6862 {
6863 cinfo.indx[i] = cnt;
6864 cnt += cinfo.counts[i];
6865 }
6866 BFD_ASSERT (cnt == dynsymcount);
6867 cinfo.bucketcount = bucketcount;
6868 cinfo.local_indx = cinfo.min_dynindx;
6869
6870 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6871 s->size += cinfo.maskbits / 8;
a50b1753 6872 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6873 if (contents == NULL)
6874 {
6875 free (cinfo.bitmask);
6876 free (cinfo.hashcodes);
6877 return FALSE;
6878 }
6879
6880 s->contents = contents;
6881 bfd_put_32 (output_bfd, bucketcount, contents);
6882 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6883 bfd_put_32 (output_bfd, maskwords, contents + 8);
6884 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6885 contents += 16 + cinfo.maskbits / 8;
6886
6887 for (i = 0; i < bucketcount; ++i)
6888 {
6889 if (cinfo.counts[i] == 0)
6890 bfd_put_32 (output_bfd, 0, contents);
6891 else
6892 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6893 contents += 4;
6894 }
6895
6896 cinfo.contents = contents;
6897
6898 /* Renumber dynamic symbols, populate .gnu.hash section. */
6899 elf_link_hash_traverse (elf_hash_table (info),
6900 elf_renumber_gnu_hash_syms, &cinfo);
6901
6902 contents = s->contents + 16;
6903 for (i = 0; i < maskwords; ++i)
6904 {
6905 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6906 contents);
6907 contents += bed->s->arch_size / 8;
6908 }
6909
6910 free (cinfo.bitmask);
6911 free (cinfo.hashcodes);
6912 }
6913 }
5a580b3a 6914
3d4d4302 6915 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6916 BFD_ASSERT (s != NULL);
6917
4ad4eba5 6918 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6919
eea6121a 6920 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6921
6922 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6923 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6924 return FALSE;
6925 }
6926
6927 return TRUE;
6928}
4d269e42 6929\f
4d269e42
AM
6930/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6931
6932static void
6933merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6934 asection *sec)
6935{
dbaa2011
AM
6936 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6937 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
6938}
6939
6940/* Finish SHF_MERGE section merging. */
6941
6942bfd_boolean
630993ec 6943_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
6944{
6945 bfd *ibfd;
6946 asection *sec;
6947
6948 if (!is_elf_hash_table (info->hash))
6949 return FALSE;
6950
c72f2fb2 6951 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
6952 if ((ibfd->flags & DYNAMIC) == 0
6953 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
6954 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6955 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
6956 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6957 if ((sec->flags & SEC_MERGE) != 0
6958 && !bfd_is_abs_section (sec->output_section))
6959 {
6960 struct bfd_elf_section_data *secdata;
6961
6962 secdata = elf_section_data (sec);
630993ec 6963 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
6964 &elf_hash_table (info)->merge_info,
6965 sec, &secdata->sec_info))
6966 return FALSE;
6967 else if (secdata->sec_info)
dbaa2011 6968 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
6969 }
6970
6971 if (elf_hash_table (info)->merge_info != NULL)
630993ec 6972 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
6973 merge_sections_remove_hook);
6974 return TRUE;
6975}
6976
6977/* Create an entry in an ELF linker hash table. */
6978
6979struct bfd_hash_entry *
6980_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6981 struct bfd_hash_table *table,
6982 const char *string)
6983{
6984 /* Allocate the structure if it has not already been allocated by a
6985 subclass. */
6986 if (entry == NULL)
6987 {
a50b1753 6988 entry = (struct bfd_hash_entry *)
ca4be51c 6989 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6990 if (entry == NULL)
6991 return entry;
6992 }
6993
6994 /* Call the allocation method of the superclass. */
6995 entry = _bfd_link_hash_newfunc (entry, table, string);
6996 if (entry != NULL)
6997 {
6998 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6999 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7000
7001 /* Set local fields. */
7002 ret->indx = -1;
7003 ret->dynindx = -1;
7004 ret->got = htab->init_got_refcount;
7005 ret->plt = htab->init_plt_refcount;
7006 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7007 - offsetof (struct elf_link_hash_entry, size)));
7008 /* Assume that we have been called by a non-ELF symbol reader.
7009 This flag is then reset by the code which reads an ELF input
7010 file. This ensures that a symbol created by a non-ELF symbol
7011 reader will have the flag set correctly. */
7012 ret->non_elf = 1;
7013 }
7014
7015 return entry;
7016}
7017
7018/* Copy data from an indirect symbol to its direct symbol, hiding the
7019 old indirect symbol. Also used for copying flags to a weakdef. */
7020
7021void
7022_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7023 struct elf_link_hash_entry *dir,
7024 struct elf_link_hash_entry *ind)
7025{
7026 struct elf_link_hash_table *htab;
7027
7028 /* Copy down any references that we may have already seen to the
6e33951e
L
7029 symbol which just became indirect if DIR isn't a hidden versioned
7030 symbol. */
4d269e42 7031
422f1182 7032 if (dir->versioned != versioned_hidden)
6e33951e
L
7033 {
7034 dir->ref_dynamic |= ind->ref_dynamic;
7035 dir->ref_regular |= ind->ref_regular;
7036 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7037 dir->non_got_ref |= ind->non_got_ref;
7038 dir->needs_plt |= ind->needs_plt;
7039 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7040 }
4d269e42
AM
7041
7042 if (ind->root.type != bfd_link_hash_indirect)
7043 return;
7044
7045 /* Copy over the global and procedure linkage table refcount entries.
7046 These may have been already set up by a check_relocs routine. */
7047 htab = elf_hash_table (info);
7048 if (ind->got.refcount > htab->init_got_refcount.refcount)
7049 {
7050 if (dir->got.refcount < 0)
7051 dir->got.refcount = 0;
7052 dir->got.refcount += ind->got.refcount;
7053 ind->got.refcount = htab->init_got_refcount.refcount;
7054 }
7055
7056 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7057 {
7058 if (dir->plt.refcount < 0)
7059 dir->plt.refcount = 0;
7060 dir->plt.refcount += ind->plt.refcount;
7061 ind->plt.refcount = htab->init_plt_refcount.refcount;
7062 }
7063
7064 if (ind->dynindx != -1)
7065 {
7066 if (dir->dynindx != -1)
7067 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7068 dir->dynindx = ind->dynindx;
7069 dir->dynstr_index = ind->dynstr_index;
7070 ind->dynindx = -1;
7071 ind->dynstr_index = 0;
7072 }
7073}
7074
7075void
7076_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7077 struct elf_link_hash_entry *h,
7078 bfd_boolean force_local)
7079{
3aa14d16
L
7080 /* STT_GNU_IFUNC symbol must go through PLT. */
7081 if (h->type != STT_GNU_IFUNC)
7082 {
7083 h->plt = elf_hash_table (info)->init_plt_offset;
7084 h->needs_plt = 0;
7085 }
4d269e42
AM
7086 if (force_local)
7087 {
7088 h->forced_local = 1;
7089 if (h->dynindx != -1)
7090 {
7091 h->dynindx = -1;
7092 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7093 h->dynstr_index);
7094 }
7095 }
7096}
7097
7bf52ea2
AM
7098/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7099 caller. */
4d269e42
AM
7100
7101bfd_boolean
7102_bfd_elf_link_hash_table_init
7103 (struct elf_link_hash_table *table,
7104 bfd *abfd,
7105 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7106 struct bfd_hash_table *,
7107 const char *),
4dfe6ac6
NC
7108 unsigned int entsize,
7109 enum elf_target_id target_id)
4d269e42
AM
7110{
7111 bfd_boolean ret;
7112 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7113
4d269e42
AM
7114 table->init_got_refcount.refcount = can_refcount - 1;
7115 table->init_plt_refcount.refcount = can_refcount - 1;
7116 table->init_got_offset.offset = -(bfd_vma) 1;
7117 table->init_plt_offset.offset = -(bfd_vma) 1;
7118 /* The first dynamic symbol is a dummy. */
7119 table->dynsymcount = 1;
7120
7121 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7122
4d269e42 7123 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7124 table->hash_table_id = target_id;
4d269e42
AM
7125
7126 return ret;
7127}
7128
7129/* Create an ELF linker hash table. */
7130
7131struct bfd_link_hash_table *
7132_bfd_elf_link_hash_table_create (bfd *abfd)
7133{
7134 struct elf_link_hash_table *ret;
7135 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7136
7bf52ea2 7137 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7138 if (ret == NULL)
7139 return NULL;
7140
7141 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7142 sizeof (struct elf_link_hash_entry),
7143 GENERIC_ELF_DATA))
4d269e42
AM
7144 {
7145 free (ret);
7146 return NULL;
7147 }
d495ab0d 7148 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7149
7150 return &ret->root;
7151}
7152
9f7c3e5e
AM
7153/* Destroy an ELF linker hash table. */
7154
7155void
d495ab0d 7156_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7157{
d495ab0d
AM
7158 struct elf_link_hash_table *htab;
7159
7160 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7161 if (htab->dynstr != NULL)
7162 _bfd_elf_strtab_free (htab->dynstr);
7163 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7164 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7165}
7166
4d269e42
AM
7167/* This is a hook for the ELF emulation code in the generic linker to
7168 tell the backend linker what file name to use for the DT_NEEDED
7169 entry for a dynamic object. */
7170
7171void
7172bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7173{
7174 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7175 && bfd_get_format (abfd) == bfd_object)
7176 elf_dt_name (abfd) = name;
7177}
7178
7179int
7180bfd_elf_get_dyn_lib_class (bfd *abfd)
7181{
7182 int lib_class;
7183 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7184 && bfd_get_format (abfd) == bfd_object)
7185 lib_class = elf_dyn_lib_class (abfd);
7186 else
7187 lib_class = 0;
7188 return lib_class;
7189}
7190
7191void
7192bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7193{
7194 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7195 && bfd_get_format (abfd) == bfd_object)
7196 elf_dyn_lib_class (abfd) = lib_class;
7197}
7198
7199/* Get the list of DT_NEEDED entries for a link. This is a hook for
7200 the linker ELF emulation code. */
7201
7202struct bfd_link_needed_list *
7203bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7204 struct bfd_link_info *info)
7205{
7206 if (! is_elf_hash_table (info->hash))
7207 return NULL;
7208 return elf_hash_table (info)->needed;
7209}
7210
7211/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7212 hook for the linker ELF emulation code. */
7213
7214struct bfd_link_needed_list *
7215bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7216 struct bfd_link_info *info)
7217{
7218 if (! is_elf_hash_table (info->hash))
7219 return NULL;
7220 return elf_hash_table (info)->runpath;
7221}
7222
7223/* Get the name actually used for a dynamic object for a link. This
7224 is the SONAME entry if there is one. Otherwise, it is the string
7225 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7226
7227const char *
7228bfd_elf_get_dt_soname (bfd *abfd)
7229{
7230 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7231 && bfd_get_format (abfd) == bfd_object)
7232 return elf_dt_name (abfd);
7233 return NULL;
7234}
7235
7236/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7237 the ELF linker emulation code. */
7238
7239bfd_boolean
7240bfd_elf_get_bfd_needed_list (bfd *abfd,
7241 struct bfd_link_needed_list **pneeded)
7242{
7243 asection *s;
7244 bfd_byte *dynbuf = NULL;
cb33740c 7245 unsigned int elfsec;
4d269e42
AM
7246 unsigned long shlink;
7247 bfd_byte *extdyn, *extdynend;
7248 size_t extdynsize;
7249 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7250
7251 *pneeded = NULL;
7252
7253 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7254 || bfd_get_format (abfd) != bfd_object)
7255 return TRUE;
7256
7257 s = bfd_get_section_by_name (abfd, ".dynamic");
7258 if (s == NULL || s->size == 0)
7259 return TRUE;
7260
7261 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7262 goto error_return;
7263
7264 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7265 if (elfsec == SHN_BAD)
4d269e42
AM
7266 goto error_return;
7267
7268 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7269
4d269e42
AM
7270 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7271 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7272
7273 extdyn = dynbuf;
7274 extdynend = extdyn + s->size;
7275 for (; extdyn < extdynend; extdyn += extdynsize)
7276 {
7277 Elf_Internal_Dyn dyn;
7278
7279 (*swap_dyn_in) (abfd, extdyn, &dyn);
7280
7281 if (dyn.d_tag == DT_NULL)
7282 break;
7283
7284 if (dyn.d_tag == DT_NEEDED)
7285 {
7286 const char *string;
7287 struct bfd_link_needed_list *l;
7288 unsigned int tagv = dyn.d_un.d_val;
7289 bfd_size_type amt;
7290
7291 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7292 if (string == NULL)
7293 goto error_return;
7294
7295 amt = sizeof *l;
a50b1753 7296 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7297 if (l == NULL)
7298 goto error_return;
7299
7300 l->by = abfd;
7301 l->name = string;
7302 l->next = *pneeded;
7303 *pneeded = l;
7304 }
7305 }
7306
7307 free (dynbuf);
7308
7309 return TRUE;
7310
7311 error_return:
7312 if (dynbuf != NULL)
7313 free (dynbuf);
7314 return FALSE;
7315}
7316
7317struct elf_symbuf_symbol
7318{
7319 unsigned long st_name; /* Symbol name, index in string tbl */
7320 unsigned char st_info; /* Type and binding attributes */
7321 unsigned char st_other; /* Visibilty, and target specific */
7322};
7323
7324struct elf_symbuf_head
7325{
7326 struct elf_symbuf_symbol *ssym;
ef53be89 7327 size_t count;
4d269e42
AM
7328 unsigned int st_shndx;
7329};
7330
7331struct elf_symbol
7332{
7333 union
7334 {
7335 Elf_Internal_Sym *isym;
7336 struct elf_symbuf_symbol *ssym;
7337 } u;
7338 const char *name;
7339};
7340
7341/* Sort references to symbols by ascending section number. */
7342
7343static int
7344elf_sort_elf_symbol (const void *arg1, const void *arg2)
7345{
7346 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7347 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7348
7349 return s1->st_shndx - s2->st_shndx;
7350}
7351
7352static int
7353elf_sym_name_compare (const void *arg1, const void *arg2)
7354{
7355 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7356 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7357 return strcmp (s1->name, s2->name);
7358}
7359
7360static struct elf_symbuf_head *
ef53be89 7361elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7362{
14b1c01e 7363 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7364 struct elf_symbuf_symbol *ssym;
7365 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7366 size_t i, shndx_count, total_size;
4d269e42 7367
a50b1753 7368 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7369 if (indbuf == NULL)
7370 return NULL;
7371
7372 for (ind = indbuf, i = 0; i < symcount; i++)
7373 if (isymbuf[i].st_shndx != SHN_UNDEF)
7374 *ind++ = &isymbuf[i];
7375 indbufend = ind;
7376
7377 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7378 elf_sort_elf_symbol);
7379
7380 shndx_count = 0;
7381 if (indbufend > indbuf)
7382 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7383 if (ind[0]->st_shndx != ind[1]->st_shndx)
7384 shndx_count++;
7385
3ae181ee
L
7386 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7387 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7388 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7389 if (ssymbuf == NULL)
7390 {
7391 free (indbuf);
7392 return NULL;
7393 }
7394
3ae181ee 7395 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7396 ssymbuf->ssym = NULL;
7397 ssymbuf->count = shndx_count;
7398 ssymbuf->st_shndx = 0;
7399 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7400 {
7401 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7402 {
7403 ssymhead++;
7404 ssymhead->ssym = ssym;
7405 ssymhead->count = 0;
7406 ssymhead->st_shndx = (*ind)->st_shndx;
7407 }
7408 ssym->st_name = (*ind)->st_name;
7409 ssym->st_info = (*ind)->st_info;
7410 ssym->st_other = (*ind)->st_other;
7411 ssymhead->count++;
7412 }
ef53be89 7413 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7414 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7415 == total_size));
4d269e42
AM
7416
7417 free (indbuf);
7418 return ssymbuf;
7419}
7420
7421/* Check if 2 sections define the same set of local and global
7422 symbols. */
7423
8f317e31 7424static bfd_boolean
4d269e42
AM
7425bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7426 struct bfd_link_info *info)
7427{
7428 bfd *bfd1, *bfd2;
7429 const struct elf_backend_data *bed1, *bed2;
7430 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7431 size_t symcount1, symcount2;
4d269e42
AM
7432 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7433 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7434 Elf_Internal_Sym *isym, *isymend;
7435 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7436 size_t count1, count2, i;
cb33740c 7437 unsigned int shndx1, shndx2;
4d269e42
AM
7438 bfd_boolean result;
7439
7440 bfd1 = sec1->owner;
7441 bfd2 = sec2->owner;
7442
4d269e42
AM
7443 /* Both sections have to be in ELF. */
7444 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7445 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7446 return FALSE;
7447
7448 if (elf_section_type (sec1) != elf_section_type (sec2))
7449 return FALSE;
7450
4d269e42
AM
7451 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7452 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7453 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7454 return FALSE;
7455
7456 bed1 = get_elf_backend_data (bfd1);
7457 bed2 = get_elf_backend_data (bfd2);
7458 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7459 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7460 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7461 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7462
7463 if (symcount1 == 0 || symcount2 == 0)
7464 return FALSE;
7465
7466 result = FALSE;
7467 isymbuf1 = NULL;
7468 isymbuf2 = NULL;
a50b1753
NC
7469 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7470 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7471
7472 if (ssymbuf1 == NULL)
7473 {
7474 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7475 NULL, NULL, NULL);
7476 if (isymbuf1 == NULL)
7477 goto done;
7478
7479 if (!info->reduce_memory_overheads)
7480 elf_tdata (bfd1)->symbuf = ssymbuf1
7481 = elf_create_symbuf (symcount1, isymbuf1);
7482 }
7483
7484 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7485 {
7486 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7487 NULL, NULL, NULL);
7488 if (isymbuf2 == NULL)
7489 goto done;
7490
7491 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7492 elf_tdata (bfd2)->symbuf = ssymbuf2
7493 = elf_create_symbuf (symcount2, isymbuf2);
7494 }
7495
7496 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7497 {
7498 /* Optimized faster version. */
ef53be89 7499 size_t lo, hi, mid;
4d269e42
AM
7500 struct elf_symbol *symp;
7501 struct elf_symbuf_symbol *ssym, *ssymend;
7502
7503 lo = 0;
7504 hi = ssymbuf1->count;
7505 ssymbuf1++;
7506 count1 = 0;
7507 while (lo < hi)
7508 {
7509 mid = (lo + hi) / 2;
cb33740c 7510 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7511 hi = mid;
cb33740c 7512 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7513 lo = mid + 1;
7514 else
7515 {
7516 count1 = ssymbuf1[mid].count;
7517 ssymbuf1 += mid;
7518 break;
7519 }
7520 }
7521
7522 lo = 0;
7523 hi = ssymbuf2->count;
7524 ssymbuf2++;
7525 count2 = 0;
7526 while (lo < hi)
7527 {
7528 mid = (lo + hi) / 2;
cb33740c 7529 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7530 hi = mid;
cb33740c 7531 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7532 lo = mid + 1;
7533 else
7534 {
7535 count2 = ssymbuf2[mid].count;
7536 ssymbuf2 += mid;
7537 break;
7538 }
7539 }
7540
7541 if (count1 == 0 || count2 == 0 || count1 != count2)
7542 goto done;
7543
ca4be51c
AM
7544 symtable1
7545 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7546 symtable2
7547 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7548 if (symtable1 == NULL || symtable2 == NULL)
7549 goto done;
7550
7551 symp = symtable1;
7552 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7553 ssym < ssymend; ssym++, symp++)
7554 {
7555 symp->u.ssym = ssym;
7556 symp->name = bfd_elf_string_from_elf_section (bfd1,
7557 hdr1->sh_link,
7558 ssym->st_name);
7559 }
7560
7561 symp = symtable2;
7562 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7563 ssym < ssymend; ssym++, symp++)
7564 {
7565 symp->u.ssym = ssym;
7566 symp->name = bfd_elf_string_from_elf_section (bfd2,
7567 hdr2->sh_link,
7568 ssym->st_name);
7569 }
7570
7571 /* Sort symbol by name. */
7572 qsort (symtable1, count1, sizeof (struct elf_symbol),
7573 elf_sym_name_compare);
7574 qsort (symtable2, count1, sizeof (struct elf_symbol),
7575 elf_sym_name_compare);
7576
7577 for (i = 0; i < count1; i++)
7578 /* Two symbols must have the same binding, type and name. */
7579 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7580 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7581 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7582 goto done;
7583
7584 result = TRUE;
7585 goto done;
7586 }
7587
a50b1753
NC
7588 symtable1 = (struct elf_symbol *)
7589 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7590 symtable2 = (struct elf_symbol *)
7591 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7592 if (symtable1 == NULL || symtable2 == NULL)
7593 goto done;
7594
7595 /* Count definitions in the section. */
7596 count1 = 0;
7597 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7598 if (isym->st_shndx == shndx1)
4d269e42
AM
7599 symtable1[count1++].u.isym = isym;
7600
7601 count2 = 0;
7602 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7603 if (isym->st_shndx == shndx2)
4d269e42
AM
7604 symtable2[count2++].u.isym = isym;
7605
7606 if (count1 == 0 || count2 == 0 || count1 != count2)
7607 goto done;
7608
7609 for (i = 0; i < count1; i++)
7610 symtable1[i].name
7611 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7612 symtable1[i].u.isym->st_name);
7613
7614 for (i = 0; i < count2; i++)
7615 symtable2[i].name
7616 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7617 symtable2[i].u.isym->st_name);
7618
7619 /* Sort symbol by name. */
7620 qsort (symtable1, count1, sizeof (struct elf_symbol),
7621 elf_sym_name_compare);
7622 qsort (symtable2, count1, sizeof (struct elf_symbol),
7623 elf_sym_name_compare);
7624
7625 for (i = 0; i < count1; i++)
7626 /* Two symbols must have the same binding, type and name. */
7627 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7628 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7629 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7630 goto done;
7631
7632 result = TRUE;
7633
7634done:
7635 if (symtable1)
7636 free (symtable1);
7637 if (symtable2)
7638 free (symtable2);
7639 if (isymbuf1)
7640 free (isymbuf1);
7641 if (isymbuf2)
7642 free (isymbuf2);
7643
7644 return result;
7645}
7646
7647/* Return TRUE if 2 section types are compatible. */
7648
7649bfd_boolean
7650_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7651 bfd *bbfd, const asection *bsec)
7652{
7653 if (asec == NULL
7654 || bsec == NULL
7655 || abfd->xvec->flavour != bfd_target_elf_flavour
7656 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7657 return TRUE;
7658
7659 return elf_section_type (asec) == elf_section_type (bsec);
7660}
7661\f
c152c796
AM
7662/* Final phase of ELF linker. */
7663
7664/* A structure we use to avoid passing large numbers of arguments. */
7665
7666struct elf_final_link_info
7667{
7668 /* General link information. */
7669 struct bfd_link_info *info;
7670 /* Output BFD. */
7671 bfd *output_bfd;
7672 /* Symbol string table. */
ef10c3ac 7673 struct elf_strtab_hash *symstrtab;
c152c796
AM
7674 /* .hash section. */
7675 asection *hash_sec;
7676 /* symbol version section (.gnu.version). */
7677 asection *symver_sec;
7678 /* Buffer large enough to hold contents of any section. */
7679 bfd_byte *contents;
7680 /* Buffer large enough to hold external relocs of any section. */
7681 void *external_relocs;
7682 /* Buffer large enough to hold internal relocs of any section. */
7683 Elf_Internal_Rela *internal_relocs;
7684 /* Buffer large enough to hold external local symbols of any input
7685 BFD. */
7686 bfd_byte *external_syms;
7687 /* And a buffer for symbol section indices. */
7688 Elf_External_Sym_Shndx *locsym_shndx;
7689 /* Buffer large enough to hold internal local symbols of any input
7690 BFD. */
7691 Elf_Internal_Sym *internal_syms;
7692 /* Array large enough to hold a symbol index for each local symbol
7693 of any input BFD. */
7694 long *indices;
7695 /* Array large enough to hold a section pointer for each local
7696 symbol of any input BFD. */
7697 asection **sections;
ef10c3ac 7698 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7699 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7700 /* Number of STT_FILE syms seen. */
7701 size_t filesym_count;
c152c796
AM
7702};
7703
7704/* This struct is used to pass information to elf_link_output_extsym. */
7705
7706struct elf_outext_info
7707{
7708 bfd_boolean failed;
7709 bfd_boolean localsyms;
34a79995 7710 bfd_boolean file_sym_done;
8b127cbc 7711 struct elf_final_link_info *flinfo;
c152c796
AM
7712};
7713
d9352518
DB
7714
7715/* Support for evaluating a complex relocation.
7716
7717 Complex relocations are generalized, self-describing relocations. The
7718 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7719 relocations themselves.
d9352518
DB
7720
7721 The relocations are use a reserved elf-wide relocation type code (R_RELC
7722 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7723 information (start bit, end bit, word width, etc) into the addend. This
7724 information is extracted from CGEN-generated operand tables within gas.
7725
7726 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7727 internal) representing prefix-notation expressions, including but not
7728 limited to those sorts of expressions normally encoded as addends in the
7729 addend field. The symbol mangling format is:
7730
7731 <node> := <literal>
7732 | <unary-operator> ':' <node>
7733 | <binary-operator> ':' <node> ':' <node>
7734 ;
7735
7736 <literal> := 's' <digits=N> ':' <N character symbol name>
7737 | 'S' <digits=N> ':' <N character section name>
7738 | '#' <hexdigits>
7739 ;
7740
7741 <binary-operator> := as in C
7742 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7743
7744static void
a0c8462f
AM
7745set_symbol_value (bfd *bfd_with_globals,
7746 Elf_Internal_Sym *isymbuf,
7747 size_t locsymcount,
7748 size_t symidx,
7749 bfd_vma val)
d9352518 7750{
8977835c
AM
7751 struct elf_link_hash_entry **sym_hashes;
7752 struct elf_link_hash_entry *h;
7753 size_t extsymoff = locsymcount;
d9352518 7754
8977835c 7755 if (symidx < locsymcount)
d9352518 7756 {
8977835c
AM
7757 Elf_Internal_Sym *sym;
7758
7759 sym = isymbuf + symidx;
7760 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7761 {
7762 /* It is a local symbol: move it to the
7763 "absolute" section and give it a value. */
7764 sym->st_shndx = SHN_ABS;
7765 sym->st_value = val;
7766 return;
7767 }
7768 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7769 extsymoff = 0;
d9352518 7770 }
8977835c
AM
7771
7772 /* It is a global symbol: set its link type
7773 to "defined" and give it a value. */
7774
7775 sym_hashes = elf_sym_hashes (bfd_with_globals);
7776 h = sym_hashes [symidx - extsymoff];
7777 while (h->root.type == bfd_link_hash_indirect
7778 || h->root.type == bfd_link_hash_warning)
7779 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7780 h->root.type = bfd_link_hash_defined;
7781 h->root.u.def.value = val;
7782 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7783}
7784
a0c8462f
AM
7785static bfd_boolean
7786resolve_symbol (const char *name,
7787 bfd *input_bfd,
8b127cbc 7788 struct elf_final_link_info *flinfo,
a0c8462f
AM
7789 bfd_vma *result,
7790 Elf_Internal_Sym *isymbuf,
7791 size_t locsymcount)
d9352518 7792{
a0c8462f
AM
7793 Elf_Internal_Sym *sym;
7794 struct bfd_link_hash_entry *global_entry;
7795 const char *candidate = NULL;
7796 Elf_Internal_Shdr *symtab_hdr;
7797 size_t i;
7798
d9352518
DB
7799 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7800
7801 for (i = 0; i < locsymcount; ++ i)
7802 {
8977835c 7803 sym = isymbuf + i;
d9352518
DB
7804
7805 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7806 continue;
7807
7808 candidate = bfd_elf_string_from_elf_section (input_bfd,
7809 symtab_hdr->sh_link,
7810 sym->st_name);
7811#ifdef DEBUG
0f02bbd9
AM
7812 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7813 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7814#endif
7815 if (candidate && strcmp (candidate, name) == 0)
7816 {
8b127cbc 7817 asection *sec = flinfo->sections [i];
d9352518 7818
0f02bbd9
AM
7819 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7820 *result += sec->output_offset + sec->output_section->vma;
d9352518 7821#ifdef DEBUG
0f02bbd9
AM
7822 printf ("Found symbol with value %8.8lx\n",
7823 (unsigned long) *result);
d9352518
DB
7824#endif
7825 return TRUE;
7826 }
7827 }
7828
7829 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7830 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7831 FALSE, FALSE, TRUE);
d9352518
DB
7832 if (!global_entry)
7833 return FALSE;
a0c8462f 7834
d9352518
DB
7835 if (global_entry->type == bfd_link_hash_defined
7836 || global_entry->type == bfd_link_hash_defweak)
7837 {
a0c8462f
AM
7838 *result = (global_entry->u.def.value
7839 + global_entry->u.def.section->output_section->vma
7840 + global_entry->u.def.section->output_offset);
d9352518 7841#ifdef DEBUG
0f02bbd9
AM
7842 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7843 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7844#endif
7845 return TRUE;
a0c8462f 7846 }
d9352518 7847
d9352518
DB
7848 return FALSE;
7849}
7850
37b01f6a
DG
7851/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7852 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7853 names like "foo.end" which is the end address of section "foo". */
7854
d9352518 7855static bfd_boolean
a0c8462f
AM
7856resolve_section (const char *name,
7857 asection *sections,
37b01f6a
DG
7858 bfd_vma *result,
7859 bfd * abfd)
d9352518 7860{
a0c8462f
AM
7861 asection *curr;
7862 unsigned int len;
d9352518 7863
a0c8462f 7864 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7865 if (strcmp (curr->name, name) == 0)
7866 {
7867 *result = curr->vma;
7868 return TRUE;
7869 }
7870
7871 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 7872 /* FIXME: This could be coded more efficiently... */
a0c8462f 7873 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7874 {
7875 len = strlen (curr->name);
a0c8462f 7876 if (len > strlen (name))
d9352518
DB
7877 continue;
7878
7879 if (strncmp (curr->name, name, len) == 0)
7880 {
7881 if (strncmp (".end", name + len, 4) == 0)
7882 {
37b01f6a 7883 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
7884 return TRUE;
7885 }
7886
7887 /* Insert more pseudo-section names here, if you like. */
7888 }
7889 }
a0c8462f 7890
d9352518
DB
7891 return FALSE;
7892}
7893
7894static void
a0c8462f 7895undefined_reference (const char *reftype, const char *name)
d9352518 7896{
a0c8462f
AM
7897 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7898 reftype, name);
d9352518
DB
7899}
7900
7901static bfd_boolean
a0c8462f
AM
7902eval_symbol (bfd_vma *result,
7903 const char **symp,
7904 bfd *input_bfd,
8b127cbc 7905 struct elf_final_link_info *flinfo,
a0c8462f
AM
7906 bfd_vma dot,
7907 Elf_Internal_Sym *isymbuf,
7908 size_t locsymcount,
7909 int signed_p)
d9352518 7910{
4b93929b
NC
7911 size_t len;
7912 size_t symlen;
a0c8462f
AM
7913 bfd_vma a;
7914 bfd_vma b;
4b93929b 7915 char symbuf[4096];
0f02bbd9 7916 const char *sym = *symp;
a0c8462f
AM
7917 const char *symend;
7918 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7919
7920 len = strlen (sym);
7921 symend = sym + len;
7922
4b93929b 7923 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7924 {
7925 bfd_set_error (bfd_error_invalid_operation);
7926 return FALSE;
7927 }
a0c8462f 7928
d9352518
DB
7929 switch (* sym)
7930 {
7931 case '.':
0f02bbd9
AM
7932 *result = dot;
7933 *symp = sym + 1;
d9352518
DB
7934 return TRUE;
7935
7936 case '#':
0f02bbd9
AM
7937 ++sym;
7938 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7939 return TRUE;
7940
7941 case 'S':
7942 symbol_is_section = TRUE;
a0c8462f 7943 case 's':
0f02bbd9
AM
7944 ++sym;
7945 symlen = strtol (sym, (char **) symp, 10);
7946 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7947
4b93929b 7948 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7949 {
7950 bfd_set_error (bfd_error_invalid_operation);
7951 return FALSE;
7952 }
7953
7954 memcpy (symbuf, sym, symlen);
a0c8462f 7955 symbuf[symlen] = '\0';
0f02bbd9 7956 *symp = sym + symlen;
a0c8462f
AM
7957
7958 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7959 the symbol as a section, or vice-versa. so we're pretty liberal in our
7960 interpretation here; section means "try section first", not "must be a
7961 section", and likewise with symbol. */
7962
a0c8462f 7963 if (symbol_is_section)
d9352518 7964 {
37b01f6a 7965 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 7966 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7967 isymbuf, locsymcount))
d9352518
DB
7968 {
7969 undefined_reference ("section", symbuf);
7970 return FALSE;
7971 }
a0c8462f
AM
7972 }
7973 else
d9352518 7974 {
8b127cbc 7975 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7976 isymbuf, locsymcount)
8b127cbc 7977 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 7978 result, input_bfd))
d9352518
DB
7979 {
7980 undefined_reference ("symbol", symbuf);
7981 return FALSE;
7982 }
7983 }
7984
7985 return TRUE;
a0c8462f 7986
d9352518
DB
7987 /* All that remains are operators. */
7988
7989#define UNARY_OP(op) \
7990 if (strncmp (sym, #op, strlen (#op)) == 0) \
7991 { \
7992 sym += strlen (#op); \
a0c8462f
AM
7993 if (*sym == ':') \
7994 ++sym; \
0f02bbd9 7995 *symp = sym; \
8b127cbc 7996 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7997 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7998 return FALSE; \
7999 if (signed_p) \
0f02bbd9 8000 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8001 else \
8002 *result = op a; \
d9352518
DB
8003 return TRUE; \
8004 }
8005
8006#define BINARY_OP(op) \
8007 if (strncmp (sym, #op, strlen (#op)) == 0) \
8008 { \
8009 sym += strlen (#op); \
a0c8462f
AM
8010 if (*sym == ':') \
8011 ++sym; \
0f02bbd9 8012 *symp = sym; \
8b127cbc 8013 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8014 isymbuf, locsymcount, signed_p)) \
a0c8462f 8015 return FALSE; \
0f02bbd9 8016 ++*symp; \
8b127cbc 8017 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8018 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8019 return FALSE; \
8020 if (signed_p) \
0f02bbd9 8021 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8022 else \
8023 *result = a op b; \
d9352518
DB
8024 return TRUE; \
8025 }
8026
8027 default:
8028 UNARY_OP (0-);
8029 BINARY_OP (<<);
8030 BINARY_OP (>>);
8031 BINARY_OP (==);
8032 BINARY_OP (!=);
8033 BINARY_OP (<=);
8034 BINARY_OP (>=);
8035 BINARY_OP (&&);
8036 BINARY_OP (||);
8037 UNARY_OP (~);
8038 UNARY_OP (!);
8039 BINARY_OP (*);
8040 BINARY_OP (/);
8041 BINARY_OP (%);
8042 BINARY_OP (^);
8043 BINARY_OP (|);
8044 BINARY_OP (&);
8045 BINARY_OP (+);
8046 BINARY_OP (-);
8047 BINARY_OP (<);
8048 BINARY_OP (>);
8049#undef UNARY_OP
8050#undef BINARY_OP
8051 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8052 bfd_set_error (bfd_error_invalid_operation);
8053 return FALSE;
8054 }
8055}
8056
d9352518 8057static void
a0c8462f
AM
8058put_value (bfd_vma size,
8059 unsigned long chunksz,
8060 bfd *input_bfd,
8061 bfd_vma x,
8062 bfd_byte *location)
d9352518
DB
8063{
8064 location += (size - chunksz);
8065
41cd1ad1 8066 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8067 {
8068 switch (chunksz)
8069 {
d9352518
DB
8070 case 1:
8071 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8072 x >>= 8;
d9352518
DB
8073 break;
8074 case 2:
8075 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8076 x >>= 16;
d9352518
DB
8077 break;
8078 case 4:
8079 bfd_put_32 (input_bfd, x, location);
65164438
NC
8080 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8081 x >>= 16;
8082 x >>= 16;
d9352518 8083 break;
d9352518 8084#ifdef BFD64
41cd1ad1 8085 case 8:
d9352518 8086 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8087 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8088 x >>= 32;
8089 x >>= 32;
8090 break;
d9352518 8091#endif
41cd1ad1
NC
8092 default:
8093 abort ();
d9352518
DB
8094 break;
8095 }
8096 }
8097}
8098
a0c8462f
AM
8099static bfd_vma
8100get_value (bfd_vma size,
8101 unsigned long chunksz,
8102 bfd *input_bfd,
8103 bfd_byte *location)
d9352518 8104{
9b239e0e 8105 int shift;
d9352518
DB
8106 bfd_vma x = 0;
8107
9b239e0e
NC
8108 /* Sanity checks. */
8109 BFD_ASSERT (chunksz <= sizeof (x)
8110 && size >= chunksz
8111 && chunksz != 0
8112 && (size % chunksz) == 0
8113 && input_bfd != NULL
8114 && location != NULL);
8115
8116 if (chunksz == sizeof (x))
8117 {
8118 BFD_ASSERT (size == chunksz);
8119
8120 /* Make sure that we do not perform an undefined shift operation.
8121 We know that size == chunksz so there will only be one iteration
8122 of the loop below. */
8123 shift = 0;
8124 }
8125 else
8126 shift = 8 * chunksz;
8127
a0c8462f 8128 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8129 {
8130 switch (chunksz)
8131 {
d9352518 8132 case 1:
9b239e0e 8133 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8134 break;
8135 case 2:
9b239e0e 8136 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8137 break;
8138 case 4:
9b239e0e 8139 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8140 break;
d9352518 8141#ifdef BFD64
9b239e0e
NC
8142 case 8:
8143 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8144 break;
9b239e0e
NC
8145#endif
8146 default:
8147 abort ();
d9352518
DB
8148 }
8149 }
8150 return x;
8151}
8152
a0c8462f
AM
8153static void
8154decode_complex_addend (unsigned long *start, /* in bits */
8155 unsigned long *oplen, /* in bits */
8156 unsigned long *len, /* in bits */
8157 unsigned long *wordsz, /* in bytes */
8158 unsigned long *chunksz, /* in bytes */
8159 unsigned long *lsb0_p,
8160 unsigned long *signed_p,
8161 unsigned long *trunc_p,
8162 unsigned long encoded)
d9352518
DB
8163{
8164 * start = encoded & 0x3F;
8165 * len = (encoded >> 6) & 0x3F;
8166 * oplen = (encoded >> 12) & 0x3F;
8167 * wordsz = (encoded >> 18) & 0xF;
8168 * chunksz = (encoded >> 22) & 0xF;
8169 * lsb0_p = (encoded >> 27) & 1;
8170 * signed_p = (encoded >> 28) & 1;
8171 * trunc_p = (encoded >> 29) & 1;
8172}
8173
cdfeee4f 8174bfd_reloc_status_type
0f02bbd9 8175bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8176 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8177 bfd_byte *contents,
8178 Elf_Internal_Rela *rel,
8179 bfd_vma relocation)
d9352518 8180{
0f02bbd9
AM
8181 bfd_vma shift, x, mask;
8182 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8183 bfd_reloc_status_type r;
d9352518
DB
8184
8185 /* Perform this reloc, since it is complex.
8186 (this is not to say that it necessarily refers to a complex
8187 symbol; merely that it is a self-describing CGEN based reloc.
8188 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8189 word size, etc) encoded within it.). */
d9352518 8190
a0c8462f
AM
8191 decode_complex_addend (&start, &oplen, &len, &wordsz,
8192 &chunksz, &lsb0_p, &signed_p,
8193 &trunc_p, rel->r_addend);
d9352518
DB
8194
8195 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8196
8197 if (lsb0_p)
8198 shift = (start + 1) - len;
8199 else
8200 shift = (8 * wordsz) - (start + len);
8201
37b01f6a
DG
8202 x = get_value (wordsz, chunksz, input_bfd,
8203 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8204
8205#ifdef DEBUG
8206 printf ("Doing complex reloc: "
8207 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8208 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8209 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8210 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8211 oplen, (unsigned long) x, (unsigned long) mask,
8212 (unsigned long) relocation);
d9352518
DB
8213#endif
8214
cdfeee4f 8215 r = bfd_reloc_ok;
d9352518 8216 if (! trunc_p)
cdfeee4f
AM
8217 /* Now do an overflow check. */
8218 r = bfd_check_overflow ((signed_p
8219 ? complain_overflow_signed
8220 : complain_overflow_unsigned),
8221 len, 0, (8 * wordsz),
8222 relocation);
a0c8462f 8223
d9352518
DB
8224 /* Do the deed. */
8225 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8226
8227#ifdef DEBUG
8228 printf (" relocation: %8.8lx\n"
8229 " shifted mask: %8.8lx\n"
8230 " shifted/masked reloc: %8.8lx\n"
8231 " result: %8.8lx\n",
9ccb8af9
AM
8232 (unsigned long) relocation, (unsigned long) (mask << shift),
8233 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8234#endif
37b01f6a
DG
8235 put_value (wordsz, chunksz, input_bfd, x,
8236 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8237 return r;
d9352518
DB
8238}
8239
0e287786
AM
8240/* Functions to read r_offset from external (target order) reloc
8241 entry. Faster than bfd_getl32 et al, because we let the compiler
8242 know the value is aligned. */
53df40a4 8243
0e287786
AM
8244static bfd_vma
8245ext32l_r_offset (const void *p)
53df40a4
AM
8246{
8247 union aligned32
8248 {
8249 uint32_t v;
8250 unsigned char c[4];
8251 };
8252 const union aligned32 *a
0e287786 8253 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8254
8255 uint32_t aval = ( (uint32_t) a->c[0]
8256 | (uint32_t) a->c[1] << 8
8257 | (uint32_t) a->c[2] << 16
8258 | (uint32_t) a->c[3] << 24);
0e287786 8259 return aval;
53df40a4
AM
8260}
8261
0e287786
AM
8262static bfd_vma
8263ext32b_r_offset (const void *p)
53df40a4
AM
8264{
8265 union aligned32
8266 {
8267 uint32_t v;
8268 unsigned char c[4];
8269 };
8270 const union aligned32 *a
0e287786 8271 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8272
8273 uint32_t aval = ( (uint32_t) a->c[0] << 24
8274 | (uint32_t) a->c[1] << 16
8275 | (uint32_t) a->c[2] << 8
8276 | (uint32_t) a->c[3]);
0e287786 8277 return aval;
53df40a4
AM
8278}
8279
8280#ifdef BFD_HOST_64_BIT
0e287786
AM
8281static bfd_vma
8282ext64l_r_offset (const void *p)
53df40a4
AM
8283{
8284 union aligned64
8285 {
8286 uint64_t v;
8287 unsigned char c[8];
8288 };
8289 const union aligned64 *a
0e287786 8290 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8291
8292 uint64_t aval = ( (uint64_t) a->c[0]
8293 | (uint64_t) a->c[1] << 8
8294 | (uint64_t) a->c[2] << 16
8295 | (uint64_t) a->c[3] << 24
8296 | (uint64_t) a->c[4] << 32
8297 | (uint64_t) a->c[5] << 40
8298 | (uint64_t) a->c[6] << 48
8299 | (uint64_t) a->c[7] << 56);
0e287786 8300 return aval;
53df40a4
AM
8301}
8302
0e287786
AM
8303static bfd_vma
8304ext64b_r_offset (const void *p)
53df40a4
AM
8305{
8306 union aligned64
8307 {
8308 uint64_t v;
8309 unsigned char c[8];
8310 };
8311 const union aligned64 *a
0e287786 8312 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8313
8314 uint64_t aval = ( (uint64_t) a->c[0] << 56
8315 | (uint64_t) a->c[1] << 48
8316 | (uint64_t) a->c[2] << 40
8317 | (uint64_t) a->c[3] << 32
8318 | (uint64_t) a->c[4] << 24
8319 | (uint64_t) a->c[5] << 16
8320 | (uint64_t) a->c[6] << 8
8321 | (uint64_t) a->c[7]);
0e287786 8322 return aval;
53df40a4
AM
8323}
8324#endif
8325
c152c796
AM
8326/* When performing a relocatable link, the input relocations are
8327 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8328 referenced must be updated. Update all the relocations found in
8329 RELDATA. */
c152c796 8330
bca6d0e3 8331static bfd_boolean
c152c796 8332elf_link_adjust_relocs (bfd *abfd,
28dbcedc
AM
8333 struct bfd_elf_section_reloc_data *reldata,
8334 bfd_boolean sort)
c152c796
AM
8335{
8336 unsigned int i;
8337 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8338 bfd_byte *erela;
8339 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8340 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8341 bfd_vma r_type_mask;
8342 int r_sym_shift;
d4730f92
BS
8343 unsigned int count = reldata->count;
8344 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8345
d4730f92 8346 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8347 {
8348 swap_in = bed->s->swap_reloc_in;
8349 swap_out = bed->s->swap_reloc_out;
8350 }
d4730f92 8351 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8352 {
8353 swap_in = bed->s->swap_reloca_in;
8354 swap_out = bed->s->swap_reloca_out;
8355 }
8356 else
8357 abort ();
8358
8359 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8360 abort ();
8361
8362 if (bed->s->arch_size == 32)
8363 {
8364 r_type_mask = 0xff;
8365 r_sym_shift = 8;
8366 }
8367 else
8368 {
8369 r_type_mask = 0xffffffff;
8370 r_sym_shift = 32;
8371 }
8372
d4730f92
BS
8373 erela = reldata->hdr->contents;
8374 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8375 {
8376 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8377 unsigned int j;
8378
8379 if (*rel_hash == NULL)
8380 continue;
8381
8382 BFD_ASSERT ((*rel_hash)->indx >= 0);
8383
8384 (*swap_in) (abfd, erela, irela);
8385 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8386 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8387 | (irela[j].r_info & r_type_mask));
8388 (*swap_out) (abfd, irela, erela);
8389 }
53df40a4 8390
0e287786 8391 if (sort && count != 0)
53df40a4 8392 {
0e287786
AM
8393 bfd_vma (*ext_r_off) (const void *);
8394 bfd_vma r_off;
8395 size_t elt_size;
8396 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8397 bfd_byte *buf = NULL;
28dbcedc
AM
8398
8399 if (bed->s->arch_size == 32)
8400 {
8401 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8402 ext_r_off = ext32l_r_offset;
28dbcedc 8403 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8404 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8405 else
8406 abort ();
8407 }
53df40a4 8408 else
28dbcedc 8409 {
53df40a4 8410#ifdef BFD_HOST_64_BIT
28dbcedc 8411 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8412 ext_r_off = ext64l_r_offset;
28dbcedc 8413 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8414 ext_r_off = ext64b_r_offset;
28dbcedc 8415 else
53df40a4 8416#endif
28dbcedc
AM
8417 abort ();
8418 }
0e287786 8419
bca6d0e3
AM
8420 /* Must use a stable sort here. A modified insertion sort,
8421 since the relocs are mostly sorted already. */
0e287786
AM
8422 elt_size = reldata->hdr->sh_entsize;
8423 base = reldata->hdr->contents;
8424 end = base + count * elt_size;
bca6d0e3 8425 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8426 abort ();
8427
8428 /* Ensure the first element is lowest. This acts as a sentinel,
8429 speeding the main loop below. */
8430 r_off = (*ext_r_off) (base);
8431 for (p = loc = base; (p += elt_size) < end; )
8432 {
8433 bfd_vma r_off2 = (*ext_r_off) (p);
8434 if (r_off > r_off2)
8435 {
8436 r_off = r_off2;
8437 loc = p;
8438 }
8439 }
8440 if (loc != base)
8441 {
8442 /* Don't just swap *base and *loc as that changes the order
8443 of the original base[0] and base[1] if they happen to
8444 have the same r_offset. */
bca6d0e3
AM
8445 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8446 memcpy (onebuf, loc, elt_size);
0e287786 8447 memmove (base + elt_size, base, loc - base);
bca6d0e3 8448 memcpy (base, onebuf, elt_size);
0e287786
AM
8449 }
8450
b29b8669 8451 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8452 {
8453 /* base to p is sorted, *p is next to insert. */
8454 r_off = (*ext_r_off) (p);
8455 /* Search the sorted region for location to insert. */
8456 loc = p - elt_size;
8457 while (r_off < (*ext_r_off) (loc))
8458 loc -= elt_size;
8459 loc += elt_size;
8460 if (loc != p)
8461 {
bca6d0e3
AM
8462 /* Chances are there is a run of relocs to insert here,
8463 from one of more input files. Files are not always
8464 linked in order due to the way elf_link_input_bfd is
8465 called. See pr17666. */
8466 size_t sortlen = p - loc;
8467 bfd_vma r_off2 = (*ext_r_off) (loc);
8468 size_t runlen = elt_size;
8469 size_t buf_size = 96 * 1024;
8470 while (p + runlen < end
8471 && (sortlen <= buf_size
8472 || runlen + elt_size <= buf_size)
8473 && r_off2 > (*ext_r_off) (p + runlen))
8474 runlen += elt_size;
8475 if (buf == NULL)
8476 {
8477 buf = bfd_malloc (buf_size);
8478 if (buf == NULL)
8479 return FALSE;
8480 }
8481 if (runlen < sortlen)
8482 {
8483 memcpy (buf, p, runlen);
8484 memmove (loc + runlen, loc, sortlen);
8485 memcpy (loc, buf, runlen);
8486 }
8487 else
8488 {
8489 memcpy (buf, loc, sortlen);
8490 memmove (loc, p, runlen);
8491 memcpy (loc + runlen, buf, sortlen);
8492 }
b29b8669 8493 p += runlen - elt_size;
0e287786
AM
8494 }
8495 }
8496 /* Hashes are no longer valid. */
28dbcedc
AM
8497 free (reldata->hashes);
8498 reldata->hashes = NULL;
bca6d0e3 8499 free (buf);
53df40a4 8500 }
bca6d0e3 8501 return TRUE;
c152c796
AM
8502}
8503
8504struct elf_link_sort_rela
8505{
8506 union {
8507 bfd_vma offset;
8508 bfd_vma sym_mask;
8509 } u;
8510 enum elf_reloc_type_class type;
8511 /* We use this as an array of size int_rels_per_ext_rel. */
8512 Elf_Internal_Rela rela[1];
8513};
8514
8515static int
8516elf_link_sort_cmp1 (const void *A, const void *B)
8517{
a50b1753
NC
8518 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8519 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8520 int relativea, relativeb;
8521
8522 relativea = a->type == reloc_class_relative;
8523 relativeb = b->type == reloc_class_relative;
8524
8525 if (relativea < relativeb)
8526 return 1;
8527 if (relativea > relativeb)
8528 return -1;
8529 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8530 return -1;
8531 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8532 return 1;
8533 if (a->rela->r_offset < b->rela->r_offset)
8534 return -1;
8535 if (a->rela->r_offset > b->rela->r_offset)
8536 return 1;
8537 return 0;
8538}
8539
8540static int
8541elf_link_sort_cmp2 (const void *A, const void *B)
8542{
a50b1753
NC
8543 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8544 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8545
7e612e98 8546 if (a->type < b->type)
c152c796 8547 return -1;
7e612e98 8548 if (a->type > b->type)
c152c796 8549 return 1;
7e612e98 8550 if (a->u.offset < b->u.offset)
c152c796 8551 return -1;
7e612e98 8552 if (a->u.offset > b->u.offset)
c152c796
AM
8553 return 1;
8554 if (a->rela->r_offset < b->rela->r_offset)
8555 return -1;
8556 if (a->rela->r_offset > b->rela->r_offset)
8557 return 1;
8558 return 0;
8559}
8560
8561static size_t
8562elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8563{
3410fea8 8564 asection *dynamic_relocs;
fc66a176
L
8565 asection *rela_dyn;
8566 asection *rel_dyn;
c152c796
AM
8567 bfd_size_type count, size;
8568 size_t i, ret, sort_elt, ext_size;
8569 bfd_byte *sort, *s_non_relative, *p;
8570 struct elf_link_sort_rela *sq;
8571 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8572 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8573 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8574 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8575 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8576 struct bfd_link_order *lo;
8577 bfd_vma r_sym_mask;
3410fea8 8578 bfd_boolean use_rela;
c152c796 8579
3410fea8
NC
8580 /* Find a dynamic reloc section. */
8581 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8582 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8583 if (rela_dyn != NULL && rela_dyn->size > 0
8584 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8585 {
3410fea8
NC
8586 bfd_boolean use_rela_initialised = FALSE;
8587
8588 /* This is just here to stop gcc from complaining.
c8e44c6d 8589 Its initialization checking code is not perfect. */
3410fea8
NC
8590 use_rela = TRUE;
8591
8592 /* Both sections are present. Examine the sizes
8593 of the indirect sections to help us choose. */
8594 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8595 if (lo->type == bfd_indirect_link_order)
8596 {
8597 asection *o = lo->u.indirect.section;
8598
8599 if ((o->size % bed->s->sizeof_rela) == 0)
8600 {
8601 if ((o->size % bed->s->sizeof_rel) == 0)
8602 /* Section size is divisible by both rel and rela sizes.
8603 It is of no help to us. */
8604 ;
8605 else
8606 {
8607 /* Section size is only divisible by rela. */
8608 if (use_rela_initialised && (use_rela == FALSE))
8609 {
c8e44c6d
AM
8610 _bfd_error_handler (_("%B: Unable to sort relocs - "
8611 "they are in more than one size"),
8612 abfd);
3410fea8
NC
8613 bfd_set_error (bfd_error_invalid_operation);
8614 return 0;
8615 }
8616 else
8617 {
8618 use_rela = TRUE;
8619 use_rela_initialised = TRUE;
8620 }
8621 }
8622 }
8623 else if ((o->size % bed->s->sizeof_rel) == 0)
8624 {
8625 /* Section size is only divisible by rel. */
8626 if (use_rela_initialised && (use_rela == TRUE))
8627 {
c8e44c6d
AM
8628 _bfd_error_handler (_("%B: Unable to sort relocs - "
8629 "they are in more than one size"),
8630 abfd);
3410fea8
NC
8631 bfd_set_error (bfd_error_invalid_operation);
8632 return 0;
8633 }
8634 else
8635 {
8636 use_rela = FALSE;
8637 use_rela_initialised = TRUE;
8638 }
8639 }
8640 else
8641 {
c8e44c6d
AM
8642 /* The section size is not divisible by either -
8643 something is wrong. */
8644 _bfd_error_handler (_("%B: Unable to sort relocs - "
8645 "they are of an unknown size"), abfd);
3410fea8
NC
8646 bfd_set_error (bfd_error_invalid_operation);
8647 return 0;
8648 }
8649 }
8650
8651 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8652 if (lo->type == bfd_indirect_link_order)
8653 {
8654 asection *o = lo->u.indirect.section;
8655
8656 if ((o->size % bed->s->sizeof_rela) == 0)
8657 {
8658 if ((o->size % bed->s->sizeof_rel) == 0)
8659 /* Section size is divisible by both rel and rela sizes.
8660 It is of no help to us. */
8661 ;
8662 else
8663 {
8664 /* Section size is only divisible by rela. */
8665 if (use_rela_initialised && (use_rela == FALSE))
8666 {
c8e44c6d
AM
8667 _bfd_error_handler (_("%B: Unable to sort relocs - "
8668 "they are in more than one size"),
8669 abfd);
3410fea8
NC
8670 bfd_set_error (bfd_error_invalid_operation);
8671 return 0;
8672 }
8673 else
8674 {
8675 use_rela = TRUE;
8676 use_rela_initialised = TRUE;
8677 }
8678 }
8679 }
8680 else if ((o->size % bed->s->sizeof_rel) == 0)
8681 {
8682 /* Section size is only divisible by rel. */
8683 if (use_rela_initialised && (use_rela == TRUE))
8684 {
c8e44c6d
AM
8685 _bfd_error_handler (_("%B: Unable to sort relocs - "
8686 "they are in more than one size"),
8687 abfd);
3410fea8
NC
8688 bfd_set_error (bfd_error_invalid_operation);
8689 return 0;
8690 }
8691 else
8692 {
8693 use_rela = FALSE;
8694 use_rela_initialised = TRUE;
8695 }
8696 }
8697 else
8698 {
c8e44c6d
AM
8699 /* The section size is not divisible by either -
8700 something is wrong. */
8701 _bfd_error_handler (_("%B: Unable to sort relocs - "
8702 "they are of an unknown size"), abfd);
3410fea8
NC
8703 bfd_set_error (bfd_error_invalid_operation);
8704 return 0;
8705 }
8706 }
8707
8708 if (! use_rela_initialised)
8709 /* Make a guess. */
8710 use_rela = TRUE;
c152c796 8711 }
fc66a176
L
8712 else if (rela_dyn != NULL && rela_dyn->size > 0)
8713 use_rela = TRUE;
8714 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8715 use_rela = FALSE;
c152c796 8716 else
fc66a176 8717 return 0;
3410fea8
NC
8718
8719 if (use_rela)
c152c796 8720 {
3410fea8 8721 dynamic_relocs = rela_dyn;
c152c796
AM
8722 ext_size = bed->s->sizeof_rela;
8723 swap_in = bed->s->swap_reloca_in;
8724 swap_out = bed->s->swap_reloca_out;
8725 }
3410fea8
NC
8726 else
8727 {
8728 dynamic_relocs = rel_dyn;
8729 ext_size = bed->s->sizeof_rel;
8730 swap_in = bed->s->swap_reloc_in;
8731 swap_out = bed->s->swap_reloc_out;
8732 }
c152c796
AM
8733
8734 size = 0;
3410fea8 8735 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8736 if (lo->type == bfd_indirect_link_order)
3410fea8 8737 size += lo->u.indirect.section->size;
c152c796 8738
3410fea8 8739 if (size != dynamic_relocs->size)
c152c796
AM
8740 return 0;
8741
8742 sort_elt = (sizeof (struct elf_link_sort_rela)
8743 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8744
8745 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8746 if (count == 0)
8747 return 0;
a50b1753 8748 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8749
c152c796
AM
8750 if (sort == NULL)
8751 {
8752 (*info->callbacks->warning)
8753 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8754 return 0;
8755 }
8756
8757 if (bed->s->arch_size == 32)
8758 r_sym_mask = ~(bfd_vma) 0xff;
8759 else
8760 r_sym_mask = ~(bfd_vma) 0xffffffff;
8761
3410fea8 8762 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8763 if (lo->type == bfd_indirect_link_order)
8764 {
8765 bfd_byte *erel, *erelend;
8766 asection *o = lo->u.indirect.section;
8767
1da212d6
AM
8768 if (o->contents == NULL && o->size != 0)
8769 {
8770 /* This is a reloc section that is being handled as a normal
8771 section. See bfd_section_from_shdr. We can't combine
8772 relocs in this case. */
8773 free (sort);
8774 return 0;
8775 }
c152c796 8776 erel = o->contents;
eea6121a 8777 erelend = o->contents + o->size;
c8e44c6d 8778 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 8779
c152c796
AM
8780 while (erel < erelend)
8781 {
8782 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8783
c152c796 8784 (*swap_in) (abfd, erel, s->rela);
7e612e98 8785 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8786 s->u.sym_mask = r_sym_mask;
8787 p += sort_elt;
8788 erel += ext_size;
8789 }
8790 }
8791
8792 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8793
8794 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8795 {
8796 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8797 if (s->type != reloc_class_relative)
8798 break;
8799 }
8800 ret = i;
8801 s_non_relative = p;
8802
8803 sq = (struct elf_link_sort_rela *) s_non_relative;
8804 for (; i < count; i++, p += sort_elt)
8805 {
8806 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8807 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8808 sq = sp;
8809 sp->u.offset = sq->rela->r_offset;
8810 }
8811
8812 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8813
c8e44c6d
AM
8814 struct elf_link_hash_table *htab = elf_hash_table (info);
8815 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8816 {
8817 /* We have plt relocs in .rela.dyn. */
8818 sq = (struct elf_link_sort_rela *) sort;
8819 for (i = 0; i < count; i++)
8820 if (sq[count - i - 1].type != reloc_class_plt)
8821 break;
8822 if (i != 0 && htab->srelplt->size == i * ext_size)
8823 {
8824 struct bfd_link_order **plo;
8825 /* Put srelplt link_order last. This is so the output_offset
8826 set in the next loop is correct for DT_JMPREL. */
8827 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8828 if ((*plo)->type == bfd_indirect_link_order
8829 && (*plo)->u.indirect.section == htab->srelplt)
8830 {
8831 lo = *plo;
8832 *plo = lo->next;
8833 }
8834 else
8835 plo = &(*plo)->next;
8836 *plo = lo;
8837 lo->next = NULL;
8838 dynamic_relocs->map_tail.link_order = lo;
8839 }
8840 }
8841
8842 p = sort;
3410fea8 8843 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8844 if (lo->type == bfd_indirect_link_order)
8845 {
8846 bfd_byte *erel, *erelend;
8847 asection *o = lo->u.indirect.section;
8848
8849 erel = o->contents;
eea6121a 8850 erelend = o->contents + o->size;
c8e44c6d 8851 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
8852 while (erel < erelend)
8853 {
8854 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8855 (*swap_out) (abfd, s->rela, erel);
8856 p += sort_elt;
8857 erel += ext_size;
8858 }
8859 }
8860
8861 free (sort);
3410fea8 8862 *psec = dynamic_relocs;
c152c796
AM
8863 return ret;
8864}
8865
ef10c3ac 8866/* Add a symbol to the output symbol string table. */
c152c796 8867
6e0b88f1 8868static int
ef10c3ac
L
8869elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8870 const char *name,
8871 Elf_Internal_Sym *elfsym,
8872 asection *input_sec,
8873 struct elf_link_hash_entry *h)
c152c796 8874{
6e0b88f1 8875 int (*output_symbol_hook)
c152c796
AM
8876 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8877 struct elf_link_hash_entry *);
ef10c3ac 8878 struct elf_link_hash_table *hash_table;
c152c796 8879 const struct elf_backend_data *bed;
ef10c3ac 8880 bfd_size_type strtabsize;
c152c796 8881
8539e4e8
AM
8882 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8883
8b127cbc 8884 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
8885 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8886 if (output_symbol_hook != NULL)
8887 {
8b127cbc 8888 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
8889 if (ret != 1)
8890 return ret;
c152c796
AM
8891 }
8892
ef10c3ac
L
8893 if (name == NULL
8894 || *name == '\0'
8895 || (input_sec->flags & SEC_EXCLUDE))
8896 elfsym->st_name = (unsigned long) -1;
c152c796
AM
8897 else
8898 {
ef10c3ac
L
8899 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8900 to get the final offset for st_name. */
8901 elfsym->st_name
8902 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8903 name, FALSE);
c152c796 8904 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8905 return 0;
c152c796
AM
8906 }
8907
ef10c3ac
L
8908 hash_table = elf_hash_table (flinfo->info);
8909 strtabsize = hash_table->strtabsize;
8910 if (strtabsize <= hash_table->strtabcount)
c152c796 8911 {
ef10c3ac
L
8912 strtabsize += strtabsize;
8913 hash_table->strtabsize = strtabsize;
8914 strtabsize *= sizeof (*hash_table->strtab);
8915 hash_table->strtab
8916 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8917 strtabsize);
8918 if (hash_table->strtab == NULL)
6e0b88f1 8919 return 0;
c152c796 8920 }
ef10c3ac
L
8921 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8922 hash_table->strtab[hash_table->strtabcount].dest_index
8923 = hash_table->strtabcount;
8924 hash_table->strtab[hash_table->strtabcount].destshndx_index
8925 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8926
8927 bfd_get_symcount (flinfo->output_bfd) += 1;
8928 hash_table->strtabcount += 1;
8929
8930 return 1;
8931}
8932
8933/* Swap symbols out to the symbol table and flush the output symbols to
8934 the file. */
8935
8936static bfd_boolean
8937elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8938{
8939 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
8940 bfd_size_type amt;
8941 size_t i;
ef10c3ac
L
8942 const struct elf_backend_data *bed;
8943 bfd_byte *symbuf;
8944 Elf_Internal_Shdr *hdr;
8945 file_ptr pos;
8946 bfd_boolean ret;
8947
8948 if (!hash_table->strtabcount)
8949 return TRUE;
8950
8951 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8952
8953 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 8954
ef10c3ac
L
8955 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8956 symbuf = (bfd_byte *) bfd_malloc (amt);
8957 if (symbuf == NULL)
8958 return FALSE;
1b786873 8959
ef10c3ac 8960 if (flinfo->symshndxbuf)
c152c796 8961 {
ef53be89
AM
8962 amt = sizeof (Elf_External_Sym_Shndx);
8963 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
8964 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8965 if (flinfo->symshndxbuf == NULL)
c152c796 8966 {
ef10c3ac
L
8967 free (symbuf);
8968 return FALSE;
c152c796 8969 }
c152c796
AM
8970 }
8971
ef10c3ac
L
8972 for (i = 0; i < hash_table->strtabcount; i++)
8973 {
8974 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8975 if (elfsym->sym.st_name == (unsigned long) -1)
8976 elfsym->sym.st_name = 0;
8977 else
8978 elfsym->sym.st_name
8979 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8980 elfsym->sym.st_name);
8981 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8982 ((bfd_byte *) symbuf
8983 + (elfsym->dest_index
8984 * bed->s->sizeof_sym)),
8985 (flinfo->symshndxbuf
8986 + elfsym->destshndx_index));
8987 }
8988
8989 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8990 pos = hdr->sh_offset + hdr->sh_size;
8991 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8992 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8993 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8994 {
8995 hdr->sh_size += amt;
8996 ret = TRUE;
8997 }
8998 else
8999 ret = FALSE;
c152c796 9000
ef10c3ac
L
9001 free (symbuf);
9002
9003 free (hash_table->strtab);
9004 hash_table->strtab = NULL;
9005
9006 return ret;
c152c796
AM
9007}
9008
c0d5a53d
L
9009/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9010
9011static bfd_boolean
9012check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9013{
4fbb74a6
AM
9014 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9015 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9016 {
9017 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9018 beyond 64k. */
c0d5a53d
L
9019 (*_bfd_error_handler)
9020 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9021 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9022 bfd_set_error (bfd_error_nonrepresentable_section);
9023 return FALSE;
9024 }
9025 return TRUE;
9026}
9027
c152c796
AM
9028/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9029 allowing an unsatisfied unversioned symbol in the DSO to match a
9030 versioned symbol that would normally require an explicit version.
9031 We also handle the case that a DSO references a hidden symbol
9032 which may be satisfied by a versioned symbol in another DSO. */
9033
9034static bfd_boolean
9035elf_link_check_versioned_symbol (struct bfd_link_info *info,
9036 const struct elf_backend_data *bed,
9037 struct elf_link_hash_entry *h)
9038{
9039 bfd *abfd;
9040 struct elf_link_loaded_list *loaded;
9041
9042 if (!is_elf_hash_table (info->hash))
9043 return FALSE;
9044
90c984fc
L
9045 /* Check indirect symbol. */
9046 while (h->root.type == bfd_link_hash_indirect)
9047 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9048
c152c796
AM
9049 switch (h->root.type)
9050 {
9051 default:
9052 abfd = NULL;
9053 break;
9054
9055 case bfd_link_hash_undefined:
9056 case bfd_link_hash_undefweak:
9057 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9058 if (abfd == NULL
9059 || (abfd->flags & DYNAMIC) == 0
e56f61be 9060 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9061 return FALSE;
9062 break;
9063
9064 case bfd_link_hash_defined:
9065 case bfd_link_hash_defweak:
9066 abfd = h->root.u.def.section->owner;
9067 break;
9068
9069 case bfd_link_hash_common:
9070 abfd = h->root.u.c.p->section->owner;
9071 break;
9072 }
9073 BFD_ASSERT (abfd != NULL);
9074
9075 for (loaded = elf_hash_table (info)->loaded;
9076 loaded != NULL;
9077 loaded = loaded->next)
9078 {
9079 bfd *input;
9080 Elf_Internal_Shdr *hdr;
ef53be89
AM
9081 size_t symcount;
9082 size_t extsymcount;
9083 size_t extsymoff;
c152c796
AM
9084 Elf_Internal_Shdr *versymhdr;
9085 Elf_Internal_Sym *isym;
9086 Elf_Internal_Sym *isymend;
9087 Elf_Internal_Sym *isymbuf;
9088 Elf_External_Versym *ever;
9089 Elf_External_Versym *extversym;
9090
9091 input = loaded->abfd;
9092
9093 /* We check each DSO for a possible hidden versioned definition. */
9094 if (input == abfd
9095 || (input->flags & DYNAMIC) == 0
9096 || elf_dynversym (input) == 0)
9097 continue;
9098
9099 hdr = &elf_tdata (input)->dynsymtab_hdr;
9100
9101 symcount = hdr->sh_size / bed->s->sizeof_sym;
9102 if (elf_bad_symtab (input))
9103 {
9104 extsymcount = symcount;
9105 extsymoff = 0;
9106 }
9107 else
9108 {
9109 extsymcount = symcount - hdr->sh_info;
9110 extsymoff = hdr->sh_info;
9111 }
9112
9113 if (extsymcount == 0)
9114 continue;
9115
9116 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9117 NULL, NULL, NULL);
9118 if (isymbuf == NULL)
9119 return FALSE;
9120
9121 /* Read in any version definitions. */
9122 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9123 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9124 if (extversym == NULL)
9125 goto error_ret;
9126
9127 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9128 || (bfd_bread (extversym, versymhdr->sh_size, input)
9129 != versymhdr->sh_size))
9130 {
9131 free (extversym);
9132 error_ret:
9133 free (isymbuf);
9134 return FALSE;
9135 }
9136
9137 ever = extversym + extsymoff;
9138 isymend = isymbuf + extsymcount;
9139 for (isym = isymbuf; isym < isymend; isym++, ever++)
9140 {
9141 const char *name;
9142 Elf_Internal_Versym iver;
9143 unsigned short version_index;
9144
9145 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9146 || isym->st_shndx == SHN_UNDEF)
9147 continue;
9148
9149 name = bfd_elf_string_from_elf_section (input,
9150 hdr->sh_link,
9151 isym->st_name);
9152 if (strcmp (name, h->root.root.string) != 0)
9153 continue;
9154
9155 _bfd_elf_swap_versym_in (input, ever, &iver);
9156
d023c380
L
9157 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9158 && !(h->def_regular
9159 && h->forced_local))
c152c796
AM
9160 {
9161 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9162 have provided a definition for the undefined sym unless
9163 it is defined in a non-shared object and forced local.
9164 */
c152c796
AM
9165 abort ();
9166 }
9167
9168 version_index = iver.vs_vers & VERSYM_VERSION;
9169 if (version_index == 1 || version_index == 2)
9170 {
9171 /* This is the base or first version. We can use it. */
9172 free (extversym);
9173 free (isymbuf);
9174 return TRUE;
9175 }
9176 }
9177
9178 free (extversym);
9179 free (isymbuf);
9180 }
9181
9182 return FALSE;
9183}
9184
b8871f35
L
9185/* Convert ELF common symbol TYPE. */
9186
9187static int
9188elf_link_convert_common_type (struct bfd_link_info *info, int type)
9189{
9190 /* Commom symbol can only appear in relocatable link. */
9191 if (!bfd_link_relocatable (info))
9192 abort ();
9193 switch (info->elf_stt_common)
9194 {
9195 case unchanged:
9196 break;
9197 case elf_stt_common:
9198 type = STT_COMMON;
9199 break;
9200 case no_elf_stt_common:
9201 type = STT_OBJECT;
9202 break;
9203 }
9204 return type;
9205}
9206
c152c796
AM
9207/* Add an external symbol to the symbol table. This is called from
9208 the hash table traversal routine. When generating a shared object,
9209 we go through the symbol table twice. The first time we output
9210 anything that might have been forced to local scope in a version
9211 script. The second time we output the symbols that are still
9212 global symbols. */
9213
9214static bfd_boolean
7686d77d 9215elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9216{
7686d77d 9217 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9218 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9219 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9220 bfd_boolean strip;
9221 Elf_Internal_Sym sym;
9222 asection *input_sec;
9223 const struct elf_backend_data *bed;
6e0b88f1
AM
9224 long indx;
9225 int ret;
b8871f35 9226 unsigned int type;
6e33951e
L
9227 /* A symbol is bound locally if it is forced local or it is locally
9228 defined, hidden versioned, not referenced by shared library and
9229 not exported when linking executable. */
9230 bfd_boolean local_bind = (h->forced_local
0e1862bb 9231 || (bfd_link_executable (flinfo->info)
6e33951e
L
9232 && !flinfo->info->export_dynamic
9233 && !h->dynamic
9234 && !h->ref_dynamic
9235 && h->def_regular
422f1182 9236 && h->versioned == versioned_hidden));
c152c796
AM
9237
9238 if (h->root.type == bfd_link_hash_warning)
9239 {
9240 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9241 if (h->root.type == bfd_link_hash_new)
9242 return TRUE;
9243 }
9244
9245 /* Decide whether to output this symbol in this pass. */
9246 if (eoinfo->localsyms)
9247 {
6e33951e 9248 if (!local_bind)
c152c796
AM
9249 return TRUE;
9250 }
9251 else
9252 {
6e33951e 9253 if (local_bind)
c152c796
AM
9254 return TRUE;
9255 }
9256
8b127cbc 9257 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9258
12ac1cf5 9259 if (h->root.type == bfd_link_hash_undefined)
c152c796 9260 {
12ac1cf5
NC
9261 /* If we have an undefined symbol reference here then it must have
9262 come from a shared library that is being linked in. (Undefined
98da7939
L
9263 references in regular files have already been handled unless
9264 they are in unreferenced sections which are removed by garbage
9265 collection). */
12ac1cf5
NC
9266 bfd_boolean ignore_undef = FALSE;
9267
9268 /* Some symbols may be special in that the fact that they're
9269 undefined can be safely ignored - let backend determine that. */
9270 if (bed->elf_backend_ignore_undef_symbol)
9271 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9272
9273 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9274 if (!ignore_undef
12ac1cf5 9275 && h->ref_dynamic
8b127cbc
AM
9276 && (!h->ref_regular || flinfo->info->gc_sections)
9277 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9278 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9279 (*flinfo->info->callbacks->undefined_symbol)
9280 (flinfo->info, h->root.root.string,
9281 h->ref_regular ? NULL : h->root.u.undef.abfd,
9282 NULL, 0,
9283 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9284
9285 /* Strip a global symbol defined in a discarded section. */
9286 if (h->indx == -3)
9287 return TRUE;
c152c796
AM
9288 }
9289
9290 /* We should also warn if a forced local symbol is referenced from
9291 shared libraries. */
0e1862bb 9292 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9293 && h->forced_local
9294 && h->ref_dynamic
371a5866 9295 && h->def_regular
f5385ebf 9296 && !h->dynamic_def
ee659f1f 9297 && h->ref_dynamic_nonweak
8b127cbc 9298 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9299 {
17d078c5
AM
9300 bfd *def_bfd;
9301 const char *msg;
90c984fc
L
9302 struct elf_link_hash_entry *hi = h;
9303
9304 /* Check indirect symbol. */
9305 while (hi->root.type == bfd_link_hash_indirect)
9306 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9307
9308 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9309 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9310 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9311 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9312 else
9313 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9314 def_bfd = flinfo->output_bfd;
90c984fc
L
9315 if (hi->root.u.def.section != bfd_abs_section_ptr)
9316 def_bfd = hi->root.u.def.section->owner;
8b127cbc 9317 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
17d078c5
AM
9318 h->root.root.string);
9319 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9320 eoinfo->failed = TRUE;
9321 return FALSE;
9322 }
9323
9324 /* We don't want to output symbols that have never been mentioned by
9325 a regular file, or that we have been told to strip. However, if
9326 h->indx is set to -2, the symbol is used by a reloc and we must
9327 output it. */
d983c8c5 9328 strip = FALSE;
c152c796 9329 if (h->indx == -2)
d983c8c5 9330 ;
f5385ebf 9331 else if ((h->def_dynamic
77cfaee6
AM
9332 || h->ref_dynamic
9333 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9334 && !h->def_regular
9335 && !h->ref_regular)
c152c796 9336 strip = TRUE;
8b127cbc 9337 else if (flinfo->info->strip == strip_all)
c152c796 9338 strip = TRUE;
8b127cbc
AM
9339 else if (flinfo->info->strip == strip_some
9340 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9341 h->root.root.string, FALSE, FALSE) == NULL)
9342 strip = TRUE;
d56d55e7
AM
9343 else if ((h->root.type == bfd_link_hash_defined
9344 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9345 && ((flinfo->info->strip_discarded
dbaa2011 9346 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9347 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9348 && h->root.u.def.section->owner != NULL
d56d55e7 9349 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9350 strip = TRUE;
9e2278f5
AM
9351 else if ((h->root.type == bfd_link_hash_undefined
9352 || h->root.type == bfd_link_hash_undefweak)
9353 && h->root.u.undef.abfd != NULL
9354 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9355 strip = TRUE;
c152c796 9356
b8871f35
L
9357 type = h->type;
9358
c152c796 9359 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9360 nothing else to do. However, if it is a forced local symbol or
9361 an ifunc symbol we need to give the backend finish_dynamic_symbol
9362 function a chance to make it dynamic. */
c152c796
AM
9363 if (strip
9364 && h->dynindx == -1
b8871f35 9365 && type != STT_GNU_IFUNC
f5385ebf 9366 && !h->forced_local)
c152c796
AM
9367 return TRUE;
9368
9369 sym.st_value = 0;
9370 sym.st_size = h->size;
9371 sym.st_other = h->other;
c152c796
AM
9372 switch (h->root.type)
9373 {
9374 default:
9375 case bfd_link_hash_new:
9376 case bfd_link_hash_warning:
9377 abort ();
9378 return FALSE;
9379
9380 case bfd_link_hash_undefined:
9381 case bfd_link_hash_undefweak:
9382 input_sec = bfd_und_section_ptr;
9383 sym.st_shndx = SHN_UNDEF;
9384 break;
9385
9386 case bfd_link_hash_defined:
9387 case bfd_link_hash_defweak:
9388 {
9389 input_sec = h->root.u.def.section;
9390 if (input_sec->output_section != NULL)
9391 {
9392 sym.st_shndx =
8b127cbc 9393 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9394 input_sec->output_section);
9395 if (sym.st_shndx == SHN_BAD)
9396 {
9397 (*_bfd_error_handler)
d003868e 9398 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9399 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9400 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9401 eoinfo->failed = TRUE;
9402 return FALSE;
9403 }
9404
9405 /* ELF symbols in relocatable files are section relative,
9406 but in nonrelocatable files they are virtual
9407 addresses. */
9408 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9409 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9410 {
9411 sym.st_value += input_sec->output_section->vma;
9412 if (h->type == STT_TLS)
9413 {
8b127cbc 9414 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9415 if (tls_sec != NULL)
9416 sym.st_value -= tls_sec->vma;
c152c796
AM
9417 }
9418 }
9419 }
9420 else
9421 {
9422 BFD_ASSERT (input_sec->owner == NULL
9423 || (input_sec->owner->flags & DYNAMIC) != 0);
9424 sym.st_shndx = SHN_UNDEF;
9425 input_sec = bfd_und_section_ptr;
9426 }
9427 }
9428 break;
9429
9430 case bfd_link_hash_common:
9431 input_sec = h->root.u.c.p->section;
a4d8e49b 9432 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9433 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9434 break;
9435
9436 case bfd_link_hash_indirect:
9437 /* These symbols are created by symbol versioning. They point
9438 to the decorated version of the name. For example, if the
9439 symbol foo@@GNU_1.2 is the default, which should be used when
9440 foo is used with no version, then we add an indirect symbol
9441 foo which points to foo@@GNU_1.2. We ignore these symbols,
9442 since the indirected symbol is already in the hash table. */
9443 return TRUE;
9444 }
9445
b8871f35
L
9446 if (type == STT_COMMON || type == STT_OBJECT)
9447 switch (h->root.type)
9448 {
9449 case bfd_link_hash_common:
9450 type = elf_link_convert_common_type (flinfo->info, type);
9451 break;
9452 case bfd_link_hash_defined:
9453 case bfd_link_hash_defweak:
9454 if (bed->common_definition (&sym))
9455 type = elf_link_convert_common_type (flinfo->info, type);
9456 else
9457 type = STT_OBJECT;
9458 break;
9459 case bfd_link_hash_undefined:
9460 case bfd_link_hash_undefweak:
9461 break;
9462 default:
9463 abort ();
9464 }
9465
9466 if (local_bind)
9467 {
9468 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9469 /* Turn off visibility on local symbol. */
9470 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9471 }
9472 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9473 else if (h->unique_global && h->def_regular)
9474 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9475 else if (h->root.type == bfd_link_hash_undefweak
9476 || h->root.type == bfd_link_hash_defweak)
9477 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9478 else
9479 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9480 sym.st_target_internal = h->target_internal;
9481
c152c796
AM
9482 /* Give the processor backend a chance to tweak the symbol value,
9483 and also to finish up anything that needs to be done for this
9484 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9485 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9486 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9487 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9488 && h->def_regular
0e1862bb 9489 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9490 || ((h->dynindx != -1
9491 || h->forced_local)
0e1862bb 9492 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9493 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9494 || h->root.type != bfd_link_hash_undefweak))
9495 || !h->forced_local)
8b127cbc 9496 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9497 {
9498 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9499 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9500 {
9501 eoinfo->failed = TRUE;
9502 return FALSE;
9503 }
9504 }
9505
9506 /* If we are marking the symbol as undefined, and there are no
9507 non-weak references to this symbol from a regular object, then
9508 mark the symbol as weak undefined; if there are non-weak
9509 references, mark the symbol as strong. We can't do this earlier,
9510 because it might not be marked as undefined until the
9511 finish_dynamic_symbol routine gets through with it. */
9512 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9513 && h->ref_regular
c152c796
AM
9514 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9515 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9516 {
9517 int bindtype;
b8871f35 9518 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9519
9520 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9521 if (type == STT_GNU_IFUNC)
9522 type = STT_FUNC;
c152c796 9523
f5385ebf 9524 if (h->ref_regular_nonweak)
c152c796
AM
9525 bindtype = STB_GLOBAL;
9526 else
9527 bindtype = STB_WEAK;
2955ec4c 9528 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9529 }
9530
bda987c2
CD
9531 /* If this is a symbol defined in a dynamic library, don't use the
9532 symbol size from the dynamic library. Relinking an executable
9533 against a new library may introduce gratuitous changes in the
9534 executable's symbols if we keep the size. */
9535 if (sym.st_shndx == SHN_UNDEF
9536 && !h->def_regular
9537 && h->def_dynamic)
9538 sym.st_size = 0;
9539
c152c796
AM
9540 /* If a non-weak symbol with non-default visibility is not defined
9541 locally, it is a fatal error. */
0e1862bb 9542 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9543 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9544 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9545 && h->root.type == bfd_link_hash_undefined
f5385ebf 9546 && !h->def_regular)
c152c796 9547 {
17d078c5
AM
9548 const char *msg;
9549
9550 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9551 msg = _("%B: protected symbol `%s' isn't defined");
9552 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9553 msg = _("%B: internal symbol `%s' isn't defined");
9554 else
9555 msg = _("%B: hidden symbol `%s' isn't defined");
8b127cbc 9556 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9557 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9558 eoinfo->failed = TRUE;
9559 return FALSE;
9560 }
9561
9562 /* If this symbol should be put in the .dynsym section, then put it
9563 there now. We already know the symbol index. We also fill in
9564 the entry in the .hash section. */
cae1fbbb 9565 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9566 && h->dynindx != -1
8b127cbc 9567 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9568 {
c152c796
AM
9569 bfd_byte *esym;
9570
90c984fc
L
9571 /* Since there is no version information in the dynamic string,
9572 if there is no version info in symbol version section, we will
1659f720 9573 have a run-time problem if not linking executable, referenced
6e33951e
L
9574 by shared library, not locally defined, or not bound locally.
9575 */
1659f720 9576 if (h->verinfo.verdef == NULL
6e33951e 9577 && !local_bind
0e1862bb 9578 && (!bfd_link_executable (flinfo->info)
1659f720
L
9579 || h->ref_dynamic
9580 || !h->def_regular))
90c984fc
L
9581 {
9582 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9583
9584 if (p && p [1] != '\0')
9585 {
9586 (*_bfd_error_handler)
9587 (_("%B: No symbol version section for versioned symbol `%s'"),
9588 flinfo->output_bfd, h->root.root.string);
9589 eoinfo->failed = TRUE;
9590 return FALSE;
9591 }
9592 }
9593
c152c796 9594 sym.st_name = h->dynstr_index;
cae1fbbb
L
9595 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9596 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9597 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9598 {
9599 eoinfo->failed = TRUE;
9600 return FALSE;
9601 }
8b127cbc 9602 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9603
8b127cbc 9604 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9605 {
9606 size_t hash_entry_size;
9607 bfd_byte *bucketpos;
9608 bfd_vma chain;
41198d0c
L
9609 size_t bucketcount;
9610 size_t bucket;
9611
8b127cbc 9612 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9613 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9614
9615 hash_entry_size
8b127cbc
AM
9616 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9617 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9618 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9619 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9620 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9621 bucketpos);
9622 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9623 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9624 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9625 }
c152c796 9626
8b127cbc 9627 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9628 {
9629 Elf_Internal_Versym iversym;
9630 Elf_External_Versym *eversym;
9631
f5385ebf 9632 if (!h->def_regular)
c152c796 9633 {
7b20f099
AM
9634 if (h->verinfo.verdef == NULL
9635 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9636 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9637 iversym.vs_vers = 0;
9638 else
9639 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9640 }
9641 else
9642 {
9643 if (h->verinfo.vertree == NULL)
9644 iversym.vs_vers = 1;
9645 else
9646 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9647 if (flinfo->info->create_default_symver)
3e3b46e5 9648 iversym.vs_vers++;
c152c796
AM
9649 }
9650
422f1182 9651 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9652 defined locally. */
422f1182 9653 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9654 iversym.vs_vers |= VERSYM_HIDDEN;
9655
8b127cbc 9656 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9657 eversym += h->dynindx;
8b127cbc 9658 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9659 }
9660 }
9661
d983c8c5
AM
9662 /* If the symbol is undefined, and we didn't output it to .dynsym,
9663 strip it from .symtab too. Obviously we can't do this for
9664 relocatable output or when needed for --emit-relocs. */
9665 else if (input_sec == bfd_und_section_ptr
9666 && h->indx != -2
0e1862bb 9667 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9668 return TRUE;
9669 /* Also strip others that we couldn't earlier due to dynamic symbol
9670 processing. */
9671 if (strip)
9672 return TRUE;
9673 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9674 return TRUE;
9675
2ec55de3
AM
9676 /* Output a FILE symbol so that following locals are not associated
9677 with the wrong input file. We need one for forced local symbols
9678 if we've seen more than one FILE symbol or when we have exactly
9679 one FILE symbol but global symbols are present in a file other
9680 than the one with the FILE symbol. We also need one if linker
9681 defined symbols are present. In practice these conditions are
9682 always met, so just emit the FILE symbol unconditionally. */
9683 if (eoinfo->localsyms
9684 && !eoinfo->file_sym_done
9685 && eoinfo->flinfo->filesym_count != 0)
9686 {
9687 Elf_Internal_Sym fsym;
9688
9689 memset (&fsym, 0, sizeof (fsym));
9690 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9691 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9692 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9693 bfd_und_section_ptr, NULL))
2ec55de3
AM
9694 return FALSE;
9695
9696 eoinfo->file_sym_done = TRUE;
9697 }
9698
8b127cbc 9699 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9700 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9701 input_sec, h);
6e0b88f1 9702 if (ret == 0)
c152c796
AM
9703 {
9704 eoinfo->failed = TRUE;
9705 return FALSE;
9706 }
6e0b88f1
AM
9707 else if (ret == 1)
9708 h->indx = indx;
9709 else if (h->indx == -2)
9710 abort();
c152c796
AM
9711
9712 return TRUE;
9713}
9714
cdd3575c
AM
9715/* Return TRUE if special handling is done for relocs in SEC against
9716 symbols defined in discarded sections. */
9717
c152c796
AM
9718static bfd_boolean
9719elf_section_ignore_discarded_relocs (asection *sec)
9720{
9721 const struct elf_backend_data *bed;
9722
cdd3575c
AM
9723 switch (sec->sec_info_type)
9724 {
dbaa2011
AM
9725 case SEC_INFO_TYPE_STABS:
9726 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9727 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9728 return TRUE;
9729 default:
9730 break;
9731 }
c152c796
AM
9732
9733 bed = get_elf_backend_data (sec->owner);
9734 if (bed->elf_backend_ignore_discarded_relocs != NULL
9735 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9736 return TRUE;
9737
9738 return FALSE;
9739}
9740
9e66c942
AM
9741/* Return a mask saying how ld should treat relocations in SEC against
9742 symbols defined in discarded sections. If this function returns
9743 COMPLAIN set, ld will issue a warning message. If this function
9744 returns PRETEND set, and the discarded section was link-once and the
9745 same size as the kept link-once section, ld will pretend that the
9746 symbol was actually defined in the kept section. Otherwise ld will
9747 zero the reloc (at least that is the intent, but some cooperation by
9748 the target dependent code is needed, particularly for REL targets). */
9749
8a696751
AM
9750unsigned int
9751_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9752{
9e66c942 9753 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9754 return PRETEND;
cdd3575c
AM
9755
9756 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9757 return 0;
cdd3575c
AM
9758
9759 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9760 return 0;
cdd3575c 9761
9e66c942 9762 return COMPLAIN | PRETEND;
cdd3575c
AM
9763}
9764
3d7f7666
L
9765/* Find a match between a section and a member of a section group. */
9766
9767static asection *
c0f00686
L
9768match_group_member (asection *sec, asection *group,
9769 struct bfd_link_info *info)
3d7f7666
L
9770{
9771 asection *first = elf_next_in_group (group);
9772 asection *s = first;
9773
9774 while (s != NULL)
9775 {
c0f00686 9776 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9777 return s;
9778
83180ade 9779 s = elf_next_in_group (s);
3d7f7666
L
9780 if (s == first)
9781 break;
9782 }
9783
9784 return NULL;
9785}
9786
01b3c8ab 9787/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9788 to replace it. Return the replacement if it is OK. Otherwise return
9789 NULL. */
01b3c8ab
L
9790
9791asection *
c0f00686 9792_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9793{
9794 asection *kept;
9795
9796 kept = sec->kept_section;
9797 if (kept != NULL)
9798 {
c2370991 9799 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9800 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9801 if (kept != NULL
9802 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9803 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9804 kept = NULL;
c2370991 9805 sec->kept_section = kept;
01b3c8ab
L
9806 }
9807 return kept;
9808}
9809
c152c796
AM
9810/* Link an input file into the linker output file. This function
9811 handles all the sections and relocations of the input file at once.
9812 This is so that we only have to read the local symbols once, and
9813 don't have to keep them in memory. */
9814
9815static bfd_boolean
8b127cbc 9816elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9817{
ece5ef60 9818 int (*relocate_section)
c152c796
AM
9819 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9820 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9821 bfd *output_bfd;
9822 Elf_Internal_Shdr *symtab_hdr;
9823 size_t locsymcount;
9824 size_t extsymoff;
9825 Elf_Internal_Sym *isymbuf;
9826 Elf_Internal_Sym *isym;
9827 Elf_Internal_Sym *isymend;
9828 long *pindex;
9829 asection **ppsection;
9830 asection *o;
9831 const struct elf_backend_data *bed;
c152c796 9832 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9833 bfd_size_type address_size;
9834 bfd_vma r_type_mask;
9835 int r_sym_shift;
ffbc01cc 9836 bfd_boolean have_file_sym = FALSE;
c152c796 9837
8b127cbc 9838 output_bfd = flinfo->output_bfd;
c152c796
AM
9839 bed = get_elf_backend_data (output_bfd);
9840 relocate_section = bed->elf_backend_relocate_section;
9841
9842 /* If this is a dynamic object, we don't want to do anything here:
9843 we don't want the local symbols, and we don't want the section
9844 contents. */
9845 if ((input_bfd->flags & DYNAMIC) != 0)
9846 return TRUE;
9847
c152c796
AM
9848 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9849 if (elf_bad_symtab (input_bfd))
9850 {
9851 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9852 extsymoff = 0;
9853 }
9854 else
9855 {
9856 locsymcount = symtab_hdr->sh_info;
9857 extsymoff = symtab_hdr->sh_info;
9858 }
9859
9860 /* Read the local symbols. */
9861 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9862 if (isymbuf == NULL && locsymcount != 0)
9863 {
9864 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9865 flinfo->internal_syms,
9866 flinfo->external_syms,
9867 flinfo->locsym_shndx);
c152c796
AM
9868 if (isymbuf == NULL)
9869 return FALSE;
9870 }
9871
9872 /* Find local symbol sections and adjust values of symbols in
9873 SEC_MERGE sections. Write out those local symbols we know are
9874 going into the output file. */
9875 isymend = isymbuf + locsymcount;
8b127cbc 9876 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9877 isym < isymend;
9878 isym++, pindex++, ppsection++)
9879 {
9880 asection *isec;
9881 const char *name;
9882 Elf_Internal_Sym osym;
6e0b88f1
AM
9883 long indx;
9884 int ret;
c152c796
AM
9885
9886 *pindex = -1;
9887
9888 if (elf_bad_symtab (input_bfd))
9889 {
9890 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9891 {
9892 *ppsection = NULL;
9893 continue;
9894 }
9895 }
9896
9897 if (isym->st_shndx == SHN_UNDEF)
9898 isec = bfd_und_section_ptr;
c152c796
AM
9899 else if (isym->st_shndx == SHN_ABS)
9900 isec = bfd_abs_section_ptr;
9901 else if (isym->st_shndx == SHN_COMMON)
9902 isec = bfd_com_section_ptr;
9903 else
9904 {
cb33740c
AM
9905 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9906 if (isec == NULL)
9907 {
9908 /* Don't attempt to output symbols with st_shnx in the
9909 reserved range other than SHN_ABS and SHN_COMMON. */
9910 *ppsection = NULL;
9911 continue;
9912 }
dbaa2011 9913 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9914 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9915 isym->st_value =
9916 _bfd_merged_section_offset (output_bfd, &isec,
9917 elf_section_data (isec)->sec_info,
9918 isym->st_value);
c152c796
AM
9919 }
9920
9921 *ppsection = isec;
9922
d983c8c5
AM
9923 /* Don't output the first, undefined, symbol. In fact, don't
9924 output any undefined local symbol. */
9925 if (isec == bfd_und_section_ptr)
c152c796
AM
9926 continue;
9927
9928 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9929 {
9930 /* We never output section symbols. Instead, we use the
9931 section symbol of the corresponding section in the output
9932 file. */
9933 continue;
9934 }
9935
9936 /* If we are stripping all symbols, we don't want to output this
9937 one. */
8b127cbc 9938 if (flinfo->info->strip == strip_all)
c152c796
AM
9939 continue;
9940
9941 /* If we are discarding all local symbols, we don't want to
9942 output this one. If we are generating a relocatable output
9943 file, then some of the local symbols may be required by
9944 relocs; we output them below as we discover that they are
9945 needed. */
8b127cbc 9946 if (flinfo->info->discard == discard_all)
c152c796
AM
9947 continue;
9948
9949 /* If this symbol is defined in a section which we are
f02571c5
AM
9950 discarding, we don't need to keep it. */
9951 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9952 && isym->st_shndx < SHN_LORESERVE
9953 && bfd_section_removed_from_list (output_bfd,
9954 isec->output_section))
e75a280b
L
9955 continue;
9956
c152c796
AM
9957 /* Get the name of the symbol. */
9958 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9959 isym->st_name);
9960 if (name == NULL)
9961 return FALSE;
9962
9963 /* See if we are discarding symbols with this name. */
8b127cbc
AM
9964 if ((flinfo->info->strip == strip_some
9965 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 9966 == NULL))
8b127cbc 9967 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
9968 && (isec->flags & SEC_MERGE)
9969 && !bfd_link_relocatable (flinfo->info))
8b127cbc 9970 || flinfo->info->discard == discard_l)
c152c796
AM
9971 && bfd_is_local_label_name (input_bfd, name)))
9972 continue;
9973
ffbc01cc
AM
9974 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9975 {
ce875075
AM
9976 if (input_bfd->lto_output)
9977 /* -flto puts a temp file name here. This means builds
9978 are not reproducible. Discard the symbol. */
9979 continue;
ffbc01cc
AM
9980 have_file_sym = TRUE;
9981 flinfo->filesym_count += 1;
9982 }
9983 if (!have_file_sym)
9984 {
9985 /* In the absence of debug info, bfd_find_nearest_line uses
9986 FILE symbols to determine the source file for local
9987 function symbols. Provide a FILE symbol here if input
9988 files lack such, so that their symbols won't be
9989 associated with a previous input file. It's not the
9990 source file, but the best we can do. */
9991 have_file_sym = TRUE;
9992 flinfo->filesym_count += 1;
9993 memset (&osym, 0, sizeof (osym));
9994 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9995 osym.st_shndx = SHN_ABS;
ef10c3ac
L
9996 if (!elf_link_output_symstrtab (flinfo,
9997 (input_bfd->lto_output ? NULL
9998 : input_bfd->filename),
9999 &osym, bfd_abs_section_ptr,
10000 NULL))
ffbc01cc
AM
10001 return FALSE;
10002 }
10003
c152c796
AM
10004 osym = *isym;
10005
10006 /* Adjust the section index for the output file. */
10007 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10008 isec->output_section);
10009 if (osym.st_shndx == SHN_BAD)
10010 return FALSE;
10011
c152c796
AM
10012 /* ELF symbols in relocatable files are section relative, but
10013 in executable files they are virtual addresses. Note that
10014 this code assumes that all ELF sections have an associated
10015 BFD section with a reasonable value for output_offset; below
10016 we assume that they also have a reasonable value for
10017 output_section. Any special sections must be set up to meet
10018 these requirements. */
10019 osym.st_value += isec->output_offset;
0e1862bb 10020 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10021 {
10022 osym.st_value += isec->output_section->vma;
10023 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10024 {
10025 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10026 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10027 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10028 }
10029 }
10030
6e0b88f1 10031 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10032 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10033 if (ret == 0)
c152c796 10034 return FALSE;
6e0b88f1
AM
10035 else if (ret == 1)
10036 *pindex = indx;
c152c796
AM
10037 }
10038
310fd250
L
10039 if (bed->s->arch_size == 32)
10040 {
10041 r_type_mask = 0xff;
10042 r_sym_shift = 8;
10043 address_size = 4;
10044 }
10045 else
10046 {
10047 r_type_mask = 0xffffffff;
10048 r_sym_shift = 32;
10049 address_size = 8;
10050 }
10051
c152c796
AM
10052 /* Relocate the contents of each section. */
10053 sym_hashes = elf_sym_hashes (input_bfd);
10054 for (o = input_bfd->sections; o != NULL; o = o->next)
10055 {
10056 bfd_byte *contents;
10057
10058 if (! o->linker_mark)
10059 {
10060 /* This section was omitted from the link. */
10061 continue;
10062 }
10063
0e1862bb 10064 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
10065 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10066 {
10067 /* Deal with the group signature symbol. */
10068 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10069 unsigned long symndx = sec_data->this_hdr.sh_info;
10070 asection *osec = o->output_section;
10071
10072 if (symndx >= locsymcount
10073 || (elf_bad_symtab (input_bfd)
8b127cbc 10074 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10075 {
10076 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10077 while (h->root.type == bfd_link_hash_indirect
10078 || h->root.type == bfd_link_hash_warning)
10079 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10080 /* Arrange for symbol to be output. */
10081 h->indx = -2;
10082 elf_section_data (osec)->this_hdr.sh_info = -2;
10083 }
10084 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10085 {
10086 /* We'll use the output section target_index. */
8b127cbc 10087 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10088 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10089 }
10090 else
10091 {
8b127cbc 10092 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10093 {
10094 /* Otherwise output the local symbol now. */
10095 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10096 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10097 const char *name;
6e0b88f1
AM
10098 long indx;
10099 int ret;
bcacc0f5
AM
10100
10101 name = bfd_elf_string_from_elf_section (input_bfd,
10102 symtab_hdr->sh_link,
10103 sym.st_name);
10104 if (name == NULL)
10105 return FALSE;
10106
10107 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10108 sec);
10109 if (sym.st_shndx == SHN_BAD)
10110 return FALSE;
10111
10112 sym.st_value += o->output_offset;
10113
6e0b88f1 10114 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10115 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10116 NULL);
6e0b88f1 10117 if (ret == 0)
bcacc0f5 10118 return FALSE;
6e0b88f1 10119 else if (ret == 1)
8b127cbc 10120 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10121 else
10122 abort ();
bcacc0f5
AM
10123 }
10124 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10125 = flinfo->indices[symndx];
bcacc0f5
AM
10126 }
10127 }
10128
c152c796 10129 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10130 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10131 continue;
10132
10133 if ((o->flags & SEC_LINKER_CREATED) != 0)
10134 {
10135 /* Section was created by _bfd_elf_link_create_dynamic_sections
10136 or somesuch. */
10137 continue;
10138 }
10139
10140 /* Get the contents of the section. They have been cached by a
10141 relaxation routine. Note that o is a section in an input
10142 file, so the contents field will not have been set by any of
10143 the routines which work on output files. */
10144 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10145 {
10146 contents = elf_section_data (o)->this_hdr.contents;
10147 if (bed->caches_rawsize
10148 && o->rawsize != 0
10149 && o->rawsize < o->size)
10150 {
10151 memcpy (flinfo->contents, contents, o->rawsize);
10152 contents = flinfo->contents;
10153 }
10154 }
c152c796
AM
10155 else
10156 {
8b127cbc 10157 contents = flinfo->contents;
4a114e3e 10158 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10159 return FALSE;
10160 }
10161
10162 if ((o->flags & SEC_RELOC) != 0)
10163 {
10164 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10165 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10166 int action_discarded;
ece5ef60 10167 int ret;
c152c796
AM
10168
10169 /* Get the swapped relocs. */
10170 internal_relocs
8b127cbc
AM
10171 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10172 flinfo->internal_relocs, FALSE);
c152c796
AM
10173 if (internal_relocs == NULL
10174 && o->reloc_count > 0)
10175 return FALSE;
10176
310fd250
L
10177 /* We need to reverse-copy input .ctors/.dtors sections if
10178 they are placed in .init_array/.finit_array for output. */
10179 if (o->size > address_size
10180 && ((strncmp (o->name, ".ctors", 6) == 0
10181 && strcmp (o->output_section->name,
10182 ".init_array") == 0)
10183 || (strncmp (o->name, ".dtors", 6) == 0
10184 && strcmp (o->output_section->name,
10185 ".fini_array") == 0))
10186 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10187 {
310fd250
L
10188 if (o->size != o->reloc_count * address_size)
10189 {
10190 (*_bfd_error_handler)
10191 (_("error: %B: size of section %A is not "
10192 "multiple of address size"),
10193 input_bfd, o);
10194 bfd_set_error (bfd_error_on_input);
10195 return FALSE;
10196 }
10197 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10198 }
10199
0f02bbd9 10200 action_discarded = -1;
c152c796 10201 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10202 action_discarded = (*bed->action_discarded) (o);
10203
10204 /* Run through the relocs evaluating complex reloc symbols and
10205 looking for relocs against symbols from discarded sections
10206 or section symbols from removed link-once sections.
10207 Complain about relocs against discarded sections. Zero
10208 relocs against removed link-once sections. */
10209
10210 rel = internal_relocs;
10211 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10212 for ( ; rel < relend; rel++)
c152c796 10213 {
0f02bbd9
AM
10214 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10215 unsigned int s_type;
10216 asection **ps, *sec;
10217 struct elf_link_hash_entry *h = NULL;
10218 const char *sym_name;
c152c796 10219
0f02bbd9
AM
10220 if (r_symndx == STN_UNDEF)
10221 continue;
c152c796 10222
0f02bbd9
AM
10223 if (r_symndx >= locsymcount
10224 || (elf_bad_symtab (input_bfd)
8b127cbc 10225 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10226 {
10227 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10228
0f02bbd9
AM
10229 /* Badly formatted input files can contain relocs that
10230 reference non-existant symbols. Check here so that
10231 we do not seg fault. */
10232 if (h == NULL)
c152c796 10233 {
0f02bbd9 10234 char buffer [32];
dce669a1 10235
0f02bbd9
AM
10236 sprintf_vma (buffer, rel->r_info);
10237 (*_bfd_error_handler)
10238 (_("error: %B contains a reloc (0x%s) for section %A "
10239 "that references a non-existent global symbol"),
10240 input_bfd, o, buffer);
10241 bfd_set_error (bfd_error_bad_value);
10242 return FALSE;
10243 }
3b36f7e6 10244
0f02bbd9
AM
10245 while (h->root.type == bfd_link_hash_indirect
10246 || h->root.type == bfd_link_hash_warning)
10247 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10248
0f02bbd9 10249 s_type = h->type;
cdd3575c 10250
9e2dec47 10251 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10252 mark the symbol as undefined. Note that the
10253 linker may attach linker created dynamic sections
10254 to the plugin bfd. Symbols defined in linker
10255 created sections are not plugin symbols. */
9e2dec47
L
10256 if (h->root.non_ir_ref
10257 && (h->root.type == bfd_link_hash_defined
10258 || h->root.type == bfd_link_hash_defweak)
10259 && (h->root.u.def.section->flags
10260 & SEC_LINKER_CREATED) == 0
10261 && h->root.u.def.section->owner != NULL
10262 && (h->root.u.def.section->owner->flags
10263 & BFD_PLUGIN) != 0)
10264 {
10265 h->root.type = bfd_link_hash_undefined;
10266 h->root.u.undef.abfd = h->root.u.def.section->owner;
10267 }
10268
0f02bbd9
AM
10269 ps = NULL;
10270 if (h->root.type == bfd_link_hash_defined
10271 || h->root.type == bfd_link_hash_defweak)
10272 ps = &h->root.u.def.section;
10273
10274 sym_name = h->root.root.string;
10275 }
10276 else
10277 {
10278 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10279
10280 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10281 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10282 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10283 sym, *ps);
10284 }
c152c796 10285
c301e700 10286 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10287 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10288 {
10289 bfd_vma val;
10290 bfd_vma dot = (rel->r_offset
10291 + o->output_offset + o->output_section->vma);
10292#ifdef DEBUG
10293 printf ("Encountered a complex symbol!");
10294 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10295 input_bfd->filename, o->name,
10296 (long) (rel - internal_relocs));
0f02bbd9
AM
10297 printf (" symbol: idx %8.8lx, name %s\n",
10298 r_symndx, sym_name);
10299 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10300 (unsigned long) rel->r_info,
10301 (unsigned long) rel->r_offset);
10302#endif
8b127cbc 10303 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10304 isymbuf, locsymcount, s_type == STT_SRELC))
10305 return FALSE;
10306
10307 /* Symbol evaluated OK. Update to absolute value. */
10308 set_symbol_value (input_bfd, isymbuf, locsymcount,
10309 r_symndx, val);
10310 continue;
10311 }
10312
10313 if (action_discarded != -1 && ps != NULL)
10314 {
cdd3575c
AM
10315 /* Complain if the definition comes from a
10316 discarded section. */
dbaa2011 10317 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10318 {
cf35638d 10319 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10320 if (action_discarded & COMPLAIN)
8b127cbc 10321 (*flinfo->info->callbacks->einfo)
e1fffbe6 10322 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10323 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10324 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10325
87e5235d 10326 /* Try to do the best we can to support buggy old
e0ae6d6f 10327 versions of gcc. Pretend that the symbol is
87e5235d
AM
10328 really defined in the kept linkonce section.
10329 FIXME: This is quite broken. Modifying the
10330 symbol here means we will be changing all later
e0ae6d6f 10331 uses of the symbol, not just in this section. */
0f02bbd9 10332 if (action_discarded & PRETEND)
87e5235d 10333 {
01b3c8ab
L
10334 asection *kept;
10335
c0f00686 10336 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10337 flinfo->info);
01b3c8ab 10338 if (kept != NULL)
87e5235d
AM
10339 {
10340 *ps = kept;
10341 continue;
10342 }
10343 }
c152c796
AM
10344 }
10345 }
10346 }
10347
10348 /* Relocate the section by invoking a back end routine.
10349
10350 The back end routine is responsible for adjusting the
10351 section contents as necessary, and (if using Rela relocs
10352 and generating a relocatable output file) adjusting the
10353 reloc addend as necessary.
10354
10355 The back end routine does not have to worry about setting
10356 the reloc address or the reloc symbol index.
10357
10358 The back end routine is given a pointer to the swapped in
10359 internal symbols, and can access the hash table entries
10360 for the external symbols via elf_sym_hashes (input_bfd).
10361
10362 When generating relocatable output, the back end routine
10363 must handle STB_LOCAL/STT_SECTION symbols specially. The
10364 output symbol is going to be a section symbol
10365 corresponding to the output section, which will require
10366 the addend to be adjusted. */
10367
8b127cbc 10368 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10369 input_bfd, o, contents,
10370 internal_relocs,
10371 isymbuf,
8b127cbc 10372 flinfo->sections);
ece5ef60 10373 if (!ret)
c152c796
AM
10374 return FALSE;
10375
ece5ef60 10376 if (ret == 2
0e1862bb 10377 || bfd_link_relocatable (flinfo->info)
8b127cbc 10378 || flinfo->info->emitrelocations)
c152c796
AM
10379 {
10380 Elf_Internal_Rela *irela;
d4730f92 10381 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10382 bfd_vma last_offset;
10383 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10384 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10385 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10386 unsigned int next_erel;
c152c796 10387 bfd_boolean rela_normal;
d4730f92 10388 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10389
d4730f92
BS
10390 esdi = elf_section_data (o);
10391 esdo = elf_section_data (o->output_section);
10392 rela_normal = FALSE;
c152c796
AM
10393
10394 /* Adjust the reloc addresses and symbol indices. */
10395
10396 irela = internal_relocs;
10397 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10398 rel_hash = esdo->rel.hashes + esdo->rel.count;
10399 /* We start processing the REL relocs, if any. When we reach
10400 IRELAMID in the loop, we switch to the RELA relocs. */
10401 irelamid = irela;
10402 if (esdi->rel.hdr != NULL)
10403 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10404 * bed->s->int_rels_per_ext_rel);
eac338cf 10405 rel_hash_list = rel_hash;
d4730f92 10406 rela_hash_list = NULL;
c152c796 10407 last_offset = o->output_offset;
0e1862bb 10408 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10409 last_offset += o->output_section->vma;
10410 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10411 {
10412 unsigned long r_symndx;
10413 asection *sec;
10414 Elf_Internal_Sym sym;
10415
10416 if (next_erel == bed->s->int_rels_per_ext_rel)
10417 {
10418 rel_hash++;
10419 next_erel = 0;
10420 }
10421
d4730f92
BS
10422 if (irela == irelamid)
10423 {
10424 rel_hash = esdo->rela.hashes + esdo->rela.count;
10425 rela_hash_list = rel_hash;
10426 rela_normal = bed->rela_normal;
10427 }
10428
c152c796 10429 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10430 flinfo->info, o,
c152c796
AM
10431 irela->r_offset);
10432 if (irela->r_offset >= (bfd_vma) -2)
10433 {
10434 /* This is a reloc for a deleted entry or somesuch.
10435 Turn it into an R_*_NONE reloc, at the same
10436 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10437 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10438 being ordered. */
10439 irela->r_offset = last_offset;
10440 irela->r_info = 0;
10441 irela->r_addend = 0;
10442 continue;
10443 }
10444
10445 irela->r_offset += o->output_offset;
10446
10447 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10448 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10449 irela->r_offset += o->output_section->vma;
10450
10451 last_offset = irela->r_offset;
10452
10453 r_symndx = irela->r_info >> r_sym_shift;
10454 if (r_symndx == STN_UNDEF)
10455 continue;
10456
10457 if (r_symndx >= locsymcount
10458 || (elf_bad_symtab (input_bfd)
8b127cbc 10459 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10460 {
10461 struct elf_link_hash_entry *rh;
10462 unsigned long indx;
10463
10464 /* This is a reloc against a global symbol. We
10465 have not yet output all the local symbols, so
10466 we do not know the symbol index of any global
10467 symbol. We set the rel_hash entry for this
10468 reloc to point to the global hash table entry
10469 for this symbol. The symbol index is then
ee75fd95 10470 set at the end of bfd_elf_final_link. */
c152c796
AM
10471 indx = r_symndx - extsymoff;
10472 rh = elf_sym_hashes (input_bfd)[indx];
10473 while (rh->root.type == bfd_link_hash_indirect
10474 || rh->root.type == bfd_link_hash_warning)
10475 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10476
10477 /* Setting the index to -2 tells
10478 elf_link_output_extsym that this symbol is
10479 used by a reloc. */
10480 BFD_ASSERT (rh->indx < 0);
10481 rh->indx = -2;
10482
10483 *rel_hash = rh;
10484
10485 continue;
10486 }
10487
10488 /* This is a reloc against a local symbol. */
10489
10490 *rel_hash = NULL;
10491 sym = isymbuf[r_symndx];
8b127cbc 10492 sec = flinfo->sections[r_symndx];
c152c796
AM
10493 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10494 {
10495 /* I suppose the backend ought to fill in the
10496 section of any STT_SECTION symbol against a
6a8d1586 10497 processor specific section. */
cf35638d 10498 r_symndx = STN_UNDEF;
6a8d1586
AM
10499 if (bfd_is_abs_section (sec))
10500 ;
c152c796
AM
10501 else if (sec == NULL || sec->owner == NULL)
10502 {
10503 bfd_set_error (bfd_error_bad_value);
10504 return FALSE;
10505 }
10506 else
10507 {
6a8d1586
AM
10508 asection *osec = sec->output_section;
10509
10510 /* If we have discarded a section, the output
10511 section will be the absolute section. In
ab96bf03
AM
10512 case of discarded SEC_MERGE sections, use
10513 the kept section. relocate_section should
10514 have already handled discarded linkonce
10515 sections. */
6a8d1586
AM
10516 if (bfd_is_abs_section (osec)
10517 && sec->kept_section != NULL
10518 && sec->kept_section->output_section != NULL)
10519 {
10520 osec = sec->kept_section->output_section;
10521 irela->r_addend -= osec->vma;
10522 }
10523
10524 if (!bfd_is_abs_section (osec))
10525 {
10526 r_symndx = osec->target_index;
cf35638d 10527 if (r_symndx == STN_UNDEF)
74541ad4 10528 {
051d833a
AM
10529 irela->r_addend += osec->vma;
10530 osec = _bfd_nearby_section (output_bfd, osec,
10531 osec->vma);
10532 irela->r_addend -= osec->vma;
10533 r_symndx = osec->target_index;
74541ad4 10534 }
6a8d1586 10535 }
c152c796
AM
10536 }
10537
10538 /* Adjust the addend according to where the
10539 section winds up in the output section. */
10540 if (rela_normal)
10541 irela->r_addend += sec->output_offset;
10542 }
10543 else
10544 {
8b127cbc 10545 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10546 {
10547 unsigned long shlink;
10548 const char *name;
10549 asection *osec;
6e0b88f1 10550 long indx;
c152c796 10551
8b127cbc 10552 if (flinfo->info->strip == strip_all)
c152c796
AM
10553 {
10554 /* You can't do ld -r -s. */
10555 bfd_set_error (bfd_error_invalid_operation);
10556 return FALSE;
10557 }
10558
10559 /* This symbol was skipped earlier, but
10560 since it is needed by a reloc, we
10561 must output it now. */
10562 shlink = symtab_hdr->sh_link;
10563 name = (bfd_elf_string_from_elf_section
10564 (input_bfd, shlink, sym.st_name));
10565 if (name == NULL)
10566 return FALSE;
10567
10568 osec = sec->output_section;
10569 sym.st_shndx =
10570 _bfd_elf_section_from_bfd_section (output_bfd,
10571 osec);
10572 if (sym.st_shndx == SHN_BAD)
10573 return FALSE;
10574
10575 sym.st_value += sec->output_offset;
0e1862bb 10576 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10577 {
10578 sym.st_value += osec->vma;
10579 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10580 {
10581 /* STT_TLS symbols are relative to PT_TLS
10582 segment base. */
8b127cbc 10583 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10584 ->tls_sec != NULL);
8b127cbc 10585 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10586 ->tls_sec->vma);
10587 }
10588 }
10589
6e0b88f1 10590 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10591 ret = elf_link_output_symstrtab (flinfo, name,
10592 &sym, sec,
10593 NULL);
6e0b88f1 10594 if (ret == 0)
c152c796 10595 return FALSE;
6e0b88f1 10596 else if (ret == 1)
8b127cbc 10597 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10598 else
10599 abort ();
c152c796
AM
10600 }
10601
8b127cbc 10602 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10603 }
10604
10605 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10606 | (irela->r_info & r_type_mask));
10607 }
10608
10609 /* Swap out the relocs. */
d4730f92
BS
10610 input_rel_hdr = esdi->rel.hdr;
10611 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10612 {
d4730f92
BS
10613 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10614 input_rel_hdr,
10615 internal_relocs,
10616 rel_hash_list))
10617 return FALSE;
c152c796
AM
10618 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10619 * bed->s->int_rels_per_ext_rel);
eac338cf 10620 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10621 }
10622
10623 input_rela_hdr = esdi->rela.hdr;
10624 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10625 {
eac338cf 10626 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10627 input_rela_hdr,
eac338cf 10628 internal_relocs,
d4730f92 10629 rela_hash_list))
c152c796
AM
10630 return FALSE;
10631 }
10632 }
10633 }
10634
10635 /* Write out the modified section contents. */
10636 if (bed->elf_backend_write_section
8b127cbc 10637 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10638 contents))
c152c796
AM
10639 {
10640 /* Section written out. */
10641 }
10642 else switch (o->sec_info_type)
10643 {
dbaa2011 10644 case SEC_INFO_TYPE_STABS:
c152c796
AM
10645 if (! (_bfd_write_section_stabs
10646 (output_bfd,
8b127cbc 10647 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10648 o, &elf_section_data (o)->sec_info, contents)))
10649 return FALSE;
10650 break;
dbaa2011 10651 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10652 if (! _bfd_write_merged_section (output_bfd, o,
10653 elf_section_data (o)->sec_info))
10654 return FALSE;
10655 break;
dbaa2011 10656 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10657 {
8b127cbc 10658 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10659 o, contents))
10660 return FALSE;
10661 }
10662 break;
2f0c68f2
CM
10663 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10664 {
10665 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10666 flinfo->info,
10667 o, contents))
10668 return FALSE;
10669 }
10670 break;
c152c796
AM
10671 default:
10672 {
310fd250
L
10673 if (! (o->flags & SEC_EXCLUDE))
10674 {
10675 file_ptr offset = (file_ptr) o->output_offset;
10676 bfd_size_type todo = o->size;
37b01f6a
DG
10677
10678 offset *= bfd_octets_per_byte (output_bfd);
10679
310fd250
L
10680 if ((o->flags & SEC_ELF_REVERSE_COPY))
10681 {
10682 /* Reverse-copy input section to output. */
10683 do
10684 {
10685 todo -= address_size;
10686 if (! bfd_set_section_contents (output_bfd,
10687 o->output_section,
10688 contents + todo,
10689 offset,
10690 address_size))
10691 return FALSE;
10692 if (todo == 0)
10693 break;
10694 offset += address_size;
10695 }
10696 while (1);
10697 }
10698 else if (! bfd_set_section_contents (output_bfd,
10699 o->output_section,
10700 contents,
10701 offset, todo))
10702 return FALSE;
10703 }
c152c796
AM
10704 }
10705 break;
10706 }
10707 }
10708
10709 return TRUE;
10710}
10711
10712/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10713 requested by the linker, and does not come from any input file. This
c152c796
AM
10714 is used to build constructor and destructor tables when linking
10715 with -Ur. */
10716
10717static bfd_boolean
10718elf_reloc_link_order (bfd *output_bfd,
10719 struct bfd_link_info *info,
10720 asection *output_section,
10721 struct bfd_link_order *link_order)
10722{
10723 reloc_howto_type *howto;
10724 long indx;
10725 bfd_vma offset;
10726 bfd_vma addend;
d4730f92 10727 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10728 struct elf_link_hash_entry **rel_hash_ptr;
10729 Elf_Internal_Shdr *rel_hdr;
10730 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10731 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10732 bfd_byte *erel;
10733 unsigned int i;
d4730f92 10734 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10735
10736 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10737 if (howto == NULL)
10738 {
10739 bfd_set_error (bfd_error_bad_value);
10740 return FALSE;
10741 }
10742
10743 addend = link_order->u.reloc.p->addend;
10744
d4730f92
BS
10745 if (esdo->rel.hdr)
10746 reldata = &esdo->rel;
10747 else if (esdo->rela.hdr)
10748 reldata = &esdo->rela;
10749 else
10750 {
10751 reldata = NULL;
10752 BFD_ASSERT (0);
10753 }
10754
c152c796 10755 /* Figure out the symbol index. */
d4730f92 10756 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10757 if (link_order->type == bfd_section_reloc_link_order)
10758 {
10759 indx = link_order->u.reloc.p->u.section->target_index;
10760 BFD_ASSERT (indx != 0);
10761 *rel_hash_ptr = NULL;
10762 }
10763 else
10764 {
10765 struct elf_link_hash_entry *h;
10766
10767 /* Treat a reloc against a defined symbol as though it were
10768 actually against the section. */
10769 h = ((struct elf_link_hash_entry *)
10770 bfd_wrapped_link_hash_lookup (output_bfd, info,
10771 link_order->u.reloc.p->u.name,
10772 FALSE, FALSE, TRUE));
10773 if (h != NULL
10774 && (h->root.type == bfd_link_hash_defined
10775 || h->root.type == bfd_link_hash_defweak))
10776 {
10777 asection *section;
10778
10779 section = h->root.u.def.section;
10780 indx = section->output_section->target_index;
10781 *rel_hash_ptr = NULL;
10782 /* It seems that we ought to add the symbol value to the
10783 addend here, but in practice it has already been added
10784 because it was passed to constructor_callback. */
10785 addend += section->output_section->vma + section->output_offset;
10786 }
10787 else if (h != NULL)
10788 {
10789 /* Setting the index to -2 tells elf_link_output_extsym that
10790 this symbol is used by a reloc. */
10791 h->indx = -2;
10792 *rel_hash_ptr = h;
10793 indx = 0;
10794 }
10795 else
10796 {
1a72702b
AM
10797 (*info->callbacks->unattached_reloc)
10798 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
10799 indx = 0;
10800 }
10801 }
10802
10803 /* If this is an inplace reloc, we must write the addend into the
10804 object file. */
10805 if (howto->partial_inplace && addend != 0)
10806 {
10807 bfd_size_type size;
10808 bfd_reloc_status_type rstat;
10809 bfd_byte *buf;
10810 bfd_boolean ok;
10811 const char *sym_name;
10812
a50b1753
NC
10813 size = (bfd_size_type) bfd_get_reloc_size (howto);
10814 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10815 if (buf == NULL && size != 0)
c152c796
AM
10816 return FALSE;
10817 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10818 switch (rstat)
10819 {
10820 case bfd_reloc_ok:
10821 break;
10822
10823 default:
10824 case bfd_reloc_outofrange:
10825 abort ();
10826
10827 case bfd_reloc_overflow:
10828 if (link_order->type == bfd_section_reloc_link_order)
10829 sym_name = bfd_section_name (output_bfd,
10830 link_order->u.reloc.p->u.section);
10831 else
10832 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
10833 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10834 howto->name, addend, NULL, NULL,
10835 (bfd_vma) 0);
c152c796
AM
10836 break;
10837 }
37b01f6a 10838
c152c796 10839 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
10840 link_order->offset
10841 * bfd_octets_per_byte (output_bfd),
10842 size);
c152c796
AM
10843 free (buf);
10844 if (! ok)
10845 return FALSE;
10846 }
10847
10848 /* The address of a reloc is relative to the section in a
10849 relocatable file, and is a virtual address in an executable
10850 file. */
10851 offset = link_order->offset;
0e1862bb 10852 if (! bfd_link_relocatable (info))
c152c796
AM
10853 offset += output_section->vma;
10854
10855 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10856 {
10857 irel[i].r_offset = offset;
10858 irel[i].r_info = 0;
10859 irel[i].r_addend = 0;
10860 }
10861 if (bed->s->arch_size == 32)
10862 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10863 else
10864 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10865
d4730f92 10866 rel_hdr = reldata->hdr;
c152c796
AM
10867 erel = rel_hdr->contents;
10868 if (rel_hdr->sh_type == SHT_REL)
10869 {
d4730f92 10870 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10871 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10872 }
10873 else
10874 {
10875 irel[0].r_addend = addend;
d4730f92 10876 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10877 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10878 }
10879
d4730f92 10880 ++reldata->count;
c152c796
AM
10881
10882 return TRUE;
10883}
10884
0b52efa6
PB
10885
10886/* Get the output vma of the section pointed to by the sh_link field. */
10887
10888static bfd_vma
10889elf_get_linked_section_vma (struct bfd_link_order *p)
10890{
10891 Elf_Internal_Shdr **elf_shdrp;
10892 asection *s;
10893 int elfsec;
10894
10895 s = p->u.indirect.section;
10896 elf_shdrp = elf_elfsections (s->owner);
10897 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10898 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10899 /* PR 290:
10900 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10901 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10902 sh_info fields. Hence we could get the situation
10903 where elfsec is 0. */
10904 if (elfsec == 0)
10905 {
10906 const struct elf_backend_data *bed
10907 = get_elf_backend_data (s->owner);
10908 if (bed->link_order_error_handler)
d003868e
AM
10909 bed->link_order_error_handler
10910 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10911 return 0;
10912 }
10913 else
10914 {
10915 s = elf_shdrp[elfsec]->bfd_section;
10916 return s->output_section->vma + s->output_offset;
10917 }
0b52efa6
PB
10918}
10919
10920
10921/* Compare two sections based on the locations of the sections they are
10922 linked to. Used by elf_fixup_link_order. */
10923
10924static int
10925compare_link_order (const void * a, const void * b)
10926{
10927 bfd_vma apos;
10928 bfd_vma bpos;
10929
10930 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10931 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10932 if (apos < bpos)
10933 return -1;
10934 return apos > bpos;
10935}
10936
10937
10938/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10939 order as their linked sections. Returns false if this could not be done
10940 because an output section includes both ordered and unordered
10941 sections. Ideally we'd do this in the linker proper. */
10942
10943static bfd_boolean
10944elf_fixup_link_order (bfd *abfd, asection *o)
10945{
10946 int seen_linkorder;
10947 int seen_other;
10948 int n;
10949 struct bfd_link_order *p;
10950 bfd *sub;
10951 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10952 unsigned elfsec;
0b52efa6 10953 struct bfd_link_order **sections;
d33cdfe3 10954 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10955 bfd_vma offset;
3b36f7e6 10956
d33cdfe3
L
10957 other_sec = NULL;
10958 linkorder_sec = NULL;
0b52efa6
PB
10959 seen_other = 0;
10960 seen_linkorder = 0;
8423293d 10961 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10962 {
d33cdfe3 10963 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10964 {
10965 s = p->u.indirect.section;
d33cdfe3
L
10966 sub = s->owner;
10967 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10968 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10969 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10970 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10971 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10972 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10973 {
10974 seen_linkorder++;
10975 linkorder_sec = s;
10976 }
0b52efa6 10977 else
d33cdfe3
L
10978 {
10979 seen_other++;
10980 other_sec = s;
10981 }
0b52efa6
PB
10982 }
10983 else
10984 seen_other++;
d33cdfe3
L
10985
10986 if (seen_other && seen_linkorder)
10987 {
10988 if (other_sec && linkorder_sec)
10989 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10990 o, linkorder_sec,
10991 linkorder_sec->owner, other_sec,
10992 other_sec->owner);
10993 else
10994 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10995 o);
10996 bfd_set_error (bfd_error_bad_value);
10997 return FALSE;
10998 }
0b52efa6
PB
10999 }
11000
11001 if (!seen_linkorder)
11002 return TRUE;
11003
0b52efa6 11004 sections = (struct bfd_link_order **)
14b1c01e
AM
11005 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11006 if (sections == NULL)
11007 return FALSE;
0b52efa6 11008 seen_linkorder = 0;
3b36f7e6 11009
8423293d 11010 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11011 {
11012 sections[seen_linkorder++] = p;
11013 }
11014 /* Sort the input sections in the order of their linked section. */
11015 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11016 compare_link_order);
11017
11018 /* Change the offsets of the sections. */
11019 offset = 0;
11020 for (n = 0; n < seen_linkorder; n++)
11021 {
11022 s = sections[n]->u.indirect.section;
461686a3 11023 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11024 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11025 sections[n]->offset = offset;
11026 offset += sections[n]->size;
11027 }
11028
4dd07732 11029 free (sections);
0b52efa6
PB
11030 return TRUE;
11031}
11032
76359541
TP
11033/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11034 Returns TRUE upon success, FALSE otherwise. */
11035
11036static bfd_boolean
11037elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11038{
11039 bfd_boolean ret = FALSE;
11040 bfd *implib_bfd;
11041 const struct elf_backend_data *bed;
11042 flagword flags;
11043 enum bfd_architecture arch;
11044 unsigned int mach;
11045 asymbol **sympp = NULL;
11046 long symsize;
11047 long symcount;
11048 long src_count;
11049 elf_symbol_type *osymbuf;
11050
11051 implib_bfd = info->out_implib_bfd;
11052 bed = get_elf_backend_data (abfd);
11053
11054 if (!bfd_set_format (implib_bfd, bfd_object))
11055 return FALSE;
11056
11057 flags = bfd_get_file_flags (abfd);
11058 flags &= ~HAS_RELOC;
11059 if (!bfd_set_start_address (implib_bfd, 0)
11060 || !bfd_set_file_flags (implib_bfd, flags))
11061 return FALSE;
11062
11063 /* Copy architecture of output file to import library file. */
11064 arch = bfd_get_arch (abfd);
11065 mach = bfd_get_mach (abfd);
11066 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11067 && (abfd->target_defaulted
11068 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11069 return FALSE;
11070
11071 /* Get symbol table size. */
11072 symsize = bfd_get_symtab_upper_bound (abfd);
11073 if (symsize < 0)
11074 return FALSE;
11075
11076 /* Read in the symbol table. */
11077 sympp = (asymbol **) xmalloc (symsize);
11078 symcount = bfd_canonicalize_symtab (abfd, sympp);
11079 if (symcount < 0)
11080 goto free_sym_buf;
11081
11082 /* Allow the BFD backend to copy any private header data it
11083 understands from the output BFD to the import library BFD. */
11084 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11085 goto free_sym_buf;
11086
11087 /* Filter symbols to appear in the import library. */
11088 if (bed->elf_backend_filter_implib_symbols)
11089 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11090 symcount);
11091 else
11092 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11093 if (symcount == 0)
11094 {
5df1bc57 11095 bfd_set_error (bfd_error_no_symbols);
76359541
TP
11096 (*_bfd_error_handler) (_("%B: no symbol found for import library"),
11097 implib_bfd);
11098 goto free_sym_buf;
11099 }
11100
11101
11102 /* Make symbols absolute. */
11103 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11104 sizeof (*osymbuf));
11105 for (src_count = 0; src_count < symcount; src_count++)
11106 {
11107 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11108 sizeof (*osymbuf));
11109 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11110 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11111 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11112 osymbuf[src_count].internal_elf_sym.st_value =
11113 osymbuf[src_count].symbol.value;
11114 sympp[src_count] = &osymbuf[src_count].symbol;
11115 }
11116
11117 bfd_set_symtab (implib_bfd, sympp, symcount);
11118
11119 /* Allow the BFD backend to copy any private data it understands
11120 from the output BFD to the import library BFD. This is done last
11121 to permit the routine to look at the filtered symbol table. */
11122 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11123 goto free_sym_buf;
11124
11125 if (!bfd_close (implib_bfd))
11126 goto free_sym_buf;
11127
11128 ret = TRUE;
11129
11130free_sym_buf:
11131 free (sympp);
11132 return ret;
11133}
11134
9f7c3e5e
AM
11135static void
11136elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11137{
11138 asection *o;
11139
11140 if (flinfo->symstrtab != NULL)
ef10c3ac 11141 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11142 if (flinfo->contents != NULL)
11143 free (flinfo->contents);
11144 if (flinfo->external_relocs != NULL)
11145 free (flinfo->external_relocs);
11146 if (flinfo->internal_relocs != NULL)
11147 free (flinfo->internal_relocs);
11148 if (flinfo->external_syms != NULL)
11149 free (flinfo->external_syms);
11150 if (flinfo->locsym_shndx != NULL)
11151 free (flinfo->locsym_shndx);
11152 if (flinfo->internal_syms != NULL)
11153 free (flinfo->internal_syms);
11154 if (flinfo->indices != NULL)
11155 free (flinfo->indices);
11156 if (flinfo->sections != NULL)
11157 free (flinfo->sections);
9f7c3e5e
AM
11158 if (flinfo->symshndxbuf != NULL)
11159 free (flinfo->symshndxbuf);
11160 for (o = obfd->sections; o != NULL; o = o->next)
11161 {
11162 struct bfd_elf_section_data *esdo = elf_section_data (o);
11163 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11164 free (esdo->rel.hashes);
11165 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11166 free (esdo->rela.hashes);
11167 }
11168}
0b52efa6 11169
c152c796
AM
11170/* Do the final step of an ELF link. */
11171
11172bfd_boolean
11173bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11174{
11175 bfd_boolean dynamic;
11176 bfd_boolean emit_relocs;
11177 bfd *dynobj;
8b127cbc 11178 struct elf_final_link_info flinfo;
91d6fa6a
NC
11179 asection *o;
11180 struct bfd_link_order *p;
11181 bfd *sub;
c152c796
AM
11182 bfd_size_type max_contents_size;
11183 bfd_size_type max_external_reloc_size;
11184 bfd_size_type max_internal_reloc_count;
11185 bfd_size_type max_sym_count;
11186 bfd_size_type max_sym_shndx_count;
c152c796
AM
11187 Elf_Internal_Sym elfsym;
11188 unsigned int i;
11189 Elf_Internal_Shdr *symtab_hdr;
11190 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11191 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11192 struct elf_outext_info eoinfo;
11193 bfd_boolean merged;
11194 size_t relativecount = 0;
11195 asection *reldyn = 0;
11196 bfd_size_type amt;
104d59d1
JM
11197 asection *attr_section = NULL;
11198 bfd_vma attr_size = 0;
11199 const char *std_attrs_section;
c152c796
AM
11200
11201 if (! is_elf_hash_table (info->hash))
11202 return FALSE;
11203
0e1862bb 11204 if (bfd_link_pic (info))
c152c796
AM
11205 abfd->flags |= DYNAMIC;
11206
11207 dynamic = elf_hash_table (info)->dynamic_sections_created;
11208 dynobj = elf_hash_table (info)->dynobj;
11209
0e1862bb 11210 emit_relocs = (bfd_link_relocatable (info)
a4676736 11211 || info->emitrelocations);
c152c796 11212
8b127cbc
AM
11213 flinfo.info = info;
11214 flinfo.output_bfd = abfd;
ef10c3ac 11215 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11216 if (flinfo.symstrtab == NULL)
c152c796
AM
11217 return FALSE;
11218
11219 if (! dynamic)
11220 {
8b127cbc
AM
11221 flinfo.hash_sec = NULL;
11222 flinfo.symver_sec = NULL;
c152c796
AM
11223 }
11224 else
11225 {
3d4d4302 11226 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11227 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11228 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11229 /* Note that it is OK if symver_sec is NULL. */
11230 }
11231
8b127cbc
AM
11232 flinfo.contents = NULL;
11233 flinfo.external_relocs = NULL;
11234 flinfo.internal_relocs = NULL;
11235 flinfo.external_syms = NULL;
11236 flinfo.locsym_shndx = NULL;
11237 flinfo.internal_syms = NULL;
11238 flinfo.indices = NULL;
11239 flinfo.sections = NULL;
8b127cbc 11240 flinfo.symshndxbuf = NULL;
ffbc01cc 11241 flinfo.filesym_count = 0;
c152c796 11242
104d59d1
JM
11243 /* The object attributes have been merged. Remove the input
11244 sections from the link, and set the contents of the output
11245 secton. */
11246 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11247 for (o = abfd->sections; o != NULL; o = o->next)
11248 {
11249 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11250 || strcmp (o->name, ".gnu.attributes") == 0)
11251 {
11252 for (p = o->map_head.link_order; p != NULL; p = p->next)
11253 {
11254 asection *input_section;
11255
11256 if (p->type != bfd_indirect_link_order)
11257 continue;
11258 input_section = p->u.indirect.section;
11259 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11260 elf_link_input_bfd ignores this section. */
11261 input_section->flags &= ~SEC_HAS_CONTENTS;
11262 }
a0c8462f 11263
104d59d1
JM
11264 attr_size = bfd_elf_obj_attr_size (abfd);
11265 if (attr_size)
11266 {
11267 bfd_set_section_size (abfd, o, attr_size);
11268 attr_section = o;
11269 /* Skip this section later on. */
11270 o->map_head.link_order = NULL;
11271 }
11272 else
11273 o->flags |= SEC_EXCLUDE;
11274 }
11275 }
11276
c152c796
AM
11277 /* Count up the number of relocations we will output for each output
11278 section, so that we know the sizes of the reloc sections. We
11279 also figure out some maximum sizes. */
11280 max_contents_size = 0;
11281 max_external_reloc_size = 0;
11282 max_internal_reloc_count = 0;
11283 max_sym_count = 0;
11284 max_sym_shndx_count = 0;
11285 merged = FALSE;
11286 for (o = abfd->sections; o != NULL; o = o->next)
11287 {
11288 struct bfd_elf_section_data *esdo = elf_section_data (o);
11289 o->reloc_count = 0;
11290
8423293d 11291 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11292 {
11293 unsigned int reloc_count = 0;
491d01d3 11294 unsigned int additional_reloc_count = 0;
c152c796 11295 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11296
11297 if (p->type == bfd_section_reloc_link_order
11298 || p->type == bfd_symbol_reloc_link_order)
11299 reloc_count = 1;
11300 else if (p->type == bfd_indirect_link_order)
11301 {
11302 asection *sec;
11303
11304 sec = p->u.indirect.section;
11305 esdi = elf_section_data (sec);
11306
11307 /* Mark all sections which are to be included in the
11308 link. This will normally be every section. We need
11309 to do this so that we can identify any sections which
11310 the linker has decided to not include. */
11311 sec->linker_mark = TRUE;
11312
11313 if (sec->flags & SEC_MERGE)
11314 merged = TRUE;
11315
aed64b35
L
11316 if (esdo->this_hdr.sh_type == SHT_REL
11317 || esdo->this_hdr.sh_type == SHT_RELA)
11318 /* Some backends use reloc_count in relocation sections
11319 to count particular types of relocs. Of course,
11320 reloc sections themselves can't have relocations. */
11321 reloc_count = 0;
0e1862bb 11322 else if (emit_relocs)
491d01d3
YU
11323 {
11324 reloc_count = sec->reloc_count;
11325 if (bed->elf_backend_count_additional_relocs)
11326 {
11327 int c;
11328 c = (*bed->elf_backend_count_additional_relocs) (sec);
11329 additional_reloc_count += c;
11330 }
11331 }
c152c796 11332 else if (bed->elf_backend_count_relocs)
58217f29 11333 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 11334
eea6121a
AM
11335 if (sec->rawsize > max_contents_size)
11336 max_contents_size = sec->rawsize;
11337 if (sec->size > max_contents_size)
11338 max_contents_size = sec->size;
c152c796
AM
11339
11340 /* We are interested in just local symbols, not all
11341 symbols. */
11342 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11343 && (sec->owner->flags & DYNAMIC) == 0)
11344 {
11345 size_t sym_count;
11346
11347 if (elf_bad_symtab (sec->owner))
11348 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11349 / bed->s->sizeof_sym);
11350 else
11351 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11352
11353 if (sym_count > max_sym_count)
11354 max_sym_count = sym_count;
11355
11356 if (sym_count > max_sym_shndx_count
6a40cf0c 11357 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11358 max_sym_shndx_count = sym_count;
11359
11360 if ((sec->flags & SEC_RELOC) != 0)
11361 {
d4730f92 11362 size_t ext_size = 0;
c152c796 11363
d4730f92
BS
11364 if (esdi->rel.hdr != NULL)
11365 ext_size = esdi->rel.hdr->sh_size;
11366 if (esdi->rela.hdr != NULL)
11367 ext_size += esdi->rela.hdr->sh_size;
7326c758 11368
c152c796
AM
11369 if (ext_size > max_external_reloc_size)
11370 max_external_reloc_size = ext_size;
11371 if (sec->reloc_count > max_internal_reloc_count)
11372 max_internal_reloc_count = sec->reloc_count;
11373 }
11374 }
11375 }
11376
11377 if (reloc_count == 0)
11378 continue;
11379
491d01d3 11380 reloc_count += additional_reloc_count;
c152c796
AM
11381 o->reloc_count += reloc_count;
11382
0e1862bb 11383 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11384 {
d4730f92 11385 if (esdi->rel.hdr)
491d01d3
YU
11386 {
11387 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11388 esdo->rel.count += additional_reloc_count;
11389 }
d4730f92 11390 if (esdi->rela.hdr)
491d01d3
YU
11391 {
11392 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11393 esdo->rela.count += additional_reloc_count;
11394 }
d4730f92
BS
11395 }
11396 else
11397 {
11398 if (o->use_rela_p)
11399 esdo->rela.count += reloc_count;
2c2b4ed4 11400 else
d4730f92 11401 esdo->rel.count += reloc_count;
c152c796 11402 }
c152c796
AM
11403 }
11404
11405 if (o->reloc_count > 0)
11406 o->flags |= SEC_RELOC;
11407 else
11408 {
11409 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11410 set it (this is probably a bug) and if it is set
11411 assign_section_numbers will create a reloc section. */
11412 o->flags &=~ SEC_RELOC;
11413 }
11414
11415 /* If the SEC_ALLOC flag is not set, force the section VMA to
11416 zero. This is done in elf_fake_sections as well, but forcing
11417 the VMA to 0 here will ensure that relocs against these
11418 sections are handled correctly. */
11419 if ((o->flags & SEC_ALLOC) == 0
11420 && ! o->user_set_vma)
11421 o->vma = 0;
11422 }
11423
0e1862bb 11424 if (! bfd_link_relocatable (info) && merged)
c152c796
AM
11425 elf_link_hash_traverse (elf_hash_table (info),
11426 _bfd_elf_link_sec_merge_syms, abfd);
11427
11428 /* Figure out the file positions for everything but the symbol table
11429 and the relocs. We set symcount to force assign_section_numbers
11430 to create a symbol table. */
8539e4e8 11431 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11432 BFD_ASSERT (! abfd->output_has_begun);
11433 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11434 goto error_return;
11435
ee75fd95 11436 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11437 for (o = abfd->sections; o != NULL; o = o->next)
11438 {
d4730f92 11439 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11440 if ((o->flags & SEC_RELOC) != 0)
11441 {
d4730f92
BS
11442 if (esdo->rel.hdr
11443 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11444 goto error_return;
11445
d4730f92
BS
11446 if (esdo->rela.hdr
11447 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11448 goto error_return;
11449 }
11450
11451 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11452 to count upwards while actually outputting the relocations. */
d4730f92
BS
11453 esdo->rel.count = 0;
11454 esdo->rela.count = 0;
0ce398f1
L
11455
11456 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11457 {
11458 /* Cache the section contents so that they can be compressed
11459 later. Use bfd_malloc since it will be freed by
11460 bfd_compress_section_contents. */
11461 unsigned char *contents = esdo->this_hdr.contents;
11462 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11463 abort ();
11464 contents
11465 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11466 if (contents == NULL)
11467 goto error_return;
11468 esdo->this_hdr.contents = contents;
11469 }
c152c796
AM
11470 }
11471
c152c796 11472 /* We have now assigned file positions for all the sections except
a485e98e
AM
11473 .symtab, .strtab, and non-loaded reloc sections. We start the
11474 .symtab section at the current file position, and write directly
11475 to it. We build the .strtab section in memory. */
c152c796
AM
11476 bfd_get_symcount (abfd) = 0;
11477 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11478 /* sh_name is set in prep_headers. */
11479 symtab_hdr->sh_type = SHT_SYMTAB;
11480 /* sh_flags, sh_addr and sh_size all start off zero. */
11481 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11482 /* sh_link is set in assign_section_numbers. */
11483 /* sh_info is set below. */
11484 /* sh_offset is set just below. */
72de5009 11485 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11486
ef10c3ac
L
11487 if (max_sym_count < 20)
11488 max_sym_count = 20;
11489 elf_hash_table (info)->strtabsize = max_sym_count;
11490 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11491 elf_hash_table (info)->strtab
11492 = (struct elf_sym_strtab *) bfd_malloc (amt);
11493 if (elf_hash_table (info)->strtab == NULL)
c152c796 11494 goto error_return;
ef10c3ac
L
11495 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11496 flinfo.symshndxbuf
11497 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11498 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11499
8539e4e8 11500 if (info->strip != strip_all || emit_relocs)
c152c796 11501 {
8539e4e8
AM
11502 file_ptr off = elf_next_file_pos (abfd);
11503
11504 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11505
11506 /* Note that at this point elf_next_file_pos (abfd) is
11507 incorrect. We do not yet know the size of the .symtab section.
11508 We correct next_file_pos below, after we do know the size. */
11509
11510 /* Start writing out the symbol table. The first symbol is always a
11511 dummy symbol. */
c152c796
AM
11512 elfsym.st_value = 0;
11513 elfsym.st_size = 0;
11514 elfsym.st_info = 0;
11515 elfsym.st_other = 0;
11516 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11517 elfsym.st_target_internal = 0;
ef10c3ac
L
11518 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11519 bfd_und_section_ptr, NULL) != 1)
c152c796 11520 goto error_return;
c152c796 11521
8539e4e8
AM
11522 /* Output a symbol for each section. We output these even if we are
11523 discarding local symbols, since they are used for relocs. These
11524 symbols have no names. We store the index of each one in the
11525 index field of the section, so that we can find it again when
11526 outputting relocs. */
11527
c152c796
AM
11528 elfsym.st_size = 0;
11529 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11530 elfsym.st_other = 0;
f0b5bb34 11531 elfsym.st_value = 0;
35fc36a8 11532 elfsym.st_target_internal = 0;
c152c796
AM
11533 for (i = 1; i < elf_numsections (abfd); i++)
11534 {
11535 o = bfd_section_from_elf_index (abfd, i);
11536 if (o != NULL)
f0b5bb34
AM
11537 {
11538 o->target_index = bfd_get_symcount (abfd);
11539 elfsym.st_shndx = i;
0e1862bb 11540 if (!bfd_link_relocatable (info))
f0b5bb34 11541 elfsym.st_value = o->vma;
ef10c3ac
L
11542 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11543 NULL) != 1)
f0b5bb34
AM
11544 goto error_return;
11545 }
c152c796
AM
11546 }
11547 }
11548
11549 /* Allocate some memory to hold information read in from the input
11550 files. */
11551 if (max_contents_size != 0)
11552 {
8b127cbc
AM
11553 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11554 if (flinfo.contents == NULL)
c152c796
AM
11555 goto error_return;
11556 }
11557
11558 if (max_external_reloc_size != 0)
11559 {
8b127cbc
AM
11560 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11561 if (flinfo.external_relocs == NULL)
c152c796
AM
11562 goto error_return;
11563 }
11564
11565 if (max_internal_reloc_count != 0)
11566 {
11567 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11568 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11569 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11570 if (flinfo.internal_relocs == NULL)
c152c796
AM
11571 goto error_return;
11572 }
11573
11574 if (max_sym_count != 0)
11575 {
11576 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11577 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11578 if (flinfo.external_syms == NULL)
c152c796
AM
11579 goto error_return;
11580
11581 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11582 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11583 if (flinfo.internal_syms == NULL)
c152c796
AM
11584 goto error_return;
11585
11586 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11587 flinfo.indices = (long int *) bfd_malloc (amt);
11588 if (flinfo.indices == NULL)
c152c796
AM
11589 goto error_return;
11590
11591 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11592 flinfo.sections = (asection **) bfd_malloc (amt);
11593 if (flinfo.sections == NULL)
c152c796
AM
11594 goto error_return;
11595 }
11596
11597 if (max_sym_shndx_count != 0)
11598 {
11599 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11600 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11601 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11602 goto error_return;
11603 }
11604
11605 if (elf_hash_table (info)->tls_sec)
11606 {
11607 bfd_vma base, end = 0;
11608 asection *sec;
11609
11610 for (sec = elf_hash_table (info)->tls_sec;
11611 sec && (sec->flags & SEC_THREAD_LOCAL);
11612 sec = sec->next)
11613 {
3a800eb9 11614 bfd_size_type size = sec->size;
c152c796 11615
3a800eb9
AM
11616 if (size == 0
11617 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11618 {
91d6fa6a
NC
11619 struct bfd_link_order *ord = sec->map_tail.link_order;
11620
11621 if (ord != NULL)
11622 size = ord->offset + ord->size;
c152c796
AM
11623 }
11624 end = sec->vma + size;
11625 }
11626 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
11627 /* Only align end of TLS section if static TLS doesn't have special
11628 alignment requirements. */
11629 if (bed->static_tls_alignment == 1)
11630 end = align_power (end,
11631 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
11632 elf_hash_table (info)->tls_size = end - base;
11633 }
11634
0b52efa6
PB
11635 /* Reorder SHF_LINK_ORDER sections. */
11636 for (o = abfd->sections; o != NULL; o = o->next)
11637 {
11638 if (!elf_fixup_link_order (abfd, o))
11639 return FALSE;
11640 }
11641
2f0c68f2
CM
11642 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11643 return FALSE;
11644
c152c796
AM
11645 /* Since ELF permits relocations to be against local symbols, we
11646 must have the local symbols available when we do the relocations.
11647 Since we would rather only read the local symbols once, and we
11648 would rather not keep them in memory, we handle all the
11649 relocations for a single input file at the same time.
11650
11651 Unfortunately, there is no way to know the total number of local
11652 symbols until we have seen all of them, and the local symbol
11653 indices precede the global symbol indices. This means that when
11654 we are generating relocatable output, and we see a reloc against
11655 a global symbol, we can not know the symbol index until we have
11656 finished examining all the local symbols to see which ones we are
11657 going to output. To deal with this, we keep the relocations in
11658 memory, and don't output them until the end of the link. This is
11659 an unfortunate waste of memory, but I don't see a good way around
11660 it. Fortunately, it only happens when performing a relocatable
11661 link, which is not the common case. FIXME: If keep_memory is set
11662 we could write the relocs out and then read them again; I don't
11663 know how bad the memory loss will be. */
11664
c72f2fb2 11665 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11666 sub->output_has_begun = FALSE;
11667 for (o = abfd->sections; o != NULL; o = o->next)
11668 {
8423293d 11669 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11670 {
11671 if (p->type == bfd_indirect_link_order
11672 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11673 == bfd_target_elf_flavour)
11674 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11675 {
11676 if (! sub->output_has_begun)
11677 {
8b127cbc 11678 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11679 goto error_return;
11680 sub->output_has_begun = TRUE;
11681 }
11682 }
11683 else if (p->type == bfd_section_reloc_link_order
11684 || p->type == bfd_symbol_reloc_link_order)
11685 {
11686 if (! elf_reloc_link_order (abfd, info, o, p))
11687 goto error_return;
11688 }
11689 else
11690 {
11691 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11692 {
11693 if (p->type == bfd_indirect_link_order
11694 && (bfd_get_flavour (sub)
11695 == bfd_target_elf_flavour)
11696 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11697 != bed->s->elfclass))
11698 {
11699 const char *iclass, *oclass;
11700
aebf9be7 11701 switch (bed->s->elfclass)
351f65ca 11702 {
aebf9be7
NC
11703 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11704 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11705 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11706 default: abort ();
351f65ca 11707 }
aebf9be7
NC
11708
11709 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11710 {
aebf9be7
NC
11711 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11712 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11713 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11714 default: abort ();
351f65ca
L
11715 }
11716
11717 bfd_set_error (bfd_error_wrong_format);
11718 (*_bfd_error_handler)
11719 (_("%B: file class %s incompatible with %s"),
11720 sub, iclass, oclass);
11721 }
11722
11723 goto error_return;
11724 }
c152c796
AM
11725 }
11726 }
11727 }
11728
c0f00686
L
11729 /* Free symbol buffer if needed. */
11730 if (!info->reduce_memory_overheads)
11731 {
c72f2fb2 11732 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11733 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11734 && elf_tdata (sub)->symbuf)
c0f00686
L
11735 {
11736 free (elf_tdata (sub)->symbuf);
11737 elf_tdata (sub)->symbuf = NULL;
11738 }
11739 }
11740
c152c796
AM
11741 /* Output any global symbols that got converted to local in a
11742 version script or due to symbol visibility. We do this in a
11743 separate step since ELF requires all local symbols to appear
11744 prior to any global symbols. FIXME: We should only do this if
11745 some global symbols were, in fact, converted to become local.
11746 FIXME: Will this work correctly with the Irix 5 linker? */
11747 eoinfo.failed = FALSE;
8b127cbc 11748 eoinfo.flinfo = &flinfo;
c152c796 11749 eoinfo.localsyms = TRUE;
34a79995 11750 eoinfo.file_sym_done = FALSE;
7686d77d 11751 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11752 if (eoinfo.failed)
11753 return FALSE;
11754
4e617b1e
PB
11755 /* If backend needs to output some local symbols not present in the hash
11756 table, do it now. */
8539e4e8
AM
11757 if (bed->elf_backend_output_arch_local_syms
11758 && (info->strip != strip_all || emit_relocs))
4e617b1e 11759 {
6e0b88f1 11760 typedef int (*out_sym_func)
4e617b1e
PB
11761 (void *, const char *, Elf_Internal_Sym *, asection *,
11762 struct elf_link_hash_entry *);
11763
11764 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11765 (abfd, info, &flinfo,
11766 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11767 return FALSE;
11768 }
11769
c152c796
AM
11770 /* That wrote out all the local symbols. Finish up the symbol table
11771 with the global symbols. Even if we want to strip everything we
11772 can, we still need to deal with those global symbols that got
11773 converted to local in a version script. */
11774
11775 /* The sh_info field records the index of the first non local symbol. */
11776 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11777
11778 if (dynamic
cae1fbbb
L
11779 && elf_hash_table (info)->dynsym != NULL
11780 && (elf_hash_table (info)->dynsym->output_section
11781 != bfd_abs_section_ptr))
c152c796
AM
11782 {
11783 Elf_Internal_Sym sym;
cae1fbbb 11784 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
90ac2420
AM
11785
11786 o = elf_hash_table (info)->dynsym->output_section;
11787 elf_section_data (o)->this_hdr.sh_info
11788 = elf_hash_table (info)->local_dynsymcount + 1;
c152c796
AM
11789
11790 /* Write out the section symbols for the output sections. */
0e1862bb
L
11791 if (bfd_link_pic (info)
11792 || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
11793 {
11794 asection *s;
11795
11796 sym.st_size = 0;
11797 sym.st_name = 0;
11798 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11799 sym.st_other = 0;
35fc36a8 11800 sym.st_target_internal = 0;
c152c796
AM
11801
11802 for (s = abfd->sections; s != NULL; s = s->next)
11803 {
11804 int indx;
11805 bfd_byte *dest;
11806 long dynindx;
11807
c152c796 11808 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11809 if (dynindx <= 0)
11810 continue;
11811 indx = elf_section_data (s)->this_idx;
c152c796
AM
11812 BFD_ASSERT (indx > 0);
11813 sym.st_shndx = indx;
c0d5a53d
L
11814 if (! check_dynsym (abfd, &sym))
11815 return FALSE;
c152c796
AM
11816 sym.st_value = s->vma;
11817 dest = dynsym + dynindx * bed->s->sizeof_sym;
11818 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11819 }
c152c796
AM
11820 }
11821
11822 /* Write out the local dynsyms. */
11823 if (elf_hash_table (info)->dynlocal)
11824 {
11825 struct elf_link_local_dynamic_entry *e;
11826 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11827 {
11828 asection *s;
11829 bfd_byte *dest;
11830
935bd1e0 11831 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11832 Note that we saved a word of storage and overwrote
11833 the original st_name with the dynstr_index. */
11834 sym = e->isym;
935bd1e0 11835 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11836
cb33740c
AM
11837 s = bfd_section_from_elf_index (e->input_bfd,
11838 e->isym.st_shndx);
11839 if (s != NULL)
c152c796 11840 {
c152c796
AM
11841 sym.st_shndx =
11842 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11843 if (! check_dynsym (abfd, &sym))
11844 return FALSE;
c152c796
AM
11845 sym.st_value = (s->output_section->vma
11846 + s->output_offset
11847 + e->isym.st_value);
11848 }
11849
c152c796
AM
11850 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11851 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11852 }
11853 }
c152c796
AM
11854 }
11855
11856 /* We get the global symbols from the hash table. */
11857 eoinfo.failed = FALSE;
11858 eoinfo.localsyms = FALSE;
8b127cbc 11859 eoinfo.flinfo = &flinfo;
7686d77d 11860 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11861 if (eoinfo.failed)
11862 return FALSE;
11863
11864 /* If backend needs to output some symbols not present in the hash
11865 table, do it now. */
8539e4e8
AM
11866 if (bed->elf_backend_output_arch_syms
11867 && (info->strip != strip_all || emit_relocs))
c152c796 11868 {
6e0b88f1 11869 typedef int (*out_sym_func)
c152c796
AM
11870 (void *, const char *, Elf_Internal_Sym *, asection *,
11871 struct elf_link_hash_entry *);
11872
11873 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
11874 (abfd, info, &flinfo,
11875 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
11876 return FALSE;
11877 }
11878
ef10c3ac
L
11879 /* Finalize the .strtab section. */
11880 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11881
11882 /* Swap out the .strtab section. */
11883 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
11884 return FALSE;
11885
11886 /* Now we know the size of the symtab section. */
c152c796
AM
11887 if (bfd_get_symcount (abfd) > 0)
11888 {
ee3b52e9
L
11889 /* Finish up and write out the symbol string table (.strtab)
11890 section. */
11891 Elf_Internal_Shdr *symstrtab_hdr;
8539e4e8
AM
11892 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11893
6a40cf0c
NC
11894 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11895 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
8539e4e8
AM
11896 {
11897 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11898 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11899 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11900 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11901 symtab_shndx_hdr->sh_size = amt;
11902
11903 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11904 off, TRUE);
11905
11906 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11907 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11908 return FALSE;
11909 }
ee3b52e9
L
11910
11911 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11912 /* sh_name was set in prep_headers. */
11913 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 11914 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 11915 symstrtab_hdr->sh_addr = 0;
ef10c3ac 11916 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
11917 symstrtab_hdr->sh_entsize = 0;
11918 symstrtab_hdr->sh_link = 0;
11919 symstrtab_hdr->sh_info = 0;
11920 /* sh_offset is set just below. */
11921 symstrtab_hdr->sh_addralign = 1;
11922
11923 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11924 off, TRUE);
11925 elf_next_file_pos (abfd) = off;
11926
c152c796 11927 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 11928 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
11929 return FALSE;
11930 }
11931
76359541
TP
11932 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
11933 {
11934 (*_bfd_error_handler) (_("%B: failed to generate import library"),
11935 info->out_implib_bfd);
11936 return FALSE;
11937 }
11938
c152c796
AM
11939 /* Adjust the relocs to have the correct symbol indices. */
11940 for (o = abfd->sections; o != NULL; o = o->next)
11941 {
d4730f92 11942 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 11943 bfd_boolean sort;
c152c796
AM
11944 if ((o->flags & SEC_RELOC) == 0)
11945 continue;
11946
28dbcedc 11947 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3
AM
11948 if (esdo->rel.hdr != NULL
11949 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11950 return FALSE;
11951 if (esdo->rela.hdr != NULL
11952 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11953 return FALSE;
c152c796
AM
11954
11955 /* Set the reloc_count field to 0 to prevent write_relocs from
11956 trying to swap the relocs out itself. */
11957 o->reloc_count = 0;
11958 }
11959
11960 if (dynamic && info->combreloc && dynobj != NULL)
11961 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11962
11963 /* If we are linking against a dynamic object, or generating a
11964 shared library, finish up the dynamic linking information. */
11965 if (dynamic)
11966 {
11967 bfd_byte *dyncon, *dynconend;
11968
11969 /* Fix up .dynamic entries. */
3d4d4302 11970 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
11971 BFD_ASSERT (o != NULL);
11972
11973 dyncon = o->contents;
eea6121a 11974 dynconend = o->contents + o->size;
c152c796
AM
11975 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11976 {
11977 Elf_Internal_Dyn dyn;
11978 const char *name;
11979 unsigned int type;
11980
11981 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11982
11983 switch (dyn.d_tag)
11984 {
11985 default:
11986 continue;
11987 case DT_NULL:
11988 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11989 {
11990 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11991 {
11992 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11993 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11994 default: continue;
11995 }
11996 dyn.d_un.d_val = relativecount;
11997 relativecount = 0;
11998 break;
11999 }
12000 continue;
12001
12002 case DT_INIT:
12003 name = info->init_function;
12004 goto get_sym;
12005 case DT_FINI:
12006 name = info->fini_function;
12007 get_sym:
12008 {
12009 struct elf_link_hash_entry *h;
12010
12011 h = elf_link_hash_lookup (elf_hash_table (info), name,
12012 FALSE, FALSE, TRUE);
12013 if (h != NULL
12014 && (h->root.type == bfd_link_hash_defined
12015 || h->root.type == bfd_link_hash_defweak))
12016 {
bef26483 12017 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12018 o = h->root.u.def.section;
12019 if (o->output_section != NULL)
bef26483 12020 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12021 + o->output_offset);
12022 else
12023 {
12024 /* The symbol is imported from another shared
12025 library and does not apply to this one. */
bef26483 12026 dyn.d_un.d_ptr = 0;
c152c796
AM
12027 }
12028 break;
12029 }
12030 }
12031 continue;
12032
12033 case DT_PREINIT_ARRAYSZ:
12034 name = ".preinit_array";
4ade44b7 12035 goto get_out_size;
c152c796
AM
12036 case DT_INIT_ARRAYSZ:
12037 name = ".init_array";
4ade44b7 12038 goto get_out_size;
c152c796
AM
12039 case DT_FINI_ARRAYSZ:
12040 name = ".fini_array";
4ade44b7 12041 get_out_size:
c152c796
AM
12042 o = bfd_get_section_by_name (abfd, name);
12043 if (o == NULL)
12044 {
12045 (*_bfd_error_handler)
4ade44b7 12046 (_("could not find section %s"), name);
c152c796
AM
12047 goto error_return;
12048 }
eea6121a 12049 if (o->size == 0)
c152c796
AM
12050 (*_bfd_error_handler)
12051 (_("warning: %s section has zero size"), name);
eea6121a 12052 dyn.d_un.d_val = o->size;
c152c796
AM
12053 break;
12054
12055 case DT_PREINIT_ARRAY:
12056 name = ".preinit_array";
4ade44b7 12057 goto get_out_vma;
c152c796
AM
12058 case DT_INIT_ARRAY:
12059 name = ".init_array";
4ade44b7 12060 goto get_out_vma;
c152c796
AM
12061 case DT_FINI_ARRAY:
12062 name = ".fini_array";
4ade44b7
AM
12063 get_out_vma:
12064 o = bfd_get_section_by_name (abfd, name);
12065 goto do_vma;
c152c796
AM
12066
12067 case DT_HASH:
12068 name = ".hash";
12069 goto get_vma;
fdc90cb4
JJ
12070 case DT_GNU_HASH:
12071 name = ".gnu.hash";
12072 goto get_vma;
c152c796
AM
12073 case DT_STRTAB:
12074 name = ".dynstr";
12075 goto get_vma;
12076 case DT_SYMTAB:
12077 name = ".dynsym";
12078 goto get_vma;
12079 case DT_VERDEF:
12080 name = ".gnu.version_d";
12081 goto get_vma;
12082 case DT_VERNEED:
12083 name = ".gnu.version_r";
12084 goto get_vma;
12085 case DT_VERSYM:
12086 name = ".gnu.version";
12087 get_vma:
4ade44b7
AM
12088 o = bfd_get_linker_section (dynobj, name);
12089 do_vma:
c152c796
AM
12090 if (o == NULL)
12091 {
12092 (*_bfd_error_handler)
4ade44b7 12093 (_("could not find section %s"), name);
c152c796
AM
12094 goto error_return;
12095 }
894891db
NC
12096 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12097 {
12098 (*_bfd_error_handler)
12099 (_("warning: section '%s' is being made into a note"), name);
12100 bfd_set_error (bfd_error_nonrepresentable_section);
12101 goto error_return;
12102 }
4ade44b7 12103 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12104 break;
12105
12106 case DT_REL:
12107 case DT_RELA:
12108 case DT_RELSZ:
12109 case DT_RELASZ:
12110 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12111 type = SHT_REL;
12112 else
12113 type = SHT_RELA;
12114 dyn.d_un.d_val = 0;
bef26483 12115 dyn.d_un.d_ptr = 0;
c152c796
AM
12116 for (i = 1; i < elf_numsections (abfd); i++)
12117 {
12118 Elf_Internal_Shdr *hdr;
12119
12120 hdr = elf_elfsections (abfd)[i];
12121 if (hdr->sh_type == type
12122 && (hdr->sh_flags & SHF_ALLOC) != 0)
12123 {
12124 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12125 dyn.d_un.d_val += hdr->sh_size;
12126 else
12127 {
bef26483
AM
12128 if (dyn.d_un.d_ptr == 0
12129 || hdr->sh_addr < dyn.d_un.d_ptr)
12130 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
12131 }
12132 }
12133 }
12134 break;
12135 }
12136 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12137 }
12138 }
12139
12140 /* If we have created any dynamic sections, then output them. */
12141 if (dynobj != NULL)
12142 {
12143 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12144 goto error_return;
12145
943284cc 12146 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12147 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12148 || info->error_textrel)
3d4d4302 12149 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12150 {
12151 bfd_byte *dyncon, *dynconend;
12152
943284cc
DJ
12153 dyncon = o->contents;
12154 dynconend = o->contents + o->size;
12155 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12156 {
12157 Elf_Internal_Dyn dyn;
12158
12159 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12160
12161 if (dyn.d_tag == DT_TEXTREL)
12162 {
c192a133
AM
12163 if (info->error_textrel)
12164 info->callbacks->einfo
12165 (_("%P%X: read-only segment has dynamic relocations.\n"));
12166 else
12167 info->callbacks->einfo
12168 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12169 break;
12170 }
12171 }
12172 }
12173
c152c796
AM
12174 for (o = dynobj->sections; o != NULL; o = o->next)
12175 {
12176 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12177 || o->size == 0
c152c796
AM
12178 || o->output_section == bfd_abs_section_ptr)
12179 continue;
12180 if ((o->flags & SEC_LINKER_CREATED) == 0)
12181 {
12182 /* At this point, we are only interested in sections
12183 created by _bfd_elf_link_create_dynamic_sections. */
12184 continue;
12185 }
3722b82f
AM
12186 if (elf_hash_table (info)->stab_info.stabstr == o)
12187 continue;
eea6121a
AM
12188 if (elf_hash_table (info)->eh_info.hdr_sec == o)
12189 continue;
3d4d4302 12190 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12191 {
12192 if (! bfd_set_section_contents (abfd, o->output_section,
12193 o->contents,
37b01f6a
DG
12194 (file_ptr) o->output_offset
12195 * bfd_octets_per_byte (abfd),
eea6121a 12196 o->size))
c152c796
AM
12197 goto error_return;
12198 }
12199 else
12200 {
12201 /* The contents of the .dynstr section are actually in a
12202 stringtab. */
8539e4e8
AM
12203 file_ptr off;
12204
c152c796
AM
12205 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12206 if (bfd_seek (abfd, off, SEEK_SET) != 0
12207 || ! _bfd_elf_strtab_emit (abfd,
12208 elf_hash_table (info)->dynstr))
12209 goto error_return;
12210 }
12211 }
12212 }
12213
0e1862bb 12214 if (bfd_link_relocatable (info))
c152c796
AM
12215 {
12216 bfd_boolean failed = FALSE;
12217
12218 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12219 if (failed)
12220 goto error_return;
12221 }
12222
12223 /* If we have optimized stabs strings, output them. */
3722b82f 12224 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
12225 {
12226 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
12227 goto error_return;
12228 }
12229
9f7c3e5e
AM
12230 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12231 goto error_return;
c152c796 12232
9f7c3e5e 12233 elf_final_link_free (abfd, &flinfo);
c152c796 12234
12bd6957 12235 elf_linker (abfd) = TRUE;
c152c796 12236
104d59d1
JM
12237 if (attr_section)
12238 {
a50b1753 12239 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12240 if (contents == NULL)
d0f16d5e 12241 return FALSE; /* Bail out and fail. */
104d59d1
JM
12242 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12243 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12244 free (contents);
12245 }
12246
c152c796
AM
12247 return TRUE;
12248
12249 error_return:
9f7c3e5e 12250 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12251 return FALSE;
12252}
12253\f
5241d853
RS
12254/* Initialize COOKIE for input bfd ABFD. */
12255
12256static bfd_boolean
12257init_reloc_cookie (struct elf_reloc_cookie *cookie,
12258 struct bfd_link_info *info, bfd *abfd)
12259{
12260 Elf_Internal_Shdr *symtab_hdr;
12261 const struct elf_backend_data *bed;
12262
12263 bed = get_elf_backend_data (abfd);
12264 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12265
12266 cookie->abfd = abfd;
12267 cookie->sym_hashes = elf_sym_hashes (abfd);
12268 cookie->bad_symtab = elf_bad_symtab (abfd);
12269 if (cookie->bad_symtab)
12270 {
12271 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12272 cookie->extsymoff = 0;
12273 }
12274 else
12275 {
12276 cookie->locsymcount = symtab_hdr->sh_info;
12277 cookie->extsymoff = symtab_hdr->sh_info;
12278 }
12279
12280 if (bed->s->arch_size == 32)
12281 cookie->r_sym_shift = 8;
12282 else
12283 cookie->r_sym_shift = 32;
12284
12285 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12286 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12287 {
12288 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12289 cookie->locsymcount, 0,
12290 NULL, NULL, NULL);
12291 if (cookie->locsyms == NULL)
12292 {
12293 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12294 return FALSE;
12295 }
12296 if (info->keep_memory)
12297 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12298 }
12299 return TRUE;
12300}
12301
12302/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12303
12304static void
12305fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12306{
12307 Elf_Internal_Shdr *symtab_hdr;
12308
12309 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12310 if (cookie->locsyms != NULL
12311 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12312 free (cookie->locsyms);
12313}
12314
12315/* Initialize the relocation information in COOKIE for input section SEC
12316 of input bfd ABFD. */
12317
12318static bfd_boolean
12319init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12320 struct bfd_link_info *info, bfd *abfd,
12321 asection *sec)
12322{
12323 const struct elf_backend_data *bed;
12324
12325 if (sec->reloc_count == 0)
12326 {
12327 cookie->rels = NULL;
12328 cookie->relend = NULL;
12329 }
12330 else
12331 {
12332 bed = get_elf_backend_data (abfd);
12333
12334 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12335 info->keep_memory);
12336 if (cookie->rels == NULL)
12337 return FALSE;
12338 cookie->rel = cookie->rels;
12339 cookie->relend = (cookie->rels
12340 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12341 }
12342 cookie->rel = cookie->rels;
12343 return TRUE;
12344}
12345
12346/* Free the memory allocated by init_reloc_cookie_rels,
12347 if appropriate. */
12348
12349static void
12350fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12351 asection *sec)
12352{
12353 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12354 free (cookie->rels);
12355}
12356
12357/* Initialize the whole of COOKIE for input section SEC. */
12358
12359static bfd_boolean
12360init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12361 struct bfd_link_info *info,
12362 asection *sec)
12363{
12364 if (!init_reloc_cookie (cookie, info, sec->owner))
12365 goto error1;
12366 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12367 goto error2;
12368 return TRUE;
12369
12370 error2:
12371 fini_reloc_cookie (cookie, sec->owner);
12372 error1:
12373 return FALSE;
12374}
12375
12376/* Free the memory allocated by init_reloc_cookie_for_section,
12377 if appropriate. */
12378
12379static void
12380fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12381 asection *sec)
12382{
12383 fini_reloc_cookie_rels (cookie, sec);
12384 fini_reloc_cookie (cookie, sec->owner);
12385}
12386\f
c152c796
AM
12387/* Garbage collect unused sections. */
12388
07adf181
AM
12389/* Default gc_mark_hook. */
12390
12391asection *
12392_bfd_elf_gc_mark_hook (asection *sec,
12393 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12394 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12395 struct elf_link_hash_entry *h,
12396 Elf_Internal_Sym *sym)
12397{
12398 if (h != NULL)
12399 {
12400 switch (h->root.type)
12401 {
12402 case bfd_link_hash_defined:
12403 case bfd_link_hash_defweak:
12404 return h->root.u.def.section;
12405
12406 case bfd_link_hash_common:
12407 return h->root.u.c.p->section;
12408
12409 default:
12410 break;
12411 }
12412 }
12413 else
12414 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12415
12416 return NULL;
12417}
12418
a6a4679f
AM
12419/* For undefined __start_<name> and __stop_<name> symbols, return the
12420 first input section matching <name>. Return NULL otherwise. */
12421
12422asection *
12423_bfd_elf_is_start_stop (const struct bfd_link_info *info,
12424 struct elf_link_hash_entry *h)
12425{
12426 asection *s;
12427 const char *sec_name;
12428
12429 if (h->root.type != bfd_link_hash_undefined
12430 && h->root.type != bfd_link_hash_undefweak)
12431 return NULL;
12432
12433 s = h->root.u.undef.section;
12434 if (s != NULL)
12435 {
12436 if (s == (asection *) 0 - 1)
12437 return NULL;
12438 return s;
12439 }
12440
12441 sec_name = NULL;
12442 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12443 sec_name = h->root.root.string + 8;
12444 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12445 sec_name = h->root.root.string + 7;
12446
12447 if (sec_name != NULL && *sec_name != '\0')
12448 {
12449 bfd *i;
12450
12451 for (i = info->input_bfds; i != NULL; i = i->link.next)
12452 {
12453 s = bfd_get_section_by_name (i, sec_name);
12454 if (s != NULL)
12455 {
12456 h->root.u.undef.section = s;
12457 break;
12458 }
12459 }
12460 }
12461
12462 if (s == NULL)
12463 h->root.u.undef.section = (asection *) 0 - 1;
12464
12465 return s;
12466}
12467
5241d853
RS
12468/* COOKIE->rel describes a relocation against section SEC, which is
12469 a section we've decided to keep. Return the section that contains
12470 the relocation symbol, or NULL if no section contains it. */
12471
12472asection *
12473_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12474 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12475 struct elf_reloc_cookie *cookie,
12476 bfd_boolean *start_stop)
5241d853
RS
12477{
12478 unsigned long r_symndx;
12479 struct elf_link_hash_entry *h;
12480
12481 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12482 if (r_symndx == STN_UNDEF)
5241d853
RS
12483 return NULL;
12484
12485 if (r_symndx >= cookie->locsymcount
12486 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12487 {
12488 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12489 if (h == NULL)
12490 {
12491 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12492 sec->owner);
12493 return NULL;
12494 }
5241d853
RS
12495 while (h->root.type == bfd_link_hash_indirect
12496 || h->root.type == bfd_link_hash_warning)
12497 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12498 h->mark = 1;
4e6b54a6
AM
12499 /* If this symbol is weak and there is a non-weak definition, we
12500 keep the non-weak definition because many backends put
12501 dynamic reloc info on the non-weak definition for code
12502 handling copy relocs. */
12503 if (h->u.weakdef != NULL)
12504 h->u.weakdef->mark = 1;
1cce69b9 12505
a6a4679f 12506 if (start_stop != NULL)
1cce69b9
AM
12507 {
12508 /* To work around a glibc bug, mark all XXX input sections
12509 when there is an as yet undefined reference to __start_XXX
12510 or __stop_XXX symbols. The linker will later define such
12511 symbols for orphan input sections that have a name
12512 representable as a C identifier. */
a6a4679f 12513 asection *s = _bfd_elf_is_start_stop (info, h);
1cce69b9 12514
a6a4679f 12515 if (s != NULL)
1cce69b9 12516 {
a6a4679f
AM
12517 *start_stop = !s->gc_mark;
12518 return s;
1cce69b9
AM
12519 }
12520 }
12521
5241d853
RS
12522 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12523 }
12524
12525 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12526 &cookie->locsyms[r_symndx]);
12527}
12528
12529/* COOKIE->rel describes a relocation against section SEC, which is
12530 a section we've decided to keep. Mark the section that contains
9d0a14d3 12531 the relocation symbol. */
5241d853
RS
12532
12533bfd_boolean
12534_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12535 asection *sec,
12536 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12537 struct elf_reloc_cookie *cookie)
5241d853
RS
12538{
12539 asection *rsec;
1cce69b9 12540 bfd_boolean start_stop = FALSE;
5241d853 12541
1cce69b9
AM
12542 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12543 while (rsec != NULL)
5241d853 12544 {
1cce69b9
AM
12545 if (!rsec->gc_mark)
12546 {
12547 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12548 || (rsec->owner->flags & DYNAMIC) != 0)
12549 rsec->gc_mark = 1;
12550 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12551 return FALSE;
12552 }
12553 if (!start_stop)
12554 break;
199af150 12555 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12556 }
12557 return TRUE;
12558}
12559
07adf181
AM
12560/* The mark phase of garbage collection. For a given section, mark
12561 it and any sections in this section's group, and all the sections
12562 which define symbols to which it refers. */
12563
ccfa59ea
AM
12564bfd_boolean
12565_bfd_elf_gc_mark (struct bfd_link_info *info,
12566 asection *sec,
6a5bb875 12567 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12568{
12569 bfd_boolean ret;
9d0a14d3 12570 asection *group_sec, *eh_frame;
c152c796
AM
12571
12572 sec->gc_mark = 1;
12573
12574 /* Mark all the sections in the group. */
12575 group_sec = elf_section_data (sec)->next_in_group;
12576 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12577 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12578 return FALSE;
12579
12580 /* Look through the section relocs. */
12581 ret = TRUE;
9d0a14d3
RS
12582 eh_frame = elf_eh_frame_section (sec->owner);
12583 if ((sec->flags & SEC_RELOC) != 0
12584 && sec->reloc_count > 0
12585 && sec != eh_frame)
c152c796 12586 {
5241d853 12587 struct elf_reloc_cookie cookie;
c152c796 12588
5241d853
RS
12589 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12590 ret = FALSE;
c152c796 12591 else
c152c796 12592 {
5241d853 12593 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12594 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12595 {
12596 ret = FALSE;
12597 break;
12598 }
12599 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12600 }
12601 }
9d0a14d3
RS
12602
12603 if (ret && eh_frame && elf_fde_list (sec))
12604 {
12605 struct elf_reloc_cookie cookie;
12606
12607 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12608 ret = FALSE;
12609 else
12610 {
12611 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12612 gc_mark_hook, &cookie))
12613 ret = FALSE;
12614 fini_reloc_cookie_for_section (&cookie, eh_frame);
12615 }
12616 }
12617
2f0c68f2
CM
12618 eh_frame = elf_section_eh_frame_entry (sec);
12619 if (ret && eh_frame && !eh_frame->gc_mark)
12620 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12621 ret = FALSE;
12622
c152c796
AM
12623 return ret;
12624}
12625
3c758495
TG
12626/* Scan and mark sections in a special or debug section group. */
12627
12628static void
12629_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12630{
12631 /* Point to first section of section group. */
12632 asection *ssec;
12633 /* Used to iterate the section group. */
12634 asection *msec;
12635
12636 bfd_boolean is_special_grp = TRUE;
12637 bfd_boolean is_debug_grp = TRUE;
12638
12639 /* First scan to see if group contains any section other than debug
12640 and special section. */
12641 ssec = msec = elf_next_in_group (grp);
12642 do
12643 {
12644 if ((msec->flags & SEC_DEBUGGING) == 0)
12645 is_debug_grp = FALSE;
12646
12647 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12648 is_special_grp = FALSE;
12649
12650 msec = elf_next_in_group (msec);
12651 }
12652 while (msec != ssec);
12653
12654 /* If this is a pure debug section group or pure special section group,
12655 keep all sections in this group. */
12656 if (is_debug_grp || is_special_grp)
12657 {
12658 do
12659 {
12660 msec->gc_mark = 1;
12661 msec = elf_next_in_group (msec);
12662 }
12663 while (msec != ssec);
12664 }
12665}
12666
7f6ab9f8
AM
12667/* Keep debug and special sections. */
12668
12669bfd_boolean
12670_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12671 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12672{
12673 bfd *ibfd;
12674
c72f2fb2 12675 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12676 {
12677 asection *isec;
12678 bfd_boolean some_kept;
b40bf0a2 12679 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12680
12681 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12682 continue;
12683
b40bf0a2
NC
12684 /* Ensure all linker created sections are kept,
12685 see if any other section is already marked,
12686 and note if we have any fragmented debug sections. */
12687 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12688 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12689 {
12690 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12691 isec->gc_mark = 1;
12692 else if (isec->gc_mark)
12693 some_kept = TRUE;
b40bf0a2
NC
12694
12695 if (debug_frag_seen == FALSE
12696 && (isec->flags & SEC_DEBUGGING)
12697 && CONST_STRNEQ (isec->name, ".debug_line."))
12698 debug_frag_seen = TRUE;
7f6ab9f8
AM
12699 }
12700
12701 /* If no section in this file will be kept, then we can
b40bf0a2 12702 toss out the debug and special sections. */
7f6ab9f8
AM
12703 if (!some_kept)
12704 continue;
12705
12706 /* Keep debug and special sections like .comment when they are
3c758495
TG
12707 not part of a group. Also keep section groups that contain
12708 just debug sections or special sections. */
7f6ab9f8 12709 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12710 {
12711 if ((isec->flags & SEC_GROUP) != 0)
12712 _bfd_elf_gc_mark_debug_special_section_group (isec);
12713 else if (((isec->flags & SEC_DEBUGGING) != 0
12714 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12715 && elf_next_in_group (isec) == NULL)
12716 isec->gc_mark = 1;
12717 }
b40bf0a2
NC
12718
12719 if (! debug_frag_seen)
12720 continue;
12721
12722 /* Look for CODE sections which are going to be discarded,
12723 and find and discard any fragmented debug sections which
12724 are associated with that code section. */
12725 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12726 if ((isec->flags & SEC_CODE) != 0
12727 && isec->gc_mark == 0)
12728 {
12729 unsigned int ilen;
12730 asection *dsec;
12731
12732 ilen = strlen (isec->name);
12733
12734 /* Association is determined by the name of the debug section
12735 containing the name of the code section as a suffix. For
12736 example .debug_line.text.foo is a debug section associated
12737 with .text.foo. */
12738 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12739 {
12740 unsigned int dlen;
12741
12742 if (dsec->gc_mark == 0
12743 || (dsec->flags & SEC_DEBUGGING) == 0)
12744 continue;
12745
12746 dlen = strlen (dsec->name);
12747
12748 if (dlen > ilen
12749 && strncmp (dsec->name + (dlen - ilen),
12750 isec->name, ilen) == 0)
12751 {
12752 dsec->gc_mark = 0;
b40bf0a2
NC
12753 }
12754 }
12755 }
7f6ab9f8
AM
12756 }
12757 return TRUE;
12758}
12759
c152c796
AM
12760/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12761
c17d87de
NC
12762struct elf_gc_sweep_symbol_info
12763{
ccabcbe5
AM
12764 struct bfd_link_info *info;
12765 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12766 bfd_boolean);
12767};
12768
c152c796 12769static bfd_boolean
ccabcbe5 12770elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 12771{
1d5316ab
AM
12772 if (!h->mark
12773 && (((h->root.type == bfd_link_hash_defined
12774 || h->root.type == bfd_link_hash_defweak)
c4621b33 12775 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6673f753 12776 && h->root.u.def.section->gc_mark))
1d5316ab
AM
12777 || h->root.type == bfd_link_hash_undefined
12778 || h->root.type == bfd_link_hash_undefweak))
12779 {
12780 struct elf_gc_sweep_symbol_info *inf;
12781
12782 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 12783 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
12784 h->def_regular = 0;
12785 h->ref_regular = 0;
12786 h->ref_regular_nonweak = 0;
ccabcbe5 12787 }
c152c796
AM
12788
12789 return TRUE;
12790}
12791
12792/* The sweep phase of garbage collection. Remove all garbage sections. */
12793
12794typedef bfd_boolean (*gc_sweep_hook_fn)
12795 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12796
12797static bfd_boolean
ccabcbe5 12798elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12799{
12800 bfd *sub;
ccabcbe5
AM
12801 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12802 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12803 unsigned long section_sym_count;
12804 struct elf_gc_sweep_symbol_info sweep_info;
c152c796 12805
c72f2fb2 12806 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12807 {
12808 asection *o;
12809
b19a8f85
L
12810 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12811 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12812 continue;
12813
12814 for (o = sub->sections; o != NULL; o = o->next)
12815 {
a33dafc3
L
12816 /* When any section in a section group is kept, we keep all
12817 sections in the section group. If the first member of
12818 the section group is excluded, we will also exclude the
12819 group section. */
12820 if (o->flags & SEC_GROUP)
12821 {
12822 asection *first = elf_next_in_group (o);
12823 o->gc_mark = first->gc_mark;
12824 }
c152c796 12825
1e7eae0d 12826 if (o->gc_mark)
c152c796
AM
12827 continue;
12828
12829 /* Skip sweeping sections already excluded. */
12830 if (o->flags & SEC_EXCLUDE)
12831 continue;
12832
12833 /* Since this is early in the link process, it is simple
12834 to remove a section from the output. */
12835 o->flags |= SEC_EXCLUDE;
12836
c55fe096 12837 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
12838 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12839
c152c796
AM
12840 /* But we also have to update some of the relocation
12841 info we collected before. */
12842 if (gc_sweep_hook
e8aaee2a 12843 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12844 && o->reloc_count != 0
12845 && !((info->strip == strip_all || info->strip == strip_debugger)
12846 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12847 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12848 {
12849 Elf_Internal_Rela *internal_relocs;
12850 bfd_boolean r;
12851
12852 internal_relocs
12853 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12854 info->keep_memory);
12855 if (internal_relocs == NULL)
12856 return FALSE;
12857
12858 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12859
12860 if (elf_section_data (o)->relocs != internal_relocs)
12861 free (internal_relocs);
12862
12863 if (!r)
12864 return FALSE;
12865 }
12866 }
12867 }
12868
12869 /* Remove the symbols that were in the swept sections from the dynamic
12870 symbol table. GCFIXME: Anyone know how to get them out of the
12871 static symbol table as well? */
ccabcbe5
AM
12872 sweep_info.info = info;
12873 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12874 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12875 &sweep_info);
c152c796 12876
ccabcbe5 12877 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
12878 return TRUE;
12879}
12880
12881/* Propagate collected vtable information. This is called through
12882 elf_link_hash_traverse. */
12883
12884static bfd_boolean
12885elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12886{
c152c796 12887 /* Those that are not vtables. */
f6e332e6 12888 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12889 return TRUE;
12890
12891 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12892 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12893 return TRUE;
12894
12895 /* If we've already been done, exit. */
f6e332e6 12896 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12897 return TRUE;
12898
12899 /* Make sure the parent's table is up to date. */
f6e332e6 12900 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 12901
f6e332e6 12902 if (h->vtable->used == NULL)
c152c796
AM
12903 {
12904 /* None of this table's entries were referenced. Re-use the
12905 parent's table. */
f6e332e6
AM
12906 h->vtable->used = h->vtable->parent->vtable->used;
12907 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
12908 }
12909 else
12910 {
12911 size_t n;
12912 bfd_boolean *cu, *pu;
12913
12914 /* Or the parent's entries into ours. */
f6e332e6 12915 cu = h->vtable->used;
c152c796 12916 cu[-1] = TRUE;
f6e332e6 12917 pu = h->vtable->parent->vtable->used;
c152c796
AM
12918 if (pu != NULL)
12919 {
12920 const struct elf_backend_data *bed;
12921 unsigned int log_file_align;
12922
12923 bed = get_elf_backend_data (h->root.u.def.section->owner);
12924 log_file_align = bed->s->log_file_align;
f6e332e6 12925 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
12926 while (n--)
12927 {
12928 if (*pu)
12929 *cu = TRUE;
12930 pu++;
12931 cu++;
12932 }
12933 }
12934 }
12935
12936 return TRUE;
12937}
12938
12939static bfd_boolean
12940elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12941{
12942 asection *sec;
12943 bfd_vma hstart, hend;
12944 Elf_Internal_Rela *relstart, *relend, *rel;
12945 const struct elf_backend_data *bed;
12946 unsigned int log_file_align;
12947
c152c796
AM
12948 /* Take care of both those symbols that do not describe vtables as
12949 well as those that are not loaded. */
f6e332e6 12950 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12951 return TRUE;
12952
12953 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12954 || h->root.type == bfd_link_hash_defweak);
12955
12956 sec = h->root.u.def.section;
12957 hstart = h->root.u.def.value;
12958 hend = hstart + h->size;
12959
12960 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12961 if (!relstart)
12962 return *(bfd_boolean *) okp = FALSE;
12963 bed = get_elf_backend_data (sec->owner);
12964 log_file_align = bed->s->log_file_align;
12965
12966 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12967
12968 for (rel = relstart; rel < relend; ++rel)
12969 if (rel->r_offset >= hstart && rel->r_offset < hend)
12970 {
12971 /* If the entry is in use, do nothing. */
f6e332e6
AM
12972 if (h->vtable->used
12973 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
12974 {
12975 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 12976 if (h->vtable->used[entry])
c152c796
AM
12977 continue;
12978 }
12979 /* Otherwise, kill it. */
12980 rel->r_offset = rel->r_info = rel->r_addend = 0;
12981 }
12982
12983 return TRUE;
12984}
12985
87538722
AM
12986/* Mark sections containing dynamically referenced symbols. When
12987 building shared libraries, we must assume that any visible symbol is
12988 referenced. */
715df9b8 12989
64d03ab5
AM
12990bfd_boolean
12991bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 12992{
87538722 12993 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 12994 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 12995
715df9b8
EB
12996 if ((h->root.type == bfd_link_hash_defined
12997 || h->root.type == bfd_link_hash_defweak)
87538722 12998 && (h->ref_dynamic
c4621b33 12999 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13000 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13001 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13002 && (!bfd_link_executable (info)
b407645f
AM
13003 || info->export_dynamic
13004 || (h->dynamic
13005 && d != NULL
13006 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13007 && (h->versioned >= versioned
54e8959c
L
13008 || !bfd_hide_sym_by_version (info->version_info,
13009 h->root.root.string)))))
715df9b8
EB
13010 h->root.u.def.section->flags |= SEC_KEEP;
13011
13012 return TRUE;
13013}
3b36f7e6 13014
74f0fb50
AM
13015/* Keep all sections containing symbols undefined on the command-line,
13016 and the section containing the entry symbol. */
13017
13018void
13019_bfd_elf_gc_keep (struct bfd_link_info *info)
13020{
13021 struct bfd_sym_chain *sym;
13022
13023 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13024 {
13025 struct elf_link_hash_entry *h;
13026
13027 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13028 FALSE, FALSE, FALSE);
13029
13030 if (h != NULL
13031 && (h->root.type == bfd_link_hash_defined
13032 || h->root.type == bfd_link_hash_defweak)
13033 && !bfd_is_abs_section (h->root.u.def.section))
13034 h->root.u.def.section->flags |= SEC_KEEP;
13035 }
13036}
13037
2f0c68f2
CM
13038bfd_boolean
13039bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13040 struct bfd_link_info *info)
13041{
13042 bfd *ibfd = info->input_bfds;
13043
13044 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13045 {
13046 asection *sec;
13047 struct elf_reloc_cookie cookie;
13048
13049 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13050 continue;
13051
13052 if (!init_reloc_cookie (&cookie, info, ibfd))
13053 return FALSE;
13054
13055 for (sec = ibfd->sections; sec; sec = sec->next)
13056 {
13057 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13058 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13059 {
13060 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13061 fini_reloc_cookie_rels (&cookie, sec);
13062 }
13063 }
13064 }
13065 return TRUE;
13066}
13067
c152c796
AM
13068/* Do mark and sweep of unused sections. */
13069
13070bfd_boolean
13071bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13072{
13073 bfd_boolean ok = TRUE;
13074 bfd *sub;
6a5bb875 13075 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13076 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13077 struct elf_link_hash_table *htab;
c152c796 13078
64d03ab5 13079 if (!bed->can_gc_sections
715df9b8 13080 || !is_elf_hash_table (info->hash))
c152c796
AM
13081 {
13082 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
13083 return TRUE;
13084 }
13085
74f0fb50 13086 bed->gc_keep (info);
da44f4e5 13087 htab = elf_hash_table (info);
74f0fb50 13088
9d0a14d3
RS
13089 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13090 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13091 for (sub = info->input_bfds;
13092 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13093 sub = sub->link.next)
9d0a14d3
RS
13094 {
13095 asection *sec;
13096 struct elf_reloc_cookie cookie;
13097
13098 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13099 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13100 {
13101 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13102 if (elf_section_data (sec)->sec_info
13103 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13104 elf_eh_frame_section (sub) = sec;
13105 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13106 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13107 }
13108 }
9d0a14d3 13109
c152c796 13110 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13111 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13112 if (!ok)
13113 return FALSE;
13114
13115 /* Kill the vtable relocations that were not used. */
da44f4e5 13116 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13117 if (!ok)
13118 return FALSE;
13119
715df9b8 13120 /* Mark dynamically referenced symbols. */
da44f4e5
AM
13121 if (htab->dynamic_sections_created)
13122 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13123
715df9b8 13124 /* Grovel through relocs to find out who stays ... */
64d03ab5 13125 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13126 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13127 {
13128 asection *o;
13129
b19a8f85
L
13130 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13131 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13132 continue;
13133
7f6ab9f8
AM
13134 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13135 Also treat note sections as a root, if the section is not part
13136 of a group. */
c152c796 13137 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13138 if (!o->gc_mark
13139 && (o->flags & SEC_EXCLUDE) == 0
24007750 13140 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13141 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13142 && elf_next_in_group (o) == NULL )))
13143 {
13144 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13145 return FALSE;
13146 }
c152c796
AM
13147 }
13148
6a5bb875 13149 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13150 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13151
c152c796 13152 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13153 return elf_gc_sweep (abfd, info);
c152c796
AM
13154}
13155\f
13156/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13157
13158bfd_boolean
13159bfd_elf_gc_record_vtinherit (bfd *abfd,
13160 asection *sec,
13161 struct elf_link_hash_entry *h,
13162 bfd_vma offset)
13163{
13164 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13165 struct elf_link_hash_entry **search, *child;
ef53be89 13166 size_t extsymcount;
c152c796
AM
13167 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13168
13169 /* The sh_info field of the symtab header tells us where the
13170 external symbols start. We don't care about the local symbols at
13171 this point. */
13172 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13173 if (!elf_bad_symtab (abfd))
13174 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13175
13176 sym_hashes = elf_sym_hashes (abfd);
13177 sym_hashes_end = sym_hashes + extsymcount;
13178
13179 /* Hunt down the child symbol, which is in this section at the same
13180 offset as the relocation. */
13181 for (search = sym_hashes; search != sym_hashes_end; ++search)
13182 {
13183 if ((child = *search) != NULL
13184 && (child->root.type == bfd_link_hash_defined
13185 || child->root.type == bfd_link_hash_defweak)
13186 && child->root.u.def.section == sec
13187 && child->root.u.def.value == offset)
13188 goto win;
13189 }
13190
d003868e
AM
13191 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
13192 abfd, sec, (unsigned long) offset);
c152c796
AM
13193 bfd_set_error (bfd_error_invalid_operation);
13194 return FALSE;
13195
13196 win:
f6e332e6
AM
13197 if (!child->vtable)
13198 {
ca4be51c
AM
13199 child->vtable = ((struct elf_link_virtual_table_entry *)
13200 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
13201 if (!child->vtable)
13202 return FALSE;
13203 }
c152c796
AM
13204 if (!h)
13205 {
13206 /* This *should* only be the absolute section. It could potentially
13207 be that someone has defined a non-global vtable though, which
13208 would be bad. It isn't worth paging in the local symbols to be
13209 sure though; that case should simply be handled by the assembler. */
13210
f6e332e6 13211 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13212 }
13213 else
f6e332e6 13214 child->vtable->parent = h;
c152c796
AM
13215
13216 return TRUE;
13217}
13218
13219/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13220
13221bfd_boolean
13222bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13223 asection *sec ATTRIBUTE_UNUSED,
13224 struct elf_link_hash_entry *h,
13225 bfd_vma addend)
13226{
13227 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13228 unsigned int log_file_align = bed->s->log_file_align;
13229
f6e332e6
AM
13230 if (!h->vtable)
13231 {
ca4be51c
AM
13232 h->vtable = ((struct elf_link_virtual_table_entry *)
13233 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
13234 if (!h->vtable)
13235 return FALSE;
13236 }
13237
13238 if (addend >= h->vtable->size)
c152c796
AM
13239 {
13240 size_t size, bytes, file_align;
f6e332e6 13241 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
13242
13243 /* While the symbol is undefined, we have to be prepared to handle
13244 a zero size. */
13245 file_align = 1 << log_file_align;
13246 if (h->root.type == bfd_link_hash_undefined)
13247 size = addend + file_align;
13248 else
13249 {
13250 size = h->size;
13251 if (addend >= size)
13252 {
13253 /* Oops! We've got a reference past the defined end of
13254 the table. This is probably a bug -- shall we warn? */
13255 size = addend + file_align;
13256 }
13257 }
13258 size = (size + file_align - 1) & -file_align;
13259
13260 /* Allocate one extra entry for use as a "done" flag for the
13261 consolidation pass. */
13262 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13263
13264 if (ptr)
13265 {
a50b1753 13266 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13267
13268 if (ptr != NULL)
13269 {
13270 size_t oldbytes;
13271
f6e332e6 13272 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
13273 * sizeof (bfd_boolean));
13274 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13275 }
13276 }
13277 else
a50b1753 13278 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13279
13280 if (ptr == NULL)
13281 return FALSE;
13282
13283 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
13284 h->vtable->used = ptr + 1;
13285 h->vtable->size = size;
c152c796
AM
13286 }
13287
f6e332e6 13288 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13289
13290 return TRUE;
13291}
13292
ae17ab41
CM
13293/* Map an ELF section header flag to its corresponding string. */
13294typedef struct
13295{
13296 char *flag_name;
13297 flagword flag_value;
13298} elf_flags_to_name_table;
13299
13300static elf_flags_to_name_table elf_flags_to_names [] =
13301{
13302 { "SHF_WRITE", SHF_WRITE },
13303 { "SHF_ALLOC", SHF_ALLOC },
13304 { "SHF_EXECINSTR", SHF_EXECINSTR },
13305 { "SHF_MERGE", SHF_MERGE },
13306 { "SHF_STRINGS", SHF_STRINGS },
13307 { "SHF_INFO_LINK", SHF_INFO_LINK},
13308 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13309 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13310 { "SHF_GROUP", SHF_GROUP },
13311 { "SHF_TLS", SHF_TLS },
13312 { "SHF_MASKOS", SHF_MASKOS },
13313 { "SHF_EXCLUDE", SHF_EXCLUDE },
13314};
13315
b9c361e0
JL
13316/* Returns TRUE if the section is to be included, otherwise FALSE. */
13317bfd_boolean
ae17ab41 13318bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13319 struct flag_info *flaginfo,
b9c361e0 13320 asection *section)
ae17ab41 13321{
8b127cbc 13322 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13323
8b127cbc 13324 if (!flaginfo->flags_initialized)
ae17ab41 13325 {
8b127cbc
AM
13326 bfd *obfd = info->output_bfd;
13327 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13328 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13329 int with_hex = 0;
13330 int without_hex = 0;
13331
8b127cbc 13332 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13333 {
b9c361e0 13334 unsigned i;
8b127cbc 13335 flagword (*lookup) (char *);
ae17ab41 13336
8b127cbc
AM
13337 lookup = bed->elf_backend_lookup_section_flags_hook;
13338 if (lookup != NULL)
ae17ab41 13339 {
8b127cbc 13340 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13341
13342 if (hexval != 0)
13343 {
13344 if (tf->with == with_flags)
13345 with_hex |= hexval;
13346 else if (tf->with == without_flags)
13347 without_hex |= hexval;
13348 tf->valid = TRUE;
13349 continue;
13350 }
ae17ab41 13351 }
8b127cbc 13352 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13353 {
8b127cbc 13354 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13355 {
13356 if (tf->with == with_flags)
13357 with_hex |= elf_flags_to_names[i].flag_value;
13358 else if (tf->with == without_flags)
13359 without_hex |= elf_flags_to_names[i].flag_value;
13360 tf->valid = TRUE;
13361 break;
13362 }
13363 }
8b127cbc 13364 if (!tf->valid)
b9c361e0 13365 {
68ffbac6 13366 info->callbacks->einfo
8b127cbc 13367 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13368 return FALSE;
ae17ab41
CM
13369 }
13370 }
8b127cbc
AM
13371 flaginfo->flags_initialized = TRUE;
13372 flaginfo->only_with_flags |= with_hex;
13373 flaginfo->not_with_flags |= without_hex;
ae17ab41 13374 }
ae17ab41 13375
8b127cbc 13376 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13377 return FALSE;
13378
8b127cbc 13379 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13380 return FALSE;
13381
13382 return TRUE;
ae17ab41
CM
13383}
13384
c152c796
AM
13385struct alloc_got_off_arg {
13386 bfd_vma gotoff;
10455f89 13387 struct bfd_link_info *info;
c152c796
AM
13388};
13389
13390/* We need a special top-level link routine to convert got reference counts
13391 to real got offsets. */
13392
13393static bfd_boolean
13394elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13395{
a50b1753 13396 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13397 bfd *obfd = gofarg->info->output_bfd;
13398 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13399
c152c796
AM
13400 if (h->got.refcount > 0)
13401 {
13402 h->got.offset = gofarg->gotoff;
10455f89 13403 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13404 }
13405 else
13406 h->got.offset = (bfd_vma) -1;
13407
13408 return TRUE;
13409}
13410
13411/* And an accompanying bit to work out final got entry offsets once
13412 we're done. Should be called from final_link. */
13413
13414bfd_boolean
13415bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13416 struct bfd_link_info *info)
13417{
13418 bfd *i;
13419 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13420 bfd_vma gotoff;
c152c796
AM
13421 struct alloc_got_off_arg gofarg;
13422
10455f89
HPN
13423 BFD_ASSERT (abfd == info->output_bfd);
13424
c152c796
AM
13425 if (! is_elf_hash_table (info->hash))
13426 return FALSE;
13427
13428 /* The GOT offset is relative to the .got section, but the GOT header is
13429 put into the .got.plt section, if the backend uses it. */
13430 if (bed->want_got_plt)
13431 gotoff = 0;
13432 else
13433 gotoff = bed->got_header_size;
13434
13435 /* Do the local .got entries first. */
c72f2fb2 13436 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13437 {
13438 bfd_signed_vma *local_got;
ef53be89 13439 size_t j, locsymcount;
c152c796
AM
13440 Elf_Internal_Shdr *symtab_hdr;
13441
13442 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13443 continue;
13444
13445 local_got = elf_local_got_refcounts (i);
13446 if (!local_got)
13447 continue;
13448
13449 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13450 if (elf_bad_symtab (i))
13451 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13452 else
13453 locsymcount = symtab_hdr->sh_info;
13454
13455 for (j = 0; j < locsymcount; ++j)
13456 {
13457 if (local_got[j] > 0)
13458 {
13459 local_got[j] = gotoff;
10455f89 13460 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13461 }
13462 else
13463 local_got[j] = (bfd_vma) -1;
13464 }
13465 }
13466
13467 /* Then the global .got entries. .plt refcounts are handled by
13468 adjust_dynamic_symbol */
13469 gofarg.gotoff = gotoff;
10455f89 13470 gofarg.info = info;
c152c796
AM
13471 elf_link_hash_traverse (elf_hash_table (info),
13472 elf_gc_allocate_got_offsets,
13473 &gofarg);
13474 return TRUE;
13475}
13476
13477/* Many folk need no more in the way of final link than this, once
13478 got entry reference counting is enabled. */
13479
13480bfd_boolean
13481bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13482{
13483 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13484 return FALSE;
13485
13486 /* Invoke the regular ELF backend linker to do all the work. */
13487 return bfd_elf_final_link (abfd, info);
13488}
13489
13490bfd_boolean
13491bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13492{
a50b1753 13493 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13494
13495 if (rcookie->bad_symtab)
13496 rcookie->rel = rcookie->rels;
13497
13498 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13499 {
13500 unsigned long r_symndx;
13501
13502 if (! rcookie->bad_symtab)
13503 if (rcookie->rel->r_offset > offset)
13504 return FALSE;
13505 if (rcookie->rel->r_offset != offset)
13506 continue;
13507
13508 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13509 if (r_symndx == STN_UNDEF)
c152c796
AM
13510 return TRUE;
13511
13512 if (r_symndx >= rcookie->locsymcount
13513 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13514 {
13515 struct elf_link_hash_entry *h;
13516
13517 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13518
13519 while (h->root.type == bfd_link_hash_indirect
13520 || h->root.type == bfd_link_hash_warning)
13521 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13522
13523 if ((h->root.type == bfd_link_hash_defined
13524 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13525 && (h->root.u.def.section->owner != rcookie->abfd
13526 || h->root.u.def.section->kept_section != NULL
13527 || discarded_section (h->root.u.def.section)))
c152c796 13528 return TRUE;
c152c796
AM
13529 }
13530 else
13531 {
13532 /* It's not a relocation against a global symbol,
13533 but it could be a relocation against a local
13534 symbol for a discarded section. */
13535 asection *isec;
13536 Elf_Internal_Sym *isym;
13537
13538 /* Need to: get the symbol; get the section. */
13539 isym = &rcookie->locsyms[r_symndx];
cb33740c 13540 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13541 if (isec != NULL
13542 && (isec->kept_section != NULL
13543 || discarded_section (isec)))
cb33740c 13544 return TRUE;
c152c796
AM
13545 }
13546 return FALSE;
13547 }
13548 return FALSE;
13549}
13550
13551/* Discard unneeded references to discarded sections.
75938853
AM
13552 Returns -1 on error, 1 if any section's size was changed, 0 if
13553 nothing changed. This function assumes that the relocations are in
13554 sorted order, which is true for all known assemblers. */
c152c796 13555
75938853 13556int
c152c796
AM
13557bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13558{
13559 struct elf_reloc_cookie cookie;
18cd5bce 13560 asection *o;
c152c796 13561 bfd *abfd;
75938853 13562 int changed = 0;
c152c796
AM
13563
13564 if (info->traditional_format
13565 || !is_elf_hash_table (info->hash))
75938853 13566 return 0;
c152c796 13567
18cd5bce
AM
13568 o = bfd_get_section_by_name (output_bfd, ".stab");
13569 if (o != NULL)
c152c796 13570 {
18cd5bce 13571 asection *i;
c152c796 13572
18cd5bce 13573 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13574 {
18cd5bce
AM
13575 if (i->size == 0
13576 || i->reloc_count == 0
13577 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13578 continue;
c152c796 13579
18cd5bce
AM
13580 abfd = i->owner;
13581 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13582 continue;
c152c796 13583
18cd5bce 13584 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13585 return -1;
c152c796 13586
18cd5bce
AM
13587 if (_bfd_discard_section_stabs (abfd, i,
13588 elf_section_data (i)->sec_info,
5241d853
RS
13589 bfd_elf_reloc_symbol_deleted_p,
13590 &cookie))
75938853 13591 changed = 1;
18cd5bce
AM
13592
13593 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13594 }
18cd5bce
AM
13595 }
13596
2f0c68f2
CM
13597 o = NULL;
13598 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13599 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13600 if (o != NULL)
13601 {
13602 asection *i;
c152c796 13603
18cd5bce 13604 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13605 {
18cd5bce
AM
13606 if (i->size == 0)
13607 continue;
13608
13609 abfd = i->owner;
13610 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13611 continue;
13612
13613 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13614 return -1;
18cd5bce
AM
13615
13616 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13617 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13618 bfd_elf_reloc_symbol_deleted_p,
13619 &cookie))
75938853 13620 changed = 1;
18cd5bce
AM
13621
13622 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13623 }
18cd5bce 13624 }
c152c796 13625
18cd5bce
AM
13626 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13627 {
13628 const struct elf_backend_data *bed;
c152c796 13629
18cd5bce
AM
13630 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13631 continue;
13632
13633 bed = get_elf_backend_data (abfd);
13634
13635 if (bed->elf_backend_discard_info != NULL)
13636 {
13637 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13638 return -1;
18cd5bce
AM
13639
13640 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13641 changed = 1;
18cd5bce
AM
13642
13643 fini_reloc_cookie (&cookie, abfd);
13644 }
c152c796
AM
13645 }
13646
2f0c68f2
CM
13647 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13648 _bfd_elf_end_eh_frame_parsing (info);
13649
13650 if (info->eh_frame_hdr_type
0e1862bb 13651 && !bfd_link_relocatable (info)
c152c796 13652 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13653 changed = 1;
c152c796 13654
75938853 13655 return changed;
c152c796 13656}
082b7297 13657
43e1669b 13658bfd_boolean
0c511000 13659_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13660 asection *sec,
c0f00686 13661 struct bfd_link_info *info)
082b7297
L
13662{
13663 flagword flags;
c77ec726 13664 const char *name, *key;
082b7297
L
13665 struct bfd_section_already_linked *l;
13666 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13667
c77ec726
AM
13668 if (sec->output_section == bfd_abs_section_ptr)
13669 return FALSE;
0c511000 13670
c77ec726 13671 flags = sec->flags;
0c511000 13672
c77ec726
AM
13673 /* Return if it isn't a linkonce section. A comdat group section
13674 also has SEC_LINK_ONCE set. */
13675 if ((flags & SEC_LINK_ONCE) == 0)
13676 return FALSE;
0c511000 13677
c77ec726
AM
13678 /* Don't put group member sections on our list of already linked
13679 sections. They are handled as a group via their group section. */
13680 if (elf_sec_group (sec) != NULL)
13681 return FALSE;
0c511000 13682
c77ec726
AM
13683 /* For a SHT_GROUP section, use the group signature as the key. */
13684 name = sec->name;
13685 if ((flags & SEC_GROUP) != 0
13686 && elf_next_in_group (sec) != NULL
13687 && elf_group_name (elf_next_in_group (sec)) != NULL)
13688 key = elf_group_name (elf_next_in_group (sec));
13689 else
13690 {
13691 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13692 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13693 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13694 key++;
0c511000 13695 else
c77ec726
AM
13696 /* Must be a user linkonce section that doesn't follow gcc's
13697 naming convention. In this case we won't be matching
13698 single member groups. */
13699 key = name;
0c511000 13700 }
6d2cd210 13701
c77ec726 13702 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13703
13704 for (l = already_linked_list->entry; l != NULL; l = l->next)
13705 {
c2370991 13706 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13707 sections with a signature of <key> (<key> is some string),
13708 and linkonce sections named .gnu.linkonce.<type>.<key>.
13709 Match like sections. LTO plugin sections are an exception.
13710 They are always named .gnu.linkonce.t.<key> and match either
13711 type of section. */
13712 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13713 && ((flags & SEC_GROUP) != 0
13714 || strcmp (name, l->sec->name) == 0))
13715 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13716 {
13717 /* The section has already been linked. See if we should
6d2cd210 13718 issue a warning. */
c77ec726
AM
13719 if (!_bfd_handle_already_linked (sec, l, info))
13720 return FALSE;
082b7297 13721
c77ec726 13722 if (flags & SEC_GROUP)
3d7f7666 13723 {
c77ec726
AM
13724 asection *first = elf_next_in_group (sec);
13725 asection *s = first;
3d7f7666 13726
c77ec726 13727 while (s != NULL)
3d7f7666 13728 {
c77ec726
AM
13729 s->output_section = bfd_abs_section_ptr;
13730 /* Record which group discards it. */
13731 s->kept_section = l->sec;
13732 s = elf_next_in_group (s);
13733 /* These lists are circular. */
13734 if (s == first)
13735 break;
3d7f7666
L
13736 }
13737 }
082b7297 13738
43e1669b 13739 return TRUE;
082b7297
L
13740 }
13741 }
13742
c77ec726
AM
13743 /* A single member comdat group section may be discarded by a
13744 linkonce section and vice versa. */
13745 if ((flags & SEC_GROUP) != 0)
3d7f7666 13746 {
c77ec726 13747 asection *first = elf_next_in_group (sec);
c2370991 13748
c77ec726
AM
13749 if (first != NULL && elf_next_in_group (first) == first)
13750 /* Check this single member group against linkonce sections. */
13751 for (l = already_linked_list->entry; l != NULL; l = l->next)
13752 if ((l->sec->flags & SEC_GROUP) == 0
13753 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13754 {
13755 first->output_section = bfd_abs_section_ptr;
13756 first->kept_section = l->sec;
13757 sec->output_section = bfd_abs_section_ptr;
13758 break;
13759 }
13760 }
13761 else
13762 /* Check this linkonce section against single member groups. */
13763 for (l = already_linked_list->entry; l != NULL; l = l->next)
13764 if (l->sec->flags & SEC_GROUP)
6d2cd210 13765 {
c77ec726 13766 asection *first = elf_next_in_group (l->sec);
6d2cd210 13767
c77ec726
AM
13768 if (first != NULL
13769 && elf_next_in_group (first) == first
13770 && bfd_elf_match_symbols_in_sections (first, sec, info))
13771 {
13772 sec->output_section = bfd_abs_section_ptr;
13773 sec->kept_section = first;
13774 break;
13775 }
6d2cd210 13776 }
0c511000 13777
c77ec726
AM
13778 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13779 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13780 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13781 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13782 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13783 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13784 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13785 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13786 The reverse order cannot happen as there is never a bfd with only the
13787 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13788 matter as here were are looking only for cross-bfd sections. */
13789
13790 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13791 for (l = already_linked_list->entry; l != NULL; l = l->next)
13792 if ((l->sec->flags & SEC_GROUP) == 0
13793 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13794 {
13795 if (abfd != l->sec->owner)
13796 sec->output_section = bfd_abs_section_ptr;
13797 break;
13798 }
80c29487 13799
082b7297 13800 /* This is the first section with this name. Record it. */
c77ec726 13801 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13802 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13803 return sec->output_section == bfd_abs_section_ptr;
082b7297 13804}
81e1b023 13805
a4d8e49b
L
13806bfd_boolean
13807_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13808{
13809 return sym->st_shndx == SHN_COMMON;
13810}
13811
13812unsigned int
13813_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13814{
13815 return SHN_COMMON;
13816}
13817
13818asection *
13819_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13820{
13821 return bfd_com_section_ptr;
13822}
10455f89
HPN
13823
13824bfd_vma
13825_bfd_elf_default_got_elt_size (bfd *abfd,
13826 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13827 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13828 bfd *ibfd ATTRIBUTE_UNUSED,
13829 unsigned long symndx ATTRIBUTE_UNUSED)
13830{
13831 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13832 return bed->s->arch_size / 8;
13833}
83bac4b0
NC
13834
13835/* Routines to support the creation of dynamic relocs. */
13836
83bac4b0
NC
13837/* Returns the name of the dynamic reloc section associated with SEC. */
13838
13839static const char *
13840get_dynamic_reloc_section_name (bfd * abfd,
13841 asection * sec,
13842 bfd_boolean is_rela)
13843{
ddcf1fcf
BS
13844 char *name;
13845 const char *old_name = bfd_get_section_name (NULL, sec);
13846 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13847
ddcf1fcf 13848 if (old_name == NULL)
83bac4b0
NC
13849 return NULL;
13850
ddcf1fcf 13851 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13852 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13853
13854 return name;
13855}
13856
13857/* Returns the dynamic reloc section associated with SEC.
13858 If necessary compute the name of the dynamic reloc section based
13859 on SEC's name (looked up in ABFD's string table) and the setting
13860 of IS_RELA. */
13861
13862asection *
13863_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13864 asection * sec,
13865 bfd_boolean is_rela)
13866{
13867 asection * reloc_sec = elf_section_data (sec)->sreloc;
13868
13869 if (reloc_sec == NULL)
13870 {
13871 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13872
13873 if (name != NULL)
13874 {
3d4d4302 13875 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
13876
13877 if (reloc_sec != NULL)
13878 elf_section_data (sec)->sreloc = reloc_sec;
13879 }
13880 }
13881
13882 return reloc_sec;
13883}
13884
13885/* Returns the dynamic reloc section associated with SEC. If the
13886 section does not exist it is created and attached to the DYNOBJ
13887 bfd and stored in the SRELOC field of SEC's elf_section_data
13888 structure.
f8076f98 13889
83bac4b0
NC
13890 ALIGNMENT is the alignment for the newly created section and
13891 IS_RELA defines whether the name should be .rela.<SEC's name>
13892 or .rel.<SEC's name>. The section name is looked up in the
13893 string table associated with ABFD. */
13894
13895asection *
ca4be51c
AM
13896_bfd_elf_make_dynamic_reloc_section (asection *sec,
13897 bfd *dynobj,
13898 unsigned int alignment,
13899 bfd *abfd,
13900 bfd_boolean is_rela)
83bac4b0
NC
13901{
13902 asection * reloc_sec = elf_section_data (sec)->sreloc;
13903
13904 if (reloc_sec == NULL)
13905 {
13906 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13907
13908 if (name == NULL)
13909 return NULL;
13910
3d4d4302 13911 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
13912
13913 if (reloc_sec == NULL)
13914 {
3d4d4302
AM
13915 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13916 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
13917 if ((sec->flags & SEC_ALLOC) != 0)
13918 flags |= SEC_ALLOC | SEC_LOAD;
13919
3d4d4302 13920 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
13921 if (reloc_sec != NULL)
13922 {
8877b5e5
AM
13923 /* _bfd_elf_get_sec_type_attr chooses a section type by
13924 name. Override as it may be wrong, eg. for a user
13925 section named "auto" we'll get ".relauto" which is
13926 seen to be a .rela section. */
13927 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
13928 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13929 reloc_sec = NULL;
13930 }
13931 }
13932
13933 elf_section_data (sec)->sreloc = reloc_sec;
13934 }
13935
13936 return reloc_sec;
13937}
1338dd10 13938
bffebb6b
AM
13939/* Copy the ELF symbol type and other attributes for a linker script
13940 assignment from HSRC to HDEST. Generally this should be treated as
13941 if we found a strong non-dynamic definition for HDEST (except that
13942 ld ignores multiple definition errors). */
1338dd10 13943void
bffebb6b
AM
13944_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13945 struct bfd_link_hash_entry *hdest,
13946 struct bfd_link_hash_entry *hsrc)
1338dd10 13947{
bffebb6b
AM
13948 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13949 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13950 Elf_Internal_Sym isym;
1338dd10
PB
13951
13952 ehdest->type = ehsrc->type;
35fc36a8 13953 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
13954
13955 isym.st_other = ehsrc->other;
b8417128 13956 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 13957}
351f65ca
L
13958
13959/* Append a RELA relocation REL to section S in BFD. */
13960
13961void
13962elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13963{
13964 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13965 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13966 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13967 bed->s->swap_reloca_out (abfd, rel, loc);
13968}
13969
13970/* Append a REL relocation REL to section S in BFD. */
13971
13972void
13973elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13974{
13975 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13976 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13977 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 13978 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 13979}
This page took 2.300871 seconds and 4 git commands to generate.