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