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