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