2004-02-09 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / bfd / elflink.h
CommitLineData
252b5132 1/* ELF linker support.
68bfbfcc 2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
7898deda 3 Free Software Foundation, Inc.
252b5132 4
ae9a127f 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
ae9a127f
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
ae9a127f
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
ae9a127f
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/* ELF linker code. */
22
29ca8dc5
NS
23#include "safe-ctype.h"
24
268b6b39
AM
25static bfd_boolean elf_link_add_object_symbols (bfd *, struct bfd_link_info *);
26static bfd_boolean elf_link_add_archive_symbols (bfd *,
27 struct bfd_link_info *);
28static bfd_boolean elf_finalize_dynstr (bfd *, struct bfd_link_info *);
29static bfd_boolean elf_collect_hash_codes (struct elf_link_hash_entry *,
30 void *);
31static bfd_boolean elf_section_ignore_discarded_relocs (asection *);
252b5132
RH
32
33/* Given an ELF BFD, add symbols to the global hash table as
34 appropriate. */
35
b34976b6 36bfd_boolean
268b6b39 37elf_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
38{
39 switch (bfd_get_format (abfd))
40 {
41 case bfd_object:
42 return elf_link_add_object_symbols (abfd, info);
43 case bfd_archive:
44 return elf_link_add_archive_symbols (abfd, info);
45 default:
46 bfd_set_error (bfd_error_wrong_format);
b34976b6 47 return FALSE;
252b5132
RH
48 }
49}
50\f
b34976b6
AM
51/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
52static bfd_boolean
268b6b39
AM
53is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
54 Elf_Internal_Sym *sym)
48dfb430
NC
55{
56 /* Local symbols do not count, but target specific ones might. */
57 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
58 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
b34976b6 59 return FALSE;
48dfb430 60
7da9d88f
NC
61 /* Function symbols do not count. */
62 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
b34976b6 63 return FALSE;
7da9d88f 64
48dfb430
NC
65 /* If the section is undefined, then so is the symbol. */
66 if (sym->st_shndx == SHN_UNDEF)
b34976b6 67 return FALSE;
3e932841 68
48dfb430
NC
69 /* If the symbol is defined in the common section, then
70 it is a common definition and so does not count. */
71 if (sym->st_shndx == SHN_COMMON)
b34976b6 72 return FALSE;
48dfb430
NC
73
74 /* If the symbol is in a target specific section then we
75 must rely upon the backend to tell us what it is. */
76 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
77 /* FIXME - this function is not coded yet:
3e932841 78
48dfb430 79 return _bfd_is_global_symbol_definition (abfd, sym);
3e932841 80
48dfb430
NC
81 Instead for now assume that the definition is not global,
82 Even if this is wrong, at least the linker will behave
83 in the same way that it used to do. */
b34976b6 84 return FALSE;
3e932841 85
b34976b6 86 return TRUE;
48dfb430
NC
87}
88
a3a8c91d 89/* Search the symbol table of the archive element of the archive ABFD
4e8a9624 90 whose archive map contains a mention of SYMDEF, and determine if
a3a8c91d 91 the symbol is defined in this element. */
b34976b6 92static bfd_boolean
268b6b39 93elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
a3a8c91d
NC
94{
95 Elf_Internal_Shdr * hdr;
dc810e39
AM
96 bfd_size_type symcount;
97 bfd_size_type extsymcount;
98 bfd_size_type extsymoff;
6cdc0ccc
AM
99 Elf_Internal_Sym *isymbuf;
100 Elf_Internal_Sym *isym;
101 Elf_Internal_Sym *isymend;
b34976b6 102 bfd_boolean result;
3e932841 103
a3a8c91d 104 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
268b6b39 105 if (abfd == NULL)
b34976b6 106 return FALSE;
a3a8c91d
NC
107
108 if (! bfd_check_format (abfd, bfd_object))
b34976b6 109 return FALSE;
a3a8c91d 110
48dfb430
NC
111 /* If we have already included the element containing this symbol in the
112 link then we do not need to include it again. Just claim that any symbol
113 it contains is not a definition, so that our caller will not decide to
114 (re)include this element. */
115 if (abfd->archive_pass)
b34976b6 116 return FALSE;
3e932841 117
a3a8c91d
NC
118 /* Select the appropriate symbol table. */
119 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
6cdc0ccc 120 hdr = &elf_tdata (abfd)->symtab_hdr;
a3a8c91d 121 else
6cdc0ccc 122 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
a3a8c91d
NC
123
124 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
125
126 /* The sh_info field of the symtab header tells us where the
127 external symbols start. We don't care about the local symbols. */
128 if (elf_bad_symtab (abfd))
129 {
130 extsymcount = symcount;
131 extsymoff = 0;
132 }
133 else
134 {
135 extsymcount = symcount - hdr->sh_info;
136 extsymoff = hdr->sh_info;
137 }
138
6cdc0ccc 139 if (extsymcount == 0)
b34976b6 140 return FALSE;
a3a8c91d 141
6cdc0ccc
AM
142 /* Read in the symbol table. */
143 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
144 NULL, NULL, NULL);
145 if (isymbuf == NULL)
b34976b6 146 return FALSE;
a3a8c91d
NC
147
148 /* Scan the symbol table looking for SYMDEF. */
b34976b6 149 result = FALSE;
6cdc0ccc 150 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
a3a8c91d 151 {
6cdc0ccc 152 const char *name;
a3a8c91d 153
6cdc0ccc
AM
154 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
155 isym->st_name);
268b6b39 156 if (name == NULL)
a3a8c91d
NC
157 break;
158
159 if (strcmp (name, symdef->name) == 0)
160 {
6cdc0ccc 161 result = is_global_data_symbol_definition (abfd, isym);
a3a8c91d
NC
162 break;
163 }
164 }
165
6cdc0ccc 166 free (isymbuf);
3e932841 167
a3a8c91d
NC
168 return result;
169}
170\f
252b5132
RH
171/* Add symbols from an ELF archive file to the linker hash table. We
172 don't use _bfd_generic_link_add_archive_symbols because of a
173 problem which arises on UnixWare. The UnixWare libc.so is an
174 archive which includes an entry libc.so.1 which defines a bunch of
175 symbols. The libc.so archive also includes a number of other
176 object files, which also define symbols, some of which are the same
177 as those defined in libc.so.1. Correct linking requires that we
178 consider each object file in turn, and include it if it defines any
179 symbols we need. _bfd_generic_link_add_archive_symbols does not do
180 this; it looks through the list of undefined symbols, and includes
181 any object file which defines them. When this algorithm is used on
182 UnixWare, it winds up pulling in libc.so.1 early and defining a
183 bunch of symbols. This means that some of the other objects in the
184 archive are not included in the link, which is incorrect since they
185 precede libc.so.1 in the archive.
186
187 Fortunately, ELF archive handling is simpler than that done by
188 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
189 oddities. In ELF, if we find a symbol in the archive map, and the
190 symbol is currently undefined, we know that we must pull in that
191 object file.
192
193 Unfortunately, we do have to make multiple passes over the symbol
194 table until nothing further is resolved. */
195
b34976b6 196static bfd_boolean
268b6b39 197elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
198{
199 symindex c;
b34976b6
AM
200 bfd_boolean *defined = NULL;
201 bfd_boolean *included = NULL;
252b5132 202 carsym *symdefs;
b34976b6 203 bfd_boolean loop;
dc810e39 204 bfd_size_type amt;
252b5132
RH
205
206 if (! bfd_has_map (abfd))
207 {
208 /* An empty archive is a special case. */
268b6b39 209 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
b34976b6 210 return TRUE;
252b5132 211 bfd_set_error (bfd_error_no_armap);
b34976b6 212 return FALSE;
252b5132
RH
213 }
214
215 /* Keep track of all symbols we know to be already defined, and all
216 files we know to be already included. This is to speed up the
217 second and subsequent passes. */
218 c = bfd_ardata (abfd)->symdef_count;
219 if (c == 0)
b34976b6 220 return TRUE;
dc810e39 221 amt = c;
b34976b6 222 amt *= sizeof (bfd_boolean);
268b6b39
AM
223 defined = bfd_zmalloc (amt);
224 included = bfd_zmalloc (amt);
225 if (defined == NULL || included == NULL)
252b5132 226 goto error_return;
252b5132
RH
227
228 symdefs = bfd_ardata (abfd)->symdefs;
229
230 do
231 {
232 file_ptr last;
233 symindex i;
234 carsym *symdef;
235 carsym *symdefend;
236
b34976b6 237 loop = FALSE;
252b5132
RH
238 last = -1;
239
240 symdef = symdefs;
241 symdefend = symdef + c;
242 for (i = 0; symdef < symdefend; symdef++, i++)
243 {
244 struct elf_link_hash_entry *h;
245 bfd *element;
246 struct bfd_link_hash_entry *undefs_tail;
247 symindex mark;
248
249 if (defined[i] || included[i])
250 continue;
251 if (symdef->file_offset == last)
252 {
b34976b6 253 included[i] = TRUE;
252b5132
RH
254 continue;
255 }
256
257 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
b34976b6 258 FALSE, FALSE, FALSE);
252b5132
RH
259
260 if (h == NULL)
261 {
262 char *p, *copy;
48fc70a2 263 size_t len, first;
252b5132
RH
264
265 /* If this is a default version (the name contains @@),
48fc70a2
AM
266 look up the symbol again with only one `@' as well
267 as without the version. The effect is that references
268 to the symbol with and without the version will be
269 matched by the default symbol in the archive. */
252b5132
RH
270
271 p = strchr (symdef->name, ELF_VER_CHR);
272 if (p == NULL || p[1] != ELF_VER_CHR)
273 continue;
274
48fc70a2
AM
275 /* First check with only one `@'. */
276 len = strlen (symdef->name);
268b6b39 277 copy = bfd_alloc (abfd, len);
252b5132
RH
278 if (copy == NULL)
279 goto error_return;
48fc70a2
AM
280 first = p - symdef->name + 1;
281 memcpy (copy, symdef->name, first);
282 memcpy (copy + first, symdef->name + first + 1, len - first);
252b5132
RH
283
284 h = elf_link_hash_lookup (elf_hash_table (info), copy,
b34976b6 285 FALSE, FALSE, FALSE);
252b5132 286
48fc70a2 287 if (h == NULL)
58821868 288 {
48fc70a2
AM
289 /* We also need to check references to the symbol
290 without the version. */
291
292 copy[first - 1] = '\0';
293 h = elf_link_hash_lookup (elf_hash_table (info),
b34976b6 294 copy, FALSE, FALSE, FALSE);
48fc70a2
AM
295 }
296
252b5132
RH
297 bfd_release (abfd, copy);
298 }
299
300 if (h == NULL)
301 continue;
302
a3a8c91d
NC
303 if (h->root.type == bfd_link_hash_common)
304 {
305 /* We currently have a common symbol. The archive map contains
306 a reference to this symbol, so we may want to include it. We
307 only want to include it however, if this archive element
308 contains a definition of the symbol, not just another common
309 declaration of it.
310
311 Unfortunately some archivers (including GNU ar) will put
312 declarations of common symbols into their archive maps, as
313 well as real definitions, so we cannot just go by the archive
314 map alone. Instead we must read in the element's symbol
315 table and check that to see what kind of symbol definition
316 this is. */
317 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
318 continue;
319 }
320 else if (h->root.type != bfd_link_hash_undefined)
252b5132
RH
321 {
322 if (h->root.type != bfd_link_hash_undefweak)
b34976b6 323 defined[i] = TRUE;
252b5132
RH
324 continue;
325 }
326
327 /* We need to include this archive member. */
252b5132 328 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
268b6b39 329 if (element == NULL)
252b5132
RH
330 goto error_return;
331
332 if (! bfd_check_format (element, bfd_object))
333 goto error_return;
334
335 /* Doublecheck that we have not included this object
336 already--it should be impossible, but there may be
337 something wrong with the archive. */
338 if (element->archive_pass != 0)
339 {
340 bfd_set_error (bfd_error_bad_value);
341 goto error_return;
342 }
343 element->archive_pass = 1;
344
345 undefs_tail = info->hash->undefs_tail;
346
347 if (! (*info->callbacks->add_archive_element) (info, element,
348 symdef->name))
349 goto error_return;
350 if (! elf_link_add_object_symbols (element, info))
351 goto error_return;
352
353 /* If there are any new undefined symbols, we need to make
354 another pass through the archive in order to see whether
355 they can be defined. FIXME: This isn't perfect, because
356 common symbols wind up on undefs_tail and because an
357 undefined symbol which is defined later on in this pass
358 does not require another pass. This isn't a bug, but it
359 does make the code less efficient than it could be. */
360 if (undefs_tail != info->hash->undefs_tail)
b34976b6 361 loop = TRUE;
252b5132
RH
362
363 /* Look backward to mark all symbols from this object file
364 which we have already seen in this pass. */
365 mark = i;
366 do
367 {
b34976b6 368 included[mark] = TRUE;
252b5132
RH
369 if (mark == 0)
370 break;
371 --mark;
372 }
373 while (symdefs[mark].file_offset == symdef->file_offset);
374
375 /* We mark subsequent symbols from this object file as we go
376 on through the loop. */
377 last = symdef->file_offset;
378 }
379 }
380 while (loop);
381
382 free (defined);
383 free (included);
384
b34976b6 385 return TRUE;
252b5132
RH
386
387 error_return:
268b6b39 388 if (defined != NULL)
252b5132 389 free (defined);
268b6b39 390 if (included != NULL)
252b5132 391 free (included);
b34976b6 392 return FALSE;
252b5132
RH
393}
394
45d6a902 395/* Add symbols from an ELF object file to the linker hash table. */
252b5132 396
b34976b6 397static bfd_boolean
268b6b39 398elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
252b5132 399{
45d6a902 400 bfd_boolean (*add_symbol_hook)
268b6b39
AM
401 (bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
402 const char **, flagword *, asection **, bfd_vma *);
45d6a902 403 bfd_boolean (*check_relocs)
268b6b39 404 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
45d6a902
AM
405 bfd_boolean collect;
406 Elf_Internal_Shdr *hdr;
407 bfd_size_type symcount;
408 bfd_size_type extsymcount;
409 bfd_size_type extsymoff;
410 struct elf_link_hash_entry **sym_hash;
411 bfd_boolean dynamic;
412 Elf_External_Versym *extversym = NULL;
413 Elf_External_Versym *ever;
414 struct elf_link_hash_entry *weaks;
415 struct elf_link_hash_entry **nondeflt_vers = NULL;
416 bfd_size_type nondeflt_vers_cnt = 0;
417 Elf_Internal_Sym *isymbuf = NULL;
418 Elf_Internal_Sym *isym;
419 Elf_Internal_Sym *isymend;
9c5bfbb7 420 const struct elf_backend_data *bed;
45d6a902
AM
421 bfd_boolean dt_needed;
422 struct elf_link_hash_table * hash_table;
423 bfd_size_type amt;
252b5132 424
45d6a902 425 hash_table = elf_hash_table (info);
252b5132 426
45d6a902
AM
427 bed = get_elf_backend_data (abfd);
428 add_symbol_hook = bed->elf_add_symbol_hook;
429 collect = bed->collect;
252b5132 430
45d6a902
AM
431 if ((abfd->flags & DYNAMIC) == 0)
432 dynamic = FALSE;
252b5132 433 else
252b5132 434 {
45d6a902 435 dynamic = TRUE;
252b5132 436
45d6a902
AM
437 /* You can't use -r against a dynamic object. Also, there's no
438 hope of using a dynamic object which does not exactly match
439 the format of the output file. */
0eddce27
AM
440 if (info->relocatable
441 || !is_elf_hash_table (hash_table)
442 || hash_table->root.creator != abfd->xvec)
45d6a902
AM
443 {
444 bfd_set_error (bfd_error_invalid_operation);
445 goto error_return;
446 }
252b5132
RH
447 }
448
45d6a902
AM
449 /* As a GNU extension, any input sections which are named
450 .gnu.warning.SYMBOL are treated as warning symbols for the given
451 symbol. This differs from .gnu.warning sections, which generate
452 warnings when they are included in an output file. */
36af4a4e 453 if (info->executable)
45d6a902
AM
454 {
455 asection *s;
b4536acd 456
45d6a902
AM
457 for (s = abfd->sections; s != NULL; s = s->next)
458 {
459 const char *name;
252b5132 460
45d6a902
AM
461 name = bfd_get_section_name (abfd, s);
462 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
463 {
464 char *msg;
465 bfd_size_type sz;
8c675694
NC
466 bfd_size_type prefix_len;
467 const char * gnu_warning_prefix = _("warning: ");
252b5132 468
45d6a902 469 name += sizeof ".gnu.warning." - 1;
0035bd7b 470
45d6a902
AM
471 /* If this is a shared object, then look up the symbol
472 in the hash table. If it is there, and it is already
473 been defined, then we will not be using the entry
474 from this shared object, so we don't need to warn.
475 FIXME: If we see the definition in a regular object
476 later on, we will warn, but we shouldn't. The only
477 fix is to keep track of what warnings we are supposed
478 to emit, and then handle them all at the end of the
479 link. */
0eddce27 480 if (dynamic)
45d6a902
AM
481 {
482 struct elf_link_hash_entry *h;
0035bd7b 483
45d6a902
AM
484 h = elf_link_hash_lookup (hash_table, name,
485 FALSE, FALSE, TRUE);
0035bd7b 486
45d6a902
AM
487 /* FIXME: What about bfd_link_hash_common? */
488 if (h != NULL
489 && (h->root.type == bfd_link_hash_defined
490 || h->root.type == bfd_link_hash_defweak))
491 {
492 /* We don't want to issue this warning. Clobber
493 the section size so that the warning does not
494 get copied into the output file. */
495 s->_raw_size = 0;
496 continue;
497 }
498 }
0035bd7b 499
45d6a902 500 sz = bfd_section_size (abfd, s);
8c675694
NC
501 prefix_len = strlen (gnu_warning_prefix);
502 msg = bfd_alloc (abfd, prefix_len + sz + 1);
45d6a902
AM
503 if (msg == NULL)
504 goto error_return;
252b5132 505
8c675694
NC
506 strcpy (msg, gnu_warning_prefix);
507 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
45d6a902 508 goto error_return;
252b5132 509
8c675694 510 msg[prefix_len + sz] = '\0';
252b5132 511
45d6a902 512 if (! (_bfd_generic_link_add_one_symbol
268b6b39
AM
513 (info, abfd, name, BSF_WARNING, s, 0, msg,
514 FALSE, collect, NULL)))
45d6a902 515 goto error_return;
252b5132 516
1049f94e 517 if (! info->relocatable)
45d6a902
AM
518 {
519 /* Clobber the section size so that the warning does
520 not get copied into the output file. */
521 s->_raw_size = 0;
522 }
1b1fe8fe 523 }
d31e3dfe 524 }
1b1fe8fe
L
525 }
526
45d6a902
AM
527 dt_needed = FALSE;
528 if (! dynamic)
c424e0e9 529 {
45d6a902
AM
530 /* If we are creating a shared library, create all the dynamic
531 sections immediately. We need to attach them to something,
532 so we attach them to this BFD, provided it is the right
533 format. FIXME: If there are no input BFD's of the same
534 format as the output, we can't make a shared library. */
535 if (info->shared
0eddce27
AM
536 && is_elf_hash_table (hash_table)
537 && hash_table->root.creator == abfd->xvec
538 && ! hash_table->dynamic_sections_created)
c424e0e9 539 {
45d6a902
AM
540 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
541 goto error_return;
c424e0e9
L
542 }
543 }
0eddce27 544 else if (!is_elf_hash_table (hash_table))
45d6a902 545 goto error_return;
c424e0e9 546 else
c424e0e9 547 {
45d6a902
AM
548 asection *s;
549 bfd_boolean add_needed;
550 const char *name;
551 bfd_size_type oldsize;
552 bfd_size_type strindex;
553 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
252b5132 554
45d6a902
AM
555 /* ld --just-symbols and dynamic objects don't mix very well.
556 Test for --just-symbols by looking at info set up by
557 _bfd_elf_link_just_syms. */
558 if ((s = abfd->sections) != NULL
559 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
560 goto error_return;
252b5132 561
45d6a902
AM
562 /* Find the name to use in a DT_NEEDED entry that refers to this
563 object. If the object has a DT_SONAME entry, we use it.
564 Otherwise, if the generic linker stuck something in
565 elf_dt_name, we use that. Otherwise, we just use the file
566 name. If the generic linker put a null string into
567 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
568 there is a DT_SONAME entry. */
569 add_needed = TRUE;
570 name = bfd_get_filename (abfd);
571 if (elf_dt_name (abfd) != NULL)
572 {
573 name = elf_dt_name (abfd);
574 if (*name == '\0')
575 {
576 if (elf_dt_soname (abfd) != NULL)
577 dt_needed = TRUE;
252b5132 578
45d6a902
AM
579 add_needed = FALSE;
580 }
581 }
582 s = bfd_get_section_by_name (abfd, ".dynamic");
583 if (s != NULL)
584 {
585 Elf_External_Dyn *dynbuf = NULL;
586 Elf_External_Dyn *extdyn;
587 Elf_External_Dyn *extdynend;
588 int elfsec;
589 unsigned long shlink;
252b5132 590
268b6b39 591 dynbuf = bfd_malloc (s->_raw_size);
45d6a902
AM
592 if (dynbuf == NULL)
593 goto error_return;
252b5132 594
268b6b39 595 if (! bfd_get_section_contents (abfd, s, dynbuf, 0, s->_raw_size))
45d6a902 596 goto error_free_dyn;
252b5132 597
45d6a902
AM
598 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
599 if (elfsec == -1)
600 goto error_free_dyn;
601 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132 602
45d6a902
AM
603 extdyn = dynbuf;
604 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
605 for (; extdyn < extdynend; extdyn++)
606 {
607 Elf_Internal_Dyn dyn;
252b5132 608
45d6a902
AM
609 elf_swap_dyn_in (abfd, extdyn, &dyn);
610 if (dyn.d_tag == DT_SONAME)
611 {
612 unsigned int tagv = dyn.d_un.d_val;
613 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
614 if (name == NULL)
615 goto error_free_dyn;
616 }
617 if (dyn.d_tag == DT_NEEDED)
618 {
619 struct bfd_link_needed_list *n, **pn;
620 char *fnm, *anm;
621 unsigned int tagv = dyn.d_un.d_val;
252b5132 622
45d6a902 623 amt = sizeof (struct bfd_link_needed_list);
268b6b39 624 n = bfd_alloc (abfd, amt);
45d6a902
AM
625 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
626 if (n == NULL || fnm == NULL)
627 goto error_free_dyn;
628 amt = strlen (fnm) + 1;
629 anm = bfd_alloc (abfd, amt);
630 if (anm == NULL)
631 goto error_free_dyn;
268b6b39 632 memcpy (anm, fnm, amt);
45d6a902
AM
633 n->name = anm;
634 n->by = abfd;
635 n->next = NULL;
636 for (pn = & hash_table->needed;
637 *pn != NULL;
638 pn = &(*pn)->next)
639 ;
640 *pn = n;
641 }
642 if (dyn.d_tag == DT_RUNPATH)
643 {
644 struct bfd_link_needed_list *n, **pn;
645 char *fnm, *anm;
646 unsigned int tagv = dyn.d_un.d_val;
252b5132 647
45d6a902 648 amt = sizeof (struct bfd_link_needed_list);
268b6b39 649 n = bfd_alloc (abfd, amt);
45d6a902
AM
650 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
651 if (n == NULL || fnm == NULL)
652 goto error_free_dyn;
653 amt = strlen (fnm) + 1;
654 anm = bfd_alloc (abfd, amt);
655 if (anm == NULL)
656 goto error_free_dyn;
268b6b39 657 memcpy (anm, fnm, amt);
45d6a902
AM
658 n->name = anm;
659 n->by = abfd;
660 n->next = NULL;
661 for (pn = & runpath;
662 *pn != NULL;
663 pn = &(*pn)->next)
664 ;
665 *pn = n;
666 }
667 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
668 if (!runpath && dyn.d_tag == DT_RPATH)
669 {
670 struct bfd_link_needed_list *n, **pn;
671 char *fnm, *anm;
672 unsigned int tagv = dyn.d_un.d_val;
252b5132 673
45d6a902 674 amt = sizeof (struct bfd_link_needed_list);
268b6b39 675 n = bfd_alloc (abfd, amt);
45d6a902
AM
676 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
677 if (n == NULL || fnm == NULL)
678 goto error_free_dyn;
679 amt = strlen (fnm) + 1;
680 anm = bfd_alloc (abfd, amt);
681 if (anm == NULL)
682 {
683 error_free_dyn:
684 free (dynbuf);
685 goto error_return;
686 }
268b6b39 687 memcpy (anm, fnm, amt);
45d6a902
AM
688 n->name = anm;
689 n->by = abfd;
690 n->next = NULL;
691 for (pn = & rpath;
692 *pn != NULL;
693 pn = &(*pn)->next)
694 ;
695 *pn = n;
696 }
697 }
252b5132 698
45d6a902
AM
699 free (dynbuf);
700 }
252b5132 701
45d6a902
AM
702 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
703 frees all more recently bfd_alloc'd blocks as well. */
704 if (runpath)
705 rpath = runpath;
3c0a515d 706
45d6a902 707 if (rpath)
3c0a515d 708 {
45d6a902
AM
709 struct bfd_link_needed_list **pn;
710 for (pn = & hash_table->runpath;
711 *pn != NULL;
712 pn = &(*pn)->next)
713 ;
714 *pn = rpath;
3c0a515d 715 }
252b5132 716
45d6a902
AM
717 /* We do not want to include any of the sections in a dynamic
718 object in the output file. We hack by simply clobbering the
719 list of sections in the BFD. This could be handled more
720 cleanly by, say, a new section flag; the existing
721 SEC_NEVER_LOAD flag is not the one we want, because that one
722 still implies that the section takes up space in the output
723 file. */
724 bfd_section_list_clear (abfd);
0525d26e 725
45d6a902
AM
726 /* If this is the first dynamic object found in the link, create
727 the special sections required for dynamic linking. */
0eddce27
AM
728 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
729 goto error_return;
0525d26e 730
45d6a902
AM
731 if (add_needed)
732 {
733 /* Add a DT_NEEDED entry for this dynamic object. */
734 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
735 strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, FALSE);
736 if (strindex == (bfd_size_type) -1)
737 goto error_return;
252b5132 738
45d6a902
AM
739 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
740 {
741 asection *sdyn;
742 Elf_External_Dyn *dyncon, *dynconend;
215007a6 743
45d6a902
AM
744 /* The hash table size did not change, which means that
745 the dynamic object name was already entered. If we
746 have already included this dynamic object in the
747 link, just ignore it. There is no reason to include
748 a particular dynamic object more than once. */
749 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
750 BFD_ASSERT (sdyn != NULL);
215007a6 751
45d6a902
AM
752 dyncon = (Elf_External_Dyn *) sdyn->contents;
753 dynconend = (Elf_External_Dyn *) (sdyn->contents +
754 sdyn->_raw_size);
755 for (; dyncon < dynconend; dyncon++)
756 {
757 Elf_Internal_Dyn dyn;
215007a6 758
45d6a902
AM
759 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
760 if (dyn.d_tag == DT_NEEDED
761 && dyn.d_un.d_val == strindex)
762 {
763 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
764 return TRUE;
765 }
766 }
767 }
768
268b6b39 769 if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
45d6a902 770 goto error_return;
215007a6 771 }
45d6a902
AM
772
773 /* Save the SONAME, if there is one, because sometimes the
774 linker emulation code will need to know it. */
775 if (*name == '\0')
776 name = basename (bfd_get_filename (abfd));
777 elf_dt_name (abfd) = name;
215007a6
L
778 }
779
45d6a902
AM
780 /* If this is a dynamic object, we always link against the .dynsym
781 symbol table, not the .symtab symbol table. The dynamic linker
782 will only see the .dynsym symbol table, so there is no reason to
783 look at .symtab for a dynamic object. */
215007a6 784
45d6a902
AM
785 if (! dynamic || elf_dynsymtab (abfd) == 0)
786 hdr = &elf_tdata (abfd)->symtab_hdr;
787 else
788 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
215007a6 789
45d6a902 790 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
d31e3dfe 791
45d6a902
AM
792 /* The sh_info field of the symtab header tells us where the
793 external symbols start. We don't care about the local symbols at
794 this point. */
795 if (elf_bad_symtab (abfd))
215007a6 796 {
45d6a902
AM
797 extsymcount = symcount;
798 extsymoff = 0;
215007a6
L
799 }
800 else
801 {
45d6a902
AM
802 extsymcount = symcount - hdr->sh_info;
803 extsymoff = hdr->sh_info;
215007a6
L
804 }
805
45d6a902
AM
806 sym_hash = NULL;
807 if (extsymcount != 0)
215007a6 808 {
45d6a902
AM
809 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
810 NULL, NULL, NULL);
811 if (isymbuf == NULL)
812 goto error_return;
215007a6 813
45d6a902
AM
814 /* We store a pointer to the hash table entry for each external
815 symbol. */
816 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
268b6b39 817 sym_hash = bfd_alloc (abfd, amt);
45d6a902
AM
818 if (sym_hash == NULL)
819 goto error_free_sym;
820 elf_sym_hashes (abfd) = sym_hash;
821 }
215007a6 822
45d6a902
AM
823 if (dynamic)
824 {
825 /* Read in any version definitions. */
826 if (! _bfd_elf_slurp_version_tables (abfd))
827 goto error_free_sym;
215007a6 828
45d6a902
AM
829 /* Read in the symbol versions, but don't bother to convert them
830 to internal format. */
831 if (elf_dynversym (abfd) != 0)
215007a6 832 {
45d6a902
AM
833 Elf_Internal_Shdr *versymhdr;
834
835 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
268b6b39 836 extversym = bfd_malloc (versymhdr->sh_size);
45d6a902
AM
837 if (extversym == NULL)
838 goto error_free_sym;
839 amt = versymhdr->sh_size;
840 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
268b6b39 841 || bfd_bread (extversym, amt, abfd) != amt)
45d6a902 842 goto error_free_vers;
215007a6
L
843 }
844 }
845
45d6a902 846 weaks = NULL;
215007a6 847
45d6a902
AM
848 ever = extversym != NULL ? extversym + extsymoff : NULL;
849 for (isym = isymbuf, isymend = isymbuf + extsymcount;
850 isym < isymend;
851 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
852 {
853 int bind;
854 bfd_vma value;
855 asection *sec;
856 flagword flags;
857 const char *name;
858 struct elf_link_hash_entry *h;
859 bfd_boolean definition;
860 bfd_boolean size_change_ok;
861 bfd_boolean type_change_ok;
862 bfd_boolean new_weakdef;
863 bfd_boolean override;
864 unsigned int old_alignment;
865 bfd *old_bfd;
215007a6 866
45d6a902 867 override = FALSE;
d31e3dfe 868
45d6a902
AM
869 flags = BSF_NO_FLAGS;
870 sec = NULL;
871 value = isym->st_value;
872 *sym_hash = NULL;
215007a6 873
45d6a902
AM
874 bind = ELF_ST_BIND (isym->st_info);
875 if (bind == STB_LOCAL)
876 {
877 /* This should be impossible, since ELF requires that all
878 global symbols follow all local symbols, and that sh_info
4cc11e76 879 point to the first global symbol. Unfortunately, Irix 5
45d6a902
AM
880 screws this up. */
881 continue;
882 }
883 else if (bind == STB_GLOBAL)
884 {
885 if (isym->st_shndx != SHN_UNDEF
886 && isym->st_shndx != SHN_COMMON)
887 flags = BSF_GLOBAL;
888 }
889 else if (bind == STB_WEAK)
890 flags = BSF_WEAK;
891 else
892 {
893 /* Leave it up to the processor backend. */
894 }
215007a6 895
45d6a902
AM
896 if (isym->st_shndx == SHN_UNDEF)
897 sec = bfd_und_section_ptr;
898 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
899 {
900 sec = section_from_elf_index (abfd, isym->st_shndx);
901 if (sec == NULL)
902 sec = bfd_abs_section_ptr;
903 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
904 value -= sec->vma;
905 }
906 else if (isym->st_shndx == SHN_ABS)
907 sec = bfd_abs_section_ptr;
908 else if (isym->st_shndx == SHN_COMMON)
909 {
910 sec = bfd_com_section_ptr;
911 /* What ELF calls the size we call the value. What ELF
912 calls the value we call the alignment. */
913 value = isym->st_size;
914 }
915 else
215007a6 916 {
45d6a902
AM
917 /* Leave it up to the processor backend. */
918 }
919
920 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
921 isym->st_name);
268b6b39 922 if (name == NULL)
45d6a902 923 goto error_free_vers;
215007a6 924
45d6a902
AM
925 if (isym->st_shndx == SHN_COMMON
926 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
927 {
928 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
215007a6 929
45d6a902 930 if (tcomm == NULL)
215007a6 931 {
45d6a902
AM
932 tcomm = bfd_make_section (abfd, ".tcommon");
933 if (tcomm == NULL
934 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
935 | SEC_IS_COMMON
936 | SEC_LINKER_CREATED
937 | SEC_THREAD_LOCAL)))
938 goto error_free_vers;
215007a6 939 }
45d6a902 940 sec = tcomm;
215007a6 941 }
45d6a902
AM
942 else if (add_symbol_hook)
943 {
944 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
945 &value))
946 goto error_free_vers;
252b5132 947
45d6a902
AM
948 /* The hook function sets the name to NULL if this symbol
949 should be skipped for some reason. */
268b6b39 950 if (name == NULL)
45d6a902
AM
951 continue;
952 }
252b5132 953
45d6a902 954 /* Sanity check that all possibilities were handled. */
268b6b39 955 if (sec == NULL)
252b5132 956 {
45d6a902
AM
957 bfd_set_error (bfd_error_bad_value);
958 goto error_free_vers;
252b5132 959 }
252b5132 960
45d6a902
AM
961 if (bfd_is_und_section (sec)
962 || bfd_is_com_section (sec))
963 definition = FALSE;
964 else
965 definition = TRUE;
252b5132 966
45d6a902
AM
967 size_change_ok = FALSE;
968 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
969 old_alignment = 0;
970 old_bfd = NULL;
971
0eddce27 972 if (is_elf_hash_table (hash_table))
252b5132 973 {
45d6a902
AM
974 Elf_Internal_Versym iver;
975 unsigned int vernum = 0;
976 bfd_boolean skip;
252b5132 977
45d6a902 978 if (ever != NULL)
252b5132 979 {
45d6a902
AM
980 _bfd_elf_swap_versym_in (abfd, ever, &iver);
981 vernum = iver.vs_vers & VERSYM_VERSION;
252b5132 982
45d6a902
AM
983 /* If this is a hidden symbol, or if it is not version
984 1, we append the version name to the symbol name.
985 However, we do not modify a non-hidden absolute
986 symbol, because it might be the version symbol
987 itself. FIXME: What if it isn't? */
988 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
989 || (vernum > 1 && ! bfd_is_abs_section (sec)))
252b5132 990 {
45d6a902
AM
991 const char *verstr;
992 size_t namelen, verlen, newlen;
993 char *newname, *p;
252b5132 994
45d6a902 995 if (isym->st_shndx != SHN_UNDEF)
252b5132 996 {
45d6a902
AM
997 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
998 {
999 (*_bfd_error_handler)
1000 (_("%s: %s: invalid version %u (max %d)"),
1001 bfd_archive_filename (abfd), name, vernum,
1002 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1003 bfd_set_error (bfd_error_bad_value);
1004 goto error_free_vers;
1005 }
1006 else if (vernum > 1)
1007 verstr =
1008 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1009 else
1010 verstr = "";
6cdc0ccc 1011 }
45d6a902
AM
1012 else
1013 {
1014 /* We cannot simply test for the number of
1015 entries in the VERNEED section since the
1016 numbers for the needed versions do not start
1017 at 0. */
1018 Elf_Internal_Verneed *t;
252b5132 1019
45d6a902
AM
1020 verstr = NULL;
1021 for (t = elf_tdata (abfd)->verref;
1022 t != NULL;
1023 t = t->vn_nextref)
1024 {
1025 Elf_Internal_Vernaux *a;
252b5132 1026
45d6a902
AM
1027 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1028 {
1029 if (a->vna_other == vernum)
1030 {
1031 verstr = a->vna_nodename;
1032 break;
1033 }
1034 }
1035 if (a != NULL)
1036 break;
1037 }
1038 if (verstr == NULL)
1039 {
1040 (*_bfd_error_handler)
1041 (_("%s: %s: invalid needed version %d"),
1042 bfd_archive_filename (abfd), name, vernum);
1043 bfd_set_error (bfd_error_bad_value);
1044 goto error_free_vers;
1045 }
1046 }
252b5132 1047
45d6a902
AM
1048 namelen = strlen (name);
1049 verlen = strlen (verstr);
1050 newlen = namelen + verlen + 2;
1051 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1052 && isym->st_shndx != SHN_UNDEF)
1053 ++newlen;
252b5132 1054
268b6b39 1055 newname = bfd_alloc (abfd, newlen);
45d6a902
AM
1056 if (newname == NULL)
1057 goto error_free_vers;
1058 memcpy (newname, name, namelen);
1059 p = newname + namelen;
1060 *p++ = ELF_VER_CHR;
1061 /* If this is a defined non-hidden version symbol,
1062 we add another @ to the name. This indicates the
1063 default version of the symbol. */
1064 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1065 && isym->st_shndx != SHN_UNDEF)
1066 *p++ = ELF_VER_CHR;
1067 memcpy (p, verstr, verlen + 1);
252b5132 1068
45d6a902
AM
1069 name = newname;
1070 }
1071 }
252b5132 1072
45d6a902
AM
1073 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
1074 sym_hash, &skip, &override,
1075 &type_change_ok, &size_change_ok,
1076 dt_needed))
1077 goto error_free_vers;
252b5132 1078
45d6a902
AM
1079 if (skip)
1080 continue;
252b5132 1081
45d6a902
AM
1082 if (override)
1083 definition = FALSE;
252b5132 1084
45d6a902
AM
1085 h = *sym_hash;
1086 while (h->root.type == bfd_link_hash_indirect
1087 || h->root.type == bfd_link_hash_warning)
1088 h = (struct elf_link_hash_entry *) h->root.u.i.link;
252b5132 1089
45d6a902
AM
1090 /* Remember the old alignment if this is a common symbol, so
1091 that we don't reduce the alignment later on. We can't
1092 check later, because _bfd_generic_link_add_one_symbol
1093 will set a default for the alignment which we want to
1094 override. We also remember the old bfd where the existing
1095 definition comes from. */
1096 switch (h->root.type)
1097 {
1098 default:
1099 break;
252b5132 1100
45d6a902
AM
1101 case bfd_link_hash_defined:
1102 case bfd_link_hash_defweak:
1103 old_bfd = h->root.u.def.section->owner;
1104 break;
1105
1106 case bfd_link_hash_common:
1107 old_bfd = h->root.u.c.p->section->owner;
1108 old_alignment = h->root.u.c.p->alignment_power;
1109 break;
1110 }
252b5132 1111
45d6a902
AM
1112 if (elf_tdata (abfd)->verdef != NULL
1113 && ! override
1114 && vernum > 1
1115 && definition)
1116 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1117 }
252b5132 1118
45d6a902 1119 if (! (_bfd_generic_link_add_one_symbol
268b6b39
AM
1120 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
1121 (struct bfd_link_hash_entry **) sym_hash)))
45d6a902 1122 goto error_free_vers;
252b5132 1123
45d6a902
AM
1124 h = *sym_hash;
1125 while (h->root.type == bfd_link_hash_indirect
1126 || h->root.type == bfd_link_hash_warning)
1127 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1128 *sym_hash = h;
252b5132 1129
45d6a902
AM
1130 new_weakdef = FALSE;
1131 if (dynamic
1132 && definition
1133 && (flags & BSF_WEAK) != 0
1134 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
0eddce27 1135 && is_elf_hash_table (hash_table)
45d6a902
AM
1136 && h->weakdef == NULL)
1137 {
1138 /* Keep a list of all weak defined non function symbols from
1139 a dynamic object, using the weakdef field. Later in this
1140 function we will set the weakdef field to the correct
1141 value. We only put non-function symbols from dynamic
1142 objects on this list, because that happens to be the only
1143 time we need to know the normal symbol corresponding to a
1144 weak symbol, and the information is time consuming to
1145 figure out. If the weakdef field is not already NULL,
1146 then this symbol was already defined by some previous
1147 dynamic object, and we will be using that previous
1148 definition anyhow. */
252b5132 1149
45d6a902
AM
1150 h->weakdef = weaks;
1151 weaks = h;
1152 new_weakdef = TRUE;
252b5132
RH
1153 }
1154
45d6a902
AM
1155 /* Set the alignment of a common symbol. */
1156 if (isym->st_shndx == SHN_COMMON
1157 && h->root.type == bfd_link_hash_common)
252b5132 1158 {
45d6a902
AM
1159 unsigned int align;
1160
1161 align = bfd_log2 (isym->st_value);
1162 if (align > old_alignment
1163 /* Permit an alignment power of zero if an alignment of one
1164 is specified and no other alignments have been specified. */
1165 || (isym->st_value == 1 && old_alignment == 0))
1166 h->root.u.c.p->alignment_power = align;
1167 else
1168 h->root.u.c.p->alignment_power = old_alignment;
252b5132 1169 }
252b5132 1170
0eddce27 1171 if (is_elf_hash_table (hash_table))
45d6a902
AM
1172 {
1173 int old_flags;
1174 bfd_boolean dynsym;
1175 int new_flag;
1176
1177 /* Check the alignment when a common symbol is involved. This
4cc11e76 1178 can change when a common symbol is overridden by a normal
45d6a902
AM
1179 definition or a common symbol is ignored due to the old
1180 normal definition. We need to make sure the maximum
1181 alignment is maintained. */
1182 if ((old_alignment || isym->st_shndx == SHN_COMMON)
1183 && h->root.type != bfd_link_hash_common)
1184 {
1185 unsigned int common_align;
1186 unsigned int normal_align;
1187 unsigned int symbol_align;
1188 bfd *normal_bfd;
1189 bfd *common_bfd;
1190
1191 symbol_align = ffs (h->root.u.def.value) - 1;
40a0491a
AM
1192 if (h->root.u.def.section->owner != NULL
1193 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
45d6a902
AM
1194 {
1195 normal_align = h->root.u.def.section->alignment_power;
1196 if (normal_align > symbol_align)
1197 normal_align = symbol_align;
1198 }
1199 else
1200 normal_align = symbol_align;
1201
1202 if (old_alignment)
1203 {
1204 common_align = old_alignment;
1205 common_bfd = old_bfd;
1206 normal_bfd = abfd;
1207 }
1208 else
1209 {
1210 common_align = bfd_log2 (isym->st_value);
1211 common_bfd = abfd;
1212 normal_bfd = old_bfd;
1213 }
1214
1215 if (normal_align < common_align)
1216 (*_bfd_error_handler)
1217 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
1218 1 << normal_align,
1219 name,
1220 bfd_archive_filename (normal_bfd),
1221 1 << common_align,
1222 bfd_archive_filename (common_bfd));
1223 }
252b5132 1224
45d6a902
AM
1225 /* Remember the symbol size and type. */
1226 if (isym->st_size != 0
1227 && (definition || h->size == 0))
1228 {
1229 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
1230 (*_bfd_error_handler)
1231 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
1232 name, (unsigned long) h->size,
1233 bfd_archive_filename (old_bfd),
1234 (unsigned long) isym->st_size,
1235 bfd_archive_filename (abfd));
252b5132 1236
45d6a902
AM
1237 h->size = isym->st_size;
1238 }
252b5132 1239
45d6a902
AM
1240 /* If this is a common symbol, then we always want H->SIZE
1241 to be the size of the common symbol. The code just above
1242 won't fix the size if a common symbol becomes larger. We
1243 don't warn about a size change here, because that is
1244 covered by --warn-common. */
1245 if (h->root.type == bfd_link_hash_common)
1246 h->size = h->root.u.c.size;
252b5132 1247
45d6a902
AM
1248 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
1249 && (definition || h->type == STT_NOTYPE))
1250 {
1251 if (h->type != STT_NOTYPE
1252 && h->type != ELF_ST_TYPE (isym->st_info)
1253 && ! type_change_ok)
1254 (*_bfd_error_handler)
1255 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1256 name, h->type, ELF_ST_TYPE (isym->st_info),
1257 bfd_archive_filename (abfd));
252b5132 1258
45d6a902
AM
1259 h->type = ELF_ST_TYPE (isym->st_info);
1260 }
252b5132 1261
45d6a902
AM
1262 /* If st_other has a processor-specific meaning, specific
1263 code might be needed here. We never merge the visibility
1264 attribute with the one from a dynamic object. */
9bf7216d
KK
1265 if (bed->elf_backend_merge_symbol_attribute)
1266 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1267 dynamic);
1268
45d6a902
AM
1269 if (isym->st_other != 0 && !dynamic)
1270 {
1271 unsigned char hvis, symvis, other, nvis;
252b5132 1272
45d6a902
AM
1273 /* Take the balance of OTHER from the definition. */
1274 other = (definition ? isym->st_other : h->other);
1275 other &= ~ ELF_ST_VISIBILITY (-1);
8ea2e4bd 1276
45d6a902
AM
1277 /* Combine visibilities, using the most constraining one. */
1278 hvis = ELF_ST_VISIBILITY (h->other);
1279 symvis = ELF_ST_VISIBILITY (isym->st_other);
1280 if (! hvis)
1281 nvis = symvis;
1282 else if (! symvis)
1283 nvis = hvis;
1284 else
1285 nvis = hvis < symvis ? hvis : symvis;
51b64d56 1286
45d6a902
AM
1287 h->other = other | nvis;
1288 }
252b5132 1289
45d6a902
AM
1290 /* Set a flag in the hash table entry indicating the type of
1291 reference or definition we just found. Keep a count of
1292 the number of dynamic symbols we find. A dynamic symbol
1293 is one which is referenced or defined by both a regular
1294 object and a shared object. */
1295 old_flags = h->elf_link_hash_flags;
1296 dynsym = FALSE;
1297 if (! dynamic)
1298 {
1299 if (! definition)
1300 {
1301 new_flag = ELF_LINK_HASH_REF_REGULAR;
1302 if (bind != STB_WEAK)
1303 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
1304 }
1305 else
1306 new_flag = ELF_LINK_HASH_DEF_REGULAR;
36af4a4e 1307 if (! info->executable
45d6a902
AM
1308 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
1309 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
1310 dynsym = TRUE;
1311 }
1312 else
1313 {
1314 if (! definition)
1315 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
1316 else
1317 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
1318 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
1319 | ELF_LINK_HASH_REF_REGULAR)) != 0
1320 || (h->weakdef != NULL
1321 && ! new_weakdef
1322 && h->weakdef->dynindx != -1))
1323 dynsym = TRUE;
1324 }
252b5132 1325
45d6a902 1326 h->elf_link_hash_flags |= new_flag;
252b5132 1327
45d6a902
AM
1328 /* Check to see if we need to add an indirect symbol for
1329 the default name. */
1330 if (definition || h->root.type == bfd_link_hash_common)
1331 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
1332 &sec, &value, &dynsym,
1333 override, dt_needed))
1334 goto error_free_vers;
68f69152 1335
45d6a902
AM
1336 if (definition && !dynamic)
1337 {
1338 char *p = strchr (name, ELF_VER_CHR);
1339 if (p != NULL && p[1] != ELF_VER_CHR)
1340 {
1341 /* Queue non-default versions so that .symver x, x@FOO
1342 aliases can be checked. */
1343 if (! nondeflt_vers)
1344 {
1345 amt = (isymend - isym + 1)
1346 * sizeof (struct elf_link_hash_entry *);
1347 nondeflt_vers = bfd_malloc (amt);
1348 }
1349 nondeflt_vers [nondeflt_vers_cnt++] = h;
1350 }
1351 }
252b5132 1352
45d6a902
AM
1353 if (dynsym && h->dynindx == -1)
1354 {
1355 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1356 goto error_free_vers;
1357 if (h->weakdef != NULL
1358 && ! new_weakdef
1359 && h->weakdef->dynindx == -1)
1360 {
1361 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
1362 goto error_free_vers;
1363 }
1364 }
1365 else if (dynsym && h->dynindx != -1)
1366 /* If the symbol already has a dynamic index, but
1367 visibility says it should not be visible, turn it into
1368 a local symbol. */
1369 switch (ELF_ST_VISIBILITY (h->other))
1370 {
1371 case STV_INTERNAL:
1372 case STV_HIDDEN:
1373 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1374 break;
1375 }
252b5132 1376
45d6a902
AM
1377 if (dt_needed && definition
1378 && (h->elf_link_hash_flags
1379 & ELF_LINK_HASH_REF_REGULAR) != 0)
1380 {
1381 bfd_size_type oldsize;
1382 bfd_size_type strindex;
252b5132 1383
45d6a902
AM
1384 /* The symbol from a DT_NEEDED object is referenced from
1385 the regular object to create a dynamic executable. We
1386 have to make sure there is a DT_NEEDED entry for it. */
252b5132 1387
45d6a902
AM
1388 dt_needed = FALSE;
1389 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1390 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
1391 elf_dt_soname (abfd), FALSE);
1392 if (strindex == (bfd_size_type) -1)
1393 goto error_free_vers;
252b5132 1394
45d6a902
AM
1395 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
1396 {
1397 asection *sdyn;
1398 Elf_External_Dyn *dyncon, *dynconend;
252b5132 1399
45d6a902
AM
1400 sdyn = bfd_get_section_by_name (hash_table->dynobj,
1401 ".dynamic");
1402 BFD_ASSERT (sdyn != NULL);
252b5132 1403
45d6a902
AM
1404 dyncon = (Elf_External_Dyn *) sdyn->contents;
1405 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1406 sdyn->_raw_size);
1407 for (; dyncon < dynconend; dyncon++)
1408 {
1409 Elf_Internal_Dyn dyn;
252b5132 1410
45d6a902
AM
1411 elf_swap_dyn_in (hash_table->dynobj,
1412 dyncon, &dyn);
1413 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
1414 dyn.d_un.d_val != strindex);
1415 }
1416 }
252b5132 1417
268b6b39 1418 if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
45d6a902 1419 goto error_free_vers;
252b5132
RH
1420 }
1421 }
45d6a902 1422 }
252b5132 1423
45d6a902
AM
1424 /* Now that all the symbols from this input file are created, handle
1425 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
1426 if (nondeflt_vers != NULL)
1427 {
1428 bfd_size_type cnt, symidx;
391a809a 1429
45d6a902 1430 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
ea44b734 1431 {
45d6a902
AM
1432 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
1433 char *shortname, *p;
ea44b734 1434
45d6a902
AM
1435 p = strchr (h->root.root.string, ELF_VER_CHR);
1436 if (p == NULL
1437 || (h->root.type != bfd_link_hash_defined
1438 && h->root.type != bfd_link_hash_defweak))
1439 continue;
31941635 1440
45d6a902
AM
1441 amt = p - h->root.root.string;
1442 shortname = bfd_malloc (amt + 1);
1443 memcpy (shortname, h->root.root.string, amt);
1444 shortname[amt] = '\0';
31941635 1445
45d6a902 1446 hi = (struct elf_link_hash_entry *)
0eddce27 1447 bfd_link_hash_lookup (&hash_table->root, shortname,
45d6a902
AM
1448 FALSE, FALSE, FALSE);
1449 if (hi != NULL
1450 && hi->root.type == h->root.type
1451 && hi->root.u.def.value == h->root.u.def.value
1452 && hi->root.u.def.section == h->root.u.def.section)
1453 {
1454 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1455 hi->root.type = bfd_link_hash_indirect;
1456 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
1457 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1458 sym_hash = elf_sym_hashes (abfd);
1459 if (sym_hash)
1460 for (symidx = 0; symidx < extsymcount; ++symidx)
1461 if (sym_hash[symidx] == hi)
1462 {
1463 sym_hash[symidx] = h;
1464 break;
1465 }
1466 }
1467 free (shortname);
1468 }
1469 free (nondeflt_vers);
1470 nondeflt_vers = NULL;
1471 }
31941635 1472
45d6a902
AM
1473 if (extversym != NULL)
1474 {
1475 free (extversym);
1476 extversym = NULL;
1477 }
31941635 1478
45d6a902
AM
1479 if (isymbuf != NULL)
1480 free (isymbuf);
1481 isymbuf = NULL;
31941635 1482
45d6a902
AM
1483 /* Now set the weakdefs field correctly for all the weak defined
1484 symbols we found. The only way to do this is to search all the
1485 symbols. Since we only need the information for non functions in
1486 dynamic objects, that's the only time we actually put anything on
1487 the list WEAKS. We need this information so that if a regular
1488 object refers to a symbol defined weakly in a dynamic object, the
1489 real symbol in the dynamic object is also put in the dynamic
1490 symbols; we also must arrange for both symbols to point to the
1491 same memory location. We could handle the general case of symbol
1492 aliasing, but a general symbol alias can only be generated in
1493 assembler code, handling it correctly would be very time
1494 consuming, and other ELF linkers don't handle general aliasing
1495 either. */
1496 while (weaks != NULL)
1497 {
1498 struct elf_link_hash_entry *hlook;
1499 asection *slook;
1500 bfd_vma vlook;
1501 struct elf_link_hash_entry **hpp;
1502 struct elf_link_hash_entry **hppend;
252b5132 1503
45d6a902
AM
1504 hlook = weaks;
1505 weaks = hlook->weakdef;
1506 hlook->weakdef = NULL;
252b5132 1507
45d6a902
AM
1508 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
1509 || hlook->root.type == bfd_link_hash_defweak
1510 || hlook->root.type == bfd_link_hash_common
1511 || hlook->root.type == bfd_link_hash_indirect);
1512 slook = hlook->root.u.def.section;
1513 vlook = hlook->root.u.def.value;
1514
1515 hpp = elf_sym_hashes (abfd);
1516 hppend = hpp + extsymcount;
1517 for (; hpp < hppend; hpp++)
31941635 1518 {
45d6a902 1519 struct elf_link_hash_entry *h;
31941635 1520
45d6a902
AM
1521 h = *hpp;
1522 if (h != NULL && h != hlook
1523 && h->root.type == bfd_link_hash_defined
1524 && h->root.u.def.section == slook
1525 && h->root.u.def.value == vlook)
31941635 1526 {
45d6a902 1527 hlook->weakdef = h;
252b5132 1528
45d6a902
AM
1529 /* If the weak definition is in the list of dynamic
1530 symbols, make sure the real definition is put there
1531 as well. */
1532 if (hlook->dynindx != -1
1533 && h->dynindx == -1)
1534 {
1535 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1536 goto error_return;
1537 }
f0c2e336 1538
45d6a902
AM
1539 /* If the real definition is in the list of dynamic
1540 symbols, make sure the weak definition is put there
1541 as well. If we don't do this, then the dynamic
1542 loader might not merge the entries for the real
1543 definition and the weak definition. */
1544 if (h->dynindx != -1
1545 && hlook->dynindx == -1)
1546 {
1547 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
1548 goto error_return;
1549 }
1550 break;
1551 }
252b5132 1552 }
45d6a902 1553 }
f0c2e336 1554
45d6a902
AM
1555 /* If this object is the same format as the output object, and it is
1556 not a shared library, then let the backend look through the
1557 relocs.
fa7ea4d8 1558
45d6a902
AM
1559 This is required to build global offset table entries and to
1560 arrange for dynamic relocs. It is not required for the
1561 particular common case of linking non PIC code, even when linking
1562 against shared libraries, but unfortunately there is no way of
1563 knowing whether an object file has been compiled PIC or not.
1564 Looking through the relocs is not particularly time consuming.
1565 The problem is that we must either (1) keep the relocs in memory,
1566 which causes the linker to require additional runtime memory or
1567 (2) read the relocs twice from the input file, which wastes time.
1568 This would be a good case for using mmap.
60166579 1569
45d6a902
AM
1570 I have no idea how to handle linking PIC code into a file of a
1571 different format. It probably can't be done. */
1572 check_relocs = get_elf_backend_data (abfd)->check_relocs;
1573 if (! dynamic
0eddce27
AM
1574 && is_elf_hash_table (hash_table)
1575 && hash_table->root.creator == abfd->xvec
45d6a902
AM
1576 && check_relocs != NULL)
1577 {
1578 asection *o;
fa7ea4d8 1579
45d6a902 1580 for (o = abfd->sections; o != NULL; o = o->next)
fa7ea4d8 1581 {
45d6a902
AM
1582 Elf_Internal_Rela *internal_relocs;
1583 bfd_boolean ok;
1584
1585 if ((o->flags & SEC_RELOC) == 0
1586 || o->reloc_count == 0
1587 || ((info->strip == strip_all || info->strip == strip_debugger)
1588 && (o->flags & SEC_DEBUGGING) != 0)
1589 || bfd_is_abs_section (o->output_section))
1590 continue;
1591
268b6b39
AM
1592 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
1593 info->keep_memory);
45d6a902
AM
1594 if (internal_relocs == NULL)
1595 goto error_return;
1596
1597 ok = (*check_relocs) (abfd, info, o, internal_relocs);
1598
1599 if (elf_section_data (o)->relocs != internal_relocs)
1600 free (internal_relocs);
1601
1602 if (! ok)
1603 goto error_return;
fa7ea4d8 1604 }
45d6a902 1605 }
30831527 1606
45d6a902
AM
1607 /* If this is a non-traditional link, try to optimize the handling
1608 of the .stab/.stabstr sections. */
1609 if (! dynamic
1610 && ! info->traditional_format
0eddce27 1611 && is_elf_hash_table (hash_table)
45d6a902
AM
1612 && (info->strip != strip_all && info->strip != strip_debugger))
1613 {
29ca8dc5
NS
1614 asection *stabstr;
1615
1616 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
1617 if (stabstr != NULL)
fc8c40a0 1618 {
29ca8dc5
NS
1619 bfd_size_type string_offset = 0;
1620 asection *stab;
1621
1622 for (stab = abfd->sections; stab; stab = stab->next)
1623 if (strncmp (".stab", stab->name, 5) == 0
1624 && (!stab->name[5] ||
1625 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
1626 && (stab->flags & SEC_MERGE) == 0
1627 && !bfd_is_abs_section (stab->output_section))
1628 {
1629 struct bfd_elf_section_data *secdata;
1630
1631 secdata = elf_section_data (stab);
1632 if (! _bfd_link_section_stabs (abfd,
1633 & hash_table->stab_info,
1634 stab, stabstr,
1635 &secdata->sec_info,
1636 &string_offset))
1637 goto error_return;
1638 if (secdata->sec_info)
1639 stab->sec_info_type = ELF_INFO_TYPE_STABS;
45d6a902 1640 }
fc8c40a0 1641 }
252b5132
RH
1642 }
1643
0eddce27
AM
1644 if (! info->relocatable
1645 && ! dynamic
1646 && is_elf_hash_table (hash_table))
252b5132 1647 {
252b5132 1648 asection *s;
252b5132 1649
45d6a902
AM
1650 for (s = abfd->sections; s != NULL; s = s->next)
1651 if ((s->flags & SEC_MERGE) != 0
1652 && !bfd_is_abs_section (s->output_section))
1653 {
1654 struct bfd_elf_section_data *secdata;
252b5132 1655
45d6a902
AM
1656 secdata = elf_section_data (s);
1657 if (! _bfd_merge_section (abfd,
1658 & hash_table->merge_info,
1659 s, &secdata->sec_info))
1660 goto error_return;
1661 else if (secdata->sec_info)
1662 s->sec_info_type = ELF_INFO_TYPE_MERGE;
1663 }
1664 }
252b5132 1665
0eddce27 1666 if (is_elf_hash_table (hash_table))
45d6a902
AM
1667 {
1668 /* Add this bfd to the loaded list. */
1669 struct elf_link_loaded_list *n;
6b9b879a 1670
268b6b39 1671 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
45d6a902
AM
1672 if (n == NULL)
1673 goto error_return;
1674 n->abfd = abfd;
1675 n->next = hash_table->loaded;
1676 hash_table->loaded = n;
1677 }
252b5132 1678
45d6a902 1679 return TRUE;
252b5132 1680
45d6a902
AM
1681 error_free_vers:
1682 if (nondeflt_vers != NULL)
1683 free (nondeflt_vers);
1684 if (extversym != NULL)
1685 free (extversym);
1686 error_free_sym:
1687 if (isymbuf != NULL)
1688 free (isymbuf);
1689 error_return:
1690 return FALSE;
1691}
252b5132 1692
45d6a902 1693/* Add an entry to the .dynamic table. */
252b5132 1694
45d6a902 1695bfd_boolean
268b6b39 1696elf_add_dynamic_entry (struct bfd_link_info *info, bfd_vma tag, bfd_vma val)
45d6a902
AM
1697{
1698 Elf_Internal_Dyn dyn;
1699 bfd *dynobj;
1700 asection *s;
1701 bfd_size_type newsize;
1702 bfd_byte *newcontents;
252b5132 1703
0eddce27 1704 if (! is_elf_hash_table (info->hash))
45d6a902 1705 return FALSE;
252b5132 1706
45d6a902
AM
1707 dynobj = elf_hash_table (info)->dynobj;
1708
1709 s = bfd_get_section_by_name (dynobj, ".dynamic");
1710 BFD_ASSERT (s != NULL);
1711
1712 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
268b6b39 1713 newcontents = bfd_realloc (s->contents, newsize);
45d6a902
AM
1714 if (newcontents == NULL)
1715 return FALSE;
1716
1717 dyn.d_tag = tag;
1718 dyn.d_un.d_val = val;
1719 elf_swap_dyn_out (dynobj, &dyn,
1720 (Elf_External_Dyn *) (newcontents + s->_raw_size));
1721
1722 s->_raw_size = newsize;
1723 s->contents = newcontents;
1724
1725 return TRUE;
1726}
1727\f
1728/* Array used to determine the number of hash table buckets to use
1729 based on the number of symbols there are. If there are fewer than
1730 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
1731 fewer than 37 we use 17 buckets, and so forth. We never use more
1732 than 32771 buckets. */
252b5132 1733
45d6a902
AM
1734static const size_t elf_buckets[] =
1735{
1736 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
1737 16411, 32771, 0
1738};
252b5132 1739
45d6a902
AM
1740/* Compute bucket count for hashing table. We do not use a static set
1741 of possible tables sizes anymore. Instead we determine for all
1742 possible reasonable sizes of the table the outcome (i.e., the
1743 number of collisions etc) and choose the best solution. The
1744 weighting functions are not too simple to allow the table to grow
1745 without bounds. Instead one of the weighting factors is the size.
1746 Therefore the result is always a good payoff between few collisions
1747 (= short chain lengths) and table size. */
1748static size_t
268b6b39 1749compute_bucket_count (struct bfd_link_info *info)
45d6a902
AM
1750{
1751 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
1752 size_t best_size = 0;
1753 unsigned long int *hashcodes;
1754 unsigned long int *hashcodesp;
1755 unsigned long int i;
1756 bfd_size_type amt;
252b5132 1757
45d6a902
AM
1758 /* Compute the hash values for all exported symbols. At the same
1759 time store the values in an array so that we could use them for
1760 optimizations. */
1761 amt = dynsymcount;
1762 amt *= sizeof (unsigned long int);
268b6b39 1763 hashcodes = bfd_malloc (amt);
45d6a902
AM
1764 if (hashcodes == NULL)
1765 return 0;
1766 hashcodesp = hashcodes;
252b5132 1767
45d6a902
AM
1768 /* Put all hash values in HASHCODES. */
1769 elf_link_hash_traverse (elf_hash_table (info),
1770 elf_collect_hash_codes, &hashcodesp);
252b5132 1771
45d6a902
AM
1772 /* We have a problem here. The following code to optimize the table
1773 size requires an integer type with more the 32 bits. If
1774 BFD_HOST_U_64_BIT is set we know about such a type. */
1775#ifdef BFD_HOST_U_64_BIT
1776 if (info->optimize)
1777 {
1778 unsigned long int nsyms = hashcodesp - hashcodes;
1779 size_t minsize;
1780 size_t maxsize;
1781 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
1782 unsigned long int *counts ;
252b5132 1783
45d6a902
AM
1784 /* Possible optimization parameters: if we have NSYMS symbols we say
1785 that the hashing table must at least have NSYMS/4 and at most
1786 2*NSYMS buckets. */
1787 minsize = nsyms / 4;
1788 if (minsize == 0)
1789 minsize = 1;
1790 best_size = maxsize = nsyms * 2;
252b5132 1791
45d6a902
AM
1792 /* Create array where we count the collisions in. We must use bfd_malloc
1793 since the size could be large. */
1794 amt = maxsize;
1795 amt *= sizeof (unsigned long int);
268b6b39 1796 counts = bfd_malloc (amt);
45d6a902
AM
1797 if (counts == NULL)
1798 {
1799 free (hashcodes);
1800 return 0;
1801 }
252b5132 1802
45d6a902
AM
1803 /* Compute the "optimal" size for the hash table. The criteria is a
1804 minimal chain length. The minor criteria is (of course) the size
1805 of the table. */
1806 for (i = minsize; i < maxsize; ++i)
1807 {
1808 /* Walk through the array of hashcodes and count the collisions. */
1809 BFD_HOST_U_64_BIT max;
1810 unsigned long int j;
1811 unsigned long int fact;
252b5132 1812
45d6a902 1813 memset (counts, '\0', i * sizeof (unsigned long int));
252b5132 1814
45d6a902
AM
1815 /* Determine how often each hash bucket is used. */
1816 for (j = 0; j < nsyms; ++j)
1817 ++counts[hashcodes[j] % i];
252b5132 1818
45d6a902
AM
1819 /* For the weight function we need some information about the
1820 pagesize on the target. This is information need not be 100%
1821 accurate. Since this information is not available (so far) we
1822 define it here to a reasonable default value. If it is crucial
1823 to have a better value some day simply define this value. */
1824# ifndef BFD_TARGET_PAGESIZE
1825# define BFD_TARGET_PAGESIZE (4096)
1826# endif
252b5132 1827
45d6a902
AM
1828 /* We in any case need 2 + NSYMS entries for the size values and
1829 the chains. */
1830 max = (2 + nsyms) * (ARCH_SIZE / 8);
252b5132 1831
45d6a902
AM
1832# if 1
1833 /* Variant 1: optimize for short chains. We add the squares
4cc11e76 1834 of all the chain lengths (which favors many small chain
45d6a902
AM
1835 over a few long chains). */
1836 for (j = 0; j < i; ++j)
1837 max += counts[j] * counts[j];
252b5132 1838
45d6a902
AM
1839 /* This adds penalties for the overall size of the table. */
1840 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
1841 max *= fact * fact;
1842# else
1843 /* Variant 2: Optimize a lot more for small table. Here we
1844 also add squares of the size but we also add penalties for
1845 empty slots (the +1 term). */
1846 for (j = 0; j < i; ++j)
1847 max += (1 + counts[j]) * (1 + counts[j]);
252b5132 1848
45d6a902
AM
1849 /* The overall size of the table is considered, but not as
1850 strong as in variant 1, where it is squared. */
1851 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
1852 max *= fact;
1853# endif
252b5132 1854
45d6a902
AM
1855 /* Compare with current best results. */
1856 if (max < best_chlen)
1857 {
1858 best_chlen = max;
1859 best_size = i;
252b5132 1860 }
d6cf2879
L
1861 }
1862
45d6a902
AM
1863 free (counts);
1864 }
1865 else
1866#endif /* defined (BFD_HOST_U_64_BIT) */
1867 {
1868 /* This is the fallback solution if no 64bit type is available or if we
1869 are not supposed to spend much time on optimizations. We select the
1870 bucket count using a fixed set of numbers. */
1871 for (i = 0; elf_buckets[i] != 0; i++)
d6cf2879 1872 {
45d6a902
AM
1873 best_size = elf_buckets[i];
1874 if (dynsymcount < elf_buckets[i + 1])
1875 break;
d6cf2879 1876 }
45d6a902 1877 }
d6cf2879 1878
45d6a902
AM
1879 /* Free the arrays we needed. */
1880 free (hashcodes);
252b5132 1881
45d6a902
AM
1882 return best_size;
1883}
252b5132 1884
45d6a902
AM
1885/* Set up the sizes and contents of the ELF dynamic sections. This is
1886 called by the ELF linker emulation before_allocation routine. We
1887 must set the sizes of the sections before the linker sets the
1888 addresses of the various sections. */
252b5132 1889
45d6a902 1890bfd_boolean
268b6b39
AM
1891NAME(bfd_elf,size_dynamic_sections) (bfd *output_bfd,
1892 const char *soname,
1893 const char *rpath,
1894 const char *filter_shlib,
1895 const char * const *auxiliary_filters,
1896 struct bfd_link_info *info,
1897 asection **sinterpptr,
1898 struct bfd_elf_version_tree *verdefs)
45d6a902
AM
1899{
1900 bfd_size_type soname_indx;
1901 bfd *dynobj;
9c5bfbb7 1902 const struct elf_backend_data *bed;
45d6a902 1903 struct elf_assign_sym_version_info asvinfo;
252b5132 1904
45d6a902 1905 *sinterpptr = NULL;
252b5132 1906
45d6a902 1907 soname_indx = (bfd_size_type) -1;
252b5132 1908
0eddce27 1909 if (!is_elf_hash_table (info->hash))
45d6a902 1910 return TRUE;
252b5132 1911
9ee5e499
JJ
1912 if (info->execstack)
1913 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
1914 else if (info->noexecstack)
1915 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
1916 else
1917 {
1918 bfd *inputobj;
1919 asection *notesec = NULL;
1920 int exec = 0;
1921
1922 for (inputobj = info->input_bfds;
1923 inputobj;
1924 inputobj = inputobj->link_next)
1925 {
1926 asection *s;
1927
1928 if (inputobj->flags & DYNAMIC)
1929 continue;
1930 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
1931 if (s)
1932 {
1933 if (s->flags & SEC_CODE)
1934 exec = PF_X;
1935 notesec = s;
1936 }
1937 else
1938 exec = PF_X;
1939 }
1940 if (notesec)
1941 {
1942 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
1049f94e 1943 if (exec && info->relocatable
9ee5e499
JJ
1944 && notesec->output_section != bfd_abs_section_ptr)
1945 notesec->output_section->flags |= SEC_CODE;
1946 }
1947 }
1948
45d6a902
AM
1949 /* Any syms created from now on start with -1 in
1950 got.refcount/offset and plt.refcount/offset. */
1951 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
252b5132 1952
45d6a902
AM
1953 /* The backend may have to create some sections regardless of whether
1954 we're dynamic or not. */
1955 bed = get_elf_backend_data (output_bfd);
1956 if (bed->elf_backend_always_size_sections
1957 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
1958 return FALSE;
252b5132 1959
45d6a902 1960 dynobj = elf_hash_table (info)->dynobj;
252b5132 1961
45d6a902
AM
1962 /* If there were no dynamic objects in the link, there is nothing to
1963 do here. */
1964 if (dynobj == NULL)
1965 return TRUE;
252b5132 1966
45d6a902
AM
1967 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
1968 return FALSE;
252b5132 1969
45d6a902
AM
1970 if (elf_hash_table (info)->dynamic_sections_created)
1971 {
1972 struct elf_info_failed eif;
1973 struct elf_link_hash_entry *h;
1974 asection *dynstr;
1975 struct bfd_elf_version_tree *t;
1976 struct bfd_elf_version_expr *d;
1977 bfd_boolean all_defined;
30b30c21 1978
45d6a902 1979 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
893c4fe2 1980 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
252b5132 1981
45d6a902 1982 if (soname != NULL)
252b5132 1983 {
45d6a902
AM
1984 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
1985 soname, TRUE);
1986 if (soname_indx == (bfd_size_type) -1
268b6b39 1987 || ! elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
45d6a902 1988 return FALSE;
252b5132 1989 }
45d6a902
AM
1990
1991 if (info->symbolic)
252b5132 1992 {
268b6b39 1993 if (! elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
b34976b6 1994 return FALSE;
45d6a902
AM
1995 info->flags |= DF_SYMBOLIC;
1996 }
252b5132 1997
45d6a902
AM
1998 if (rpath != NULL)
1999 {
2000 bfd_size_type indx;
2001
2002 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
2003 TRUE);
2004 if (info->new_dtags)
2005 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
2006 if (indx == (bfd_size_type) -1
268b6b39 2007 || ! elf_add_dynamic_entry (info, DT_RPATH, indx)
45d6a902 2008 || (info->new_dtags
268b6b39 2009 && ! elf_add_dynamic_entry (info, DT_RUNPATH, indx)))
b34976b6 2010 return FALSE;
252b5132
RH
2011 }
2012
45d6a902 2013 if (filter_shlib != NULL)
fc8c40a0 2014 {
45d6a902 2015 bfd_size_type indx;
fc8c40a0 2016
45d6a902
AM
2017 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2018 filter_shlib, TRUE);
2019 if (indx == (bfd_size_type) -1
268b6b39 2020 || ! elf_add_dynamic_entry (info, DT_FILTER, indx))
45d6a902 2021 return FALSE;
fc8c40a0 2022 }
252b5132 2023
45d6a902
AM
2024 if (auxiliary_filters != NULL)
2025 {
2026 const char * const *p;
252b5132 2027
45d6a902
AM
2028 for (p = auxiliary_filters; *p != NULL; p++)
2029 {
2030 bfd_size_type indx;
2031
2032 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2033 *p, TRUE);
2034 if (indx == (bfd_size_type) -1
268b6b39 2035 || ! elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
45d6a902
AM
2036 return FALSE;
2037 }
2038 }
2039
2040 eif.info = info;
2041 eif.verdefs = verdefs;
2042 eif.failed = FALSE;
252b5132 2043
45d6a902
AM
2044 /* If we are supposed to export all symbols into the dynamic symbol
2045 table (this is not the normal case), then do so. */
2046 if (info->export_dynamic)
2047 {
2048 elf_link_hash_traverse (elf_hash_table (info),
2049 _bfd_elf_export_symbol,
268b6b39 2050 &eif);
45d6a902
AM
2051 if (eif.failed)
2052 return FALSE;
2053 }
252b5132 2054
4cc11e76 2055 /* Make all global versions with definition. */
45d6a902 2056 for (t = verdefs; t != NULL; t = t->next)
108ba305 2057 for (d = t->globals.list; d != NULL; d = d->next)
5e35cbc2 2058 if (!d->symver && d->symbol)
45d6a902
AM
2059 {
2060 const char *verstr, *name;
2061 size_t namelen, verlen, newlen;
2062 char *newname, *p;
2063 struct elf_link_hash_entry *newh;
252b5132 2064
5e35cbc2 2065 name = d->symbol;
45d6a902
AM
2066 namelen = strlen (name);
2067 verstr = t->name;
2068 verlen = strlen (verstr);
2069 newlen = namelen + verlen + 3;
2b0f7ef9 2070
268b6b39 2071 newname = bfd_malloc (newlen);
45d6a902
AM
2072 if (newname == NULL)
2073 return FALSE;
2074 memcpy (newname, name, namelen);
2b0f7ef9 2075
45d6a902
AM
2076 /* Check the hidden versioned definition. */
2077 p = newname + namelen;
2078 *p++ = ELF_VER_CHR;
2079 memcpy (p, verstr, verlen + 1);
2080 newh = elf_link_hash_lookup (elf_hash_table (info),
2081 newname, FALSE, FALSE,
2082 FALSE);
2083 if (newh == NULL
2084 || (newh->root.type != bfd_link_hash_defined
2085 && newh->root.type != bfd_link_hash_defweak))
2086 {
2087 /* Check the default versioned definition. */
2088 *p++ = ELF_VER_CHR;
2089 memcpy (p, verstr, verlen + 1);
2090 newh = elf_link_hash_lookup (elf_hash_table (info),
2091 newname, FALSE, FALSE,
2092 FALSE);
2093 }
2094 free (newname);
252b5132 2095
45d6a902
AM
2096 /* Mark this version if there is a definition and it is
2097 not defined in a shared object. */
2098 if (newh != NULL
2099 && ((newh->elf_link_hash_flags
2100 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
2101 && (newh->root.type == bfd_link_hash_defined
2102 || newh->root.type == bfd_link_hash_defweak))
2103 d->symver = 1;
2104 }
252b5132 2105
45d6a902
AM
2106 /* Attach all the symbols to their version information. */
2107 asvinfo.output_bfd = output_bfd;
2108 asvinfo.info = info;
2109 asvinfo.verdefs = verdefs;
2110 asvinfo.failed = FALSE;
c44233aa 2111
45d6a902
AM
2112 elf_link_hash_traverse (elf_hash_table (info),
2113 _bfd_elf_link_assign_sym_version,
268b6b39 2114 &asvinfo);
45d6a902
AM
2115 if (asvinfo.failed)
2116 return FALSE;
c44233aa 2117
45d6a902
AM
2118 if (!info->allow_undefined_version)
2119 {
4cc11e76 2120 /* Check if all global versions have a definition. */
45d6a902
AM
2121 all_defined = TRUE;
2122 for (t = verdefs; t != NULL; t = t->next)
108ba305 2123 for (d = t->globals.list; d != NULL; d = d->next)
3e0e1ef1 2124 if (!d->symver && !d->script)
45d6a902
AM
2125 {
2126 (*_bfd_error_handler)
2127 (_("%s: undefined version: %s"),
2128 d->pattern, t->name);
2129 all_defined = FALSE;
2130 }
2b0f7ef9 2131
45d6a902
AM
2132 if (!all_defined)
2133 {
2134 bfd_set_error (bfd_error_bad_value);
2135 return FALSE;
2136 }
2137 }
e92d460e 2138
45d6a902
AM
2139 /* Find all symbols which were defined in a dynamic object and make
2140 the backend pick a reasonable value for them. */
2141 elf_link_hash_traverse (elf_hash_table (info),
2142 _bfd_elf_adjust_dynamic_symbol,
268b6b39 2143 &eif);
45d6a902
AM
2144 if (eif.failed)
2145 return FALSE;
2b0f7ef9 2146
45d6a902
AM
2147 /* Add some entries to the .dynamic section. We fill in some of the
2148 values later, in elf_bfd_final_link, but we must add the entries
2149 now so that we know the final size of the .dynamic section. */
2b0f7ef9 2150
45d6a902
AM
2151 /* If there are initialization and/or finalization functions to
2152 call then add the corresponding DT_INIT/DT_FINI entries. */
2153 h = (info->init_function
2154 ? elf_link_hash_lookup (elf_hash_table (info),
2155 info->init_function, FALSE,
2156 FALSE, FALSE)
2157 : NULL);
2158 if (h != NULL
2159 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
2160 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
2161 {
268b6b39 2162 if (! elf_add_dynamic_entry (info, DT_INIT, 0))
45d6a902
AM
2163 return FALSE;
2164 }
2165 h = (info->fini_function
2166 ? elf_link_hash_lookup (elf_hash_table (info),
2167 info->fini_function, FALSE,
2168 FALSE, FALSE)
2169 : NULL);
2170 if (h != NULL
2171 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
2172 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
2173 {
268b6b39 2174 if (! elf_add_dynamic_entry (info, DT_FINI, 0))
45d6a902
AM
2175 return FALSE;
2176 }
2b0f7ef9 2177
45d6a902
AM
2178 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
2179 {
2180 /* DT_PREINIT_ARRAY is not allowed in shared library. */
36af4a4e 2181 if (! info->executable)
45d6a902
AM
2182 {
2183 bfd *sub;
2184 asection *o;
2b0f7ef9 2185
45d6a902
AM
2186 for (sub = info->input_bfds; sub != NULL;
2187 sub = sub->link_next)
2188 for (o = sub->sections; o != NULL; o = o->next)
2189 if (elf_section_data (o)->this_hdr.sh_type
2190 == SHT_PREINIT_ARRAY)
2191 {
2192 (*_bfd_error_handler)
2193 (_("%s: .preinit_array section is not allowed in DSO"),
2194 bfd_archive_filename (sub));
2195 break;
2196 }
2b0f7ef9 2197
45d6a902
AM
2198 bfd_set_error (bfd_error_nonrepresentable_section);
2199 return FALSE;
2200 }
2b0f7ef9 2201
268b6b39
AM
2202 if (!elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
2203 || !elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
45d6a902
AM
2204 return FALSE;
2205 }
2206 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
2b0f7ef9 2207 {
268b6b39
AM
2208 if (!elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
2209 || !elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
45d6a902
AM
2210 return FALSE;
2211 }
2212 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
2213 {
268b6b39
AM
2214 if (!elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
2215 || !elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
45d6a902 2216 return FALSE;
2b0f7ef9 2217 }
2b0f7ef9 2218
45d6a902
AM
2219 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
2220 /* If .dynstr is excluded from the link, we don't want any of
2221 these tags. Strictly, we should be checking each section
2222 individually; This quick check covers for the case where
2223 someone does a /DISCARD/ : { *(*) }. */
2224 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
2225 {
2226 bfd_size_type strsize;
2227
2228 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
268b6b39
AM
2229 if (! elf_add_dynamic_entry (info, DT_HASH, 0)
2230 || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
2231 || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0)
2232 || ! elf_add_dynamic_entry (info, DT_STRSZ, strsize)
2233 || ! elf_add_dynamic_entry (info, DT_SYMENT,
2234 sizeof (Elf_External_Sym)))
45d6a902
AM
2235 return FALSE;
2236 }
2237 }
2b0f7ef9 2238
45d6a902
AM
2239 /* The backend must work out the sizes of all the other dynamic
2240 sections. */
2241 if (bed->elf_backend_size_dynamic_sections
2242 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
2243 return FALSE;
2b0f7ef9 2244
45d6a902 2245 if (elf_hash_table (info)->dynamic_sections_created)
2b0f7ef9 2246 {
45d6a902 2247 bfd_size_type dynsymcount;
2b0f7ef9 2248 asection *s;
45d6a902
AM
2249 size_t bucketcount = 0;
2250 size_t hash_entry_size;
2251 unsigned int dtagcount;
c44233aa 2252
45d6a902 2253 /* Set up the version definition section. */
2b0f7ef9 2254 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
45d6a902 2255 BFD_ASSERT (s != NULL);
2b0f7ef9 2256
45d6a902
AM
2257 /* We may have created additional version definitions if we are
2258 just linking a regular application. */
2259 verdefs = asvinfo.verdefs;
c44233aa 2260
45d6a902
AM
2261 /* Skip anonymous version tag. */
2262 if (verdefs != NULL && verdefs->vernum == 0)
2263 verdefs = verdefs->next;
2264
2265 if (verdefs == NULL)
2266 _bfd_strip_section_from_output (info, s);
2267 else
2b0f7ef9 2268 {
45d6a902
AM
2269 unsigned int cdefs;
2270 bfd_size_type size;
2271 struct bfd_elf_version_tree *t;
2272 bfd_byte *p;
2273 Elf_Internal_Verdef def;
2274 Elf_Internal_Verdaux defaux;
2275
2276 cdefs = 0;
2277 size = 0;
2278
2279 /* Make space for the base version. */
2280 size += sizeof (Elf_External_Verdef);
2281 size += sizeof (Elf_External_Verdaux);
2282 ++cdefs;
2283
2284 for (t = verdefs; t != NULL; t = t->next)
2b0f7ef9 2285 {
45d6a902
AM
2286 struct bfd_elf_version_deps *n;
2287
2288 size += sizeof (Elf_External_Verdef);
2289 size += sizeof (Elf_External_Verdaux);
2290 ++cdefs;
2291
2292 for (n = t->deps; n != NULL; n = n->next)
2293 size += sizeof (Elf_External_Verdaux);
2b0f7ef9 2294 }
2b0f7ef9 2295
45d6a902 2296 s->_raw_size = size;
268b6b39 2297 s->contents = bfd_alloc (output_bfd, s->_raw_size);
45d6a902
AM
2298 if (s->contents == NULL && s->_raw_size != 0)
2299 return FALSE;
2b0f7ef9 2300
45d6a902 2301 /* Fill in the version definition section. */
252b5132 2302
45d6a902 2303 p = s->contents;
94b6c40a 2304
45d6a902
AM
2305 def.vd_version = VER_DEF_CURRENT;
2306 def.vd_flags = VER_FLG_BASE;
2307 def.vd_ndx = 1;
2308 def.vd_cnt = 1;
2309 def.vd_aux = sizeof (Elf_External_Verdef);
2310 def.vd_next = (sizeof (Elf_External_Verdef)
2311 + sizeof (Elf_External_Verdaux));
252b5132 2312
45d6a902 2313 if (soname_indx != (bfd_size_type) -1)
252b5132 2314 {
45d6a902
AM
2315 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2316 soname_indx);
2317 def.vd_hash = bfd_elf_hash (soname);
2318 defaux.vda_name = soname_indx;
252b5132 2319 }
45d6a902
AM
2320 else
2321 {
2322 const char *name;
2323 bfd_size_type indx;
252b5132 2324
45d6a902
AM
2325 name = basename (output_bfd->filename);
2326 def.vd_hash = bfd_elf_hash (name);
2327 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2328 name, FALSE);
2329 if (indx == (bfd_size_type) -1)
2330 return FALSE;
2331 defaux.vda_name = indx;
2332 }
2333 defaux.vda_next = 0;
8ea2e4bd 2334
45d6a902
AM
2335 _bfd_elf_swap_verdef_out (output_bfd, &def,
2336 (Elf_External_Verdef *) p);
2337 p += sizeof (Elf_External_Verdef);
2338 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2339 (Elf_External_Verdaux *) p);
2340 p += sizeof (Elf_External_Verdaux);
e5094212 2341
45d6a902
AM
2342 for (t = verdefs; t != NULL; t = t->next)
2343 {
2344 unsigned int cdeps;
2345 struct bfd_elf_version_deps *n;
2346 struct elf_link_hash_entry *h;
2347 struct bfd_link_hash_entry *bh;
252b5132 2348
45d6a902
AM
2349 cdeps = 0;
2350 for (n = t->deps; n != NULL; n = n->next)
2351 ++cdeps;
ef5aade5 2352
45d6a902
AM
2353 /* Add a symbol representing this version. */
2354 bh = NULL;
2355 if (! (_bfd_generic_link_add_one_symbol
2356 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
268b6b39 2357 0, NULL, FALSE,
45d6a902
AM
2358 get_elf_backend_data (dynobj)->collect, &bh)))
2359 return FALSE;
2360 h = (struct elf_link_hash_entry *) bh;
2361 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
2362 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2363 h->type = STT_OBJECT;
2364 h->verinfo.vertree = t;
fc4cc5bb 2365
45d6a902
AM
2366 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2367 return FALSE;
2368
2369 def.vd_version = VER_DEF_CURRENT;
2370 def.vd_flags = 0;
108ba305 2371 if (t->globals.list == NULL && t->locals.list == NULL && ! t->used)
45d6a902
AM
2372 def.vd_flags |= VER_FLG_WEAK;
2373 def.vd_ndx = t->vernum + 1;
2374 def.vd_cnt = cdeps + 1;
2375 def.vd_hash = bfd_elf_hash (t->name);
2376 def.vd_aux = sizeof (Elf_External_Verdef);
2377 if (t->next != NULL)
2378 def.vd_next = (sizeof (Elf_External_Verdef)
2379 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
2380 else
2381 def.vd_next = 0;
2382
2383 _bfd_elf_swap_verdef_out (output_bfd, &def,
2384 (Elf_External_Verdef *) p);
2385 p += sizeof (Elf_External_Verdef);
2386
2387 defaux.vda_name = h->dynstr_index;
2388 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2389 h->dynstr_index);
2390 if (t->deps == NULL)
2391 defaux.vda_next = 0;
2392 else
2393 defaux.vda_next = sizeof (Elf_External_Verdaux);
2394 t->name_indx = defaux.vda_name;
2395
2396 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2397 (Elf_External_Verdaux *) p);
2398 p += sizeof (Elf_External_Verdaux);
2399
2400 for (n = t->deps; n != NULL; n = n->next)
2401 {
2402 if (n->version_needed == NULL)
2403 {
2404 /* This can happen if there was an error in the
2405 version script. */
2406 defaux.vda_name = 0;
2407 }
2408 else
2409 {
2410 defaux.vda_name = n->version_needed->name_indx;
2411 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2412 defaux.vda_name);
2413 }
2414 if (n->next == NULL)
2415 defaux.vda_next = 0;
2416 else
2417 defaux.vda_next = sizeof (Elf_External_Verdaux);
2418
2419 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2420 (Elf_External_Verdaux *) p);
2421 p += sizeof (Elf_External_Verdaux);
2422 }
2423 }
2424
268b6b39
AM
2425 if (! elf_add_dynamic_entry (info, DT_VERDEF, 0)
2426 || ! elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
45d6a902
AM
2427 return FALSE;
2428
2429 elf_tdata (output_bfd)->cverdefs = cdefs;
2430 }
8e67855b 2431
45d6a902 2432 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
0a991dfe 2433 {
268b6b39 2434 if (! elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
45d6a902
AM
2435 return FALSE;
2436 }
0a991dfe 2437
45d6a902
AM
2438 if (info->flags_1)
2439 {
36af4a4e 2440 if (info->executable)
45d6a902
AM
2441 info->flags_1 &= ~ (DF_1_INITFIRST
2442 | DF_1_NODELETE
2443 | DF_1_NOOPEN);
268b6b39 2444 if (! elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
45d6a902 2445 return FALSE;
0a991dfe 2446 }
fc4cc5bb 2447
45d6a902 2448 /* Work out the size of the version reference section. */
252b5132 2449
45d6a902
AM
2450 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2451 BFD_ASSERT (s != NULL);
2452 {
2453 struct elf_find_verdep_info sinfo;
252b5132 2454
45d6a902
AM
2455 sinfo.output_bfd = output_bfd;
2456 sinfo.info = info;
2457 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
2458 if (sinfo.vers == 0)
2459 sinfo.vers = 1;
2460 sinfo.failed = FALSE;
252b5132 2461
45d6a902
AM
2462 elf_link_hash_traverse (elf_hash_table (info),
2463 _bfd_elf_link_find_version_dependencies,
268b6b39 2464 &sinfo);
5cab59f6 2465
45d6a902
AM
2466 if (elf_tdata (output_bfd)->verref == NULL)
2467 _bfd_strip_section_from_output (info, s);
2468 else
2469 {
2470 Elf_Internal_Verneed *t;
2471 unsigned int size;
2472 unsigned int crefs;
2473 bfd_byte *p;
e92d460e 2474
45d6a902
AM
2475 /* Build the version definition section. */
2476 size = 0;
2477 crefs = 0;
2478 for (t = elf_tdata (output_bfd)->verref;
2479 t != NULL;
2480 t = t->vn_nextref)
2481 {
2482 Elf_Internal_Vernaux *a;
e92d460e 2483
45d6a902
AM
2484 size += sizeof (Elf_External_Verneed);
2485 ++crefs;
2486 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2487 size += sizeof (Elf_External_Vernaux);
2488 }
252b5132 2489
45d6a902 2490 s->_raw_size = size;
268b6b39 2491 s->contents = bfd_alloc (output_bfd, s->_raw_size);
45d6a902
AM
2492 if (s->contents == NULL)
2493 return FALSE;
252b5132 2494
45d6a902
AM
2495 p = s->contents;
2496 for (t = elf_tdata (output_bfd)->verref;
2497 t != NULL;
2498 t = t->vn_nextref)
2499 {
2500 unsigned int caux;
2501 Elf_Internal_Vernaux *a;
2502 bfd_size_type indx;
252b5132 2503
45d6a902
AM
2504 caux = 0;
2505 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2506 ++caux;
252b5132 2507
45d6a902
AM
2508 t->vn_version = VER_NEED_CURRENT;
2509 t->vn_cnt = caux;
2510 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2511 elf_dt_name (t->vn_bfd) != NULL
2512 ? elf_dt_name (t->vn_bfd)
2513 : basename (t->vn_bfd->filename),
2514 FALSE);
2515 if (indx == (bfd_size_type) -1)
2516 return FALSE;
2517 t->vn_file = indx;
2518 t->vn_aux = sizeof (Elf_External_Verneed);
2519 if (t->vn_nextref == NULL)
2520 t->vn_next = 0;
2521 else
2522 t->vn_next = (sizeof (Elf_External_Verneed)
2523 + caux * sizeof (Elf_External_Vernaux));
252b5132 2524
45d6a902
AM
2525 _bfd_elf_swap_verneed_out (output_bfd, t,
2526 (Elf_External_Verneed *) p);
2527 p += sizeof (Elf_External_Verneed);
252b5132 2528
45d6a902
AM
2529 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2530 {
2531 a->vna_hash = bfd_elf_hash (a->vna_nodename);
2532 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2533 a->vna_nodename, FALSE);
2534 if (indx == (bfd_size_type) -1)
2535 return FALSE;
2536 a->vna_name = indx;
2537 if (a->vna_nextptr == NULL)
2538 a->vna_next = 0;
2539 else
2540 a->vna_next = sizeof (Elf_External_Vernaux);
252b5132 2541
45d6a902
AM
2542 _bfd_elf_swap_vernaux_out (output_bfd, a,
2543 (Elf_External_Vernaux *) p);
2544 p += sizeof (Elf_External_Vernaux);
2545 }
2546 }
252b5132 2547
268b6b39
AM
2548 if (! elf_add_dynamic_entry (info, DT_VERNEED, 0)
2549 || ! elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
45d6a902 2550 return FALSE;
252b5132 2551
45d6a902
AM
2552 elf_tdata (output_bfd)->cverrefs = crefs;
2553 }
2554 }
252b5132 2555
45d6a902
AM
2556 /* Assign dynsym indicies. In a shared library we generate a
2557 section symbol for each output section, which come first.
2558 Next come all of the back-end allocated local dynamic syms,
2559 followed by the rest of the global symbols. */
e92d460e 2560
45d6a902 2561 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
bc2b6df7 2562
45d6a902
AM
2563 /* Work out the size of the symbol version section. */
2564 s = bfd_get_section_by_name (dynobj, ".gnu.version");
2565 BFD_ASSERT (s != NULL);
2566 if (dynsymcount == 0
2567 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
252b5132 2568 {
45d6a902
AM
2569 _bfd_strip_section_from_output (info, s);
2570 /* The DYNSYMCOUNT might have changed if we were going to
2571 output a dynamic symbol table entry for S. */
2572 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
252b5132 2573 }
45d6a902 2574 else
c44233aa 2575 {
45d6a902 2576 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
268b6b39 2577 s->contents = bfd_zalloc (output_bfd, s->_raw_size);
45d6a902
AM
2578 if (s->contents == NULL)
2579 return FALSE;
252b5132 2580
268b6b39 2581 if (! elf_add_dynamic_entry (info, DT_VERSYM, 0))
45d6a902
AM
2582 return FALSE;
2583 }
252b5132 2584
45d6a902
AM
2585 /* Set the size of the .dynsym and .hash sections. We counted
2586 the number of dynamic symbols in elf_link_add_object_symbols.
2587 We will build the contents of .dynsym and .hash when we build
2588 the final symbol table, because until then we do not know the
2589 correct value to give the symbols. We built the .dynstr
2590 section as we went along in elf_link_add_object_symbols. */
2591 s = bfd_get_section_by_name (dynobj, ".dynsym");
2592 BFD_ASSERT (s != NULL);
2593 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
268b6b39 2594 s->contents = bfd_alloc (output_bfd, s->_raw_size);
45d6a902
AM
2595 if (s->contents == NULL && s->_raw_size != 0)
2596 return FALSE;
252b5132 2597
45d6a902 2598 if (dynsymcount != 0)
252b5132 2599 {
45d6a902
AM
2600 Elf_Internal_Sym isym;
2601
2602 /* The first entry in .dynsym is a dummy symbol. */
2603 isym.st_value = 0;
2604 isym.st_size = 0;
2605 isym.st_name = 0;
2606 isym.st_info = 0;
2607 isym.st_other = 0;
2608 isym.st_shndx = 0;
268b6b39 2609 elf_swap_symbol_out (output_bfd, &isym, s->contents, 0);
252b5132
RH
2610 }
2611
45d6a902
AM
2612 /* Compute the size of the hashing table. As a side effect this
2613 computes the hash values for all the names we export. */
2614 bucketcount = compute_bucket_count (info);
2615
2616 s = bfd_get_section_by_name (dynobj, ".hash");
2617 BFD_ASSERT (s != NULL);
2618 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
2619 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
268b6b39 2620 s->contents = bfd_zalloc (output_bfd, s->_raw_size);
45d6a902
AM
2621 if (s->contents == NULL)
2622 return FALSE;
252b5132 2623
268b6b39
AM
2624 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
2625 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
45d6a902 2626 s->contents + hash_entry_size);
252b5132 2627
45d6a902 2628 elf_hash_table (info)->bucketcount = bucketcount;
252b5132 2629
45d6a902
AM
2630 s = bfd_get_section_by_name (dynobj, ".dynstr");
2631 BFD_ASSERT (s != NULL);
252b5132 2632
45d6a902 2633 elf_finalize_dynstr (output_bfd, info);
252b5132 2634
45d6a902 2635 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
252b5132 2636
45d6a902 2637 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
268b6b39 2638 if (! elf_add_dynamic_entry (info, DT_NULL, 0))
45d6a902
AM
2639 return FALSE;
2640 }
252b5132 2641
b34976b6 2642 return TRUE;
252b5132 2643}
45d6a902
AM
2644\f
2645/* This function is used to adjust offsets into .dynstr for
2646 dynamic symbols. This is called via elf_link_hash_traverse. */
252b5132 2647
b34976b6 2648static bfd_boolean
268b6b39 2649elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
252b5132 2650{
268b6b39 2651 struct elf_strtab_hash *dynstr = data;
252b5132 2652
e92d460e
AM
2653 if (h->root.type == bfd_link_hash_warning)
2654 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2655
45d6a902
AM
2656 if (h->dynindx != -1)
2657 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2658 return TRUE;
2659}
252b5132 2660
45d6a902
AM
2661/* Assign string offsets in .dynstr, update all structures referencing
2662 them. */
252b5132 2663
45d6a902 2664static bfd_boolean
268b6b39 2665elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
45d6a902
AM
2666{
2667 struct elf_link_local_dynamic_entry *entry;
2668 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
2669 bfd *dynobj = elf_hash_table (info)->dynobj;
2670 asection *sdyn;
2671 bfd_size_type size;
2672 Elf_External_Dyn *dyncon, *dynconend;
252b5132 2673
45d6a902
AM
2674 _bfd_elf_strtab_finalize (dynstr);
2675 size = _bfd_elf_strtab_size (dynstr);
252b5132 2676
45d6a902
AM
2677 /* Update all .dynamic entries referencing .dynstr strings. */
2678 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2679 BFD_ASSERT (sdyn != NULL);
252b5132 2680
45d6a902
AM
2681 dyncon = (Elf_External_Dyn *) sdyn->contents;
2682 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2683 sdyn->_raw_size);
2684 for (; dyncon < dynconend; dyncon++)
2685 {
2686 Elf_Internal_Dyn dyn;
252b5132 2687
45d6a902
AM
2688 elf_swap_dyn_in (dynobj, dyncon, & dyn);
2689 switch (dyn.d_tag)
252b5132 2690 {
45d6a902
AM
2691 case DT_STRSZ:
2692 dyn.d_un.d_val = size;
2693 elf_swap_dyn_out (dynobj, & dyn, dyncon);
2694 break;
2695 case DT_NEEDED:
2696 case DT_SONAME:
2697 case DT_RPATH:
2698 case DT_RUNPATH:
2699 case DT_FILTER:
2700 case DT_AUXILIARY:
2701 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2702 elf_swap_dyn_out (dynobj, & dyn, dyncon);
2703 break;
2704 default:
2705 break;
2706 }
2707 }
252b5132 2708
45d6a902
AM
2709 /* Now update local dynamic symbols. */
2710 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2711 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2712 entry->isym.st_name);
252b5132 2713
45d6a902
AM
2714 /* And the rest of dynamic symbols. */
2715 elf_link_hash_traverse (elf_hash_table (info),
2716 elf_adjust_dynstr_offsets, dynstr);
252b5132 2717
45d6a902
AM
2718 /* Adjust version definitions. */
2719 if (elf_tdata (output_bfd)->cverdefs)
2720 {
2721 asection *s;
2722 bfd_byte *p;
2723 bfd_size_type i;
2724 Elf_Internal_Verdef def;
2725 Elf_Internal_Verdaux defaux;
252b5132 2726
45d6a902
AM
2727 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2728 p = (bfd_byte *) s->contents;
2729 do
252b5132 2730 {
45d6a902
AM
2731 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2732 &def);
2733 p += sizeof (Elf_External_Verdef);
2734 for (i = 0; i < def.vd_cnt; ++i)
252b5132 2735 {
45d6a902
AM
2736 _bfd_elf_swap_verdaux_in (output_bfd,
2737 (Elf_External_Verdaux *) p, &defaux);
2738 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2739 defaux.vda_name);
2740 _bfd_elf_swap_verdaux_out (output_bfd,
2741 &defaux, (Elf_External_Verdaux *) p);
2742 p += sizeof (Elf_External_Verdaux);
252b5132 2743 }
252b5132 2744 }
45d6a902 2745 while (def.vd_next);
252b5132
RH
2746 }
2747
45d6a902
AM
2748 /* Adjust version references. */
2749 if (elf_tdata (output_bfd)->verref)
252b5132 2750 {
45d6a902
AM
2751 asection *s;
2752 bfd_byte *p;
2753 bfd_size_type i;
2754 Elf_Internal_Verneed need;
2755 Elf_Internal_Vernaux needaux;
252b5132 2756
45d6a902
AM
2757 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2758 p = (bfd_byte *) s->contents;
2759 do
252b5132 2760 {
45d6a902
AM
2761 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2762 &need);
2763 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2764 _bfd_elf_swap_verneed_out (output_bfd, &need,
2765 (Elf_External_Verneed *) p);
2766 p += sizeof (Elf_External_Verneed);
2767 for (i = 0; i < need.vn_cnt; ++i)
252b5132 2768 {
45d6a902
AM
2769 _bfd_elf_swap_vernaux_in (output_bfd,
2770 (Elf_External_Vernaux *) p, &needaux);
2771 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2772 needaux.vna_name);
2773 _bfd_elf_swap_vernaux_out (output_bfd,
2774 &needaux,
2775 (Elf_External_Vernaux *) p);
2776 p += sizeof (Elf_External_Vernaux);
252b5132
RH
2777 }
2778 }
45d6a902 2779 while (need.vn_next);
252b5132
RH
2780 }
2781
b34976b6 2782 return TRUE;
252b5132 2783}
252b5132
RH
2784\f
2785/* Final phase of ELF linker. */
2786
2787/* A structure we use to avoid passing large numbers of arguments. */
2788
2789struct elf_final_link_info
2790{
2791 /* General link information. */
2792 struct bfd_link_info *info;
2793 /* Output BFD. */
2794 bfd *output_bfd;
2795 /* Symbol string table. */
2796 struct bfd_strtab_hash *symstrtab;
2797 /* .dynsym section. */
2798 asection *dynsym_sec;
2799 /* .hash section. */
2800 asection *hash_sec;
2801 /* symbol version section (.gnu.version). */
2802 asection *symver_sec;
2803 /* Buffer large enough to hold contents of any section. */
2804 bfd_byte *contents;
2805 /* Buffer large enough to hold external relocs of any section. */
268b6b39 2806 void *external_relocs;
252b5132
RH
2807 /* Buffer large enough to hold internal relocs of any section. */
2808 Elf_Internal_Rela *internal_relocs;
2809 /* Buffer large enough to hold external local symbols of any input
2810 BFD. */
2811 Elf_External_Sym *external_syms;
9ad5cbcf
AM
2812 /* And a buffer for symbol section indices. */
2813 Elf_External_Sym_Shndx *locsym_shndx;
252b5132
RH
2814 /* Buffer large enough to hold internal local symbols of any input
2815 BFD. */
2816 Elf_Internal_Sym *internal_syms;
2817 /* Array large enough to hold a symbol index for each local symbol
2818 of any input BFD. */
2819 long *indices;
2820 /* Array large enough to hold a section pointer for each local
2821 symbol of any input BFD. */
2822 asection **sections;
2823 /* Buffer to hold swapped out symbols. */
2824 Elf_External_Sym *symbuf;
9ad5cbcf
AM
2825 /* And one for symbol section indices. */
2826 Elf_External_Sym_Shndx *symshndxbuf;
252b5132
RH
2827 /* Number of swapped out symbols in buffer. */
2828 size_t symbuf_count;
2829 /* Number of symbols which fit in symbuf. */
2830 size_t symbuf_size;
c97e73dd
AM
2831 /* And same for symshndxbuf. */
2832 size_t shndxbuf_size;
252b5132
RH
2833};
2834
b34976b6 2835static bfd_boolean elf_link_output_sym
754021d0
AM
2836 (struct elf_final_link_info *, const char *, Elf_Internal_Sym *, asection *,
2837 struct elf_link_hash_entry *);
b34976b6 2838static bfd_boolean elf_link_flush_output_syms
268b6b39 2839 (struct elf_final_link_info *);
b34976b6 2840static bfd_boolean elf_link_output_extsym
268b6b39 2841 (struct elf_link_hash_entry *, void *);
b34976b6 2842static bfd_boolean elf_link_input_bfd
268b6b39 2843 (struct elf_final_link_info *, bfd *);
b34976b6 2844static bfd_boolean elf_reloc_link_order
268b6b39 2845 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
252b5132
RH
2846
2847/* This struct is used to pass information to elf_link_output_extsym. */
2848
2849struct elf_outext_info
2850{
b34976b6
AM
2851 bfd_boolean failed;
2852 bfd_boolean localsyms;
252b5132
RH
2853 struct elf_final_link_info *finfo;
2854};
2855
1049f94e 2856/* When performing a relocatable link, the input relocations are
31367b81
MM
2857 preserved. But, if they reference global symbols, the indices
2858 referenced must be updated. Update all the relocations in
2859 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
2860
2861static void
268b6b39
AM
2862elf_link_adjust_relocs (bfd *abfd,
2863 Elf_Internal_Shdr *rel_hdr,
2864 unsigned int count,
2865 struct elf_link_hash_entry **rel_hash)
31367b81
MM
2866{
2867 unsigned int i;
9c5bfbb7 2868 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
947216bf 2869 bfd_byte *erela;
268b6b39
AM
2870 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2871 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
209f668e 2872
947216bf 2873 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
209f668e 2874 {
947216bf
AM
2875 swap_in = bed->s->swap_reloc_in;
2876 swap_out = bed->s->swap_reloc_out;
209f668e 2877 }
947216bf 2878 else if (rel_hdr->sh_entsize == sizeof (Elf_External_Rela))
209f668e 2879 {
947216bf
AM
2880 swap_in = bed->s->swap_reloca_in;
2881 swap_out = bed->s->swap_reloca_out;
209f668e 2882 }
947216bf
AM
2883 else
2884 abort ();
2885
2886 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
2887 abort ();
31367b81 2888
947216bf
AM
2889 erela = rel_hdr->contents;
2890 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
31367b81 2891 {
947216bf
AM
2892 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
2893 unsigned int j;
2894
31367b81
MM
2895 if (*rel_hash == NULL)
2896 continue;
2897
2898 BFD_ASSERT ((*rel_hash)->indx >= 0);
2899
947216bf
AM
2900 (*swap_in) (abfd, erela, irela);
2901 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
2902 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
2903 ELF_R_TYPE (irela[j].r_info));
2904 (*swap_out) (abfd, irela, erela);
31367b81
MM
2905 }
2906}
2907
58821868
AM
2908struct elf_link_sort_rela
2909{
db6751f2
JJ
2910 bfd_vma offset;
2911 enum elf_reloc_type_class type;
947216bf
AM
2912 /* We use this as an array of size int_rels_per_ext_rel. */
2913 Elf_Internal_Rela rela[1];
db6751f2
JJ
2914};
2915
2916static int
268b6b39 2917elf_link_sort_cmp1 (const void *A, const void *B)
db6751f2 2918{
268b6b39
AM
2919 const struct elf_link_sort_rela *a = A;
2920 const struct elf_link_sort_rela *b = B;
db6751f2
JJ
2921 int relativea, relativeb;
2922
2923 relativea = a->type == reloc_class_relative;
2924 relativeb = b->type == reloc_class_relative;
2925
2926 if (relativea < relativeb)
db6751f2 2927 return 1;
fcfbdf31
JJ
2928 if (relativea > relativeb)
2929 return -1;
947216bf 2930 if (ELF_R_SYM (a->rela->r_info) < ELF_R_SYM (b->rela->r_info))
db6751f2 2931 return -1;
947216bf 2932 if (ELF_R_SYM (a->rela->r_info) > ELF_R_SYM (b->rela->r_info))
db6751f2 2933 return 1;
947216bf 2934 if (a->rela->r_offset < b->rela->r_offset)
db6751f2 2935 return -1;
947216bf 2936 if (a->rela->r_offset > b->rela->r_offset)
db6751f2
JJ
2937 return 1;
2938 return 0;
2939}
2940
2941static int
268b6b39 2942elf_link_sort_cmp2 (const void *A, const void *B)
db6751f2 2943{
268b6b39
AM
2944 const struct elf_link_sort_rela *a = A;
2945 const struct elf_link_sort_rela *b = B;
db6751f2
JJ
2946 int copya, copyb;
2947
2948 if (a->offset < b->offset)
2949 return -1;
2950 if (a->offset > b->offset)
2951 return 1;
290394d6
JJ
2952 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
2953 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
db6751f2
JJ
2954 if (copya < copyb)
2955 return -1;
2956 if (copya > copyb)
2957 return 1;
947216bf 2958 if (a->rela->r_offset < b->rela->r_offset)
db6751f2 2959 return -1;
947216bf 2960 if (a->rela->r_offset > b->rela->r_offset)
db6751f2
JJ
2961 return 1;
2962 return 0;
2963}
2964
2965static size_t
268b6b39 2966elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
db6751f2 2967{
e717da7e 2968 asection *reldyn;
f51e552e 2969 bfd_size_type count, size;
947216bf
AM
2970 size_t i, ret, sort_elt, ext_size;
2971 bfd_byte *sort, *s_non_relative, *p;
2972 struct elf_link_sort_rela *sq;
9c5bfbb7 2973 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
033fd5f9 2974 int i2e = bed->s->int_rels_per_ext_rel;
268b6b39
AM
2975 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2976 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
e717da7e 2977 struct bfd_link_order *lo;
db6751f2
JJ
2978
2979 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
2980 if (reldyn == NULL || reldyn->_raw_size == 0)
2981 {
2982 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
2983 if (reldyn == NULL || reldyn->_raw_size == 0)
2984 return 0;
947216bf
AM
2985 ext_size = sizeof (Elf_External_Rel);
2986 swap_in = bed->s->swap_reloc_in;
2987 swap_out = bed->s->swap_reloc_out;
db6751f2
JJ
2988 }
2989 else
947216bf
AM
2990 {
2991 ext_size = sizeof (Elf_External_Rela);
2992 swap_in = bed->s->swap_reloca_in;
2993 swap_out = bed->s->swap_reloca_out;
2994 }
2995 count = reldyn->_raw_size / ext_size;
db6751f2
JJ
2996
2997 size = 0;
e717da7e
AM
2998 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
2999 if (lo->type == bfd_indirect_link_order)
3000 {
3001 asection *o = lo->u.indirect.section;
3002 size += o->_raw_size;
3003 }
db6751f2
JJ
3004
3005 if (size != reldyn->_raw_size)
3006 return 0;
3007
947216bf
AM
3008 sort_elt = (sizeof (struct elf_link_sort_rela)
3009 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3010 sort = bfd_zmalloc (sort_elt * count);
3011 if (sort == NULL)
db6751f2
JJ
3012 {
3013 (*info->callbacks->warning)
268b6b39 3014 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
db6751f2
JJ
3015 return 0;
3016 }
3017
e717da7e
AM
3018 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
3019 if (lo->type == bfd_indirect_link_order)
db6751f2 3020 {
947216bf 3021 bfd_byte *erel, *erelend;
e717da7e 3022 asection *o = lo->u.indirect.section;
db6751f2 3023
947216bf
AM
3024 erel = o->contents;
3025 erelend = o->contents + o->_raw_size;
3026 p = sort + o->output_offset / ext_size * sort_elt;
3027 while (erel < erelend)
db6751f2 3028 {
947216bf
AM
3029 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3030 (*swap_in) (abfd, erel, s->rela);
3031 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
3032 p += sort_elt;
3033 erel += ext_size;
db6751f2
JJ
3034 }
3035 }
3036
268b6b39 3037 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
947216bf
AM
3038
3039 for (i = 0, p = sort; i < count; i++, p += sort_elt)
db6751f2 3040 {
947216bf
AM
3041 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3042 if (s->type != reloc_class_relative)
3043 break;
db6751f2 3044 }
947216bf
AM
3045 ret = i;
3046 s_non_relative = p;
3047
3048 sq = (struct elf_link_sort_rela *) s_non_relative;
3049 for (; i < count; i++, p += sort_elt)
3050 {
3051 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
3052 if (ELF_R_SYM (sp->rela->r_info) != ELF_R_SYM (sq->rela->r_info))
3053 sq = sp;
3054 sp->offset = sq->rela->r_offset;
3055 }
3056
268b6b39 3057 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
dc810e39 3058
e717da7e
AM
3059 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
3060 if (lo->type == bfd_indirect_link_order)
db6751f2 3061 {
947216bf 3062 bfd_byte *erel, *erelend;
e717da7e 3063 asection *o = lo->u.indirect.section;
db6751f2 3064
947216bf
AM
3065 erel = o->contents;
3066 erelend = o->contents + o->_raw_size;
3067 p = sort + o->output_offset / ext_size * sort_elt;
3068 while (erel < erelend)
db6751f2 3069 {
947216bf
AM
3070 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3071 (*swap_out) (abfd, s->rela, erel);
3072 p += sort_elt;
3073 erel += ext_size;
db6751f2
JJ
3074 }
3075 }
3076
5ed6aba4 3077 free (sort);
db6751f2
JJ
3078 *psec = reldyn;
3079 return ret;
3080}
3081
252b5132
RH
3082/* Do the final step of an ELF link. */
3083
b34976b6 3084bfd_boolean
268b6b39 3085elf_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
252b5132 3086{
b34976b6
AM
3087 bfd_boolean dynamic;
3088 bfd_boolean emit_relocs;
252b5132
RH
3089 bfd *dynobj;
3090 struct elf_final_link_info finfo;
3091 register asection *o;
3092 register struct bfd_link_order *p;
3093 register bfd *sub;
dc810e39
AM
3094 bfd_size_type max_contents_size;
3095 bfd_size_type max_external_reloc_size;
3096 bfd_size_type max_internal_reloc_count;
3097 bfd_size_type max_sym_count;
9ad5cbcf 3098 bfd_size_type max_sym_shndx_count;
252b5132
RH
3099 file_ptr off;
3100 Elf_Internal_Sym elfsym;
3101 unsigned int i;
3102 Elf_Internal_Shdr *symtab_hdr;
c97e73dd 3103 Elf_Internal_Shdr *symtab_shndx_hdr;
252b5132 3104 Elf_Internal_Shdr *symstrtab_hdr;
9c5bfbb7 3105 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 3106 struct elf_outext_info eoinfo;
b34976b6 3107 bfd_boolean merged;
db6751f2
JJ
3108 size_t relativecount = 0;
3109 asection *reldyn = 0;
dc810e39 3110 bfd_size_type amt;
252b5132 3111
0eddce27 3112 if (! is_elf_hash_table (info->hash))
b34976b6 3113 return FALSE;
8ea2e4bd 3114
252b5132
RH
3115 if (info->shared)
3116 abfd->flags |= DYNAMIC;
3117
3118 dynamic = elf_hash_table (info)->dynamic_sections_created;
3119 dynobj = elf_hash_table (info)->dynobj;
3120
1049f94e 3121 emit_relocs = (info->relocatable
c44233aa
AM
3122 || info->emitrelocations
3123 || bed->elf_backend_emit_relocs);
9317eacc 3124
252b5132
RH
3125 finfo.info = info;
3126 finfo.output_bfd = abfd;
3127 finfo.symstrtab = elf_stringtab_init ();
3128 if (finfo.symstrtab == NULL)
b34976b6 3129 return FALSE;
252b5132
RH
3130
3131 if (! dynamic)
3132 {
3133 finfo.dynsym_sec = NULL;
3134 finfo.hash_sec = NULL;
3135 finfo.symver_sec = NULL;
3136 }
3137 else
3138 {
3139 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
3140 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
3141 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
3142 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
3143 /* Note that it is OK if symver_sec is NULL. */
3144 }
3145
3146 finfo.contents = NULL;
3147 finfo.external_relocs = NULL;
3148 finfo.internal_relocs = NULL;
3149 finfo.external_syms = NULL;
9ad5cbcf 3150 finfo.locsym_shndx = NULL;
252b5132
RH
3151 finfo.internal_syms = NULL;
3152 finfo.indices = NULL;
3153 finfo.sections = NULL;
3154 finfo.symbuf = NULL;
9ad5cbcf 3155 finfo.symshndxbuf = NULL;
252b5132 3156 finfo.symbuf_count = 0;
c97e73dd 3157 finfo.shndxbuf_size = 0;
252b5132
RH
3158
3159 /* Count up the number of relocations we will output for each output
3160 section, so that we know the sizes of the reloc sections. We
3161 also figure out some maximum sizes. */
3162 max_contents_size = 0;
3163 max_external_reloc_size = 0;
3164 max_internal_reloc_count = 0;
3165 max_sym_count = 0;
9ad5cbcf 3166 max_sym_shndx_count = 0;
b34976b6 3167 merged = FALSE;
268b6b39 3168 for (o = abfd->sections; o != NULL; o = o->next)
252b5132 3169 {
3f9a32bd 3170 struct bfd_elf_section_data *esdo = elf_section_data (o);
252b5132
RH
3171 o->reloc_count = 0;
3172
3173 for (p = o->link_order_head; p != NULL; p = p->next)
3174 {
3f9a32bd
AM
3175 unsigned int reloc_count = 0;
3176 struct bfd_elf_section_data *esdi = NULL;
3177 unsigned int *rel_count1;
3178
252b5132
RH
3179 if (p->type == bfd_section_reloc_link_order
3180 || p->type == bfd_symbol_reloc_link_order)
3f9a32bd 3181 reloc_count = 1;
252b5132
RH
3182 else if (p->type == bfd_indirect_link_order)
3183 {
3184 asection *sec;
3185
3186 sec = p->u.indirect.section;
3f9a32bd 3187 esdi = elf_section_data (sec);
252b5132
RH
3188
3189 /* Mark all sections which are to be included in the
3190 link. This will normally be every section. We need
3191 to do this so that we can identify any sections which
3192 the linker has decided to not include. */
b34976b6 3193 sec->linker_mark = TRUE;
252b5132 3194
f5fa8ca2 3195 if (sec->flags & SEC_MERGE)
b34976b6 3196 merged = TRUE;
f5fa8ca2 3197
1049f94e 3198 if (info->relocatable || info->emitrelocations)
3f9a32bd 3199 reloc_count = sec->reloc_count;
c44233aa 3200 else if (bed->elf_backend_count_relocs)
9317eacc
CM
3201 {
3202 Elf_Internal_Rela * relocs;
3203
268b6b39
AM
3204 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
3205 info->keep_memory);
9317eacc 3206
3f9a32bd 3207 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
9317eacc 3208
6cdc0ccc 3209 if (elf_section_data (o)->relocs != relocs)
9317eacc
CM
3210 free (relocs);
3211 }
252b5132
RH
3212
3213 if (sec->_raw_size > max_contents_size)
3214 max_contents_size = sec->_raw_size;
3215 if (sec->_cooked_size > max_contents_size)
3216 max_contents_size = sec->_cooked_size;
3217
3218 /* We are interested in just local symbols, not all
3219 symbols. */
3220 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
3221 && (sec->owner->flags & DYNAMIC) == 0)
3222 {
3223 size_t sym_count;
3224
3225 if (elf_bad_symtab (sec->owner))
3226 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
3227 / sizeof (Elf_External_Sym));
3228 else
3229 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
3230
3231 if (sym_count > max_sym_count)
3232 max_sym_count = sym_count;
3233
9ad5cbcf
AM
3234 if (sym_count > max_sym_shndx_count
3235 && elf_symtab_shndx (sec->owner) != 0)
3236 max_sym_shndx_count = sym_count;
3237
252b5132
RH
3238 if ((sec->flags & SEC_RELOC) != 0)
3239 {
3240 size_t ext_size;
3241
3242 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
3243 if (ext_size > max_external_reloc_size)
3244 max_external_reloc_size = ext_size;
3245 if (sec->reloc_count > max_internal_reloc_count)
3246 max_internal_reloc_count = sec->reloc_count;
3247 }
3248 }
3249 }
3f9a32bd
AM
3250
3251 if (reloc_count == 0)
3252 continue;
3253
3254 o->reloc_count += reloc_count;
3255
3256 /* MIPS may have a mix of REL and RELA relocs on sections.
3257 To support this curious ABI we keep reloc counts in
3258 elf_section_data too. We must be careful to add the
3259 relocations from the input section to the right output
3260 count. FIXME: Get rid of one count. We have
3261 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
3262 rel_count1 = &esdo->rel_count;
3263 if (esdi != NULL)
3264 {
3265 bfd_boolean same_size;
3266 bfd_size_type entsize1;
3267
3268 entsize1 = esdi->rel_hdr.sh_entsize;
3269 BFD_ASSERT (entsize1 == sizeof (Elf_External_Rel)
3270 || entsize1 == sizeof (Elf_External_Rela));
3271 same_size = (!o->use_rela_p
3272 == (entsize1 == sizeof (Elf_External_Rel)));
3273
3274 if (!same_size)
3275 rel_count1 = &esdo->rel_count2;
3276
3277 if (esdi->rel_hdr2 != NULL)
3278 {
3279 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
3280 unsigned int alt_count;
3281 unsigned int *rel_count2;
3282
3283 BFD_ASSERT (entsize2 != entsize1
3284 && (entsize2 == sizeof (Elf_External_Rel)
3285 || entsize2 == sizeof (Elf_External_Rela)));
3286
3287 rel_count2 = &esdo->rel_count2;
3288 if (!same_size)
3289 rel_count2 = &esdo->rel_count;
3290
3291 /* The following is probably too simplistic if the
3292 backend counts output relocs unusually. */
3293 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
3294 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
3295 *rel_count2 += alt_count;
3296 reloc_count -= alt_count;
3297 }
3298 }
3299 *rel_count1 += reloc_count;
252b5132
RH
3300 }
3301
3302 if (o->reloc_count > 0)
3303 o->flags |= SEC_RELOC;
3304 else
3305 {
3306 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3307 set it (this is probably a bug) and if it is set
3308 assign_section_numbers will create a reloc section. */
3309 o->flags &=~ SEC_RELOC;
3310 }
3311
3312 /* If the SEC_ALLOC flag is not set, force the section VMA to
3313 zero. This is done in elf_fake_sections as well, but forcing
3314 the VMA to 0 here will ensure that relocs against these
3315 sections are handled correctly. */
3316 if ((o->flags & SEC_ALLOC) == 0
3317 && ! o->user_set_vma)
3318 o->vma = 0;
3319 }
3320
1049f94e 3321 if (! info->relocatable && merged)
f5fa8ca2 3322 elf_link_hash_traverse (elf_hash_table (info),
268b6b39 3323 _bfd_elf_link_sec_merge_syms, abfd);
f5fa8ca2 3324
252b5132
RH
3325 /* Figure out the file positions for everything but the symbol table
3326 and the relocs. We set symcount to force assign_section_numbers
3327 to create a symbol table. */
3328 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
3329 BFD_ASSERT (! abfd->output_has_begun);
3330 if (! _bfd_elf_compute_section_file_positions (abfd, info))
3331 goto error_return;
3332
3333 /* That created the reloc sections. Set their sizes, and assign
3334 them file positions, and allocate some buffers. */
3335 for (o = abfd->sections; o != NULL; o = o->next)
3336 {
3337 if ((o->flags & SEC_RELOC) != 0)
3338 {
45d6a902
AM
3339 if (!(_bfd_elf_link_size_reloc_section
3340 (abfd, &elf_section_data (o)->rel_hdr, o)))
252b5132
RH
3341 goto error_return;
3342
23bc299b 3343 if (elf_section_data (o)->rel_hdr2
45d6a902
AM
3344 && !(_bfd_elf_link_size_reloc_section
3345 (abfd, elf_section_data (o)->rel_hdr2, o)))
252b5132 3346 goto error_return;
252b5132 3347 }
b037af20
MM
3348
3349 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
3e932841 3350 to count upwards while actually outputting the relocations. */
b037af20
MM
3351 elf_section_data (o)->rel_count = 0;
3352 elf_section_data (o)->rel_count2 = 0;
252b5132
RH
3353 }
3354
3355 _bfd_elf_assign_file_positions_for_relocs (abfd);
3356
3357 /* We have now assigned file positions for all the sections except
3358 .symtab and .strtab. We start the .symtab section at the current
3359 file position, and write directly to it. We build the .strtab
3360 section in memory. */
3361 bfd_get_symcount (abfd) = 0;
3362 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3363 /* sh_name is set in prep_headers. */
3364 symtab_hdr->sh_type = SHT_SYMTAB;
c97e73dd 3365 /* sh_flags, sh_addr and sh_size all start off zero. */
252b5132
RH
3366 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
3367 /* sh_link is set in assign_section_numbers. */
3368 /* sh_info is set below. */
3369 /* sh_offset is set just below. */
45d6a902 3370 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
252b5132
RH
3371
3372 off = elf_tdata (abfd)->next_file_pos;
b34976b6 3373 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
252b5132
RH
3374
3375 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3376 incorrect. We do not yet know the size of the .symtab section.
3377 We correct next_file_pos below, after we do know the size. */
3378
3379 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3380 continuously seeking to the right position in the file. */
3381 if (! info->keep_memory || max_sym_count < 20)
3382 finfo.symbuf_size = 20;
3383 else
3384 finfo.symbuf_size = max_sym_count;
dc810e39
AM
3385 amt = finfo.symbuf_size;
3386 amt *= sizeof (Elf_External_Sym);
268b6b39 3387 finfo.symbuf = bfd_malloc (amt);
252b5132
RH
3388 if (finfo.symbuf == NULL)
3389 goto error_return;
9ad5cbcf
AM
3390 if (elf_numsections (abfd) > SHN_LORESERVE)
3391 {
c97e73dd
AM
3392 /* Wild guess at number of output symbols. realloc'd as needed. */
3393 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
3394 finfo.shndxbuf_size = amt;
9ad5cbcf 3395 amt *= sizeof (Elf_External_Sym_Shndx);
268b6b39 3396 finfo.symshndxbuf = bfd_zmalloc (amt);
9ad5cbcf
AM
3397 if (finfo.symshndxbuf == NULL)
3398 goto error_return;
3399 }
252b5132
RH
3400
3401 /* Start writing out the symbol table. The first symbol is always a
3402 dummy symbol. */
9317eacc
CM
3403 if (info->strip != strip_all
3404 || emit_relocs)
252b5132
RH
3405 {
3406 elfsym.st_value = 0;
3407 elfsym.st_size = 0;
3408 elfsym.st_info = 0;
3409 elfsym.st_other = 0;
3410 elfsym.st_shndx = SHN_UNDEF;
754021d0
AM
3411 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
3412 NULL))
252b5132
RH
3413 goto error_return;
3414 }
3415
3416#if 0
3417 /* Some standard ELF linkers do this, but we don't because it causes
3418 bootstrap comparison failures. */
3419 /* Output a file symbol for the output file as the second symbol.
3420 We output this even if we are discarding local symbols, although
3421 I'm not sure if this is correct. */
3422 elfsym.st_value = 0;
3423 elfsym.st_size = 0;
3424 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3425 elfsym.st_other = 0;
3426 elfsym.st_shndx = SHN_ABS;
3427 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
754021d0 3428 &elfsym, bfd_abs_section_ptr, NULL))
252b5132
RH
3429 goto error_return;
3430#endif
3431
3432 /* Output a symbol for each section. We output these even if we are
3433 discarding local symbols, since they are used for relocs. These
3434 symbols have no names. We store the index of each one in the
3435 index field of the section, so that we can find it again when
3436 outputting relocs. */
9317eacc
CM
3437 if (info->strip != strip_all
3438 || emit_relocs)
252b5132
RH
3439 {
3440 elfsym.st_size = 0;
3441 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3442 elfsym.st_other = 0;
9ad5cbcf 3443 for (i = 1; i < elf_numsections (abfd); i++)
252b5132
RH
3444 {
3445 o = section_from_elf_index (abfd, i);
3446 if (o != NULL)
3447 o->target_index = bfd_get_symcount (abfd);
3448 elfsym.st_shndx = i;
1049f94e 3449 if (info->relocatable || o == NULL)
252b5132
RH
3450 elfsym.st_value = 0;
3451 else
3452 elfsym.st_value = o->vma;
754021d0 3453 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
252b5132 3454 goto error_return;
c97e73dd 3455 if (i == SHN_LORESERVE - 1)
9ad5cbcf 3456 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132
RH
3457 }
3458 }
3459
3460 /* Allocate some memory to hold information read in from the input
3461 files. */
9ad5cbcf
AM
3462 if (max_contents_size != 0)
3463 {
268b6b39 3464 finfo.contents = bfd_malloc (max_contents_size);
9ad5cbcf
AM
3465 if (finfo.contents == NULL)
3466 goto error_return;
3467 }
3468
3469 if (max_external_reloc_size != 0)
3470 {
268b6b39 3471 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
9ad5cbcf
AM
3472 if (finfo.external_relocs == NULL)
3473 goto error_return;
3474 }
3475
3476 if (max_internal_reloc_count != 0)
3477 {
3478 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
3479 amt *= sizeof (Elf_Internal_Rela);
268b6b39 3480 finfo.internal_relocs = bfd_malloc (amt);
9ad5cbcf
AM
3481 if (finfo.internal_relocs == NULL)
3482 goto error_return;
3483 }
3484
3485 if (max_sym_count != 0)
3486 {
3487 amt = max_sym_count * sizeof (Elf_External_Sym);
268b6b39 3488 finfo.external_syms = bfd_malloc (amt);
9ad5cbcf
AM
3489 if (finfo.external_syms == NULL)
3490 goto error_return;
3491
3492 amt = max_sym_count * sizeof (Elf_Internal_Sym);
268b6b39 3493 finfo.internal_syms = bfd_malloc (amt);
9ad5cbcf
AM
3494 if (finfo.internal_syms == NULL)
3495 goto error_return;
3496
3497 amt = max_sym_count * sizeof (long);
268b6b39 3498 finfo.indices = bfd_malloc (amt);
9ad5cbcf
AM
3499 if (finfo.indices == NULL)
3500 goto error_return;
3501
3502 amt = max_sym_count * sizeof (asection *);
268b6b39 3503 finfo.sections = bfd_malloc (amt);
9ad5cbcf
AM
3504 if (finfo.sections == NULL)
3505 goto error_return;
3506 }
3507
3508 if (max_sym_shndx_count != 0)
3509 {
3510 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
268b6b39 3511 finfo.locsym_shndx = bfd_malloc (amt);
9ad5cbcf
AM
3512 if (finfo.locsym_shndx == NULL)
3513 goto error_return;
3514 }
252b5132 3515
e1918d23 3516 if (elf_hash_table (info)->tls_sec)
13ae64f3 3517 {
e1918d23 3518 bfd_vma base, end = 0;
13ae64f3
JJ
3519 asection *sec;
3520
e1918d23 3521 for (sec = elf_hash_table (info)->tls_sec;
13ae64f3
JJ
3522 sec && (sec->flags & SEC_THREAD_LOCAL);
3523 sec = sec->next)
3524 {
3525 bfd_vma size = sec->_raw_size;
3526
e1918d23 3527 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
13ae64f3
JJ
3528 {
3529 struct bfd_link_order *o;
3530
13ae64f3
JJ
3531 for (o = sec->link_order_head; o != NULL; o = o->next)
3532 if (size < o->offset + o->size)
58821868 3533 size = o->offset + o->size;
13ae64f3
JJ
3534 }
3535 end = sec->vma + size;
3536 }
e1918d23
AM
3537 base = elf_hash_table (info)->tls_sec->vma;
3538 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
3539 elf_hash_table (info)->tls_size = end - base;
13ae64f3
JJ
3540 }
3541
252b5132
RH
3542 /* Since ELF permits relocations to be against local symbols, we
3543 must have the local symbols available when we do the relocations.
3544 Since we would rather only read the local symbols once, and we
3545 would rather not keep them in memory, we handle all the
3546 relocations for a single input file at the same time.
3547
3548 Unfortunately, there is no way to know the total number of local
3549 symbols until we have seen all of them, and the local symbol
3550 indices precede the global symbol indices. This means that when
1049f94e 3551 we are generating relocatable output, and we see a reloc against
252b5132
RH
3552 a global symbol, we can not know the symbol index until we have
3553 finished examining all the local symbols to see which ones we are
3554 going to output. To deal with this, we keep the relocations in
3555 memory, and don't output them until the end of the link. This is
3556 an unfortunate waste of memory, but I don't see a good way around
1049f94e 3557 it. Fortunately, it only happens when performing a relocatable
252b5132
RH
3558 link, which is not the common case. FIXME: If keep_memory is set
3559 we could write the relocs out and then read them again; I don't
3560 know how bad the memory loss will be. */
3561
3562 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
b34976b6 3563 sub->output_has_begun = FALSE;
252b5132
RH
3564 for (o = abfd->sections; o != NULL; o = o->next)
3565 {
3566 for (p = o->link_order_head; p != NULL; p = p->next)
3567 {
3568 if (p->type == bfd_indirect_link_order
a50c1845 3569 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
4ddafab0 3570 == bfd_target_elf_flavour)
a50c1845 3571 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
252b5132 3572 {
252b5132
RH
3573 if (! sub->output_has_begun)
3574 {
3575 if (! elf_link_input_bfd (&finfo, sub))
3576 goto error_return;
b34976b6 3577 sub->output_has_begun = TRUE;
252b5132
RH
3578 }
3579 }
3580 else if (p->type == bfd_section_reloc_link_order
3581 || p->type == bfd_symbol_reloc_link_order)
3582 {
3583 if (! elf_reloc_link_order (abfd, info, o, p))
3584 goto error_return;
3585 }
3586 else
3587 {
3588 if (! _bfd_default_link_order (abfd, info, o, p))
3589 goto error_return;
3590 }
3591 }
3592 }
3593
c44233aa
AM
3594 /* Output any global symbols that got converted to local in a
3595 version script or due to symbol visibility. We do this in a
3596 separate step since ELF requires all local symbols to appear
3597 prior to any global symbols. FIXME: We should only do this if
3598 some global symbols were, in fact, converted to become local.
3599 FIXME: Will this work correctly with the Irix 5 linker? */
b34976b6 3600 eoinfo.failed = FALSE;
c44233aa 3601 eoinfo.finfo = &finfo;
b34976b6 3602 eoinfo.localsyms = TRUE;
c44233aa 3603 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
268b6b39 3604 &eoinfo);
c44233aa 3605 if (eoinfo.failed)
b34976b6 3606 return FALSE;
c44233aa 3607
252b5132 3608 /* That wrote out all the local symbols. Finish up the symbol table
5cc7c785
L
3609 with the global symbols. Even if we want to strip everything we
3610 can, we still need to deal with those global symbols that got
3e932841 3611 converted to local in a version script. */
252b5132 3612
30b30c21 3613 /* The sh_info field records the index of the first non local symbol. */
252b5132 3614 symtab_hdr->sh_info = bfd_get_symcount (abfd);
30b30c21 3615
fc8c40a0
AM
3616 if (dynamic
3617 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
30b30c21
RH
3618 {
3619 Elf_Internal_Sym sym;
3620 Elf_External_Sym *dynsym =
a7b97311 3621 (Elf_External_Sym *) finfo.dynsym_sec->contents;
71a40b32 3622 long last_local = 0;
30b30c21
RH
3623
3624 /* Write out the section symbols for the output sections. */
3625 if (info->shared)
3626 {
3627 asection *s;
3628
3629 sym.st_size = 0;
3630 sym.st_name = 0;
3631 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3632 sym.st_other = 0;
3633
3634 for (s = abfd->sections; s != NULL; s = s->next)
3635 {
3636 int indx;
9ad5cbcf
AM
3637 Elf_External_Sym *dest;
3638
30b30c21
RH
3639 indx = elf_section_data (s)->this_idx;
3640 BFD_ASSERT (indx > 0);
3641 sym.st_shndx = indx;
3642 sym.st_value = s->vma;
9ad5cbcf 3643 dest = dynsym + elf_section_data (s)->dynindx;
268b6b39 3644 elf_swap_symbol_out (abfd, &sym, dest, 0);
30b30c21
RH
3645 }
3646
3647 last_local = bfd_count_sections (abfd);
3648 }
3649
3650 /* Write out the local dynsyms. */
3651 if (elf_hash_table (info)->dynlocal)
3652 {
3653 struct elf_link_local_dynamic_entry *e;
3654 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
3655 {
318da145 3656 asection *s;
9ad5cbcf 3657 Elf_External_Sym *dest;
30b30c21 3658
b037af20
MM
3659 sym.st_size = e->isym.st_size;
3660 sym.st_other = e->isym.st_other;
3661
1fa0ddb3
RH
3662 /* Copy the internal symbol as is.
3663 Note that we saved a word of storage and overwrote
c44233aa
AM
3664 the original st_name with the dynstr_index. */
3665 sym = e->isym;
30b30c21 3666
c8e5ddc8 3667 if (e->isym.st_shndx != SHN_UNDEF
58821868
AM
3668 && (e->isym.st_shndx < SHN_LORESERVE
3669 || e->isym.st_shndx > SHN_HIRESERVE))
587ff49e
RH
3670 {
3671 s = bfd_section_from_elf_index (e->input_bfd,
3672 e->isym.st_shndx);
3673
3674 sym.st_shndx =
3675 elf_section_data (s->output_section)->this_idx;
3676 sym.st_value = (s->output_section->vma
3677 + s->output_offset
3678 + e->isym.st_value);
3679 }
30b30c21
RH
3680
3681 if (last_local < e->dynindx)
3682 last_local = e->dynindx;
3683
9ad5cbcf 3684 dest = dynsym + e->dynindx;
268b6b39 3685 elf_swap_symbol_out (abfd, &sym, dest, 0);
30b30c21
RH
3686 }
3687 }
3688
71a40b32
ILT
3689 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
3690 last_local + 1;
30b30c21 3691 }
252b5132
RH
3692
3693 /* We get the global symbols from the hash table. */
b34976b6
AM
3694 eoinfo.failed = FALSE;
3695 eoinfo.localsyms = FALSE;
252b5132
RH
3696 eoinfo.finfo = &finfo;
3697 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
268b6b39 3698 &eoinfo);
252b5132 3699 if (eoinfo.failed)
b34976b6 3700 return FALSE;
252b5132 3701
587ff49e
RH
3702 /* If backend needs to output some symbols not present in the hash
3703 table, do it now. */
3704 if (bed->elf_backend_output_arch_syms)
3705 {
b34976b6 3706 typedef bfd_boolean (*out_sym_func)
754021d0
AM
3707 (void *, const char *, Elf_Internal_Sym *, asection *,
3708 struct elf_link_hash_entry *);
dc810e39
AM
3709
3710 if (! ((*bed->elf_backend_output_arch_syms)
268b6b39 3711 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
b34976b6 3712 return FALSE;
3e932841 3713 }
587ff49e 3714
252b5132
RH
3715 /* Flush all symbols to the file. */
3716 if (! elf_link_flush_output_syms (&finfo))
b34976b6 3717 return FALSE;
252b5132
RH
3718
3719 /* Now we know the size of the symtab section. */
3720 off += symtab_hdr->sh_size;
3721
c97e73dd
AM
3722 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3723 if (symtab_shndx_hdr->sh_name != 0)
3724 {
3725 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
3726 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
3727 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
3728 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
3729 symtab_shndx_hdr->sh_size = amt;
3730
3731 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
b34976b6 3732 off, TRUE);
c97e73dd
AM
3733
3734 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
268b6b39 3735 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
b34976b6 3736 return FALSE;
c97e73dd
AM
3737 }
3738
3739
252b5132
RH
3740 /* Finish up and write out the symbol string table (.strtab)
3741 section. */
3742 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3743 /* sh_name was set in prep_headers. */
3744 symstrtab_hdr->sh_type = SHT_STRTAB;
3745 symstrtab_hdr->sh_flags = 0;
3746 symstrtab_hdr->sh_addr = 0;
3747 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
3748 symstrtab_hdr->sh_entsize = 0;
3749 symstrtab_hdr->sh_link = 0;
3750 symstrtab_hdr->sh_info = 0;
3751 /* sh_offset is set just below. */
3752 symstrtab_hdr->sh_addralign = 1;
3753
b34976b6 3754 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
252b5132
RH
3755 elf_tdata (abfd)->next_file_pos = off;
3756
3757 if (bfd_get_symcount (abfd) > 0)
3758 {
3759 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
3760 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
b34976b6 3761 return FALSE;
252b5132
RH
3762 }
3763
3764 /* Adjust the relocs to have the correct symbol indices. */
3765 for (o = abfd->sections; o != NULL; o = o->next)
3766 {
252b5132
RH
3767 if ((o->flags & SEC_RELOC) == 0)
3768 continue;
3769
3e932841 3770 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
31367b81
MM
3771 elf_section_data (o)->rel_count,
3772 elf_section_data (o)->rel_hashes);
3773 if (elf_section_data (o)->rel_hdr2 != NULL)
3774 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
3775 elf_section_data (o)->rel_count2,
3e932841 3776 (elf_section_data (o)->rel_hashes
31367b81 3777 + elf_section_data (o)->rel_count));
252b5132
RH
3778
3779 /* Set the reloc_count field to 0 to prevent write_relocs from
3780 trying to swap the relocs out itself. */
3781 o->reloc_count = 0;
3782 }
3783
db6751f2
JJ
3784 if (dynamic && info->combreloc && dynobj != NULL)
3785 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
3786
252b5132
RH
3787 /* If we are linking against a dynamic object, or generating a
3788 shared library, finish up the dynamic linking information. */
3789 if (dynamic)
3790 {
3791 Elf_External_Dyn *dyncon, *dynconend;
3792
3793 /* Fix up .dynamic entries. */
3794 o = bfd_get_section_by_name (dynobj, ".dynamic");
3795 BFD_ASSERT (o != NULL);
3796
3797 dyncon = (Elf_External_Dyn *) o->contents;
3798 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
3799 for (; dyncon < dynconend; dyncon++)
3800 {
3801 Elf_Internal_Dyn dyn;
3802 const char *name;
3803 unsigned int type;
3804
3805 elf_swap_dyn_in (dynobj, dyncon, &dyn);
3806
3807 switch (dyn.d_tag)
3808 {
3809 default:
3810 break;
db6751f2
JJ
3811 case DT_NULL:
3812 if (relativecount > 0 && dyncon + 1 < dynconend)
3813 {
3814 switch (elf_section_data (reldyn)->this_hdr.sh_type)
3815 {
3816 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
3817 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
3818 default: break;
3819 }
3820 if (dyn.d_tag != DT_NULL)
3821 {
3822 dyn.d_un.d_val = relativecount;
3823 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3824 relativecount = 0;
3825 }
3826 }
3827 break;
252b5132 3828 case DT_INIT:
f0c2e336 3829 name = info->init_function;
252b5132
RH
3830 goto get_sym;
3831 case DT_FINI:
f0c2e336 3832 name = info->fini_function;
252b5132
RH
3833 get_sym:
3834 {
3835 struct elf_link_hash_entry *h;
3836
3837 h = elf_link_hash_lookup (elf_hash_table (info), name,
b34976b6 3838 FALSE, FALSE, TRUE);
252b5132
RH
3839 if (h != NULL
3840 && (h->root.type == bfd_link_hash_defined
3841 || h->root.type == bfd_link_hash_defweak))
3842 {
3843 dyn.d_un.d_val = h->root.u.def.value;
3844 o = h->root.u.def.section;
3845 if (o->output_section != NULL)
3846 dyn.d_un.d_val += (o->output_section->vma
3847 + o->output_offset);
3848 else
3849 {
3850 /* The symbol is imported from another shared
3851 library and does not apply to this one. */
3852 dyn.d_un.d_val = 0;
3853 }
3854
3855 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3856 }
3857 }
3858 break;
3859
30831527
RH
3860 case DT_PREINIT_ARRAYSZ:
3861 name = ".preinit_array";
3862 goto get_size;
3863 case DT_INIT_ARRAYSZ:
3864 name = ".init_array";
3865 goto get_size;
3866 case DT_FINI_ARRAYSZ:
3867 name = ".fini_array";
3868 get_size:
3869 o = bfd_get_section_by_name (abfd, name);
2cb69dd3
JL
3870 if (o == NULL)
3871 {
3872 (*_bfd_error_handler)
3873 (_("%s: could not find output section %s"),
3874 bfd_get_filename (abfd), name);
3875 goto error_return;
3876 }
25e27870
L
3877 if (o->_raw_size == 0)
3878 (*_bfd_error_handler)
3879 (_("warning: %s section has zero size"), name);
30831527
RH
3880 dyn.d_un.d_val = o->_raw_size;
3881 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3882 break;
3883
3884 case DT_PREINIT_ARRAY:
3885 name = ".preinit_array";
3886 goto get_vma;
3887 case DT_INIT_ARRAY:
3888 name = ".init_array";
3889 goto get_vma;
3890 case DT_FINI_ARRAY:
3891 name = ".fini_array";
3892 goto get_vma;
3893
252b5132
RH
3894 case DT_HASH:
3895 name = ".hash";
3896 goto get_vma;
3897 case DT_STRTAB:
3898 name = ".dynstr";
3899 goto get_vma;
3900 case DT_SYMTAB:
3901 name = ".dynsym";
3902 goto get_vma;
3903 case DT_VERDEF:
3904 name = ".gnu.version_d";
3905 goto get_vma;
3906 case DT_VERNEED:
3907 name = ".gnu.version_r";
3908 goto get_vma;
3909 case DT_VERSYM:
3910 name = ".gnu.version";
3911 get_vma:
3912 o = bfd_get_section_by_name (abfd, name);
2cb69dd3
JL
3913 if (o == NULL)
3914 {
3915 (*_bfd_error_handler)
3916 (_("%s: could not find output section %s"),
3917 bfd_get_filename (abfd), name);
3918 goto error_return;
3919 }
252b5132
RH
3920 dyn.d_un.d_ptr = o->vma;
3921 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3922 break;
3923
3924 case DT_REL:
3925 case DT_RELA:
3926 case DT_RELSZ:
3927 case DT_RELASZ:
3928 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
3929 type = SHT_REL;
3930 else
3931 type = SHT_RELA;
3932 dyn.d_un.d_val = 0;
9ad5cbcf 3933 for (i = 1; i < elf_numsections (abfd); i++)
252b5132
RH
3934 {
3935 Elf_Internal_Shdr *hdr;
3936
3937 hdr = elf_elfsections (abfd)[i];
3938 if (hdr->sh_type == type
3939 && (hdr->sh_flags & SHF_ALLOC) != 0)
3940 {
3941 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
3942 dyn.d_un.d_val += hdr->sh_size;
3943 else
3944 {
3945 if (dyn.d_un.d_val == 0
3946 || hdr->sh_addr < dyn.d_un.d_val)
3947 dyn.d_un.d_val = hdr->sh_addr;
3948 }
3949 }
3950 }
3951 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3952 break;
3953 }
3954 }
3955 }
3956
3957 /* If we have created any dynamic sections, then output them. */
3958 if (dynobj != NULL)
3959 {
3960 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
3961 goto error_return;
3962
3963 for (o = dynobj->sections; o != NULL; o = o->next)
3964 {
3965 if ((o->flags & SEC_HAS_CONTENTS) == 0
fc8c40a0
AM
3966 || o->_raw_size == 0
3967 || o->output_section == bfd_abs_section_ptr)
252b5132
RH
3968 continue;
3969 if ((o->flags & SEC_LINKER_CREATED) == 0)
3970 {
3971 /* At this point, we are only interested in sections
45d6a902 3972 created by _bfd_elf_link_create_dynamic_sections. */
252b5132
RH
3973 continue;
3974 }
3975 if ((elf_section_data (o->output_section)->this_hdr.sh_type
3976 != SHT_STRTAB)
3977 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
3978 {
3979 if (! bfd_set_section_contents (abfd, o->output_section,
dc810e39
AM
3980 o->contents,
3981 (file_ptr) o->output_offset,
252b5132
RH
3982 o->_raw_size))
3983 goto error_return;
3984 }
3985 else
3986 {
252b5132 3987 /* The contents of the .dynstr section are actually in a
c44233aa 3988 stringtab. */
252b5132
RH
3989 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
3990 if (bfd_seek (abfd, off, SEEK_SET) != 0
2b0f7ef9
JJ
3991 || ! _bfd_elf_strtab_emit (abfd,
3992 elf_hash_table (info)->dynstr))
252b5132
RH
3993 goto error_return;
3994 }
3995 }
3996 }
3997
1049f94e 3998 if (info->relocatable)
1126897b 3999 {
b34976b6 4000 bfd_boolean failed = FALSE;
1126897b
AM
4001
4002 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4003 if (failed)
4004 goto error_return;
4005 }
4006
252b5132
RH
4007 /* If we have optimized stabs strings, output them. */
4008 if (elf_hash_table (info)->stab_info != NULL)
4009 {
4010 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
4011 goto error_return;
4012 }
4013
126495ed 4014 if (info->eh_frame_hdr)
65765700 4015 {
126495ed
AM
4016 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
4017 goto error_return;
65765700
JJ
4018 }
4019
252b5132
RH
4020 if (finfo.symstrtab != NULL)
4021 _bfd_stringtab_free (finfo.symstrtab);
4022 if (finfo.contents != NULL)
4023 free (finfo.contents);
4024 if (finfo.external_relocs != NULL)
4025 free (finfo.external_relocs);
4026 if (finfo.internal_relocs != NULL)
4027 free (finfo.internal_relocs);
4028 if (finfo.external_syms != NULL)
4029 free (finfo.external_syms);
9ad5cbcf
AM
4030 if (finfo.locsym_shndx != NULL)
4031 free (finfo.locsym_shndx);
252b5132
RH
4032 if (finfo.internal_syms != NULL)
4033 free (finfo.internal_syms);
4034 if (finfo.indices != NULL)
4035 free (finfo.indices);
4036 if (finfo.sections != NULL)
4037 free (finfo.sections);
4038 if (finfo.symbuf != NULL)
4039 free (finfo.symbuf);
9ad5cbcf 4040 if (finfo.symshndxbuf != NULL)
c97e73dd 4041 free (finfo.symshndxbuf);
252b5132
RH
4042 for (o = abfd->sections; o != NULL; o = o->next)
4043 {
4044 if ((o->flags & SEC_RELOC) != 0
4045 && elf_section_data (o)->rel_hashes != NULL)
c44233aa 4046 free (elf_section_data (o)->rel_hashes);
252b5132
RH
4047 }
4048
b34976b6 4049 elf_tdata (abfd)->linker = TRUE;
252b5132 4050
b34976b6 4051 return TRUE;
252b5132
RH
4052
4053 error_return:
4054 if (finfo.symstrtab != NULL)
4055 _bfd_stringtab_free (finfo.symstrtab);
4056 if (finfo.contents != NULL)
4057 free (finfo.contents);
4058 if (finfo.external_relocs != NULL)
4059 free (finfo.external_relocs);
4060 if (finfo.internal_relocs != NULL)
4061 free (finfo.internal_relocs);
4062 if (finfo.external_syms != NULL)
4063 free (finfo.external_syms);
9ad5cbcf
AM
4064 if (finfo.locsym_shndx != NULL)
4065 free (finfo.locsym_shndx);
252b5132
RH
4066 if (finfo.internal_syms != NULL)
4067 free (finfo.internal_syms);
4068 if (finfo.indices != NULL)
4069 free (finfo.indices);
4070 if (finfo.sections != NULL)
4071 free (finfo.sections);
4072 if (finfo.symbuf != NULL)
4073 free (finfo.symbuf);
9ad5cbcf 4074 if (finfo.symshndxbuf != NULL)
c97e73dd 4075 free (finfo.symshndxbuf);
252b5132
RH
4076 for (o = abfd->sections; o != NULL; o = o->next)
4077 {
4078 if ((o->flags & SEC_RELOC) != 0
4079 && elf_section_data (o)->rel_hashes != NULL)
4080 free (elf_section_data (o)->rel_hashes);
4081 }
4082
b34976b6 4083 return FALSE;
252b5132
RH
4084}
4085
4086/* Add a symbol to the output symbol table. */
4087
b34976b6 4088static bfd_boolean
268b6b39
AM
4089elf_link_output_sym (struct elf_final_link_info *finfo,
4090 const char *name,
4091 Elf_Internal_Sym *elfsym,
754021d0
AM
4092 asection *input_sec,
4093 struct elf_link_hash_entry *h)
252b5132 4094{
9ad5cbcf
AM
4095 Elf_External_Sym *dest;
4096 Elf_External_Sym_Shndx *destshndx;
b34976b6 4097 bfd_boolean (*output_symbol_hook)
754021d0
AM
4098 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
4099 struct elf_link_hash_entry *);
252b5132
RH
4100
4101 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
4102 elf_backend_link_output_symbol_hook;
4103 if (output_symbol_hook != NULL)
4104 {
754021d0 4105 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
b34976b6 4106 return FALSE;
252b5132
RH
4107 }
4108
268b6b39 4109 if (name == NULL || *name == '\0')
252b5132
RH
4110 elfsym->st_name = 0;
4111 else if (input_sec->flags & SEC_EXCLUDE)
4112 elfsym->st_name = 0;
4113 else
4114 {
4115 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
b34976b6 4116 name, TRUE, FALSE);
252b5132 4117 if (elfsym->st_name == (unsigned long) -1)
b34976b6 4118 return FALSE;
252b5132
RH
4119 }
4120
4121 if (finfo->symbuf_count >= finfo->symbuf_size)
4122 {
4123 if (! elf_link_flush_output_syms (finfo))
b34976b6 4124 return FALSE;
252b5132
RH
4125 }
4126
9ad5cbcf
AM
4127 dest = finfo->symbuf + finfo->symbuf_count;
4128 destshndx = finfo->symshndxbuf;
4129 if (destshndx != NULL)
c97e73dd
AM
4130 {
4131 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
4132 {
4133 bfd_size_type amt;
252b5132 4134
c97e73dd
AM
4135 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
4136 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
4137 if (destshndx == NULL)
b34976b6 4138 return FALSE;
c97e73dd
AM
4139 memset ((char *) destshndx + amt, 0, amt);
4140 finfo->shndxbuf_size *= 2;
4141 }
4142 destshndx += bfd_get_symcount (finfo->output_bfd);
4143 }
4144
268b6b39 4145 elf_swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
c97e73dd
AM
4146 finfo->symbuf_count += 1;
4147 bfd_get_symcount (finfo->output_bfd) += 1;
252b5132 4148
b34976b6 4149 return TRUE;
252b5132
RH
4150}
4151
4152/* Flush the output symbols to the file. */
4153
b34976b6 4154static bfd_boolean
268b6b39 4155elf_link_flush_output_syms (struct elf_final_link_info *finfo)
252b5132
RH
4156{
4157 if (finfo->symbuf_count > 0)
4158 {
9ad5cbcf 4159 Elf_Internal_Shdr *hdr;
dc810e39
AM
4160 file_ptr pos;
4161 bfd_size_type amt;
252b5132 4162
9ad5cbcf
AM
4163 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
4164 pos = hdr->sh_offset + hdr->sh_size;
dc810e39
AM
4165 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
4166 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
268b6b39 4167 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
b34976b6 4168 return FALSE;
252b5132 4169
9ad5cbcf 4170 hdr->sh_size += amt;
252b5132
RH
4171 finfo->symbuf_count = 0;
4172 }
4173
b34976b6 4174 return TRUE;
252b5132
RH
4175}
4176
f5d44ba0
AM
4177/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
4178 allowing an unsatisfied unversioned symbol in the DSO to match a
71552942
L
4179 versioned symbol that would normally require an explicit version.
4180 We also handle the case that a DSO references a hidden symbol
4181 which may be satisfied by a versioned symbol in another DSO. */
f5d44ba0 4182
b34976b6 4183static bfd_boolean
268b6b39
AM
4184elf_link_check_versioned_symbol (struct bfd_link_info *info,
4185 struct elf_link_hash_entry *h)
f5d44ba0 4186{
71552942 4187 bfd *abfd;
f5d44ba0 4188 struct elf_link_loaded_list *loaded;
f5d44ba0 4189
0eddce27 4190 if (!is_elf_hash_table (info->hash))
b34976b6 4191 return FALSE;
f5d44ba0 4192
71552942
L
4193 switch (h->root.type)
4194 {
4195 default:
4196 abfd = NULL;
4197 break;
4198
4199 case bfd_link_hash_undefined:
4200 case bfd_link_hash_undefweak:
4201 abfd = h->root.u.undef.abfd;
4202 if ((abfd->flags & DYNAMIC) == 0 || elf_dt_soname (abfd) == NULL)
4203 return FALSE;
4204 break;
4205
4206 case bfd_link_hash_defined:
4207 case bfd_link_hash_defweak:
4208 abfd = h->root.u.def.section->owner;
4209 break;
4210
4211 case bfd_link_hash_common:
4212 abfd = h->root.u.c.p->section->owner;
4213 break;
4214 }
4215 BFD_ASSERT (abfd != NULL);
4216
f5d44ba0
AM
4217 for (loaded = elf_hash_table (info)->loaded;
4218 loaded != NULL;
4219 loaded = loaded->next)
4220 {
4221 bfd *input;
4222 Elf_Internal_Shdr *hdr;
4223 bfd_size_type symcount;
4224 bfd_size_type extsymcount;
4225 bfd_size_type extsymoff;
4226 Elf_Internal_Shdr *versymhdr;
6cdc0ccc
AM
4227 Elf_Internal_Sym *isym;
4228 Elf_Internal_Sym *isymend;
4229 Elf_Internal_Sym *isymbuf;
f5d44ba0 4230 Elf_External_Versym *ever;
6cdc0ccc 4231 Elf_External_Versym *extversym;
f5d44ba0
AM
4232
4233 input = loaded->abfd;
4234
4235 /* We check each DSO for a possible hidden versioned definition. */
71552942 4236 if (input == abfd
f5d44ba0
AM
4237 || (input->flags & DYNAMIC) == 0
4238 || elf_dynversym (input) == 0)
4239 continue;
4240
4241 hdr = &elf_tdata (input)->dynsymtab_hdr;
4242
4243 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
4244 if (elf_bad_symtab (input))
4245 {
4246 extsymcount = symcount;
4247 extsymoff = 0;
4248 }
4249 else
4250 {
4251 extsymcount = symcount - hdr->sh_info;
4252 extsymoff = hdr->sh_info;
4253 }
4254
4255 if (extsymcount == 0)
4256 continue;
4257
6cdc0ccc
AM
4258 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
4259 NULL, NULL, NULL);
4260 if (isymbuf == NULL)
b34976b6 4261 return FALSE;
f5d44ba0 4262
f5d44ba0
AM
4263 /* Read in any version definitions. */
4264 versymhdr = &elf_tdata (input)->dynversym_hdr;
268b6b39 4265 extversym = bfd_malloc (versymhdr->sh_size);
f5d44ba0
AM
4266 if (extversym == NULL)
4267 goto error_ret;
4268
4269 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
268b6b39 4270 || (bfd_bread (extversym, versymhdr->sh_size, input)
f5d44ba0
AM
4271 != versymhdr->sh_size))
4272 {
4273 free (extversym);
4274 error_ret:
6cdc0ccc 4275 free (isymbuf);
b34976b6 4276 return FALSE;
f5d44ba0
AM
4277 }
4278
4279 ever = extversym + extsymoff;
6cdc0ccc
AM
4280 isymend = isymbuf + extsymcount;
4281 for (isym = isymbuf; isym < isymend; isym++, ever++)
f5d44ba0
AM
4282 {
4283 const char *name;
f5d44ba0 4284 Elf_Internal_Versym iver;
a15d9d3a 4285 unsigned short version_index;
f5d44ba0 4286
6cdc0ccc
AM
4287 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
4288 || isym->st_shndx == SHN_UNDEF)
f5d44ba0
AM
4289 continue;
4290
4291 name = bfd_elf_string_from_elf_section (input,
4292 hdr->sh_link,
6cdc0ccc 4293 isym->st_name);
f5d44ba0
AM
4294 if (strcmp (name, h->root.root.string) != 0)
4295 continue;
4296
4297 _bfd_elf_swap_versym_in (input, ever, &iver);
4298
4299 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
4300 {
4301 /* If we have a non-hidden versioned sym, then it should
4302 have provided a definition for the undefined sym. */
4303 abort ();
4304 }
4305
a15d9d3a
L
4306 version_index = iver.vs_vers & VERSYM_VERSION;
4307 if (version_index == 1 || version_index == 2)
f5d44ba0 4308 {
a15d9d3a 4309 /* This is the base or first version. We can use it. */
f5d44ba0 4310 free (extversym);
6cdc0ccc 4311 free (isymbuf);
b34976b6 4312 return TRUE;
f5d44ba0
AM
4313 }
4314 }
4315
4316 free (extversym);
6cdc0ccc 4317 free (isymbuf);
f5d44ba0
AM
4318 }
4319
b34976b6 4320 return FALSE;
f5d44ba0
AM
4321}
4322
252b5132
RH
4323/* Add an external symbol to the symbol table. This is called from
4324 the hash table traversal routine. When generating a shared object,
4325 we go through the symbol table twice. The first time we output
4326 anything that might have been forced to local scope in a version
4327 script. The second time we output the symbols that are still
4328 global symbols. */
4329
b34976b6 4330static bfd_boolean
268b6b39 4331elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
252b5132 4332{
268b6b39 4333 struct elf_outext_info *eoinfo = data;
252b5132 4334 struct elf_final_link_info *finfo = eoinfo->finfo;
b34976b6 4335 bfd_boolean strip;
252b5132
RH
4336 Elf_Internal_Sym sym;
4337 asection *input_sec;
4338
e92d460e
AM
4339 if (h->root.type == bfd_link_hash_warning)
4340 {
4341 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4342 if (h->root.type == bfd_link_hash_new)
b34976b6 4343 return TRUE;
e92d460e
AM
4344 }
4345
252b5132
RH
4346 /* Decide whether to output this symbol in this pass. */
4347 if (eoinfo->localsyms)
4348 {
4349 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
b34976b6 4350 return TRUE;
252b5132
RH
4351 }
4352 else
4353 {
4354 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
b34976b6 4355 return TRUE;
252b5132
RH
4356 }
4357
560e09e9
NC
4358 /* If we have an undefined symbol reference here then it must have
4359 come from a shared library that is being linked in. (Undefined
4360 references in regular files have already been handled). If we
4361 are reporting errors for this situation then do so now. */
4362 if (h->root.type == bfd_link_hash_undefined
252b5132 4363 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
f5d44ba0 4364 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
560e09e9
NC
4365 && ! elf_link_check_versioned_symbol (finfo->info, h)
4366 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
252b5132
RH
4367 {
4368 if (! ((*finfo->info->callbacks->undefined_symbol)
4369 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
560e09e9 4370 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
252b5132 4371 {
b34976b6
AM
4372 eoinfo->failed = TRUE;
4373 return FALSE;
252b5132
RH
4374 }
4375 }
4376
1b1fe8fe
L
4377 /* We should also warn if a forced local symbol is referenced from
4378 shared libraries. */
1049f94e 4379 if (! finfo->info->relocatable
560e09e9 4380 && (! finfo->info->shared)
1b1fe8fe 4381 && (h->elf_link_hash_flags
560e09e9 4382 & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK))
71552942
L
4383 == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)
4384 && ! elf_link_check_versioned_symbol (finfo->info, h))
1b1fe8fe
L
4385 {
4386 (*_bfd_error_handler)
4387 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
4388 bfd_get_filename (finfo->output_bfd),
4389 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
4390 ? "internal"
4391 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
4392 ? "hidden" : "local",
4393 h->root.root.string,
4394 bfd_archive_filename (h->root.u.def.section->owner));
4395 eoinfo->failed = TRUE;
4396 return FALSE;
4397 }
4398
252b5132
RH
4399 /* We don't want to output symbols that have never been mentioned by
4400 a regular file, or that we have been told to strip. However, if
4401 h->indx is set to -2, the symbol is used by a reloc and we must
4402 output it. */
4403 if (h->indx == -2)
b34976b6 4404 strip = FALSE;
252b5132
RH
4405 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4406 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
4407 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4408 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
b34976b6 4409 strip = TRUE;
75828352
AM
4410 else if (finfo->info->strip == strip_all)
4411 strip = TRUE;
4412 else if (finfo->info->strip == strip_some
4413 && bfd_hash_lookup (finfo->info->keep_hash,
4414 h->root.root.string, FALSE, FALSE) == NULL)
4415 strip = TRUE;
4416 else if (finfo->info->strip_discarded
4417 && (h->root.type == bfd_link_hash_defined
4418 || h->root.type == bfd_link_hash_defweak)
4419 && elf_discarded_section (h->root.u.def.section))
b34976b6 4420 strip = TRUE;
252b5132 4421 else
b34976b6 4422 strip = FALSE;
252b5132
RH
4423
4424 /* If we're stripping it, and it's not a dynamic symbol, there's
2bd171e0
ILT
4425 nothing else to do unless it is a forced local symbol. */
4426 if (strip
4427 && h->dynindx == -1
4428 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
b34976b6 4429 return TRUE;
252b5132
RH
4430
4431 sym.st_value = 0;
4432 sym.st_size = h->size;
4433 sym.st_other = h->other;
4434 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4435 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
4436 else if (h->root.type == bfd_link_hash_undefweak
4437 || h->root.type == bfd_link_hash_defweak)
4438 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
4439 else
4440 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
4441
4442 switch (h->root.type)
4443 {
4444 default:
4445 case bfd_link_hash_new:
e92d460e 4446 case bfd_link_hash_warning:
252b5132 4447 abort ();
b34976b6 4448 return FALSE;
252b5132
RH
4449
4450 case bfd_link_hash_undefined:
252b5132
RH
4451 case bfd_link_hash_undefweak:
4452 input_sec = bfd_und_section_ptr;
4453 sym.st_shndx = SHN_UNDEF;
4454 break;
4455
4456 case bfd_link_hash_defined:
4457 case bfd_link_hash_defweak:
4458 {
4459 input_sec = h->root.u.def.section;
4460 if (input_sec->output_section != NULL)
4461 {
4462 sym.st_shndx =
4463 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
4464 input_sec->output_section);
9ad5cbcf 4465 if (sym.st_shndx == SHN_BAD)
252b5132
RH
4466 {
4467 (*_bfd_error_handler)
4468 (_("%s: could not find output section %s for input section %s"),
4469 bfd_get_filename (finfo->output_bfd),
4470 input_sec->output_section->name,
4471 input_sec->name);
b34976b6
AM
4472 eoinfo->failed = TRUE;
4473 return FALSE;
252b5132
RH
4474 }
4475
1049f94e
AM
4476 /* ELF symbols in relocatable files are section relative,
4477 but in nonrelocatable files they are virtual
252b5132
RH
4478 addresses. */
4479 sym.st_value = h->root.u.def.value + input_sec->output_offset;
1049f94e 4480 if (! finfo->info->relocatable)
13ae64f3
JJ
4481 {
4482 sym.st_value += input_sec->output_section->vma;
4483 if (h->type == STT_TLS)
4484 {
4485 /* STT_TLS symbols are relative to PT_TLS segment
4486 base. */
e1918d23
AM
4487 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
4488 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
13ae64f3
JJ
4489 }
4490 }
252b5132
RH
4491 }
4492 else
4493 {
4494 BFD_ASSERT (input_sec->owner == NULL
4495 || (input_sec->owner->flags & DYNAMIC) != 0);
4496 sym.st_shndx = SHN_UNDEF;
4497 input_sec = bfd_und_section_ptr;
4498 }
4499 }
4500 break;
4501
4502 case bfd_link_hash_common:
4503 input_sec = h->root.u.c.p->section;
4504 sym.st_shndx = SHN_COMMON;
4505 sym.st_value = 1 << h->root.u.c.p->alignment_power;
4506 break;
4507
4508 case bfd_link_hash_indirect:
4509 /* These symbols are created by symbol versioning. They point
c44233aa
AM
4510 to the decorated version of the name. For example, if the
4511 symbol foo@@GNU_1.2 is the default, which should be used when
4512 foo is used with no version, then we add an indirect symbol
4513 foo which points to foo@@GNU_1.2. We ignore these symbols,
4514 since the indirected symbol is already in the hash table. */
b34976b6 4515 return TRUE;
252b5132
RH
4516 }
4517
4518 /* Give the processor backend a chance to tweak the symbol value,
4519 and also to finish up anything that needs to be done for this
c44233aa
AM
4520 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
4521 forced local syms when non-shared is due to a historical quirk. */
252b5132
RH
4522 if ((h->dynindx != -1
4523 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
ef5aade5
L
4524 && ((finfo->info->shared
4525 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4526 || h->root.type != bfd_link_hash_undefweak))
c44233aa 4527 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
252b5132
RH
4528 && elf_hash_table (finfo->info)->dynamic_sections_created)
4529 {
9c5bfbb7 4530 const struct elf_backend_data *bed;
252b5132
RH
4531
4532 bed = get_elf_backend_data (finfo->output_bfd);
4533 if (! ((*bed->elf_backend_finish_dynamic_symbol)
4534 (finfo->output_bfd, finfo->info, h, &sym)))
4535 {
b34976b6
AM
4536 eoinfo->failed = TRUE;
4537 return FALSE;
252b5132
RH
4538 }
4539 }
4540
4541 /* If we are marking the symbol as undefined, and there are no
4542 non-weak references to this symbol from a regular object, then
91d3970e
ILT
4543 mark the symbol as weak undefined; if there are non-weak
4544 references, mark the symbol as strong. We can't do this earlier,
252b5132
RH
4545 because it might not be marked as undefined until the
4546 finish_dynamic_symbol routine gets through with it. */
4547 if (sym.st_shndx == SHN_UNDEF
252b5132 4548 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
a7b97311
AM
4549 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
4550 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
91d3970e
ILT
4551 {
4552 int bindtype;
4553
4554 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
4555 bindtype = STB_GLOBAL;
4556 else
4557 bindtype = STB_WEAK;
4558 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
4559 }
252b5132 4560
1b1fe8fe
L
4561 /* If a non-weak symbol with non-default visibility is not defined
4562 locally, it is a fatal error. */
1049f94e 4563 if (! finfo->info->relocatable
9c7a29a3 4564 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
1b1fe8fe 4565 && ELF_ST_BIND (sym.st_info) != STB_WEAK
22d5e339 4566 && h->root.type == bfd_link_hash_undefined
2cd533b7 4567 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1b1fe8fe
L
4568 {
4569 (*_bfd_error_handler)
4570 (_("%s: %s symbol `%s' isn't defined"),
4571 bfd_get_filename (finfo->output_bfd),
4572 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
4573 ? "protected"
4574 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
4575 ? "internal" : "hidden",
4576 h->root.root.string);
4577 eoinfo->failed = TRUE;
4578 return FALSE;
4579 }
32c092c3 4580
252b5132 4581 /* If this symbol should be put in the .dynsym section, then put it
f5d44ba0
AM
4582 there now. We already know the symbol index. We also fill in
4583 the entry in the .hash section. */
252b5132
RH
4584 if (h->dynindx != -1
4585 && elf_hash_table (finfo->info)->dynamic_sections_created)
4586 {
4587 size_t bucketcount;
4588 size_t bucket;
c7ac6ff8 4589 size_t hash_entry_size;
252b5132
RH
4590 bfd_byte *bucketpos;
4591 bfd_vma chain;
dc810e39 4592 Elf_External_Sym *esym;
252b5132
RH
4593
4594 sym.st_name = h->dynstr_index;
dc810e39 4595 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
268b6b39 4596 elf_swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
252b5132
RH
4597
4598 bucketcount = elf_hash_table (finfo->info)->bucketcount;
4599 bucket = h->elf_hash_value % bucketcount;
3e932841 4600 hash_entry_size
c7ac6ff8 4601 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
252b5132 4602 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
c7ac6ff8
MM
4603 + (bucket + 2) * hash_entry_size);
4604 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
268b6b39 4605 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
c7ac6ff8
MM
4606 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
4607 ((bfd_byte *) finfo->hash_sec->contents
4608 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
252b5132
RH
4609
4610 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
4611 {
4612 Elf_Internal_Versym iversym;
dc810e39 4613 Elf_External_Versym *eversym;
252b5132
RH
4614
4615 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4616 {
4617 if (h->verinfo.verdef == NULL)
4618 iversym.vs_vers = 0;
4619 else
4620 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
4621 }
4622 else
4623 {
4624 if (h->verinfo.vertree == NULL)
4625 iversym.vs_vers = 1;
4626 else
4627 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
4628 }
4629
4630 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
4631 iversym.vs_vers |= VERSYM_HIDDEN;
4632
dc810e39
AM
4633 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
4634 eversym += h->dynindx;
4635 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
252b5132
RH
4636 }
4637 }
4638
4639 /* If we're stripping it, then it was just a dynamic symbol, and
4640 there's nothing else to do. */
7330fb86 4641 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
b34976b6 4642 return TRUE;
252b5132
RH
4643
4644 h->indx = bfd_get_symcount (finfo->output_bfd);
4645
754021d0 4646 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
252b5132 4647 {
b34976b6
AM
4648 eoinfo->failed = TRUE;
4649 return FALSE;
252b5132
RH
4650 }
4651
b34976b6 4652 return TRUE;
252b5132
RH
4653}
4654
4655/* Link an input file into the linker output file. This function
4656 handles all the sections and relocations of the input file at once.
4657 This is so that we only have to read the local symbols once, and
4658 don't have to keep them in memory. */
4659
b34976b6 4660static bfd_boolean
268b6b39 4661elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
252b5132 4662{
b34976b6 4663 bfd_boolean (*relocate_section)
268b6b39
AM
4664 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
4665 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
252b5132
RH
4666 bfd *output_bfd;
4667 Elf_Internal_Shdr *symtab_hdr;
4668 size_t locsymcount;
4669 size_t extsymoff;
6cdc0ccc 4670 Elf_Internal_Sym *isymbuf;
252b5132 4671 Elf_Internal_Sym *isym;
6cdc0ccc 4672 Elf_Internal_Sym *isymend;
252b5132
RH
4673 long *pindex;
4674 asection **ppsection;
4675 asection *o;
9c5bfbb7 4676 const struct elf_backend_data *bed;
b34976b6 4677 bfd_boolean emit_relocs;
f8deed93 4678 struct elf_link_hash_entry **sym_hashes;
252b5132
RH
4679
4680 output_bfd = finfo->output_bfd;
c7ac6ff8
MM
4681 bed = get_elf_backend_data (output_bfd);
4682 relocate_section = bed->elf_backend_relocate_section;
252b5132
RH
4683
4684 /* If this is a dynamic object, we don't want to do anything here:
4685 we don't want the local symbols, and we don't want the section
4686 contents. */
4687 if ((input_bfd->flags & DYNAMIC) != 0)
b34976b6 4688 return TRUE;
252b5132 4689
1049f94e 4690 emit_relocs = (finfo->info->relocatable
c44233aa
AM
4691 || finfo->info->emitrelocations
4692 || bed->elf_backend_emit_relocs);
9317eacc 4693
252b5132
RH
4694 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4695 if (elf_bad_symtab (input_bfd))
4696 {
4697 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
4698 extsymoff = 0;
4699 }
4700 else
4701 {
4702 locsymcount = symtab_hdr->sh_info;
4703 extsymoff = symtab_hdr->sh_info;
4704 }
4705
4706 /* Read the local symbols. */
6cdc0ccc
AM
4707 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4708 if (isymbuf == NULL && locsymcount != 0)
4709 {
4710 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
4711 finfo->internal_syms,
4712 finfo->external_syms,
4713 finfo->locsym_shndx);
4714 if (isymbuf == NULL)
b34976b6 4715 return FALSE;
252b5132
RH
4716 }
4717
6cdc0ccc
AM
4718 /* Find local symbol sections and adjust values of symbols in
4719 SEC_MERGE sections. Write out those local symbols we know are
4720 going into the output file. */
4721 isymend = isymbuf + locsymcount;
4722 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
4723 isym < isymend;
4724 isym++, pindex++, ppsection++)
252b5132
RH
4725 {
4726 asection *isec;
4727 const char *name;
4728 Elf_Internal_Sym osym;
4729
252b5132
RH
4730 *pindex = -1;
4731
4732 if (elf_bad_symtab (input_bfd))
4733 {
4734 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
4735 {
4736 *ppsection = NULL;
4737 continue;
4738 }
4739 }
4740
4741 if (isym->st_shndx == SHN_UNDEF)
862517b6 4742 isec = bfd_und_section_ptr;
9ad5cbcf
AM
4743 else if (isym->st_shndx < SHN_LORESERVE
4744 || isym->st_shndx > SHN_HIRESERVE)
f5fa8ca2
JJ
4745 {
4746 isec = section_from_elf_index (input_bfd, isym->st_shndx);
65765700 4747 if (isec
68bfbfcc 4748 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
f5fa8ca2
JJ
4749 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
4750 isym->st_value =
4751 _bfd_merged_section_offset (output_bfd, &isec,
65765700 4752 elf_section_data (isec)->sec_info,
268b6b39 4753 isym->st_value, 0);
f5fa8ca2 4754 }
252b5132 4755 else if (isym->st_shndx == SHN_ABS)
862517b6 4756 isec = bfd_abs_section_ptr;
252b5132 4757 else if (isym->st_shndx == SHN_COMMON)
862517b6 4758 isec = bfd_com_section_ptr;
252b5132
RH
4759 else
4760 {
4761 /* Who knows? */
4762 isec = NULL;
4763 }
4764
4765 *ppsection = isec;
4766
4767 /* Don't output the first, undefined, symbol. */
6cdc0ccc 4768 if (ppsection == finfo->sections)
252b5132
RH
4769 continue;
4770
24376d1b
AM
4771 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4772 {
24376d1b
AM
4773 /* We never output section symbols. Instead, we use the
4774 section symbol of the corresponding section in the output
4775 file. */
4776 continue;
4777 }
4778
252b5132
RH
4779 /* If we are stripping all symbols, we don't want to output this
4780 one. */
4781 if (finfo->info->strip == strip_all)
4782 continue;
4783
252b5132 4784 /* If we are discarding all local symbols, we don't want to
1049f94e 4785 output this one. If we are generating a relocatable output
252b5132
RH
4786 file, then some of the local symbols may be required by
4787 relocs; we output them below as we discover that they are
4788 needed. */
4789 if (finfo->info->discard == discard_all)
4790 continue;
4791
4792 /* If this symbol is defined in a section which we are
c44233aa
AM
4793 discarding, we don't need to keep it, but note that
4794 linker_mark is only reliable for sections that have contents.
4795 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4796 as well as linker_mark. */
9ad5cbcf 4797 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
252b5132
RH
4798 && isec != NULL
4799 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
1049f94e 4800 || (! finfo->info->relocatable
252b5132
RH
4801 && (isec->flags & SEC_EXCLUDE) != 0)))
4802 continue;
4803
4804 /* Get the name of the symbol. */
4805 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
4806 isym->st_name);
4807 if (name == NULL)
b34976b6 4808 return FALSE;
252b5132
RH
4809
4810 /* See if we are discarding symbols with this name. */
4811 if ((finfo->info->strip == strip_some
b34976b6 4812 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
252b5132 4813 == NULL))
f5fa8ca2 4814 || (((finfo->info->discard == discard_sec_merge
1049f94e 4815 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
f5fa8ca2 4816 || finfo->info->discard == discard_l)
252b5132
RH
4817 && bfd_is_local_label_name (input_bfd, name)))
4818 continue;
4819
4820 /* If we get here, we are going to output this symbol. */
4821
4822 osym = *isym;
4823
4824 /* Adjust the section index for the output file. */
4825 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
4826 isec->output_section);
9ad5cbcf 4827 if (osym.st_shndx == SHN_BAD)
b34976b6 4828 return FALSE;
252b5132
RH
4829
4830 *pindex = bfd_get_symcount (output_bfd);
4831
1049f94e 4832 /* ELF symbols in relocatable files are section relative, but
252b5132
RH
4833 in executable files they are virtual addresses. Note that
4834 this code assumes that all ELF sections have an associated
4835 BFD section with a reasonable value for output_offset; below
4836 we assume that they also have a reasonable value for
4837 output_section. Any special sections must be set up to meet
4838 these requirements. */
4839 osym.st_value += isec->output_offset;
1049f94e 4840 if (! finfo->info->relocatable)
13ae64f3
JJ
4841 {
4842 osym.st_value += isec->output_section->vma;
4843 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
4844 {
4845 /* STT_TLS symbols are relative to PT_TLS segment base. */
e1918d23
AM
4846 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
4847 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
13ae64f3
JJ
4848 }
4849 }
252b5132 4850
754021d0 4851 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
b34976b6 4852 return FALSE;
252b5132
RH
4853 }
4854
4855 /* Relocate the contents of each section. */
f8deed93 4856 sym_hashes = elf_sym_hashes (input_bfd);
252b5132
RH
4857 for (o = input_bfd->sections; o != NULL; o = o->next)
4858 {
4859 bfd_byte *contents;
4860
4861 if (! o->linker_mark)
4862 {
4863 /* This section was omitted from the link. */
4864 continue;
4865 }
4866
4867 if ((o->flags & SEC_HAS_CONTENTS) == 0
4868 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
4869 continue;
4870
4871 if ((o->flags & SEC_LINKER_CREATED) != 0)
4872 {
45d6a902 4873 /* Section was created by _bfd_elf_link_create_dynamic_sections
252b5132
RH
4874 or somesuch. */
4875 continue;
4876 }
4877
4878 /* Get the contents of the section. They have been cached by a
c44233aa
AM
4879 relaxation routine. Note that o is a section in an input
4880 file, so the contents field will not have been set by any of
4881 the routines which work on output files. */
252b5132
RH
4882 if (elf_section_data (o)->this_hdr.contents != NULL)
4883 contents = elf_section_data (o)->this_hdr.contents;
4884 else
4885 {
4886 contents = finfo->contents;
268b6b39
AM
4887 if (! bfd_get_section_contents (input_bfd, o, contents, 0,
4888 o->_raw_size))
b34976b6 4889 return FALSE;
252b5132
RH
4890 }
4891
4892 if ((o->flags & SEC_RELOC) != 0)
4893 {
4894 Elf_Internal_Rela *internal_relocs;
4895
4896 /* Get the swapped relocs. */
45d6a902
AM
4897 internal_relocs
4898 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
4899 finfo->internal_relocs, FALSE);
252b5132
RH
4900 if (internal_relocs == NULL
4901 && o->reloc_count > 0)
b34976b6 4902 return FALSE;
252b5132 4903
ec338859
AM
4904 /* Run through the relocs looking for any against symbols
4905 from discarded sections and section symbols from
4906 removed link-once sections. Complain about relocs
4907 against discarded sections. Zero relocs against removed
f97b9cb8
L
4908 link-once sections. Preserve debug information as much
4909 as we can. */
4910 if (!elf_section_ignore_discarded_relocs (o))
ec338859
AM
4911 {
4912 Elf_Internal_Rela *rel, *relend;
50b4d486 4913
ec338859
AM
4914 rel = internal_relocs;
4915 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
4916 for ( ; rel < relend; rel++)
4917 {
4918 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
f97b9cb8 4919 asection *sec;
ec338859
AM
4920
4921 if (r_symndx >= locsymcount
4922 || (elf_bad_symtab (input_bfd)
4923 && finfo->sections[r_symndx] == NULL))
4924 {
4925 struct elf_link_hash_entry *h;
4926
4927 h = sym_hashes[r_symndx - extsymoff];
4928 while (h->root.type == bfd_link_hash_indirect
4929 || h->root.type == bfd_link_hash_warning)
4930 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4931
4932 /* Complain if the definition comes from a
4933 discarded section. */
f97b9cb8 4934 sec = h->root.u.def.section;
ec338859
AM
4935 if ((h->root.type == bfd_link_hash_defined
4936 || h->root.type == bfd_link_hash_defweak)
f97b9cb8 4937 && elf_discarded_section (sec))
ec338859 4938 {
ec338859
AM
4939 if ((o->flags & SEC_DEBUGGING) != 0)
4940 {
45e9217a 4941 BFD_ASSERT (r_symndx != 0);
f97b9cb8
L
4942 /* Try to preserve debug information. */
4943 if ((o->flags & SEC_DEBUGGING) != 0
4944 && sec->kept_section != NULL
6e35c4da 4945 && sec->_raw_size == sec->kept_section->_raw_size)
f97b9cb8
L
4946 h->root.u.def.section
4947 = sec->kept_section;
4948 else
4949 memset (rel, 0, sizeof (*rel));
ec338859
AM
4950 }
4951 else
6f6f27f8
L
4952 finfo->info->callbacks->error_handler
4953 (LD_DEFINITION_IN_DISCARDED_SECTION,
4954 _("%T: discarded in section `%s' from %s\n"),
4955 h->root.root.string,
4956 h->root.root.string,
4957 h->root.u.def.section->name,
4958 bfd_archive_filename (h->root.u.def.section->owner));
ec338859
AM
4959 }
4960 }
4961 else
4962 {
f97b9cb8 4963 sec = finfo->sections[r_symndx];
50b4d486 4964
ed4de5e2 4965 if (sec != NULL && elf_discarded_section (sec))
f9f32305 4966 {
ad43ed4c
L
4967 if ((o->flags & SEC_DEBUGGING) != 0
4968 || (sec->flags & SEC_LINK_ONCE) != 0)
f9f32305 4969 {
45e9217a 4970 BFD_ASSERT (r_symndx != 0);
f97b9cb8
L
4971 /* Try to preserve debug information. */
4972 if ((o->flags & SEC_DEBUGGING) != 0
4973 && sec->kept_section != NULL
6e35c4da 4974 && sec->_raw_size == sec->kept_section->_raw_size)
f97b9cb8
L
4975 finfo->sections[r_symndx]
4976 = sec->kept_section;
4977 else
4978 {
4979 rel->r_info
4980 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
4981 rel->r_addend = 0;
4982 }
f9f32305
AM
4983 }
4984 else
f9f32305 4985 {
6f6f27f8
L
4986 static int count;
4987 int ok;
4988 char *buf;
4989
4990 ok = asprintf (&buf, "local symbol %d",
4991 count++);
4992 if (ok <= 0)
4993 buf = (char *) "local symbol";
4994 finfo->info->callbacks->error_handler
4995 (LD_DEFINITION_IN_DISCARDED_SECTION,
4996 _("%T: discarded in section `%s' from %s\n"),
4997 buf, buf, sec->name,
4998 bfd_archive_filename (input_bfd));
4999 if (ok != -1)
f9f32305 5000 free (buf);
ec338859
AM
5001 }
5002 }
5003 }
5004 }
5005 }
50b4d486 5006
252b5132
RH
5007 /* Relocate the section by invoking a back end routine.
5008
5009 The back end routine is responsible for adjusting the
5010 section contents as necessary, and (if using Rela relocs
1049f94e 5011 and generating a relocatable output file) adjusting the
252b5132
RH
5012 reloc addend as necessary.
5013
5014 The back end routine does not have to worry about setting
5015 the reloc address or the reloc symbol index.
5016
5017 The back end routine is given a pointer to the swapped in
5018 internal symbols, and can access the hash table entries
5019 for the external symbols via elf_sym_hashes (input_bfd).
5020
1049f94e 5021 When generating relocatable output, the back end routine
252b5132
RH
5022 must handle STB_LOCAL/STT_SECTION symbols specially. The
5023 output symbol is going to be a section symbol
5024 corresponding to the output section, which will require
5025 the addend to be adjusted. */
5026
5027 if (! (*relocate_section) (output_bfd, finfo->info,
5028 input_bfd, o, contents,
5029 internal_relocs,
6cdc0ccc 5030 isymbuf,
252b5132 5031 finfo->sections))
b34976b6 5032 return FALSE;
252b5132 5033
9317eacc 5034 if (emit_relocs)
252b5132
RH
5035 {
5036 Elf_Internal_Rela *irela;
5037 Elf_Internal_Rela *irelaend;
73722af0 5038 bfd_vma last_offset;
252b5132 5039 struct elf_link_hash_entry **rel_hash;
c89583f8 5040 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
4e8a9624 5041 unsigned int next_erel;
b34976b6 5042 bfd_boolean (*reloc_emitter)
268b6b39 5043 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
b34976b6 5044 bfd_boolean rela_normal;
b491616a
AM
5045
5046 input_rel_hdr = &elf_section_data (o)->rel_hdr;
5047 rela_normal = (bed->rela_normal
5048 && (input_rel_hdr->sh_entsize
5049 == sizeof (Elf_External_Rela)));
252b5132
RH
5050
5051 /* Adjust the reloc addresses and symbol indices. */
5052
5053 irela = internal_relocs;
dc810e39 5054 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132 5055 rel_hash = (elf_section_data (o->output_section)->rel_hashes
31367b81
MM
5056 + elf_section_data (o->output_section)->rel_count
5057 + elf_section_data (o->output_section)->rel_count2);
73722af0 5058 last_offset = o->output_offset;
1049f94e 5059 if (!finfo->info->relocatable)
73722af0 5060 last_offset += o->output_section->vma;
209f668e 5061 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
252b5132
RH
5062 {
5063 unsigned long r_symndx;
252b5132 5064 asection *sec;
fad2542d 5065 Elf_Internal_Sym sym;
252b5132 5066
209f668e
NC
5067 if (next_erel == bed->s->int_rels_per_ext_rel)
5068 {
5069 rel_hash++;
5070 next_erel = 0;
5071 }
5072
d6fe2dc1
AM
5073 irela->r_offset = _bfd_elf_section_offset (output_bfd,
5074 finfo->info, o,
5075 irela->r_offset);
5076 if (irela->r_offset >= (bfd_vma) -2)
5077 {
73722af0
AM
5078 /* This is a reloc for a deleted entry or somesuch.
5079 Turn it into an R_*_NONE reloc, at the same
5080 offset as the last reloc. elf_eh_frame.c and
5081 elf_bfd_discard_info rely on reloc offsets
b34976b6 5082 being ordered. */
73722af0
AM
5083 irela->r_offset = last_offset;
5084 irela->r_info = 0;
5085 irela->r_addend = 0;
d6fe2dc1
AM
5086 continue;
5087 }
5088
252b5132
RH
5089 irela->r_offset += o->output_offset;
5090
7ad34365 5091 /* Relocs in an executable have to be virtual addresses. */
1049f94e 5092 if (!finfo->info->relocatable)
7ad34365
NC
5093 irela->r_offset += o->output_section->vma;
5094
73722af0 5095 last_offset = irela->r_offset;
252b5132 5096
73722af0
AM
5097 r_symndx = ELF_R_SYM (irela->r_info);
5098 if (r_symndx == STN_UNDEF)
252b5132
RH
5099 continue;
5100
5101 if (r_symndx >= locsymcount
5102 || (elf_bad_symtab (input_bfd)
5103 && finfo->sections[r_symndx] == NULL))
5104 {
5105 struct elf_link_hash_entry *rh;
209f668e 5106 unsigned long indx;
252b5132
RH
5107
5108 /* This is a reloc against a global symbol. We
5109 have not yet output all the local symbols, so
5110 we do not know the symbol index of any global
5111 symbol. We set the rel_hash entry for this
5112 reloc to point to the global hash table entry
5113 for this symbol. The symbol index is then
5114 set at the end of elf_bfd_final_link. */
5115 indx = r_symndx - extsymoff;
5116 rh = elf_sym_hashes (input_bfd)[indx];
5117 while (rh->root.type == bfd_link_hash_indirect
5118 || rh->root.type == bfd_link_hash_warning)
5119 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
5120
5121 /* Setting the index to -2 tells
5122 elf_link_output_extsym that this symbol is
5123 used by a reloc. */
5124 BFD_ASSERT (rh->indx < 0);
5125 rh->indx = -2;
5126
5127 *rel_hash = rh;
5128
5129 continue;
5130 }
5131
3e932841 5132 /* This is a reloc against a local symbol. */
252b5132
RH
5133
5134 *rel_hash = NULL;
fad2542d 5135 sym = isymbuf[r_symndx];
252b5132 5136 sec = finfo->sections[r_symndx];
fad2542d 5137 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
252b5132
RH
5138 {
5139 /* I suppose the backend ought to fill in the
5140 section of any STT_SECTION symbol against a
5141 processor specific section. If we have
5142 discarded a section, the output_section will
5143 be the absolute section. */
b491616a
AM
5144 if (bfd_is_abs_section (sec)
5145 || (sec != NULL
5146 && bfd_is_abs_section (sec->output_section)))
252b5132
RH
5147 r_symndx = 0;
5148 else if (sec == NULL || sec->owner == NULL)
5149 {
5150 bfd_set_error (bfd_error_bad_value);
b34976b6 5151 return FALSE;
252b5132
RH
5152 }
5153 else
5154 {
5155 r_symndx = sec->output_section->target_index;
5156 BFD_ASSERT (r_symndx != 0);
5157 }
b491616a
AM
5158
5159 /* Adjust the addend according to where the
f5d44ba0 5160 section winds up in the output section. */
b491616a
AM
5161 if (rela_normal)
5162 irela->r_addend += sec->output_offset;
252b5132
RH
5163 }
5164 else
5165 {
5166 if (finfo->indices[r_symndx] == -1)
5167 {
dc810e39 5168 unsigned long shlink;
252b5132
RH
5169 const char *name;
5170 asection *osec;
5171
5172 if (finfo->info->strip == strip_all)
5173 {
5174 /* You can't do ld -r -s. */
5175 bfd_set_error (bfd_error_invalid_operation);
b34976b6 5176 return FALSE;
252b5132
RH
5177 }
5178
5179 /* This symbol was skipped earlier, but
5180 since it is needed by a reloc, we
5181 must output it now. */
dc810e39 5182 shlink = symtab_hdr->sh_link;
a7b97311 5183 name = (bfd_elf_string_from_elf_section
fad2542d 5184 (input_bfd, shlink, sym.st_name));
252b5132 5185 if (name == NULL)
b34976b6 5186 return FALSE;
252b5132
RH
5187
5188 osec = sec->output_section;
fad2542d 5189 sym.st_shndx =
252b5132
RH
5190 _bfd_elf_section_from_bfd_section (output_bfd,
5191 osec);
fad2542d 5192 if (sym.st_shndx == SHN_BAD)
b34976b6 5193 return FALSE;
252b5132 5194
fad2542d 5195 sym.st_value += sec->output_offset;
1049f94e 5196 if (! finfo->info->relocatable)
13ae64f3 5197 {
fad2542d
AM
5198 sym.st_value += osec->vma;
5199 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
13ae64f3
JJ
5200 {
5201 /* STT_TLS symbols are relative to PT_TLS
5202 segment base. */
e1918d23
AM
5203 BFD_ASSERT (elf_hash_table (finfo->info)
5204 ->tls_sec != NULL);
5205 sym.st_value -= (elf_hash_table (finfo->info)
5206 ->tls_sec->vma);
13ae64f3
JJ
5207 }
5208 }
252b5132 5209
a7b97311
AM
5210 finfo->indices[r_symndx]
5211 = bfd_get_symcount (output_bfd);
252b5132 5212
754021d0
AM
5213 if (! elf_link_output_sym (finfo, name, &sym, sec,
5214 NULL))
b34976b6 5215 return FALSE;
252b5132
RH
5216 }
5217
5218 r_symndx = finfo->indices[r_symndx];
5219 }
5220
5221 irela->r_info = ELF_R_INFO (r_symndx,
5222 ELF_R_TYPE (irela->r_info));
5223 }
5224
5225 /* Swap out the relocs. */
c44233aa 5226 if (bed->elf_backend_emit_relocs
1049f94e 5227 && !(finfo->info->relocatable
a7b97311 5228 || finfo->info->emitrelocations))
c44233aa
AM
5229 reloc_emitter = bed->elf_backend_emit_relocs;
5230 else
45d6a902 5231 reloc_emitter = _bfd_elf_link_output_relocs;
9317eacc 5232
c89583f8
AM
5233 if (input_rel_hdr->sh_size != 0
5234 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
5235 internal_relocs))
b34976b6 5236 return FALSE;
9317eacc 5237
c89583f8
AM
5238 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
5239 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
c44233aa
AM
5240 {
5241 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
dc810e39 5242 * bed->s->int_rels_per_ext_rel);
c89583f8 5243 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
41241523 5244 internal_relocs))
b34976b6 5245 return FALSE;
c44233aa 5246 }
252b5132
RH
5247 }
5248 }
5249
5250 /* Write out the modified section contents. */
73d074b4 5251 if (bed->elf_backend_write_section
f9f32305 5252 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
73d074b4
DJ
5253 {
5254 /* Section written out. */
5255 }
68bfbfcc 5256 else switch (o->sec_info_type)
f5fa8ca2 5257 {
65765700 5258 case ELF_INFO_TYPE_STABS:
f5fa8ca2 5259 if (! (_bfd_write_section_stabs
65765700
JJ
5260 (output_bfd,
5261 &elf_hash_table (finfo->info)->stab_info,
5262 o, &elf_section_data (o)->sec_info, contents)))
b34976b6 5263 return FALSE;
65765700
JJ
5264 break;
5265 case ELF_INFO_TYPE_MERGE:
126495ed
AM
5266 if (! _bfd_write_merged_section (output_bfd, o,
5267 elf_section_data (o)->sec_info))
b34976b6 5268 return FALSE;
65765700
JJ
5269 break;
5270 case ELF_INFO_TYPE_EH_FRAME:
5271 {
126495ed
AM
5272 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
5273 o, contents))
b34976b6 5274 return FALSE;
65765700
JJ
5275 }
5276 break;
5277 default:
5278 {
5279 bfd_size_type sec_size;
5280
5281 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
5282 if (! (o->flags & SEC_EXCLUDE)
5283 && ! bfd_set_section_contents (output_bfd, o->output_section,
5284 contents,
5285 (file_ptr) o->output_offset,
5286 sec_size))
b34976b6 5287 return FALSE;
65765700
JJ
5288 }
5289 break;
252b5132
RH
5290 }
5291 }
5292
b34976b6 5293 return TRUE;
252b5132
RH
5294}
5295
5296/* Generate a reloc when linking an ELF file. This is a reloc
5297 requested by the linker, and does come from any input file. This
5298 is used to build constructor and destructor tables when linking
5299 with -Ur. */
5300
b34976b6 5301static bfd_boolean
268b6b39
AM
5302elf_reloc_link_order (bfd *output_bfd,
5303 struct bfd_link_info *info,
5304 asection *output_section,
5305 struct bfd_link_order *link_order)
252b5132
RH
5306{
5307 reloc_howto_type *howto;
5308 long indx;
5309 bfd_vma offset;
5310 bfd_vma addend;
5311 struct elf_link_hash_entry **rel_hash_ptr;
5312 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 5313 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
947216bf
AM
5314 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
5315 bfd_byte *erel;
5316 unsigned int i;
252b5132
RH
5317
5318 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5319 if (howto == NULL)
5320 {
5321 bfd_set_error (bfd_error_bad_value);
b34976b6 5322 return FALSE;
252b5132
RH
5323 }
5324
5325 addend = link_order->u.reloc.p->addend;
5326
5327 /* Figure out the symbol index. */
5328 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
31367b81
MM
5329 + elf_section_data (output_section)->rel_count
5330 + elf_section_data (output_section)->rel_count2);
252b5132
RH
5331 if (link_order->type == bfd_section_reloc_link_order)
5332 {
5333 indx = link_order->u.reloc.p->u.section->target_index;
5334 BFD_ASSERT (indx != 0);
5335 *rel_hash_ptr = NULL;
5336 }
5337 else
5338 {
5339 struct elf_link_hash_entry *h;
5340
5341 /* Treat a reloc against a defined symbol as though it were
c44233aa 5342 actually against the section. */
252b5132
RH
5343 h = ((struct elf_link_hash_entry *)
5344 bfd_wrapped_link_hash_lookup (output_bfd, info,
5345 link_order->u.reloc.p->u.name,
b34976b6 5346 FALSE, FALSE, TRUE));
252b5132
RH
5347 if (h != NULL
5348 && (h->root.type == bfd_link_hash_defined
5349 || h->root.type == bfd_link_hash_defweak))
5350 {
5351 asection *section;
5352
5353 section = h->root.u.def.section;
5354 indx = section->output_section->target_index;
5355 *rel_hash_ptr = NULL;
5356 /* It seems that we ought to add the symbol value to the
c44233aa
AM
5357 addend here, but in practice it has already been added
5358 because it was passed to constructor_callback. */
252b5132
RH
5359 addend += section->output_section->vma + section->output_offset;
5360 }
5361 else if (h != NULL)
5362 {
5363 /* Setting the index to -2 tells elf_link_output_extsym that
5364 this symbol is used by a reloc. */
5365 h->indx = -2;
5366 *rel_hash_ptr = h;
5367 indx = 0;
5368 }
5369 else
5370 {
5371 if (! ((*info->callbacks->unattached_reloc)
268b6b39 5372 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
b34976b6 5373 return FALSE;
252b5132
RH
5374 indx = 0;
5375 }
5376 }
5377
5378 /* If this is an inplace reloc, we must write the addend into the
5379 object file. */
5380 if (howto->partial_inplace && addend != 0)
5381 {
5382 bfd_size_type size;
5383 bfd_reloc_status_type rstat;
5384 bfd_byte *buf;
b34976b6 5385 bfd_boolean ok;
dc810e39 5386 const char *sym_name;
252b5132
RH
5387
5388 size = bfd_get_reloc_size (howto);
268b6b39
AM
5389 buf = bfd_zmalloc (size);
5390 if (buf == NULL)
b34976b6 5391 return FALSE;
268b6b39 5392 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
252b5132
RH
5393 switch (rstat)
5394 {
5395 case bfd_reloc_ok:
5396 break;
dc810e39 5397
252b5132
RH
5398 default:
5399 case bfd_reloc_outofrange:
5400 abort ();
dc810e39 5401
252b5132 5402 case bfd_reloc_overflow:
dc810e39
AM
5403 if (link_order->type == bfd_section_reloc_link_order)
5404 sym_name = bfd_section_name (output_bfd,
5405 link_order->u.reloc.p->u.section);
5406 else
5407 sym_name = link_order->u.reloc.p->u.name;
252b5132 5408 if (! ((*info->callbacks->reloc_overflow)
268b6b39 5409 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
252b5132
RH
5410 {
5411 free (buf);
b34976b6 5412 return FALSE;
252b5132
RH
5413 }
5414 break;
5415 }
268b6b39
AM
5416 ok = bfd_set_section_contents (output_bfd, output_section, buf,
5417 link_order->offset, size);
252b5132
RH
5418 free (buf);
5419 if (! ok)
b34976b6 5420 return FALSE;
252b5132
RH
5421 }
5422
5423 /* The address of a reloc is relative to the section in a
1049f94e 5424 relocatable file, and is a virtual address in an executable
252b5132
RH
5425 file. */
5426 offset = link_order->offset;
1049f94e 5427 if (! info->relocatable)
252b5132
RH
5428 offset += output_section->vma;
5429
947216bf
AM
5430 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
5431 {
5432 irel[i].r_offset = offset;
5433 irel[i].r_info = 0;
5434 irel[i].r_addend = 0;
5435 }
5436 irel[0].r_info = ELF_R_INFO (indx, howto->type);
252b5132 5437
947216bf
AM
5438 rel_hdr = &elf_section_data (output_section)->rel_hdr;
5439 erel = rel_hdr->contents;
252b5132
RH
5440 if (rel_hdr->sh_type == SHT_REL)
5441 {
947216bf
AM
5442 erel += (elf_section_data (output_section)->rel_count
5443 * sizeof (Elf_External_Rel));
5444 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
252b5132
RH
5445 }
5446 else
5447 {
947216bf
AM
5448 irel[0].r_addend = addend;
5449 erel += (elf_section_data (output_section)->rel_count
5450 * sizeof (Elf_External_Rela));
5451 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
252b5132
RH
5452 }
5453
0525d26e 5454 ++elf_section_data (output_section)->rel_count;
252b5132 5455
b34976b6 5456 return TRUE;
252b5132 5457}
252b5132 5458\f
252b5132
RH
5459/* Garbage collect unused sections. */
5460
b34976b6 5461static bfd_boolean elf_gc_sweep_symbol
268b6b39 5462 (struct elf_link_hash_entry *, void *);
252b5132 5463
b34976b6 5464static bfd_boolean elf_gc_allocate_got_offsets
268b6b39 5465 (struct elf_link_hash_entry *, void *);
252b5132
RH
5466
5467/* The mark phase of garbage collection. For a given section, mark
dbb410c3
AM
5468 it and any sections in this section's group, and all the sections
5469 which define symbols to which it refers. */
252b5132 5470
b34976b6 5471typedef asection * (*gc_mark_hook_fn)
268b6b39
AM
5472 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5473 struct elf_link_hash_entry *, Elf_Internal_Sym *);
b34976b6
AM
5474
5475static bfd_boolean
268b6b39
AM
5476elf_gc_mark (struct bfd_link_info *info,
5477 asection *sec,
5478 gc_mark_hook_fn gc_mark_hook)
252b5132 5479{
b34976b6 5480 bfd_boolean ret;
dbb410c3 5481 asection *group_sec;
252b5132
RH
5482
5483 sec->gc_mark = 1;
5484
dbb410c3
AM
5485 /* Mark all the sections in the group. */
5486 group_sec = elf_section_data (sec)->next_in_group;
5487 if (group_sec && !group_sec->gc_mark)
5488 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
b34976b6 5489 return FALSE;
252b5132 5490
dbb410c3 5491 /* Look through the section relocs. */
b34976b6 5492 ret = TRUE;
252b5132
RH
5493 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
5494 {
5495 Elf_Internal_Rela *relstart, *rel, *relend;
5496 Elf_Internal_Shdr *symtab_hdr;
5497 struct elf_link_hash_entry **sym_hashes;
5498 size_t nlocsyms;
5499 size_t extsymoff;
252b5132 5500 bfd *input_bfd = sec->owner;
9c5bfbb7 5501 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
6cdc0ccc 5502 Elf_Internal_Sym *isym = NULL;
252b5132
RH
5503
5504 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5505 sym_hashes = elf_sym_hashes (input_bfd);
5506
5507 /* Read the local symbols. */
5508 if (elf_bad_symtab (input_bfd))
5509 {
5510 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
c44233aa 5511 extsymoff = 0;
252b5132
RH
5512 }
5513 else
5514 extsymoff = nlocsyms = symtab_hdr->sh_info;
9ad5cbcf 5515
6cdc0ccc
AM
5516 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
5517 if (isym == NULL && nlocsyms != 0)
9ad5cbcf 5518 {
6cdc0ccc
AM
5519 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
5520 NULL, NULL, NULL);
5521 if (isym == NULL)
b34976b6 5522 return FALSE;
9ad5cbcf
AM
5523 }
5524
252b5132 5525 /* Read the relocations. */
268b6b39 5526 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
45d6a902 5527 info->keep_memory);
252b5132
RH
5528 if (relstart == NULL)
5529 {
b34976b6 5530 ret = FALSE;
252b5132
RH
5531 goto out1;
5532 }
c7ac6ff8 5533 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132
RH
5534
5535 for (rel = relstart; rel < relend; rel++)
5536 {
5537 unsigned long r_symndx;
5538 asection *rsec;
5539 struct elf_link_hash_entry *h;
252b5132
RH
5540
5541 r_symndx = ELF_R_SYM (rel->r_info);
5542 if (r_symndx == 0)
5543 continue;
5544
6cdc0ccc
AM
5545 if (r_symndx >= nlocsyms
5546 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
252b5132
RH
5547 {
5548 h = sym_hashes[r_symndx - extsymoff];
1e2f5b6e 5549 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
252b5132
RH
5550 }
5551 else
5552 {
6cdc0ccc 5553 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
252b5132
RH
5554 }
5555
5556 if (rsec && !rsec->gc_mark)
b91afed7
AM
5557 {
5558 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
5559 rsec->gc_mark = 1;
5560 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
5561 {
b34976b6 5562 ret = FALSE;
b91afed7
AM
5563 goto out2;
5564 }
5565 }
252b5132
RH
5566 }
5567
5568 out2:
6cdc0ccc 5569 if (elf_section_data (sec)->relocs != relstart)
252b5132
RH
5570 free (relstart);
5571 out1:
6cdc0ccc
AM
5572 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
5573 {
5574 if (! info->keep_memory)
5575 free (isym);
5576 else
5577 symtab_hdr->contents = (unsigned char *) isym;
5578 }
252b5132
RH
5579 }
5580
5581 return ret;
5582}
5583
5584/* The sweep phase of garbage collection. Remove all garbage sections. */
5585
b34976b6 5586typedef bfd_boolean (*gc_sweep_hook_fn)
268b6b39 5587 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
b34976b6
AM
5588
5589static bfd_boolean
268b6b39 5590elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
252b5132
RH
5591{
5592 bfd *sub;
5593
5594 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5595 {
5596 asection *o;
5597
f6af82bd
AM
5598 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5599 continue;
5600
252b5132
RH
5601 for (o = sub->sections; o != NULL; o = o->next)
5602 {
5603 /* Keep special sections. Keep .debug sections. */
5604 if ((o->flags & SEC_LINKER_CREATED)
5605 || (o->flags & SEC_DEBUGGING))
5606 o->gc_mark = 1;
5607
5608 if (o->gc_mark)
5609 continue;
5610
5611 /* Skip sweeping sections already excluded. */
5612 if (o->flags & SEC_EXCLUDE)
5613 continue;
5614
5615 /* Since this is early in the link process, it is simple
5616 to remove a section from the output. */
5617 o->flags |= SEC_EXCLUDE;
5618
5619 /* But we also have to update some of the relocation
5620 info we collected before. */
5621 if (gc_sweep_hook
5622 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
5623 {
5624 Elf_Internal_Rela *internal_relocs;
b34976b6 5625 bfd_boolean r;
252b5132 5626
45d6a902 5627 internal_relocs
268b6b39 5628 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
45d6a902 5629 info->keep_memory);
252b5132 5630 if (internal_relocs == NULL)
b34976b6 5631 return FALSE;
252b5132 5632
3e932841 5633 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
252b5132 5634
6cdc0ccc 5635 if (elf_section_data (o)->relocs != internal_relocs)
252b5132
RH
5636 free (internal_relocs);
5637
5638 if (!r)
b34976b6 5639 return FALSE;
252b5132
RH
5640 }
5641 }
5642 }
5643
5644 /* Remove the symbols that were in the swept sections from the dynamic
5645 symbol table. GCFIXME: Anyone know how to get them out of the
5646 static symbol table as well? */
5647 {
5648 int i = 0;
5649
268b6b39 5650 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
252b5132
RH
5651
5652 elf_hash_table (info)->dynsymcount = i;
5653 }
5654
b34976b6 5655 return TRUE;
252b5132
RH
5656}
5657
5658/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5659
b34976b6 5660static bfd_boolean
268b6b39 5661elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
252b5132 5662{
268b6b39 5663 int *idx = idxptr;
252b5132 5664
e92d460e
AM
5665 if (h->root.type == bfd_link_hash_warning)
5666 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5667
252b5132
RH
5668 if (h->dynindx != -1
5669 && ((h->root.type != bfd_link_hash_defined
5670 && h->root.type != bfd_link_hash_defweak)
5671 || h->root.u.def.section->gc_mark))
5672 h->dynindx = (*idx)++;
5673
b34976b6 5674 return TRUE;
252b5132
RH
5675}
5676
4cc11e76 5677/* Propagate collected vtable information. This is called through
252b5132
RH
5678 elf_link_hash_traverse. */
5679
b34976b6 5680static bfd_boolean
268b6b39 5681elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
252b5132 5682{
e92d460e
AM
5683 if (h->root.type == bfd_link_hash_warning)
5684 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5685
3e932841 5686 /* Those that are not vtables. */
252b5132 5687 if (h->vtable_parent == NULL)
b34976b6 5688 return TRUE;
252b5132
RH
5689
5690 /* Those vtables that do not have parents, we cannot merge. */
5691 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
b34976b6 5692 return TRUE;
252b5132
RH
5693
5694 /* If we've already been done, exit. */
5695 if (h->vtable_entries_used && h->vtable_entries_used[-1])
b34976b6 5696 return TRUE;
252b5132
RH
5697
5698 /* Make sure the parent's table is up to date. */
5699 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
5700
5701 if (h->vtable_entries_used == NULL)
5702 {
5703 /* None of this table's entries were referenced. Re-use the
5704 parent's table. */
5705 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
5706 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
5707 }
5708 else
5709 {
5710 size_t n;
b34976b6 5711 bfd_boolean *cu, *pu;
252b5132
RH
5712
5713 /* Or the parent's entries into ours. */
5714 cu = h->vtable_entries_used;
b34976b6 5715 cu[-1] = TRUE;
252b5132
RH
5716 pu = h->vtable_parent->vtable_entries_used;
5717 if (pu != NULL)
5718 {
9c5bfbb7
AM
5719 const struct elf_backend_data *bed;
5720 unsigned int log_file_align;
0d1ea5c0 5721
9c5bfbb7
AM
5722 bed = get_elf_backend_data (h->root.u.def.section->owner);
5723 log_file_align = bed->s->log_file_align;
45d6a902 5724 n = h->vtable_parent->vtable_entries_size >> log_file_align;
374b596d 5725 while (n--)
252b5132 5726 {
374b596d 5727 if (*pu)
b34976b6 5728 *cu = TRUE;
374b596d
NC
5729 pu++;
5730 cu++;
252b5132
RH
5731 }
5732 }
5733 }
5734
b34976b6 5735 return TRUE;
252b5132
RH
5736}
5737
b34976b6 5738static bfd_boolean
268b6b39 5739elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
252b5132
RH
5740{
5741 asection *sec;
5742 bfd_vma hstart, hend;
5743 Elf_Internal_Rela *relstart, *relend, *rel;
9c5bfbb7 5744 const struct elf_backend_data *bed;
45d6a902 5745 unsigned int log_file_align;
252b5132 5746
e92d460e
AM
5747 if (h->root.type == bfd_link_hash_warning)
5748 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5749
252b5132
RH
5750 /* Take care of both those symbols that do not describe vtables as
5751 well as those that are not loaded. */
5752 if (h->vtable_parent == NULL)
b34976b6 5753 return TRUE;
252b5132
RH
5754
5755 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5756 || h->root.type == bfd_link_hash_defweak);
5757
5758 sec = h->root.u.def.section;
5759 hstart = h->root.u.def.value;
5760 hend = hstart + h->size;
5761
268b6b39 5762 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
252b5132 5763 if (!relstart)
b34976b6 5764 return *(bfd_boolean *) okp = FALSE;
c7ac6ff8 5765 bed = get_elf_backend_data (sec->owner);
45d6a902 5766 log_file_align = bed->s->log_file_align;
0d1ea5c0 5767
c7ac6ff8 5768 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132
RH
5769
5770 for (rel = relstart; rel < relend; ++rel)
5771 if (rel->r_offset >= hstart && rel->r_offset < hend)
5772 {
5773 /* If the entry is in use, do nothing. */
5774 if (h->vtable_entries_used
5775 && (rel->r_offset - hstart) < h->vtable_entries_size)
5776 {
45d6a902 5777 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
252b5132
RH
5778 if (h->vtable_entries_used[entry])
5779 continue;
5780 }
5781 /* Otherwise, kill it. */
5782 rel->r_offset = rel->r_info = rel->r_addend = 0;
5783 }
5784
b34976b6 5785 return TRUE;
252b5132
RH
5786}
5787
5788/* Do mark and sweep of unused sections. */
5789
b34976b6 5790bfd_boolean
268b6b39 5791elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 5792{
b34976b6 5793 bfd_boolean ok = TRUE;
252b5132
RH
5794 bfd *sub;
5795 asection * (*gc_mark_hook)
268b6b39
AM
5796 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5797 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
252b5132
RH
5798
5799 if (!get_elf_backend_data (abfd)->can_gc_sections
0eddce27
AM
5800 || info->relocatable
5801 || info->emitrelocations
5802 || !is_elf_hash_table (info->hash)
252b5132 5803 || elf_hash_table (info)->dynamic_sections_created)
2627de83
AM
5804 {
5805 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
5806 return TRUE;
5807 }
252b5132
RH
5808
5809 /* Apply transitive closure to the vtable entry usage info. */
5810 elf_link_hash_traverse (elf_hash_table (info),
5811 elf_gc_propagate_vtable_entries_used,
268b6b39 5812 &ok);
252b5132 5813 if (!ok)
b34976b6 5814 return FALSE;
252b5132
RH
5815
5816 /* Kill the vtable relocations that were not used. */
5817 elf_link_hash_traverse (elf_hash_table (info),
5818 elf_gc_smash_unused_vtentry_relocs,
268b6b39 5819 &ok);
252b5132 5820 if (!ok)
b34976b6 5821 return FALSE;
252b5132
RH
5822
5823 /* Grovel through relocs to find out who stays ... */
5824
5825 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
5826 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5827 {
5828 asection *o;
f6af82bd
AM
5829
5830 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5831 continue;
5832
252b5132
RH
5833 for (o = sub->sections; o != NULL; o = o->next)
5834 {
5835 if (o->flags & SEC_KEEP)
c44233aa 5836 if (!elf_gc_mark (info, o, gc_mark_hook))
b34976b6 5837 return FALSE;
252b5132
RH
5838 }
5839 }
5840
5841 /* ... and mark SEC_EXCLUDE for those that go. */
a7b97311 5842 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
b34976b6 5843 return FALSE;
252b5132 5844
b34976b6 5845 return TRUE;
252b5132
RH
5846}
5847\f
4cc11e76 5848/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
252b5132 5849
b34976b6 5850bfd_boolean
268b6b39
AM
5851elf_gc_record_vtinherit (bfd *abfd,
5852 asection *sec,
5853 struct elf_link_hash_entry *h,
5854 bfd_vma offset)
252b5132
RH
5855{
5856 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
5857 struct elf_link_hash_entry **search, *child;
5858 bfd_size_type extsymcount;
5859
5860 /* The sh_info field of the symtab header tells us where the
5861 external symbols start. We don't care about the local symbols at
5862 this point. */
5863 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
5864 if (!elf_bad_symtab (abfd))
5865 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
5866
5867 sym_hashes = elf_sym_hashes (abfd);
5868 sym_hashes_end = sym_hashes + extsymcount;
5869
5870 /* Hunt down the child symbol, which is in this section at the same
5871 offset as the relocation. */
5872 for (search = sym_hashes; search != sym_hashes_end; ++search)
5873 {
5874 if ((child = *search) != NULL
5875 && (child->root.type == bfd_link_hash_defined
5876 || child->root.type == bfd_link_hash_defweak)
5877 && child->root.u.def.section == sec
5878 && child->root.u.def.value == offset)
5879 goto win;
5880 }
5881
5882 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
8f615d07 5883 bfd_archive_filename (abfd), sec->name,
a7b97311 5884 (unsigned long) offset);
252b5132 5885 bfd_set_error (bfd_error_invalid_operation);
b34976b6 5886 return FALSE;
252b5132 5887
dc810e39 5888 win:
252b5132
RH
5889 if (!h)
5890 {
5891 /* This *should* only be the absolute section. It could potentially
5892 be that someone has defined a non-global vtable though, which
5893 would be bad. It isn't worth paging in the local symbols to be
5894 sure though; that case should simply be handled by the assembler. */
5895
5896 child->vtable_parent = (struct elf_link_hash_entry *) -1;
5897 }
5898 else
5899 child->vtable_parent = h;
5900
b34976b6 5901 return TRUE;
252b5132
RH
5902}
5903
4cc11e76 5904/* Called from check_relocs to record the existence of a VTENTRY reloc. */
252b5132 5905
b34976b6 5906bfd_boolean
268b6b39
AM
5907elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
5908 asection *sec ATTRIBUTE_UNUSED,
5909 struct elf_link_hash_entry *h,
5910 bfd_vma addend)
252b5132 5911{
9c5bfbb7 5912 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
45d6a902 5913 unsigned int log_file_align = bed->s->log_file_align;
0d1ea5c0 5914
64c456e1 5915 if (addend >= h->vtable_entries_size)
252b5132 5916 {
64c456e1 5917 size_t size, bytes, file_align;
b34976b6 5918 bfd_boolean *ptr = h->vtable_entries_used;
252b5132
RH
5919
5920 /* While the symbol is undefined, we have to be prepared to handle
5921 a zero size. */
64c456e1 5922 file_align = 1 << log_file_align;
252b5132 5923 if (h->root.type == bfd_link_hash_undefined)
64c456e1 5924 size = addend + file_align;
252b5132
RH
5925 else
5926 {
5927 size = h->size;
64c456e1 5928 if (addend >= size)
252b5132
RH
5929 {
5930 /* Oops! We've got a reference past the defined end of
5931 the table. This is probably a bug -- shall we warn? */
64c456e1 5932 size = addend + file_align;
252b5132
RH
5933 }
5934 }
64c456e1 5935 size = (size + file_align - 1) & -file_align;
252b5132
RH
5936
5937 /* Allocate one extra entry for use as a "done" flag for the
64c456e1
AM
5938 consolidation pass. */
5939 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
252b5132
RH
5940
5941 if (ptr)
5942 {
268b6b39 5943 ptr = bfd_realloc (ptr - 1, bytes);
3e932841 5944
fed79cc6
NC
5945 if (ptr != NULL)
5946 {
5947 size_t oldbytes;
252b5132 5948
45d6a902 5949 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
b34976b6 5950 * sizeof (bfd_boolean));
a7b97311 5951 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
fed79cc6 5952 }
252b5132
RH
5953 }
5954 else
268b6b39 5955 ptr = bfd_zmalloc (bytes);
252b5132 5956
fed79cc6 5957 if (ptr == NULL)
b34976b6 5958 return FALSE;
3e932841 5959
252b5132 5960 /* And arrange for that done flag to be at index -1. */
fed79cc6 5961 h->vtable_entries_used = ptr + 1;
252b5132
RH
5962 h->vtable_entries_size = size;
5963 }
3e932841 5964
45d6a902 5965 h->vtable_entries_used[addend >> log_file_align] = TRUE;
252b5132 5966
b34976b6 5967 return TRUE;
252b5132
RH
5968}
5969
5970/* And an accompanying bit to work out final got entry offsets once
5971 we're done. Should be called from final_link. */
5972
b34976b6 5973bfd_boolean
268b6b39
AM
5974elf_gc_common_finalize_got_offsets (bfd *abfd,
5975 struct bfd_link_info *info)
252b5132
RH
5976{
5977 bfd *i;
9c5bfbb7 5978 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
5979 bfd_vma gotoff;
5980
0eddce27
AM
5981 if (! is_elf_hash_table (info->hash))
5982 return FALSE;
5983
252b5132
RH
5984 /* The GOT offset is relative to the .got section, but the GOT header is
5985 put into the .got.plt section, if the backend uses it. */
5986 if (bed->want_got_plt)
5987 gotoff = 0;
5988 else
5989 gotoff = bed->got_header_size;
5990
5991 /* Do the local .got entries first. */
5992 for (i = info->input_bfds; i; i = i->link_next)
5993 {
f6af82bd 5994 bfd_signed_vma *local_got;
252b5132
RH
5995 bfd_size_type j, locsymcount;
5996 Elf_Internal_Shdr *symtab_hdr;
5997
f6af82bd
AM
5998 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
5999 continue;
6000
6001 local_got = elf_local_got_refcounts (i);
252b5132
RH
6002 if (!local_got)
6003 continue;
6004
6005 symtab_hdr = &elf_tdata (i)->symtab_hdr;
6006 if (elf_bad_symtab (i))
6007 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6008 else
6009 locsymcount = symtab_hdr->sh_info;
6010
6011 for (j = 0; j < locsymcount; ++j)
6012 {
6013 if (local_got[j] > 0)
6014 {
6015 local_got[j] = gotoff;
6016 gotoff += ARCH_SIZE / 8;
6017 }
6018 else
6019 local_got[j] = (bfd_vma) -1;
6020 }
6021 }
6022
dd5724d5
AM
6023 /* Then the global .got entries. .plt refcounts are handled by
6024 adjust_dynamic_symbol */
252b5132
RH
6025 elf_link_hash_traverse (elf_hash_table (info),
6026 elf_gc_allocate_got_offsets,
268b6b39 6027 &gotoff);
b34976b6 6028 return TRUE;
252b5132
RH
6029}
6030
6031/* We need a special top-level link routine to convert got reference counts
6032 to real got offsets. */
6033
b34976b6 6034static bfd_boolean
268b6b39 6035elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *offarg)
252b5132 6036{
268b6b39 6037 bfd_vma *off = offarg;
252b5132 6038
e92d460e
AM
6039 if (h->root.type == bfd_link_hash_warning)
6040 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6041
252b5132
RH
6042 if (h->got.refcount > 0)
6043 {
6044 h->got.offset = off[0];
6045 off[0] += ARCH_SIZE / 8;
6046 }
6047 else
6048 h->got.offset = (bfd_vma) -1;
6049
b34976b6 6050 return TRUE;
252b5132
RH
6051}
6052
6053/* Many folk need no more in the way of final link than this, once
6054 got entry reference counting is enabled. */
6055
b34976b6 6056bfd_boolean
268b6b39 6057elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
6058{
6059 if (!elf_gc_common_finalize_got_offsets (abfd, info))
b34976b6 6060 return FALSE;
252b5132
RH
6061
6062 /* Invoke the regular ELF backend linker to do all the work. */
6063 return elf_bfd_final_link (abfd, info);
6064}
6065
6066/* This function will be called though elf_link_hash_traverse to store
6067 all hash value of the exported symbols in an array. */
6068
b34976b6 6069static bfd_boolean
268b6b39 6070elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
252b5132 6071{
268b6b39 6072 unsigned long **valuep = data;
252b5132
RH
6073 const char *name;
6074 char *p;
6075 unsigned long ha;
6076 char *alc = NULL;
6077
e92d460e
AM
6078 if (h->root.type == bfd_link_hash_warning)
6079 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6080
252b5132
RH
6081 /* Ignore indirect symbols. These are added by the versioning code. */
6082 if (h->dynindx == -1)
b34976b6 6083 return TRUE;
252b5132
RH
6084
6085 name = h->root.root.string;
6086 p = strchr (name, ELF_VER_CHR);
6087 if (p != NULL)
6088 {
268b6b39
AM
6089 alc = bfd_malloc (p - name + 1);
6090 memcpy (alc, name, p - name);
252b5132
RH
6091 alc[p - name] = '\0';
6092 name = alc;
6093 }
6094
6095 /* Compute the hash value. */
6096 ha = bfd_elf_hash (name);
6097
6098 /* Store the found hash value in the array given as the argument. */
6099 *(*valuep)++ = ha;
6100
6101 /* And store it in the struct so that we can put it in the hash table
6102 later. */
6103 h->elf_hash_value = ha;
6104
6105 if (alc != NULL)
6106 free (alc);
6107
b34976b6 6108 return TRUE;
252b5132 6109}
73d074b4 6110
b34976b6 6111bfd_boolean
268b6b39 6112elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
73d074b4 6113{
268b6b39 6114 struct elf_reloc_cookie *rcookie = cookie;
73d074b4
DJ
6115
6116 if (rcookie->bad_symtab)
6117 rcookie->rel = rcookie->rels;
6118
6119 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
6120 {
d6fe2dc1 6121 unsigned long r_symndx;
73d074b4
DJ
6122
6123 if (! rcookie->bad_symtab)
6124 if (rcookie->rel->r_offset > offset)
b34976b6 6125 return FALSE;
73d074b4
DJ
6126 if (rcookie->rel->r_offset != offset)
6127 continue;
6128
d6fe2dc1
AM
6129 r_symndx = ELF_R_SYM (rcookie->rel->r_info);
6130 if (r_symndx == SHN_UNDEF)
b34976b6 6131 return TRUE;
d6fe2dc1 6132
73d074b4 6133 if (r_symndx >= rcookie->locsymcount
6cdc0ccc 6134 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
73d074b4
DJ
6135 {
6136 struct elf_link_hash_entry *h;
6137
6138 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
6139
6140 while (h->root.type == bfd_link_hash_indirect
6141 || h->root.type == bfd_link_hash_warning)
6142 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6143
6144 if ((h->root.type == bfd_link_hash_defined
6145 || h->root.type == bfd_link_hash_defweak)
ed4de5e2 6146 && elf_discarded_section (h->root.u.def.section))
b34976b6 6147 return TRUE;
73d074b4 6148 else
b34976b6 6149 return FALSE;
73d074b4 6150 }
6cdc0ccc 6151 else
73d074b4
DJ
6152 {
6153 /* It's not a relocation against a global symbol,
44421011 6154 but it could be a relocation against a local
73d074b4
DJ
6155 symbol for a discarded section. */
6156 asection *isec;
6cdc0ccc 6157 Elf_Internal_Sym *isym;
73d074b4
DJ
6158
6159 /* Need to: get the symbol; get the section. */
6cdc0ccc
AM
6160 isym = &rcookie->locsyms[r_symndx];
6161 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
73d074b4 6162 {
6cdc0ccc 6163 isec = section_from_elf_index (rcookie->abfd, isym->st_shndx);
ed4de5e2 6164 if (isec != NULL && elf_discarded_section (isec))
b34976b6 6165 return TRUE;
73d074b4
DJ
6166 }
6167 }
b34976b6 6168 return FALSE;
73d074b4 6169 }
b34976b6 6170 return FALSE;
73d074b4
DJ
6171}
6172
6173/* Discard unneeded references to discarded sections.
b34976b6 6174 Returns TRUE if any section's size was changed. */
73d074b4 6175/* This function assumes that the relocations are in sorted order,
ab3acfbe 6176 which is true for all known assemblers. */
73d074b4 6177
b34976b6 6178bfd_boolean
268b6b39 6179elf_bfd_discard_info (bfd *output_bfd, struct bfd_link_info *info)
73d074b4
DJ
6180{
6181 struct elf_reloc_cookie cookie;
126495ed 6182 asection *stab, *eh;
73d074b4 6183 Elf_Internal_Shdr *symtab_hdr;
9c5bfbb7 6184 const struct elf_backend_data *bed;
73d074b4 6185 bfd *abfd;
99eb2ac8 6186 unsigned int count;
b34976b6 6187 bfd_boolean ret = FALSE;
73d074b4 6188
d6fe2dc1 6189 if (info->traditional_format
0eddce27 6190 || !is_elf_hash_table (info->hash))
b34976b6 6191 return FALSE;
65765700 6192
73d074b4
DJ
6193 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
6194 {
163c1c30
L
6195 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6196 continue;
6197
73d074b4
DJ
6198 bed = get_elf_backend_data (abfd);
6199
6200 if ((abfd->flags & DYNAMIC) != 0)
6201 continue;
6202
126495ed 6203 eh = bfd_get_section_by_name (abfd, ".eh_frame");
1049f94e 6204 if (info->relocatable
2d36fe5f
AH
6205 || (eh != NULL
6206 && (eh->_raw_size == 0
6207 || bfd_is_abs_section (eh->output_section))))
126495ed 6208 eh = NULL;
65765700 6209
99eb2ac8
AM
6210 stab = bfd_get_section_by_name (abfd, ".stab");
6211 if (stab != NULL
6212 && (stab->_raw_size == 0
6213 || bfd_is_abs_section (stab->output_section)
68bfbfcc 6214 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
99eb2ac8
AM
6215 stab = NULL;
6216
6217 if (stab == NULL
6218 && eh == NULL
6219 && bed->elf_backend_discard_info == NULL)
73d074b4
DJ
6220 continue;
6221
6222 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
73d074b4
DJ
6223 cookie.abfd = abfd;
6224 cookie.sym_hashes = elf_sym_hashes (abfd);
6225 cookie.bad_symtab = elf_bad_symtab (abfd);
6226 if (cookie.bad_symtab)
6227 {
99eb2ac8 6228 cookie.locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
73d074b4
DJ
6229 cookie.extsymoff = 0;
6230 }
6231 else
6232 {
6233 cookie.locsymcount = symtab_hdr->sh_info;
6234 cookie.extsymoff = symtab_hdr->sh_info;
6235 }
6236
6cdc0ccc
AM
6237 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6238 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
c44233aa 6239 {
6cdc0ccc
AM
6240 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
6241 cookie.locsymcount, 0,
6242 NULL, NULL, NULL);
c44233aa 6243 if (cookie.locsyms == NULL)
b34976b6 6244 return FALSE;
9ad5cbcf 6245 }
73d074b4 6246
99eb2ac8 6247 if (stab != NULL)
73d074b4 6248 {
99eb2ac8
AM
6249 cookie.rels = NULL;
6250 count = stab->reloc_count;
6251 if (count != 0)
268b6b39
AM
6252 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
6253 info->keep_memory);
99eb2ac8 6254 if (cookie.rels != NULL)
73d074b4
DJ
6255 {
6256 cookie.rel = cookie.rels;
99eb2ac8
AM
6257 cookie.relend = cookie.rels;
6258 cookie.relend += count * bed->s->int_rels_per_ext_rel;
65765700
JJ
6259 if (_bfd_discard_section_stabs (abfd, stab,
6260 elf_section_data (stab)->sec_info,
73d074b4
DJ
6261 elf_reloc_symbol_deleted_p,
6262 &cookie))
b34976b6 6263 ret = TRUE;
6cdc0ccc 6264 if (elf_section_data (stab)->relocs != cookie.rels)
73d074b4
DJ
6265 free (cookie.rels);
6266 }
6267 }
6268
99eb2ac8 6269 if (eh != NULL)
65765700
JJ
6270 {
6271 cookie.rels = NULL;
99eb2ac8
AM
6272 count = eh->reloc_count;
6273 if (count != 0)
268b6b39
AM
6274 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
6275 info->keep_memory);
99eb2ac8
AM
6276 cookie.rel = cookie.rels;
6277 cookie.relend = cookie.rels;
6278 if (cookie.rels != NULL)
6279 cookie.relend += count * bed->s->int_rels_per_ext_rel;
6280
126495ed 6281 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
65765700
JJ
6282 elf_reloc_symbol_deleted_p,
6283 &cookie))
b34976b6 6284 ret = TRUE;
99eb2ac8
AM
6285
6286 if (cookie.rels != NULL
6287 && elf_section_data (eh)->relocs != cookie.rels)
65765700
JJ
6288 free (cookie.rels);
6289 }
6290
99eb2ac8
AM
6291 if (bed->elf_backend_discard_info != NULL
6292 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
b34976b6 6293 ret = TRUE;
73d074b4 6294
6cdc0ccc
AM
6295 if (cookie.locsyms != NULL
6296 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
6297 {
6298 if (! info->keep_memory)
6299 free (cookie.locsyms);
6300 else
6301 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
6302 }
73d074b4 6303 }
65765700 6304
126495ed 6305 if (info->eh_frame_hdr
1049f94e 6306 && !info->relocatable
126495ed 6307 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
b34976b6 6308 ret = TRUE;
99eb2ac8 6309
73d074b4
DJ
6310 return ret;
6311}
6312
b34976b6 6313static bfd_boolean
268b6b39 6314elf_section_ignore_discarded_relocs (asection *sec)
73d074b4 6315{
9c5bfbb7 6316 const struct elf_backend_data *bed;
40b829d4 6317
68bfbfcc 6318 switch (sec->sec_info_type)
65765700
JJ
6319 {
6320 case ELF_INFO_TYPE_STABS:
6321 case ELF_INFO_TYPE_EH_FRAME:
b34976b6 6322 return TRUE;
65765700
JJ
6323 default:
6324 break;
6325 }
40b829d4
AM
6326
6327 bed = get_elf_backend_data (sec->owner);
6328 if (bed->elf_backend_ignore_discarded_relocs != NULL
6329 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
b34976b6 6330 return TRUE;
65765700 6331
b34976b6 6332 return FALSE;
73d074b4 6333}
This page took 0.820975 seconds and 4 git commands to generate.