* elf-bfd.h (is_elf_hash_table): Take hash tab rather than info arg.
[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
268b6b39 2836 (struct elf_final_link_info *, const char *, Elf_Internal_Sym *, asection *);
b34976b6 2837static bfd_boolean elf_link_flush_output_syms
268b6b39 2838 (struct elf_final_link_info *);
b34976b6 2839static bfd_boolean elf_link_output_extsym
268b6b39 2840 (struct elf_link_hash_entry *, void *);
b34976b6 2841static bfd_boolean elf_link_input_bfd
268b6b39 2842 (struct elf_final_link_info *, bfd *);
b34976b6 2843static bfd_boolean elf_reloc_link_order
268b6b39 2844 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
252b5132
RH
2845
2846/* This struct is used to pass information to elf_link_output_extsym. */
2847
2848struct elf_outext_info
2849{
b34976b6
AM
2850 bfd_boolean failed;
2851 bfd_boolean localsyms;
252b5132
RH
2852 struct elf_final_link_info *finfo;
2853};
2854
1049f94e 2855/* When performing a relocatable link, the input relocations are
31367b81
MM
2856 preserved. But, if they reference global symbols, the indices
2857 referenced must be updated. Update all the relocations in
2858 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
2859
2860static void
268b6b39
AM
2861elf_link_adjust_relocs (bfd *abfd,
2862 Elf_Internal_Shdr *rel_hdr,
2863 unsigned int count,
2864 struct elf_link_hash_entry **rel_hash)
31367b81
MM
2865{
2866 unsigned int i;
9c5bfbb7 2867 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
947216bf 2868 bfd_byte *erela;
268b6b39
AM
2869 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2870 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
209f668e 2871
947216bf 2872 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
209f668e 2873 {
947216bf
AM
2874 swap_in = bed->s->swap_reloc_in;
2875 swap_out = bed->s->swap_reloc_out;
209f668e 2876 }
947216bf 2877 else if (rel_hdr->sh_entsize == sizeof (Elf_External_Rela))
209f668e 2878 {
947216bf
AM
2879 swap_in = bed->s->swap_reloca_in;
2880 swap_out = bed->s->swap_reloca_out;
209f668e 2881 }
947216bf
AM
2882 else
2883 abort ();
2884
2885 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
2886 abort ();
31367b81 2887
947216bf
AM
2888 erela = rel_hdr->contents;
2889 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
31367b81 2890 {
947216bf
AM
2891 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
2892 unsigned int j;
2893
31367b81
MM
2894 if (*rel_hash == NULL)
2895 continue;
2896
2897 BFD_ASSERT ((*rel_hash)->indx >= 0);
2898
947216bf
AM
2899 (*swap_in) (abfd, erela, irela);
2900 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
2901 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
2902 ELF_R_TYPE (irela[j].r_info));
2903 (*swap_out) (abfd, irela, erela);
31367b81
MM
2904 }
2905}
2906
58821868
AM
2907struct elf_link_sort_rela
2908{
db6751f2
JJ
2909 bfd_vma offset;
2910 enum elf_reloc_type_class type;
947216bf
AM
2911 /* We use this as an array of size int_rels_per_ext_rel. */
2912 Elf_Internal_Rela rela[1];
db6751f2
JJ
2913};
2914
2915static int
268b6b39 2916elf_link_sort_cmp1 (const void *A, const void *B)
db6751f2 2917{
268b6b39
AM
2918 const struct elf_link_sort_rela *a = A;
2919 const struct elf_link_sort_rela *b = B;
db6751f2
JJ
2920 int relativea, relativeb;
2921
2922 relativea = a->type == reloc_class_relative;
2923 relativeb = b->type == reloc_class_relative;
2924
2925 if (relativea < relativeb)
db6751f2 2926 return 1;
fcfbdf31
JJ
2927 if (relativea > relativeb)
2928 return -1;
947216bf 2929 if (ELF_R_SYM (a->rela->r_info) < ELF_R_SYM (b->rela->r_info))
db6751f2 2930 return -1;
947216bf 2931 if (ELF_R_SYM (a->rela->r_info) > ELF_R_SYM (b->rela->r_info))
db6751f2 2932 return 1;
947216bf 2933 if (a->rela->r_offset < b->rela->r_offset)
db6751f2 2934 return -1;
947216bf 2935 if (a->rela->r_offset > b->rela->r_offset)
db6751f2
JJ
2936 return 1;
2937 return 0;
2938}
2939
2940static int
268b6b39 2941elf_link_sort_cmp2 (const void *A, const void *B)
db6751f2 2942{
268b6b39
AM
2943 const struct elf_link_sort_rela *a = A;
2944 const struct elf_link_sort_rela *b = B;
db6751f2
JJ
2945 int copya, copyb;
2946
2947 if (a->offset < b->offset)
2948 return -1;
2949 if (a->offset > b->offset)
2950 return 1;
290394d6
JJ
2951 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
2952 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
db6751f2
JJ
2953 if (copya < copyb)
2954 return -1;
2955 if (copya > copyb)
2956 return 1;
947216bf 2957 if (a->rela->r_offset < b->rela->r_offset)
db6751f2 2958 return -1;
947216bf 2959 if (a->rela->r_offset > b->rela->r_offset)
db6751f2
JJ
2960 return 1;
2961 return 0;
2962}
2963
2964static size_t
268b6b39 2965elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
db6751f2 2966{
e717da7e 2967 asection *reldyn;
f51e552e 2968 bfd_size_type count, size;
947216bf
AM
2969 size_t i, ret, sort_elt, ext_size;
2970 bfd_byte *sort, *s_non_relative, *p;
2971 struct elf_link_sort_rela *sq;
9c5bfbb7 2972 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
033fd5f9 2973 int i2e = bed->s->int_rels_per_ext_rel;
268b6b39
AM
2974 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2975 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
e717da7e 2976 struct bfd_link_order *lo;
db6751f2
JJ
2977
2978 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
2979 if (reldyn == NULL || reldyn->_raw_size == 0)
2980 {
2981 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
2982 if (reldyn == NULL || reldyn->_raw_size == 0)
2983 return 0;
947216bf
AM
2984 ext_size = sizeof (Elf_External_Rel);
2985 swap_in = bed->s->swap_reloc_in;
2986 swap_out = bed->s->swap_reloc_out;
db6751f2
JJ
2987 }
2988 else
947216bf
AM
2989 {
2990 ext_size = sizeof (Elf_External_Rela);
2991 swap_in = bed->s->swap_reloca_in;
2992 swap_out = bed->s->swap_reloca_out;
2993 }
2994 count = reldyn->_raw_size / ext_size;
db6751f2
JJ
2995
2996 size = 0;
e717da7e
AM
2997 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
2998 if (lo->type == bfd_indirect_link_order)
2999 {
3000 asection *o = lo->u.indirect.section;
3001 size += o->_raw_size;
3002 }
db6751f2
JJ
3003
3004 if (size != reldyn->_raw_size)
3005 return 0;
3006
947216bf
AM
3007 sort_elt = (sizeof (struct elf_link_sort_rela)
3008 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3009 sort = bfd_zmalloc (sort_elt * count);
3010 if (sort == NULL)
db6751f2
JJ
3011 {
3012 (*info->callbacks->warning)
268b6b39 3013 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
db6751f2
JJ
3014 return 0;
3015 }
3016
e717da7e
AM
3017 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
3018 if (lo->type == bfd_indirect_link_order)
db6751f2 3019 {
947216bf 3020 bfd_byte *erel, *erelend;
e717da7e 3021 asection *o = lo->u.indirect.section;
db6751f2 3022
947216bf
AM
3023 erel = o->contents;
3024 erelend = o->contents + o->_raw_size;
3025 p = sort + o->output_offset / ext_size * sort_elt;
3026 while (erel < erelend)
db6751f2 3027 {
947216bf
AM
3028 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3029 (*swap_in) (abfd, erel, s->rela);
3030 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
3031 p += sort_elt;
3032 erel += ext_size;
db6751f2
JJ
3033 }
3034 }
3035
268b6b39 3036 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
947216bf
AM
3037
3038 for (i = 0, p = sort; i < count; i++, p += sort_elt)
db6751f2 3039 {
947216bf
AM
3040 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3041 if (s->type != reloc_class_relative)
3042 break;
db6751f2 3043 }
947216bf
AM
3044 ret = i;
3045 s_non_relative = p;
3046
3047 sq = (struct elf_link_sort_rela *) s_non_relative;
3048 for (; i < count; i++, p += sort_elt)
3049 {
3050 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
3051 if (ELF_R_SYM (sp->rela->r_info) != ELF_R_SYM (sq->rela->r_info))
3052 sq = sp;
3053 sp->offset = sq->rela->r_offset;
3054 }
3055
268b6b39 3056 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
dc810e39 3057
e717da7e
AM
3058 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
3059 if (lo->type == bfd_indirect_link_order)
db6751f2 3060 {
947216bf 3061 bfd_byte *erel, *erelend;
e717da7e 3062 asection *o = lo->u.indirect.section;
db6751f2 3063
947216bf
AM
3064 erel = o->contents;
3065 erelend = o->contents + o->_raw_size;
3066 p = sort + o->output_offset / ext_size * sort_elt;
3067 while (erel < erelend)
db6751f2 3068 {
947216bf
AM
3069 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3070 (*swap_out) (abfd, s->rela, erel);
3071 p += sort_elt;
3072 erel += ext_size;
db6751f2
JJ
3073 }
3074 }
3075
5ed6aba4 3076 free (sort);
db6751f2
JJ
3077 *psec = reldyn;
3078 return ret;
3079}
3080
252b5132
RH
3081/* Do the final step of an ELF link. */
3082
b34976b6 3083bfd_boolean
268b6b39 3084elf_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
252b5132 3085{
b34976b6
AM
3086 bfd_boolean dynamic;
3087 bfd_boolean emit_relocs;
252b5132
RH
3088 bfd *dynobj;
3089 struct elf_final_link_info finfo;
3090 register asection *o;
3091 register struct bfd_link_order *p;
3092 register bfd *sub;
dc810e39
AM
3093 bfd_size_type max_contents_size;
3094 bfd_size_type max_external_reloc_size;
3095 bfd_size_type max_internal_reloc_count;
3096 bfd_size_type max_sym_count;
9ad5cbcf 3097 bfd_size_type max_sym_shndx_count;
252b5132
RH
3098 file_ptr off;
3099 Elf_Internal_Sym elfsym;
3100 unsigned int i;
3101 Elf_Internal_Shdr *symtab_hdr;
c97e73dd 3102 Elf_Internal_Shdr *symtab_shndx_hdr;
252b5132 3103 Elf_Internal_Shdr *symstrtab_hdr;
9c5bfbb7 3104 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 3105 struct elf_outext_info eoinfo;
b34976b6 3106 bfd_boolean merged;
db6751f2
JJ
3107 size_t relativecount = 0;
3108 asection *reldyn = 0;
dc810e39 3109 bfd_size_type amt;
252b5132 3110
0eddce27 3111 if (! is_elf_hash_table (info->hash))
b34976b6 3112 return FALSE;
8ea2e4bd 3113
252b5132
RH
3114 if (info->shared)
3115 abfd->flags |= DYNAMIC;
3116
3117 dynamic = elf_hash_table (info)->dynamic_sections_created;
3118 dynobj = elf_hash_table (info)->dynobj;
3119
1049f94e 3120 emit_relocs = (info->relocatable
c44233aa
AM
3121 || info->emitrelocations
3122 || bed->elf_backend_emit_relocs);
9317eacc 3123
252b5132
RH
3124 finfo.info = info;
3125 finfo.output_bfd = abfd;
3126 finfo.symstrtab = elf_stringtab_init ();
3127 if (finfo.symstrtab == NULL)
b34976b6 3128 return FALSE;
252b5132
RH
3129
3130 if (! dynamic)
3131 {
3132 finfo.dynsym_sec = NULL;
3133 finfo.hash_sec = NULL;
3134 finfo.symver_sec = NULL;
3135 }
3136 else
3137 {
3138 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
3139 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
3140 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
3141 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
3142 /* Note that it is OK if symver_sec is NULL. */
3143 }
3144
3145 finfo.contents = NULL;
3146 finfo.external_relocs = NULL;
3147 finfo.internal_relocs = NULL;
3148 finfo.external_syms = NULL;
9ad5cbcf 3149 finfo.locsym_shndx = NULL;
252b5132
RH
3150 finfo.internal_syms = NULL;
3151 finfo.indices = NULL;
3152 finfo.sections = NULL;
3153 finfo.symbuf = NULL;
9ad5cbcf 3154 finfo.symshndxbuf = NULL;
252b5132 3155 finfo.symbuf_count = 0;
c97e73dd 3156 finfo.shndxbuf_size = 0;
252b5132
RH
3157
3158 /* Count up the number of relocations we will output for each output
3159 section, so that we know the sizes of the reloc sections. We
3160 also figure out some maximum sizes. */
3161 max_contents_size = 0;
3162 max_external_reloc_size = 0;
3163 max_internal_reloc_count = 0;
3164 max_sym_count = 0;
9ad5cbcf 3165 max_sym_shndx_count = 0;
b34976b6 3166 merged = FALSE;
268b6b39 3167 for (o = abfd->sections; o != NULL; o = o->next)
252b5132 3168 {
3f9a32bd 3169 struct bfd_elf_section_data *esdo = elf_section_data (o);
252b5132
RH
3170 o->reloc_count = 0;
3171
3172 for (p = o->link_order_head; p != NULL; p = p->next)
3173 {
3f9a32bd
AM
3174 unsigned int reloc_count = 0;
3175 struct bfd_elf_section_data *esdi = NULL;
3176 unsigned int *rel_count1;
3177
252b5132
RH
3178 if (p->type == bfd_section_reloc_link_order
3179 || p->type == bfd_symbol_reloc_link_order)
3f9a32bd 3180 reloc_count = 1;
252b5132
RH
3181 else if (p->type == bfd_indirect_link_order)
3182 {
3183 asection *sec;
3184
3185 sec = p->u.indirect.section;
3f9a32bd 3186 esdi = elf_section_data (sec);
252b5132
RH
3187
3188 /* Mark all sections which are to be included in the
3189 link. This will normally be every section. We need
3190 to do this so that we can identify any sections which
3191 the linker has decided to not include. */
b34976b6 3192 sec->linker_mark = TRUE;
252b5132 3193
f5fa8ca2 3194 if (sec->flags & SEC_MERGE)
b34976b6 3195 merged = TRUE;
f5fa8ca2 3196
1049f94e 3197 if (info->relocatable || info->emitrelocations)
3f9a32bd 3198 reloc_count = sec->reloc_count;
c44233aa 3199 else if (bed->elf_backend_count_relocs)
9317eacc
CM
3200 {
3201 Elf_Internal_Rela * relocs;
3202
268b6b39
AM
3203 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
3204 info->keep_memory);
9317eacc 3205
3f9a32bd 3206 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
9317eacc 3207
6cdc0ccc 3208 if (elf_section_data (o)->relocs != relocs)
9317eacc
CM
3209 free (relocs);
3210 }
252b5132
RH
3211
3212 if (sec->_raw_size > max_contents_size)
3213 max_contents_size = sec->_raw_size;
3214 if (sec->_cooked_size > max_contents_size)
3215 max_contents_size = sec->_cooked_size;
3216
3217 /* We are interested in just local symbols, not all
3218 symbols. */
3219 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
3220 && (sec->owner->flags & DYNAMIC) == 0)
3221 {
3222 size_t sym_count;
3223
3224 if (elf_bad_symtab (sec->owner))
3225 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
3226 / sizeof (Elf_External_Sym));
3227 else
3228 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
3229
3230 if (sym_count > max_sym_count)
3231 max_sym_count = sym_count;
3232
9ad5cbcf
AM
3233 if (sym_count > max_sym_shndx_count
3234 && elf_symtab_shndx (sec->owner) != 0)
3235 max_sym_shndx_count = sym_count;
3236
252b5132
RH
3237 if ((sec->flags & SEC_RELOC) != 0)
3238 {
3239 size_t ext_size;
3240
3241 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
3242 if (ext_size > max_external_reloc_size)
3243 max_external_reloc_size = ext_size;
3244 if (sec->reloc_count > max_internal_reloc_count)
3245 max_internal_reloc_count = sec->reloc_count;
3246 }
3247 }
3248 }
3f9a32bd
AM
3249
3250 if (reloc_count == 0)
3251 continue;
3252
3253 o->reloc_count += reloc_count;
3254
3255 /* MIPS may have a mix of REL and RELA relocs on sections.
3256 To support this curious ABI we keep reloc counts in
3257 elf_section_data too. We must be careful to add the
3258 relocations from the input section to the right output
3259 count. FIXME: Get rid of one count. We have
3260 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
3261 rel_count1 = &esdo->rel_count;
3262 if (esdi != NULL)
3263 {
3264 bfd_boolean same_size;
3265 bfd_size_type entsize1;
3266
3267 entsize1 = esdi->rel_hdr.sh_entsize;
3268 BFD_ASSERT (entsize1 == sizeof (Elf_External_Rel)
3269 || entsize1 == sizeof (Elf_External_Rela));
3270 same_size = (!o->use_rela_p
3271 == (entsize1 == sizeof (Elf_External_Rel)));
3272
3273 if (!same_size)
3274 rel_count1 = &esdo->rel_count2;
3275
3276 if (esdi->rel_hdr2 != NULL)
3277 {
3278 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
3279 unsigned int alt_count;
3280 unsigned int *rel_count2;
3281
3282 BFD_ASSERT (entsize2 != entsize1
3283 && (entsize2 == sizeof (Elf_External_Rel)
3284 || entsize2 == sizeof (Elf_External_Rela)));
3285
3286 rel_count2 = &esdo->rel_count2;
3287 if (!same_size)
3288 rel_count2 = &esdo->rel_count;
3289
3290 /* The following is probably too simplistic if the
3291 backend counts output relocs unusually. */
3292 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
3293 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
3294 *rel_count2 += alt_count;
3295 reloc_count -= alt_count;
3296 }
3297 }
3298 *rel_count1 += reloc_count;
252b5132
RH
3299 }
3300
3301 if (o->reloc_count > 0)
3302 o->flags |= SEC_RELOC;
3303 else
3304 {
3305 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3306 set it (this is probably a bug) and if it is set
3307 assign_section_numbers will create a reloc section. */
3308 o->flags &=~ SEC_RELOC;
3309 }
3310
3311 /* If the SEC_ALLOC flag is not set, force the section VMA to
3312 zero. This is done in elf_fake_sections as well, but forcing
3313 the VMA to 0 here will ensure that relocs against these
3314 sections are handled correctly. */
3315 if ((o->flags & SEC_ALLOC) == 0
3316 && ! o->user_set_vma)
3317 o->vma = 0;
3318 }
3319
1049f94e 3320 if (! info->relocatable && merged)
f5fa8ca2 3321 elf_link_hash_traverse (elf_hash_table (info),
268b6b39 3322 _bfd_elf_link_sec_merge_syms, abfd);
f5fa8ca2 3323
252b5132
RH
3324 /* Figure out the file positions for everything but the symbol table
3325 and the relocs. We set symcount to force assign_section_numbers
3326 to create a symbol table. */
3327 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
3328 BFD_ASSERT (! abfd->output_has_begun);
3329 if (! _bfd_elf_compute_section_file_positions (abfd, info))
3330 goto error_return;
3331
3332 /* That created the reloc sections. Set their sizes, and assign
3333 them file positions, and allocate some buffers. */
3334 for (o = abfd->sections; o != NULL; o = o->next)
3335 {
3336 if ((o->flags & SEC_RELOC) != 0)
3337 {
45d6a902
AM
3338 if (!(_bfd_elf_link_size_reloc_section
3339 (abfd, &elf_section_data (o)->rel_hdr, o)))
252b5132
RH
3340 goto error_return;
3341
23bc299b 3342 if (elf_section_data (o)->rel_hdr2
45d6a902
AM
3343 && !(_bfd_elf_link_size_reloc_section
3344 (abfd, elf_section_data (o)->rel_hdr2, o)))
252b5132 3345 goto error_return;
252b5132 3346 }
b037af20
MM
3347
3348 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
3e932841 3349 to count upwards while actually outputting the relocations. */
b037af20
MM
3350 elf_section_data (o)->rel_count = 0;
3351 elf_section_data (o)->rel_count2 = 0;
252b5132
RH
3352 }
3353
3354 _bfd_elf_assign_file_positions_for_relocs (abfd);
3355
3356 /* We have now assigned file positions for all the sections except
3357 .symtab and .strtab. We start the .symtab section at the current
3358 file position, and write directly to it. We build the .strtab
3359 section in memory. */
3360 bfd_get_symcount (abfd) = 0;
3361 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3362 /* sh_name is set in prep_headers. */
3363 symtab_hdr->sh_type = SHT_SYMTAB;
c97e73dd 3364 /* sh_flags, sh_addr and sh_size all start off zero. */
252b5132
RH
3365 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
3366 /* sh_link is set in assign_section_numbers. */
3367 /* sh_info is set below. */
3368 /* sh_offset is set just below. */
45d6a902 3369 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
252b5132
RH
3370
3371 off = elf_tdata (abfd)->next_file_pos;
b34976b6 3372 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
252b5132
RH
3373
3374 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3375 incorrect. We do not yet know the size of the .symtab section.
3376 We correct next_file_pos below, after we do know the size. */
3377
3378 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3379 continuously seeking to the right position in the file. */
3380 if (! info->keep_memory || max_sym_count < 20)
3381 finfo.symbuf_size = 20;
3382 else
3383 finfo.symbuf_size = max_sym_count;
dc810e39
AM
3384 amt = finfo.symbuf_size;
3385 amt *= sizeof (Elf_External_Sym);
268b6b39 3386 finfo.symbuf = bfd_malloc (amt);
252b5132
RH
3387 if (finfo.symbuf == NULL)
3388 goto error_return;
9ad5cbcf
AM
3389 if (elf_numsections (abfd) > SHN_LORESERVE)
3390 {
c97e73dd
AM
3391 /* Wild guess at number of output symbols. realloc'd as needed. */
3392 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
3393 finfo.shndxbuf_size = amt;
9ad5cbcf 3394 amt *= sizeof (Elf_External_Sym_Shndx);
268b6b39 3395 finfo.symshndxbuf = bfd_zmalloc (amt);
9ad5cbcf
AM
3396 if (finfo.symshndxbuf == NULL)
3397 goto error_return;
3398 }
252b5132
RH
3399
3400 /* Start writing out the symbol table. The first symbol is always a
3401 dummy symbol. */
9317eacc
CM
3402 if (info->strip != strip_all
3403 || emit_relocs)
252b5132
RH
3404 {
3405 elfsym.st_value = 0;
3406 elfsym.st_size = 0;
3407 elfsym.st_info = 0;
3408 elfsym.st_other = 0;
3409 elfsym.st_shndx = SHN_UNDEF;
268b6b39 3410 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr))
252b5132
RH
3411 goto error_return;
3412 }
3413
3414#if 0
3415 /* Some standard ELF linkers do this, but we don't because it causes
3416 bootstrap comparison failures. */
3417 /* Output a file symbol for the output file as the second symbol.
3418 We output this even if we are discarding local symbols, although
3419 I'm not sure if this is correct. */
3420 elfsym.st_value = 0;
3421 elfsym.st_size = 0;
3422 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3423 elfsym.st_other = 0;
3424 elfsym.st_shndx = SHN_ABS;
3425 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
3426 &elfsym, bfd_abs_section_ptr))
3427 goto error_return;
3428#endif
3429
3430 /* Output a symbol for each section. We output these even if we are
3431 discarding local symbols, since they are used for relocs. These
3432 symbols have no names. We store the index of each one in the
3433 index field of the section, so that we can find it again when
3434 outputting relocs. */
9317eacc
CM
3435 if (info->strip != strip_all
3436 || emit_relocs)
252b5132
RH
3437 {
3438 elfsym.st_size = 0;
3439 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3440 elfsym.st_other = 0;
9ad5cbcf 3441 for (i = 1; i < elf_numsections (abfd); i++)
252b5132
RH
3442 {
3443 o = section_from_elf_index (abfd, i);
3444 if (o != NULL)
3445 o->target_index = bfd_get_symcount (abfd);
3446 elfsym.st_shndx = i;
1049f94e 3447 if (info->relocatable || o == NULL)
252b5132
RH
3448 elfsym.st_value = 0;
3449 else
3450 elfsym.st_value = o->vma;
268b6b39 3451 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o))
252b5132 3452 goto error_return;
c97e73dd 3453 if (i == SHN_LORESERVE - 1)
9ad5cbcf 3454 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132
RH
3455 }
3456 }
3457
3458 /* Allocate some memory to hold information read in from the input
3459 files. */
9ad5cbcf
AM
3460 if (max_contents_size != 0)
3461 {
268b6b39 3462 finfo.contents = bfd_malloc (max_contents_size);
9ad5cbcf
AM
3463 if (finfo.contents == NULL)
3464 goto error_return;
3465 }
3466
3467 if (max_external_reloc_size != 0)
3468 {
268b6b39 3469 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
9ad5cbcf
AM
3470 if (finfo.external_relocs == NULL)
3471 goto error_return;
3472 }
3473
3474 if (max_internal_reloc_count != 0)
3475 {
3476 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
3477 amt *= sizeof (Elf_Internal_Rela);
268b6b39 3478 finfo.internal_relocs = bfd_malloc (amt);
9ad5cbcf
AM
3479 if (finfo.internal_relocs == NULL)
3480 goto error_return;
3481 }
3482
3483 if (max_sym_count != 0)
3484 {
3485 amt = max_sym_count * sizeof (Elf_External_Sym);
268b6b39 3486 finfo.external_syms = bfd_malloc (amt);
9ad5cbcf
AM
3487 if (finfo.external_syms == NULL)
3488 goto error_return;
3489
3490 amt = max_sym_count * sizeof (Elf_Internal_Sym);
268b6b39 3491 finfo.internal_syms = bfd_malloc (amt);
9ad5cbcf
AM
3492 if (finfo.internal_syms == NULL)
3493 goto error_return;
3494
3495 amt = max_sym_count * sizeof (long);
268b6b39 3496 finfo.indices = bfd_malloc (amt);
9ad5cbcf
AM
3497 if (finfo.indices == NULL)
3498 goto error_return;
3499
3500 amt = max_sym_count * sizeof (asection *);
268b6b39 3501 finfo.sections = bfd_malloc (amt);
9ad5cbcf
AM
3502 if (finfo.sections == NULL)
3503 goto error_return;
3504 }
3505
3506 if (max_sym_shndx_count != 0)
3507 {
3508 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
268b6b39 3509 finfo.locsym_shndx = bfd_malloc (amt);
9ad5cbcf
AM
3510 if (finfo.locsym_shndx == NULL)
3511 goto error_return;
3512 }
252b5132 3513
e1918d23 3514 if (elf_hash_table (info)->tls_sec)
13ae64f3 3515 {
e1918d23 3516 bfd_vma base, end = 0;
13ae64f3
JJ
3517 asection *sec;
3518
e1918d23 3519 for (sec = elf_hash_table (info)->tls_sec;
13ae64f3
JJ
3520 sec && (sec->flags & SEC_THREAD_LOCAL);
3521 sec = sec->next)
3522 {
3523 bfd_vma size = sec->_raw_size;
3524
e1918d23 3525 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
13ae64f3
JJ
3526 {
3527 struct bfd_link_order *o;
3528
13ae64f3
JJ
3529 for (o = sec->link_order_head; o != NULL; o = o->next)
3530 if (size < o->offset + o->size)
58821868 3531 size = o->offset + o->size;
13ae64f3
JJ
3532 }
3533 end = sec->vma + size;
3534 }
e1918d23
AM
3535 base = elf_hash_table (info)->tls_sec->vma;
3536 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
3537 elf_hash_table (info)->tls_size = end - base;
13ae64f3
JJ
3538 }
3539
252b5132
RH
3540 /* Since ELF permits relocations to be against local symbols, we
3541 must have the local symbols available when we do the relocations.
3542 Since we would rather only read the local symbols once, and we
3543 would rather not keep them in memory, we handle all the
3544 relocations for a single input file at the same time.
3545
3546 Unfortunately, there is no way to know the total number of local
3547 symbols until we have seen all of them, and the local symbol
3548 indices precede the global symbol indices. This means that when
1049f94e 3549 we are generating relocatable output, and we see a reloc against
252b5132
RH
3550 a global symbol, we can not know the symbol index until we have
3551 finished examining all the local symbols to see which ones we are
3552 going to output. To deal with this, we keep the relocations in
3553 memory, and don't output them until the end of the link. This is
3554 an unfortunate waste of memory, but I don't see a good way around
1049f94e 3555 it. Fortunately, it only happens when performing a relocatable
252b5132
RH
3556 link, which is not the common case. FIXME: If keep_memory is set
3557 we could write the relocs out and then read them again; I don't
3558 know how bad the memory loss will be. */
3559
3560 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
b34976b6 3561 sub->output_has_begun = FALSE;
252b5132
RH
3562 for (o = abfd->sections; o != NULL; o = o->next)
3563 {
3564 for (p = o->link_order_head; p != NULL; p = p->next)
3565 {
3566 if (p->type == bfd_indirect_link_order
a50c1845 3567 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
4ddafab0 3568 == bfd_target_elf_flavour)
a50c1845 3569 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
252b5132 3570 {
252b5132
RH
3571 if (! sub->output_has_begun)
3572 {
3573 if (! elf_link_input_bfd (&finfo, sub))
3574 goto error_return;
b34976b6 3575 sub->output_has_begun = TRUE;
252b5132
RH
3576 }
3577 }
3578 else if (p->type == bfd_section_reloc_link_order
3579 || p->type == bfd_symbol_reloc_link_order)
3580 {
3581 if (! elf_reloc_link_order (abfd, info, o, p))
3582 goto error_return;
3583 }
3584 else
3585 {
3586 if (! _bfd_default_link_order (abfd, info, o, p))
3587 goto error_return;
3588 }
3589 }
3590 }
3591
c44233aa
AM
3592 /* Output any global symbols that got converted to local in a
3593 version script or due to symbol visibility. We do this in a
3594 separate step since ELF requires all local symbols to appear
3595 prior to any global symbols. FIXME: We should only do this if
3596 some global symbols were, in fact, converted to become local.
3597 FIXME: Will this work correctly with the Irix 5 linker? */
b34976b6 3598 eoinfo.failed = FALSE;
c44233aa 3599 eoinfo.finfo = &finfo;
b34976b6 3600 eoinfo.localsyms = TRUE;
c44233aa 3601 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
268b6b39 3602 &eoinfo);
c44233aa 3603 if (eoinfo.failed)
b34976b6 3604 return FALSE;
c44233aa 3605
252b5132 3606 /* That wrote out all the local symbols. Finish up the symbol table
5cc7c785
L
3607 with the global symbols. Even if we want to strip everything we
3608 can, we still need to deal with those global symbols that got
3e932841 3609 converted to local in a version script. */
252b5132 3610
30b30c21 3611 /* The sh_info field records the index of the first non local symbol. */
252b5132 3612 symtab_hdr->sh_info = bfd_get_symcount (abfd);
30b30c21 3613
fc8c40a0
AM
3614 if (dynamic
3615 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
30b30c21
RH
3616 {
3617 Elf_Internal_Sym sym;
3618 Elf_External_Sym *dynsym =
a7b97311 3619 (Elf_External_Sym *) finfo.dynsym_sec->contents;
71a40b32 3620 long last_local = 0;
30b30c21
RH
3621
3622 /* Write out the section symbols for the output sections. */
3623 if (info->shared)
3624 {
3625 asection *s;
3626
3627 sym.st_size = 0;
3628 sym.st_name = 0;
3629 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3630 sym.st_other = 0;
3631
3632 for (s = abfd->sections; s != NULL; s = s->next)
3633 {
3634 int indx;
9ad5cbcf
AM
3635 Elf_External_Sym *dest;
3636
30b30c21
RH
3637 indx = elf_section_data (s)->this_idx;
3638 BFD_ASSERT (indx > 0);
3639 sym.st_shndx = indx;
3640 sym.st_value = s->vma;
9ad5cbcf 3641 dest = dynsym + elf_section_data (s)->dynindx;
268b6b39 3642 elf_swap_symbol_out (abfd, &sym, dest, 0);
30b30c21
RH
3643 }
3644
3645 last_local = bfd_count_sections (abfd);
3646 }
3647
3648 /* Write out the local dynsyms. */
3649 if (elf_hash_table (info)->dynlocal)
3650 {
3651 struct elf_link_local_dynamic_entry *e;
3652 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
3653 {
318da145 3654 asection *s;
9ad5cbcf 3655 Elf_External_Sym *dest;
30b30c21 3656
b037af20
MM
3657 sym.st_size = e->isym.st_size;
3658 sym.st_other = e->isym.st_other;
3659
1fa0ddb3
RH
3660 /* Copy the internal symbol as is.
3661 Note that we saved a word of storage and overwrote
c44233aa
AM
3662 the original st_name with the dynstr_index. */
3663 sym = e->isym;
30b30c21 3664
c8e5ddc8 3665 if (e->isym.st_shndx != SHN_UNDEF
58821868
AM
3666 && (e->isym.st_shndx < SHN_LORESERVE
3667 || e->isym.st_shndx > SHN_HIRESERVE))
587ff49e
RH
3668 {
3669 s = bfd_section_from_elf_index (e->input_bfd,
3670 e->isym.st_shndx);
3671
3672 sym.st_shndx =
3673 elf_section_data (s->output_section)->this_idx;
3674 sym.st_value = (s->output_section->vma
3675 + s->output_offset
3676 + e->isym.st_value);
3677 }
30b30c21
RH
3678
3679 if (last_local < e->dynindx)
3680 last_local = e->dynindx;
3681
9ad5cbcf 3682 dest = dynsym + e->dynindx;
268b6b39 3683 elf_swap_symbol_out (abfd, &sym, dest, 0);
30b30c21
RH
3684 }
3685 }
3686
71a40b32
ILT
3687 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
3688 last_local + 1;
30b30c21 3689 }
252b5132
RH
3690
3691 /* We get the global symbols from the hash table. */
b34976b6
AM
3692 eoinfo.failed = FALSE;
3693 eoinfo.localsyms = FALSE;
252b5132
RH
3694 eoinfo.finfo = &finfo;
3695 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
268b6b39 3696 &eoinfo);
252b5132 3697 if (eoinfo.failed)
b34976b6 3698 return FALSE;
252b5132 3699
587ff49e
RH
3700 /* If backend needs to output some symbols not present in the hash
3701 table, do it now. */
3702 if (bed->elf_backend_output_arch_syms)
3703 {
b34976b6 3704 typedef bfd_boolean (*out_sym_func)
268b6b39 3705 (void *, const char *, Elf_Internal_Sym *, asection *);
dc810e39
AM
3706
3707 if (! ((*bed->elf_backend_output_arch_syms)
268b6b39 3708 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
b34976b6 3709 return FALSE;
3e932841 3710 }
587ff49e 3711
252b5132
RH
3712 /* Flush all symbols to the file. */
3713 if (! elf_link_flush_output_syms (&finfo))
b34976b6 3714 return FALSE;
252b5132
RH
3715
3716 /* Now we know the size of the symtab section. */
3717 off += symtab_hdr->sh_size;
3718
c97e73dd
AM
3719 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3720 if (symtab_shndx_hdr->sh_name != 0)
3721 {
3722 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
3723 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
3724 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
3725 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
3726 symtab_shndx_hdr->sh_size = amt;
3727
3728 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
b34976b6 3729 off, TRUE);
c97e73dd
AM
3730
3731 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
268b6b39 3732 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
b34976b6 3733 return FALSE;
c97e73dd
AM
3734 }
3735
3736
252b5132
RH
3737 /* Finish up and write out the symbol string table (.strtab)
3738 section. */
3739 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3740 /* sh_name was set in prep_headers. */
3741 symstrtab_hdr->sh_type = SHT_STRTAB;
3742 symstrtab_hdr->sh_flags = 0;
3743 symstrtab_hdr->sh_addr = 0;
3744 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
3745 symstrtab_hdr->sh_entsize = 0;
3746 symstrtab_hdr->sh_link = 0;
3747 symstrtab_hdr->sh_info = 0;
3748 /* sh_offset is set just below. */
3749 symstrtab_hdr->sh_addralign = 1;
3750
b34976b6 3751 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
252b5132
RH
3752 elf_tdata (abfd)->next_file_pos = off;
3753
3754 if (bfd_get_symcount (abfd) > 0)
3755 {
3756 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
3757 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
b34976b6 3758 return FALSE;
252b5132
RH
3759 }
3760
3761 /* Adjust the relocs to have the correct symbol indices. */
3762 for (o = abfd->sections; o != NULL; o = o->next)
3763 {
252b5132
RH
3764 if ((o->flags & SEC_RELOC) == 0)
3765 continue;
3766
3e932841 3767 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
31367b81
MM
3768 elf_section_data (o)->rel_count,
3769 elf_section_data (o)->rel_hashes);
3770 if (elf_section_data (o)->rel_hdr2 != NULL)
3771 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
3772 elf_section_data (o)->rel_count2,
3e932841 3773 (elf_section_data (o)->rel_hashes
31367b81 3774 + elf_section_data (o)->rel_count));
252b5132
RH
3775
3776 /* Set the reloc_count field to 0 to prevent write_relocs from
3777 trying to swap the relocs out itself. */
3778 o->reloc_count = 0;
3779 }
3780
db6751f2
JJ
3781 if (dynamic && info->combreloc && dynobj != NULL)
3782 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
3783
252b5132
RH
3784 /* If we are linking against a dynamic object, or generating a
3785 shared library, finish up the dynamic linking information. */
3786 if (dynamic)
3787 {
3788 Elf_External_Dyn *dyncon, *dynconend;
3789
3790 /* Fix up .dynamic entries. */
3791 o = bfd_get_section_by_name (dynobj, ".dynamic");
3792 BFD_ASSERT (o != NULL);
3793
3794 dyncon = (Elf_External_Dyn *) o->contents;
3795 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
3796 for (; dyncon < dynconend; dyncon++)
3797 {
3798 Elf_Internal_Dyn dyn;
3799 const char *name;
3800 unsigned int type;
3801
3802 elf_swap_dyn_in (dynobj, dyncon, &dyn);
3803
3804 switch (dyn.d_tag)
3805 {
3806 default:
3807 break;
db6751f2
JJ
3808 case DT_NULL:
3809 if (relativecount > 0 && dyncon + 1 < dynconend)
3810 {
3811 switch (elf_section_data (reldyn)->this_hdr.sh_type)
3812 {
3813 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
3814 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
3815 default: break;
3816 }
3817 if (dyn.d_tag != DT_NULL)
3818 {
3819 dyn.d_un.d_val = relativecount;
3820 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3821 relativecount = 0;
3822 }
3823 }
3824 break;
252b5132 3825 case DT_INIT:
f0c2e336 3826 name = info->init_function;
252b5132
RH
3827 goto get_sym;
3828 case DT_FINI:
f0c2e336 3829 name = info->fini_function;
252b5132
RH
3830 get_sym:
3831 {
3832 struct elf_link_hash_entry *h;
3833
3834 h = elf_link_hash_lookup (elf_hash_table (info), name,
b34976b6 3835 FALSE, FALSE, TRUE);
252b5132
RH
3836 if (h != NULL
3837 && (h->root.type == bfd_link_hash_defined
3838 || h->root.type == bfd_link_hash_defweak))
3839 {
3840 dyn.d_un.d_val = h->root.u.def.value;
3841 o = h->root.u.def.section;
3842 if (o->output_section != NULL)
3843 dyn.d_un.d_val += (o->output_section->vma
3844 + o->output_offset);
3845 else
3846 {
3847 /* The symbol is imported from another shared
3848 library and does not apply to this one. */
3849 dyn.d_un.d_val = 0;
3850 }
3851
3852 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3853 }
3854 }
3855 break;
3856
30831527
RH
3857 case DT_PREINIT_ARRAYSZ:
3858 name = ".preinit_array";
3859 goto get_size;
3860 case DT_INIT_ARRAYSZ:
3861 name = ".init_array";
3862 goto get_size;
3863 case DT_FINI_ARRAYSZ:
3864 name = ".fini_array";
3865 get_size:
3866 o = bfd_get_section_by_name (abfd, name);
2cb69dd3
JL
3867 if (o == NULL)
3868 {
3869 (*_bfd_error_handler)
3870 (_("%s: could not find output section %s"),
3871 bfd_get_filename (abfd), name);
3872 goto error_return;
3873 }
25e27870
L
3874 if (o->_raw_size == 0)
3875 (*_bfd_error_handler)
3876 (_("warning: %s section has zero size"), name);
30831527
RH
3877 dyn.d_un.d_val = o->_raw_size;
3878 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3879 break;
3880
3881 case DT_PREINIT_ARRAY:
3882 name = ".preinit_array";
3883 goto get_vma;
3884 case DT_INIT_ARRAY:
3885 name = ".init_array";
3886 goto get_vma;
3887 case DT_FINI_ARRAY:
3888 name = ".fini_array";
3889 goto get_vma;
3890
252b5132
RH
3891 case DT_HASH:
3892 name = ".hash";
3893 goto get_vma;
3894 case DT_STRTAB:
3895 name = ".dynstr";
3896 goto get_vma;
3897 case DT_SYMTAB:
3898 name = ".dynsym";
3899 goto get_vma;
3900 case DT_VERDEF:
3901 name = ".gnu.version_d";
3902 goto get_vma;
3903 case DT_VERNEED:
3904 name = ".gnu.version_r";
3905 goto get_vma;
3906 case DT_VERSYM:
3907 name = ".gnu.version";
3908 get_vma:
3909 o = bfd_get_section_by_name (abfd, name);
2cb69dd3
JL
3910 if (o == NULL)
3911 {
3912 (*_bfd_error_handler)
3913 (_("%s: could not find output section %s"),
3914 bfd_get_filename (abfd), name);
3915 goto error_return;
3916 }
252b5132
RH
3917 dyn.d_un.d_ptr = o->vma;
3918 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3919 break;
3920
3921 case DT_REL:
3922 case DT_RELA:
3923 case DT_RELSZ:
3924 case DT_RELASZ:
3925 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
3926 type = SHT_REL;
3927 else
3928 type = SHT_RELA;
3929 dyn.d_un.d_val = 0;
9ad5cbcf 3930 for (i = 1; i < elf_numsections (abfd); i++)
252b5132
RH
3931 {
3932 Elf_Internal_Shdr *hdr;
3933
3934 hdr = elf_elfsections (abfd)[i];
3935 if (hdr->sh_type == type
3936 && (hdr->sh_flags & SHF_ALLOC) != 0)
3937 {
3938 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
3939 dyn.d_un.d_val += hdr->sh_size;
3940 else
3941 {
3942 if (dyn.d_un.d_val == 0
3943 || hdr->sh_addr < dyn.d_un.d_val)
3944 dyn.d_un.d_val = hdr->sh_addr;
3945 }
3946 }
3947 }
3948 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3949 break;
3950 }
3951 }
3952 }
3953
3954 /* If we have created any dynamic sections, then output them. */
3955 if (dynobj != NULL)
3956 {
3957 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
3958 goto error_return;
3959
3960 for (o = dynobj->sections; o != NULL; o = o->next)
3961 {
3962 if ((o->flags & SEC_HAS_CONTENTS) == 0
fc8c40a0
AM
3963 || o->_raw_size == 0
3964 || o->output_section == bfd_abs_section_ptr)
252b5132
RH
3965 continue;
3966 if ((o->flags & SEC_LINKER_CREATED) == 0)
3967 {
3968 /* At this point, we are only interested in sections
45d6a902 3969 created by _bfd_elf_link_create_dynamic_sections. */
252b5132
RH
3970 continue;
3971 }
3972 if ((elf_section_data (o->output_section)->this_hdr.sh_type
3973 != SHT_STRTAB)
3974 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
3975 {
3976 if (! bfd_set_section_contents (abfd, o->output_section,
dc810e39
AM
3977 o->contents,
3978 (file_ptr) o->output_offset,
252b5132
RH
3979 o->_raw_size))
3980 goto error_return;
3981 }
3982 else
3983 {
252b5132 3984 /* The contents of the .dynstr section are actually in a
c44233aa 3985 stringtab. */
252b5132
RH
3986 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
3987 if (bfd_seek (abfd, off, SEEK_SET) != 0
2b0f7ef9
JJ
3988 || ! _bfd_elf_strtab_emit (abfd,
3989 elf_hash_table (info)->dynstr))
252b5132
RH
3990 goto error_return;
3991 }
3992 }
3993 }
3994
1049f94e 3995 if (info->relocatable)
1126897b 3996 {
b34976b6 3997 bfd_boolean failed = FALSE;
1126897b
AM
3998
3999 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4000 if (failed)
4001 goto error_return;
4002 }
4003
252b5132
RH
4004 /* If we have optimized stabs strings, output them. */
4005 if (elf_hash_table (info)->stab_info != NULL)
4006 {
4007 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
4008 goto error_return;
4009 }
4010
126495ed 4011 if (info->eh_frame_hdr)
65765700 4012 {
126495ed
AM
4013 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
4014 goto error_return;
65765700
JJ
4015 }
4016
252b5132
RH
4017 if (finfo.symstrtab != NULL)
4018 _bfd_stringtab_free (finfo.symstrtab);
4019 if (finfo.contents != NULL)
4020 free (finfo.contents);
4021 if (finfo.external_relocs != NULL)
4022 free (finfo.external_relocs);
4023 if (finfo.internal_relocs != NULL)
4024 free (finfo.internal_relocs);
4025 if (finfo.external_syms != NULL)
4026 free (finfo.external_syms);
9ad5cbcf
AM
4027 if (finfo.locsym_shndx != NULL)
4028 free (finfo.locsym_shndx);
252b5132
RH
4029 if (finfo.internal_syms != NULL)
4030 free (finfo.internal_syms);
4031 if (finfo.indices != NULL)
4032 free (finfo.indices);
4033 if (finfo.sections != NULL)
4034 free (finfo.sections);
4035 if (finfo.symbuf != NULL)
4036 free (finfo.symbuf);
9ad5cbcf 4037 if (finfo.symshndxbuf != NULL)
c97e73dd 4038 free (finfo.symshndxbuf);
252b5132
RH
4039 for (o = abfd->sections; o != NULL; o = o->next)
4040 {
4041 if ((o->flags & SEC_RELOC) != 0
4042 && elf_section_data (o)->rel_hashes != NULL)
c44233aa 4043 free (elf_section_data (o)->rel_hashes);
252b5132
RH
4044 }
4045
b34976b6 4046 elf_tdata (abfd)->linker = TRUE;
252b5132 4047
b34976b6 4048 return TRUE;
252b5132
RH
4049
4050 error_return:
4051 if (finfo.symstrtab != NULL)
4052 _bfd_stringtab_free (finfo.symstrtab);
4053 if (finfo.contents != NULL)
4054 free (finfo.contents);
4055 if (finfo.external_relocs != NULL)
4056 free (finfo.external_relocs);
4057 if (finfo.internal_relocs != NULL)
4058 free (finfo.internal_relocs);
4059 if (finfo.external_syms != NULL)
4060 free (finfo.external_syms);
9ad5cbcf
AM
4061 if (finfo.locsym_shndx != NULL)
4062 free (finfo.locsym_shndx);
252b5132
RH
4063 if (finfo.internal_syms != NULL)
4064 free (finfo.internal_syms);
4065 if (finfo.indices != NULL)
4066 free (finfo.indices);
4067 if (finfo.sections != NULL)
4068 free (finfo.sections);
4069 if (finfo.symbuf != NULL)
4070 free (finfo.symbuf);
9ad5cbcf 4071 if (finfo.symshndxbuf != NULL)
c97e73dd 4072 free (finfo.symshndxbuf);
252b5132
RH
4073 for (o = abfd->sections; o != NULL; o = o->next)
4074 {
4075 if ((o->flags & SEC_RELOC) != 0
4076 && elf_section_data (o)->rel_hashes != NULL)
4077 free (elf_section_data (o)->rel_hashes);
4078 }
4079
b34976b6 4080 return FALSE;
252b5132
RH
4081}
4082
4083/* Add a symbol to the output symbol table. */
4084
b34976b6 4085static bfd_boolean
268b6b39
AM
4086elf_link_output_sym (struct elf_final_link_info *finfo,
4087 const char *name,
4088 Elf_Internal_Sym *elfsym,
4089 asection *input_sec)
252b5132 4090{
9ad5cbcf
AM
4091 Elf_External_Sym *dest;
4092 Elf_External_Sym_Shndx *destshndx;
b34976b6 4093 bfd_boolean (*output_symbol_hook)
268b6b39
AM
4094 (bfd *, struct bfd_link_info *info, const char *,
4095 Elf_Internal_Sym *, asection *);
252b5132
RH
4096
4097 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
4098 elf_backend_link_output_symbol_hook;
4099 if (output_symbol_hook != NULL)
4100 {
4101 if (! ((*output_symbol_hook)
4102 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
b34976b6 4103 return FALSE;
252b5132
RH
4104 }
4105
268b6b39 4106 if (name == NULL || *name == '\0')
252b5132
RH
4107 elfsym->st_name = 0;
4108 else if (input_sec->flags & SEC_EXCLUDE)
4109 elfsym->st_name = 0;
4110 else
4111 {
4112 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
b34976b6 4113 name, TRUE, FALSE);
252b5132 4114 if (elfsym->st_name == (unsigned long) -1)
b34976b6 4115 return FALSE;
252b5132
RH
4116 }
4117
4118 if (finfo->symbuf_count >= finfo->symbuf_size)
4119 {
4120 if (! elf_link_flush_output_syms (finfo))
b34976b6 4121 return FALSE;
252b5132
RH
4122 }
4123
9ad5cbcf
AM
4124 dest = finfo->symbuf + finfo->symbuf_count;
4125 destshndx = finfo->symshndxbuf;
4126 if (destshndx != NULL)
c97e73dd
AM
4127 {
4128 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
4129 {
4130 bfd_size_type amt;
252b5132 4131
c97e73dd
AM
4132 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
4133 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
4134 if (destshndx == NULL)
b34976b6 4135 return FALSE;
c97e73dd
AM
4136 memset ((char *) destshndx + amt, 0, amt);
4137 finfo->shndxbuf_size *= 2;
4138 }
4139 destshndx += bfd_get_symcount (finfo->output_bfd);
4140 }
4141
268b6b39 4142 elf_swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
c97e73dd
AM
4143 finfo->symbuf_count += 1;
4144 bfd_get_symcount (finfo->output_bfd) += 1;
252b5132 4145
b34976b6 4146 return TRUE;
252b5132
RH
4147}
4148
4149/* Flush the output symbols to the file. */
4150
b34976b6 4151static bfd_boolean
268b6b39 4152elf_link_flush_output_syms (struct elf_final_link_info *finfo)
252b5132
RH
4153{
4154 if (finfo->symbuf_count > 0)
4155 {
9ad5cbcf 4156 Elf_Internal_Shdr *hdr;
dc810e39
AM
4157 file_ptr pos;
4158 bfd_size_type amt;
252b5132 4159
9ad5cbcf
AM
4160 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
4161 pos = hdr->sh_offset + hdr->sh_size;
dc810e39
AM
4162 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
4163 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
268b6b39 4164 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
b34976b6 4165 return FALSE;
252b5132 4166
9ad5cbcf 4167 hdr->sh_size += amt;
252b5132
RH
4168 finfo->symbuf_count = 0;
4169 }
4170
b34976b6 4171 return TRUE;
252b5132
RH
4172}
4173
f5d44ba0
AM
4174/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
4175 allowing an unsatisfied unversioned symbol in the DSO to match a
71552942
L
4176 versioned symbol that would normally require an explicit version.
4177 We also handle the case that a DSO references a hidden symbol
4178 which may be satisfied by a versioned symbol in another DSO. */
f5d44ba0 4179
b34976b6 4180static bfd_boolean
268b6b39
AM
4181elf_link_check_versioned_symbol (struct bfd_link_info *info,
4182 struct elf_link_hash_entry *h)
f5d44ba0 4183{
71552942 4184 bfd *abfd;
f5d44ba0 4185 struct elf_link_loaded_list *loaded;
f5d44ba0 4186
0eddce27 4187 if (!is_elf_hash_table (info->hash))
b34976b6 4188 return FALSE;
f5d44ba0 4189
71552942
L
4190 switch (h->root.type)
4191 {
4192 default:
4193 abfd = NULL;
4194 break;
4195
4196 case bfd_link_hash_undefined:
4197 case bfd_link_hash_undefweak:
4198 abfd = h->root.u.undef.abfd;
4199 if ((abfd->flags & DYNAMIC) == 0 || elf_dt_soname (abfd) == NULL)
4200 return FALSE;
4201 break;
4202
4203 case bfd_link_hash_defined:
4204 case bfd_link_hash_defweak:
4205 abfd = h->root.u.def.section->owner;
4206 break;
4207
4208 case bfd_link_hash_common:
4209 abfd = h->root.u.c.p->section->owner;
4210 break;
4211 }
4212 BFD_ASSERT (abfd != NULL);
4213
f5d44ba0
AM
4214 for (loaded = elf_hash_table (info)->loaded;
4215 loaded != NULL;
4216 loaded = loaded->next)
4217 {
4218 bfd *input;
4219 Elf_Internal_Shdr *hdr;
4220 bfd_size_type symcount;
4221 bfd_size_type extsymcount;
4222 bfd_size_type extsymoff;
4223 Elf_Internal_Shdr *versymhdr;
6cdc0ccc
AM
4224 Elf_Internal_Sym *isym;
4225 Elf_Internal_Sym *isymend;
4226 Elf_Internal_Sym *isymbuf;
f5d44ba0 4227 Elf_External_Versym *ever;
6cdc0ccc 4228 Elf_External_Versym *extversym;
f5d44ba0
AM
4229
4230 input = loaded->abfd;
4231
4232 /* We check each DSO for a possible hidden versioned definition. */
71552942 4233 if (input == abfd
f5d44ba0
AM
4234 || (input->flags & DYNAMIC) == 0
4235 || elf_dynversym (input) == 0)
4236 continue;
4237
4238 hdr = &elf_tdata (input)->dynsymtab_hdr;
4239
4240 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
4241 if (elf_bad_symtab (input))
4242 {
4243 extsymcount = symcount;
4244 extsymoff = 0;
4245 }
4246 else
4247 {
4248 extsymcount = symcount - hdr->sh_info;
4249 extsymoff = hdr->sh_info;
4250 }
4251
4252 if (extsymcount == 0)
4253 continue;
4254
6cdc0ccc
AM
4255 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
4256 NULL, NULL, NULL);
4257 if (isymbuf == NULL)
b34976b6 4258 return FALSE;
f5d44ba0 4259
f5d44ba0
AM
4260 /* Read in any version definitions. */
4261 versymhdr = &elf_tdata (input)->dynversym_hdr;
268b6b39 4262 extversym = bfd_malloc (versymhdr->sh_size);
f5d44ba0
AM
4263 if (extversym == NULL)
4264 goto error_ret;
4265
4266 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
268b6b39 4267 || (bfd_bread (extversym, versymhdr->sh_size, input)
f5d44ba0
AM
4268 != versymhdr->sh_size))
4269 {
4270 free (extversym);
4271 error_ret:
6cdc0ccc 4272 free (isymbuf);
b34976b6 4273 return FALSE;
f5d44ba0
AM
4274 }
4275
4276 ever = extversym + extsymoff;
6cdc0ccc
AM
4277 isymend = isymbuf + extsymcount;
4278 for (isym = isymbuf; isym < isymend; isym++, ever++)
f5d44ba0
AM
4279 {
4280 const char *name;
f5d44ba0 4281 Elf_Internal_Versym iver;
a15d9d3a 4282 unsigned short version_index;
f5d44ba0 4283
6cdc0ccc
AM
4284 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
4285 || isym->st_shndx == SHN_UNDEF)
f5d44ba0
AM
4286 continue;
4287
4288 name = bfd_elf_string_from_elf_section (input,
4289 hdr->sh_link,
6cdc0ccc 4290 isym->st_name);
f5d44ba0
AM
4291 if (strcmp (name, h->root.root.string) != 0)
4292 continue;
4293
4294 _bfd_elf_swap_versym_in (input, ever, &iver);
4295
4296 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
4297 {
4298 /* If we have a non-hidden versioned sym, then it should
4299 have provided a definition for the undefined sym. */
4300 abort ();
4301 }
4302
a15d9d3a
L
4303 version_index = iver.vs_vers & VERSYM_VERSION;
4304 if (version_index == 1 || version_index == 2)
f5d44ba0 4305 {
a15d9d3a 4306 /* This is the base or first version. We can use it. */
f5d44ba0 4307 free (extversym);
6cdc0ccc 4308 free (isymbuf);
b34976b6 4309 return TRUE;
f5d44ba0
AM
4310 }
4311 }
4312
4313 free (extversym);
6cdc0ccc 4314 free (isymbuf);
f5d44ba0
AM
4315 }
4316
b34976b6 4317 return FALSE;
f5d44ba0
AM
4318}
4319
252b5132
RH
4320/* Add an external symbol to the symbol table. This is called from
4321 the hash table traversal routine. When generating a shared object,
4322 we go through the symbol table twice. The first time we output
4323 anything that might have been forced to local scope in a version
4324 script. The second time we output the symbols that are still
4325 global symbols. */
4326
b34976b6 4327static bfd_boolean
268b6b39 4328elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
252b5132 4329{
268b6b39 4330 struct elf_outext_info *eoinfo = data;
252b5132 4331 struct elf_final_link_info *finfo = eoinfo->finfo;
b34976b6 4332 bfd_boolean strip;
252b5132
RH
4333 Elf_Internal_Sym sym;
4334 asection *input_sec;
4335
e92d460e
AM
4336 if (h->root.type == bfd_link_hash_warning)
4337 {
4338 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4339 if (h->root.type == bfd_link_hash_new)
b34976b6 4340 return TRUE;
e92d460e
AM
4341 }
4342
252b5132
RH
4343 /* Decide whether to output this symbol in this pass. */
4344 if (eoinfo->localsyms)
4345 {
4346 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
b34976b6 4347 return TRUE;
252b5132
RH
4348 }
4349 else
4350 {
4351 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
b34976b6 4352 return TRUE;
252b5132
RH
4353 }
4354
560e09e9
NC
4355 /* If we have an undefined symbol reference here then it must have
4356 come from a shared library that is being linked in. (Undefined
4357 references in regular files have already been handled). If we
4358 are reporting errors for this situation then do so now. */
4359 if (h->root.type == bfd_link_hash_undefined
252b5132 4360 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
f5d44ba0 4361 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
560e09e9
NC
4362 && ! elf_link_check_versioned_symbol (finfo->info, h)
4363 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
252b5132
RH
4364 {
4365 if (! ((*finfo->info->callbacks->undefined_symbol)
4366 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
560e09e9 4367 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
252b5132 4368 {
b34976b6
AM
4369 eoinfo->failed = TRUE;
4370 return FALSE;
252b5132
RH
4371 }
4372 }
4373
1b1fe8fe
L
4374 /* We should also warn if a forced local symbol is referenced from
4375 shared libraries. */
1049f94e 4376 if (! finfo->info->relocatable
560e09e9 4377 && (! finfo->info->shared)
1b1fe8fe 4378 && (h->elf_link_hash_flags
560e09e9 4379 & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK))
71552942
L
4380 == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)
4381 && ! elf_link_check_versioned_symbol (finfo->info, h))
1b1fe8fe
L
4382 {
4383 (*_bfd_error_handler)
4384 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
4385 bfd_get_filename (finfo->output_bfd),
4386 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
4387 ? "internal"
4388 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
4389 ? "hidden" : "local",
4390 h->root.root.string,
4391 bfd_archive_filename (h->root.u.def.section->owner));
4392 eoinfo->failed = TRUE;
4393 return FALSE;
4394 }
4395
252b5132
RH
4396 /* We don't want to output symbols that have never been mentioned by
4397 a regular file, or that we have been told to strip. However, if
4398 h->indx is set to -2, the symbol is used by a reloc and we must
4399 output it. */
4400 if (h->indx == -2)
b34976b6 4401 strip = FALSE;
252b5132
RH
4402 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4403 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
4404 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4405 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
b34976b6 4406 strip = TRUE;
75828352
AM
4407 else if (finfo->info->strip == strip_all)
4408 strip = TRUE;
4409 else if (finfo->info->strip == strip_some
4410 && bfd_hash_lookup (finfo->info->keep_hash,
4411 h->root.root.string, FALSE, FALSE) == NULL)
4412 strip = TRUE;
4413 else if (finfo->info->strip_discarded
4414 && (h->root.type == bfd_link_hash_defined
4415 || h->root.type == bfd_link_hash_defweak)
4416 && elf_discarded_section (h->root.u.def.section))
b34976b6 4417 strip = TRUE;
252b5132 4418 else
b34976b6 4419 strip = FALSE;
252b5132
RH
4420
4421 /* If we're stripping it, and it's not a dynamic symbol, there's
2bd171e0
ILT
4422 nothing else to do unless it is a forced local symbol. */
4423 if (strip
4424 && h->dynindx == -1
4425 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
b34976b6 4426 return TRUE;
252b5132
RH
4427
4428 sym.st_value = 0;
4429 sym.st_size = h->size;
4430 sym.st_other = h->other;
4431 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4432 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
4433 else if (h->root.type == bfd_link_hash_undefweak
4434 || h->root.type == bfd_link_hash_defweak)
4435 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
4436 else
4437 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
4438
4439 switch (h->root.type)
4440 {
4441 default:
4442 case bfd_link_hash_new:
e92d460e 4443 case bfd_link_hash_warning:
252b5132 4444 abort ();
b34976b6 4445 return FALSE;
252b5132
RH
4446
4447 case bfd_link_hash_undefined:
252b5132
RH
4448 case bfd_link_hash_undefweak:
4449 input_sec = bfd_und_section_ptr;
4450 sym.st_shndx = SHN_UNDEF;
4451 break;
4452
4453 case bfd_link_hash_defined:
4454 case bfd_link_hash_defweak:
4455 {
4456 input_sec = h->root.u.def.section;
4457 if (input_sec->output_section != NULL)
4458 {
4459 sym.st_shndx =
4460 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
4461 input_sec->output_section);
9ad5cbcf 4462 if (sym.st_shndx == SHN_BAD)
252b5132
RH
4463 {
4464 (*_bfd_error_handler)
4465 (_("%s: could not find output section %s for input section %s"),
4466 bfd_get_filename (finfo->output_bfd),
4467 input_sec->output_section->name,
4468 input_sec->name);
b34976b6
AM
4469 eoinfo->failed = TRUE;
4470 return FALSE;
252b5132
RH
4471 }
4472
1049f94e
AM
4473 /* ELF symbols in relocatable files are section relative,
4474 but in nonrelocatable files they are virtual
252b5132
RH
4475 addresses. */
4476 sym.st_value = h->root.u.def.value + input_sec->output_offset;
1049f94e 4477 if (! finfo->info->relocatable)
13ae64f3
JJ
4478 {
4479 sym.st_value += input_sec->output_section->vma;
4480 if (h->type == STT_TLS)
4481 {
4482 /* STT_TLS symbols are relative to PT_TLS segment
4483 base. */
e1918d23
AM
4484 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
4485 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
13ae64f3
JJ
4486 }
4487 }
252b5132
RH
4488 }
4489 else
4490 {
4491 BFD_ASSERT (input_sec->owner == NULL
4492 || (input_sec->owner->flags & DYNAMIC) != 0);
4493 sym.st_shndx = SHN_UNDEF;
4494 input_sec = bfd_und_section_ptr;
4495 }
4496 }
4497 break;
4498
4499 case bfd_link_hash_common:
4500 input_sec = h->root.u.c.p->section;
4501 sym.st_shndx = SHN_COMMON;
4502 sym.st_value = 1 << h->root.u.c.p->alignment_power;
4503 break;
4504
4505 case bfd_link_hash_indirect:
4506 /* These symbols are created by symbol versioning. They point
c44233aa
AM
4507 to the decorated version of the name. For example, if the
4508 symbol foo@@GNU_1.2 is the default, which should be used when
4509 foo is used with no version, then we add an indirect symbol
4510 foo which points to foo@@GNU_1.2. We ignore these symbols,
4511 since the indirected symbol is already in the hash table. */
b34976b6 4512 return TRUE;
252b5132
RH
4513 }
4514
4515 /* Give the processor backend a chance to tweak the symbol value,
4516 and also to finish up anything that needs to be done for this
c44233aa
AM
4517 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
4518 forced local syms when non-shared is due to a historical quirk. */
252b5132
RH
4519 if ((h->dynindx != -1
4520 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
ef5aade5
L
4521 && ((finfo->info->shared
4522 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4523 || h->root.type != bfd_link_hash_undefweak))
c44233aa 4524 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
252b5132
RH
4525 && elf_hash_table (finfo->info)->dynamic_sections_created)
4526 {
9c5bfbb7 4527 const struct elf_backend_data *bed;
252b5132
RH
4528
4529 bed = get_elf_backend_data (finfo->output_bfd);
4530 if (! ((*bed->elf_backend_finish_dynamic_symbol)
4531 (finfo->output_bfd, finfo->info, h, &sym)))
4532 {
b34976b6
AM
4533 eoinfo->failed = TRUE;
4534 return FALSE;
252b5132
RH
4535 }
4536 }
4537
4538 /* If we are marking the symbol as undefined, and there are no
4539 non-weak references to this symbol from a regular object, then
91d3970e
ILT
4540 mark the symbol as weak undefined; if there are non-weak
4541 references, mark the symbol as strong. We can't do this earlier,
252b5132
RH
4542 because it might not be marked as undefined until the
4543 finish_dynamic_symbol routine gets through with it. */
4544 if (sym.st_shndx == SHN_UNDEF
252b5132 4545 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
a7b97311
AM
4546 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
4547 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
91d3970e
ILT
4548 {
4549 int bindtype;
4550
4551 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
4552 bindtype = STB_GLOBAL;
4553 else
4554 bindtype = STB_WEAK;
4555 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
4556 }
252b5132 4557
1b1fe8fe
L
4558 /* If a non-weak symbol with non-default visibility is not defined
4559 locally, it is a fatal error. */
1049f94e 4560 if (! finfo->info->relocatable
9c7a29a3 4561 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
1b1fe8fe 4562 && ELF_ST_BIND (sym.st_info) != STB_WEAK
22d5e339 4563 && h->root.type == bfd_link_hash_undefined
2cd533b7 4564 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1b1fe8fe
L
4565 {
4566 (*_bfd_error_handler)
4567 (_("%s: %s symbol `%s' isn't defined"),
4568 bfd_get_filename (finfo->output_bfd),
4569 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
4570 ? "protected"
4571 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
4572 ? "internal" : "hidden",
4573 h->root.root.string);
4574 eoinfo->failed = TRUE;
4575 return FALSE;
4576 }
32c092c3 4577
252b5132 4578 /* If this symbol should be put in the .dynsym section, then put it
f5d44ba0
AM
4579 there now. We already know the symbol index. We also fill in
4580 the entry in the .hash section. */
252b5132
RH
4581 if (h->dynindx != -1
4582 && elf_hash_table (finfo->info)->dynamic_sections_created)
4583 {
4584 size_t bucketcount;
4585 size_t bucket;
c7ac6ff8 4586 size_t hash_entry_size;
252b5132
RH
4587 bfd_byte *bucketpos;
4588 bfd_vma chain;
dc810e39 4589 Elf_External_Sym *esym;
252b5132
RH
4590
4591 sym.st_name = h->dynstr_index;
dc810e39 4592 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
268b6b39 4593 elf_swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
252b5132
RH
4594
4595 bucketcount = elf_hash_table (finfo->info)->bucketcount;
4596 bucket = h->elf_hash_value % bucketcount;
3e932841 4597 hash_entry_size
c7ac6ff8 4598 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
252b5132 4599 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
c7ac6ff8
MM
4600 + (bucket + 2) * hash_entry_size);
4601 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
268b6b39 4602 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
c7ac6ff8
MM
4603 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
4604 ((bfd_byte *) finfo->hash_sec->contents
4605 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
252b5132
RH
4606
4607 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
4608 {
4609 Elf_Internal_Versym iversym;
dc810e39 4610 Elf_External_Versym *eversym;
252b5132
RH
4611
4612 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4613 {
4614 if (h->verinfo.verdef == NULL)
4615 iversym.vs_vers = 0;
4616 else
4617 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
4618 }
4619 else
4620 {
4621 if (h->verinfo.vertree == NULL)
4622 iversym.vs_vers = 1;
4623 else
4624 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
4625 }
4626
4627 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
4628 iversym.vs_vers |= VERSYM_HIDDEN;
4629
dc810e39
AM
4630 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
4631 eversym += h->dynindx;
4632 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
252b5132
RH
4633 }
4634 }
4635
4636 /* If we're stripping it, then it was just a dynamic symbol, and
4637 there's nothing else to do. */
7330fb86 4638 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
b34976b6 4639 return TRUE;
252b5132
RH
4640
4641 h->indx = bfd_get_symcount (finfo->output_bfd);
4642
4643 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
4644 {
b34976b6
AM
4645 eoinfo->failed = TRUE;
4646 return FALSE;
252b5132
RH
4647 }
4648
b34976b6 4649 return TRUE;
252b5132
RH
4650}
4651
4652/* Link an input file into the linker output file. This function
4653 handles all the sections and relocations of the input file at once.
4654 This is so that we only have to read the local symbols once, and
4655 don't have to keep them in memory. */
4656
b34976b6 4657static bfd_boolean
268b6b39 4658elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
252b5132 4659{
b34976b6 4660 bfd_boolean (*relocate_section)
268b6b39
AM
4661 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
4662 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
252b5132
RH
4663 bfd *output_bfd;
4664 Elf_Internal_Shdr *symtab_hdr;
4665 size_t locsymcount;
4666 size_t extsymoff;
6cdc0ccc 4667 Elf_Internal_Sym *isymbuf;
252b5132 4668 Elf_Internal_Sym *isym;
6cdc0ccc 4669 Elf_Internal_Sym *isymend;
252b5132
RH
4670 long *pindex;
4671 asection **ppsection;
4672 asection *o;
9c5bfbb7 4673 const struct elf_backend_data *bed;
b34976b6 4674 bfd_boolean emit_relocs;
f8deed93 4675 struct elf_link_hash_entry **sym_hashes;
252b5132
RH
4676
4677 output_bfd = finfo->output_bfd;
c7ac6ff8
MM
4678 bed = get_elf_backend_data (output_bfd);
4679 relocate_section = bed->elf_backend_relocate_section;
252b5132
RH
4680
4681 /* If this is a dynamic object, we don't want to do anything here:
4682 we don't want the local symbols, and we don't want the section
4683 contents. */
4684 if ((input_bfd->flags & DYNAMIC) != 0)
b34976b6 4685 return TRUE;
252b5132 4686
1049f94e 4687 emit_relocs = (finfo->info->relocatable
c44233aa
AM
4688 || finfo->info->emitrelocations
4689 || bed->elf_backend_emit_relocs);
9317eacc 4690
252b5132
RH
4691 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4692 if (elf_bad_symtab (input_bfd))
4693 {
4694 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
4695 extsymoff = 0;
4696 }
4697 else
4698 {
4699 locsymcount = symtab_hdr->sh_info;
4700 extsymoff = symtab_hdr->sh_info;
4701 }
4702
4703 /* Read the local symbols. */
6cdc0ccc
AM
4704 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4705 if (isymbuf == NULL && locsymcount != 0)
4706 {
4707 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
4708 finfo->internal_syms,
4709 finfo->external_syms,
4710 finfo->locsym_shndx);
4711 if (isymbuf == NULL)
b34976b6 4712 return FALSE;
252b5132
RH
4713 }
4714
6cdc0ccc
AM
4715 /* Find local symbol sections and adjust values of symbols in
4716 SEC_MERGE sections. Write out those local symbols we know are
4717 going into the output file. */
4718 isymend = isymbuf + locsymcount;
4719 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
4720 isym < isymend;
4721 isym++, pindex++, ppsection++)
252b5132
RH
4722 {
4723 asection *isec;
4724 const char *name;
4725 Elf_Internal_Sym osym;
4726
252b5132
RH
4727 *pindex = -1;
4728
4729 if (elf_bad_symtab (input_bfd))
4730 {
4731 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
4732 {
4733 *ppsection = NULL;
4734 continue;
4735 }
4736 }
4737
4738 if (isym->st_shndx == SHN_UNDEF)
862517b6 4739 isec = bfd_und_section_ptr;
9ad5cbcf
AM
4740 else if (isym->st_shndx < SHN_LORESERVE
4741 || isym->st_shndx > SHN_HIRESERVE)
f5fa8ca2
JJ
4742 {
4743 isec = section_from_elf_index (input_bfd, isym->st_shndx);
65765700 4744 if (isec
68bfbfcc 4745 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
f5fa8ca2
JJ
4746 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
4747 isym->st_value =
4748 _bfd_merged_section_offset (output_bfd, &isec,
65765700 4749 elf_section_data (isec)->sec_info,
268b6b39 4750 isym->st_value, 0);
f5fa8ca2 4751 }
252b5132 4752 else if (isym->st_shndx == SHN_ABS)
862517b6 4753 isec = bfd_abs_section_ptr;
252b5132 4754 else if (isym->st_shndx == SHN_COMMON)
862517b6 4755 isec = bfd_com_section_ptr;
252b5132
RH
4756 else
4757 {
4758 /* Who knows? */
4759 isec = NULL;
4760 }
4761
4762 *ppsection = isec;
4763
4764 /* Don't output the first, undefined, symbol. */
6cdc0ccc 4765 if (ppsection == finfo->sections)
252b5132
RH
4766 continue;
4767
24376d1b
AM
4768 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4769 {
24376d1b
AM
4770 /* We never output section symbols. Instead, we use the
4771 section symbol of the corresponding section in the output
4772 file. */
4773 continue;
4774 }
4775
252b5132
RH
4776 /* If we are stripping all symbols, we don't want to output this
4777 one. */
4778 if (finfo->info->strip == strip_all)
4779 continue;
4780
252b5132 4781 /* If we are discarding all local symbols, we don't want to
1049f94e 4782 output this one. If we are generating a relocatable output
252b5132
RH
4783 file, then some of the local symbols may be required by
4784 relocs; we output them below as we discover that they are
4785 needed. */
4786 if (finfo->info->discard == discard_all)
4787 continue;
4788
4789 /* If this symbol is defined in a section which we are
c44233aa
AM
4790 discarding, we don't need to keep it, but note that
4791 linker_mark is only reliable for sections that have contents.
4792 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4793 as well as linker_mark. */
9ad5cbcf 4794 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
252b5132
RH
4795 && isec != NULL
4796 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
1049f94e 4797 || (! finfo->info->relocatable
252b5132
RH
4798 && (isec->flags & SEC_EXCLUDE) != 0)))
4799 continue;
4800
4801 /* Get the name of the symbol. */
4802 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
4803 isym->st_name);
4804 if (name == NULL)
b34976b6 4805 return FALSE;
252b5132
RH
4806
4807 /* See if we are discarding symbols with this name. */
4808 if ((finfo->info->strip == strip_some
b34976b6 4809 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
252b5132 4810 == NULL))
f5fa8ca2 4811 || (((finfo->info->discard == discard_sec_merge
1049f94e 4812 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
f5fa8ca2 4813 || finfo->info->discard == discard_l)
252b5132
RH
4814 && bfd_is_local_label_name (input_bfd, name)))
4815 continue;
4816
4817 /* If we get here, we are going to output this symbol. */
4818
4819 osym = *isym;
4820
4821 /* Adjust the section index for the output file. */
4822 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
4823 isec->output_section);
9ad5cbcf 4824 if (osym.st_shndx == SHN_BAD)
b34976b6 4825 return FALSE;
252b5132
RH
4826
4827 *pindex = bfd_get_symcount (output_bfd);
4828
1049f94e 4829 /* ELF symbols in relocatable files are section relative, but
252b5132
RH
4830 in executable files they are virtual addresses. Note that
4831 this code assumes that all ELF sections have an associated
4832 BFD section with a reasonable value for output_offset; below
4833 we assume that they also have a reasonable value for
4834 output_section. Any special sections must be set up to meet
4835 these requirements. */
4836 osym.st_value += isec->output_offset;
1049f94e 4837 if (! finfo->info->relocatable)
13ae64f3
JJ
4838 {
4839 osym.st_value += isec->output_section->vma;
4840 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
4841 {
4842 /* STT_TLS symbols are relative to PT_TLS segment base. */
e1918d23
AM
4843 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
4844 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
13ae64f3
JJ
4845 }
4846 }
252b5132
RH
4847
4848 if (! elf_link_output_sym (finfo, name, &osym, isec))
b34976b6 4849 return FALSE;
252b5132
RH
4850 }
4851
4852 /* Relocate the contents of each section. */
f8deed93 4853 sym_hashes = elf_sym_hashes (input_bfd);
252b5132
RH
4854 for (o = input_bfd->sections; o != NULL; o = o->next)
4855 {
4856 bfd_byte *contents;
4857
4858 if (! o->linker_mark)
4859 {
4860 /* This section was omitted from the link. */
4861 continue;
4862 }
4863
4864 if ((o->flags & SEC_HAS_CONTENTS) == 0
4865 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
4866 continue;
4867
4868 if ((o->flags & SEC_LINKER_CREATED) != 0)
4869 {
45d6a902 4870 /* Section was created by _bfd_elf_link_create_dynamic_sections
252b5132
RH
4871 or somesuch. */
4872 continue;
4873 }
4874
4875 /* Get the contents of the section. They have been cached by a
c44233aa
AM
4876 relaxation routine. Note that o is a section in an input
4877 file, so the contents field will not have been set by any of
4878 the routines which work on output files. */
252b5132
RH
4879 if (elf_section_data (o)->this_hdr.contents != NULL)
4880 contents = elf_section_data (o)->this_hdr.contents;
4881 else
4882 {
4883 contents = finfo->contents;
268b6b39
AM
4884 if (! bfd_get_section_contents (input_bfd, o, contents, 0,
4885 o->_raw_size))
b34976b6 4886 return FALSE;
252b5132
RH
4887 }
4888
4889 if ((o->flags & SEC_RELOC) != 0)
4890 {
4891 Elf_Internal_Rela *internal_relocs;
4892
4893 /* Get the swapped relocs. */
45d6a902
AM
4894 internal_relocs
4895 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
4896 finfo->internal_relocs, FALSE);
252b5132
RH
4897 if (internal_relocs == NULL
4898 && o->reloc_count > 0)
b34976b6 4899 return FALSE;
252b5132 4900
ec338859
AM
4901 /* Run through the relocs looking for any against symbols
4902 from discarded sections and section symbols from
4903 removed link-once sections. Complain about relocs
4904 against discarded sections. Zero relocs against removed
f97b9cb8
L
4905 link-once sections. Preserve debug information as much
4906 as we can. */
4907 if (!elf_section_ignore_discarded_relocs (o))
ec338859
AM
4908 {
4909 Elf_Internal_Rela *rel, *relend;
50b4d486 4910
ec338859
AM
4911 rel = internal_relocs;
4912 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
4913 for ( ; rel < relend; rel++)
4914 {
4915 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
f97b9cb8 4916 asection *sec;
ec338859
AM
4917
4918 if (r_symndx >= locsymcount
4919 || (elf_bad_symtab (input_bfd)
4920 && finfo->sections[r_symndx] == NULL))
4921 {
4922 struct elf_link_hash_entry *h;
4923
4924 h = sym_hashes[r_symndx - extsymoff];
4925 while (h->root.type == bfd_link_hash_indirect
4926 || h->root.type == bfd_link_hash_warning)
4927 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4928
4929 /* Complain if the definition comes from a
4930 discarded section. */
f97b9cb8 4931 sec = h->root.u.def.section;
ec338859
AM
4932 if ((h->root.type == bfd_link_hash_defined
4933 || h->root.type == bfd_link_hash_defweak)
f97b9cb8 4934 && elf_discarded_section (sec))
ec338859 4935 {
ec338859
AM
4936 if ((o->flags & SEC_DEBUGGING) != 0)
4937 {
45e9217a 4938 BFD_ASSERT (r_symndx != 0);
f97b9cb8
L
4939 /* Try to preserve debug information. */
4940 if ((o->flags & SEC_DEBUGGING) != 0
4941 && sec->kept_section != NULL
6e35c4da 4942 && sec->_raw_size == sec->kept_section->_raw_size)
f97b9cb8
L
4943 h->root.u.def.section
4944 = sec->kept_section;
4945 else
4946 memset (rel, 0, sizeof (*rel));
ec338859
AM
4947 }
4948 else
6f6f27f8
L
4949 finfo->info->callbacks->error_handler
4950 (LD_DEFINITION_IN_DISCARDED_SECTION,
4951 _("%T: discarded in section `%s' from %s\n"),
4952 h->root.root.string,
4953 h->root.root.string,
4954 h->root.u.def.section->name,
4955 bfd_archive_filename (h->root.u.def.section->owner));
ec338859
AM
4956 }
4957 }
4958 else
4959 {
f97b9cb8 4960 sec = finfo->sections[r_symndx];
50b4d486 4961
ed4de5e2 4962 if (sec != NULL && elf_discarded_section (sec))
f9f32305 4963 {
ad43ed4c
L
4964 if ((o->flags & SEC_DEBUGGING) != 0
4965 || (sec->flags & SEC_LINK_ONCE) != 0)
f9f32305 4966 {
45e9217a 4967 BFD_ASSERT (r_symndx != 0);
f97b9cb8
L
4968 /* Try to preserve debug information. */
4969 if ((o->flags & SEC_DEBUGGING) != 0
4970 && sec->kept_section != NULL
6e35c4da 4971 && sec->_raw_size == sec->kept_section->_raw_size)
f97b9cb8
L
4972 finfo->sections[r_symndx]
4973 = sec->kept_section;
4974 else
4975 {
4976 rel->r_info
4977 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
4978 rel->r_addend = 0;
4979 }
f9f32305
AM
4980 }
4981 else
f9f32305 4982 {
6f6f27f8
L
4983 static int count;
4984 int ok;
4985 char *buf;
4986
4987 ok = asprintf (&buf, "local symbol %d",
4988 count++);
4989 if (ok <= 0)
4990 buf = (char *) "local symbol";
4991 finfo->info->callbacks->error_handler
4992 (LD_DEFINITION_IN_DISCARDED_SECTION,
4993 _("%T: discarded in section `%s' from %s\n"),
4994 buf, buf, sec->name,
4995 bfd_archive_filename (input_bfd));
4996 if (ok != -1)
f9f32305 4997 free (buf);
ec338859
AM
4998 }
4999 }
5000 }
5001 }
5002 }
50b4d486 5003
252b5132
RH
5004 /* Relocate the section by invoking a back end routine.
5005
5006 The back end routine is responsible for adjusting the
5007 section contents as necessary, and (if using Rela relocs
1049f94e 5008 and generating a relocatable output file) adjusting the
252b5132
RH
5009 reloc addend as necessary.
5010
5011 The back end routine does not have to worry about setting
5012 the reloc address or the reloc symbol index.
5013
5014 The back end routine is given a pointer to the swapped in
5015 internal symbols, and can access the hash table entries
5016 for the external symbols via elf_sym_hashes (input_bfd).
5017
1049f94e 5018 When generating relocatable output, the back end routine
252b5132
RH
5019 must handle STB_LOCAL/STT_SECTION symbols specially. The
5020 output symbol is going to be a section symbol
5021 corresponding to the output section, which will require
5022 the addend to be adjusted. */
5023
5024 if (! (*relocate_section) (output_bfd, finfo->info,
5025 input_bfd, o, contents,
5026 internal_relocs,
6cdc0ccc 5027 isymbuf,
252b5132 5028 finfo->sections))
b34976b6 5029 return FALSE;
252b5132 5030
9317eacc 5031 if (emit_relocs)
252b5132
RH
5032 {
5033 Elf_Internal_Rela *irela;
5034 Elf_Internal_Rela *irelaend;
73722af0 5035 bfd_vma last_offset;
252b5132 5036 struct elf_link_hash_entry **rel_hash;
c89583f8 5037 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
4e8a9624 5038 unsigned int next_erel;
b34976b6 5039 bfd_boolean (*reloc_emitter)
268b6b39 5040 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
b34976b6 5041 bfd_boolean rela_normal;
b491616a
AM
5042
5043 input_rel_hdr = &elf_section_data (o)->rel_hdr;
5044 rela_normal = (bed->rela_normal
5045 && (input_rel_hdr->sh_entsize
5046 == sizeof (Elf_External_Rela)));
252b5132
RH
5047
5048 /* Adjust the reloc addresses and symbol indices. */
5049
5050 irela = internal_relocs;
dc810e39 5051 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132 5052 rel_hash = (elf_section_data (o->output_section)->rel_hashes
31367b81
MM
5053 + elf_section_data (o->output_section)->rel_count
5054 + elf_section_data (o->output_section)->rel_count2);
73722af0 5055 last_offset = o->output_offset;
1049f94e 5056 if (!finfo->info->relocatable)
73722af0 5057 last_offset += o->output_section->vma;
209f668e 5058 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
252b5132
RH
5059 {
5060 unsigned long r_symndx;
252b5132 5061 asection *sec;
fad2542d 5062 Elf_Internal_Sym sym;
252b5132 5063
209f668e
NC
5064 if (next_erel == bed->s->int_rels_per_ext_rel)
5065 {
5066 rel_hash++;
5067 next_erel = 0;
5068 }
5069
d6fe2dc1
AM
5070 irela->r_offset = _bfd_elf_section_offset (output_bfd,
5071 finfo->info, o,
5072 irela->r_offset);
5073 if (irela->r_offset >= (bfd_vma) -2)
5074 {
73722af0
AM
5075 /* This is a reloc for a deleted entry or somesuch.
5076 Turn it into an R_*_NONE reloc, at the same
5077 offset as the last reloc. elf_eh_frame.c and
5078 elf_bfd_discard_info rely on reloc offsets
b34976b6 5079 being ordered. */
73722af0
AM
5080 irela->r_offset = last_offset;
5081 irela->r_info = 0;
5082 irela->r_addend = 0;
d6fe2dc1
AM
5083 continue;
5084 }
5085
252b5132
RH
5086 irela->r_offset += o->output_offset;
5087
7ad34365 5088 /* Relocs in an executable have to be virtual addresses. */
1049f94e 5089 if (!finfo->info->relocatable)
7ad34365
NC
5090 irela->r_offset += o->output_section->vma;
5091
73722af0 5092 last_offset = irela->r_offset;
252b5132 5093
73722af0
AM
5094 r_symndx = ELF_R_SYM (irela->r_info);
5095 if (r_symndx == STN_UNDEF)
252b5132
RH
5096 continue;
5097
5098 if (r_symndx >= locsymcount
5099 || (elf_bad_symtab (input_bfd)
5100 && finfo->sections[r_symndx] == NULL))
5101 {
5102 struct elf_link_hash_entry *rh;
209f668e 5103 unsigned long indx;
252b5132
RH
5104
5105 /* This is a reloc against a global symbol. We
5106 have not yet output all the local symbols, so
5107 we do not know the symbol index of any global
5108 symbol. We set the rel_hash entry for this
5109 reloc to point to the global hash table entry
5110 for this symbol. The symbol index is then
5111 set at the end of elf_bfd_final_link. */
5112 indx = r_symndx - extsymoff;
5113 rh = elf_sym_hashes (input_bfd)[indx];
5114 while (rh->root.type == bfd_link_hash_indirect
5115 || rh->root.type == bfd_link_hash_warning)
5116 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
5117
5118 /* Setting the index to -2 tells
5119 elf_link_output_extsym that this symbol is
5120 used by a reloc. */
5121 BFD_ASSERT (rh->indx < 0);
5122 rh->indx = -2;
5123
5124 *rel_hash = rh;
5125
5126 continue;
5127 }
5128
3e932841 5129 /* This is a reloc against a local symbol. */
252b5132
RH
5130
5131 *rel_hash = NULL;
fad2542d 5132 sym = isymbuf[r_symndx];
252b5132 5133 sec = finfo->sections[r_symndx];
fad2542d 5134 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
252b5132
RH
5135 {
5136 /* I suppose the backend ought to fill in the
5137 section of any STT_SECTION symbol against a
5138 processor specific section. If we have
5139 discarded a section, the output_section will
5140 be the absolute section. */
b491616a
AM
5141 if (bfd_is_abs_section (sec)
5142 || (sec != NULL
5143 && bfd_is_abs_section (sec->output_section)))
252b5132
RH
5144 r_symndx = 0;
5145 else if (sec == NULL || sec->owner == NULL)
5146 {
5147 bfd_set_error (bfd_error_bad_value);
b34976b6 5148 return FALSE;
252b5132
RH
5149 }
5150 else
5151 {
5152 r_symndx = sec->output_section->target_index;
5153 BFD_ASSERT (r_symndx != 0);
5154 }
b491616a
AM
5155
5156 /* Adjust the addend according to where the
f5d44ba0 5157 section winds up in the output section. */
b491616a
AM
5158 if (rela_normal)
5159 irela->r_addend += sec->output_offset;
252b5132
RH
5160 }
5161 else
5162 {
5163 if (finfo->indices[r_symndx] == -1)
5164 {
dc810e39 5165 unsigned long shlink;
252b5132
RH
5166 const char *name;
5167 asection *osec;
5168
5169 if (finfo->info->strip == strip_all)
5170 {
5171 /* You can't do ld -r -s. */
5172 bfd_set_error (bfd_error_invalid_operation);
b34976b6 5173 return FALSE;
252b5132
RH
5174 }
5175
5176 /* This symbol was skipped earlier, but
5177 since it is needed by a reloc, we
5178 must output it now. */
dc810e39 5179 shlink = symtab_hdr->sh_link;
a7b97311 5180 name = (bfd_elf_string_from_elf_section
fad2542d 5181 (input_bfd, shlink, sym.st_name));
252b5132 5182 if (name == NULL)
b34976b6 5183 return FALSE;
252b5132
RH
5184
5185 osec = sec->output_section;
fad2542d 5186 sym.st_shndx =
252b5132
RH
5187 _bfd_elf_section_from_bfd_section (output_bfd,
5188 osec);
fad2542d 5189 if (sym.st_shndx == SHN_BAD)
b34976b6 5190 return FALSE;
252b5132 5191
fad2542d 5192 sym.st_value += sec->output_offset;
1049f94e 5193 if (! finfo->info->relocatable)
13ae64f3 5194 {
fad2542d
AM
5195 sym.st_value += osec->vma;
5196 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
13ae64f3
JJ
5197 {
5198 /* STT_TLS symbols are relative to PT_TLS
5199 segment base. */
e1918d23
AM
5200 BFD_ASSERT (elf_hash_table (finfo->info)
5201 ->tls_sec != NULL);
5202 sym.st_value -= (elf_hash_table (finfo->info)
5203 ->tls_sec->vma);
13ae64f3
JJ
5204 }
5205 }
252b5132 5206
a7b97311
AM
5207 finfo->indices[r_symndx]
5208 = bfd_get_symcount (output_bfd);
252b5132 5209
fad2542d 5210 if (! elf_link_output_sym (finfo, name, &sym, sec))
b34976b6 5211 return FALSE;
252b5132
RH
5212 }
5213
5214 r_symndx = finfo->indices[r_symndx];
5215 }
5216
5217 irela->r_info = ELF_R_INFO (r_symndx,
5218 ELF_R_TYPE (irela->r_info));
5219 }
5220
5221 /* Swap out the relocs. */
c44233aa 5222 if (bed->elf_backend_emit_relocs
1049f94e 5223 && !(finfo->info->relocatable
a7b97311 5224 || finfo->info->emitrelocations))
c44233aa
AM
5225 reloc_emitter = bed->elf_backend_emit_relocs;
5226 else
45d6a902 5227 reloc_emitter = _bfd_elf_link_output_relocs;
9317eacc 5228
c89583f8
AM
5229 if (input_rel_hdr->sh_size != 0
5230 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
5231 internal_relocs))
b34976b6 5232 return FALSE;
9317eacc 5233
c89583f8
AM
5234 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
5235 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
c44233aa
AM
5236 {
5237 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
dc810e39 5238 * bed->s->int_rels_per_ext_rel);
c89583f8 5239 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
41241523 5240 internal_relocs))
b34976b6 5241 return FALSE;
c44233aa 5242 }
252b5132
RH
5243 }
5244 }
5245
5246 /* Write out the modified section contents. */
73d074b4 5247 if (bed->elf_backend_write_section
f9f32305 5248 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
73d074b4
DJ
5249 {
5250 /* Section written out. */
5251 }
68bfbfcc 5252 else switch (o->sec_info_type)
f5fa8ca2 5253 {
65765700 5254 case ELF_INFO_TYPE_STABS:
f5fa8ca2 5255 if (! (_bfd_write_section_stabs
65765700
JJ
5256 (output_bfd,
5257 &elf_hash_table (finfo->info)->stab_info,
5258 o, &elf_section_data (o)->sec_info, contents)))
b34976b6 5259 return FALSE;
65765700
JJ
5260 break;
5261 case ELF_INFO_TYPE_MERGE:
126495ed
AM
5262 if (! _bfd_write_merged_section (output_bfd, o,
5263 elf_section_data (o)->sec_info))
b34976b6 5264 return FALSE;
65765700
JJ
5265 break;
5266 case ELF_INFO_TYPE_EH_FRAME:
5267 {
126495ed
AM
5268 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
5269 o, contents))
b34976b6 5270 return FALSE;
65765700
JJ
5271 }
5272 break;
5273 default:
5274 {
5275 bfd_size_type sec_size;
5276
5277 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
5278 if (! (o->flags & SEC_EXCLUDE)
5279 && ! bfd_set_section_contents (output_bfd, o->output_section,
5280 contents,
5281 (file_ptr) o->output_offset,
5282 sec_size))
b34976b6 5283 return FALSE;
65765700
JJ
5284 }
5285 break;
252b5132
RH
5286 }
5287 }
5288
b34976b6 5289 return TRUE;
252b5132
RH
5290}
5291
5292/* Generate a reloc when linking an ELF file. This is a reloc
5293 requested by the linker, and does come from any input file. This
5294 is used to build constructor and destructor tables when linking
5295 with -Ur. */
5296
b34976b6 5297static bfd_boolean
268b6b39
AM
5298elf_reloc_link_order (bfd *output_bfd,
5299 struct bfd_link_info *info,
5300 asection *output_section,
5301 struct bfd_link_order *link_order)
252b5132
RH
5302{
5303 reloc_howto_type *howto;
5304 long indx;
5305 bfd_vma offset;
5306 bfd_vma addend;
5307 struct elf_link_hash_entry **rel_hash_ptr;
5308 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 5309 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
947216bf
AM
5310 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
5311 bfd_byte *erel;
5312 unsigned int i;
252b5132
RH
5313
5314 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5315 if (howto == NULL)
5316 {
5317 bfd_set_error (bfd_error_bad_value);
b34976b6 5318 return FALSE;
252b5132
RH
5319 }
5320
5321 addend = link_order->u.reloc.p->addend;
5322
5323 /* Figure out the symbol index. */
5324 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
31367b81
MM
5325 + elf_section_data (output_section)->rel_count
5326 + elf_section_data (output_section)->rel_count2);
252b5132
RH
5327 if (link_order->type == bfd_section_reloc_link_order)
5328 {
5329 indx = link_order->u.reloc.p->u.section->target_index;
5330 BFD_ASSERT (indx != 0);
5331 *rel_hash_ptr = NULL;
5332 }
5333 else
5334 {
5335 struct elf_link_hash_entry *h;
5336
5337 /* Treat a reloc against a defined symbol as though it were
c44233aa 5338 actually against the section. */
252b5132
RH
5339 h = ((struct elf_link_hash_entry *)
5340 bfd_wrapped_link_hash_lookup (output_bfd, info,
5341 link_order->u.reloc.p->u.name,
b34976b6 5342 FALSE, FALSE, TRUE));
252b5132
RH
5343 if (h != NULL
5344 && (h->root.type == bfd_link_hash_defined
5345 || h->root.type == bfd_link_hash_defweak))
5346 {
5347 asection *section;
5348
5349 section = h->root.u.def.section;
5350 indx = section->output_section->target_index;
5351 *rel_hash_ptr = NULL;
5352 /* It seems that we ought to add the symbol value to the
c44233aa
AM
5353 addend here, but in practice it has already been added
5354 because it was passed to constructor_callback. */
252b5132
RH
5355 addend += section->output_section->vma + section->output_offset;
5356 }
5357 else if (h != NULL)
5358 {
5359 /* Setting the index to -2 tells elf_link_output_extsym that
5360 this symbol is used by a reloc. */
5361 h->indx = -2;
5362 *rel_hash_ptr = h;
5363 indx = 0;
5364 }
5365 else
5366 {
5367 if (! ((*info->callbacks->unattached_reloc)
268b6b39 5368 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
b34976b6 5369 return FALSE;
252b5132
RH
5370 indx = 0;
5371 }
5372 }
5373
5374 /* If this is an inplace reloc, we must write the addend into the
5375 object file. */
5376 if (howto->partial_inplace && addend != 0)
5377 {
5378 bfd_size_type size;
5379 bfd_reloc_status_type rstat;
5380 bfd_byte *buf;
b34976b6 5381 bfd_boolean ok;
dc810e39 5382 const char *sym_name;
252b5132
RH
5383
5384 size = bfd_get_reloc_size (howto);
268b6b39
AM
5385 buf = bfd_zmalloc (size);
5386 if (buf == NULL)
b34976b6 5387 return FALSE;
268b6b39 5388 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
252b5132
RH
5389 switch (rstat)
5390 {
5391 case bfd_reloc_ok:
5392 break;
dc810e39 5393
252b5132
RH
5394 default:
5395 case bfd_reloc_outofrange:
5396 abort ();
dc810e39 5397
252b5132 5398 case bfd_reloc_overflow:
dc810e39
AM
5399 if (link_order->type == bfd_section_reloc_link_order)
5400 sym_name = bfd_section_name (output_bfd,
5401 link_order->u.reloc.p->u.section);
5402 else
5403 sym_name = link_order->u.reloc.p->u.name;
252b5132 5404 if (! ((*info->callbacks->reloc_overflow)
268b6b39 5405 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
252b5132
RH
5406 {
5407 free (buf);
b34976b6 5408 return FALSE;
252b5132
RH
5409 }
5410 break;
5411 }
268b6b39
AM
5412 ok = bfd_set_section_contents (output_bfd, output_section, buf,
5413 link_order->offset, size);
252b5132
RH
5414 free (buf);
5415 if (! ok)
b34976b6 5416 return FALSE;
252b5132
RH
5417 }
5418
5419 /* The address of a reloc is relative to the section in a
1049f94e 5420 relocatable file, and is a virtual address in an executable
252b5132
RH
5421 file. */
5422 offset = link_order->offset;
1049f94e 5423 if (! info->relocatable)
252b5132
RH
5424 offset += output_section->vma;
5425
947216bf
AM
5426 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
5427 {
5428 irel[i].r_offset = offset;
5429 irel[i].r_info = 0;
5430 irel[i].r_addend = 0;
5431 }
5432 irel[0].r_info = ELF_R_INFO (indx, howto->type);
252b5132 5433
947216bf
AM
5434 rel_hdr = &elf_section_data (output_section)->rel_hdr;
5435 erel = rel_hdr->contents;
252b5132
RH
5436 if (rel_hdr->sh_type == SHT_REL)
5437 {
947216bf
AM
5438 erel += (elf_section_data (output_section)->rel_count
5439 * sizeof (Elf_External_Rel));
5440 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
252b5132
RH
5441 }
5442 else
5443 {
947216bf
AM
5444 irel[0].r_addend = addend;
5445 erel += (elf_section_data (output_section)->rel_count
5446 * sizeof (Elf_External_Rela));
5447 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
252b5132
RH
5448 }
5449
0525d26e 5450 ++elf_section_data (output_section)->rel_count;
252b5132 5451
b34976b6 5452 return TRUE;
252b5132 5453}
252b5132 5454\f
252b5132
RH
5455/* Garbage collect unused sections. */
5456
b34976b6 5457static bfd_boolean elf_gc_sweep_symbol
268b6b39 5458 (struct elf_link_hash_entry *, void *);
252b5132 5459
b34976b6 5460static bfd_boolean elf_gc_allocate_got_offsets
268b6b39 5461 (struct elf_link_hash_entry *, void *);
252b5132
RH
5462
5463/* The mark phase of garbage collection. For a given section, mark
dbb410c3
AM
5464 it and any sections in this section's group, and all the sections
5465 which define symbols to which it refers. */
252b5132 5466
b34976b6 5467typedef asection * (*gc_mark_hook_fn)
268b6b39
AM
5468 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5469 struct elf_link_hash_entry *, Elf_Internal_Sym *);
b34976b6
AM
5470
5471static bfd_boolean
268b6b39
AM
5472elf_gc_mark (struct bfd_link_info *info,
5473 asection *sec,
5474 gc_mark_hook_fn gc_mark_hook)
252b5132 5475{
b34976b6 5476 bfd_boolean ret;
dbb410c3 5477 asection *group_sec;
252b5132
RH
5478
5479 sec->gc_mark = 1;
5480
dbb410c3
AM
5481 /* Mark all the sections in the group. */
5482 group_sec = elf_section_data (sec)->next_in_group;
5483 if (group_sec && !group_sec->gc_mark)
5484 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
b34976b6 5485 return FALSE;
252b5132 5486
dbb410c3 5487 /* Look through the section relocs. */
b34976b6 5488 ret = TRUE;
252b5132
RH
5489 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
5490 {
5491 Elf_Internal_Rela *relstart, *rel, *relend;
5492 Elf_Internal_Shdr *symtab_hdr;
5493 struct elf_link_hash_entry **sym_hashes;
5494 size_t nlocsyms;
5495 size_t extsymoff;
252b5132 5496 bfd *input_bfd = sec->owner;
9c5bfbb7 5497 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
6cdc0ccc 5498 Elf_Internal_Sym *isym = NULL;
252b5132
RH
5499
5500 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5501 sym_hashes = elf_sym_hashes (input_bfd);
5502
5503 /* Read the local symbols. */
5504 if (elf_bad_symtab (input_bfd))
5505 {
5506 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
c44233aa 5507 extsymoff = 0;
252b5132
RH
5508 }
5509 else
5510 extsymoff = nlocsyms = symtab_hdr->sh_info;
9ad5cbcf 5511
6cdc0ccc
AM
5512 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
5513 if (isym == NULL && nlocsyms != 0)
9ad5cbcf 5514 {
6cdc0ccc
AM
5515 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
5516 NULL, NULL, NULL);
5517 if (isym == NULL)
b34976b6 5518 return FALSE;
9ad5cbcf
AM
5519 }
5520
252b5132 5521 /* Read the relocations. */
268b6b39 5522 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
45d6a902 5523 info->keep_memory);
252b5132
RH
5524 if (relstart == NULL)
5525 {
b34976b6 5526 ret = FALSE;
252b5132
RH
5527 goto out1;
5528 }
c7ac6ff8 5529 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132
RH
5530
5531 for (rel = relstart; rel < relend; rel++)
5532 {
5533 unsigned long r_symndx;
5534 asection *rsec;
5535 struct elf_link_hash_entry *h;
252b5132
RH
5536
5537 r_symndx = ELF_R_SYM (rel->r_info);
5538 if (r_symndx == 0)
5539 continue;
5540
6cdc0ccc
AM
5541 if (r_symndx >= nlocsyms
5542 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
252b5132
RH
5543 {
5544 h = sym_hashes[r_symndx - extsymoff];
1e2f5b6e 5545 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
252b5132
RH
5546 }
5547 else
5548 {
6cdc0ccc 5549 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
252b5132
RH
5550 }
5551
5552 if (rsec && !rsec->gc_mark)
b91afed7
AM
5553 {
5554 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
5555 rsec->gc_mark = 1;
5556 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
5557 {
b34976b6 5558 ret = FALSE;
b91afed7
AM
5559 goto out2;
5560 }
5561 }
252b5132
RH
5562 }
5563
5564 out2:
6cdc0ccc 5565 if (elf_section_data (sec)->relocs != relstart)
252b5132
RH
5566 free (relstart);
5567 out1:
6cdc0ccc
AM
5568 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
5569 {
5570 if (! info->keep_memory)
5571 free (isym);
5572 else
5573 symtab_hdr->contents = (unsigned char *) isym;
5574 }
252b5132
RH
5575 }
5576
5577 return ret;
5578}
5579
5580/* The sweep phase of garbage collection. Remove all garbage sections. */
5581
b34976b6 5582typedef bfd_boolean (*gc_sweep_hook_fn)
268b6b39 5583 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
b34976b6
AM
5584
5585static bfd_boolean
268b6b39 5586elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
252b5132
RH
5587{
5588 bfd *sub;
5589
5590 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5591 {
5592 asection *o;
5593
f6af82bd
AM
5594 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5595 continue;
5596
252b5132
RH
5597 for (o = sub->sections; o != NULL; o = o->next)
5598 {
5599 /* Keep special sections. Keep .debug sections. */
5600 if ((o->flags & SEC_LINKER_CREATED)
5601 || (o->flags & SEC_DEBUGGING))
5602 o->gc_mark = 1;
5603
5604 if (o->gc_mark)
5605 continue;
5606
5607 /* Skip sweeping sections already excluded. */
5608 if (o->flags & SEC_EXCLUDE)
5609 continue;
5610
5611 /* Since this is early in the link process, it is simple
5612 to remove a section from the output. */
5613 o->flags |= SEC_EXCLUDE;
5614
5615 /* But we also have to update some of the relocation
5616 info we collected before. */
5617 if (gc_sweep_hook
5618 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
5619 {
5620 Elf_Internal_Rela *internal_relocs;
b34976b6 5621 bfd_boolean r;
252b5132 5622
45d6a902 5623 internal_relocs
268b6b39 5624 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
45d6a902 5625 info->keep_memory);
252b5132 5626 if (internal_relocs == NULL)
b34976b6 5627 return FALSE;
252b5132 5628
3e932841 5629 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
252b5132 5630
6cdc0ccc 5631 if (elf_section_data (o)->relocs != internal_relocs)
252b5132
RH
5632 free (internal_relocs);
5633
5634 if (!r)
b34976b6 5635 return FALSE;
252b5132
RH
5636 }
5637 }
5638 }
5639
5640 /* Remove the symbols that were in the swept sections from the dynamic
5641 symbol table. GCFIXME: Anyone know how to get them out of the
5642 static symbol table as well? */
5643 {
5644 int i = 0;
5645
268b6b39 5646 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
252b5132
RH
5647
5648 elf_hash_table (info)->dynsymcount = i;
5649 }
5650
b34976b6 5651 return TRUE;
252b5132
RH
5652}
5653
5654/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5655
b34976b6 5656static bfd_boolean
268b6b39 5657elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
252b5132 5658{
268b6b39 5659 int *idx = idxptr;
252b5132 5660
e92d460e
AM
5661 if (h->root.type == bfd_link_hash_warning)
5662 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5663
252b5132
RH
5664 if (h->dynindx != -1
5665 && ((h->root.type != bfd_link_hash_defined
5666 && h->root.type != bfd_link_hash_defweak)
5667 || h->root.u.def.section->gc_mark))
5668 h->dynindx = (*idx)++;
5669
b34976b6 5670 return TRUE;
252b5132
RH
5671}
5672
4cc11e76 5673/* Propagate collected vtable information. This is called through
252b5132
RH
5674 elf_link_hash_traverse. */
5675
b34976b6 5676static bfd_boolean
268b6b39 5677elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
252b5132 5678{
e92d460e
AM
5679 if (h->root.type == bfd_link_hash_warning)
5680 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5681
3e932841 5682 /* Those that are not vtables. */
252b5132 5683 if (h->vtable_parent == NULL)
b34976b6 5684 return TRUE;
252b5132
RH
5685
5686 /* Those vtables that do not have parents, we cannot merge. */
5687 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
b34976b6 5688 return TRUE;
252b5132
RH
5689
5690 /* If we've already been done, exit. */
5691 if (h->vtable_entries_used && h->vtable_entries_used[-1])
b34976b6 5692 return TRUE;
252b5132
RH
5693
5694 /* Make sure the parent's table is up to date. */
5695 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
5696
5697 if (h->vtable_entries_used == NULL)
5698 {
5699 /* None of this table's entries were referenced. Re-use the
5700 parent's table. */
5701 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
5702 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
5703 }
5704 else
5705 {
5706 size_t n;
b34976b6 5707 bfd_boolean *cu, *pu;
252b5132
RH
5708
5709 /* Or the parent's entries into ours. */
5710 cu = h->vtable_entries_used;
b34976b6 5711 cu[-1] = TRUE;
252b5132
RH
5712 pu = h->vtable_parent->vtable_entries_used;
5713 if (pu != NULL)
5714 {
9c5bfbb7
AM
5715 const struct elf_backend_data *bed;
5716 unsigned int log_file_align;
0d1ea5c0 5717
9c5bfbb7
AM
5718 bed = get_elf_backend_data (h->root.u.def.section->owner);
5719 log_file_align = bed->s->log_file_align;
45d6a902 5720 n = h->vtable_parent->vtable_entries_size >> log_file_align;
374b596d 5721 while (n--)
252b5132 5722 {
374b596d 5723 if (*pu)
b34976b6 5724 *cu = TRUE;
374b596d
NC
5725 pu++;
5726 cu++;
252b5132
RH
5727 }
5728 }
5729 }
5730
b34976b6 5731 return TRUE;
252b5132
RH
5732}
5733
b34976b6 5734static bfd_boolean
268b6b39 5735elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
252b5132
RH
5736{
5737 asection *sec;
5738 bfd_vma hstart, hend;
5739 Elf_Internal_Rela *relstart, *relend, *rel;
9c5bfbb7 5740 const struct elf_backend_data *bed;
45d6a902 5741 unsigned int log_file_align;
252b5132 5742
e92d460e
AM
5743 if (h->root.type == bfd_link_hash_warning)
5744 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5745
252b5132
RH
5746 /* Take care of both those symbols that do not describe vtables as
5747 well as those that are not loaded. */
5748 if (h->vtable_parent == NULL)
b34976b6 5749 return TRUE;
252b5132
RH
5750
5751 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5752 || h->root.type == bfd_link_hash_defweak);
5753
5754 sec = h->root.u.def.section;
5755 hstart = h->root.u.def.value;
5756 hend = hstart + h->size;
5757
268b6b39 5758 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
252b5132 5759 if (!relstart)
b34976b6 5760 return *(bfd_boolean *) okp = FALSE;
c7ac6ff8 5761 bed = get_elf_backend_data (sec->owner);
45d6a902 5762 log_file_align = bed->s->log_file_align;
0d1ea5c0 5763
c7ac6ff8 5764 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132
RH
5765
5766 for (rel = relstart; rel < relend; ++rel)
5767 if (rel->r_offset >= hstart && rel->r_offset < hend)
5768 {
5769 /* If the entry is in use, do nothing. */
5770 if (h->vtable_entries_used
5771 && (rel->r_offset - hstart) < h->vtable_entries_size)
5772 {
45d6a902 5773 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
252b5132
RH
5774 if (h->vtable_entries_used[entry])
5775 continue;
5776 }
5777 /* Otherwise, kill it. */
5778 rel->r_offset = rel->r_info = rel->r_addend = 0;
5779 }
5780
b34976b6 5781 return TRUE;
252b5132
RH
5782}
5783
5784/* Do mark and sweep of unused sections. */
5785
b34976b6 5786bfd_boolean
268b6b39 5787elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 5788{
b34976b6 5789 bfd_boolean ok = TRUE;
252b5132
RH
5790 bfd *sub;
5791 asection * (*gc_mark_hook)
268b6b39
AM
5792 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5793 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
252b5132
RH
5794
5795 if (!get_elf_backend_data (abfd)->can_gc_sections
0eddce27
AM
5796 || info->relocatable
5797 || info->emitrelocations
5798 || !is_elf_hash_table (info->hash)
252b5132 5799 || elf_hash_table (info)->dynamic_sections_created)
b34976b6 5800 return TRUE;
252b5132
RH
5801
5802 /* Apply transitive closure to the vtable entry usage info. */
5803 elf_link_hash_traverse (elf_hash_table (info),
5804 elf_gc_propagate_vtable_entries_used,
268b6b39 5805 &ok);
252b5132 5806 if (!ok)
b34976b6 5807 return FALSE;
252b5132
RH
5808
5809 /* Kill the vtable relocations that were not used. */
5810 elf_link_hash_traverse (elf_hash_table (info),
5811 elf_gc_smash_unused_vtentry_relocs,
268b6b39 5812 &ok);
252b5132 5813 if (!ok)
b34976b6 5814 return FALSE;
252b5132
RH
5815
5816 /* Grovel through relocs to find out who stays ... */
5817
5818 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
5819 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5820 {
5821 asection *o;
f6af82bd
AM
5822
5823 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5824 continue;
5825
252b5132
RH
5826 for (o = sub->sections; o != NULL; o = o->next)
5827 {
5828 if (o->flags & SEC_KEEP)
c44233aa 5829 if (!elf_gc_mark (info, o, gc_mark_hook))
b34976b6 5830 return FALSE;
252b5132
RH
5831 }
5832 }
5833
5834 /* ... and mark SEC_EXCLUDE for those that go. */
a7b97311 5835 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
b34976b6 5836 return FALSE;
252b5132 5837
b34976b6 5838 return TRUE;
252b5132
RH
5839}
5840\f
4cc11e76 5841/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
252b5132 5842
b34976b6 5843bfd_boolean
268b6b39
AM
5844elf_gc_record_vtinherit (bfd *abfd,
5845 asection *sec,
5846 struct elf_link_hash_entry *h,
5847 bfd_vma offset)
252b5132
RH
5848{
5849 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
5850 struct elf_link_hash_entry **search, *child;
5851 bfd_size_type extsymcount;
5852
5853 /* The sh_info field of the symtab header tells us where the
5854 external symbols start. We don't care about the local symbols at
5855 this point. */
5856 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
5857 if (!elf_bad_symtab (abfd))
5858 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
5859
5860 sym_hashes = elf_sym_hashes (abfd);
5861 sym_hashes_end = sym_hashes + extsymcount;
5862
5863 /* Hunt down the child symbol, which is in this section at the same
5864 offset as the relocation. */
5865 for (search = sym_hashes; search != sym_hashes_end; ++search)
5866 {
5867 if ((child = *search) != NULL
5868 && (child->root.type == bfd_link_hash_defined
5869 || child->root.type == bfd_link_hash_defweak)
5870 && child->root.u.def.section == sec
5871 && child->root.u.def.value == offset)
5872 goto win;
5873 }
5874
5875 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
8f615d07 5876 bfd_archive_filename (abfd), sec->name,
a7b97311 5877 (unsigned long) offset);
252b5132 5878 bfd_set_error (bfd_error_invalid_operation);
b34976b6 5879 return FALSE;
252b5132 5880
dc810e39 5881 win:
252b5132
RH
5882 if (!h)
5883 {
5884 /* This *should* only be the absolute section. It could potentially
5885 be that someone has defined a non-global vtable though, which
5886 would be bad. It isn't worth paging in the local symbols to be
5887 sure though; that case should simply be handled by the assembler. */
5888
5889 child->vtable_parent = (struct elf_link_hash_entry *) -1;
5890 }
5891 else
5892 child->vtable_parent = h;
5893
b34976b6 5894 return TRUE;
252b5132
RH
5895}
5896
4cc11e76 5897/* Called from check_relocs to record the existence of a VTENTRY reloc. */
252b5132 5898
b34976b6 5899bfd_boolean
268b6b39
AM
5900elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
5901 asection *sec ATTRIBUTE_UNUSED,
5902 struct elf_link_hash_entry *h,
5903 bfd_vma addend)
252b5132 5904{
9c5bfbb7 5905 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
45d6a902 5906 unsigned int log_file_align = bed->s->log_file_align;
0d1ea5c0 5907
64c456e1 5908 if (addend >= h->vtable_entries_size)
252b5132 5909 {
64c456e1 5910 size_t size, bytes, file_align;
b34976b6 5911 bfd_boolean *ptr = h->vtable_entries_used;
252b5132
RH
5912
5913 /* While the symbol is undefined, we have to be prepared to handle
5914 a zero size. */
64c456e1 5915 file_align = 1 << log_file_align;
252b5132 5916 if (h->root.type == bfd_link_hash_undefined)
64c456e1 5917 size = addend + file_align;
252b5132
RH
5918 else
5919 {
5920 size = h->size;
64c456e1 5921 if (addend >= size)
252b5132
RH
5922 {
5923 /* Oops! We've got a reference past the defined end of
5924 the table. This is probably a bug -- shall we warn? */
64c456e1 5925 size = addend + file_align;
252b5132
RH
5926 }
5927 }
64c456e1 5928 size = (size + file_align - 1) & -file_align;
252b5132
RH
5929
5930 /* Allocate one extra entry for use as a "done" flag for the
64c456e1
AM
5931 consolidation pass. */
5932 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
252b5132
RH
5933
5934 if (ptr)
5935 {
268b6b39 5936 ptr = bfd_realloc (ptr - 1, bytes);
3e932841 5937
fed79cc6
NC
5938 if (ptr != NULL)
5939 {
5940 size_t oldbytes;
252b5132 5941
45d6a902 5942 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
b34976b6 5943 * sizeof (bfd_boolean));
a7b97311 5944 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
fed79cc6 5945 }
252b5132
RH
5946 }
5947 else
268b6b39 5948 ptr = bfd_zmalloc (bytes);
252b5132 5949
fed79cc6 5950 if (ptr == NULL)
b34976b6 5951 return FALSE;
3e932841 5952
252b5132 5953 /* And arrange for that done flag to be at index -1. */
fed79cc6 5954 h->vtable_entries_used = ptr + 1;
252b5132
RH
5955 h->vtable_entries_size = size;
5956 }
3e932841 5957
45d6a902 5958 h->vtable_entries_used[addend >> log_file_align] = TRUE;
252b5132 5959
b34976b6 5960 return TRUE;
252b5132
RH
5961}
5962
5963/* And an accompanying bit to work out final got entry offsets once
5964 we're done. Should be called from final_link. */
5965
b34976b6 5966bfd_boolean
268b6b39
AM
5967elf_gc_common_finalize_got_offsets (bfd *abfd,
5968 struct bfd_link_info *info)
252b5132
RH
5969{
5970 bfd *i;
9c5bfbb7 5971 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
5972 bfd_vma gotoff;
5973
0eddce27
AM
5974 if (! is_elf_hash_table (info->hash))
5975 return FALSE;
5976
252b5132
RH
5977 /* The GOT offset is relative to the .got section, but the GOT header is
5978 put into the .got.plt section, if the backend uses it. */
5979 if (bed->want_got_plt)
5980 gotoff = 0;
5981 else
5982 gotoff = bed->got_header_size;
5983
5984 /* Do the local .got entries first. */
5985 for (i = info->input_bfds; i; i = i->link_next)
5986 {
f6af82bd 5987 bfd_signed_vma *local_got;
252b5132
RH
5988 bfd_size_type j, locsymcount;
5989 Elf_Internal_Shdr *symtab_hdr;
5990
f6af82bd
AM
5991 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
5992 continue;
5993
5994 local_got = elf_local_got_refcounts (i);
252b5132
RH
5995 if (!local_got)
5996 continue;
5997
5998 symtab_hdr = &elf_tdata (i)->symtab_hdr;
5999 if (elf_bad_symtab (i))
6000 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6001 else
6002 locsymcount = symtab_hdr->sh_info;
6003
6004 for (j = 0; j < locsymcount; ++j)
6005 {
6006 if (local_got[j] > 0)
6007 {
6008 local_got[j] = gotoff;
6009 gotoff += ARCH_SIZE / 8;
6010 }
6011 else
6012 local_got[j] = (bfd_vma) -1;
6013 }
6014 }
6015
dd5724d5
AM
6016 /* Then the global .got entries. .plt refcounts are handled by
6017 adjust_dynamic_symbol */
252b5132
RH
6018 elf_link_hash_traverse (elf_hash_table (info),
6019 elf_gc_allocate_got_offsets,
268b6b39 6020 &gotoff);
b34976b6 6021 return TRUE;
252b5132
RH
6022}
6023
6024/* We need a special top-level link routine to convert got reference counts
6025 to real got offsets. */
6026
b34976b6 6027static bfd_boolean
268b6b39 6028elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *offarg)
252b5132 6029{
268b6b39 6030 bfd_vma *off = offarg;
252b5132 6031
e92d460e
AM
6032 if (h->root.type == bfd_link_hash_warning)
6033 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6034
252b5132
RH
6035 if (h->got.refcount > 0)
6036 {
6037 h->got.offset = off[0];
6038 off[0] += ARCH_SIZE / 8;
6039 }
6040 else
6041 h->got.offset = (bfd_vma) -1;
6042
b34976b6 6043 return TRUE;
252b5132
RH
6044}
6045
6046/* Many folk need no more in the way of final link than this, once
6047 got entry reference counting is enabled. */
6048
b34976b6 6049bfd_boolean
268b6b39 6050elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
6051{
6052 if (!elf_gc_common_finalize_got_offsets (abfd, info))
b34976b6 6053 return FALSE;
252b5132
RH
6054
6055 /* Invoke the regular ELF backend linker to do all the work. */
6056 return elf_bfd_final_link (abfd, info);
6057}
6058
6059/* This function will be called though elf_link_hash_traverse to store
6060 all hash value of the exported symbols in an array. */
6061
b34976b6 6062static bfd_boolean
268b6b39 6063elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
252b5132 6064{
268b6b39 6065 unsigned long **valuep = data;
252b5132
RH
6066 const char *name;
6067 char *p;
6068 unsigned long ha;
6069 char *alc = NULL;
6070
e92d460e
AM
6071 if (h->root.type == bfd_link_hash_warning)
6072 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6073
252b5132
RH
6074 /* Ignore indirect symbols. These are added by the versioning code. */
6075 if (h->dynindx == -1)
b34976b6 6076 return TRUE;
252b5132
RH
6077
6078 name = h->root.root.string;
6079 p = strchr (name, ELF_VER_CHR);
6080 if (p != NULL)
6081 {
268b6b39
AM
6082 alc = bfd_malloc (p - name + 1);
6083 memcpy (alc, name, p - name);
252b5132
RH
6084 alc[p - name] = '\0';
6085 name = alc;
6086 }
6087
6088 /* Compute the hash value. */
6089 ha = bfd_elf_hash (name);
6090
6091 /* Store the found hash value in the array given as the argument. */
6092 *(*valuep)++ = ha;
6093
6094 /* And store it in the struct so that we can put it in the hash table
6095 later. */
6096 h->elf_hash_value = ha;
6097
6098 if (alc != NULL)
6099 free (alc);
6100
b34976b6 6101 return TRUE;
252b5132 6102}
73d074b4 6103
b34976b6 6104bfd_boolean
268b6b39 6105elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
73d074b4 6106{
268b6b39 6107 struct elf_reloc_cookie *rcookie = cookie;
73d074b4
DJ
6108
6109 if (rcookie->bad_symtab)
6110 rcookie->rel = rcookie->rels;
6111
6112 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
6113 {
d6fe2dc1 6114 unsigned long r_symndx;
73d074b4
DJ
6115
6116 if (! rcookie->bad_symtab)
6117 if (rcookie->rel->r_offset > offset)
b34976b6 6118 return FALSE;
73d074b4
DJ
6119 if (rcookie->rel->r_offset != offset)
6120 continue;
6121
d6fe2dc1
AM
6122 r_symndx = ELF_R_SYM (rcookie->rel->r_info);
6123 if (r_symndx == SHN_UNDEF)
b34976b6 6124 return TRUE;
d6fe2dc1 6125
73d074b4 6126 if (r_symndx >= rcookie->locsymcount
6cdc0ccc 6127 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
73d074b4
DJ
6128 {
6129 struct elf_link_hash_entry *h;
6130
6131 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
6132
6133 while (h->root.type == bfd_link_hash_indirect
6134 || h->root.type == bfd_link_hash_warning)
6135 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6136
6137 if ((h->root.type == bfd_link_hash_defined
6138 || h->root.type == bfd_link_hash_defweak)
ed4de5e2 6139 && elf_discarded_section (h->root.u.def.section))
b34976b6 6140 return TRUE;
73d074b4 6141 else
b34976b6 6142 return FALSE;
73d074b4 6143 }
6cdc0ccc 6144 else
73d074b4
DJ
6145 {
6146 /* It's not a relocation against a global symbol,
44421011 6147 but it could be a relocation against a local
73d074b4
DJ
6148 symbol for a discarded section. */
6149 asection *isec;
6cdc0ccc 6150 Elf_Internal_Sym *isym;
73d074b4
DJ
6151
6152 /* Need to: get the symbol; get the section. */
6cdc0ccc
AM
6153 isym = &rcookie->locsyms[r_symndx];
6154 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
73d074b4 6155 {
6cdc0ccc 6156 isec = section_from_elf_index (rcookie->abfd, isym->st_shndx);
ed4de5e2 6157 if (isec != NULL && elf_discarded_section (isec))
b34976b6 6158 return TRUE;
73d074b4
DJ
6159 }
6160 }
b34976b6 6161 return FALSE;
73d074b4 6162 }
b34976b6 6163 return FALSE;
73d074b4
DJ
6164}
6165
6166/* Discard unneeded references to discarded sections.
b34976b6 6167 Returns TRUE if any section's size was changed. */
73d074b4 6168/* This function assumes that the relocations are in sorted order,
ab3acfbe 6169 which is true for all known assemblers. */
73d074b4 6170
b34976b6 6171bfd_boolean
268b6b39 6172elf_bfd_discard_info (bfd *output_bfd, struct bfd_link_info *info)
73d074b4
DJ
6173{
6174 struct elf_reloc_cookie cookie;
126495ed 6175 asection *stab, *eh;
73d074b4 6176 Elf_Internal_Shdr *symtab_hdr;
9c5bfbb7 6177 const struct elf_backend_data *bed;
73d074b4 6178 bfd *abfd;
99eb2ac8 6179 unsigned int count;
b34976b6 6180 bfd_boolean ret = FALSE;
73d074b4 6181
d6fe2dc1 6182 if (info->traditional_format
0eddce27 6183 || !is_elf_hash_table (info->hash))
b34976b6 6184 return FALSE;
65765700 6185
73d074b4
DJ
6186 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
6187 {
163c1c30
L
6188 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6189 continue;
6190
73d074b4
DJ
6191 bed = get_elf_backend_data (abfd);
6192
6193 if ((abfd->flags & DYNAMIC) != 0)
6194 continue;
6195
126495ed 6196 eh = bfd_get_section_by_name (abfd, ".eh_frame");
1049f94e 6197 if (info->relocatable
2d36fe5f
AH
6198 || (eh != NULL
6199 && (eh->_raw_size == 0
6200 || bfd_is_abs_section (eh->output_section))))
126495ed 6201 eh = NULL;
65765700 6202
99eb2ac8
AM
6203 stab = bfd_get_section_by_name (abfd, ".stab");
6204 if (stab != NULL
6205 && (stab->_raw_size == 0
6206 || bfd_is_abs_section (stab->output_section)
68bfbfcc 6207 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
99eb2ac8
AM
6208 stab = NULL;
6209
6210 if (stab == NULL
6211 && eh == NULL
6212 && bed->elf_backend_discard_info == NULL)
73d074b4
DJ
6213 continue;
6214
6215 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
73d074b4
DJ
6216 cookie.abfd = abfd;
6217 cookie.sym_hashes = elf_sym_hashes (abfd);
6218 cookie.bad_symtab = elf_bad_symtab (abfd);
6219 if (cookie.bad_symtab)
6220 {
99eb2ac8 6221 cookie.locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
73d074b4
DJ
6222 cookie.extsymoff = 0;
6223 }
6224 else
6225 {
6226 cookie.locsymcount = symtab_hdr->sh_info;
6227 cookie.extsymoff = symtab_hdr->sh_info;
6228 }
6229
6cdc0ccc
AM
6230 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6231 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
c44233aa 6232 {
6cdc0ccc
AM
6233 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
6234 cookie.locsymcount, 0,
6235 NULL, NULL, NULL);
c44233aa 6236 if (cookie.locsyms == NULL)
b34976b6 6237 return FALSE;
9ad5cbcf 6238 }
73d074b4 6239
99eb2ac8 6240 if (stab != NULL)
73d074b4 6241 {
99eb2ac8
AM
6242 cookie.rels = NULL;
6243 count = stab->reloc_count;
6244 if (count != 0)
268b6b39
AM
6245 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
6246 info->keep_memory);
99eb2ac8 6247 if (cookie.rels != NULL)
73d074b4
DJ
6248 {
6249 cookie.rel = cookie.rels;
99eb2ac8
AM
6250 cookie.relend = cookie.rels;
6251 cookie.relend += count * bed->s->int_rels_per_ext_rel;
65765700
JJ
6252 if (_bfd_discard_section_stabs (abfd, stab,
6253 elf_section_data (stab)->sec_info,
73d074b4
DJ
6254 elf_reloc_symbol_deleted_p,
6255 &cookie))
b34976b6 6256 ret = TRUE;
6cdc0ccc 6257 if (elf_section_data (stab)->relocs != cookie.rels)
73d074b4
DJ
6258 free (cookie.rels);
6259 }
6260 }
6261
99eb2ac8 6262 if (eh != NULL)
65765700
JJ
6263 {
6264 cookie.rels = NULL;
99eb2ac8
AM
6265 count = eh->reloc_count;
6266 if (count != 0)
268b6b39
AM
6267 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
6268 info->keep_memory);
99eb2ac8
AM
6269 cookie.rel = cookie.rels;
6270 cookie.relend = cookie.rels;
6271 if (cookie.rels != NULL)
6272 cookie.relend += count * bed->s->int_rels_per_ext_rel;
6273
126495ed 6274 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
65765700
JJ
6275 elf_reloc_symbol_deleted_p,
6276 &cookie))
b34976b6 6277 ret = TRUE;
99eb2ac8
AM
6278
6279 if (cookie.rels != NULL
6280 && elf_section_data (eh)->relocs != cookie.rels)
65765700
JJ
6281 free (cookie.rels);
6282 }
6283
99eb2ac8
AM
6284 if (bed->elf_backend_discard_info != NULL
6285 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
b34976b6 6286 ret = TRUE;
73d074b4 6287
6cdc0ccc
AM
6288 if (cookie.locsyms != NULL
6289 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
6290 {
6291 if (! info->keep_memory)
6292 free (cookie.locsyms);
6293 else
6294 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
6295 }
73d074b4 6296 }
65765700 6297
126495ed 6298 if (info->eh_frame_hdr
1049f94e 6299 && !info->relocatable
126495ed 6300 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
b34976b6 6301 ret = TRUE;
99eb2ac8 6302
73d074b4
DJ
6303 return ret;
6304}
6305
b34976b6 6306static bfd_boolean
268b6b39 6307elf_section_ignore_discarded_relocs (asection *sec)
73d074b4 6308{
9c5bfbb7 6309 const struct elf_backend_data *bed;
40b829d4 6310
68bfbfcc 6311 switch (sec->sec_info_type)
65765700
JJ
6312 {
6313 case ELF_INFO_TYPE_STABS:
6314 case ELF_INFO_TYPE_EH_FRAME:
b34976b6 6315 return TRUE;
65765700
JJ
6316 default:
6317 break;
6318 }
40b829d4
AM
6319
6320 bed = get_elf_backend_data (sec->owner);
6321 if (bed->elf_backend_ignore_discarded_relocs != NULL
6322 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
b34976b6 6323 return TRUE;
65765700 6324
b34976b6 6325 return FALSE;
73d074b4 6326}
This page took 1.097136 seconds and 4 git commands to generate.