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