Specify source file explicitly when setting a breakpoint
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
4b95cf5c 2 Copyright (C) 1995-2014 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"
252b5132
RH
23#include "bfdlink.h"
24#include "libbfd.h"
25#define ARCH_SIZE 0
26#include "elf-bfd.h"
4ad4eba5 27#include "safe-ctype.h"
ccf2f652 28#include "libiberty.h"
66eb6687 29#include "objalloc.h"
252b5132 30
28caa186
AM
31/* This struct is used to pass information to routines called via
32 elf_link_hash_traverse which must return failure. */
33
34struct elf_info_failed
35{
36 struct bfd_link_info *info;
28caa186
AM
37 bfd_boolean failed;
38};
39
40/* This structure is used to pass information to
41 _bfd_elf_link_find_version_dependencies. */
42
43struct elf_find_verdep_info
44{
45 /* General link information. */
46 struct bfd_link_info *info;
47 /* The number of dependencies. */
48 unsigned int vers;
49 /* Whether we had a failure. */
50 bfd_boolean failed;
51};
52
53static bfd_boolean _bfd_elf_fix_symbol_flags
54 (struct elf_link_hash_entry *, struct elf_info_failed *);
55
d98685ac
AM
56/* Define a symbol in a dynamic linkage section. */
57
58struct elf_link_hash_entry *
59_bfd_elf_define_linkage_sym (bfd *abfd,
60 struct bfd_link_info *info,
61 asection *sec,
62 const char *name)
63{
64 struct elf_link_hash_entry *h;
65 struct bfd_link_hash_entry *bh;
ccabcbe5 66 const struct elf_backend_data *bed;
d98685ac
AM
67
68 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
69 if (h != NULL)
70 {
71 /* Zap symbol defined in an as-needed lib that wasn't linked.
72 This is a symptom of a larger problem: Absolute symbols
73 defined in shared libraries can't be overridden, because we
74 lose the link to the bfd which is via the symbol section. */
75 h->root.type = bfd_link_hash_new;
76 }
77
78 bh = &h->root;
79 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
80 sec, 0, NULL, FALSE,
81 get_elf_backend_data (abfd)->collect,
82 &bh))
83 return NULL;
84 h = (struct elf_link_hash_entry *) bh;
85 h->def_regular = 1;
e28df02b 86 h->non_elf = 0;
d98685ac 87 h->type = STT_OBJECT;
00b7642b
AM
88 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
89 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 90
ccabcbe5
AM
91 bed = get_elf_backend_data (abfd);
92 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
93 return h;
94}
95
b34976b6 96bfd_boolean
268b6b39 97_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
98{
99 flagword flags;
aad5d350 100 asection *s;
252b5132 101 struct elf_link_hash_entry *h;
9c5bfbb7 102 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 103 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
104
105 /* This function may be called more than once. */
3d4d4302
AM
106 s = bfd_get_linker_section (abfd, ".got");
107 if (s != NULL)
b34976b6 108 return TRUE;
252b5132 109
e5a52504 110 flags = bed->dynamic_sec_flags;
252b5132 111
14b2f831
AM
112 s = bfd_make_section_anyway_with_flags (abfd,
113 (bed->rela_plts_and_copies_p
114 ? ".rela.got" : ".rel.got"),
115 (bed->dynamic_sec_flags
116 | SEC_READONLY));
6de2ae4a
L
117 if (s == NULL
118 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
119 return FALSE;
120 htab->srelgot = s;
252b5132 121
14b2f831 122 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
123 if (s == NULL
124 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
125 return FALSE;
126 htab->sgot = s;
127
252b5132
RH
128 if (bed->want_got_plt)
129 {
14b2f831 130 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 131 if (s == NULL
6de2ae4a
L
132 || !bfd_set_section_alignment (abfd, s,
133 bed->s->log_file_align))
b34976b6 134 return FALSE;
6de2ae4a 135 htab->sgotplt = s;
252b5132
RH
136 }
137
64e77c6d
L
138 /* The first bit of the global offset table is the header. */
139 s->size += bed->got_header_size;
140
2517a57f
AM
141 if (bed->want_got_sym)
142 {
143 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
144 (or .got.plt) section. We don't do this in the linker script
145 because we don't want to define the symbol if we are not creating
146 a global offset table. */
6de2ae4a
L
147 h = _bfd_elf_define_linkage_sym (abfd, info, s,
148 "_GLOBAL_OFFSET_TABLE_");
2517a57f 149 elf_hash_table (info)->hgot = h;
d98685ac
AM
150 if (h == NULL)
151 return FALSE;
2517a57f 152 }
252b5132 153
b34976b6 154 return TRUE;
252b5132
RH
155}
156\f
7e9f0867
AM
157/* Create a strtab to hold the dynamic symbol names. */
158static bfd_boolean
159_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
160{
161 struct elf_link_hash_table *hash_table;
162
163 hash_table = elf_hash_table (info);
164 if (hash_table->dynobj == NULL)
165 hash_table->dynobj = abfd;
166
167 if (hash_table->dynstr == NULL)
168 {
169 hash_table->dynstr = _bfd_elf_strtab_init ();
170 if (hash_table->dynstr == NULL)
171 return FALSE;
172 }
173 return TRUE;
174}
175
45d6a902
AM
176/* Create some sections which will be filled in with dynamic linking
177 information. ABFD is an input file which requires dynamic sections
178 to be created. The dynamic sections take up virtual memory space
179 when the final executable is run, so we need to create them before
180 addresses are assigned to the output sections. We work out the
181 actual contents and size of these sections later. */
252b5132 182
b34976b6 183bfd_boolean
268b6b39 184_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 185{
45d6a902 186 flagword flags;
91d6fa6a 187 asection *s;
9c5bfbb7 188 const struct elf_backend_data *bed;
9637f6ef 189 struct elf_link_hash_entry *h;
252b5132 190
0eddce27 191 if (! is_elf_hash_table (info->hash))
45d6a902
AM
192 return FALSE;
193
194 if (elf_hash_table (info)->dynamic_sections_created)
195 return TRUE;
196
7e9f0867
AM
197 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
198 return FALSE;
45d6a902 199
7e9f0867 200 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
201 bed = get_elf_backend_data (abfd);
202
203 flags = bed->dynamic_sec_flags;
45d6a902
AM
204
205 /* A dynamically linked executable has a .interp section, but a
206 shared library does not. */
36af4a4e 207 if (info->executable)
252b5132 208 {
14b2f831
AM
209 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
210 flags | SEC_READONLY);
3496cb2a 211 if (s == NULL)
45d6a902
AM
212 return FALSE;
213 }
bb0deeff 214
45d6a902
AM
215 /* Create sections to hold version informations. These are removed
216 if they are not needed. */
14b2f831
AM
217 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
218 flags | SEC_READONLY);
45d6a902 219 if (s == NULL
45d6a902
AM
220 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
221 return FALSE;
222
14b2f831
AM
223 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
224 flags | SEC_READONLY);
45d6a902 225 if (s == NULL
45d6a902
AM
226 || ! bfd_set_section_alignment (abfd, s, 1))
227 return FALSE;
228
14b2f831
AM
229 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
230 flags | SEC_READONLY);
45d6a902 231 if (s == NULL
45d6a902
AM
232 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
233 return FALSE;
234
14b2f831
AM
235 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
236 flags | SEC_READONLY);
45d6a902 237 if (s == NULL
45d6a902
AM
238 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
239 return FALSE;
240
14b2f831
AM
241 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
242 flags | SEC_READONLY);
3496cb2a 243 if (s == NULL)
45d6a902
AM
244 return FALSE;
245
14b2f831 246 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 247 if (s == NULL
45d6a902
AM
248 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
249 return FALSE;
250
251 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
252 .dynamic section. We could set _DYNAMIC in a linker script, but we
253 only want to define it if we are, in fact, creating a .dynamic
254 section. We don't want to define it if there is no .dynamic
255 section, since on some ELF platforms the start up code examines it
256 to decide how to initialize the process. */
9637f6ef
L
257 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
258 elf_hash_table (info)->hdynamic = h;
259 if (h == NULL)
45d6a902
AM
260 return FALSE;
261
fdc90cb4
JJ
262 if (info->emit_hash)
263 {
14b2f831
AM
264 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
265 flags | SEC_READONLY);
fdc90cb4
JJ
266 if (s == NULL
267 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
268 return FALSE;
269 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
270 }
271
272 if (info->emit_gnu_hash)
273 {
14b2f831
AM
274 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
275 flags | SEC_READONLY);
fdc90cb4
JJ
276 if (s == NULL
277 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
278 return FALSE;
279 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
280 4 32-bit words followed by variable count of 64-bit words, then
281 variable count of 32-bit words. */
282 if (bed->s->arch_size == 64)
283 elf_section_data (s)->this_hdr.sh_entsize = 0;
284 else
285 elf_section_data (s)->this_hdr.sh_entsize = 4;
286 }
45d6a902
AM
287
288 /* Let the backend create the rest of the sections. This lets the
289 backend set the right flags. The backend will normally create
290 the .got and .plt sections. */
894891db
NC
291 if (bed->elf_backend_create_dynamic_sections == NULL
292 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
293 return FALSE;
294
295 elf_hash_table (info)->dynamic_sections_created = TRUE;
296
297 return TRUE;
298}
299
300/* Create dynamic sections when linking against a dynamic object. */
301
302bfd_boolean
268b6b39 303_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
304{
305 flagword flags, pltflags;
7325306f 306 struct elf_link_hash_entry *h;
45d6a902 307 asection *s;
9c5bfbb7 308 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 309 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 310
252b5132
RH
311 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
312 .rel[a].bss sections. */
e5a52504 313 flags = bed->dynamic_sec_flags;
252b5132
RH
314
315 pltflags = flags;
252b5132 316 if (bed->plt_not_loaded)
6df4d94c
MM
317 /* We do not clear SEC_ALLOC here because we still want the OS to
318 allocate space for the section; it's just that there's nothing
319 to read in from the object file. */
5d1634d7 320 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
321 else
322 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
323 if (bed->plt_readonly)
324 pltflags |= SEC_READONLY;
325
14b2f831 326 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 327 if (s == NULL
252b5132 328 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 329 return FALSE;
6de2ae4a 330 htab->splt = s;
252b5132 331
d98685ac
AM
332 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
333 .plt section. */
7325306f
RS
334 if (bed->want_plt_sym)
335 {
336 h = _bfd_elf_define_linkage_sym (abfd, info, s,
337 "_PROCEDURE_LINKAGE_TABLE_");
338 elf_hash_table (info)->hplt = h;
339 if (h == NULL)
340 return FALSE;
341 }
252b5132 342
14b2f831
AM
343 s = bfd_make_section_anyway_with_flags (abfd,
344 (bed->rela_plts_and_copies_p
345 ? ".rela.plt" : ".rel.plt"),
346 flags | SEC_READONLY);
252b5132 347 if (s == NULL
45d6a902 348 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 349 return FALSE;
6de2ae4a 350 htab->srelplt = s;
252b5132
RH
351
352 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 353 return FALSE;
252b5132 354
3018b441
RH
355 if (bed->want_dynbss)
356 {
357 /* The .dynbss section is a place to put symbols which are defined
358 by dynamic objects, are referenced by regular objects, and are
359 not functions. We must allocate space for them in the process
360 image and use a R_*_COPY reloc to tell the dynamic linker to
361 initialize them at run time. The linker script puts the .dynbss
362 section into the .bss section of the final image. */
14b2f831
AM
363 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
364 (SEC_ALLOC | SEC_LINKER_CREATED));
3496cb2a 365 if (s == NULL)
b34976b6 366 return FALSE;
252b5132 367
3018b441 368 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
369 normally needed. We need to create it here, though, so that the
370 linker will map it to an output section. We can't just create it
371 only if we need it, because we will not know whether we need it
372 until we have seen all the input files, and the first time the
373 main linker code calls BFD after examining all the input files
374 (size_dynamic_sections) the input sections have already been
375 mapped to the output sections. If the section turns out not to
376 be needed, we can discard it later. We will never need this
377 section when generating a shared object, since they do not use
378 copy relocs. */
3018b441
RH
379 if (! info->shared)
380 {
14b2f831
AM
381 s = bfd_make_section_anyway_with_flags (abfd,
382 (bed->rela_plts_and_copies_p
383 ? ".rela.bss" : ".rel.bss"),
384 flags | SEC_READONLY);
3018b441 385 if (s == NULL
45d6a902 386 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 387 return FALSE;
3018b441 388 }
252b5132
RH
389 }
390
b34976b6 391 return TRUE;
252b5132
RH
392}
393\f
252b5132
RH
394/* Record a new dynamic symbol. We record the dynamic symbols as we
395 read the input files, since we need to have a list of all of them
396 before we can determine the final sizes of the output sections.
397 Note that we may actually call this function even though we are not
398 going to output any dynamic symbols; in some cases we know that a
399 symbol should be in the dynamic symbol table, but only if there is
400 one. */
401
b34976b6 402bfd_boolean
c152c796
AM
403bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
404 struct elf_link_hash_entry *h)
252b5132
RH
405{
406 if (h->dynindx == -1)
407 {
2b0f7ef9 408 struct elf_strtab_hash *dynstr;
68b6ddd0 409 char *p;
252b5132 410 const char *name;
252b5132
RH
411 bfd_size_type indx;
412
7a13edea
NC
413 /* XXX: The ABI draft says the linker must turn hidden and
414 internal symbols into STB_LOCAL symbols when producing the
415 DSO. However, if ld.so honors st_other in the dynamic table,
416 this would not be necessary. */
417 switch (ELF_ST_VISIBILITY (h->other))
418 {
419 case STV_INTERNAL:
420 case STV_HIDDEN:
9d6eee78
L
421 if (h->root.type != bfd_link_hash_undefined
422 && h->root.type != bfd_link_hash_undefweak)
38048eb9 423 {
f5385ebf 424 h->forced_local = 1;
67687978
PB
425 if (!elf_hash_table (info)->is_relocatable_executable)
426 return TRUE;
7a13edea 427 }
0444bdd4 428
7a13edea
NC
429 default:
430 break;
431 }
432
252b5132
RH
433 h->dynindx = elf_hash_table (info)->dynsymcount;
434 ++elf_hash_table (info)->dynsymcount;
435
436 dynstr = elf_hash_table (info)->dynstr;
437 if (dynstr == NULL)
438 {
439 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 440 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 441 if (dynstr == NULL)
b34976b6 442 return FALSE;
252b5132
RH
443 }
444
445 /* We don't put any version information in the dynamic string
aad5d350 446 table. */
252b5132
RH
447 name = h->root.root.string;
448 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
449 if (p != NULL)
450 /* We know that the p points into writable memory. In fact,
451 there are only a few symbols that have read-only names, being
452 those like _GLOBAL_OFFSET_TABLE_ that are created specially
453 by the backends. Most symbols will have names pointing into
454 an ELF string table read from a file, or to objalloc memory. */
455 *p = 0;
456
457 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
458
459 if (p != NULL)
460 *p = ELF_VER_CHR;
252b5132
RH
461
462 if (indx == (bfd_size_type) -1)
b34976b6 463 return FALSE;
252b5132
RH
464 h->dynstr_index = indx;
465 }
466
b34976b6 467 return TRUE;
252b5132 468}
45d6a902 469\f
55255dae
L
470/* Mark a symbol dynamic. */
471
28caa186 472static void
55255dae 473bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
474 struct elf_link_hash_entry *h,
475 Elf_Internal_Sym *sym)
55255dae 476{
40b36307 477 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 478
40b36307
L
479 /* It may be called more than once on the same H. */
480 if(h->dynamic || info->relocatable)
55255dae
L
481 return;
482
40b36307
L
483 if ((info->dynamic_data
484 && (h->type == STT_OBJECT
485 || (sym != NULL
486 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
a0c8462f 487 || (d != NULL
40b36307
L
488 && h->root.type == bfd_link_hash_new
489 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
490 h->dynamic = 1;
491}
492
45d6a902
AM
493/* Record an assignment to a symbol made by a linker script. We need
494 this in case some dynamic object refers to this symbol. */
495
496bfd_boolean
fe21a8fc
L
497bfd_elf_record_link_assignment (bfd *output_bfd,
498 struct bfd_link_info *info,
268b6b39 499 const char *name,
fe21a8fc
L
500 bfd_boolean provide,
501 bfd_boolean hidden)
45d6a902 502{
00cbee0a 503 struct elf_link_hash_entry *h, *hv;
4ea42fb7 504 struct elf_link_hash_table *htab;
00cbee0a 505 const struct elf_backend_data *bed;
45d6a902 506
0eddce27 507 if (!is_elf_hash_table (info->hash))
45d6a902
AM
508 return TRUE;
509
4ea42fb7
AM
510 htab = elf_hash_table (info);
511 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 512 if (h == NULL)
4ea42fb7 513 return provide;
45d6a902 514
00cbee0a 515 switch (h->root.type)
77cfaee6 516 {
00cbee0a
L
517 case bfd_link_hash_defined:
518 case bfd_link_hash_defweak:
519 case bfd_link_hash_common:
520 break;
521 case bfd_link_hash_undefweak:
522 case bfd_link_hash_undefined:
523 /* Since we're defining the symbol, don't let it seem to have not
524 been defined. record_dynamic_symbol and size_dynamic_sections
525 may depend on this. */
4ea42fb7 526 h->root.type = bfd_link_hash_new;
77cfaee6
AM
527 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
528 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
529 break;
530 case bfd_link_hash_new:
40b36307 531 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 532 h->non_elf = 0;
00cbee0a
L
533 break;
534 case bfd_link_hash_indirect:
535 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 536 the versioned symbol point to this one. */
00cbee0a
L
537 bed = get_elf_backend_data (output_bfd);
538 hv = h;
539 while (hv->root.type == bfd_link_hash_indirect
540 || hv->root.type == bfd_link_hash_warning)
541 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
542 /* We don't need to update h->root.u since linker will set them
543 later. */
544 h->root.type = bfd_link_hash_undefined;
545 hv->root.type = bfd_link_hash_indirect;
546 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
547 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
548 break;
549 case bfd_link_hash_warning:
550 abort ();
551 break;
55255dae 552 }
45d6a902
AM
553
554 /* If this symbol is being provided by the linker script, and it is
555 currently defined by a dynamic object, but not by a regular
556 object, then mark it as undefined so that the generic linker will
557 force the correct value. */
558 if (provide
f5385ebf
AM
559 && h->def_dynamic
560 && !h->def_regular)
45d6a902
AM
561 h->root.type = bfd_link_hash_undefined;
562
563 /* If this symbol is not being provided by the linker script, and it is
564 currently defined by a dynamic object, but not by a regular object,
565 then clear out any version information because the symbol will not be
566 associated with the dynamic object any more. */
567 if (!provide
f5385ebf
AM
568 && h->def_dynamic
569 && !h->def_regular)
45d6a902
AM
570 h->verinfo.verdef = NULL;
571
f5385ebf 572 h->def_regular = 1;
45d6a902 573
eb8476a6 574 if (hidden)
fe21a8fc 575 {
91d6fa6a 576 bed = get_elf_backend_data (output_bfd);
b8297068
AM
577 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
578 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
579 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
580 }
581
6fa3860b
PB
582 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
583 and executables. */
584 if (!info->relocatable
585 && h->dynindx != -1
586 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
587 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
588 h->forced_local = 1;
589
f5385ebf
AM
590 if ((h->def_dynamic
591 || h->ref_dynamic
67687978
PB
592 || info->shared
593 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
45d6a902
AM
594 && h->dynindx == -1)
595 {
c152c796 596 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
597 return FALSE;
598
599 /* If this is a weak defined symbol, and we know a corresponding
600 real symbol from the same dynamic object, make sure the real
601 symbol is also made into a dynamic symbol. */
f6e332e6
AM
602 if (h->u.weakdef != NULL
603 && h->u.weakdef->dynindx == -1)
45d6a902 604 {
f6e332e6 605 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
606 return FALSE;
607 }
608 }
609
610 return TRUE;
611}
42751cf3 612
8c58d23b
AM
613/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
614 success, and 2 on a failure caused by attempting to record a symbol
615 in a discarded section, eg. a discarded link-once section symbol. */
616
617int
c152c796
AM
618bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
619 bfd *input_bfd,
620 long input_indx)
8c58d23b
AM
621{
622 bfd_size_type amt;
623 struct elf_link_local_dynamic_entry *entry;
624 struct elf_link_hash_table *eht;
625 struct elf_strtab_hash *dynstr;
626 unsigned long dynstr_index;
627 char *name;
628 Elf_External_Sym_Shndx eshndx;
629 char esym[sizeof (Elf64_External_Sym)];
630
0eddce27 631 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
632 return 0;
633
634 /* See if the entry exists already. */
635 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
636 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
637 return 1;
638
639 amt = sizeof (*entry);
a50b1753 640 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
641 if (entry == NULL)
642 return 0;
643
644 /* Go find the symbol, so that we can find it's name. */
645 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 646 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
647 {
648 bfd_release (input_bfd, entry);
649 return 0;
650 }
651
652 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 653 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
654 {
655 asection *s;
656
657 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
658 if (s == NULL || bfd_is_abs_section (s->output_section))
659 {
660 /* We can still bfd_release here as nothing has done another
661 bfd_alloc. We can't do this later in this function. */
662 bfd_release (input_bfd, entry);
663 return 2;
664 }
665 }
666
667 name = (bfd_elf_string_from_elf_section
668 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
669 entry->isym.st_name));
670
671 dynstr = elf_hash_table (info)->dynstr;
672 if (dynstr == NULL)
673 {
674 /* Create a strtab to hold the dynamic symbol names. */
675 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
676 if (dynstr == NULL)
677 return 0;
678 }
679
b34976b6 680 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
8c58d23b
AM
681 if (dynstr_index == (unsigned long) -1)
682 return 0;
683 entry->isym.st_name = dynstr_index;
684
685 eht = elf_hash_table (info);
686
687 entry->next = eht->dynlocal;
688 eht->dynlocal = entry;
689 entry->input_bfd = input_bfd;
690 entry->input_indx = input_indx;
691 eht->dynsymcount++;
692
693 /* Whatever binding the symbol had before, it's now local. */
694 entry->isym.st_info
695 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
696
697 /* The dynindx will be set at the end of size_dynamic_sections. */
698
699 return 1;
700}
701
30b30c21 702/* Return the dynindex of a local dynamic symbol. */
42751cf3 703
30b30c21 704long
268b6b39
AM
705_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
706 bfd *input_bfd,
707 long input_indx)
30b30c21
RH
708{
709 struct elf_link_local_dynamic_entry *e;
710
711 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
712 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
713 return e->dynindx;
714 return -1;
715}
716
717/* This function is used to renumber the dynamic symbols, if some of
718 them are removed because they are marked as local. This is called
719 via elf_link_hash_traverse. */
720
b34976b6 721static bfd_boolean
268b6b39
AM
722elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
723 void *data)
42751cf3 724{
a50b1753 725 size_t *count = (size_t *) data;
30b30c21 726
6fa3860b
PB
727 if (h->forced_local)
728 return TRUE;
729
730 if (h->dynindx != -1)
731 h->dynindx = ++(*count);
732
733 return TRUE;
734}
735
736
737/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
738 STB_LOCAL binding. */
739
740static bfd_boolean
741elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
742 void *data)
743{
a50b1753 744 size_t *count = (size_t *) data;
6fa3860b 745
6fa3860b
PB
746 if (!h->forced_local)
747 return TRUE;
748
42751cf3 749 if (h->dynindx != -1)
30b30c21
RH
750 h->dynindx = ++(*count);
751
b34976b6 752 return TRUE;
42751cf3 753}
30b30c21 754
aee6f5b4
AO
755/* Return true if the dynamic symbol for a given section should be
756 omitted when creating a shared library. */
757bfd_boolean
758_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
759 struct bfd_link_info *info,
760 asection *p)
761{
74541ad4
AM
762 struct elf_link_hash_table *htab;
763
aee6f5b4
AO
764 switch (elf_section_data (p)->this_hdr.sh_type)
765 {
766 case SHT_PROGBITS:
767 case SHT_NOBITS:
768 /* If sh_type is yet undecided, assume it could be
769 SHT_PROGBITS/SHT_NOBITS. */
770 case SHT_NULL:
74541ad4
AM
771 htab = elf_hash_table (info);
772 if (p == htab->tls_sec)
773 return FALSE;
774
775 if (htab->text_index_section != NULL)
776 return p != htab->text_index_section && p != htab->data_index_section;
777
aee6f5b4
AO
778 if (strcmp (p->name, ".got") == 0
779 || strcmp (p->name, ".got.plt") == 0
780 || strcmp (p->name, ".plt") == 0)
781 {
782 asection *ip;
aee6f5b4 783
74541ad4 784 if (htab->dynobj != NULL
3d4d4302 785 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
aee6f5b4
AO
786 && ip->output_section == p)
787 return TRUE;
788 }
789 return FALSE;
790
791 /* There shouldn't be section relative relocations
792 against any other section. */
793 default:
794 return TRUE;
795 }
796}
797
062e2358 798/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
799 symbol for each output section, which come first. Next come symbols
800 which have been forced to local binding. Then all of the back-end
801 allocated local dynamic syms, followed by the rest of the global
802 symbols. */
30b30c21 803
554220db
AM
804static unsigned long
805_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
806 struct bfd_link_info *info,
807 unsigned long *section_sym_count)
30b30c21
RH
808{
809 unsigned long dynsymcount = 0;
810
67687978 811 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
30b30c21 812 {
aee6f5b4 813 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
814 asection *p;
815 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 816 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
817 && (p->flags & SEC_ALLOC) != 0
818 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
819 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
820 else
821 elf_section_data (p)->dynindx = 0;
30b30c21 822 }
554220db 823 *section_sym_count = dynsymcount;
30b30c21 824
6fa3860b
PB
825 elf_link_hash_traverse (elf_hash_table (info),
826 elf_link_renumber_local_hash_table_dynsyms,
827 &dynsymcount);
828
30b30c21
RH
829 if (elf_hash_table (info)->dynlocal)
830 {
831 struct elf_link_local_dynamic_entry *p;
832 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
833 p->dynindx = ++dynsymcount;
834 }
835
836 elf_link_hash_traverse (elf_hash_table (info),
837 elf_link_renumber_hash_table_dynsyms,
838 &dynsymcount);
839
840 /* There is an unused NULL entry at the head of the table which
841 we must account for in our count. Unless there weren't any
842 symbols, which means we'll have no table at all. */
843 if (dynsymcount != 0)
844 ++dynsymcount;
845
ccabcbe5
AM
846 elf_hash_table (info)->dynsymcount = dynsymcount;
847 return dynsymcount;
30b30c21 848}
252b5132 849
54ac0771
L
850/* Merge st_other field. */
851
852static void
853elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
854 Elf_Internal_Sym *isym, bfd_boolean definition,
855 bfd_boolean dynamic)
856{
857 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
858
859 /* If st_other has a processor-specific meaning, specific
860 code might be needed here. We never merge the visibility
861 attribute with the one from a dynamic object. */
862 if (bed->elf_backend_merge_symbol_attribute)
863 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
864 dynamic);
865
866 /* If this symbol has default visibility and the user has requested
867 we not re-export it, then mark it as hidden. */
868 if (definition
869 && !dynamic
870 && (abfd->no_export
871 || (abfd->my_archive && abfd->my_archive->no_export))
872 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
873 isym->st_other = (STV_HIDDEN
874 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
875
876 if (!dynamic && ELF_ST_VISIBILITY (isym->st_other) != 0)
877 {
878 unsigned char hvis, symvis, other, nvis;
879
880 /* Only merge the visibility. Leave the remainder of the
881 st_other field to elf_backend_merge_symbol_attribute. */
882 other = h->other & ~ELF_ST_VISIBILITY (-1);
883
884 /* Combine visibilities, using the most constraining one. */
885 hvis = ELF_ST_VISIBILITY (h->other);
886 symvis = ELF_ST_VISIBILITY (isym->st_other);
887 if (! hvis)
888 nvis = symvis;
889 else if (! symvis)
890 nvis = hvis;
891 else
892 nvis = hvis < symvis ? hvis : symvis;
893
894 h->other = other | nvis;
895 }
896}
897
4f3fedcf
AM
898/* This function is called when we want to merge a new symbol with an
899 existing symbol. It handles the various cases which arise when we
900 find a definition in a dynamic object, or when there is already a
901 definition in a dynamic object. The new symbol is described by
902 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
903 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
904 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
905 of an old common symbol. We set OVERRIDE if the old symbol is
906 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
907 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
908 to change. By OK to change, we mean that we shouldn't warn if the
909 type or size does change. */
45d6a902 910
8a56bd02 911static bfd_boolean
268b6b39
AM
912_bfd_elf_merge_symbol (bfd *abfd,
913 struct bfd_link_info *info,
914 const char *name,
915 Elf_Internal_Sym *sym,
916 asection **psec,
917 bfd_vma *pvalue,
4f3fedcf
AM
918 struct elf_link_hash_entry **sym_hash,
919 bfd **poldbfd,
37a9e49a 920 bfd_boolean *pold_weak,
af44c138 921 unsigned int *pold_alignment,
268b6b39
AM
922 bfd_boolean *skip,
923 bfd_boolean *override,
924 bfd_boolean *type_change_ok,
0f8a2703 925 bfd_boolean *size_change_ok)
252b5132 926{
7479dfd4 927 asection *sec, *oldsec;
45d6a902 928 struct elf_link_hash_entry *h;
90c984fc 929 struct elf_link_hash_entry *hi;
45d6a902
AM
930 struct elf_link_hash_entry *flip;
931 int bind;
932 bfd *oldbfd;
933 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 934 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 935 const struct elf_backend_data *bed;
45d6a902
AM
936
937 *skip = FALSE;
938 *override = FALSE;
939
940 sec = *psec;
941 bind = ELF_ST_BIND (sym->st_info);
942
943 if (! bfd_is_und_section (sec))
944 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
945 else
946 h = ((struct elf_link_hash_entry *)
947 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
948 if (h == NULL)
949 return FALSE;
950 *sym_hash = h;
252b5132 951
88ba32a0
L
952 bed = get_elf_backend_data (abfd);
953
90c984fc
L
954 /* For merging, we only care about real symbols. But we need to make
955 sure that indirect symbol dynamic flags are updated. */
956 hi = h;
45d6a902
AM
957 while (h->root.type == bfd_link_hash_indirect
958 || h->root.type == bfd_link_hash_warning)
959 h = (struct elf_link_hash_entry *) h->root.u.i.link;
960
934bce08
AM
961 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
962 existing symbol. */
963
964 oldbfd = NULL;
965 oldsec = NULL;
966 switch (h->root.type)
967 {
968 default:
969 break;
970
971 case bfd_link_hash_undefined:
972 case bfd_link_hash_undefweak:
973 oldbfd = h->root.u.undef.abfd;
974 break;
975
976 case bfd_link_hash_defined:
977 case bfd_link_hash_defweak:
978 oldbfd = h->root.u.def.section->owner;
979 oldsec = h->root.u.def.section;
980 break;
981
982 case bfd_link_hash_common:
983 oldbfd = h->root.u.c.p->section->owner;
984 oldsec = h->root.u.c.p->section;
985 if (pold_alignment)
986 *pold_alignment = h->root.u.c.p->alignment_power;
987 break;
988 }
989 if (poldbfd && *poldbfd == NULL)
990 *poldbfd = oldbfd;
991
992 /* Differentiate strong and weak symbols. */
993 newweak = bind == STB_WEAK;
994 oldweak = (h->root.type == bfd_link_hash_defweak
995 || h->root.type == bfd_link_hash_undefweak);
996 if (pold_weak)
997 *pold_weak = oldweak;
998
999 /* This code is for coping with dynamic objects, and is only useful
1000 if we are doing an ELF link. */
1001 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1002 return TRUE;
1003
40b36307 1004 /* We have to check it for every instance since the first few may be
ee659f1f 1005 references and not all compilers emit symbol type for undefined
40b36307
L
1006 symbols. */
1007 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1008
ee659f1f
AM
1009 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1010 respectively, is from a dynamic object. */
1011
1012 newdyn = (abfd->flags & DYNAMIC) != 0;
1013
1014 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1015 syms and defined syms in dynamic libraries respectively.
1016 ref_dynamic on the other hand can be set for a symbol defined in
1017 a dynamic library, and def_dynamic may not be set; When the
1018 definition in a dynamic lib is overridden by a definition in the
1019 executable use of the symbol in the dynamic lib becomes a
1020 reference to the executable symbol. */
1021 if (newdyn)
1022 {
1023 if (bfd_is_und_section (sec))
1024 {
1025 if (bind != STB_WEAK)
1026 {
1027 h->ref_dynamic_nonweak = 1;
1028 hi->ref_dynamic_nonweak = 1;
1029 }
1030 }
1031 else
1032 {
1033 h->dynamic_def = 1;
1034 hi->dynamic_def = 1;
1035 }
1036 }
1037
45d6a902
AM
1038 /* If we just created the symbol, mark it as being an ELF symbol.
1039 Other than that, there is nothing to do--there is no merge issue
1040 with a newly defined symbol--so we just return. */
1041
1042 if (h->root.type == bfd_link_hash_new)
252b5132 1043 {
f5385ebf 1044 h->non_elf = 0;
45d6a902
AM
1045 return TRUE;
1046 }
252b5132 1047
45d6a902
AM
1048 /* In cases involving weak versioned symbols, we may wind up trying
1049 to merge a symbol with itself. Catch that here, to avoid the
1050 confusion that results if we try to override a symbol with
1051 itself. The additional tests catch cases like
1052 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1053 dynamic object, which we do want to handle here. */
1054 if (abfd == oldbfd
895fa45f 1055 && (newweak || oldweak)
45d6a902 1056 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1057 || !h->def_regular))
45d6a902
AM
1058 return TRUE;
1059
707bba77 1060 olddyn = FALSE;
45d6a902
AM
1061 if (oldbfd != NULL)
1062 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1063 else if (oldsec != NULL)
45d6a902 1064 {
707bba77 1065 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1066 indices used by MIPS ELF. */
707bba77 1067 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1068 }
252b5132 1069
45d6a902
AM
1070 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1071 respectively, appear to be a definition rather than reference. */
1072
707bba77 1073 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1074
707bba77
AM
1075 olddef = (h->root.type != bfd_link_hash_undefined
1076 && h->root.type != bfd_link_hash_undefweak
1077 && h->root.type != bfd_link_hash_common);
45d6a902 1078
0a36a439
L
1079 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1080 respectively, appear to be a function. */
1081
1082 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1083 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1084
1085 oldfunc = (h->type != STT_NOTYPE
1086 && bed->is_function_type (h->type));
1087
580a2b6e
L
1088 /* When we try to create a default indirect symbol from the dynamic
1089 definition with the default version, we skip it if its type and
40101021 1090 the type of existing regular definition mismatch. */
580a2b6e 1091 if (pold_alignment == NULL
580a2b6e
L
1092 && newdyn
1093 && newdef
1094 && !olddyn
4584ec12
L
1095 && (((olddef || h->root.type == bfd_link_hash_common)
1096 && ELF_ST_TYPE (sym->st_info) != h->type
1097 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1098 && h->type != STT_NOTYPE
1099 && !(newfunc && oldfunc))
1100 || (olddef
1101 && ((h->type == STT_GNU_IFUNC)
1102 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
580a2b6e
L
1103 {
1104 *skip = TRUE;
1105 return TRUE;
1106 }
1107
4c34aff8
AM
1108 /* Check TLS symbols. We don't check undefined symbols introduced
1109 by "ld -u" which have no type (and oldbfd NULL), and we don't
1110 check symbols from plugins because they also have no type. */
1111 if (oldbfd != NULL
1112 && (oldbfd->flags & BFD_PLUGIN) == 0
1113 && (abfd->flags & BFD_PLUGIN) == 0
1114 && ELF_ST_TYPE (sym->st_info) != h->type
1115 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1116 {
1117 bfd *ntbfd, *tbfd;
1118 bfd_boolean ntdef, tdef;
1119 asection *ntsec, *tsec;
1120
1121 if (h->type == STT_TLS)
1122 {
3b36f7e6 1123 ntbfd = abfd;
7479dfd4
L
1124 ntsec = sec;
1125 ntdef = newdef;
1126 tbfd = oldbfd;
1127 tsec = oldsec;
1128 tdef = olddef;
1129 }
1130 else
1131 {
1132 ntbfd = oldbfd;
1133 ntsec = oldsec;
1134 ntdef = olddef;
1135 tbfd = abfd;
1136 tsec = sec;
1137 tdef = newdef;
1138 }
1139
1140 if (tdef && ntdef)
1141 (*_bfd_error_handler)
191c0c42
AM
1142 (_("%s: TLS definition in %B section %A "
1143 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1144 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1145 else if (!tdef && !ntdef)
1146 (*_bfd_error_handler)
191c0c42
AM
1147 (_("%s: TLS reference in %B "
1148 "mismatches non-TLS reference in %B"),
7479dfd4
L
1149 tbfd, ntbfd, h->root.root.string);
1150 else if (tdef)
1151 (*_bfd_error_handler)
191c0c42
AM
1152 (_("%s: TLS definition in %B section %A "
1153 "mismatches non-TLS reference in %B"),
7479dfd4
L
1154 tbfd, tsec, ntbfd, h->root.root.string);
1155 else
1156 (*_bfd_error_handler)
191c0c42
AM
1157 (_("%s: TLS reference in %B "
1158 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1159 tbfd, ntbfd, ntsec, h->root.root.string);
1160
1161 bfd_set_error (bfd_error_bad_value);
1162 return FALSE;
1163 }
1164
45d6a902
AM
1165 /* If the old symbol has non-default visibility, we ignore the new
1166 definition from a dynamic object. */
1167 if (newdyn
9c7a29a3 1168 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1169 && !bfd_is_und_section (sec))
1170 {
1171 *skip = TRUE;
1172 /* Make sure this symbol is dynamic. */
f5385ebf 1173 h->ref_dynamic = 1;
90c984fc 1174 hi->ref_dynamic = 1;
45d6a902
AM
1175 /* A protected symbol has external availability. Make sure it is
1176 recorded as dynamic.
1177
1178 FIXME: Should we check type and size for protected symbol? */
1179 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1180 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1181 else
1182 return TRUE;
1183 }
1184 else if (!newdyn
9c7a29a3 1185 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1186 && h->def_dynamic)
45d6a902
AM
1187 {
1188 /* If the new symbol with non-default visibility comes from a
1189 relocatable file and the old definition comes from a dynamic
1190 object, we remove the old definition. */
6c9b78e6 1191 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1192 {
1193 /* Handle the case where the old dynamic definition is
1194 default versioned. We need to copy the symbol info from
1195 the symbol with default version to the normal one if it
1196 was referenced before. */
1197 if (h->ref_regular)
1198 {
6c9b78e6 1199 hi->root.type = h->root.type;
d2dee3b2 1200 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1201 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1202
6c9b78e6 1203 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1204 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1205 {
aed81c4e
MR
1206 /* If the new symbol is hidden or internal, completely undo
1207 any dynamic link state. */
1208 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1209 h->forced_local = 0;
1210 h->ref_dynamic = 0;
d2dee3b2
L
1211 }
1212 else
aed81c4e
MR
1213 h->ref_dynamic = 1;
1214
1215 h->def_dynamic = 0;
aed81c4e
MR
1216 /* FIXME: Should we check type and size for protected symbol? */
1217 h->size = 0;
1218 h->type = 0;
1219
6c9b78e6 1220 h = hi;
d2dee3b2
L
1221 }
1222 else
6c9b78e6 1223 h = hi;
d2dee3b2 1224 }
1de1a317 1225
f5eda473
AM
1226 /* If the old symbol was undefined before, then it will still be
1227 on the undefs list. If the new symbol is undefined or
1228 common, we can't make it bfd_link_hash_new here, because new
1229 undefined or common symbols will be added to the undefs list
1230 by _bfd_generic_link_add_one_symbol. Symbols may not be
1231 added twice to the undefs list. Also, if the new symbol is
1232 undefweak then we don't want to lose the strong undef. */
1233 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1234 {
1de1a317 1235 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1236 h->root.u.undef.abfd = abfd;
1237 }
1238 else
1239 {
1240 h->root.type = bfd_link_hash_new;
1241 h->root.u.undef.abfd = NULL;
1242 }
1243
f5eda473 1244 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1245 {
f5eda473
AM
1246 /* If the new symbol is hidden or internal, completely undo
1247 any dynamic link state. */
1248 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1249 h->forced_local = 0;
1250 h->ref_dynamic = 0;
45d6a902 1251 }
f5eda473
AM
1252 else
1253 h->ref_dynamic = 1;
1254 h->def_dynamic = 0;
45d6a902
AM
1255 /* FIXME: Should we check type and size for protected symbol? */
1256 h->size = 0;
1257 h->type = 0;
1258 return TRUE;
1259 }
14a793b2 1260
15b43f48
AM
1261 /* If a new weak symbol definition comes from a regular file and the
1262 old symbol comes from a dynamic library, we treat the new one as
1263 strong. Similarly, an old weak symbol definition from a regular
1264 file is treated as strong when the new symbol comes from a dynamic
1265 library. Further, an old weak symbol from a dynamic library is
1266 treated as strong if the new symbol is from a dynamic library.
1267 This reflects the way glibc's ld.so works.
1268
1269 Do this before setting *type_change_ok or *size_change_ok so that
1270 we warn properly when dynamic library symbols are overridden. */
1271
1272 if (newdef && !newdyn && olddyn)
0f8a2703 1273 newweak = FALSE;
15b43f48 1274 if (olddef && newdyn)
0f8a2703
AM
1275 oldweak = FALSE;
1276
d334575b 1277 /* Allow changes between different types of function symbol. */
0a36a439 1278 if (newfunc && oldfunc)
fcb93ecf
PB
1279 *type_change_ok = TRUE;
1280
79349b09
AM
1281 /* It's OK to change the type if either the existing symbol or the
1282 new symbol is weak. A type change is also OK if the old symbol
1283 is undefined and the new symbol is defined. */
252b5132 1284
79349b09
AM
1285 if (oldweak
1286 || newweak
1287 || (newdef
1288 && h->root.type == bfd_link_hash_undefined))
1289 *type_change_ok = TRUE;
1290
1291 /* It's OK to change the size if either the existing symbol or the
1292 new symbol is weak, or if the old symbol is undefined. */
1293
1294 if (*type_change_ok
1295 || h->root.type == bfd_link_hash_undefined)
1296 *size_change_ok = TRUE;
45d6a902 1297
45d6a902
AM
1298 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1299 symbol, respectively, appears to be a common symbol in a dynamic
1300 object. If a symbol appears in an uninitialized section, and is
1301 not weak, and is not a function, then it may be a common symbol
1302 which was resolved when the dynamic object was created. We want
1303 to treat such symbols specially, because they raise special
1304 considerations when setting the symbol size: if the symbol
1305 appears as a common symbol in a regular object, and the size in
1306 the regular object is larger, we must make sure that we use the
1307 larger size. This problematic case can always be avoided in C,
1308 but it must be handled correctly when using Fortran shared
1309 libraries.
1310
1311 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1312 likewise for OLDDYNCOMMON and OLDDEF.
1313
1314 Note that this test is just a heuristic, and that it is quite
1315 possible to have an uninitialized symbol in a shared object which
1316 is really a definition, rather than a common symbol. This could
1317 lead to some minor confusion when the symbol really is a common
1318 symbol in some regular object. However, I think it will be
1319 harmless. */
1320
1321 if (newdyn
1322 && newdef
79349b09 1323 && !newweak
45d6a902
AM
1324 && (sec->flags & SEC_ALLOC) != 0
1325 && (sec->flags & SEC_LOAD) == 0
1326 && sym->st_size > 0
0a36a439 1327 && !newfunc)
45d6a902
AM
1328 newdyncommon = TRUE;
1329 else
1330 newdyncommon = FALSE;
1331
1332 if (olddyn
1333 && olddef
1334 && h->root.type == bfd_link_hash_defined
f5385ebf 1335 && h->def_dynamic
45d6a902
AM
1336 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1337 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1338 && h->size > 0
0a36a439 1339 && !oldfunc)
45d6a902
AM
1340 olddyncommon = TRUE;
1341 else
1342 olddyncommon = FALSE;
1343
a4d8e49b
L
1344 /* We now know everything about the old and new symbols. We ask the
1345 backend to check if we can merge them. */
5d13b3b3
AM
1346 if (bed->merge_symbol != NULL)
1347 {
1348 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1349 return FALSE;
1350 sec = *psec;
1351 }
a4d8e49b 1352
45d6a902
AM
1353 /* If both the old and the new symbols look like common symbols in a
1354 dynamic object, set the size of the symbol to the larger of the
1355 two. */
1356
1357 if (olddyncommon
1358 && newdyncommon
1359 && sym->st_size != h->size)
1360 {
1361 /* Since we think we have two common symbols, issue a multiple
1362 common warning if desired. Note that we only warn if the
1363 size is different. If the size is the same, we simply let
1364 the old symbol override the new one as normally happens with
1365 symbols defined in dynamic objects. */
1366
1367 if (! ((*info->callbacks->multiple_common)
24f58f47 1368 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902 1369 return FALSE;
252b5132 1370
45d6a902
AM
1371 if (sym->st_size > h->size)
1372 h->size = sym->st_size;
252b5132 1373
45d6a902 1374 *size_change_ok = TRUE;
252b5132
RH
1375 }
1376
45d6a902
AM
1377 /* If we are looking at a dynamic object, and we have found a
1378 definition, we need to see if the symbol was already defined by
1379 some other object. If so, we want to use the existing
1380 definition, and we do not want to report a multiple symbol
1381 definition error; we do this by clobbering *PSEC to be
1382 bfd_und_section_ptr.
1383
1384 We treat a common symbol as a definition if the symbol in the
1385 shared library is a function, since common symbols always
1386 represent variables; this can cause confusion in principle, but
1387 any such confusion would seem to indicate an erroneous program or
1388 shared library. We also permit a common symbol in a regular
79349b09 1389 object to override a weak symbol in a shared object. */
45d6a902
AM
1390
1391 if (newdyn
1392 && newdef
77cfaee6 1393 && (olddef
45d6a902 1394 || (h->root.type == bfd_link_hash_common
0a36a439 1395 && (newweak || newfunc))))
45d6a902
AM
1396 {
1397 *override = TRUE;
1398 newdef = FALSE;
1399 newdyncommon = FALSE;
252b5132 1400
45d6a902
AM
1401 *psec = sec = bfd_und_section_ptr;
1402 *size_change_ok = TRUE;
252b5132 1403
45d6a902
AM
1404 /* If we get here when the old symbol is a common symbol, then
1405 we are explicitly letting it override a weak symbol or
1406 function in a dynamic object, and we don't want to warn about
1407 a type change. If the old symbol is a defined symbol, a type
1408 change warning may still be appropriate. */
252b5132 1409
45d6a902
AM
1410 if (h->root.type == bfd_link_hash_common)
1411 *type_change_ok = TRUE;
1412 }
1413
1414 /* Handle the special case of an old common symbol merging with a
1415 new symbol which looks like a common symbol in a shared object.
1416 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1417 common symbol, and let _bfd_generic_link_add_one_symbol do the
1418 right thing. */
45d6a902
AM
1419
1420 if (newdyncommon
1421 && h->root.type == bfd_link_hash_common)
1422 {
1423 *override = TRUE;
1424 newdef = FALSE;
1425 newdyncommon = FALSE;
1426 *pvalue = sym->st_size;
a4d8e49b 1427 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1428 *size_change_ok = TRUE;
1429 }
1430
c5e2cead 1431 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1432 if (newdef && olddef && newweak)
54ac0771 1433 {
35ed3f94 1434 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1435 if (!(oldbfd != NULL
1436 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1437 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1438 {
1439 newdef = FALSE;
1440 *skip = TRUE;
1441 }
54ac0771
L
1442
1443 /* Merge st_other. If the symbol already has a dynamic index,
1444 but visibility says it should not be visible, turn it into a
1445 local symbol. */
1446 elf_merge_st_other (abfd, h, sym, newdef, newdyn);
1447 if (h->dynindx != -1)
1448 switch (ELF_ST_VISIBILITY (h->other))
1449 {
1450 case STV_INTERNAL:
1451 case STV_HIDDEN:
1452 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1453 break;
1454 }
1455 }
c5e2cead 1456
45d6a902
AM
1457 /* If the old symbol is from a dynamic object, and the new symbol is
1458 a definition which is not from a dynamic object, then the new
1459 symbol overrides the old symbol. Symbols from regular files
1460 always take precedence over symbols from dynamic objects, even if
1461 they are defined after the dynamic object in the link.
1462
1463 As above, we again permit a common symbol in a regular object to
1464 override a definition in a shared object if the shared object
0f8a2703 1465 symbol is a function or is weak. */
45d6a902
AM
1466
1467 flip = NULL;
77cfaee6 1468 if (!newdyn
45d6a902
AM
1469 && (newdef
1470 || (bfd_is_com_section (sec)
0a36a439 1471 && (oldweak || oldfunc)))
45d6a902
AM
1472 && olddyn
1473 && olddef
f5385ebf 1474 && h->def_dynamic)
45d6a902
AM
1475 {
1476 /* Change the hash table entry to undefined, and let
1477 _bfd_generic_link_add_one_symbol do the right thing with the
1478 new definition. */
1479
1480 h->root.type = bfd_link_hash_undefined;
1481 h->root.u.undef.abfd = h->root.u.def.section->owner;
1482 *size_change_ok = TRUE;
1483
1484 olddef = FALSE;
1485 olddyncommon = FALSE;
1486
1487 /* We again permit a type change when a common symbol may be
1488 overriding a function. */
1489
1490 if (bfd_is_com_section (sec))
0a36a439
L
1491 {
1492 if (oldfunc)
1493 {
1494 /* If a common symbol overrides a function, make sure
1495 that it isn't defined dynamically nor has type
1496 function. */
1497 h->def_dynamic = 0;
1498 h->type = STT_NOTYPE;
1499 }
1500 *type_change_ok = TRUE;
1501 }
45d6a902 1502
6c9b78e6
AM
1503 if (hi->root.type == bfd_link_hash_indirect)
1504 flip = hi;
45d6a902
AM
1505 else
1506 /* This union may have been set to be non-NULL when this symbol
1507 was seen in a dynamic object. We must force the union to be
1508 NULL, so that it is correct for a regular symbol. */
1509 h->verinfo.vertree = NULL;
1510 }
1511
1512 /* Handle the special case of a new common symbol merging with an
1513 old symbol that looks like it might be a common symbol defined in
1514 a shared object. Note that we have already handled the case in
1515 which a new common symbol should simply override the definition
1516 in the shared library. */
1517
1518 if (! newdyn
1519 && bfd_is_com_section (sec)
1520 && olddyncommon)
1521 {
1522 /* It would be best if we could set the hash table entry to a
1523 common symbol, but we don't know what to use for the section
1524 or the alignment. */
1525 if (! ((*info->callbacks->multiple_common)
24f58f47 1526 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902
AM
1527 return FALSE;
1528
4cc11e76 1529 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1530 larger, pretend that the new symbol has its size. */
1531
1532 if (h->size > *pvalue)
1533 *pvalue = h->size;
1534
af44c138
L
1535 /* We need to remember the alignment required by the symbol
1536 in the dynamic object. */
1537 BFD_ASSERT (pold_alignment);
1538 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1539
1540 olddef = FALSE;
1541 olddyncommon = FALSE;
1542
1543 h->root.type = bfd_link_hash_undefined;
1544 h->root.u.undef.abfd = h->root.u.def.section->owner;
1545
1546 *size_change_ok = TRUE;
1547 *type_change_ok = TRUE;
1548
6c9b78e6
AM
1549 if (hi->root.type == bfd_link_hash_indirect)
1550 flip = hi;
45d6a902
AM
1551 else
1552 h->verinfo.vertree = NULL;
1553 }
1554
1555 if (flip != NULL)
1556 {
1557 /* Handle the case where we had a versioned symbol in a dynamic
1558 library and now find a definition in a normal object. In this
1559 case, we make the versioned symbol point to the normal one. */
45d6a902 1560 flip->root.type = h->root.type;
00cbee0a 1561 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1562 h->root.type = bfd_link_hash_indirect;
1563 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1564 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1565 if (h->def_dynamic)
45d6a902 1566 {
f5385ebf
AM
1567 h->def_dynamic = 0;
1568 flip->ref_dynamic = 1;
45d6a902
AM
1569 }
1570 }
1571
45d6a902
AM
1572 return TRUE;
1573}
1574
1575/* This function is called to create an indirect symbol from the
1576 default for the symbol with the default version if needed. The
4f3fedcf 1577 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1578 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1579
28caa186 1580static bfd_boolean
268b6b39
AM
1581_bfd_elf_add_default_symbol (bfd *abfd,
1582 struct bfd_link_info *info,
1583 struct elf_link_hash_entry *h,
1584 const char *name,
1585 Elf_Internal_Sym *sym,
4f3fedcf
AM
1586 asection *sec,
1587 bfd_vma value,
1588 bfd **poldbfd,
e3c9d234 1589 bfd_boolean *dynsym)
45d6a902
AM
1590{
1591 bfd_boolean type_change_ok;
1592 bfd_boolean size_change_ok;
1593 bfd_boolean skip;
1594 char *shortname;
1595 struct elf_link_hash_entry *hi;
1596 struct bfd_link_hash_entry *bh;
9c5bfbb7 1597 const struct elf_backend_data *bed;
45d6a902
AM
1598 bfd_boolean collect;
1599 bfd_boolean dynamic;
e3c9d234 1600 bfd_boolean override;
45d6a902
AM
1601 char *p;
1602 size_t len, shortlen;
ffd65175 1603 asection *tmp_sec;
45d6a902
AM
1604
1605 /* If this symbol has a version, and it is the default version, we
1606 create an indirect symbol from the default name to the fully
1607 decorated name. This will cause external references which do not
1608 specify a version to be bound to this version of the symbol. */
1609 p = strchr (name, ELF_VER_CHR);
1610 if (p == NULL || p[1] != ELF_VER_CHR)
1611 return TRUE;
1612
45d6a902
AM
1613 bed = get_elf_backend_data (abfd);
1614 collect = bed->collect;
1615 dynamic = (abfd->flags & DYNAMIC) != 0;
1616
1617 shortlen = p - name;
a50b1753 1618 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1619 if (shortname == NULL)
1620 return FALSE;
1621 memcpy (shortname, name, shortlen);
1622 shortname[shortlen] = '\0';
1623
1624 /* We are going to create a new symbol. Merge it with any existing
1625 symbol with this name. For the purposes of the merge, act as
1626 though we were defining the symbol we just defined, although we
1627 actually going to define an indirect symbol. */
1628 type_change_ok = FALSE;
1629 size_change_ok = FALSE;
ffd65175
AM
1630 tmp_sec = sec;
1631 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1632 &hi, poldbfd, NULL, NULL, &skip, &override,
af44c138 1633 &type_change_ok, &size_change_ok))
45d6a902
AM
1634 return FALSE;
1635
1636 if (skip)
1637 goto nondefault;
1638
1639 if (! override)
1640 {
1641 bh = &hi->root;
1642 if (! (_bfd_generic_link_add_one_symbol
1643 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
268b6b39 1644 0, name, FALSE, collect, &bh)))
45d6a902
AM
1645 return FALSE;
1646 hi = (struct elf_link_hash_entry *) bh;
1647 }
1648 else
1649 {
1650 /* In this case the symbol named SHORTNAME is overriding the
1651 indirect symbol we want to add. We were planning on making
1652 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1653 is the name without a version. NAME is the fully versioned
1654 name, and it is the default version.
1655
1656 Overriding means that we already saw a definition for the
1657 symbol SHORTNAME in a regular object, and it is overriding
1658 the symbol defined in the dynamic object.
1659
1660 When this happens, we actually want to change NAME, the
1661 symbol we just added, to refer to SHORTNAME. This will cause
1662 references to NAME in the shared object to become references
1663 to SHORTNAME in the regular object. This is what we expect
1664 when we override a function in a shared object: that the
1665 references in the shared object will be mapped to the
1666 definition in the regular object. */
1667
1668 while (hi->root.type == bfd_link_hash_indirect
1669 || hi->root.type == bfd_link_hash_warning)
1670 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1671
1672 h->root.type = bfd_link_hash_indirect;
1673 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1674 if (h->def_dynamic)
45d6a902 1675 {
f5385ebf
AM
1676 h->def_dynamic = 0;
1677 hi->ref_dynamic = 1;
1678 if (hi->ref_regular
1679 || hi->def_regular)
45d6a902 1680 {
c152c796 1681 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1682 return FALSE;
1683 }
1684 }
1685
1686 /* Now set HI to H, so that the following code will set the
1687 other fields correctly. */
1688 hi = h;
1689 }
1690
fab4a87f
L
1691 /* Check if HI is a warning symbol. */
1692 if (hi->root.type == bfd_link_hash_warning)
1693 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1694
45d6a902
AM
1695 /* If there is a duplicate definition somewhere, then HI may not
1696 point to an indirect symbol. We will have reported an error to
1697 the user in that case. */
1698
1699 if (hi->root.type == bfd_link_hash_indirect)
1700 {
1701 struct elf_link_hash_entry *ht;
1702
45d6a902 1703 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1704 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1705
68c88cd4
AM
1706 /* A reference to the SHORTNAME symbol from a dynamic library
1707 will be satisfied by the versioned symbol at runtime. In
1708 effect, we have a reference to the versioned symbol. */
1709 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1710 hi->dynamic_def |= ht->dynamic_def;
1711
45d6a902
AM
1712 /* See if the new flags lead us to realize that the symbol must
1713 be dynamic. */
1714 if (! *dynsym)
1715 {
1716 if (! dynamic)
1717 {
ca4a656b 1718 if (! info->executable
90c984fc 1719 || hi->def_dynamic
f5385ebf 1720 || hi->ref_dynamic)
45d6a902
AM
1721 *dynsym = TRUE;
1722 }
1723 else
1724 {
f5385ebf 1725 if (hi->ref_regular)
45d6a902
AM
1726 *dynsym = TRUE;
1727 }
1728 }
1729 }
1730
1731 /* We also need to define an indirection from the nondefault version
1732 of the symbol. */
1733
1734nondefault:
1735 len = strlen (name);
a50b1753 1736 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1737 if (shortname == NULL)
1738 return FALSE;
1739 memcpy (shortname, name, shortlen);
1740 memcpy (shortname + shortlen, p + 1, len - shortlen);
1741
1742 /* Once again, merge with any existing symbol. */
1743 type_change_ok = FALSE;
1744 size_change_ok = FALSE;
ffd65175
AM
1745 tmp_sec = sec;
1746 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1747 &hi, poldbfd, NULL, NULL, &skip, &override,
af44c138 1748 &type_change_ok, &size_change_ok))
45d6a902
AM
1749 return FALSE;
1750
1751 if (skip)
1752 return TRUE;
1753
1754 if (override)
1755 {
1756 /* Here SHORTNAME is a versioned name, so we don't expect to see
1757 the type of override we do in the case above unless it is
4cc11e76 1758 overridden by a versioned definition. */
45d6a902
AM
1759 if (hi->root.type != bfd_link_hash_defined
1760 && hi->root.type != bfd_link_hash_defweak)
1761 (*_bfd_error_handler)
d003868e
AM
1762 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1763 abfd, shortname);
45d6a902
AM
1764 }
1765 else
1766 {
1767 bh = &hi->root;
1768 if (! (_bfd_generic_link_add_one_symbol
1769 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1770 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1771 return FALSE;
1772 hi = (struct elf_link_hash_entry *) bh;
1773
1774 /* If there is a duplicate definition somewhere, then HI may not
1775 point to an indirect symbol. We will have reported an error
1776 to the user in that case. */
1777
1778 if (hi->root.type == bfd_link_hash_indirect)
1779 {
fcfa13d2 1780 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
1781 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1782 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
1783
1784 /* See if the new flags lead us to realize that the symbol
1785 must be dynamic. */
1786 if (! *dynsym)
1787 {
1788 if (! dynamic)
1789 {
ca4a656b 1790 if (! info->executable
f5385ebf 1791 || hi->ref_dynamic)
45d6a902
AM
1792 *dynsym = TRUE;
1793 }
1794 else
1795 {
f5385ebf 1796 if (hi->ref_regular)
45d6a902
AM
1797 *dynsym = TRUE;
1798 }
1799 }
1800 }
1801 }
1802
1803 return TRUE;
1804}
1805\f
1806/* This routine is used to export all defined symbols into the dynamic
1807 symbol table. It is called via elf_link_hash_traverse. */
1808
28caa186 1809static bfd_boolean
268b6b39 1810_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1811{
a50b1753 1812 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
1813
1814 /* Ignore indirect symbols. These are added by the versioning code. */
1815 if (h->root.type == bfd_link_hash_indirect)
1816 return TRUE;
1817
7686d77d
AM
1818 /* Ignore this if we won't export it. */
1819 if (!eif->info->export_dynamic && !h->dynamic)
1820 return TRUE;
45d6a902
AM
1821
1822 if (h->dynindx == -1
fd91d419
L
1823 && (h->def_regular || h->ref_regular)
1824 && ! bfd_hide_sym_by_version (eif->info->version_info,
1825 h->root.root.string))
45d6a902 1826 {
fd91d419 1827 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 1828 {
fd91d419
L
1829 eif->failed = TRUE;
1830 return FALSE;
45d6a902
AM
1831 }
1832 }
1833
1834 return TRUE;
1835}
1836\f
1837/* Look through the symbols which are defined in other shared
1838 libraries and referenced here. Update the list of version
1839 dependencies. This will be put into the .gnu.version_r section.
1840 This function is called via elf_link_hash_traverse. */
1841
28caa186 1842static bfd_boolean
268b6b39
AM
1843_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1844 void *data)
45d6a902 1845{
a50b1753 1846 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
1847 Elf_Internal_Verneed *t;
1848 Elf_Internal_Vernaux *a;
1849 bfd_size_type amt;
1850
45d6a902
AM
1851 /* We only care about symbols defined in shared objects with version
1852 information. */
f5385ebf
AM
1853 if (!h->def_dynamic
1854 || h->def_regular
45d6a902
AM
1855 || h->dynindx == -1
1856 || h->verinfo.verdef == NULL)
1857 return TRUE;
1858
1859 /* See if we already know about this version. */
28caa186
AM
1860 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1861 t != NULL;
1862 t = t->vn_nextref)
45d6a902
AM
1863 {
1864 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1865 continue;
1866
1867 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1868 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1869 return TRUE;
1870
1871 break;
1872 }
1873
1874 /* This is a new version. Add it to tree we are building. */
1875
1876 if (t == NULL)
1877 {
1878 amt = sizeof *t;
a50b1753 1879 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
1880 if (t == NULL)
1881 {
1882 rinfo->failed = TRUE;
1883 return FALSE;
1884 }
1885
1886 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
1887 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
1888 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
1889 }
1890
1891 amt = sizeof *a;
a50b1753 1892 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
1893 if (a == NULL)
1894 {
1895 rinfo->failed = TRUE;
1896 return FALSE;
1897 }
45d6a902
AM
1898
1899 /* Note that we are copying a string pointer here, and testing it
1900 above. If bfd_elf_string_from_elf_section is ever changed to
1901 discard the string data when low in memory, this will have to be
1902 fixed. */
1903 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1904
1905 a->vna_flags = h->verinfo.verdef->vd_flags;
1906 a->vna_nextptr = t->vn_auxptr;
1907
1908 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1909 ++rinfo->vers;
1910
1911 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1912
1913 t->vn_auxptr = a;
1914
1915 return TRUE;
1916}
1917
1918/* Figure out appropriate versions for all the symbols. We may not
1919 have the version number script until we have read all of the input
1920 files, so until that point we don't know which symbols should be
1921 local. This function is called via elf_link_hash_traverse. */
1922
28caa186 1923static bfd_boolean
268b6b39 1924_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 1925{
28caa186 1926 struct elf_info_failed *sinfo;
45d6a902 1927 struct bfd_link_info *info;
9c5bfbb7 1928 const struct elf_backend_data *bed;
45d6a902
AM
1929 struct elf_info_failed eif;
1930 char *p;
1931 bfd_size_type amt;
1932
a50b1753 1933 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
1934 info = sinfo->info;
1935
45d6a902
AM
1936 /* Fix the symbol flags. */
1937 eif.failed = FALSE;
1938 eif.info = info;
1939 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1940 {
1941 if (eif.failed)
1942 sinfo->failed = TRUE;
1943 return FALSE;
1944 }
1945
1946 /* We only need version numbers for symbols defined in regular
1947 objects. */
f5385ebf 1948 if (!h->def_regular)
45d6a902
AM
1949 return TRUE;
1950
28caa186 1951 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
1952 p = strchr (h->root.root.string, ELF_VER_CHR);
1953 if (p != NULL && h->verinfo.vertree == NULL)
1954 {
1955 struct bfd_elf_version_tree *t;
1956 bfd_boolean hidden;
1957
1958 hidden = TRUE;
1959
1960 /* There are two consecutive ELF_VER_CHR characters if this is
1961 not a hidden symbol. */
1962 ++p;
1963 if (*p == ELF_VER_CHR)
1964 {
1965 hidden = FALSE;
1966 ++p;
1967 }
1968
1969 /* If there is no version string, we can just return out. */
1970 if (*p == '\0')
1971 {
1972 if (hidden)
f5385ebf 1973 h->hidden = 1;
45d6a902
AM
1974 return TRUE;
1975 }
1976
1977 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 1978 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
1979 {
1980 if (strcmp (t->name, p) == 0)
1981 {
1982 size_t len;
1983 char *alc;
1984 struct bfd_elf_version_expr *d;
1985
1986 len = p - h->root.root.string;
a50b1753 1987 alc = (char *) bfd_malloc (len);
45d6a902 1988 if (alc == NULL)
14b1c01e
AM
1989 {
1990 sinfo->failed = TRUE;
1991 return FALSE;
1992 }
45d6a902
AM
1993 memcpy (alc, h->root.root.string, len - 1);
1994 alc[len - 1] = '\0';
1995 if (alc[len - 2] == ELF_VER_CHR)
1996 alc[len - 2] = '\0';
1997
1998 h->verinfo.vertree = t;
1999 t->used = TRUE;
2000 d = NULL;
2001
108ba305
JJ
2002 if (t->globals.list != NULL)
2003 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2004
2005 /* See if there is anything to force this symbol to
2006 local scope. */
108ba305 2007 if (d == NULL && t->locals.list != NULL)
45d6a902 2008 {
108ba305
JJ
2009 d = (*t->match) (&t->locals, NULL, alc);
2010 if (d != NULL
2011 && h->dynindx != -1
108ba305
JJ
2012 && ! info->export_dynamic)
2013 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2014 }
2015
2016 free (alc);
2017 break;
2018 }
2019 }
2020
2021 /* If we are building an application, we need to create a
2022 version node for this version. */
36af4a4e 2023 if (t == NULL && info->executable)
45d6a902
AM
2024 {
2025 struct bfd_elf_version_tree **pp;
2026 int version_index;
2027
2028 /* If we aren't going to export this symbol, we don't need
2029 to worry about it. */
2030 if (h->dynindx == -1)
2031 return TRUE;
2032
2033 amt = sizeof *t;
a50b1753 2034 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
45d6a902
AM
2035 if (t == NULL)
2036 {
2037 sinfo->failed = TRUE;
2038 return FALSE;
2039 }
2040
45d6a902 2041 t->name = p;
45d6a902
AM
2042 t->name_indx = (unsigned int) -1;
2043 t->used = TRUE;
2044
2045 version_index = 1;
2046 /* Don't count anonymous version tag. */
fd91d419
L
2047 if (sinfo->info->version_info != NULL
2048 && sinfo->info->version_info->vernum == 0)
45d6a902 2049 version_index = 0;
fd91d419
L
2050 for (pp = &sinfo->info->version_info;
2051 *pp != NULL;
2052 pp = &(*pp)->next)
45d6a902
AM
2053 ++version_index;
2054 t->vernum = version_index;
2055
2056 *pp = t;
2057
2058 h->verinfo.vertree = t;
2059 }
2060 else if (t == NULL)
2061 {
2062 /* We could not find the version for a symbol when
2063 generating a shared archive. Return an error. */
2064 (*_bfd_error_handler)
c55fe096 2065 (_("%B: version node not found for symbol %s"),
28caa186 2066 info->output_bfd, h->root.root.string);
45d6a902
AM
2067 bfd_set_error (bfd_error_bad_value);
2068 sinfo->failed = TRUE;
2069 return FALSE;
2070 }
2071
2072 if (hidden)
f5385ebf 2073 h->hidden = 1;
45d6a902
AM
2074 }
2075
2076 /* If we don't have a version for this symbol, see if we can find
2077 something. */
fd91d419 2078 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2079 {
1e8fa21e 2080 bfd_boolean hide;
ae5a3597 2081
fd91d419
L
2082 h->verinfo.vertree
2083 = bfd_find_version_for_sym (sinfo->info->version_info,
2084 h->root.root.string, &hide);
1e8fa21e
AM
2085 if (h->verinfo.vertree != NULL && hide)
2086 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2087 }
2088
2089 return TRUE;
2090}
2091\f
45d6a902
AM
2092/* Read and swap the relocs from the section indicated by SHDR. This
2093 may be either a REL or a RELA section. The relocations are
2094 translated into RELA relocations and stored in INTERNAL_RELOCS,
2095 which should have already been allocated to contain enough space.
2096 The EXTERNAL_RELOCS are a buffer where the external form of the
2097 relocations should be stored.
2098
2099 Returns FALSE if something goes wrong. */
2100
2101static bfd_boolean
268b6b39 2102elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2103 asection *sec,
268b6b39
AM
2104 Elf_Internal_Shdr *shdr,
2105 void *external_relocs,
2106 Elf_Internal_Rela *internal_relocs)
45d6a902 2107{
9c5bfbb7 2108 const struct elf_backend_data *bed;
268b6b39 2109 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2110 const bfd_byte *erela;
2111 const bfd_byte *erelaend;
2112 Elf_Internal_Rela *irela;
243ef1e0
L
2113 Elf_Internal_Shdr *symtab_hdr;
2114 size_t nsyms;
45d6a902 2115
45d6a902
AM
2116 /* Position ourselves at the start of the section. */
2117 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2118 return FALSE;
2119
2120 /* Read the relocations. */
2121 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2122 return FALSE;
2123
243ef1e0 2124 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2125 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2126
45d6a902
AM
2127 bed = get_elf_backend_data (abfd);
2128
2129 /* Convert the external relocations to the internal format. */
2130 if (shdr->sh_entsize == bed->s->sizeof_rel)
2131 swap_in = bed->s->swap_reloc_in;
2132 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2133 swap_in = bed->s->swap_reloca_in;
2134 else
2135 {
2136 bfd_set_error (bfd_error_wrong_format);
2137 return FALSE;
2138 }
2139
a50b1753 2140 erela = (const bfd_byte *) external_relocs;
51992aec 2141 erelaend = erela + shdr->sh_size;
45d6a902
AM
2142 irela = internal_relocs;
2143 while (erela < erelaend)
2144 {
243ef1e0
L
2145 bfd_vma r_symndx;
2146
45d6a902 2147 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2148 r_symndx = ELF32_R_SYM (irela->r_info);
2149 if (bed->s->arch_size == 64)
2150 r_symndx >>= 24;
ce98a316
NC
2151 if (nsyms > 0)
2152 {
2153 if ((size_t) r_symndx >= nsyms)
2154 {
2155 (*_bfd_error_handler)
2156 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2157 " for offset 0x%lx in section `%A'"),
2158 abfd, sec,
2159 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2160 bfd_set_error (bfd_error_bad_value);
2161 return FALSE;
2162 }
2163 }
cf35638d 2164 else if (r_symndx != STN_UNDEF)
243ef1e0
L
2165 {
2166 (*_bfd_error_handler)
ce98a316
NC
2167 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2168 " when the object file has no symbol table"),
d003868e
AM
2169 abfd, sec,
2170 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2171 bfd_set_error (bfd_error_bad_value);
2172 return FALSE;
2173 }
45d6a902
AM
2174 irela += bed->s->int_rels_per_ext_rel;
2175 erela += shdr->sh_entsize;
2176 }
2177
2178 return TRUE;
2179}
2180
2181/* Read and swap the relocs for a section O. They may have been
2182 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2183 not NULL, they are used as buffers to read into. They are known to
2184 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2185 the return value is allocated using either malloc or bfd_alloc,
2186 according to the KEEP_MEMORY argument. If O has two relocation
2187 sections (both REL and RELA relocations), then the REL_HDR
2188 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2189 RELA_HDR relocations. */
45d6a902
AM
2190
2191Elf_Internal_Rela *
268b6b39
AM
2192_bfd_elf_link_read_relocs (bfd *abfd,
2193 asection *o,
2194 void *external_relocs,
2195 Elf_Internal_Rela *internal_relocs,
2196 bfd_boolean keep_memory)
45d6a902 2197{
268b6b39 2198 void *alloc1 = NULL;
45d6a902 2199 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2200 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2201 struct bfd_elf_section_data *esdo = elf_section_data (o);
2202 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2203
d4730f92
BS
2204 if (esdo->relocs != NULL)
2205 return esdo->relocs;
45d6a902
AM
2206
2207 if (o->reloc_count == 0)
2208 return NULL;
2209
45d6a902
AM
2210 if (internal_relocs == NULL)
2211 {
2212 bfd_size_type size;
2213
2214 size = o->reloc_count;
2215 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2216 if (keep_memory)
a50b1753 2217 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2218 else
a50b1753 2219 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2220 if (internal_relocs == NULL)
2221 goto error_return;
2222 }
2223
2224 if (external_relocs == NULL)
2225 {
d4730f92
BS
2226 bfd_size_type size = 0;
2227
2228 if (esdo->rel.hdr)
2229 size += esdo->rel.hdr->sh_size;
2230 if (esdo->rela.hdr)
2231 size += esdo->rela.hdr->sh_size;
45d6a902 2232
268b6b39 2233 alloc1 = bfd_malloc (size);
45d6a902
AM
2234 if (alloc1 == NULL)
2235 goto error_return;
2236 external_relocs = alloc1;
2237 }
2238
d4730f92
BS
2239 internal_rela_relocs = internal_relocs;
2240 if (esdo->rel.hdr)
2241 {
2242 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2243 external_relocs,
2244 internal_relocs))
2245 goto error_return;
2246 external_relocs = (((bfd_byte *) external_relocs)
2247 + esdo->rel.hdr->sh_size);
2248 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2249 * bed->s->int_rels_per_ext_rel);
2250 }
2251
2252 if (esdo->rela.hdr
2253 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2254 external_relocs,
2255 internal_rela_relocs)))
45d6a902
AM
2256 goto error_return;
2257
2258 /* Cache the results for next time, if we can. */
2259 if (keep_memory)
d4730f92 2260 esdo->relocs = internal_relocs;
45d6a902
AM
2261
2262 if (alloc1 != NULL)
2263 free (alloc1);
2264
2265 /* Don't free alloc2, since if it was allocated we are passing it
2266 back (under the name of internal_relocs). */
2267
2268 return internal_relocs;
2269
2270 error_return:
2271 if (alloc1 != NULL)
2272 free (alloc1);
2273 if (alloc2 != NULL)
4dd07732
AM
2274 {
2275 if (keep_memory)
2276 bfd_release (abfd, alloc2);
2277 else
2278 free (alloc2);
2279 }
45d6a902
AM
2280 return NULL;
2281}
2282
2283/* Compute the size of, and allocate space for, REL_HDR which is the
2284 section header for a section containing relocations for O. */
2285
28caa186 2286static bfd_boolean
268b6b39 2287_bfd_elf_link_size_reloc_section (bfd *abfd,
d4730f92 2288 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2289{
d4730f92 2290 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2291
2292 /* That allows us to calculate the size of the section. */
d4730f92 2293 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2294
2295 /* The contents field must last into write_object_contents, so we
2296 allocate it with bfd_alloc rather than malloc. Also since we
2297 cannot be sure that the contents will actually be filled in,
2298 we zero the allocated space. */
a50b1753 2299 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2300 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2301 return FALSE;
2302
d4730f92 2303 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2304 {
2305 struct elf_link_hash_entry **p;
2306
a50b1753 2307 p = (struct elf_link_hash_entry **)
d4730f92 2308 bfd_zmalloc (reldata->count * sizeof (struct elf_link_hash_entry *));
45d6a902
AM
2309 if (p == NULL)
2310 return FALSE;
2311
d4730f92 2312 reldata->hashes = p;
45d6a902
AM
2313 }
2314
2315 return TRUE;
2316}
2317
2318/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2319 originated from the section given by INPUT_REL_HDR) to the
2320 OUTPUT_BFD. */
2321
2322bfd_boolean
268b6b39
AM
2323_bfd_elf_link_output_relocs (bfd *output_bfd,
2324 asection *input_section,
2325 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2326 Elf_Internal_Rela *internal_relocs,
2327 struct elf_link_hash_entry **rel_hash
2328 ATTRIBUTE_UNUSED)
45d6a902
AM
2329{
2330 Elf_Internal_Rela *irela;
2331 Elf_Internal_Rela *irelaend;
2332 bfd_byte *erel;
d4730f92 2333 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2334 asection *output_section;
9c5bfbb7 2335 const struct elf_backend_data *bed;
268b6b39 2336 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2337 struct bfd_elf_section_data *esdo;
45d6a902
AM
2338
2339 output_section = input_section->output_section;
45d6a902 2340
d4730f92
BS
2341 bed = get_elf_backend_data (output_bfd);
2342 esdo = elf_section_data (output_section);
2343 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2344 {
d4730f92
BS
2345 output_reldata = &esdo->rel;
2346 swap_out = bed->s->swap_reloc_out;
45d6a902 2347 }
d4730f92
BS
2348 else if (esdo->rela.hdr
2349 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2350 {
d4730f92
BS
2351 output_reldata = &esdo->rela;
2352 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2353 }
2354 else
2355 {
2356 (*_bfd_error_handler)
d003868e
AM
2357 (_("%B: relocation size mismatch in %B section %A"),
2358 output_bfd, input_section->owner, input_section);
297d8443 2359 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2360 return FALSE;
2361 }
2362
d4730f92
BS
2363 erel = output_reldata->hdr->contents;
2364 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2365 irela = internal_relocs;
2366 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2367 * bed->s->int_rels_per_ext_rel);
2368 while (irela < irelaend)
2369 {
2370 (*swap_out) (output_bfd, irela, erel);
2371 irela += bed->s->int_rels_per_ext_rel;
2372 erel += input_rel_hdr->sh_entsize;
2373 }
2374
2375 /* Bump the counter, so that we know where to add the next set of
2376 relocations. */
d4730f92 2377 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2378
2379 return TRUE;
2380}
2381\f
508c3946
L
2382/* Make weak undefined symbols in PIE dynamic. */
2383
2384bfd_boolean
2385_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2386 struct elf_link_hash_entry *h)
2387{
2388 if (info->pie
2389 && h->dynindx == -1
2390 && h->root.type == bfd_link_hash_undefweak)
2391 return bfd_elf_link_record_dynamic_symbol (info, h);
2392
2393 return TRUE;
2394}
2395
45d6a902
AM
2396/* Fix up the flags for a symbol. This handles various cases which
2397 can only be fixed after all the input files are seen. This is
2398 currently called by both adjust_dynamic_symbol and
2399 assign_sym_version, which is unnecessary but perhaps more robust in
2400 the face of future changes. */
2401
28caa186 2402static bfd_boolean
268b6b39
AM
2403_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2404 struct elf_info_failed *eif)
45d6a902 2405{
33774f08 2406 const struct elf_backend_data *bed;
508c3946 2407
45d6a902
AM
2408 /* If this symbol was mentioned in a non-ELF file, try to set
2409 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2410 permit a non-ELF file to correctly refer to a symbol defined in
2411 an ELF dynamic object. */
f5385ebf 2412 if (h->non_elf)
45d6a902
AM
2413 {
2414 while (h->root.type == bfd_link_hash_indirect)
2415 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2416
2417 if (h->root.type != bfd_link_hash_defined
2418 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2419 {
2420 h->ref_regular = 1;
2421 h->ref_regular_nonweak = 1;
2422 }
45d6a902
AM
2423 else
2424 {
2425 if (h->root.u.def.section->owner != NULL
2426 && (bfd_get_flavour (h->root.u.def.section->owner)
2427 == bfd_target_elf_flavour))
f5385ebf
AM
2428 {
2429 h->ref_regular = 1;
2430 h->ref_regular_nonweak = 1;
2431 }
45d6a902 2432 else
f5385ebf 2433 h->def_regular = 1;
45d6a902
AM
2434 }
2435
2436 if (h->dynindx == -1
f5385ebf
AM
2437 && (h->def_dynamic
2438 || h->ref_dynamic))
45d6a902 2439 {
c152c796 2440 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2441 {
2442 eif->failed = TRUE;
2443 return FALSE;
2444 }
2445 }
2446 }
2447 else
2448 {
f5385ebf 2449 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2450 was first seen in a non-ELF file. Fortunately, if the symbol
2451 was first seen in an ELF file, we're probably OK unless the
2452 symbol was defined in a non-ELF file. Catch that case here.
2453 FIXME: We're still in trouble if the symbol was first seen in
2454 a dynamic object, and then later in a non-ELF regular object. */
2455 if ((h->root.type == bfd_link_hash_defined
2456 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2457 && !h->def_regular
45d6a902
AM
2458 && (h->root.u.def.section->owner != NULL
2459 ? (bfd_get_flavour (h->root.u.def.section->owner)
2460 != bfd_target_elf_flavour)
2461 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2462 && !h->def_dynamic)))
2463 h->def_regular = 1;
45d6a902
AM
2464 }
2465
508c3946 2466 /* Backend specific symbol fixup. */
33774f08
AM
2467 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2468 if (bed->elf_backend_fixup_symbol
2469 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2470 return FALSE;
508c3946 2471
45d6a902
AM
2472 /* If this is a final link, and the symbol was defined as a common
2473 symbol in a regular object file, and there was no definition in
2474 any dynamic object, then the linker will have allocated space for
f5385ebf 2475 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2476 flag will not have been set. */
2477 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2478 && !h->def_regular
2479 && h->ref_regular
2480 && !h->def_dynamic
96f29d96 2481 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2482 h->def_regular = 1;
45d6a902
AM
2483
2484 /* If -Bsymbolic was used (which means to bind references to global
2485 symbols to the definition within the shared object), and this
2486 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2487 need a PLT entry. Likewise, if the symbol has non-default
2488 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2489 will force it local. */
f5385ebf 2490 if (h->needs_plt
45d6a902 2491 && eif->info->shared
0eddce27 2492 && is_elf_hash_table (eif->info->hash)
55255dae 2493 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2494 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2495 && h->def_regular)
45d6a902 2496 {
45d6a902
AM
2497 bfd_boolean force_local;
2498
45d6a902
AM
2499 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2500 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2501 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2502 }
2503
2504 /* If a weak undefined symbol has non-default visibility, we also
2505 hide it from the dynamic linker. */
9c7a29a3 2506 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2507 && h->root.type == bfd_link_hash_undefweak)
33774f08 2508 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2509
2510 /* If this is a weak defined symbol in a dynamic object, and we know
2511 the real definition in the dynamic object, copy interesting flags
2512 over to the real definition. */
f6e332e6 2513 if (h->u.weakdef != NULL)
45d6a902 2514 {
45d6a902
AM
2515 /* If the real definition is defined by a regular object file,
2516 don't do anything special. See the longer description in
2517 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2518 if (h->u.weakdef->def_regular)
f6e332e6 2519 h->u.weakdef = NULL;
45d6a902 2520 else
a26587ba 2521 {
4e6b54a6
AM
2522 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2523
2524 while (h->root.type == bfd_link_hash_indirect)
2525 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2526
2527 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2528 || h->root.type == bfd_link_hash_defweak);
2529 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2530 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2531 || weakdef->root.type == bfd_link_hash_defweak);
2532 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2533 }
45d6a902
AM
2534 }
2535
2536 return TRUE;
2537}
2538
2539/* Make the backend pick a good value for a dynamic symbol. This is
2540 called via elf_link_hash_traverse, and also calls itself
2541 recursively. */
2542
28caa186 2543static bfd_boolean
268b6b39 2544_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2545{
a50b1753 2546 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2547 bfd *dynobj;
9c5bfbb7 2548 const struct elf_backend_data *bed;
45d6a902 2549
0eddce27 2550 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2551 return FALSE;
2552
45d6a902
AM
2553 /* Ignore indirect symbols. These are added by the versioning code. */
2554 if (h->root.type == bfd_link_hash_indirect)
2555 return TRUE;
2556
2557 /* Fix the symbol flags. */
2558 if (! _bfd_elf_fix_symbol_flags (h, eif))
2559 return FALSE;
2560
2561 /* If this symbol does not require a PLT entry, and it is not
2562 defined by a dynamic object, or is not referenced by a regular
2563 object, ignore it. We do have to handle a weak defined symbol,
2564 even if no regular object refers to it, if we decided to add it
2565 to the dynamic symbol table. FIXME: Do we normally need to worry
2566 about symbols which are defined by one dynamic object and
2567 referenced by another one? */
f5385ebf 2568 if (!h->needs_plt
91e21fb7 2569 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2570 && (h->def_regular
2571 || !h->def_dynamic
2572 || (!h->ref_regular
f6e332e6 2573 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2574 {
a6aa5195 2575 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2576 return TRUE;
2577 }
2578
2579 /* If we've already adjusted this symbol, don't do it again. This
2580 can happen via a recursive call. */
f5385ebf 2581 if (h->dynamic_adjusted)
45d6a902
AM
2582 return TRUE;
2583
2584 /* Don't look at this symbol again. Note that we must set this
2585 after checking the above conditions, because we may look at a
2586 symbol once, decide not to do anything, and then get called
2587 recursively later after REF_REGULAR is set below. */
f5385ebf 2588 h->dynamic_adjusted = 1;
45d6a902
AM
2589
2590 /* If this is a weak definition, and we know a real definition, and
2591 the real symbol is not itself defined by a regular object file,
2592 then get a good value for the real definition. We handle the
2593 real symbol first, for the convenience of the backend routine.
2594
2595 Note that there is a confusing case here. If the real definition
2596 is defined by a regular object file, we don't get the real symbol
2597 from the dynamic object, but we do get the weak symbol. If the
2598 processor backend uses a COPY reloc, then if some routine in the
2599 dynamic object changes the real symbol, we will not see that
2600 change in the corresponding weak symbol. This is the way other
2601 ELF linkers work as well, and seems to be a result of the shared
2602 library model.
2603
2604 I will clarify this issue. Most SVR4 shared libraries define the
2605 variable _timezone and define timezone as a weak synonym. The
2606 tzset call changes _timezone. If you write
2607 extern int timezone;
2608 int _timezone = 5;
2609 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2610 you might expect that, since timezone is a synonym for _timezone,
2611 the same number will print both times. However, if the processor
2612 backend uses a COPY reloc, then actually timezone will be copied
2613 into your process image, and, since you define _timezone
2614 yourself, _timezone will not. Thus timezone and _timezone will
2615 wind up at different memory locations. The tzset call will set
2616 _timezone, leaving timezone unchanged. */
2617
f6e332e6 2618 if (h->u.weakdef != NULL)
45d6a902 2619 {
ec24dc88
AM
2620 /* If we get to this point, there is an implicit reference to
2621 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2622 h->u.weakdef->ref_regular = 1;
45d6a902 2623
ec24dc88
AM
2624 /* Ensure that the backend adjust_dynamic_symbol function sees
2625 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2626 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2627 return FALSE;
2628 }
2629
2630 /* If a symbol has no type and no size and does not require a PLT
2631 entry, then we are probably about to do the wrong thing here: we
2632 are probably going to create a COPY reloc for an empty object.
2633 This case can arise when a shared object is built with assembly
2634 code, and the assembly code fails to set the symbol type. */
2635 if (h->size == 0
2636 && h->type == STT_NOTYPE
f5385ebf 2637 && !h->needs_plt)
45d6a902
AM
2638 (*_bfd_error_handler)
2639 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2640 h->root.root.string);
2641
2642 dynobj = elf_hash_table (eif->info)->dynobj;
2643 bed = get_elf_backend_data (dynobj);
e7c33416 2644
45d6a902
AM
2645 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2646 {
2647 eif->failed = TRUE;
2648 return FALSE;
2649 }
2650
2651 return TRUE;
2652}
2653
027297b7
L
2654/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2655 DYNBSS. */
2656
2657bfd_boolean
2658_bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2659 asection *dynbss)
2660{
91ac5911 2661 unsigned int power_of_two;
027297b7
L
2662 bfd_vma mask;
2663 asection *sec = h->root.u.def.section;
2664
2665 /* The section aligment of definition is the maximum alignment
91ac5911
L
2666 requirement of symbols defined in the section. Since we don't
2667 know the symbol alignment requirement, we start with the
2668 maximum alignment and check low bits of the symbol address
2669 for the minimum alignment. */
2670 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2671 mask = ((bfd_vma) 1 << power_of_two) - 1;
2672 while ((h->root.u.def.value & mask) != 0)
2673 {
2674 mask >>= 1;
2675 --power_of_two;
2676 }
027297b7 2677
91ac5911
L
2678 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2679 dynbss))
027297b7
L
2680 {
2681 /* Adjust the section alignment if needed. */
2682 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2683 power_of_two))
027297b7
L
2684 return FALSE;
2685 }
2686
91ac5911 2687 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2688 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2689
2690 /* Define the symbol as being at this point in DYNBSS. */
2691 h->root.u.def.section = dynbss;
2692 h->root.u.def.value = dynbss->size;
2693
2694 /* Increment the size of DYNBSS to make room for the symbol. */
2695 dynbss->size += h->size;
2696
2697 return TRUE;
2698}
2699
45d6a902
AM
2700/* Adjust all external symbols pointing into SEC_MERGE sections
2701 to reflect the object merging within the sections. */
2702
28caa186 2703static bfd_boolean
268b6b39 2704_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2705{
2706 asection *sec;
2707
45d6a902
AM
2708 if ((h->root.type == bfd_link_hash_defined
2709 || h->root.type == bfd_link_hash_defweak)
2710 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2711 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2712 {
a50b1753 2713 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2714
2715 h->root.u.def.value =
2716 _bfd_merged_section_offset (output_bfd,
2717 &h->root.u.def.section,
2718 elf_section_data (sec)->sec_info,
753731ee 2719 h->root.u.def.value);
45d6a902
AM
2720 }
2721
2722 return TRUE;
2723}
986a241f
RH
2724
2725/* Returns false if the symbol referred to by H should be considered
2726 to resolve local to the current module, and true if it should be
2727 considered to bind dynamically. */
2728
2729bfd_boolean
268b6b39
AM
2730_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2731 struct bfd_link_info *info,
89a2ee5a 2732 bfd_boolean not_local_protected)
986a241f
RH
2733{
2734 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2735 const struct elf_backend_data *bed;
2736 struct elf_link_hash_table *hash_table;
986a241f
RH
2737
2738 if (h == NULL)
2739 return FALSE;
2740
2741 while (h->root.type == bfd_link_hash_indirect
2742 || h->root.type == bfd_link_hash_warning)
2743 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2744
2745 /* If it was forced local, then clearly it's not dynamic. */
2746 if (h->dynindx == -1)
2747 return FALSE;
f5385ebf 2748 if (h->forced_local)
986a241f
RH
2749 return FALSE;
2750
2751 /* Identify the cases where name binding rules say that a
2752 visible symbol resolves locally. */
55255dae 2753 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
986a241f
RH
2754
2755 switch (ELF_ST_VISIBILITY (h->other))
2756 {
2757 case STV_INTERNAL:
2758 case STV_HIDDEN:
2759 return FALSE;
2760
2761 case STV_PROTECTED:
fcb93ecf
PB
2762 hash_table = elf_hash_table (info);
2763 if (!is_elf_hash_table (hash_table))
2764 return FALSE;
2765
2766 bed = get_elf_backend_data (hash_table->dynobj);
2767
986a241f
RH
2768 /* Proper resolution for function pointer equality may require
2769 that these symbols perhaps be resolved dynamically, even though
2770 we should be resolving them to the current module. */
89a2ee5a 2771 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2772 binding_stays_local_p = TRUE;
2773 break;
2774
2775 default:
986a241f
RH
2776 break;
2777 }
2778
aa37626c 2779 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2780 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2781 return TRUE;
2782
986a241f
RH
2783 /* Otherwise, the symbol is dynamic if binding rules don't tell
2784 us that it remains local. */
2785 return !binding_stays_local_p;
2786}
f6c52c13
AM
2787
2788/* Return true if the symbol referred to by H should be considered
2789 to resolve local to the current module, and false otherwise. Differs
2790 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2791 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2792 for the place where forced_local and dynindx == -1 are tested. If
2793 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2794 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2795 the symbol is local only for defined symbols.
2796 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2797 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2798 treatment of undefined weak symbols. For those that do not make
2799 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2800
2801bfd_boolean
268b6b39
AM
2802_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2803 struct bfd_link_info *info,
2804 bfd_boolean local_protected)
f6c52c13 2805{
fcb93ecf
PB
2806 const struct elf_backend_data *bed;
2807 struct elf_link_hash_table *hash_table;
2808
f6c52c13
AM
2809 /* If it's a local sym, of course we resolve locally. */
2810 if (h == NULL)
2811 return TRUE;
2812
d95edcac
L
2813 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2814 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2815 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2816 return TRUE;
2817
7e2294f9
AO
2818 /* Common symbols that become definitions don't get the DEF_REGULAR
2819 flag set, so test it first, and don't bail out. */
2820 if (ELF_COMMON_DEF_P (h))
2821 /* Do nothing. */;
f6c52c13 2822 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2823 resolve locally. The sym is either undefined or dynamic. */
2824 else if (!h->def_regular)
f6c52c13
AM
2825 return FALSE;
2826
2827 /* Forced local symbols resolve locally. */
f5385ebf 2828 if (h->forced_local)
f6c52c13
AM
2829 return TRUE;
2830
2831 /* As do non-dynamic symbols. */
2832 if (h->dynindx == -1)
2833 return TRUE;
2834
2835 /* At this point, we know the symbol is defined and dynamic. In an
2836 executable it must resolve locally, likewise when building symbolic
2837 shared libraries. */
55255dae 2838 if (info->executable || SYMBOLIC_BIND (info, h))
f6c52c13
AM
2839 return TRUE;
2840
2841 /* Now deal with defined dynamic symbols in shared libraries. Ones
2842 with default visibility might not resolve locally. */
2843 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2844 return FALSE;
2845
fcb93ecf
PB
2846 hash_table = elf_hash_table (info);
2847 if (!is_elf_hash_table (hash_table))
2848 return TRUE;
2849
2850 bed = get_elf_backend_data (hash_table->dynobj);
2851
1c16dfa5 2852 /* STV_PROTECTED non-function symbols are local. */
fcb93ecf 2853 if (!bed->is_function_type (h->type))
1c16dfa5
L
2854 return TRUE;
2855
f6c52c13 2856 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
2857 symbols be treated as dynamic symbols. If the address of a
2858 function not defined in an executable is set to that function's
2859 plt entry in the executable, then the address of the function in
2860 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
2861 return local_protected;
2862}
e1918d23
AM
2863
2864/* Caches some TLS segment info, and ensures that the TLS segment vma is
2865 aligned. Returns the first TLS output section. */
2866
2867struct bfd_section *
2868_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2869{
2870 struct bfd_section *sec, *tls;
2871 unsigned int align = 0;
2872
2873 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2874 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2875 break;
2876 tls = sec;
2877
2878 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2879 if (sec->alignment_power > align)
2880 align = sec->alignment_power;
2881
2882 elf_hash_table (info)->tls_sec = tls;
2883
2884 /* Ensure the alignment of the first section is the largest alignment,
2885 so that the tls segment starts aligned. */
2886 if (tls != NULL)
2887 tls->alignment_power = align;
2888
2889 return tls;
2890}
0ad989f9
L
2891
2892/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2893static bfd_boolean
2894is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2895 Elf_Internal_Sym *sym)
2896{
a4d8e49b
L
2897 const struct elf_backend_data *bed;
2898
0ad989f9
L
2899 /* Local symbols do not count, but target specific ones might. */
2900 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2901 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2902 return FALSE;
2903
fcb93ecf 2904 bed = get_elf_backend_data (abfd);
0ad989f9 2905 /* Function symbols do not count. */
fcb93ecf 2906 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
2907 return FALSE;
2908
2909 /* If the section is undefined, then so is the symbol. */
2910 if (sym->st_shndx == SHN_UNDEF)
2911 return FALSE;
2912
2913 /* If the symbol is defined in the common section, then
2914 it is a common definition and so does not count. */
a4d8e49b 2915 if (bed->common_definition (sym))
0ad989f9
L
2916 return FALSE;
2917
2918 /* If the symbol is in a target specific section then we
2919 must rely upon the backend to tell us what it is. */
2920 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2921 /* FIXME - this function is not coded yet:
2922
2923 return _bfd_is_global_symbol_definition (abfd, sym);
2924
2925 Instead for now assume that the definition is not global,
2926 Even if this is wrong, at least the linker will behave
2927 in the same way that it used to do. */
2928 return FALSE;
2929
2930 return TRUE;
2931}
2932
2933/* Search the symbol table of the archive element of the archive ABFD
2934 whose archive map contains a mention of SYMDEF, and determine if
2935 the symbol is defined in this element. */
2936static bfd_boolean
2937elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2938{
2939 Elf_Internal_Shdr * hdr;
2940 bfd_size_type symcount;
2941 bfd_size_type extsymcount;
2942 bfd_size_type extsymoff;
2943 Elf_Internal_Sym *isymbuf;
2944 Elf_Internal_Sym *isym;
2945 Elf_Internal_Sym *isymend;
2946 bfd_boolean result;
2947
2948 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2949 if (abfd == NULL)
2950 return FALSE;
2951
2952 if (! bfd_check_format (abfd, bfd_object))
2953 return FALSE;
2954
2955 /* If we have already included the element containing this symbol in the
2956 link then we do not need to include it again. Just claim that any symbol
2957 it contains is not a definition, so that our caller will not decide to
2958 (re)include this element. */
2959 if (abfd->archive_pass)
2960 return FALSE;
2961
2962 /* Select the appropriate symbol table. */
2963 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2964 hdr = &elf_tdata (abfd)->symtab_hdr;
2965 else
2966 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2967
2968 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2969
2970 /* The sh_info field of the symtab header tells us where the
2971 external symbols start. We don't care about the local symbols. */
2972 if (elf_bad_symtab (abfd))
2973 {
2974 extsymcount = symcount;
2975 extsymoff = 0;
2976 }
2977 else
2978 {
2979 extsymcount = symcount - hdr->sh_info;
2980 extsymoff = hdr->sh_info;
2981 }
2982
2983 if (extsymcount == 0)
2984 return FALSE;
2985
2986 /* Read in the symbol table. */
2987 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2988 NULL, NULL, NULL);
2989 if (isymbuf == NULL)
2990 return FALSE;
2991
2992 /* Scan the symbol table looking for SYMDEF. */
2993 result = FALSE;
2994 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2995 {
2996 const char *name;
2997
2998 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2999 isym->st_name);
3000 if (name == NULL)
3001 break;
3002
3003 if (strcmp (name, symdef->name) == 0)
3004 {
3005 result = is_global_data_symbol_definition (abfd, isym);
3006 break;
3007 }
3008 }
3009
3010 free (isymbuf);
3011
3012 return result;
3013}
3014\f
5a580b3a
AM
3015/* Add an entry to the .dynamic table. */
3016
3017bfd_boolean
3018_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3019 bfd_vma tag,
3020 bfd_vma val)
3021{
3022 struct elf_link_hash_table *hash_table;
3023 const struct elf_backend_data *bed;
3024 asection *s;
3025 bfd_size_type newsize;
3026 bfd_byte *newcontents;
3027 Elf_Internal_Dyn dyn;
3028
3029 hash_table = elf_hash_table (info);
3030 if (! is_elf_hash_table (hash_table))
3031 return FALSE;
3032
3033 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3034 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3035 BFD_ASSERT (s != NULL);
3036
eea6121a 3037 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3038 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3039 if (newcontents == NULL)
3040 return FALSE;
3041
3042 dyn.d_tag = tag;
3043 dyn.d_un.d_val = val;
eea6121a 3044 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3045
eea6121a 3046 s->size = newsize;
5a580b3a
AM
3047 s->contents = newcontents;
3048
3049 return TRUE;
3050}
3051
3052/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3053 otherwise just check whether one already exists. Returns -1 on error,
3054 1 if a DT_NEEDED tag already exists, and 0 on success. */
3055
4ad4eba5 3056static int
7e9f0867
AM
3057elf_add_dt_needed_tag (bfd *abfd,
3058 struct bfd_link_info *info,
4ad4eba5
AM
3059 const char *soname,
3060 bfd_boolean do_it)
5a580b3a
AM
3061{
3062 struct elf_link_hash_table *hash_table;
5a580b3a
AM
3063 bfd_size_type strindex;
3064
7e9f0867
AM
3065 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3066 return -1;
3067
5a580b3a 3068 hash_table = elf_hash_table (info);
5a580b3a
AM
3069 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3070 if (strindex == (bfd_size_type) -1)
3071 return -1;
3072
02be4619 3073 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3074 {
3075 asection *sdyn;
3076 const struct elf_backend_data *bed;
3077 bfd_byte *extdyn;
3078
3079 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3080 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3081 if (sdyn != NULL)
3082 for (extdyn = sdyn->contents;
3083 extdyn < sdyn->contents + sdyn->size;
3084 extdyn += bed->s->sizeof_dyn)
3085 {
3086 Elf_Internal_Dyn dyn;
5a580b3a 3087
7e9f0867
AM
3088 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3089 if (dyn.d_tag == DT_NEEDED
3090 && dyn.d_un.d_val == strindex)
3091 {
3092 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3093 return 1;
3094 }
3095 }
5a580b3a
AM
3096 }
3097
3098 if (do_it)
3099 {
7e9f0867
AM
3100 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3101 return -1;
3102
5a580b3a
AM
3103 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3104 return -1;
3105 }
3106 else
3107 /* We were just checking for existence of the tag. */
3108 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3109
3110 return 0;
3111}
3112
010e5ae2
AM
3113static bfd_boolean
3114on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3115{
3116 for (; needed != NULL; needed = needed->next)
3117 if (strcmp (soname, needed->name) == 0)
3118 return TRUE;
3119
3120 return FALSE;
3121}
3122
14160578 3123/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3124static int
3125elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3126{
3127 const struct elf_link_hash_entry *h1;
3128 const struct elf_link_hash_entry *h2;
10b7e05b 3129 bfd_signed_vma vdiff;
5a580b3a
AM
3130
3131 h1 = *(const struct elf_link_hash_entry **) arg1;
3132 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3133 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3134 if (vdiff != 0)
3135 return vdiff > 0 ? 1 : -1;
3136 else
3137 {
3138 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3139 if (sdiff != 0)
3140 return sdiff > 0 ? 1 : -1;
3141 }
14160578
AM
3142 vdiff = h1->size - h2->size;
3143 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3144}
4ad4eba5 3145
5a580b3a
AM
3146/* This function is used to adjust offsets into .dynstr for
3147 dynamic symbols. This is called via elf_link_hash_traverse. */
3148
3149static bfd_boolean
3150elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3151{
a50b1753 3152 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3153
5a580b3a
AM
3154 if (h->dynindx != -1)
3155 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3156 return TRUE;
3157}
3158
3159/* Assign string offsets in .dynstr, update all structures referencing
3160 them. */
3161
4ad4eba5
AM
3162static bfd_boolean
3163elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3164{
3165 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3166 struct elf_link_local_dynamic_entry *entry;
3167 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3168 bfd *dynobj = hash_table->dynobj;
3169 asection *sdyn;
3170 bfd_size_type size;
3171 const struct elf_backend_data *bed;
3172 bfd_byte *extdyn;
3173
3174 _bfd_elf_strtab_finalize (dynstr);
3175 size = _bfd_elf_strtab_size (dynstr);
3176
3177 bed = get_elf_backend_data (dynobj);
3d4d4302 3178 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3179 BFD_ASSERT (sdyn != NULL);
3180
3181 /* Update all .dynamic entries referencing .dynstr strings. */
3182 for (extdyn = sdyn->contents;
eea6121a 3183 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3184 extdyn += bed->s->sizeof_dyn)
3185 {
3186 Elf_Internal_Dyn dyn;
3187
3188 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3189 switch (dyn.d_tag)
3190 {
3191 case DT_STRSZ:
3192 dyn.d_un.d_val = size;
3193 break;
3194 case DT_NEEDED:
3195 case DT_SONAME:
3196 case DT_RPATH:
3197 case DT_RUNPATH:
3198 case DT_FILTER:
3199 case DT_AUXILIARY:
7ee314fa
AM
3200 case DT_AUDIT:
3201 case DT_DEPAUDIT:
5a580b3a
AM
3202 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3203 break;
3204 default:
3205 continue;
3206 }
3207 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3208 }
3209
3210 /* Now update local dynamic symbols. */
3211 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3212 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3213 entry->isym.st_name);
3214
3215 /* And the rest of dynamic symbols. */
3216 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3217
3218 /* Adjust version definitions. */
3219 if (elf_tdata (output_bfd)->cverdefs)
3220 {
3221 asection *s;
3222 bfd_byte *p;
3223 bfd_size_type i;
3224 Elf_Internal_Verdef def;
3225 Elf_Internal_Verdaux defaux;
3226
3d4d4302 3227 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3228 p = s->contents;
3229 do
3230 {
3231 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3232 &def);
3233 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3234 if (def.vd_aux != sizeof (Elf_External_Verdef))
3235 continue;
5a580b3a
AM
3236 for (i = 0; i < def.vd_cnt; ++i)
3237 {
3238 _bfd_elf_swap_verdaux_in (output_bfd,
3239 (Elf_External_Verdaux *) p, &defaux);
3240 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3241 defaux.vda_name);
3242 _bfd_elf_swap_verdaux_out (output_bfd,
3243 &defaux, (Elf_External_Verdaux *) p);
3244 p += sizeof (Elf_External_Verdaux);
3245 }
3246 }
3247 while (def.vd_next);
3248 }
3249
3250 /* Adjust version references. */
3251 if (elf_tdata (output_bfd)->verref)
3252 {
3253 asection *s;
3254 bfd_byte *p;
3255 bfd_size_type i;
3256 Elf_Internal_Verneed need;
3257 Elf_Internal_Vernaux needaux;
3258
3d4d4302 3259 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3260 p = s->contents;
3261 do
3262 {
3263 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3264 &need);
3265 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3266 _bfd_elf_swap_verneed_out (output_bfd, &need,
3267 (Elf_External_Verneed *) p);
3268 p += sizeof (Elf_External_Verneed);
3269 for (i = 0; i < need.vn_cnt; ++i)
3270 {
3271 _bfd_elf_swap_vernaux_in (output_bfd,
3272 (Elf_External_Vernaux *) p, &needaux);
3273 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3274 needaux.vna_name);
3275 _bfd_elf_swap_vernaux_out (output_bfd,
3276 &needaux,
3277 (Elf_External_Vernaux *) p);
3278 p += sizeof (Elf_External_Vernaux);
3279 }
3280 }
3281 while (need.vn_next);
3282 }
3283
3284 return TRUE;
3285}
3286\f
13285a1b
AM
3287/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3288 The default is to only match when the INPUT and OUTPUT are exactly
3289 the same target. */
3290
3291bfd_boolean
3292_bfd_elf_default_relocs_compatible (const bfd_target *input,
3293 const bfd_target *output)
3294{
3295 return input == output;
3296}
3297
3298/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3299 This version is used when different targets for the same architecture
3300 are virtually identical. */
3301
3302bfd_boolean
3303_bfd_elf_relocs_compatible (const bfd_target *input,
3304 const bfd_target *output)
3305{
3306 const struct elf_backend_data *obed, *ibed;
3307
3308 if (input == output)
3309 return TRUE;
3310
3311 ibed = xvec_get_elf_backend_data (input);
3312 obed = xvec_get_elf_backend_data (output);
3313
3314 if (ibed->arch != obed->arch)
3315 return FALSE;
3316
3317 /* If both backends are using this function, deem them compatible. */
3318 return ibed->relocs_compatible == obed->relocs_compatible;
3319}
3320
e5034e59
AM
3321/* Make a special call to the linker "notice" function to tell it that
3322 we are about to handle an as-needed lib, or have finished
3323 processing the lib. */
3324
3325bfd_boolean
3326_bfd_elf_notice_as_needed (bfd *ibfd,
3327 struct bfd_link_info *info,
3328 enum notice_asneeded_action act)
3329{
3330 return (*info->callbacks->notice) (info, NULL, ibfd, NULL, act, 0, NULL);
3331}
3332
4ad4eba5
AM
3333/* Add symbols from an ELF object file to the linker hash table. */
3334
3335static bfd_boolean
3336elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3337{
a0c402a5 3338 Elf_Internal_Ehdr *ehdr;
4ad4eba5
AM
3339 Elf_Internal_Shdr *hdr;
3340 bfd_size_type symcount;
3341 bfd_size_type extsymcount;
3342 bfd_size_type extsymoff;
3343 struct elf_link_hash_entry **sym_hash;
3344 bfd_boolean dynamic;
3345 Elf_External_Versym *extversym = NULL;
3346 Elf_External_Versym *ever;
3347 struct elf_link_hash_entry *weaks;
3348 struct elf_link_hash_entry **nondeflt_vers = NULL;
3349 bfd_size_type nondeflt_vers_cnt = 0;
3350 Elf_Internal_Sym *isymbuf = NULL;
3351 Elf_Internal_Sym *isym;
3352 Elf_Internal_Sym *isymend;
3353 const struct elf_backend_data *bed;
3354 bfd_boolean add_needed;
66eb6687 3355 struct elf_link_hash_table *htab;
4ad4eba5 3356 bfd_size_type amt;
66eb6687 3357 void *alloc_mark = NULL;
4f87808c
AM
3358 struct bfd_hash_entry **old_table = NULL;
3359 unsigned int old_size = 0;
3360 unsigned int old_count = 0;
66eb6687 3361 void *old_tab = NULL;
66eb6687
AM
3362 void *old_ent;
3363 struct bfd_link_hash_entry *old_undefs = NULL;
3364 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3365 long old_dynsymcount = 0;
a4542f1b 3366 bfd_size_type old_dynstr_size = 0;
66eb6687 3367 size_t tabsize = 0;
db6a5d5f 3368 asection *s;
4ad4eba5 3369
66eb6687 3370 htab = elf_hash_table (info);
4ad4eba5 3371 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3372
3373 if ((abfd->flags & DYNAMIC) == 0)
3374 dynamic = FALSE;
3375 else
3376 {
3377 dynamic = TRUE;
3378
3379 /* You can't use -r against a dynamic object. Also, there's no
3380 hope of using a dynamic object which does not exactly match
3381 the format of the output file. */
3382 if (info->relocatable
66eb6687 3383 || !is_elf_hash_table (htab)
f13a99db 3384 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3385 {
9a0789ec
NC
3386 if (info->relocatable)
3387 bfd_set_error (bfd_error_invalid_operation);
3388 else
3389 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3390 goto error_return;
3391 }
3392 }
3393
a0c402a5
L
3394 ehdr = elf_elfheader (abfd);
3395 if (info->warn_alternate_em
3396 && bed->elf_machine_code != ehdr->e_machine
3397 && ((bed->elf_machine_alt1 != 0
3398 && ehdr->e_machine == bed->elf_machine_alt1)
3399 || (bed->elf_machine_alt2 != 0
3400 && ehdr->e_machine == bed->elf_machine_alt2)))
3401 info->callbacks->einfo
3402 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3403 ehdr->e_machine, abfd, bed->elf_machine_code);
3404
4ad4eba5
AM
3405 /* As a GNU extension, any input sections which are named
3406 .gnu.warning.SYMBOL are treated as warning symbols for the given
3407 symbol. This differs from .gnu.warning sections, which generate
3408 warnings when they are included in an output file. */
dd98f8d2 3409 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3410 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3411 {
db6a5d5f 3412 const char *name;
4ad4eba5 3413
db6a5d5f
AM
3414 name = bfd_get_section_name (abfd, s);
3415 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3416 {
db6a5d5f
AM
3417 char *msg;
3418 bfd_size_type sz;
3419
3420 name += sizeof ".gnu.warning." - 1;
3421
3422 /* If this is a shared object, then look up the symbol
3423 in the hash table. If it is there, and it is already
3424 been defined, then we will not be using the entry
3425 from this shared object, so we don't need to warn.
3426 FIXME: If we see the definition in a regular object
3427 later on, we will warn, but we shouldn't. The only
3428 fix is to keep track of what warnings we are supposed
3429 to emit, and then handle them all at the end of the
3430 link. */
3431 if (dynamic)
4ad4eba5 3432 {
db6a5d5f
AM
3433 struct elf_link_hash_entry *h;
3434
3435 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3436
3437 /* FIXME: What about bfd_link_hash_common? */
3438 if (h != NULL
3439 && (h->root.type == bfd_link_hash_defined
3440 || h->root.type == bfd_link_hash_defweak))
3441 continue;
3442 }
4ad4eba5 3443
db6a5d5f
AM
3444 sz = s->size;
3445 msg = (char *) bfd_alloc (abfd, sz + 1);
3446 if (msg == NULL)
3447 goto error_return;
4ad4eba5 3448
db6a5d5f
AM
3449 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3450 goto error_return;
4ad4eba5 3451
db6a5d5f 3452 msg[sz] = '\0';
4ad4eba5 3453
db6a5d5f
AM
3454 if (! (_bfd_generic_link_add_one_symbol
3455 (info, abfd, name, BSF_WARNING, s, 0, msg,
3456 FALSE, bed->collect, NULL)))
3457 goto error_return;
4ad4eba5 3458
db6a5d5f
AM
3459 if (!info->relocatable && info->executable)
3460 {
3461 /* Clobber the section size so that the warning does
3462 not get copied into the output file. */
3463 s->size = 0;
11d2f718 3464
db6a5d5f
AM
3465 /* Also set SEC_EXCLUDE, so that symbols defined in
3466 the warning section don't get copied to the output. */
3467 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3468 }
3469 }
3470 }
3471
3472 add_needed = TRUE;
3473 if (! dynamic)
3474 {
3475 /* If we are creating a shared library, create all the dynamic
3476 sections immediately. We need to attach them to something,
3477 so we attach them to this BFD, provided it is the right
3478 format. FIXME: If there are no input BFD's of the same
3479 format as the output, we can't make a shared library. */
3480 if (info->shared
66eb6687 3481 && is_elf_hash_table (htab)
f13a99db 3482 && info->output_bfd->xvec == abfd->xvec
66eb6687 3483 && !htab->dynamic_sections_created)
4ad4eba5
AM
3484 {
3485 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3486 goto error_return;
3487 }
3488 }
66eb6687 3489 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3490 goto error_return;
3491 else
3492 {
4ad4eba5 3493 const char *soname = NULL;
7ee314fa 3494 char *audit = NULL;
4ad4eba5
AM
3495 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3496 int ret;
3497
3498 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3499 ld shouldn't allow it. */
4ad4eba5 3500 if ((s = abfd->sections) != NULL
dbaa2011 3501 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
92fd189d 3502 abort ();
4ad4eba5
AM
3503
3504 /* If this dynamic lib was specified on the command line with
3505 --as-needed in effect, then we don't want to add a DT_NEEDED
3506 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3507 in by another lib's DT_NEEDED. When --no-add-needed is used
3508 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3509 any dynamic library in DT_NEEDED tags in the dynamic lib at
3510 all. */
3511 add_needed = (elf_dyn_lib_class (abfd)
3512 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3513 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3514
3515 s = bfd_get_section_by_name (abfd, ".dynamic");
3516 if (s != NULL)
3517 {
3518 bfd_byte *dynbuf;
3519 bfd_byte *extdyn;
cb33740c 3520 unsigned int elfsec;
4ad4eba5
AM
3521 unsigned long shlink;
3522
eea6121a 3523 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3524 {
3525error_free_dyn:
3526 free (dynbuf);
3527 goto error_return;
3528 }
4ad4eba5
AM
3529
3530 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3531 if (elfsec == SHN_BAD)
4ad4eba5
AM
3532 goto error_free_dyn;
3533 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3534
3535 for (extdyn = dynbuf;
eea6121a 3536 extdyn < dynbuf + s->size;
4ad4eba5
AM
3537 extdyn += bed->s->sizeof_dyn)
3538 {
3539 Elf_Internal_Dyn dyn;
3540
3541 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3542 if (dyn.d_tag == DT_SONAME)
3543 {
3544 unsigned int tagv = dyn.d_un.d_val;
3545 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3546 if (soname == NULL)
3547 goto error_free_dyn;
3548 }
3549 if (dyn.d_tag == DT_NEEDED)
3550 {
3551 struct bfd_link_needed_list *n, **pn;
3552 char *fnm, *anm;
3553 unsigned int tagv = dyn.d_un.d_val;
3554
3555 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3556 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3557 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3558 if (n == NULL || fnm == NULL)
3559 goto error_free_dyn;
3560 amt = strlen (fnm) + 1;
a50b1753 3561 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3562 if (anm == NULL)
3563 goto error_free_dyn;
3564 memcpy (anm, fnm, amt);
3565 n->name = anm;
3566 n->by = abfd;
3567 n->next = NULL;
66eb6687 3568 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3569 ;
3570 *pn = n;
3571 }
3572 if (dyn.d_tag == DT_RUNPATH)
3573 {
3574 struct bfd_link_needed_list *n, **pn;
3575 char *fnm, *anm;
3576 unsigned int tagv = dyn.d_un.d_val;
3577
3578 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3579 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3580 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3581 if (n == NULL || fnm == NULL)
3582 goto error_free_dyn;
3583 amt = strlen (fnm) + 1;
a50b1753 3584 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3585 if (anm == NULL)
3586 goto error_free_dyn;
3587 memcpy (anm, fnm, amt);
3588 n->name = anm;
3589 n->by = abfd;
3590 n->next = NULL;
3591 for (pn = & runpath;
3592 *pn != NULL;
3593 pn = &(*pn)->next)
3594 ;
3595 *pn = n;
3596 }
3597 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3598 if (!runpath && dyn.d_tag == DT_RPATH)
3599 {
3600 struct bfd_link_needed_list *n, **pn;
3601 char *fnm, *anm;
3602 unsigned int tagv = dyn.d_un.d_val;
3603
3604 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3605 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3606 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3607 if (n == NULL || fnm == NULL)
3608 goto error_free_dyn;
3609 amt = strlen (fnm) + 1;
a50b1753 3610 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3611 if (anm == NULL)
f8703194 3612 goto error_free_dyn;
4ad4eba5
AM
3613 memcpy (anm, fnm, amt);
3614 n->name = anm;
3615 n->by = abfd;
3616 n->next = NULL;
3617 for (pn = & rpath;
3618 *pn != NULL;
3619 pn = &(*pn)->next)
3620 ;
3621 *pn = n;
3622 }
7ee314fa
AM
3623 if (dyn.d_tag == DT_AUDIT)
3624 {
3625 unsigned int tagv = dyn.d_un.d_val;
3626 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3627 }
4ad4eba5
AM
3628 }
3629
3630 free (dynbuf);
3631 }
3632
3633 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3634 frees all more recently bfd_alloc'd blocks as well. */
3635 if (runpath)
3636 rpath = runpath;
3637
3638 if (rpath)
3639 {
3640 struct bfd_link_needed_list **pn;
66eb6687 3641 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3642 ;
3643 *pn = rpath;
3644 }
3645
3646 /* We do not want to include any of the sections in a dynamic
3647 object in the output file. We hack by simply clobbering the
3648 list of sections in the BFD. This could be handled more
3649 cleanly by, say, a new section flag; the existing
3650 SEC_NEVER_LOAD flag is not the one we want, because that one
3651 still implies that the section takes up space in the output
3652 file. */
3653 bfd_section_list_clear (abfd);
3654
4ad4eba5
AM
3655 /* Find the name to use in a DT_NEEDED entry that refers to this
3656 object. If the object has a DT_SONAME entry, we use it.
3657 Otherwise, if the generic linker stuck something in
3658 elf_dt_name, we use that. Otherwise, we just use the file
3659 name. */
3660 if (soname == NULL || *soname == '\0')
3661 {
3662 soname = elf_dt_name (abfd);
3663 if (soname == NULL || *soname == '\0')
3664 soname = bfd_get_filename (abfd);
3665 }
3666
3667 /* Save the SONAME because sometimes the linker emulation code
3668 will need to know it. */
3669 elf_dt_name (abfd) = soname;
3670
7e9f0867 3671 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3672 if (ret < 0)
3673 goto error_return;
3674
3675 /* If we have already included this dynamic object in the
3676 link, just ignore it. There is no reason to include a
3677 particular dynamic object more than once. */
3678 if (ret > 0)
3679 return TRUE;
7ee314fa
AM
3680
3681 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 3682 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3683 }
3684
3685 /* If this is a dynamic object, we always link against the .dynsym
3686 symbol table, not the .symtab symbol table. The dynamic linker
3687 will only see the .dynsym symbol table, so there is no reason to
3688 look at .symtab for a dynamic object. */
3689
3690 if (! dynamic || elf_dynsymtab (abfd) == 0)
3691 hdr = &elf_tdata (abfd)->symtab_hdr;
3692 else
3693 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3694
3695 symcount = hdr->sh_size / bed->s->sizeof_sym;
3696
3697 /* The sh_info field of the symtab header tells us where the
3698 external symbols start. We don't care about the local symbols at
3699 this point. */
3700 if (elf_bad_symtab (abfd))
3701 {
3702 extsymcount = symcount;
3703 extsymoff = 0;
3704 }
3705 else
3706 {
3707 extsymcount = symcount - hdr->sh_info;
3708 extsymoff = hdr->sh_info;
3709 }
3710
f45794cb 3711 sym_hash = elf_sym_hashes (abfd);
012b2306 3712 if (extsymcount != 0)
4ad4eba5
AM
3713 {
3714 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3715 NULL, NULL, NULL);
3716 if (isymbuf == NULL)
3717 goto error_return;
3718
4ad4eba5 3719 if (sym_hash == NULL)
012b2306
AM
3720 {
3721 /* We store a pointer to the hash table entry for each
3722 external symbol. */
3723 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3724 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3725 if (sym_hash == NULL)
3726 goto error_free_sym;
3727 elf_sym_hashes (abfd) = sym_hash;
3728 }
4ad4eba5
AM
3729 }
3730
3731 if (dynamic)
3732 {
3733 /* Read in any version definitions. */
fc0e6df6
PB
3734 if (!_bfd_elf_slurp_version_tables (abfd,
3735 info->default_imported_symver))
4ad4eba5
AM
3736 goto error_free_sym;
3737
3738 /* Read in the symbol versions, but don't bother to convert them
3739 to internal format. */
3740 if (elf_dynversym (abfd) != 0)
3741 {
3742 Elf_Internal_Shdr *versymhdr;
3743
3744 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 3745 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
3746 if (extversym == NULL)
3747 goto error_free_sym;
3748 amt = versymhdr->sh_size;
3749 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3750 || bfd_bread (extversym, amt, abfd) != amt)
3751 goto error_free_vers;
3752 }
3753 }
3754
66eb6687
AM
3755 /* If we are loading an as-needed shared lib, save the symbol table
3756 state before we start adding symbols. If the lib turns out
3757 to be unneeded, restore the state. */
3758 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3759 {
3760 unsigned int i;
3761 size_t entsize;
3762
3763 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3764 {
3765 struct bfd_hash_entry *p;
2de92251 3766 struct elf_link_hash_entry *h;
66eb6687
AM
3767
3768 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
3769 {
3770 h = (struct elf_link_hash_entry *) p;
3771 entsize += htab->root.table.entsize;
3772 if (h->root.type == bfd_link_hash_warning)
3773 entsize += htab->root.table.entsize;
3774 }
66eb6687
AM
3775 }
3776
3777 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 3778 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
3779 if (old_tab == NULL)
3780 goto error_free_vers;
3781
3782 /* Remember the current objalloc pointer, so that all mem for
3783 symbols added can later be reclaimed. */
3784 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3785 if (alloc_mark == NULL)
3786 goto error_free_vers;
3787
5061a885
AM
3788 /* Make a special call to the linker "notice" function to
3789 tell it that we are about to handle an as-needed lib. */
e5034e59 3790 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 3791 goto error_free_vers;
5061a885 3792
f45794cb
AM
3793 /* Clone the symbol table. Remember some pointers into the
3794 symbol table, and dynamic symbol count. */
3795 old_ent = (char *) old_tab + tabsize;
66eb6687 3796 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
3797 old_undefs = htab->root.undefs;
3798 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
3799 old_table = htab->root.table.table;
3800 old_size = htab->root.table.size;
3801 old_count = htab->root.table.count;
66eb6687 3802 old_dynsymcount = htab->dynsymcount;
a4542f1b 3803 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
66eb6687
AM
3804
3805 for (i = 0; i < htab->root.table.size; i++)
3806 {
3807 struct bfd_hash_entry *p;
2de92251 3808 struct elf_link_hash_entry *h;
66eb6687
AM
3809
3810 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3811 {
3812 memcpy (old_ent, p, htab->root.table.entsize);
3813 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
3814 h = (struct elf_link_hash_entry *) p;
3815 if (h->root.type == bfd_link_hash_warning)
3816 {
3817 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3818 old_ent = (char *) old_ent + htab->root.table.entsize;
3819 }
66eb6687
AM
3820 }
3821 }
3822 }
4ad4eba5 3823
66eb6687 3824 weaks = NULL;
4ad4eba5
AM
3825 ever = extversym != NULL ? extversym + extsymoff : NULL;
3826 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3827 isym < isymend;
3828 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3829 {
3830 int bind;
3831 bfd_vma value;
af44c138 3832 asection *sec, *new_sec;
4ad4eba5
AM
3833 flagword flags;
3834 const char *name;
3835 struct elf_link_hash_entry *h;
90c984fc 3836 struct elf_link_hash_entry *hi;
4ad4eba5
AM
3837 bfd_boolean definition;
3838 bfd_boolean size_change_ok;
3839 bfd_boolean type_change_ok;
3840 bfd_boolean new_weakdef;
37a9e49a
L
3841 bfd_boolean new_weak;
3842 bfd_boolean old_weak;
4ad4eba5 3843 bfd_boolean override;
a4d8e49b 3844 bfd_boolean common;
4ad4eba5
AM
3845 unsigned int old_alignment;
3846 bfd *old_bfd;
3847
3848 override = FALSE;
3849
3850 flags = BSF_NO_FLAGS;
3851 sec = NULL;
3852 value = isym->st_value;
a4d8e49b 3853 common = bed->common_definition (isym);
4ad4eba5
AM
3854
3855 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 3856 switch (bind)
4ad4eba5 3857 {
3e7a7d11 3858 case STB_LOCAL:
4ad4eba5
AM
3859 /* This should be impossible, since ELF requires that all
3860 global symbols follow all local symbols, and that sh_info
3861 point to the first global symbol. Unfortunately, Irix 5
3862 screws this up. */
3863 continue;
3e7a7d11
NC
3864
3865 case STB_GLOBAL:
a4d8e49b 3866 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 3867 flags = BSF_GLOBAL;
3e7a7d11
NC
3868 break;
3869
3870 case STB_WEAK:
3871 flags = BSF_WEAK;
3872 break;
3873
3874 case STB_GNU_UNIQUE:
3875 flags = BSF_GNU_UNIQUE;
3876 break;
3877
3878 default:
4ad4eba5 3879 /* Leave it up to the processor backend. */
3e7a7d11 3880 break;
4ad4eba5
AM
3881 }
3882
3883 if (isym->st_shndx == SHN_UNDEF)
3884 sec = bfd_und_section_ptr;
cb33740c
AM
3885 else if (isym->st_shndx == SHN_ABS)
3886 sec = bfd_abs_section_ptr;
3887 else if (isym->st_shndx == SHN_COMMON)
3888 {
3889 sec = bfd_com_section_ptr;
3890 /* What ELF calls the size we call the value. What ELF
3891 calls the value we call the alignment. */
3892 value = isym->st_size;
3893 }
3894 else
4ad4eba5
AM
3895 {
3896 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3897 if (sec == NULL)
3898 sec = bfd_abs_section_ptr;
dbaa2011 3899 else if (discarded_section (sec))
529fcb95 3900 {
e5d08002
L
3901 /* Symbols from discarded section are undefined. We keep
3902 its visibility. */
529fcb95
PB
3903 sec = bfd_und_section_ptr;
3904 isym->st_shndx = SHN_UNDEF;
3905 }
4ad4eba5
AM
3906 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3907 value -= sec->vma;
3908 }
4ad4eba5
AM
3909
3910 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3911 isym->st_name);
3912 if (name == NULL)
3913 goto error_free_vers;
3914
3915 if (isym->st_shndx == SHN_COMMON
02d00247
AM
3916 && (abfd->flags & BFD_PLUGIN) != 0)
3917 {
3918 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
3919
3920 if (xc == NULL)
3921 {
3922 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
3923 | SEC_EXCLUDE);
3924 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
3925 if (xc == NULL)
3926 goto error_free_vers;
3927 }
3928 sec = xc;
3929 }
3930 else if (isym->st_shndx == SHN_COMMON
3931 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3932 && !info->relocatable)
4ad4eba5
AM
3933 {
3934 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3935
3936 if (tcomm == NULL)
3937 {
02d00247
AM
3938 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
3939 | SEC_LINKER_CREATED);
3940 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 3941 if (tcomm == NULL)
4ad4eba5
AM
3942 goto error_free_vers;
3943 }
3944 sec = tcomm;
3945 }
66eb6687 3946 else if (bed->elf_add_symbol_hook)
4ad4eba5 3947 {
66eb6687
AM
3948 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3949 &sec, &value))
4ad4eba5
AM
3950 goto error_free_vers;
3951
3952 /* The hook function sets the name to NULL if this symbol
3953 should be skipped for some reason. */
3954 if (name == NULL)
3955 continue;
3956 }
3957
3958 /* Sanity check that all possibilities were handled. */
3959 if (sec == NULL)
3960 {
3961 bfd_set_error (bfd_error_bad_value);
3962 goto error_free_vers;
3963 }
3964
191c0c42
AM
3965 /* Silently discard TLS symbols from --just-syms. There's
3966 no way to combine a static TLS block with a new TLS block
3967 for this executable. */
3968 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
3969 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
3970 continue;
3971
4ad4eba5
AM
3972 if (bfd_is_und_section (sec)
3973 || bfd_is_com_section (sec))
3974 definition = FALSE;
3975 else
3976 definition = TRUE;
3977
3978 size_change_ok = FALSE;
66eb6687 3979 type_change_ok = bed->type_change_ok;
37a9e49a 3980 old_weak = FALSE;
4ad4eba5
AM
3981 old_alignment = 0;
3982 old_bfd = NULL;
af44c138 3983 new_sec = sec;
4ad4eba5 3984
66eb6687 3985 if (is_elf_hash_table (htab))
4ad4eba5
AM
3986 {
3987 Elf_Internal_Versym iver;
3988 unsigned int vernum = 0;
3989 bfd_boolean skip;
3990
fc0e6df6 3991 if (ever == NULL)
4ad4eba5 3992 {
fc0e6df6
PB
3993 if (info->default_imported_symver)
3994 /* Use the default symbol version created earlier. */
3995 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3996 else
3997 iver.vs_vers = 0;
3998 }
3999 else
4000 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4001
4002 vernum = iver.vs_vers & VERSYM_VERSION;
4003
4004 /* If this is a hidden symbol, or if it is not version
4005 1, we append the version name to the symbol name.
cc86ff91
EB
4006 However, we do not modify a non-hidden absolute symbol
4007 if it is not a function, because it might be the version
4008 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4009 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4010 || (vernum > 1
4011 && (!bfd_is_abs_section (sec)
4012 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4013 {
4014 const char *verstr;
4015 size_t namelen, verlen, newlen;
4016 char *newname, *p;
4017
4018 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4019 {
fc0e6df6
PB
4020 if (vernum > elf_tdata (abfd)->cverdefs)
4021 verstr = NULL;
4022 else if (vernum > 1)
4023 verstr =
4024 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4025 else
4026 verstr = "";
4ad4eba5 4027
fc0e6df6 4028 if (verstr == NULL)
4ad4eba5 4029 {
fc0e6df6
PB
4030 (*_bfd_error_handler)
4031 (_("%B: %s: invalid version %u (max %d)"),
4032 abfd, name, vernum,
4033 elf_tdata (abfd)->cverdefs);
4034 bfd_set_error (bfd_error_bad_value);
4035 goto error_free_vers;
4ad4eba5 4036 }
fc0e6df6
PB
4037 }
4038 else
4039 {
4040 /* We cannot simply test for the number of
4041 entries in the VERNEED section since the
4042 numbers for the needed versions do not start
4043 at 0. */
4044 Elf_Internal_Verneed *t;
4045
4046 verstr = NULL;
4047 for (t = elf_tdata (abfd)->verref;
4048 t != NULL;
4049 t = t->vn_nextref)
4ad4eba5 4050 {
fc0e6df6 4051 Elf_Internal_Vernaux *a;
4ad4eba5 4052
fc0e6df6
PB
4053 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4054 {
4055 if (a->vna_other == vernum)
4ad4eba5 4056 {
fc0e6df6
PB
4057 verstr = a->vna_nodename;
4058 break;
4ad4eba5 4059 }
4ad4eba5 4060 }
fc0e6df6
PB
4061 if (a != NULL)
4062 break;
4063 }
4064 if (verstr == NULL)
4065 {
4066 (*_bfd_error_handler)
4067 (_("%B: %s: invalid needed version %d"),
4068 abfd, name, vernum);
4069 bfd_set_error (bfd_error_bad_value);
4070 goto error_free_vers;
4ad4eba5 4071 }
4ad4eba5 4072 }
fc0e6df6
PB
4073
4074 namelen = strlen (name);
4075 verlen = strlen (verstr);
4076 newlen = namelen + verlen + 2;
4077 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4078 && isym->st_shndx != SHN_UNDEF)
4079 ++newlen;
4080
a50b1753 4081 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4082 if (newname == NULL)
4083 goto error_free_vers;
4084 memcpy (newname, name, namelen);
4085 p = newname + namelen;
4086 *p++ = ELF_VER_CHR;
4087 /* If this is a defined non-hidden version symbol,
4088 we add another @ to the name. This indicates the
4089 default version of the symbol. */
4090 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4091 && isym->st_shndx != SHN_UNDEF)
4092 *p++ = ELF_VER_CHR;
4093 memcpy (p, verstr, verlen + 1);
4094
4095 name = newname;
4ad4eba5
AM
4096 }
4097
4f3fedcf
AM
4098 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4099 sym_hash, &old_bfd, &old_weak,
4100 &old_alignment, &skip, &override,
4ad4eba5
AM
4101 &type_change_ok, &size_change_ok))
4102 goto error_free_vers;
4103
4104 if (skip)
4105 continue;
4106
4107 if (override)
4108 definition = FALSE;
4109
4110 h = *sym_hash;
4111 while (h->root.type == bfd_link_hash_indirect
4112 || h->root.type == bfd_link_hash_warning)
4113 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4114
4ad4eba5 4115 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4116 && vernum > 1
4117 && definition)
4118 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4119 }
4120
4121 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4122 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4123 (struct bfd_link_hash_entry **) sym_hash)))
4124 goto error_free_vers;
4125
4126 h = *sym_hash;
90c984fc
L
4127 /* We need to make sure that indirect symbol dynamic flags are
4128 updated. */
4129 hi = h;
4ad4eba5
AM
4130 while (h->root.type == bfd_link_hash_indirect
4131 || h->root.type == bfd_link_hash_warning)
4132 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4133
4ad4eba5
AM
4134 *sym_hash = h;
4135
37a9e49a 4136 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4137 new_weakdef = FALSE;
4138 if (dynamic
4139 && definition
37a9e49a 4140 && new_weak
fcb93ecf 4141 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4142 && is_elf_hash_table (htab)
f6e332e6 4143 && h->u.weakdef == NULL)
4ad4eba5
AM
4144 {
4145 /* Keep a list of all weak defined non function symbols from
4146 a dynamic object, using the weakdef field. Later in this
4147 function we will set the weakdef field to the correct
4148 value. We only put non-function symbols from dynamic
4149 objects on this list, because that happens to be the only
4150 time we need to know the normal symbol corresponding to a
4151 weak symbol, and the information is time consuming to
4152 figure out. If the weakdef field is not already NULL,
4153 then this symbol was already defined by some previous
4154 dynamic object, and we will be using that previous
4155 definition anyhow. */
4156
f6e332e6 4157 h->u.weakdef = weaks;
4ad4eba5
AM
4158 weaks = h;
4159 new_weakdef = TRUE;
4160 }
4161
4162 /* Set the alignment of a common symbol. */
a4d8e49b 4163 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4164 && h->root.type == bfd_link_hash_common)
4165 {
4166 unsigned int align;
4167
a4d8e49b 4168 if (common)
af44c138
L
4169 align = bfd_log2 (isym->st_value);
4170 else
4171 {
4172 /* The new symbol is a common symbol in a shared object.
4173 We need to get the alignment from the section. */
4174 align = new_sec->alignment_power;
4175 }
595213d4 4176 if (align > old_alignment)
4ad4eba5
AM
4177 h->root.u.c.p->alignment_power = align;
4178 else
4179 h->root.u.c.p->alignment_power = old_alignment;
4180 }
4181
66eb6687 4182 if (is_elf_hash_table (htab))
4ad4eba5 4183 {
4f3fedcf
AM
4184 /* Set a flag in the hash table entry indicating the type of
4185 reference or definition we just found. A dynamic symbol
4186 is one which is referenced or defined by both a regular
4187 object and a shared object. */
4188 bfd_boolean dynsym = FALSE;
4189
4190 /* Plugin symbols aren't normal. Don't set def_regular or
4191 ref_regular for them, or make them dynamic. */
4192 if ((abfd->flags & BFD_PLUGIN) != 0)
4193 ;
4194 else if (! dynamic)
4195 {
4196 if (! definition)
4197 {
4198 h->ref_regular = 1;
4199 if (bind != STB_WEAK)
4200 h->ref_regular_nonweak = 1;
4201 }
4202 else
4203 {
4204 h->def_regular = 1;
4205 if (h->def_dynamic)
4206 {
4207 h->def_dynamic = 0;
4208 h->ref_dynamic = 1;
4209 }
4210 }
4211
4212 /* If the indirect symbol has been forced local, don't
4213 make the real symbol dynamic. */
4214 if ((h == hi || !hi->forced_local)
4215 && (! info->executable
4216 || h->def_dynamic
4217 || h->ref_dynamic))
4218 dynsym = TRUE;
4219 }
4220 else
4221 {
4222 if (! definition)
4223 {
4224 h->ref_dynamic = 1;
4225 hi->ref_dynamic = 1;
4226 }
4227 else
4228 {
4229 h->def_dynamic = 1;
4230 hi->def_dynamic = 1;
4231 }
4232
4233 /* If the indirect symbol has been forced local, don't
4234 make the real symbol dynamic. */
4235 if ((h == hi || !hi->forced_local)
4236 && (h->def_regular
4237 || h->ref_regular
4238 || (h->u.weakdef != NULL
4239 && ! new_weakdef
4240 && h->u.weakdef->dynindx != -1)))
4241 dynsym = TRUE;
4242 }
4243
4244 /* Check to see if we need to add an indirect symbol for
4245 the default name. */
4246 if (definition
4247 || (!override && h->root.type == bfd_link_hash_common))
4248 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4249 sec, value, &old_bfd, &dynsym))
4250 goto error_free_vers;
4ad4eba5
AM
4251
4252 /* Check the alignment when a common symbol is involved. This
4253 can change when a common symbol is overridden by a normal
4254 definition or a common symbol is ignored due to the old
4255 normal definition. We need to make sure the maximum
4256 alignment is maintained. */
a4d8e49b 4257 if ((old_alignment || common)
4ad4eba5
AM
4258 && h->root.type != bfd_link_hash_common)
4259 {
4260 unsigned int common_align;
4261 unsigned int normal_align;
4262 unsigned int symbol_align;
4263 bfd *normal_bfd;
4264 bfd *common_bfd;
4265
3a81e825
AM
4266 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4267 || h->root.type == bfd_link_hash_defweak);
4268
4ad4eba5
AM
4269 symbol_align = ffs (h->root.u.def.value) - 1;
4270 if (h->root.u.def.section->owner != NULL
4271 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4272 {
4273 normal_align = h->root.u.def.section->alignment_power;
4274 if (normal_align > symbol_align)
4275 normal_align = symbol_align;
4276 }
4277 else
4278 normal_align = symbol_align;
4279
4280 if (old_alignment)
4281 {
4282 common_align = old_alignment;
4283 common_bfd = old_bfd;
4284 normal_bfd = abfd;
4285 }
4286 else
4287 {
4288 common_align = bfd_log2 (isym->st_value);
4289 common_bfd = abfd;
4290 normal_bfd = old_bfd;
4291 }
4292
4293 if (normal_align < common_align)
d07676f8
NC
4294 {
4295 /* PR binutils/2735 */
4296 if (normal_bfd == NULL)
4297 (*_bfd_error_handler)
4f3fedcf
AM
4298 (_("Warning: alignment %u of common symbol `%s' in %B is"
4299 " greater than the alignment (%u) of its section %A"),
d07676f8
NC
4300 common_bfd, h->root.u.def.section,
4301 1 << common_align, name, 1 << normal_align);
4302 else
4303 (*_bfd_error_handler)
4304 (_("Warning: alignment %u of symbol `%s' in %B"
4305 " is smaller than %u in %B"),
4306 normal_bfd, common_bfd,
4307 1 << normal_align, name, 1 << common_align);
4308 }
4ad4eba5
AM
4309 }
4310
83ad0046 4311 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4312 if (isym->st_size != 0
4313 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4314 && (definition || h->size == 0))
4315 {
83ad0046
L
4316 if (h->size != 0
4317 && h->size != isym->st_size
4318 && ! size_change_ok)
4ad4eba5 4319 (*_bfd_error_handler)
d003868e
AM
4320 (_("Warning: size of symbol `%s' changed"
4321 " from %lu in %B to %lu in %B"),
4322 old_bfd, abfd,
4ad4eba5 4323 name, (unsigned long) h->size,
d003868e 4324 (unsigned long) isym->st_size);
4ad4eba5
AM
4325
4326 h->size = isym->st_size;
4327 }
4328
4329 /* If this is a common symbol, then we always want H->SIZE
4330 to be the size of the common symbol. The code just above
4331 won't fix the size if a common symbol becomes larger. We
4332 don't warn about a size change here, because that is
4f3fedcf 4333 covered by --warn-common. Allow changes between different
fcb93ecf 4334 function types. */
4ad4eba5
AM
4335 if (h->root.type == bfd_link_hash_common)
4336 h->size = h->root.u.c.size;
4337
4338 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4339 && ((definition && !new_weak)
4340 || (old_weak && h->root.type == bfd_link_hash_common)
4341 || h->type == STT_NOTYPE))
4ad4eba5 4342 {
2955ec4c
L
4343 unsigned int type = ELF_ST_TYPE (isym->st_info);
4344
4345 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4346 symbol. */
4347 if (type == STT_GNU_IFUNC
4348 && (abfd->flags & DYNAMIC) != 0)
4349 type = STT_FUNC;
4ad4eba5 4350
2955ec4c
L
4351 if (h->type != type)
4352 {
4353 if (h->type != STT_NOTYPE && ! type_change_ok)
4354 (*_bfd_error_handler)
4355 (_("Warning: type of symbol `%s' changed"
4356 " from %d to %d in %B"),
4357 abfd, name, h->type, type);
4358
4359 h->type = type;
4360 }
4ad4eba5
AM
4361 }
4362
54ac0771
L
4363 /* Merge st_other field. */
4364 elf_merge_st_other (abfd, h, isym, definition, dynamic);
4ad4eba5 4365
c3df8c14 4366 /* We don't want to make debug symbol dynamic. */
b2064611 4367 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
c3df8c14
AM
4368 dynsym = FALSE;
4369
4f3fedcf
AM
4370 /* Nor should we make plugin symbols dynamic. */
4371 if ((abfd->flags & BFD_PLUGIN) != 0)
4372 dynsym = FALSE;
4373
35fc36a8 4374 if (definition)
35399224
L
4375 {
4376 h->target_internal = isym->st_target_internal;
4377 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4378 }
35fc36a8 4379
4ad4eba5
AM
4380 if (definition && !dynamic)
4381 {
4382 char *p = strchr (name, ELF_VER_CHR);
4383 if (p != NULL && p[1] != ELF_VER_CHR)
4384 {
4385 /* Queue non-default versions so that .symver x, x@FOO
4386 aliases can be checked. */
66eb6687 4387 if (!nondeflt_vers)
4ad4eba5 4388 {
66eb6687
AM
4389 amt = ((isymend - isym + 1)
4390 * sizeof (struct elf_link_hash_entry *));
a50b1753
NC
4391 nondeflt_vers =
4392 (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4393 if (!nondeflt_vers)
4394 goto error_free_vers;
4ad4eba5 4395 }
66eb6687 4396 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4397 }
4398 }
4399
4400 if (dynsym && h->dynindx == -1)
4401 {
c152c796 4402 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4403 goto error_free_vers;
f6e332e6 4404 if (h->u.weakdef != NULL
4ad4eba5 4405 && ! new_weakdef
f6e332e6 4406 && h->u.weakdef->dynindx == -1)
4ad4eba5 4407 {
66eb6687 4408 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4409 goto error_free_vers;
4410 }
4411 }
4412 else if (dynsym && h->dynindx != -1)
4413 /* If the symbol already has a dynamic index, but
4414 visibility says it should not be visible, turn it into
4415 a local symbol. */
4416 switch (ELF_ST_VISIBILITY (h->other))
4417 {
4418 case STV_INTERNAL:
4419 case STV_HIDDEN:
4420 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4421 dynsym = FALSE;
4422 break;
4423 }
4424
3d5bef4c 4425 /* Don't add DT_NEEDED for references from the dummy bfd. */
4ad4eba5
AM
4426 if (!add_needed
4427 && definition
010e5ae2 4428 && ((dynsym
ffa9430d 4429 && h->ref_regular_nonweak
4f3fedcf
AM
4430 && (old_bfd == NULL
4431 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4432 || (h->ref_dynamic_nonweak
010e5ae2
AM
4433 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4434 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4ad4eba5
AM
4435 {
4436 int ret;
4437 const char *soname = elf_dt_name (abfd);
4438
16e4ecc0
AM
4439 info->callbacks->minfo ("%!", soname, old_bfd,
4440 h->root.root.string);
4441
4ad4eba5
AM
4442 /* A symbol from a library loaded via DT_NEEDED of some
4443 other library is referenced by a regular object.
e56f61be 4444 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4445 --no-add-needed is used and the reference was not
4446 a weak one. */
4f3fedcf 4447 if (old_bfd != NULL
b918acf9 4448 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4449 {
4450 (*_bfd_error_handler)
3cbc5de0 4451 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4452 old_bfd, name);
ff5ac77b 4453 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4454 goto error_free_vers;
4455 }
4456
a50b1753
NC
4457 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4458 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4459
4ad4eba5 4460 add_needed = TRUE;
7e9f0867 4461 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4462 if (ret < 0)
4463 goto error_free_vers;
4464
4465 BFD_ASSERT (ret == 0);
4466 }
4467 }
4468 }
4469
66eb6687
AM
4470 if (extversym != NULL)
4471 {
4472 free (extversym);
4473 extversym = NULL;
4474 }
4475
4476 if (isymbuf != NULL)
4477 {
4478 free (isymbuf);
4479 isymbuf = NULL;
4480 }
4481
4482 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4483 {
4484 unsigned int i;
4485
4486 /* Restore the symbol table. */
f45794cb
AM
4487 old_ent = (char *) old_tab + tabsize;
4488 memset (elf_sym_hashes (abfd), 0,
4489 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4490 htab->root.table.table = old_table;
4491 htab->root.table.size = old_size;
4492 htab->root.table.count = old_count;
66eb6687 4493 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4494 htab->root.undefs = old_undefs;
4495 htab->root.undefs_tail = old_undefs_tail;
d45f8bda 4496 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
66eb6687
AM
4497 for (i = 0; i < htab->root.table.size; i++)
4498 {
4499 struct bfd_hash_entry *p;
4500 struct elf_link_hash_entry *h;
3e0882af
L
4501 bfd_size_type size;
4502 unsigned int alignment_power;
66eb6687
AM
4503
4504 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4505 {
4506 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4507 if (h->root.type == bfd_link_hash_warning)
4508 h = (struct elf_link_hash_entry *) h->root.u.i.link;
a4542f1b
AM
4509 if (h->dynindx >= old_dynsymcount
4510 && h->dynstr_index < old_dynstr_size)
66eb6687 4511 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
2de92251 4512
3e0882af
L
4513 /* Preserve the maximum alignment and size for common
4514 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4515 since it can still be loaded at run time by another
3e0882af
L
4516 dynamic lib. */
4517 if (h->root.type == bfd_link_hash_common)
4518 {
4519 size = h->root.u.c.size;
4520 alignment_power = h->root.u.c.p->alignment_power;
4521 }
4522 else
4523 {
4524 size = 0;
4525 alignment_power = 0;
4526 }
66eb6687
AM
4527 memcpy (p, old_ent, htab->root.table.entsize);
4528 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4529 h = (struct elf_link_hash_entry *) p;
4530 if (h->root.type == bfd_link_hash_warning)
4531 {
4532 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4533 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4534 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4535 }
a4542f1b 4536 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4537 {
4538 if (size > h->root.u.c.size)
4539 h->root.u.c.size = size;
4540 if (alignment_power > h->root.u.c.p->alignment_power)
4541 h->root.u.c.p->alignment_power = alignment_power;
4542 }
66eb6687
AM
4543 }
4544 }
4545
5061a885
AM
4546 /* Make a special call to the linker "notice" function to
4547 tell it that symbols added for crefs may need to be removed. */
e5034e59 4548 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4549 goto error_free_vers;
5061a885 4550
66eb6687
AM
4551 free (old_tab);
4552 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4553 alloc_mark);
4554 if (nondeflt_vers != NULL)
4555 free (nondeflt_vers);
4556 return TRUE;
4557 }
2de92251 4558
66eb6687
AM
4559 if (old_tab != NULL)
4560 {
e5034e59 4561 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4562 goto error_free_vers;
66eb6687
AM
4563 free (old_tab);
4564 old_tab = NULL;
4565 }
4566
4ad4eba5
AM
4567 /* Now that all the symbols from this input file are created, handle
4568 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4569 if (nondeflt_vers != NULL)
4570 {
4571 bfd_size_type cnt, symidx;
4572
4573 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4574 {
4575 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4576 char *shortname, *p;
4577
4578 p = strchr (h->root.root.string, ELF_VER_CHR);
4579 if (p == NULL
4580 || (h->root.type != bfd_link_hash_defined
4581 && h->root.type != bfd_link_hash_defweak))
4582 continue;
4583
4584 amt = p - h->root.root.string;
a50b1753 4585 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4586 if (!shortname)
4587 goto error_free_vers;
4ad4eba5
AM
4588 memcpy (shortname, h->root.root.string, amt);
4589 shortname[amt] = '\0';
4590
4591 hi = (struct elf_link_hash_entry *)
66eb6687 4592 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4593 FALSE, FALSE, FALSE);
4594 if (hi != NULL
4595 && hi->root.type == h->root.type
4596 && hi->root.u.def.value == h->root.u.def.value
4597 && hi->root.u.def.section == h->root.u.def.section)
4598 {
4599 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4600 hi->root.type = bfd_link_hash_indirect;
4601 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4602 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4603 sym_hash = elf_sym_hashes (abfd);
4604 if (sym_hash)
4605 for (symidx = 0; symidx < extsymcount; ++symidx)
4606 if (sym_hash[symidx] == hi)
4607 {
4608 sym_hash[symidx] = h;
4609 break;
4610 }
4611 }
4612 free (shortname);
4613 }
4614 free (nondeflt_vers);
4615 nondeflt_vers = NULL;
4616 }
4617
4ad4eba5
AM
4618 /* Now set the weakdefs field correctly for all the weak defined
4619 symbols we found. The only way to do this is to search all the
4620 symbols. Since we only need the information for non functions in
4621 dynamic objects, that's the only time we actually put anything on
4622 the list WEAKS. We need this information so that if a regular
4623 object refers to a symbol defined weakly in a dynamic object, the
4624 real symbol in the dynamic object is also put in the dynamic
4625 symbols; we also must arrange for both symbols to point to the
4626 same memory location. We could handle the general case of symbol
4627 aliasing, but a general symbol alias can only be generated in
4628 assembler code, handling it correctly would be very time
4629 consuming, and other ELF linkers don't handle general aliasing
4630 either. */
4631 if (weaks != NULL)
4632 {
4633 struct elf_link_hash_entry **hpp;
4634 struct elf_link_hash_entry **hppend;
4635 struct elf_link_hash_entry **sorted_sym_hash;
4636 struct elf_link_hash_entry *h;
4637 size_t sym_count;
4638
4639 /* Since we have to search the whole symbol list for each weak
4640 defined symbol, search time for N weak defined symbols will be
4641 O(N^2). Binary search will cut it down to O(NlogN). */
4642 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 4643 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4644 if (sorted_sym_hash == NULL)
4645 goto error_return;
4646 sym_hash = sorted_sym_hash;
4647 hpp = elf_sym_hashes (abfd);
4648 hppend = hpp + extsymcount;
4649 sym_count = 0;
4650 for (; hpp < hppend; hpp++)
4651 {
4652 h = *hpp;
4653 if (h != NULL
4654 && h->root.type == bfd_link_hash_defined
fcb93ecf 4655 && !bed->is_function_type (h->type))
4ad4eba5
AM
4656 {
4657 *sym_hash = h;
4658 sym_hash++;
4659 sym_count++;
4660 }
4661 }
4662
4663 qsort (sorted_sym_hash, sym_count,
4664 sizeof (struct elf_link_hash_entry *),
4665 elf_sort_symbol);
4666
4667 while (weaks != NULL)
4668 {
4669 struct elf_link_hash_entry *hlook;
4670 asection *slook;
4671 bfd_vma vlook;
ed54588d 4672 size_t i, j, idx = 0;
4ad4eba5
AM
4673
4674 hlook = weaks;
f6e332e6
AM
4675 weaks = hlook->u.weakdef;
4676 hlook->u.weakdef = NULL;
4ad4eba5
AM
4677
4678 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4679 || hlook->root.type == bfd_link_hash_defweak
4680 || hlook->root.type == bfd_link_hash_common
4681 || hlook->root.type == bfd_link_hash_indirect);
4682 slook = hlook->root.u.def.section;
4683 vlook = hlook->root.u.def.value;
4684
4ad4eba5
AM
4685 i = 0;
4686 j = sym_count;
14160578 4687 while (i != j)
4ad4eba5
AM
4688 {
4689 bfd_signed_vma vdiff;
4690 idx = (i + j) / 2;
14160578 4691 h = sorted_sym_hash[idx];
4ad4eba5
AM
4692 vdiff = vlook - h->root.u.def.value;
4693 if (vdiff < 0)
4694 j = idx;
4695 else if (vdiff > 0)
4696 i = idx + 1;
4697 else
4698 {
a9b881be 4699 long sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4700 if (sdiff < 0)
4701 j = idx;
4702 else if (sdiff > 0)
4703 i = idx + 1;
4704 else
14160578 4705 break;
4ad4eba5
AM
4706 }
4707 }
4708
4709 /* We didn't find a value/section match. */
14160578 4710 if (i == j)
4ad4eba5
AM
4711 continue;
4712
14160578
AM
4713 /* With multiple aliases, or when the weak symbol is already
4714 strongly defined, we have multiple matching symbols and
4715 the binary search above may land on any of them. Step
4716 one past the matching symbol(s). */
4717 while (++idx != j)
4718 {
4719 h = sorted_sym_hash[idx];
4720 if (h->root.u.def.section != slook
4721 || h->root.u.def.value != vlook)
4722 break;
4723 }
4724
4725 /* Now look back over the aliases. Since we sorted by size
4726 as well as value and section, we'll choose the one with
4727 the largest size. */
4728 while (idx-- != i)
4ad4eba5 4729 {
14160578 4730 h = sorted_sym_hash[idx];
4ad4eba5
AM
4731
4732 /* Stop if value or section doesn't match. */
14160578
AM
4733 if (h->root.u.def.section != slook
4734 || h->root.u.def.value != vlook)
4ad4eba5
AM
4735 break;
4736 else if (h != hlook)
4737 {
f6e332e6 4738 hlook->u.weakdef = h;
4ad4eba5
AM
4739
4740 /* If the weak definition is in the list of dynamic
4741 symbols, make sure the real definition is put
4742 there as well. */
4743 if (hlook->dynindx != -1 && h->dynindx == -1)
4744 {
c152c796 4745 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
4746 {
4747 err_free_sym_hash:
4748 free (sorted_sym_hash);
4749 goto error_return;
4750 }
4ad4eba5
AM
4751 }
4752
4753 /* If the real definition is in the list of dynamic
4754 symbols, make sure the weak definition is put
4755 there as well. If we don't do this, then the
4756 dynamic loader might not merge the entries for the
4757 real definition and the weak definition. */
4758 if (h->dynindx != -1 && hlook->dynindx == -1)
4759 {
c152c796 4760 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 4761 goto err_free_sym_hash;
4ad4eba5
AM
4762 }
4763 break;
4764 }
4765 }
4766 }
4767
4768 free (sorted_sym_hash);
4769 }
4770
33177bb1
AM
4771 if (bed->check_directives
4772 && !(*bed->check_directives) (abfd, info))
4773 return FALSE;
85fbca6a 4774
4ad4eba5
AM
4775 /* If this object is the same format as the output object, and it is
4776 not a shared library, then let the backend look through the
4777 relocs.
4778
4779 This is required to build global offset table entries and to
4780 arrange for dynamic relocs. It is not required for the
4781 particular common case of linking non PIC code, even when linking
4782 against shared libraries, but unfortunately there is no way of
4783 knowing whether an object file has been compiled PIC or not.
4784 Looking through the relocs is not particularly time consuming.
4785 The problem is that we must either (1) keep the relocs in memory,
4786 which causes the linker to require additional runtime memory or
4787 (2) read the relocs twice from the input file, which wastes time.
4788 This would be a good case for using mmap.
4789
4790 I have no idea how to handle linking PIC code into a file of a
4791 different format. It probably can't be done. */
4ad4eba5 4792 if (! dynamic
66eb6687 4793 && is_elf_hash_table (htab)
13285a1b 4794 && bed->check_relocs != NULL
39334f3a 4795 && elf_object_id (abfd) == elf_hash_table_id (htab)
f13a99db 4796 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4ad4eba5
AM
4797 {
4798 asection *o;
4799
4800 for (o = abfd->sections; o != NULL; o = o->next)
4801 {
4802 Elf_Internal_Rela *internal_relocs;
4803 bfd_boolean ok;
4804
4805 if ((o->flags & SEC_RELOC) == 0
4806 || o->reloc_count == 0
4807 || ((info->strip == strip_all || info->strip == strip_debugger)
4808 && (o->flags & SEC_DEBUGGING) != 0)
4809 || bfd_is_abs_section (o->output_section))
4810 continue;
4811
4812 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4813 info->keep_memory);
4814 if (internal_relocs == NULL)
4815 goto error_return;
4816
66eb6687 4817 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4ad4eba5
AM
4818
4819 if (elf_section_data (o)->relocs != internal_relocs)
4820 free (internal_relocs);
4821
4822 if (! ok)
4823 goto error_return;
4824 }
4825 }
4826
4827 /* If this is a non-traditional link, try to optimize the handling
4828 of the .stab/.stabstr sections. */
4829 if (! dynamic
4830 && ! info->traditional_format
66eb6687 4831 && is_elf_hash_table (htab)
4ad4eba5
AM
4832 && (info->strip != strip_all && info->strip != strip_debugger))
4833 {
4834 asection *stabstr;
4835
4836 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4837 if (stabstr != NULL)
4838 {
4839 bfd_size_type string_offset = 0;
4840 asection *stab;
4841
4842 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 4843 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
4844 && (!stab->name[5] ||
4845 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4846 && (stab->flags & SEC_MERGE) == 0
4847 && !bfd_is_abs_section (stab->output_section))
4848 {
4849 struct bfd_elf_section_data *secdata;
4850
4851 secdata = elf_section_data (stab);
66eb6687
AM
4852 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4853 stabstr, &secdata->sec_info,
4ad4eba5
AM
4854 &string_offset))
4855 goto error_return;
4856 if (secdata->sec_info)
dbaa2011 4857 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
4858 }
4859 }
4860 }
4861
66eb6687 4862 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
4863 {
4864 /* Add this bfd to the loaded list. */
4865 struct elf_link_loaded_list *n;
4866
a50b1753
NC
4867 n = (struct elf_link_loaded_list *)
4868 bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4ad4eba5
AM
4869 if (n == NULL)
4870 goto error_return;
4871 n->abfd = abfd;
66eb6687
AM
4872 n->next = htab->loaded;
4873 htab->loaded = n;
4ad4eba5
AM
4874 }
4875
4876 return TRUE;
4877
4878 error_free_vers:
66eb6687
AM
4879 if (old_tab != NULL)
4880 free (old_tab);
4ad4eba5
AM
4881 if (nondeflt_vers != NULL)
4882 free (nondeflt_vers);
4883 if (extversym != NULL)
4884 free (extversym);
4885 error_free_sym:
4886 if (isymbuf != NULL)
4887 free (isymbuf);
4888 error_return:
4889 return FALSE;
4890}
4891
8387904d
AM
4892/* Return the linker hash table entry of a symbol that might be
4893 satisfied by an archive symbol. Return -1 on error. */
4894
4895struct elf_link_hash_entry *
4896_bfd_elf_archive_symbol_lookup (bfd *abfd,
4897 struct bfd_link_info *info,
4898 const char *name)
4899{
4900 struct elf_link_hash_entry *h;
4901 char *p, *copy;
4902 size_t len, first;
4903
2a41f396 4904 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
4905 if (h != NULL)
4906 return h;
4907
4908 /* If this is a default version (the name contains @@), look up the
4909 symbol again with only one `@' as well as without the version.
4910 The effect is that references to the symbol with and without the
4911 version will be matched by the default symbol in the archive. */
4912
4913 p = strchr (name, ELF_VER_CHR);
4914 if (p == NULL || p[1] != ELF_VER_CHR)
4915 return h;
4916
4917 /* First check with only one `@'. */
4918 len = strlen (name);
a50b1753 4919 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
4920 if (copy == NULL)
4921 return (struct elf_link_hash_entry *) 0 - 1;
4922
4923 first = p - name + 1;
4924 memcpy (copy, name, first);
4925 memcpy (copy + first, name + first + 1, len - first);
4926
2a41f396 4927 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
4928 if (h == NULL)
4929 {
4930 /* We also need to check references to the symbol without the
4931 version. */
4932 copy[first - 1] = '\0';
4933 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 4934 FALSE, FALSE, TRUE);
8387904d
AM
4935 }
4936
4937 bfd_release (abfd, copy);
4938 return h;
4939}
4940
0ad989f9
L
4941/* Add symbols from an ELF archive file to the linker hash table. We
4942 don't use _bfd_generic_link_add_archive_symbols because of a
4943 problem which arises on UnixWare. The UnixWare libc.so is an
4944 archive which includes an entry libc.so.1 which defines a bunch of
4945 symbols. The libc.so archive also includes a number of other
4946 object files, which also define symbols, some of which are the same
4947 as those defined in libc.so.1. Correct linking requires that we
4948 consider each object file in turn, and include it if it defines any
4949 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4950 this; it looks through the list of undefined symbols, and includes
4951 any object file which defines them. When this algorithm is used on
4952 UnixWare, it winds up pulling in libc.so.1 early and defining a
4953 bunch of symbols. This means that some of the other objects in the
4954 archive are not included in the link, which is incorrect since they
4955 precede libc.so.1 in the archive.
4956
4957 Fortunately, ELF archive handling is simpler than that done by
4958 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4959 oddities. In ELF, if we find a symbol in the archive map, and the
4960 symbol is currently undefined, we know that we must pull in that
4961 object file.
4962
4963 Unfortunately, we do have to make multiple passes over the symbol
4964 table until nothing further is resolved. */
4965
4ad4eba5
AM
4966static bfd_boolean
4967elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
4968{
4969 symindex c;
4970 bfd_boolean *defined = NULL;
4971 bfd_boolean *included = NULL;
4972 carsym *symdefs;
4973 bfd_boolean loop;
4974 bfd_size_type amt;
8387904d
AM
4975 const struct elf_backend_data *bed;
4976 struct elf_link_hash_entry * (*archive_symbol_lookup)
4977 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
4978
4979 if (! bfd_has_map (abfd))
4980 {
4981 /* An empty archive is a special case. */
4982 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4983 return TRUE;
4984 bfd_set_error (bfd_error_no_armap);
4985 return FALSE;
4986 }
4987
4988 /* Keep track of all symbols we know to be already defined, and all
4989 files we know to be already included. This is to speed up the
4990 second and subsequent passes. */
4991 c = bfd_ardata (abfd)->symdef_count;
4992 if (c == 0)
4993 return TRUE;
4994 amt = c;
4995 amt *= sizeof (bfd_boolean);
a50b1753
NC
4996 defined = (bfd_boolean *) bfd_zmalloc (amt);
4997 included = (bfd_boolean *) bfd_zmalloc (amt);
0ad989f9
L
4998 if (defined == NULL || included == NULL)
4999 goto error_return;
5000
5001 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5002 bed = get_elf_backend_data (abfd);
5003 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5004
5005 do
5006 {
5007 file_ptr last;
5008 symindex i;
5009 carsym *symdef;
5010 carsym *symdefend;
5011
5012 loop = FALSE;
5013 last = -1;
5014
5015 symdef = symdefs;
5016 symdefend = symdef + c;
5017 for (i = 0; symdef < symdefend; symdef++, i++)
5018 {
5019 struct elf_link_hash_entry *h;
5020 bfd *element;
5021 struct bfd_link_hash_entry *undefs_tail;
5022 symindex mark;
5023
5024 if (defined[i] || included[i])
5025 continue;
5026 if (symdef->file_offset == last)
5027 {
5028 included[i] = TRUE;
5029 continue;
5030 }
5031
8387904d
AM
5032 h = archive_symbol_lookup (abfd, info, symdef->name);
5033 if (h == (struct elf_link_hash_entry *) 0 - 1)
5034 goto error_return;
0ad989f9
L
5035
5036 if (h == NULL)
5037 continue;
5038
5039 if (h->root.type == bfd_link_hash_common)
5040 {
5041 /* We currently have a common symbol. The archive map contains
5042 a reference to this symbol, so we may want to include it. We
5043 only want to include it however, if this archive element
5044 contains a definition of the symbol, not just another common
5045 declaration of it.
5046
5047 Unfortunately some archivers (including GNU ar) will put
5048 declarations of common symbols into their archive maps, as
5049 well as real definitions, so we cannot just go by the archive
5050 map alone. Instead we must read in the element's symbol
5051 table and check that to see what kind of symbol definition
5052 this is. */
5053 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5054 continue;
5055 }
5056 else if (h->root.type != bfd_link_hash_undefined)
5057 {
5058 if (h->root.type != bfd_link_hash_undefweak)
5059 defined[i] = TRUE;
5060 continue;
5061 }
5062
5063 /* We need to include this archive member. */
5064 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5065 if (element == NULL)
5066 goto error_return;
5067
5068 if (! bfd_check_format (element, bfd_object))
5069 goto error_return;
5070
5071 /* Doublecheck that we have not included this object
5072 already--it should be impossible, but there may be
5073 something wrong with the archive. */
5074 if (element->archive_pass != 0)
5075 {
5076 bfd_set_error (bfd_error_bad_value);
5077 goto error_return;
5078 }
5079 element->archive_pass = 1;
5080
5081 undefs_tail = info->hash->undefs_tail;
5082
0e144ba7
AM
5083 if (!(*info->callbacks
5084 ->add_archive_element) (info, element, symdef->name, &element))
0ad989f9 5085 goto error_return;
0e144ba7 5086 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5087 goto error_return;
5088
5089 /* If there are any new undefined symbols, we need to make
5090 another pass through the archive in order to see whether
5091 they can be defined. FIXME: This isn't perfect, because
5092 common symbols wind up on undefs_tail and because an
5093 undefined symbol which is defined later on in this pass
5094 does not require another pass. This isn't a bug, but it
5095 does make the code less efficient than it could be. */
5096 if (undefs_tail != info->hash->undefs_tail)
5097 loop = TRUE;
5098
5099 /* Look backward to mark all symbols from this object file
5100 which we have already seen in this pass. */
5101 mark = i;
5102 do
5103 {
5104 included[mark] = TRUE;
5105 if (mark == 0)
5106 break;
5107 --mark;
5108 }
5109 while (symdefs[mark].file_offset == symdef->file_offset);
5110
5111 /* We mark subsequent symbols from this object file as we go
5112 on through the loop. */
5113 last = symdef->file_offset;
5114 }
5115 }
5116 while (loop);
5117
5118 free (defined);
5119 free (included);
5120
5121 return TRUE;
5122
5123 error_return:
5124 if (defined != NULL)
5125 free (defined);
5126 if (included != NULL)
5127 free (included);
5128 return FALSE;
5129}
4ad4eba5
AM
5130
5131/* Given an ELF BFD, add symbols to the global hash table as
5132 appropriate. */
5133
5134bfd_boolean
5135bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5136{
5137 switch (bfd_get_format (abfd))
5138 {
5139 case bfd_object:
5140 return elf_link_add_object_symbols (abfd, info);
5141 case bfd_archive:
5142 return elf_link_add_archive_symbols (abfd, info);
5143 default:
5144 bfd_set_error (bfd_error_wrong_format);
5145 return FALSE;
5146 }
5147}
5a580b3a 5148\f
14b1c01e
AM
5149struct hash_codes_info
5150{
5151 unsigned long *hashcodes;
5152 bfd_boolean error;
5153};
a0c8462f 5154
5a580b3a
AM
5155/* This function will be called though elf_link_hash_traverse to store
5156 all hash value of the exported symbols in an array. */
5157
5158static bfd_boolean
5159elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5160{
a50b1753 5161 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a
AM
5162 const char *name;
5163 char *p;
5164 unsigned long ha;
5165 char *alc = NULL;
5166
5a580b3a
AM
5167 /* Ignore indirect symbols. These are added by the versioning code. */
5168 if (h->dynindx == -1)
5169 return TRUE;
5170
5171 name = h->root.root.string;
5172 p = strchr (name, ELF_VER_CHR);
5173 if (p != NULL)
5174 {
a50b1753 5175 alc = (char *) bfd_malloc (p - name + 1);
14b1c01e
AM
5176 if (alc == NULL)
5177 {
5178 inf->error = TRUE;
5179 return FALSE;
5180 }
5a580b3a
AM
5181 memcpy (alc, name, p - name);
5182 alc[p - name] = '\0';
5183 name = alc;
5184 }
5185
5186 /* Compute the hash value. */
5187 ha = bfd_elf_hash (name);
5188
5189 /* Store the found hash value in the array given as the argument. */
14b1c01e 5190 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5191
5192 /* And store it in the struct so that we can put it in the hash table
5193 later. */
f6e332e6 5194 h->u.elf_hash_value = ha;
5a580b3a
AM
5195
5196 if (alc != NULL)
5197 free (alc);
5198
5199 return TRUE;
5200}
5201
fdc90cb4
JJ
5202struct collect_gnu_hash_codes
5203{
5204 bfd *output_bfd;
5205 const struct elf_backend_data *bed;
5206 unsigned long int nsyms;
5207 unsigned long int maskbits;
5208 unsigned long int *hashcodes;
5209 unsigned long int *hashval;
5210 unsigned long int *indx;
5211 unsigned long int *counts;
5212 bfd_vma *bitmask;
5213 bfd_byte *contents;
5214 long int min_dynindx;
5215 unsigned long int bucketcount;
5216 unsigned long int symindx;
5217 long int local_indx;
5218 long int shift1, shift2;
5219 unsigned long int mask;
14b1c01e 5220 bfd_boolean error;
fdc90cb4
JJ
5221};
5222
5223/* This function will be called though elf_link_hash_traverse to store
5224 all hash value of the exported symbols in an array. */
5225
5226static bfd_boolean
5227elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5228{
a50b1753 5229 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5230 const char *name;
5231 char *p;
5232 unsigned long ha;
5233 char *alc = NULL;
5234
fdc90cb4
JJ
5235 /* Ignore indirect symbols. These are added by the versioning code. */
5236 if (h->dynindx == -1)
5237 return TRUE;
5238
5239 /* Ignore also local symbols and undefined symbols. */
5240 if (! (*s->bed->elf_hash_symbol) (h))
5241 return TRUE;
5242
5243 name = h->root.root.string;
5244 p = strchr (name, ELF_VER_CHR);
5245 if (p != NULL)
5246 {
a50b1753 5247 alc = (char *) bfd_malloc (p - name + 1);
14b1c01e
AM
5248 if (alc == NULL)
5249 {
5250 s->error = TRUE;
5251 return FALSE;
5252 }
fdc90cb4
JJ
5253 memcpy (alc, name, p - name);
5254 alc[p - name] = '\0';
5255 name = alc;
5256 }
5257
5258 /* Compute the hash value. */
5259 ha = bfd_elf_gnu_hash (name);
5260
5261 /* Store the found hash value in the array for compute_bucket_count,
5262 and also for .dynsym reordering purposes. */
5263 s->hashcodes[s->nsyms] = ha;
5264 s->hashval[h->dynindx] = ha;
5265 ++s->nsyms;
5266 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5267 s->min_dynindx = h->dynindx;
5268
5269 if (alc != NULL)
5270 free (alc);
5271
5272 return TRUE;
5273}
5274
5275/* This function will be called though elf_link_hash_traverse to do
5276 final dynaminc symbol renumbering. */
5277
5278static bfd_boolean
5279elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5280{
a50b1753 5281 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5282 unsigned long int bucket;
5283 unsigned long int val;
5284
fdc90cb4
JJ
5285 /* Ignore indirect symbols. */
5286 if (h->dynindx == -1)
5287 return TRUE;
5288
5289 /* Ignore also local symbols and undefined symbols. */
5290 if (! (*s->bed->elf_hash_symbol) (h))
5291 {
5292 if (h->dynindx >= s->min_dynindx)
5293 h->dynindx = s->local_indx++;
5294 return TRUE;
5295 }
5296
5297 bucket = s->hashval[h->dynindx] % s->bucketcount;
5298 val = (s->hashval[h->dynindx] >> s->shift1)
5299 & ((s->maskbits >> s->shift1) - 1);
5300 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5301 s->bitmask[val]
5302 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5303 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5304 if (s->counts[bucket] == 1)
5305 /* Last element terminates the chain. */
5306 val |= 1;
5307 bfd_put_32 (s->output_bfd, val,
5308 s->contents + (s->indx[bucket] - s->symindx) * 4);
5309 --s->counts[bucket];
5310 h->dynindx = s->indx[bucket]++;
5311 return TRUE;
5312}
5313
5314/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5315
5316bfd_boolean
5317_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5318{
5319 return !(h->forced_local
5320 || h->root.type == bfd_link_hash_undefined
5321 || h->root.type == bfd_link_hash_undefweak
5322 || ((h->root.type == bfd_link_hash_defined
5323 || h->root.type == bfd_link_hash_defweak)
5324 && h->root.u.def.section->output_section == NULL));
5325}
5326
5a580b3a
AM
5327/* Array used to determine the number of hash table buckets to use
5328 based on the number of symbols there are. If there are fewer than
5329 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5330 fewer than 37 we use 17 buckets, and so forth. We never use more
5331 than 32771 buckets. */
5332
5333static const size_t elf_buckets[] =
5334{
5335 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5336 16411, 32771, 0
5337};
5338
5339/* Compute bucket count for hashing table. We do not use a static set
5340 of possible tables sizes anymore. Instead we determine for all
5341 possible reasonable sizes of the table the outcome (i.e., the
5342 number of collisions etc) and choose the best solution. The
5343 weighting functions are not too simple to allow the table to grow
5344 without bounds. Instead one of the weighting factors is the size.
5345 Therefore the result is always a good payoff between few collisions
5346 (= short chain lengths) and table size. */
5347static size_t
b20dd2ce 5348compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5349 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5350 unsigned long int nsyms,
5351 int gnu_hash)
5a580b3a 5352{
5a580b3a 5353 size_t best_size = 0;
5a580b3a 5354 unsigned long int i;
5a580b3a 5355
5a580b3a
AM
5356 /* We have a problem here. The following code to optimize the table
5357 size requires an integer type with more the 32 bits. If
5358 BFD_HOST_U_64_BIT is set we know about such a type. */
5359#ifdef BFD_HOST_U_64_BIT
5360 if (info->optimize)
5361 {
5a580b3a
AM
5362 size_t minsize;
5363 size_t maxsize;
5364 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5365 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5366 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5367 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5368 unsigned long int *counts;
d40f3da9 5369 bfd_size_type amt;
0883b6e0 5370 unsigned int no_improvement_count = 0;
5a580b3a
AM
5371
5372 /* Possible optimization parameters: if we have NSYMS symbols we say
5373 that the hashing table must at least have NSYMS/4 and at most
5374 2*NSYMS buckets. */
5375 minsize = nsyms / 4;
5376 if (minsize == 0)
5377 minsize = 1;
5378 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5379 if (gnu_hash)
5380 {
5381 if (minsize < 2)
5382 minsize = 2;
5383 if ((best_size & 31) == 0)
5384 ++best_size;
5385 }
5a580b3a
AM
5386
5387 /* Create array where we count the collisions in. We must use bfd_malloc
5388 since the size could be large. */
5389 amt = maxsize;
5390 amt *= sizeof (unsigned long int);
a50b1753 5391 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5392 if (counts == NULL)
fdc90cb4 5393 return 0;
5a580b3a
AM
5394
5395 /* Compute the "optimal" size for the hash table. The criteria is a
5396 minimal chain length. The minor criteria is (of course) the size
5397 of the table. */
5398 for (i = minsize; i < maxsize; ++i)
5399 {
5400 /* Walk through the array of hashcodes and count the collisions. */
5401 BFD_HOST_U_64_BIT max;
5402 unsigned long int j;
5403 unsigned long int fact;
5404
fdc90cb4
JJ
5405 if (gnu_hash && (i & 31) == 0)
5406 continue;
5407
5a580b3a
AM
5408 memset (counts, '\0', i * sizeof (unsigned long int));
5409
5410 /* Determine how often each hash bucket is used. */
5411 for (j = 0; j < nsyms; ++j)
5412 ++counts[hashcodes[j] % i];
5413
5414 /* For the weight function we need some information about the
5415 pagesize on the target. This is information need not be 100%
5416 accurate. Since this information is not available (so far) we
5417 define it here to a reasonable default value. If it is crucial
5418 to have a better value some day simply define this value. */
5419# ifndef BFD_TARGET_PAGESIZE
5420# define BFD_TARGET_PAGESIZE (4096)
5421# endif
5422
fdc90cb4
JJ
5423 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5424 and the chains. */
5425 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5426
5427# if 1
5428 /* Variant 1: optimize for short chains. We add the squares
5429 of all the chain lengths (which favors many small chain
5430 over a few long chains). */
5431 for (j = 0; j < i; ++j)
5432 max += counts[j] * counts[j];
5433
5434 /* This adds penalties for the overall size of the table. */
fdc90cb4 5435 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5436 max *= fact * fact;
5437# else
5438 /* Variant 2: Optimize a lot more for small table. Here we
5439 also add squares of the size but we also add penalties for
5440 empty slots (the +1 term). */
5441 for (j = 0; j < i; ++j)
5442 max += (1 + counts[j]) * (1 + counts[j]);
5443
5444 /* The overall size of the table is considered, but not as
5445 strong as in variant 1, where it is squared. */
fdc90cb4 5446 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5447 max *= fact;
5448# endif
5449
5450 /* Compare with current best results. */
5451 if (max < best_chlen)
5452 {
5453 best_chlen = max;
5454 best_size = i;
0883b6e0 5455 no_improvement_count = 0;
5a580b3a 5456 }
0883b6e0
NC
5457 /* PR 11843: Avoid futile long searches for the best bucket size
5458 when there are a large number of symbols. */
5459 else if (++no_improvement_count == 100)
5460 break;
5a580b3a
AM
5461 }
5462
5463 free (counts);
5464 }
5465 else
5466#endif /* defined (BFD_HOST_U_64_BIT) */
5467 {
5468 /* This is the fallback solution if no 64bit type is available or if we
5469 are not supposed to spend much time on optimizations. We select the
5470 bucket count using a fixed set of numbers. */
5471 for (i = 0; elf_buckets[i] != 0; i++)
5472 {
5473 best_size = elf_buckets[i];
fdc90cb4 5474 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5475 break;
5476 }
fdc90cb4
JJ
5477 if (gnu_hash && best_size < 2)
5478 best_size = 2;
5a580b3a
AM
5479 }
5480
5a580b3a
AM
5481 return best_size;
5482}
5483
d0bf826b
AM
5484/* Size any SHT_GROUP section for ld -r. */
5485
5486bfd_boolean
5487_bfd_elf_size_group_sections (struct bfd_link_info *info)
5488{
5489 bfd *ibfd;
5490
5491 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5492 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5493 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5494 return FALSE;
5495 return TRUE;
5496}
5497
04c3a755
NS
5498/* Set a default stack segment size. The value in INFO wins. If it
5499 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5500 undefined it is initialized. */
5501
5502bfd_boolean
5503bfd_elf_stack_segment_size (bfd *output_bfd,
5504 struct bfd_link_info *info,
5505 const char *legacy_symbol,
5506 bfd_vma default_size)
5507{
5508 struct elf_link_hash_entry *h = NULL;
5509
5510 /* Look for legacy symbol. */
5511 if (legacy_symbol)
5512 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5513 FALSE, FALSE, FALSE);
5514 if (h && (h->root.type == bfd_link_hash_defined
5515 || h->root.type == bfd_link_hash_defweak)
5516 && h->def_regular
5517 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5518 {
5519 /* The symbol has no type if specified on the command line. */
5520 h->type = STT_OBJECT;
5521 if (info->stacksize)
5522 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5523 output_bfd, legacy_symbol);
5524 else if (h->root.u.def.section != bfd_abs_section_ptr)
5525 (*_bfd_error_handler) (_("%B: %s not absolute"),
5526 output_bfd, legacy_symbol);
5527 else
5528 info->stacksize = h->root.u.def.value;
5529 }
5530
5531 if (!info->stacksize)
5532 /* If the user didn't set a size, or explicitly inhibit the
5533 size, set it now. */
5534 info->stacksize = default_size;
5535
5536 /* Provide the legacy symbol, if it is referenced. */
5537 if (h && (h->root.type == bfd_link_hash_undefined
5538 || h->root.type == bfd_link_hash_undefweak))
5539 {
5540 struct bfd_link_hash_entry *bh = NULL;
5541
5542 if (!(_bfd_generic_link_add_one_symbol
5543 (info, output_bfd, legacy_symbol,
5544 BSF_GLOBAL, bfd_abs_section_ptr,
5545 info->stacksize >= 0 ? info->stacksize : 0,
5546 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5547 return FALSE;
5548
5549 h = (struct elf_link_hash_entry *) bh;
5550 h->def_regular = 1;
5551 h->type = STT_OBJECT;
5552 }
5553
5554 return TRUE;
5555}
5556
5a580b3a
AM
5557/* Set up the sizes and contents of the ELF dynamic sections. This is
5558 called by the ELF linker emulation before_allocation routine. We
5559 must set the sizes of the sections before the linker sets the
5560 addresses of the various sections. */
5561
5562bfd_boolean
5563bfd_elf_size_dynamic_sections (bfd *output_bfd,
5564 const char *soname,
5565 const char *rpath,
5566 const char *filter_shlib,
7ee314fa
AM
5567 const char *audit,
5568 const char *depaudit,
5a580b3a
AM
5569 const char * const *auxiliary_filters,
5570 struct bfd_link_info *info,
fd91d419 5571 asection **sinterpptr)
5a580b3a
AM
5572{
5573 bfd_size_type soname_indx;
5574 bfd *dynobj;
5575 const struct elf_backend_data *bed;
28caa186 5576 struct elf_info_failed asvinfo;
5a580b3a
AM
5577
5578 *sinterpptr = NULL;
5579
5580 soname_indx = (bfd_size_type) -1;
5581
5582 if (!is_elf_hash_table (info->hash))
5583 return TRUE;
5584
6bfdb61b 5585 bed = get_elf_backend_data (output_bfd);
04c3a755
NS
5586
5587 /* Any syms created from now on start with -1 in
5588 got.refcount/offset and plt.refcount/offset. */
5589 elf_hash_table (info)->init_got_refcount
5590 = elf_hash_table (info)->init_got_offset;
5591 elf_hash_table (info)->init_plt_refcount
5592 = elf_hash_table (info)->init_plt_offset;
5593
5594 if (info->relocatable
5595 && !_bfd_elf_size_group_sections (info))
5596 return FALSE;
5597
5598 /* The backend may have to create some sections regardless of whether
5599 we're dynamic or not. */
5600 if (bed->elf_backend_always_size_sections
5601 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5602 return FALSE;
5603
5604 /* Determine any GNU_STACK segment requirements, after the backend
5605 has had a chance to set a default segment size. */
5a580b3a 5606 if (info->execstack)
12bd6957 5607 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5a580b3a 5608 else if (info->noexecstack)
12bd6957 5609 elf_stack_flags (output_bfd) = PF_R | PF_W;
5a580b3a
AM
5610 else
5611 {
5612 bfd *inputobj;
5613 asection *notesec = NULL;
5614 int exec = 0;
5615
5616 for (inputobj = info->input_bfds;
5617 inputobj;
5618 inputobj = inputobj->link_next)
5619 {
5620 asection *s;
5621
a92c088a
L
5622 if (inputobj->flags
5623 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5624 continue;
5625 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5626 if (s)
5627 {
5628 if (s->flags & SEC_CODE)
5629 exec = PF_X;
5630 notesec = s;
5631 }
6bfdb61b 5632 else if (bed->default_execstack)
5a580b3a
AM
5633 exec = PF_X;
5634 }
04c3a755 5635 if (notesec || info->stacksize > 0)
12bd6957 5636 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
04c3a755
NS
5637 if (notesec && exec && info->relocatable
5638 && notesec->output_section != bfd_abs_section_ptr)
5639 notesec->output_section->flags |= SEC_CODE;
5a580b3a
AM
5640 }
5641
5a580b3a
AM
5642 dynobj = elf_hash_table (info)->dynobj;
5643
9a2a56cc 5644 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
5645 {
5646 struct elf_info_failed eif;
5647 struct elf_link_hash_entry *h;
5648 asection *dynstr;
5649 struct bfd_elf_version_tree *t;
5650 struct bfd_elf_version_expr *d;
046183de 5651 asection *s;
5a580b3a
AM
5652 bfd_boolean all_defined;
5653
3d4d4302 5654 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5a580b3a
AM
5655 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5656
5657 if (soname != NULL)
5658 {
5659 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5660 soname, TRUE);
5661 if (soname_indx == (bfd_size_type) -1
5662 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5663 return FALSE;
5664 }
5665
5666 if (info->symbolic)
5667 {
5668 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5669 return FALSE;
5670 info->flags |= DF_SYMBOLIC;
5671 }
5672
5673 if (rpath != NULL)
5674 {
5675 bfd_size_type indx;
b1b00fcc 5676 bfd_vma tag;
5a580b3a
AM
5677
5678 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5679 TRUE);
b1b00fcc 5680 if (indx == (bfd_size_type) -1)
5a580b3a
AM
5681 return FALSE;
5682
b1b00fcc
MF
5683 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5684 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5685 return FALSE;
5a580b3a
AM
5686 }
5687
5688 if (filter_shlib != NULL)
5689 {
5690 bfd_size_type indx;
5691
5692 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5693 filter_shlib, TRUE);
5694 if (indx == (bfd_size_type) -1
5695 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5696 return FALSE;
5697 }
5698
5699 if (auxiliary_filters != NULL)
5700 {
5701 const char * const *p;
5702
5703 for (p = auxiliary_filters; *p != NULL; p++)
5704 {
5705 bfd_size_type indx;
5706
5707 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5708 *p, TRUE);
5709 if (indx == (bfd_size_type) -1
5710 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5711 return FALSE;
5712 }
5713 }
5714
7ee314fa
AM
5715 if (audit != NULL)
5716 {
5717 bfd_size_type indx;
5718
5719 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5720 TRUE);
5721 if (indx == (bfd_size_type) -1
5722 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5723 return FALSE;
5724 }
5725
5726 if (depaudit != NULL)
5727 {
5728 bfd_size_type indx;
5729
5730 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5731 TRUE);
5732 if (indx == (bfd_size_type) -1
5733 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5734 return FALSE;
5735 }
5736
5a580b3a 5737 eif.info = info;
5a580b3a
AM
5738 eif.failed = FALSE;
5739
5740 /* If we are supposed to export all symbols into the dynamic symbol
5741 table (this is not the normal case), then do so. */
55255dae
L
5742 if (info->export_dynamic
5743 || (info->executable && info->dynamic))
5a580b3a
AM
5744 {
5745 elf_link_hash_traverse (elf_hash_table (info),
5746 _bfd_elf_export_symbol,
5747 &eif);
5748 if (eif.failed)
5749 return FALSE;
5750 }
5751
5752 /* Make all global versions with definition. */
fd91d419 5753 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5754 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5755 if (!d->symver && d->literal)
5a580b3a
AM
5756 {
5757 const char *verstr, *name;
5758 size_t namelen, verlen, newlen;
93252b1c 5759 char *newname, *p, leading_char;
5a580b3a
AM
5760 struct elf_link_hash_entry *newh;
5761
93252b1c 5762 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 5763 name = d->pattern;
93252b1c 5764 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
5765 verstr = t->name;
5766 verlen = strlen (verstr);
5767 newlen = namelen + verlen + 3;
5768
a50b1753 5769 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
5770 if (newname == NULL)
5771 return FALSE;
93252b1c
MF
5772 newname[0] = leading_char;
5773 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
5774
5775 /* Check the hidden versioned definition. */
5776 p = newname + namelen;
5777 *p++ = ELF_VER_CHR;
5778 memcpy (p, verstr, verlen + 1);
5779 newh = elf_link_hash_lookup (elf_hash_table (info),
5780 newname, FALSE, FALSE,
5781 FALSE);
5782 if (newh == NULL
5783 || (newh->root.type != bfd_link_hash_defined
5784 && newh->root.type != bfd_link_hash_defweak))
5785 {
5786 /* Check the default versioned definition. */
5787 *p++ = ELF_VER_CHR;
5788 memcpy (p, verstr, verlen + 1);
5789 newh = elf_link_hash_lookup (elf_hash_table (info),
5790 newname, FALSE, FALSE,
5791 FALSE);
5792 }
5793 free (newname);
5794
5795 /* Mark this version if there is a definition and it is
5796 not defined in a shared object. */
5797 if (newh != NULL
f5385ebf 5798 && !newh->def_dynamic
5a580b3a
AM
5799 && (newh->root.type == bfd_link_hash_defined
5800 || newh->root.type == bfd_link_hash_defweak))
5801 d->symver = 1;
5802 }
5803
5804 /* Attach all the symbols to their version information. */
5a580b3a 5805 asvinfo.info = info;
5a580b3a
AM
5806 asvinfo.failed = FALSE;
5807
5808 elf_link_hash_traverse (elf_hash_table (info),
5809 _bfd_elf_link_assign_sym_version,
5810 &asvinfo);
5811 if (asvinfo.failed)
5812 return FALSE;
5813
5814 if (!info->allow_undefined_version)
5815 {
5816 /* Check if all global versions have a definition. */
5817 all_defined = TRUE;
fd91d419 5818 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5819 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5820 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
5821 {
5822 (*_bfd_error_handler)
5823 (_("%s: undefined version: %s"),
5824 d->pattern, t->name);
5825 all_defined = FALSE;
5826 }
5827
5828 if (!all_defined)
5829 {
5830 bfd_set_error (bfd_error_bad_value);
5831 return FALSE;
5832 }
5833 }
5834
5835 /* Find all symbols which were defined in a dynamic object and make
5836 the backend pick a reasonable value for them. */
5837 elf_link_hash_traverse (elf_hash_table (info),
5838 _bfd_elf_adjust_dynamic_symbol,
5839 &eif);
5840 if (eif.failed)
5841 return FALSE;
5842
5843 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 5844 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
5845 now so that we know the final size of the .dynamic section. */
5846
5847 /* If there are initialization and/or finalization functions to
5848 call then add the corresponding DT_INIT/DT_FINI entries. */
5849 h = (info->init_function
5850 ? elf_link_hash_lookup (elf_hash_table (info),
5851 info->init_function, FALSE,
5852 FALSE, FALSE)
5853 : NULL);
5854 if (h != NULL
f5385ebf
AM
5855 && (h->ref_regular
5856 || h->def_regular))
5a580b3a
AM
5857 {
5858 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5859 return FALSE;
5860 }
5861 h = (info->fini_function
5862 ? elf_link_hash_lookup (elf_hash_table (info),
5863 info->fini_function, FALSE,
5864 FALSE, FALSE)
5865 : NULL);
5866 if (h != NULL
f5385ebf
AM
5867 && (h->ref_regular
5868 || h->def_regular))
5a580b3a
AM
5869 {
5870 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5871 return FALSE;
5872 }
5873
046183de
AM
5874 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5875 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5876 {
5877 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5878 if (! info->executable)
5879 {
5880 bfd *sub;
5881 asection *o;
5882
5883 for (sub = info->input_bfds; sub != NULL;
5884 sub = sub->link_next)
3fcd97f1
JJ
5885 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5886 for (o = sub->sections; o != NULL; o = o->next)
5887 if (elf_section_data (o)->this_hdr.sh_type
5888 == SHT_PREINIT_ARRAY)
5889 {
5890 (*_bfd_error_handler)
5891 (_("%B: .preinit_array section is not allowed in DSO"),
5892 sub);
5893 break;
5894 }
5a580b3a
AM
5895
5896 bfd_set_error (bfd_error_nonrepresentable_section);
5897 return FALSE;
5898 }
5899
5900 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5901 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5902 return FALSE;
5903 }
046183de
AM
5904 s = bfd_get_section_by_name (output_bfd, ".init_array");
5905 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5906 {
5907 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5908 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5909 return FALSE;
5910 }
046183de
AM
5911 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5912 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5913 {
5914 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5915 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5916 return FALSE;
5917 }
5918
3d4d4302 5919 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
5920 /* If .dynstr is excluded from the link, we don't want any of
5921 these tags. Strictly, we should be checking each section
5922 individually; This quick check covers for the case where
5923 someone does a /DISCARD/ : { *(*) }. */
5924 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5925 {
5926 bfd_size_type strsize;
5927
5928 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
5929 if ((info->emit_hash
5930 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5931 || (info->emit_gnu_hash
5932 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
5933 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5934 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5935 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5936 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5937 bed->s->sizeof_sym))
5938 return FALSE;
5939 }
5940 }
5941
5942 /* The backend must work out the sizes of all the other dynamic
5943 sections. */
9a2a56cc
AM
5944 if (dynobj != NULL
5945 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
5946 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5947 return FALSE;
5948
9a2a56cc
AM
5949 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5950 return FALSE;
5951
5952 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 5953 {
554220db 5954 unsigned long section_sym_count;
fd91d419 5955 struct bfd_elf_version_tree *verdefs;
5a580b3a 5956 asection *s;
5a580b3a
AM
5957
5958 /* Set up the version definition section. */
3d4d4302 5959 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
5960 BFD_ASSERT (s != NULL);
5961
5962 /* We may have created additional version definitions if we are
5963 just linking a regular application. */
fd91d419 5964 verdefs = info->version_info;
5a580b3a
AM
5965
5966 /* Skip anonymous version tag. */
5967 if (verdefs != NULL && verdefs->vernum == 0)
5968 verdefs = verdefs->next;
5969
3e3b46e5 5970 if (verdefs == NULL && !info->create_default_symver)
8423293d 5971 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
5972 else
5973 {
5974 unsigned int cdefs;
5975 bfd_size_type size;
5976 struct bfd_elf_version_tree *t;
5977 bfd_byte *p;
5978 Elf_Internal_Verdef def;
5979 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
5980 struct bfd_link_hash_entry *bh;
5981 struct elf_link_hash_entry *h;
5982 const char *name;
5a580b3a
AM
5983
5984 cdefs = 0;
5985 size = 0;
5986
5987 /* Make space for the base version. */
5988 size += sizeof (Elf_External_Verdef);
5989 size += sizeof (Elf_External_Verdaux);
5990 ++cdefs;
5991
3e3b46e5
PB
5992 /* Make space for the default version. */
5993 if (info->create_default_symver)
5994 {
5995 size += sizeof (Elf_External_Verdef);
5996 ++cdefs;
5997 }
5998
5a580b3a
AM
5999 for (t = verdefs; t != NULL; t = t->next)
6000 {
6001 struct bfd_elf_version_deps *n;
6002
a6cc6b3b
RO
6003 /* Don't emit base version twice. */
6004 if (t->vernum == 0)
6005 continue;
6006
5a580b3a
AM
6007 size += sizeof (Elf_External_Verdef);
6008 size += sizeof (Elf_External_Verdaux);
6009 ++cdefs;
6010
6011 for (n = t->deps; n != NULL; n = n->next)
6012 size += sizeof (Elf_External_Verdaux);
6013 }
6014
eea6121a 6015 s->size = size;
a50b1753 6016 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6017 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6018 return FALSE;
6019
6020 /* Fill in the version definition section. */
6021
6022 p = s->contents;
6023
6024 def.vd_version = VER_DEF_CURRENT;
6025 def.vd_flags = VER_FLG_BASE;
6026 def.vd_ndx = 1;
6027 def.vd_cnt = 1;
3e3b46e5
PB
6028 if (info->create_default_symver)
6029 {
6030 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6031 def.vd_next = sizeof (Elf_External_Verdef);
6032 }
6033 else
6034 {
6035 def.vd_aux = sizeof (Elf_External_Verdef);
6036 def.vd_next = (sizeof (Elf_External_Verdef)
6037 + sizeof (Elf_External_Verdaux));
6038 }
5a580b3a
AM
6039
6040 if (soname_indx != (bfd_size_type) -1)
6041 {
6042 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6043 soname_indx);
6044 def.vd_hash = bfd_elf_hash (soname);
6045 defaux.vda_name = soname_indx;
3e3b46e5 6046 name = soname;
5a580b3a
AM
6047 }
6048 else
6049 {
5a580b3a
AM
6050 bfd_size_type indx;
6051
06084812 6052 name = lbasename (output_bfd->filename);
5a580b3a
AM
6053 def.vd_hash = bfd_elf_hash (name);
6054 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6055 name, FALSE);
6056 if (indx == (bfd_size_type) -1)
6057 return FALSE;
6058 defaux.vda_name = indx;
6059 }
6060 defaux.vda_next = 0;
6061
6062 _bfd_elf_swap_verdef_out (output_bfd, &def,
6063 (Elf_External_Verdef *) p);
6064 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6065 if (info->create_default_symver)
6066 {
6067 /* Add a symbol representing this version. */
6068 bh = NULL;
6069 if (! (_bfd_generic_link_add_one_symbol
6070 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6071 0, NULL, FALSE,
6072 get_elf_backend_data (dynobj)->collect, &bh)))
6073 return FALSE;
6074 h = (struct elf_link_hash_entry *) bh;
6075 h->non_elf = 0;
6076 h->def_regular = 1;
6077 h->type = STT_OBJECT;
6078 h->verinfo.vertree = NULL;
6079
6080 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6081 return FALSE;
6082
6083 /* Create a duplicate of the base version with the same
6084 aux block, but different flags. */
6085 def.vd_flags = 0;
6086 def.vd_ndx = 2;
6087 def.vd_aux = sizeof (Elf_External_Verdef);
6088 if (verdefs)
6089 def.vd_next = (sizeof (Elf_External_Verdef)
6090 + sizeof (Elf_External_Verdaux));
6091 else
6092 def.vd_next = 0;
6093 _bfd_elf_swap_verdef_out (output_bfd, &def,
6094 (Elf_External_Verdef *) p);
6095 p += sizeof (Elf_External_Verdef);
6096 }
5a580b3a
AM
6097 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6098 (Elf_External_Verdaux *) p);
6099 p += sizeof (Elf_External_Verdaux);
6100
6101 for (t = verdefs; t != NULL; t = t->next)
6102 {
6103 unsigned int cdeps;
6104 struct bfd_elf_version_deps *n;
5a580b3a 6105
a6cc6b3b
RO
6106 /* Don't emit the base version twice. */
6107 if (t->vernum == 0)
6108 continue;
6109
5a580b3a
AM
6110 cdeps = 0;
6111 for (n = t->deps; n != NULL; n = n->next)
6112 ++cdeps;
6113
6114 /* Add a symbol representing this version. */
6115 bh = NULL;
6116 if (! (_bfd_generic_link_add_one_symbol
6117 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6118 0, NULL, FALSE,
6119 get_elf_backend_data (dynobj)->collect, &bh)))
6120 return FALSE;
6121 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6122 h->non_elf = 0;
6123 h->def_regular = 1;
5a580b3a
AM
6124 h->type = STT_OBJECT;
6125 h->verinfo.vertree = t;
6126
c152c796 6127 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6128 return FALSE;
6129
6130 def.vd_version = VER_DEF_CURRENT;
6131 def.vd_flags = 0;
6132 if (t->globals.list == NULL
6133 && t->locals.list == NULL
6134 && ! t->used)
6135 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6136 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6137 def.vd_cnt = cdeps + 1;
6138 def.vd_hash = bfd_elf_hash (t->name);
6139 def.vd_aux = sizeof (Elf_External_Verdef);
6140 def.vd_next = 0;
a6cc6b3b
RO
6141
6142 /* If a basever node is next, it *must* be the last node in
6143 the chain, otherwise Verdef construction breaks. */
6144 if (t->next != NULL && t->next->vernum == 0)
6145 BFD_ASSERT (t->next->next == NULL);
6146
6147 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6148 def.vd_next = (sizeof (Elf_External_Verdef)
6149 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6150
6151 _bfd_elf_swap_verdef_out (output_bfd, &def,
6152 (Elf_External_Verdef *) p);
6153 p += sizeof (Elf_External_Verdef);
6154
6155 defaux.vda_name = h->dynstr_index;
6156 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6157 h->dynstr_index);
6158 defaux.vda_next = 0;
6159 if (t->deps != NULL)
6160 defaux.vda_next = sizeof (Elf_External_Verdaux);
6161 t->name_indx = defaux.vda_name;
6162
6163 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6164 (Elf_External_Verdaux *) p);
6165 p += sizeof (Elf_External_Verdaux);
6166
6167 for (n = t->deps; n != NULL; n = n->next)
6168 {
6169 if (n->version_needed == NULL)
6170 {
6171 /* This can happen if there was an error in the
6172 version script. */
6173 defaux.vda_name = 0;
6174 }
6175 else
6176 {
6177 defaux.vda_name = n->version_needed->name_indx;
6178 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6179 defaux.vda_name);
6180 }
6181 if (n->next == NULL)
6182 defaux.vda_next = 0;
6183 else
6184 defaux.vda_next = sizeof (Elf_External_Verdaux);
6185
6186 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6187 (Elf_External_Verdaux *) p);
6188 p += sizeof (Elf_External_Verdaux);
6189 }
6190 }
6191
6192 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6193 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6194 return FALSE;
6195
6196 elf_tdata (output_bfd)->cverdefs = cdefs;
6197 }
6198
6199 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6200 {
6201 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6202 return FALSE;
6203 }
6204 else if (info->flags & DF_BIND_NOW)
6205 {
6206 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6207 return FALSE;
6208 }
6209
6210 if (info->flags_1)
6211 {
6212 if (info->executable)
6213 info->flags_1 &= ~ (DF_1_INITFIRST
6214 | DF_1_NODELETE
6215 | DF_1_NOOPEN);
6216 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6217 return FALSE;
6218 }
6219
6220 /* Work out the size of the version reference section. */
6221
3d4d4302 6222 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6223 BFD_ASSERT (s != NULL);
6224 {
6225 struct elf_find_verdep_info sinfo;
6226
5a580b3a
AM
6227 sinfo.info = info;
6228 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6229 if (sinfo.vers == 0)
6230 sinfo.vers = 1;
6231 sinfo.failed = FALSE;
6232
6233 elf_link_hash_traverse (elf_hash_table (info),
6234 _bfd_elf_link_find_version_dependencies,
6235 &sinfo);
14b1c01e
AM
6236 if (sinfo.failed)
6237 return FALSE;
5a580b3a
AM
6238
6239 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6240 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6241 else
6242 {
6243 Elf_Internal_Verneed *t;
6244 unsigned int size;
6245 unsigned int crefs;
6246 bfd_byte *p;
6247
a6cc6b3b 6248 /* Build the version dependency section. */
5a580b3a
AM
6249 size = 0;
6250 crefs = 0;
6251 for (t = elf_tdata (output_bfd)->verref;
6252 t != NULL;
6253 t = t->vn_nextref)
6254 {
6255 Elf_Internal_Vernaux *a;
6256
6257 size += sizeof (Elf_External_Verneed);
6258 ++crefs;
6259 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6260 size += sizeof (Elf_External_Vernaux);
6261 }
6262
eea6121a 6263 s->size = size;
a50b1753 6264 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6265 if (s->contents == NULL)
6266 return FALSE;
6267
6268 p = s->contents;
6269 for (t = elf_tdata (output_bfd)->verref;
6270 t != NULL;
6271 t = t->vn_nextref)
6272 {
6273 unsigned int caux;
6274 Elf_Internal_Vernaux *a;
6275 bfd_size_type indx;
6276
6277 caux = 0;
6278 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6279 ++caux;
6280
6281 t->vn_version = VER_NEED_CURRENT;
6282 t->vn_cnt = caux;
6283 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6284 elf_dt_name (t->vn_bfd) != NULL
6285 ? elf_dt_name (t->vn_bfd)
06084812 6286 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6287 FALSE);
6288 if (indx == (bfd_size_type) -1)
6289 return FALSE;
6290 t->vn_file = indx;
6291 t->vn_aux = sizeof (Elf_External_Verneed);
6292 if (t->vn_nextref == NULL)
6293 t->vn_next = 0;
6294 else
6295 t->vn_next = (sizeof (Elf_External_Verneed)
6296 + caux * sizeof (Elf_External_Vernaux));
6297
6298 _bfd_elf_swap_verneed_out (output_bfd, t,
6299 (Elf_External_Verneed *) p);
6300 p += sizeof (Elf_External_Verneed);
6301
6302 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6303 {
6304 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6305 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6306 a->vna_nodename, FALSE);
6307 if (indx == (bfd_size_type) -1)
6308 return FALSE;
6309 a->vna_name = indx;
6310 if (a->vna_nextptr == NULL)
6311 a->vna_next = 0;
6312 else
6313 a->vna_next = sizeof (Elf_External_Vernaux);
6314
6315 _bfd_elf_swap_vernaux_out (output_bfd, a,
6316 (Elf_External_Vernaux *) p);
6317 p += sizeof (Elf_External_Vernaux);
6318 }
6319 }
6320
6321 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6322 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6323 return FALSE;
6324
6325 elf_tdata (output_bfd)->cverrefs = crefs;
6326 }
6327 }
6328
8423293d
AM
6329 if ((elf_tdata (output_bfd)->cverrefs == 0
6330 && elf_tdata (output_bfd)->cverdefs == 0)
6331 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6332 &section_sym_count) == 0)
6333 {
3d4d4302 6334 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6335 s->flags |= SEC_EXCLUDE;
6336 }
6337 }
6338 return TRUE;
6339}
6340
74541ad4
AM
6341/* Find the first non-excluded output section. We'll use its
6342 section symbol for some emitted relocs. */
6343void
6344_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6345{
6346 asection *s;
6347
6348 for (s = output_bfd->sections; s != NULL; s = s->next)
6349 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6350 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6351 {
6352 elf_hash_table (info)->text_index_section = s;
6353 break;
6354 }
6355}
6356
6357/* Find two non-excluded output sections, one for code, one for data.
6358 We'll use their section symbols for some emitted relocs. */
6359void
6360_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6361{
6362 asection *s;
6363
266b05cf
DJ
6364 /* Data first, since setting text_index_section changes
6365 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6366 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6367 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6368 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6369 {
266b05cf 6370 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6371 break;
6372 }
6373
6374 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6375 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6376 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6377 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6378 {
266b05cf 6379 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6380 break;
6381 }
6382
6383 if (elf_hash_table (info)->text_index_section == NULL)
6384 elf_hash_table (info)->text_index_section
6385 = elf_hash_table (info)->data_index_section;
6386}
6387
8423293d
AM
6388bfd_boolean
6389bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6390{
74541ad4
AM
6391 const struct elf_backend_data *bed;
6392
8423293d
AM
6393 if (!is_elf_hash_table (info->hash))
6394 return TRUE;
6395
74541ad4
AM
6396 bed = get_elf_backend_data (output_bfd);
6397 (*bed->elf_backend_init_index_section) (output_bfd, info);
6398
8423293d
AM
6399 if (elf_hash_table (info)->dynamic_sections_created)
6400 {
6401 bfd *dynobj;
8423293d
AM
6402 asection *s;
6403 bfd_size_type dynsymcount;
6404 unsigned long section_sym_count;
8423293d
AM
6405 unsigned int dtagcount;
6406
6407 dynobj = elf_hash_table (info)->dynobj;
6408
5a580b3a
AM
6409 /* Assign dynsym indicies. In a shared library we generate a
6410 section symbol for each output section, which come first.
6411 Next come all of the back-end allocated local dynamic syms,
6412 followed by the rest of the global symbols. */
6413
554220db
AM
6414 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6415 &section_sym_count);
5a580b3a
AM
6416
6417 /* Work out the size of the symbol version section. */
3d4d4302 6418 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6419 BFD_ASSERT (s != NULL);
8423293d
AM
6420 if (dynsymcount != 0
6421 && (s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6422 {
eea6121a 6423 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6424 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6425 if (s->contents == NULL)
6426 return FALSE;
6427
6428 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6429 return FALSE;
6430 }
6431
6432 /* Set the size of the .dynsym and .hash sections. We counted
6433 the number of dynamic symbols in elf_link_add_object_symbols.
6434 We will build the contents of .dynsym and .hash when we build
6435 the final symbol table, because until then we do not know the
6436 correct value to give the symbols. We built the .dynstr
6437 section as we went along in elf_link_add_object_symbols. */
3d4d4302 6438 s = bfd_get_linker_section (dynobj, ".dynsym");
5a580b3a 6439 BFD_ASSERT (s != NULL);
eea6121a 6440 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a
AM
6441
6442 if (dynsymcount != 0)
6443 {
a50b1753 6444 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
554220db
AM
6445 if (s->contents == NULL)
6446 return FALSE;
5a580b3a 6447
554220db
AM
6448 /* The first entry in .dynsym is a dummy symbol.
6449 Clear all the section syms, in case we don't output them all. */
6450 ++section_sym_count;
6451 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a
AM
6452 }
6453
fdc90cb4
JJ
6454 elf_hash_table (info)->bucketcount = 0;
6455
5a580b3a
AM
6456 /* Compute the size of the hashing table. As a side effect this
6457 computes the hash values for all the names we export. */
fdc90cb4
JJ
6458 if (info->emit_hash)
6459 {
6460 unsigned long int *hashcodes;
14b1c01e 6461 struct hash_codes_info hashinf;
fdc90cb4
JJ
6462 bfd_size_type amt;
6463 unsigned long int nsyms;
6464 size_t bucketcount;
6465 size_t hash_entry_size;
6466
6467 /* Compute the hash values for all exported symbols. At the same
6468 time store the values in an array so that we could use them for
6469 optimizations. */
6470 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6471 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6472 if (hashcodes == NULL)
6473 return FALSE;
14b1c01e
AM
6474 hashinf.hashcodes = hashcodes;
6475 hashinf.error = FALSE;
5a580b3a 6476
fdc90cb4
JJ
6477 /* Put all hash values in HASHCODES. */
6478 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6479 elf_collect_hash_codes, &hashinf);
6480 if (hashinf.error)
4dd07732
AM
6481 {
6482 free (hashcodes);
6483 return FALSE;
6484 }
5a580b3a 6485
14b1c01e 6486 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6487 bucketcount
6488 = compute_bucket_count (info, hashcodes, nsyms, 0);
6489 free (hashcodes);
6490
6491 if (bucketcount == 0)
6492 return FALSE;
5a580b3a 6493
fdc90cb4
JJ
6494 elf_hash_table (info)->bucketcount = bucketcount;
6495
3d4d4302 6496 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6497 BFD_ASSERT (s != NULL);
6498 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6499 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6500 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6501 if (s->contents == NULL)
6502 return FALSE;
6503
6504 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6505 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6506 s->contents + hash_entry_size);
6507 }
6508
6509 if (info->emit_gnu_hash)
6510 {
6511 size_t i, cnt;
6512 unsigned char *contents;
6513 struct collect_gnu_hash_codes cinfo;
6514 bfd_size_type amt;
6515 size_t bucketcount;
6516
6517 memset (&cinfo, 0, sizeof (cinfo));
6518
6519 /* Compute the hash values for all exported symbols. At the same
6520 time store the values in an array so that we could use them for
6521 optimizations. */
6522 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6523 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6524 if (cinfo.hashcodes == NULL)
6525 return FALSE;
6526
6527 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6528 cinfo.min_dynindx = -1;
6529 cinfo.output_bfd = output_bfd;
6530 cinfo.bed = bed;
6531
6532 /* Put all hash values in HASHCODES. */
6533 elf_link_hash_traverse (elf_hash_table (info),
6534 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6535 if (cinfo.error)
4dd07732
AM
6536 {
6537 free (cinfo.hashcodes);
6538 return FALSE;
6539 }
fdc90cb4
JJ
6540
6541 bucketcount
6542 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6543
6544 if (bucketcount == 0)
6545 {
6546 free (cinfo.hashcodes);
6547 return FALSE;
6548 }
6549
3d4d4302 6550 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6551 BFD_ASSERT (s != NULL);
6552
6553 if (cinfo.nsyms == 0)
6554 {
6555 /* Empty .gnu.hash section is special. */
6556 BFD_ASSERT (cinfo.min_dynindx == -1);
6557 free (cinfo.hashcodes);
6558 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6559 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6560 if (contents == NULL)
6561 return FALSE;
6562 s->contents = contents;
6563 /* 1 empty bucket. */
6564 bfd_put_32 (output_bfd, 1, contents);
6565 /* SYMIDX above the special symbol 0. */
6566 bfd_put_32 (output_bfd, 1, contents + 4);
6567 /* Just one word for bitmask. */
6568 bfd_put_32 (output_bfd, 1, contents + 8);
6569 /* Only hash fn bloom filter. */
6570 bfd_put_32 (output_bfd, 0, contents + 12);
6571 /* No hashes are valid - empty bitmask. */
6572 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6573 /* No hashes in the only bucket. */
6574 bfd_put_32 (output_bfd, 0,
6575 contents + 16 + bed->s->arch_size / 8);
6576 }
6577 else
6578 {
9e6619e2 6579 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6580 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6581
9e6619e2
AM
6582 x = cinfo.nsyms;
6583 maskbitslog2 = 1;
6584 while ((x >>= 1) != 0)
6585 ++maskbitslog2;
fdc90cb4
JJ
6586 if (maskbitslog2 < 3)
6587 maskbitslog2 = 5;
6588 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6589 maskbitslog2 = maskbitslog2 + 3;
6590 else
6591 maskbitslog2 = maskbitslog2 + 2;
6592 if (bed->s->arch_size == 64)
6593 {
6594 if (maskbitslog2 == 5)
6595 maskbitslog2 = 6;
6596 cinfo.shift1 = 6;
6597 }
6598 else
6599 cinfo.shift1 = 5;
6600 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6601 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6602 cinfo.maskbits = 1 << maskbitslog2;
6603 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6604 amt = bucketcount * sizeof (unsigned long int) * 2;
6605 amt += maskwords * sizeof (bfd_vma);
a50b1753 6606 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6607 if (cinfo.bitmask == NULL)
6608 {
6609 free (cinfo.hashcodes);
6610 return FALSE;
6611 }
6612
a50b1753 6613 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6614 cinfo.indx = cinfo.counts + bucketcount;
6615 cinfo.symindx = dynsymcount - cinfo.nsyms;
6616 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6617
6618 /* Determine how often each hash bucket is used. */
6619 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6620 for (i = 0; i < cinfo.nsyms; ++i)
6621 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6622
6623 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6624 if (cinfo.counts[i] != 0)
6625 {
6626 cinfo.indx[i] = cnt;
6627 cnt += cinfo.counts[i];
6628 }
6629 BFD_ASSERT (cnt == dynsymcount);
6630 cinfo.bucketcount = bucketcount;
6631 cinfo.local_indx = cinfo.min_dynindx;
6632
6633 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6634 s->size += cinfo.maskbits / 8;
a50b1753 6635 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6636 if (contents == NULL)
6637 {
6638 free (cinfo.bitmask);
6639 free (cinfo.hashcodes);
6640 return FALSE;
6641 }
6642
6643 s->contents = contents;
6644 bfd_put_32 (output_bfd, bucketcount, contents);
6645 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6646 bfd_put_32 (output_bfd, maskwords, contents + 8);
6647 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6648 contents += 16 + cinfo.maskbits / 8;
6649
6650 for (i = 0; i < bucketcount; ++i)
6651 {
6652 if (cinfo.counts[i] == 0)
6653 bfd_put_32 (output_bfd, 0, contents);
6654 else
6655 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6656 contents += 4;
6657 }
6658
6659 cinfo.contents = contents;
6660
6661 /* Renumber dynamic symbols, populate .gnu.hash section. */
6662 elf_link_hash_traverse (elf_hash_table (info),
6663 elf_renumber_gnu_hash_syms, &cinfo);
6664
6665 contents = s->contents + 16;
6666 for (i = 0; i < maskwords; ++i)
6667 {
6668 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6669 contents);
6670 contents += bed->s->arch_size / 8;
6671 }
6672
6673 free (cinfo.bitmask);
6674 free (cinfo.hashcodes);
6675 }
6676 }
5a580b3a 6677
3d4d4302 6678 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6679 BFD_ASSERT (s != NULL);
6680
4ad4eba5 6681 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6682
eea6121a 6683 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6684
6685 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6686 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6687 return FALSE;
6688 }
6689
6690 return TRUE;
6691}
4d269e42 6692\f
4d269e42
AM
6693/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6694
6695static void
6696merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6697 asection *sec)
6698{
dbaa2011
AM
6699 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6700 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
6701}
6702
6703/* Finish SHF_MERGE section merging. */
6704
6705bfd_boolean
6706_bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6707{
6708 bfd *ibfd;
6709 asection *sec;
6710
6711 if (!is_elf_hash_table (info->hash))
6712 return FALSE;
6713
6714 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6715 if ((ibfd->flags & DYNAMIC) == 0)
6716 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6717 if ((sec->flags & SEC_MERGE) != 0
6718 && !bfd_is_abs_section (sec->output_section))
6719 {
6720 struct bfd_elf_section_data *secdata;
6721
6722 secdata = elf_section_data (sec);
6723 if (! _bfd_add_merge_section (abfd,
6724 &elf_hash_table (info)->merge_info,
6725 sec, &secdata->sec_info))
6726 return FALSE;
6727 else if (secdata->sec_info)
dbaa2011 6728 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
6729 }
6730
6731 if (elf_hash_table (info)->merge_info != NULL)
6732 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6733 merge_sections_remove_hook);
6734 return TRUE;
6735}
6736
6737/* Create an entry in an ELF linker hash table. */
6738
6739struct bfd_hash_entry *
6740_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6741 struct bfd_hash_table *table,
6742 const char *string)
6743{
6744 /* Allocate the structure if it has not already been allocated by a
6745 subclass. */
6746 if (entry == NULL)
6747 {
a50b1753
NC
6748 entry = (struct bfd_hash_entry *)
6749 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6750 if (entry == NULL)
6751 return entry;
6752 }
6753
6754 /* Call the allocation method of the superclass. */
6755 entry = _bfd_link_hash_newfunc (entry, table, string);
6756 if (entry != NULL)
6757 {
6758 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6759 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6760
6761 /* Set local fields. */
6762 ret->indx = -1;
6763 ret->dynindx = -1;
6764 ret->got = htab->init_got_refcount;
6765 ret->plt = htab->init_plt_refcount;
6766 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6767 - offsetof (struct elf_link_hash_entry, size)));
6768 /* Assume that we have been called by a non-ELF symbol reader.
6769 This flag is then reset by the code which reads an ELF input
6770 file. This ensures that a symbol created by a non-ELF symbol
6771 reader will have the flag set correctly. */
6772 ret->non_elf = 1;
6773 }
6774
6775 return entry;
6776}
6777
6778/* Copy data from an indirect symbol to its direct symbol, hiding the
6779 old indirect symbol. Also used for copying flags to a weakdef. */
6780
6781void
6782_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6783 struct elf_link_hash_entry *dir,
6784 struct elf_link_hash_entry *ind)
6785{
6786 struct elf_link_hash_table *htab;
6787
6788 /* Copy down any references that we may have already seen to the
6789 symbol which just became indirect. */
6790
6791 dir->ref_dynamic |= ind->ref_dynamic;
6792 dir->ref_regular |= ind->ref_regular;
6793 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6794 dir->non_got_ref |= ind->non_got_ref;
6795 dir->needs_plt |= ind->needs_plt;
6796 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6797
6798 if (ind->root.type != bfd_link_hash_indirect)
6799 return;
6800
6801 /* Copy over the global and procedure linkage table refcount entries.
6802 These may have been already set up by a check_relocs routine. */
6803 htab = elf_hash_table (info);
6804 if (ind->got.refcount > htab->init_got_refcount.refcount)
6805 {
6806 if (dir->got.refcount < 0)
6807 dir->got.refcount = 0;
6808 dir->got.refcount += ind->got.refcount;
6809 ind->got.refcount = htab->init_got_refcount.refcount;
6810 }
6811
6812 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6813 {
6814 if (dir->plt.refcount < 0)
6815 dir->plt.refcount = 0;
6816 dir->plt.refcount += ind->plt.refcount;
6817 ind->plt.refcount = htab->init_plt_refcount.refcount;
6818 }
6819
6820 if (ind->dynindx != -1)
6821 {
6822 if (dir->dynindx != -1)
6823 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6824 dir->dynindx = ind->dynindx;
6825 dir->dynstr_index = ind->dynstr_index;
6826 ind->dynindx = -1;
6827 ind->dynstr_index = 0;
6828 }
6829}
6830
6831void
6832_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6833 struct elf_link_hash_entry *h,
6834 bfd_boolean force_local)
6835{
3aa14d16
L
6836 /* STT_GNU_IFUNC symbol must go through PLT. */
6837 if (h->type != STT_GNU_IFUNC)
6838 {
6839 h->plt = elf_hash_table (info)->init_plt_offset;
6840 h->needs_plt = 0;
6841 }
4d269e42
AM
6842 if (force_local)
6843 {
6844 h->forced_local = 1;
6845 if (h->dynindx != -1)
6846 {
6847 h->dynindx = -1;
6848 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6849 h->dynstr_index);
6850 }
6851 }
6852}
6853
7bf52ea2
AM
6854/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
6855 caller. */
4d269e42
AM
6856
6857bfd_boolean
6858_bfd_elf_link_hash_table_init
6859 (struct elf_link_hash_table *table,
6860 bfd *abfd,
6861 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6862 struct bfd_hash_table *,
6863 const char *),
4dfe6ac6
NC
6864 unsigned int entsize,
6865 enum elf_target_id target_id)
4d269e42
AM
6866{
6867 bfd_boolean ret;
6868 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6869
4d269e42
AM
6870 table->init_got_refcount.refcount = can_refcount - 1;
6871 table->init_plt_refcount.refcount = can_refcount - 1;
6872 table->init_got_offset.offset = -(bfd_vma) 1;
6873 table->init_plt_offset.offset = -(bfd_vma) 1;
6874 /* The first dynamic symbol is a dummy. */
6875 table->dynsymcount = 1;
6876
6877 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 6878
4d269e42 6879 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 6880 table->hash_table_id = target_id;
4d269e42
AM
6881
6882 return ret;
6883}
6884
6885/* Create an ELF linker hash table. */
6886
6887struct bfd_link_hash_table *
6888_bfd_elf_link_hash_table_create (bfd *abfd)
6889{
6890 struct elf_link_hash_table *ret;
6891 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6892
7bf52ea2 6893 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
6894 if (ret == NULL)
6895 return NULL;
6896
6897 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
6898 sizeof (struct elf_link_hash_entry),
6899 GENERIC_ELF_DATA))
4d269e42
AM
6900 {
6901 free (ret);
6902 return NULL;
6903 }
6904
6905 return &ret->root;
6906}
6907
9f7c3e5e
AM
6908/* Destroy an ELF linker hash table. */
6909
6910void
6911_bfd_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
6912{
6913 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) hash;
6914 if (htab->dynstr != NULL)
6915 _bfd_elf_strtab_free (htab->dynstr);
6916 _bfd_merge_sections_free (htab->merge_info);
6917 _bfd_generic_link_hash_table_free (hash);
6918}
6919
4d269e42
AM
6920/* This is a hook for the ELF emulation code in the generic linker to
6921 tell the backend linker what file name to use for the DT_NEEDED
6922 entry for a dynamic object. */
6923
6924void
6925bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6926{
6927 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6928 && bfd_get_format (abfd) == bfd_object)
6929 elf_dt_name (abfd) = name;
6930}
6931
6932int
6933bfd_elf_get_dyn_lib_class (bfd *abfd)
6934{
6935 int lib_class;
6936 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6937 && bfd_get_format (abfd) == bfd_object)
6938 lib_class = elf_dyn_lib_class (abfd);
6939 else
6940 lib_class = 0;
6941 return lib_class;
6942}
6943
6944void
6945bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6946{
6947 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6948 && bfd_get_format (abfd) == bfd_object)
6949 elf_dyn_lib_class (abfd) = lib_class;
6950}
6951
6952/* Get the list of DT_NEEDED entries for a link. This is a hook for
6953 the linker ELF emulation code. */
6954
6955struct bfd_link_needed_list *
6956bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6957 struct bfd_link_info *info)
6958{
6959 if (! is_elf_hash_table (info->hash))
6960 return NULL;
6961 return elf_hash_table (info)->needed;
6962}
6963
6964/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6965 hook for the linker ELF emulation code. */
6966
6967struct bfd_link_needed_list *
6968bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6969 struct bfd_link_info *info)
6970{
6971 if (! is_elf_hash_table (info->hash))
6972 return NULL;
6973 return elf_hash_table (info)->runpath;
6974}
6975
6976/* Get the name actually used for a dynamic object for a link. This
6977 is the SONAME entry if there is one. Otherwise, it is the string
6978 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6979
6980const char *
6981bfd_elf_get_dt_soname (bfd *abfd)
6982{
6983 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6984 && bfd_get_format (abfd) == bfd_object)
6985 return elf_dt_name (abfd);
6986 return NULL;
6987}
6988
6989/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6990 the ELF linker emulation code. */
6991
6992bfd_boolean
6993bfd_elf_get_bfd_needed_list (bfd *abfd,
6994 struct bfd_link_needed_list **pneeded)
6995{
6996 asection *s;
6997 bfd_byte *dynbuf = NULL;
cb33740c 6998 unsigned int elfsec;
4d269e42
AM
6999 unsigned long shlink;
7000 bfd_byte *extdyn, *extdynend;
7001 size_t extdynsize;
7002 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7003
7004 *pneeded = NULL;
7005
7006 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7007 || bfd_get_format (abfd) != bfd_object)
7008 return TRUE;
7009
7010 s = bfd_get_section_by_name (abfd, ".dynamic");
7011 if (s == NULL || s->size == 0)
7012 return TRUE;
7013
7014 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7015 goto error_return;
7016
7017 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7018 if (elfsec == SHN_BAD)
4d269e42
AM
7019 goto error_return;
7020
7021 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7022
4d269e42
AM
7023 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7024 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7025
7026 extdyn = dynbuf;
7027 extdynend = extdyn + s->size;
7028 for (; extdyn < extdynend; extdyn += extdynsize)
7029 {
7030 Elf_Internal_Dyn dyn;
7031
7032 (*swap_dyn_in) (abfd, extdyn, &dyn);
7033
7034 if (dyn.d_tag == DT_NULL)
7035 break;
7036
7037 if (dyn.d_tag == DT_NEEDED)
7038 {
7039 const char *string;
7040 struct bfd_link_needed_list *l;
7041 unsigned int tagv = dyn.d_un.d_val;
7042 bfd_size_type amt;
7043
7044 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7045 if (string == NULL)
7046 goto error_return;
7047
7048 amt = sizeof *l;
a50b1753 7049 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7050 if (l == NULL)
7051 goto error_return;
7052
7053 l->by = abfd;
7054 l->name = string;
7055 l->next = *pneeded;
7056 *pneeded = l;
7057 }
7058 }
7059
7060 free (dynbuf);
7061
7062 return TRUE;
7063
7064 error_return:
7065 if (dynbuf != NULL)
7066 free (dynbuf);
7067 return FALSE;
7068}
7069
7070struct elf_symbuf_symbol
7071{
7072 unsigned long st_name; /* Symbol name, index in string tbl */
7073 unsigned char st_info; /* Type and binding attributes */
7074 unsigned char st_other; /* Visibilty, and target specific */
7075};
7076
7077struct elf_symbuf_head
7078{
7079 struct elf_symbuf_symbol *ssym;
7080 bfd_size_type count;
7081 unsigned int st_shndx;
7082};
7083
7084struct elf_symbol
7085{
7086 union
7087 {
7088 Elf_Internal_Sym *isym;
7089 struct elf_symbuf_symbol *ssym;
7090 } u;
7091 const char *name;
7092};
7093
7094/* Sort references to symbols by ascending section number. */
7095
7096static int
7097elf_sort_elf_symbol (const void *arg1, const void *arg2)
7098{
7099 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7100 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7101
7102 return s1->st_shndx - s2->st_shndx;
7103}
7104
7105static int
7106elf_sym_name_compare (const void *arg1, const void *arg2)
7107{
7108 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7109 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7110 return strcmp (s1->name, s2->name);
7111}
7112
7113static struct elf_symbuf_head *
7114elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7115{
14b1c01e 7116 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7117 struct elf_symbuf_symbol *ssym;
7118 struct elf_symbuf_head *ssymbuf, *ssymhead;
3ae181ee 7119 bfd_size_type i, shndx_count, total_size;
4d269e42 7120
a50b1753 7121 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7122 if (indbuf == NULL)
7123 return NULL;
7124
7125 for (ind = indbuf, i = 0; i < symcount; i++)
7126 if (isymbuf[i].st_shndx != SHN_UNDEF)
7127 *ind++ = &isymbuf[i];
7128 indbufend = ind;
7129
7130 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7131 elf_sort_elf_symbol);
7132
7133 shndx_count = 0;
7134 if (indbufend > indbuf)
7135 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7136 if (ind[0]->st_shndx != ind[1]->st_shndx)
7137 shndx_count++;
7138
3ae181ee
L
7139 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7140 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7141 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7142 if (ssymbuf == NULL)
7143 {
7144 free (indbuf);
7145 return NULL;
7146 }
7147
3ae181ee 7148 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7149 ssymbuf->ssym = NULL;
7150 ssymbuf->count = shndx_count;
7151 ssymbuf->st_shndx = 0;
7152 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7153 {
7154 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7155 {
7156 ssymhead++;
7157 ssymhead->ssym = ssym;
7158 ssymhead->count = 0;
7159 ssymhead->st_shndx = (*ind)->st_shndx;
7160 }
7161 ssym->st_name = (*ind)->st_name;
7162 ssym->st_info = (*ind)->st_info;
7163 ssym->st_other = (*ind)->st_other;
7164 ssymhead->count++;
7165 }
3ae181ee
L
7166 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7167 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7168 == total_size));
4d269e42
AM
7169
7170 free (indbuf);
7171 return ssymbuf;
7172}
7173
7174/* Check if 2 sections define the same set of local and global
7175 symbols. */
7176
8f317e31 7177static bfd_boolean
4d269e42
AM
7178bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7179 struct bfd_link_info *info)
7180{
7181 bfd *bfd1, *bfd2;
7182 const struct elf_backend_data *bed1, *bed2;
7183 Elf_Internal_Shdr *hdr1, *hdr2;
7184 bfd_size_type symcount1, symcount2;
7185 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7186 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7187 Elf_Internal_Sym *isym, *isymend;
7188 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7189 bfd_size_type count1, count2, i;
cb33740c 7190 unsigned int shndx1, shndx2;
4d269e42
AM
7191 bfd_boolean result;
7192
7193 bfd1 = sec1->owner;
7194 bfd2 = sec2->owner;
7195
4d269e42
AM
7196 /* Both sections have to be in ELF. */
7197 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7198 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7199 return FALSE;
7200
7201 if (elf_section_type (sec1) != elf_section_type (sec2))
7202 return FALSE;
7203
4d269e42
AM
7204 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7205 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7206 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7207 return FALSE;
7208
7209 bed1 = get_elf_backend_data (bfd1);
7210 bed2 = get_elf_backend_data (bfd2);
7211 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7212 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7213 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7214 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7215
7216 if (symcount1 == 0 || symcount2 == 0)
7217 return FALSE;
7218
7219 result = FALSE;
7220 isymbuf1 = NULL;
7221 isymbuf2 = NULL;
a50b1753
NC
7222 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7223 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7224
7225 if (ssymbuf1 == NULL)
7226 {
7227 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7228 NULL, NULL, NULL);
7229 if (isymbuf1 == NULL)
7230 goto done;
7231
7232 if (!info->reduce_memory_overheads)
7233 elf_tdata (bfd1)->symbuf = ssymbuf1
7234 = elf_create_symbuf (symcount1, isymbuf1);
7235 }
7236
7237 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7238 {
7239 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7240 NULL, NULL, NULL);
7241 if (isymbuf2 == NULL)
7242 goto done;
7243
7244 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7245 elf_tdata (bfd2)->symbuf = ssymbuf2
7246 = elf_create_symbuf (symcount2, isymbuf2);
7247 }
7248
7249 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7250 {
7251 /* Optimized faster version. */
7252 bfd_size_type lo, hi, mid;
7253 struct elf_symbol *symp;
7254 struct elf_symbuf_symbol *ssym, *ssymend;
7255
7256 lo = 0;
7257 hi = ssymbuf1->count;
7258 ssymbuf1++;
7259 count1 = 0;
7260 while (lo < hi)
7261 {
7262 mid = (lo + hi) / 2;
cb33740c 7263 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7264 hi = mid;
cb33740c 7265 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7266 lo = mid + 1;
7267 else
7268 {
7269 count1 = ssymbuf1[mid].count;
7270 ssymbuf1 += mid;
7271 break;
7272 }
7273 }
7274
7275 lo = 0;
7276 hi = ssymbuf2->count;
7277 ssymbuf2++;
7278 count2 = 0;
7279 while (lo < hi)
7280 {
7281 mid = (lo + hi) / 2;
cb33740c 7282 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7283 hi = mid;
cb33740c 7284 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7285 lo = mid + 1;
7286 else
7287 {
7288 count2 = ssymbuf2[mid].count;
7289 ssymbuf2 += mid;
7290 break;
7291 }
7292 }
7293
7294 if (count1 == 0 || count2 == 0 || count1 != count2)
7295 goto done;
7296
a50b1753
NC
7297 symtable1 = (struct elf_symbol *)
7298 bfd_malloc (count1 * sizeof (struct elf_symbol));
7299 symtable2 = (struct elf_symbol *)
7300 bfd_malloc (count2 * sizeof (struct elf_symbol));
4d269e42
AM
7301 if (symtable1 == NULL || symtable2 == NULL)
7302 goto done;
7303
7304 symp = symtable1;
7305 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7306 ssym < ssymend; ssym++, symp++)
7307 {
7308 symp->u.ssym = ssym;
7309 symp->name = bfd_elf_string_from_elf_section (bfd1,
7310 hdr1->sh_link,
7311 ssym->st_name);
7312 }
7313
7314 symp = symtable2;
7315 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7316 ssym < ssymend; ssym++, symp++)
7317 {
7318 symp->u.ssym = ssym;
7319 symp->name = bfd_elf_string_from_elf_section (bfd2,
7320 hdr2->sh_link,
7321 ssym->st_name);
7322 }
7323
7324 /* Sort symbol by name. */
7325 qsort (symtable1, count1, sizeof (struct elf_symbol),
7326 elf_sym_name_compare);
7327 qsort (symtable2, count1, sizeof (struct elf_symbol),
7328 elf_sym_name_compare);
7329
7330 for (i = 0; i < count1; i++)
7331 /* Two symbols must have the same binding, type and name. */
7332 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7333 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7334 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7335 goto done;
7336
7337 result = TRUE;
7338 goto done;
7339 }
7340
a50b1753
NC
7341 symtable1 = (struct elf_symbol *)
7342 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7343 symtable2 = (struct elf_symbol *)
7344 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7345 if (symtable1 == NULL || symtable2 == NULL)
7346 goto done;
7347
7348 /* Count definitions in the section. */
7349 count1 = 0;
7350 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7351 if (isym->st_shndx == shndx1)
4d269e42
AM
7352 symtable1[count1++].u.isym = isym;
7353
7354 count2 = 0;
7355 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7356 if (isym->st_shndx == shndx2)
4d269e42
AM
7357 symtable2[count2++].u.isym = isym;
7358
7359 if (count1 == 0 || count2 == 0 || count1 != count2)
7360 goto done;
7361
7362 for (i = 0; i < count1; i++)
7363 symtable1[i].name
7364 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7365 symtable1[i].u.isym->st_name);
7366
7367 for (i = 0; i < count2; i++)
7368 symtable2[i].name
7369 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7370 symtable2[i].u.isym->st_name);
7371
7372 /* Sort symbol by name. */
7373 qsort (symtable1, count1, sizeof (struct elf_symbol),
7374 elf_sym_name_compare);
7375 qsort (symtable2, count1, sizeof (struct elf_symbol),
7376 elf_sym_name_compare);
7377
7378 for (i = 0; i < count1; i++)
7379 /* Two symbols must have the same binding, type and name. */
7380 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7381 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7382 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7383 goto done;
7384
7385 result = TRUE;
7386
7387done:
7388 if (symtable1)
7389 free (symtable1);
7390 if (symtable2)
7391 free (symtable2);
7392 if (isymbuf1)
7393 free (isymbuf1);
7394 if (isymbuf2)
7395 free (isymbuf2);
7396
7397 return result;
7398}
7399
7400/* Return TRUE if 2 section types are compatible. */
7401
7402bfd_boolean
7403_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7404 bfd *bbfd, const asection *bsec)
7405{
7406 if (asec == NULL
7407 || bsec == NULL
7408 || abfd->xvec->flavour != bfd_target_elf_flavour
7409 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7410 return TRUE;
7411
7412 return elf_section_type (asec) == elf_section_type (bsec);
7413}
7414\f
c152c796
AM
7415/* Final phase of ELF linker. */
7416
7417/* A structure we use to avoid passing large numbers of arguments. */
7418
7419struct elf_final_link_info
7420{
7421 /* General link information. */
7422 struct bfd_link_info *info;
7423 /* Output BFD. */
7424 bfd *output_bfd;
7425 /* Symbol string table. */
7426 struct bfd_strtab_hash *symstrtab;
7427 /* .dynsym section. */
7428 asection *dynsym_sec;
7429 /* .hash section. */
7430 asection *hash_sec;
7431 /* symbol version section (.gnu.version). */
7432 asection *symver_sec;
7433 /* Buffer large enough to hold contents of any section. */
7434 bfd_byte *contents;
7435 /* Buffer large enough to hold external relocs of any section. */
7436 void *external_relocs;
7437 /* Buffer large enough to hold internal relocs of any section. */
7438 Elf_Internal_Rela *internal_relocs;
7439 /* Buffer large enough to hold external local symbols of any input
7440 BFD. */
7441 bfd_byte *external_syms;
7442 /* And a buffer for symbol section indices. */
7443 Elf_External_Sym_Shndx *locsym_shndx;
7444 /* Buffer large enough to hold internal local symbols of any input
7445 BFD. */
7446 Elf_Internal_Sym *internal_syms;
7447 /* Array large enough to hold a symbol index for each local symbol
7448 of any input BFD. */
7449 long *indices;
7450 /* Array large enough to hold a section pointer for each local
7451 symbol of any input BFD. */
7452 asection **sections;
7453 /* Buffer to hold swapped out symbols. */
7454 bfd_byte *symbuf;
7455 /* And one for symbol section indices. */
7456 Elf_External_Sym_Shndx *symshndxbuf;
7457 /* Number of swapped out symbols in buffer. */
7458 size_t symbuf_count;
7459 /* Number of symbols which fit in symbuf. */
7460 size_t symbuf_size;
7461 /* And same for symshndxbuf. */
7462 size_t shndxbuf_size;
ffbc01cc
AM
7463 /* Number of STT_FILE syms seen. */
7464 size_t filesym_count;
c152c796
AM
7465};
7466
7467/* This struct is used to pass information to elf_link_output_extsym. */
7468
7469struct elf_outext_info
7470{
7471 bfd_boolean failed;
7472 bfd_boolean localsyms;
ffbc01cc
AM
7473 bfd_boolean need_second_pass;
7474 bfd_boolean second_pass;
34a79995 7475 bfd_boolean file_sym_done;
8b127cbc 7476 struct elf_final_link_info *flinfo;
c152c796
AM
7477};
7478
d9352518
DB
7479
7480/* Support for evaluating a complex relocation.
7481
7482 Complex relocations are generalized, self-describing relocations. The
7483 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7484 relocations themselves.
d9352518
DB
7485
7486 The relocations are use a reserved elf-wide relocation type code (R_RELC
7487 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7488 information (start bit, end bit, word width, etc) into the addend. This
7489 information is extracted from CGEN-generated operand tables within gas.
7490
7491 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7492 internal) representing prefix-notation expressions, including but not
7493 limited to those sorts of expressions normally encoded as addends in the
7494 addend field. The symbol mangling format is:
7495
7496 <node> := <literal>
7497 | <unary-operator> ':' <node>
7498 | <binary-operator> ':' <node> ':' <node>
7499 ;
7500
7501 <literal> := 's' <digits=N> ':' <N character symbol name>
7502 | 'S' <digits=N> ':' <N character section name>
7503 | '#' <hexdigits>
7504 ;
7505
7506 <binary-operator> := as in C
7507 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7508
7509static void
a0c8462f
AM
7510set_symbol_value (bfd *bfd_with_globals,
7511 Elf_Internal_Sym *isymbuf,
7512 size_t locsymcount,
7513 size_t symidx,
7514 bfd_vma val)
d9352518 7515{
8977835c
AM
7516 struct elf_link_hash_entry **sym_hashes;
7517 struct elf_link_hash_entry *h;
7518 size_t extsymoff = locsymcount;
d9352518 7519
8977835c 7520 if (symidx < locsymcount)
d9352518 7521 {
8977835c
AM
7522 Elf_Internal_Sym *sym;
7523
7524 sym = isymbuf + symidx;
7525 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7526 {
7527 /* It is a local symbol: move it to the
7528 "absolute" section and give it a value. */
7529 sym->st_shndx = SHN_ABS;
7530 sym->st_value = val;
7531 return;
7532 }
7533 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7534 extsymoff = 0;
d9352518 7535 }
8977835c
AM
7536
7537 /* It is a global symbol: set its link type
7538 to "defined" and give it a value. */
7539
7540 sym_hashes = elf_sym_hashes (bfd_with_globals);
7541 h = sym_hashes [symidx - extsymoff];
7542 while (h->root.type == bfd_link_hash_indirect
7543 || h->root.type == bfd_link_hash_warning)
7544 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7545 h->root.type = bfd_link_hash_defined;
7546 h->root.u.def.value = val;
7547 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7548}
7549
a0c8462f
AM
7550static bfd_boolean
7551resolve_symbol (const char *name,
7552 bfd *input_bfd,
8b127cbc 7553 struct elf_final_link_info *flinfo,
a0c8462f
AM
7554 bfd_vma *result,
7555 Elf_Internal_Sym *isymbuf,
7556 size_t locsymcount)
d9352518 7557{
a0c8462f
AM
7558 Elf_Internal_Sym *sym;
7559 struct bfd_link_hash_entry *global_entry;
7560 const char *candidate = NULL;
7561 Elf_Internal_Shdr *symtab_hdr;
7562 size_t i;
7563
d9352518
DB
7564 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7565
7566 for (i = 0; i < locsymcount; ++ i)
7567 {
8977835c 7568 sym = isymbuf + i;
d9352518
DB
7569
7570 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7571 continue;
7572
7573 candidate = bfd_elf_string_from_elf_section (input_bfd,
7574 symtab_hdr->sh_link,
7575 sym->st_name);
7576#ifdef DEBUG
0f02bbd9
AM
7577 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7578 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7579#endif
7580 if (candidate && strcmp (candidate, name) == 0)
7581 {
8b127cbc 7582 asection *sec = flinfo->sections [i];
d9352518 7583
0f02bbd9
AM
7584 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7585 *result += sec->output_offset + sec->output_section->vma;
d9352518 7586#ifdef DEBUG
0f02bbd9
AM
7587 printf ("Found symbol with value %8.8lx\n",
7588 (unsigned long) *result);
d9352518
DB
7589#endif
7590 return TRUE;
7591 }
7592 }
7593
7594 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7595 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7596 FALSE, FALSE, TRUE);
d9352518
DB
7597 if (!global_entry)
7598 return FALSE;
a0c8462f 7599
d9352518
DB
7600 if (global_entry->type == bfd_link_hash_defined
7601 || global_entry->type == bfd_link_hash_defweak)
7602 {
a0c8462f
AM
7603 *result = (global_entry->u.def.value
7604 + global_entry->u.def.section->output_section->vma
7605 + global_entry->u.def.section->output_offset);
d9352518 7606#ifdef DEBUG
0f02bbd9
AM
7607 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7608 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7609#endif
7610 return TRUE;
a0c8462f 7611 }
d9352518 7612
d9352518
DB
7613 return FALSE;
7614}
7615
7616static bfd_boolean
a0c8462f
AM
7617resolve_section (const char *name,
7618 asection *sections,
7619 bfd_vma *result)
d9352518 7620{
a0c8462f
AM
7621 asection *curr;
7622 unsigned int len;
d9352518 7623
a0c8462f 7624 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7625 if (strcmp (curr->name, name) == 0)
7626 {
7627 *result = curr->vma;
7628 return TRUE;
7629 }
7630
7631 /* Hmm. still haven't found it. try pseudo-section names. */
a0c8462f 7632 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7633 {
7634 len = strlen (curr->name);
a0c8462f 7635 if (len > strlen (name))
d9352518
DB
7636 continue;
7637
7638 if (strncmp (curr->name, name, len) == 0)
7639 {
7640 if (strncmp (".end", name + len, 4) == 0)
7641 {
7642 *result = curr->vma + curr->size;
7643 return TRUE;
7644 }
7645
7646 /* Insert more pseudo-section names here, if you like. */
7647 }
7648 }
a0c8462f 7649
d9352518
DB
7650 return FALSE;
7651}
7652
7653static void
a0c8462f 7654undefined_reference (const char *reftype, const char *name)
d9352518 7655{
a0c8462f
AM
7656 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7657 reftype, name);
d9352518
DB
7658}
7659
7660static bfd_boolean
a0c8462f
AM
7661eval_symbol (bfd_vma *result,
7662 const char **symp,
7663 bfd *input_bfd,
8b127cbc 7664 struct elf_final_link_info *flinfo,
a0c8462f
AM
7665 bfd_vma dot,
7666 Elf_Internal_Sym *isymbuf,
7667 size_t locsymcount,
7668 int signed_p)
d9352518 7669{
4b93929b
NC
7670 size_t len;
7671 size_t symlen;
a0c8462f
AM
7672 bfd_vma a;
7673 bfd_vma b;
4b93929b 7674 char symbuf[4096];
0f02bbd9 7675 const char *sym = *symp;
a0c8462f
AM
7676 const char *symend;
7677 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7678
7679 len = strlen (sym);
7680 symend = sym + len;
7681
4b93929b 7682 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7683 {
7684 bfd_set_error (bfd_error_invalid_operation);
7685 return FALSE;
7686 }
a0c8462f 7687
d9352518
DB
7688 switch (* sym)
7689 {
7690 case '.':
0f02bbd9
AM
7691 *result = dot;
7692 *symp = sym + 1;
d9352518
DB
7693 return TRUE;
7694
7695 case '#':
0f02bbd9
AM
7696 ++sym;
7697 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7698 return TRUE;
7699
7700 case 'S':
7701 symbol_is_section = TRUE;
a0c8462f 7702 case 's':
0f02bbd9
AM
7703 ++sym;
7704 symlen = strtol (sym, (char **) symp, 10);
7705 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7706
4b93929b 7707 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7708 {
7709 bfd_set_error (bfd_error_invalid_operation);
7710 return FALSE;
7711 }
7712
7713 memcpy (symbuf, sym, symlen);
a0c8462f 7714 symbuf[symlen] = '\0';
0f02bbd9 7715 *symp = sym + symlen;
a0c8462f
AM
7716
7717 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7718 the symbol as a section, or vice-versa. so we're pretty liberal in our
7719 interpretation here; section means "try section first", not "must be a
7720 section", and likewise with symbol. */
7721
a0c8462f 7722 if (symbol_is_section)
d9352518 7723 {
8b127cbc
AM
7724 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7725 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7726 isymbuf, locsymcount))
d9352518
DB
7727 {
7728 undefined_reference ("section", symbuf);
7729 return FALSE;
7730 }
a0c8462f
AM
7731 }
7732 else
d9352518 7733 {
8b127cbc 7734 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7735 isymbuf, locsymcount)
8b127cbc 7736 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8977835c 7737 result))
d9352518
DB
7738 {
7739 undefined_reference ("symbol", symbuf);
7740 return FALSE;
7741 }
7742 }
7743
7744 return TRUE;
a0c8462f 7745
d9352518
DB
7746 /* All that remains are operators. */
7747
7748#define UNARY_OP(op) \
7749 if (strncmp (sym, #op, strlen (#op)) == 0) \
7750 { \
7751 sym += strlen (#op); \
a0c8462f
AM
7752 if (*sym == ':') \
7753 ++sym; \
0f02bbd9 7754 *symp = sym; \
8b127cbc 7755 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7756 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7757 return FALSE; \
7758 if (signed_p) \
0f02bbd9 7759 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7760 else \
7761 *result = op a; \
d9352518
DB
7762 return TRUE; \
7763 }
7764
7765#define BINARY_OP(op) \
7766 if (strncmp (sym, #op, strlen (#op)) == 0) \
7767 { \
7768 sym += strlen (#op); \
a0c8462f
AM
7769 if (*sym == ':') \
7770 ++sym; \
0f02bbd9 7771 *symp = sym; \
8b127cbc 7772 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7773 isymbuf, locsymcount, signed_p)) \
a0c8462f 7774 return FALSE; \
0f02bbd9 7775 ++*symp; \
8b127cbc 7776 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 7777 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7778 return FALSE; \
7779 if (signed_p) \
0f02bbd9 7780 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
7781 else \
7782 *result = a op b; \
d9352518
DB
7783 return TRUE; \
7784 }
7785
7786 default:
7787 UNARY_OP (0-);
7788 BINARY_OP (<<);
7789 BINARY_OP (>>);
7790 BINARY_OP (==);
7791 BINARY_OP (!=);
7792 BINARY_OP (<=);
7793 BINARY_OP (>=);
7794 BINARY_OP (&&);
7795 BINARY_OP (||);
7796 UNARY_OP (~);
7797 UNARY_OP (!);
7798 BINARY_OP (*);
7799 BINARY_OP (/);
7800 BINARY_OP (%);
7801 BINARY_OP (^);
7802 BINARY_OP (|);
7803 BINARY_OP (&);
7804 BINARY_OP (+);
7805 BINARY_OP (-);
7806 BINARY_OP (<);
7807 BINARY_OP (>);
7808#undef UNARY_OP
7809#undef BINARY_OP
7810 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7811 bfd_set_error (bfd_error_invalid_operation);
7812 return FALSE;
7813 }
7814}
7815
d9352518 7816static void
a0c8462f
AM
7817put_value (bfd_vma size,
7818 unsigned long chunksz,
7819 bfd *input_bfd,
7820 bfd_vma x,
7821 bfd_byte *location)
d9352518
DB
7822{
7823 location += (size - chunksz);
7824
a0c8462f 7825 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
d9352518
DB
7826 {
7827 switch (chunksz)
7828 {
7829 default:
7830 case 0:
7831 abort ();
7832 case 1:
7833 bfd_put_8 (input_bfd, x, location);
7834 break;
7835 case 2:
7836 bfd_put_16 (input_bfd, x, location);
7837 break;
7838 case 4:
7839 bfd_put_32 (input_bfd, x, location);
7840 break;
7841 case 8:
7842#ifdef BFD64
7843 bfd_put_64 (input_bfd, x, location);
7844#else
7845 abort ();
7846#endif
7847 break;
7848 }
7849 }
7850}
7851
a0c8462f
AM
7852static bfd_vma
7853get_value (bfd_vma size,
7854 unsigned long chunksz,
7855 bfd *input_bfd,
7856 bfd_byte *location)
d9352518 7857{
9b239e0e 7858 int shift;
d9352518
DB
7859 bfd_vma x = 0;
7860
9b239e0e
NC
7861 /* Sanity checks. */
7862 BFD_ASSERT (chunksz <= sizeof (x)
7863 && size >= chunksz
7864 && chunksz != 0
7865 && (size % chunksz) == 0
7866 && input_bfd != NULL
7867 && location != NULL);
7868
7869 if (chunksz == sizeof (x))
7870 {
7871 BFD_ASSERT (size == chunksz);
7872
7873 /* Make sure that we do not perform an undefined shift operation.
7874 We know that size == chunksz so there will only be one iteration
7875 of the loop below. */
7876 shift = 0;
7877 }
7878 else
7879 shift = 8 * chunksz;
7880
a0c8462f 7881 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
7882 {
7883 switch (chunksz)
7884 {
d9352518 7885 case 1:
9b239e0e 7886 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
7887 break;
7888 case 2:
9b239e0e 7889 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
7890 break;
7891 case 4:
9b239e0e 7892 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 7893 break;
d9352518 7894#ifdef BFD64
9b239e0e
NC
7895 case 8:
7896 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 7897 break;
9b239e0e
NC
7898#endif
7899 default:
7900 abort ();
d9352518
DB
7901 }
7902 }
7903 return x;
7904}
7905
a0c8462f
AM
7906static void
7907decode_complex_addend (unsigned long *start, /* in bits */
7908 unsigned long *oplen, /* in bits */
7909 unsigned long *len, /* in bits */
7910 unsigned long *wordsz, /* in bytes */
7911 unsigned long *chunksz, /* in bytes */
7912 unsigned long *lsb0_p,
7913 unsigned long *signed_p,
7914 unsigned long *trunc_p,
7915 unsigned long encoded)
d9352518
DB
7916{
7917 * start = encoded & 0x3F;
7918 * len = (encoded >> 6) & 0x3F;
7919 * oplen = (encoded >> 12) & 0x3F;
7920 * wordsz = (encoded >> 18) & 0xF;
7921 * chunksz = (encoded >> 22) & 0xF;
7922 * lsb0_p = (encoded >> 27) & 1;
7923 * signed_p = (encoded >> 28) & 1;
7924 * trunc_p = (encoded >> 29) & 1;
7925}
7926
cdfeee4f 7927bfd_reloc_status_type
0f02bbd9 7928bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 7929 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
7930 bfd_byte *contents,
7931 Elf_Internal_Rela *rel,
7932 bfd_vma relocation)
d9352518 7933{
0f02bbd9
AM
7934 bfd_vma shift, x, mask;
7935 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 7936 bfd_reloc_status_type r;
d9352518
DB
7937
7938 /* Perform this reloc, since it is complex.
7939 (this is not to say that it necessarily refers to a complex
7940 symbol; merely that it is a self-describing CGEN based reloc.
7941 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 7942 word size, etc) encoded within it.). */
d9352518 7943
a0c8462f
AM
7944 decode_complex_addend (&start, &oplen, &len, &wordsz,
7945 &chunksz, &lsb0_p, &signed_p,
7946 &trunc_p, rel->r_addend);
d9352518
DB
7947
7948 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7949
7950 if (lsb0_p)
7951 shift = (start + 1) - len;
7952 else
7953 shift = (8 * wordsz) - (start + len);
7954
5dabe785 7955 /* FIXME: octets_per_byte. */
a0c8462f 7956 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
d9352518
DB
7957
7958#ifdef DEBUG
7959 printf ("Doing complex reloc: "
7960 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7961 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7962 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7963 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
7964 oplen, (unsigned long) x, (unsigned long) mask,
7965 (unsigned long) relocation);
d9352518
DB
7966#endif
7967
cdfeee4f 7968 r = bfd_reloc_ok;
d9352518 7969 if (! trunc_p)
cdfeee4f
AM
7970 /* Now do an overflow check. */
7971 r = bfd_check_overflow ((signed_p
7972 ? complain_overflow_signed
7973 : complain_overflow_unsigned),
7974 len, 0, (8 * wordsz),
7975 relocation);
a0c8462f 7976
d9352518
DB
7977 /* Do the deed. */
7978 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7979
7980#ifdef DEBUG
7981 printf (" relocation: %8.8lx\n"
7982 " shifted mask: %8.8lx\n"
7983 " shifted/masked reloc: %8.8lx\n"
7984 " result: %8.8lx\n",
9ccb8af9
AM
7985 (unsigned long) relocation, (unsigned long) (mask << shift),
7986 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 7987#endif
5dabe785 7988 /* FIXME: octets_per_byte. */
d9352518 7989 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
cdfeee4f 7990 return r;
d9352518
DB
7991}
7992
c152c796
AM
7993/* When performing a relocatable link, the input relocations are
7994 preserved. But, if they reference global symbols, the indices
d4730f92
BS
7995 referenced must be updated. Update all the relocations found in
7996 RELDATA. */
c152c796
AM
7997
7998static void
7999elf_link_adjust_relocs (bfd *abfd,
d4730f92 8000 struct bfd_elf_section_reloc_data *reldata)
c152c796
AM
8001{
8002 unsigned int i;
8003 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8004 bfd_byte *erela;
8005 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8006 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8007 bfd_vma r_type_mask;
8008 int r_sym_shift;
d4730f92
BS
8009 unsigned int count = reldata->count;
8010 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8011
d4730f92 8012 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8013 {
8014 swap_in = bed->s->swap_reloc_in;
8015 swap_out = bed->s->swap_reloc_out;
8016 }
d4730f92 8017 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8018 {
8019 swap_in = bed->s->swap_reloca_in;
8020 swap_out = bed->s->swap_reloca_out;
8021 }
8022 else
8023 abort ();
8024
8025 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8026 abort ();
8027
8028 if (bed->s->arch_size == 32)
8029 {
8030 r_type_mask = 0xff;
8031 r_sym_shift = 8;
8032 }
8033 else
8034 {
8035 r_type_mask = 0xffffffff;
8036 r_sym_shift = 32;
8037 }
8038
d4730f92
BS
8039 erela = reldata->hdr->contents;
8040 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8041 {
8042 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8043 unsigned int j;
8044
8045 if (*rel_hash == NULL)
8046 continue;
8047
8048 BFD_ASSERT ((*rel_hash)->indx >= 0);
8049
8050 (*swap_in) (abfd, erela, irela);
8051 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8052 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8053 | (irela[j].r_info & r_type_mask));
8054 (*swap_out) (abfd, irela, erela);
8055 }
8056}
8057
8058struct elf_link_sort_rela
8059{
8060 union {
8061 bfd_vma offset;
8062 bfd_vma sym_mask;
8063 } u;
8064 enum elf_reloc_type_class type;
8065 /* We use this as an array of size int_rels_per_ext_rel. */
8066 Elf_Internal_Rela rela[1];
8067};
8068
8069static int
8070elf_link_sort_cmp1 (const void *A, const void *B)
8071{
a50b1753
NC
8072 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8073 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8074 int relativea, relativeb;
8075
8076 relativea = a->type == reloc_class_relative;
8077 relativeb = b->type == reloc_class_relative;
8078
8079 if (relativea < relativeb)
8080 return 1;
8081 if (relativea > relativeb)
8082 return -1;
8083 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8084 return -1;
8085 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8086 return 1;
8087 if (a->rela->r_offset < b->rela->r_offset)
8088 return -1;
8089 if (a->rela->r_offset > b->rela->r_offset)
8090 return 1;
8091 return 0;
8092}
8093
8094static int
8095elf_link_sort_cmp2 (const void *A, const void *B)
8096{
a50b1753
NC
8097 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8098 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8099
7e612e98 8100 if (a->type < b->type)
c152c796 8101 return -1;
7e612e98 8102 if (a->type > b->type)
c152c796 8103 return 1;
7e612e98 8104 if (a->u.offset < b->u.offset)
c152c796 8105 return -1;
7e612e98 8106 if (a->u.offset > b->u.offset)
c152c796
AM
8107 return 1;
8108 if (a->rela->r_offset < b->rela->r_offset)
8109 return -1;
8110 if (a->rela->r_offset > b->rela->r_offset)
8111 return 1;
8112 return 0;
8113}
8114
8115static size_t
8116elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8117{
3410fea8 8118 asection *dynamic_relocs;
fc66a176
L
8119 asection *rela_dyn;
8120 asection *rel_dyn;
c152c796
AM
8121 bfd_size_type count, size;
8122 size_t i, ret, sort_elt, ext_size;
8123 bfd_byte *sort, *s_non_relative, *p;
8124 struct elf_link_sort_rela *sq;
8125 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8126 int i2e = bed->s->int_rels_per_ext_rel;
8127 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8128 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8129 struct bfd_link_order *lo;
8130 bfd_vma r_sym_mask;
3410fea8 8131 bfd_boolean use_rela;
c152c796 8132
3410fea8
NC
8133 /* Find a dynamic reloc section. */
8134 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8135 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8136 if (rela_dyn != NULL && rela_dyn->size > 0
8137 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8138 {
3410fea8
NC
8139 bfd_boolean use_rela_initialised = FALSE;
8140
8141 /* This is just here to stop gcc from complaining.
8142 It's initialization checking code is not perfect. */
8143 use_rela = TRUE;
8144
8145 /* Both sections are present. Examine the sizes
8146 of the indirect sections to help us choose. */
8147 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8148 if (lo->type == bfd_indirect_link_order)
8149 {
8150 asection *o = lo->u.indirect.section;
8151
8152 if ((o->size % bed->s->sizeof_rela) == 0)
8153 {
8154 if ((o->size % bed->s->sizeof_rel) == 0)
8155 /* Section size is divisible by both rel and rela sizes.
8156 It is of no help to us. */
8157 ;
8158 else
8159 {
8160 /* Section size is only divisible by rela. */
8161 if (use_rela_initialised && (use_rela == FALSE))
8162 {
8163 _bfd_error_handler
8164 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8165 bfd_set_error (bfd_error_invalid_operation);
8166 return 0;
8167 }
8168 else
8169 {
8170 use_rela = TRUE;
8171 use_rela_initialised = TRUE;
8172 }
8173 }
8174 }
8175 else if ((o->size % bed->s->sizeof_rel) == 0)
8176 {
8177 /* Section size is only divisible by rel. */
8178 if (use_rela_initialised && (use_rela == TRUE))
8179 {
8180 _bfd_error_handler
8181 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8182 bfd_set_error (bfd_error_invalid_operation);
8183 return 0;
8184 }
8185 else
8186 {
8187 use_rela = FALSE;
8188 use_rela_initialised = TRUE;
8189 }
8190 }
8191 else
8192 {
8193 /* The section size is not divisible by either - something is wrong. */
8194 _bfd_error_handler
8195 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8196 bfd_set_error (bfd_error_invalid_operation);
8197 return 0;
8198 }
8199 }
8200
8201 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8202 if (lo->type == bfd_indirect_link_order)
8203 {
8204 asection *o = lo->u.indirect.section;
8205
8206 if ((o->size % bed->s->sizeof_rela) == 0)
8207 {
8208 if ((o->size % bed->s->sizeof_rel) == 0)
8209 /* Section size is divisible by both rel and rela sizes.
8210 It is of no help to us. */
8211 ;
8212 else
8213 {
8214 /* Section size is only divisible by rela. */
8215 if (use_rela_initialised && (use_rela == FALSE))
8216 {
8217 _bfd_error_handler
8218 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8219 bfd_set_error (bfd_error_invalid_operation);
8220 return 0;
8221 }
8222 else
8223 {
8224 use_rela = TRUE;
8225 use_rela_initialised = TRUE;
8226 }
8227 }
8228 }
8229 else if ((o->size % bed->s->sizeof_rel) == 0)
8230 {
8231 /* Section size is only divisible by rel. */
8232 if (use_rela_initialised && (use_rela == TRUE))
8233 {
8234 _bfd_error_handler
8235 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8236 bfd_set_error (bfd_error_invalid_operation);
8237 return 0;
8238 }
8239 else
8240 {
8241 use_rela = FALSE;
8242 use_rela_initialised = TRUE;
8243 }
8244 }
8245 else
8246 {
8247 /* The section size is not divisible by either - something is wrong. */
8248 _bfd_error_handler
8249 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8250 bfd_set_error (bfd_error_invalid_operation);
8251 return 0;
8252 }
8253 }
8254
8255 if (! use_rela_initialised)
8256 /* Make a guess. */
8257 use_rela = TRUE;
c152c796 8258 }
fc66a176
L
8259 else if (rela_dyn != NULL && rela_dyn->size > 0)
8260 use_rela = TRUE;
8261 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8262 use_rela = FALSE;
c152c796 8263 else
fc66a176 8264 return 0;
3410fea8
NC
8265
8266 if (use_rela)
c152c796 8267 {
3410fea8 8268 dynamic_relocs = rela_dyn;
c152c796
AM
8269 ext_size = bed->s->sizeof_rela;
8270 swap_in = bed->s->swap_reloca_in;
8271 swap_out = bed->s->swap_reloca_out;
8272 }
3410fea8
NC
8273 else
8274 {
8275 dynamic_relocs = rel_dyn;
8276 ext_size = bed->s->sizeof_rel;
8277 swap_in = bed->s->swap_reloc_in;
8278 swap_out = bed->s->swap_reloc_out;
8279 }
c152c796
AM
8280
8281 size = 0;
3410fea8 8282 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8283 if (lo->type == bfd_indirect_link_order)
3410fea8 8284 size += lo->u.indirect.section->size;
c152c796 8285
3410fea8 8286 if (size != dynamic_relocs->size)
c152c796
AM
8287 return 0;
8288
8289 sort_elt = (sizeof (struct elf_link_sort_rela)
8290 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8291
8292 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8293 if (count == 0)
8294 return 0;
a50b1753 8295 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8296
c152c796
AM
8297 if (sort == NULL)
8298 {
8299 (*info->callbacks->warning)
8300 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8301 return 0;
8302 }
8303
8304 if (bed->s->arch_size == 32)
8305 r_sym_mask = ~(bfd_vma) 0xff;
8306 else
8307 r_sym_mask = ~(bfd_vma) 0xffffffff;
8308
3410fea8 8309 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8310 if (lo->type == bfd_indirect_link_order)
8311 {
8312 bfd_byte *erel, *erelend;
8313 asection *o = lo->u.indirect.section;
8314
1da212d6
AM
8315 if (o->contents == NULL && o->size != 0)
8316 {
8317 /* This is a reloc section that is being handled as a normal
8318 section. See bfd_section_from_shdr. We can't combine
8319 relocs in this case. */
8320 free (sort);
8321 return 0;
8322 }
c152c796 8323 erel = o->contents;
eea6121a 8324 erelend = o->contents + o->size;
5dabe785 8325 /* FIXME: octets_per_byte. */
c152c796 8326 p = sort + o->output_offset / ext_size * sort_elt;
3410fea8 8327
c152c796
AM
8328 while (erel < erelend)
8329 {
8330 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8331
c152c796 8332 (*swap_in) (abfd, erel, s->rela);
7e612e98 8333 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8334 s->u.sym_mask = r_sym_mask;
8335 p += sort_elt;
8336 erel += ext_size;
8337 }
8338 }
8339
8340 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8341
8342 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8343 {
8344 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8345 if (s->type != reloc_class_relative)
8346 break;
8347 }
8348 ret = i;
8349 s_non_relative = p;
8350
8351 sq = (struct elf_link_sort_rela *) s_non_relative;
8352 for (; i < count; i++, p += sort_elt)
8353 {
8354 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8355 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8356 sq = sp;
8357 sp->u.offset = sq->rela->r_offset;
8358 }
8359
8360 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8361
3410fea8 8362 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8363 if (lo->type == bfd_indirect_link_order)
8364 {
8365 bfd_byte *erel, *erelend;
8366 asection *o = lo->u.indirect.section;
8367
8368 erel = o->contents;
eea6121a 8369 erelend = o->contents + o->size;
5dabe785 8370 /* FIXME: octets_per_byte. */
c152c796
AM
8371 p = sort + o->output_offset / ext_size * sort_elt;
8372 while (erel < erelend)
8373 {
8374 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8375 (*swap_out) (abfd, s->rela, erel);
8376 p += sort_elt;
8377 erel += ext_size;
8378 }
8379 }
8380
8381 free (sort);
3410fea8 8382 *psec = dynamic_relocs;
c152c796
AM
8383 return ret;
8384}
8385
8386/* Flush the output symbols to the file. */
8387
8388static bfd_boolean
8b127cbc 8389elf_link_flush_output_syms (struct elf_final_link_info *flinfo,
c152c796
AM
8390 const struct elf_backend_data *bed)
8391{
8b127cbc 8392 if (flinfo->symbuf_count > 0)
c152c796
AM
8393 {
8394 Elf_Internal_Shdr *hdr;
8395 file_ptr pos;
8396 bfd_size_type amt;
8397
8b127cbc 8398 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
c152c796 8399 pos = hdr->sh_offset + hdr->sh_size;
8b127cbc
AM
8400 amt = flinfo->symbuf_count * bed->s->sizeof_sym;
8401 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) != 0
8402 || bfd_bwrite (flinfo->symbuf, amt, flinfo->output_bfd) != amt)
c152c796
AM
8403 return FALSE;
8404
8405 hdr->sh_size += amt;
8b127cbc 8406 flinfo->symbuf_count = 0;
c152c796
AM
8407 }
8408
8409 return TRUE;
8410}
8411
8412/* Add a symbol to the output symbol table. */
8413
6e0b88f1 8414static int
8b127cbc 8415elf_link_output_sym (struct elf_final_link_info *flinfo,
c152c796
AM
8416 const char *name,
8417 Elf_Internal_Sym *elfsym,
8418 asection *input_sec,
8419 struct elf_link_hash_entry *h)
8420{
8421 bfd_byte *dest;
8422 Elf_External_Sym_Shndx *destshndx;
6e0b88f1 8423 int (*output_symbol_hook)
c152c796
AM
8424 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8425 struct elf_link_hash_entry *);
8426 const struct elf_backend_data *bed;
8427
8b127cbc 8428 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
8429 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8430 if (output_symbol_hook != NULL)
8431 {
8b127cbc 8432 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
8433 if (ret != 1)
8434 return ret;
c152c796
AM
8435 }
8436
8437 if (name == NULL || *name == '\0')
8438 elfsym->st_name = 0;
8439 else if (input_sec->flags & SEC_EXCLUDE)
8440 elfsym->st_name = 0;
8441 else
8442 {
8b127cbc 8443 elfsym->st_name = (unsigned long) _bfd_stringtab_add (flinfo->symstrtab,
c152c796
AM
8444 name, TRUE, FALSE);
8445 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8446 return 0;
c152c796
AM
8447 }
8448
8b127cbc 8449 if (flinfo->symbuf_count >= flinfo->symbuf_size)
c152c796 8450 {
8b127cbc 8451 if (! elf_link_flush_output_syms (flinfo, bed))
6e0b88f1 8452 return 0;
c152c796
AM
8453 }
8454
8b127cbc
AM
8455 dest = flinfo->symbuf + flinfo->symbuf_count * bed->s->sizeof_sym;
8456 destshndx = flinfo->symshndxbuf;
c152c796
AM
8457 if (destshndx != NULL)
8458 {
8b127cbc 8459 if (bfd_get_symcount (flinfo->output_bfd) >= flinfo->shndxbuf_size)
c152c796
AM
8460 {
8461 bfd_size_type amt;
8462
8b127cbc 8463 amt = flinfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
a50b1753
NC
8464 destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx,
8465 amt * 2);
c152c796 8466 if (destshndx == NULL)
6e0b88f1 8467 return 0;
8b127cbc 8468 flinfo->symshndxbuf = destshndx;
c152c796 8469 memset ((char *) destshndx + amt, 0, amt);
8b127cbc 8470 flinfo->shndxbuf_size *= 2;
c152c796 8471 }
8b127cbc 8472 destshndx += bfd_get_symcount (flinfo->output_bfd);
c152c796
AM
8473 }
8474
8b127cbc
AM
8475 bed->s->swap_symbol_out (flinfo->output_bfd, elfsym, dest, destshndx);
8476 flinfo->symbuf_count += 1;
8477 bfd_get_symcount (flinfo->output_bfd) += 1;
c152c796 8478
6e0b88f1 8479 return 1;
c152c796
AM
8480}
8481
c0d5a53d
L
8482/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8483
8484static bfd_boolean
8485check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8486{
4fbb74a6
AM
8487 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8488 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
8489 {
8490 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 8491 beyond 64k. */
c0d5a53d
L
8492 (*_bfd_error_handler)
8493 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 8494 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
8495 bfd_set_error (bfd_error_nonrepresentable_section);
8496 return FALSE;
8497 }
8498 return TRUE;
8499}
8500
c152c796
AM
8501/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8502 allowing an unsatisfied unversioned symbol in the DSO to match a
8503 versioned symbol that would normally require an explicit version.
8504 We also handle the case that a DSO references a hidden symbol
8505 which may be satisfied by a versioned symbol in another DSO. */
8506
8507static bfd_boolean
8508elf_link_check_versioned_symbol (struct bfd_link_info *info,
8509 const struct elf_backend_data *bed,
8510 struct elf_link_hash_entry *h)
8511{
8512 bfd *abfd;
8513 struct elf_link_loaded_list *loaded;
8514
8515 if (!is_elf_hash_table (info->hash))
8516 return FALSE;
8517
90c984fc
L
8518 /* Check indirect symbol. */
8519 while (h->root.type == bfd_link_hash_indirect)
8520 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8521
c152c796
AM
8522 switch (h->root.type)
8523 {
8524 default:
8525 abfd = NULL;
8526 break;
8527
8528 case bfd_link_hash_undefined:
8529 case bfd_link_hash_undefweak:
8530 abfd = h->root.u.undef.abfd;
8531 if ((abfd->flags & DYNAMIC) == 0
e56f61be 8532 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
8533 return FALSE;
8534 break;
8535
8536 case bfd_link_hash_defined:
8537 case bfd_link_hash_defweak:
8538 abfd = h->root.u.def.section->owner;
8539 break;
8540
8541 case bfd_link_hash_common:
8542 abfd = h->root.u.c.p->section->owner;
8543 break;
8544 }
8545 BFD_ASSERT (abfd != NULL);
8546
8547 for (loaded = elf_hash_table (info)->loaded;
8548 loaded != NULL;
8549 loaded = loaded->next)
8550 {
8551 bfd *input;
8552 Elf_Internal_Shdr *hdr;
8553 bfd_size_type symcount;
8554 bfd_size_type extsymcount;
8555 bfd_size_type extsymoff;
8556 Elf_Internal_Shdr *versymhdr;
8557 Elf_Internal_Sym *isym;
8558 Elf_Internal_Sym *isymend;
8559 Elf_Internal_Sym *isymbuf;
8560 Elf_External_Versym *ever;
8561 Elf_External_Versym *extversym;
8562
8563 input = loaded->abfd;
8564
8565 /* We check each DSO for a possible hidden versioned definition. */
8566 if (input == abfd
8567 || (input->flags & DYNAMIC) == 0
8568 || elf_dynversym (input) == 0)
8569 continue;
8570
8571 hdr = &elf_tdata (input)->dynsymtab_hdr;
8572
8573 symcount = hdr->sh_size / bed->s->sizeof_sym;
8574 if (elf_bad_symtab (input))
8575 {
8576 extsymcount = symcount;
8577 extsymoff = 0;
8578 }
8579 else
8580 {
8581 extsymcount = symcount - hdr->sh_info;
8582 extsymoff = hdr->sh_info;
8583 }
8584
8585 if (extsymcount == 0)
8586 continue;
8587
8588 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8589 NULL, NULL, NULL);
8590 if (isymbuf == NULL)
8591 return FALSE;
8592
8593 /* Read in any version definitions. */
8594 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 8595 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
8596 if (extversym == NULL)
8597 goto error_ret;
8598
8599 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8600 || (bfd_bread (extversym, versymhdr->sh_size, input)
8601 != versymhdr->sh_size))
8602 {
8603 free (extversym);
8604 error_ret:
8605 free (isymbuf);
8606 return FALSE;
8607 }
8608
8609 ever = extversym + extsymoff;
8610 isymend = isymbuf + extsymcount;
8611 for (isym = isymbuf; isym < isymend; isym++, ever++)
8612 {
8613 const char *name;
8614 Elf_Internal_Versym iver;
8615 unsigned short version_index;
8616
8617 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8618 || isym->st_shndx == SHN_UNDEF)
8619 continue;
8620
8621 name = bfd_elf_string_from_elf_section (input,
8622 hdr->sh_link,
8623 isym->st_name);
8624 if (strcmp (name, h->root.root.string) != 0)
8625 continue;
8626
8627 _bfd_elf_swap_versym_in (input, ever, &iver);
8628
d023c380
L
8629 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8630 && !(h->def_regular
8631 && h->forced_local))
c152c796
AM
8632 {
8633 /* If we have a non-hidden versioned sym, then it should
d023c380
L
8634 have provided a definition for the undefined sym unless
8635 it is defined in a non-shared object and forced local.
8636 */
c152c796
AM
8637 abort ();
8638 }
8639
8640 version_index = iver.vs_vers & VERSYM_VERSION;
8641 if (version_index == 1 || version_index == 2)
8642 {
8643 /* This is the base or first version. We can use it. */
8644 free (extversym);
8645 free (isymbuf);
8646 return TRUE;
8647 }
8648 }
8649
8650 free (extversym);
8651 free (isymbuf);
8652 }
8653
8654 return FALSE;
8655}
8656
8657/* Add an external symbol to the symbol table. This is called from
8658 the hash table traversal routine. When generating a shared object,
8659 we go through the symbol table twice. The first time we output
8660 anything that might have been forced to local scope in a version
8661 script. The second time we output the symbols that are still
8662 global symbols. */
8663
8664static bfd_boolean
7686d77d 8665elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 8666{
7686d77d 8667 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 8668 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 8669 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
8670 bfd_boolean strip;
8671 Elf_Internal_Sym sym;
8672 asection *input_sec;
8673 const struct elf_backend_data *bed;
6e0b88f1
AM
8674 long indx;
8675 int ret;
c152c796
AM
8676
8677 if (h->root.type == bfd_link_hash_warning)
8678 {
8679 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8680 if (h->root.type == bfd_link_hash_new)
8681 return TRUE;
8682 }
8683
8684 /* Decide whether to output this symbol in this pass. */
8685 if (eoinfo->localsyms)
8686 {
f5385ebf 8687 if (!h->forced_local)
c152c796 8688 return TRUE;
ffbc01cc
AM
8689 if (eoinfo->second_pass
8690 && !((h->root.type == bfd_link_hash_defined
8691 || h->root.type == bfd_link_hash_defweak)
8692 && h->root.u.def.section->output_section != NULL))
8693 return TRUE;
34a79995
JB
8694
8695 if (!eoinfo->file_sym_done
8696 && (eoinfo->second_pass ? eoinfo->flinfo->filesym_count == 1
8697 : eoinfo->flinfo->filesym_count > 1))
8698 {
8699 /* Output a FILE symbol so that following locals are not associated
8700 with the wrong input file. */
8701 memset (&sym, 0, sizeof (sym));
8702 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8703 sym.st_shndx = SHN_ABS;
8704 if (!elf_link_output_sym (eoinfo->flinfo, NULL, &sym,
8705 bfd_und_section_ptr, NULL))
8706 return FALSE;
8707
8708 eoinfo->file_sym_done = TRUE;
8709 }
c152c796
AM
8710 }
8711 else
8712 {
f5385ebf 8713 if (h->forced_local)
c152c796
AM
8714 return TRUE;
8715 }
8716
8b127cbc 8717 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 8718
12ac1cf5 8719 if (h->root.type == bfd_link_hash_undefined)
c152c796 8720 {
12ac1cf5
NC
8721 /* If we have an undefined symbol reference here then it must have
8722 come from a shared library that is being linked in. (Undefined
98da7939
L
8723 references in regular files have already been handled unless
8724 they are in unreferenced sections which are removed by garbage
8725 collection). */
12ac1cf5
NC
8726 bfd_boolean ignore_undef = FALSE;
8727
8728 /* Some symbols may be special in that the fact that they're
8729 undefined can be safely ignored - let backend determine that. */
8730 if (bed->elf_backend_ignore_undef_symbol)
8731 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8732
8733 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 8734 if (!ignore_undef
12ac1cf5 8735 && h->ref_dynamic
8b127cbc
AM
8736 && (!h->ref_regular || flinfo->info->gc_sections)
8737 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
8738 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8739 {
8740 if (!(flinfo->info->callbacks->undefined_symbol
8741 (flinfo->info, h->root.root.string,
8742 h->ref_regular ? NULL : h->root.u.undef.abfd,
8743 NULL, 0,
8744 (flinfo->info->unresolved_syms_in_shared_libs
8745 == RM_GENERATE_ERROR))))
12ac1cf5 8746 {
17d078c5 8747 bfd_set_error (bfd_error_bad_value);
12ac1cf5
NC
8748 eoinfo->failed = TRUE;
8749 return FALSE;
8750 }
c152c796
AM
8751 }
8752 }
8753
8754 /* We should also warn if a forced local symbol is referenced from
8755 shared libraries. */
8b127cbc
AM
8756 if (!flinfo->info->relocatable
8757 && flinfo->info->executable
f5385ebf
AM
8758 && h->forced_local
8759 && h->ref_dynamic
371a5866 8760 && h->def_regular
f5385ebf 8761 && !h->dynamic_def
ee659f1f 8762 && h->ref_dynamic_nonweak
8b127cbc 8763 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 8764 {
17d078c5
AM
8765 bfd *def_bfd;
8766 const char *msg;
90c984fc
L
8767 struct elf_link_hash_entry *hi = h;
8768
8769 /* Check indirect symbol. */
8770 while (hi->root.type == bfd_link_hash_indirect)
8771 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
8772
8773 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
8774 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
8775 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
8776 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
8777 else
8778 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 8779 def_bfd = flinfo->output_bfd;
90c984fc
L
8780 if (hi->root.u.def.section != bfd_abs_section_ptr)
8781 def_bfd = hi->root.u.def.section->owner;
8b127cbc 8782 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
17d078c5
AM
8783 h->root.root.string);
8784 bfd_set_error (bfd_error_bad_value);
c152c796
AM
8785 eoinfo->failed = TRUE;
8786 return FALSE;
8787 }
8788
8789 /* We don't want to output symbols that have never been mentioned by
8790 a regular file, or that we have been told to strip. However, if
8791 h->indx is set to -2, the symbol is used by a reloc and we must
8792 output it. */
8793 if (h->indx == -2)
8794 strip = FALSE;
f5385ebf 8795 else if ((h->def_dynamic
77cfaee6
AM
8796 || h->ref_dynamic
8797 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
8798 && !h->def_regular
8799 && !h->ref_regular)
c152c796 8800 strip = TRUE;
8b127cbc 8801 else if (flinfo->info->strip == strip_all)
c152c796 8802 strip = TRUE;
8b127cbc
AM
8803 else if (flinfo->info->strip == strip_some
8804 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
8805 h->root.root.string, FALSE, FALSE) == NULL)
8806 strip = TRUE;
d56d55e7
AM
8807 else if ((h->root.type == bfd_link_hash_defined
8808 || h->root.type == bfd_link_hash_defweak)
8b127cbc 8809 && ((flinfo->info->strip_discarded
dbaa2011 8810 && discarded_section (h->root.u.def.section))
d56d55e7
AM
8811 || (h->root.u.def.section->owner != NULL
8812 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 8813 strip = TRUE;
9e2278f5
AM
8814 else if ((h->root.type == bfd_link_hash_undefined
8815 || h->root.type == bfd_link_hash_undefweak)
8816 && h->root.u.undef.abfd != NULL
8817 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
8818 strip = TRUE;
c152c796
AM
8819 else
8820 strip = FALSE;
8821
8822 /* If we're stripping it, and it's not a dynamic symbol, there's
57ca8ac7
L
8823 nothing else to do unless it is a forced local symbol or a
8824 STT_GNU_IFUNC symbol. */
c152c796
AM
8825 if (strip
8826 && h->dynindx == -1
57ca8ac7 8827 && h->type != STT_GNU_IFUNC
f5385ebf 8828 && !h->forced_local)
c152c796
AM
8829 return TRUE;
8830
8831 sym.st_value = 0;
8832 sym.st_size = h->size;
8833 sym.st_other = h->other;
f5385ebf 8834 if (h->forced_local)
935bd1e0
L
8835 {
8836 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8837 /* Turn off visibility on local symbol. */
8838 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
8839 }
02acbe22
L
8840 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
8841 else if (h->unique_global && h->def_regular)
3e7a7d11 8842 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
c152c796
AM
8843 else if (h->root.type == bfd_link_hash_undefweak
8844 || h->root.type == bfd_link_hash_defweak)
8845 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8846 else
8847 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
35fc36a8 8848 sym.st_target_internal = h->target_internal;
c152c796
AM
8849
8850 switch (h->root.type)
8851 {
8852 default:
8853 case bfd_link_hash_new:
8854 case bfd_link_hash_warning:
8855 abort ();
8856 return FALSE;
8857
8858 case bfd_link_hash_undefined:
8859 case bfd_link_hash_undefweak:
8860 input_sec = bfd_und_section_ptr;
8861 sym.st_shndx = SHN_UNDEF;
8862 break;
8863
8864 case bfd_link_hash_defined:
8865 case bfd_link_hash_defweak:
8866 {
8867 input_sec = h->root.u.def.section;
8868 if (input_sec->output_section != NULL)
8869 {
ffbc01cc
AM
8870 if (eoinfo->localsyms && flinfo->filesym_count == 1)
8871 {
8872 bfd_boolean second_pass_sym
8873 = (input_sec->owner == flinfo->output_bfd
8874 || input_sec->owner == NULL
8875 || (input_sec->flags & SEC_LINKER_CREATED) != 0
8876 || (input_sec->owner->flags & BFD_LINKER_CREATED) != 0);
8877
8878 eoinfo->need_second_pass |= second_pass_sym;
8879 if (eoinfo->second_pass != second_pass_sym)
8880 return TRUE;
8881 }
8882
c152c796 8883 sym.st_shndx =
8b127cbc 8884 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
8885 input_sec->output_section);
8886 if (sym.st_shndx == SHN_BAD)
8887 {
8888 (*_bfd_error_handler)
d003868e 8889 (_("%B: could not find output section %A for input section %A"),
8b127cbc 8890 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 8891 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
8892 eoinfo->failed = TRUE;
8893 return FALSE;
8894 }
8895
8896 /* ELF symbols in relocatable files are section relative,
8897 but in nonrelocatable files they are virtual
8898 addresses. */
8899 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8b127cbc 8900 if (!flinfo->info->relocatable)
c152c796
AM
8901 {
8902 sym.st_value += input_sec->output_section->vma;
8903 if (h->type == STT_TLS)
8904 {
8b127cbc 8905 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
8906 if (tls_sec != NULL)
8907 sym.st_value -= tls_sec->vma;
8908 else
8909 {
8910 /* The TLS section may have been garbage collected. */
8b127cbc 8911 BFD_ASSERT (flinfo->info->gc_sections
430a16a5
NC
8912 && !input_sec->gc_mark);
8913 }
c152c796
AM
8914 }
8915 }
8916 }
8917 else
8918 {
8919 BFD_ASSERT (input_sec->owner == NULL
8920 || (input_sec->owner->flags & DYNAMIC) != 0);
8921 sym.st_shndx = SHN_UNDEF;
8922 input_sec = bfd_und_section_ptr;
8923 }
8924 }
8925 break;
8926
8927 case bfd_link_hash_common:
8928 input_sec = h->root.u.c.p->section;
a4d8e49b 8929 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
8930 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8931 break;
8932
8933 case bfd_link_hash_indirect:
8934 /* These symbols are created by symbol versioning. They point
8935 to the decorated version of the name. For example, if the
8936 symbol foo@@GNU_1.2 is the default, which should be used when
8937 foo is used with no version, then we add an indirect symbol
8938 foo which points to foo@@GNU_1.2. We ignore these symbols,
8939 since the indirected symbol is already in the hash table. */
8940 return TRUE;
8941 }
8942
8943 /* Give the processor backend a chance to tweak the symbol value,
8944 and also to finish up anything that needs to be done for this
8945 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 8946 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 8947 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 8948 if ((h->type == STT_GNU_IFUNC
5f35ea9c 8949 && h->def_regular
8b127cbc 8950 && !flinfo->info->relocatable)
3aa14d16
L
8951 || ((h->dynindx != -1
8952 || h->forced_local)
8b127cbc 8953 && ((flinfo->info->shared
3aa14d16
L
8954 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8955 || h->root.type != bfd_link_hash_undefweak))
8956 || !h->forced_local)
8b127cbc 8957 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
8958 {
8959 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 8960 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
8961 {
8962 eoinfo->failed = TRUE;
8963 return FALSE;
8964 }
8965 }
8966
8967 /* If we are marking the symbol as undefined, and there are no
8968 non-weak references to this symbol from a regular object, then
8969 mark the symbol as weak undefined; if there are non-weak
8970 references, mark the symbol as strong. We can't do this earlier,
8971 because it might not be marked as undefined until the
8972 finish_dynamic_symbol routine gets through with it. */
8973 if (sym.st_shndx == SHN_UNDEF
f5385ebf 8974 && h->ref_regular
c152c796
AM
8975 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8976 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8977 {
8978 int bindtype;
2955ec4c
L
8979 unsigned int type = ELF_ST_TYPE (sym.st_info);
8980
8981 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
8982 if (type == STT_GNU_IFUNC)
8983 type = STT_FUNC;
c152c796 8984
f5385ebf 8985 if (h->ref_regular_nonweak)
c152c796
AM
8986 bindtype = STB_GLOBAL;
8987 else
8988 bindtype = STB_WEAK;
2955ec4c 8989 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
8990 }
8991
bda987c2
CD
8992 /* If this is a symbol defined in a dynamic library, don't use the
8993 symbol size from the dynamic library. Relinking an executable
8994 against a new library may introduce gratuitous changes in the
8995 executable's symbols if we keep the size. */
8996 if (sym.st_shndx == SHN_UNDEF
8997 && !h->def_regular
8998 && h->def_dynamic)
8999 sym.st_size = 0;
9000
c152c796
AM
9001 /* If a non-weak symbol with non-default visibility is not defined
9002 locally, it is a fatal error. */
8b127cbc 9003 if (!flinfo->info->relocatable
c152c796
AM
9004 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9005 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9006 && h->root.type == bfd_link_hash_undefined
f5385ebf 9007 && !h->def_regular)
c152c796 9008 {
17d078c5
AM
9009 const char *msg;
9010
9011 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9012 msg = _("%B: protected symbol `%s' isn't defined");
9013 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9014 msg = _("%B: internal symbol `%s' isn't defined");
9015 else
9016 msg = _("%B: hidden symbol `%s' isn't defined");
8b127cbc 9017 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9018 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9019 eoinfo->failed = TRUE;
9020 return FALSE;
9021 }
9022
9023 /* If this symbol should be put in the .dynsym section, then put it
9024 there now. We already know the symbol index. We also fill in
9025 the entry in the .hash section. */
8b127cbc 9026 if (flinfo->dynsym_sec != NULL
202e2356 9027 && h->dynindx != -1
8b127cbc 9028 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9029 {
c152c796
AM
9030 bfd_byte *esym;
9031
90c984fc
L
9032 /* Since there is no version information in the dynamic string,
9033 if there is no version info in symbol version section, we will
9034 have a run-time problem. */
9035 if (h->verinfo.verdef == NULL)
9036 {
9037 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9038
9039 if (p && p [1] != '\0')
9040 {
9041 (*_bfd_error_handler)
9042 (_("%B: No symbol version section for versioned symbol `%s'"),
9043 flinfo->output_bfd, h->root.root.string);
9044 eoinfo->failed = TRUE;
9045 return FALSE;
9046 }
9047 }
9048
c152c796 9049 sym.st_name = h->dynstr_index;
8b127cbc
AM
9050 esym = flinfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
9051 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9052 {
9053 eoinfo->failed = TRUE;
9054 return FALSE;
9055 }
8b127cbc 9056 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9057
8b127cbc 9058 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9059 {
9060 size_t hash_entry_size;
9061 bfd_byte *bucketpos;
9062 bfd_vma chain;
41198d0c
L
9063 size_t bucketcount;
9064 size_t bucket;
9065
8b127cbc 9066 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9067 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9068
9069 hash_entry_size
8b127cbc
AM
9070 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9071 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9072 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9073 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9074 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9075 bucketpos);
9076 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9077 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9078 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9079 }
c152c796 9080
8b127cbc 9081 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9082 {
9083 Elf_Internal_Versym iversym;
9084 Elf_External_Versym *eversym;
9085
f5385ebf 9086 if (!h->def_regular)
c152c796
AM
9087 {
9088 if (h->verinfo.verdef == NULL)
9089 iversym.vs_vers = 0;
9090 else
9091 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9092 }
9093 else
9094 {
9095 if (h->verinfo.vertree == NULL)
9096 iversym.vs_vers = 1;
9097 else
9098 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9099 if (flinfo->info->create_default_symver)
3e3b46e5 9100 iversym.vs_vers++;
c152c796
AM
9101 }
9102
f5385ebf 9103 if (h->hidden)
c152c796
AM
9104 iversym.vs_vers |= VERSYM_HIDDEN;
9105
8b127cbc 9106 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9107 eversym += h->dynindx;
8b127cbc 9108 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9109 }
9110 }
9111
9112 /* If we're stripping it, then it was just a dynamic symbol, and
9113 there's nothing else to do. */
9114 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
9115 return TRUE;
9116
8b127cbc
AM
9117 indx = bfd_get_symcount (flinfo->output_bfd);
9118 ret = elf_link_output_sym (flinfo, h->root.root.string, &sym, input_sec, h);
6e0b88f1 9119 if (ret == 0)
c152c796
AM
9120 {
9121 eoinfo->failed = TRUE;
9122 return FALSE;
9123 }
6e0b88f1
AM
9124 else if (ret == 1)
9125 h->indx = indx;
9126 else if (h->indx == -2)
9127 abort();
c152c796
AM
9128
9129 return TRUE;
9130}
9131
cdd3575c
AM
9132/* Return TRUE if special handling is done for relocs in SEC against
9133 symbols defined in discarded sections. */
9134
c152c796
AM
9135static bfd_boolean
9136elf_section_ignore_discarded_relocs (asection *sec)
9137{
9138 const struct elf_backend_data *bed;
9139
cdd3575c
AM
9140 switch (sec->sec_info_type)
9141 {
dbaa2011
AM
9142 case SEC_INFO_TYPE_STABS:
9143 case SEC_INFO_TYPE_EH_FRAME:
cdd3575c
AM
9144 return TRUE;
9145 default:
9146 break;
9147 }
c152c796
AM
9148
9149 bed = get_elf_backend_data (sec->owner);
9150 if (bed->elf_backend_ignore_discarded_relocs != NULL
9151 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9152 return TRUE;
9153
9154 return FALSE;
9155}
9156
9e66c942
AM
9157/* Return a mask saying how ld should treat relocations in SEC against
9158 symbols defined in discarded sections. If this function returns
9159 COMPLAIN set, ld will issue a warning message. If this function
9160 returns PRETEND set, and the discarded section was link-once and the
9161 same size as the kept link-once section, ld will pretend that the
9162 symbol was actually defined in the kept section. Otherwise ld will
9163 zero the reloc (at least that is the intent, but some cooperation by
9164 the target dependent code is needed, particularly for REL targets). */
9165
8a696751
AM
9166unsigned int
9167_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9168{
9e66c942 9169 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9170 return PRETEND;
cdd3575c
AM
9171
9172 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9173 return 0;
cdd3575c
AM
9174
9175 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9176 return 0;
cdd3575c 9177
9e66c942 9178 return COMPLAIN | PRETEND;
cdd3575c
AM
9179}
9180
3d7f7666
L
9181/* Find a match between a section and a member of a section group. */
9182
9183static asection *
c0f00686
L
9184match_group_member (asection *sec, asection *group,
9185 struct bfd_link_info *info)
3d7f7666
L
9186{
9187 asection *first = elf_next_in_group (group);
9188 asection *s = first;
9189
9190 while (s != NULL)
9191 {
c0f00686 9192 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9193 return s;
9194
83180ade 9195 s = elf_next_in_group (s);
3d7f7666
L
9196 if (s == first)
9197 break;
9198 }
9199
9200 return NULL;
9201}
9202
01b3c8ab 9203/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9204 to replace it. Return the replacement if it is OK. Otherwise return
9205 NULL. */
01b3c8ab
L
9206
9207asection *
c0f00686 9208_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9209{
9210 asection *kept;
9211
9212 kept = sec->kept_section;
9213 if (kept != NULL)
9214 {
c2370991 9215 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9216 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9217 if (kept != NULL
9218 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9219 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9220 kept = NULL;
c2370991 9221 sec->kept_section = kept;
01b3c8ab
L
9222 }
9223 return kept;
9224}
9225
c152c796
AM
9226/* Link an input file into the linker output file. This function
9227 handles all the sections and relocations of the input file at once.
9228 This is so that we only have to read the local symbols once, and
9229 don't have to keep them in memory. */
9230
9231static bfd_boolean
8b127cbc 9232elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9233{
ece5ef60 9234 int (*relocate_section)
c152c796
AM
9235 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9236 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9237 bfd *output_bfd;
9238 Elf_Internal_Shdr *symtab_hdr;
9239 size_t locsymcount;
9240 size_t extsymoff;
9241 Elf_Internal_Sym *isymbuf;
9242 Elf_Internal_Sym *isym;
9243 Elf_Internal_Sym *isymend;
9244 long *pindex;
9245 asection **ppsection;
9246 asection *o;
9247 const struct elf_backend_data *bed;
c152c796 9248 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9249 bfd_size_type address_size;
9250 bfd_vma r_type_mask;
9251 int r_sym_shift;
ffbc01cc 9252 bfd_boolean have_file_sym = FALSE;
c152c796 9253
8b127cbc 9254 output_bfd = flinfo->output_bfd;
c152c796
AM
9255 bed = get_elf_backend_data (output_bfd);
9256 relocate_section = bed->elf_backend_relocate_section;
9257
9258 /* If this is a dynamic object, we don't want to do anything here:
9259 we don't want the local symbols, and we don't want the section
9260 contents. */
9261 if ((input_bfd->flags & DYNAMIC) != 0)
9262 return TRUE;
9263
c152c796
AM
9264 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9265 if (elf_bad_symtab (input_bfd))
9266 {
9267 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9268 extsymoff = 0;
9269 }
9270 else
9271 {
9272 locsymcount = symtab_hdr->sh_info;
9273 extsymoff = symtab_hdr->sh_info;
9274 }
9275
9276 /* Read the local symbols. */
9277 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9278 if (isymbuf == NULL && locsymcount != 0)
9279 {
9280 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9281 flinfo->internal_syms,
9282 flinfo->external_syms,
9283 flinfo->locsym_shndx);
c152c796
AM
9284 if (isymbuf == NULL)
9285 return FALSE;
9286 }
9287
9288 /* Find local symbol sections and adjust values of symbols in
9289 SEC_MERGE sections. Write out those local symbols we know are
9290 going into the output file. */
9291 isymend = isymbuf + locsymcount;
8b127cbc 9292 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9293 isym < isymend;
9294 isym++, pindex++, ppsection++)
9295 {
9296 asection *isec;
9297 const char *name;
9298 Elf_Internal_Sym osym;
6e0b88f1
AM
9299 long indx;
9300 int ret;
c152c796
AM
9301
9302 *pindex = -1;
9303
9304 if (elf_bad_symtab (input_bfd))
9305 {
9306 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9307 {
9308 *ppsection = NULL;
9309 continue;
9310 }
9311 }
9312
9313 if (isym->st_shndx == SHN_UNDEF)
9314 isec = bfd_und_section_ptr;
c152c796
AM
9315 else if (isym->st_shndx == SHN_ABS)
9316 isec = bfd_abs_section_ptr;
9317 else if (isym->st_shndx == SHN_COMMON)
9318 isec = bfd_com_section_ptr;
9319 else
9320 {
cb33740c
AM
9321 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9322 if (isec == NULL)
9323 {
9324 /* Don't attempt to output symbols with st_shnx in the
9325 reserved range other than SHN_ABS and SHN_COMMON. */
9326 *ppsection = NULL;
9327 continue;
9328 }
dbaa2011 9329 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9330 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9331 isym->st_value =
9332 _bfd_merged_section_offset (output_bfd, &isec,
9333 elf_section_data (isec)->sec_info,
9334 isym->st_value);
c152c796
AM
9335 }
9336
9337 *ppsection = isec;
9338
9339 /* Don't output the first, undefined, symbol. */
8b127cbc 9340 if (ppsection == flinfo->sections)
c152c796
AM
9341 continue;
9342
9343 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9344 {
9345 /* We never output section symbols. Instead, we use the
9346 section symbol of the corresponding section in the output
9347 file. */
9348 continue;
9349 }
9350
9351 /* If we are stripping all symbols, we don't want to output this
9352 one. */
8b127cbc 9353 if (flinfo->info->strip == strip_all)
c152c796
AM
9354 continue;
9355
9356 /* If we are discarding all local symbols, we don't want to
9357 output this one. If we are generating a relocatable output
9358 file, then some of the local symbols may be required by
9359 relocs; we output them below as we discover that they are
9360 needed. */
8b127cbc 9361 if (flinfo->info->discard == discard_all)
c152c796
AM
9362 continue;
9363
9364 /* If this symbol is defined in a section which we are
f02571c5
AM
9365 discarding, we don't need to keep it. */
9366 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9367 && isym->st_shndx < SHN_LORESERVE
9368 && bfd_section_removed_from_list (output_bfd,
9369 isec->output_section))
e75a280b
L
9370 continue;
9371
c152c796
AM
9372 /* Get the name of the symbol. */
9373 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9374 isym->st_name);
9375 if (name == NULL)
9376 return FALSE;
9377
9378 /* See if we are discarding symbols with this name. */
8b127cbc
AM
9379 if ((flinfo->info->strip == strip_some
9380 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 9381 == NULL))
8b127cbc
AM
9382 || (((flinfo->info->discard == discard_sec_merge
9383 && (isec->flags & SEC_MERGE) && !flinfo->info->relocatable)
9384 || flinfo->info->discard == discard_l)
c152c796
AM
9385 && bfd_is_local_label_name (input_bfd, name)))
9386 continue;
9387
ffbc01cc
AM
9388 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9389 {
9390 have_file_sym = TRUE;
9391 flinfo->filesym_count += 1;
9392 }
9393 if (!have_file_sym)
9394 {
9395 /* In the absence of debug info, bfd_find_nearest_line uses
9396 FILE symbols to determine the source file for local
9397 function symbols. Provide a FILE symbol here if input
9398 files lack such, so that their symbols won't be
9399 associated with a previous input file. It's not the
9400 source file, but the best we can do. */
9401 have_file_sym = TRUE;
9402 flinfo->filesym_count += 1;
9403 memset (&osym, 0, sizeof (osym));
9404 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9405 osym.st_shndx = SHN_ABS;
9406 if (!elf_link_output_sym (flinfo, input_bfd->filename, &osym,
9407 bfd_abs_section_ptr, NULL))
9408 return FALSE;
9409 }
9410
c152c796
AM
9411 osym = *isym;
9412
9413 /* Adjust the section index for the output file. */
9414 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9415 isec->output_section);
9416 if (osym.st_shndx == SHN_BAD)
9417 return FALSE;
9418
c152c796
AM
9419 /* ELF symbols in relocatable files are section relative, but
9420 in executable files they are virtual addresses. Note that
9421 this code assumes that all ELF sections have an associated
9422 BFD section with a reasonable value for output_offset; below
9423 we assume that they also have a reasonable value for
9424 output_section. Any special sections must be set up to meet
9425 these requirements. */
9426 osym.st_value += isec->output_offset;
8b127cbc 9427 if (!flinfo->info->relocatable)
c152c796
AM
9428 {
9429 osym.st_value += isec->output_section->vma;
9430 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9431 {
9432 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
9433 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9434 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
9435 }
9436 }
9437
6e0b88f1 9438 indx = bfd_get_symcount (output_bfd);
8b127cbc 9439 ret = elf_link_output_sym (flinfo, name, &osym, isec, NULL);
6e0b88f1 9440 if (ret == 0)
c152c796 9441 return FALSE;
6e0b88f1
AM
9442 else if (ret == 1)
9443 *pindex = indx;
c152c796
AM
9444 }
9445
310fd250
L
9446 if (bed->s->arch_size == 32)
9447 {
9448 r_type_mask = 0xff;
9449 r_sym_shift = 8;
9450 address_size = 4;
9451 }
9452 else
9453 {
9454 r_type_mask = 0xffffffff;
9455 r_sym_shift = 32;
9456 address_size = 8;
9457 }
9458
c152c796
AM
9459 /* Relocate the contents of each section. */
9460 sym_hashes = elf_sym_hashes (input_bfd);
9461 for (o = input_bfd->sections; o != NULL; o = o->next)
9462 {
9463 bfd_byte *contents;
9464
9465 if (! o->linker_mark)
9466 {
9467 /* This section was omitted from the link. */
9468 continue;
9469 }
9470
8b127cbc 9471 if (flinfo->info->relocatable
bcacc0f5
AM
9472 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9473 {
9474 /* Deal with the group signature symbol. */
9475 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9476 unsigned long symndx = sec_data->this_hdr.sh_info;
9477 asection *osec = o->output_section;
9478
9479 if (symndx >= locsymcount
9480 || (elf_bad_symtab (input_bfd)
8b127cbc 9481 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
9482 {
9483 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9484 while (h->root.type == bfd_link_hash_indirect
9485 || h->root.type == bfd_link_hash_warning)
9486 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9487 /* Arrange for symbol to be output. */
9488 h->indx = -2;
9489 elf_section_data (osec)->this_hdr.sh_info = -2;
9490 }
9491 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9492 {
9493 /* We'll use the output section target_index. */
8b127cbc 9494 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
9495 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9496 }
9497 else
9498 {
8b127cbc 9499 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
9500 {
9501 /* Otherwise output the local symbol now. */
9502 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 9503 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 9504 const char *name;
6e0b88f1
AM
9505 long indx;
9506 int ret;
bcacc0f5
AM
9507
9508 name = bfd_elf_string_from_elf_section (input_bfd,
9509 symtab_hdr->sh_link,
9510 sym.st_name);
9511 if (name == NULL)
9512 return FALSE;
9513
9514 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9515 sec);
9516 if (sym.st_shndx == SHN_BAD)
9517 return FALSE;
9518
9519 sym.st_value += o->output_offset;
9520
6e0b88f1 9521 indx = bfd_get_symcount (output_bfd);
8b127cbc 9522 ret = elf_link_output_sym (flinfo, name, &sym, o, NULL);
6e0b88f1 9523 if (ret == 0)
bcacc0f5 9524 return FALSE;
6e0b88f1 9525 else if (ret == 1)
8b127cbc 9526 flinfo->indices[symndx] = indx;
6e0b88f1
AM
9527 else
9528 abort ();
bcacc0f5
AM
9529 }
9530 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 9531 = flinfo->indices[symndx];
bcacc0f5
AM
9532 }
9533 }
9534
c152c796 9535 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 9536 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
9537 continue;
9538
9539 if ((o->flags & SEC_LINKER_CREATED) != 0)
9540 {
9541 /* Section was created by _bfd_elf_link_create_dynamic_sections
9542 or somesuch. */
9543 continue;
9544 }
9545
9546 /* Get the contents of the section. They have been cached by a
9547 relaxation routine. Note that o is a section in an input
9548 file, so the contents field will not have been set by any of
9549 the routines which work on output files. */
9550 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
9551 {
9552 contents = elf_section_data (o)->this_hdr.contents;
9553 if (bed->caches_rawsize
9554 && o->rawsize != 0
9555 && o->rawsize < o->size)
9556 {
9557 memcpy (flinfo->contents, contents, o->rawsize);
9558 contents = flinfo->contents;
9559 }
9560 }
c152c796
AM
9561 else
9562 {
8b127cbc 9563 contents = flinfo->contents;
4a114e3e 9564 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
9565 return FALSE;
9566 }
9567
9568 if ((o->flags & SEC_RELOC) != 0)
9569 {
9570 Elf_Internal_Rela *internal_relocs;
0f02bbd9 9571 Elf_Internal_Rela *rel, *relend;
0f02bbd9 9572 int action_discarded;
ece5ef60 9573 int ret;
c152c796
AM
9574
9575 /* Get the swapped relocs. */
9576 internal_relocs
8b127cbc
AM
9577 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
9578 flinfo->internal_relocs, FALSE);
c152c796
AM
9579 if (internal_relocs == NULL
9580 && o->reloc_count > 0)
9581 return FALSE;
9582
310fd250
L
9583 /* We need to reverse-copy input .ctors/.dtors sections if
9584 they are placed in .init_array/.finit_array for output. */
9585 if (o->size > address_size
9586 && ((strncmp (o->name, ".ctors", 6) == 0
9587 && strcmp (o->output_section->name,
9588 ".init_array") == 0)
9589 || (strncmp (o->name, ".dtors", 6) == 0
9590 && strcmp (o->output_section->name,
9591 ".fini_array") == 0))
9592 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 9593 {
310fd250
L
9594 if (o->size != o->reloc_count * address_size)
9595 {
9596 (*_bfd_error_handler)
9597 (_("error: %B: size of section %A is not "
9598 "multiple of address size"),
9599 input_bfd, o);
9600 bfd_set_error (bfd_error_on_input);
9601 return FALSE;
9602 }
9603 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
9604 }
9605
0f02bbd9 9606 action_discarded = -1;
c152c796 9607 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
9608 action_discarded = (*bed->action_discarded) (o);
9609
9610 /* Run through the relocs evaluating complex reloc symbols and
9611 looking for relocs against symbols from discarded sections
9612 or section symbols from removed link-once sections.
9613 Complain about relocs against discarded sections. Zero
9614 relocs against removed link-once sections. */
9615
9616 rel = internal_relocs;
9617 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9618 for ( ; rel < relend; rel++)
c152c796 9619 {
0f02bbd9
AM
9620 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9621 unsigned int s_type;
9622 asection **ps, *sec;
9623 struct elf_link_hash_entry *h = NULL;
9624 const char *sym_name;
c152c796 9625
0f02bbd9
AM
9626 if (r_symndx == STN_UNDEF)
9627 continue;
c152c796 9628
0f02bbd9
AM
9629 if (r_symndx >= locsymcount
9630 || (elf_bad_symtab (input_bfd)
8b127cbc 9631 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
9632 {
9633 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 9634
0f02bbd9
AM
9635 /* Badly formatted input files can contain relocs that
9636 reference non-existant symbols. Check here so that
9637 we do not seg fault. */
9638 if (h == NULL)
c152c796 9639 {
0f02bbd9 9640 char buffer [32];
dce669a1 9641
0f02bbd9
AM
9642 sprintf_vma (buffer, rel->r_info);
9643 (*_bfd_error_handler)
9644 (_("error: %B contains a reloc (0x%s) for section %A "
9645 "that references a non-existent global symbol"),
9646 input_bfd, o, buffer);
9647 bfd_set_error (bfd_error_bad_value);
9648 return FALSE;
9649 }
3b36f7e6 9650
0f02bbd9
AM
9651 while (h->root.type == bfd_link_hash_indirect
9652 || h->root.type == bfd_link_hash_warning)
9653 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 9654
0f02bbd9 9655 s_type = h->type;
cdd3575c 9656
0f02bbd9
AM
9657 ps = NULL;
9658 if (h->root.type == bfd_link_hash_defined
9659 || h->root.type == bfd_link_hash_defweak)
9660 ps = &h->root.u.def.section;
9661
9662 sym_name = h->root.root.string;
9663 }
9664 else
9665 {
9666 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9667
9668 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 9669 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
9670 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9671 sym, *ps);
9672 }
c152c796 9673
c301e700 9674 if ((s_type == STT_RELC || s_type == STT_SRELC)
8b127cbc 9675 && !flinfo->info->relocatable)
0f02bbd9
AM
9676 {
9677 bfd_vma val;
9678 bfd_vma dot = (rel->r_offset
9679 + o->output_offset + o->output_section->vma);
9680#ifdef DEBUG
9681 printf ("Encountered a complex symbol!");
9682 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
9683 input_bfd->filename, o->name,
9684 (long) (rel - internal_relocs));
0f02bbd9
AM
9685 printf (" symbol: idx %8.8lx, name %s\n",
9686 r_symndx, sym_name);
9687 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9688 (unsigned long) rel->r_info,
9689 (unsigned long) rel->r_offset);
9690#endif
8b127cbc 9691 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
9692 isymbuf, locsymcount, s_type == STT_SRELC))
9693 return FALSE;
9694
9695 /* Symbol evaluated OK. Update to absolute value. */
9696 set_symbol_value (input_bfd, isymbuf, locsymcount,
9697 r_symndx, val);
9698 continue;
9699 }
9700
9701 if (action_discarded != -1 && ps != NULL)
9702 {
cdd3575c
AM
9703 /* Complain if the definition comes from a
9704 discarded section. */
dbaa2011 9705 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 9706 {
cf35638d 9707 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 9708 if (action_discarded & COMPLAIN)
8b127cbc 9709 (*flinfo->info->callbacks->einfo)
e1fffbe6 9710 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 9711 "defined in discarded section `%A' of %B\n"),
e1fffbe6 9712 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 9713
87e5235d 9714 /* Try to do the best we can to support buggy old
e0ae6d6f 9715 versions of gcc. Pretend that the symbol is
87e5235d
AM
9716 really defined in the kept linkonce section.
9717 FIXME: This is quite broken. Modifying the
9718 symbol here means we will be changing all later
e0ae6d6f 9719 uses of the symbol, not just in this section. */
0f02bbd9 9720 if (action_discarded & PRETEND)
87e5235d 9721 {
01b3c8ab
L
9722 asection *kept;
9723
c0f00686 9724 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 9725 flinfo->info);
01b3c8ab 9726 if (kept != NULL)
87e5235d
AM
9727 {
9728 *ps = kept;
9729 continue;
9730 }
9731 }
c152c796
AM
9732 }
9733 }
9734 }
9735
9736 /* Relocate the section by invoking a back end routine.
9737
9738 The back end routine is responsible for adjusting the
9739 section contents as necessary, and (if using Rela relocs
9740 and generating a relocatable output file) adjusting the
9741 reloc addend as necessary.
9742
9743 The back end routine does not have to worry about setting
9744 the reloc address or the reloc symbol index.
9745
9746 The back end routine is given a pointer to the swapped in
9747 internal symbols, and can access the hash table entries
9748 for the external symbols via elf_sym_hashes (input_bfd).
9749
9750 When generating relocatable output, the back end routine
9751 must handle STB_LOCAL/STT_SECTION symbols specially. The
9752 output symbol is going to be a section symbol
9753 corresponding to the output section, which will require
9754 the addend to be adjusted. */
9755
8b127cbc 9756 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
9757 input_bfd, o, contents,
9758 internal_relocs,
9759 isymbuf,
8b127cbc 9760 flinfo->sections);
ece5ef60 9761 if (!ret)
c152c796
AM
9762 return FALSE;
9763
ece5ef60 9764 if (ret == 2
8b127cbc
AM
9765 || flinfo->info->relocatable
9766 || flinfo->info->emitrelocations)
c152c796
AM
9767 {
9768 Elf_Internal_Rela *irela;
d4730f92 9769 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
9770 bfd_vma last_offset;
9771 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
9772 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
9773 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 9774 unsigned int next_erel;
c152c796 9775 bfd_boolean rela_normal;
d4730f92 9776 struct bfd_elf_section_data *esdi, *esdo;
c152c796 9777
d4730f92
BS
9778 esdi = elf_section_data (o);
9779 esdo = elf_section_data (o->output_section);
9780 rela_normal = FALSE;
c152c796
AM
9781
9782 /* Adjust the reloc addresses and symbol indices. */
9783
9784 irela = internal_relocs;
9785 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
9786 rel_hash = esdo->rel.hashes + esdo->rel.count;
9787 /* We start processing the REL relocs, if any. When we reach
9788 IRELAMID in the loop, we switch to the RELA relocs. */
9789 irelamid = irela;
9790 if (esdi->rel.hdr != NULL)
9791 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
9792 * bed->s->int_rels_per_ext_rel);
eac338cf 9793 rel_hash_list = rel_hash;
d4730f92 9794 rela_hash_list = NULL;
c152c796 9795 last_offset = o->output_offset;
8b127cbc 9796 if (!flinfo->info->relocatable)
c152c796
AM
9797 last_offset += o->output_section->vma;
9798 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9799 {
9800 unsigned long r_symndx;
9801 asection *sec;
9802 Elf_Internal_Sym sym;
9803
9804 if (next_erel == bed->s->int_rels_per_ext_rel)
9805 {
9806 rel_hash++;
9807 next_erel = 0;
9808 }
9809
d4730f92
BS
9810 if (irela == irelamid)
9811 {
9812 rel_hash = esdo->rela.hashes + esdo->rela.count;
9813 rela_hash_list = rel_hash;
9814 rela_normal = bed->rela_normal;
9815 }
9816
c152c796 9817 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 9818 flinfo->info, o,
c152c796
AM
9819 irela->r_offset);
9820 if (irela->r_offset >= (bfd_vma) -2)
9821 {
9822 /* This is a reloc for a deleted entry or somesuch.
9823 Turn it into an R_*_NONE reloc, at the same
9824 offset as the last reloc. elf_eh_frame.c and
e460dd0d 9825 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
9826 being ordered. */
9827 irela->r_offset = last_offset;
9828 irela->r_info = 0;
9829 irela->r_addend = 0;
9830 continue;
9831 }
9832
9833 irela->r_offset += o->output_offset;
9834
9835 /* Relocs in an executable have to be virtual addresses. */
8b127cbc 9836 if (!flinfo->info->relocatable)
c152c796
AM
9837 irela->r_offset += o->output_section->vma;
9838
9839 last_offset = irela->r_offset;
9840
9841 r_symndx = irela->r_info >> r_sym_shift;
9842 if (r_symndx == STN_UNDEF)
9843 continue;
9844
9845 if (r_symndx >= locsymcount
9846 || (elf_bad_symtab (input_bfd)
8b127cbc 9847 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
9848 {
9849 struct elf_link_hash_entry *rh;
9850 unsigned long indx;
9851
9852 /* This is a reloc against a global symbol. We
9853 have not yet output all the local symbols, so
9854 we do not know the symbol index of any global
9855 symbol. We set the rel_hash entry for this
9856 reloc to point to the global hash table entry
9857 for this symbol. The symbol index is then
ee75fd95 9858 set at the end of bfd_elf_final_link. */
c152c796
AM
9859 indx = r_symndx - extsymoff;
9860 rh = elf_sym_hashes (input_bfd)[indx];
9861 while (rh->root.type == bfd_link_hash_indirect
9862 || rh->root.type == bfd_link_hash_warning)
9863 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9864
9865 /* Setting the index to -2 tells
9866 elf_link_output_extsym that this symbol is
9867 used by a reloc. */
9868 BFD_ASSERT (rh->indx < 0);
9869 rh->indx = -2;
9870
9871 *rel_hash = rh;
9872
9873 continue;
9874 }
9875
9876 /* This is a reloc against a local symbol. */
9877
9878 *rel_hash = NULL;
9879 sym = isymbuf[r_symndx];
8b127cbc 9880 sec = flinfo->sections[r_symndx];
c152c796
AM
9881 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9882 {
9883 /* I suppose the backend ought to fill in the
9884 section of any STT_SECTION symbol against a
6a8d1586 9885 processor specific section. */
cf35638d 9886 r_symndx = STN_UNDEF;
6a8d1586
AM
9887 if (bfd_is_abs_section (sec))
9888 ;
c152c796
AM
9889 else if (sec == NULL || sec->owner == NULL)
9890 {
9891 bfd_set_error (bfd_error_bad_value);
9892 return FALSE;
9893 }
9894 else
9895 {
6a8d1586
AM
9896 asection *osec = sec->output_section;
9897
9898 /* If we have discarded a section, the output
9899 section will be the absolute section. In
ab96bf03
AM
9900 case of discarded SEC_MERGE sections, use
9901 the kept section. relocate_section should
9902 have already handled discarded linkonce
9903 sections. */
6a8d1586
AM
9904 if (bfd_is_abs_section (osec)
9905 && sec->kept_section != NULL
9906 && sec->kept_section->output_section != NULL)
9907 {
9908 osec = sec->kept_section->output_section;
9909 irela->r_addend -= osec->vma;
9910 }
9911
9912 if (!bfd_is_abs_section (osec))
9913 {
9914 r_symndx = osec->target_index;
cf35638d 9915 if (r_symndx == STN_UNDEF)
74541ad4 9916 {
051d833a
AM
9917 irela->r_addend += osec->vma;
9918 osec = _bfd_nearby_section (output_bfd, osec,
9919 osec->vma);
9920 irela->r_addend -= osec->vma;
9921 r_symndx = osec->target_index;
74541ad4 9922 }
6a8d1586 9923 }
c152c796
AM
9924 }
9925
9926 /* Adjust the addend according to where the
9927 section winds up in the output section. */
9928 if (rela_normal)
9929 irela->r_addend += sec->output_offset;
9930 }
9931 else
9932 {
8b127cbc 9933 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
9934 {
9935 unsigned long shlink;
9936 const char *name;
9937 asection *osec;
6e0b88f1 9938 long indx;
c152c796 9939
8b127cbc 9940 if (flinfo->info->strip == strip_all)
c152c796
AM
9941 {
9942 /* You can't do ld -r -s. */
9943 bfd_set_error (bfd_error_invalid_operation);
9944 return FALSE;
9945 }
9946
9947 /* This symbol was skipped earlier, but
9948 since it is needed by a reloc, we
9949 must output it now. */
9950 shlink = symtab_hdr->sh_link;
9951 name = (bfd_elf_string_from_elf_section
9952 (input_bfd, shlink, sym.st_name));
9953 if (name == NULL)
9954 return FALSE;
9955
9956 osec = sec->output_section;
9957 sym.st_shndx =
9958 _bfd_elf_section_from_bfd_section (output_bfd,
9959 osec);
9960 if (sym.st_shndx == SHN_BAD)
9961 return FALSE;
9962
9963 sym.st_value += sec->output_offset;
8b127cbc 9964 if (!flinfo->info->relocatable)
c152c796
AM
9965 {
9966 sym.st_value += osec->vma;
9967 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9968 {
9969 /* STT_TLS symbols are relative to PT_TLS
9970 segment base. */
8b127cbc 9971 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 9972 ->tls_sec != NULL);
8b127cbc 9973 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
9974 ->tls_sec->vma);
9975 }
9976 }
9977
6e0b88f1 9978 indx = bfd_get_symcount (output_bfd);
8b127cbc 9979 ret = elf_link_output_sym (flinfo, name, &sym, sec,
6e0b88f1
AM
9980 NULL);
9981 if (ret == 0)
c152c796 9982 return FALSE;
6e0b88f1 9983 else if (ret == 1)
8b127cbc 9984 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
9985 else
9986 abort ();
c152c796
AM
9987 }
9988
8b127cbc 9989 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
9990 }
9991
9992 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9993 | (irela->r_info & r_type_mask));
9994 }
9995
9996 /* Swap out the relocs. */
d4730f92
BS
9997 input_rel_hdr = esdi->rel.hdr;
9998 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 9999 {
d4730f92
BS
10000 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10001 input_rel_hdr,
10002 internal_relocs,
10003 rel_hash_list))
10004 return FALSE;
c152c796
AM
10005 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10006 * bed->s->int_rels_per_ext_rel);
eac338cf 10007 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10008 }
10009
10010 input_rela_hdr = esdi->rela.hdr;
10011 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10012 {
eac338cf 10013 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10014 input_rela_hdr,
eac338cf 10015 internal_relocs,
d4730f92 10016 rela_hash_list))
c152c796
AM
10017 return FALSE;
10018 }
10019 }
10020 }
10021
10022 /* Write out the modified section contents. */
10023 if (bed->elf_backend_write_section
8b127cbc 10024 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10025 contents))
c152c796
AM
10026 {
10027 /* Section written out. */
10028 }
10029 else switch (o->sec_info_type)
10030 {
dbaa2011 10031 case SEC_INFO_TYPE_STABS:
c152c796
AM
10032 if (! (_bfd_write_section_stabs
10033 (output_bfd,
8b127cbc 10034 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10035 o, &elf_section_data (o)->sec_info, contents)))
10036 return FALSE;
10037 break;
dbaa2011 10038 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10039 if (! _bfd_write_merged_section (output_bfd, o,
10040 elf_section_data (o)->sec_info))
10041 return FALSE;
10042 break;
dbaa2011 10043 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10044 {
8b127cbc 10045 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10046 o, contents))
10047 return FALSE;
10048 }
10049 break;
10050 default:
10051 {
5dabe785 10052 /* FIXME: octets_per_byte. */
310fd250
L
10053 if (! (o->flags & SEC_EXCLUDE))
10054 {
10055 file_ptr offset = (file_ptr) o->output_offset;
10056 bfd_size_type todo = o->size;
10057 if ((o->flags & SEC_ELF_REVERSE_COPY))
10058 {
10059 /* Reverse-copy input section to output. */
10060 do
10061 {
10062 todo -= address_size;
10063 if (! bfd_set_section_contents (output_bfd,
10064 o->output_section,
10065 contents + todo,
10066 offset,
10067 address_size))
10068 return FALSE;
10069 if (todo == 0)
10070 break;
10071 offset += address_size;
10072 }
10073 while (1);
10074 }
10075 else if (! bfd_set_section_contents (output_bfd,
10076 o->output_section,
10077 contents,
10078 offset, todo))
10079 return FALSE;
10080 }
c152c796
AM
10081 }
10082 break;
10083 }
10084 }
10085
10086 return TRUE;
10087}
10088
10089/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10090 requested by the linker, and does not come from any input file. This
c152c796
AM
10091 is used to build constructor and destructor tables when linking
10092 with -Ur. */
10093
10094static bfd_boolean
10095elf_reloc_link_order (bfd *output_bfd,
10096 struct bfd_link_info *info,
10097 asection *output_section,
10098 struct bfd_link_order *link_order)
10099{
10100 reloc_howto_type *howto;
10101 long indx;
10102 bfd_vma offset;
10103 bfd_vma addend;
d4730f92 10104 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10105 struct elf_link_hash_entry **rel_hash_ptr;
10106 Elf_Internal_Shdr *rel_hdr;
10107 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10108 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10109 bfd_byte *erel;
10110 unsigned int i;
d4730f92 10111 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10112
10113 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10114 if (howto == NULL)
10115 {
10116 bfd_set_error (bfd_error_bad_value);
10117 return FALSE;
10118 }
10119
10120 addend = link_order->u.reloc.p->addend;
10121
d4730f92
BS
10122 if (esdo->rel.hdr)
10123 reldata = &esdo->rel;
10124 else if (esdo->rela.hdr)
10125 reldata = &esdo->rela;
10126 else
10127 {
10128 reldata = NULL;
10129 BFD_ASSERT (0);
10130 }
10131
c152c796 10132 /* Figure out the symbol index. */
d4730f92 10133 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10134 if (link_order->type == bfd_section_reloc_link_order)
10135 {
10136 indx = link_order->u.reloc.p->u.section->target_index;
10137 BFD_ASSERT (indx != 0);
10138 *rel_hash_ptr = NULL;
10139 }
10140 else
10141 {
10142 struct elf_link_hash_entry *h;
10143
10144 /* Treat a reloc against a defined symbol as though it were
10145 actually against the section. */
10146 h = ((struct elf_link_hash_entry *)
10147 bfd_wrapped_link_hash_lookup (output_bfd, info,
10148 link_order->u.reloc.p->u.name,
10149 FALSE, FALSE, TRUE));
10150 if (h != NULL
10151 && (h->root.type == bfd_link_hash_defined
10152 || h->root.type == bfd_link_hash_defweak))
10153 {
10154 asection *section;
10155
10156 section = h->root.u.def.section;
10157 indx = section->output_section->target_index;
10158 *rel_hash_ptr = NULL;
10159 /* It seems that we ought to add the symbol value to the
10160 addend here, but in practice it has already been added
10161 because it was passed to constructor_callback. */
10162 addend += section->output_section->vma + section->output_offset;
10163 }
10164 else if (h != NULL)
10165 {
10166 /* Setting the index to -2 tells elf_link_output_extsym that
10167 this symbol is used by a reloc. */
10168 h->indx = -2;
10169 *rel_hash_ptr = h;
10170 indx = 0;
10171 }
10172 else
10173 {
10174 if (! ((*info->callbacks->unattached_reloc)
10175 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10176 return FALSE;
10177 indx = 0;
10178 }
10179 }
10180
10181 /* If this is an inplace reloc, we must write the addend into the
10182 object file. */
10183 if (howto->partial_inplace && addend != 0)
10184 {
10185 bfd_size_type size;
10186 bfd_reloc_status_type rstat;
10187 bfd_byte *buf;
10188 bfd_boolean ok;
10189 const char *sym_name;
10190
a50b1753
NC
10191 size = (bfd_size_type) bfd_get_reloc_size (howto);
10192 buf = (bfd_byte *) bfd_zmalloc (size);
c152c796
AM
10193 if (buf == NULL)
10194 return FALSE;
10195 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10196 switch (rstat)
10197 {
10198 case bfd_reloc_ok:
10199 break;
10200
10201 default:
10202 case bfd_reloc_outofrange:
10203 abort ();
10204
10205 case bfd_reloc_overflow:
10206 if (link_order->type == bfd_section_reloc_link_order)
10207 sym_name = bfd_section_name (output_bfd,
10208 link_order->u.reloc.p->u.section);
10209 else
10210 sym_name = link_order->u.reloc.p->u.name;
10211 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
10212 (info, NULL, sym_name, howto->name, addend, NULL,
10213 NULL, (bfd_vma) 0)))
c152c796
AM
10214 {
10215 free (buf);
10216 return FALSE;
10217 }
10218 break;
10219 }
10220 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10221 link_order->offset, size);
10222 free (buf);
10223 if (! ok)
10224 return FALSE;
10225 }
10226
10227 /* The address of a reloc is relative to the section in a
10228 relocatable file, and is a virtual address in an executable
10229 file. */
10230 offset = link_order->offset;
10231 if (! info->relocatable)
10232 offset += output_section->vma;
10233
10234 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10235 {
10236 irel[i].r_offset = offset;
10237 irel[i].r_info = 0;
10238 irel[i].r_addend = 0;
10239 }
10240 if (bed->s->arch_size == 32)
10241 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10242 else
10243 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10244
d4730f92 10245 rel_hdr = reldata->hdr;
c152c796
AM
10246 erel = rel_hdr->contents;
10247 if (rel_hdr->sh_type == SHT_REL)
10248 {
d4730f92 10249 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10250 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10251 }
10252 else
10253 {
10254 irel[0].r_addend = addend;
d4730f92 10255 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10256 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10257 }
10258
d4730f92 10259 ++reldata->count;
c152c796
AM
10260
10261 return TRUE;
10262}
10263
0b52efa6
PB
10264
10265/* Get the output vma of the section pointed to by the sh_link field. */
10266
10267static bfd_vma
10268elf_get_linked_section_vma (struct bfd_link_order *p)
10269{
10270 Elf_Internal_Shdr **elf_shdrp;
10271 asection *s;
10272 int elfsec;
10273
10274 s = p->u.indirect.section;
10275 elf_shdrp = elf_elfsections (s->owner);
10276 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10277 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10278 /* PR 290:
10279 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10280 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10281 sh_info fields. Hence we could get the situation
10282 where elfsec is 0. */
10283 if (elfsec == 0)
10284 {
10285 const struct elf_backend_data *bed
10286 = get_elf_backend_data (s->owner);
10287 if (bed->link_order_error_handler)
d003868e
AM
10288 bed->link_order_error_handler
10289 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10290 return 0;
10291 }
10292 else
10293 {
10294 s = elf_shdrp[elfsec]->bfd_section;
10295 return s->output_section->vma + s->output_offset;
10296 }
0b52efa6
PB
10297}
10298
10299
10300/* Compare two sections based on the locations of the sections they are
10301 linked to. Used by elf_fixup_link_order. */
10302
10303static int
10304compare_link_order (const void * a, const void * b)
10305{
10306 bfd_vma apos;
10307 bfd_vma bpos;
10308
10309 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10310 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10311 if (apos < bpos)
10312 return -1;
10313 return apos > bpos;
10314}
10315
10316
10317/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10318 order as their linked sections. Returns false if this could not be done
10319 because an output section includes both ordered and unordered
10320 sections. Ideally we'd do this in the linker proper. */
10321
10322static bfd_boolean
10323elf_fixup_link_order (bfd *abfd, asection *o)
10324{
10325 int seen_linkorder;
10326 int seen_other;
10327 int n;
10328 struct bfd_link_order *p;
10329 bfd *sub;
10330 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10331 unsigned elfsec;
0b52efa6 10332 struct bfd_link_order **sections;
d33cdfe3 10333 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10334 bfd_vma offset;
3b36f7e6 10335
d33cdfe3
L
10336 other_sec = NULL;
10337 linkorder_sec = NULL;
0b52efa6
PB
10338 seen_other = 0;
10339 seen_linkorder = 0;
8423293d 10340 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10341 {
d33cdfe3 10342 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10343 {
10344 s = p->u.indirect.section;
d33cdfe3
L
10345 sub = s->owner;
10346 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10347 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10348 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10349 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10350 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10351 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10352 {
10353 seen_linkorder++;
10354 linkorder_sec = s;
10355 }
0b52efa6 10356 else
d33cdfe3
L
10357 {
10358 seen_other++;
10359 other_sec = s;
10360 }
0b52efa6
PB
10361 }
10362 else
10363 seen_other++;
d33cdfe3
L
10364
10365 if (seen_other && seen_linkorder)
10366 {
10367 if (other_sec && linkorder_sec)
10368 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10369 o, linkorder_sec,
10370 linkorder_sec->owner, other_sec,
10371 other_sec->owner);
10372 else
10373 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10374 o);
10375 bfd_set_error (bfd_error_bad_value);
10376 return FALSE;
10377 }
0b52efa6
PB
10378 }
10379
10380 if (!seen_linkorder)
10381 return TRUE;
10382
0b52efa6 10383 sections = (struct bfd_link_order **)
14b1c01e
AM
10384 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10385 if (sections == NULL)
10386 return FALSE;
0b52efa6 10387 seen_linkorder = 0;
3b36f7e6 10388
8423293d 10389 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
10390 {
10391 sections[seen_linkorder++] = p;
10392 }
10393 /* Sort the input sections in the order of their linked section. */
10394 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10395 compare_link_order);
10396
10397 /* Change the offsets of the sections. */
10398 offset = 0;
10399 for (n = 0; n < seen_linkorder; n++)
10400 {
10401 s = sections[n]->u.indirect.section;
461686a3 10402 offset &= ~(bfd_vma) 0 << s->alignment_power;
0b52efa6
PB
10403 s->output_offset = offset;
10404 sections[n]->offset = offset;
5dabe785 10405 /* FIXME: octets_per_byte. */
0b52efa6
PB
10406 offset += sections[n]->size;
10407 }
10408
4dd07732 10409 free (sections);
0b52efa6
PB
10410 return TRUE;
10411}
10412
9f7c3e5e
AM
10413static void
10414elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10415{
10416 asection *o;
10417
10418 if (flinfo->symstrtab != NULL)
10419 _bfd_stringtab_free (flinfo->symstrtab);
10420 if (flinfo->contents != NULL)
10421 free (flinfo->contents);
10422 if (flinfo->external_relocs != NULL)
10423 free (flinfo->external_relocs);
10424 if (flinfo->internal_relocs != NULL)
10425 free (flinfo->internal_relocs);
10426 if (flinfo->external_syms != NULL)
10427 free (flinfo->external_syms);
10428 if (flinfo->locsym_shndx != NULL)
10429 free (flinfo->locsym_shndx);
10430 if (flinfo->internal_syms != NULL)
10431 free (flinfo->internal_syms);
10432 if (flinfo->indices != NULL)
10433 free (flinfo->indices);
10434 if (flinfo->sections != NULL)
10435 free (flinfo->sections);
10436 if (flinfo->symbuf != NULL)
10437 free (flinfo->symbuf);
10438 if (flinfo->symshndxbuf != NULL)
10439 free (flinfo->symshndxbuf);
10440 for (o = obfd->sections; o != NULL; o = o->next)
10441 {
10442 struct bfd_elf_section_data *esdo = elf_section_data (o);
10443 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10444 free (esdo->rel.hashes);
10445 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10446 free (esdo->rela.hashes);
10447 }
10448}
0b52efa6 10449
c152c796
AM
10450/* Do the final step of an ELF link. */
10451
10452bfd_boolean
10453bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10454{
10455 bfd_boolean dynamic;
10456 bfd_boolean emit_relocs;
10457 bfd *dynobj;
8b127cbc 10458 struct elf_final_link_info flinfo;
91d6fa6a
NC
10459 asection *o;
10460 struct bfd_link_order *p;
10461 bfd *sub;
c152c796
AM
10462 bfd_size_type max_contents_size;
10463 bfd_size_type max_external_reloc_size;
10464 bfd_size_type max_internal_reloc_count;
10465 bfd_size_type max_sym_count;
10466 bfd_size_type max_sym_shndx_count;
10467 file_ptr off;
10468 Elf_Internal_Sym elfsym;
10469 unsigned int i;
10470 Elf_Internal_Shdr *symtab_hdr;
10471 Elf_Internal_Shdr *symtab_shndx_hdr;
10472 Elf_Internal_Shdr *symstrtab_hdr;
10473 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10474 struct elf_outext_info eoinfo;
10475 bfd_boolean merged;
10476 size_t relativecount = 0;
10477 asection *reldyn = 0;
10478 bfd_size_type amt;
104d59d1
JM
10479 asection *attr_section = NULL;
10480 bfd_vma attr_size = 0;
10481 const char *std_attrs_section;
c152c796
AM
10482
10483 if (! is_elf_hash_table (info->hash))
10484 return FALSE;
10485
10486 if (info->shared)
10487 abfd->flags |= DYNAMIC;
10488
10489 dynamic = elf_hash_table (info)->dynamic_sections_created;
10490 dynobj = elf_hash_table (info)->dynobj;
10491
10492 emit_relocs = (info->relocatable
a4676736 10493 || info->emitrelocations);
c152c796 10494
8b127cbc
AM
10495 flinfo.info = info;
10496 flinfo.output_bfd = abfd;
10497 flinfo.symstrtab = _bfd_elf_stringtab_init ();
10498 if (flinfo.symstrtab == NULL)
c152c796
AM
10499 return FALSE;
10500
10501 if (! dynamic)
10502 {
8b127cbc
AM
10503 flinfo.dynsym_sec = NULL;
10504 flinfo.hash_sec = NULL;
10505 flinfo.symver_sec = NULL;
c152c796
AM
10506 }
10507 else
10508 {
3d4d4302
AM
10509 flinfo.dynsym_sec = bfd_get_linker_section (dynobj, ".dynsym");
10510 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 10511 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 10512 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
10513 /* Note that it is OK if symver_sec is NULL. */
10514 }
10515
8b127cbc
AM
10516 flinfo.contents = NULL;
10517 flinfo.external_relocs = NULL;
10518 flinfo.internal_relocs = NULL;
10519 flinfo.external_syms = NULL;
10520 flinfo.locsym_shndx = NULL;
10521 flinfo.internal_syms = NULL;
10522 flinfo.indices = NULL;
10523 flinfo.sections = NULL;
10524 flinfo.symbuf = NULL;
10525 flinfo.symshndxbuf = NULL;
10526 flinfo.symbuf_count = 0;
10527 flinfo.shndxbuf_size = 0;
ffbc01cc 10528 flinfo.filesym_count = 0;
c152c796 10529
104d59d1
JM
10530 /* The object attributes have been merged. Remove the input
10531 sections from the link, and set the contents of the output
10532 secton. */
10533 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10534 for (o = abfd->sections; o != NULL; o = o->next)
10535 {
10536 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10537 || strcmp (o->name, ".gnu.attributes") == 0)
10538 {
10539 for (p = o->map_head.link_order; p != NULL; p = p->next)
10540 {
10541 asection *input_section;
10542
10543 if (p->type != bfd_indirect_link_order)
10544 continue;
10545 input_section = p->u.indirect.section;
10546 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10547 elf_link_input_bfd ignores this section. */
10548 input_section->flags &= ~SEC_HAS_CONTENTS;
10549 }
a0c8462f 10550
104d59d1
JM
10551 attr_size = bfd_elf_obj_attr_size (abfd);
10552 if (attr_size)
10553 {
10554 bfd_set_section_size (abfd, o, attr_size);
10555 attr_section = o;
10556 /* Skip this section later on. */
10557 o->map_head.link_order = NULL;
10558 }
10559 else
10560 o->flags |= SEC_EXCLUDE;
10561 }
10562 }
10563
c152c796
AM
10564 /* Count up the number of relocations we will output for each output
10565 section, so that we know the sizes of the reloc sections. We
10566 also figure out some maximum sizes. */
10567 max_contents_size = 0;
10568 max_external_reloc_size = 0;
10569 max_internal_reloc_count = 0;
10570 max_sym_count = 0;
10571 max_sym_shndx_count = 0;
10572 merged = FALSE;
10573 for (o = abfd->sections; o != NULL; o = o->next)
10574 {
10575 struct bfd_elf_section_data *esdo = elf_section_data (o);
10576 o->reloc_count = 0;
10577
8423293d 10578 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10579 {
10580 unsigned int reloc_count = 0;
10581 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
10582
10583 if (p->type == bfd_section_reloc_link_order
10584 || p->type == bfd_symbol_reloc_link_order)
10585 reloc_count = 1;
10586 else if (p->type == bfd_indirect_link_order)
10587 {
10588 asection *sec;
10589
10590 sec = p->u.indirect.section;
10591 esdi = elf_section_data (sec);
10592
10593 /* Mark all sections which are to be included in the
10594 link. This will normally be every section. We need
10595 to do this so that we can identify any sections which
10596 the linker has decided to not include. */
10597 sec->linker_mark = TRUE;
10598
10599 if (sec->flags & SEC_MERGE)
10600 merged = TRUE;
10601
aed64b35
L
10602 if (esdo->this_hdr.sh_type == SHT_REL
10603 || esdo->this_hdr.sh_type == SHT_RELA)
10604 /* Some backends use reloc_count in relocation sections
10605 to count particular types of relocs. Of course,
10606 reloc sections themselves can't have relocations. */
10607 reloc_count = 0;
10608 else if (info->relocatable || info->emitrelocations)
c152c796
AM
10609 reloc_count = sec->reloc_count;
10610 else if (bed->elf_backend_count_relocs)
58217f29 10611 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 10612
eea6121a
AM
10613 if (sec->rawsize > max_contents_size)
10614 max_contents_size = sec->rawsize;
10615 if (sec->size > max_contents_size)
10616 max_contents_size = sec->size;
c152c796
AM
10617
10618 /* We are interested in just local symbols, not all
10619 symbols. */
10620 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10621 && (sec->owner->flags & DYNAMIC) == 0)
10622 {
10623 size_t sym_count;
10624
10625 if (elf_bad_symtab (sec->owner))
10626 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10627 / bed->s->sizeof_sym);
10628 else
10629 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10630
10631 if (sym_count > max_sym_count)
10632 max_sym_count = sym_count;
10633
10634 if (sym_count > max_sym_shndx_count
10635 && elf_symtab_shndx (sec->owner) != 0)
10636 max_sym_shndx_count = sym_count;
10637
10638 if ((sec->flags & SEC_RELOC) != 0)
10639 {
d4730f92 10640 size_t ext_size = 0;
c152c796 10641
d4730f92
BS
10642 if (esdi->rel.hdr != NULL)
10643 ext_size = esdi->rel.hdr->sh_size;
10644 if (esdi->rela.hdr != NULL)
10645 ext_size += esdi->rela.hdr->sh_size;
7326c758 10646
c152c796
AM
10647 if (ext_size > max_external_reloc_size)
10648 max_external_reloc_size = ext_size;
10649 if (sec->reloc_count > max_internal_reloc_count)
10650 max_internal_reloc_count = sec->reloc_count;
10651 }
10652 }
10653 }
10654
10655 if (reloc_count == 0)
10656 continue;
10657
10658 o->reloc_count += reloc_count;
10659
d4730f92
BS
10660 if (p->type == bfd_indirect_link_order
10661 && (info->relocatable || info->emitrelocations))
c152c796 10662 {
d4730f92
BS
10663 if (esdi->rel.hdr)
10664 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
10665 if (esdi->rela.hdr)
10666 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
10667 }
10668 else
10669 {
10670 if (o->use_rela_p)
10671 esdo->rela.count += reloc_count;
2c2b4ed4 10672 else
d4730f92 10673 esdo->rel.count += reloc_count;
c152c796 10674 }
c152c796
AM
10675 }
10676
10677 if (o->reloc_count > 0)
10678 o->flags |= SEC_RELOC;
10679 else
10680 {
10681 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10682 set it (this is probably a bug) and if it is set
10683 assign_section_numbers will create a reloc section. */
10684 o->flags &=~ SEC_RELOC;
10685 }
10686
10687 /* If the SEC_ALLOC flag is not set, force the section VMA to
10688 zero. This is done in elf_fake_sections as well, but forcing
10689 the VMA to 0 here will ensure that relocs against these
10690 sections are handled correctly. */
10691 if ((o->flags & SEC_ALLOC) == 0
10692 && ! o->user_set_vma)
10693 o->vma = 0;
10694 }
10695
10696 if (! info->relocatable && merged)
10697 elf_link_hash_traverse (elf_hash_table (info),
10698 _bfd_elf_link_sec_merge_syms, abfd);
10699
10700 /* Figure out the file positions for everything but the symbol table
10701 and the relocs. We set symcount to force assign_section_numbers
10702 to create a symbol table. */
10703 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10704 BFD_ASSERT (! abfd->output_has_begun);
10705 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10706 goto error_return;
10707
ee75fd95 10708 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
10709 for (o = abfd->sections; o != NULL; o = o->next)
10710 {
d4730f92 10711 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
10712 if ((o->flags & SEC_RELOC) != 0)
10713 {
d4730f92
BS
10714 if (esdo->rel.hdr
10715 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
10716 goto error_return;
10717
d4730f92
BS
10718 if (esdo->rela.hdr
10719 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
10720 goto error_return;
10721 }
10722
10723 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10724 to count upwards while actually outputting the relocations. */
d4730f92
BS
10725 esdo->rel.count = 0;
10726 esdo->rela.count = 0;
c152c796
AM
10727 }
10728
10729 _bfd_elf_assign_file_positions_for_relocs (abfd);
10730
10731 /* We have now assigned file positions for all the sections except
10732 .symtab and .strtab. We start the .symtab section at the current
10733 file position, and write directly to it. We build the .strtab
10734 section in memory. */
10735 bfd_get_symcount (abfd) = 0;
10736 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10737 /* sh_name is set in prep_headers. */
10738 symtab_hdr->sh_type = SHT_SYMTAB;
10739 /* sh_flags, sh_addr and sh_size all start off zero. */
10740 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10741 /* sh_link is set in assign_section_numbers. */
10742 /* sh_info is set below. */
10743 /* sh_offset is set just below. */
72de5009 10744 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 10745
12bd6957 10746 off = elf_next_file_pos (abfd);
c152c796
AM
10747 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10748
12bd6957 10749 /* Note that at this point elf_next_file_pos (abfd) is
c152c796
AM
10750 incorrect. We do not yet know the size of the .symtab section.
10751 We correct next_file_pos below, after we do know the size. */
10752
10753 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10754 continuously seeking to the right position in the file. */
10755 if (! info->keep_memory || max_sym_count < 20)
8b127cbc 10756 flinfo.symbuf_size = 20;
c152c796 10757 else
8b127cbc
AM
10758 flinfo.symbuf_size = max_sym_count;
10759 amt = flinfo.symbuf_size;
c152c796 10760 amt *= bed->s->sizeof_sym;
8b127cbc
AM
10761 flinfo.symbuf = (bfd_byte *) bfd_malloc (amt);
10762 if (flinfo.symbuf == NULL)
c152c796 10763 goto error_return;
4fbb74a6 10764 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
c152c796
AM
10765 {
10766 /* Wild guess at number of output symbols. realloc'd as needed. */
10767 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8b127cbc 10768 flinfo.shndxbuf_size = amt;
c152c796 10769 amt *= sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
10770 flinfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10771 if (flinfo.symshndxbuf == NULL)
c152c796
AM
10772 goto error_return;
10773 }
10774
10775 /* Start writing out the symbol table. The first symbol is always a
10776 dummy symbol. */
10777 if (info->strip != strip_all
10778 || emit_relocs)
10779 {
10780 elfsym.st_value = 0;
10781 elfsym.st_size = 0;
10782 elfsym.st_info = 0;
10783 elfsym.st_other = 0;
10784 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 10785 elfsym.st_target_internal = 0;
8b127cbc 10786 if (elf_link_output_sym (&flinfo, NULL, &elfsym, bfd_und_section_ptr,
6e0b88f1 10787 NULL) != 1)
c152c796
AM
10788 goto error_return;
10789 }
10790
c152c796
AM
10791 /* Output a symbol for each section. We output these even if we are
10792 discarding local symbols, since they are used for relocs. These
10793 symbols have no names. We store the index of each one in the
10794 index field of the section, so that we can find it again when
10795 outputting relocs. */
10796 if (info->strip != strip_all
10797 || emit_relocs)
10798 {
10799 elfsym.st_size = 0;
10800 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10801 elfsym.st_other = 0;
f0b5bb34 10802 elfsym.st_value = 0;
35fc36a8 10803 elfsym.st_target_internal = 0;
c152c796
AM
10804 for (i = 1; i < elf_numsections (abfd); i++)
10805 {
10806 o = bfd_section_from_elf_index (abfd, i);
10807 if (o != NULL)
f0b5bb34
AM
10808 {
10809 o->target_index = bfd_get_symcount (abfd);
10810 elfsym.st_shndx = i;
10811 if (!info->relocatable)
10812 elfsym.st_value = o->vma;
8b127cbc 10813 if (elf_link_output_sym (&flinfo, NULL, &elfsym, o, NULL) != 1)
f0b5bb34
AM
10814 goto error_return;
10815 }
c152c796
AM
10816 }
10817 }
10818
10819 /* Allocate some memory to hold information read in from the input
10820 files. */
10821 if (max_contents_size != 0)
10822 {
8b127cbc
AM
10823 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
10824 if (flinfo.contents == NULL)
c152c796
AM
10825 goto error_return;
10826 }
10827
10828 if (max_external_reloc_size != 0)
10829 {
8b127cbc
AM
10830 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
10831 if (flinfo.external_relocs == NULL)
c152c796
AM
10832 goto error_return;
10833 }
10834
10835 if (max_internal_reloc_count != 0)
10836 {
10837 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10838 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
10839 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
10840 if (flinfo.internal_relocs == NULL)
c152c796
AM
10841 goto error_return;
10842 }
10843
10844 if (max_sym_count != 0)
10845 {
10846 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
10847 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
10848 if (flinfo.external_syms == NULL)
c152c796
AM
10849 goto error_return;
10850
10851 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
10852 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
10853 if (flinfo.internal_syms == NULL)
c152c796
AM
10854 goto error_return;
10855
10856 amt = max_sym_count * sizeof (long);
8b127cbc
AM
10857 flinfo.indices = (long int *) bfd_malloc (amt);
10858 if (flinfo.indices == NULL)
c152c796
AM
10859 goto error_return;
10860
10861 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
10862 flinfo.sections = (asection **) bfd_malloc (amt);
10863 if (flinfo.sections == NULL)
c152c796
AM
10864 goto error_return;
10865 }
10866
10867 if (max_sym_shndx_count != 0)
10868 {
10869 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
10870 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
10871 if (flinfo.locsym_shndx == NULL)
c152c796
AM
10872 goto error_return;
10873 }
10874
10875 if (elf_hash_table (info)->tls_sec)
10876 {
10877 bfd_vma base, end = 0;
10878 asection *sec;
10879
10880 for (sec = elf_hash_table (info)->tls_sec;
10881 sec && (sec->flags & SEC_THREAD_LOCAL);
10882 sec = sec->next)
10883 {
3a800eb9 10884 bfd_size_type size = sec->size;
c152c796 10885
3a800eb9
AM
10886 if (size == 0
10887 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 10888 {
91d6fa6a
NC
10889 struct bfd_link_order *ord = sec->map_tail.link_order;
10890
10891 if (ord != NULL)
10892 size = ord->offset + ord->size;
c152c796
AM
10893 }
10894 end = sec->vma + size;
10895 }
10896 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
10897 /* Only align end of TLS section if static TLS doesn't have special
10898 alignment requirements. */
10899 if (bed->static_tls_alignment == 1)
10900 end = align_power (end,
10901 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
10902 elf_hash_table (info)->tls_size = end - base;
10903 }
10904
0b52efa6
PB
10905 /* Reorder SHF_LINK_ORDER sections. */
10906 for (o = abfd->sections; o != NULL; o = o->next)
10907 {
10908 if (!elf_fixup_link_order (abfd, o))
10909 return FALSE;
10910 }
10911
c152c796
AM
10912 /* Since ELF permits relocations to be against local symbols, we
10913 must have the local symbols available when we do the relocations.
10914 Since we would rather only read the local symbols once, and we
10915 would rather not keep them in memory, we handle all the
10916 relocations for a single input file at the same time.
10917
10918 Unfortunately, there is no way to know the total number of local
10919 symbols until we have seen all of them, and the local symbol
10920 indices precede the global symbol indices. This means that when
10921 we are generating relocatable output, and we see a reloc against
10922 a global symbol, we can not know the symbol index until we have
10923 finished examining all the local symbols to see which ones we are
10924 going to output. To deal with this, we keep the relocations in
10925 memory, and don't output them until the end of the link. This is
10926 an unfortunate waste of memory, but I don't see a good way around
10927 it. Fortunately, it only happens when performing a relocatable
10928 link, which is not the common case. FIXME: If keep_memory is set
10929 we could write the relocs out and then read them again; I don't
10930 know how bad the memory loss will be. */
10931
10932 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10933 sub->output_has_begun = FALSE;
10934 for (o = abfd->sections; o != NULL; o = o->next)
10935 {
8423293d 10936 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10937 {
10938 if (p->type == bfd_indirect_link_order
10939 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10940 == bfd_target_elf_flavour)
10941 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10942 {
10943 if (! sub->output_has_begun)
10944 {
8b127cbc 10945 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
10946 goto error_return;
10947 sub->output_has_begun = TRUE;
10948 }
10949 }
10950 else if (p->type == bfd_section_reloc_link_order
10951 || p->type == bfd_symbol_reloc_link_order)
10952 {
10953 if (! elf_reloc_link_order (abfd, info, o, p))
10954 goto error_return;
10955 }
10956 else
10957 {
10958 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
10959 {
10960 if (p->type == bfd_indirect_link_order
10961 && (bfd_get_flavour (sub)
10962 == bfd_target_elf_flavour)
10963 && (elf_elfheader (sub)->e_ident[EI_CLASS]
10964 != bed->s->elfclass))
10965 {
10966 const char *iclass, *oclass;
10967
10968 if (bed->s->elfclass == ELFCLASS64)
10969 {
10970 iclass = "ELFCLASS32";
10971 oclass = "ELFCLASS64";
10972 }
10973 else
10974 {
10975 iclass = "ELFCLASS64";
10976 oclass = "ELFCLASS32";
10977 }
10978
10979 bfd_set_error (bfd_error_wrong_format);
10980 (*_bfd_error_handler)
10981 (_("%B: file class %s incompatible with %s"),
10982 sub, iclass, oclass);
10983 }
10984
10985 goto error_return;
10986 }
c152c796
AM
10987 }
10988 }
10989 }
10990
c0f00686
L
10991 /* Free symbol buffer if needed. */
10992 if (!info->reduce_memory_overheads)
10993 {
10994 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3fcd97f1
JJ
10995 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10996 && elf_tdata (sub)->symbuf)
c0f00686
L
10997 {
10998 free (elf_tdata (sub)->symbuf);
10999 elf_tdata (sub)->symbuf = NULL;
11000 }
11001 }
11002
c152c796
AM
11003 /* Output any global symbols that got converted to local in a
11004 version script or due to symbol visibility. We do this in a
11005 separate step since ELF requires all local symbols to appear
11006 prior to any global symbols. FIXME: We should only do this if
11007 some global symbols were, in fact, converted to become local.
11008 FIXME: Will this work correctly with the Irix 5 linker? */
11009 eoinfo.failed = FALSE;
8b127cbc 11010 eoinfo.flinfo = &flinfo;
c152c796 11011 eoinfo.localsyms = TRUE;
ffbc01cc
AM
11012 eoinfo.need_second_pass = FALSE;
11013 eoinfo.second_pass = FALSE;
34a79995 11014 eoinfo.file_sym_done = FALSE;
7686d77d 11015 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11016 if (eoinfo.failed)
11017 return FALSE;
11018
ffbc01cc
AM
11019 if (eoinfo.need_second_pass)
11020 {
11021 eoinfo.second_pass = TRUE;
11022 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11023 if (eoinfo.failed)
11024 return FALSE;
11025 }
11026
4e617b1e
PB
11027 /* If backend needs to output some local symbols not present in the hash
11028 table, do it now. */
11029 if (bed->elf_backend_output_arch_local_syms)
11030 {
6e0b88f1 11031 typedef int (*out_sym_func)
4e617b1e
PB
11032 (void *, const char *, Elf_Internal_Sym *, asection *,
11033 struct elf_link_hash_entry *);
11034
11035 if (! ((*bed->elf_backend_output_arch_local_syms)
8b127cbc 11036 (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym)))
4e617b1e
PB
11037 return FALSE;
11038 }
11039
c152c796
AM
11040 /* That wrote out all the local symbols. Finish up the symbol table
11041 with the global symbols. Even if we want to strip everything we
11042 can, we still need to deal with those global symbols that got
11043 converted to local in a version script. */
11044
11045 /* The sh_info field records the index of the first non local symbol. */
11046 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11047
11048 if (dynamic
8b127cbc
AM
11049 && flinfo.dynsym_sec != NULL
11050 && flinfo.dynsym_sec->output_section != bfd_abs_section_ptr)
c152c796
AM
11051 {
11052 Elf_Internal_Sym sym;
8b127cbc 11053 bfd_byte *dynsym = flinfo.dynsym_sec->contents;
c152c796
AM
11054 long last_local = 0;
11055
11056 /* Write out the section symbols for the output sections. */
67687978 11057 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
11058 {
11059 asection *s;
11060
11061 sym.st_size = 0;
11062 sym.st_name = 0;
11063 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11064 sym.st_other = 0;
35fc36a8 11065 sym.st_target_internal = 0;
c152c796
AM
11066
11067 for (s = abfd->sections; s != NULL; s = s->next)
11068 {
11069 int indx;
11070 bfd_byte *dest;
11071 long dynindx;
11072
c152c796 11073 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11074 if (dynindx <= 0)
11075 continue;
11076 indx = elf_section_data (s)->this_idx;
c152c796
AM
11077 BFD_ASSERT (indx > 0);
11078 sym.st_shndx = indx;
c0d5a53d
L
11079 if (! check_dynsym (abfd, &sym))
11080 return FALSE;
c152c796
AM
11081 sym.st_value = s->vma;
11082 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
11083 if (last_local < dynindx)
11084 last_local = dynindx;
c152c796
AM
11085 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11086 }
c152c796
AM
11087 }
11088
11089 /* Write out the local dynsyms. */
11090 if (elf_hash_table (info)->dynlocal)
11091 {
11092 struct elf_link_local_dynamic_entry *e;
11093 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11094 {
11095 asection *s;
11096 bfd_byte *dest;
11097
935bd1e0 11098 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11099 Note that we saved a word of storage and overwrote
11100 the original st_name with the dynstr_index. */
11101 sym = e->isym;
935bd1e0 11102 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11103
cb33740c
AM
11104 s = bfd_section_from_elf_index (e->input_bfd,
11105 e->isym.st_shndx);
11106 if (s != NULL)
c152c796 11107 {
c152c796
AM
11108 sym.st_shndx =
11109 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11110 if (! check_dynsym (abfd, &sym))
11111 return FALSE;
c152c796
AM
11112 sym.st_value = (s->output_section->vma
11113 + s->output_offset
11114 + e->isym.st_value);
11115 }
11116
11117 if (last_local < e->dynindx)
11118 last_local = e->dynindx;
11119
11120 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11121 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11122 }
11123 }
11124
8b127cbc 11125 elf_section_data (flinfo.dynsym_sec->output_section)->this_hdr.sh_info =
c152c796
AM
11126 last_local + 1;
11127 }
11128
11129 /* We get the global symbols from the hash table. */
11130 eoinfo.failed = FALSE;
11131 eoinfo.localsyms = FALSE;
8b127cbc 11132 eoinfo.flinfo = &flinfo;
7686d77d 11133 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11134 if (eoinfo.failed)
11135 return FALSE;
11136
11137 /* If backend needs to output some symbols not present in the hash
11138 table, do it now. */
11139 if (bed->elf_backend_output_arch_syms)
11140 {
6e0b88f1 11141 typedef int (*out_sym_func)
c152c796
AM
11142 (void *, const char *, Elf_Internal_Sym *, asection *,
11143 struct elf_link_hash_entry *);
11144
11145 if (! ((*bed->elf_backend_output_arch_syms)
8b127cbc 11146 (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym)))
c152c796
AM
11147 return FALSE;
11148 }
11149
11150 /* Flush all symbols to the file. */
8b127cbc 11151 if (! elf_link_flush_output_syms (&flinfo, bed))
c152c796
AM
11152 return FALSE;
11153
11154 /* Now we know the size of the symtab section. */
11155 off += symtab_hdr->sh_size;
11156
11157 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
11158 if (symtab_shndx_hdr->sh_name != 0)
11159 {
11160 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11161 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11162 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11163 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11164 symtab_shndx_hdr->sh_size = amt;
11165
11166 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11167 off, TRUE);
11168
11169 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8b127cbc 11170 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
c152c796
AM
11171 return FALSE;
11172 }
11173
11174
11175 /* Finish up and write out the symbol string table (.strtab)
11176 section. */
11177 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11178 /* sh_name was set in prep_headers. */
11179 symstrtab_hdr->sh_type = SHT_STRTAB;
11180 symstrtab_hdr->sh_flags = 0;
11181 symstrtab_hdr->sh_addr = 0;
8b127cbc 11182 symstrtab_hdr->sh_size = _bfd_stringtab_size (flinfo.symstrtab);
c152c796
AM
11183 symstrtab_hdr->sh_entsize = 0;
11184 symstrtab_hdr->sh_link = 0;
11185 symstrtab_hdr->sh_info = 0;
11186 /* sh_offset is set just below. */
11187 symstrtab_hdr->sh_addralign = 1;
11188
11189 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
12bd6957 11190 elf_next_file_pos (abfd) = off;
c152c796
AM
11191
11192 if (bfd_get_symcount (abfd) > 0)
11193 {
11194 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8b127cbc 11195 || ! _bfd_stringtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
11196 return FALSE;
11197 }
11198
11199 /* Adjust the relocs to have the correct symbol indices. */
11200 for (o = abfd->sections; o != NULL; o = o->next)
11201 {
d4730f92 11202 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11203 if ((o->flags & SEC_RELOC) == 0)
11204 continue;
11205
d4730f92
BS
11206 if (esdo->rel.hdr != NULL)
11207 elf_link_adjust_relocs (abfd, &esdo->rel);
11208 if (esdo->rela.hdr != NULL)
11209 elf_link_adjust_relocs (abfd, &esdo->rela);
c152c796
AM
11210
11211 /* Set the reloc_count field to 0 to prevent write_relocs from
11212 trying to swap the relocs out itself. */
11213 o->reloc_count = 0;
11214 }
11215
11216 if (dynamic && info->combreloc && dynobj != NULL)
11217 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11218
11219 /* If we are linking against a dynamic object, or generating a
11220 shared library, finish up the dynamic linking information. */
11221 if (dynamic)
11222 {
11223 bfd_byte *dyncon, *dynconend;
11224
11225 /* Fix up .dynamic entries. */
3d4d4302 11226 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
11227 BFD_ASSERT (o != NULL);
11228
11229 dyncon = o->contents;
eea6121a 11230 dynconend = o->contents + o->size;
c152c796
AM
11231 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11232 {
11233 Elf_Internal_Dyn dyn;
11234 const char *name;
11235 unsigned int type;
11236
11237 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11238
11239 switch (dyn.d_tag)
11240 {
11241 default:
11242 continue;
11243 case DT_NULL:
11244 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11245 {
11246 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11247 {
11248 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11249 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11250 default: continue;
11251 }
11252 dyn.d_un.d_val = relativecount;
11253 relativecount = 0;
11254 break;
11255 }
11256 continue;
11257
11258 case DT_INIT:
11259 name = info->init_function;
11260 goto get_sym;
11261 case DT_FINI:
11262 name = info->fini_function;
11263 get_sym:
11264 {
11265 struct elf_link_hash_entry *h;
11266
11267 h = elf_link_hash_lookup (elf_hash_table (info), name,
11268 FALSE, FALSE, TRUE);
11269 if (h != NULL
11270 && (h->root.type == bfd_link_hash_defined
11271 || h->root.type == bfd_link_hash_defweak))
11272 {
bef26483 11273 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11274 o = h->root.u.def.section;
11275 if (o->output_section != NULL)
bef26483 11276 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11277 + o->output_offset);
11278 else
11279 {
11280 /* The symbol is imported from another shared
11281 library and does not apply to this one. */
bef26483 11282 dyn.d_un.d_ptr = 0;
c152c796
AM
11283 }
11284 break;
11285 }
11286 }
11287 continue;
11288
11289 case DT_PREINIT_ARRAYSZ:
11290 name = ".preinit_array";
11291 goto get_size;
11292 case DT_INIT_ARRAYSZ:
11293 name = ".init_array";
11294 goto get_size;
11295 case DT_FINI_ARRAYSZ:
11296 name = ".fini_array";
11297 get_size:
11298 o = bfd_get_section_by_name (abfd, name);
11299 if (o == NULL)
11300 {
11301 (*_bfd_error_handler)
d003868e 11302 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11303 goto error_return;
11304 }
eea6121a 11305 if (o->size == 0)
c152c796
AM
11306 (*_bfd_error_handler)
11307 (_("warning: %s section has zero size"), name);
eea6121a 11308 dyn.d_un.d_val = o->size;
c152c796
AM
11309 break;
11310
11311 case DT_PREINIT_ARRAY:
11312 name = ".preinit_array";
11313 goto get_vma;
11314 case DT_INIT_ARRAY:
11315 name = ".init_array";
11316 goto get_vma;
11317 case DT_FINI_ARRAY:
11318 name = ".fini_array";
11319 goto get_vma;
11320
11321 case DT_HASH:
11322 name = ".hash";
11323 goto get_vma;
fdc90cb4
JJ
11324 case DT_GNU_HASH:
11325 name = ".gnu.hash";
11326 goto get_vma;
c152c796
AM
11327 case DT_STRTAB:
11328 name = ".dynstr";
11329 goto get_vma;
11330 case DT_SYMTAB:
11331 name = ".dynsym";
11332 goto get_vma;
11333 case DT_VERDEF:
11334 name = ".gnu.version_d";
11335 goto get_vma;
11336 case DT_VERNEED:
11337 name = ".gnu.version_r";
11338 goto get_vma;
11339 case DT_VERSYM:
11340 name = ".gnu.version";
11341 get_vma:
11342 o = bfd_get_section_by_name (abfd, name);
11343 if (o == NULL)
11344 {
11345 (*_bfd_error_handler)
d003868e 11346 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11347 goto error_return;
11348 }
894891db
NC
11349 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11350 {
11351 (*_bfd_error_handler)
11352 (_("warning: section '%s' is being made into a note"), name);
11353 bfd_set_error (bfd_error_nonrepresentable_section);
11354 goto error_return;
11355 }
c152c796
AM
11356 dyn.d_un.d_ptr = o->vma;
11357 break;
11358
11359 case DT_REL:
11360 case DT_RELA:
11361 case DT_RELSZ:
11362 case DT_RELASZ:
11363 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11364 type = SHT_REL;
11365 else
11366 type = SHT_RELA;
11367 dyn.d_un.d_val = 0;
bef26483 11368 dyn.d_un.d_ptr = 0;
c152c796
AM
11369 for (i = 1; i < elf_numsections (abfd); i++)
11370 {
11371 Elf_Internal_Shdr *hdr;
11372
11373 hdr = elf_elfsections (abfd)[i];
11374 if (hdr->sh_type == type
11375 && (hdr->sh_flags & SHF_ALLOC) != 0)
11376 {
11377 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11378 dyn.d_un.d_val += hdr->sh_size;
11379 else
11380 {
bef26483
AM
11381 if (dyn.d_un.d_ptr == 0
11382 || hdr->sh_addr < dyn.d_un.d_ptr)
11383 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
11384 }
11385 }
11386 }
11387 break;
11388 }
11389 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11390 }
11391 }
11392
11393 /* If we have created any dynamic sections, then output them. */
11394 if (dynobj != NULL)
11395 {
11396 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11397 goto error_return;
11398
943284cc 11399 /* Check for DT_TEXTREL (late, in case the backend removes it). */
be7b303d
AM
11400 if (((info->warn_shared_textrel && info->shared)
11401 || info->error_textrel)
3d4d4302 11402 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
11403 {
11404 bfd_byte *dyncon, *dynconend;
11405
943284cc
DJ
11406 dyncon = o->contents;
11407 dynconend = o->contents + o->size;
11408 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11409 {
11410 Elf_Internal_Dyn dyn;
11411
11412 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11413
11414 if (dyn.d_tag == DT_TEXTREL)
11415 {
c192a133
AM
11416 if (info->error_textrel)
11417 info->callbacks->einfo
11418 (_("%P%X: read-only segment has dynamic relocations.\n"));
11419 else
11420 info->callbacks->einfo
11421 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
11422 break;
11423 }
11424 }
11425 }
11426
c152c796
AM
11427 for (o = dynobj->sections; o != NULL; o = o->next)
11428 {
11429 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11430 || o->size == 0
c152c796
AM
11431 || o->output_section == bfd_abs_section_ptr)
11432 continue;
11433 if ((o->flags & SEC_LINKER_CREATED) == 0)
11434 {
11435 /* At this point, we are only interested in sections
11436 created by _bfd_elf_link_create_dynamic_sections. */
11437 continue;
11438 }
3722b82f
AM
11439 if (elf_hash_table (info)->stab_info.stabstr == o)
11440 continue;
eea6121a
AM
11441 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11442 continue;
3d4d4302 11443 if (strcmp (o->name, ".dynstr") != 0)
c152c796 11444 {
5dabe785 11445 /* FIXME: octets_per_byte. */
c152c796
AM
11446 if (! bfd_set_section_contents (abfd, o->output_section,
11447 o->contents,
11448 (file_ptr) o->output_offset,
eea6121a 11449 o->size))
c152c796
AM
11450 goto error_return;
11451 }
11452 else
11453 {
11454 /* The contents of the .dynstr section are actually in a
11455 stringtab. */
11456 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11457 if (bfd_seek (abfd, off, SEEK_SET) != 0
11458 || ! _bfd_elf_strtab_emit (abfd,
11459 elf_hash_table (info)->dynstr))
11460 goto error_return;
11461 }
11462 }
11463 }
11464
11465 if (info->relocatable)
11466 {
11467 bfd_boolean failed = FALSE;
11468
11469 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11470 if (failed)
11471 goto error_return;
11472 }
11473
11474 /* If we have optimized stabs strings, output them. */
3722b82f 11475 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
11476 {
11477 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11478 goto error_return;
11479 }
11480
9f7c3e5e
AM
11481 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11482 goto error_return;
c152c796 11483
9f7c3e5e 11484 elf_final_link_free (abfd, &flinfo);
c152c796 11485
12bd6957 11486 elf_linker (abfd) = TRUE;
c152c796 11487
104d59d1
JM
11488 if (attr_section)
11489 {
a50b1753 11490 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 11491 if (contents == NULL)
d0f16d5e 11492 return FALSE; /* Bail out and fail. */
104d59d1
JM
11493 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11494 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11495 free (contents);
11496 }
11497
c152c796
AM
11498 return TRUE;
11499
11500 error_return:
9f7c3e5e 11501 elf_final_link_free (abfd, &flinfo);
c152c796
AM
11502 return FALSE;
11503}
11504\f
5241d853
RS
11505/* Initialize COOKIE for input bfd ABFD. */
11506
11507static bfd_boolean
11508init_reloc_cookie (struct elf_reloc_cookie *cookie,
11509 struct bfd_link_info *info, bfd *abfd)
11510{
11511 Elf_Internal_Shdr *symtab_hdr;
11512 const struct elf_backend_data *bed;
11513
11514 bed = get_elf_backend_data (abfd);
11515 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11516
11517 cookie->abfd = abfd;
11518 cookie->sym_hashes = elf_sym_hashes (abfd);
11519 cookie->bad_symtab = elf_bad_symtab (abfd);
11520 if (cookie->bad_symtab)
11521 {
11522 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11523 cookie->extsymoff = 0;
11524 }
11525 else
11526 {
11527 cookie->locsymcount = symtab_hdr->sh_info;
11528 cookie->extsymoff = symtab_hdr->sh_info;
11529 }
11530
11531 if (bed->s->arch_size == 32)
11532 cookie->r_sym_shift = 8;
11533 else
11534 cookie->r_sym_shift = 32;
11535
11536 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11537 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11538 {
11539 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11540 cookie->locsymcount, 0,
11541 NULL, NULL, NULL);
11542 if (cookie->locsyms == NULL)
11543 {
11544 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11545 return FALSE;
11546 }
11547 if (info->keep_memory)
11548 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11549 }
11550 return TRUE;
11551}
11552
11553/* Free the memory allocated by init_reloc_cookie, if appropriate. */
11554
11555static void
11556fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11557{
11558 Elf_Internal_Shdr *symtab_hdr;
11559
11560 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11561 if (cookie->locsyms != NULL
11562 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11563 free (cookie->locsyms);
11564}
11565
11566/* Initialize the relocation information in COOKIE for input section SEC
11567 of input bfd ABFD. */
11568
11569static bfd_boolean
11570init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11571 struct bfd_link_info *info, bfd *abfd,
11572 asection *sec)
11573{
11574 const struct elf_backend_data *bed;
11575
11576 if (sec->reloc_count == 0)
11577 {
11578 cookie->rels = NULL;
11579 cookie->relend = NULL;
11580 }
11581 else
11582 {
11583 bed = get_elf_backend_data (abfd);
11584
11585 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11586 info->keep_memory);
11587 if (cookie->rels == NULL)
11588 return FALSE;
11589 cookie->rel = cookie->rels;
11590 cookie->relend = (cookie->rels
11591 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11592 }
11593 cookie->rel = cookie->rels;
11594 return TRUE;
11595}
11596
11597/* Free the memory allocated by init_reloc_cookie_rels,
11598 if appropriate. */
11599
11600static void
11601fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11602 asection *sec)
11603{
11604 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11605 free (cookie->rels);
11606}
11607
11608/* Initialize the whole of COOKIE for input section SEC. */
11609
11610static bfd_boolean
11611init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11612 struct bfd_link_info *info,
11613 asection *sec)
11614{
11615 if (!init_reloc_cookie (cookie, info, sec->owner))
11616 goto error1;
11617 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11618 goto error2;
11619 return TRUE;
11620
11621 error2:
11622 fini_reloc_cookie (cookie, sec->owner);
11623 error1:
11624 return FALSE;
11625}
11626
11627/* Free the memory allocated by init_reloc_cookie_for_section,
11628 if appropriate. */
11629
11630static void
11631fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11632 asection *sec)
11633{
11634 fini_reloc_cookie_rels (cookie, sec);
11635 fini_reloc_cookie (cookie, sec->owner);
11636}
11637\f
c152c796
AM
11638/* Garbage collect unused sections. */
11639
07adf181
AM
11640/* Default gc_mark_hook. */
11641
11642asection *
11643_bfd_elf_gc_mark_hook (asection *sec,
11644 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11645 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11646 struct elf_link_hash_entry *h,
11647 Elf_Internal_Sym *sym)
11648{
bde6f3eb
L
11649 const char *sec_name;
11650
07adf181
AM
11651 if (h != NULL)
11652 {
11653 switch (h->root.type)
11654 {
11655 case bfd_link_hash_defined:
11656 case bfd_link_hash_defweak:
11657 return h->root.u.def.section;
11658
11659 case bfd_link_hash_common:
11660 return h->root.u.c.p->section;
11661
bde6f3eb
L
11662 case bfd_link_hash_undefined:
11663 case bfd_link_hash_undefweak:
11664 /* To work around a glibc bug, keep all XXX input sections
11665 when there is an as yet undefined reference to __start_XXX
11666 or __stop_XXX symbols. The linker will later define such
11667 symbols for orphan input sections that have a name
11668 representable as a C identifier. */
11669 if (strncmp (h->root.root.string, "__start_", 8) == 0)
11670 sec_name = h->root.root.string + 8;
11671 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
11672 sec_name = h->root.root.string + 7;
11673 else
11674 sec_name = NULL;
11675
11676 if (sec_name && *sec_name != '\0')
11677 {
11678 bfd *i;
68ffbac6 11679
bde6f3eb
L
11680 for (i = info->input_bfds; i; i = i->link_next)
11681 {
11682 sec = bfd_get_section_by_name (i, sec_name);
11683 if (sec)
11684 sec->flags |= SEC_KEEP;
11685 }
11686 }
11687 break;
11688
07adf181
AM
11689 default:
11690 break;
11691 }
11692 }
11693 else
11694 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11695
11696 return NULL;
11697}
11698
5241d853
RS
11699/* COOKIE->rel describes a relocation against section SEC, which is
11700 a section we've decided to keep. Return the section that contains
11701 the relocation symbol, or NULL if no section contains it. */
11702
11703asection *
11704_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11705 elf_gc_mark_hook_fn gc_mark_hook,
11706 struct elf_reloc_cookie *cookie)
11707{
11708 unsigned long r_symndx;
11709 struct elf_link_hash_entry *h;
11710
11711 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 11712 if (r_symndx == STN_UNDEF)
5241d853
RS
11713 return NULL;
11714
11715 if (r_symndx >= cookie->locsymcount
11716 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11717 {
11718 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11719 while (h->root.type == bfd_link_hash_indirect
11720 || h->root.type == bfd_link_hash_warning)
11721 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 11722 h->mark = 1;
4e6b54a6
AM
11723 /* If this symbol is weak and there is a non-weak definition, we
11724 keep the non-weak definition because many backends put
11725 dynamic reloc info on the non-weak definition for code
11726 handling copy relocs. */
11727 if (h->u.weakdef != NULL)
11728 h->u.weakdef->mark = 1;
5241d853
RS
11729 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11730 }
11731
11732 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11733 &cookie->locsyms[r_symndx]);
11734}
11735
11736/* COOKIE->rel describes a relocation against section SEC, which is
11737 a section we've decided to keep. Mark the section that contains
9d0a14d3 11738 the relocation symbol. */
5241d853
RS
11739
11740bfd_boolean
11741_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11742 asection *sec,
11743 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 11744 struct elf_reloc_cookie *cookie)
5241d853
RS
11745{
11746 asection *rsec;
11747
11748 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11749 if (rsec && !rsec->gc_mark)
11750 {
a66eed7a
AM
11751 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
11752 || (rsec->owner->flags & DYNAMIC) != 0)
5241d853 11753 rsec->gc_mark = 1;
5241d853
RS
11754 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11755 return FALSE;
11756 }
11757 return TRUE;
11758}
11759
07adf181
AM
11760/* The mark phase of garbage collection. For a given section, mark
11761 it and any sections in this section's group, and all the sections
11762 which define symbols to which it refers. */
11763
ccfa59ea
AM
11764bfd_boolean
11765_bfd_elf_gc_mark (struct bfd_link_info *info,
11766 asection *sec,
6a5bb875 11767 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
11768{
11769 bfd_boolean ret;
9d0a14d3 11770 asection *group_sec, *eh_frame;
c152c796
AM
11771
11772 sec->gc_mark = 1;
11773
11774 /* Mark all the sections in the group. */
11775 group_sec = elf_section_data (sec)->next_in_group;
11776 if (group_sec && !group_sec->gc_mark)
ccfa59ea 11777 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
11778 return FALSE;
11779
11780 /* Look through the section relocs. */
11781 ret = TRUE;
9d0a14d3
RS
11782 eh_frame = elf_eh_frame_section (sec->owner);
11783 if ((sec->flags & SEC_RELOC) != 0
11784 && sec->reloc_count > 0
11785 && sec != eh_frame)
c152c796 11786 {
5241d853 11787 struct elf_reloc_cookie cookie;
c152c796 11788
5241d853
RS
11789 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11790 ret = FALSE;
c152c796 11791 else
c152c796 11792 {
5241d853 11793 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 11794 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
11795 {
11796 ret = FALSE;
11797 break;
11798 }
11799 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
11800 }
11801 }
9d0a14d3
RS
11802
11803 if (ret && eh_frame && elf_fde_list (sec))
11804 {
11805 struct elf_reloc_cookie cookie;
11806
11807 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11808 ret = FALSE;
11809 else
11810 {
11811 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11812 gc_mark_hook, &cookie))
11813 ret = FALSE;
11814 fini_reloc_cookie_for_section (&cookie, eh_frame);
11815 }
11816 }
11817
c152c796
AM
11818 return ret;
11819}
11820
7f6ab9f8
AM
11821/* Keep debug and special sections. */
11822
11823bfd_boolean
11824_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
11825 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
11826{
11827 bfd *ibfd;
11828
11829 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11830 {
11831 asection *isec;
11832 bfd_boolean some_kept;
b40bf0a2 11833 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
11834
11835 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
11836 continue;
11837
b40bf0a2
NC
11838 /* Ensure all linker created sections are kept,
11839 see if any other section is already marked,
11840 and note if we have any fragmented debug sections. */
11841 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
11842 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
11843 {
11844 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11845 isec->gc_mark = 1;
11846 else if (isec->gc_mark)
11847 some_kept = TRUE;
b40bf0a2
NC
11848
11849 if (debug_frag_seen == FALSE
11850 && (isec->flags & SEC_DEBUGGING)
11851 && CONST_STRNEQ (isec->name, ".debug_line."))
11852 debug_frag_seen = TRUE;
7f6ab9f8
AM
11853 }
11854
11855 /* If no section in this file will be kept, then we can
b40bf0a2 11856 toss out the debug and special sections. */
7f6ab9f8
AM
11857 if (!some_kept)
11858 continue;
11859
11860 /* Keep debug and special sections like .comment when they are
c227efa6 11861 not part of a group, or when we have single-member groups. */
7f6ab9f8 11862 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
c227efa6
AM
11863 if ((elf_next_in_group (isec) == NULL
11864 || elf_next_in_group (isec) == isec)
7f6ab9f8
AM
11865 && ((isec->flags & SEC_DEBUGGING) != 0
11866 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0))
11867 isec->gc_mark = 1;
b40bf0a2
NC
11868
11869 if (! debug_frag_seen)
11870 continue;
11871
11872 /* Look for CODE sections which are going to be discarded,
11873 and find and discard any fragmented debug sections which
11874 are associated with that code section. */
11875 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
11876 if ((isec->flags & SEC_CODE) != 0
11877 && isec->gc_mark == 0)
11878 {
11879 unsigned int ilen;
11880 asection *dsec;
11881
11882 ilen = strlen (isec->name);
11883
11884 /* Association is determined by the name of the debug section
11885 containing the name of the code section as a suffix. For
11886 example .debug_line.text.foo is a debug section associated
11887 with .text.foo. */
11888 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
11889 {
11890 unsigned int dlen;
11891
11892 if (dsec->gc_mark == 0
11893 || (dsec->flags & SEC_DEBUGGING) == 0)
11894 continue;
11895
11896 dlen = strlen (dsec->name);
11897
11898 if (dlen > ilen
11899 && strncmp (dsec->name + (dlen - ilen),
11900 isec->name, ilen) == 0)
11901 {
11902 dsec->gc_mark = 0;
11903 break;
11904 }
11905 }
11906 }
7f6ab9f8
AM
11907 }
11908 return TRUE;
11909}
11910
c152c796
AM
11911/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11912
c17d87de
NC
11913struct elf_gc_sweep_symbol_info
11914{
ccabcbe5
AM
11915 struct bfd_link_info *info;
11916 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11917 bfd_boolean);
11918};
11919
c152c796 11920static bfd_boolean
ccabcbe5 11921elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 11922{
1d5316ab
AM
11923 if (!h->mark
11924 && (((h->root.type == bfd_link_hash_defined
11925 || h->root.type == bfd_link_hash_defweak)
6673f753
AM
11926 && !(h->def_regular
11927 && h->root.u.def.section->gc_mark))
1d5316ab
AM
11928 || h->root.type == bfd_link_hash_undefined
11929 || h->root.type == bfd_link_hash_undefweak))
11930 {
11931 struct elf_gc_sweep_symbol_info *inf;
11932
11933 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 11934 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
11935 h->def_regular = 0;
11936 h->ref_regular = 0;
11937 h->ref_regular_nonweak = 0;
ccabcbe5 11938 }
c152c796
AM
11939
11940 return TRUE;
11941}
11942
11943/* The sweep phase of garbage collection. Remove all garbage sections. */
11944
11945typedef bfd_boolean (*gc_sweep_hook_fn)
11946 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11947
11948static bfd_boolean
ccabcbe5 11949elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
11950{
11951 bfd *sub;
ccabcbe5
AM
11952 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11953 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11954 unsigned long section_sym_count;
11955 struct elf_gc_sweep_symbol_info sweep_info;
c152c796
AM
11956
11957 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11958 {
11959 asection *o;
11960
11961 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11962 continue;
11963
11964 for (o = sub->sections; o != NULL; o = o->next)
11965 {
a33dafc3
L
11966 /* When any section in a section group is kept, we keep all
11967 sections in the section group. If the first member of
11968 the section group is excluded, we will also exclude the
11969 group section. */
11970 if (o->flags & SEC_GROUP)
11971 {
11972 asection *first = elf_next_in_group (o);
11973 o->gc_mark = first->gc_mark;
11974 }
c152c796
AM
11975
11976 if (o->gc_mark)
11977 continue;
11978
11979 /* Skip sweeping sections already excluded. */
11980 if (o->flags & SEC_EXCLUDE)
11981 continue;
11982
11983 /* Since this is early in the link process, it is simple
11984 to remove a section from the output. */
11985 o->flags |= SEC_EXCLUDE;
11986
c55fe096 11987 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
11988 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11989
c152c796
AM
11990 /* But we also have to update some of the relocation
11991 info we collected before. */
11992 if (gc_sweep_hook
e8aaee2a 11993 && (o->flags & SEC_RELOC) != 0
9850436d
AM
11994 && o->reloc_count != 0
11995 && !((info->strip == strip_all || info->strip == strip_debugger)
11996 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 11997 && !bfd_is_abs_section (o->output_section))
c152c796
AM
11998 {
11999 Elf_Internal_Rela *internal_relocs;
12000 bfd_boolean r;
12001
12002 internal_relocs
12003 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12004 info->keep_memory);
12005 if (internal_relocs == NULL)
12006 return FALSE;
12007
12008 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12009
12010 if (elf_section_data (o)->relocs != internal_relocs)
12011 free (internal_relocs);
12012
12013 if (!r)
12014 return FALSE;
12015 }
12016 }
12017 }
12018
12019 /* Remove the symbols that were in the swept sections from the dynamic
12020 symbol table. GCFIXME: Anyone know how to get them out of the
12021 static symbol table as well? */
ccabcbe5
AM
12022 sweep_info.info = info;
12023 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12024 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12025 &sweep_info);
c152c796 12026
ccabcbe5 12027 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
12028 return TRUE;
12029}
12030
12031/* Propagate collected vtable information. This is called through
12032 elf_link_hash_traverse. */
12033
12034static bfd_boolean
12035elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12036{
c152c796 12037 /* Those that are not vtables. */
f6e332e6 12038 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12039 return TRUE;
12040
12041 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12042 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12043 return TRUE;
12044
12045 /* If we've already been done, exit. */
f6e332e6 12046 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12047 return TRUE;
12048
12049 /* Make sure the parent's table is up to date. */
f6e332e6 12050 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 12051
f6e332e6 12052 if (h->vtable->used == NULL)
c152c796
AM
12053 {
12054 /* None of this table's entries were referenced. Re-use the
12055 parent's table. */
f6e332e6
AM
12056 h->vtable->used = h->vtable->parent->vtable->used;
12057 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
12058 }
12059 else
12060 {
12061 size_t n;
12062 bfd_boolean *cu, *pu;
12063
12064 /* Or the parent's entries into ours. */
f6e332e6 12065 cu = h->vtable->used;
c152c796 12066 cu[-1] = TRUE;
f6e332e6 12067 pu = h->vtable->parent->vtable->used;
c152c796
AM
12068 if (pu != NULL)
12069 {
12070 const struct elf_backend_data *bed;
12071 unsigned int log_file_align;
12072
12073 bed = get_elf_backend_data (h->root.u.def.section->owner);
12074 log_file_align = bed->s->log_file_align;
f6e332e6 12075 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
12076 while (n--)
12077 {
12078 if (*pu)
12079 *cu = TRUE;
12080 pu++;
12081 cu++;
12082 }
12083 }
12084 }
12085
12086 return TRUE;
12087}
12088
12089static bfd_boolean
12090elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12091{
12092 asection *sec;
12093 bfd_vma hstart, hend;
12094 Elf_Internal_Rela *relstart, *relend, *rel;
12095 const struct elf_backend_data *bed;
12096 unsigned int log_file_align;
12097
c152c796
AM
12098 /* Take care of both those symbols that do not describe vtables as
12099 well as those that are not loaded. */
f6e332e6 12100 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12101 return TRUE;
12102
12103 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12104 || h->root.type == bfd_link_hash_defweak);
12105
12106 sec = h->root.u.def.section;
12107 hstart = h->root.u.def.value;
12108 hend = hstart + h->size;
12109
12110 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12111 if (!relstart)
12112 return *(bfd_boolean *) okp = FALSE;
12113 bed = get_elf_backend_data (sec->owner);
12114 log_file_align = bed->s->log_file_align;
12115
12116 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12117
12118 for (rel = relstart; rel < relend; ++rel)
12119 if (rel->r_offset >= hstart && rel->r_offset < hend)
12120 {
12121 /* If the entry is in use, do nothing. */
f6e332e6
AM
12122 if (h->vtable->used
12123 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
12124 {
12125 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 12126 if (h->vtable->used[entry])
c152c796
AM
12127 continue;
12128 }
12129 /* Otherwise, kill it. */
12130 rel->r_offset = rel->r_info = rel->r_addend = 0;
12131 }
12132
12133 return TRUE;
12134}
12135
87538722
AM
12136/* Mark sections containing dynamically referenced symbols. When
12137 building shared libraries, we must assume that any visible symbol is
12138 referenced. */
715df9b8 12139
64d03ab5
AM
12140bfd_boolean
12141bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 12142{
87538722 12143 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 12144 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 12145
715df9b8
EB
12146 if ((h->root.type == bfd_link_hash_defined
12147 || h->root.type == bfd_link_hash_defweak)
87538722 12148 && (h->ref_dynamic
b407645f 12149 || (h->def_regular
87538722 12150 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 12151 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
b407645f
AM
12152 && (!info->executable
12153 || info->export_dynamic
12154 || (h->dynamic
12155 && d != NULL
12156 && (*d->match) (&d->head, NULL, h->root.root.string)))
54e8959c
L
12157 && (strchr (h->root.root.string, ELF_VER_CHR) != NULL
12158 || !bfd_hide_sym_by_version (info->version_info,
12159 h->root.root.string)))))
715df9b8
EB
12160 h->root.u.def.section->flags |= SEC_KEEP;
12161
12162 return TRUE;
12163}
3b36f7e6 12164
74f0fb50
AM
12165/* Keep all sections containing symbols undefined on the command-line,
12166 and the section containing the entry symbol. */
12167
12168void
12169_bfd_elf_gc_keep (struct bfd_link_info *info)
12170{
12171 struct bfd_sym_chain *sym;
12172
12173 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12174 {
12175 struct elf_link_hash_entry *h;
12176
12177 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12178 FALSE, FALSE, FALSE);
12179
12180 if (h != NULL
12181 && (h->root.type == bfd_link_hash_defined
12182 || h->root.type == bfd_link_hash_defweak)
12183 && !bfd_is_abs_section (h->root.u.def.section))
12184 h->root.u.def.section->flags |= SEC_KEEP;
12185 }
12186}
12187
c152c796
AM
12188/* Do mark and sweep of unused sections. */
12189
12190bfd_boolean
12191bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12192{
12193 bfd_boolean ok = TRUE;
12194 bfd *sub;
6a5bb875 12195 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 12196 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 12197
64d03ab5 12198 if (!bed->can_gc_sections
715df9b8 12199 || !is_elf_hash_table (info->hash))
c152c796
AM
12200 {
12201 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12202 return TRUE;
12203 }
12204
74f0fb50
AM
12205 bed->gc_keep (info);
12206
9d0a14d3
RS
12207 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12208 at the .eh_frame section if we can mark the FDEs individually. */
12209 _bfd_elf_begin_eh_frame_parsing (info);
12210 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
12211 {
12212 asection *sec;
12213 struct elf_reloc_cookie cookie;
12214
12215 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 12216 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
12217 {
12218 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
12219 if (elf_section_data (sec)->sec_info
12220 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
12221 elf_eh_frame_section (sub) = sec;
12222 fini_reloc_cookie_for_section (&cookie, sec);
9a2a56cc 12223 sec = bfd_get_next_section_by_name (sec);
9d0a14d3
RS
12224 }
12225 }
12226 _bfd_elf_end_eh_frame_parsing (info);
12227
c152c796
AM
12228 /* Apply transitive closure to the vtable entry usage info. */
12229 elf_link_hash_traverse (elf_hash_table (info),
12230 elf_gc_propagate_vtable_entries_used,
12231 &ok);
12232 if (!ok)
12233 return FALSE;
12234
12235 /* Kill the vtable relocations that were not used. */
12236 elf_link_hash_traverse (elf_hash_table (info),
12237 elf_gc_smash_unused_vtentry_relocs,
12238 &ok);
12239 if (!ok)
12240 return FALSE;
12241
715df9b8
EB
12242 /* Mark dynamically referenced symbols. */
12243 if (elf_hash_table (info)->dynamic_sections_created)
12244 elf_link_hash_traverse (elf_hash_table (info),
64d03ab5 12245 bed->gc_mark_dynamic_ref,
87538722 12246 info);
c152c796 12247
715df9b8 12248 /* Grovel through relocs to find out who stays ... */
64d03ab5 12249 gc_mark_hook = bed->gc_mark_hook;
c152c796
AM
12250 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
12251 {
12252 asection *o;
12253
12254 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
12255 continue;
12256
7f6ab9f8
AM
12257 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12258 Also treat note sections as a root, if the section is not part
12259 of a group. */
c152c796 12260 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
12261 if (!o->gc_mark
12262 && (o->flags & SEC_EXCLUDE) == 0
24007750 12263 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
12264 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12265 && elf_next_in_group (o) == NULL )))
12266 {
12267 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12268 return FALSE;
12269 }
c152c796
AM
12270 }
12271
6a5bb875 12272 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 12273 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 12274
c152c796 12275 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 12276 return elf_gc_sweep (abfd, info);
c152c796
AM
12277}
12278\f
12279/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12280
12281bfd_boolean
12282bfd_elf_gc_record_vtinherit (bfd *abfd,
12283 asection *sec,
12284 struct elf_link_hash_entry *h,
12285 bfd_vma offset)
12286{
12287 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12288 struct elf_link_hash_entry **search, *child;
12289 bfd_size_type extsymcount;
12290 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12291
12292 /* The sh_info field of the symtab header tells us where the
12293 external symbols start. We don't care about the local symbols at
12294 this point. */
12295 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12296 if (!elf_bad_symtab (abfd))
12297 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12298
12299 sym_hashes = elf_sym_hashes (abfd);
12300 sym_hashes_end = sym_hashes + extsymcount;
12301
12302 /* Hunt down the child symbol, which is in this section at the same
12303 offset as the relocation. */
12304 for (search = sym_hashes; search != sym_hashes_end; ++search)
12305 {
12306 if ((child = *search) != NULL
12307 && (child->root.type == bfd_link_hash_defined
12308 || child->root.type == bfd_link_hash_defweak)
12309 && child->root.u.def.section == sec
12310 && child->root.u.def.value == offset)
12311 goto win;
12312 }
12313
d003868e
AM
12314 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12315 abfd, sec, (unsigned long) offset);
c152c796
AM
12316 bfd_set_error (bfd_error_invalid_operation);
12317 return FALSE;
12318
12319 win:
f6e332e6
AM
12320 if (!child->vtable)
12321 {
a50b1753
NC
12322 child->vtable = (struct elf_link_virtual_table_entry *)
12323 bfd_zalloc (abfd, sizeof (*child->vtable));
f6e332e6
AM
12324 if (!child->vtable)
12325 return FALSE;
12326 }
c152c796
AM
12327 if (!h)
12328 {
12329 /* This *should* only be the absolute section. It could potentially
12330 be that someone has defined a non-global vtable though, which
12331 would be bad. It isn't worth paging in the local symbols to be
12332 sure though; that case should simply be handled by the assembler. */
12333
f6e332e6 12334 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
12335 }
12336 else
f6e332e6 12337 child->vtable->parent = h;
c152c796
AM
12338
12339 return TRUE;
12340}
12341
12342/* Called from check_relocs to record the existence of a VTENTRY reloc. */
12343
12344bfd_boolean
12345bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12346 asection *sec ATTRIBUTE_UNUSED,
12347 struct elf_link_hash_entry *h,
12348 bfd_vma addend)
12349{
12350 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12351 unsigned int log_file_align = bed->s->log_file_align;
12352
f6e332e6
AM
12353 if (!h->vtable)
12354 {
a50b1753
NC
12355 h->vtable = (struct elf_link_virtual_table_entry *)
12356 bfd_zalloc (abfd, sizeof (*h->vtable));
f6e332e6
AM
12357 if (!h->vtable)
12358 return FALSE;
12359 }
12360
12361 if (addend >= h->vtable->size)
c152c796
AM
12362 {
12363 size_t size, bytes, file_align;
f6e332e6 12364 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
12365
12366 /* While the symbol is undefined, we have to be prepared to handle
12367 a zero size. */
12368 file_align = 1 << log_file_align;
12369 if (h->root.type == bfd_link_hash_undefined)
12370 size = addend + file_align;
12371 else
12372 {
12373 size = h->size;
12374 if (addend >= size)
12375 {
12376 /* Oops! We've got a reference past the defined end of
12377 the table. This is probably a bug -- shall we warn? */
12378 size = addend + file_align;
12379 }
12380 }
12381 size = (size + file_align - 1) & -file_align;
12382
12383 /* Allocate one extra entry for use as a "done" flag for the
12384 consolidation pass. */
12385 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12386
12387 if (ptr)
12388 {
a50b1753 12389 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
12390
12391 if (ptr != NULL)
12392 {
12393 size_t oldbytes;
12394
f6e332e6 12395 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
12396 * sizeof (bfd_boolean));
12397 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12398 }
12399 }
12400 else
a50b1753 12401 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
12402
12403 if (ptr == NULL)
12404 return FALSE;
12405
12406 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
12407 h->vtable->used = ptr + 1;
12408 h->vtable->size = size;
c152c796
AM
12409 }
12410
f6e332e6 12411 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
12412
12413 return TRUE;
12414}
12415
ae17ab41
CM
12416/* Map an ELF section header flag to its corresponding string. */
12417typedef struct
12418{
12419 char *flag_name;
12420 flagword flag_value;
12421} elf_flags_to_name_table;
12422
12423static elf_flags_to_name_table elf_flags_to_names [] =
12424{
12425 { "SHF_WRITE", SHF_WRITE },
12426 { "SHF_ALLOC", SHF_ALLOC },
12427 { "SHF_EXECINSTR", SHF_EXECINSTR },
12428 { "SHF_MERGE", SHF_MERGE },
12429 { "SHF_STRINGS", SHF_STRINGS },
12430 { "SHF_INFO_LINK", SHF_INFO_LINK},
12431 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12432 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12433 { "SHF_GROUP", SHF_GROUP },
12434 { "SHF_TLS", SHF_TLS },
12435 { "SHF_MASKOS", SHF_MASKOS },
12436 { "SHF_EXCLUDE", SHF_EXCLUDE },
12437};
12438
b9c361e0
JL
12439/* Returns TRUE if the section is to be included, otherwise FALSE. */
12440bfd_boolean
ae17ab41 12441bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 12442 struct flag_info *flaginfo,
b9c361e0 12443 asection *section)
ae17ab41 12444{
8b127cbc 12445 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 12446
8b127cbc 12447 if (!flaginfo->flags_initialized)
ae17ab41 12448 {
8b127cbc
AM
12449 bfd *obfd = info->output_bfd;
12450 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12451 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
12452 int with_hex = 0;
12453 int without_hex = 0;
12454
8b127cbc 12455 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 12456 {
b9c361e0 12457 unsigned i;
8b127cbc 12458 flagword (*lookup) (char *);
ae17ab41 12459
8b127cbc
AM
12460 lookup = bed->elf_backend_lookup_section_flags_hook;
12461 if (lookup != NULL)
ae17ab41 12462 {
8b127cbc 12463 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
12464
12465 if (hexval != 0)
12466 {
12467 if (tf->with == with_flags)
12468 with_hex |= hexval;
12469 else if (tf->with == without_flags)
12470 without_hex |= hexval;
12471 tf->valid = TRUE;
12472 continue;
12473 }
ae17ab41 12474 }
8b127cbc 12475 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 12476 {
8b127cbc 12477 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
12478 {
12479 if (tf->with == with_flags)
12480 with_hex |= elf_flags_to_names[i].flag_value;
12481 else if (tf->with == without_flags)
12482 without_hex |= elf_flags_to_names[i].flag_value;
12483 tf->valid = TRUE;
12484 break;
12485 }
12486 }
8b127cbc 12487 if (!tf->valid)
b9c361e0 12488 {
68ffbac6 12489 info->callbacks->einfo
8b127cbc 12490 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 12491 return FALSE;
ae17ab41
CM
12492 }
12493 }
8b127cbc
AM
12494 flaginfo->flags_initialized = TRUE;
12495 flaginfo->only_with_flags |= with_hex;
12496 flaginfo->not_with_flags |= without_hex;
ae17ab41 12497 }
ae17ab41 12498
8b127cbc 12499 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
12500 return FALSE;
12501
8b127cbc 12502 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
12503 return FALSE;
12504
12505 return TRUE;
ae17ab41
CM
12506}
12507
c152c796
AM
12508struct alloc_got_off_arg {
12509 bfd_vma gotoff;
10455f89 12510 struct bfd_link_info *info;
c152c796
AM
12511};
12512
12513/* We need a special top-level link routine to convert got reference counts
12514 to real got offsets. */
12515
12516static bfd_boolean
12517elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
12518{
a50b1753 12519 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
12520 bfd *obfd = gofarg->info->output_bfd;
12521 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 12522
c152c796
AM
12523 if (h->got.refcount > 0)
12524 {
12525 h->got.offset = gofarg->gotoff;
10455f89 12526 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
12527 }
12528 else
12529 h->got.offset = (bfd_vma) -1;
12530
12531 return TRUE;
12532}
12533
12534/* And an accompanying bit to work out final got entry offsets once
12535 we're done. Should be called from final_link. */
12536
12537bfd_boolean
12538bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
12539 struct bfd_link_info *info)
12540{
12541 bfd *i;
12542 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12543 bfd_vma gotoff;
c152c796
AM
12544 struct alloc_got_off_arg gofarg;
12545
10455f89
HPN
12546 BFD_ASSERT (abfd == info->output_bfd);
12547
c152c796
AM
12548 if (! is_elf_hash_table (info->hash))
12549 return FALSE;
12550
12551 /* The GOT offset is relative to the .got section, but the GOT header is
12552 put into the .got.plt section, if the backend uses it. */
12553 if (bed->want_got_plt)
12554 gotoff = 0;
12555 else
12556 gotoff = bed->got_header_size;
12557
12558 /* Do the local .got entries first. */
12559 for (i = info->input_bfds; i; i = i->link_next)
12560 {
12561 bfd_signed_vma *local_got;
12562 bfd_size_type j, locsymcount;
12563 Elf_Internal_Shdr *symtab_hdr;
12564
12565 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
12566 continue;
12567
12568 local_got = elf_local_got_refcounts (i);
12569 if (!local_got)
12570 continue;
12571
12572 symtab_hdr = &elf_tdata (i)->symtab_hdr;
12573 if (elf_bad_symtab (i))
12574 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12575 else
12576 locsymcount = symtab_hdr->sh_info;
12577
12578 for (j = 0; j < locsymcount; ++j)
12579 {
12580 if (local_got[j] > 0)
12581 {
12582 local_got[j] = gotoff;
10455f89 12583 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
12584 }
12585 else
12586 local_got[j] = (bfd_vma) -1;
12587 }
12588 }
12589
12590 /* Then the global .got entries. .plt refcounts are handled by
12591 adjust_dynamic_symbol */
12592 gofarg.gotoff = gotoff;
10455f89 12593 gofarg.info = info;
c152c796
AM
12594 elf_link_hash_traverse (elf_hash_table (info),
12595 elf_gc_allocate_got_offsets,
12596 &gofarg);
12597 return TRUE;
12598}
12599
12600/* Many folk need no more in the way of final link than this, once
12601 got entry reference counting is enabled. */
12602
12603bfd_boolean
12604bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
12605{
12606 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
12607 return FALSE;
12608
12609 /* Invoke the regular ELF backend linker to do all the work. */
12610 return bfd_elf_final_link (abfd, info);
12611}
12612
12613bfd_boolean
12614bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
12615{
a50b1753 12616 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
12617
12618 if (rcookie->bad_symtab)
12619 rcookie->rel = rcookie->rels;
12620
12621 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
12622 {
12623 unsigned long r_symndx;
12624
12625 if (! rcookie->bad_symtab)
12626 if (rcookie->rel->r_offset > offset)
12627 return FALSE;
12628 if (rcookie->rel->r_offset != offset)
12629 continue;
12630
12631 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 12632 if (r_symndx == STN_UNDEF)
c152c796
AM
12633 return TRUE;
12634
12635 if (r_symndx >= rcookie->locsymcount
12636 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12637 {
12638 struct elf_link_hash_entry *h;
12639
12640 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
12641
12642 while (h->root.type == bfd_link_hash_indirect
12643 || h->root.type == bfd_link_hash_warning)
12644 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12645
12646 if ((h->root.type == bfd_link_hash_defined
12647 || h->root.type == bfd_link_hash_defweak)
dbaa2011 12648 && discarded_section (h->root.u.def.section))
c152c796
AM
12649 return TRUE;
12650 else
12651 return FALSE;
12652 }
12653 else
12654 {
12655 /* It's not a relocation against a global symbol,
12656 but it could be a relocation against a local
12657 symbol for a discarded section. */
12658 asection *isec;
12659 Elf_Internal_Sym *isym;
12660
12661 /* Need to: get the symbol; get the section. */
12662 isym = &rcookie->locsyms[r_symndx];
cb33740c 12663 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
dbaa2011 12664 if (isec != NULL && discarded_section (isec))
cb33740c 12665 return TRUE;
c152c796
AM
12666 }
12667 return FALSE;
12668 }
12669 return FALSE;
12670}
12671
12672/* Discard unneeded references to discarded sections.
12673 Returns TRUE if any section's size was changed. */
12674/* This function assumes that the relocations are in sorted order,
12675 which is true for all known assemblers. */
12676
12677bfd_boolean
12678bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
12679{
12680 struct elf_reloc_cookie cookie;
12681 asection *stab, *eh;
c152c796
AM
12682 const struct elf_backend_data *bed;
12683 bfd *abfd;
c152c796
AM
12684 bfd_boolean ret = FALSE;
12685
12686 if (info->traditional_format
12687 || !is_elf_hash_table (info->hash))
12688 return FALSE;
12689
ca92cecb 12690 _bfd_elf_begin_eh_frame_parsing (info);
c152c796
AM
12691 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
12692 {
12693 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
12694 continue;
12695
12696 bed = get_elf_backend_data (abfd);
12697
8da3dbc5
AM
12698 eh = NULL;
12699 if (!info->relocatable)
12700 {
12701 eh = bfd_get_section_by_name (abfd, ".eh_frame");
7e01508c
AM
12702 while (eh != NULL
12703 && (eh->size == 0
12704 || bfd_is_abs_section (eh->output_section)))
12705 eh = bfd_get_next_section_by_name (eh);
8da3dbc5 12706 }
c152c796
AM
12707
12708 stab = bfd_get_section_by_name (abfd, ".stab");
12709 if (stab != NULL
eea6121a 12710 && (stab->size == 0
c152c796 12711 || bfd_is_abs_section (stab->output_section)
dbaa2011 12712 || stab->sec_info_type != SEC_INFO_TYPE_STABS))
c152c796
AM
12713 stab = NULL;
12714
12715 if (stab == NULL
12716 && eh == NULL
12717 && bed->elf_backend_discard_info == NULL)
12718 continue;
12719
5241d853
RS
12720 if (!init_reloc_cookie (&cookie, info, abfd))
12721 return FALSE;
c152c796 12722
5241d853
RS
12723 if (stab != NULL
12724 && stab->reloc_count > 0
12725 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
c152c796 12726 {
5241d853
RS
12727 if (_bfd_discard_section_stabs (abfd, stab,
12728 elf_section_data (stab)->sec_info,
12729 bfd_elf_reloc_symbol_deleted_p,
12730 &cookie))
12731 ret = TRUE;
12732 fini_reloc_cookie_rels (&cookie, stab);
c152c796
AM
12733 }
12734
90061c33
AM
12735 while (eh != NULL
12736 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
c152c796 12737 {
ca92cecb 12738 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
c152c796
AM
12739 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12740 bfd_elf_reloc_symbol_deleted_p,
12741 &cookie))
12742 ret = TRUE;
5241d853 12743 fini_reloc_cookie_rels (&cookie, eh);
90061c33 12744 eh = bfd_get_next_section_by_name (eh);
c152c796
AM
12745 }
12746
12747 if (bed->elf_backend_discard_info != NULL
12748 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12749 ret = TRUE;
12750
5241d853 12751 fini_reloc_cookie (&cookie, abfd);
c152c796 12752 }
ca92cecb 12753 _bfd_elf_end_eh_frame_parsing (info);
c152c796
AM
12754
12755 if (info->eh_frame_hdr
12756 && !info->relocatable
12757 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12758 ret = TRUE;
12759
12760 return ret;
12761}
082b7297 12762
43e1669b 12763bfd_boolean
0c511000 12764_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 12765 asection *sec,
c0f00686 12766 struct bfd_link_info *info)
082b7297
L
12767{
12768 flagword flags;
c77ec726 12769 const char *name, *key;
082b7297
L
12770 struct bfd_section_already_linked *l;
12771 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 12772
c77ec726
AM
12773 if (sec->output_section == bfd_abs_section_ptr)
12774 return FALSE;
0c511000 12775
c77ec726 12776 flags = sec->flags;
0c511000 12777
c77ec726
AM
12778 /* Return if it isn't a linkonce section. A comdat group section
12779 also has SEC_LINK_ONCE set. */
12780 if ((flags & SEC_LINK_ONCE) == 0)
12781 return FALSE;
0c511000 12782
c77ec726
AM
12783 /* Don't put group member sections on our list of already linked
12784 sections. They are handled as a group via their group section. */
12785 if (elf_sec_group (sec) != NULL)
12786 return FALSE;
0c511000 12787
c77ec726
AM
12788 /* For a SHT_GROUP section, use the group signature as the key. */
12789 name = sec->name;
12790 if ((flags & SEC_GROUP) != 0
12791 && elf_next_in_group (sec) != NULL
12792 && elf_group_name (elf_next_in_group (sec)) != NULL)
12793 key = elf_group_name (elf_next_in_group (sec));
12794 else
12795 {
12796 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 12797 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
12798 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12799 key++;
0c511000 12800 else
c77ec726
AM
12801 /* Must be a user linkonce section that doesn't follow gcc's
12802 naming convention. In this case we won't be matching
12803 single member groups. */
12804 key = name;
0c511000 12805 }
6d2cd210 12806
c77ec726 12807 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
12808
12809 for (l = already_linked_list->entry; l != NULL; l = l->next)
12810 {
c2370991 12811 /* We may have 2 different types of sections on the list: group
c77ec726
AM
12812 sections with a signature of <key> (<key> is some string),
12813 and linkonce sections named .gnu.linkonce.<type>.<key>.
12814 Match like sections. LTO plugin sections are an exception.
12815 They are always named .gnu.linkonce.t.<key> and match either
12816 type of section. */
12817 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
12818 && ((flags & SEC_GROUP) != 0
12819 || strcmp (name, l->sec->name) == 0))
12820 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
12821 {
12822 /* The section has already been linked. See if we should
6d2cd210 12823 issue a warning. */
c77ec726
AM
12824 if (!_bfd_handle_already_linked (sec, l, info))
12825 return FALSE;
082b7297 12826
c77ec726 12827 if (flags & SEC_GROUP)
3d7f7666 12828 {
c77ec726
AM
12829 asection *first = elf_next_in_group (sec);
12830 asection *s = first;
3d7f7666 12831
c77ec726 12832 while (s != NULL)
3d7f7666 12833 {
c77ec726
AM
12834 s->output_section = bfd_abs_section_ptr;
12835 /* Record which group discards it. */
12836 s->kept_section = l->sec;
12837 s = elf_next_in_group (s);
12838 /* These lists are circular. */
12839 if (s == first)
12840 break;
3d7f7666
L
12841 }
12842 }
082b7297 12843
43e1669b 12844 return TRUE;
082b7297
L
12845 }
12846 }
12847
c77ec726
AM
12848 /* A single member comdat group section may be discarded by a
12849 linkonce section and vice versa. */
12850 if ((flags & SEC_GROUP) != 0)
3d7f7666 12851 {
c77ec726 12852 asection *first = elf_next_in_group (sec);
c2370991 12853
c77ec726
AM
12854 if (first != NULL && elf_next_in_group (first) == first)
12855 /* Check this single member group against linkonce sections. */
12856 for (l = already_linked_list->entry; l != NULL; l = l->next)
12857 if ((l->sec->flags & SEC_GROUP) == 0
12858 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12859 {
12860 first->output_section = bfd_abs_section_ptr;
12861 first->kept_section = l->sec;
12862 sec->output_section = bfd_abs_section_ptr;
12863 break;
12864 }
12865 }
12866 else
12867 /* Check this linkonce section against single member groups. */
12868 for (l = already_linked_list->entry; l != NULL; l = l->next)
12869 if (l->sec->flags & SEC_GROUP)
6d2cd210 12870 {
c77ec726 12871 asection *first = elf_next_in_group (l->sec);
6d2cd210 12872
c77ec726
AM
12873 if (first != NULL
12874 && elf_next_in_group (first) == first
12875 && bfd_elf_match_symbols_in_sections (first, sec, info))
12876 {
12877 sec->output_section = bfd_abs_section_ptr;
12878 sec->kept_section = first;
12879 break;
12880 }
6d2cd210 12881 }
0c511000 12882
c77ec726
AM
12883 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
12884 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
12885 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
12886 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
12887 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
12888 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
12889 `.gnu.linkonce.t.F' section from a different bfd not requiring any
12890 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
12891 The reverse order cannot happen as there is never a bfd with only the
12892 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
12893 matter as here were are looking only for cross-bfd sections. */
12894
12895 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
12896 for (l = already_linked_list->entry; l != NULL; l = l->next)
12897 if ((l->sec->flags & SEC_GROUP) == 0
12898 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
12899 {
12900 if (abfd != l->sec->owner)
12901 sec->output_section = bfd_abs_section_ptr;
12902 break;
12903 }
80c29487 12904
082b7297 12905 /* This is the first section with this name. Record it. */
c77ec726 12906 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 12907 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 12908 return sec->output_section == bfd_abs_section_ptr;
082b7297 12909}
81e1b023 12910
a4d8e49b
L
12911bfd_boolean
12912_bfd_elf_common_definition (Elf_Internal_Sym *sym)
12913{
12914 return sym->st_shndx == SHN_COMMON;
12915}
12916
12917unsigned int
12918_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12919{
12920 return SHN_COMMON;
12921}
12922
12923asection *
12924_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12925{
12926 return bfd_com_section_ptr;
12927}
10455f89
HPN
12928
12929bfd_vma
12930_bfd_elf_default_got_elt_size (bfd *abfd,
12931 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12932 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12933 bfd *ibfd ATTRIBUTE_UNUSED,
12934 unsigned long symndx ATTRIBUTE_UNUSED)
12935{
12936 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12937 return bed->s->arch_size / 8;
12938}
83bac4b0
NC
12939
12940/* Routines to support the creation of dynamic relocs. */
12941
83bac4b0
NC
12942/* Returns the name of the dynamic reloc section associated with SEC. */
12943
12944static const char *
12945get_dynamic_reloc_section_name (bfd * abfd,
12946 asection * sec,
12947 bfd_boolean is_rela)
12948{
ddcf1fcf
BS
12949 char *name;
12950 const char *old_name = bfd_get_section_name (NULL, sec);
12951 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 12952
ddcf1fcf 12953 if (old_name == NULL)
83bac4b0
NC
12954 return NULL;
12955
ddcf1fcf 12956 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 12957 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
12958
12959 return name;
12960}
12961
12962/* Returns the dynamic reloc section associated with SEC.
12963 If necessary compute the name of the dynamic reloc section based
12964 on SEC's name (looked up in ABFD's string table) and the setting
12965 of IS_RELA. */
12966
12967asection *
12968_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
12969 asection * sec,
12970 bfd_boolean is_rela)
12971{
12972 asection * reloc_sec = elf_section_data (sec)->sreloc;
12973
12974 if (reloc_sec == NULL)
12975 {
12976 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12977
12978 if (name != NULL)
12979 {
3d4d4302 12980 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
12981
12982 if (reloc_sec != NULL)
12983 elf_section_data (sec)->sreloc = reloc_sec;
12984 }
12985 }
12986
12987 return reloc_sec;
12988}
12989
12990/* Returns the dynamic reloc section associated with SEC. If the
12991 section does not exist it is created and attached to the DYNOBJ
12992 bfd and stored in the SRELOC field of SEC's elf_section_data
12993 structure.
f8076f98 12994
83bac4b0
NC
12995 ALIGNMENT is the alignment for the newly created section and
12996 IS_RELA defines whether the name should be .rela.<SEC's name>
12997 or .rel.<SEC's name>. The section name is looked up in the
12998 string table associated with ABFD. */
12999
13000asection *
13001_bfd_elf_make_dynamic_reloc_section (asection * sec,
13002 bfd * dynobj,
13003 unsigned int alignment,
13004 bfd * abfd,
13005 bfd_boolean is_rela)
13006{
13007 asection * reloc_sec = elf_section_data (sec)->sreloc;
13008
13009 if (reloc_sec == NULL)
13010 {
13011 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13012
13013 if (name == NULL)
13014 return NULL;
13015
3d4d4302 13016 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
13017
13018 if (reloc_sec == NULL)
13019 {
3d4d4302
AM
13020 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13021 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
13022 if ((sec->flags & SEC_ALLOC) != 0)
13023 flags |= SEC_ALLOC | SEC_LOAD;
13024
3d4d4302 13025 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
13026 if (reloc_sec != NULL)
13027 {
8877b5e5
AM
13028 /* _bfd_elf_get_sec_type_attr chooses a section type by
13029 name. Override as it may be wrong, eg. for a user
13030 section named "auto" we'll get ".relauto" which is
13031 seen to be a .rela section. */
13032 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
13033 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13034 reloc_sec = NULL;
13035 }
13036 }
13037
13038 elf_section_data (sec)->sreloc = reloc_sec;
13039 }
13040
13041 return reloc_sec;
13042}
1338dd10
PB
13043
13044/* Copy the ELF symbol type associated with a linker hash entry. */
13045void
13046_bfd_elf_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
13047 struct bfd_link_hash_entry * hdest,
13048 struct bfd_link_hash_entry * hsrc)
13049{
13050 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *)hdest;
13051 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *)hsrc;
13052
13053 ehdest->type = ehsrc->type;
35fc36a8 13054 ehdest->target_internal = ehsrc->target_internal;
1338dd10 13055}
351f65ca
L
13056
13057/* Append a RELA relocation REL to section S in BFD. */
13058
13059void
13060elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13061{
13062 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13063 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13064 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13065 bed->s->swap_reloca_out (abfd, rel, loc);
13066}
13067
13068/* Append a REL relocation REL to section S in BFD. */
13069
13070void
13071elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13072{
13073 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13074 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13075 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 13076 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 13077}
This page took 1.736446 seconds and 4 git commands to generate.