]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
2004-09-07 Paolo Bonzini <bonzini@gnu.org>
[thirdparty/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
051d5130 2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
7898deda 3 Free Software Foundation, Inc.
252b5132
RH
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21#include "bfd.h"
22#include "sysdep.h"
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"
252b5132 29
b34976b6 30bfd_boolean
268b6b39 31_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
32{
33 flagword flags;
aad5d350 34 asection *s;
252b5132 35 struct elf_link_hash_entry *h;
14a793b2 36 struct bfd_link_hash_entry *bh;
9c5bfbb7 37 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
38 int ptralign;
39
40 /* This function may be called more than once. */
aad5d350
AM
41 s = bfd_get_section_by_name (abfd, ".got");
42 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
b34976b6 43 return TRUE;
252b5132
RH
44
45 switch (bed->s->arch_size)
46 {
bb0deeff
AO
47 case 32:
48 ptralign = 2;
49 break;
50
51 case 64:
52 ptralign = 3;
53 break;
54
55 default:
56 bfd_set_error (bfd_error_bad_value);
b34976b6 57 return FALSE;
252b5132
RH
58 }
59
e5a52504 60 flags = bed->dynamic_sec_flags;
252b5132
RH
61
62 s = bfd_make_section (abfd, ".got");
63 if (s == NULL
64 || !bfd_set_section_flags (abfd, s, flags)
65 || !bfd_set_section_alignment (abfd, s, ptralign))
b34976b6 66 return FALSE;
252b5132
RH
67
68 if (bed->want_got_plt)
69 {
70 s = bfd_make_section (abfd, ".got.plt");
71 if (s == NULL
72 || !bfd_set_section_flags (abfd, s, flags)
73 || !bfd_set_section_alignment (abfd, s, ptralign))
b34976b6 74 return FALSE;
252b5132
RH
75 }
76
2517a57f
AM
77 if (bed->want_got_sym)
78 {
79 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
80 (or .got.plt) section. We don't do this in the linker script
81 because we don't want to define the symbol if we are not creating
82 a global offset table. */
14a793b2 83 bh = NULL;
2517a57f
AM
84 if (!(_bfd_generic_link_add_one_symbol
85 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
268b6b39 86 bed->got_symbol_offset, NULL, FALSE, bed->collect, &bh)))
b34976b6 87 return FALSE;
14a793b2 88 h = (struct elf_link_hash_entry *) bh;
2517a57f
AM
89 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
90 h->type = STT_OBJECT;
252b5132 91
36af4a4e 92 if (! info->executable
c152c796 93 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 94 return FALSE;
252b5132 95
2517a57f
AM
96 elf_hash_table (info)->hgot = h;
97 }
252b5132
RH
98
99 /* The first bit of the global offset table is the header. */
eea6121a 100 s->size += bed->got_header_size + bed->got_symbol_offset;
252b5132 101
b34976b6 102 return TRUE;
252b5132
RH
103}
104\f
45d6a902
AM
105/* Create some sections which will be filled in with dynamic linking
106 information. ABFD is an input file which requires dynamic sections
107 to be created. The dynamic sections take up virtual memory space
108 when the final executable is run, so we need to create them before
109 addresses are assigned to the output sections. We work out the
110 actual contents and size of these sections later. */
252b5132 111
b34976b6 112bfd_boolean
268b6b39 113_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 114{
45d6a902
AM
115 flagword flags;
116 register asection *s;
117 struct elf_link_hash_entry *h;
118 struct bfd_link_hash_entry *bh;
9c5bfbb7 119 const struct elf_backend_data *bed;
252b5132 120
0eddce27 121 if (! is_elf_hash_table (info->hash))
45d6a902
AM
122 return FALSE;
123
124 if (elf_hash_table (info)->dynamic_sections_created)
125 return TRUE;
126
127 /* Make sure that all dynamic sections use the same input BFD. */
128 if (elf_hash_table (info)->dynobj == NULL)
129 elf_hash_table (info)->dynobj = abfd;
130 else
131 abfd = elf_hash_table (info)->dynobj;
132
e5a52504
MM
133 bed = get_elf_backend_data (abfd);
134
135 flags = bed->dynamic_sec_flags;
45d6a902
AM
136
137 /* A dynamically linked executable has a .interp section, but a
138 shared library does not. */
36af4a4e 139 if (info->executable)
252b5132 140 {
45d6a902
AM
141 s = bfd_make_section (abfd, ".interp");
142 if (s == NULL
143 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
144 return FALSE;
145 }
bb0deeff 146
0eddce27 147 if (! info->traditional_format)
45d6a902
AM
148 {
149 s = bfd_make_section (abfd, ".eh_frame_hdr");
150 if (s == NULL
151 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
152 || ! bfd_set_section_alignment (abfd, s, 2))
153 return FALSE;
154 elf_hash_table (info)->eh_info.hdr_sec = s;
155 }
bb0deeff 156
45d6a902
AM
157 /* Create sections to hold version informations. These are removed
158 if they are not needed. */
159 s = bfd_make_section (abfd, ".gnu.version_d");
160 if (s == NULL
161 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
162 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
163 return FALSE;
164
165 s = bfd_make_section (abfd, ".gnu.version");
166 if (s == NULL
167 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
168 || ! bfd_set_section_alignment (abfd, s, 1))
169 return FALSE;
170
171 s = bfd_make_section (abfd, ".gnu.version_r");
172 if (s == NULL
173 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
174 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
175 return FALSE;
176
177 s = bfd_make_section (abfd, ".dynsym");
178 if (s == NULL
179 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
180 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
181 return FALSE;
182
183 s = bfd_make_section (abfd, ".dynstr");
184 if (s == NULL
185 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
186 return FALSE;
187
188 /* Create a strtab to hold the dynamic symbol names. */
189 if (elf_hash_table (info)->dynstr == NULL)
190 {
191 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
192 if (elf_hash_table (info)->dynstr == NULL)
193 return FALSE;
252b5132
RH
194 }
195
45d6a902
AM
196 s = bfd_make_section (abfd, ".dynamic");
197 if (s == NULL
198 || ! bfd_set_section_flags (abfd, s, flags)
199 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
200 return FALSE;
201
202 /* The special symbol _DYNAMIC is always set to the start of the
203 .dynamic section. This call occurs before we have processed the
204 symbols for any dynamic object, so we don't have to worry about
205 overriding a dynamic definition. We could set _DYNAMIC in a
206 linker script, but we only want to define it if we are, in fact,
207 creating a .dynamic section. We don't want to define it if there
208 is no .dynamic section, since on some ELF platforms the start up
209 code examines it to decide how to initialize the process. */
210 bh = NULL;
211 if (! (_bfd_generic_link_add_one_symbol
268b6b39
AM
212 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
213 get_elf_backend_data (abfd)->collect, &bh)))
45d6a902
AM
214 return FALSE;
215 h = (struct elf_link_hash_entry *) bh;
216 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
217 h->type = STT_OBJECT;
218
36af4a4e 219 if (! info->executable
c152c796 220 && ! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
221 return FALSE;
222
223 s = bfd_make_section (abfd, ".hash");
224 if (s == NULL
225 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
226 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
227 return FALSE;
228 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
229
230 /* Let the backend create the rest of the sections. This lets the
231 backend set the right flags. The backend will normally create
232 the .got and .plt sections. */
233 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
234 return FALSE;
235
236 elf_hash_table (info)->dynamic_sections_created = TRUE;
237
238 return TRUE;
239}
240
241/* Create dynamic sections when linking against a dynamic object. */
242
243bfd_boolean
268b6b39 244_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
245{
246 flagword flags, pltflags;
247 asection *s;
9c5bfbb7 248 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
45d6a902 249
252b5132
RH
250 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
251 .rel[a].bss sections. */
e5a52504 252 flags = bed->dynamic_sec_flags;
252b5132
RH
253
254 pltflags = flags;
255 pltflags |= SEC_CODE;
256 if (bed->plt_not_loaded)
5d1634d7 257 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
252b5132
RH
258 if (bed->plt_readonly)
259 pltflags |= SEC_READONLY;
260
261 s = bfd_make_section (abfd, ".plt");
262 if (s == NULL
263 || ! bfd_set_section_flags (abfd, s, pltflags)
264 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 265 return FALSE;
252b5132
RH
266
267 if (bed->want_plt_sym)
268 {
269 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
270 .plt section. */
14a793b2
AM
271 struct elf_link_hash_entry *h;
272 struct bfd_link_hash_entry *bh = NULL;
273
252b5132 274 if (! (_bfd_generic_link_add_one_symbol
268b6b39
AM
275 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
276 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 277 return FALSE;
14a793b2 278 h = (struct elf_link_hash_entry *) bh;
252b5132
RH
279 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
280 h->type = STT_OBJECT;
281
36af4a4e 282 if (! info->executable
c152c796 283 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 284 return FALSE;
252b5132
RH
285 }
286
3e932841 287 s = bfd_make_section (abfd,
bf572ba0 288 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
252b5132
RH
289 if (s == NULL
290 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
45d6a902 291 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 292 return FALSE;
252b5132
RH
293
294 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 295 return FALSE;
252b5132 296
3018b441
RH
297 if (bed->want_dynbss)
298 {
299 /* The .dynbss section is a place to put symbols which are defined
300 by dynamic objects, are referenced by regular objects, and are
301 not functions. We must allocate space for them in the process
302 image and use a R_*_COPY reloc to tell the dynamic linker to
303 initialize them at run time. The linker script puts the .dynbss
304 section into the .bss section of the final image. */
305 s = bfd_make_section (abfd, ".dynbss");
306 if (s == NULL
77f3d027 307 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
b34976b6 308 return FALSE;
252b5132 309
3018b441 310 /* The .rel[a].bss section holds copy relocs. This section is not
252b5132
RH
311 normally needed. We need to create it here, though, so that the
312 linker will map it to an output section. We can't just create it
313 only if we need it, because we will not know whether we need it
314 until we have seen all the input files, and the first time the
315 main linker code calls BFD after examining all the input files
316 (size_dynamic_sections) the input sections have already been
317 mapped to the output sections. If the section turns out not to
318 be needed, we can discard it later. We will never need this
319 section when generating a shared object, since they do not use
320 copy relocs. */
3018b441
RH
321 if (! info->shared)
322 {
3e932841
KH
323 s = bfd_make_section (abfd,
324 (bed->default_use_rela_p
325 ? ".rela.bss" : ".rel.bss"));
3018b441
RH
326 if (s == NULL
327 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
45d6a902 328 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 329 return FALSE;
3018b441 330 }
252b5132
RH
331 }
332
b34976b6 333 return TRUE;
252b5132
RH
334}
335\f
252b5132
RH
336/* Record a new dynamic symbol. We record the dynamic symbols as we
337 read the input files, since we need to have a list of all of them
338 before we can determine the final sizes of the output sections.
339 Note that we may actually call this function even though we are not
340 going to output any dynamic symbols; in some cases we know that a
341 symbol should be in the dynamic symbol table, but only if there is
342 one. */
343
b34976b6 344bfd_boolean
c152c796
AM
345bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
346 struct elf_link_hash_entry *h)
252b5132
RH
347{
348 if (h->dynindx == -1)
349 {
2b0f7ef9 350 struct elf_strtab_hash *dynstr;
68b6ddd0 351 char *p;
252b5132 352 const char *name;
252b5132
RH
353 bfd_size_type indx;
354
7a13edea
NC
355 /* XXX: The ABI draft says the linker must turn hidden and
356 internal symbols into STB_LOCAL symbols when producing the
357 DSO. However, if ld.so honors st_other in the dynamic table,
358 this would not be necessary. */
359 switch (ELF_ST_VISIBILITY (h->other))
360 {
361 case STV_INTERNAL:
362 case STV_HIDDEN:
9d6eee78
L
363 if (h->root.type != bfd_link_hash_undefined
364 && h->root.type != bfd_link_hash_undefweak)
38048eb9
L
365 {
366 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
b34976b6 367 return TRUE;
7a13edea 368 }
0444bdd4 369
7a13edea
NC
370 default:
371 break;
372 }
373
252b5132
RH
374 h->dynindx = elf_hash_table (info)->dynsymcount;
375 ++elf_hash_table (info)->dynsymcount;
376
377 dynstr = elf_hash_table (info)->dynstr;
378 if (dynstr == NULL)
379 {
380 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 381 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 382 if (dynstr == NULL)
b34976b6 383 return FALSE;
252b5132
RH
384 }
385
386 /* We don't put any version information in the dynamic string
aad5d350 387 table. */
252b5132
RH
388 name = h->root.root.string;
389 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
390 if (p != NULL)
391 /* We know that the p points into writable memory. In fact,
392 there are only a few symbols that have read-only names, being
393 those like _GLOBAL_OFFSET_TABLE_ that are created specially
394 by the backends. Most symbols will have names pointing into
395 an ELF string table read from a file, or to objalloc memory. */
396 *p = 0;
397
398 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
399
400 if (p != NULL)
401 *p = ELF_VER_CHR;
252b5132
RH
402
403 if (indx == (bfd_size_type) -1)
b34976b6 404 return FALSE;
252b5132
RH
405 h->dynstr_index = indx;
406 }
407
b34976b6 408 return TRUE;
252b5132 409}
45d6a902
AM
410\f
411/* Record an assignment to a symbol made by a linker script. We need
412 this in case some dynamic object refers to this symbol. */
413
414bfd_boolean
268b6b39
AM
415bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
416 struct bfd_link_info *info,
417 const char *name,
418 bfd_boolean provide)
45d6a902
AM
419{
420 struct elf_link_hash_entry *h;
421
0eddce27 422 if (!is_elf_hash_table (info->hash))
45d6a902
AM
423 return TRUE;
424
425 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
426 if (h == NULL)
427 return FALSE;
428
02bb6eae
AO
429 /* Since we're defining the symbol, don't let it seem to have not
430 been defined. record_dynamic_symbol and size_dynamic_sections
a010d60f
AM
431 may depend on this.
432 ??? Changing bfd_link_hash_undefined to bfd_link_hash_new (or
433 to bfd_link_hash_undefweak, see linker.c:link_action) runs the risk
434 of some later symbol manipulation setting the symbol back to
435 bfd_link_hash_undefined, and the linker trying to add the symbol to
436 the undefs list twice. */
02bb6eae
AO
437 if (h->root.type == bfd_link_hash_undefweak
438 || h->root.type == bfd_link_hash_undefined)
439 h->root.type = bfd_link_hash_new;
440
45d6a902
AM
441 if (h->root.type == bfd_link_hash_new)
442 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
443
444 /* If this symbol is being provided by the linker script, and it is
445 currently defined by a dynamic object, but not by a regular
446 object, then mark it as undefined so that the generic linker will
447 force the correct value. */
448 if (provide
449 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
450 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
451 h->root.type = bfd_link_hash_undefined;
452
453 /* If this symbol is not being provided by the linker script, and it is
454 currently defined by a dynamic object, but not by a regular object,
455 then clear out any version information because the symbol will not be
456 associated with the dynamic object any more. */
457 if (!provide
458 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
459 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
460 h->verinfo.verdef = NULL;
461
462 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
463
464 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
465 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
466 || info->shared)
467 && h->dynindx == -1)
468 {
c152c796 469 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
470 return FALSE;
471
472 /* If this is a weak defined symbol, and we know a corresponding
473 real symbol from the same dynamic object, make sure the real
474 symbol is also made into a dynamic symbol. */
475 if (h->weakdef != NULL
476 && h->weakdef->dynindx == -1)
477 {
c152c796 478 if (! bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
45d6a902
AM
479 return FALSE;
480 }
481 }
482
483 return TRUE;
484}
42751cf3 485
8c58d23b
AM
486/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
487 success, and 2 on a failure caused by attempting to record a symbol
488 in a discarded section, eg. a discarded link-once section symbol. */
489
490int
c152c796
AM
491bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
492 bfd *input_bfd,
493 long input_indx)
8c58d23b
AM
494{
495 bfd_size_type amt;
496 struct elf_link_local_dynamic_entry *entry;
497 struct elf_link_hash_table *eht;
498 struct elf_strtab_hash *dynstr;
499 unsigned long dynstr_index;
500 char *name;
501 Elf_External_Sym_Shndx eshndx;
502 char esym[sizeof (Elf64_External_Sym)];
503
0eddce27 504 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
505 return 0;
506
507 /* See if the entry exists already. */
508 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
509 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
510 return 1;
511
512 amt = sizeof (*entry);
268b6b39 513 entry = bfd_alloc (input_bfd, amt);
8c58d23b
AM
514 if (entry == NULL)
515 return 0;
516
517 /* Go find the symbol, so that we can find it's name. */
518 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 519 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
520 {
521 bfd_release (input_bfd, entry);
522 return 0;
523 }
524
525 if (entry->isym.st_shndx != SHN_UNDEF
526 && (entry->isym.st_shndx < SHN_LORESERVE
527 || entry->isym.st_shndx > SHN_HIRESERVE))
528 {
529 asection *s;
530
531 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
532 if (s == NULL || bfd_is_abs_section (s->output_section))
533 {
534 /* We can still bfd_release here as nothing has done another
535 bfd_alloc. We can't do this later in this function. */
536 bfd_release (input_bfd, entry);
537 return 2;
538 }
539 }
540
541 name = (bfd_elf_string_from_elf_section
542 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
543 entry->isym.st_name));
544
545 dynstr = elf_hash_table (info)->dynstr;
546 if (dynstr == NULL)
547 {
548 /* Create a strtab to hold the dynamic symbol names. */
549 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
550 if (dynstr == NULL)
551 return 0;
552 }
553
b34976b6 554 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
8c58d23b
AM
555 if (dynstr_index == (unsigned long) -1)
556 return 0;
557 entry->isym.st_name = dynstr_index;
558
559 eht = elf_hash_table (info);
560
561 entry->next = eht->dynlocal;
562 eht->dynlocal = entry;
563 entry->input_bfd = input_bfd;
564 entry->input_indx = input_indx;
565 eht->dynsymcount++;
566
567 /* Whatever binding the symbol had before, it's now local. */
568 entry->isym.st_info
569 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
570
571 /* The dynindx will be set at the end of size_dynamic_sections. */
572
573 return 1;
574}
575
30b30c21 576/* Return the dynindex of a local dynamic symbol. */
42751cf3 577
30b30c21 578long
268b6b39
AM
579_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
580 bfd *input_bfd,
581 long input_indx)
30b30c21
RH
582{
583 struct elf_link_local_dynamic_entry *e;
584
585 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
586 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
587 return e->dynindx;
588 return -1;
589}
590
591/* This function is used to renumber the dynamic symbols, if some of
592 them are removed because they are marked as local. This is called
593 via elf_link_hash_traverse. */
594
b34976b6 595static bfd_boolean
268b6b39
AM
596elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
597 void *data)
42751cf3 598{
268b6b39 599 size_t *count = data;
30b30c21 600
e92d460e
AM
601 if (h->root.type == bfd_link_hash_warning)
602 h = (struct elf_link_hash_entry *) h->root.u.i.link;
603
42751cf3 604 if (h->dynindx != -1)
30b30c21
RH
605 h->dynindx = ++(*count);
606
b34976b6 607 return TRUE;
42751cf3 608}
30b30c21 609
aee6f5b4
AO
610/* Return true if the dynamic symbol for a given section should be
611 omitted when creating a shared library. */
612bfd_boolean
613_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
614 struct bfd_link_info *info,
615 asection *p)
616{
617 switch (elf_section_data (p)->this_hdr.sh_type)
618 {
619 case SHT_PROGBITS:
620 case SHT_NOBITS:
621 /* If sh_type is yet undecided, assume it could be
622 SHT_PROGBITS/SHT_NOBITS. */
623 case SHT_NULL:
624 if (strcmp (p->name, ".got") == 0
625 || strcmp (p->name, ".got.plt") == 0
626 || strcmp (p->name, ".plt") == 0)
627 {
628 asection *ip;
629 bfd *dynobj = elf_hash_table (info)->dynobj;
630
631 if (dynobj != NULL
632 && (ip = bfd_get_section_by_name (dynobj, p->name))
633 != NULL
634 && (ip->flags & SEC_LINKER_CREATED)
635 && ip->output_section == p)
636 return TRUE;
637 }
638 return FALSE;
639
640 /* There shouldn't be section relative relocations
641 against any other section. */
642 default:
643 return TRUE;
644 }
645}
646
062e2358 647/* Assign dynsym indices. In a shared library we generate a section
30b30c21
RH
648 symbol for each output section, which come first. Next come all of
649 the back-end allocated local dynamic syms, followed by the rest of
650 the global symbols. */
651
652unsigned long
268b6b39 653_bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
30b30c21
RH
654{
655 unsigned long dynsymcount = 0;
656
657 if (info->shared)
658 {
aee6f5b4 659 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
660 asection *p;
661 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 662 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
663 && (p->flags & SEC_ALLOC) != 0
664 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
665 elf_section_data (p)->dynindx = ++dynsymcount;
30b30c21
RH
666 }
667
668 if (elf_hash_table (info)->dynlocal)
669 {
670 struct elf_link_local_dynamic_entry *p;
671 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
672 p->dynindx = ++dynsymcount;
673 }
674
675 elf_link_hash_traverse (elf_hash_table (info),
676 elf_link_renumber_hash_table_dynsyms,
677 &dynsymcount);
678
679 /* There is an unused NULL entry at the head of the table which
680 we must account for in our count. Unless there weren't any
681 symbols, which means we'll have no table at all. */
682 if (dynsymcount != 0)
683 ++dynsymcount;
684
685 return elf_hash_table (info)->dynsymcount = dynsymcount;
686}
252b5132 687
45d6a902
AM
688/* This function is called when we want to define a new symbol. It
689 handles the various cases which arise when we find a definition in
690 a dynamic object, or when there is already a definition in a
691 dynamic object. The new symbol is described by NAME, SYM, PSEC,
692 and PVALUE. We set SYM_HASH to the hash table entry. We set
693 OVERRIDE if the old symbol is overriding a new definition. We set
694 TYPE_CHANGE_OK if it is OK for the type to change. We set
695 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
696 change, we mean that we shouldn't warn if the type or size does
0f8a2703 697 change. */
45d6a902
AM
698
699bfd_boolean
268b6b39
AM
700_bfd_elf_merge_symbol (bfd *abfd,
701 struct bfd_link_info *info,
702 const char *name,
703 Elf_Internal_Sym *sym,
704 asection **psec,
705 bfd_vma *pvalue,
706 struct elf_link_hash_entry **sym_hash,
707 bfd_boolean *skip,
708 bfd_boolean *override,
709 bfd_boolean *type_change_ok,
0f8a2703 710 bfd_boolean *size_change_ok)
252b5132 711{
45d6a902
AM
712 asection *sec;
713 struct elf_link_hash_entry *h;
714 struct elf_link_hash_entry *flip;
715 int bind;
716 bfd *oldbfd;
717 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
79349b09 718 bfd_boolean newweak, oldweak;
45d6a902
AM
719
720 *skip = FALSE;
721 *override = FALSE;
722
723 sec = *psec;
724 bind = ELF_ST_BIND (sym->st_info);
725
726 if (! bfd_is_und_section (sec))
727 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
728 else
729 h = ((struct elf_link_hash_entry *)
730 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
731 if (h == NULL)
732 return FALSE;
733 *sym_hash = h;
252b5132 734
45d6a902
AM
735 /* This code is for coping with dynamic objects, and is only useful
736 if we are doing an ELF link. */
737 if (info->hash->creator != abfd->xvec)
738 return TRUE;
252b5132 739
45d6a902
AM
740 /* For merging, we only care about real symbols. */
741
742 while (h->root.type == bfd_link_hash_indirect
743 || h->root.type == bfd_link_hash_warning)
744 h = (struct elf_link_hash_entry *) h->root.u.i.link;
745
746 /* If we just created the symbol, mark it as being an ELF symbol.
747 Other than that, there is nothing to do--there is no merge issue
748 with a newly defined symbol--so we just return. */
749
750 if (h->root.type == bfd_link_hash_new)
252b5132 751 {
45d6a902
AM
752 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
753 return TRUE;
754 }
252b5132 755
45d6a902 756 /* OLDBFD is a BFD associated with the existing symbol. */
252b5132 757
45d6a902
AM
758 switch (h->root.type)
759 {
760 default:
761 oldbfd = NULL;
762 break;
252b5132 763
45d6a902
AM
764 case bfd_link_hash_undefined:
765 case bfd_link_hash_undefweak:
766 oldbfd = h->root.u.undef.abfd;
767 break;
768
769 case bfd_link_hash_defined:
770 case bfd_link_hash_defweak:
771 oldbfd = h->root.u.def.section->owner;
772 break;
773
774 case bfd_link_hash_common:
775 oldbfd = h->root.u.c.p->section->owner;
776 break;
777 }
778
779 /* In cases involving weak versioned symbols, we may wind up trying
780 to merge a symbol with itself. Catch that here, to avoid the
781 confusion that results if we try to override a symbol with
782 itself. The additional tests catch cases like
783 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
784 dynamic object, which we do want to handle here. */
785 if (abfd == oldbfd
786 && ((abfd->flags & DYNAMIC) == 0
787 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
788 return TRUE;
789
790 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
791 respectively, is from a dynamic object. */
792
793 if ((abfd->flags & DYNAMIC) != 0)
794 newdyn = TRUE;
795 else
796 newdyn = FALSE;
797
798 if (oldbfd != NULL)
799 olddyn = (oldbfd->flags & DYNAMIC) != 0;
800 else
801 {
802 asection *hsec;
803
804 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
805 indices used by MIPS ELF. */
806 switch (h->root.type)
252b5132 807 {
45d6a902
AM
808 default:
809 hsec = NULL;
810 break;
252b5132 811
45d6a902
AM
812 case bfd_link_hash_defined:
813 case bfd_link_hash_defweak:
814 hsec = h->root.u.def.section;
815 break;
252b5132 816
45d6a902
AM
817 case bfd_link_hash_common:
818 hsec = h->root.u.c.p->section;
819 break;
252b5132 820 }
252b5132 821
45d6a902
AM
822 if (hsec == NULL)
823 olddyn = FALSE;
824 else
825 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
826 }
252b5132 827
45d6a902
AM
828 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
829 respectively, appear to be a definition rather than reference. */
830
831 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
832 newdef = FALSE;
833 else
834 newdef = TRUE;
835
836 if (h->root.type == bfd_link_hash_undefined
837 || h->root.type == bfd_link_hash_undefweak
838 || h->root.type == bfd_link_hash_common)
839 olddef = FALSE;
840 else
841 olddef = TRUE;
842
4cc11e76 843 /* We need to remember if a symbol has a definition in a dynamic
45d6a902
AM
844 object or is weak in all dynamic objects. Internal and hidden
845 visibility will make it unavailable to dynamic objects. */
846 if (newdyn && (h->elf_link_hash_flags & ELF_LINK_DYNAMIC_DEF) == 0)
847 {
848 if (!bfd_is_und_section (sec))
849 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_DEF;
850 else
252b5132 851 {
45d6a902
AM
852 /* Check if this symbol is weak in all dynamic objects. If it
853 is the first time we see it in a dynamic object, we mark
854 if it is weak. Otherwise, we clear it. */
855 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
79349b09 856 {
45d6a902
AM
857 if (bind == STB_WEAK)
858 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_WEAK;
252b5132 859 }
45d6a902
AM
860 else if (bind != STB_WEAK)
861 h->elf_link_hash_flags &= ~ELF_LINK_DYNAMIC_WEAK;
252b5132 862 }
45d6a902 863 }
252b5132 864
45d6a902
AM
865 /* If the old symbol has non-default visibility, we ignore the new
866 definition from a dynamic object. */
867 if (newdyn
9c7a29a3 868 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
869 && !bfd_is_und_section (sec))
870 {
871 *skip = TRUE;
872 /* Make sure this symbol is dynamic. */
873 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
874 /* A protected symbol has external availability. Make sure it is
875 recorded as dynamic.
876
877 FIXME: Should we check type and size for protected symbol? */
878 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 879 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
880 else
881 return TRUE;
882 }
883 else if (!newdyn
9c7a29a3 884 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
45d6a902
AM
885 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
886 {
887 /* If the new symbol with non-default visibility comes from a
888 relocatable file and the old definition comes from a dynamic
889 object, we remove the old definition. */
890 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
891 h = *sym_hash;
1de1a317
L
892
893 if ((h->root.und_next || info->hash->undefs_tail == &h->root)
894 && bfd_is_und_section (sec))
895 {
896 /* If the new symbol is undefined and the old symbol was
897 also undefined before, we need to make sure
898 _bfd_generic_link_add_one_symbol doesn't mess
899 up the linker hash table undefs list. Since the old
900 definition came from a dynamic object, it is still on the
901 undefs list. */
902 h->root.type = bfd_link_hash_undefined;
903 /* FIXME: What if the new symbol is weak undefined? */
904 h->root.u.undef.abfd = abfd;
905 }
906 else
907 {
908 h->root.type = bfd_link_hash_new;
909 h->root.u.undef.abfd = NULL;
910 }
911
45d6a902 912 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
252b5132 913 {
45d6a902 914 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
22d5e339
L
915 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_DYNAMIC
916 | ELF_LINK_DYNAMIC_DEF);
45d6a902
AM
917 }
918 /* FIXME: Should we check type and size for protected symbol? */
919 h->size = 0;
920 h->type = 0;
921 return TRUE;
922 }
14a793b2 923
79349b09
AM
924 /* Differentiate strong and weak symbols. */
925 newweak = bind == STB_WEAK;
926 oldweak = (h->root.type == bfd_link_hash_defweak
927 || h->root.type == bfd_link_hash_undefweak);
14a793b2 928
15b43f48
AM
929 /* If a new weak symbol definition comes from a regular file and the
930 old symbol comes from a dynamic library, we treat the new one as
931 strong. Similarly, an old weak symbol definition from a regular
932 file is treated as strong when the new symbol comes from a dynamic
933 library. Further, an old weak symbol from a dynamic library is
934 treated as strong if the new symbol is from a dynamic library.
935 This reflects the way glibc's ld.so works.
936
937 Do this before setting *type_change_ok or *size_change_ok so that
938 we warn properly when dynamic library symbols are overridden. */
939
940 if (newdef && !newdyn && olddyn)
0f8a2703 941 newweak = FALSE;
15b43f48 942 if (olddef && newdyn)
0f8a2703
AM
943 oldweak = FALSE;
944
79349b09
AM
945 /* It's OK to change the type if either the existing symbol or the
946 new symbol is weak. A type change is also OK if the old symbol
947 is undefined and the new symbol is defined. */
252b5132 948
79349b09
AM
949 if (oldweak
950 || newweak
951 || (newdef
952 && h->root.type == bfd_link_hash_undefined))
953 *type_change_ok = TRUE;
954
955 /* It's OK to change the size if either the existing symbol or the
956 new symbol is weak, or if the old symbol is undefined. */
957
958 if (*type_change_ok
959 || h->root.type == bfd_link_hash_undefined)
960 *size_change_ok = TRUE;
45d6a902 961
45d6a902
AM
962 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
963 symbol, respectively, appears to be a common symbol in a dynamic
964 object. If a symbol appears in an uninitialized section, and is
965 not weak, and is not a function, then it may be a common symbol
966 which was resolved when the dynamic object was created. We want
967 to treat such symbols specially, because they raise special
968 considerations when setting the symbol size: if the symbol
969 appears as a common symbol in a regular object, and the size in
970 the regular object is larger, we must make sure that we use the
971 larger size. This problematic case can always be avoided in C,
972 but it must be handled correctly when using Fortran shared
973 libraries.
974
975 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
976 likewise for OLDDYNCOMMON and OLDDEF.
977
978 Note that this test is just a heuristic, and that it is quite
979 possible to have an uninitialized symbol in a shared object which
980 is really a definition, rather than a common symbol. This could
981 lead to some minor confusion when the symbol really is a common
982 symbol in some regular object. However, I think it will be
983 harmless. */
984
985 if (newdyn
986 && newdef
79349b09 987 && !newweak
45d6a902
AM
988 && (sec->flags & SEC_ALLOC) != 0
989 && (sec->flags & SEC_LOAD) == 0
990 && sym->st_size > 0
45d6a902
AM
991 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
992 newdyncommon = TRUE;
993 else
994 newdyncommon = FALSE;
995
996 if (olddyn
997 && olddef
998 && h->root.type == bfd_link_hash_defined
999 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1000 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1001 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1002 && h->size > 0
1003 && h->type != STT_FUNC)
1004 olddyncommon = TRUE;
1005 else
1006 olddyncommon = FALSE;
1007
45d6a902
AM
1008 /* If both the old and the new symbols look like common symbols in a
1009 dynamic object, set the size of the symbol to the larger of the
1010 two. */
1011
1012 if (olddyncommon
1013 && newdyncommon
1014 && sym->st_size != h->size)
1015 {
1016 /* Since we think we have two common symbols, issue a multiple
1017 common warning if desired. Note that we only warn if the
1018 size is different. If the size is the same, we simply let
1019 the old symbol override the new one as normally happens with
1020 symbols defined in dynamic objects. */
1021
1022 if (! ((*info->callbacks->multiple_common)
1023 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1024 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1025 return FALSE;
252b5132 1026
45d6a902
AM
1027 if (sym->st_size > h->size)
1028 h->size = sym->st_size;
252b5132 1029
45d6a902 1030 *size_change_ok = TRUE;
252b5132
RH
1031 }
1032
45d6a902
AM
1033 /* If we are looking at a dynamic object, and we have found a
1034 definition, we need to see if the symbol was already defined by
1035 some other object. If so, we want to use the existing
1036 definition, and we do not want to report a multiple symbol
1037 definition error; we do this by clobbering *PSEC to be
1038 bfd_und_section_ptr.
1039
1040 We treat a common symbol as a definition if the symbol in the
1041 shared library is a function, since common symbols always
1042 represent variables; this can cause confusion in principle, but
1043 any such confusion would seem to indicate an erroneous program or
1044 shared library. We also permit a common symbol in a regular
79349b09 1045 object to override a weak symbol in a shared object. */
45d6a902
AM
1046
1047 if (newdyn
1048 && newdef
1049 && (olddef
1050 || (h->root.type == bfd_link_hash_common
79349b09 1051 && (newweak
0f8a2703 1052 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
45d6a902
AM
1053 {
1054 *override = TRUE;
1055 newdef = FALSE;
1056 newdyncommon = FALSE;
252b5132 1057
45d6a902
AM
1058 *psec = sec = bfd_und_section_ptr;
1059 *size_change_ok = TRUE;
252b5132 1060
45d6a902
AM
1061 /* If we get here when the old symbol is a common symbol, then
1062 we are explicitly letting it override a weak symbol or
1063 function in a dynamic object, and we don't want to warn about
1064 a type change. If the old symbol is a defined symbol, a type
1065 change warning may still be appropriate. */
252b5132 1066
45d6a902
AM
1067 if (h->root.type == bfd_link_hash_common)
1068 *type_change_ok = TRUE;
1069 }
1070
1071 /* Handle the special case of an old common symbol merging with a
1072 new symbol which looks like a common symbol in a shared object.
1073 We change *PSEC and *PVALUE to make the new symbol look like a
1074 common symbol, and let _bfd_generic_link_add_one_symbol will do
1075 the right thing. */
1076
1077 if (newdyncommon
1078 && h->root.type == bfd_link_hash_common)
1079 {
1080 *override = TRUE;
1081 newdef = FALSE;
1082 newdyncommon = FALSE;
1083 *pvalue = sym->st_size;
1084 *psec = sec = bfd_com_section_ptr;
1085 *size_change_ok = TRUE;
1086 }
1087
1088 /* If the old symbol is from a dynamic object, and the new symbol is
1089 a definition which is not from a dynamic object, then the new
1090 symbol overrides the old symbol. Symbols from regular files
1091 always take precedence over symbols from dynamic objects, even if
1092 they are defined after the dynamic object in the link.
1093
1094 As above, we again permit a common symbol in a regular object to
1095 override a definition in a shared object if the shared object
0f8a2703 1096 symbol is a function or is weak. */
45d6a902
AM
1097
1098 flip = NULL;
1099 if (! newdyn
1100 && (newdef
1101 || (bfd_is_com_section (sec)
79349b09
AM
1102 && (oldweak
1103 || h->type == STT_FUNC)))
45d6a902
AM
1104 && olddyn
1105 && olddef
0f8a2703 1106 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
45d6a902
AM
1107 {
1108 /* Change the hash table entry to undefined, and let
1109 _bfd_generic_link_add_one_symbol do the right thing with the
1110 new definition. */
1111
1112 h->root.type = bfd_link_hash_undefined;
1113 h->root.u.undef.abfd = h->root.u.def.section->owner;
1114 *size_change_ok = TRUE;
1115
1116 olddef = FALSE;
1117 olddyncommon = FALSE;
1118
1119 /* We again permit a type change when a common symbol may be
1120 overriding a function. */
1121
1122 if (bfd_is_com_section (sec))
1123 *type_change_ok = TRUE;
1124
1125 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1126 flip = *sym_hash;
1127 else
1128 /* This union may have been set to be non-NULL when this symbol
1129 was seen in a dynamic object. We must force the union to be
1130 NULL, so that it is correct for a regular symbol. */
1131 h->verinfo.vertree = NULL;
1132 }
1133
1134 /* Handle the special case of a new common symbol merging with an
1135 old symbol that looks like it might be a common symbol defined in
1136 a shared object. Note that we have already handled the case in
1137 which a new common symbol should simply override the definition
1138 in the shared library. */
1139
1140 if (! newdyn
1141 && bfd_is_com_section (sec)
1142 && olddyncommon)
1143 {
1144 /* It would be best if we could set the hash table entry to a
1145 common symbol, but we don't know what to use for the section
1146 or the alignment. */
1147 if (! ((*info->callbacks->multiple_common)
1148 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1149 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1150 return FALSE;
1151
4cc11e76 1152 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1153 larger, pretend that the new symbol has its size. */
1154
1155 if (h->size > *pvalue)
1156 *pvalue = h->size;
1157
1158 /* FIXME: We no longer know the alignment required by the symbol
1159 in the dynamic object, so we just wind up using the one from
1160 the regular object. */
1161
1162 olddef = FALSE;
1163 olddyncommon = FALSE;
1164
1165 h->root.type = bfd_link_hash_undefined;
1166 h->root.u.undef.abfd = h->root.u.def.section->owner;
1167
1168 *size_change_ok = TRUE;
1169 *type_change_ok = TRUE;
1170
1171 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1172 flip = *sym_hash;
1173 else
1174 h->verinfo.vertree = NULL;
1175 }
1176
1177 if (flip != NULL)
1178 {
1179 /* Handle the case where we had a versioned symbol in a dynamic
1180 library and now find a definition in a normal object. In this
1181 case, we make the versioned symbol point to the normal one. */
9c5bfbb7 1182 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
45d6a902
AM
1183 flip->root.type = h->root.type;
1184 h->root.type = bfd_link_hash_indirect;
1185 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1186 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1187 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1188 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1189 {
1190 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
1191 flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1192 }
1193 }
1194
45d6a902
AM
1195 return TRUE;
1196}
1197
1198/* This function is called to create an indirect symbol from the
1199 default for the symbol with the default version if needed. The
1200 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
0f8a2703 1201 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902
AM
1202
1203bfd_boolean
268b6b39
AM
1204_bfd_elf_add_default_symbol (bfd *abfd,
1205 struct bfd_link_info *info,
1206 struct elf_link_hash_entry *h,
1207 const char *name,
1208 Elf_Internal_Sym *sym,
1209 asection **psec,
1210 bfd_vma *value,
1211 bfd_boolean *dynsym,
0f8a2703 1212 bfd_boolean override)
45d6a902
AM
1213{
1214 bfd_boolean type_change_ok;
1215 bfd_boolean size_change_ok;
1216 bfd_boolean skip;
1217 char *shortname;
1218 struct elf_link_hash_entry *hi;
1219 struct bfd_link_hash_entry *bh;
9c5bfbb7 1220 const struct elf_backend_data *bed;
45d6a902
AM
1221 bfd_boolean collect;
1222 bfd_boolean dynamic;
1223 char *p;
1224 size_t len, shortlen;
1225 asection *sec;
1226
1227 /* If this symbol has a version, and it is the default version, we
1228 create an indirect symbol from the default name to the fully
1229 decorated name. This will cause external references which do not
1230 specify a version to be bound to this version of the symbol. */
1231 p = strchr (name, ELF_VER_CHR);
1232 if (p == NULL || p[1] != ELF_VER_CHR)
1233 return TRUE;
1234
1235 if (override)
1236 {
4cc11e76 1237 /* We are overridden by an old definition. We need to check if we
45d6a902
AM
1238 need to create the indirect symbol from the default name. */
1239 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1240 FALSE, FALSE);
1241 BFD_ASSERT (hi != NULL);
1242 if (hi == h)
1243 return TRUE;
1244 while (hi->root.type == bfd_link_hash_indirect
1245 || hi->root.type == bfd_link_hash_warning)
1246 {
1247 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1248 if (hi == h)
1249 return TRUE;
1250 }
1251 }
1252
1253 bed = get_elf_backend_data (abfd);
1254 collect = bed->collect;
1255 dynamic = (abfd->flags & DYNAMIC) != 0;
1256
1257 shortlen = p - name;
1258 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1259 if (shortname == NULL)
1260 return FALSE;
1261 memcpy (shortname, name, shortlen);
1262 shortname[shortlen] = '\0';
1263
1264 /* We are going to create a new symbol. Merge it with any existing
1265 symbol with this name. For the purposes of the merge, act as
1266 though we were defining the symbol we just defined, although we
1267 actually going to define an indirect symbol. */
1268 type_change_ok = FALSE;
1269 size_change_ok = FALSE;
1270 sec = *psec;
1271 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1272 &hi, &skip, &override, &type_change_ok,
0f8a2703 1273 &size_change_ok))
45d6a902
AM
1274 return FALSE;
1275
1276 if (skip)
1277 goto nondefault;
1278
1279 if (! override)
1280 {
1281 bh = &hi->root;
1282 if (! (_bfd_generic_link_add_one_symbol
1283 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
268b6b39 1284 0, name, FALSE, collect, &bh)))
45d6a902
AM
1285 return FALSE;
1286 hi = (struct elf_link_hash_entry *) bh;
1287 }
1288 else
1289 {
1290 /* In this case the symbol named SHORTNAME is overriding the
1291 indirect symbol we want to add. We were planning on making
1292 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1293 is the name without a version. NAME is the fully versioned
1294 name, and it is the default version.
1295
1296 Overriding means that we already saw a definition for the
1297 symbol SHORTNAME in a regular object, and it is overriding
1298 the symbol defined in the dynamic object.
1299
1300 When this happens, we actually want to change NAME, the
1301 symbol we just added, to refer to SHORTNAME. This will cause
1302 references to NAME in the shared object to become references
1303 to SHORTNAME in the regular object. This is what we expect
1304 when we override a function in a shared object: that the
1305 references in the shared object will be mapped to the
1306 definition in the regular object. */
1307
1308 while (hi->root.type == bfd_link_hash_indirect
1309 || hi->root.type == bfd_link_hash_warning)
1310 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1311
1312 h->root.type = bfd_link_hash_indirect;
1313 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1314 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1315 {
1316 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1317 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1318 if (hi->elf_link_hash_flags
1319 & (ELF_LINK_HASH_REF_REGULAR
1320 | ELF_LINK_HASH_DEF_REGULAR))
1321 {
c152c796 1322 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1323 return FALSE;
1324 }
1325 }
1326
1327 /* Now set HI to H, so that the following code will set the
1328 other fields correctly. */
1329 hi = h;
1330 }
1331
1332 /* If there is a duplicate definition somewhere, then HI may not
1333 point to an indirect symbol. We will have reported an error to
1334 the user in that case. */
1335
1336 if (hi->root.type == bfd_link_hash_indirect)
1337 {
1338 struct elf_link_hash_entry *ht;
1339
45d6a902
AM
1340 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1341 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1342
1343 /* See if the new flags lead us to realize that the symbol must
1344 be dynamic. */
1345 if (! *dynsym)
1346 {
1347 if (! dynamic)
1348 {
1349 if (info->shared
1350 || ((hi->elf_link_hash_flags
1351 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1352 *dynsym = TRUE;
1353 }
1354 else
1355 {
1356 if ((hi->elf_link_hash_flags
1357 & ELF_LINK_HASH_REF_REGULAR) != 0)
1358 *dynsym = TRUE;
1359 }
1360 }
1361 }
1362
1363 /* We also need to define an indirection from the nondefault version
1364 of the symbol. */
1365
1366nondefault:
1367 len = strlen (name);
1368 shortname = bfd_hash_allocate (&info->hash->table, len);
1369 if (shortname == NULL)
1370 return FALSE;
1371 memcpy (shortname, name, shortlen);
1372 memcpy (shortname + shortlen, p + 1, len - shortlen);
1373
1374 /* Once again, merge with any existing symbol. */
1375 type_change_ok = FALSE;
1376 size_change_ok = FALSE;
1377 sec = *psec;
1378 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1379 &hi, &skip, &override, &type_change_ok,
0f8a2703 1380 &size_change_ok))
45d6a902
AM
1381 return FALSE;
1382
1383 if (skip)
1384 return TRUE;
1385
1386 if (override)
1387 {
1388 /* Here SHORTNAME is a versioned name, so we don't expect to see
1389 the type of override we do in the case above unless it is
4cc11e76 1390 overridden by a versioned definition. */
45d6a902
AM
1391 if (hi->root.type != bfd_link_hash_defined
1392 && hi->root.type != bfd_link_hash_defweak)
1393 (*_bfd_error_handler)
d003868e
AM
1394 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1395 abfd, shortname);
45d6a902
AM
1396 }
1397 else
1398 {
1399 bh = &hi->root;
1400 if (! (_bfd_generic_link_add_one_symbol
1401 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1402 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1403 return FALSE;
1404 hi = (struct elf_link_hash_entry *) bh;
1405
1406 /* If there is a duplicate definition somewhere, then HI may not
1407 point to an indirect symbol. We will have reported an error
1408 to the user in that case. */
1409
1410 if (hi->root.type == bfd_link_hash_indirect)
1411 {
45d6a902
AM
1412 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1413
1414 /* See if the new flags lead us to realize that the symbol
1415 must be dynamic. */
1416 if (! *dynsym)
1417 {
1418 if (! dynamic)
1419 {
1420 if (info->shared
1421 || ((hi->elf_link_hash_flags
1422 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1423 *dynsym = TRUE;
1424 }
1425 else
1426 {
1427 if ((hi->elf_link_hash_flags
1428 & ELF_LINK_HASH_REF_REGULAR) != 0)
1429 *dynsym = TRUE;
1430 }
1431 }
1432 }
1433 }
1434
1435 return TRUE;
1436}
1437\f
1438/* This routine is used to export all defined symbols into the dynamic
1439 symbol table. It is called via elf_link_hash_traverse. */
1440
1441bfd_boolean
268b6b39 1442_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1443{
268b6b39 1444 struct elf_info_failed *eif = data;
45d6a902
AM
1445
1446 /* Ignore indirect symbols. These are added by the versioning code. */
1447 if (h->root.type == bfd_link_hash_indirect)
1448 return TRUE;
1449
1450 if (h->root.type == bfd_link_hash_warning)
1451 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1452
1453 if (h->dynindx == -1
1454 && (h->elf_link_hash_flags
1455 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
1456 {
1457 struct bfd_elf_version_tree *t;
1458 struct bfd_elf_version_expr *d;
1459
1460 for (t = eif->verdefs; t != NULL; t = t->next)
1461 {
108ba305 1462 if (t->globals.list != NULL)
45d6a902 1463 {
108ba305
JJ
1464 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1465 if (d != NULL)
1466 goto doit;
45d6a902
AM
1467 }
1468
108ba305 1469 if (t->locals.list != NULL)
45d6a902 1470 {
108ba305
JJ
1471 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1472 if (d != NULL)
1473 return TRUE;
45d6a902
AM
1474 }
1475 }
1476
1477 if (!eif->verdefs)
1478 {
1479 doit:
c152c796 1480 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
1481 {
1482 eif->failed = TRUE;
1483 return FALSE;
1484 }
1485 }
1486 }
1487
1488 return TRUE;
1489}
1490\f
1491/* Look through the symbols which are defined in other shared
1492 libraries and referenced here. Update the list of version
1493 dependencies. This will be put into the .gnu.version_r section.
1494 This function is called via elf_link_hash_traverse. */
1495
1496bfd_boolean
268b6b39
AM
1497_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1498 void *data)
45d6a902 1499{
268b6b39 1500 struct elf_find_verdep_info *rinfo = data;
45d6a902
AM
1501 Elf_Internal_Verneed *t;
1502 Elf_Internal_Vernaux *a;
1503 bfd_size_type amt;
1504
1505 if (h->root.type == bfd_link_hash_warning)
1506 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1507
1508 /* We only care about symbols defined in shared objects with version
1509 information. */
1510 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1511 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1512 || h->dynindx == -1
1513 || h->verinfo.verdef == NULL)
1514 return TRUE;
1515
1516 /* See if we already know about this version. */
1517 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1518 {
1519 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1520 continue;
1521
1522 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1523 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1524 return TRUE;
1525
1526 break;
1527 }
1528
1529 /* This is a new version. Add it to tree we are building. */
1530
1531 if (t == NULL)
1532 {
1533 amt = sizeof *t;
268b6b39 1534 t = bfd_zalloc (rinfo->output_bfd, amt);
45d6a902
AM
1535 if (t == NULL)
1536 {
1537 rinfo->failed = TRUE;
1538 return FALSE;
1539 }
1540
1541 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1542 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1543 elf_tdata (rinfo->output_bfd)->verref = t;
1544 }
1545
1546 amt = sizeof *a;
268b6b39 1547 a = bfd_zalloc (rinfo->output_bfd, amt);
45d6a902
AM
1548
1549 /* Note that we are copying a string pointer here, and testing it
1550 above. If bfd_elf_string_from_elf_section is ever changed to
1551 discard the string data when low in memory, this will have to be
1552 fixed. */
1553 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1554
1555 a->vna_flags = h->verinfo.verdef->vd_flags;
1556 a->vna_nextptr = t->vn_auxptr;
1557
1558 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1559 ++rinfo->vers;
1560
1561 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1562
1563 t->vn_auxptr = a;
1564
1565 return TRUE;
1566}
1567
1568/* Figure out appropriate versions for all the symbols. We may not
1569 have the version number script until we have read all of the input
1570 files, so until that point we don't know which symbols should be
1571 local. This function is called via elf_link_hash_traverse. */
1572
1573bfd_boolean
268b6b39 1574_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
1575{
1576 struct elf_assign_sym_version_info *sinfo;
1577 struct bfd_link_info *info;
9c5bfbb7 1578 const struct elf_backend_data *bed;
45d6a902
AM
1579 struct elf_info_failed eif;
1580 char *p;
1581 bfd_size_type amt;
1582
268b6b39 1583 sinfo = data;
45d6a902
AM
1584 info = sinfo->info;
1585
1586 if (h->root.type == bfd_link_hash_warning)
1587 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1588
1589 /* Fix the symbol flags. */
1590 eif.failed = FALSE;
1591 eif.info = info;
1592 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1593 {
1594 if (eif.failed)
1595 sinfo->failed = TRUE;
1596 return FALSE;
1597 }
1598
1599 /* We only need version numbers for symbols defined in regular
1600 objects. */
1601 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1602 return TRUE;
1603
1604 bed = get_elf_backend_data (sinfo->output_bfd);
1605 p = strchr (h->root.root.string, ELF_VER_CHR);
1606 if (p != NULL && h->verinfo.vertree == NULL)
1607 {
1608 struct bfd_elf_version_tree *t;
1609 bfd_boolean hidden;
1610
1611 hidden = TRUE;
1612
1613 /* There are two consecutive ELF_VER_CHR characters if this is
1614 not a hidden symbol. */
1615 ++p;
1616 if (*p == ELF_VER_CHR)
1617 {
1618 hidden = FALSE;
1619 ++p;
1620 }
1621
1622 /* If there is no version string, we can just return out. */
1623 if (*p == '\0')
1624 {
1625 if (hidden)
1626 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1627 return TRUE;
1628 }
1629
1630 /* Look for the version. If we find it, it is no longer weak. */
1631 for (t = sinfo->verdefs; t != NULL; t = t->next)
1632 {
1633 if (strcmp (t->name, p) == 0)
1634 {
1635 size_t len;
1636 char *alc;
1637 struct bfd_elf_version_expr *d;
1638
1639 len = p - h->root.root.string;
268b6b39 1640 alc = bfd_malloc (len);
45d6a902
AM
1641 if (alc == NULL)
1642 return FALSE;
1643 memcpy (alc, h->root.root.string, len - 1);
1644 alc[len - 1] = '\0';
1645 if (alc[len - 2] == ELF_VER_CHR)
1646 alc[len - 2] = '\0';
1647
1648 h->verinfo.vertree = t;
1649 t->used = TRUE;
1650 d = NULL;
1651
108ba305
JJ
1652 if (t->globals.list != NULL)
1653 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
1654
1655 /* See if there is anything to force this symbol to
1656 local scope. */
108ba305 1657 if (d == NULL && t->locals.list != NULL)
45d6a902 1658 {
108ba305
JJ
1659 d = (*t->match) (&t->locals, NULL, alc);
1660 if (d != NULL
1661 && h->dynindx != -1
1662 && info->shared
1663 && ! info->export_dynamic)
1664 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
1665 }
1666
1667 free (alc);
1668 break;
1669 }
1670 }
1671
1672 /* If we are building an application, we need to create a
1673 version node for this version. */
36af4a4e 1674 if (t == NULL && info->executable)
45d6a902
AM
1675 {
1676 struct bfd_elf_version_tree **pp;
1677 int version_index;
1678
1679 /* If we aren't going to export this symbol, we don't need
1680 to worry about it. */
1681 if (h->dynindx == -1)
1682 return TRUE;
1683
1684 amt = sizeof *t;
108ba305 1685 t = bfd_zalloc (sinfo->output_bfd, amt);
45d6a902
AM
1686 if (t == NULL)
1687 {
1688 sinfo->failed = TRUE;
1689 return FALSE;
1690 }
1691
45d6a902 1692 t->name = p;
45d6a902
AM
1693 t->name_indx = (unsigned int) -1;
1694 t->used = TRUE;
1695
1696 version_index = 1;
1697 /* Don't count anonymous version tag. */
1698 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1699 version_index = 0;
1700 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1701 ++version_index;
1702 t->vernum = version_index;
1703
1704 *pp = t;
1705
1706 h->verinfo.vertree = t;
1707 }
1708 else if (t == NULL)
1709 {
1710 /* We could not find the version for a symbol when
1711 generating a shared archive. Return an error. */
1712 (*_bfd_error_handler)
d003868e
AM
1713 (_("%B: undefined versioned symbol name %s"),
1714 sinfo->output_bfd, h->root.root.string);
45d6a902
AM
1715 bfd_set_error (bfd_error_bad_value);
1716 sinfo->failed = TRUE;
1717 return FALSE;
1718 }
1719
1720 if (hidden)
1721 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1722 }
1723
1724 /* If we don't have a version for this symbol, see if we can find
1725 something. */
1726 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1727 {
1728 struct bfd_elf_version_tree *t;
1729 struct bfd_elf_version_tree *local_ver;
1730 struct bfd_elf_version_expr *d;
1731
1732 /* See if can find what version this symbol is in. If the
1733 symbol is supposed to be local, then don't actually register
1734 it. */
1735 local_ver = NULL;
1736 for (t = sinfo->verdefs; t != NULL; t = t->next)
1737 {
108ba305 1738 if (t->globals.list != NULL)
45d6a902
AM
1739 {
1740 bfd_boolean matched;
1741
1742 matched = FALSE;
108ba305
JJ
1743 d = NULL;
1744 while ((d = (*t->match) (&t->globals, d,
1745 h->root.root.string)) != NULL)
1746 if (d->symver)
1747 matched = TRUE;
1748 else
1749 {
1750 /* There is a version without definition. Make
1751 the symbol the default definition for this
1752 version. */
1753 h->verinfo.vertree = t;
1754 local_ver = NULL;
1755 d->script = 1;
1756 break;
1757 }
45d6a902
AM
1758 if (d != NULL)
1759 break;
1760 else if (matched)
1761 /* There is no undefined version for this symbol. Hide the
1762 default one. */
1763 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1764 }
1765
108ba305 1766 if (t->locals.list != NULL)
45d6a902 1767 {
108ba305
JJ
1768 d = NULL;
1769 while ((d = (*t->match) (&t->locals, d,
1770 h->root.root.string)) != NULL)
45d6a902 1771 {
108ba305 1772 local_ver = t;
45d6a902 1773 /* If the match is "*", keep looking for a more
108ba305
JJ
1774 explicit, perhaps even global, match.
1775 XXX: Shouldn't this be !d->wildcard instead? */
1776 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1777 break;
45d6a902
AM
1778 }
1779
1780 if (d != NULL)
1781 break;
1782 }
1783 }
1784
1785 if (local_ver != NULL)
1786 {
1787 h->verinfo.vertree = local_ver;
1788 if (h->dynindx != -1
1789 && info->shared
1790 && ! info->export_dynamic)
1791 {
1792 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1793 }
1794 }
1795 }
1796
1797 return TRUE;
1798}
1799\f
45d6a902
AM
1800/* Read and swap the relocs from the section indicated by SHDR. This
1801 may be either a REL or a RELA section. The relocations are
1802 translated into RELA relocations and stored in INTERNAL_RELOCS,
1803 which should have already been allocated to contain enough space.
1804 The EXTERNAL_RELOCS are a buffer where the external form of the
1805 relocations should be stored.
1806
1807 Returns FALSE if something goes wrong. */
1808
1809static bfd_boolean
268b6b39 1810elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 1811 asection *sec,
268b6b39
AM
1812 Elf_Internal_Shdr *shdr,
1813 void *external_relocs,
1814 Elf_Internal_Rela *internal_relocs)
45d6a902 1815{
9c5bfbb7 1816 const struct elf_backend_data *bed;
268b6b39 1817 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
1818 const bfd_byte *erela;
1819 const bfd_byte *erelaend;
1820 Elf_Internal_Rela *irela;
243ef1e0
L
1821 Elf_Internal_Shdr *symtab_hdr;
1822 size_t nsyms;
45d6a902 1823
45d6a902
AM
1824 /* Position ourselves at the start of the section. */
1825 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1826 return FALSE;
1827
1828 /* Read the relocations. */
1829 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1830 return FALSE;
1831
243ef1e0
L
1832 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1833 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1834
45d6a902
AM
1835 bed = get_elf_backend_data (abfd);
1836
1837 /* Convert the external relocations to the internal format. */
1838 if (shdr->sh_entsize == bed->s->sizeof_rel)
1839 swap_in = bed->s->swap_reloc_in;
1840 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1841 swap_in = bed->s->swap_reloca_in;
1842 else
1843 {
1844 bfd_set_error (bfd_error_wrong_format);
1845 return FALSE;
1846 }
1847
1848 erela = external_relocs;
51992aec 1849 erelaend = erela + shdr->sh_size;
45d6a902
AM
1850 irela = internal_relocs;
1851 while (erela < erelaend)
1852 {
243ef1e0
L
1853 bfd_vma r_symndx;
1854
45d6a902 1855 (*swap_in) (abfd, erela, irela);
243ef1e0
L
1856 r_symndx = ELF32_R_SYM (irela->r_info);
1857 if (bed->s->arch_size == 64)
1858 r_symndx >>= 24;
1859 if ((size_t) r_symndx >= nsyms)
1860 {
1861 (*_bfd_error_handler)
d003868e
AM
1862 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1863 " for offset 0x%lx in section `%A'"),
1864 abfd, sec,
1865 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
1866 bfd_set_error (bfd_error_bad_value);
1867 return FALSE;
1868 }
45d6a902
AM
1869 irela += bed->s->int_rels_per_ext_rel;
1870 erela += shdr->sh_entsize;
1871 }
1872
1873 return TRUE;
1874}
1875
1876/* Read and swap the relocs for a section O. They may have been
1877 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1878 not NULL, they are used as buffers to read into. They are known to
1879 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1880 the return value is allocated using either malloc or bfd_alloc,
1881 according to the KEEP_MEMORY argument. If O has two relocation
1882 sections (both REL and RELA relocations), then the REL_HDR
1883 relocations will appear first in INTERNAL_RELOCS, followed by the
1884 REL_HDR2 relocations. */
1885
1886Elf_Internal_Rela *
268b6b39
AM
1887_bfd_elf_link_read_relocs (bfd *abfd,
1888 asection *o,
1889 void *external_relocs,
1890 Elf_Internal_Rela *internal_relocs,
1891 bfd_boolean keep_memory)
45d6a902
AM
1892{
1893 Elf_Internal_Shdr *rel_hdr;
268b6b39 1894 void *alloc1 = NULL;
45d6a902 1895 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 1896 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
45d6a902
AM
1897
1898 if (elf_section_data (o)->relocs != NULL)
1899 return elf_section_data (o)->relocs;
1900
1901 if (o->reloc_count == 0)
1902 return NULL;
1903
1904 rel_hdr = &elf_section_data (o)->rel_hdr;
1905
1906 if (internal_relocs == NULL)
1907 {
1908 bfd_size_type size;
1909
1910 size = o->reloc_count;
1911 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1912 if (keep_memory)
268b6b39 1913 internal_relocs = bfd_alloc (abfd, size);
45d6a902 1914 else
268b6b39 1915 internal_relocs = alloc2 = bfd_malloc (size);
45d6a902
AM
1916 if (internal_relocs == NULL)
1917 goto error_return;
1918 }
1919
1920 if (external_relocs == NULL)
1921 {
1922 bfd_size_type size = rel_hdr->sh_size;
1923
1924 if (elf_section_data (o)->rel_hdr2)
1925 size += elf_section_data (o)->rel_hdr2->sh_size;
268b6b39 1926 alloc1 = bfd_malloc (size);
45d6a902
AM
1927 if (alloc1 == NULL)
1928 goto error_return;
1929 external_relocs = alloc1;
1930 }
1931
243ef1e0 1932 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
45d6a902
AM
1933 external_relocs,
1934 internal_relocs))
1935 goto error_return;
51992aec
AM
1936 if (elf_section_data (o)->rel_hdr2
1937 && (!elf_link_read_relocs_from_section
1938 (abfd, o,
1939 elf_section_data (o)->rel_hdr2,
1940 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
1941 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
1942 * bed->s->int_rels_per_ext_rel))))
45d6a902
AM
1943 goto error_return;
1944
1945 /* Cache the results for next time, if we can. */
1946 if (keep_memory)
1947 elf_section_data (o)->relocs = internal_relocs;
1948
1949 if (alloc1 != NULL)
1950 free (alloc1);
1951
1952 /* Don't free alloc2, since if it was allocated we are passing it
1953 back (under the name of internal_relocs). */
1954
1955 return internal_relocs;
1956
1957 error_return:
1958 if (alloc1 != NULL)
1959 free (alloc1);
1960 if (alloc2 != NULL)
1961 free (alloc2);
1962 return NULL;
1963}
1964
1965/* Compute the size of, and allocate space for, REL_HDR which is the
1966 section header for a section containing relocations for O. */
1967
1968bfd_boolean
268b6b39
AM
1969_bfd_elf_link_size_reloc_section (bfd *abfd,
1970 Elf_Internal_Shdr *rel_hdr,
1971 asection *o)
45d6a902
AM
1972{
1973 bfd_size_type reloc_count;
1974 bfd_size_type num_rel_hashes;
1975
1976 /* Figure out how many relocations there will be. */
1977 if (rel_hdr == &elf_section_data (o)->rel_hdr)
1978 reloc_count = elf_section_data (o)->rel_count;
1979 else
1980 reloc_count = elf_section_data (o)->rel_count2;
1981
1982 num_rel_hashes = o->reloc_count;
1983 if (num_rel_hashes < reloc_count)
1984 num_rel_hashes = reloc_count;
1985
1986 /* That allows us to calculate the size of the section. */
1987 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
1988
1989 /* The contents field must last into write_object_contents, so we
1990 allocate it with bfd_alloc rather than malloc. Also since we
1991 cannot be sure that the contents will actually be filled in,
1992 we zero the allocated space. */
268b6b39 1993 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
1994 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
1995 return FALSE;
1996
1997 /* We only allocate one set of hash entries, so we only do it the
1998 first time we are called. */
1999 if (elf_section_data (o)->rel_hashes == NULL
2000 && num_rel_hashes)
2001 {
2002 struct elf_link_hash_entry **p;
2003
268b6b39 2004 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
45d6a902
AM
2005 if (p == NULL)
2006 return FALSE;
2007
2008 elf_section_data (o)->rel_hashes = p;
2009 }
2010
2011 return TRUE;
2012}
2013
2014/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2015 originated from the section given by INPUT_REL_HDR) to the
2016 OUTPUT_BFD. */
2017
2018bfd_boolean
268b6b39
AM
2019_bfd_elf_link_output_relocs (bfd *output_bfd,
2020 asection *input_section,
2021 Elf_Internal_Shdr *input_rel_hdr,
2022 Elf_Internal_Rela *internal_relocs)
45d6a902
AM
2023{
2024 Elf_Internal_Rela *irela;
2025 Elf_Internal_Rela *irelaend;
2026 bfd_byte *erel;
2027 Elf_Internal_Shdr *output_rel_hdr;
2028 asection *output_section;
2029 unsigned int *rel_countp = NULL;
9c5bfbb7 2030 const struct elf_backend_data *bed;
268b6b39 2031 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
45d6a902
AM
2032
2033 output_section = input_section->output_section;
2034 output_rel_hdr = NULL;
2035
2036 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2037 == input_rel_hdr->sh_entsize)
2038 {
2039 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2040 rel_countp = &elf_section_data (output_section)->rel_count;
2041 }
2042 else if (elf_section_data (output_section)->rel_hdr2
2043 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2044 == input_rel_hdr->sh_entsize))
2045 {
2046 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2047 rel_countp = &elf_section_data (output_section)->rel_count2;
2048 }
2049 else
2050 {
2051 (*_bfd_error_handler)
d003868e
AM
2052 (_("%B: relocation size mismatch in %B section %A"),
2053 output_bfd, input_section->owner, input_section);
45d6a902
AM
2054 bfd_set_error (bfd_error_wrong_object_format);
2055 return FALSE;
2056 }
2057
2058 bed = get_elf_backend_data (output_bfd);
2059 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2060 swap_out = bed->s->swap_reloc_out;
2061 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2062 swap_out = bed->s->swap_reloca_out;
2063 else
2064 abort ();
2065
2066 erel = output_rel_hdr->contents;
2067 erel += *rel_countp * input_rel_hdr->sh_entsize;
2068 irela = internal_relocs;
2069 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2070 * bed->s->int_rels_per_ext_rel);
2071 while (irela < irelaend)
2072 {
2073 (*swap_out) (output_bfd, irela, erel);
2074 irela += bed->s->int_rels_per_ext_rel;
2075 erel += input_rel_hdr->sh_entsize;
2076 }
2077
2078 /* Bump the counter, so that we know where to add the next set of
2079 relocations. */
2080 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2081
2082 return TRUE;
2083}
2084\f
2085/* Fix up the flags for a symbol. This handles various cases which
2086 can only be fixed after all the input files are seen. This is
2087 currently called by both adjust_dynamic_symbol and
2088 assign_sym_version, which is unnecessary but perhaps more robust in
2089 the face of future changes. */
2090
2091bfd_boolean
268b6b39
AM
2092_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2093 struct elf_info_failed *eif)
45d6a902
AM
2094{
2095 /* If this symbol was mentioned in a non-ELF file, try to set
2096 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2097 permit a non-ELF file to correctly refer to a symbol defined in
2098 an ELF dynamic object. */
2099 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
2100 {
2101 while (h->root.type == bfd_link_hash_indirect)
2102 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2103
2104 if (h->root.type != bfd_link_hash_defined
2105 && h->root.type != bfd_link_hash_defweak)
2106 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2107 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2108 else
2109 {
2110 if (h->root.u.def.section->owner != NULL
2111 && (bfd_get_flavour (h->root.u.def.section->owner)
2112 == bfd_target_elf_flavour))
2113 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2114 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2115 else
2116 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2117 }
2118
2119 if (h->dynindx == -1
2120 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2121 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
2122 {
c152c796 2123 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2124 {
2125 eif->failed = TRUE;
2126 return FALSE;
2127 }
2128 }
2129 }
2130 else
2131 {
2132 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2133 was first seen in a non-ELF file. Fortunately, if the symbol
2134 was first seen in an ELF file, we're probably OK unless the
2135 symbol was defined in a non-ELF file. Catch that case here.
2136 FIXME: We're still in trouble if the symbol was first seen in
2137 a dynamic object, and then later in a non-ELF regular object. */
2138 if ((h->root.type == bfd_link_hash_defined
2139 || h->root.type == bfd_link_hash_defweak)
2140 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2141 && (h->root.u.def.section->owner != NULL
2142 ? (bfd_get_flavour (h->root.u.def.section->owner)
2143 != bfd_target_elf_flavour)
2144 : (bfd_is_abs_section (h->root.u.def.section)
2145 && (h->elf_link_hash_flags
2146 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
2147 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2148 }
2149
2150 /* If this is a final link, and the symbol was defined as a common
2151 symbol in a regular object file, and there was no definition in
2152 any dynamic object, then the linker will have allocated space for
2153 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2154 flag will not have been set. */
2155 if (h->root.type == bfd_link_hash_defined
2156 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2157 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
2158 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2159 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2160 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2161
2162 /* If -Bsymbolic was used (which means to bind references to global
2163 symbols to the definition within the shared object), and this
2164 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2165 need a PLT entry. Likewise, if the symbol has non-default
2166 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2167 will force it local. */
45d6a902
AM
2168 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2169 && eif->info->shared
0eddce27 2170 && is_elf_hash_table (eif->info->hash)
45d6a902 2171 && (eif->info->symbolic
c1be741f 2172 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
45d6a902
AM
2173 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2174 {
9c5bfbb7 2175 const struct elf_backend_data *bed;
45d6a902
AM
2176 bfd_boolean force_local;
2177
2178 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2179
2180 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2181 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2182 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2183 }
2184
2185 /* If a weak undefined symbol has non-default visibility, we also
2186 hide it from the dynamic linker. */
9c7a29a3 2187 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
2188 && h->root.type == bfd_link_hash_undefweak)
2189 {
9c5bfbb7 2190 const struct elf_backend_data *bed;
45d6a902
AM
2191 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2192 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2193 }
2194
2195 /* If this is a weak defined symbol in a dynamic object, and we know
2196 the real definition in the dynamic object, copy interesting flags
2197 over to the real definition. */
2198 if (h->weakdef != NULL)
2199 {
2200 struct elf_link_hash_entry *weakdef;
2201
2202 weakdef = h->weakdef;
2203 if (h->root.type == bfd_link_hash_indirect)
2204 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2205
2206 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2207 || h->root.type == bfd_link_hash_defweak);
2208 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2209 || weakdef->root.type == bfd_link_hash_defweak);
2210 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
2211
2212 /* If the real definition is defined by a regular object file,
2213 don't do anything special. See the longer description in
2214 _bfd_elf_adjust_dynamic_symbol, below. */
2215 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2216 h->weakdef = NULL;
2217 else
2218 {
9c5bfbb7 2219 const struct elf_backend_data *bed;
45d6a902
AM
2220
2221 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2222 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2223 }
2224 }
2225
2226 return TRUE;
2227}
2228
2229/* Make the backend pick a good value for a dynamic symbol. This is
2230 called via elf_link_hash_traverse, and also calls itself
2231 recursively. */
2232
2233bfd_boolean
268b6b39 2234_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2235{
268b6b39 2236 struct elf_info_failed *eif = data;
45d6a902 2237 bfd *dynobj;
9c5bfbb7 2238 const struct elf_backend_data *bed;
45d6a902 2239
0eddce27 2240 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2241 return FALSE;
2242
2243 if (h->root.type == bfd_link_hash_warning)
2244 {
2245 h->plt = elf_hash_table (eif->info)->init_offset;
2246 h->got = elf_hash_table (eif->info)->init_offset;
2247
2248 /* When warning symbols are created, they **replace** the "real"
2249 entry in the hash table, thus we never get to see the real
2250 symbol in a hash traversal. So look at it now. */
2251 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2252 }
2253
2254 /* Ignore indirect symbols. These are added by the versioning code. */
2255 if (h->root.type == bfd_link_hash_indirect)
2256 return TRUE;
2257
2258 /* Fix the symbol flags. */
2259 if (! _bfd_elf_fix_symbol_flags (h, eif))
2260 return FALSE;
2261
2262 /* If this symbol does not require a PLT entry, and it is not
2263 defined by a dynamic object, or is not referenced by a regular
2264 object, ignore it. We do have to handle a weak defined symbol,
2265 even if no regular object refers to it, if we decided to add it
2266 to the dynamic symbol table. FIXME: Do we normally need to worry
2267 about symbols which are defined by one dynamic object and
2268 referenced by another one? */
2269 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
2270 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2271 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2272 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
2273 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
2274 {
2275 h->plt = elf_hash_table (eif->info)->init_offset;
2276 return TRUE;
2277 }
2278
2279 /* If we've already adjusted this symbol, don't do it again. This
2280 can happen via a recursive call. */
2281 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
2282 return TRUE;
2283
2284 /* Don't look at this symbol again. Note that we must set this
2285 after checking the above conditions, because we may look at a
2286 symbol once, decide not to do anything, and then get called
2287 recursively later after REF_REGULAR is set below. */
2288 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
2289
2290 /* If this is a weak definition, and we know a real definition, and
2291 the real symbol is not itself defined by a regular object file,
2292 then get a good value for the real definition. We handle the
2293 real symbol first, for the convenience of the backend routine.
2294
2295 Note that there is a confusing case here. If the real definition
2296 is defined by a regular object file, we don't get the real symbol
2297 from the dynamic object, but we do get the weak symbol. If the
2298 processor backend uses a COPY reloc, then if some routine in the
2299 dynamic object changes the real symbol, we will not see that
2300 change in the corresponding weak symbol. This is the way other
2301 ELF linkers work as well, and seems to be a result of the shared
2302 library model.
2303
2304 I will clarify this issue. Most SVR4 shared libraries define the
2305 variable _timezone and define timezone as a weak synonym. The
2306 tzset call changes _timezone. If you write
2307 extern int timezone;
2308 int _timezone = 5;
2309 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2310 you might expect that, since timezone is a synonym for _timezone,
2311 the same number will print both times. However, if the processor
2312 backend uses a COPY reloc, then actually timezone will be copied
2313 into your process image, and, since you define _timezone
2314 yourself, _timezone will not. Thus timezone and _timezone will
2315 wind up at different memory locations. The tzset call will set
2316 _timezone, leaving timezone unchanged. */
2317
2318 if (h->weakdef != NULL)
2319 {
2320 /* If we get to this point, we know there is an implicit
2321 reference by a regular object file via the weak symbol H.
2322 FIXME: Is this really true? What if the traversal finds
2323 H->WEAKDEF before it finds H? */
2324 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2325
268b6b39 2326 if (! _bfd_elf_adjust_dynamic_symbol (h->weakdef, eif))
45d6a902
AM
2327 return FALSE;
2328 }
2329
2330 /* If a symbol has no type and no size and does not require a PLT
2331 entry, then we are probably about to do the wrong thing here: we
2332 are probably going to create a COPY reloc for an empty object.
2333 This case can arise when a shared object is built with assembly
2334 code, and the assembly code fails to set the symbol type. */
2335 if (h->size == 0
2336 && h->type == STT_NOTYPE
2337 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
2338 (*_bfd_error_handler)
2339 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2340 h->root.root.string);
2341
2342 dynobj = elf_hash_table (eif->info)->dynobj;
2343 bed = get_elf_backend_data (dynobj);
2344 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2345 {
2346 eif->failed = TRUE;
2347 return FALSE;
2348 }
2349
2350 return TRUE;
2351}
2352
2353/* Adjust all external symbols pointing into SEC_MERGE sections
2354 to reflect the object merging within the sections. */
2355
2356bfd_boolean
268b6b39 2357_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2358{
2359 asection *sec;
2360
2361 if (h->root.type == bfd_link_hash_warning)
2362 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2363
2364 if ((h->root.type == bfd_link_hash_defined
2365 || h->root.type == bfd_link_hash_defweak)
2366 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2367 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2368 {
268b6b39 2369 bfd *output_bfd = data;
45d6a902
AM
2370
2371 h->root.u.def.value =
2372 _bfd_merged_section_offset (output_bfd,
2373 &h->root.u.def.section,
2374 elf_section_data (sec)->sec_info,
753731ee 2375 h->root.u.def.value);
45d6a902
AM
2376 }
2377
2378 return TRUE;
2379}
986a241f
RH
2380
2381/* Returns false if the symbol referred to by H should be considered
2382 to resolve local to the current module, and true if it should be
2383 considered to bind dynamically. */
2384
2385bfd_boolean
268b6b39
AM
2386_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2387 struct bfd_link_info *info,
2388 bfd_boolean ignore_protected)
986a241f
RH
2389{
2390 bfd_boolean binding_stays_local_p;
2391
2392 if (h == NULL)
2393 return FALSE;
2394
2395 while (h->root.type == bfd_link_hash_indirect
2396 || h->root.type == bfd_link_hash_warning)
2397 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2398
2399 /* If it was forced local, then clearly it's not dynamic. */
2400 if (h->dynindx == -1)
2401 return FALSE;
2402 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2403 return FALSE;
2404
2405 /* Identify the cases where name binding rules say that a
2406 visible symbol resolves locally. */
2407 binding_stays_local_p = info->executable || info->symbolic;
2408
2409 switch (ELF_ST_VISIBILITY (h->other))
2410 {
2411 case STV_INTERNAL:
2412 case STV_HIDDEN:
2413 return FALSE;
2414
2415 case STV_PROTECTED:
2416 /* Proper resolution for function pointer equality may require
2417 that these symbols perhaps be resolved dynamically, even though
2418 we should be resolving them to the current module. */
2419 if (!ignore_protected)
2420 binding_stays_local_p = TRUE;
2421 break;
2422
2423 default:
986a241f
RH
2424 break;
2425 }
2426
aa37626c
L
2427 /* If it isn't defined locally, then clearly it's dynamic. */
2428 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2429 return TRUE;
2430
986a241f
RH
2431 /* Otherwise, the symbol is dynamic if binding rules don't tell
2432 us that it remains local. */
2433 return !binding_stays_local_p;
2434}
f6c52c13
AM
2435
2436/* Return true if the symbol referred to by H should be considered
2437 to resolve local to the current module, and false otherwise. Differs
2438 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2439 undefined symbols and weak symbols. */
2440
2441bfd_boolean
268b6b39
AM
2442_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2443 struct bfd_link_info *info,
2444 bfd_boolean local_protected)
f6c52c13
AM
2445{
2446 /* If it's a local sym, of course we resolve locally. */
2447 if (h == NULL)
2448 return TRUE;
2449
7e2294f9
AO
2450 /* Common symbols that become definitions don't get the DEF_REGULAR
2451 flag set, so test it first, and don't bail out. */
2452 if (ELF_COMMON_DEF_P (h))
2453 /* Do nothing. */;
f6c52c13
AM
2454 /* If we don't have a definition in a regular file, then we can't
2455 resolve locally. The sym is either undefined or dynamic. */
7e2294f9 2456 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
f6c52c13
AM
2457 return FALSE;
2458
2459 /* Forced local symbols resolve locally. */
2460 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
2461 return TRUE;
2462
2463 /* As do non-dynamic symbols. */
2464 if (h->dynindx == -1)
2465 return TRUE;
2466
2467 /* At this point, we know the symbol is defined and dynamic. In an
2468 executable it must resolve locally, likewise when building symbolic
2469 shared libraries. */
2470 if (info->executable || info->symbolic)
2471 return TRUE;
2472
2473 /* Now deal with defined dynamic symbols in shared libraries. Ones
2474 with default visibility might not resolve locally. */
2475 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2476 return FALSE;
2477
2478 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2479 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2480 return TRUE;
2481
2482 /* Function pointer equality tests may require that STV_PROTECTED
2483 symbols be treated as dynamic symbols, even when we know that the
2484 dynamic linker will resolve them locally. */
2485 return local_protected;
2486}
e1918d23
AM
2487
2488/* Caches some TLS segment info, and ensures that the TLS segment vma is
2489 aligned. Returns the first TLS output section. */
2490
2491struct bfd_section *
2492_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2493{
2494 struct bfd_section *sec, *tls;
2495 unsigned int align = 0;
2496
2497 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2498 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2499 break;
2500 tls = sec;
2501
2502 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2503 if (sec->alignment_power > align)
2504 align = sec->alignment_power;
2505
2506 elf_hash_table (info)->tls_sec = tls;
2507
2508 /* Ensure the alignment of the first section is the largest alignment,
2509 so that the tls segment starts aligned. */
2510 if (tls != NULL)
2511 tls->alignment_power = align;
2512
2513 return tls;
2514}
0ad989f9
L
2515
2516/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2517static bfd_boolean
2518is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2519 Elf_Internal_Sym *sym)
2520{
2521 /* Local symbols do not count, but target specific ones might. */
2522 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2523 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2524 return FALSE;
2525
2526 /* Function symbols do not count. */
2527 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2528 return FALSE;
2529
2530 /* If the section is undefined, then so is the symbol. */
2531 if (sym->st_shndx == SHN_UNDEF)
2532 return FALSE;
2533
2534 /* If the symbol is defined in the common section, then
2535 it is a common definition and so does not count. */
2536 if (sym->st_shndx == SHN_COMMON)
2537 return FALSE;
2538
2539 /* If the symbol is in a target specific section then we
2540 must rely upon the backend to tell us what it is. */
2541 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2542 /* FIXME - this function is not coded yet:
2543
2544 return _bfd_is_global_symbol_definition (abfd, sym);
2545
2546 Instead for now assume that the definition is not global,
2547 Even if this is wrong, at least the linker will behave
2548 in the same way that it used to do. */
2549 return FALSE;
2550
2551 return TRUE;
2552}
2553
2554/* Search the symbol table of the archive element of the archive ABFD
2555 whose archive map contains a mention of SYMDEF, and determine if
2556 the symbol is defined in this element. */
2557static bfd_boolean
2558elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2559{
2560 Elf_Internal_Shdr * hdr;
2561 bfd_size_type symcount;
2562 bfd_size_type extsymcount;
2563 bfd_size_type extsymoff;
2564 Elf_Internal_Sym *isymbuf;
2565 Elf_Internal_Sym *isym;
2566 Elf_Internal_Sym *isymend;
2567 bfd_boolean result;
2568
2569 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2570 if (abfd == NULL)
2571 return FALSE;
2572
2573 if (! bfd_check_format (abfd, bfd_object))
2574 return FALSE;
2575
2576 /* If we have already included the element containing this symbol in the
2577 link then we do not need to include it again. Just claim that any symbol
2578 it contains is not a definition, so that our caller will not decide to
2579 (re)include this element. */
2580 if (abfd->archive_pass)
2581 return FALSE;
2582
2583 /* Select the appropriate symbol table. */
2584 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2585 hdr = &elf_tdata (abfd)->symtab_hdr;
2586 else
2587 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2588
2589 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2590
2591 /* The sh_info field of the symtab header tells us where the
2592 external symbols start. We don't care about the local symbols. */
2593 if (elf_bad_symtab (abfd))
2594 {
2595 extsymcount = symcount;
2596 extsymoff = 0;
2597 }
2598 else
2599 {
2600 extsymcount = symcount - hdr->sh_info;
2601 extsymoff = hdr->sh_info;
2602 }
2603
2604 if (extsymcount == 0)
2605 return FALSE;
2606
2607 /* Read in the symbol table. */
2608 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2609 NULL, NULL, NULL);
2610 if (isymbuf == NULL)
2611 return FALSE;
2612
2613 /* Scan the symbol table looking for SYMDEF. */
2614 result = FALSE;
2615 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2616 {
2617 const char *name;
2618
2619 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2620 isym->st_name);
2621 if (name == NULL)
2622 break;
2623
2624 if (strcmp (name, symdef->name) == 0)
2625 {
2626 result = is_global_data_symbol_definition (abfd, isym);
2627 break;
2628 }
2629 }
2630
2631 free (isymbuf);
2632
2633 return result;
2634}
2635\f
5a580b3a
AM
2636/* Add an entry to the .dynamic table. */
2637
2638bfd_boolean
2639_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2640 bfd_vma tag,
2641 bfd_vma val)
2642{
2643 struct elf_link_hash_table *hash_table;
2644 const struct elf_backend_data *bed;
2645 asection *s;
2646 bfd_size_type newsize;
2647 bfd_byte *newcontents;
2648 Elf_Internal_Dyn dyn;
2649
2650 hash_table = elf_hash_table (info);
2651 if (! is_elf_hash_table (hash_table))
2652 return FALSE;
2653
2654 bed = get_elf_backend_data (hash_table->dynobj);
2655 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2656 BFD_ASSERT (s != NULL);
2657
eea6121a 2658 newsize = s->size + bed->s->sizeof_dyn;
5a580b3a
AM
2659 newcontents = bfd_realloc (s->contents, newsize);
2660 if (newcontents == NULL)
2661 return FALSE;
2662
2663 dyn.d_tag = tag;
2664 dyn.d_un.d_val = val;
eea6121a 2665 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 2666
eea6121a 2667 s->size = newsize;
5a580b3a
AM
2668 s->contents = newcontents;
2669
2670 return TRUE;
2671}
2672
2673/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2674 otherwise just check whether one already exists. Returns -1 on error,
2675 1 if a DT_NEEDED tag already exists, and 0 on success. */
2676
4ad4eba5
AM
2677static int
2678elf_add_dt_needed_tag (struct bfd_link_info *info,
2679 const char *soname,
2680 bfd_boolean do_it)
5a580b3a
AM
2681{
2682 struct elf_link_hash_table *hash_table;
2683 bfd_size_type oldsize;
2684 bfd_size_type strindex;
2685
2686 hash_table = elf_hash_table (info);
2687 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2688 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2689 if (strindex == (bfd_size_type) -1)
2690 return -1;
2691
2692 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2693 {
2694 asection *sdyn;
2695 const struct elf_backend_data *bed;
2696 bfd_byte *extdyn;
2697
2698 bed = get_elf_backend_data (hash_table->dynobj);
2699 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2700 BFD_ASSERT (sdyn != NULL);
2701
2702 for (extdyn = sdyn->contents;
eea6121a 2703 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
2704 extdyn += bed->s->sizeof_dyn)
2705 {
2706 Elf_Internal_Dyn dyn;
2707
2708 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2709 if (dyn.d_tag == DT_NEEDED
2710 && dyn.d_un.d_val == strindex)
2711 {
2712 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2713 return 1;
2714 }
2715 }
2716 }
2717
2718 if (do_it)
2719 {
2720 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2721 return -1;
2722 }
2723 else
2724 /* We were just checking for existence of the tag. */
2725 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2726
2727 return 0;
2728}
2729
2730/* Sort symbol by value and section. */
4ad4eba5
AM
2731static int
2732elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
2733{
2734 const struct elf_link_hash_entry *h1;
2735 const struct elf_link_hash_entry *h2;
10b7e05b 2736 bfd_signed_vma vdiff;
5a580b3a
AM
2737
2738 h1 = *(const struct elf_link_hash_entry **) arg1;
2739 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
2740 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2741 if (vdiff != 0)
2742 return vdiff > 0 ? 1 : -1;
2743 else
2744 {
2745 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2746 if (sdiff != 0)
2747 return sdiff > 0 ? 1 : -1;
2748 }
5a580b3a
AM
2749 return 0;
2750}
4ad4eba5 2751
5a580b3a
AM
2752/* This function is used to adjust offsets into .dynstr for
2753 dynamic symbols. This is called via elf_link_hash_traverse. */
2754
2755static bfd_boolean
2756elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2757{
2758 struct elf_strtab_hash *dynstr = data;
2759
2760 if (h->root.type == bfd_link_hash_warning)
2761 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2762
2763 if (h->dynindx != -1)
2764 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2765 return TRUE;
2766}
2767
2768/* Assign string offsets in .dynstr, update all structures referencing
2769 them. */
2770
4ad4eba5
AM
2771static bfd_boolean
2772elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
2773{
2774 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2775 struct elf_link_local_dynamic_entry *entry;
2776 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2777 bfd *dynobj = hash_table->dynobj;
2778 asection *sdyn;
2779 bfd_size_type size;
2780 const struct elf_backend_data *bed;
2781 bfd_byte *extdyn;
2782
2783 _bfd_elf_strtab_finalize (dynstr);
2784 size = _bfd_elf_strtab_size (dynstr);
2785
2786 bed = get_elf_backend_data (dynobj);
2787 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2788 BFD_ASSERT (sdyn != NULL);
2789
2790 /* Update all .dynamic entries referencing .dynstr strings. */
2791 for (extdyn = sdyn->contents;
eea6121a 2792 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
2793 extdyn += bed->s->sizeof_dyn)
2794 {
2795 Elf_Internal_Dyn dyn;
2796
2797 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2798 switch (dyn.d_tag)
2799 {
2800 case DT_STRSZ:
2801 dyn.d_un.d_val = size;
2802 break;
2803 case DT_NEEDED:
2804 case DT_SONAME:
2805 case DT_RPATH:
2806 case DT_RUNPATH:
2807 case DT_FILTER:
2808 case DT_AUXILIARY:
2809 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2810 break;
2811 default:
2812 continue;
2813 }
2814 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2815 }
2816
2817 /* Now update local dynamic symbols. */
2818 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
2819 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2820 entry->isym.st_name);
2821
2822 /* And the rest of dynamic symbols. */
2823 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
2824
2825 /* Adjust version definitions. */
2826 if (elf_tdata (output_bfd)->cverdefs)
2827 {
2828 asection *s;
2829 bfd_byte *p;
2830 bfd_size_type i;
2831 Elf_Internal_Verdef def;
2832 Elf_Internal_Verdaux defaux;
2833
2834 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2835 p = s->contents;
2836 do
2837 {
2838 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2839 &def);
2840 p += sizeof (Elf_External_Verdef);
2841 for (i = 0; i < def.vd_cnt; ++i)
2842 {
2843 _bfd_elf_swap_verdaux_in (output_bfd,
2844 (Elf_External_Verdaux *) p, &defaux);
2845 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2846 defaux.vda_name);
2847 _bfd_elf_swap_verdaux_out (output_bfd,
2848 &defaux, (Elf_External_Verdaux *) p);
2849 p += sizeof (Elf_External_Verdaux);
2850 }
2851 }
2852 while (def.vd_next);
2853 }
2854
2855 /* Adjust version references. */
2856 if (elf_tdata (output_bfd)->verref)
2857 {
2858 asection *s;
2859 bfd_byte *p;
2860 bfd_size_type i;
2861 Elf_Internal_Verneed need;
2862 Elf_Internal_Vernaux needaux;
2863
2864 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2865 p = s->contents;
2866 do
2867 {
2868 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2869 &need);
2870 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2871 _bfd_elf_swap_verneed_out (output_bfd, &need,
2872 (Elf_External_Verneed *) p);
2873 p += sizeof (Elf_External_Verneed);
2874 for (i = 0; i < need.vn_cnt; ++i)
2875 {
2876 _bfd_elf_swap_vernaux_in (output_bfd,
2877 (Elf_External_Vernaux *) p, &needaux);
2878 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2879 needaux.vna_name);
2880 _bfd_elf_swap_vernaux_out (output_bfd,
2881 &needaux,
2882 (Elf_External_Vernaux *) p);
2883 p += sizeof (Elf_External_Vernaux);
2884 }
2885 }
2886 while (need.vn_next);
2887 }
2888
2889 return TRUE;
2890}
2891\f
4ad4eba5
AM
2892/* Add symbols from an ELF object file to the linker hash table. */
2893
2894static bfd_boolean
2895elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2896{
2897 bfd_boolean (*add_symbol_hook)
555cd476 2898 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
4ad4eba5
AM
2899 const char **, flagword *, asection **, bfd_vma *);
2900 bfd_boolean (*check_relocs)
2901 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
85fbca6a
NC
2902 bfd_boolean (*check_directives)
2903 (bfd *, struct bfd_link_info *);
4ad4eba5
AM
2904 bfd_boolean collect;
2905 Elf_Internal_Shdr *hdr;
2906 bfd_size_type symcount;
2907 bfd_size_type extsymcount;
2908 bfd_size_type extsymoff;
2909 struct elf_link_hash_entry **sym_hash;
2910 bfd_boolean dynamic;
2911 Elf_External_Versym *extversym = NULL;
2912 Elf_External_Versym *ever;
2913 struct elf_link_hash_entry *weaks;
2914 struct elf_link_hash_entry **nondeflt_vers = NULL;
2915 bfd_size_type nondeflt_vers_cnt = 0;
2916 Elf_Internal_Sym *isymbuf = NULL;
2917 Elf_Internal_Sym *isym;
2918 Elf_Internal_Sym *isymend;
2919 const struct elf_backend_data *bed;
2920 bfd_boolean add_needed;
2921 struct elf_link_hash_table * hash_table;
2922 bfd_size_type amt;
2923
2924 hash_table = elf_hash_table (info);
2925
2926 bed = get_elf_backend_data (abfd);
2927 add_symbol_hook = bed->elf_add_symbol_hook;
2928 collect = bed->collect;
2929
2930 if ((abfd->flags & DYNAMIC) == 0)
2931 dynamic = FALSE;
2932 else
2933 {
2934 dynamic = TRUE;
2935
2936 /* You can't use -r against a dynamic object. Also, there's no
2937 hope of using a dynamic object which does not exactly match
2938 the format of the output file. */
2939 if (info->relocatable
2940 || !is_elf_hash_table (hash_table)
2941 || hash_table->root.creator != abfd->xvec)
2942 {
9a0789ec
NC
2943 if (info->relocatable)
2944 bfd_set_error (bfd_error_invalid_operation);
2945 else
2946 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
2947 goto error_return;
2948 }
2949 }
2950
2951 /* As a GNU extension, any input sections which are named
2952 .gnu.warning.SYMBOL are treated as warning symbols for the given
2953 symbol. This differs from .gnu.warning sections, which generate
2954 warnings when they are included in an output file. */
2955 if (info->executable)
2956 {
2957 asection *s;
2958
2959 for (s = abfd->sections; s != NULL; s = s->next)
2960 {
2961 const char *name;
2962
2963 name = bfd_get_section_name (abfd, s);
2964 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
2965 {
2966 char *msg;
2967 bfd_size_type sz;
2968 bfd_size_type prefix_len;
2969 const char * gnu_warning_prefix = _("warning: ");
2970
2971 name += sizeof ".gnu.warning." - 1;
2972
2973 /* If this is a shared object, then look up the symbol
2974 in the hash table. If it is there, and it is already
2975 been defined, then we will not be using the entry
2976 from this shared object, so we don't need to warn.
2977 FIXME: If we see the definition in a regular object
2978 later on, we will warn, but we shouldn't. The only
2979 fix is to keep track of what warnings we are supposed
2980 to emit, and then handle them all at the end of the
2981 link. */
2982 if (dynamic)
2983 {
2984 struct elf_link_hash_entry *h;
2985
2986 h = elf_link_hash_lookup (hash_table, name,
2987 FALSE, FALSE, TRUE);
2988
2989 /* FIXME: What about bfd_link_hash_common? */
2990 if (h != NULL
2991 && (h->root.type == bfd_link_hash_defined
2992 || h->root.type == bfd_link_hash_defweak))
2993 {
2994 /* We don't want to issue this warning. Clobber
2995 the section size so that the warning does not
2996 get copied into the output file. */
eea6121a 2997 s->size = 0;
4ad4eba5
AM
2998 continue;
2999 }
3000 }
3001
eea6121a 3002 sz = s->size;
4ad4eba5
AM
3003 prefix_len = strlen (gnu_warning_prefix);
3004 msg = bfd_alloc (abfd, prefix_len + sz + 1);
3005 if (msg == NULL)
3006 goto error_return;
3007
3008 strcpy (msg, gnu_warning_prefix);
3009 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
3010 goto error_return;
3011
3012 msg[prefix_len + sz] = '\0';
3013
3014 if (! (_bfd_generic_link_add_one_symbol
3015 (info, abfd, name, BSF_WARNING, s, 0, msg,
3016 FALSE, collect, NULL)))
3017 goto error_return;
3018
3019 if (! info->relocatable)
3020 {
3021 /* Clobber the section size so that the warning does
3022 not get copied into the output file. */
eea6121a 3023 s->size = 0;
4ad4eba5
AM
3024 }
3025 }
3026 }
3027 }
3028
3029 add_needed = TRUE;
3030 if (! dynamic)
3031 {
3032 /* If we are creating a shared library, create all the dynamic
3033 sections immediately. We need to attach them to something,
3034 so we attach them to this BFD, provided it is the right
3035 format. FIXME: If there are no input BFD's of the same
3036 format as the output, we can't make a shared library. */
3037 if (info->shared
3038 && is_elf_hash_table (hash_table)
3039 && hash_table->root.creator == abfd->xvec
3040 && ! hash_table->dynamic_sections_created)
3041 {
3042 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3043 goto error_return;
3044 }
3045 }
3046 else if (!is_elf_hash_table (hash_table))
3047 goto error_return;
3048 else
3049 {
3050 asection *s;
3051 const char *soname = NULL;
3052 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3053 int ret;
3054
3055 /* ld --just-symbols and dynamic objects don't mix very well.
3056 Test for --just-symbols by looking at info set up by
3057 _bfd_elf_link_just_syms. */
3058 if ((s = abfd->sections) != NULL
3059 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3060 goto error_return;
3061
3062 /* If this dynamic lib was specified on the command line with
3063 --as-needed in effect, then we don't want to add a DT_NEEDED
3064 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3065 in by another lib's DT_NEEDED. When --no-add-needed is used
3066 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3067 any dynamic library in DT_NEEDED tags in the dynamic lib at
3068 all. */
3069 add_needed = (elf_dyn_lib_class (abfd)
3070 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3071 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3072
3073 s = bfd_get_section_by_name (abfd, ".dynamic");
3074 if (s != NULL)
3075 {
3076 bfd_byte *dynbuf;
3077 bfd_byte *extdyn;
3078 int elfsec;
3079 unsigned long shlink;
3080
eea6121a 3081 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4ad4eba5
AM
3082 goto error_free_dyn;
3083
3084 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3085 if (elfsec == -1)
3086 goto error_free_dyn;
3087 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3088
3089 for (extdyn = dynbuf;
eea6121a 3090 extdyn < dynbuf + s->size;
4ad4eba5
AM
3091 extdyn += bed->s->sizeof_dyn)
3092 {
3093 Elf_Internal_Dyn dyn;
3094
3095 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3096 if (dyn.d_tag == DT_SONAME)
3097 {
3098 unsigned int tagv = dyn.d_un.d_val;
3099 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3100 if (soname == NULL)
3101 goto error_free_dyn;
3102 }
3103 if (dyn.d_tag == DT_NEEDED)
3104 {
3105 struct bfd_link_needed_list *n, **pn;
3106 char *fnm, *anm;
3107 unsigned int tagv = dyn.d_un.d_val;
3108
3109 amt = sizeof (struct bfd_link_needed_list);
3110 n = bfd_alloc (abfd, amt);
3111 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3112 if (n == NULL || fnm == NULL)
3113 goto error_free_dyn;
3114 amt = strlen (fnm) + 1;
3115 anm = bfd_alloc (abfd, amt);
3116 if (anm == NULL)
3117 goto error_free_dyn;
3118 memcpy (anm, fnm, amt);
3119 n->name = anm;
3120 n->by = abfd;
3121 n->next = NULL;
3122 for (pn = & hash_table->needed;
3123 *pn != NULL;
3124 pn = &(*pn)->next)
3125 ;
3126 *pn = n;
3127 }
3128 if (dyn.d_tag == DT_RUNPATH)
3129 {
3130 struct bfd_link_needed_list *n, **pn;
3131 char *fnm, *anm;
3132 unsigned int tagv = dyn.d_un.d_val;
3133
3134 amt = sizeof (struct bfd_link_needed_list);
3135 n = bfd_alloc (abfd, amt);
3136 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3137 if (n == NULL || fnm == NULL)
3138 goto error_free_dyn;
3139 amt = strlen (fnm) + 1;
3140 anm = bfd_alloc (abfd, amt);
3141 if (anm == NULL)
3142 goto error_free_dyn;
3143 memcpy (anm, fnm, amt);
3144 n->name = anm;
3145 n->by = abfd;
3146 n->next = NULL;
3147 for (pn = & runpath;
3148 *pn != NULL;
3149 pn = &(*pn)->next)
3150 ;
3151 *pn = n;
3152 }
3153 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3154 if (!runpath && dyn.d_tag == DT_RPATH)
3155 {
3156 struct bfd_link_needed_list *n, **pn;
3157 char *fnm, *anm;
3158 unsigned int tagv = dyn.d_un.d_val;
3159
3160 amt = sizeof (struct bfd_link_needed_list);
3161 n = bfd_alloc (abfd, amt);
3162 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3163 if (n == NULL || fnm == NULL)
3164 goto error_free_dyn;
3165 amt = strlen (fnm) + 1;
3166 anm = bfd_alloc (abfd, amt);
3167 if (anm == NULL)
3168 {
3169 error_free_dyn:
3170 free (dynbuf);
3171 goto error_return;
3172 }
3173 memcpy (anm, fnm, amt);
3174 n->name = anm;
3175 n->by = abfd;
3176 n->next = NULL;
3177 for (pn = & rpath;
3178 *pn != NULL;
3179 pn = &(*pn)->next)
3180 ;
3181 *pn = n;
3182 }
3183 }
3184
3185 free (dynbuf);
3186 }
3187
3188 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3189 frees all more recently bfd_alloc'd blocks as well. */
3190 if (runpath)
3191 rpath = runpath;
3192
3193 if (rpath)
3194 {
3195 struct bfd_link_needed_list **pn;
3196 for (pn = & hash_table->runpath;
3197 *pn != NULL;
3198 pn = &(*pn)->next)
3199 ;
3200 *pn = rpath;
3201 }
3202
3203 /* We do not want to include any of the sections in a dynamic
3204 object in the output file. We hack by simply clobbering the
3205 list of sections in the BFD. This could be handled more
3206 cleanly by, say, a new section flag; the existing
3207 SEC_NEVER_LOAD flag is not the one we want, because that one
3208 still implies that the section takes up space in the output
3209 file. */
3210 bfd_section_list_clear (abfd);
3211
3212 /* If this is the first dynamic object found in the link, create
3213 the special sections required for dynamic linking. */
3214 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3215 goto error_return;
3216
3217 /* Find the name to use in a DT_NEEDED entry that refers to this
3218 object. If the object has a DT_SONAME entry, we use it.
3219 Otherwise, if the generic linker stuck something in
3220 elf_dt_name, we use that. Otherwise, we just use the file
3221 name. */
3222 if (soname == NULL || *soname == '\0')
3223 {
3224 soname = elf_dt_name (abfd);
3225 if (soname == NULL || *soname == '\0')
3226 soname = bfd_get_filename (abfd);
3227 }
3228
3229 /* Save the SONAME because sometimes the linker emulation code
3230 will need to know it. */
3231 elf_dt_name (abfd) = soname;
3232
3233 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3234 if (ret < 0)
3235 goto error_return;
3236
3237 /* If we have already included this dynamic object in the
3238 link, just ignore it. There is no reason to include a
3239 particular dynamic object more than once. */
3240 if (ret > 0)
3241 return TRUE;
3242 }
3243
3244 /* If this is a dynamic object, we always link against the .dynsym
3245 symbol table, not the .symtab symbol table. The dynamic linker
3246 will only see the .dynsym symbol table, so there is no reason to
3247 look at .symtab for a dynamic object. */
3248
3249 if (! dynamic || elf_dynsymtab (abfd) == 0)
3250 hdr = &elf_tdata (abfd)->symtab_hdr;
3251 else
3252 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3253
3254 symcount = hdr->sh_size / bed->s->sizeof_sym;
3255
3256 /* The sh_info field of the symtab header tells us where the
3257 external symbols start. We don't care about the local symbols at
3258 this point. */
3259 if (elf_bad_symtab (abfd))
3260 {
3261 extsymcount = symcount;
3262 extsymoff = 0;
3263 }
3264 else
3265 {
3266 extsymcount = symcount - hdr->sh_info;
3267 extsymoff = hdr->sh_info;
3268 }
3269
3270 sym_hash = NULL;
3271 if (extsymcount != 0)
3272 {
3273 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3274 NULL, NULL, NULL);
3275 if (isymbuf == NULL)
3276 goto error_return;
3277
3278 /* We store a pointer to the hash table entry for each external
3279 symbol. */
3280 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3281 sym_hash = bfd_alloc (abfd, amt);
3282 if (sym_hash == NULL)
3283 goto error_free_sym;
3284 elf_sym_hashes (abfd) = sym_hash;
3285 }
3286
3287 if (dynamic)
3288 {
3289 /* Read in any version definitions. */
3290 if (! _bfd_elf_slurp_version_tables (abfd))
3291 goto error_free_sym;
3292
3293 /* Read in the symbol versions, but don't bother to convert them
3294 to internal format. */
3295 if (elf_dynversym (abfd) != 0)
3296 {
3297 Elf_Internal_Shdr *versymhdr;
3298
3299 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3300 extversym = bfd_malloc (versymhdr->sh_size);
3301 if (extversym == NULL)
3302 goto error_free_sym;
3303 amt = versymhdr->sh_size;
3304 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3305 || bfd_bread (extversym, amt, abfd) != amt)
3306 goto error_free_vers;
3307 }
3308 }
3309
3310 weaks = NULL;
3311
3312 ever = extversym != NULL ? extversym + extsymoff : NULL;
3313 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3314 isym < isymend;
3315 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3316 {
3317 int bind;
3318 bfd_vma value;
3319 asection *sec;
3320 flagword flags;
3321 const char *name;
3322 struct elf_link_hash_entry *h;
3323 bfd_boolean definition;
3324 bfd_boolean size_change_ok;
3325 bfd_boolean type_change_ok;
3326 bfd_boolean new_weakdef;
3327 bfd_boolean override;
3328 unsigned int old_alignment;
3329 bfd *old_bfd;
3330
3331 override = FALSE;
3332
3333 flags = BSF_NO_FLAGS;
3334 sec = NULL;
3335 value = isym->st_value;
3336 *sym_hash = NULL;
3337
3338 bind = ELF_ST_BIND (isym->st_info);
3339 if (bind == STB_LOCAL)
3340 {
3341 /* This should be impossible, since ELF requires that all
3342 global symbols follow all local symbols, and that sh_info
3343 point to the first global symbol. Unfortunately, Irix 5
3344 screws this up. */
3345 continue;
3346 }
3347 else if (bind == STB_GLOBAL)
3348 {
3349 if (isym->st_shndx != SHN_UNDEF
3350 && isym->st_shndx != SHN_COMMON)
3351 flags = BSF_GLOBAL;
3352 }
3353 else if (bind == STB_WEAK)
3354 flags = BSF_WEAK;
3355 else
3356 {
3357 /* Leave it up to the processor backend. */
3358 }
3359
3360 if (isym->st_shndx == SHN_UNDEF)
3361 sec = bfd_und_section_ptr;
3362 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3363 {
3364 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3365 if (sec == NULL)
3366 sec = bfd_abs_section_ptr;
3367 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3368 value -= sec->vma;
3369 }
3370 else if (isym->st_shndx == SHN_ABS)
3371 sec = bfd_abs_section_ptr;
3372 else if (isym->st_shndx == SHN_COMMON)
3373 {
3374 sec = bfd_com_section_ptr;
3375 /* What ELF calls the size we call the value. What ELF
3376 calls the value we call the alignment. */
3377 value = isym->st_size;
3378 }
3379 else
3380 {
3381 /* Leave it up to the processor backend. */
3382 }
3383
3384 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3385 isym->st_name);
3386 if (name == NULL)
3387 goto error_free_vers;
3388
3389 if (isym->st_shndx == SHN_COMMON
3390 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3391 {
3392 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3393
3394 if (tcomm == NULL)
3395 {
3396 tcomm = bfd_make_section (abfd, ".tcommon");
3397 if (tcomm == NULL
3398 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
3399 | SEC_IS_COMMON
3400 | SEC_LINKER_CREATED
3401 | SEC_THREAD_LOCAL)))
3402 goto error_free_vers;
3403 }
3404 sec = tcomm;
3405 }
3406 else if (add_symbol_hook)
3407 {
3408 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3409 &value))
3410 goto error_free_vers;
3411
3412 /* The hook function sets the name to NULL if this symbol
3413 should be skipped for some reason. */
3414 if (name == NULL)
3415 continue;
3416 }
3417
3418 /* Sanity check that all possibilities were handled. */
3419 if (sec == NULL)
3420 {
3421 bfd_set_error (bfd_error_bad_value);
3422 goto error_free_vers;
3423 }
3424
3425 if (bfd_is_und_section (sec)
3426 || bfd_is_com_section (sec))
3427 definition = FALSE;
3428 else
3429 definition = TRUE;
3430
3431 size_change_ok = FALSE;
3432 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3433 old_alignment = 0;
3434 old_bfd = NULL;
3435
3436 if (is_elf_hash_table (hash_table))
3437 {
3438 Elf_Internal_Versym iver;
3439 unsigned int vernum = 0;
3440 bfd_boolean skip;
3441
3442 if (ever != NULL)
3443 {
3444 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3445 vernum = iver.vs_vers & VERSYM_VERSION;
3446
3447 /* If this is a hidden symbol, or if it is not version
3448 1, we append the version name to the symbol name.
3449 However, we do not modify a non-hidden absolute
3450 symbol, because it might be the version symbol
3451 itself. FIXME: What if it isn't? */
3452 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3453 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3454 {
3455 const char *verstr;
3456 size_t namelen, verlen, newlen;
3457 char *newname, *p;
3458
3459 if (isym->st_shndx != SHN_UNDEF)
3460 {
3461 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
3462 {
3463 (*_bfd_error_handler)
d003868e
AM
3464 (_("%B: %s: invalid version %u (max %d)"),
3465 abfd, name, vernum,
4ad4eba5
AM
3466 elf_tdata (abfd)->dynverdef_hdr.sh_info);
3467 bfd_set_error (bfd_error_bad_value);
3468 goto error_free_vers;
3469 }
3470 else if (vernum > 1)
3471 verstr =
3472 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3473 else
3474 verstr = "";
3475 }
3476 else
3477 {
3478 /* We cannot simply test for the number of
3479 entries in the VERNEED section since the
3480 numbers for the needed versions do not start
3481 at 0. */
3482 Elf_Internal_Verneed *t;
3483
3484 verstr = NULL;
3485 for (t = elf_tdata (abfd)->verref;
3486 t != NULL;
3487 t = t->vn_nextref)
3488 {
3489 Elf_Internal_Vernaux *a;
3490
3491 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3492 {
3493 if (a->vna_other == vernum)
3494 {
3495 verstr = a->vna_nodename;
3496 break;
3497 }
3498 }
3499 if (a != NULL)
3500 break;
3501 }
3502 if (verstr == NULL)
3503 {
3504 (*_bfd_error_handler)
d003868e
AM
3505 (_("%B: %s: invalid needed version %d"),
3506 abfd, name, vernum);
4ad4eba5
AM
3507 bfd_set_error (bfd_error_bad_value);
3508 goto error_free_vers;
3509 }
3510 }
3511
3512 namelen = strlen (name);
3513 verlen = strlen (verstr);
3514 newlen = namelen + verlen + 2;
3515 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3516 && isym->st_shndx != SHN_UNDEF)
3517 ++newlen;
3518
3519 newname = bfd_alloc (abfd, newlen);
3520 if (newname == NULL)
3521 goto error_free_vers;
3522 memcpy (newname, name, namelen);
3523 p = newname + namelen;
3524 *p++ = ELF_VER_CHR;
3525 /* If this is a defined non-hidden version symbol,
3526 we add another @ to the name. This indicates the
3527 default version of the symbol. */
3528 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3529 && isym->st_shndx != SHN_UNDEF)
3530 *p++ = ELF_VER_CHR;
3531 memcpy (p, verstr, verlen + 1);
3532
3533 name = newname;
3534 }
3535 }
3536
3537 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
3538 sym_hash, &skip, &override,
3539 &type_change_ok, &size_change_ok))
3540 goto error_free_vers;
3541
3542 if (skip)
3543 continue;
3544
3545 if (override)
3546 definition = FALSE;
3547
3548 h = *sym_hash;
3549 while (h->root.type == bfd_link_hash_indirect
3550 || h->root.type == bfd_link_hash_warning)
3551 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3552
3553 /* Remember the old alignment if this is a common symbol, so
3554 that we don't reduce the alignment later on. We can't
3555 check later, because _bfd_generic_link_add_one_symbol
3556 will set a default for the alignment which we want to
3557 override. We also remember the old bfd where the existing
3558 definition comes from. */
3559 switch (h->root.type)
3560 {
3561 default:
3562 break;
3563
3564 case bfd_link_hash_defined:
3565 case bfd_link_hash_defweak:
3566 old_bfd = h->root.u.def.section->owner;
3567 break;
3568
3569 case bfd_link_hash_common:
3570 old_bfd = h->root.u.c.p->section->owner;
3571 old_alignment = h->root.u.c.p->alignment_power;
3572 break;
3573 }
3574
3575 if (elf_tdata (abfd)->verdef != NULL
3576 && ! override
3577 && vernum > 1
3578 && definition)
3579 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3580 }
3581
3582 if (! (_bfd_generic_link_add_one_symbol
3583 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3584 (struct bfd_link_hash_entry **) sym_hash)))
3585 goto error_free_vers;
3586
3587 h = *sym_hash;
3588 while (h->root.type == bfd_link_hash_indirect
3589 || h->root.type == bfd_link_hash_warning)
3590 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3591 *sym_hash = h;
3592
3593 new_weakdef = FALSE;
3594 if (dynamic
3595 && definition
3596 && (flags & BSF_WEAK) != 0
3597 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3598 && is_elf_hash_table (hash_table)
3599 && h->weakdef == NULL)
3600 {
3601 /* Keep a list of all weak defined non function symbols from
3602 a dynamic object, using the weakdef field. Later in this
3603 function we will set the weakdef field to the correct
3604 value. We only put non-function symbols from dynamic
3605 objects on this list, because that happens to be the only
3606 time we need to know the normal symbol corresponding to a
3607 weak symbol, and the information is time consuming to
3608 figure out. If the weakdef field is not already NULL,
3609 then this symbol was already defined by some previous
3610 dynamic object, and we will be using that previous
3611 definition anyhow. */
3612
3613 h->weakdef = weaks;
3614 weaks = h;
3615 new_weakdef = TRUE;
3616 }
3617
3618 /* Set the alignment of a common symbol. */
3619 if (isym->st_shndx == SHN_COMMON
3620 && h->root.type == bfd_link_hash_common)
3621 {
3622 unsigned int align;
3623
3624 align = bfd_log2 (isym->st_value);
3625 if (align > old_alignment
3626 /* Permit an alignment power of zero if an alignment of one
3627 is specified and no other alignments have been specified. */
3628 || (isym->st_value == 1 && old_alignment == 0))
3629 h->root.u.c.p->alignment_power = align;
3630 else
3631 h->root.u.c.p->alignment_power = old_alignment;
3632 }
3633
3634 if (is_elf_hash_table (hash_table))
3635 {
3636 int old_flags;
3637 bfd_boolean dynsym;
3638 int new_flag;
3639
3640 /* Check the alignment when a common symbol is involved. This
3641 can change when a common symbol is overridden by a normal
3642 definition or a common symbol is ignored due to the old
3643 normal definition. We need to make sure the maximum
3644 alignment is maintained. */
3645 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3646 && h->root.type != bfd_link_hash_common)
3647 {
3648 unsigned int common_align;
3649 unsigned int normal_align;
3650 unsigned int symbol_align;
3651 bfd *normal_bfd;
3652 bfd *common_bfd;
3653
3654 symbol_align = ffs (h->root.u.def.value) - 1;
3655 if (h->root.u.def.section->owner != NULL
3656 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3657 {
3658 normal_align = h->root.u.def.section->alignment_power;
3659 if (normal_align > symbol_align)
3660 normal_align = symbol_align;
3661 }
3662 else
3663 normal_align = symbol_align;
3664
3665 if (old_alignment)
3666 {
3667 common_align = old_alignment;
3668 common_bfd = old_bfd;
3669 normal_bfd = abfd;
3670 }
3671 else
3672 {
3673 common_align = bfd_log2 (isym->st_value);
3674 common_bfd = abfd;
3675 normal_bfd = old_bfd;
3676 }
3677
3678 if (normal_align < common_align)
3679 (*_bfd_error_handler)
d003868e
AM
3680 (_("Warning: alignment %u of symbol `%s' in %B"
3681 " is smaller than %u in %B"),
3682 normal_bfd, common_bfd,
3683 1 << normal_align, name, 1 << common_align);
4ad4eba5
AM
3684 }
3685
3686 /* Remember the symbol size and type. */
3687 if (isym->st_size != 0
3688 && (definition || h->size == 0))
3689 {
3690 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3691 (*_bfd_error_handler)
d003868e
AM
3692 (_("Warning: size of symbol `%s' changed"
3693 " from %lu in %B to %lu in %B"),
3694 old_bfd, abfd,
4ad4eba5 3695 name, (unsigned long) h->size,
d003868e 3696 (unsigned long) isym->st_size);
4ad4eba5
AM
3697
3698 h->size = isym->st_size;
3699 }
3700
3701 /* If this is a common symbol, then we always want H->SIZE
3702 to be the size of the common symbol. The code just above
3703 won't fix the size if a common symbol becomes larger. We
3704 don't warn about a size change here, because that is
3705 covered by --warn-common. */
3706 if (h->root.type == bfd_link_hash_common)
3707 h->size = h->root.u.c.size;
3708
3709 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3710 && (definition || h->type == STT_NOTYPE))
3711 {
3712 if (h->type != STT_NOTYPE
3713 && h->type != ELF_ST_TYPE (isym->st_info)
3714 && ! type_change_ok)
3715 (*_bfd_error_handler)
d003868e
AM
3716 (_("Warning: type of symbol `%s' changed"
3717 " from %d to %d in %B"),
3718 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4ad4eba5
AM
3719
3720 h->type = ELF_ST_TYPE (isym->st_info);
3721 }
3722
3723 /* If st_other has a processor-specific meaning, specific
3724 code might be needed here. We never merge the visibility
3725 attribute with the one from a dynamic object. */
3726 if (bed->elf_backend_merge_symbol_attribute)
3727 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3728 dynamic);
3729
3730 if (isym->st_other != 0 && !dynamic)
3731 {
3732 unsigned char hvis, symvis, other, nvis;
3733
3734 /* Take the balance of OTHER from the definition. */
3735 other = (definition ? isym->st_other : h->other);
3736 other &= ~ ELF_ST_VISIBILITY (-1);
3737
3738 /* Combine visibilities, using the most constraining one. */
3739 hvis = ELF_ST_VISIBILITY (h->other);
3740 symvis = ELF_ST_VISIBILITY (isym->st_other);
3741 if (! hvis)
3742 nvis = symvis;
3743 else if (! symvis)
3744 nvis = hvis;
3745 else
3746 nvis = hvis < symvis ? hvis : symvis;
3747
3748 h->other = other | nvis;
3749 }
3750
3751 /* Set a flag in the hash table entry indicating the type of
3752 reference or definition we just found. Keep a count of
3753 the number of dynamic symbols we find. A dynamic symbol
3754 is one which is referenced or defined by both a regular
3755 object and a shared object. */
3756 old_flags = h->elf_link_hash_flags;
3757 dynsym = FALSE;
3758 if (! dynamic)
3759 {
3760 if (! definition)
3761 {
3762 new_flag = ELF_LINK_HASH_REF_REGULAR;
3763 if (bind != STB_WEAK)
3764 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
3765 }
3766 else
3767 new_flag = ELF_LINK_HASH_DEF_REGULAR;
3768 if (! info->executable
3769 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
3770 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
3771 dynsym = TRUE;
3772 }
3773 else
3774 {
3775 if (! definition)
3776 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
3777 else
3778 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
3779 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
3780 | ELF_LINK_HASH_REF_REGULAR)) != 0
3781 || (h->weakdef != NULL
3782 && ! new_weakdef
3783 && h->weakdef->dynindx != -1))
3784 dynsym = TRUE;
3785 }
3786
3787 h->elf_link_hash_flags |= new_flag;
3788
3789 /* Check to see if we need to add an indirect symbol for
3790 the default name. */
3791 if (definition || h->root.type == bfd_link_hash_common)
3792 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
3793 &sec, &value, &dynsym,
3794 override))
3795 goto error_free_vers;
3796
3797 if (definition && !dynamic)
3798 {
3799 char *p = strchr (name, ELF_VER_CHR);
3800 if (p != NULL && p[1] != ELF_VER_CHR)
3801 {
3802 /* Queue non-default versions so that .symver x, x@FOO
3803 aliases can be checked. */
3804 if (! nondeflt_vers)
3805 {
3806 amt = (isymend - isym + 1)
3807 * sizeof (struct elf_link_hash_entry *);
3808 nondeflt_vers = bfd_malloc (amt);
3809 }
3810 nondeflt_vers [nondeflt_vers_cnt++] = h;
3811 }
3812 }
3813
3814 if (dynsym && h->dynindx == -1)
3815 {
c152c796 3816 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5
AM
3817 goto error_free_vers;
3818 if (h->weakdef != NULL
3819 && ! new_weakdef
3820 && h->weakdef->dynindx == -1)
3821 {
c152c796 3822 if (! bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
4ad4eba5
AM
3823 goto error_free_vers;
3824 }
3825 }
3826 else if (dynsym && h->dynindx != -1)
3827 /* If the symbol already has a dynamic index, but
3828 visibility says it should not be visible, turn it into
3829 a local symbol. */
3830 switch (ELF_ST_VISIBILITY (h->other))
3831 {
3832 case STV_INTERNAL:
3833 case STV_HIDDEN:
3834 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
3835 dynsym = FALSE;
3836 break;
3837 }
3838
3839 if (!add_needed
3840 && definition
3841 && dynsym
3842 && (h->elf_link_hash_flags
3843 & ELF_LINK_HASH_REF_REGULAR) != 0)
3844 {
3845 int ret;
3846 const char *soname = elf_dt_name (abfd);
3847
3848 /* A symbol from a library loaded via DT_NEEDED of some
3849 other library is referenced by a regular object.
e56f61be
L
3850 Add a DT_NEEDED entry for it. Issue an error if
3851 --no-add-needed is used. */
3852 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
3853 {
3854 (*_bfd_error_handler)
3855 (_("%s: invalid DSO for symbol `%s' definition"),
d003868e 3856 abfd, name);
e56f61be
L
3857 bfd_set_error (bfd_error_bad_value);
3858 goto error_free_vers;
3859 }
3860
4ad4eba5
AM
3861 add_needed = TRUE;
3862 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3863 if (ret < 0)
3864 goto error_free_vers;
3865
3866 BFD_ASSERT (ret == 0);
3867 }
3868 }
3869 }
3870
3871 /* Now that all the symbols from this input file are created, handle
3872 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3873 if (nondeflt_vers != NULL)
3874 {
3875 bfd_size_type cnt, symidx;
3876
3877 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
3878 {
3879 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
3880 char *shortname, *p;
3881
3882 p = strchr (h->root.root.string, ELF_VER_CHR);
3883 if (p == NULL
3884 || (h->root.type != bfd_link_hash_defined
3885 && h->root.type != bfd_link_hash_defweak))
3886 continue;
3887
3888 amt = p - h->root.root.string;
3889 shortname = bfd_malloc (amt + 1);
3890 memcpy (shortname, h->root.root.string, amt);
3891 shortname[amt] = '\0';
3892
3893 hi = (struct elf_link_hash_entry *)
3894 bfd_link_hash_lookup (&hash_table->root, shortname,
3895 FALSE, FALSE, FALSE);
3896 if (hi != NULL
3897 && hi->root.type == h->root.type
3898 && hi->root.u.def.value == h->root.u.def.value
3899 && hi->root.u.def.section == h->root.u.def.section)
3900 {
3901 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
3902 hi->root.type = bfd_link_hash_indirect;
3903 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
3904 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
3905 sym_hash = elf_sym_hashes (abfd);
3906 if (sym_hash)
3907 for (symidx = 0; symidx < extsymcount; ++symidx)
3908 if (sym_hash[symidx] == hi)
3909 {
3910 sym_hash[symidx] = h;
3911 break;
3912 }
3913 }
3914 free (shortname);
3915 }
3916 free (nondeflt_vers);
3917 nondeflt_vers = NULL;
3918 }
3919
3920 if (extversym != NULL)
3921 {
3922 free (extversym);
3923 extversym = NULL;
3924 }
3925
3926 if (isymbuf != NULL)
3927 free (isymbuf);
3928 isymbuf = NULL;
3929
3930 /* Now set the weakdefs field correctly for all the weak defined
3931 symbols we found. The only way to do this is to search all the
3932 symbols. Since we only need the information for non functions in
3933 dynamic objects, that's the only time we actually put anything on
3934 the list WEAKS. We need this information so that if a regular
3935 object refers to a symbol defined weakly in a dynamic object, the
3936 real symbol in the dynamic object is also put in the dynamic
3937 symbols; we also must arrange for both symbols to point to the
3938 same memory location. We could handle the general case of symbol
3939 aliasing, but a general symbol alias can only be generated in
3940 assembler code, handling it correctly would be very time
3941 consuming, and other ELF linkers don't handle general aliasing
3942 either. */
3943 if (weaks != NULL)
3944 {
3945 struct elf_link_hash_entry **hpp;
3946 struct elf_link_hash_entry **hppend;
3947 struct elf_link_hash_entry **sorted_sym_hash;
3948 struct elf_link_hash_entry *h;
3949 size_t sym_count;
3950
3951 /* Since we have to search the whole symbol list for each weak
3952 defined symbol, search time for N weak defined symbols will be
3953 O(N^2). Binary search will cut it down to O(NlogN). */
3954 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3955 sorted_sym_hash = bfd_malloc (amt);
3956 if (sorted_sym_hash == NULL)
3957 goto error_return;
3958 sym_hash = sorted_sym_hash;
3959 hpp = elf_sym_hashes (abfd);
3960 hppend = hpp + extsymcount;
3961 sym_count = 0;
3962 for (; hpp < hppend; hpp++)
3963 {
3964 h = *hpp;
3965 if (h != NULL
3966 && h->root.type == bfd_link_hash_defined
3967 && h->type != STT_FUNC)
3968 {
3969 *sym_hash = h;
3970 sym_hash++;
3971 sym_count++;
3972 }
3973 }
3974
3975 qsort (sorted_sym_hash, sym_count,
3976 sizeof (struct elf_link_hash_entry *),
3977 elf_sort_symbol);
3978
3979 while (weaks != NULL)
3980 {
3981 struct elf_link_hash_entry *hlook;
3982 asection *slook;
3983 bfd_vma vlook;
3984 long ilook;
3985 size_t i, j, idx;
3986
3987 hlook = weaks;
3988 weaks = hlook->weakdef;
3989 hlook->weakdef = NULL;
3990
3991 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
3992 || hlook->root.type == bfd_link_hash_defweak
3993 || hlook->root.type == bfd_link_hash_common
3994 || hlook->root.type == bfd_link_hash_indirect);
3995 slook = hlook->root.u.def.section;
3996 vlook = hlook->root.u.def.value;
3997
3998 ilook = -1;
3999 i = 0;
4000 j = sym_count;
4001 while (i < j)
4002 {
4003 bfd_signed_vma vdiff;
4004 idx = (i + j) / 2;
4005 h = sorted_sym_hash [idx];
4006 vdiff = vlook - h->root.u.def.value;
4007 if (vdiff < 0)
4008 j = idx;
4009 else if (vdiff > 0)
4010 i = idx + 1;
4011 else
4012 {
a9b881be 4013 long sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4014 if (sdiff < 0)
4015 j = idx;
4016 else if (sdiff > 0)
4017 i = idx + 1;
4018 else
4019 {
4020 ilook = idx;
4021 break;
4022 }
4023 }
4024 }
4025
4026 /* We didn't find a value/section match. */
4027 if (ilook == -1)
4028 continue;
4029
4030 for (i = ilook; i < sym_count; i++)
4031 {
4032 h = sorted_sym_hash [i];
4033
4034 /* Stop if value or section doesn't match. */
4035 if (h->root.u.def.value != vlook
4036 || h->root.u.def.section != slook)
4037 break;
4038 else if (h != hlook)
4039 {
4040 hlook->weakdef = h;
4041
4042 /* If the weak definition is in the list of dynamic
4043 symbols, make sure the real definition is put
4044 there as well. */
4045 if (hlook->dynindx != -1 && h->dynindx == -1)
4046 {
c152c796 4047 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5
AM
4048 goto error_return;
4049 }
4050
4051 /* If the real definition is in the list of dynamic
4052 symbols, make sure the weak definition is put
4053 there as well. If we don't do this, then the
4054 dynamic loader might not merge the entries for the
4055 real definition and the weak definition. */
4056 if (h->dynindx != -1 && hlook->dynindx == -1)
4057 {
c152c796 4058 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4ad4eba5
AM
4059 goto error_return;
4060 }
4061 break;
4062 }
4063 }
4064 }
4065
4066 free (sorted_sym_hash);
4067 }
4068
85fbca6a
NC
4069 check_directives = get_elf_backend_data (abfd)->check_directives;
4070 if (check_directives)
4071 check_directives (abfd, info);
4072
4ad4eba5
AM
4073 /* If this object is the same format as the output object, and it is
4074 not a shared library, then let the backend look through the
4075 relocs.
4076
4077 This is required to build global offset table entries and to
4078 arrange for dynamic relocs. It is not required for the
4079 particular common case of linking non PIC code, even when linking
4080 against shared libraries, but unfortunately there is no way of
4081 knowing whether an object file has been compiled PIC or not.
4082 Looking through the relocs is not particularly time consuming.
4083 The problem is that we must either (1) keep the relocs in memory,
4084 which causes the linker to require additional runtime memory or
4085 (2) read the relocs twice from the input file, which wastes time.
4086 This would be a good case for using mmap.
4087
4088 I have no idea how to handle linking PIC code into a file of a
4089 different format. It probably can't be done. */
4090 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4091 if (! dynamic
4092 && is_elf_hash_table (hash_table)
4093 && hash_table->root.creator == abfd->xvec
4094 && check_relocs != NULL)
4095 {
4096 asection *o;
4097
4098 for (o = abfd->sections; o != NULL; o = o->next)
4099 {
4100 Elf_Internal_Rela *internal_relocs;
4101 bfd_boolean ok;
4102
4103 if ((o->flags & SEC_RELOC) == 0
4104 || o->reloc_count == 0
4105 || ((info->strip == strip_all || info->strip == strip_debugger)
4106 && (o->flags & SEC_DEBUGGING) != 0)
4107 || bfd_is_abs_section (o->output_section))
4108 continue;
4109
4110 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4111 info->keep_memory);
4112 if (internal_relocs == NULL)
4113 goto error_return;
4114
4115 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4116
4117 if (elf_section_data (o)->relocs != internal_relocs)
4118 free (internal_relocs);
4119
4120 if (! ok)
4121 goto error_return;
4122 }
4123 }
4124
4125 /* If this is a non-traditional link, try to optimize the handling
4126 of the .stab/.stabstr sections. */
4127 if (! dynamic
4128 && ! info->traditional_format
4129 && is_elf_hash_table (hash_table)
4130 && (info->strip != strip_all && info->strip != strip_debugger))
4131 {
4132 asection *stabstr;
4133
4134 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4135 if (stabstr != NULL)
4136 {
4137 bfd_size_type string_offset = 0;
4138 asection *stab;
4139
4140 for (stab = abfd->sections; stab; stab = stab->next)
4141 if (strncmp (".stab", stab->name, 5) == 0
4142 && (!stab->name[5] ||
4143 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4144 && (stab->flags & SEC_MERGE) == 0
4145 && !bfd_is_abs_section (stab->output_section))
4146 {
4147 struct bfd_elf_section_data *secdata;
4148
4149 secdata = elf_section_data (stab);
4150 if (! _bfd_link_section_stabs (abfd,
3722b82f 4151 &hash_table->stab_info,
4ad4eba5
AM
4152 stab, stabstr,
4153 &secdata->sec_info,
4154 &string_offset))
4155 goto error_return;
4156 if (secdata->sec_info)
4157 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4158 }
4159 }
4160 }
4161
4ad4eba5
AM
4162 if (is_elf_hash_table (hash_table))
4163 {
4164 /* Add this bfd to the loaded list. */
4165 struct elf_link_loaded_list *n;
4166
4167 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4168 if (n == NULL)
4169 goto error_return;
4170 n->abfd = abfd;
4171 n->next = hash_table->loaded;
4172 hash_table->loaded = n;
4173 }
4174
4175 return TRUE;
4176
4177 error_free_vers:
4178 if (nondeflt_vers != NULL)
4179 free (nondeflt_vers);
4180 if (extversym != NULL)
4181 free (extversym);
4182 error_free_sym:
4183 if (isymbuf != NULL)
4184 free (isymbuf);
4185 error_return:
4186 return FALSE;
4187}
4188
8387904d
AM
4189/* Return the linker hash table entry of a symbol that might be
4190 satisfied by an archive symbol. Return -1 on error. */
4191
4192struct elf_link_hash_entry *
4193_bfd_elf_archive_symbol_lookup (bfd *abfd,
4194 struct bfd_link_info *info,
4195 const char *name)
4196{
4197 struct elf_link_hash_entry *h;
4198 char *p, *copy;
4199 size_t len, first;
4200
4201 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4202 if (h != NULL)
4203 return h;
4204
4205 /* If this is a default version (the name contains @@), look up the
4206 symbol again with only one `@' as well as without the version.
4207 The effect is that references to the symbol with and without the
4208 version will be matched by the default symbol in the archive. */
4209
4210 p = strchr (name, ELF_VER_CHR);
4211 if (p == NULL || p[1] != ELF_VER_CHR)
4212 return h;
4213
4214 /* First check with only one `@'. */
4215 len = strlen (name);
4216 copy = bfd_alloc (abfd, len);
4217 if (copy == NULL)
4218 return (struct elf_link_hash_entry *) 0 - 1;
4219
4220 first = p - name + 1;
4221 memcpy (copy, name, first);
4222 memcpy (copy + first, name + first + 1, len - first);
4223
4224 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4225 if (h == NULL)
4226 {
4227 /* We also need to check references to the symbol without the
4228 version. */
4229 copy[first - 1] = '\0';
4230 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4231 FALSE, FALSE, FALSE);
4232 }
4233
4234 bfd_release (abfd, copy);
4235 return h;
4236}
4237
0ad989f9
L
4238/* Add symbols from an ELF archive file to the linker hash table. We
4239 don't use _bfd_generic_link_add_archive_symbols because of a
4240 problem which arises on UnixWare. The UnixWare libc.so is an
4241 archive which includes an entry libc.so.1 which defines a bunch of
4242 symbols. The libc.so archive also includes a number of other
4243 object files, which also define symbols, some of which are the same
4244 as those defined in libc.so.1. Correct linking requires that we
4245 consider each object file in turn, and include it if it defines any
4246 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4247 this; it looks through the list of undefined symbols, and includes
4248 any object file which defines them. When this algorithm is used on
4249 UnixWare, it winds up pulling in libc.so.1 early and defining a
4250 bunch of symbols. This means that some of the other objects in the
4251 archive are not included in the link, which is incorrect since they
4252 precede libc.so.1 in the archive.
4253
4254 Fortunately, ELF archive handling is simpler than that done by
4255 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4256 oddities. In ELF, if we find a symbol in the archive map, and the
4257 symbol is currently undefined, we know that we must pull in that
4258 object file.
4259
4260 Unfortunately, we do have to make multiple passes over the symbol
4261 table until nothing further is resolved. */
4262
4ad4eba5
AM
4263static bfd_boolean
4264elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
4265{
4266 symindex c;
4267 bfd_boolean *defined = NULL;
4268 bfd_boolean *included = NULL;
4269 carsym *symdefs;
4270 bfd_boolean loop;
4271 bfd_size_type amt;
8387904d
AM
4272 const struct elf_backend_data *bed;
4273 struct elf_link_hash_entry * (*archive_symbol_lookup)
4274 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
4275
4276 if (! bfd_has_map (abfd))
4277 {
4278 /* An empty archive is a special case. */
4279 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4280 return TRUE;
4281 bfd_set_error (bfd_error_no_armap);
4282 return FALSE;
4283 }
4284
4285 /* Keep track of all symbols we know to be already defined, and all
4286 files we know to be already included. This is to speed up the
4287 second and subsequent passes. */
4288 c = bfd_ardata (abfd)->symdef_count;
4289 if (c == 0)
4290 return TRUE;
4291 amt = c;
4292 amt *= sizeof (bfd_boolean);
4293 defined = bfd_zmalloc (amt);
4294 included = bfd_zmalloc (amt);
4295 if (defined == NULL || included == NULL)
4296 goto error_return;
4297
4298 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
4299 bed = get_elf_backend_data (abfd);
4300 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
4301
4302 do
4303 {
4304 file_ptr last;
4305 symindex i;
4306 carsym *symdef;
4307 carsym *symdefend;
4308
4309 loop = FALSE;
4310 last = -1;
4311
4312 symdef = symdefs;
4313 symdefend = symdef + c;
4314 for (i = 0; symdef < symdefend; symdef++, i++)
4315 {
4316 struct elf_link_hash_entry *h;
4317 bfd *element;
4318 struct bfd_link_hash_entry *undefs_tail;
4319 symindex mark;
4320
4321 if (defined[i] || included[i])
4322 continue;
4323 if (symdef->file_offset == last)
4324 {
4325 included[i] = TRUE;
4326 continue;
4327 }
4328
8387904d
AM
4329 h = archive_symbol_lookup (abfd, info, symdef->name);
4330 if (h == (struct elf_link_hash_entry *) 0 - 1)
4331 goto error_return;
0ad989f9
L
4332
4333 if (h == NULL)
4334 continue;
4335
4336 if (h->root.type == bfd_link_hash_common)
4337 {
4338 /* We currently have a common symbol. The archive map contains
4339 a reference to this symbol, so we may want to include it. We
4340 only want to include it however, if this archive element
4341 contains a definition of the symbol, not just another common
4342 declaration of it.
4343
4344 Unfortunately some archivers (including GNU ar) will put
4345 declarations of common symbols into their archive maps, as
4346 well as real definitions, so we cannot just go by the archive
4347 map alone. Instead we must read in the element's symbol
4348 table and check that to see what kind of symbol definition
4349 this is. */
4350 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4351 continue;
4352 }
4353 else if (h->root.type != bfd_link_hash_undefined)
4354 {
4355 if (h->root.type != bfd_link_hash_undefweak)
4356 defined[i] = TRUE;
4357 continue;
4358 }
4359
4360 /* We need to include this archive member. */
4361 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4362 if (element == NULL)
4363 goto error_return;
4364
4365 if (! bfd_check_format (element, bfd_object))
4366 goto error_return;
4367
4368 /* Doublecheck that we have not included this object
4369 already--it should be impossible, but there may be
4370 something wrong with the archive. */
4371 if (element->archive_pass != 0)
4372 {
4373 bfd_set_error (bfd_error_bad_value);
4374 goto error_return;
4375 }
4376 element->archive_pass = 1;
4377
4378 undefs_tail = info->hash->undefs_tail;
4379
4380 if (! (*info->callbacks->add_archive_element) (info, element,
4381 symdef->name))
4382 goto error_return;
4383 if (! bfd_link_add_symbols (element, info))
4384 goto error_return;
4385
4386 /* If there are any new undefined symbols, we need to make
4387 another pass through the archive in order to see whether
4388 they can be defined. FIXME: This isn't perfect, because
4389 common symbols wind up on undefs_tail and because an
4390 undefined symbol which is defined later on in this pass
4391 does not require another pass. This isn't a bug, but it
4392 does make the code less efficient than it could be. */
4393 if (undefs_tail != info->hash->undefs_tail)
4394 loop = TRUE;
4395
4396 /* Look backward to mark all symbols from this object file
4397 which we have already seen in this pass. */
4398 mark = i;
4399 do
4400 {
4401 included[mark] = TRUE;
4402 if (mark == 0)
4403 break;
4404 --mark;
4405 }
4406 while (symdefs[mark].file_offset == symdef->file_offset);
4407
4408 /* We mark subsequent symbols from this object file as we go
4409 on through the loop. */
4410 last = symdef->file_offset;
4411 }
4412 }
4413 while (loop);
4414
4415 free (defined);
4416 free (included);
4417
4418 return TRUE;
4419
4420 error_return:
4421 if (defined != NULL)
4422 free (defined);
4423 if (included != NULL)
4424 free (included);
4425 return FALSE;
4426}
4ad4eba5
AM
4427
4428/* Given an ELF BFD, add symbols to the global hash table as
4429 appropriate. */
4430
4431bfd_boolean
4432bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4433{
4434 switch (bfd_get_format (abfd))
4435 {
4436 case bfd_object:
4437 return elf_link_add_object_symbols (abfd, info);
4438 case bfd_archive:
4439 return elf_link_add_archive_symbols (abfd, info);
4440 default:
4441 bfd_set_error (bfd_error_wrong_format);
4442 return FALSE;
4443 }
4444}
5a580b3a
AM
4445\f
4446/* This function will be called though elf_link_hash_traverse to store
4447 all hash value of the exported symbols in an array. */
4448
4449static bfd_boolean
4450elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4451{
4452 unsigned long **valuep = data;
4453 const char *name;
4454 char *p;
4455 unsigned long ha;
4456 char *alc = NULL;
4457
4458 if (h->root.type == bfd_link_hash_warning)
4459 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4460
4461 /* Ignore indirect symbols. These are added by the versioning code. */
4462 if (h->dynindx == -1)
4463 return TRUE;
4464
4465 name = h->root.root.string;
4466 p = strchr (name, ELF_VER_CHR);
4467 if (p != NULL)
4468 {
4469 alc = bfd_malloc (p - name + 1);
4470 memcpy (alc, name, p - name);
4471 alc[p - name] = '\0';
4472 name = alc;
4473 }
4474
4475 /* Compute the hash value. */
4476 ha = bfd_elf_hash (name);
4477
4478 /* Store the found hash value in the array given as the argument. */
4479 *(*valuep)++ = ha;
4480
4481 /* And store it in the struct so that we can put it in the hash table
4482 later. */
4483 h->elf_hash_value = ha;
4484
4485 if (alc != NULL)
4486 free (alc);
4487
4488 return TRUE;
4489}
4490
4491/* Array used to determine the number of hash table buckets to use
4492 based on the number of symbols there are. If there are fewer than
4493 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4494 fewer than 37 we use 17 buckets, and so forth. We never use more
4495 than 32771 buckets. */
4496
4497static const size_t elf_buckets[] =
4498{
4499 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4500 16411, 32771, 0
4501};
4502
4503/* Compute bucket count for hashing table. We do not use a static set
4504 of possible tables sizes anymore. Instead we determine for all
4505 possible reasonable sizes of the table the outcome (i.e., the
4506 number of collisions etc) and choose the best solution. The
4507 weighting functions are not too simple to allow the table to grow
4508 without bounds. Instead one of the weighting factors is the size.
4509 Therefore the result is always a good payoff between few collisions
4510 (= short chain lengths) and table size. */
4511static size_t
4512compute_bucket_count (struct bfd_link_info *info)
4513{
4514 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4515 size_t best_size = 0;
4516 unsigned long int *hashcodes;
4517 unsigned long int *hashcodesp;
4518 unsigned long int i;
4519 bfd_size_type amt;
4520
4521 /* Compute the hash values for all exported symbols. At the same
4522 time store the values in an array so that we could use them for
4523 optimizations. */
4524 amt = dynsymcount;
4525 amt *= sizeof (unsigned long int);
4526 hashcodes = bfd_malloc (amt);
4527 if (hashcodes == NULL)
4528 return 0;
4529 hashcodesp = hashcodes;
4530
4531 /* Put all hash values in HASHCODES. */
4532 elf_link_hash_traverse (elf_hash_table (info),
4533 elf_collect_hash_codes, &hashcodesp);
4534
4535 /* We have a problem here. The following code to optimize the table
4536 size requires an integer type with more the 32 bits. If
4537 BFD_HOST_U_64_BIT is set we know about such a type. */
4538#ifdef BFD_HOST_U_64_BIT
4539 if (info->optimize)
4540 {
4541 unsigned long int nsyms = hashcodesp - hashcodes;
4542 size_t minsize;
4543 size_t maxsize;
4544 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4545 unsigned long int *counts ;
4546 bfd *dynobj = elf_hash_table (info)->dynobj;
4547 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4548
4549 /* Possible optimization parameters: if we have NSYMS symbols we say
4550 that the hashing table must at least have NSYMS/4 and at most
4551 2*NSYMS buckets. */
4552 minsize = nsyms / 4;
4553 if (minsize == 0)
4554 minsize = 1;
4555 best_size = maxsize = nsyms * 2;
4556
4557 /* Create array where we count the collisions in. We must use bfd_malloc
4558 since the size could be large. */
4559 amt = maxsize;
4560 amt *= sizeof (unsigned long int);
4561 counts = bfd_malloc (amt);
4562 if (counts == NULL)
4563 {
4564 free (hashcodes);
4565 return 0;
4566 }
4567
4568 /* Compute the "optimal" size for the hash table. The criteria is a
4569 minimal chain length. The minor criteria is (of course) the size
4570 of the table. */
4571 for (i = minsize; i < maxsize; ++i)
4572 {
4573 /* Walk through the array of hashcodes and count the collisions. */
4574 BFD_HOST_U_64_BIT max;
4575 unsigned long int j;
4576 unsigned long int fact;
4577
4578 memset (counts, '\0', i * sizeof (unsigned long int));
4579
4580 /* Determine how often each hash bucket is used. */
4581 for (j = 0; j < nsyms; ++j)
4582 ++counts[hashcodes[j] % i];
4583
4584 /* For the weight function we need some information about the
4585 pagesize on the target. This is information need not be 100%
4586 accurate. Since this information is not available (so far) we
4587 define it here to a reasonable default value. If it is crucial
4588 to have a better value some day simply define this value. */
4589# ifndef BFD_TARGET_PAGESIZE
4590# define BFD_TARGET_PAGESIZE (4096)
4591# endif
4592
4593 /* We in any case need 2 + NSYMS entries for the size values and
4594 the chains. */
4595 max = (2 + nsyms) * (bed->s->arch_size / 8);
4596
4597# if 1
4598 /* Variant 1: optimize for short chains. We add the squares
4599 of all the chain lengths (which favors many small chain
4600 over a few long chains). */
4601 for (j = 0; j < i; ++j)
4602 max += counts[j] * counts[j];
4603
4604 /* This adds penalties for the overall size of the table. */
4605 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4606 max *= fact * fact;
4607# else
4608 /* Variant 2: Optimize a lot more for small table. Here we
4609 also add squares of the size but we also add penalties for
4610 empty slots (the +1 term). */
4611 for (j = 0; j < i; ++j)
4612 max += (1 + counts[j]) * (1 + counts[j]);
4613
4614 /* The overall size of the table is considered, but not as
4615 strong as in variant 1, where it is squared. */
4616 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4617 max *= fact;
4618# endif
4619
4620 /* Compare with current best results. */
4621 if (max < best_chlen)
4622 {
4623 best_chlen = max;
4624 best_size = i;
4625 }
4626 }
4627
4628 free (counts);
4629 }
4630 else
4631#endif /* defined (BFD_HOST_U_64_BIT) */
4632 {
4633 /* This is the fallback solution if no 64bit type is available or if we
4634 are not supposed to spend much time on optimizations. We select the
4635 bucket count using a fixed set of numbers. */
4636 for (i = 0; elf_buckets[i] != 0; i++)
4637 {
4638 best_size = elf_buckets[i];
4639 if (dynsymcount < elf_buckets[i + 1])
4640 break;
4641 }
4642 }
4643
4644 /* Free the arrays we needed. */
4645 free (hashcodes);
4646
4647 return best_size;
4648}
4649
4650/* Set up the sizes and contents of the ELF dynamic sections. This is
4651 called by the ELF linker emulation before_allocation routine. We
4652 must set the sizes of the sections before the linker sets the
4653 addresses of the various sections. */
4654
4655bfd_boolean
4656bfd_elf_size_dynamic_sections (bfd *output_bfd,
4657 const char *soname,
4658 const char *rpath,
4659 const char *filter_shlib,
4660 const char * const *auxiliary_filters,
4661 struct bfd_link_info *info,
4662 asection **sinterpptr,
4663 struct bfd_elf_version_tree *verdefs)
4664{
4665 bfd_size_type soname_indx;
4666 bfd *dynobj;
4667 const struct elf_backend_data *bed;
4668 struct elf_assign_sym_version_info asvinfo;
4669
4670 *sinterpptr = NULL;
4671
4672 soname_indx = (bfd_size_type) -1;
4673
4674 if (!is_elf_hash_table (info->hash))
4675 return TRUE;
4676
8c37241b 4677 elf_tdata (output_bfd)->relro = info->relro;
5a580b3a
AM
4678 if (info->execstack)
4679 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4680 else if (info->noexecstack)
4681 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4682 else
4683 {
4684 bfd *inputobj;
4685 asection *notesec = NULL;
4686 int exec = 0;
4687
4688 for (inputobj = info->input_bfds;
4689 inputobj;
4690 inputobj = inputobj->link_next)
4691 {
4692 asection *s;
4693
4694 if (inputobj->flags & DYNAMIC)
4695 continue;
4696 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4697 if (s)
4698 {
4699 if (s->flags & SEC_CODE)
4700 exec = PF_X;
4701 notesec = s;
4702 }
4703 else
4704 exec = PF_X;
4705 }
4706 if (notesec)
4707 {
4708 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4709 if (exec && info->relocatable
4710 && notesec->output_section != bfd_abs_section_ptr)
4711 notesec->output_section->flags |= SEC_CODE;
4712 }
4713 }
4714
4715 /* Any syms created from now on start with -1 in
4716 got.refcount/offset and plt.refcount/offset. */
4717 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4718
4719 /* The backend may have to create some sections regardless of whether
4720 we're dynamic or not. */
4721 bed = get_elf_backend_data (output_bfd);
4722 if (bed->elf_backend_always_size_sections
4723 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4724 return FALSE;
4725
4726 dynobj = elf_hash_table (info)->dynobj;
4727
4728 /* If there were no dynamic objects in the link, there is nothing to
4729 do here. */
4730 if (dynobj == NULL)
4731 return TRUE;
4732
4733 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4734 return FALSE;
4735
4736 if (elf_hash_table (info)->dynamic_sections_created)
4737 {
4738 struct elf_info_failed eif;
4739 struct elf_link_hash_entry *h;
4740 asection *dynstr;
4741 struct bfd_elf_version_tree *t;
4742 struct bfd_elf_version_expr *d;
4743 bfd_boolean all_defined;
4744
4745 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
4746 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
4747
4748 if (soname != NULL)
4749 {
4750 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4751 soname, TRUE);
4752 if (soname_indx == (bfd_size_type) -1
4753 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
4754 return FALSE;
4755 }
4756
4757 if (info->symbolic)
4758 {
4759 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
4760 return FALSE;
4761 info->flags |= DF_SYMBOLIC;
4762 }
4763
4764 if (rpath != NULL)
4765 {
4766 bfd_size_type indx;
4767
4768 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
4769 TRUE);
4770 if (indx == (bfd_size_type) -1
4771 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
4772 return FALSE;
4773
4774 if (info->new_dtags)
4775 {
4776 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
4777 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
4778 return FALSE;
4779 }
4780 }
4781
4782 if (filter_shlib != NULL)
4783 {
4784 bfd_size_type indx;
4785
4786 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4787 filter_shlib, TRUE);
4788 if (indx == (bfd_size_type) -1
4789 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
4790 return FALSE;
4791 }
4792
4793 if (auxiliary_filters != NULL)
4794 {
4795 const char * const *p;
4796
4797 for (p = auxiliary_filters; *p != NULL; p++)
4798 {
4799 bfd_size_type indx;
4800
4801 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4802 *p, TRUE);
4803 if (indx == (bfd_size_type) -1
4804 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
4805 return FALSE;
4806 }
4807 }
4808
4809 eif.info = info;
4810 eif.verdefs = verdefs;
4811 eif.failed = FALSE;
4812
4813 /* If we are supposed to export all symbols into the dynamic symbol
4814 table (this is not the normal case), then do so. */
4815 if (info->export_dynamic)
4816 {
4817 elf_link_hash_traverse (elf_hash_table (info),
4818 _bfd_elf_export_symbol,
4819 &eif);
4820 if (eif.failed)
4821 return FALSE;
4822 }
4823
4824 /* Make all global versions with definition. */
4825 for (t = verdefs; t != NULL; t = t->next)
4826 for (d = t->globals.list; d != NULL; d = d->next)
4827 if (!d->symver && d->symbol)
4828 {
4829 const char *verstr, *name;
4830 size_t namelen, verlen, newlen;
4831 char *newname, *p;
4832 struct elf_link_hash_entry *newh;
4833
4834 name = d->symbol;
4835 namelen = strlen (name);
4836 verstr = t->name;
4837 verlen = strlen (verstr);
4838 newlen = namelen + verlen + 3;
4839
4840 newname = bfd_malloc (newlen);
4841 if (newname == NULL)
4842 return FALSE;
4843 memcpy (newname, name, namelen);
4844
4845 /* Check the hidden versioned definition. */
4846 p = newname + namelen;
4847 *p++ = ELF_VER_CHR;
4848 memcpy (p, verstr, verlen + 1);
4849 newh = elf_link_hash_lookup (elf_hash_table (info),
4850 newname, FALSE, FALSE,
4851 FALSE);
4852 if (newh == NULL
4853 || (newh->root.type != bfd_link_hash_defined
4854 && newh->root.type != bfd_link_hash_defweak))
4855 {
4856 /* Check the default versioned definition. */
4857 *p++ = ELF_VER_CHR;
4858 memcpy (p, verstr, verlen + 1);
4859 newh = elf_link_hash_lookup (elf_hash_table (info),
4860 newname, FALSE, FALSE,
4861 FALSE);
4862 }
4863 free (newname);
4864
4865 /* Mark this version if there is a definition and it is
4866 not defined in a shared object. */
4867 if (newh != NULL
4868 && ((newh->elf_link_hash_flags
4869 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
4870 && (newh->root.type == bfd_link_hash_defined
4871 || newh->root.type == bfd_link_hash_defweak))
4872 d->symver = 1;
4873 }
4874
4875 /* Attach all the symbols to their version information. */
4876 asvinfo.output_bfd = output_bfd;
4877 asvinfo.info = info;
4878 asvinfo.verdefs = verdefs;
4879 asvinfo.failed = FALSE;
4880
4881 elf_link_hash_traverse (elf_hash_table (info),
4882 _bfd_elf_link_assign_sym_version,
4883 &asvinfo);
4884 if (asvinfo.failed)
4885 return FALSE;
4886
4887 if (!info->allow_undefined_version)
4888 {
4889 /* Check if all global versions have a definition. */
4890 all_defined = TRUE;
4891 for (t = verdefs; t != NULL; t = t->next)
4892 for (d = t->globals.list; d != NULL; d = d->next)
4893 if (!d->symver && !d->script)
4894 {
4895 (*_bfd_error_handler)
4896 (_("%s: undefined version: %s"),
4897 d->pattern, t->name);
4898 all_defined = FALSE;
4899 }
4900
4901 if (!all_defined)
4902 {
4903 bfd_set_error (bfd_error_bad_value);
4904 return FALSE;
4905 }
4906 }
4907
4908 /* Find all symbols which were defined in a dynamic object and make
4909 the backend pick a reasonable value for them. */
4910 elf_link_hash_traverse (elf_hash_table (info),
4911 _bfd_elf_adjust_dynamic_symbol,
4912 &eif);
4913 if (eif.failed)
4914 return FALSE;
4915
4916 /* Add some entries to the .dynamic section. We fill in some of the
4917 values later, in elf_bfd_final_link, but we must add the entries
4918 now so that we know the final size of the .dynamic section. */
4919
4920 /* If there are initialization and/or finalization functions to
4921 call then add the corresponding DT_INIT/DT_FINI entries. */
4922 h = (info->init_function
4923 ? elf_link_hash_lookup (elf_hash_table (info),
4924 info->init_function, FALSE,
4925 FALSE, FALSE)
4926 : NULL);
4927 if (h != NULL
4928 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
4929 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
4930 {
4931 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
4932 return FALSE;
4933 }
4934 h = (info->fini_function
4935 ? elf_link_hash_lookup (elf_hash_table (info),
4936 info->fini_function, FALSE,
4937 FALSE, FALSE)
4938 : NULL);
4939 if (h != NULL
4940 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
4941 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
4942 {
4943 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
4944 return FALSE;
4945 }
4946
4947 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
4948 {
4949 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4950 if (! info->executable)
4951 {
4952 bfd *sub;
4953 asection *o;
4954
4955 for (sub = info->input_bfds; sub != NULL;
4956 sub = sub->link_next)
4957 for (o = sub->sections; o != NULL; o = o->next)
4958 if (elf_section_data (o)->this_hdr.sh_type
4959 == SHT_PREINIT_ARRAY)
4960 {
4961 (*_bfd_error_handler)
d003868e
AM
4962 (_("%B: .preinit_array section is not allowed in DSO"),
4963 sub);
5a580b3a
AM
4964 break;
4965 }
4966
4967 bfd_set_error (bfd_error_nonrepresentable_section);
4968 return FALSE;
4969 }
4970
4971 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
4972 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
4973 return FALSE;
4974 }
4975 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
4976 {
4977 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
4978 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
4979 return FALSE;
4980 }
4981 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
4982 {
4983 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
4984 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
4985 return FALSE;
4986 }
4987
4988 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
4989 /* If .dynstr is excluded from the link, we don't want any of
4990 these tags. Strictly, we should be checking each section
4991 individually; This quick check covers for the case where
4992 someone does a /DISCARD/ : { *(*) }. */
4993 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
4994 {
4995 bfd_size_type strsize;
4996
4997 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
4998 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
4999 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5000 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5001 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5002 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5003 bed->s->sizeof_sym))
5004 return FALSE;
5005 }
5006 }
5007
5008 /* The backend must work out the sizes of all the other dynamic
5009 sections. */
5010 if (bed->elf_backend_size_dynamic_sections
5011 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5012 return FALSE;
5013
5014 if (elf_hash_table (info)->dynamic_sections_created)
5015 {
5016 bfd_size_type dynsymcount;
5017 asection *s;
5018 size_t bucketcount = 0;
5019 size_t hash_entry_size;
5020 unsigned int dtagcount;
5021
5022 /* Set up the version definition section. */
5023 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5024 BFD_ASSERT (s != NULL);
5025
5026 /* We may have created additional version definitions if we are
5027 just linking a regular application. */
5028 verdefs = asvinfo.verdefs;
5029
5030 /* Skip anonymous version tag. */
5031 if (verdefs != NULL && verdefs->vernum == 0)
5032 verdefs = verdefs->next;
5033
5034 if (verdefs == NULL)
5035 _bfd_strip_section_from_output (info, s);
5036 else
5037 {
5038 unsigned int cdefs;
5039 bfd_size_type size;
5040 struct bfd_elf_version_tree *t;
5041 bfd_byte *p;
5042 Elf_Internal_Verdef def;
5043 Elf_Internal_Verdaux defaux;
5044
5045 cdefs = 0;
5046 size = 0;
5047
5048 /* Make space for the base version. */
5049 size += sizeof (Elf_External_Verdef);
5050 size += sizeof (Elf_External_Verdaux);
5051 ++cdefs;
5052
5053 for (t = verdefs; t != NULL; t = t->next)
5054 {
5055 struct bfd_elf_version_deps *n;
5056
5057 size += sizeof (Elf_External_Verdef);
5058 size += sizeof (Elf_External_Verdaux);
5059 ++cdefs;
5060
5061 for (n = t->deps; n != NULL; n = n->next)
5062 size += sizeof (Elf_External_Verdaux);
5063 }
5064
eea6121a
AM
5065 s->size = size;
5066 s->contents = bfd_alloc (output_bfd, s->size);
5067 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
5068 return FALSE;
5069
5070 /* Fill in the version definition section. */
5071
5072 p = s->contents;
5073
5074 def.vd_version = VER_DEF_CURRENT;
5075 def.vd_flags = VER_FLG_BASE;
5076 def.vd_ndx = 1;
5077 def.vd_cnt = 1;
5078 def.vd_aux = sizeof (Elf_External_Verdef);
5079 def.vd_next = (sizeof (Elf_External_Verdef)
5080 + sizeof (Elf_External_Verdaux));
5081
5082 if (soname_indx != (bfd_size_type) -1)
5083 {
5084 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5085 soname_indx);
5086 def.vd_hash = bfd_elf_hash (soname);
5087 defaux.vda_name = soname_indx;
5088 }
5089 else
5090 {
5091 const char *name;
5092 bfd_size_type indx;
5093
5094 name = basename (output_bfd->filename);
5095 def.vd_hash = bfd_elf_hash (name);
5096 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5097 name, FALSE);
5098 if (indx == (bfd_size_type) -1)
5099 return FALSE;
5100 defaux.vda_name = indx;
5101 }
5102 defaux.vda_next = 0;
5103
5104 _bfd_elf_swap_verdef_out (output_bfd, &def,
5105 (Elf_External_Verdef *) p);
5106 p += sizeof (Elf_External_Verdef);
5107 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5108 (Elf_External_Verdaux *) p);
5109 p += sizeof (Elf_External_Verdaux);
5110
5111 for (t = verdefs; t != NULL; t = t->next)
5112 {
5113 unsigned int cdeps;
5114 struct bfd_elf_version_deps *n;
5115 struct elf_link_hash_entry *h;
5116 struct bfd_link_hash_entry *bh;
5117
5118 cdeps = 0;
5119 for (n = t->deps; n != NULL; n = n->next)
5120 ++cdeps;
5121
5122 /* Add a symbol representing this version. */
5123 bh = NULL;
5124 if (! (_bfd_generic_link_add_one_symbol
5125 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5126 0, NULL, FALSE,
5127 get_elf_backend_data (dynobj)->collect, &bh)))
5128 return FALSE;
5129 h = (struct elf_link_hash_entry *) bh;
5130 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
5131 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5132 h->type = STT_OBJECT;
5133 h->verinfo.vertree = t;
5134
c152c796 5135 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
5136 return FALSE;
5137
5138 def.vd_version = VER_DEF_CURRENT;
5139 def.vd_flags = 0;
5140 if (t->globals.list == NULL
5141 && t->locals.list == NULL
5142 && ! t->used)
5143 def.vd_flags |= VER_FLG_WEAK;
5144 def.vd_ndx = t->vernum + 1;
5145 def.vd_cnt = cdeps + 1;
5146 def.vd_hash = bfd_elf_hash (t->name);
5147 def.vd_aux = sizeof (Elf_External_Verdef);
5148 def.vd_next = 0;
5149 if (t->next != NULL)
5150 def.vd_next = (sizeof (Elf_External_Verdef)
5151 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5152
5153 _bfd_elf_swap_verdef_out (output_bfd, &def,
5154 (Elf_External_Verdef *) p);
5155 p += sizeof (Elf_External_Verdef);
5156
5157 defaux.vda_name = h->dynstr_index;
5158 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5159 h->dynstr_index);
5160 defaux.vda_next = 0;
5161 if (t->deps != NULL)
5162 defaux.vda_next = sizeof (Elf_External_Verdaux);
5163 t->name_indx = defaux.vda_name;
5164
5165 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5166 (Elf_External_Verdaux *) p);
5167 p += sizeof (Elf_External_Verdaux);
5168
5169 for (n = t->deps; n != NULL; n = n->next)
5170 {
5171 if (n->version_needed == NULL)
5172 {
5173 /* This can happen if there was an error in the
5174 version script. */
5175 defaux.vda_name = 0;
5176 }
5177 else
5178 {
5179 defaux.vda_name = n->version_needed->name_indx;
5180 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5181 defaux.vda_name);
5182 }
5183 if (n->next == NULL)
5184 defaux.vda_next = 0;
5185 else
5186 defaux.vda_next = sizeof (Elf_External_Verdaux);
5187
5188 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5189 (Elf_External_Verdaux *) p);
5190 p += sizeof (Elf_External_Verdaux);
5191 }
5192 }
5193
5194 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5195 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5196 return FALSE;
5197
5198 elf_tdata (output_bfd)->cverdefs = cdefs;
5199 }
5200
5201 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5202 {
5203 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5204 return FALSE;
5205 }
5206 else if (info->flags & DF_BIND_NOW)
5207 {
5208 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5209 return FALSE;
5210 }
5211
5212 if (info->flags_1)
5213 {
5214 if (info->executable)
5215 info->flags_1 &= ~ (DF_1_INITFIRST
5216 | DF_1_NODELETE
5217 | DF_1_NOOPEN);
5218 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5219 return FALSE;
5220 }
5221
5222 /* Work out the size of the version reference section. */
5223
5224 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5225 BFD_ASSERT (s != NULL);
5226 {
5227 struct elf_find_verdep_info sinfo;
5228
5229 sinfo.output_bfd = output_bfd;
5230 sinfo.info = info;
5231 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5232 if (sinfo.vers == 0)
5233 sinfo.vers = 1;
5234 sinfo.failed = FALSE;
5235
5236 elf_link_hash_traverse (elf_hash_table (info),
5237 _bfd_elf_link_find_version_dependencies,
5238 &sinfo);
5239
5240 if (elf_tdata (output_bfd)->verref == NULL)
5241 _bfd_strip_section_from_output (info, s);
5242 else
5243 {
5244 Elf_Internal_Verneed *t;
5245 unsigned int size;
5246 unsigned int crefs;
5247 bfd_byte *p;
5248
5249 /* Build the version definition section. */
5250 size = 0;
5251 crefs = 0;
5252 for (t = elf_tdata (output_bfd)->verref;
5253 t != NULL;
5254 t = t->vn_nextref)
5255 {
5256 Elf_Internal_Vernaux *a;
5257
5258 size += sizeof (Elf_External_Verneed);
5259 ++crefs;
5260 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5261 size += sizeof (Elf_External_Vernaux);
5262 }
5263
eea6121a
AM
5264 s->size = size;
5265 s->contents = bfd_alloc (output_bfd, s->size);
5a580b3a
AM
5266 if (s->contents == NULL)
5267 return FALSE;
5268
5269 p = s->contents;
5270 for (t = elf_tdata (output_bfd)->verref;
5271 t != NULL;
5272 t = t->vn_nextref)
5273 {
5274 unsigned int caux;
5275 Elf_Internal_Vernaux *a;
5276 bfd_size_type indx;
5277
5278 caux = 0;
5279 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5280 ++caux;
5281
5282 t->vn_version = VER_NEED_CURRENT;
5283 t->vn_cnt = caux;
5284 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5285 elf_dt_name (t->vn_bfd) != NULL
5286 ? elf_dt_name (t->vn_bfd)
5287 : basename (t->vn_bfd->filename),
5288 FALSE);
5289 if (indx == (bfd_size_type) -1)
5290 return FALSE;
5291 t->vn_file = indx;
5292 t->vn_aux = sizeof (Elf_External_Verneed);
5293 if (t->vn_nextref == NULL)
5294 t->vn_next = 0;
5295 else
5296 t->vn_next = (sizeof (Elf_External_Verneed)
5297 + caux * sizeof (Elf_External_Vernaux));
5298
5299 _bfd_elf_swap_verneed_out (output_bfd, t,
5300 (Elf_External_Verneed *) p);
5301 p += sizeof (Elf_External_Verneed);
5302
5303 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5304 {
5305 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5306 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5307 a->vna_nodename, FALSE);
5308 if (indx == (bfd_size_type) -1)
5309 return FALSE;
5310 a->vna_name = indx;
5311 if (a->vna_nextptr == NULL)
5312 a->vna_next = 0;
5313 else
5314 a->vna_next = sizeof (Elf_External_Vernaux);
5315
5316 _bfd_elf_swap_vernaux_out (output_bfd, a,
5317 (Elf_External_Vernaux *) p);
5318 p += sizeof (Elf_External_Vernaux);
5319 }
5320 }
5321
5322 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5323 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5324 return FALSE;
5325
5326 elf_tdata (output_bfd)->cverrefs = crefs;
5327 }
5328 }
5329
5330 /* Assign dynsym indicies. In a shared library we generate a
5331 section symbol for each output section, which come first.
5332 Next come all of the back-end allocated local dynamic syms,
5333 followed by the rest of the global symbols. */
5334
5335 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5336
5337 /* Work out the size of the symbol version section. */
5338 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5339 BFD_ASSERT (s != NULL);
5340 if (dynsymcount == 0
5341 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
5342 {
5343 _bfd_strip_section_from_output (info, s);
5344 /* The DYNSYMCOUNT might have changed if we were going to
5345 output a dynamic symbol table entry for S. */
5346 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5347 }
5348 else
5349 {
eea6121a
AM
5350 s->size = dynsymcount * sizeof (Elf_External_Versym);
5351 s->contents = bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
5352 if (s->contents == NULL)
5353 return FALSE;
5354
5355 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5356 return FALSE;
5357 }
5358
5359 /* Set the size of the .dynsym and .hash sections. We counted
5360 the number of dynamic symbols in elf_link_add_object_symbols.
5361 We will build the contents of .dynsym and .hash when we build
5362 the final symbol table, because until then we do not know the
5363 correct value to give the symbols. We built the .dynstr
5364 section as we went along in elf_link_add_object_symbols. */
5365 s = bfd_get_section_by_name (dynobj, ".dynsym");
5366 BFD_ASSERT (s != NULL);
eea6121a
AM
5367 s->size = dynsymcount * bed->s->sizeof_sym;
5368 s->contents = bfd_alloc (output_bfd, s->size);
5369 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
5370 return FALSE;
5371
5372 if (dynsymcount != 0)
5373 {
5374 Elf_Internal_Sym isym;
5375
5376 /* The first entry in .dynsym is a dummy symbol. */
5377 isym.st_value = 0;
5378 isym.st_size = 0;
5379 isym.st_name = 0;
5380 isym.st_info = 0;
5381 isym.st_other = 0;
5382 isym.st_shndx = 0;
5383 bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0);
5384 }
5385
5386 /* Compute the size of the hashing table. As a side effect this
5387 computes the hash values for all the names we export. */
5388 bucketcount = compute_bucket_count (info);
5389
5390 s = bfd_get_section_by_name (dynobj, ".hash");
5391 BFD_ASSERT (s != NULL);
5392 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
eea6121a
AM
5393 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5394 s->contents = bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
5395 if (s->contents == NULL)
5396 return FALSE;
5397
5398 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5399 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5400 s->contents + hash_entry_size);
5401
5402 elf_hash_table (info)->bucketcount = bucketcount;
5403
5404 s = bfd_get_section_by_name (dynobj, ".dynstr");
5405 BFD_ASSERT (s != NULL);
5406
4ad4eba5 5407 elf_finalize_dynstr (output_bfd, info);
5a580b3a 5408
eea6121a 5409 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
5410
5411 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5412 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5413 return FALSE;
5414 }
5415
5416 return TRUE;
5417}
c152c796
AM
5418
5419/* Final phase of ELF linker. */
5420
5421/* A structure we use to avoid passing large numbers of arguments. */
5422
5423struct elf_final_link_info
5424{
5425 /* General link information. */
5426 struct bfd_link_info *info;
5427 /* Output BFD. */
5428 bfd *output_bfd;
5429 /* Symbol string table. */
5430 struct bfd_strtab_hash *symstrtab;
5431 /* .dynsym section. */
5432 asection *dynsym_sec;
5433 /* .hash section. */
5434 asection *hash_sec;
5435 /* symbol version section (.gnu.version). */
5436 asection *symver_sec;
5437 /* Buffer large enough to hold contents of any section. */
5438 bfd_byte *contents;
5439 /* Buffer large enough to hold external relocs of any section. */
5440 void *external_relocs;
5441 /* Buffer large enough to hold internal relocs of any section. */
5442 Elf_Internal_Rela *internal_relocs;
5443 /* Buffer large enough to hold external local symbols of any input
5444 BFD. */
5445 bfd_byte *external_syms;
5446 /* And a buffer for symbol section indices. */
5447 Elf_External_Sym_Shndx *locsym_shndx;
5448 /* Buffer large enough to hold internal local symbols of any input
5449 BFD. */
5450 Elf_Internal_Sym *internal_syms;
5451 /* Array large enough to hold a symbol index for each local symbol
5452 of any input BFD. */
5453 long *indices;
5454 /* Array large enough to hold a section pointer for each local
5455 symbol of any input BFD. */
5456 asection **sections;
5457 /* Buffer to hold swapped out symbols. */
5458 bfd_byte *symbuf;
5459 /* And one for symbol section indices. */
5460 Elf_External_Sym_Shndx *symshndxbuf;
5461 /* Number of swapped out symbols in buffer. */
5462 size_t symbuf_count;
5463 /* Number of symbols which fit in symbuf. */
5464 size_t symbuf_size;
5465 /* And same for symshndxbuf. */
5466 size_t shndxbuf_size;
5467};
5468
5469/* This struct is used to pass information to elf_link_output_extsym. */
5470
5471struct elf_outext_info
5472{
5473 bfd_boolean failed;
5474 bfd_boolean localsyms;
5475 struct elf_final_link_info *finfo;
5476};
5477
5478/* When performing a relocatable link, the input relocations are
5479 preserved. But, if they reference global symbols, the indices
5480 referenced must be updated. Update all the relocations in
5481 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5482
5483static void
5484elf_link_adjust_relocs (bfd *abfd,
5485 Elf_Internal_Shdr *rel_hdr,
5486 unsigned int count,
5487 struct elf_link_hash_entry **rel_hash)
5488{
5489 unsigned int i;
5490 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5491 bfd_byte *erela;
5492 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5493 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5494 bfd_vma r_type_mask;
5495 int r_sym_shift;
5496
5497 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5498 {
5499 swap_in = bed->s->swap_reloc_in;
5500 swap_out = bed->s->swap_reloc_out;
5501 }
5502 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5503 {
5504 swap_in = bed->s->swap_reloca_in;
5505 swap_out = bed->s->swap_reloca_out;
5506 }
5507 else
5508 abort ();
5509
5510 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5511 abort ();
5512
5513 if (bed->s->arch_size == 32)
5514 {
5515 r_type_mask = 0xff;
5516 r_sym_shift = 8;
5517 }
5518 else
5519 {
5520 r_type_mask = 0xffffffff;
5521 r_sym_shift = 32;
5522 }
5523
5524 erela = rel_hdr->contents;
5525 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5526 {
5527 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5528 unsigned int j;
5529
5530 if (*rel_hash == NULL)
5531 continue;
5532
5533 BFD_ASSERT ((*rel_hash)->indx >= 0);
5534
5535 (*swap_in) (abfd, erela, irela);
5536 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5537 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5538 | (irela[j].r_info & r_type_mask));
5539 (*swap_out) (abfd, irela, erela);
5540 }
5541}
5542
5543struct elf_link_sort_rela
5544{
5545 union {
5546 bfd_vma offset;
5547 bfd_vma sym_mask;
5548 } u;
5549 enum elf_reloc_type_class type;
5550 /* We use this as an array of size int_rels_per_ext_rel. */
5551 Elf_Internal_Rela rela[1];
5552};
5553
5554static int
5555elf_link_sort_cmp1 (const void *A, const void *B)
5556{
5557 const struct elf_link_sort_rela *a = A;
5558 const struct elf_link_sort_rela *b = B;
5559 int relativea, relativeb;
5560
5561 relativea = a->type == reloc_class_relative;
5562 relativeb = b->type == reloc_class_relative;
5563
5564 if (relativea < relativeb)
5565 return 1;
5566 if (relativea > relativeb)
5567 return -1;
5568 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5569 return -1;
5570 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5571 return 1;
5572 if (a->rela->r_offset < b->rela->r_offset)
5573 return -1;
5574 if (a->rela->r_offset > b->rela->r_offset)
5575 return 1;
5576 return 0;
5577}
5578
5579static int
5580elf_link_sort_cmp2 (const void *A, const void *B)
5581{
5582 const struct elf_link_sort_rela *a = A;
5583 const struct elf_link_sort_rela *b = B;
5584 int copya, copyb;
5585
5586 if (a->u.offset < b->u.offset)
5587 return -1;
5588 if (a->u.offset > b->u.offset)
5589 return 1;
5590 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5591 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5592 if (copya < copyb)
5593 return -1;
5594 if (copya > copyb)
5595 return 1;
5596 if (a->rela->r_offset < b->rela->r_offset)
5597 return -1;
5598 if (a->rela->r_offset > b->rela->r_offset)
5599 return 1;
5600 return 0;
5601}
5602
5603static size_t
5604elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5605{
5606 asection *reldyn;
5607 bfd_size_type count, size;
5608 size_t i, ret, sort_elt, ext_size;
5609 bfd_byte *sort, *s_non_relative, *p;
5610 struct elf_link_sort_rela *sq;
5611 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5612 int i2e = bed->s->int_rels_per_ext_rel;
5613 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5614 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5615 struct bfd_link_order *lo;
5616 bfd_vma r_sym_mask;
5617
5618 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
eea6121a 5619 if (reldyn == NULL || reldyn->size == 0)
c152c796
AM
5620 {
5621 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
eea6121a 5622 if (reldyn == NULL || reldyn->size == 0)
c152c796
AM
5623 return 0;
5624 ext_size = bed->s->sizeof_rel;
5625 swap_in = bed->s->swap_reloc_in;
5626 swap_out = bed->s->swap_reloc_out;
5627 }
5628 else
5629 {
5630 ext_size = bed->s->sizeof_rela;
5631 swap_in = bed->s->swap_reloca_in;
5632 swap_out = bed->s->swap_reloca_out;
5633 }
eea6121a 5634 count = reldyn->size / ext_size;
c152c796
AM
5635
5636 size = 0;
5637 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5638 if (lo->type == bfd_indirect_link_order)
5639 {
5640 asection *o = lo->u.indirect.section;
eea6121a 5641 size += o->size;
c152c796
AM
5642 }
5643
eea6121a 5644 if (size != reldyn->size)
c152c796
AM
5645 return 0;
5646
5647 sort_elt = (sizeof (struct elf_link_sort_rela)
5648 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5649 sort = bfd_zmalloc (sort_elt * count);
5650 if (sort == NULL)
5651 {
5652 (*info->callbacks->warning)
5653 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5654 return 0;
5655 }
5656
5657 if (bed->s->arch_size == 32)
5658 r_sym_mask = ~(bfd_vma) 0xff;
5659 else
5660 r_sym_mask = ~(bfd_vma) 0xffffffff;
5661
5662 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5663 if (lo->type == bfd_indirect_link_order)
5664 {
5665 bfd_byte *erel, *erelend;
5666 asection *o = lo->u.indirect.section;
5667
5668 erel = o->contents;
eea6121a 5669 erelend = o->contents + o->size;
c152c796
AM
5670 p = sort + o->output_offset / ext_size * sort_elt;
5671 while (erel < erelend)
5672 {
5673 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5674 (*swap_in) (abfd, erel, s->rela);
5675 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
5676 s->u.sym_mask = r_sym_mask;
5677 p += sort_elt;
5678 erel += ext_size;
5679 }
5680 }
5681
5682 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
5683
5684 for (i = 0, p = sort; i < count; i++, p += sort_elt)
5685 {
5686 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5687 if (s->type != reloc_class_relative)
5688 break;
5689 }
5690 ret = i;
5691 s_non_relative = p;
5692
5693 sq = (struct elf_link_sort_rela *) s_non_relative;
5694 for (; i < count; i++, p += sort_elt)
5695 {
5696 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
5697 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
5698 sq = sp;
5699 sp->u.offset = sq->rela->r_offset;
5700 }
5701
5702 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
5703
5704 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5705 if (lo->type == bfd_indirect_link_order)
5706 {
5707 bfd_byte *erel, *erelend;
5708 asection *o = lo->u.indirect.section;
5709
5710 erel = o->contents;
eea6121a 5711 erelend = o->contents + o->size;
c152c796
AM
5712 p = sort + o->output_offset / ext_size * sort_elt;
5713 while (erel < erelend)
5714 {
5715 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5716 (*swap_out) (abfd, s->rela, erel);
5717 p += sort_elt;
5718 erel += ext_size;
5719 }
5720 }
5721
5722 free (sort);
5723 *psec = reldyn;
5724 return ret;
5725}
5726
5727/* Flush the output symbols to the file. */
5728
5729static bfd_boolean
5730elf_link_flush_output_syms (struct elf_final_link_info *finfo,
5731 const struct elf_backend_data *bed)
5732{
5733 if (finfo->symbuf_count > 0)
5734 {
5735 Elf_Internal_Shdr *hdr;
5736 file_ptr pos;
5737 bfd_size_type amt;
5738
5739 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5740 pos = hdr->sh_offset + hdr->sh_size;
5741 amt = finfo->symbuf_count * bed->s->sizeof_sym;
5742 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5743 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
5744 return FALSE;
5745
5746 hdr->sh_size += amt;
5747 finfo->symbuf_count = 0;
5748 }
5749
5750 return TRUE;
5751}
5752
5753/* Add a symbol to the output symbol table. */
5754
5755static bfd_boolean
5756elf_link_output_sym (struct elf_final_link_info *finfo,
5757 const char *name,
5758 Elf_Internal_Sym *elfsym,
5759 asection *input_sec,
5760 struct elf_link_hash_entry *h)
5761{
5762 bfd_byte *dest;
5763 Elf_External_Sym_Shndx *destshndx;
5764 bfd_boolean (*output_symbol_hook)
5765 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
5766 struct elf_link_hash_entry *);
5767 const struct elf_backend_data *bed;
5768
5769 bed = get_elf_backend_data (finfo->output_bfd);
5770 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
5771 if (output_symbol_hook != NULL)
5772 {
5773 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
5774 return FALSE;
5775 }
5776
5777 if (name == NULL || *name == '\0')
5778 elfsym->st_name = 0;
5779 else if (input_sec->flags & SEC_EXCLUDE)
5780 elfsym->st_name = 0;
5781 else
5782 {
5783 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5784 name, TRUE, FALSE);
5785 if (elfsym->st_name == (unsigned long) -1)
5786 return FALSE;
5787 }
5788
5789 if (finfo->symbuf_count >= finfo->symbuf_size)
5790 {
5791 if (! elf_link_flush_output_syms (finfo, bed))
5792 return FALSE;
5793 }
5794
5795 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
5796 destshndx = finfo->symshndxbuf;
5797 if (destshndx != NULL)
5798 {
5799 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
5800 {
5801 bfd_size_type amt;
5802
5803 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
5804 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
5805 if (destshndx == NULL)
5806 return FALSE;
5807 memset ((char *) destshndx + amt, 0, amt);
5808 finfo->shndxbuf_size *= 2;
5809 }
5810 destshndx += bfd_get_symcount (finfo->output_bfd);
5811 }
5812
5813 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
5814 finfo->symbuf_count += 1;
5815 bfd_get_symcount (finfo->output_bfd) += 1;
5816
5817 return TRUE;
5818}
5819
5820/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5821 allowing an unsatisfied unversioned symbol in the DSO to match a
5822 versioned symbol that would normally require an explicit version.
5823 We also handle the case that a DSO references a hidden symbol
5824 which may be satisfied by a versioned symbol in another DSO. */
5825
5826static bfd_boolean
5827elf_link_check_versioned_symbol (struct bfd_link_info *info,
5828 const struct elf_backend_data *bed,
5829 struct elf_link_hash_entry *h)
5830{
5831 bfd *abfd;
5832 struct elf_link_loaded_list *loaded;
5833
5834 if (!is_elf_hash_table (info->hash))
5835 return FALSE;
5836
5837 switch (h->root.type)
5838 {
5839 default:
5840 abfd = NULL;
5841 break;
5842
5843 case bfd_link_hash_undefined:
5844 case bfd_link_hash_undefweak:
5845 abfd = h->root.u.undef.abfd;
5846 if ((abfd->flags & DYNAMIC) == 0
e56f61be 5847 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
5848 return FALSE;
5849 break;
5850
5851 case bfd_link_hash_defined:
5852 case bfd_link_hash_defweak:
5853 abfd = h->root.u.def.section->owner;
5854 break;
5855
5856 case bfd_link_hash_common:
5857 abfd = h->root.u.c.p->section->owner;
5858 break;
5859 }
5860 BFD_ASSERT (abfd != NULL);
5861
5862 for (loaded = elf_hash_table (info)->loaded;
5863 loaded != NULL;
5864 loaded = loaded->next)
5865 {
5866 bfd *input;
5867 Elf_Internal_Shdr *hdr;
5868 bfd_size_type symcount;
5869 bfd_size_type extsymcount;
5870 bfd_size_type extsymoff;
5871 Elf_Internal_Shdr *versymhdr;
5872 Elf_Internal_Sym *isym;
5873 Elf_Internal_Sym *isymend;
5874 Elf_Internal_Sym *isymbuf;
5875 Elf_External_Versym *ever;
5876 Elf_External_Versym *extversym;
5877
5878 input = loaded->abfd;
5879
5880 /* We check each DSO for a possible hidden versioned definition. */
5881 if (input == abfd
5882 || (input->flags & DYNAMIC) == 0
5883 || elf_dynversym (input) == 0)
5884 continue;
5885
5886 hdr = &elf_tdata (input)->dynsymtab_hdr;
5887
5888 symcount = hdr->sh_size / bed->s->sizeof_sym;
5889 if (elf_bad_symtab (input))
5890 {
5891 extsymcount = symcount;
5892 extsymoff = 0;
5893 }
5894 else
5895 {
5896 extsymcount = symcount - hdr->sh_info;
5897 extsymoff = hdr->sh_info;
5898 }
5899
5900 if (extsymcount == 0)
5901 continue;
5902
5903 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
5904 NULL, NULL, NULL);
5905 if (isymbuf == NULL)
5906 return FALSE;
5907
5908 /* Read in any version definitions. */
5909 versymhdr = &elf_tdata (input)->dynversym_hdr;
5910 extversym = bfd_malloc (versymhdr->sh_size);
5911 if (extversym == NULL)
5912 goto error_ret;
5913
5914 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
5915 || (bfd_bread (extversym, versymhdr->sh_size, input)
5916 != versymhdr->sh_size))
5917 {
5918 free (extversym);
5919 error_ret:
5920 free (isymbuf);
5921 return FALSE;
5922 }
5923
5924 ever = extversym + extsymoff;
5925 isymend = isymbuf + extsymcount;
5926 for (isym = isymbuf; isym < isymend; isym++, ever++)
5927 {
5928 const char *name;
5929 Elf_Internal_Versym iver;
5930 unsigned short version_index;
5931
5932 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
5933 || isym->st_shndx == SHN_UNDEF)
5934 continue;
5935
5936 name = bfd_elf_string_from_elf_section (input,
5937 hdr->sh_link,
5938 isym->st_name);
5939 if (strcmp (name, h->root.root.string) != 0)
5940 continue;
5941
5942 _bfd_elf_swap_versym_in (input, ever, &iver);
5943
5944 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
5945 {
5946 /* If we have a non-hidden versioned sym, then it should
5947 have provided a definition for the undefined sym. */
5948 abort ();
5949 }
5950
5951 version_index = iver.vs_vers & VERSYM_VERSION;
5952 if (version_index == 1 || version_index == 2)
5953 {
5954 /* This is the base or first version. We can use it. */
5955 free (extversym);
5956 free (isymbuf);
5957 return TRUE;
5958 }
5959 }
5960
5961 free (extversym);
5962 free (isymbuf);
5963 }
5964
5965 return FALSE;
5966}
5967
5968/* Add an external symbol to the symbol table. This is called from
5969 the hash table traversal routine. When generating a shared object,
5970 we go through the symbol table twice. The first time we output
5971 anything that might have been forced to local scope in a version
5972 script. The second time we output the symbols that are still
5973 global symbols. */
5974
5975static bfd_boolean
5976elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
5977{
5978 struct elf_outext_info *eoinfo = data;
5979 struct elf_final_link_info *finfo = eoinfo->finfo;
5980 bfd_boolean strip;
5981 Elf_Internal_Sym sym;
5982 asection *input_sec;
5983 const struct elf_backend_data *bed;
5984
5985 if (h->root.type == bfd_link_hash_warning)
5986 {
5987 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5988 if (h->root.type == bfd_link_hash_new)
5989 return TRUE;
5990 }
5991
5992 /* Decide whether to output this symbol in this pass. */
5993 if (eoinfo->localsyms)
5994 {
5995 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5996 return TRUE;
5997 }
5998 else
5999 {
6000 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6001 return TRUE;
6002 }
6003
6004 bed = get_elf_backend_data (finfo->output_bfd);
6005
6006 /* If we have an undefined symbol reference here then it must have
6007 come from a shared library that is being linked in. (Undefined
6008 references in regular files have already been handled). If we
6009 are reporting errors for this situation then do so now. */
6010 if (h->root.type == bfd_link_hash_undefined
6011 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
6012 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
6013 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6014 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6015 {
6016 if (! ((*finfo->info->callbacks->undefined_symbol)
6017 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6018 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6019 {
6020 eoinfo->failed = TRUE;
6021 return FALSE;
6022 }
6023 }
6024
6025 /* We should also warn if a forced local symbol is referenced from
6026 shared libraries. */
6027 if (! finfo->info->relocatable
6028 && (! finfo->info->shared)
6029 && (h->elf_link_hash_flags
6030 & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK))
6031 == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)
6032 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6033 {
6034 (*_bfd_error_handler)
d003868e
AM
6035 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6036 finfo->output_bfd, h->root.u.def.section->owner,
c152c796
AM
6037 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6038 ? "internal"
6039 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
d003868e
AM
6040 ? "hidden" : "local",
6041 h->root.root.string);
c152c796
AM
6042 eoinfo->failed = TRUE;
6043 return FALSE;
6044 }
6045
6046 /* We don't want to output symbols that have never been mentioned by
6047 a regular file, or that we have been told to strip. However, if
6048 h->indx is set to -2, the symbol is used by a reloc and we must
6049 output it. */
6050 if (h->indx == -2)
6051 strip = FALSE;
6052 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6053 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
6054 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
6055 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6056 strip = TRUE;
6057 else if (finfo->info->strip == strip_all)
6058 strip = TRUE;
6059 else if (finfo->info->strip == strip_some
6060 && bfd_hash_lookup (finfo->info->keep_hash,
6061 h->root.root.string, FALSE, FALSE) == NULL)
6062 strip = TRUE;
6063 else if (finfo->info->strip_discarded
6064 && (h->root.type == bfd_link_hash_defined
6065 || h->root.type == bfd_link_hash_defweak)
6066 && elf_discarded_section (h->root.u.def.section))
6067 strip = TRUE;
6068 else
6069 strip = FALSE;
6070
6071 /* If we're stripping it, and it's not a dynamic symbol, there's
6072 nothing else to do unless it is a forced local symbol. */
6073 if (strip
6074 && h->dynindx == -1
6075 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6076 return TRUE;
6077
6078 sym.st_value = 0;
6079 sym.st_size = h->size;
6080 sym.st_other = h->other;
6081 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6082 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6083 else if (h->root.type == bfd_link_hash_undefweak
6084 || h->root.type == bfd_link_hash_defweak)
6085 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6086 else
6087 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6088
6089 switch (h->root.type)
6090 {
6091 default:
6092 case bfd_link_hash_new:
6093 case bfd_link_hash_warning:
6094 abort ();
6095 return FALSE;
6096
6097 case bfd_link_hash_undefined:
6098 case bfd_link_hash_undefweak:
6099 input_sec = bfd_und_section_ptr;
6100 sym.st_shndx = SHN_UNDEF;
6101 break;
6102
6103 case bfd_link_hash_defined:
6104 case bfd_link_hash_defweak:
6105 {
6106 input_sec = h->root.u.def.section;
6107 if (input_sec->output_section != NULL)
6108 {
6109 sym.st_shndx =
6110 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6111 input_sec->output_section);
6112 if (sym.st_shndx == SHN_BAD)
6113 {
6114 (*_bfd_error_handler)
d003868e
AM
6115 (_("%B: could not find output section %A for input section %A"),
6116 finfo->output_bfd, input_sec->output_section, input_sec);
c152c796
AM
6117 eoinfo->failed = TRUE;
6118 return FALSE;
6119 }
6120
6121 /* ELF symbols in relocatable files are section relative,
6122 but in nonrelocatable files they are virtual
6123 addresses. */
6124 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6125 if (! finfo->info->relocatable)
6126 {
6127 sym.st_value += input_sec->output_section->vma;
6128 if (h->type == STT_TLS)
6129 {
6130 /* STT_TLS symbols are relative to PT_TLS segment
6131 base. */
6132 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6133 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6134 }
6135 }
6136 }
6137 else
6138 {
6139 BFD_ASSERT (input_sec->owner == NULL
6140 || (input_sec->owner->flags & DYNAMIC) != 0);
6141 sym.st_shndx = SHN_UNDEF;
6142 input_sec = bfd_und_section_ptr;
6143 }
6144 }
6145 break;
6146
6147 case bfd_link_hash_common:
6148 input_sec = h->root.u.c.p->section;
6149 sym.st_shndx = SHN_COMMON;
6150 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6151 break;
6152
6153 case bfd_link_hash_indirect:
6154 /* These symbols are created by symbol versioning. They point
6155 to the decorated version of the name. For example, if the
6156 symbol foo@@GNU_1.2 is the default, which should be used when
6157 foo is used with no version, then we add an indirect symbol
6158 foo which points to foo@@GNU_1.2. We ignore these symbols,
6159 since the indirected symbol is already in the hash table. */
6160 return TRUE;
6161 }
6162
6163 /* Give the processor backend a chance to tweak the symbol value,
6164 and also to finish up anything that needs to be done for this
6165 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6166 forced local syms when non-shared is due to a historical quirk. */
6167 if ((h->dynindx != -1
6168 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6169 && ((finfo->info->shared
6170 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6171 || h->root.type != bfd_link_hash_undefweak))
6172 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6173 && elf_hash_table (finfo->info)->dynamic_sections_created)
6174 {
6175 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6176 (finfo->output_bfd, finfo->info, h, &sym)))
6177 {
6178 eoinfo->failed = TRUE;
6179 return FALSE;
6180 }
6181 }
6182
6183 /* If we are marking the symbol as undefined, and there are no
6184 non-weak references to this symbol from a regular object, then
6185 mark the symbol as weak undefined; if there are non-weak
6186 references, mark the symbol as strong. We can't do this earlier,
6187 because it might not be marked as undefined until the
6188 finish_dynamic_symbol routine gets through with it. */
6189 if (sym.st_shndx == SHN_UNDEF
6190 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
6191 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6192 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6193 {
6194 int bindtype;
6195
6196 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6197 bindtype = STB_GLOBAL;
6198 else
6199 bindtype = STB_WEAK;
6200 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6201 }
6202
6203 /* If a non-weak symbol with non-default visibility is not defined
6204 locally, it is a fatal error. */
6205 if (! finfo->info->relocatable
6206 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6207 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6208 && h->root.type == bfd_link_hash_undefined
6209 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6210 {
6211 (*_bfd_error_handler)
d003868e
AM
6212 (_("%B: %s symbol `%s' isn't defined"),
6213 finfo->output_bfd,
6214 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6215 ? "protected"
6216 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6217 ? "internal" : "hidden",
6218 h->root.root.string);
c152c796
AM
6219 eoinfo->failed = TRUE;
6220 return FALSE;
6221 }
6222
6223 /* If this symbol should be put in the .dynsym section, then put it
6224 there now. We already know the symbol index. We also fill in
6225 the entry in the .hash section. */
6226 if (h->dynindx != -1
6227 && elf_hash_table (finfo->info)->dynamic_sections_created)
6228 {
6229 size_t bucketcount;
6230 size_t bucket;
6231 size_t hash_entry_size;
6232 bfd_byte *bucketpos;
6233 bfd_vma chain;
6234 bfd_byte *esym;
6235
6236 sym.st_name = h->dynstr_index;
6237 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6238 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6239
6240 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6241 bucket = h->elf_hash_value % bucketcount;
6242 hash_entry_size
6243 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6244 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6245 + (bucket + 2) * hash_entry_size);
6246 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6247 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6248 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6249 ((bfd_byte *) finfo->hash_sec->contents
6250 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6251
6252 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6253 {
6254 Elf_Internal_Versym iversym;
6255 Elf_External_Versym *eversym;
6256
6257 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6258 {
6259 if (h->verinfo.verdef == NULL)
6260 iversym.vs_vers = 0;
6261 else
6262 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6263 }
6264 else
6265 {
6266 if (h->verinfo.vertree == NULL)
6267 iversym.vs_vers = 1;
6268 else
6269 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6270 }
6271
6272 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6273 iversym.vs_vers |= VERSYM_HIDDEN;
6274
6275 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6276 eversym += h->dynindx;
6277 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6278 }
6279 }
6280
6281 /* If we're stripping it, then it was just a dynamic symbol, and
6282 there's nothing else to do. */
6283 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6284 return TRUE;
6285
6286 h->indx = bfd_get_symcount (finfo->output_bfd);
6287
6288 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6289 {
6290 eoinfo->failed = TRUE;
6291 return FALSE;
6292 }
6293
6294 return TRUE;
6295}
6296
cdd3575c
AM
6297/* Return TRUE if special handling is done for relocs in SEC against
6298 symbols defined in discarded sections. */
6299
c152c796
AM
6300static bfd_boolean
6301elf_section_ignore_discarded_relocs (asection *sec)
6302{
6303 const struct elf_backend_data *bed;
6304
cdd3575c
AM
6305 switch (sec->sec_info_type)
6306 {
6307 case ELF_INFO_TYPE_STABS:
6308 case ELF_INFO_TYPE_EH_FRAME:
6309 return TRUE;
6310 default:
6311 break;
6312 }
c152c796
AM
6313
6314 bed = get_elf_backend_data (sec->owner);
6315 if (bed->elf_backend_ignore_discarded_relocs != NULL
6316 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6317 return TRUE;
6318
6319 return FALSE;
6320}
6321
cdd3575c
AM
6322/* Return TRUE if we should complain about a reloc in SEC against a
6323 symbol defined in a discarded section. */
6324
6325static bfd_boolean
6326elf_section_complain_discarded (asection *sec)
6327{
6328 if (strncmp (".stab", sec->name, 5) == 0
6329 && (!sec->name[5] ||
6330 (sec->name[5] == '.' && ISDIGIT (sec->name[6]))))
6331 return FALSE;
6332
6333 if (strcmp (".eh_frame", sec->name) == 0)
6334 return FALSE;
6335
6336 if (strcmp (".gcc_except_table", sec->name) == 0)
6337 return FALSE;
6338
27b56da8
DA
6339 if (strcmp (".PARISC.unwind", sec->name) == 0)
6340 return FALSE;
327c1315
AM
6341
6342 if (strcmp (".fixup", sec->name) == 0)
6343 return FALSE;
27b56da8 6344
cdd3575c
AM
6345 return TRUE;
6346}
6347
3d7f7666
L
6348/* Find a match between a section and a member of a section group. */
6349
6350static asection *
6351match_group_member (asection *sec, asection *group)
6352{
6353 asection *first = elf_next_in_group (group);
6354 asection *s = first;
6355
6356 while (s != NULL)
6357 {
6358 if (bfd_elf_match_symbols_in_sections (s, sec))
6359 return s;
6360
6361 if (s == first)
6362 break;
6363 }
6364
6365 return NULL;
6366}
6367
c152c796
AM
6368/* Link an input file into the linker output file. This function
6369 handles all the sections and relocations of the input file at once.
6370 This is so that we only have to read the local symbols once, and
6371 don't have to keep them in memory. */
6372
6373static bfd_boolean
6374elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6375{
6376 bfd_boolean (*relocate_section)
6377 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6378 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6379 bfd *output_bfd;
6380 Elf_Internal_Shdr *symtab_hdr;
6381 size_t locsymcount;
6382 size_t extsymoff;
6383 Elf_Internal_Sym *isymbuf;
6384 Elf_Internal_Sym *isym;
6385 Elf_Internal_Sym *isymend;
6386 long *pindex;
6387 asection **ppsection;
6388 asection *o;
6389 const struct elf_backend_data *bed;
6390 bfd_boolean emit_relocs;
6391 struct elf_link_hash_entry **sym_hashes;
6392
6393 output_bfd = finfo->output_bfd;
6394 bed = get_elf_backend_data (output_bfd);
6395 relocate_section = bed->elf_backend_relocate_section;
6396
6397 /* If this is a dynamic object, we don't want to do anything here:
6398 we don't want the local symbols, and we don't want the section
6399 contents. */
6400 if ((input_bfd->flags & DYNAMIC) != 0)
6401 return TRUE;
6402
6403 emit_relocs = (finfo->info->relocatable
6404 || finfo->info->emitrelocations
6405 || bed->elf_backend_emit_relocs);
6406
6407 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6408 if (elf_bad_symtab (input_bfd))
6409 {
6410 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6411 extsymoff = 0;
6412 }
6413 else
6414 {
6415 locsymcount = symtab_hdr->sh_info;
6416 extsymoff = symtab_hdr->sh_info;
6417 }
6418
6419 /* Read the local symbols. */
6420 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6421 if (isymbuf == NULL && locsymcount != 0)
6422 {
6423 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6424 finfo->internal_syms,
6425 finfo->external_syms,
6426 finfo->locsym_shndx);
6427 if (isymbuf == NULL)
6428 return FALSE;
6429 }
6430
6431 /* Find local symbol sections and adjust values of symbols in
6432 SEC_MERGE sections. Write out those local symbols we know are
6433 going into the output file. */
6434 isymend = isymbuf + locsymcount;
6435 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6436 isym < isymend;
6437 isym++, pindex++, ppsection++)
6438 {
6439 asection *isec;
6440 const char *name;
6441 Elf_Internal_Sym osym;
6442
6443 *pindex = -1;
6444
6445 if (elf_bad_symtab (input_bfd))
6446 {
6447 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6448 {
6449 *ppsection = NULL;
6450 continue;
6451 }
6452 }
6453
6454 if (isym->st_shndx == SHN_UNDEF)
6455 isec = bfd_und_section_ptr;
6456 else if (isym->st_shndx < SHN_LORESERVE
6457 || isym->st_shndx > SHN_HIRESERVE)
6458 {
6459 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6460 if (isec
6461 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6462 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6463 isym->st_value =
6464 _bfd_merged_section_offset (output_bfd, &isec,
6465 elf_section_data (isec)->sec_info,
753731ee 6466 isym->st_value);
c152c796
AM
6467 }
6468 else if (isym->st_shndx == SHN_ABS)
6469 isec = bfd_abs_section_ptr;
6470 else if (isym->st_shndx == SHN_COMMON)
6471 isec = bfd_com_section_ptr;
6472 else
6473 {
6474 /* Who knows? */
6475 isec = NULL;
6476 }
6477
6478 *ppsection = isec;
6479
6480 /* Don't output the first, undefined, symbol. */
6481 if (ppsection == finfo->sections)
6482 continue;
6483
6484 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6485 {
6486 /* We never output section symbols. Instead, we use the
6487 section symbol of the corresponding section in the output
6488 file. */
6489 continue;
6490 }
6491
6492 /* If we are stripping all symbols, we don't want to output this
6493 one. */
6494 if (finfo->info->strip == strip_all)
6495 continue;
6496
6497 /* If we are discarding all local symbols, we don't want to
6498 output this one. If we are generating a relocatable output
6499 file, then some of the local symbols may be required by
6500 relocs; we output them below as we discover that they are
6501 needed. */
6502 if (finfo->info->discard == discard_all)
6503 continue;
6504
6505 /* If this symbol is defined in a section which we are
6506 discarding, we don't need to keep it, but note that
6507 linker_mark is only reliable for sections that have contents.
6508 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6509 as well as linker_mark. */
6510 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6511 && isec != NULL
6512 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6513 || (! finfo->info->relocatable
6514 && (isec->flags & SEC_EXCLUDE) != 0)))
6515 continue;
6516
6517 /* Get the name of the symbol. */
6518 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6519 isym->st_name);
6520 if (name == NULL)
6521 return FALSE;
6522
6523 /* See if we are discarding symbols with this name. */
6524 if ((finfo->info->strip == strip_some
6525 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6526 == NULL))
6527 || (((finfo->info->discard == discard_sec_merge
6528 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6529 || finfo->info->discard == discard_l)
6530 && bfd_is_local_label_name (input_bfd, name)))
6531 continue;
6532
6533 /* If we get here, we are going to output this symbol. */
6534
6535 osym = *isym;
6536
6537 /* Adjust the section index for the output file. */
6538 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6539 isec->output_section);
6540 if (osym.st_shndx == SHN_BAD)
6541 return FALSE;
6542
6543 *pindex = bfd_get_symcount (output_bfd);
6544
6545 /* ELF symbols in relocatable files are section relative, but
6546 in executable files they are virtual addresses. Note that
6547 this code assumes that all ELF sections have an associated
6548 BFD section with a reasonable value for output_offset; below
6549 we assume that they also have a reasonable value for
6550 output_section. Any special sections must be set up to meet
6551 these requirements. */
6552 osym.st_value += isec->output_offset;
6553 if (! finfo->info->relocatable)
6554 {
6555 osym.st_value += isec->output_section->vma;
6556 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6557 {
6558 /* STT_TLS symbols are relative to PT_TLS segment base. */
6559 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6560 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6561 }
6562 }
6563
6564 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6565 return FALSE;
6566 }
6567
6568 /* Relocate the contents of each section. */
6569 sym_hashes = elf_sym_hashes (input_bfd);
6570 for (o = input_bfd->sections; o != NULL; o = o->next)
6571 {
6572 bfd_byte *contents;
6573
6574 if (! o->linker_mark)
6575 {
6576 /* This section was omitted from the link. */
6577 continue;
6578 }
6579
6580 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 6581 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
6582 continue;
6583
6584 if ((o->flags & SEC_LINKER_CREATED) != 0)
6585 {
6586 /* Section was created by _bfd_elf_link_create_dynamic_sections
6587 or somesuch. */
6588 continue;
6589 }
6590
6591 /* Get the contents of the section. They have been cached by a
6592 relaxation routine. Note that o is a section in an input
6593 file, so the contents field will not have been set by any of
6594 the routines which work on output files. */
6595 if (elf_section_data (o)->this_hdr.contents != NULL)
6596 contents = elf_section_data (o)->this_hdr.contents;
6597 else
6598 {
eea6121a
AM
6599 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6600
c152c796 6601 contents = finfo->contents;
eea6121a 6602 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
c152c796
AM
6603 return FALSE;
6604 }
6605
6606 if ((o->flags & SEC_RELOC) != 0)
6607 {
6608 Elf_Internal_Rela *internal_relocs;
6609 bfd_vma r_type_mask;
6610 int r_sym_shift;
6611
6612 /* Get the swapped relocs. */
6613 internal_relocs
6614 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6615 finfo->internal_relocs, FALSE);
6616 if (internal_relocs == NULL
6617 && o->reloc_count > 0)
6618 return FALSE;
6619
6620 if (bed->s->arch_size == 32)
6621 {
6622 r_type_mask = 0xff;
6623 r_sym_shift = 8;
6624 }
6625 else
6626 {
6627 r_type_mask = 0xffffffff;
6628 r_sym_shift = 32;
6629 }
6630
6631 /* Run through the relocs looking for any against symbols
6632 from discarded sections and section symbols from
6633 removed link-once sections. Complain about relocs
6634 against discarded sections. Zero relocs against removed
6635 link-once sections. Preserve debug information as much
6636 as we can. */
6637 if (!elf_section_ignore_discarded_relocs (o))
6638 {
6639 Elf_Internal_Rela *rel, *relend;
cdd3575c 6640 bfd_boolean complain = elf_section_complain_discarded (o);
c152c796
AM
6641
6642 rel = internal_relocs;
6643 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6644 for ( ; rel < relend; rel++)
6645 {
6646 unsigned long r_symndx = rel->r_info >> r_sym_shift;
cdd3575c
AM
6647 asection **ps, *sec;
6648 struct elf_link_hash_entry *h = NULL;
6649 const char *sym_name;
c152c796
AM
6650
6651 if (r_symndx >= locsymcount
6652 || (elf_bad_symtab (input_bfd)
6653 && finfo->sections[r_symndx] == NULL))
6654 {
c152c796
AM
6655 h = sym_hashes[r_symndx - extsymoff];
6656 while (h->root.type == bfd_link_hash_indirect
6657 || h->root.type == bfd_link_hash_warning)
6658 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6659
cdd3575c
AM
6660 if (h->root.type != bfd_link_hash_defined
6661 && h->root.type != bfd_link_hash_defweak)
6662 continue;
6663
6664 ps = &h->root.u.def.section;
6665 sym_name = h->root.root.string;
c152c796
AM
6666 }
6667 else
6668 {
cdd3575c
AM
6669 Elf_Internal_Sym *sym = isymbuf + r_symndx;
6670 ps = &finfo->sections[r_symndx];
6671 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
6672 }
c152c796 6673
cdd3575c
AM
6674 /* Complain if the definition comes from a
6675 discarded section. */
6676 if ((sec = *ps) != NULL && elf_discarded_section (sec))
6677 {
6678 if ((o->flags & SEC_DEBUGGING) != 0)
c152c796 6679 {
cdd3575c
AM
6680 BFD_ASSERT (r_symndx != 0);
6681
6682 /* Try to preserve debug information.
6683 FIXME: This is quite broken. Modifying
6684 the symbol here means we will be changing
6685 all uses of the symbol, not just those in
6686 debug sections. The only thing that makes
6687 this half reasonable is that debug sections
6688 tend to come after other sections. Of
6689 course, that doesn't help with globals.
6690 ??? All link-once sections of the same name
6691 ought to define the same set of symbols, so
6692 it would seem that globals ought to always
6693 be defined in the kept section. */
3d7f7666 6694 if (sec->kept_section != NULL)
c152c796 6695 {
3d7f7666
L
6696 asection *member;
6697
6698 /* Check if it is a linkonce section or
6699 member of a comdat group. */
6700 if (elf_sec_group (sec) == NULL
6701 && sec->size == sec->kept_section->size)
6702 {
6703 *ps = sec->kept_section;
6704 continue;
6705 }
6706 else if (elf_sec_group (sec) != NULL
6707 && (member = match_group_member (sec, sec->kept_section))
6708 && sec->size == member->size)
6709 {
6710 *ps = member;
6711 continue;
6712 }
c152c796
AM
6713 }
6714 }
cdd3575c
AM
6715 else if (complain)
6716 {
d003868e
AM
6717 (*_bfd_error_handler)
6718 (_("`%s' referenced in section `%A' of %B: "
6719 "defined in discarded section `%A' of %B\n"),
6720 o, input_bfd, sec, sec->owner, sym_name);
cdd3575c
AM
6721 }
6722
6723 /* Remove the symbol reference from the reloc, but
6724 don't kill the reloc completely. This is so that
6725 a zero value will be written into the section,
6726 which may have non-zero contents put there by the
6727 assembler. Zero in things like an eh_frame fde
6728 pc_begin allows stack unwinders to recognize the
6729 fde as bogus. */
6730 rel->r_info &= r_type_mask;
6731 rel->r_addend = 0;
c152c796
AM
6732 }
6733 }
6734 }
6735
6736 /* Relocate the section by invoking a back end routine.
6737
6738 The back end routine is responsible for adjusting the
6739 section contents as necessary, and (if using Rela relocs
6740 and generating a relocatable output file) adjusting the
6741 reloc addend as necessary.
6742
6743 The back end routine does not have to worry about setting
6744 the reloc address or the reloc symbol index.
6745
6746 The back end routine is given a pointer to the swapped in
6747 internal symbols, and can access the hash table entries
6748 for the external symbols via elf_sym_hashes (input_bfd).
6749
6750 When generating relocatable output, the back end routine
6751 must handle STB_LOCAL/STT_SECTION symbols specially. The
6752 output symbol is going to be a section symbol
6753 corresponding to the output section, which will require
6754 the addend to be adjusted. */
6755
6756 if (! (*relocate_section) (output_bfd, finfo->info,
6757 input_bfd, o, contents,
6758 internal_relocs,
6759 isymbuf,
6760 finfo->sections))
6761 return FALSE;
6762
6763 if (emit_relocs)
6764 {
6765 Elf_Internal_Rela *irela;
6766 Elf_Internal_Rela *irelaend;
6767 bfd_vma last_offset;
6768 struct elf_link_hash_entry **rel_hash;
6769 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
6770 unsigned int next_erel;
6771 bfd_boolean (*reloc_emitter)
6772 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
6773 bfd_boolean rela_normal;
6774
6775 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6776 rela_normal = (bed->rela_normal
6777 && (input_rel_hdr->sh_entsize
6778 == bed->s->sizeof_rela));
6779
6780 /* Adjust the reloc addresses and symbol indices. */
6781
6782 irela = internal_relocs;
6783 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6784 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6785 + elf_section_data (o->output_section)->rel_count
6786 + elf_section_data (o->output_section)->rel_count2);
6787 last_offset = o->output_offset;
6788 if (!finfo->info->relocatable)
6789 last_offset += o->output_section->vma;
6790 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6791 {
6792 unsigned long r_symndx;
6793 asection *sec;
6794 Elf_Internal_Sym sym;
6795
6796 if (next_erel == bed->s->int_rels_per_ext_rel)
6797 {
6798 rel_hash++;
6799 next_erel = 0;
6800 }
6801
6802 irela->r_offset = _bfd_elf_section_offset (output_bfd,
6803 finfo->info, o,
6804 irela->r_offset);
6805 if (irela->r_offset >= (bfd_vma) -2)
6806 {
6807 /* This is a reloc for a deleted entry or somesuch.
6808 Turn it into an R_*_NONE reloc, at the same
6809 offset as the last reloc. elf_eh_frame.c and
6810 elf_bfd_discard_info rely on reloc offsets
6811 being ordered. */
6812 irela->r_offset = last_offset;
6813 irela->r_info = 0;
6814 irela->r_addend = 0;
6815 continue;
6816 }
6817
6818 irela->r_offset += o->output_offset;
6819
6820 /* Relocs in an executable have to be virtual addresses. */
6821 if (!finfo->info->relocatable)
6822 irela->r_offset += o->output_section->vma;
6823
6824 last_offset = irela->r_offset;
6825
6826 r_symndx = irela->r_info >> r_sym_shift;
6827 if (r_symndx == STN_UNDEF)
6828 continue;
6829
6830 if (r_symndx >= locsymcount
6831 || (elf_bad_symtab (input_bfd)
6832 && finfo->sections[r_symndx] == NULL))
6833 {
6834 struct elf_link_hash_entry *rh;
6835 unsigned long indx;
6836
6837 /* This is a reloc against a global symbol. We
6838 have not yet output all the local symbols, so
6839 we do not know the symbol index of any global
6840 symbol. We set the rel_hash entry for this
6841 reloc to point to the global hash table entry
6842 for this symbol. The symbol index is then
6843 set at the end of elf_bfd_final_link. */
6844 indx = r_symndx - extsymoff;
6845 rh = elf_sym_hashes (input_bfd)[indx];
6846 while (rh->root.type == bfd_link_hash_indirect
6847 || rh->root.type == bfd_link_hash_warning)
6848 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6849
6850 /* Setting the index to -2 tells
6851 elf_link_output_extsym that this symbol is
6852 used by a reloc. */
6853 BFD_ASSERT (rh->indx < 0);
6854 rh->indx = -2;
6855
6856 *rel_hash = rh;
6857
6858 continue;
6859 }
6860
6861 /* This is a reloc against a local symbol. */
6862
6863 *rel_hash = NULL;
6864 sym = isymbuf[r_symndx];
6865 sec = finfo->sections[r_symndx];
6866 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
6867 {
6868 /* I suppose the backend ought to fill in the
6869 section of any STT_SECTION symbol against a
6a8d1586
AM
6870 processor specific section. */
6871 r_symndx = 0;
6872 if (bfd_is_abs_section (sec))
6873 ;
c152c796
AM
6874 else if (sec == NULL || sec->owner == NULL)
6875 {
6876 bfd_set_error (bfd_error_bad_value);
6877 return FALSE;
6878 }
6879 else
6880 {
6a8d1586
AM
6881 asection *osec = sec->output_section;
6882
6883 /* If we have discarded a section, the output
6884 section will be the absolute section. In
6885 case of discarded link-once and discarded
6886 SEC_MERGE sections, use the kept section. */
6887 if (bfd_is_abs_section (osec)
6888 && sec->kept_section != NULL
6889 && sec->kept_section->output_section != NULL)
6890 {
6891 osec = sec->kept_section->output_section;
6892 irela->r_addend -= osec->vma;
6893 }
6894
6895 if (!bfd_is_abs_section (osec))
6896 {
6897 r_symndx = osec->target_index;
6898 BFD_ASSERT (r_symndx != 0);
6899 }
c152c796
AM
6900 }
6901
6902 /* Adjust the addend according to where the
6903 section winds up in the output section. */
6904 if (rela_normal)
6905 irela->r_addend += sec->output_offset;
6906 }
6907 else
6908 {
6909 if (finfo->indices[r_symndx] == -1)
6910 {
6911 unsigned long shlink;
6912 const char *name;
6913 asection *osec;
6914
6915 if (finfo->info->strip == strip_all)
6916 {
6917 /* You can't do ld -r -s. */
6918 bfd_set_error (bfd_error_invalid_operation);
6919 return FALSE;
6920 }
6921
6922 /* This symbol was skipped earlier, but
6923 since it is needed by a reloc, we
6924 must output it now. */
6925 shlink = symtab_hdr->sh_link;
6926 name = (bfd_elf_string_from_elf_section
6927 (input_bfd, shlink, sym.st_name));
6928 if (name == NULL)
6929 return FALSE;
6930
6931 osec = sec->output_section;
6932 sym.st_shndx =
6933 _bfd_elf_section_from_bfd_section (output_bfd,
6934 osec);
6935 if (sym.st_shndx == SHN_BAD)
6936 return FALSE;
6937
6938 sym.st_value += sec->output_offset;
6939 if (! finfo->info->relocatable)
6940 {
6941 sym.st_value += osec->vma;
6942 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
6943 {
6944 /* STT_TLS symbols are relative to PT_TLS
6945 segment base. */
6946 BFD_ASSERT (elf_hash_table (finfo->info)
6947 ->tls_sec != NULL);
6948 sym.st_value -= (elf_hash_table (finfo->info)
6949 ->tls_sec->vma);
6950 }
6951 }
6952
6953 finfo->indices[r_symndx]
6954 = bfd_get_symcount (output_bfd);
6955
6956 if (! elf_link_output_sym (finfo, name, &sym, sec,
6957 NULL))
6958 return FALSE;
6959 }
6960
6961 r_symndx = finfo->indices[r_symndx];
6962 }
6963
6964 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
6965 | (irela->r_info & r_type_mask));
6966 }
6967
6968 /* Swap out the relocs. */
6969 if (bed->elf_backend_emit_relocs
6970 && !(finfo->info->relocatable
6971 || finfo->info->emitrelocations))
6972 reloc_emitter = bed->elf_backend_emit_relocs;
6973 else
6974 reloc_emitter = _bfd_elf_link_output_relocs;
6975
6976 if (input_rel_hdr->sh_size != 0
6977 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
6978 internal_relocs))
6979 return FALSE;
6980
6981 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
6982 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
6983 {
6984 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
6985 * bed->s->int_rels_per_ext_rel);
6986 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
6987 internal_relocs))
6988 return FALSE;
6989 }
6990 }
6991 }
6992
6993 /* Write out the modified section contents. */
6994 if (bed->elf_backend_write_section
6995 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
6996 {
6997 /* Section written out. */
6998 }
6999 else switch (o->sec_info_type)
7000 {
7001 case ELF_INFO_TYPE_STABS:
7002 if (! (_bfd_write_section_stabs
7003 (output_bfd,
7004 &elf_hash_table (finfo->info)->stab_info,
7005 o, &elf_section_data (o)->sec_info, contents)))
7006 return FALSE;
7007 break;
7008 case ELF_INFO_TYPE_MERGE:
7009 if (! _bfd_write_merged_section (output_bfd, o,
7010 elf_section_data (o)->sec_info))
7011 return FALSE;
7012 break;
7013 case ELF_INFO_TYPE_EH_FRAME:
7014 {
7015 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7016 o, contents))
7017 return FALSE;
7018 }
7019 break;
7020 default:
7021 {
c152c796
AM
7022 if (! (o->flags & SEC_EXCLUDE)
7023 && ! bfd_set_section_contents (output_bfd, o->output_section,
7024 contents,
7025 (file_ptr) o->output_offset,
eea6121a 7026 o->size))
c152c796
AM
7027 return FALSE;
7028 }
7029 break;
7030 }
7031 }
7032
7033 return TRUE;
7034}
7035
7036/* Generate a reloc when linking an ELF file. This is a reloc
7037 requested by the linker, and does come from any input file. This
7038 is used to build constructor and destructor tables when linking
7039 with -Ur. */
7040
7041static bfd_boolean
7042elf_reloc_link_order (bfd *output_bfd,
7043 struct bfd_link_info *info,
7044 asection *output_section,
7045 struct bfd_link_order *link_order)
7046{
7047 reloc_howto_type *howto;
7048 long indx;
7049 bfd_vma offset;
7050 bfd_vma addend;
7051 struct elf_link_hash_entry **rel_hash_ptr;
7052 Elf_Internal_Shdr *rel_hdr;
7053 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7054 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7055 bfd_byte *erel;
7056 unsigned int i;
7057
7058 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7059 if (howto == NULL)
7060 {
7061 bfd_set_error (bfd_error_bad_value);
7062 return FALSE;
7063 }
7064
7065 addend = link_order->u.reloc.p->addend;
7066
7067 /* Figure out the symbol index. */
7068 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7069 + elf_section_data (output_section)->rel_count
7070 + elf_section_data (output_section)->rel_count2);
7071 if (link_order->type == bfd_section_reloc_link_order)
7072 {
7073 indx = link_order->u.reloc.p->u.section->target_index;
7074 BFD_ASSERT (indx != 0);
7075 *rel_hash_ptr = NULL;
7076 }
7077 else
7078 {
7079 struct elf_link_hash_entry *h;
7080
7081 /* Treat a reloc against a defined symbol as though it were
7082 actually against the section. */
7083 h = ((struct elf_link_hash_entry *)
7084 bfd_wrapped_link_hash_lookup (output_bfd, info,
7085 link_order->u.reloc.p->u.name,
7086 FALSE, FALSE, TRUE));
7087 if (h != NULL
7088 && (h->root.type == bfd_link_hash_defined
7089 || h->root.type == bfd_link_hash_defweak))
7090 {
7091 asection *section;
7092
7093 section = h->root.u.def.section;
7094 indx = section->output_section->target_index;
7095 *rel_hash_ptr = NULL;
7096 /* It seems that we ought to add the symbol value to the
7097 addend here, but in practice it has already been added
7098 because it was passed to constructor_callback. */
7099 addend += section->output_section->vma + section->output_offset;
7100 }
7101 else if (h != NULL)
7102 {
7103 /* Setting the index to -2 tells elf_link_output_extsym that
7104 this symbol is used by a reloc. */
7105 h->indx = -2;
7106 *rel_hash_ptr = h;
7107 indx = 0;
7108 }
7109 else
7110 {
7111 if (! ((*info->callbacks->unattached_reloc)
7112 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7113 return FALSE;
7114 indx = 0;
7115 }
7116 }
7117
7118 /* If this is an inplace reloc, we must write the addend into the
7119 object file. */
7120 if (howto->partial_inplace && addend != 0)
7121 {
7122 bfd_size_type size;
7123 bfd_reloc_status_type rstat;
7124 bfd_byte *buf;
7125 bfd_boolean ok;
7126 const char *sym_name;
7127
7128 size = bfd_get_reloc_size (howto);
7129 buf = bfd_zmalloc (size);
7130 if (buf == NULL)
7131 return FALSE;
7132 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7133 switch (rstat)
7134 {
7135 case bfd_reloc_ok:
7136 break;
7137
7138 default:
7139 case bfd_reloc_outofrange:
7140 abort ();
7141
7142 case bfd_reloc_overflow:
7143 if (link_order->type == bfd_section_reloc_link_order)
7144 sym_name = bfd_section_name (output_bfd,
7145 link_order->u.reloc.p->u.section);
7146 else
7147 sym_name = link_order->u.reloc.p->u.name;
7148 if (! ((*info->callbacks->reloc_overflow)
7149 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
7150 {
7151 free (buf);
7152 return FALSE;
7153 }
7154 break;
7155 }
7156 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7157 link_order->offset, size);
7158 free (buf);
7159 if (! ok)
7160 return FALSE;
7161 }
7162
7163 /* The address of a reloc is relative to the section in a
7164 relocatable file, and is a virtual address in an executable
7165 file. */
7166 offset = link_order->offset;
7167 if (! info->relocatable)
7168 offset += output_section->vma;
7169
7170 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7171 {
7172 irel[i].r_offset = offset;
7173 irel[i].r_info = 0;
7174 irel[i].r_addend = 0;
7175 }
7176 if (bed->s->arch_size == 32)
7177 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7178 else
7179 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7180
7181 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7182 erel = rel_hdr->contents;
7183 if (rel_hdr->sh_type == SHT_REL)
7184 {
7185 erel += (elf_section_data (output_section)->rel_count
7186 * bed->s->sizeof_rel);
7187 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7188 }
7189 else
7190 {
7191 irel[0].r_addend = addend;
7192 erel += (elf_section_data (output_section)->rel_count
7193 * bed->s->sizeof_rela);
7194 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7195 }
7196
7197 ++elf_section_data (output_section)->rel_count;
7198
7199 return TRUE;
7200}
7201
0b52efa6
PB
7202
7203/* Get the output vma of the section pointed to by the sh_link field. */
7204
7205static bfd_vma
7206elf_get_linked_section_vma (struct bfd_link_order *p)
7207{
7208 Elf_Internal_Shdr **elf_shdrp;
7209 asection *s;
7210 int elfsec;
7211
7212 s = p->u.indirect.section;
7213 elf_shdrp = elf_elfsections (s->owner);
7214 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7215 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
7216 /* PR 290:
7217 The Intel C compiler generates SHT_IA_64_UNWIND with
7218 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7219 sh_info fields. Hence we could get the situation
7220 where elfsec is 0. */
7221 if (elfsec == 0)
7222 {
7223 const struct elf_backend_data *bed
7224 = get_elf_backend_data (s->owner);
7225 if (bed->link_order_error_handler)
d003868e
AM
7226 bed->link_order_error_handler
7227 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
7228 return 0;
7229 }
7230 else
7231 {
7232 s = elf_shdrp[elfsec]->bfd_section;
7233 return s->output_section->vma + s->output_offset;
7234 }
0b52efa6
PB
7235}
7236
7237
7238/* Compare two sections based on the locations of the sections they are
7239 linked to. Used by elf_fixup_link_order. */
7240
7241static int
7242compare_link_order (const void * a, const void * b)
7243{
7244 bfd_vma apos;
7245 bfd_vma bpos;
7246
7247 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7248 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7249 if (apos < bpos)
7250 return -1;
7251 return apos > bpos;
7252}
7253
7254
7255/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7256 order as their linked sections. Returns false if this could not be done
7257 because an output section includes both ordered and unordered
7258 sections. Ideally we'd do this in the linker proper. */
7259
7260static bfd_boolean
7261elf_fixup_link_order (bfd *abfd, asection *o)
7262{
7263 int seen_linkorder;
7264 int seen_other;
7265 int n;
7266 struct bfd_link_order *p;
7267 bfd *sub;
7268 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7269 int elfsec;
7270 struct bfd_link_order **sections;
7271 asection *s;
7272 bfd_vma offset;
7273
7274 seen_other = 0;
7275 seen_linkorder = 0;
7276 for (p = o->link_order_head; p != NULL; p = p->next)
7277 {
7278 if (p->type == bfd_indirect_link_order
7279 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7280 == bfd_target_elf_flavour)
7281 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7282 {
7283 s = p->u.indirect.section;
7284 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7285 if (elfsec != -1
7286 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7287 seen_linkorder++;
7288 else
7289 seen_other++;
7290 }
7291 else
7292 seen_other++;
7293 }
7294
7295 if (!seen_linkorder)
7296 return TRUE;
7297
7298 if (seen_other && seen_linkorder)
08ccf96b 7299 {
d003868e
AM
7300 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7301 o);
08ccf96b
L
7302 bfd_set_error (bfd_error_bad_value);
7303 return FALSE;
7304 }
0b52efa6
PB
7305
7306 sections = (struct bfd_link_order **)
7307 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7308 seen_linkorder = 0;
7309
7310 for (p = o->link_order_head; p != NULL; p = p->next)
7311 {
7312 sections[seen_linkorder++] = p;
7313 }
7314 /* Sort the input sections in the order of their linked section. */
7315 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7316 compare_link_order);
7317
7318 /* Change the offsets of the sections. */
7319 offset = 0;
7320 for (n = 0; n < seen_linkorder; n++)
7321 {
7322 s = sections[n]->u.indirect.section;
7323 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7324 s->output_offset = offset;
7325 sections[n]->offset = offset;
7326 offset += sections[n]->size;
7327 }
7328
7329 return TRUE;
7330}
7331
7332
c152c796
AM
7333/* Do the final step of an ELF link. */
7334
7335bfd_boolean
7336bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7337{
7338 bfd_boolean dynamic;
7339 bfd_boolean emit_relocs;
7340 bfd *dynobj;
7341 struct elf_final_link_info finfo;
7342 register asection *o;
7343 register struct bfd_link_order *p;
7344 register bfd *sub;
7345 bfd_size_type max_contents_size;
7346 bfd_size_type max_external_reloc_size;
7347 bfd_size_type max_internal_reloc_count;
7348 bfd_size_type max_sym_count;
7349 bfd_size_type max_sym_shndx_count;
7350 file_ptr off;
7351 Elf_Internal_Sym elfsym;
7352 unsigned int i;
7353 Elf_Internal_Shdr *symtab_hdr;
7354 Elf_Internal_Shdr *symtab_shndx_hdr;
7355 Elf_Internal_Shdr *symstrtab_hdr;
7356 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7357 struct elf_outext_info eoinfo;
7358 bfd_boolean merged;
7359 size_t relativecount = 0;
7360 asection *reldyn = 0;
7361 bfd_size_type amt;
7362
7363 if (! is_elf_hash_table (info->hash))
7364 return FALSE;
7365
7366 if (info->shared)
7367 abfd->flags |= DYNAMIC;
7368
7369 dynamic = elf_hash_table (info)->dynamic_sections_created;
7370 dynobj = elf_hash_table (info)->dynobj;
7371
7372 emit_relocs = (info->relocatable
7373 || info->emitrelocations
7374 || bed->elf_backend_emit_relocs);
7375
7376 finfo.info = info;
7377 finfo.output_bfd = abfd;
7378 finfo.symstrtab = _bfd_elf_stringtab_init ();
7379 if (finfo.symstrtab == NULL)
7380 return FALSE;
7381
7382 if (! dynamic)
7383 {
7384 finfo.dynsym_sec = NULL;
7385 finfo.hash_sec = NULL;
7386 finfo.symver_sec = NULL;
7387 }
7388 else
7389 {
7390 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7391 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7392 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7393 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7394 /* Note that it is OK if symver_sec is NULL. */
7395 }
7396
7397 finfo.contents = NULL;
7398 finfo.external_relocs = NULL;
7399 finfo.internal_relocs = NULL;
7400 finfo.external_syms = NULL;
7401 finfo.locsym_shndx = NULL;
7402 finfo.internal_syms = NULL;
7403 finfo.indices = NULL;
7404 finfo.sections = NULL;
7405 finfo.symbuf = NULL;
7406 finfo.symshndxbuf = NULL;
7407 finfo.symbuf_count = 0;
7408 finfo.shndxbuf_size = 0;
7409
7410 /* Count up the number of relocations we will output for each output
7411 section, so that we know the sizes of the reloc sections. We
7412 also figure out some maximum sizes. */
7413 max_contents_size = 0;
7414 max_external_reloc_size = 0;
7415 max_internal_reloc_count = 0;
7416 max_sym_count = 0;
7417 max_sym_shndx_count = 0;
7418 merged = FALSE;
7419 for (o = abfd->sections; o != NULL; o = o->next)
7420 {
7421 struct bfd_elf_section_data *esdo = elf_section_data (o);
7422 o->reloc_count = 0;
7423
7424 for (p = o->link_order_head; p != NULL; p = p->next)
7425 {
7426 unsigned int reloc_count = 0;
7427 struct bfd_elf_section_data *esdi = NULL;
7428 unsigned int *rel_count1;
7429
7430 if (p->type == bfd_section_reloc_link_order
7431 || p->type == bfd_symbol_reloc_link_order)
7432 reloc_count = 1;
7433 else if (p->type == bfd_indirect_link_order)
7434 {
7435 asection *sec;
7436
7437 sec = p->u.indirect.section;
7438 esdi = elf_section_data (sec);
7439
7440 /* Mark all sections which are to be included in the
7441 link. This will normally be every section. We need
7442 to do this so that we can identify any sections which
7443 the linker has decided to not include. */
7444 sec->linker_mark = TRUE;
7445
7446 if (sec->flags & SEC_MERGE)
7447 merged = TRUE;
7448
7449 if (info->relocatable || info->emitrelocations)
7450 reloc_count = sec->reloc_count;
7451 else if (bed->elf_backend_count_relocs)
7452 {
7453 Elf_Internal_Rela * relocs;
7454
7455 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7456 info->keep_memory);
7457
7458 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7459
7460 if (elf_section_data (o)->relocs != relocs)
7461 free (relocs);
7462 }
7463
eea6121a
AM
7464 if (sec->rawsize > max_contents_size)
7465 max_contents_size = sec->rawsize;
7466 if (sec->size > max_contents_size)
7467 max_contents_size = sec->size;
c152c796
AM
7468
7469 /* We are interested in just local symbols, not all
7470 symbols. */
7471 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7472 && (sec->owner->flags & DYNAMIC) == 0)
7473 {
7474 size_t sym_count;
7475
7476 if (elf_bad_symtab (sec->owner))
7477 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7478 / bed->s->sizeof_sym);
7479 else
7480 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7481
7482 if (sym_count > max_sym_count)
7483 max_sym_count = sym_count;
7484
7485 if (sym_count > max_sym_shndx_count
7486 && elf_symtab_shndx (sec->owner) != 0)
7487 max_sym_shndx_count = sym_count;
7488
7489 if ((sec->flags & SEC_RELOC) != 0)
7490 {
7491 size_t ext_size;
7492
7493 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7494 if (ext_size > max_external_reloc_size)
7495 max_external_reloc_size = ext_size;
7496 if (sec->reloc_count > max_internal_reloc_count)
7497 max_internal_reloc_count = sec->reloc_count;
7498 }
7499 }
7500 }
7501
7502 if (reloc_count == 0)
7503 continue;
7504
7505 o->reloc_count += reloc_count;
7506
7507 /* MIPS may have a mix of REL and RELA relocs on sections.
7508 To support this curious ABI we keep reloc counts in
7509 elf_section_data too. We must be careful to add the
7510 relocations from the input section to the right output
7511 count. FIXME: Get rid of one count. We have
7512 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7513 rel_count1 = &esdo->rel_count;
7514 if (esdi != NULL)
7515 {
7516 bfd_boolean same_size;
7517 bfd_size_type entsize1;
7518
7519 entsize1 = esdi->rel_hdr.sh_entsize;
7520 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7521 || entsize1 == bed->s->sizeof_rela);
7522 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7523
7524 if (!same_size)
7525 rel_count1 = &esdo->rel_count2;
7526
7527 if (esdi->rel_hdr2 != NULL)
7528 {
7529 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7530 unsigned int alt_count;
7531 unsigned int *rel_count2;
7532
7533 BFD_ASSERT (entsize2 != entsize1
7534 && (entsize2 == bed->s->sizeof_rel
7535 || entsize2 == bed->s->sizeof_rela));
7536
7537 rel_count2 = &esdo->rel_count2;
7538 if (!same_size)
7539 rel_count2 = &esdo->rel_count;
7540
7541 /* The following is probably too simplistic if the
7542 backend counts output relocs unusually. */
7543 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7544 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7545 *rel_count2 += alt_count;
7546 reloc_count -= alt_count;
7547 }
7548 }
7549 *rel_count1 += reloc_count;
7550 }
7551
7552 if (o->reloc_count > 0)
7553 o->flags |= SEC_RELOC;
7554 else
7555 {
7556 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7557 set it (this is probably a bug) and if it is set
7558 assign_section_numbers will create a reloc section. */
7559 o->flags &=~ SEC_RELOC;
7560 }
7561
7562 /* If the SEC_ALLOC flag is not set, force the section VMA to
7563 zero. This is done in elf_fake_sections as well, but forcing
7564 the VMA to 0 here will ensure that relocs against these
7565 sections are handled correctly. */
7566 if ((o->flags & SEC_ALLOC) == 0
7567 && ! o->user_set_vma)
7568 o->vma = 0;
7569 }
7570
7571 if (! info->relocatable && merged)
7572 elf_link_hash_traverse (elf_hash_table (info),
7573 _bfd_elf_link_sec_merge_syms, abfd);
7574
7575 /* Figure out the file positions for everything but the symbol table
7576 and the relocs. We set symcount to force assign_section_numbers
7577 to create a symbol table. */
7578 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7579 BFD_ASSERT (! abfd->output_has_begun);
7580 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7581 goto error_return;
7582
7583 /* That created the reloc sections. Set their sizes, and assign
7584 them file positions, and allocate some buffers. */
7585 for (o = abfd->sections; o != NULL; o = o->next)
7586 {
7587 if ((o->flags & SEC_RELOC) != 0)
7588 {
7589 if (!(_bfd_elf_link_size_reloc_section
7590 (abfd, &elf_section_data (o)->rel_hdr, o)))
7591 goto error_return;
7592
7593 if (elf_section_data (o)->rel_hdr2
7594 && !(_bfd_elf_link_size_reloc_section
7595 (abfd, elf_section_data (o)->rel_hdr2, o)))
7596 goto error_return;
7597 }
7598
7599 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7600 to count upwards while actually outputting the relocations. */
7601 elf_section_data (o)->rel_count = 0;
7602 elf_section_data (o)->rel_count2 = 0;
7603 }
7604
7605 _bfd_elf_assign_file_positions_for_relocs (abfd);
7606
7607 /* We have now assigned file positions for all the sections except
7608 .symtab and .strtab. We start the .symtab section at the current
7609 file position, and write directly to it. We build the .strtab
7610 section in memory. */
7611 bfd_get_symcount (abfd) = 0;
7612 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7613 /* sh_name is set in prep_headers. */
7614 symtab_hdr->sh_type = SHT_SYMTAB;
7615 /* sh_flags, sh_addr and sh_size all start off zero. */
7616 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7617 /* sh_link is set in assign_section_numbers. */
7618 /* sh_info is set below. */
7619 /* sh_offset is set just below. */
7620 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7621
7622 off = elf_tdata (abfd)->next_file_pos;
7623 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7624
7625 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7626 incorrect. We do not yet know the size of the .symtab section.
7627 We correct next_file_pos below, after we do know the size. */
7628
7629 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7630 continuously seeking to the right position in the file. */
7631 if (! info->keep_memory || max_sym_count < 20)
7632 finfo.symbuf_size = 20;
7633 else
7634 finfo.symbuf_size = max_sym_count;
7635 amt = finfo.symbuf_size;
7636 amt *= bed->s->sizeof_sym;
7637 finfo.symbuf = bfd_malloc (amt);
7638 if (finfo.symbuf == NULL)
7639 goto error_return;
7640 if (elf_numsections (abfd) > SHN_LORESERVE)
7641 {
7642 /* Wild guess at number of output symbols. realloc'd as needed. */
7643 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
7644 finfo.shndxbuf_size = amt;
7645 amt *= sizeof (Elf_External_Sym_Shndx);
7646 finfo.symshndxbuf = bfd_zmalloc (amt);
7647 if (finfo.symshndxbuf == NULL)
7648 goto error_return;
7649 }
7650
7651 /* Start writing out the symbol table. The first symbol is always a
7652 dummy symbol. */
7653 if (info->strip != strip_all
7654 || emit_relocs)
7655 {
7656 elfsym.st_value = 0;
7657 elfsym.st_size = 0;
7658 elfsym.st_info = 0;
7659 elfsym.st_other = 0;
7660 elfsym.st_shndx = SHN_UNDEF;
7661 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
7662 NULL))
7663 goto error_return;
7664 }
7665
7666#if 0
7667 /* Some standard ELF linkers do this, but we don't because it causes
7668 bootstrap comparison failures. */
7669 /* Output a file symbol for the output file as the second symbol.
7670 We output this even if we are discarding local symbols, although
7671 I'm not sure if this is correct. */
7672 elfsym.st_value = 0;
7673 elfsym.st_size = 0;
7674 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7675 elfsym.st_other = 0;
7676 elfsym.st_shndx = SHN_ABS;
7677 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
7678 &elfsym, bfd_abs_section_ptr, NULL))
7679 goto error_return;
7680#endif
7681
7682 /* Output a symbol for each section. We output these even if we are
7683 discarding local symbols, since they are used for relocs. These
7684 symbols have no names. We store the index of each one in the
7685 index field of the section, so that we can find it again when
7686 outputting relocs. */
7687 if (info->strip != strip_all
7688 || emit_relocs)
7689 {
7690 elfsym.st_size = 0;
7691 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7692 elfsym.st_other = 0;
7693 for (i = 1; i < elf_numsections (abfd); i++)
7694 {
7695 o = bfd_section_from_elf_index (abfd, i);
7696 if (o != NULL)
7697 o->target_index = bfd_get_symcount (abfd);
7698 elfsym.st_shndx = i;
7699 if (info->relocatable || o == NULL)
7700 elfsym.st_value = 0;
7701 else
7702 elfsym.st_value = o->vma;
7703 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
7704 goto error_return;
7705 if (i == SHN_LORESERVE - 1)
7706 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
7707 }
7708 }
7709
7710 /* Allocate some memory to hold information read in from the input
7711 files. */
7712 if (max_contents_size != 0)
7713 {
7714 finfo.contents = bfd_malloc (max_contents_size);
7715 if (finfo.contents == NULL)
7716 goto error_return;
7717 }
7718
7719 if (max_external_reloc_size != 0)
7720 {
7721 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
7722 if (finfo.external_relocs == NULL)
7723 goto error_return;
7724 }
7725
7726 if (max_internal_reloc_count != 0)
7727 {
7728 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
7729 amt *= sizeof (Elf_Internal_Rela);
7730 finfo.internal_relocs = bfd_malloc (amt);
7731 if (finfo.internal_relocs == NULL)
7732 goto error_return;
7733 }
7734
7735 if (max_sym_count != 0)
7736 {
7737 amt = max_sym_count * bed->s->sizeof_sym;
7738 finfo.external_syms = bfd_malloc (amt);
7739 if (finfo.external_syms == NULL)
7740 goto error_return;
7741
7742 amt = max_sym_count * sizeof (Elf_Internal_Sym);
7743 finfo.internal_syms = bfd_malloc (amt);
7744 if (finfo.internal_syms == NULL)
7745 goto error_return;
7746
7747 amt = max_sym_count * sizeof (long);
7748 finfo.indices = bfd_malloc (amt);
7749 if (finfo.indices == NULL)
7750 goto error_return;
7751
7752 amt = max_sym_count * sizeof (asection *);
7753 finfo.sections = bfd_malloc (amt);
7754 if (finfo.sections == NULL)
7755 goto error_return;
7756 }
7757
7758 if (max_sym_shndx_count != 0)
7759 {
7760 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
7761 finfo.locsym_shndx = bfd_malloc (amt);
7762 if (finfo.locsym_shndx == NULL)
7763 goto error_return;
7764 }
7765
7766 if (elf_hash_table (info)->tls_sec)
7767 {
7768 bfd_vma base, end = 0;
7769 asection *sec;
7770
7771 for (sec = elf_hash_table (info)->tls_sec;
7772 sec && (sec->flags & SEC_THREAD_LOCAL);
7773 sec = sec->next)
7774 {
eea6121a 7775 bfd_vma size = sec->size;
c152c796
AM
7776
7777 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
7778 {
7779 struct bfd_link_order *o;
7780
7781 for (o = sec->link_order_head; o != NULL; o = o->next)
7782 if (size < o->offset + o->size)
7783 size = o->offset + o->size;
7784 }
7785 end = sec->vma + size;
7786 }
7787 base = elf_hash_table (info)->tls_sec->vma;
7788 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
7789 elf_hash_table (info)->tls_size = end - base;
7790 }
7791
0b52efa6
PB
7792 /* Reorder SHF_LINK_ORDER sections. */
7793 for (o = abfd->sections; o != NULL; o = o->next)
7794 {
7795 if (!elf_fixup_link_order (abfd, o))
7796 return FALSE;
7797 }
7798
c152c796
AM
7799 /* Since ELF permits relocations to be against local symbols, we
7800 must have the local symbols available when we do the relocations.
7801 Since we would rather only read the local symbols once, and we
7802 would rather not keep them in memory, we handle all the
7803 relocations for a single input file at the same time.
7804
7805 Unfortunately, there is no way to know the total number of local
7806 symbols until we have seen all of them, and the local symbol
7807 indices precede the global symbol indices. This means that when
7808 we are generating relocatable output, and we see a reloc against
7809 a global symbol, we can not know the symbol index until we have
7810 finished examining all the local symbols to see which ones we are
7811 going to output. To deal with this, we keep the relocations in
7812 memory, and don't output them until the end of the link. This is
7813 an unfortunate waste of memory, but I don't see a good way around
7814 it. Fortunately, it only happens when performing a relocatable
7815 link, which is not the common case. FIXME: If keep_memory is set
7816 we could write the relocs out and then read them again; I don't
7817 know how bad the memory loss will be. */
7818
7819 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7820 sub->output_has_begun = FALSE;
7821 for (o = abfd->sections; o != NULL; o = o->next)
7822 {
7823 for (p = o->link_order_head; p != NULL; p = p->next)
7824 {
7825 if (p->type == bfd_indirect_link_order
7826 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7827 == bfd_target_elf_flavour)
7828 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7829 {
7830 if (! sub->output_has_begun)
7831 {
7832 if (! elf_link_input_bfd (&finfo, sub))
7833 goto error_return;
7834 sub->output_has_begun = TRUE;
7835 }
7836 }
7837 else if (p->type == bfd_section_reloc_link_order
7838 || p->type == bfd_symbol_reloc_link_order)
7839 {
7840 if (! elf_reloc_link_order (abfd, info, o, p))
7841 goto error_return;
7842 }
7843 else
7844 {
7845 if (! _bfd_default_link_order (abfd, info, o, p))
7846 goto error_return;
7847 }
7848 }
7849 }
7850
7851 /* Output any global symbols that got converted to local in a
7852 version script or due to symbol visibility. We do this in a
7853 separate step since ELF requires all local symbols to appear
7854 prior to any global symbols. FIXME: We should only do this if
7855 some global symbols were, in fact, converted to become local.
7856 FIXME: Will this work correctly with the Irix 5 linker? */
7857 eoinfo.failed = FALSE;
7858 eoinfo.finfo = &finfo;
7859 eoinfo.localsyms = TRUE;
7860 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
7861 &eoinfo);
7862 if (eoinfo.failed)
7863 return FALSE;
7864
7865 /* That wrote out all the local symbols. Finish up the symbol table
7866 with the global symbols. Even if we want to strip everything we
7867 can, we still need to deal with those global symbols that got
7868 converted to local in a version script. */
7869
7870 /* The sh_info field records the index of the first non local symbol. */
7871 symtab_hdr->sh_info = bfd_get_symcount (abfd);
7872
7873 if (dynamic
7874 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
7875 {
7876 Elf_Internal_Sym sym;
7877 bfd_byte *dynsym = finfo.dynsym_sec->contents;
7878 long last_local = 0;
7879
7880 /* Write out the section symbols for the output sections. */
7881 if (info->shared)
7882 {
7883 asection *s;
7884
7885 sym.st_size = 0;
7886 sym.st_name = 0;
7887 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7888 sym.st_other = 0;
7889
7890 for (s = abfd->sections; s != NULL; s = s->next)
7891 {
7892 int indx;
7893 bfd_byte *dest;
7894 long dynindx;
7895
c152c796 7896 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
7897 if (dynindx <= 0)
7898 continue;
7899 indx = elf_section_data (s)->this_idx;
c152c796
AM
7900 BFD_ASSERT (indx > 0);
7901 sym.st_shndx = indx;
7902 sym.st_value = s->vma;
7903 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
7904 if (last_local < dynindx)
7905 last_local = dynindx;
c152c796
AM
7906 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7907 }
c152c796
AM
7908 }
7909
7910 /* Write out the local dynsyms. */
7911 if (elf_hash_table (info)->dynlocal)
7912 {
7913 struct elf_link_local_dynamic_entry *e;
7914 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
7915 {
7916 asection *s;
7917 bfd_byte *dest;
7918
7919 sym.st_size = e->isym.st_size;
7920 sym.st_other = e->isym.st_other;
7921
7922 /* Copy the internal symbol as is.
7923 Note that we saved a word of storage and overwrote
7924 the original st_name with the dynstr_index. */
7925 sym = e->isym;
7926
7927 if (e->isym.st_shndx != SHN_UNDEF
7928 && (e->isym.st_shndx < SHN_LORESERVE
7929 || e->isym.st_shndx > SHN_HIRESERVE))
7930 {
7931 s = bfd_section_from_elf_index (e->input_bfd,
7932 e->isym.st_shndx);
7933
7934 sym.st_shndx =
7935 elf_section_data (s->output_section)->this_idx;
7936 sym.st_value = (s->output_section->vma
7937 + s->output_offset
7938 + e->isym.st_value);
7939 }
7940
7941 if (last_local < e->dynindx)
7942 last_local = e->dynindx;
7943
7944 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
7945 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7946 }
7947 }
7948
7949 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
7950 last_local + 1;
7951 }
7952
7953 /* We get the global symbols from the hash table. */
7954 eoinfo.failed = FALSE;
7955 eoinfo.localsyms = FALSE;
7956 eoinfo.finfo = &finfo;
7957 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
7958 &eoinfo);
7959 if (eoinfo.failed)
7960 return FALSE;
7961
7962 /* If backend needs to output some symbols not present in the hash
7963 table, do it now. */
7964 if (bed->elf_backend_output_arch_syms)
7965 {
7966 typedef bfd_boolean (*out_sym_func)
7967 (void *, const char *, Elf_Internal_Sym *, asection *,
7968 struct elf_link_hash_entry *);
7969
7970 if (! ((*bed->elf_backend_output_arch_syms)
7971 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
7972 return FALSE;
7973 }
7974
7975 /* Flush all symbols to the file. */
7976 if (! elf_link_flush_output_syms (&finfo, bed))
7977 return FALSE;
7978
7979 /* Now we know the size of the symtab section. */
7980 off += symtab_hdr->sh_size;
7981
7982 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
7983 if (symtab_shndx_hdr->sh_name != 0)
7984 {
7985 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7986 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7987 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7988 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
7989 symtab_shndx_hdr->sh_size = amt;
7990
7991 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
7992 off, TRUE);
7993
7994 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
7995 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
7996 return FALSE;
7997 }
7998
7999
8000 /* Finish up and write out the symbol string table (.strtab)
8001 section. */
8002 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8003 /* sh_name was set in prep_headers. */
8004 symstrtab_hdr->sh_type = SHT_STRTAB;
8005 symstrtab_hdr->sh_flags = 0;
8006 symstrtab_hdr->sh_addr = 0;
8007 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8008 symstrtab_hdr->sh_entsize = 0;
8009 symstrtab_hdr->sh_link = 0;
8010 symstrtab_hdr->sh_info = 0;
8011 /* sh_offset is set just below. */
8012 symstrtab_hdr->sh_addralign = 1;
8013
8014 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8015 elf_tdata (abfd)->next_file_pos = off;
8016
8017 if (bfd_get_symcount (abfd) > 0)
8018 {
8019 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8020 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8021 return FALSE;
8022 }
8023
8024 /* Adjust the relocs to have the correct symbol indices. */
8025 for (o = abfd->sections; o != NULL; o = o->next)
8026 {
8027 if ((o->flags & SEC_RELOC) == 0)
8028 continue;
8029
8030 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8031 elf_section_data (o)->rel_count,
8032 elf_section_data (o)->rel_hashes);
8033 if (elf_section_data (o)->rel_hdr2 != NULL)
8034 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8035 elf_section_data (o)->rel_count2,
8036 (elf_section_data (o)->rel_hashes
8037 + elf_section_data (o)->rel_count));
8038
8039 /* Set the reloc_count field to 0 to prevent write_relocs from
8040 trying to swap the relocs out itself. */
8041 o->reloc_count = 0;
8042 }
8043
8044 if (dynamic && info->combreloc && dynobj != NULL)
8045 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8046
8047 /* If we are linking against a dynamic object, or generating a
8048 shared library, finish up the dynamic linking information. */
8049 if (dynamic)
8050 {
8051 bfd_byte *dyncon, *dynconend;
8052
8053 /* Fix up .dynamic entries. */
8054 o = bfd_get_section_by_name (dynobj, ".dynamic");
8055 BFD_ASSERT (o != NULL);
8056
8057 dyncon = o->contents;
eea6121a 8058 dynconend = o->contents + o->size;
c152c796
AM
8059 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8060 {
8061 Elf_Internal_Dyn dyn;
8062 const char *name;
8063 unsigned int type;
8064
8065 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8066
8067 switch (dyn.d_tag)
8068 {
8069 default:
8070 continue;
8071 case DT_NULL:
8072 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8073 {
8074 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8075 {
8076 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8077 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8078 default: continue;
8079 }
8080 dyn.d_un.d_val = relativecount;
8081 relativecount = 0;
8082 break;
8083 }
8084 continue;
8085
8086 case DT_INIT:
8087 name = info->init_function;
8088 goto get_sym;
8089 case DT_FINI:
8090 name = info->fini_function;
8091 get_sym:
8092 {
8093 struct elf_link_hash_entry *h;
8094
8095 h = elf_link_hash_lookup (elf_hash_table (info), name,
8096 FALSE, FALSE, TRUE);
8097 if (h != NULL
8098 && (h->root.type == bfd_link_hash_defined
8099 || h->root.type == bfd_link_hash_defweak))
8100 {
8101 dyn.d_un.d_val = h->root.u.def.value;
8102 o = h->root.u.def.section;
8103 if (o->output_section != NULL)
8104 dyn.d_un.d_val += (o->output_section->vma
8105 + o->output_offset);
8106 else
8107 {
8108 /* The symbol is imported from another shared
8109 library and does not apply to this one. */
8110 dyn.d_un.d_val = 0;
8111 }
8112 break;
8113 }
8114 }
8115 continue;
8116
8117 case DT_PREINIT_ARRAYSZ:
8118 name = ".preinit_array";
8119 goto get_size;
8120 case DT_INIT_ARRAYSZ:
8121 name = ".init_array";
8122 goto get_size;
8123 case DT_FINI_ARRAYSZ:
8124 name = ".fini_array";
8125 get_size:
8126 o = bfd_get_section_by_name (abfd, name);
8127 if (o == NULL)
8128 {
8129 (*_bfd_error_handler)
d003868e 8130 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
8131 goto error_return;
8132 }
eea6121a 8133 if (o->size == 0)
c152c796
AM
8134 (*_bfd_error_handler)
8135 (_("warning: %s section has zero size"), name);
eea6121a 8136 dyn.d_un.d_val = o->size;
c152c796
AM
8137 break;
8138
8139 case DT_PREINIT_ARRAY:
8140 name = ".preinit_array";
8141 goto get_vma;
8142 case DT_INIT_ARRAY:
8143 name = ".init_array";
8144 goto get_vma;
8145 case DT_FINI_ARRAY:
8146 name = ".fini_array";
8147 goto get_vma;
8148
8149 case DT_HASH:
8150 name = ".hash";
8151 goto get_vma;
8152 case DT_STRTAB:
8153 name = ".dynstr";
8154 goto get_vma;
8155 case DT_SYMTAB:
8156 name = ".dynsym";
8157 goto get_vma;
8158 case DT_VERDEF:
8159 name = ".gnu.version_d";
8160 goto get_vma;
8161 case DT_VERNEED:
8162 name = ".gnu.version_r";
8163 goto get_vma;
8164 case DT_VERSYM:
8165 name = ".gnu.version";
8166 get_vma:
8167 o = bfd_get_section_by_name (abfd, name);
8168 if (o == NULL)
8169 {
8170 (*_bfd_error_handler)
d003868e 8171 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
8172 goto error_return;
8173 }
8174 dyn.d_un.d_ptr = o->vma;
8175 break;
8176
8177 case DT_REL:
8178 case DT_RELA:
8179 case DT_RELSZ:
8180 case DT_RELASZ:
8181 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8182 type = SHT_REL;
8183 else
8184 type = SHT_RELA;
8185 dyn.d_un.d_val = 0;
8186 for (i = 1; i < elf_numsections (abfd); i++)
8187 {
8188 Elf_Internal_Shdr *hdr;
8189
8190 hdr = elf_elfsections (abfd)[i];
8191 if (hdr->sh_type == type
8192 && (hdr->sh_flags & SHF_ALLOC) != 0)
8193 {
8194 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8195 dyn.d_un.d_val += hdr->sh_size;
8196 else
8197 {
8198 if (dyn.d_un.d_val == 0
8199 || hdr->sh_addr < dyn.d_un.d_val)
8200 dyn.d_un.d_val = hdr->sh_addr;
8201 }
8202 }
8203 }
8204 break;
8205 }
8206 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8207 }
8208 }
8209
8210 /* If we have created any dynamic sections, then output them. */
8211 if (dynobj != NULL)
8212 {
8213 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8214 goto error_return;
8215
8216 for (o = dynobj->sections; o != NULL; o = o->next)
8217 {
8218 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 8219 || o->size == 0
c152c796
AM
8220 || o->output_section == bfd_abs_section_ptr)
8221 continue;
8222 if ((o->flags & SEC_LINKER_CREATED) == 0)
8223 {
8224 /* At this point, we are only interested in sections
8225 created by _bfd_elf_link_create_dynamic_sections. */
8226 continue;
8227 }
3722b82f
AM
8228 if (elf_hash_table (info)->stab_info.stabstr == o)
8229 continue;
eea6121a
AM
8230 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8231 continue;
c152c796
AM
8232 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8233 != SHT_STRTAB)
8234 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8235 {
8236 if (! bfd_set_section_contents (abfd, o->output_section,
8237 o->contents,
8238 (file_ptr) o->output_offset,
eea6121a 8239 o->size))
c152c796
AM
8240 goto error_return;
8241 }
8242 else
8243 {
8244 /* The contents of the .dynstr section are actually in a
8245 stringtab. */
8246 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8247 if (bfd_seek (abfd, off, SEEK_SET) != 0
8248 || ! _bfd_elf_strtab_emit (abfd,
8249 elf_hash_table (info)->dynstr))
8250 goto error_return;
8251 }
8252 }
8253 }
8254
8255 if (info->relocatable)
8256 {
8257 bfd_boolean failed = FALSE;
8258
8259 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8260 if (failed)
8261 goto error_return;
8262 }
8263
8264 /* If we have optimized stabs strings, output them. */
3722b82f 8265 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
8266 {
8267 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8268 goto error_return;
8269 }
8270
8271 if (info->eh_frame_hdr)
8272 {
8273 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8274 goto error_return;
8275 }
8276
8277 if (finfo.symstrtab != NULL)
8278 _bfd_stringtab_free (finfo.symstrtab);
8279 if (finfo.contents != NULL)
8280 free (finfo.contents);
8281 if (finfo.external_relocs != NULL)
8282 free (finfo.external_relocs);
8283 if (finfo.internal_relocs != NULL)
8284 free (finfo.internal_relocs);
8285 if (finfo.external_syms != NULL)
8286 free (finfo.external_syms);
8287 if (finfo.locsym_shndx != NULL)
8288 free (finfo.locsym_shndx);
8289 if (finfo.internal_syms != NULL)
8290 free (finfo.internal_syms);
8291 if (finfo.indices != NULL)
8292 free (finfo.indices);
8293 if (finfo.sections != NULL)
8294 free (finfo.sections);
8295 if (finfo.symbuf != NULL)
8296 free (finfo.symbuf);
8297 if (finfo.symshndxbuf != NULL)
8298 free (finfo.symshndxbuf);
8299 for (o = abfd->sections; o != NULL; o = o->next)
8300 {
8301 if ((o->flags & SEC_RELOC) != 0
8302 && elf_section_data (o)->rel_hashes != NULL)
8303 free (elf_section_data (o)->rel_hashes);
8304 }
8305
8306 elf_tdata (abfd)->linker = TRUE;
8307
8308 return TRUE;
8309
8310 error_return:
8311 if (finfo.symstrtab != NULL)
8312 _bfd_stringtab_free (finfo.symstrtab);
8313 if (finfo.contents != NULL)
8314 free (finfo.contents);
8315 if (finfo.external_relocs != NULL)
8316 free (finfo.external_relocs);
8317 if (finfo.internal_relocs != NULL)
8318 free (finfo.internal_relocs);
8319 if (finfo.external_syms != NULL)
8320 free (finfo.external_syms);
8321 if (finfo.locsym_shndx != NULL)
8322 free (finfo.locsym_shndx);
8323 if (finfo.internal_syms != NULL)
8324 free (finfo.internal_syms);
8325 if (finfo.indices != NULL)
8326 free (finfo.indices);
8327 if (finfo.sections != NULL)
8328 free (finfo.sections);
8329 if (finfo.symbuf != NULL)
8330 free (finfo.symbuf);
8331 if (finfo.symshndxbuf != NULL)
8332 free (finfo.symshndxbuf);
8333 for (o = abfd->sections; o != NULL; o = o->next)
8334 {
8335 if ((o->flags & SEC_RELOC) != 0
8336 && elf_section_data (o)->rel_hashes != NULL)
8337 free (elf_section_data (o)->rel_hashes);
8338 }
8339
8340 return FALSE;
8341}
8342\f
8343/* Garbage collect unused sections. */
8344
8345/* The mark phase of garbage collection. For a given section, mark
8346 it and any sections in this section's group, and all the sections
8347 which define symbols to which it refers. */
8348
8349typedef asection * (*gc_mark_hook_fn)
8350 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8351 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8352
ccfa59ea
AM
8353bfd_boolean
8354_bfd_elf_gc_mark (struct bfd_link_info *info,
8355 asection *sec,
8356 gc_mark_hook_fn gc_mark_hook)
c152c796
AM
8357{
8358 bfd_boolean ret;
8359 asection *group_sec;
8360
8361 sec->gc_mark = 1;
8362
8363 /* Mark all the sections in the group. */
8364 group_sec = elf_section_data (sec)->next_in_group;
8365 if (group_sec && !group_sec->gc_mark)
ccfa59ea 8366 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
8367 return FALSE;
8368
8369 /* Look through the section relocs. */
8370 ret = TRUE;
8371 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8372 {
8373 Elf_Internal_Rela *relstart, *rel, *relend;
8374 Elf_Internal_Shdr *symtab_hdr;
8375 struct elf_link_hash_entry **sym_hashes;
8376 size_t nlocsyms;
8377 size_t extsymoff;
8378 bfd *input_bfd = sec->owner;
8379 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8380 Elf_Internal_Sym *isym = NULL;
8381 int r_sym_shift;
8382
8383 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8384 sym_hashes = elf_sym_hashes (input_bfd);
8385
8386 /* Read the local symbols. */
8387 if (elf_bad_symtab (input_bfd))
8388 {
8389 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8390 extsymoff = 0;
8391 }
8392 else
8393 extsymoff = nlocsyms = symtab_hdr->sh_info;
8394
8395 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8396 if (isym == NULL && nlocsyms != 0)
8397 {
8398 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8399 NULL, NULL, NULL);
8400 if (isym == NULL)
8401 return FALSE;
8402 }
8403
8404 /* Read the relocations. */
8405 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8406 info->keep_memory);
8407 if (relstart == NULL)
8408 {
8409 ret = FALSE;
8410 goto out1;
8411 }
8412 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8413
8414 if (bed->s->arch_size == 32)
8415 r_sym_shift = 8;
8416 else
8417 r_sym_shift = 32;
8418
8419 for (rel = relstart; rel < relend; rel++)
8420 {
8421 unsigned long r_symndx;
8422 asection *rsec;
8423 struct elf_link_hash_entry *h;
8424
8425 r_symndx = rel->r_info >> r_sym_shift;
8426 if (r_symndx == 0)
8427 continue;
8428
8429 if (r_symndx >= nlocsyms
8430 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8431 {
8432 h = sym_hashes[r_symndx - extsymoff];
20f0a1ad
AM
8433 while (h->root.type == bfd_link_hash_indirect
8434 || h->root.type == bfd_link_hash_warning)
8435 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796
AM
8436 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8437 }
8438 else
8439 {
8440 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8441 }
8442
8443 if (rsec && !rsec->gc_mark)
8444 {
8445 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8446 rsec->gc_mark = 1;
ccfa59ea 8447 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
c152c796
AM
8448 {
8449 ret = FALSE;
8450 goto out2;
8451 }
8452 }
8453 }
8454
8455 out2:
8456 if (elf_section_data (sec)->relocs != relstart)
8457 free (relstart);
8458 out1:
8459 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8460 {
8461 if (! info->keep_memory)
8462 free (isym);
8463 else
8464 symtab_hdr->contents = (unsigned char *) isym;
8465 }
8466 }
8467
8468 return ret;
8469}
8470
8471/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8472
8473static bfd_boolean
8474elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8475{
8476 int *idx = idxptr;
8477
8478 if (h->root.type == bfd_link_hash_warning)
8479 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8480
8481 if (h->dynindx != -1
8482 && ((h->root.type != bfd_link_hash_defined
8483 && h->root.type != bfd_link_hash_defweak)
8484 || h->root.u.def.section->gc_mark))
8485 h->dynindx = (*idx)++;
8486
8487 return TRUE;
8488}
8489
8490/* The sweep phase of garbage collection. Remove all garbage sections. */
8491
8492typedef bfd_boolean (*gc_sweep_hook_fn)
8493 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8494
8495static bfd_boolean
8496elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8497{
8498 bfd *sub;
8499
8500 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8501 {
8502 asection *o;
8503
8504 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8505 continue;
8506
8507 for (o = sub->sections; o != NULL; o = o->next)
8508 {
7c2c8505
AM
8509 /* Keep debug and special sections. */
8510 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8511 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
c152c796
AM
8512 o->gc_mark = 1;
8513
8514 if (o->gc_mark)
8515 continue;
8516
8517 /* Skip sweeping sections already excluded. */
8518 if (o->flags & SEC_EXCLUDE)
8519 continue;
8520
8521 /* Since this is early in the link process, it is simple
8522 to remove a section from the output. */
8523 o->flags |= SEC_EXCLUDE;
8524
8525 /* But we also have to update some of the relocation
8526 info we collected before. */
8527 if (gc_sweep_hook
8528 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
8529 {
8530 Elf_Internal_Rela *internal_relocs;
8531 bfd_boolean r;
8532
8533 internal_relocs
8534 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8535 info->keep_memory);
8536 if (internal_relocs == NULL)
8537 return FALSE;
8538
8539 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8540
8541 if (elf_section_data (o)->relocs != internal_relocs)
8542 free (internal_relocs);
8543
8544 if (!r)
8545 return FALSE;
8546 }
8547 }
8548 }
8549
8550 /* Remove the symbols that were in the swept sections from the dynamic
8551 symbol table. GCFIXME: Anyone know how to get them out of the
8552 static symbol table as well? */
8553 {
8554 int i = 0;
8555
8556 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8557
8558 elf_hash_table (info)->dynsymcount = i;
8559 }
8560
8561 return TRUE;
8562}
8563
8564/* Propagate collected vtable information. This is called through
8565 elf_link_hash_traverse. */
8566
8567static bfd_boolean
8568elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8569{
8570 if (h->root.type == bfd_link_hash_warning)
8571 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8572
8573 /* Those that are not vtables. */
8574 if (h->vtable_parent == NULL)
8575 return TRUE;
8576
8577 /* Those vtables that do not have parents, we cannot merge. */
8578 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
8579 return TRUE;
8580
8581 /* If we've already been done, exit. */
8582 if (h->vtable_entries_used && h->vtable_entries_used[-1])
8583 return TRUE;
8584
8585 /* Make sure the parent's table is up to date. */
8586 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
8587
8588 if (h->vtable_entries_used == NULL)
8589 {
8590 /* None of this table's entries were referenced. Re-use the
8591 parent's table. */
8592 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
8593 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
8594 }
8595 else
8596 {
8597 size_t n;
8598 bfd_boolean *cu, *pu;
8599
8600 /* Or the parent's entries into ours. */
8601 cu = h->vtable_entries_used;
8602 cu[-1] = TRUE;
8603 pu = h->vtable_parent->vtable_entries_used;
8604 if (pu != NULL)
8605 {
8606 const struct elf_backend_data *bed;
8607 unsigned int log_file_align;
8608
8609 bed = get_elf_backend_data (h->root.u.def.section->owner);
8610 log_file_align = bed->s->log_file_align;
8611 n = h->vtable_parent->vtable_entries_size >> log_file_align;
8612 while (n--)
8613 {
8614 if (*pu)
8615 *cu = TRUE;
8616 pu++;
8617 cu++;
8618 }
8619 }
8620 }
8621
8622 return TRUE;
8623}
8624
8625static bfd_boolean
8626elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
8627{
8628 asection *sec;
8629 bfd_vma hstart, hend;
8630 Elf_Internal_Rela *relstart, *relend, *rel;
8631 const struct elf_backend_data *bed;
8632 unsigned int log_file_align;
8633
8634 if (h->root.type == bfd_link_hash_warning)
8635 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8636
8637 /* Take care of both those symbols that do not describe vtables as
8638 well as those that are not loaded. */
8639 if (h->vtable_parent == NULL)
8640 return TRUE;
8641
8642 BFD_ASSERT (h->root.type == bfd_link_hash_defined
8643 || h->root.type == bfd_link_hash_defweak);
8644
8645 sec = h->root.u.def.section;
8646 hstart = h->root.u.def.value;
8647 hend = hstart + h->size;
8648
8649 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
8650 if (!relstart)
8651 return *(bfd_boolean *) okp = FALSE;
8652 bed = get_elf_backend_data (sec->owner);
8653 log_file_align = bed->s->log_file_align;
8654
8655 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8656
8657 for (rel = relstart; rel < relend; ++rel)
8658 if (rel->r_offset >= hstart && rel->r_offset < hend)
8659 {
8660 /* If the entry is in use, do nothing. */
8661 if (h->vtable_entries_used
8662 && (rel->r_offset - hstart) < h->vtable_entries_size)
8663 {
8664 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
8665 if (h->vtable_entries_used[entry])
8666 continue;
8667 }
8668 /* Otherwise, kill it. */
8669 rel->r_offset = rel->r_info = rel->r_addend = 0;
8670 }
8671
8672 return TRUE;
8673}
8674
715df9b8
EB
8675/* Mark sections containing dynamically referenced symbols. This is called
8676 through elf_link_hash_traverse. */
8677
8678static bfd_boolean
8679elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
8680 void *okp ATTRIBUTE_UNUSED)
8681{
8682 if (h->root.type == bfd_link_hash_warning)
8683 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8684
8685 if ((h->root.type == bfd_link_hash_defined
8686 || h->root.type == bfd_link_hash_defweak)
8687 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC))
8688 h->root.u.def.section->flags |= SEC_KEEP;
8689
8690 return TRUE;
8691}
8692
c152c796
AM
8693/* Do mark and sweep of unused sections. */
8694
8695bfd_boolean
8696bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
8697{
8698 bfd_boolean ok = TRUE;
8699 bfd *sub;
8700 asection * (*gc_mark_hook)
8701 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8702 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
8703
8704 if (!get_elf_backend_data (abfd)->can_gc_sections
8705 || info->relocatable
8706 || info->emitrelocations
715df9b8
EB
8707 || info->shared
8708 || !is_elf_hash_table (info->hash))
c152c796
AM
8709 {
8710 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
8711 return TRUE;
8712 }
8713
8714 /* Apply transitive closure to the vtable entry usage info. */
8715 elf_link_hash_traverse (elf_hash_table (info),
8716 elf_gc_propagate_vtable_entries_used,
8717 &ok);
8718 if (!ok)
8719 return FALSE;
8720
8721 /* Kill the vtable relocations that were not used. */
8722 elf_link_hash_traverse (elf_hash_table (info),
8723 elf_gc_smash_unused_vtentry_relocs,
8724 &ok);
8725 if (!ok)
8726 return FALSE;
8727
715df9b8
EB
8728 /* Mark dynamically referenced symbols. */
8729 if (elf_hash_table (info)->dynamic_sections_created)
8730 elf_link_hash_traverse (elf_hash_table (info),
8731 elf_gc_mark_dynamic_ref_symbol,
8732 &ok);
8733 if (!ok)
8734 return FALSE;
c152c796 8735
715df9b8 8736 /* Grovel through relocs to find out who stays ... */
c152c796
AM
8737 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8738 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8739 {
8740 asection *o;
8741
8742 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8743 continue;
8744
8745 for (o = sub->sections; o != NULL; o = o->next)
8746 {
8747 if (o->flags & SEC_KEEP)
715df9b8
EB
8748 {
8749 /* _bfd_elf_discard_section_eh_frame knows how to discard
8750 orphaned FDEs so don't mark sections referenced by the
8751 EH frame section. */
8752 if (strcmp (o->name, ".eh_frame") == 0)
8753 o->gc_mark = 1;
ccfa59ea 8754 else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
715df9b8
EB
8755 return FALSE;
8756 }
c152c796
AM
8757 }
8758 }
8759
8760 /* ... and mark SEC_EXCLUDE for those that go. */
8761 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
8762 return FALSE;
8763
8764 return TRUE;
8765}
8766\f
8767/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8768
8769bfd_boolean
8770bfd_elf_gc_record_vtinherit (bfd *abfd,
8771 asection *sec,
8772 struct elf_link_hash_entry *h,
8773 bfd_vma offset)
8774{
8775 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8776 struct elf_link_hash_entry **search, *child;
8777 bfd_size_type extsymcount;
8778 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8779
8780 /* The sh_info field of the symtab header tells us where the
8781 external symbols start. We don't care about the local symbols at
8782 this point. */
8783 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
8784 if (!elf_bad_symtab (abfd))
8785 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8786
8787 sym_hashes = elf_sym_hashes (abfd);
8788 sym_hashes_end = sym_hashes + extsymcount;
8789
8790 /* Hunt down the child symbol, which is in this section at the same
8791 offset as the relocation. */
8792 for (search = sym_hashes; search != sym_hashes_end; ++search)
8793 {
8794 if ((child = *search) != NULL
8795 && (child->root.type == bfd_link_hash_defined
8796 || child->root.type == bfd_link_hash_defweak)
8797 && child->root.u.def.section == sec
8798 && child->root.u.def.value == offset)
8799 goto win;
8800 }
8801
d003868e
AM
8802 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
8803 abfd, sec, (unsigned long) offset);
c152c796
AM
8804 bfd_set_error (bfd_error_invalid_operation);
8805 return FALSE;
8806
8807 win:
8808 if (!h)
8809 {
8810 /* This *should* only be the absolute section. It could potentially
8811 be that someone has defined a non-global vtable though, which
8812 would be bad. It isn't worth paging in the local symbols to be
8813 sure though; that case should simply be handled by the assembler. */
8814
8815 child->vtable_parent = (struct elf_link_hash_entry *) -1;
8816 }
8817 else
8818 child->vtable_parent = h;
8819
8820 return TRUE;
8821}
8822
8823/* Called from check_relocs to record the existence of a VTENTRY reloc. */
8824
8825bfd_boolean
8826bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
8827 asection *sec ATTRIBUTE_UNUSED,
8828 struct elf_link_hash_entry *h,
8829 bfd_vma addend)
8830{
8831 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8832 unsigned int log_file_align = bed->s->log_file_align;
8833
8834 if (addend >= h->vtable_entries_size)
8835 {
8836 size_t size, bytes, file_align;
8837 bfd_boolean *ptr = h->vtable_entries_used;
8838
8839 /* While the symbol is undefined, we have to be prepared to handle
8840 a zero size. */
8841 file_align = 1 << log_file_align;
8842 if (h->root.type == bfd_link_hash_undefined)
8843 size = addend + file_align;
8844 else
8845 {
8846 size = h->size;
8847 if (addend >= size)
8848 {
8849 /* Oops! We've got a reference past the defined end of
8850 the table. This is probably a bug -- shall we warn? */
8851 size = addend + file_align;
8852 }
8853 }
8854 size = (size + file_align - 1) & -file_align;
8855
8856 /* Allocate one extra entry for use as a "done" flag for the
8857 consolidation pass. */
8858 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
8859
8860 if (ptr)
8861 {
8862 ptr = bfd_realloc (ptr - 1, bytes);
8863
8864 if (ptr != NULL)
8865 {
8866 size_t oldbytes;
8867
8868 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
8869 * sizeof (bfd_boolean));
8870 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
8871 }
8872 }
8873 else
8874 ptr = bfd_zmalloc (bytes);
8875
8876 if (ptr == NULL)
8877 return FALSE;
8878
8879 /* And arrange for that done flag to be at index -1. */
8880 h->vtable_entries_used = ptr + 1;
8881 h->vtable_entries_size = size;
8882 }
8883
8884 h->vtable_entries_used[addend >> log_file_align] = TRUE;
8885
8886 return TRUE;
8887}
8888
8889struct alloc_got_off_arg {
8890 bfd_vma gotoff;
8891 unsigned int got_elt_size;
8892};
8893
8894/* We need a special top-level link routine to convert got reference counts
8895 to real got offsets. */
8896
8897static bfd_boolean
8898elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
8899{
8900 struct alloc_got_off_arg *gofarg = arg;
8901
8902 if (h->root.type == bfd_link_hash_warning)
8903 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8904
8905 if (h->got.refcount > 0)
8906 {
8907 h->got.offset = gofarg->gotoff;
8908 gofarg->gotoff += gofarg->got_elt_size;
8909 }
8910 else
8911 h->got.offset = (bfd_vma) -1;
8912
8913 return TRUE;
8914}
8915
8916/* And an accompanying bit to work out final got entry offsets once
8917 we're done. Should be called from final_link. */
8918
8919bfd_boolean
8920bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
8921 struct bfd_link_info *info)
8922{
8923 bfd *i;
8924 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8925 bfd_vma gotoff;
8926 unsigned int got_elt_size = bed->s->arch_size / 8;
8927 struct alloc_got_off_arg gofarg;
8928
8929 if (! is_elf_hash_table (info->hash))
8930 return FALSE;
8931
8932 /* The GOT offset is relative to the .got section, but the GOT header is
8933 put into the .got.plt section, if the backend uses it. */
8934 if (bed->want_got_plt)
8935 gotoff = 0;
8936 else
8937 gotoff = bed->got_header_size;
8938
8939 /* Do the local .got entries first. */
8940 for (i = info->input_bfds; i; i = i->link_next)
8941 {
8942 bfd_signed_vma *local_got;
8943 bfd_size_type j, locsymcount;
8944 Elf_Internal_Shdr *symtab_hdr;
8945
8946 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
8947 continue;
8948
8949 local_got = elf_local_got_refcounts (i);
8950 if (!local_got)
8951 continue;
8952
8953 symtab_hdr = &elf_tdata (i)->symtab_hdr;
8954 if (elf_bad_symtab (i))
8955 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8956 else
8957 locsymcount = symtab_hdr->sh_info;
8958
8959 for (j = 0; j < locsymcount; ++j)
8960 {
8961 if (local_got[j] > 0)
8962 {
8963 local_got[j] = gotoff;
8964 gotoff += got_elt_size;
8965 }
8966 else
8967 local_got[j] = (bfd_vma) -1;
8968 }
8969 }
8970
8971 /* Then the global .got entries. .plt refcounts are handled by
8972 adjust_dynamic_symbol */
8973 gofarg.gotoff = gotoff;
8974 gofarg.got_elt_size = got_elt_size;
8975 elf_link_hash_traverse (elf_hash_table (info),
8976 elf_gc_allocate_got_offsets,
8977 &gofarg);
8978 return TRUE;
8979}
8980
8981/* Many folk need no more in the way of final link than this, once
8982 got entry reference counting is enabled. */
8983
8984bfd_boolean
8985bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
8986{
8987 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
8988 return FALSE;
8989
8990 /* Invoke the regular ELF backend linker to do all the work. */
8991 return bfd_elf_final_link (abfd, info);
8992}
8993
8994bfd_boolean
8995bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
8996{
8997 struct elf_reloc_cookie *rcookie = cookie;
8998
8999 if (rcookie->bad_symtab)
9000 rcookie->rel = rcookie->rels;
9001
9002 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9003 {
9004 unsigned long r_symndx;
9005
9006 if (! rcookie->bad_symtab)
9007 if (rcookie->rel->r_offset > offset)
9008 return FALSE;
9009 if (rcookie->rel->r_offset != offset)
9010 continue;
9011
9012 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9013 if (r_symndx == SHN_UNDEF)
9014 return TRUE;
9015
9016 if (r_symndx >= rcookie->locsymcount
9017 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9018 {
9019 struct elf_link_hash_entry *h;
9020
9021 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9022
9023 while (h->root.type == bfd_link_hash_indirect
9024 || h->root.type == bfd_link_hash_warning)
9025 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9026
9027 if ((h->root.type == bfd_link_hash_defined
9028 || h->root.type == bfd_link_hash_defweak)
9029 && elf_discarded_section (h->root.u.def.section))
9030 return TRUE;
9031 else
9032 return FALSE;
9033 }
9034 else
9035 {
9036 /* It's not a relocation against a global symbol,
9037 but it could be a relocation against a local
9038 symbol for a discarded section. */
9039 asection *isec;
9040 Elf_Internal_Sym *isym;
9041
9042 /* Need to: get the symbol; get the section. */
9043 isym = &rcookie->locsyms[r_symndx];
9044 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9045 {
9046 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9047 if (isec != NULL && elf_discarded_section (isec))
9048 return TRUE;
9049 }
9050 }
9051 return FALSE;
9052 }
9053 return FALSE;
9054}
9055
9056/* Discard unneeded references to discarded sections.
9057 Returns TRUE if any section's size was changed. */
9058/* This function assumes that the relocations are in sorted order,
9059 which is true for all known assemblers. */
9060
9061bfd_boolean
9062bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9063{
9064 struct elf_reloc_cookie cookie;
9065 asection *stab, *eh;
9066 Elf_Internal_Shdr *symtab_hdr;
9067 const struct elf_backend_data *bed;
9068 bfd *abfd;
9069 unsigned int count;
9070 bfd_boolean ret = FALSE;
9071
9072 if (info->traditional_format
9073 || !is_elf_hash_table (info->hash))
9074 return FALSE;
9075
9076 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9077 {
9078 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9079 continue;
9080
9081 bed = get_elf_backend_data (abfd);
9082
9083 if ((abfd->flags & DYNAMIC) != 0)
9084 continue;
9085
9086 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9087 if (info->relocatable
9088 || (eh != NULL
eea6121a 9089 && (eh->size == 0
c152c796
AM
9090 || bfd_is_abs_section (eh->output_section))))
9091 eh = NULL;
9092
9093 stab = bfd_get_section_by_name (abfd, ".stab");
9094 if (stab != NULL
eea6121a 9095 && (stab->size == 0
c152c796
AM
9096 || bfd_is_abs_section (stab->output_section)
9097 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9098 stab = NULL;
9099
9100 if (stab == NULL
9101 && eh == NULL
9102 && bed->elf_backend_discard_info == NULL)
9103 continue;
9104
9105 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9106 cookie.abfd = abfd;
9107 cookie.sym_hashes = elf_sym_hashes (abfd);
9108 cookie.bad_symtab = elf_bad_symtab (abfd);
9109 if (cookie.bad_symtab)
9110 {
9111 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9112 cookie.extsymoff = 0;
9113 }
9114 else
9115 {
9116 cookie.locsymcount = symtab_hdr->sh_info;
9117 cookie.extsymoff = symtab_hdr->sh_info;
9118 }
9119
9120 if (bed->s->arch_size == 32)
9121 cookie.r_sym_shift = 8;
9122 else
9123 cookie.r_sym_shift = 32;
9124
9125 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9126 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9127 {
9128 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9129 cookie.locsymcount, 0,
9130 NULL, NULL, NULL);
9131 if (cookie.locsyms == NULL)
9132 return FALSE;
9133 }
9134
9135 if (stab != NULL)
9136 {
9137 cookie.rels = NULL;
9138 count = stab->reloc_count;
9139 if (count != 0)
9140 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9141 info->keep_memory);
9142 if (cookie.rels != NULL)
9143 {
9144 cookie.rel = cookie.rels;
9145 cookie.relend = cookie.rels;
9146 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9147 if (_bfd_discard_section_stabs (abfd, stab,
9148 elf_section_data (stab)->sec_info,
9149 bfd_elf_reloc_symbol_deleted_p,
9150 &cookie))
9151 ret = TRUE;
9152 if (elf_section_data (stab)->relocs != cookie.rels)
9153 free (cookie.rels);
9154 }
9155 }
9156
9157 if (eh != NULL)
9158 {
9159 cookie.rels = NULL;
9160 count = eh->reloc_count;
9161 if (count != 0)
9162 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9163 info->keep_memory);
9164 cookie.rel = cookie.rels;
9165 cookie.relend = cookie.rels;
9166 if (cookie.rels != NULL)
9167 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9168
9169 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9170 bfd_elf_reloc_symbol_deleted_p,
9171 &cookie))
9172 ret = TRUE;
9173
9174 if (cookie.rels != NULL
9175 && elf_section_data (eh)->relocs != cookie.rels)
9176 free (cookie.rels);
9177 }
9178
9179 if (bed->elf_backend_discard_info != NULL
9180 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9181 ret = TRUE;
9182
9183 if (cookie.locsyms != NULL
9184 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9185 {
9186 if (! info->keep_memory)
9187 free (cookie.locsyms);
9188 else
9189 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9190 }
9191 }
9192
9193 if (info->eh_frame_hdr
9194 && !info->relocatable
9195 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9196 ret = TRUE;
9197
9198 return ret;
9199}
082b7297 9200
3d7f7666
L
9201struct already_linked_section
9202{
9203 asection *sec;
9204 asection *linked;
9205};
9206
9207/* Check if the member of a single member comdat group matches a
9208 linkonce section and vice versa. */
9209static bfd_boolean
9210try_match_symbols_in_sections
9211 (struct bfd_section_already_linked_hash_entry *h, void *info)
9212{
9213 struct bfd_section_already_linked *l;
9214 struct already_linked_section *s
9215 = (struct already_linked_section *) info;
9216
9217 if (elf_sec_group (s->sec) == NULL)
9218 {
9219 /* It is a linkonce section. Try to match it with the member of a
9220 single member comdat group. */
9221 for (l = h->entry; l != NULL; l = l->next)
9222 if ((l->sec->flags & SEC_GROUP))
9223 {
9224 asection *first = elf_next_in_group (l->sec);
9225
9226 if (first != NULL
9227 && elf_next_in_group (first) == first
9228 && bfd_elf_match_symbols_in_sections (first, s->sec))
9229 {
9230 s->linked = first;
9231 return FALSE;
9232 }
9233 }
9234 }
9235 else
9236 {
9237 /* It is the member of a single member comdat group. Try to match
9238 it with a linkonce section. */
9239 for (l = h->entry; l != NULL; l = l->next)
9240 if ((l->sec->flags & SEC_GROUP) == 0
9241 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9242 && bfd_elf_match_symbols_in_sections (l->sec, s->sec))
9243 {
9244 s->linked = l->sec;
9245 return FALSE;
9246 }
9247 }
9248
9249 return TRUE;
9250}
9251
9252static bfd_boolean
9253already_linked (asection *sec, asection *group)
9254{
9255 struct already_linked_section result;
9256
9257 result.sec = sec;
9258 result.linked = NULL;
9259
9260 bfd_section_already_linked_table_traverse
9261 (try_match_symbols_in_sections, &result);
9262
9263 if (result.linked)
9264 {
9265 sec->output_section = bfd_abs_section_ptr;
9266 sec->kept_section = result.linked;
9267
9268 /* Also discard the group section. */
9269 if (group)
9270 group->output_section = bfd_abs_section_ptr;
9271
9272 return TRUE;
9273 }
9274
9275 return FALSE;
9276}
9277
082b7297
L
9278void
9279_bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9280{
9281 flagword flags;
9282 const char *name;
9283 struct bfd_section_already_linked *l;
9284 struct bfd_section_already_linked_hash_entry *already_linked_list;
3d7f7666
L
9285 asection *group;
9286
9287 /* A single member comdat group section may be discarded by a
9288 linkonce section. See below. */
9289 if (sec->output_section == bfd_abs_section_ptr)
9290 return;
082b7297
L
9291
9292 flags = sec->flags;
3d7f7666
L
9293
9294 /* Check if it belongs to a section group. */
9295 group = elf_sec_group (sec);
9296
9297 /* Return if it isn't a linkonce section nor a member of a group. A
9298 comdat group section also has SEC_LINK_ONCE set. */
9299 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
082b7297
L
9300 return;
9301
3d7f7666
L
9302 if (group)
9303 {
9304 /* If this is the member of a single member comdat group, check if
9305 the group should be discarded. */
9306 if (elf_next_in_group (sec) == sec
9307 && (group->flags & SEC_LINK_ONCE) != 0)
9308 sec = group;
9309 else
9310 return;
9311 }
9312
082b7297
L
9313 /* FIXME: When doing a relocatable link, we may have trouble
9314 copying relocations in other sections that refer to local symbols
9315 in the section being discarded. Those relocations will have to
9316 be converted somehow; as of this writing I'm not sure that any of
9317 the backends handle that correctly.
9318
9319 It is tempting to instead not discard link once sections when
9320 doing a relocatable link (technically, they should be discarded
9321 whenever we are building constructors). However, that fails,
9322 because the linker winds up combining all the link once sections
9323 into a single large link once section, which defeats the purpose
9324 of having link once sections in the first place.
9325
9326 Also, not merging link once sections in a relocatable link
9327 causes trouble for MIPS ELF, which relies on link once semantics
9328 to handle the .reginfo section correctly. */
9329
9330 name = bfd_get_section_name (abfd, sec);
9331
9332 already_linked_list = bfd_section_already_linked_table_lookup (name);
9333
9334 for (l = already_linked_list->entry; l != NULL; l = l->next)
9335 {
9336 /* We may have 3 different sections on the list: group section,
9337 comdat section and linkonce section. SEC may be a linkonce or
9338 group section. We match a group section with a group section,
9339 a linkonce section with a linkonce section, and ignore comdat
9340 section. */
3d7f7666 9341 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
082b7297
L
9342 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9343 {
9344 /* The section has already been linked. See if we should
9345 issue a warning. */
9346 switch (flags & SEC_LINK_DUPLICATES)
9347 {
9348 default:
9349 abort ();
9350
9351 case SEC_LINK_DUPLICATES_DISCARD:
9352 break;
9353
9354 case SEC_LINK_DUPLICATES_ONE_ONLY:
9355 (*_bfd_error_handler)
d003868e
AM
9356 (_("%B: ignoring duplicate section `%A'\n"),
9357 abfd, sec);
082b7297
L
9358 break;
9359
9360 case SEC_LINK_DUPLICATES_SAME_SIZE:
9361 if (sec->size != l->sec->size)
9362 (*_bfd_error_handler)
d003868e
AM
9363 (_("%B: duplicate section `%A' has different size\n"),
9364 abfd, sec);
082b7297 9365 break;
ea5158d8
DJ
9366
9367 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9368 if (sec->size != l->sec->size)
9369 (*_bfd_error_handler)
9370 (_("%B: duplicate section `%A' has different size\n"),
9371 abfd, sec);
9372 else if (sec->size != 0)
9373 {
9374 bfd_byte *sec_contents, *l_sec_contents;
9375
9376 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9377 (*_bfd_error_handler)
9378 (_("%B: warning: could not read contents of section `%A'\n"),
9379 abfd, sec);
9380 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9381 &l_sec_contents))
9382 (*_bfd_error_handler)
9383 (_("%B: warning: could not read contents of section `%A'\n"),
9384 l->sec->owner, l->sec);
9385 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9386 (*_bfd_error_handler)
9387 (_("%B: warning: duplicate section `%A' has different contents\n"),
9388 abfd, sec);
9389
9390 if (sec_contents)
9391 free (sec_contents);
9392 if (l_sec_contents)
9393 free (l_sec_contents);
9394 }
9395 break;
082b7297
L
9396 }
9397
9398 /* Set the output_section field so that lang_add_section
9399 does not create a lang_input_section structure for this
9400 section. Since there might be a symbol in the section
9401 being discarded, we must retain a pointer to the section
9402 which we are really going to use. */
9403 sec->output_section = bfd_abs_section_ptr;
9404 sec->kept_section = l->sec;
9405
9406 if (flags & SEC_GROUP)
3d7f7666
L
9407 {
9408 asection *first = elf_next_in_group (sec);
9409 asection *s = first;
9410
9411 while (s != NULL)
9412 {
9413 s->output_section = bfd_abs_section_ptr;
9414 /* Record which group discards it. */
9415 s->kept_section = l->sec;
9416 s = elf_next_in_group (s);
9417 /* These lists are circular. */
9418 if (s == first)
9419 break;
9420 }
9421 }
082b7297
L
9422
9423 return;
9424 }
9425 }
9426
3d7f7666
L
9427 if (group)
9428 {
9429 /* If this is the member of a single member comdat group and the
9430 group hasn't be discarded, we check if it matches a linkonce
9431 section. We only record the discarded comdat group. Otherwise
9432 the undiscarded group will be discarded incorrectly later since
9433 itself has been recorded. */
9434 if (! already_linked (elf_next_in_group (sec), group))
9435 return;
9436 }
9437 else
9438 /* There is no direct match. But for linkonce section, we should
9439 check if there is a match with comdat group member. We always
9440 record the linkonce section, discarded or not. */
9441 already_linked (sec, group);
9442
082b7297
L
9443 /* This is the first section with this name. Record it. */
9444 bfd_section_already_linked_table_insert (already_linked_list, sec);
9445}