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