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