bfd/
[deliverable/binutils-gdb.git] / bfd / elflink.h
CommitLineData
252b5132 1/* ELF linker support.
c8e5ddc8 2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
7898deda 3 Free Software Foundation, Inc.
252b5132
RH
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21/* ELF linker code. */
22
23/* This struct is used to pass information to routines called via
24 elf_link_hash_traverse which must return failure. */
25
26struct elf_info_failed
27{
28 boolean failed;
29 struct bfd_link_info *info;
bc2b6df7 30 struct bfd_elf_version_tree *verdefs;
252b5132
RH
31};
32
a7b97311
AM
33static boolean is_global_data_symbol_definition
34 PARAMS ((bfd *, Elf_Internal_Sym *));
35static boolean elf_link_is_defined_archive_symbol
36 PARAMS ((bfd *, carsym *));
252b5132
RH
37static boolean elf_link_add_object_symbols
38 PARAMS ((bfd *, struct bfd_link_info *));
39static boolean elf_link_add_archive_symbols
40 PARAMS ((bfd *, struct bfd_link_info *));
41static boolean elf_merge_symbol
215007a6
L
42 PARAMS ((bfd *, struct bfd_link_info *, const char *,
43 Elf_Internal_Sym *, asection **, bfd_vma *,
44 struct elf_link_hash_entry **, boolean *, boolean *,
45 boolean *, boolean));
46static boolean elf_add_default_symbol
47 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
48 const char *, Elf_Internal_Sym *, asection **, bfd_vma *,
49 boolean *, boolean, boolean));
252b5132
RH
50static boolean elf_export_symbol
51 PARAMS ((struct elf_link_hash_entry *, PTR));
2b0f7ef9
JJ
52static boolean elf_finalize_dynstr
53 PARAMS ((bfd *, struct bfd_link_info *));
252b5132
RH
54static boolean elf_fix_symbol_flags
55 PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
56static boolean elf_adjust_dynamic_symbol
57 PARAMS ((struct elf_link_hash_entry *, PTR));
58static boolean elf_link_find_version_dependencies
59 PARAMS ((struct elf_link_hash_entry *, PTR));
252b5132
RH
60static boolean elf_link_assign_sym_version
61 PARAMS ((struct elf_link_hash_entry *, PTR));
252b5132
RH
62static boolean elf_collect_hash_codes
63 PARAMS ((struct elf_link_hash_entry *, PTR));
3e932841 64static boolean elf_link_read_relocs_from_section
6b5bd373 65 PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
a7b97311
AM
66static size_t compute_bucket_count
67 PARAMS ((struct bfd_link_info *));
41241523 68static boolean elf_link_output_relocs
23bc299b
MM
69 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
70static boolean elf_link_size_reloc_section
71 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
3e932841
KH
72static void elf_link_adjust_relocs
73 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
31367b81 74 struct elf_link_hash_entry **));
db6751f2
JJ
75static int elf_link_sort_cmp1
76 PARAMS ((const void *, const void *));
77static int elf_link_sort_cmp2
78 PARAMS ((const void *, const void *));
79static size_t elf_link_sort_relocs
80 PARAMS ((bfd *, struct bfd_link_info *, asection **));
73d074b4
DJ
81static boolean elf_section_ignore_discarded_relocs
82 PARAMS ((asection *));
252b5132
RH
83
84/* Given an ELF BFD, add symbols to the global hash table as
85 appropriate. */
86
87boolean
88elf_bfd_link_add_symbols (abfd, info)
89 bfd *abfd;
90 struct bfd_link_info *info;
91{
92 switch (bfd_get_format (abfd))
93 {
94 case bfd_object:
95 return elf_link_add_object_symbols (abfd, info);
96 case bfd_archive:
97 return elf_link_add_archive_symbols (abfd, info);
98 default:
99 bfd_set_error (bfd_error_wrong_format);
100 return false;
101 }
102}
103\f
7da9d88f 104/* Return true iff this is a non-common, definition of a non-function symbol. */
48dfb430 105static boolean
7da9d88f 106is_global_data_symbol_definition (abfd, sym)
86033394 107 bfd * abfd ATTRIBUTE_UNUSED;
48dfb430
NC
108 Elf_Internal_Sym * sym;
109{
110 /* Local symbols do not count, but target specific ones might. */
111 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
112 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
113 return false;
114
7da9d88f
NC
115 /* Function symbols do not count. */
116 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
117 return false;
118
48dfb430
NC
119 /* If the section is undefined, then so is the symbol. */
120 if (sym->st_shndx == SHN_UNDEF)
121 return false;
3e932841 122
48dfb430
NC
123 /* If the symbol is defined in the common section, then
124 it is a common definition and so does not count. */
125 if (sym->st_shndx == SHN_COMMON)
126 return false;
127
128 /* If the symbol is in a target specific section then we
129 must rely upon the backend to tell us what it is. */
130 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
131 /* FIXME - this function is not coded yet:
3e932841 132
48dfb430 133 return _bfd_is_global_symbol_definition (abfd, sym);
3e932841 134
48dfb430
NC
135 Instead for now assume that the definition is not global,
136 Even if this is wrong, at least the linker will behave
137 in the same way that it used to do. */
138 return false;
3e932841 139
48dfb430
NC
140 return true;
141}
142
a3a8c91d 143/* Search the symbol table of the archive element of the archive ABFD
4e8a9624 144 whose archive map contains a mention of SYMDEF, and determine if
a3a8c91d
NC
145 the symbol is defined in this element. */
146static boolean
147elf_link_is_defined_archive_symbol (abfd, symdef)
148 bfd * abfd;
149 carsym * symdef;
150{
151 Elf_Internal_Shdr * hdr;
dc810e39
AM
152 bfd_size_type symcount;
153 bfd_size_type extsymcount;
154 bfd_size_type extsymoff;
6cdc0ccc
AM
155 Elf_Internal_Sym *isymbuf;
156 Elf_Internal_Sym *isym;
157 Elf_Internal_Sym *isymend;
158 boolean result;
3e932841 159
a3a8c91d
NC
160 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
161 if (abfd == (bfd *) NULL)
162 return false;
163
164 if (! bfd_check_format (abfd, bfd_object))
165 return false;
166
48dfb430
NC
167 /* If we have already included the element containing this symbol in the
168 link then we do not need to include it again. Just claim that any symbol
169 it contains is not a definition, so that our caller will not decide to
170 (re)include this element. */
171 if (abfd->archive_pass)
172 return false;
3e932841 173
a3a8c91d
NC
174 /* Select the appropriate symbol table. */
175 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
6cdc0ccc 176 hdr = &elf_tdata (abfd)->symtab_hdr;
a3a8c91d 177 else
6cdc0ccc 178 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
a3a8c91d
NC
179
180 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
181
182 /* The sh_info field of the symtab header tells us where the
183 external symbols start. We don't care about the local symbols. */
184 if (elf_bad_symtab (abfd))
185 {
186 extsymcount = symcount;
187 extsymoff = 0;
188 }
189 else
190 {
191 extsymcount = symcount - hdr->sh_info;
192 extsymoff = hdr->sh_info;
193 }
194
6cdc0ccc 195 if (extsymcount == 0)
a3a8c91d
NC
196 return false;
197
6cdc0ccc
AM
198 /* Read in the symbol table. */
199 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
200 NULL, NULL, NULL);
201 if (isymbuf == NULL)
202 return false;
a3a8c91d
NC
203
204 /* Scan the symbol table looking for SYMDEF. */
6cdc0ccc
AM
205 result = false;
206 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
a3a8c91d 207 {
6cdc0ccc 208 const char *name;
a3a8c91d 209
6cdc0ccc
AM
210 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
211 isym->st_name);
a3a8c91d
NC
212 if (name == (const char *) NULL)
213 break;
214
215 if (strcmp (name, symdef->name) == 0)
216 {
6cdc0ccc 217 result = is_global_data_symbol_definition (abfd, isym);
a3a8c91d
NC
218 break;
219 }
220 }
221
6cdc0ccc 222 free (isymbuf);
3e932841 223
a3a8c91d
NC
224 return result;
225}
226\f
252b5132
RH
227/* Add symbols from an ELF archive file to the linker hash table. We
228 don't use _bfd_generic_link_add_archive_symbols because of a
229 problem which arises on UnixWare. The UnixWare libc.so is an
230 archive which includes an entry libc.so.1 which defines a bunch of
231 symbols. The libc.so archive also includes a number of other
232 object files, which also define symbols, some of which are the same
233 as those defined in libc.so.1. Correct linking requires that we
234 consider each object file in turn, and include it if it defines any
235 symbols we need. _bfd_generic_link_add_archive_symbols does not do
236 this; it looks through the list of undefined symbols, and includes
237 any object file which defines them. When this algorithm is used on
238 UnixWare, it winds up pulling in libc.so.1 early and defining a
239 bunch of symbols. This means that some of the other objects in the
240 archive are not included in the link, which is incorrect since they
241 precede libc.so.1 in the archive.
242
243 Fortunately, ELF archive handling is simpler than that done by
244 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
245 oddities. In ELF, if we find a symbol in the archive map, and the
246 symbol is currently undefined, we know that we must pull in that
247 object file.
248
249 Unfortunately, we do have to make multiple passes over the symbol
250 table until nothing further is resolved. */
251
252static boolean
253elf_link_add_archive_symbols (abfd, info)
254 bfd *abfd;
255 struct bfd_link_info *info;
256{
257 symindex c;
258 boolean *defined = NULL;
259 boolean *included = NULL;
260 carsym *symdefs;
261 boolean loop;
dc810e39 262 bfd_size_type amt;
252b5132
RH
263
264 if (! bfd_has_map (abfd))
265 {
266 /* An empty archive is a special case. */
267 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
268 return true;
269 bfd_set_error (bfd_error_no_armap);
270 return false;
271 }
272
273 /* Keep track of all symbols we know to be already defined, and all
274 files we know to be already included. This is to speed up the
275 second and subsequent passes. */
276 c = bfd_ardata (abfd)->symdef_count;
277 if (c == 0)
278 return true;
dc810e39
AM
279 amt = c;
280 amt *= sizeof (boolean);
1126897b
AM
281 defined = (boolean *) bfd_zmalloc (amt);
282 included = (boolean *) bfd_zmalloc (amt);
252b5132
RH
283 if (defined == (boolean *) NULL || included == (boolean *) NULL)
284 goto error_return;
252b5132
RH
285
286 symdefs = bfd_ardata (abfd)->symdefs;
287
288 do
289 {
290 file_ptr last;
291 symindex i;
292 carsym *symdef;
293 carsym *symdefend;
294
295 loop = false;
296 last = -1;
297
298 symdef = symdefs;
299 symdefend = symdef + c;
300 for (i = 0; symdef < symdefend; symdef++, i++)
301 {
302 struct elf_link_hash_entry *h;
303 bfd *element;
304 struct bfd_link_hash_entry *undefs_tail;
305 symindex mark;
306
307 if (defined[i] || included[i])
308 continue;
309 if (symdef->file_offset == last)
310 {
311 included[i] = true;
312 continue;
313 }
314
315 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
316 false, false, false);
317
318 if (h == NULL)
319 {
320 char *p, *copy;
48fc70a2 321 size_t len, first;
252b5132
RH
322
323 /* If this is a default version (the name contains @@),
48fc70a2
AM
324 look up the symbol again with only one `@' as well
325 as without the version. The effect is that references
326 to the symbol with and without the version will be
327 matched by the default symbol in the archive. */
252b5132
RH
328
329 p = strchr (symdef->name, ELF_VER_CHR);
330 if (p == NULL || p[1] != ELF_VER_CHR)
331 continue;
332
48fc70a2
AM
333 /* First check with only one `@'. */
334 len = strlen (symdef->name);
335 copy = bfd_alloc (abfd, (bfd_size_type) len);
252b5132
RH
336 if (copy == NULL)
337 goto error_return;
48fc70a2
AM
338 first = p - symdef->name + 1;
339 memcpy (copy, symdef->name, first);
340 memcpy (copy + first, symdef->name + first + 1, len - first);
252b5132
RH
341
342 h = elf_link_hash_lookup (elf_hash_table (info), copy,
343 false, false, false);
344
48fc70a2
AM
345 if (h == NULL)
346 {
347 /* We also need to check references to the symbol
348 without the version. */
349
350 copy[first - 1] = '\0';
351 h = elf_link_hash_lookup (elf_hash_table (info),
352 copy, false, false, false);
353 }
354
252b5132
RH
355 bfd_release (abfd, copy);
356 }
357
358 if (h == NULL)
359 continue;
360
a3a8c91d
NC
361 if (h->root.type == bfd_link_hash_common)
362 {
363 /* We currently have a common symbol. The archive map contains
364 a reference to this symbol, so we may want to include it. We
365 only want to include it however, if this archive element
366 contains a definition of the symbol, not just another common
367 declaration of it.
368
369 Unfortunately some archivers (including GNU ar) will put
370 declarations of common symbols into their archive maps, as
371 well as real definitions, so we cannot just go by the archive
372 map alone. Instead we must read in the element's symbol
373 table and check that to see what kind of symbol definition
374 this is. */
375 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
376 continue;
377 }
378 else if (h->root.type != bfd_link_hash_undefined)
252b5132
RH
379 {
380 if (h->root.type != bfd_link_hash_undefweak)
381 defined[i] = true;
382 continue;
383 }
384
385 /* We need to include this archive member. */
252b5132
RH
386 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
387 if (element == (bfd *) NULL)
388 goto error_return;
389
390 if (! bfd_check_format (element, bfd_object))
391 goto error_return;
392
393 /* Doublecheck that we have not included this object
394 already--it should be impossible, but there may be
395 something wrong with the archive. */
396 if (element->archive_pass != 0)
397 {
398 bfd_set_error (bfd_error_bad_value);
399 goto error_return;
400 }
401 element->archive_pass = 1;
402
403 undefs_tail = info->hash->undefs_tail;
404
405 if (! (*info->callbacks->add_archive_element) (info, element,
406 symdef->name))
407 goto error_return;
408 if (! elf_link_add_object_symbols (element, info))
409 goto error_return;
410
411 /* If there are any new undefined symbols, we need to make
412 another pass through the archive in order to see whether
413 they can be defined. FIXME: This isn't perfect, because
414 common symbols wind up on undefs_tail and because an
415 undefined symbol which is defined later on in this pass
416 does not require another pass. This isn't a bug, but it
417 does make the code less efficient than it could be. */
418 if (undefs_tail != info->hash->undefs_tail)
419 loop = true;
420
421 /* Look backward to mark all symbols from this object file
422 which we have already seen in this pass. */
423 mark = i;
424 do
425 {
426 included[mark] = true;
427 if (mark == 0)
428 break;
429 --mark;
430 }
431 while (symdefs[mark].file_offset == symdef->file_offset);
432
433 /* We mark subsequent symbols from this object file as we go
434 on through the loop. */
435 last = symdef->file_offset;
436 }
437 }
438 while (loop);
439
440 free (defined);
441 free (included);
442
443 return true;
444
445 error_return:
446 if (defined != (boolean *) NULL)
447 free (defined);
448 if (included != (boolean *) NULL)
449 free (included);
450 return false;
451}
452
453/* This function is called when we want to define a new symbol. It
454 handles the various cases which arise when we find a definition in
455 a dynamic object, or when there is already a definition in a
456 dynamic object. The new symbol is described by NAME, SYM, PSEC,
457 and PVALUE. We set SYM_HASH to the hash table entry. We set
458 OVERRIDE if the old symbol is overriding a new definition. We set
459 TYPE_CHANGE_OK if it is OK for the type to change. We set
460 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
461 change, we mean that we shouldn't warn if the type or size does
456981d7
L
462 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
463 a shared object. */
252b5132
RH
464
465static boolean
466elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
456981d7 467 override, type_change_ok, size_change_ok, dt_needed)
252b5132
RH
468 bfd *abfd;
469 struct bfd_link_info *info;
470 const char *name;
471 Elf_Internal_Sym *sym;
472 asection **psec;
473 bfd_vma *pvalue;
474 struct elf_link_hash_entry **sym_hash;
475 boolean *override;
476 boolean *type_change_ok;
477 boolean *size_change_ok;
456981d7 478 boolean dt_needed;
252b5132
RH
479{
480 asection *sec;
481 struct elf_link_hash_entry *h;
482 int bind;
483 bfd *oldbfd;
484 boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
485
486 *override = false;
487
488 sec = *psec;
489 bind = ELF_ST_BIND (sym->st_info);
490
491 if (! bfd_is_und_section (sec))
492 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
493 else
494 h = ((struct elf_link_hash_entry *)
495 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
496 if (h == NULL)
497 return false;
498 *sym_hash = h;
499
500 /* This code is for coping with dynamic objects, and is only useful
501 if we are doing an ELF link. */
502 if (info->hash->creator != abfd->xvec)
503 return true;
504
505 /* For merging, we only care about real symbols. */
506
507 while (h->root.type == bfd_link_hash_indirect
508 || h->root.type == bfd_link_hash_warning)
509 h = (struct elf_link_hash_entry *) h->root.u.i.link;
510
511 /* If we just created the symbol, mark it as being an ELF symbol.
512 Other than that, there is nothing to do--there is no merge issue
513 with a newly defined symbol--so we just return. */
514
515 if (h->root.type == bfd_link_hash_new)
516 {
517 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
518 return true;
519 }
520
521 /* OLDBFD is a BFD associated with the existing symbol. */
522
523 switch (h->root.type)
524 {
525 default:
526 oldbfd = NULL;
527 break;
528
529 case bfd_link_hash_undefined:
530 case bfd_link_hash_undefweak:
531 oldbfd = h->root.u.undef.abfd;
532 break;
533
534 case bfd_link_hash_defined:
535 case bfd_link_hash_defweak:
536 oldbfd = h->root.u.def.section->owner;
537 break;
538
539 case bfd_link_hash_common:
540 oldbfd = h->root.u.c.p->section->owner;
541 break;
542 }
543
b4536acd
ILT
544 /* In cases involving weak versioned symbols, we may wind up trying
545 to merge a symbol with itself. Catch that here, to avoid the
546 confusion that results if we try to override a symbol with
accc7f69
ILT
547 itself. The additional tests catch cases like
548 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
549 dynamic object, which we do want to handle here. */
550 if (abfd == oldbfd
551 && ((abfd->flags & DYNAMIC) == 0
552 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
b4536acd
ILT
553 return true;
554
252b5132
RH
555 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
556 respectively, is from a dynamic object. */
557
558 if ((abfd->flags & DYNAMIC) != 0)
559 newdyn = true;
560 else
561 newdyn = false;
562
0035bd7b
ILT
563 if (oldbfd != NULL)
564 olddyn = (oldbfd->flags & DYNAMIC) != 0;
252b5132 565 else
0035bd7b
ILT
566 {
567 asection *hsec;
568
569 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
c44233aa 570 indices used by MIPS ELF. */
0035bd7b
ILT
571 switch (h->root.type)
572 {
573 default:
574 hsec = NULL;
575 break;
576
577 case bfd_link_hash_defined:
578 case bfd_link_hash_defweak:
579 hsec = h->root.u.def.section;
580 break;
581
582 case bfd_link_hash_common:
583 hsec = h->root.u.c.p->section;
584 break;
585 }
586
587 if (hsec == NULL)
588 olddyn = false;
589 else
590 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
591 }
252b5132
RH
592
593 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
594 respectively, appear to be a definition rather than reference. */
595
596 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
597 newdef = false;
598 else
599 newdef = true;
600
601 if (h->root.type == bfd_link_hash_undefined
602 || h->root.type == bfd_link_hash_undefweak
603 || h->root.type == bfd_link_hash_common)
604 olddef = false;
605 else
606 olddef = true;
607
608 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
609 symbol, respectively, appears to be a common symbol in a dynamic
610 object. If a symbol appears in an uninitialized section, and is
611 not weak, and is not a function, then it may be a common symbol
612 which was resolved when the dynamic object was created. We want
613 to treat such symbols specially, because they raise special
614 considerations when setting the symbol size: if the symbol
615 appears as a common symbol in a regular object, and the size in
616 the regular object is larger, we must make sure that we use the
617 larger size. This problematic case can always be avoided in C,
618 but it must be handled correctly when using Fortran shared
619 libraries.
620
621 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
622 likewise for OLDDYNCOMMON and OLDDEF.
623
624 Note that this test is just a heuristic, and that it is quite
625 possible to have an uninitialized symbol in a shared object which
626 is really a definition, rather than a common symbol. This could
627 lead to some minor confusion when the symbol really is a common
628 symbol in some regular object. However, I think it will be
629 harmless. */
630
631 if (newdyn
632 && newdef
633 && (sec->flags & SEC_ALLOC) != 0
634 && (sec->flags & SEC_LOAD) == 0
635 && sym->st_size > 0
636 && bind != STB_WEAK
637 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
638 newdyncommon = true;
639 else
640 newdyncommon = false;
641
642 if (olddyn
643 && olddef
644 && h->root.type == bfd_link_hash_defined
645 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
646 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
647 && (h->root.u.def.section->flags & SEC_LOAD) == 0
648 && h->size > 0
649 && h->type != STT_FUNC)
650 olddyncommon = true;
651 else
652 olddyncommon = false;
653
654 /* It's OK to change the type if either the existing symbol or the
456981d7
L
655 new symbol is weak unless it comes from a DT_NEEDED entry of
656 a shared object, in which case, the DT_NEEDED entry may not be
3e932841 657 required at the run time. */
252b5132 658
456981d7 659 if ((! dt_needed && h->root.type == bfd_link_hash_defweak)
252b5132
RH
660 || h->root.type == bfd_link_hash_undefweak
661 || bind == STB_WEAK)
662 *type_change_ok = true;
663
664 /* It's OK to change the size if either the existing symbol or the
665 new symbol is weak, or if the old symbol is undefined. */
666
667 if (*type_change_ok
668 || h->root.type == bfd_link_hash_undefined)
669 *size_change_ok = true;
670
671 /* If both the old and the new symbols look like common symbols in a
672 dynamic object, set the size of the symbol to the larger of the
673 two. */
674
675 if (olddyncommon
676 && newdyncommon
677 && sym->st_size != h->size)
678 {
679 /* Since we think we have two common symbols, issue a multiple
c44233aa
AM
680 common warning if desired. Note that we only warn if the
681 size is different. If the size is the same, we simply let
682 the old symbol override the new one as normally happens with
683 symbols defined in dynamic objects. */
252b5132
RH
684
685 if (! ((*info->callbacks->multiple_common)
686 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
687 h->size, abfd, bfd_link_hash_common, sym->st_size)))
688 return false;
689
690 if (sym->st_size > h->size)
691 h->size = sym->st_size;
692
693 *size_change_ok = true;
694 }
695
696 /* If we are looking at a dynamic object, and we have found a
697 definition, we need to see if the symbol was already defined by
698 some other object. If so, we want to use the existing
699 definition, and we do not want to report a multiple symbol
700 definition error; we do this by clobbering *PSEC to be
701 bfd_und_section_ptr.
702
703 We treat a common symbol as a definition if the symbol in the
704 shared library is a function, since common symbols always
705 represent variables; this can cause confusion in principle, but
706 any such confusion would seem to indicate an erroneous program or
707 shared library. We also permit a common symbol in a regular
0525d26e
ILT
708 object to override a weak symbol in a shared object.
709
710 We prefer a non-weak definition in a shared library to a weak
456981d7
L
711 definition in the executable unless it comes from a DT_NEEDED
712 entry of a shared object, in which case, the DT_NEEDED entry
3e932841 713 may not be required at the run time. */
252b5132
RH
714
715 if (newdyn
716 && newdef
717 && (olddef
718 || (h->root.type == bfd_link_hash_common
719 && (bind == STB_WEAK
0525d26e 720 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
3e932841 721 && (h->root.type != bfd_link_hash_defweak
456981d7 722 || dt_needed
0525d26e 723 || bind == STB_WEAK))
252b5132
RH
724 {
725 *override = true;
726 newdef = false;
727 newdyncommon = false;
728
729 *psec = sec = bfd_und_section_ptr;
730 *size_change_ok = true;
731
732 /* If we get here when the old symbol is a common symbol, then
c44233aa
AM
733 we are explicitly letting it override a weak symbol or
734 function in a dynamic object, and we don't want to warn about
735 a type change. If the old symbol is a defined symbol, a type
736 change warning may still be appropriate. */
252b5132
RH
737
738 if (h->root.type == bfd_link_hash_common)
739 *type_change_ok = true;
740 }
741
742 /* Handle the special case of an old common symbol merging with a
743 new symbol which looks like a common symbol in a shared object.
744 We change *PSEC and *PVALUE to make the new symbol look like a
745 common symbol, and let _bfd_generic_link_add_one_symbol will do
746 the right thing. */
747
748 if (newdyncommon
749 && h->root.type == bfd_link_hash_common)
750 {
751 *override = true;
752 newdef = false;
753 newdyncommon = false;
754 *pvalue = sym->st_size;
755 *psec = sec = bfd_com_section_ptr;
756 *size_change_ok = true;
757 }
758
759 /* If the old symbol is from a dynamic object, and the new symbol is
760 a definition which is not from a dynamic object, then the new
761 symbol overrides the old symbol. Symbols from regular files
762 always take precedence over symbols from dynamic objects, even if
763 they are defined after the dynamic object in the link.
764
765 As above, we again permit a common symbol in a regular object to
766 override a definition in a shared object if the shared object
0525d26e
ILT
767 symbol is a function or is weak.
768
769 As above, we permit a non-weak definition in a shared object to
770 override a weak definition in a regular object. */
252b5132
RH
771
772 if (! newdyn
773 && (newdef
774 || (bfd_is_com_section (sec)
775 && (h->root.type == bfd_link_hash_defweak
776 || h->type == STT_FUNC)))
777 && olddyn
778 && olddef
0525d26e
ILT
779 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
780 && (bind != STB_WEAK
781 || h->root.type == bfd_link_hash_defweak))
252b5132
RH
782 {
783 /* Change the hash table entry to undefined, and let
784 _bfd_generic_link_add_one_symbol do the right thing with the
785 new definition. */
786
787 h->root.type = bfd_link_hash_undefined;
788 h->root.u.undef.abfd = h->root.u.def.section->owner;
789 *size_change_ok = true;
790
791 olddef = false;
792 olddyncommon = false;
793
794 /* We again permit a type change when a common symbol may be
c44233aa 795 overriding a function. */
252b5132
RH
796
797 if (bfd_is_com_section (sec))
798 *type_change_ok = true;
799
800 /* This union may have been set to be non-NULL when this symbol
801 was seen in a dynamic object. We must force the union to be
802 NULL, so that it is correct for a regular symbol. */
803
804 h->verinfo.vertree = NULL;
805
806 /* In this special case, if H is the target of an indirection,
c44233aa
AM
807 we want the caller to frob with H rather than with the
808 indirect symbol. That will permit the caller to redefine the
809 target of the indirection, rather than the indirect symbol
810 itself. FIXME: This will break the -y option if we store a
811 symbol with a different name. */
252b5132
RH
812 *sym_hash = h;
813 }
814
815 /* Handle the special case of a new common symbol merging with an
816 old symbol that looks like it might be a common symbol defined in
817 a shared object. Note that we have already handled the case in
818 which a new common symbol should simply override the definition
819 in the shared library. */
820
821 if (! newdyn
822 && bfd_is_com_section (sec)
823 && olddyncommon)
824 {
825 /* It would be best if we could set the hash table entry to a
826 common symbol, but we don't know what to use for the section
827 or the alignment. */
828 if (! ((*info->callbacks->multiple_common)
829 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
830 h->size, abfd, bfd_link_hash_common, sym->st_size)))
831 return false;
832
833 /* If the predumed common symbol in the dynamic object is
c44233aa 834 larger, pretend that the new symbol has its size. */
252b5132
RH
835
836 if (h->size > *pvalue)
837 *pvalue = h->size;
838
839 /* FIXME: We no longer know the alignment required by the symbol
840 in the dynamic object, so we just wind up using the one from
841 the regular object. */
842
843 olddef = false;
844 olddyncommon = false;
845
846 h->root.type = bfd_link_hash_undefined;
847 h->root.u.undef.abfd = h->root.u.def.section->owner;
848
849 *size_change_ok = true;
850 *type_change_ok = true;
851
852 h->verinfo.vertree = NULL;
853 }
854
0525d26e
ILT
855 /* Handle the special case of a weak definition in a regular object
856 followed by a non-weak definition in a shared object. In this
456981d7
L
857 case, we prefer the definition in the shared object unless it
858 comes from a DT_NEEDED entry of a shared object, in which case,
3e932841 859 the DT_NEEDED entry may not be required at the run time. */
0525d26e 860 if (olddef
456981d7 861 && ! dt_needed
0525d26e
ILT
862 && h->root.type == bfd_link_hash_defweak
863 && newdef
864 && newdyn
865 && bind != STB_WEAK)
b4536acd
ILT
866 {
867 /* To make this work we have to frob the flags so that the rest
c44233aa
AM
868 of the code does not think we are using the regular
869 definition. */
64df8d0b
ILT
870 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
871 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
872 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
873 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
874 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
875 | ELF_LINK_HASH_DEF_DYNAMIC);
b4536acd
ILT
876
877 /* If H is the target of an indirection, we want the caller to
c44233aa
AM
878 use H rather than the indirect symbol. Otherwise if we are
879 defining a new indirect symbol we will wind up attaching it
880 to the entry we are overriding. */
b4536acd
ILT
881 *sym_hash = h;
882 }
0525d26e
ILT
883
884 /* Handle the special case of a non-weak definition in a shared
885 object followed by a weak definition in a regular object. In
886 this case we prefer to definition in the shared object. To make
887 this work we have to tell the caller to not treat the new symbol
888 as a definition. */
889 if (olddef
890 && olddyn
891 && h->root.type != bfd_link_hash_defweak
892 && newdef
893 && ! newdyn
894 && bind == STB_WEAK)
895 *override = true;
896
252b5132
RH
897 return true;
898}
899
215007a6
L
900/* This function is called to create an indirect symbol from the
901 default for the symbol with the default version if needed. The
902 symbol is described by H, NAME, SYM, SEC, VALUE, and OVERRIDE. We
903 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
904 indicates if it comes from a DT_NEEDED entry of a shared object. */
905
906static boolean
907elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
908 dynsym, override, dt_needed)
909 bfd *abfd;
910 struct bfd_link_info *info;
911 struct elf_link_hash_entry *h;
912 const char *name;
913 Elf_Internal_Sym *sym;
914 asection **sec;
915 bfd_vma *value;
916 boolean *dynsym;
917 boolean override;
918 boolean dt_needed;
919{
920 boolean type_change_ok;
921 boolean size_change_ok;
922 char *shortname;
923 struct elf_link_hash_entry *hi;
924 struct elf_backend_data *bed;
925 boolean collect;
926 boolean dynamic;
927 char *p;
d4c88bbb 928 size_t len, shortlen;
215007a6
L
929
930 /* If this symbol has a version, and it is the default version, we
931 create an indirect symbol from the default name to the fully
932 decorated name. This will cause external references which do not
933 specify a version to be bound to this version of the symbol. */
934 p = strchr (name, ELF_VER_CHR);
935 if (p == NULL || p[1] != ELF_VER_CHR)
936 return true;
937
938 if (override)
939 {
940 /* We are overridden by an old defition. We need to check if we
d4c88bbb 941 need to create the indirect symbol from the default name. */
215007a6
L
942 hi = elf_link_hash_lookup (elf_hash_table (info), name, true,
943 false, false);
944 BFD_ASSERT (hi != NULL);
945 if (hi == h)
946 return true;
947 while (hi->root.type == bfd_link_hash_indirect
948 || hi->root.type == bfd_link_hash_warning)
949 {
950 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
951 if (hi == h)
952 return true;
953 }
954 }
955
956 bed = get_elf_backend_data (abfd);
957 collect = bed->collect;
958 dynamic = (abfd->flags & DYNAMIC) != 0;
959
d4c88bbb
AM
960 shortlen = p - name;
961 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
215007a6
L
962 if (shortname == NULL)
963 return false;
d4c88bbb
AM
964 memcpy (shortname, name, shortlen);
965 shortname[shortlen] = '\0';
215007a6
L
966
967 /* We are going to create a new symbol. Merge it with any existing
968 symbol with this name. For the purposes of the merge, act as
969 though we were defining the symbol we just defined, although we
970 actually going to define an indirect symbol. */
971 type_change_ok = false;
972 size_change_ok = false;
973 if (! elf_merge_symbol (abfd, info, shortname, sym, sec, value,
974 &hi, &override, &type_change_ok,
975 &size_change_ok, dt_needed))
976 return false;
977
978 if (! override)
979 {
980 if (! (_bfd_generic_link_add_one_symbol
981 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
982 (bfd_vma) 0, name, false, collect,
983 (struct bfd_link_hash_entry **) &hi)))
984 return false;
985 }
986 else
987 {
988 /* In this case the symbol named SHORTNAME is overriding the
989 indirect symbol we want to add. We were planning on making
990 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
991 is the name without a version. NAME is the fully versioned
992 name, and it is the default version.
993
994 Overriding means that we already saw a definition for the
995 symbol SHORTNAME in a regular object, and it is overriding
996 the symbol defined in the dynamic object.
997
998 When this happens, we actually want to change NAME, the
999 symbol we just added, to refer to SHORTNAME. This will cause
1000 references to NAME in the shared object to become references
1001 to SHORTNAME in the regular object. This is what we expect
1002 when we override a function in a shared object: that the
1003 references in the shared object will be mapped to the
1004 definition in the regular object. */
1005
1006 while (hi->root.type == bfd_link_hash_indirect
1007 || hi->root.type == bfd_link_hash_warning)
1008 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1009
1010 h->root.type = bfd_link_hash_indirect;
1011 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1012 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1013 {
1014 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1015 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1016 if (hi->elf_link_hash_flags
1017 & (ELF_LINK_HASH_REF_REGULAR
1018 | ELF_LINK_HASH_DEF_REGULAR))
1019 {
1020 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1021 return false;
1022 }
1023 }
1024
1025 /* Now set HI to H, so that the following code will set the
c44233aa 1026 other fields correctly. */
215007a6
L
1027 hi = h;
1028 }
1029
1030 /* If there is a duplicate definition somewhere, then HI may not
1031 point to an indirect symbol. We will have reported an error to
1032 the user in that case. */
1033
1034 if (hi->root.type == bfd_link_hash_indirect)
1035 {
1036 struct elf_link_hash_entry *ht;
1037
1038 /* If the symbol became indirect, then we assume that we have
1039 not seen a definition before. */
1040 BFD_ASSERT ((hi->elf_link_hash_flags
1041 & (ELF_LINK_HASH_DEF_DYNAMIC
1042 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1043
1044 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1045 (*bed->elf_backend_copy_indirect_symbol) (ht, hi);
1046
1047 /* See if the new flags lead us to realize that the symbol must
1048 be dynamic. */
1049 if (! *dynsym)
1050 {
1051 if (! dynamic)
1052 {
1053 if (info->shared
1054 || ((hi->elf_link_hash_flags
1055 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1056 *dynsym = true;
1057 }
1058 else
1059 {
1060 if ((hi->elf_link_hash_flags
1061 & ELF_LINK_HASH_REF_REGULAR) != 0)
1062 *dynsym = true;
1063 }
1064 }
1065 }
1066
1067 /* We also need to define an indirection from the nondefault version
1068 of the symbol. */
1069
d4c88bbb
AM
1070 len = strlen (name);
1071 shortname = bfd_hash_allocate (&info->hash->table, len);
215007a6
L
1072 if (shortname == NULL)
1073 return false;
d4c88bbb
AM
1074 memcpy (shortname, name, shortlen);
1075 memcpy (shortname + shortlen, p + 1, len - shortlen);
215007a6
L
1076
1077 /* Once again, merge with any existing symbol. */
1078 type_change_ok = false;
1079 size_change_ok = false;
1080 if (! elf_merge_symbol (abfd, info, shortname, sym, sec, value,
1081 &hi, &override, &type_change_ok,
1082 &size_change_ok, dt_needed))
1083 return false;
1084
1085 if (override)
1086 {
1087 /* Here SHORTNAME is a versioned name, so we don't expect to see
1088 the type of override we do in the case above. */
1089 (*_bfd_error_handler)
1090 (_("%s: warning: unexpected redefinition of `%s'"),
1091 bfd_archive_filename (abfd), shortname);
1092 }
1093 else
1094 {
1095 if (! (_bfd_generic_link_add_one_symbol
1096 (info, abfd, shortname, BSF_INDIRECT,
1097 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1098 collect, (struct bfd_link_hash_entry **) &hi)))
1099 return false;
1100
1101 /* If there is a duplicate definition somewhere, then HI may not
1102 point to an indirect symbol. We will have reported an error
1103 to the user in that case. */
1104
1105 if (hi->root.type == bfd_link_hash_indirect)
1106 {
1107 /* If the symbol became indirect, then we assume that we have
1108 not seen a definition before. */
1109 BFD_ASSERT ((hi->elf_link_hash_flags
1110 & (ELF_LINK_HASH_DEF_DYNAMIC
1111 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1112
c44233aa 1113 (*bed->elf_backend_copy_indirect_symbol) (h, hi);
215007a6
L
1114
1115 /* See if the new flags lead us to realize that the symbol
1116 must be dynamic. */
1117 if (! *dynsym)
1118 {
1119 if (! dynamic)
1120 {
1121 if (info->shared
1122 || ((hi->elf_link_hash_flags
1123 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1124 *dynsym = true;
1125 }
1126 else
1127 {
1128 if ((hi->elf_link_hash_flags
1129 & ELF_LINK_HASH_REF_REGULAR) != 0)
1130 *dynsym = true;
1131 }
1132 }
1133 }
1134 }
1135
1136 return true;
1137}
1138
252b5132
RH
1139/* Add symbols from an ELF object file to the linker hash table. */
1140
1141static boolean
1142elf_link_add_object_symbols (abfd, info)
1143 bfd *abfd;
1144 struct bfd_link_info *info;
1145{
1146 boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
1147 const Elf_Internal_Sym *,
1148 const char **, flagword *,
1149 asection **, bfd_vma *));
1150 boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
1151 asection *, const Elf_Internal_Rela *));
1152 boolean collect;
1153 Elf_Internal_Shdr *hdr;
dc810e39
AM
1154 bfd_size_type symcount;
1155 bfd_size_type extsymcount;
1156 bfd_size_type extsymoff;
252b5132
RH
1157 struct elf_link_hash_entry **sym_hash;
1158 boolean dynamic;
252b5132
RH
1159 Elf_External_Versym *extversym = NULL;
1160 Elf_External_Versym *ever;
252b5132 1161 struct elf_link_hash_entry *weaks;
6cdc0ccc
AM
1162 Elf_Internal_Sym *isymbuf = NULL;
1163 Elf_Internal_Sym *isym;
1164 Elf_Internal_Sym *isymend;
c61b8717 1165 struct elf_backend_data *bed;
74816898 1166 boolean dt_needed;
8ea2e4bd 1167 struct elf_link_hash_table * hash_table;
dc810e39 1168 bfd_size_type amt;
8ea2e4bd
NC
1169
1170 hash_table = elf_hash_table (info);
252b5132 1171
c61b8717
RH
1172 bed = get_elf_backend_data (abfd);
1173 add_symbol_hook = bed->elf_add_symbol_hook;
1174 collect = bed->collect;
252b5132
RH
1175
1176 if ((abfd->flags & DYNAMIC) == 0)
1177 dynamic = false;
1178 else
1179 {
1180 dynamic = true;
1181
1182 /* You can't use -r against a dynamic object. Also, there's no
1183 hope of using a dynamic object which does not exactly match
1184 the format of the output file. */
1185 if (info->relocateable || info->hash->creator != abfd->xvec)
1186 {
1187 bfd_set_error (bfd_error_invalid_operation);
1188 goto error_return;
1189 }
1190 }
1191
1192 /* As a GNU extension, any input sections which are named
1193 .gnu.warning.SYMBOL are treated as warning symbols for the given
1194 symbol. This differs from .gnu.warning sections, which generate
1195 warnings when they are included in an output file. */
1196 if (! info->shared)
1197 {
1198 asection *s;
1199
1200 for (s = abfd->sections; s != NULL; s = s->next)
1201 {
1202 const char *name;
1203
1204 name = bfd_get_section_name (abfd, s);
1205 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1206 {
1207 char *msg;
1208 bfd_size_type sz;
1209
1210 name += sizeof ".gnu.warning." - 1;
1211
1212 /* If this is a shared object, then look up the symbol
1213 in the hash table. If it is there, and it is already
1214 been defined, then we will not be using the entry
1215 from this shared object, so we don't need to warn.
1216 FIXME: If we see the definition in a regular object
1217 later on, we will warn, but we shouldn't. The only
1218 fix is to keep track of what warnings we are supposed
1219 to emit, and then handle them all at the end of the
1220 link. */
1221 if (dynamic && abfd->xvec == info->hash->creator)
1222 {
1223 struct elf_link_hash_entry *h;
1224
8ea2e4bd 1225 h = elf_link_hash_lookup (hash_table, name,
252b5132
RH
1226 false, false, true);
1227
1228 /* FIXME: What about bfd_link_hash_common? */
1229 if (h != NULL
1230 && (h->root.type == bfd_link_hash_defined
1231 || h->root.type == bfd_link_hash_defweak))
1232 {
1233 /* We don't want to issue this warning. Clobber
c44233aa
AM
1234 the section size so that the warning does not
1235 get copied into the output file. */
252b5132
RH
1236 s->_raw_size = 0;
1237 continue;
1238 }
1239 }
1240
1241 sz = bfd_section_size (abfd, s);
1242 msg = (char *) bfd_alloc (abfd, sz + 1);
1243 if (msg == NULL)
1244 goto error_return;
1245
1246 if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
1247 goto error_return;
1248
1249 msg[sz] = '\0';
1250
1251 if (! (_bfd_generic_link_add_one_symbol
1252 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
1253 false, collect, (struct bfd_link_hash_entry **) NULL)))
1254 goto error_return;
1255
1256 if (! info->relocateable)
1257 {
1258 /* Clobber the section size so that the warning does
c44233aa 1259 not get copied into the output file. */
252b5132
RH
1260 s->_raw_size = 0;
1261 }
1262 }
1263 }
1264 }
1265
74816898 1266 dt_needed = false;
252b5132
RH
1267 if (! dynamic)
1268 {
1269 /* If we are creating a shared library, create all the dynamic
c44233aa
AM
1270 sections immediately. We need to attach them to something,
1271 so we attach them to this BFD, provided it is the right
1272 format. FIXME: If there are no input BFD's of the same
1273 format as the output, we can't make a shared library. */
252b5132 1274 if (info->shared
8ea2e4bd
NC
1275 && is_elf_hash_table (info)
1276 && ! hash_table->dynamic_sections_created
252b5132
RH
1277 && abfd->xvec == info->hash->creator)
1278 {
1279 if (! elf_link_create_dynamic_sections (abfd, info))
1280 goto error_return;
1281 }
1282 }
8ea2e4bd
NC
1283 else if (! is_elf_hash_table (info))
1284 goto error_return;
252b5132
RH
1285 else
1286 {
1287 asection *s;
1288 boolean add_needed;
1289 const char *name;
1290 bfd_size_type oldsize;
1291 bfd_size_type strindex;
1292
1293 /* Find the name to use in a DT_NEEDED entry that refers to this
1294 object. If the object has a DT_SONAME entry, we use it.
1295 Otherwise, if the generic linker stuck something in
1296 elf_dt_name, we use that. Otherwise, we just use the file
1297 name. If the generic linker put a null string into
1298 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1299 there is a DT_SONAME entry. */
1300 add_needed = true;
7913c838 1301 name = bfd_get_filename (abfd);
252b5132
RH
1302 if (elf_dt_name (abfd) != NULL)
1303 {
1304 name = elf_dt_name (abfd);
1305 if (*name == '\0')
74816898
L
1306 {
1307 if (elf_dt_soname (abfd) != NULL)
c44233aa 1308 dt_needed = true;
74816898
L
1309
1310 add_needed = false;
1311 }
252b5132
RH
1312 }
1313 s = bfd_get_section_by_name (abfd, ".dynamic");
1314 if (s != NULL)
1315 {
6cdc0ccc 1316 Elf_External_Dyn *dynbuf = NULL;
252b5132
RH
1317 Elf_External_Dyn *extdyn;
1318 Elf_External_Dyn *extdynend;
1319 int elfsec;
dc810e39 1320 unsigned long shlink;
a963dc6a
L
1321 int rpath;
1322 int runpath;
252b5132 1323
dc810e39 1324 dynbuf = (Elf_External_Dyn *) bfd_malloc (s->_raw_size);
252b5132
RH
1325 if (dynbuf == NULL)
1326 goto error_return;
1327
1328 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
1329 (file_ptr) 0, s->_raw_size))
6cdc0ccc 1330 goto error_free_dyn;
252b5132
RH
1331
1332 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1333 if (elfsec == -1)
6cdc0ccc 1334 goto error_free_dyn;
dc810e39 1335 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1336
1337 extdyn = dynbuf;
1338 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
a963dc6a
L
1339 rpath = 0;
1340 runpath = 0;
252b5132
RH
1341 for (; extdyn < extdynend; extdyn++)
1342 {
1343 Elf_Internal_Dyn dyn;
1344
1345 elf_swap_dyn_in (abfd, extdyn, &dyn);
1346 if (dyn.d_tag == DT_SONAME)
1347 {
dc810e39
AM
1348 unsigned int tagv = dyn.d_un.d_val;
1349 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132 1350 if (name == NULL)
6cdc0ccc 1351 goto error_free_dyn;
252b5132
RH
1352 }
1353 if (dyn.d_tag == DT_NEEDED)
1354 {
1355 struct bfd_link_needed_list *n, **pn;
1356 char *fnm, *anm;
dc810e39 1357 unsigned int tagv = dyn.d_un.d_val;
252b5132 1358
dc810e39
AM
1359 amt = sizeof (struct bfd_link_needed_list);
1360 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1361 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132 1362 if (n == NULL || fnm == NULL)
6cdc0ccc 1363 goto error_free_dyn;
d4c88bbb
AM
1364 amt = strlen (fnm) + 1;
1365 anm = bfd_alloc (abfd, amt);
252b5132 1366 if (anm == NULL)
6cdc0ccc 1367 goto error_free_dyn;
d4c88bbb 1368 memcpy (anm, fnm, (size_t) amt);
252b5132
RH
1369 n->name = anm;
1370 n->by = abfd;
1371 n->next = NULL;
8ea2e4bd 1372 for (pn = & hash_table->needed;
252b5132
RH
1373 *pn != NULL;
1374 pn = &(*pn)->next)
1375 ;
1376 *pn = n;
1377 }
a963dc6a
L
1378 if (dyn.d_tag == DT_RUNPATH)
1379 {
1380 struct bfd_link_needed_list *n, **pn;
1381 char *fnm, *anm;
dc810e39 1382 unsigned int tagv = dyn.d_un.d_val;
a963dc6a
L
1383
1384 /* When we see DT_RPATH before DT_RUNPATH, we have
512a2384
AM
1385 to clear runpath. Do _NOT_ bfd_release, as that
1386 frees all more recently bfd_alloc'd blocks as
1387 well. */
8ea2e4bd
NC
1388 if (rpath && hash_table->runpath)
1389 hash_table->runpath = NULL;
a963dc6a 1390
dc810e39
AM
1391 amt = sizeof (struct bfd_link_needed_list);
1392 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1393 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
a963dc6a 1394 if (n == NULL || fnm == NULL)
6cdc0ccc 1395 goto error_free_dyn;
d4c88bbb
AM
1396 amt = strlen (fnm) + 1;
1397 anm = bfd_alloc (abfd, amt);
a963dc6a 1398 if (anm == NULL)
6cdc0ccc 1399 goto error_free_dyn;
d4c88bbb 1400 memcpy (anm, fnm, (size_t) amt);
a963dc6a
L
1401 n->name = anm;
1402 n->by = abfd;
1403 n->next = NULL;
8ea2e4bd 1404 for (pn = & hash_table->runpath;
a963dc6a
L
1405 *pn != NULL;
1406 pn = &(*pn)->next)
1407 ;
1408 *pn = n;
1409 runpath = 1;
1410 rpath = 0;
1411 }
3e932841 1412 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
a963dc6a 1413 if (!runpath && dyn.d_tag == DT_RPATH)
c44233aa 1414 {
a963dc6a
L
1415 struct bfd_link_needed_list *n, **pn;
1416 char *fnm, *anm;
dc810e39 1417 unsigned int tagv = dyn.d_un.d_val;
a963dc6a 1418
dc810e39
AM
1419 amt = sizeof (struct bfd_link_needed_list);
1420 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1421 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
a963dc6a 1422 if (n == NULL || fnm == NULL)
6cdc0ccc 1423 goto error_free_dyn;
d4c88bbb
AM
1424 amt = strlen (fnm) + 1;
1425 anm = bfd_alloc (abfd, amt);
a963dc6a 1426 if (anm == NULL)
6cdc0ccc
AM
1427 {
1428 error_free_dyn:
1429 free (dynbuf);
1430 goto error_return;
1431 }
d4c88bbb 1432 memcpy (anm, fnm, (size_t) amt);
a963dc6a
L
1433 n->name = anm;
1434 n->by = abfd;
1435 n->next = NULL;
8ea2e4bd 1436 for (pn = & hash_table->runpath;
a963dc6a
L
1437 *pn != NULL;
1438 pn = &(*pn)->next)
1439 ;
1440 *pn = n;
1441 rpath = 1;
1442 }
252b5132
RH
1443 }
1444
1445 free (dynbuf);
252b5132
RH
1446 }
1447
1448 /* We do not want to include any of the sections in a dynamic
1449 object in the output file. We hack by simply clobbering the
1450 list of sections in the BFD. This could be handled more
1451 cleanly by, say, a new section flag; the existing
1452 SEC_NEVER_LOAD flag is not the one we want, because that one
1453 still implies that the section takes up space in the output
1454 file. */
c601ffdb 1455 bfd_section_list_clear (abfd);
252b5132
RH
1456
1457 /* If this is the first dynamic object found in the link, create
1458 the special sections required for dynamic linking. */
8ea2e4bd
NC
1459 if (! hash_table->dynamic_sections_created)
1460 if (! elf_link_create_dynamic_sections (abfd, info))
1461 goto error_return;
252b5132
RH
1462
1463 if (add_needed)
1464 {
1465 /* Add a DT_NEEDED entry for this dynamic object. */
2b0f7ef9
JJ
1466 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1467 strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, false);
252b5132
RH
1468 if (strindex == (bfd_size_type) -1)
1469 goto error_return;
1470
2b0f7ef9 1471 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
252b5132
RH
1472 {
1473 asection *sdyn;
1474 Elf_External_Dyn *dyncon, *dynconend;
1475
1476 /* The hash table size did not change, which means that
1477 the dynamic object name was already entered. If we
1478 have already included this dynamic object in the
1479 link, just ignore it. There is no reason to include
1480 a particular dynamic object more than once. */
8ea2e4bd 1481 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
252b5132
RH
1482 BFD_ASSERT (sdyn != NULL);
1483
1484 dyncon = (Elf_External_Dyn *) sdyn->contents;
1485 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1486 sdyn->_raw_size);
1487 for (; dyncon < dynconend; dyncon++)
1488 {
1489 Elf_Internal_Dyn dyn;
1490
8ea2e4bd 1491 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
252b5132
RH
1492 if (dyn.d_tag == DT_NEEDED
1493 && dyn.d_un.d_val == strindex)
1494 {
2b0f7ef9 1495 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
252b5132
RH
1496 return true;
1497 }
1498 }
1499 }
1500
dc810e39 1501 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
252b5132
RH
1502 goto error_return;
1503 }
1504
1505 /* Save the SONAME, if there is one, because sometimes the
c44233aa 1506 linker emulation code will need to know it. */
252b5132 1507 if (*name == '\0')
210ba1e8 1508 name = basename (bfd_get_filename (abfd));
252b5132
RH
1509 elf_dt_name (abfd) = name;
1510 }
1511
6cdc0ccc
AM
1512 /* If this is a dynamic object, we always link against the .dynsym
1513 symbol table, not the .symtab symbol table. The dynamic linker
1514 will only see the .dynsym symbol table, so there is no reason to
1515 look at .symtab for a dynamic object. */
1516
1517 if (! dynamic || elf_dynsymtab (abfd) == 0)
1518 hdr = &elf_tdata (abfd)->symtab_hdr;
1519 else
1520 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1521
1522 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1523
1524 /* The sh_info field of the symtab header tells us where the
1525 external symbols start. We don't care about the local symbols at
1526 this point. */
1527 if (elf_bad_symtab (abfd))
1528 {
1529 extsymcount = symcount;
1530 extsymoff = 0;
1531 }
1532 else
1533 {
1534 extsymcount = symcount - hdr->sh_info;
1535 extsymoff = hdr->sh_info;
1536 }
252b5132 1537
6cdc0ccc
AM
1538 sym_hash = NULL;
1539 if (extsymcount != 0)
9ad5cbcf 1540 {
6cdc0ccc
AM
1541 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
1542 NULL, NULL, NULL);
1543 if (isymbuf == NULL)
9ad5cbcf 1544 goto error_return;
6cdc0ccc
AM
1545
1546 /* We store a pointer to the hash table entry for each external
1547 symbol. */
1548 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1549 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
1550 if (sym_hash == NULL)
1551 goto error_free_sym;
1552 elf_sym_hashes (abfd) = sym_hash;
1553 }
1554
1555 if (dynamic)
1556 {
1557 /* Read in any version definitions. */
1558 if (! _bfd_elf_slurp_version_tables (abfd))
1559 goto error_free_sym;
1560
1561 /* Read in the symbol versions, but don't bother to convert them
1562 to internal format. */
1563 if (elf_dynversym (abfd) != 0)
1564 {
1565 Elf_Internal_Shdr *versymhdr;
1566
1567 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
1568 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
1569 if (extversym == NULL)
1570 goto error_free_sym;
1571 amt = versymhdr->sh_size;
1572 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
1573 || bfd_bread ((PTR) extversym, amt, abfd) != amt)
1574 goto error_free_vers;
1575 }
9ad5cbcf
AM
1576 }
1577
252b5132
RH
1578 weaks = NULL;
1579
1580 ever = extversym != NULL ? extversym + extsymoff : NULL;
6cdc0ccc
AM
1581 for (isym = isymbuf, isymend = isymbuf + extsymcount;
1582 isym < isymend;
1583 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
252b5132 1584 {
252b5132
RH
1585 int bind;
1586 bfd_vma value;
1587 asection *sec;
1588 flagword flags;
1589 const char *name;
1590 struct elf_link_hash_entry *h;
1591 boolean definition;
1592 boolean size_change_ok, type_change_ok;
1593 boolean new_weakdef;
1594 unsigned int old_alignment;
215007a6
L
1595 boolean override;
1596
1597 override = false;
252b5132 1598
252b5132
RH
1599 flags = BSF_NO_FLAGS;
1600 sec = NULL;
6cdc0ccc 1601 value = isym->st_value;
252b5132
RH
1602 *sym_hash = NULL;
1603
6cdc0ccc 1604 bind = ELF_ST_BIND (isym->st_info);
252b5132
RH
1605 if (bind == STB_LOCAL)
1606 {
1607 /* This should be impossible, since ELF requires that all
1608 global symbols follow all local symbols, and that sh_info
1609 point to the first global symbol. Unfortunatealy, Irix 5
1610 screws this up. */
1611 continue;
1612 }
1613 else if (bind == STB_GLOBAL)
1614 {
6cdc0ccc
AM
1615 if (isym->st_shndx != SHN_UNDEF
1616 && isym->st_shndx != SHN_COMMON)
252b5132 1617 flags = BSF_GLOBAL;
252b5132
RH
1618 }
1619 else if (bind == STB_WEAK)
1620 flags = BSF_WEAK;
1621 else
1622 {
1623 /* Leave it up to the processor backend. */
1624 }
1625
6cdc0ccc 1626 if (isym->st_shndx == SHN_UNDEF)
252b5132 1627 sec = bfd_und_section_ptr;
6cdc0ccc 1628 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
252b5132 1629 {
6cdc0ccc 1630 sec = section_from_elf_index (abfd, isym->st_shndx);
252b5132
RH
1631 if (sec == NULL)
1632 sec = bfd_abs_section_ptr;
1633 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1634 value -= sec->vma;
1635 }
6cdc0ccc 1636 else if (isym->st_shndx == SHN_ABS)
252b5132 1637 sec = bfd_abs_section_ptr;
6cdc0ccc 1638 else if (isym->st_shndx == SHN_COMMON)
252b5132
RH
1639 {
1640 sec = bfd_com_section_ptr;
1641 /* What ELF calls the size we call the value. What ELF
1642 calls the value we call the alignment. */
6cdc0ccc 1643 value = isym->st_size;
252b5132
RH
1644 }
1645 else
1646 {
1647 /* Leave it up to the processor backend. */
1648 }
1649
6cdc0ccc
AM
1650 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
1651 isym->st_name);
252b5132 1652 if (name == (const char *) NULL)
6cdc0ccc 1653 goto error_free_vers;
252b5132 1654
6cdc0ccc
AM
1655 if (isym->st_shndx == SHN_COMMON
1656 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
13ae64f3
JJ
1657 {
1658 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
1659
1660 if (tcomm == NULL)
1661 {
1662 tcomm = bfd_make_section (abfd, ".tcommon");
1663 if (tcomm == NULL
1664 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
1665 | SEC_IS_COMMON
1666 | SEC_LINKER_CREATED
1667 | SEC_THREAD_LOCAL)))
6cdc0ccc 1668 goto error_free_vers;
13ae64f3
JJ
1669 }
1670 sec = tcomm;
1671 }
1672 else if (add_symbol_hook)
252b5132 1673 {
6cdc0ccc 1674 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
252b5132 1675 &value))
6cdc0ccc 1676 goto error_free_vers;
252b5132
RH
1677
1678 /* The hook function sets the name to NULL if this symbol
1679 should be skipped for some reason. */
1680 if (name == (const char *) NULL)
1681 continue;
1682 }
1683
1684 /* Sanity check that all possibilities were handled. */
1685 if (sec == (asection *) NULL)
1686 {
1687 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 1688 goto error_free_vers;
252b5132
RH
1689 }
1690
1691 if (bfd_is_und_section (sec)
1692 || bfd_is_com_section (sec))
1693 definition = false;
1694 else
1695 definition = true;
1696
1697 size_change_ok = false;
1698 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
1699 old_alignment = 0;
1700 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1701 {
1702 Elf_Internal_Versym iver;
1703 unsigned int vernum = 0;
252b5132
RH
1704
1705 if (ever != NULL)
1706 {
1707 _bfd_elf_swap_versym_in (abfd, ever, &iver);
1708 vernum = iver.vs_vers & VERSYM_VERSION;
1709
1710 /* If this is a hidden symbol, or if it is not version
c44233aa
AM
1711 1, we append the version name to the symbol name.
1712 However, we do not modify a non-hidden absolute
1713 symbol, because it might be the version symbol
1714 itself. FIXME: What if it isn't? */
252b5132
RH
1715 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1716 || (vernum > 1 && ! bfd_is_abs_section (sec)))
1717 {
1718 const char *verstr;
d4c88bbb 1719 size_t namelen, verlen, newlen;
252b5132
RH
1720 char *newname, *p;
1721
6cdc0ccc 1722 if (isym->st_shndx != SHN_UNDEF)
252b5132
RH
1723 {
1724 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1725 {
1726 (*_bfd_error_handler)
1727 (_("%s: %s: invalid version %u (max %d)"),
8f615d07 1728 bfd_archive_filename (abfd), name, vernum,
252b5132
RH
1729 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1730 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 1731 goto error_free_vers;
252b5132
RH
1732 }
1733 else if (vernum > 1)
1734 verstr =
1735 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1736 else
1737 verstr = "";
1738 }
1739 else
1740 {
1741 /* We cannot simply test for the number of
1742 entries in the VERNEED section since the
1743 numbers for the needed versions do not start
1744 at 0. */
1745 Elf_Internal_Verneed *t;
1746
1747 verstr = NULL;
1748 for (t = elf_tdata (abfd)->verref;
1749 t != NULL;
1750 t = t->vn_nextref)
1751 {
1752 Elf_Internal_Vernaux *a;
1753
1754 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1755 {
1756 if (a->vna_other == vernum)
1757 {
1758 verstr = a->vna_nodename;
1759 break;
1760 }
1761 }
1762 if (a != NULL)
1763 break;
1764 }
1765 if (verstr == NULL)
1766 {
1767 (*_bfd_error_handler)
1768 (_("%s: %s: invalid needed version %d"),
8f615d07 1769 bfd_archive_filename (abfd), name, vernum);
252b5132 1770 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 1771 goto error_free_vers;
252b5132
RH
1772 }
1773 }
1774
1775 namelen = strlen (name);
d4c88bbb
AM
1776 verlen = strlen (verstr);
1777 newlen = namelen + verlen + 2;
1778 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
6cdc0ccc 1779 && isym->st_shndx != SHN_UNDEF)
252b5132
RH
1780 ++newlen;
1781
d4c88bbb 1782 newname = (char *) bfd_alloc (abfd, (bfd_size_type) newlen);
252b5132 1783 if (newname == NULL)
6cdc0ccc 1784 goto error_free_vers;
d4c88bbb 1785 memcpy (newname, name, namelen);
252b5132
RH
1786 p = newname + namelen;
1787 *p++ = ELF_VER_CHR;
1287d1cc
ILT
1788 /* If this is a defined non-hidden version symbol,
1789 we add another @ to the name. This indicates the
1790 default version of the symbol. */
1791 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
6cdc0ccc 1792 && isym->st_shndx != SHN_UNDEF)
252b5132 1793 *p++ = ELF_VER_CHR;
d4c88bbb 1794 memcpy (p, verstr, verlen + 1);
252b5132
RH
1795
1796 name = newname;
1797 }
1798 }
1799
6cdc0ccc 1800 if (! elf_merge_symbol (abfd, info, name, isym, &sec, &value,
252b5132 1801 sym_hash, &override, &type_change_ok,
456981d7 1802 &size_change_ok, dt_needed))
6cdc0ccc 1803 goto error_free_vers;
252b5132
RH
1804
1805 if (override)
1806 definition = false;
1807
1808 h = *sym_hash;
1809 while (h->root.type == bfd_link_hash_indirect
1810 || h->root.type == bfd_link_hash_warning)
1811 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1812
1813 /* Remember the old alignment if this is a common symbol, so
c44233aa
AM
1814 that we don't reduce the alignment later on. We can't
1815 check later, because _bfd_generic_link_add_one_symbol
1816 will set a default for the alignment which we want to
1817 override. */
252b5132
RH
1818 if (h->root.type == bfd_link_hash_common)
1819 old_alignment = h->root.u.c.p->alignment_power;
1820
1821 if (elf_tdata (abfd)->verdef != NULL
1822 && ! override
1823 && vernum > 1
1824 && definition)
1825 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1826 }
1827
1828 if (! (_bfd_generic_link_add_one_symbol
1829 (info, abfd, name, flags, sec, value, (const char *) NULL,
1830 false, collect, (struct bfd_link_hash_entry **) sym_hash)))
6cdc0ccc 1831 goto error_free_vers;
252b5132
RH
1832
1833 h = *sym_hash;
1834 while (h->root.type == bfd_link_hash_indirect
1835 || h->root.type == bfd_link_hash_warning)
1836 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1837 *sym_hash = h;
1838
1839 new_weakdef = false;
1840 if (dynamic
1841 && definition
1842 && (flags & BSF_WEAK) != 0
6cdc0ccc 1843 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
252b5132
RH
1844 && info->hash->creator->flavour == bfd_target_elf_flavour
1845 && h->weakdef == NULL)
1846 {
1847 /* Keep a list of all weak defined non function symbols from
1848 a dynamic object, using the weakdef field. Later in this
1849 function we will set the weakdef field to the correct
1850 value. We only put non-function symbols from dynamic
1851 objects on this list, because that happens to be the only
1852 time we need to know the normal symbol corresponding to a
1853 weak symbol, and the information is time consuming to
1854 figure out. If the weakdef field is not already NULL,
1855 then this symbol was already defined by some previous
1856 dynamic object, and we will be using that previous
1857 definition anyhow. */
1858
1859 h->weakdef = weaks;
1860 weaks = h;
1861 new_weakdef = true;
1862 }
1863
1864 /* Set the alignment of a common symbol. */
6cdc0ccc 1865 if (isym->st_shndx == SHN_COMMON
252b5132
RH
1866 && h->root.type == bfd_link_hash_common)
1867 {
1868 unsigned int align;
1869
6cdc0ccc 1870 align = bfd_log2 (isym->st_value);
724982f6
NC
1871 if (align > old_alignment
1872 /* Permit an alignment power of zero if an alignment of one
1873 is specified and no other alignments have been specified. */
6cdc0ccc 1874 || (isym->st_value == 1 && old_alignment == 0))
252b5132
RH
1875 h->root.u.c.p->alignment_power = align;
1876 }
1877
1878 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1879 {
1880 int old_flags;
1881 boolean dynsym;
1882 int new_flag;
1883
1884 /* Remember the symbol size and type. */
6cdc0ccc 1885 if (isym->st_size != 0
252b5132
RH
1886 && (definition || h->size == 0))
1887 {
6cdc0ccc 1888 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
252b5132
RH
1889 (*_bfd_error_handler)
1890 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
6cdc0ccc
AM
1891 name, (unsigned long) h->size,
1892 (unsigned long) isym->st_size, bfd_archive_filename (abfd));
252b5132 1893
6cdc0ccc 1894 h->size = isym->st_size;
252b5132
RH
1895 }
1896
1897 /* If this is a common symbol, then we always want H->SIZE
c44233aa
AM
1898 to be the size of the common symbol. The code just above
1899 won't fix the size if a common symbol becomes larger. We
1900 don't warn about a size change here, because that is
1901 covered by --warn-common. */
252b5132
RH
1902 if (h->root.type == bfd_link_hash_common)
1903 h->size = h->root.u.c.size;
1904
6cdc0ccc 1905 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
252b5132
RH
1906 && (definition || h->type == STT_NOTYPE))
1907 {
1908 if (h->type != STT_NOTYPE
6cdc0ccc 1909 && h->type != ELF_ST_TYPE (isym->st_info)
252b5132
RH
1910 && ! type_change_ok)
1911 (*_bfd_error_handler)
1912 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
6cdc0ccc 1913 name, h->type, ELF_ST_TYPE (isym->st_info),
8f615d07 1914 bfd_archive_filename (abfd));
252b5132 1915
6cdc0ccc 1916 h->type = ELF_ST_TYPE (isym->st_info);
252b5132
RH
1917 }
1918
7a13edea
NC
1919 /* If st_other has a processor-specific meaning, specific code
1920 might be needed here. */
6cdc0ccc 1921 if (isym->st_other != 0)
7a13edea
NC
1922 {
1923 /* Combine visibilities, using the most constraining one. */
1924 unsigned char hvis = ELF_ST_VISIBILITY (h->other);
6cdc0ccc 1925 unsigned char symvis = ELF_ST_VISIBILITY (isym->st_other);
3e932841 1926
7a13edea 1927 if (symvis && (hvis > symvis || hvis == 0))
6cdc0ccc 1928 h->other = isym->st_other;
3e932841 1929
7a13edea 1930 /* If neither has visibility, use the st_other of the
c44233aa
AM
1931 definition. This is an arbitrary choice, since the
1932 other bits have no general meaning. */
7a13edea
NC
1933 if (!symvis && !hvis
1934 && (definition || h->other == 0))
6cdc0ccc 1935 h->other = isym->st_other;
7a13edea 1936 }
252b5132
RH
1937
1938 /* Set a flag in the hash table entry indicating the type of
1939 reference or definition we just found. Keep a count of
1940 the number of dynamic symbols we find. A dynamic symbol
1941 is one which is referenced or defined by both a regular
1942 object and a shared object. */
1943 old_flags = h->elf_link_hash_flags;
1944 dynsym = false;
1945 if (! dynamic)
1946 {
1947 if (! definition)
1948 {
1949 new_flag = ELF_LINK_HASH_REF_REGULAR;
1950 if (bind != STB_WEAK)
1951 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
1952 }
1953 else
1954 new_flag = ELF_LINK_HASH_DEF_REGULAR;
1955 if (info->shared
1956 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
1957 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
1958 dynsym = true;
1959 }
1960 else
1961 {
1962 if (! definition)
1963 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
1964 else
1965 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
1966 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
1967 | ELF_LINK_HASH_REF_REGULAR)) != 0
1968 || (h->weakdef != NULL
1969 && ! new_weakdef
1970 && h->weakdef->dynindx != -1))
1971 dynsym = true;
1972 }
1973
1974 h->elf_link_hash_flags |= new_flag;
1975
215007a6
L
1976 /* Check to see if we need to add an indirect symbol for
1977 the default name. */
051b8577 1978 if (definition || h->root.type == bfd_link_hash_common)
6cdc0ccc 1979 if (! elf_add_default_symbol (abfd, info, h, name, isym,
215007a6
L
1980 &sec, &value, &dynsym,
1981 override, dt_needed))
6cdc0ccc 1982 goto error_free_vers;
252b5132
RH
1983
1984 if (dynsym && h->dynindx == -1)
1985 {
1986 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
6cdc0ccc 1987 goto error_free_vers;
252b5132
RH
1988 if (h->weakdef != NULL
1989 && ! new_weakdef
1990 && h->weakdef->dynindx == -1)
1991 {
a7b97311 1992 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
6cdc0ccc 1993 goto error_free_vers;
252b5132
RH
1994 }
1995 }
38048eb9 1996 else if (dynsym && h->dynindx != -1)
0444bdd4
L
1997 /* If the symbol already has a dynamic index, but
1998 visibility says it should not be visible, turn it into
1999 a local symbol. */
2000 switch (ELF_ST_VISIBILITY (h->other))
2001 {
2002 case STV_INTERNAL:
3e932841 2003 case STV_HIDDEN:
e5094212 2004 (*bed->elf_backend_hide_symbol) (info, h, true);
0444bdd4
L
2005 break;
2006 }
74816898
L
2007
2008 if (dt_needed && definition
2009 && (h->elf_link_hash_flags
2010 & ELF_LINK_HASH_REF_REGULAR) != 0)
2011 {
2012 bfd_size_type oldsize;
2013 bfd_size_type strindex;
2014
8ea2e4bd 2015 if (! is_elf_hash_table (info))
6cdc0ccc 2016 goto error_free_vers;
8ea2e4bd 2017
74816898 2018 /* The symbol from a DT_NEEDED object is referenced from
c44233aa 2019 the regular object to create a dynamic executable. We
3e932841 2020 have to make sure there is a DT_NEEDED entry for it. */
74816898
L
2021
2022 dt_needed = false;
2b0f7ef9
JJ
2023 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2024 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
2025 elf_dt_soname (abfd), false);
74816898 2026 if (strindex == (bfd_size_type) -1)
6cdc0ccc 2027 goto error_free_vers;
74816898 2028
2b0f7ef9 2029 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
74816898
L
2030 {
2031 asection *sdyn;
2032 Elf_External_Dyn *dyncon, *dynconend;
2033
8ea2e4bd 2034 sdyn = bfd_get_section_by_name (hash_table->dynobj,
74816898
L
2035 ".dynamic");
2036 BFD_ASSERT (sdyn != NULL);
2037
2038 dyncon = (Elf_External_Dyn *) sdyn->contents;
2039 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2040 sdyn->_raw_size);
2041 for (; dyncon < dynconend; dyncon++)
2042 {
2043 Elf_Internal_Dyn dyn;
2044
8ea2e4bd 2045 elf_swap_dyn_in (hash_table->dynobj,
74816898
L
2046 dyncon, &dyn);
2047 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
2048 dyn.d_un.d_val != strindex);
2049 }
2050 }
2051
dc810e39 2052 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
6cdc0ccc 2053 goto error_free_vers;
74816898 2054 }
252b5132
RH
2055 }
2056 }
2057
6cdc0ccc
AM
2058 if (extversym != NULL)
2059 {
2060 free (extversym);
2061 extversym = NULL;
2062 }
2063
2064 if (isymbuf != NULL)
2065 free (isymbuf);
2066 isymbuf = NULL;
2067
252b5132
RH
2068 /* Now set the weakdefs field correctly for all the weak defined
2069 symbols we found. The only way to do this is to search all the
2070 symbols. Since we only need the information for non functions in
2071 dynamic objects, that's the only time we actually put anything on
2072 the list WEAKS. We need this information so that if a regular
2073 object refers to a symbol defined weakly in a dynamic object, the
2074 real symbol in the dynamic object is also put in the dynamic
2075 symbols; we also must arrange for both symbols to point to the
2076 same memory location. We could handle the general case of symbol
2077 aliasing, but a general symbol alias can only be generated in
2078 assembler code, handling it correctly would be very time
2079 consuming, and other ELF linkers don't handle general aliasing
2080 either. */
2081 while (weaks != NULL)
2082 {
2083 struct elf_link_hash_entry *hlook;
2084 asection *slook;
2085 bfd_vma vlook;
2086 struct elf_link_hash_entry **hpp;
2087 struct elf_link_hash_entry **hppend;
2088
2089 hlook = weaks;
2090 weaks = hlook->weakdef;
2091 hlook->weakdef = NULL;
2092
2093 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
2094 || hlook->root.type == bfd_link_hash_defweak
2095 || hlook->root.type == bfd_link_hash_common
2096 || hlook->root.type == bfd_link_hash_indirect);
2097 slook = hlook->root.u.def.section;
2098 vlook = hlook->root.u.def.value;
2099
2100 hpp = elf_sym_hashes (abfd);
2101 hppend = hpp + extsymcount;
2102 for (; hpp < hppend; hpp++)
2103 {
2104 struct elf_link_hash_entry *h;
2105
2106 h = *hpp;
2107 if (h != NULL && h != hlook
2108 && h->root.type == bfd_link_hash_defined
2109 && h->root.u.def.section == slook
2110 && h->root.u.def.value == vlook)
2111 {
2112 hlook->weakdef = h;
2113
2114 /* If the weak definition is in the list of dynamic
2115 symbols, make sure the real definition is put there
2116 as well. */
2117 if (hlook->dynindx != -1
2118 && h->dynindx == -1)
2119 {
2120 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2121 goto error_return;
2122 }
2123
2124 /* If the real definition is in the list of dynamic
c44233aa
AM
2125 symbols, make sure the weak definition is put there
2126 as well. If we don't do this, then the dynamic
2127 loader might not merge the entries for the real
2128 definition and the weak definition. */
252b5132
RH
2129 if (h->dynindx != -1
2130 && hlook->dynindx == -1)
2131 {
2132 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
2133 goto error_return;
2134 }
252b5132
RH
2135 break;
2136 }
2137 }
2138 }
2139
252b5132
RH
2140 /* If this object is the same format as the output object, and it is
2141 not a shared library, then let the backend look through the
2142 relocs.
2143
2144 This is required to build global offset table entries and to
2145 arrange for dynamic relocs. It is not required for the
2146 particular common case of linking non PIC code, even when linking
2147 against shared libraries, but unfortunately there is no way of
2148 knowing whether an object file has been compiled PIC or not.
2149 Looking through the relocs is not particularly time consuming.
2150 The problem is that we must either (1) keep the relocs in memory,
2151 which causes the linker to require additional runtime memory or
2152 (2) read the relocs twice from the input file, which wastes time.
2153 This would be a good case for using mmap.
2154
2155 I have no idea how to handle linking PIC code into a file of a
2156 different format. It probably can't be done. */
2157 check_relocs = get_elf_backend_data (abfd)->check_relocs;
2158 if (! dynamic
2159 && abfd->xvec == info->hash->creator
2160 && check_relocs != NULL)
2161 {
2162 asection *o;
2163
2164 for (o = abfd->sections; o != NULL; o = o->next)
2165 {
2166 Elf_Internal_Rela *internal_relocs;
2167 boolean ok;
2168
2169 if ((o->flags & SEC_RELOC) == 0
2170 || o->reloc_count == 0
2171 || ((info->strip == strip_all || info->strip == strip_debugger)
2172 && (o->flags & SEC_DEBUGGING) != 0)
2173 || bfd_is_abs_section (o->output_section))
2174 continue;
2175
2176 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
2177 (abfd, o, (PTR) NULL,
2178 (Elf_Internal_Rela *) NULL,
2179 info->keep_memory));
2180 if (internal_relocs == NULL)
2181 goto error_return;
2182
2183 ok = (*check_relocs) (abfd, info, o, internal_relocs);
2184
6cdc0ccc 2185 if (elf_section_data (o)->relocs != internal_relocs)
252b5132
RH
2186 free (internal_relocs);
2187
2188 if (! ok)
2189 goto error_return;
2190 }
2191 }
2192
2193 /* If this is a non-traditional, non-relocateable link, try to
2194 optimize the handling of the .stab/.stabstr sections. */
2195 if (! dynamic
2196 && ! info->relocateable
2197 && ! info->traditional_format
2198 && info->hash->creator->flavour == bfd_target_elf_flavour
8ea2e4bd 2199 && is_elf_hash_table (info)
252b5132
RH
2200 && (info->strip != strip_all && info->strip != strip_debugger))
2201 {
2202 asection *stab, *stabstr;
2203
2204 stab = bfd_get_section_by_name (abfd, ".stab");
2d653fc7
AM
2205 if (stab != NULL
2206 && (stab->flags & SEC_MERGE) == 0
2207 && !bfd_is_abs_section (stab->output_section))
252b5132
RH
2208 {
2209 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
2210
2211 if (stabstr != NULL)
2212 {
2213 struct bfd_elf_section_data *secdata;
2214
2215 secdata = elf_section_data (stab);
2216 if (! _bfd_link_section_stabs (abfd,
8ea2e4bd 2217 & hash_table->stab_info,
252b5132 2218 stab, stabstr,
65765700 2219 &secdata->sec_info))
252b5132 2220 goto error_return;
65765700
JJ
2221 if (secdata->sec_info)
2222 secdata->sec_info_type = ELF_INFO_TYPE_STABS;
252b5132
RH
2223 }
2224 }
2225 }
2226
8ea2e4bd
NC
2227 if (! info->relocateable && ! dynamic
2228 && is_elf_hash_table (info))
f5fa8ca2
JJ
2229 {
2230 asection *s;
2231
2232 for (s = abfd->sections; s != NULL; s = s->next)
2d653fc7
AM
2233 if ((s->flags & SEC_MERGE) != 0
2234 && !bfd_is_abs_section (s->output_section))
65765700
JJ
2235 {
2236 struct bfd_elf_section_data *secdata;
2237
2238 secdata = elf_section_data (s);
2239 if (! _bfd_merge_section (abfd,
2240 & hash_table->merge_info,
2241 s, &secdata->sec_info))
2242 goto error_return;
2243 else if (secdata->sec_info)
2244 secdata->sec_info_type = ELF_INFO_TYPE_MERGE;
2245 }
f5fa8ca2
JJ
2246 }
2247
f5d44ba0
AM
2248 if (is_elf_hash_table (info))
2249 {
2250 /* Add this bfd to the loaded list. */
2251 struct elf_link_loaded_list *n;
2252
2253 n = ((struct elf_link_loaded_list *)
2254 bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)));
2255 if (n == NULL)
2256 goto error_return;
2257 n->abfd = abfd;
2258 n->next = hash_table->loaded;
2259 hash_table->loaded = n;
2260 }
2261
252b5132
RH
2262 return true;
2263
6cdc0ccc 2264 error_free_vers:
252b5132
RH
2265 if (extversym != NULL)
2266 free (extversym);
6cdc0ccc
AM
2267 error_free_sym:
2268 if (isymbuf != NULL)
2269 free (isymbuf);
2270 error_return:
252b5132
RH
2271 return false;
2272}
2273
2274/* Create some sections which will be filled in with dynamic linking
2275 information. ABFD is an input file which requires dynamic sections
2276 to be created. The dynamic sections take up virtual memory space
2277 when the final executable is run, so we need to create them before
2278 addresses are assigned to the output sections. We work out the
2279 actual contents and size of these sections later. */
2280
2281boolean
2282elf_link_create_dynamic_sections (abfd, info)
2283 bfd *abfd;
2284 struct bfd_link_info *info;
2285{
2286 flagword flags;
2287 register asection *s;
2288 struct elf_link_hash_entry *h;
2289 struct elf_backend_data *bed;
2290
8ea2e4bd
NC
2291 if (! is_elf_hash_table (info))
2292 return false;
2293
252b5132
RH
2294 if (elf_hash_table (info)->dynamic_sections_created)
2295 return true;
2296
2297 /* Make sure that all dynamic sections use the same input BFD. */
2298 if (elf_hash_table (info)->dynobj == NULL)
2299 elf_hash_table (info)->dynobj = abfd;
2300 else
2301 abfd = elf_hash_table (info)->dynobj;
2302
2303 /* Note that we set the SEC_IN_MEMORY flag for all of these
2304 sections. */
2305 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2306 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2307
2308 /* A dynamically linked executable has a .interp section, but a
2309 shared library does not. */
2310 if (! info->shared)
2311 {
2312 s = bfd_make_section (abfd, ".interp");
2313 if (s == NULL
2314 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2315 return false;
2316 }
2317
65765700
JJ
2318 if (! info->traditional_format
2319 && info->hash->creator->flavour == bfd_target_elf_flavour)
2320 {
2321 s = bfd_make_section (abfd, ".eh_frame_hdr");
2322 if (s == NULL
2323 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2324 || ! bfd_set_section_alignment (abfd, s, 2))
2325 return false;
2326 }
2327
252b5132
RH
2328 /* Create sections to hold version informations. These are removed
2329 if they are not needed. */
2330 s = bfd_make_section (abfd, ".gnu.version_d");
2331 if (s == NULL
2332 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2333 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2334 return false;
2335
2336 s = bfd_make_section (abfd, ".gnu.version");
2337 if (s == NULL
2338 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2339 || ! bfd_set_section_alignment (abfd, s, 1))
2340 return false;
2341
2342 s = bfd_make_section (abfd, ".gnu.version_r");
2343 if (s == NULL
2344 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2345 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2346 return false;
2347
2348 s = bfd_make_section (abfd, ".dynsym");
2349 if (s == NULL
2350 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2351 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2352 return false;
2353
2354 s = bfd_make_section (abfd, ".dynstr");
2355 if (s == NULL
2356 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2357 return false;
2358
2359 /* Create a strtab to hold the dynamic symbol names. */
2360 if (elf_hash_table (info)->dynstr == NULL)
2361 {
2b0f7ef9 2362 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
252b5132
RH
2363 if (elf_hash_table (info)->dynstr == NULL)
2364 return false;
2365 }
2366
2367 s = bfd_make_section (abfd, ".dynamic");
2368 if (s == NULL
2369 || ! bfd_set_section_flags (abfd, s, flags)
2370 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2371 return false;
2372
2373 /* The special symbol _DYNAMIC is always set to the start of the
2374 .dynamic section. This call occurs before we have processed the
2375 symbols for any dynamic object, so we don't have to worry about
2376 overriding a dynamic definition. We could set _DYNAMIC in a
2377 linker script, but we only want to define it if we are, in fact,
2378 creating a .dynamic section. We don't want to define it if there
2379 is no .dynamic section, since on some ELF platforms the start up
2380 code examines it to decide how to initialize the process. */
2381 h = NULL;
2382 if (! (_bfd_generic_link_add_one_symbol
2383 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
2384 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
2385 (struct bfd_link_hash_entry **) &h)))
2386 return false;
2387 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2388 h->type = STT_OBJECT;
2389
2390 if (info->shared
2391 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2392 return false;
2393
c7ac6ff8
MM
2394 bed = get_elf_backend_data (abfd);
2395
252b5132
RH
2396 s = bfd_make_section (abfd, ".hash");
2397 if (s == NULL
2398 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2399 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2400 return false;
c7ac6ff8 2401 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
252b5132
RH
2402
2403 /* Let the backend create the rest of the sections. This lets the
2404 backend set the right flags. The backend will normally create
2405 the .got and .plt sections. */
252b5132
RH
2406 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
2407 return false;
2408
2409 elf_hash_table (info)->dynamic_sections_created = true;
2410
2411 return true;
2412}
2413
2414/* Add an entry to the .dynamic table. */
2415
2416boolean
2417elf_add_dynamic_entry (info, tag, val)
2418 struct bfd_link_info *info;
2419 bfd_vma tag;
2420 bfd_vma val;
2421{
2422 Elf_Internal_Dyn dyn;
2423 bfd *dynobj;
2424 asection *s;
dc810e39 2425 bfd_size_type newsize;
252b5132
RH
2426 bfd_byte *newcontents;
2427
8ea2e4bd
NC
2428 if (! is_elf_hash_table (info))
2429 return false;
2430
252b5132
RH
2431 dynobj = elf_hash_table (info)->dynobj;
2432
2433 s = bfd_get_section_by_name (dynobj, ".dynamic");
2434 BFD_ASSERT (s != NULL);
2435
2436 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2437 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2438 if (newcontents == NULL)
2439 return false;
2440
2441 dyn.d_tag = tag;
2442 dyn.d_un.d_val = val;
2443 elf_swap_dyn_out (dynobj, &dyn,
2444 (Elf_External_Dyn *) (newcontents + s->_raw_size));
2445
2446 s->_raw_size = newsize;
2447 s->contents = newcontents;
2448
2449 return true;
2450}
30b30c21
RH
2451
2452/* Record a new local dynamic symbol. */
2453
2454boolean
2455elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
2456 struct bfd_link_info *info;
2457 bfd *input_bfd;
2458 long input_indx;
2459{
2460 struct elf_link_local_dynamic_entry *entry;
2461 struct elf_link_hash_table *eht;
2b0f7ef9 2462 struct elf_strtab_hash *dynstr;
30b30c21 2463 Elf_External_Sym esym;
9ad5cbcf
AM
2464 Elf_External_Sym_Shndx eshndx;
2465 Elf_External_Sym_Shndx *shndx;
30b30c21
RH
2466 unsigned long dynstr_index;
2467 char *name;
dc810e39
AM
2468 file_ptr pos;
2469 bfd_size_type amt;
30b30c21 2470
8ea2e4bd
NC
2471 if (! is_elf_hash_table (info))
2472 return false;
2473
30b30c21
RH
2474 /* See if the entry exists already. */
2475 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2476 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
2477 return true;
2478
2479 entry = (struct elf_link_local_dynamic_entry *)
dc810e39 2480 bfd_alloc (input_bfd, (bfd_size_type) sizeof (*entry));
30b30c21
RH
2481 if (entry == NULL)
2482 return false;
2483
2484 /* Go find the symbol, so that we can find it's name. */
dc810e39
AM
2485 amt = sizeof (Elf_External_Sym);
2486 pos = elf_tdata (input_bfd)->symtab_hdr.sh_offset + input_indx * amt;
2487 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
9ad5cbcf 2488 || bfd_bread ((PTR) &esym, amt, input_bfd) != amt)
30b30c21 2489 return false;
9ad5cbcf
AM
2490 shndx = NULL;
2491 if (elf_tdata (input_bfd)->symtab_shndx_hdr.sh_size != 0)
2492 {
2493 amt = sizeof (Elf_External_Sym_Shndx);
2494 pos = elf_tdata (input_bfd)->symtab_shndx_hdr.sh_offset;
2495 pos += input_indx * amt;
2496 shndx = &eshndx;
2497 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2498 || bfd_bread ((PTR) shndx, amt, input_bfd) != amt)
2499 return false;
2500 }
f8ecb12b 2501 elf_swap_symbol_in (input_bfd, (const PTR) &esym, (const PTR) shndx,
73ff0d56 2502 &entry->isym);
30b30c21
RH
2503
2504 name = (bfd_elf_string_from_elf_section
2505 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
2506 entry->isym.st_name));
2507
2508 dynstr = elf_hash_table (info)->dynstr;
2509 if (dynstr == NULL)
2510 {
2511 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 2512 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
30b30c21
RH
2513 if (dynstr == NULL)
2514 return false;
2515 }
2516
2b0f7ef9 2517 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
30b30c21
RH
2518 if (dynstr_index == (unsigned long) -1)
2519 return false;
2520 entry->isym.st_name = dynstr_index;
2521
2522 eht = elf_hash_table (info);
2523
2524 entry->next = eht->dynlocal;
2525 eht->dynlocal = entry;
2526 entry->input_bfd = input_bfd;
2527 entry->input_indx = input_indx;
2528 eht->dynsymcount++;
2529
587ff49e
RH
2530 /* Whatever binding the symbol had before, it's now local. */
2531 entry->isym.st_info
2532 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
2533
30b30c21
RH
2534 /* The dynindx will be set at the end of size_dynamic_sections. */
2535
2536 return true;
2537}
252b5132 2538\f
6b5bd373
MM
2539/* Read and swap the relocs from the section indicated by SHDR. This
2540 may be either a REL or a RELA section. The relocations are
2541 translated into RELA relocations and stored in INTERNAL_RELOCS,
2542 which should have already been allocated to contain enough space.
2543 The EXTERNAL_RELOCS are a buffer where the external form of the
2544 relocations should be stored.
2545
2546 Returns false if something goes wrong. */
2547
2548static boolean
2549elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2550 internal_relocs)
2551 bfd *abfd;
2552 Elf_Internal_Shdr *shdr;
2553 PTR external_relocs;
2554 Elf_Internal_Rela *internal_relocs;
2555{
c7ac6ff8 2556 struct elf_backend_data *bed;
dc810e39 2557 bfd_size_type amt;
c7ac6ff8 2558
6b5bd373
MM
2559 /* If there aren't any relocations, that's OK. */
2560 if (!shdr)
2561 return true;
2562
2563 /* Position ourselves at the start of the section. */
2564 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2565 return false;
2566
2567 /* Read the relocations. */
dc810e39 2568 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
6b5bd373
MM
2569 return false;
2570
c7ac6ff8
MM
2571 bed = get_elf_backend_data (abfd);
2572
6b5bd373
MM
2573 /* Convert the external relocations to the internal format. */
2574 if (shdr->sh_entsize == sizeof (Elf_External_Rel))
2575 {
2576 Elf_External_Rel *erel;
2577 Elf_External_Rel *erelend;
2578 Elf_Internal_Rela *irela;
c7ac6ff8 2579 Elf_Internal_Rel *irel;
6b5bd373
MM
2580
2581 erel = (Elf_External_Rel *) external_relocs;
d9bc7a44 2582 erelend = erel + NUM_SHDR_ENTRIES (shdr);
6b5bd373 2583 irela = internal_relocs;
dc810e39
AM
2584 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
2585 irel = bfd_alloc (abfd, amt);
c7ac6ff8 2586 for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
6b5bd373 2587 {
4e8a9624 2588 unsigned int i;
c7ac6ff8
MM
2589
2590 if (bed->s->swap_reloc_in)
2591 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
2592 else
2593 elf_swap_reloc_in (abfd, erel, irel);
6b5bd373 2594
c7ac6ff8
MM
2595 for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i)
2596 {
2597 irela[i].r_offset = irel[i].r_offset;
2598 irela[i].r_info = irel[i].r_info;
2599 irela[i].r_addend = 0;
2600 }
6b5bd373
MM
2601 }
2602 }
2603 else
2604 {
2605 Elf_External_Rela *erela;
2606 Elf_External_Rela *erelaend;
2607 Elf_Internal_Rela *irela;
2608
2609 BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
2610
2611 erela = (Elf_External_Rela *) external_relocs;
d9bc7a44 2612 erelaend = erela + NUM_SHDR_ENTRIES (shdr);
6b5bd373 2613 irela = internal_relocs;
c7ac6ff8
MM
2614 for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
2615 {
2616 if (bed->s->swap_reloca_in)
2617 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
2618 else
2619 elf_swap_reloca_in (abfd, erela, irela);
2620 }
6b5bd373
MM
2621 }
2622
2623 return true;
2624}
2625
23bc299b
MM
2626/* Read and swap the relocs for a section O. They may have been
2627 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2628 not NULL, they are used as buffers to read into. They are known to
2629 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2630 the return value is allocated using either malloc or bfd_alloc,
2631 according to the KEEP_MEMORY argument. If O has two relocation
2632 sections (both REL and RELA relocations), then the REL_HDR
2633 relocations will appear first in INTERNAL_RELOCS, followed by the
2634 REL_HDR2 relocations. */
252b5132
RH
2635
2636Elf_Internal_Rela *
2637NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2638 keep_memory)
2639 bfd *abfd;
2640 asection *o;
2641 PTR external_relocs;
2642 Elf_Internal_Rela *internal_relocs;
2643 boolean keep_memory;
2644{
2645 Elf_Internal_Shdr *rel_hdr;
2646 PTR alloc1 = NULL;
2647 Elf_Internal_Rela *alloc2 = NULL;
c7ac6ff8 2648 struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
2649
2650 if (elf_section_data (o)->relocs != NULL)
2651 return elf_section_data (o)->relocs;
2652
2653 if (o->reloc_count == 0)
2654 return NULL;
2655
2656 rel_hdr = &elf_section_data (o)->rel_hdr;
2657
2658 if (internal_relocs == NULL)
2659 {
dc810e39 2660 bfd_size_type size;
252b5132 2661
dc810e39
AM
2662 size = o->reloc_count;
2663 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
252b5132
RH
2664 if (keep_memory)
2665 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2666 else
2667 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2668 if (internal_relocs == NULL)
2669 goto error_return;
2670 }
2671
2672 if (external_relocs == NULL)
2673 {
dc810e39 2674 bfd_size_type size = rel_hdr->sh_size;
6b5bd373
MM
2675
2676 if (elf_section_data (o)->rel_hdr2)
dc810e39 2677 size += elf_section_data (o)->rel_hdr2->sh_size;
6b5bd373 2678 alloc1 = (PTR) bfd_malloc (size);
252b5132
RH
2679 if (alloc1 == NULL)
2680 goto error_return;
2681 external_relocs = alloc1;
2682 }
2683
6b5bd373
MM
2684 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2685 external_relocs,
2686 internal_relocs))
2687 goto error_return;
3e932841
KH
2688 if (!elf_link_read_relocs_from_section
2689 (abfd,
6b5bd373 2690 elf_section_data (o)->rel_hdr2,
2f5116e2 2691 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
d9bc7a44 2692 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
c7ac6ff8 2693 * bed->s->int_rels_per_ext_rel)))
252b5132 2694 goto error_return;
252b5132
RH
2695
2696 /* Cache the results for next time, if we can. */
2697 if (keep_memory)
2698 elf_section_data (o)->relocs = internal_relocs;
2699
2700 if (alloc1 != NULL)
2701 free (alloc1);
2702
2703 /* Don't free alloc2, since if it was allocated we are passing it
2704 back (under the name of internal_relocs). */
2705
2706 return internal_relocs;
2707
2708 error_return:
2709 if (alloc1 != NULL)
2710 free (alloc1);
2711 if (alloc2 != NULL)
2712 free (alloc2);
2713 return NULL;
2714}
2715\f
252b5132
RH
2716/* Record an assignment to a symbol made by a linker script. We need
2717 this in case some dynamic object refers to this symbol. */
2718
252b5132
RH
2719boolean
2720NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
7442e600 2721 bfd *output_bfd ATTRIBUTE_UNUSED;
252b5132
RH
2722 struct bfd_link_info *info;
2723 const char *name;
2724 boolean provide;
2725{
2726 struct elf_link_hash_entry *h;
2727
2728 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2729 return true;
2730
2731 h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
2732 if (h == NULL)
2733 return false;
2734
2735 if (h->root.type == bfd_link_hash_new)
a7b97311 2736 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
252b5132
RH
2737
2738 /* If this symbol is being provided by the linker script, and it is
2739 currently defined by a dynamic object, but not by a regular
2740 object, then mark it as undefined so that the generic linker will
2741 force the correct value. */
2742 if (provide
2743 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2744 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2745 h->root.type = bfd_link_hash_undefined;
2746
2747 /* If this symbol is not being provided by the linker script, and it is
2748 currently defined by a dynamic object, but not by a regular object,
2749 then clear out any version information because the symbol will not be
2750 associated with the dynamic object any more. */
2751 if (!provide
2752 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2753 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2754 h->verinfo.verdef = NULL;
2755
2756 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
994819d2 2757
252b5132
RH
2758 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2759 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2760 || info->shared)
2761 && h->dynindx == -1)
2762 {
2763 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2764 return false;
2765
2766 /* If this is a weak defined symbol, and we know a corresponding
2767 real symbol from the same dynamic object, make sure the real
2768 symbol is also made into a dynamic symbol. */
2769 if (h->weakdef != NULL
2770 && h->weakdef->dynindx == -1)
2771 {
2772 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2773 return false;
2774 }
2775 }
2776
2777 return true;
2778}
2779\f
2780/* This structure is used to pass information to
2781 elf_link_assign_sym_version. */
2782
2783struct elf_assign_sym_version_info
2784{
2785 /* Output BFD. */
2786 bfd *output_bfd;
2787 /* General link information. */
2788 struct bfd_link_info *info;
2789 /* Version tree. */
2790 struct bfd_elf_version_tree *verdefs;
252b5132
RH
2791 /* Whether we had a failure. */
2792 boolean failed;
2793};
2794
2795/* This structure is used to pass information to
2796 elf_link_find_version_dependencies. */
2797
2798struct elf_find_verdep_info
2799{
2800 /* Output BFD. */
2801 bfd *output_bfd;
2802 /* General link information. */
2803 struct bfd_link_info *info;
2804 /* The number of dependencies. */
2805 unsigned int vers;
2806 /* Whether we had a failure. */
2807 boolean failed;
2808};
2809
2810/* Array used to determine the number of hash table buckets to use
2811 based on the number of symbols there are. If there are fewer than
2812 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2813 fewer than 37 we use 17 buckets, and so forth. We never use more
2814 than 32771 buckets. */
2815
2816static const size_t elf_buckets[] =
2817{
2818 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2819 16411, 32771, 0
2820};
2821
2822/* Compute bucket count for hashing table. We do not use a static set
2823 of possible tables sizes anymore. Instead we determine for all
2824 possible reasonable sizes of the table the outcome (i.e., the
2825 number of collisions etc) and choose the best solution. The
2826 weighting functions are not too simple to allow the table to grow
2827 without bounds. Instead one of the weighting factors is the size.
2828 Therefore the result is always a good payoff between few collisions
2829 (= short chain lengths) and table size. */
2830static size_t
2831compute_bucket_count (info)
2832 struct bfd_link_info *info;
2833{
2834 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
7442e600 2835 size_t best_size = 0;
252b5132
RH
2836 unsigned long int *hashcodes;
2837 unsigned long int *hashcodesp;
2838 unsigned long int i;
dc810e39 2839 bfd_size_type amt;
252b5132
RH
2840
2841 /* Compute the hash values for all exported symbols. At the same
2842 time store the values in an array so that we could use them for
2843 optimizations. */
dc810e39
AM
2844 amt = dynsymcount;
2845 amt *= sizeof (unsigned long int);
2846 hashcodes = (unsigned long int *) bfd_malloc (amt);
252b5132
RH
2847 if (hashcodes == NULL)
2848 return 0;
2849 hashcodesp = hashcodes;
2850
2851 /* Put all hash values in HASHCODES. */
2852 elf_link_hash_traverse (elf_hash_table (info),
2853 elf_collect_hash_codes, &hashcodesp);
2854
2855/* We have a problem here. The following code to optimize the table
2856 size requires an integer type with more the 32 bits. If
2857 BFD_HOST_U_64_BIT is set we know about such a type. */
2858#ifdef BFD_HOST_U_64_BIT
82e51918 2859 if (info->optimize)
252b5132
RH
2860 {
2861 unsigned long int nsyms = hashcodesp - hashcodes;
2862 size_t minsize;
2863 size_t maxsize;
2864 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2865 unsigned long int *counts ;
2866
2867 /* Possible optimization parameters: if we have NSYMS symbols we say
2868 that the hashing table must at least have NSYMS/4 and at most
2869 2*NSYMS buckets. */
2870 minsize = nsyms / 4;
2871 if (minsize == 0)
2872 minsize = 1;
2873 best_size = maxsize = nsyms * 2;
2874
2875 /* Create array where we count the collisions in. We must use bfd_malloc
2876 since the size could be large. */
dc810e39
AM
2877 amt = maxsize;
2878 amt *= sizeof (unsigned long int);
2879 counts = (unsigned long int *) bfd_malloc (amt);
252b5132
RH
2880 if (counts == NULL)
2881 {
2882 free (hashcodes);
2883 return 0;
2884 }
2885
2886 /* Compute the "optimal" size for the hash table. The criteria is a
2887 minimal chain length. The minor criteria is (of course) the size
2888 of the table. */
2889 for (i = minsize; i < maxsize; ++i)
2890 {
2891 /* Walk through the array of hashcodes and count the collisions. */
2892 BFD_HOST_U_64_BIT max;
2893 unsigned long int j;
2894 unsigned long int fact;
2895
2896 memset (counts, '\0', i * sizeof (unsigned long int));
2897
2898 /* Determine how often each hash bucket is used. */
2899 for (j = 0; j < nsyms; ++j)
2900 ++counts[hashcodes[j] % i];
2901
2902 /* For the weight function we need some information about the
2903 pagesize on the target. This is information need not be 100%
2904 accurate. Since this information is not available (so far) we
2905 define it here to a reasonable default value. If it is crucial
2906 to have a better value some day simply define this value. */
2907# ifndef BFD_TARGET_PAGESIZE
2908# define BFD_TARGET_PAGESIZE (4096)
2909# endif
2910
2911 /* We in any case need 2 + NSYMS entries for the size values and
2912 the chains. */
2913 max = (2 + nsyms) * (ARCH_SIZE / 8);
2914
2915# if 1
2916 /* Variant 1: optimize for short chains. We add the squares
2917 of all the chain lengths (which favous many small chain
2918 over a few long chains). */
2919 for (j = 0; j < i; ++j)
2920 max += counts[j] * counts[j];
2921
2922 /* This adds penalties for the overall size of the table. */
2923 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2924 max *= fact * fact;
2925# else
2926 /* Variant 2: Optimize a lot more for small table. Here we
2927 also add squares of the size but we also add penalties for
2928 empty slots (the +1 term). */
2929 for (j = 0; j < i; ++j)
2930 max += (1 + counts[j]) * (1 + counts[j]);
2931
2932 /* The overall size of the table is considered, but not as
2933 strong as in variant 1, where it is squared. */
2934 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2935 max *= fact;
2936# endif
2937
2938 /* Compare with current best results. */
2939 if (max < best_chlen)
2940 {
2941 best_chlen = max;
2942 best_size = i;
2943 }
2944 }
2945
2946 free (counts);
2947 }
2948 else
2949#endif /* defined (BFD_HOST_U_64_BIT) */
2950 {
2951 /* This is the fallback solution if no 64bit type is available or if we
2952 are not supposed to spend much time on optimizations. We select the
2953 bucket count using a fixed set of numbers. */
2954 for (i = 0; elf_buckets[i] != 0; i++)
2955 {
2956 best_size = elf_buckets[i];
2957 if (dynsymcount < elf_buckets[i + 1])
2958 break;
2959 }
2960 }
2961
2962 /* Free the arrays we needed. */
2963 free (hashcodes);
2964
2965 return best_size;
2966}
2967
2968/* Set up the sizes and contents of the ELF dynamic sections. This is
2969 called by the ELF linker emulation before_allocation routine. We
2970 must set the sizes of the sections before the linker sets the
2971 addresses of the various sections. */
2972
2973boolean
2974NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
99293407 2975 filter_shlib,
252b5132
RH
2976 auxiliary_filters, info, sinterpptr,
2977 verdefs)
2978 bfd *output_bfd;
2979 const char *soname;
2980 const char *rpath;
252b5132
RH
2981 const char *filter_shlib;
2982 const char * const *auxiliary_filters;
2983 struct bfd_link_info *info;
2984 asection **sinterpptr;
2985 struct bfd_elf_version_tree *verdefs;
2986{
2987 bfd_size_type soname_indx;
25e27870 2988 bfd *dynobj;
252b5132 2989 struct elf_backend_data *bed;
252b5132
RH
2990 struct elf_assign_sym_version_info asvinfo;
2991
2992 *sinterpptr = NULL;
2993
2994 soname_indx = (bfd_size_type) -1;
2995
2996 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2997 return true;
2998
8ea2e4bd 2999 if (! is_elf_hash_table (info))
cc36acdf 3000 return true;
8ea2e4bd 3001
51b64d56
AM
3002 /* Any syms created from now on start with -1 in
3003 got.refcount/offset and plt.refcount/offset. */
3004 elf_hash_table (info)->init_refcount = -1;
3005
252b5132
RH
3006 /* The backend may have to create some sections regardless of whether
3007 we're dynamic or not. */
3008 bed = get_elf_backend_data (output_bfd);
3009 if (bed->elf_backend_always_size_sections
3010 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
3011 return false;
3012
3013 dynobj = elf_hash_table (info)->dynobj;
3014
3015 /* If there were no dynamic objects in the link, there is nothing to
3016 do here. */
3017 if (dynobj == NULL)
3018 return true;
3019
68f69152
JJ
3020 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
3021 return false;
3022
252b5132
RH
3023 if (elf_hash_table (info)->dynamic_sections_created)
3024 {
3025 struct elf_info_failed eif;
3026 struct elf_link_hash_entry *h;
fc8c40a0 3027 asection *dynstr;
252b5132
RH
3028
3029 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
3030 BFD_ASSERT (*sinterpptr != NULL || info->shared);
3031
3032 if (soname != NULL)
3033 {
2b0f7ef9
JJ
3034 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3035 soname, true);
252b5132 3036 if (soname_indx == (bfd_size_type) -1
dc810e39
AM
3037 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
3038 soname_indx))
252b5132
RH
3039 return false;
3040 }
3041
3042 if (info->symbolic)
3043 {
dc810e39
AM
3044 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
3045 (bfd_vma) 0))
252b5132 3046 return false;
d6cf2879 3047 info->flags |= DF_SYMBOLIC;
252b5132
RH
3048 }
3049
3050 if (rpath != NULL)
3051 {
3052 bfd_size_type indx;
3053
2b0f7ef9
JJ
3054 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
3055 true);
3056 if (info->new_dtags)
3057 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
252b5132 3058 if (indx == (bfd_size_type) -1
dc810e39 3059 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
c25373b7 3060 || (info->new_dtags
dc810e39
AM
3061 && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
3062 indx)))
252b5132
RH
3063 return false;
3064 }
3065
3066 if (filter_shlib != NULL)
3067 {
3068 bfd_size_type indx;
3069
2b0f7ef9
JJ
3070 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3071 filter_shlib, true);
252b5132 3072 if (indx == (bfd_size_type) -1
dc810e39 3073 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
252b5132
RH
3074 return false;
3075 }
3076
3077 if (auxiliary_filters != NULL)
3078 {
3079 const char * const *p;
3080
3081 for (p = auxiliary_filters; *p != NULL; p++)
3082 {
3083 bfd_size_type indx;
3084
2b0f7ef9
JJ
3085 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3086 *p, true);
252b5132 3087 if (indx == (bfd_size_type) -1
dc810e39
AM
3088 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
3089 indx))
252b5132
RH
3090 return false;
3091 }
3092 }
3093
391a809a 3094 eif.info = info;
bc2b6df7 3095 eif.verdefs = verdefs;
391a809a
AM
3096 eif.failed = false;
3097
ea44b734 3098 /* If we are supposed to export all symbols into the dynamic symbol
c44233aa 3099 table (this is not the normal case), then do so. */
99293407 3100 if (info->export_dynamic)
ea44b734 3101 {
ea44b734 3102 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
c44233aa 3103 (PTR) &eif);
ea44b734
RH
3104 if (eif.failed)
3105 return false;
3106 }
3107
252b5132
RH
3108 /* Attach all the symbols to their version information. */
3109 asvinfo.output_bfd = output_bfd;
3110 asvinfo.info = info;
3111 asvinfo.verdefs = verdefs;
252b5132
RH
3112 asvinfo.failed = false;
3113
3114 elf_link_hash_traverse (elf_hash_table (info),
3115 elf_link_assign_sym_version,
3116 (PTR) &asvinfo);
3117 if (asvinfo.failed)
3118 return false;
3119
3120 /* Find all symbols which were defined in a dynamic object and make
3121 the backend pick a reasonable value for them. */
252b5132
RH
3122 elf_link_hash_traverse (elf_hash_table (info),
3123 elf_adjust_dynamic_symbol,
3124 (PTR) &eif);
3125 if (eif.failed)
3126 return false;
3127
3128 /* Add some entries to the .dynamic section. We fill in some of the
3129 values later, in elf_bfd_final_link, but we must add the entries
3130 now so that we know the final size of the .dynamic section. */
f0c2e336
MM
3131
3132 /* If there are initialization and/or finalization functions to
3133 call then add the corresponding DT_INIT/DT_FINI entries. */
3134 h = (info->init_function
3e932841 3135 ? elf_link_hash_lookup (elf_hash_table (info),
f0c2e336
MM
3136 info->init_function, false,
3137 false, false)
3138 : NULL);
252b5132
RH
3139 if (h != NULL
3140 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3141 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3142 {
dc810e39 3143 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
252b5132
RH
3144 return false;
3145 }
f0c2e336 3146 h = (info->fini_function
3e932841 3147 ? elf_link_hash_lookup (elf_hash_table (info),
f0c2e336
MM
3148 info->fini_function, false,
3149 false, false)
3150 : NULL);
252b5132
RH
3151 if (h != NULL
3152 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3153 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3154 {
dc810e39 3155 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
252b5132
RH
3156 return false;
3157 }
f0c2e336 3158
fa7ea4d8
AM
3159 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
3160 {
3161 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3162 if (info->shared)
3163 {
3164 bfd *sub;
3165 asection *o;
3166
3167 for (sub = info->input_bfds; sub != NULL;
3168 sub = sub->link_next)
3169 for (o = sub->sections; o != NULL; o = o->next)
3170 if (elf_section_data (o)->this_hdr.sh_type
3171 == SHT_PREINIT_ARRAY)
3172 {
3173 (*_bfd_error_handler)
3174 (_("%s: .preinit_array section is not allowed in DSO"),
3175 bfd_archive_filename (sub));
3176 break;
3177 }
60166579 3178
36b4f6e7 3179 bfd_set_error (bfd_error_nonrepresentable_section);
60166579 3180 return false;
fa7ea4d8
AM
3181 }
3182
3183 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAY,
3184 (bfd_vma) 0)
3185 || !elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAYSZ,
3186 (bfd_vma) 0))
3187 return false;
3188 }
3189 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
3190 {
3191 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAY,
3192 (bfd_vma) 0)
3193 || !elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAYSZ,
3194 (bfd_vma) 0))
3195 return false;
3196 }
3197 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
3198 {
3199 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAY,
3200 (bfd_vma) 0)
3201 || !elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAYSZ,
3202 (bfd_vma) 0))
3203 return false;
3204 }
30831527 3205
fc8c40a0
AM
3206 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
3207 /* If .dynstr is excluded from the link, we don't want any of
3208 these tags. Strictly, we should be checking each section
3209 individually; This quick check covers for the case where
3210 someone does a /DISCARD/ : { *(*) }. */
3211 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
3212 {
3213 bfd_size_type strsize;
3214
2b0f7ef9 3215 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
dc810e39
AM
3216 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
3217 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
3218 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
3219 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
3220 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
3221 (bfd_vma) sizeof (Elf_External_Sym)))
fc8c40a0
AM
3222 return false;
3223 }
252b5132
RH
3224 }
3225
3226 /* The backend must work out the sizes of all the other dynamic
3227 sections. */
252b5132
RH
3228 if (bed->elf_backend_size_dynamic_sections
3229 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
3230 return false;
3231
3232 if (elf_hash_table (info)->dynamic_sections_created)
3233 {
dc810e39 3234 bfd_size_type dynsymcount;
252b5132
RH
3235 asection *s;
3236 size_t bucketcount = 0;
c7ac6ff8 3237 size_t hash_entry_size;
db6751f2 3238 unsigned int dtagcount;
252b5132
RH
3239
3240 /* Set up the version definition section. */
3241 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3242 BFD_ASSERT (s != NULL);
3243
3244 /* We may have created additional version definitions if we are
c44233aa 3245 just linking a regular application. */
252b5132
RH
3246 verdefs = asvinfo.verdefs;
3247
6b9b879a
JJ
3248 /* Skip anonymous version tag. */
3249 if (verdefs != NULL && verdefs->vernum == 0)
3250 verdefs = verdefs->next;
3251
252b5132 3252 if (verdefs == NULL)
7f8d5fc9 3253 _bfd_strip_section_from_output (info, s);
252b5132
RH
3254 else
3255 {
3256 unsigned int cdefs;
3257 bfd_size_type size;
3258 struct bfd_elf_version_tree *t;
3259 bfd_byte *p;
3260 Elf_Internal_Verdef def;
3261 Elf_Internal_Verdaux defaux;
3262
252b5132
RH
3263 cdefs = 0;
3264 size = 0;
3265
3266 /* Make space for the base version. */
3267 size += sizeof (Elf_External_Verdef);
3268 size += sizeof (Elf_External_Verdaux);
3269 ++cdefs;
3270
3271 for (t = verdefs; t != NULL; t = t->next)
3272 {
3273 struct bfd_elf_version_deps *n;
3274
3275 size += sizeof (Elf_External_Verdef);
3276 size += sizeof (Elf_External_Verdaux);
3277 ++cdefs;
3278
3279 for (n = t->deps; n != NULL; n = n->next)
3280 size += sizeof (Elf_External_Verdaux);
3281 }
3282
3283 s->_raw_size = size;
3284 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3285 if (s->contents == NULL && s->_raw_size != 0)
3286 return false;
3287
3288 /* Fill in the version definition section. */
3289
3290 p = s->contents;
3291
3292 def.vd_version = VER_DEF_CURRENT;
3293 def.vd_flags = VER_FLG_BASE;
3294 def.vd_ndx = 1;
3295 def.vd_cnt = 1;
3296 def.vd_aux = sizeof (Elf_External_Verdef);
3297 def.vd_next = (sizeof (Elf_External_Verdef)
3298 + sizeof (Elf_External_Verdaux));
3299
3300 if (soname_indx != (bfd_size_type) -1)
3301 {
2b0f7ef9
JJ
3302 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3303 soname_indx);
3a99b017 3304 def.vd_hash = bfd_elf_hash (soname);
252b5132
RH
3305 defaux.vda_name = soname_indx;
3306 }
3307 else
3308 {
3309 const char *name;
3310 bfd_size_type indx;
3311
96fd004e 3312 name = basename (output_bfd->filename);
3a99b017 3313 def.vd_hash = bfd_elf_hash (name);
2b0f7ef9
JJ
3314 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3315 name, false);
252b5132
RH
3316 if (indx == (bfd_size_type) -1)
3317 return false;
3318 defaux.vda_name = indx;
3319 }
3320 defaux.vda_next = 0;
3321
3322 _bfd_elf_swap_verdef_out (output_bfd, &def,
a7b97311 3323 (Elf_External_Verdef *) p);
252b5132
RH
3324 p += sizeof (Elf_External_Verdef);
3325 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3326 (Elf_External_Verdaux *) p);
3327 p += sizeof (Elf_External_Verdaux);
3328
3329 for (t = verdefs; t != NULL; t = t->next)
3330 {
3331 unsigned int cdeps;
3332 struct bfd_elf_version_deps *n;
3333 struct elf_link_hash_entry *h;
3334
3335 cdeps = 0;
3336 for (n = t->deps; n != NULL; n = n->next)
3337 ++cdeps;
3338
3339 /* Add a symbol representing this version. */
3340 h = NULL;
3341 if (! (_bfd_generic_link_add_one_symbol
3342 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
3343 (bfd_vma) 0, (const char *) NULL, false,
3344 get_elf_backend_data (dynobj)->collect,
3345 (struct bfd_link_hash_entry **) &h)))
3346 return false;
3347 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
3348 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3349 h->type = STT_OBJECT;
3350 h->verinfo.vertree = t;
3351
3352 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
3353 return false;
3354
3355 def.vd_version = VER_DEF_CURRENT;
3356 def.vd_flags = 0;
3357 if (t->globals == NULL && t->locals == NULL && ! t->used)
3358 def.vd_flags |= VER_FLG_WEAK;
3359 def.vd_ndx = t->vernum + 1;
3360 def.vd_cnt = cdeps + 1;
3a99b017 3361 def.vd_hash = bfd_elf_hash (t->name);
252b5132
RH
3362 def.vd_aux = sizeof (Elf_External_Verdef);
3363 if (t->next != NULL)
3364 def.vd_next = (sizeof (Elf_External_Verdef)
3365 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
3366 else
3367 def.vd_next = 0;
3368
3369 _bfd_elf_swap_verdef_out (output_bfd, &def,
3370 (Elf_External_Verdef *) p);
3371 p += sizeof (Elf_External_Verdef);
3372
3373 defaux.vda_name = h->dynstr_index;
2b0f7ef9
JJ
3374 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3375 h->dynstr_index);
252b5132
RH
3376 if (t->deps == NULL)
3377 defaux.vda_next = 0;
3378 else
3379 defaux.vda_next = sizeof (Elf_External_Verdaux);
3380 t->name_indx = defaux.vda_name;
3381
3382 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3383 (Elf_External_Verdaux *) p);
3384 p += sizeof (Elf_External_Verdaux);
3385
3386 for (n = t->deps; n != NULL; n = n->next)
3387 {
3388 if (n->version_needed == NULL)
3389 {
3390 /* This can happen if there was an error in the
3391 version script. */
3392 defaux.vda_name = 0;
3393 }
3394 else
2b0f7ef9
JJ
3395 {
3396 defaux.vda_name = n->version_needed->name_indx;
3397 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3398 defaux.vda_name);
3399 }
252b5132
RH
3400 if (n->next == NULL)
3401 defaux.vda_next = 0;
3402 else
3403 defaux.vda_next = sizeof (Elf_External_Verdaux);
3404
3405 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3406 (Elf_External_Verdaux *) p);
3407 p += sizeof (Elf_External_Verdaux);
3408 }
3409 }
3410
dc810e39
AM
3411 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
3412 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
3413 (bfd_vma) cdefs))
252b5132
RH
3414 return false;
3415
3416 elf_tdata (output_bfd)->cverdefs = cdefs;
3417 }
3418
13ae64f3 3419 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
d6cf2879 3420 {
dc810e39 3421 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
d6cf2879
L
3422 return false;
3423 }
3424
4d538889 3425 if (info->flags_1)
d6cf2879
L
3426 {
3427 if (! info->shared)
3428 info->flags_1 &= ~ (DF_1_INITFIRST
3429 | DF_1_NODELETE
3430 | DF_1_NOOPEN);
dc810e39
AM
3431 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
3432 info->flags_1))
d6cf2879
L
3433 return false;
3434 }
3435
252b5132
RH
3436 /* Work out the size of the version reference section. */
3437
3438 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3439 BFD_ASSERT (s != NULL);
3440 {
3441 struct elf_find_verdep_info sinfo;
3442
3443 sinfo.output_bfd = output_bfd;
3444 sinfo.info = info;
3445 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
3446 if (sinfo.vers == 0)
3447 sinfo.vers = 1;
3448 sinfo.failed = false;
3449
3450 elf_link_hash_traverse (elf_hash_table (info),
3451 elf_link_find_version_dependencies,
3452 (PTR) &sinfo);
3453
3454 if (elf_tdata (output_bfd)->verref == NULL)
7f8d5fc9 3455 _bfd_strip_section_from_output (info, s);
252b5132
RH
3456 else
3457 {
3458 Elf_Internal_Verneed *t;
3459 unsigned int size;
3460 unsigned int crefs;
3461 bfd_byte *p;
3462
3463 /* Build the version definition section. */
3464 size = 0;
3465 crefs = 0;
3466 for (t = elf_tdata (output_bfd)->verref;
3467 t != NULL;
3468 t = t->vn_nextref)
3469 {
3470 Elf_Internal_Vernaux *a;
3471
3472 size += sizeof (Elf_External_Verneed);
3473 ++crefs;
3474 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3475 size += sizeof (Elf_External_Vernaux);
3476 }
3477
3478 s->_raw_size = size;
dc810e39 3479 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
252b5132
RH
3480 if (s->contents == NULL)
3481 return false;
3482
3483 p = s->contents;
3484 for (t = elf_tdata (output_bfd)->verref;
3485 t != NULL;
3486 t = t->vn_nextref)
3487 {
3488 unsigned int caux;
3489 Elf_Internal_Vernaux *a;
3490 bfd_size_type indx;
3491
3492 caux = 0;
3493 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3494 ++caux;
3495
3496 t->vn_version = VER_NEED_CURRENT;
3497 t->vn_cnt = caux;
2b0f7ef9
JJ
3498 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3499 elf_dt_name (t->vn_bfd) != NULL
3500 ? elf_dt_name (t->vn_bfd)
3501 : basename (t->vn_bfd->filename),
3502 false);
252b5132
RH
3503 if (indx == (bfd_size_type) -1)
3504 return false;
3505 t->vn_file = indx;
3506 t->vn_aux = sizeof (Elf_External_Verneed);
3507 if (t->vn_nextref == NULL)
3508 t->vn_next = 0;
3509 else
3510 t->vn_next = (sizeof (Elf_External_Verneed)
3511 + caux * sizeof (Elf_External_Vernaux));
3512
3513 _bfd_elf_swap_verneed_out (output_bfd, t,
3514 (Elf_External_Verneed *) p);
3515 p += sizeof (Elf_External_Verneed);
3516
3517 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3518 {
3a99b017 3519 a->vna_hash = bfd_elf_hash (a->vna_nodename);
2b0f7ef9
JJ
3520 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3521 a->vna_nodename, false);
252b5132
RH
3522 if (indx == (bfd_size_type) -1)
3523 return false;
3524 a->vna_name = indx;
3525 if (a->vna_nextptr == NULL)
3526 a->vna_next = 0;
3527 else
3528 a->vna_next = sizeof (Elf_External_Vernaux);
3529
3530 _bfd_elf_swap_vernaux_out (output_bfd, a,
3531 (Elf_External_Vernaux *) p);
3532 p += sizeof (Elf_External_Vernaux);
3533 }
3534 }
3535
dc810e39
AM
3536 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
3537 (bfd_vma) 0)
3538 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
3539 (bfd_vma) crefs))
252b5132
RH
3540 return false;
3541
3542 elf_tdata (output_bfd)->cverrefs = crefs;
3543 }
3544 }
3545
3e932841 3546 /* Assign dynsym indicies. In a shared library we generate a
30b30c21
RH
3547 section symbol for each output section, which come first.
3548 Next come all of the back-end allocated local dynamic syms,
3549 followed by the rest of the global symbols. */
3550
3551 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
252b5132
RH
3552
3553 /* Work out the size of the symbol version section. */
3554 s = bfd_get_section_by_name (dynobj, ".gnu.version");
3555 BFD_ASSERT (s != NULL);
3556 if (dynsymcount == 0
3557 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3558 {
7f8d5fc9 3559 _bfd_strip_section_from_output (info, s);
42751cf3
MM
3560 /* The DYNSYMCOUNT might have changed if we were going to
3561 output a dynamic symbol table entry for S. */
30b30c21 3562 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
252b5132
RH
3563 }
3564 else
3565 {
3566 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3567 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3568 if (s->contents == NULL)
3569 return false;
3570
dc810e39 3571 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
252b5132
RH
3572 return false;
3573 }
3574
3575 /* Set the size of the .dynsym and .hash sections. We counted
3576 the number of dynamic symbols in elf_link_add_object_symbols.
3577 We will build the contents of .dynsym and .hash when we build
3578 the final symbol table, because until then we do not know the
3579 correct value to give the symbols. We built the .dynstr
3580 section as we went along in elf_link_add_object_symbols. */
3581 s = bfd_get_section_by_name (dynobj, ".dynsym");
3582 BFD_ASSERT (s != NULL);
3583 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3584 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3585 if (s->contents == NULL && s->_raw_size != 0)
3586 return false;
3587
fc8c40a0
AM
3588 if (dynsymcount != 0)
3589 {
3590 Elf_Internal_Sym isym;
3591
3592 /* The first entry in .dynsym is a dummy symbol. */
3593 isym.st_value = 0;
3594 isym.st_size = 0;
3595 isym.st_name = 0;
3596 isym.st_info = 0;
3597 isym.st_other = 0;
3598 isym.st_shndx = 0;
9ad5cbcf 3599 elf_swap_symbol_out (output_bfd, &isym, (PTR) s->contents, (PTR) 0);
fc8c40a0 3600 }
252b5132
RH
3601
3602 /* Compute the size of the hashing table. As a side effect this
3603 computes the hash values for all the names we export. */
3604 bucketcount = compute_bucket_count (info);
3605
3606 s = bfd_get_section_by_name (dynobj, ".hash");
3607 BFD_ASSERT (s != NULL);
c7ac6ff8
MM
3608 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3609 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
1126897b 3610 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
252b5132
RH
3611 if (s->contents == NULL)
3612 return false;
252b5132 3613
dc810e39
AM
3614 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
3615 s->contents);
3616 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
c7ac6ff8 3617 s->contents + hash_entry_size);
252b5132
RH
3618
3619 elf_hash_table (info)->bucketcount = bucketcount;
3620
3621 s = bfd_get_section_by_name (dynobj, ".dynstr");
3622 BFD_ASSERT (s != NULL);
2b0f7ef9
JJ
3623
3624 elf_finalize_dynstr (output_bfd, info);
3625
3626 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
252b5132 3627
db6751f2 3628 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
dc810e39 3629 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
db6751f2 3630 return false;
252b5132
RH
3631 }
3632
3633 return true;
3634}
3635\f
2b0f7ef9
JJ
3636/* This function is used to adjust offsets into .dynstr for
3637 dynamic symbols. This is called via elf_link_hash_traverse. */
c44233aa 3638
2b0f7ef9
JJ
3639static boolean elf_adjust_dynstr_offsets
3640PARAMS ((struct elf_link_hash_entry *, PTR));
c44233aa 3641
2b0f7ef9
JJ
3642static boolean
3643elf_adjust_dynstr_offsets (h, data)
3644 struct elf_link_hash_entry *h;
3645 PTR data;
3646{
3647 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3648
e92d460e
AM
3649 if (h->root.type == bfd_link_hash_warning)
3650 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3651
2b0f7ef9
JJ
3652 if (h->dynindx != -1)
3653 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3654 return true;
3655}
3656
3657/* Assign string offsets in .dynstr, update all structures referencing
3658 them. */
3659
3660static boolean
3661elf_finalize_dynstr (output_bfd, info)
3662 bfd *output_bfd;
3663 struct bfd_link_info *info;
3664{
3665 struct elf_link_local_dynamic_entry *entry;
3666 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
3667 bfd *dynobj = elf_hash_table (info)->dynobj;
3668 asection *sdyn;
3669 bfd_size_type size;
3670 Elf_External_Dyn *dyncon, *dynconend;
3671
3672 _bfd_elf_strtab_finalize (dynstr);
3673 size = _bfd_elf_strtab_size (dynstr);
3674
3675 /* Update all .dynamic entries referencing .dynstr strings. */
3676 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3677 BFD_ASSERT (sdyn != NULL);
3678
3679 dyncon = (Elf_External_Dyn *) sdyn->contents;
3680 dynconend = (Elf_External_Dyn *) (sdyn->contents +
3681 sdyn->_raw_size);
3682 for (; dyncon < dynconend; dyncon++)
3683 {
3684 Elf_Internal_Dyn dyn;
3685
3686 elf_swap_dyn_in (dynobj, dyncon, & dyn);
3687 switch (dyn.d_tag)
3688 {
3689 case DT_STRSZ:
3690 dyn.d_un.d_val = size;
3691 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3692 break;
3693 case DT_NEEDED:
3694 case DT_SONAME:
3695 case DT_RPATH:
3696 case DT_RUNPATH:
3697 case DT_FILTER:
3698 case DT_AUXILIARY:
3699 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3700 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3701 break;
3702 default:
3703 break;
3704 }
3705 }
3706
3707 /* Now update local dynamic symbols. */
3708 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
3709 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3710 entry->isym.st_name);
3711
3712 /* And the rest of dynamic symbols. */
3713 elf_link_hash_traverse (elf_hash_table (info),
3714 elf_adjust_dynstr_offsets, dynstr);
3715
3716 /* Adjust version definitions. */
3717 if (elf_tdata (output_bfd)->cverdefs)
3718 {
3719 asection *s;
3720 bfd_byte *p;
3721 bfd_size_type i;
3722 Elf_Internal_Verdef def;
3723 Elf_Internal_Verdaux defaux;
c44233aa 3724
2b0f7ef9
JJ
3725 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3726 p = (bfd_byte *) s->contents;
3727 do
3728 {
3729 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3730 &def);
3731 p += sizeof (Elf_External_Verdef);
3732 for (i = 0; i < def.vd_cnt; ++i)
3733 {
3734 _bfd_elf_swap_verdaux_in (output_bfd,
3735 (Elf_External_Verdaux *) p, &defaux);
3736 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3737 defaux.vda_name);
3738 _bfd_elf_swap_verdaux_out (output_bfd,
3739 &defaux, (Elf_External_Verdaux *) p);
3740 p += sizeof (Elf_External_Verdaux);
3741 }
3742 }
3743 while (def.vd_next);
3744 }
3745
3746 /* Adjust version references. */
3747 if (elf_tdata (output_bfd)->verref)
3748 {
3749 asection *s;
3750 bfd_byte *p;
3751 bfd_size_type i;
3752 Elf_Internal_Verneed need;
3753 Elf_Internal_Vernaux needaux;
c44233aa 3754
2b0f7ef9
JJ
3755 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3756 p = (bfd_byte *) s->contents;
3757 do
3758 {
3759 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3760 &need);
3761 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3762 _bfd_elf_swap_verneed_out (output_bfd, &need,
3763 (Elf_External_Verneed *) p);
3764 p += sizeof (Elf_External_Verneed);
3765 for (i = 0; i < need.vn_cnt; ++i)
3766 {
3767 _bfd_elf_swap_vernaux_in (output_bfd,
3768 (Elf_External_Vernaux *) p, &needaux);
3769 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3770 needaux.vna_name);
3771 _bfd_elf_swap_vernaux_out (output_bfd,
3772 &needaux,
3773 (Elf_External_Vernaux *) p);
3774 p += sizeof (Elf_External_Vernaux);
3775 }
3776 }
3777 while (need.vn_next);
3778 }
3779
3780 return true;
3781}
3782
252b5132
RH
3783/* Fix up the flags for a symbol. This handles various cases which
3784 can only be fixed after all the input files are seen. This is
3785 currently called by both adjust_dynamic_symbol and
3786 assign_sym_version, which is unnecessary but perhaps more robust in
3787 the face of future changes. */
3788
3789static boolean
3790elf_fix_symbol_flags (h, eif)
3791 struct elf_link_hash_entry *h;
3792 struct elf_info_failed *eif;
3793{
3794 /* If this symbol was mentioned in a non-ELF file, try to set
3795 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3796 permit a non-ELF file to correctly refer to a symbol defined in
3797 an ELF dynamic object. */
3798 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3799 {
94b6c40a
L
3800 while (h->root.type == bfd_link_hash_indirect)
3801 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3802
252b5132
RH
3803 if (h->root.type != bfd_link_hash_defined
3804 && h->root.type != bfd_link_hash_defweak)
3805 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3806 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3807 else
3808 {
3809 if (h->root.u.def.section->owner != NULL
3810 && (bfd_get_flavour (h->root.u.def.section->owner)
3811 == bfd_target_elf_flavour))
3812 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3813 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3814 else
3815 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3816 }
3817
3818 if (h->dynindx == -1
3819 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3820 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3821 {
3822 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3823 {
3824 eif->failed = true;
3825 return false;
3826 }
3827 }
3828 }
3829 else
3830 {
3831 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
c44233aa
AM
3832 was first seen in a non-ELF file. Fortunately, if the symbol
3833 was first seen in an ELF file, we're probably OK unless the
3834 symbol was defined in a non-ELF file. Catch that case here.
3835 FIXME: We're still in trouble if the symbol was first seen in
3836 a dynamic object, and then later in a non-ELF regular object. */
252b5132
RH
3837 if ((h->root.type == bfd_link_hash_defined
3838 || h->root.type == bfd_link_hash_defweak)
3839 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3840 && (h->root.u.def.section->owner != NULL
3841 ? (bfd_get_flavour (h->root.u.def.section->owner)
3842 != bfd_target_elf_flavour)
3843 : (bfd_is_abs_section (h->root.u.def.section)
3844 && (h->elf_link_hash_flags
3845 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3846 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3847 }
3848
3849 /* If this is a final link, and the symbol was defined as a common
3850 symbol in a regular object file, and there was no definition in
3851 any dynamic object, then the linker will have allocated space for
3852 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3853 flag will not have been set. */
3854 if (h->root.type == bfd_link_hash_defined
3855 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3856 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3857 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3858 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3859 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3860
3861 /* If -Bsymbolic was used (which means to bind references to global
3862 symbols to the definition within the shared object), and this
3863 symbol was defined in a regular object, then it actually doesn't
d954b040
HPN
3864 need a PLT entry, and we can accomplish that by forcing it local.
3865 Likewise, if the symbol has hidden or internal visibility.
3866 FIXME: It might be that we also do not need a PLT for other
3867 non-hidden visibilities, but we would have to tell that to the
3868 backend specifically; we can't just clear PLT-related data here. */
252b5132
RH
3869 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3870 && eif->info->shared
8ea2e4bd 3871 && is_elf_hash_table (eif->info)
d954b040
HPN
3872 && (eif->info->symbolic
3873 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3874 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
252b5132
RH
3875 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3876 {
391a809a 3877 struct elf_backend_data *bed;
e5094212 3878 boolean force_local;
8ea2e4bd 3879
391a809a 3880 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
e5094212
AM
3881
3882 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3883 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3884 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
252b5132
RH
3885 }
3886
fc4cc5bb
ILT
3887 /* If this is a weak defined symbol in a dynamic object, and we know
3888 the real definition in the dynamic object, copy interesting flags
3889 over to the real definition. */
3890 if (h->weakdef != NULL)
3891 {
3892 struct elf_link_hash_entry *weakdef;
3893
3894 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3895 || h->root.type == bfd_link_hash_defweak);
3896 weakdef = h->weakdef;
3897 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
3898 || weakdef->root.type == bfd_link_hash_defweak);
3899 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
3900
3901 /* If the real definition is defined by a regular object file,
3902 don't do anything special. See the longer description in
3903 elf_adjust_dynamic_symbol, below. */
3904 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3905 h->weakdef = NULL;
3906 else
0a991dfe
AM
3907 {
3908 struct elf_backend_data *bed;
3909
3910 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3911 (*bed->elf_backend_copy_indirect_symbol) (weakdef, h);
3912 }
fc4cc5bb
ILT
3913 }
3914
252b5132
RH
3915 return true;
3916}
3917
3918/* Make the backend pick a good value for a dynamic symbol. This is
3919 called via elf_link_hash_traverse, and also calls itself
3920 recursively. */
3921
3922static boolean
3923elf_adjust_dynamic_symbol (h, data)
3924 struct elf_link_hash_entry *h;
3925 PTR data;
3926{
3927 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3928 bfd *dynobj;
3929 struct elf_backend_data *bed;
3930
e92d460e
AM
3931 if (h->root.type == bfd_link_hash_warning)
3932 {
3933 h->plt.offset = (bfd_vma) -1;
3934 h->got.offset = (bfd_vma) -1;
3935
3936 /* When warning symbols are created, they **replace** the "real"
3937 entry in the hash table, thus we never get to see the real
3938 symbol in a hash traversal. So look at it now. */
3939 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3940 }
3941
252b5132
RH
3942 /* Ignore indirect symbols. These are added by the versioning code. */
3943 if (h->root.type == bfd_link_hash_indirect)
3944 return true;
3945
8ea2e4bd
NC
3946 if (! is_elf_hash_table (eif->info))
3947 return false;
3948
252b5132
RH
3949 /* Fix the symbol flags. */
3950 if (! elf_fix_symbol_flags (h, eif))
3951 return false;
3952
3953 /* If this symbol does not require a PLT entry, and it is not
3954 defined by a dynamic object, or is not referenced by a regular
3955 object, ignore it. We do have to handle a weak defined symbol,
3956 even if no regular object refers to it, if we decided to add it
3957 to the dynamic symbol table. FIXME: Do we normally need to worry
3958 about symbols which are defined by one dynamic object and
3959 referenced by another one? */
3960 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
3961 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3962 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3963 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
3964 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
3965 {
3966 h->plt.offset = (bfd_vma) -1;
3967 return true;
3968 }
3969
3970 /* If we've already adjusted this symbol, don't do it again. This
3971 can happen via a recursive call. */
3972 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
3973 return true;
3974
3975 /* Don't look at this symbol again. Note that we must set this
3976 after checking the above conditions, because we may look at a
3977 symbol once, decide not to do anything, and then get called
3978 recursively later after REF_REGULAR is set below. */
3979 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
3980
3981 /* If this is a weak definition, and we know a real definition, and
3982 the real symbol is not itself defined by a regular object file,
3983 then get a good value for the real definition. We handle the
3984 real symbol first, for the convenience of the backend routine.
3985
3986 Note that there is a confusing case here. If the real definition
3987 is defined by a regular object file, we don't get the real symbol
3988 from the dynamic object, but we do get the weak symbol. If the
3989 processor backend uses a COPY reloc, then if some routine in the
3990 dynamic object changes the real symbol, we will not see that
3991 change in the corresponding weak symbol. This is the way other
3992 ELF linkers work as well, and seems to be a result of the shared
3993 library model.
3994
3995 I will clarify this issue. Most SVR4 shared libraries define the
3996 variable _timezone and define timezone as a weak synonym. The
3997 tzset call changes _timezone. If you write
3998 extern int timezone;
3999 int _timezone = 5;
4000 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4001 you might expect that, since timezone is a synonym for _timezone,
4002 the same number will print both times. However, if the processor
4003 backend uses a COPY reloc, then actually timezone will be copied
4004 into your process image, and, since you define _timezone
4005 yourself, _timezone will not. Thus timezone and _timezone will
4006 wind up at different memory locations. The tzset call will set
4007 _timezone, leaving timezone unchanged. */
4008
4009 if (h->weakdef != NULL)
4010 {
fc4cc5bb
ILT
4011 /* If we get to this point, we know there is an implicit
4012 reference by a regular object file via the weak symbol H.
4013 FIXME: Is this really true? What if the traversal finds
4014 H->WEAKDEF before it finds H? */
4015 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
252b5132 4016
fc4cc5bb
ILT
4017 if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
4018 return false;
252b5132
RH
4019 }
4020
4021 /* If a symbol has no type and no size and does not require a PLT
4022 entry, then we are probably about to do the wrong thing here: we
4023 are probably going to create a COPY reloc for an empty object.
4024 This case can arise when a shared object is built with assembly
4025 code, and the assembly code fails to set the symbol type. */
4026 if (h->size == 0
4027 && h->type == STT_NOTYPE
4028 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
4029 (*_bfd_error_handler)
4030 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4031 h->root.root.string);
4032
4033 dynobj = elf_hash_table (eif->info)->dynobj;
4034 bed = get_elf_backend_data (dynobj);
4035 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
4036 {
4037 eif->failed = true;
4038 return false;
4039 }
4040
4041 return true;
4042}
4043\f
4044/* This routine is used to export all defined symbols into the dynamic
4045 symbol table. It is called via elf_link_hash_traverse. */
4046
4047static boolean
4048elf_export_symbol (h, data)
4049 struct elf_link_hash_entry *h;
4050 PTR data;
4051{
4052 struct elf_info_failed *eif = (struct elf_info_failed *) data;
4053
4054 /* Ignore indirect symbols. These are added by the versioning code. */
4055 if (h->root.type == bfd_link_hash_indirect)
4056 return true;
4057
e92d460e
AM
4058 if (h->root.type == bfd_link_hash_warning)
4059 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4060
252b5132
RH
4061 if (h->dynindx == -1
4062 && (h->elf_link_hash_flags
4063 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
4064 {
bc2b6df7
L
4065 struct bfd_elf_version_tree *t;
4066 struct bfd_elf_version_expr *d;
4067
4068 for (t = eif->verdefs; t != NULL; t = t->next)
252b5132 4069 {
bc2b6df7
L
4070 if (t->globals != NULL)
4071 {
4072 for (d = t->globals; d != NULL; d = d->next)
4073 {
4074 if ((*d->match) (d, h->root.root.string))
4075 goto doit;
4076 }
4077 }
4078
4079 if (t->locals != NULL)
4080 {
4081 for (d = t->locals ; d != NULL; d = d->next)
4082 {
4083 if ((*d->match) (d, h->root.root.string))
4084 return true;
4085 }
4086 }
252b5132 4087 }
bc2b6df7
L
4088
4089 if (!eif->verdefs)
c44233aa 4090 {
bc2b6df7
L
4091doit:
4092 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
4093 {
4094 eif->failed = true;
4095 return false;
4096 }
c44233aa 4097 }
252b5132
RH
4098 }
4099
4100 return true;
4101}
4102\f
4103/* Look through the symbols which are defined in other shared
4104 libraries and referenced here. Update the list of version
4105 dependencies. This will be put into the .gnu.version_r section.
4106 This function is called via elf_link_hash_traverse. */
4107
4108static boolean
4109elf_link_find_version_dependencies (h, data)
4110 struct elf_link_hash_entry *h;
4111 PTR data;
4112{
4113 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
4114 Elf_Internal_Verneed *t;
4115 Elf_Internal_Vernaux *a;
dc810e39 4116 bfd_size_type amt;
252b5132 4117
e92d460e
AM
4118 if (h->root.type == bfd_link_hash_warning)
4119 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4120
252b5132
RH
4121 /* We only care about symbols defined in shared objects with version
4122 information. */
4123 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4124 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4125 || h->dynindx == -1
4126 || h->verinfo.verdef == NULL)
4127 return true;
4128
4129 /* See if we already know about this version. */
4130 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
4131 {
4132 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
4133 continue;
4134
4135 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4136 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
4137 return true;
4138
4139 break;
4140 }
4141
4142 /* This is a new version. Add it to tree we are building. */
4143
4144 if (t == NULL)
4145 {
dc810e39
AM
4146 amt = sizeof *t;
4147 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
252b5132
RH
4148 if (t == NULL)
4149 {
4150 rinfo->failed = true;
4151 return false;
4152 }
4153
4154 t->vn_bfd = h->verinfo.verdef->vd_bfd;
4155 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
4156 elf_tdata (rinfo->output_bfd)->verref = t;
4157 }
4158
dc810e39
AM
4159 amt = sizeof *a;
4160 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
252b5132
RH
4161
4162 /* Note that we are copying a string pointer here, and testing it
4163 above. If bfd_elf_string_from_elf_section is ever changed to
4164 discard the string data when low in memory, this will have to be
4165 fixed. */
4166 a->vna_nodename = h->verinfo.verdef->vd_nodename;
4167
4168 a->vna_flags = h->verinfo.verdef->vd_flags;
4169 a->vna_nextptr = t->vn_auxptr;
4170
4171 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
4172 ++rinfo->vers;
4173
4174 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
4175
4176 t->vn_auxptr = a;
4177
4178 return true;
4179}
4180
4181/* Figure out appropriate versions for all the symbols. We may not
4182 have the version number script until we have read all of the input
4183 files, so until that point we don't know which symbols should be
4184 local. This function is called via elf_link_hash_traverse. */
4185
4186static boolean
4187elf_link_assign_sym_version (h, data)
4188 struct elf_link_hash_entry *h;
4189 PTR data;
4190{
dc810e39
AM
4191 struct elf_assign_sym_version_info *sinfo;
4192 struct bfd_link_info *info;
c61b8717 4193 struct elf_backend_data *bed;
252b5132
RH
4194 struct elf_info_failed eif;
4195 char *p;
dc810e39
AM
4196 bfd_size_type amt;
4197
4198 sinfo = (struct elf_assign_sym_version_info *) data;
4199 info = sinfo->info;
252b5132 4200
e92d460e
AM
4201 if (h->root.type == bfd_link_hash_warning)
4202 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4203
252b5132
RH
4204 /* Fix the symbol flags. */
4205 eif.failed = false;
4206 eif.info = info;
4207 if (! elf_fix_symbol_flags (h, &eif))
4208 {
4209 if (eif.failed)
4210 sinfo->failed = true;
4211 return false;
4212 }
4213
4214 /* We only need version numbers for symbols defined in regular
4215 objects. */
4216 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4217 return true;
4218
c61b8717 4219 bed = get_elf_backend_data (sinfo->output_bfd);
252b5132
RH
4220 p = strchr (h->root.root.string, ELF_VER_CHR);
4221 if (p != NULL && h->verinfo.vertree == NULL)
4222 {
4223 struct bfd_elf_version_tree *t;
4224 boolean hidden;
4225
4226 hidden = true;
4227
4228 /* There are two consecutive ELF_VER_CHR characters if this is
c44233aa 4229 not a hidden symbol. */
252b5132
RH
4230 ++p;
4231 if (*p == ELF_VER_CHR)
4232 {
4233 hidden = false;
4234 ++p;
4235 }
4236
4237 /* If there is no version string, we can just return out. */
4238 if (*p == '\0')
4239 {
4240 if (hidden)
4241 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4242 return true;
4243 }
4244
4245 /* Look for the version. If we find it, it is no longer weak. */
4246 for (t = sinfo->verdefs; t != NULL; t = t->next)
4247 {
4248 if (strcmp (t->name, p) == 0)
4249 {
dc810e39 4250 size_t len;
252b5132
RH
4251 char *alc;
4252 struct bfd_elf_version_expr *d;
4253
4254 len = p - h->root.root.string;
e5094212 4255 alc = bfd_malloc ((bfd_size_type) len);
252b5132 4256 if (alc == NULL)
c44233aa 4257 return false;
d4c88bbb 4258 memcpy (alc, h->root.root.string, len - 1);
252b5132
RH
4259 alc[len - 1] = '\0';
4260 if (alc[len - 2] == ELF_VER_CHR)
c44233aa 4261 alc[len - 2] = '\0';
252b5132
RH
4262
4263 h->verinfo.vertree = t;
4264 t->used = true;
4265 d = NULL;
4266
4267 if (t->globals != NULL)
4268 {
4269 for (d = t->globals; d != NULL; d = d->next)
4270 if ((*d->match) (d, alc))
4271 break;
4272 }
4273
4274 /* See if there is anything to force this symbol to
c44233aa 4275 local scope. */
252b5132
RH
4276 if (d == NULL && t->locals != NULL)
4277 {
4278 for (d = t->locals; d != NULL; d = d->next)
4279 {
4280 if ((*d->match) (d, alc))
4281 {
4282 if (h->dynindx != -1
4283 && info->shared
99293407 4284 && ! info->export_dynamic)
252b5132 4285 {
e5094212 4286 (*bed->elf_backend_hide_symbol) (info, h, true);
252b5132
RH
4287 }
4288
4289 break;
4290 }
4291 }
4292 }
4293
e5094212 4294 free (alc);
252b5132
RH
4295 break;
4296 }
4297 }
4298
4299 /* If we are building an application, we need to create a
c44233aa 4300 version node for this version. */
252b5132
RH
4301 if (t == NULL && ! info->shared)
4302 {
4303 struct bfd_elf_version_tree **pp;
4304 int version_index;
4305
4306 /* If we aren't going to export this symbol, we don't need
c44233aa 4307 to worry about it. */
252b5132
RH
4308 if (h->dynindx == -1)
4309 return true;
4310
dc810e39 4311 amt = sizeof *t;
252b5132 4312 t = ((struct bfd_elf_version_tree *)
dc810e39 4313 bfd_alloc (sinfo->output_bfd, amt));
252b5132
RH
4314 if (t == NULL)
4315 {
4316 sinfo->failed = true;
4317 return false;
4318 }
4319
4320 t->next = NULL;
4321 t->name = p;
4322 t->globals = NULL;
4323 t->locals = NULL;
4324 t->deps = NULL;
4325 t->name_indx = (unsigned int) -1;
4326 t->used = true;
4327
4328 version_index = 1;
6b9b879a
JJ
4329 /* Don't count anonymous version tag. */
4330 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
4331 version_index = 0;
252b5132
RH
4332 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
4333 ++version_index;
4334 t->vernum = version_index;
4335
4336 *pp = t;
4337
4338 h->verinfo.vertree = t;
4339 }
4340 else if (t == NULL)
4341 {
4342 /* We could not find the version for a symbol when
c44233aa 4343 generating a shared archive. Return an error. */
252b5132
RH
4344 (*_bfd_error_handler)
4345 (_("%s: undefined versioned symbol name %s"),
4346 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
4347 bfd_set_error (bfd_error_bad_value);
96a94295 4348error_return:
252b5132
RH
4349 sinfo->failed = true;
4350 return false;
4351 }
4352
4353 if (hidden)
4354 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4355 }
4356
4357 /* If we don't have a version for this symbol, see if we can find
4358 something. */
4359 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
4360 {
4361 struct bfd_elf_version_tree *t;
4362 struct bfd_elf_version_tree *deflt;
4363 struct bfd_elf_version_expr *d;
4364
4365 /* See if can find what version this symbol is in. If the
c44233aa
AM
4366 symbol is supposed to be local, then don't actually register
4367 it. */
252b5132
RH
4368 deflt = NULL;
4369 for (t = sinfo->verdefs; t != NULL; t = t->next)
4370 {
4371 if (t->globals != NULL)
4372 {
4373 for (d = t->globals; d != NULL; d = d->next)
4374 {
4375 if ((*d->match) (d, h->root.root.string))
4376 {
4377 h->verinfo.vertree = t;
4378 break;
4379 }
4380 }
4381
4382 if (d != NULL)
4383 break;
4384 }
4385
4386 if (t->locals != NULL)
4387 {
4388 for (d = t->locals; d != NULL; d = d->next)
4389 {
4390 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
4391 deflt = t;
4392 else if ((*d->match) (d, h->root.root.string))
4393 {
4394 h->verinfo.vertree = t;
4395 if (h->dynindx != -1
4396 && info->shared
99293407 4397 && ! info->export_dynamic)
252b5132 4398 {
e5094212 4399 (*bed->elf_backend_hide_symbol) (info, h, true);
252b5132
RH
4400 }
4401 break;
4402 }
4403 }
4404
4405 if (d != NULL)
4406 break;
4407 }
4408 }
4409
4410 if (deflt != NULL && h->verinfo.vertree == NULL)
4411 {
4412 h->verinfo.vertree = deflt;
4413 if (h->dynindx != -1
4414 && info->shared
99293407 4415 && ! info->export_dynamic)
252b5132 4416 {
e5094212 4417 (*bed->elf_backend_hide_symbol) (info, h, true);
252b5132
RH
4418 }
4419 }
96a94295
L
4420
4421 /* We need to check if a hidden versioned definition should
4422 hide the default one. */
4423 if (h->dynindx != -1 && h->verinfo.vertree != NULL)
4424 {
4425 const char *verstr, *name;
4426 size_t namelen, verlen, newlen;
4427 char *newname;
4428 struct elf_link_hash_entry *newh;
4429
4430 name = h->root.root.string;
4431 namelen = strlen (name);
4432 verstr = h->verinfo.vertree->name;
4433 verlen = strlen (verstr);
4434 newlen = namelen + verlen + 2;
4435
4436 newname = (char *) bfd_malloc ((bfd_size_type) newlen);
4437 if (newname == NULL)
4438 goto error_return;
4439 memcpy (newname, name, namelen);
4440
4441 /* Check the hidden versioned definition. */
4442 p = newname + namelen;
4443 *p++ = ELF_VER_CHR;
4444 memcpy (p, verstr, verlen + 1);
4445 newh = elf_link_hash_lookup (elf_hash_table (info), newname,
4446 false, false, false);
4447
4448 if (newh
4449 && (newh->root.type == bfd_link_hash_defined
4450 || newh->root.type == bfd_link_hash_defweak))
4451 /* We find a hidden versioned definition. Hide the default
4452 one. */
4453 (*bed->elf_backend_hide_symbol) (info, h, true);
4454
4455 free (newname);
4456 }
252b5132
RH
4457 }
4458
4459 return true;
4460}
252b5132
RH
4461\f
4462/* Final phase of ELF linker. */
4463
4464/* A structure we use to avoid passing large numbers of arguments. */
4465
4466struct elf_final_link_info
4467{
4468 /* General link information. */
4469 struct bfd_link_info *info;
4470 /* Output BFD. */
4471 bfd *output_bfd;
4472 /* Symbol string table. */
4473 struct bfd_strtab_hash *symstrtab;
4474 /* .dynsym section. */
4475 asection *dynsym_sec;
4476 /* .hash section. */
4477 asection *hash_sec;
4478 /* symbol version section (.gnu.version). */
4479 asection *symver_sec;
13ae64f3
JJ
4480 /* first SHF_TLS section (if any). */
4481 asection *first_tls_sec;
252b5132
RH
4482 /* Buffer large enough to hold contents of any section. */
4483 bfd_byte *contents;
4484 /* Buffer large enough to hold external relocs of any section. */
4485 PTR external_relocs;
4486 /* Buffer large enough to hold internal relocs of any section. */
4487 Elf_Internal_Rela *internal_relocs;
4488 /* Buffer large enough to hold external local symbols of any input
4489 BFD. */
4490 Elf_External_Sym *external_syms;
9ad5cbcf
AM
4491 /* And a buffer for symbol section indices. */
4492 Elf_External_Sym_Shndx *locsym_shndx;
252b5132
RH
4493 /* Buffer large enough to hold internal local symbols of any input
4494 BFD. */
4495 Elf_Internal_Sym *internal_syms;
4496 /* Array large enough to hold a symbol index for each local symbol
4497 of any input BFD. */
4498 long *indices;
4499 /* Array large enough to hold a section pointer for each local
4500 symbol of any input BFD. */
4501 asection **sections;
4502 /* Buffer to hold swapped out symbols. */
4503 Elf_External_Sym *symbuf;
9ad5cbcf
AM
4504 /* And one for symbol section indices. */
4505 Elf_External_Sym_Shndx *symshndxbuf;
252b5132
RH
4506 /* Number of swapped out symbols in buffer. */
4507 size_t symbuf_count;
4508 /* Number of symbols which fit in symbuf. */
4509 size_t symbuf_size;
4510};
4511
4512static boolean elf_link_output_sym
4513 PARAMS ((struct elf_final_link_info *, const char *,
4514 Elf_Internal_Sym *, asection *));
4515static boolean elf_link_flush_output_syms
4516 PARAMS ((struct elf_final_link_info *));
4517static boolean elf_link_output_extsym
4518 PARAMS ((struct elf_link_hash_entry *, PTR));
f5fa8ca2
JJ
4519static boolean elf_link_sec_merge_syms
4520 PARAMS ((struct elf_link_hash_entry *, PTR));
f5d44ba0
AM
4521static boolean elf_link_check_versioned_symbol
4522 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
252b5132
RH
4523static boolean elf_link_input_bfd
4524 PARAMS ((struct elf_final_link_info *, bfd *));
4525static boolean elf_reloc_link_order
4526 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4527 struct bfd_link_order *));
4528
4529/* This struct is used to pass information to elf_link_output_extsym. */
4530
4531struct elf_outext_info
4532{
4533 boolean failed;
4534 boolean localsyms;
4535 struct elf_final_link_info *finfo;
4536};
4537
23bc299b
MM
4538/* Compute the size of, and allocate space for, REL_HDR which is the
4539 section header for a section containing relocations for O. */
4540
4541static boolean
4542elf_link_size_reloc_section (abfd, rel_hdr, o)
4543 bfd *abfd;
4544 Elf_Internal_Shdr *rel_hdr;
4545 asection *o;
4546{
dc810e39
AM
4547 bfd_size_type reloc_count;
4548 bfd_size_type num_rel_hashes;
23bc299b 4549
b037af20
MM
4550 /* Figure out how many relocations there will be. */
4551 if (rel_hdr == &elf_section_data (o)->rel_hdr)
4552 reloc_count = elf_section_data (o)->rel_count;
4553 else
4554 reloc_count = elf_section_data (o)->rel_count2;
4555
9317eacc
CM
4556 num_rel_hashes = o->reloc_count;
4557 if (num_rel_hashes < reloc_count)
4558 num_rel_hashes = reloc_count;
dc810e39 4559
b037af20
MM
4560 /* That allows us to calculate the size of the section. */
4561 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
23bc299b
MM
4562
4563 /* The contents field must last into write_object_contents, so we
755cfd29
NC
4564 allocate it with bfd_alloc rather than malloc. Also since we
4565 cannot be sure that the contents will actually be filled in,
4566 we zero the allocated space. */
4567 rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
23bc299b
MM
4568 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
4569 return false;
3e932841 4570
b037af20
MM
4571 /* We only allocate one set of hash entries, so we only do it the
4572 first time we are called. */
9317eacc
CM
4573 if (elf_section_data (o)->rel_hashes == NULL
4574 && num_rel_hashes)
b037af20 4575 {
209f668e
NC
4576 struct elf_link_hash_entry **p;
4577
b037af20 4578 p = ((struct elf_link_hash_entry **)
9317eacc 4579 bfd_zmalloc (num_rel_hashes
209f668e 4580 * sizeof (struct elf_link_hash_entry *)));
9317eacc 4581 if (p == NULL)
b037af20 4582 return false;
23bc299b 4583
b037af20 4584 elf_section_data (o)->rel_hashes = p;
b037af20 4585 }
23bc299b
MM
4586
4587 return true;
4588}
4589
31367b81
MM
4590/* When performing a relocateable link, the input relocations are
4591 preserved. But, if they reference global symbols, the indices
4592 referenced must be updated. Update all the relocations in
4593 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4594
4595static void
4596elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
4597 bfd *abfd;
4598 Elf_Internal_Shdr *rel_hdr;
4599 unsigned int count;
4600 struct elf_link_hash_entry **rel_hash;
4601{
4602 unsigned int i;
32f0787a 4603 struct elf_backend_data *bed = get_elf_backend_data (abfd);
209f668e
NC
4604 Elf_Internal_Rel *irel;
4605 Elf_Internal_Rela *irela;
dc810e39 4606 bfd_size_type amt = sizeof (Elf_Internal_Rel) * bed->s->int_rels_per_ext_rel;
209f668e 4607
dc810e39 4608 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
209f668e
NC
4609 if (irel == NULL)
4610 {
4611 (*_bfd_error_handler) (_("Error: out of memory"));
4612 abort ();
4613 }
4614
dc810e39
AM
4615 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
4616 irela = (Elf_Internal_Rela *) bfd_zmalloc (amt);
209f668e
NC
4617 if (irela == NULL)
4618 {
4619 (*_bfd_error_handler) (_("Error: out of memory"));
4620 abort ();
4621 }
31367b81
MM
4622
4623 for (i = 0; i < count; i++, rel_hash++)
4624 {
4625 if (*rel_hash == NULL)
4626 continue;
4627
4628 BFD_ASSERT ((*rel_hash)->indx >= 0);
4629
4630 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4631 {
4632 Elf_External_Rel *erel;
209f668e 4633 unsigned int j;
3e932841 4634
31367b81 4635 erel = (Elf_External_Rel *) rel_hdr->contents + i;
32f0787a 4636 if (bed->s->swap_reloc_in)
209f668e 4637 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
32f0787a 4638 else
209f668e
NC
4639 elf_swap_reloc_in (abfd, erel, irel);
4640
4641 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4642 irel[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4643 ELF_R_TYPE (irel[j].r_info));
4644
32f0787a 4645 if (bed->s->swap_reloc_out)
209f668e 4646 (*bed->s->swap_reloc_out) (abfd, irel, (bfd_byte *) erel);
32f0787a 4647 else
209f668e 4648 elf_swap_reloc_out (abfd, irel, erel);
31367b81
MM
4649 }
4650 else
4651 {
4652 Elf_External_Rela *erela;
209f668e 4653 unsigned int j;
3e932841 4654
31367b81
MM
4655 BFD_ASSERT (rel_hdr->sh_entsize
4656 == sizeof (Elf_External_Rela));
3e932841 4657
31367b81 4658 erela = (Elf_External_Rela *) rel_hdr->contents + i;
32f0787a 4659 if (bed->s->swap_reloca_in)
209f668e 4660 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
32f0787a 4661 else
209f668e
NC
4662 elf_swap_reloca_in (abfd, erela, irela);
4663
4664 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4665 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4666 ELF_R_TYPE (irela[j].r_info));
4667
32f0787a 4668 if (bed->s->swap_reloca_out)
209f668e 4669 (*bed->s->swap_reloca_out) (abfd, irela, (bfd_byte *) erela);
32f0787a 4670 else
209f668e 4671 elf_swap_reloca_out (abfd, irela, erela);
31367b81
MM
4672 }
4673 }
209f668e
NC
4674
4675 free (irel);
4676 free (irela);
31367b81
MM
4677}
4678
db6751f2
JJ
4679struct elf_link_sort_rela {
4680 bfd_vma offset;
4681 enum elf_reloc_type_class type;
4682 union {
4683 Elf_Internal_Rel rel;
4684 Elf_Internal_Rela rela;
4685 } u;
4686};
4687
4688static int
4689elf_link_sort_cmp1 (A, B)
4690 const PTR A;
4691 const PTR B;
4692{
f51e552e
AM
4693 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4694 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
db6751f2
JJ
4695 int relativea, relativeb;
4696
4697 relativea = a->type == reloc_class_relative;
4698 relativeb = b->type == reloc_class_relative;
4699
4700 if (relativea < relativeb)
db6751f2 4701 return 1;
fcfbdf31
JJ
4702 if (relativea > relativeb)
4703 return -1;
db6751f2
JJ
4704 if (ELF_R_SYM (a->u.rel.r_info) < ELF_R_SYM (b->u.rel.r_info))
4705 return -1;
4706 if (ELF_R_SYM (a->u.rel.r_info) > ELF_R_SYM (b->u.rel.r_info))
4707 return 1;
4708 if (a->u.rel.r_offset < b->u.rel.r_offset)
4709 return -1;
4710 if (a->u.rel.r_offset > b->u.rel.r_offset)
4711 return 1;
4712 return 0;
4713}
4714
4715static int
4716elf_link_sort_cmp2 (A, B)
4717 const PTR A;
4718 const PTR B;
4719{
f51e552e
AM
4720 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4721 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
db6751f2
JJ
4722 int copya, copyb;
4723
4724 if (a->offset < b->offset)
4725 return -1;
4726 if (a->offset > b->offset)
4727 return 1;
290394d6
JJ
4728 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
4729 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
db6751f2
JJ
4730 if (copya < copyb)
4731 return -1;
4732 if (copya > copyb)
4733 return 1;
4734 if (a->u.rel.r_offset < b->u.rel.r_offset)
4735 return -1;
4736 if (a->u.rel.r_offset > b->u.rel.r_offset)
4737 return 1;
4738 return 0;
4739}
4740
4741static size_t
4742elf_link_sort_relocs (abfd, info, psec)
4743 bfd *abfd;
4744 struct bfd_link_info *info;
4745 asection **psec;
4746{
4747 bfd *dynobj = elf_hash_table (info)->dynobj;
4748 asection *reldyn, *o;
4749 boolean rel = false;
f51e552e
AM
4750 bfd_size_type count, size;
4751 size_t i, j, ret;
db6751f2
JJ
4752 struct elf_link_sort_rela *rela;
4753 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4754
4755 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
4756 if (reldyn == NULL || reldyn->_raw_size == 0)
4757 {
4758 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
4759 if (reldyn == NULL || reldyn->_raw_size == 0)
4760 return 0;
4761 rel = true;
4762 count = reldyn->_raw_size / sizeof (Elf_External_Rel);
4763 }
4764 else
4765 count = reldyn->_raw_size / sizeof (Elf_External_Rela);
4766
4767 size = 0;
4768 for (o = dynobj->sections; o != NULL; o = o->next)
4769 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4770 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4771 && o->output_section == reldyn)
4772 size += o->_raw_size;
4773
4774 if (size != reldyn->_raw_size)
4775 return 0;
4776
f51e552e 4777 rela = (struct elf_link_sort_rela *) bfd_zmalloc (sizeof (*rela) * count);
db6751f2
JJ
4778 if (rela == NULL)
4779 {
4780 (*info->callbacks->warning)
dc810e39
AM
4781 (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
4782 (bfd_vma) 0);
db6751f2
JJ
4783 return 0;
4784 }
4785
4786 for (o = dynobj->sections; o != NULL; o = o->next)
4787 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4788 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4789 && o->output_section == reldyn)
4790 {
4791 if (rel)
4792 {
4793 Elf_External_Rel *erel, *erelend;
4794 struct elf_link_sort_rela *s;
4795
4796 erel = (Elf_External_Rel *) o->contents;
f51e552e 4797 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
db6751f2
JJ
4798 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4799 for (; erel < erelend; erel++, s++)
4800 {
4801 if (bed->s->swap_reloc_in)
4802 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, &s->u.rel);
4803 else
4804 elf_swap_reloc_in (abfd, erel, &s->u.rel);
4805
f51e552e 4806 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
dc810e39 4807 }
db6751f2
JJ
4808 }
4809 else
4810 {
4811 Elf_External_Rela *erela, *erelaend;
4812 struct elf_link_sort_rela *s;
4813
4814 erela = (Elf_External_Rela *) o->contents;
f51e552e 4815 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
db6751f2
JJ
4816 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4817 for (; erela < erelaend; erela++, s++)
4818 {
4819 if (bed->s->swap_reloca_in)
dc810e39
AM
4820 (*bed->s->swap_reloca_in) (dynobj, (bfd_byte *) erela,
4821 &s->u.rela);
db6751f2
JJ
4822 else
4823 elf_swap_reloca_in (dynobj, erela, &s->u.rela);
4824
f51e552e 4825 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
dc810e39 4826 }
db6751f2
JJ
4827 }
4828 }
4829
973ffd63 4830 qsort (rela, (size_t) count, sizeof (*rela), elf_link_sort_cmp1);
fcfbdf31
JJ
4831 for (ret = 0; ret < count && rela[ret].type == reloc_class_relative; ret++)
4832 ;
4833 for (i = ret, j = ret; i < count; i++)
db6751f2
JJ
4834 {
4835 if (ELF_R_SYM (rela[i].u.rel.r_info) != ELF_R_SYM (rela[j].u.rel.r_info))
4836 j = i;
4837 rela[i].offset = rela[j].u.rel.r_offset;
4838 }
973ffd63 4839 qsort (rela + ret, (size_t) count - ret, sizeof (*rela), elf_link_sort_cmp2);
dc810e39 4840
db6751f2
JJ
4841 for (o = dynobj->sections; o != NULL; o = o->next)
4842 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4843 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4844 && o->output_section == reldyn)
4845 {
4846 if (rel)
4847 {
4848 Elf_External_Rel *erel, *erelend;
4849 struct elf_link_sort_rela *s;
4850
4851 erel = (Elf_External_Rel *) o->contents;
df22989b 4852 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
db6751f2
JJ
4853 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4854 for (; erel < erelend; erel++, s++)
4855 {
4856 if (bed->s->swap_reloc_out)
dc810e39
AM
4857 (*bed->s->swap_reloc_out) (abfd, &s->u.rel,
4858 (bfd_byte *) erel);
db6751f2
JJ
4859 else
4860 elf_swap_reloc_out (abfd, &s->u.rel, erel);
4861 }
4862 }
4863 else
4864 {
4865 Elf_External_Rela *erela, *erelaend;
4866 struct elf_link_sort_rela *s;
4867
4868 erela = (Elf_External_Rela *) o->contents;
df22989b 4869 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
db6751f2
JJ
4870 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4871 for (; erela < erelaend; erela++, s++)
4872 {
4873 if (bed->s->swap_reloca_out)
dc810e39
AM
4874 (*bed->s->swap_reloca_out) (dynobj, &s->u.rela,
4875 (bfd_byte *) erela);
db6751f2
JJ
4876 else
4877 elf_swap_reloca_out (dynobj, &s->u.rela, erela);
dc810e39 4878 }
db6751f2
JJ
4879 }
4880 }
4881
4882 free (rela);
4883 *psec = reldyn;
4884 return ret;
4885}
4886
252b5132
RH
4887/* Do the final step of an ELF link. */
4888
4889boolean
4890elf_bfd_final_link (abfd, info)
4891 bfd *abfd;
4892 struct bfd_link_info *info;
4893{
4894 boolean dynamic;
9317eacc 4895 boolean emit_relocs;
252b5132
RH
4896 bfd *dynobj;
4897 struct elf_final_link_info finfo;
4898 register asection *o;
4899 register struct bfd_link_order *p;
4900 register bfd *sub;
dc810e39
AM
4901 bfd_size_type max_contents_size;
4902 bfd_size_type max_external_reloc_size;
4903 bfd_size_type max_internal_reloc_count;
4904 bfd_size_type max_sym_count;
9ad5cbcf 4905 bfd_size_type max_sym_shndx_count;
252b5132
RH
4906 file_ptr off;
4907 Elf_Internal_Sym elfsym;
4908 unsigned int i;
4909 Elf_Internal_Shdr *symtab_hdr;
4910 Elf_Internal_Shdr *symstrtab_hdr;
4911 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4912 struct elf_outext_info eoinfo;
f5fa8ca2 4913 boolean merged;
db6751f2
JJ
4914 size_t relativecount = 0;
4915 asection *reldyn = 0;
dc810e39 4916 bfd_size_type amt;
252b5132 4917
8ea2e4bd
NC
4918 if (! is_elf_hash_table (info))
4919 return false;
4920
252b5132
RH
4921 if (info->shared)
4922 abfd->flags |= DYNAMIC;
4923
4924 dynamic = elf_hash_table (info)->dynamic_sections_created;
4925 dynobj = elf_hash_table (info)->dynobj;
4926
9317eacc 4927 emit_relocs = (info->relocateable
c44233aa
AM
4928 || info->emitrelocations
4929 || bed->elf_backend_emit_relocs);
9317eacc 4930
252b5132
RH
4931 finfo.info = info;
4932 finfo.output_bfd = abfd;
4933 finfo.symstrtab = elf_stringtab_init ();
4934 if (finfo.symstrtab == NULL)
4935 return false;
4936
4937 if (! dynamic)
4938 {
4939 finfo.dynsym_sec = NULL;
4940 finfo.hash_sec = NULL;
4941 finfo.symver_sec = NULL;
4942 }
4943 else
4944 {
4945 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
4946 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
4947 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
4948 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
4949 /* Note that it is OK if symver_sec is NULL. */
4950 }
4951
4952 finfo.contents = NULL;
4953 finfo.external_relocs = NULL;
4954 finfo.internal_relocs = NULL;
4955 finfo.external_syms = NULL;
9ad5cbcf 4956 finfo.locsym_shndx = NULL;
252b5132
RH
4957 finfo.internal_syms = NULL;
4958 finfo.indices = NULL;
4959 finfo.sections = NULL;
4960 finfo.symbuf = NULL;
9ad5cbcf 4961 finfo.symshndxbuf = NULL;
252b5132 4962 finfo.symbuf_count = 0;
13ae64f3
JJ
4963 finfo.first_tls_sec = NULL;
4964 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4965 if ((o->flags & SEC_THREAD_LOCAL) != 0
4966 && (o->flags & SEC_LOAD) != 0)
4967 {
4968 finfo.first_tls_sec = o;
4969 break;
4970 }
252b5132
RH
4971
4972 /* Count up the number of relocations we will output for each output
4973 section, so that we know the sizes of the reloc sections. We
4974 also figure out some maximum sizes. */
4975 max_contents_size = 0;
4976 max_external_reloc_size = 0;
4977 max_internal_reloc_count = 0;
4978 max_sym_count = 0;
9ad5cbcf 4979 max_sym_shndx_count = 0;
f5fa8ca2 4980 merged = false;
252b5132
RH
4981 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4982 {
4983 o->reloc_count = 0;
4984
4985 for (p = o->link_order_head; p != NULL; p = p->next)
4986 {
4987 if (p->type == bfd_section_reloc_link_order
4988 || p->type == bfd_symbol_reloc_link_order)
4989 ++o->reloc_count;
4990 else if (p->type == bfd_indirect_link_order)
4991 {
4992 asection *sec;
4993
4994 sec = p->u.indirect.section;
4995
4996 /* Mark all sections which are to be included in the
4997 link. This will normally be every section. We need
4998 to do this so that we can identify any sections which
4999 the linker has decided to not include. */
5000 sec->linker_mark = true;
5001
f5fa8ca2
JJ
5002 if (sec->flags & SEC_MERGE)
5003 merged = true;
5004
a712da20 5005 if (info->relocateable || info->emitrelocations)
252b5132 5006 o->reloc_count += sec->reloc_count;
c44233aa 5007 else if (bed->elf_backend_count_relocs)
9317eacc
CM
5008 {
5009 Elf_Internal_Rela * relocs;
5010
5011 relocs = (NAME(_bfd_elf,link_read_relocs)
5012 (abfd, sec, (PTR) NULL,
5013 (Elf_Internal_Rela *) NULL, info->keep_memory));
5014
c44233aa
AM
5015 o->reloc_count
5016 += (*bed->elf_backend_count_relocs) (sec, relocs);
9317eacc 5017
6cdc0ccc 5018 if (elf_section_data (o)->relocs != relocs)
9317eacc
CM
5019 free (relocs);
5020 }
252b5132
RH
5021
5022 if (sec->_raw_size > max_contents_size)
5023 max_contents_size = sec->_raw_size;
5024 if (sec->_cooked_size > max_contents_size)
5025 max_contents_size = sec->_cooked_size;
5026
5027 /* We are interested in just local symbols, not all
5028 symbols. */
5029 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
5030 && (sec->owner->flags & DYNAMIC) == 0)
5031 {
5032 size_t sym_count;
5033
5034 if (elf_bad_symtab (sec->owner))
5035 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
5036 / sizeof (Elf_External_Sym));
5037 else
5038 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
5039
5040 if (sym_count > max_sym_count)
5041 max_sym_count = sym_count;
5042
9ad5cbcf
AM
5043 if (sym_count > max_sym_shndx_count
5044 && elf_symtab_shndx (sec->owner) != 0)
5045 max_sym_shndx_count = sym_count;
5046
252b5132
RH
5047 if ((sec->flags & SEC_RELOC) != 0)
5048 {
5049 size_t ext_size;
5050
5051 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
5052 if (ext_size > max_external_reloc_size)
5053 max_external_reloc_size = ext_size;
5054 if (sec->reloc_count > max_internal_reloc_count)
5055 max_internal_reloc_count = sec->reloc_count;
5056 }
5057 }
5058 }
5059 }
5060
5061 if (o->reloc_count > 0)
5062 o->flags |= SEC_RELOC;
5063 else
5064 {
5065 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5066 set it (this is probably a bug) and if it is set
5067 assign_section_numbers will create a reloc section. */
5068 o->flags &=~ SEC_RELOC;
5069 }
5070
5071 /* If the SEC_ALLOC flag is not set, force the section VMA to
5072 zero. This is done in elf_fake_sections as well, but forcing
5073 the VMA to 0 here will ensure that relocs against these
5074 sections are handled correctly. */
5075 if ((o->flags & SEC_ALLOC) == 0
5076 && ! o->user_set_vma)
5077 o->vma = 0;
5078 }
5079
f5fa8ca2
JJ
5080 if (! info->relocateable && merged)
5081 elf_link_hash_traverse (elf_hash_table (info),
5082 elf_link_sec_merge_syms, (PTR) abfd);
5083
252b5132
RH
5084 /* Figure out the file positions for everything but the symbol table
5085 and the relocs. We set symcount to force assign_section_numbers
5086 to create a symbol table. */
5087 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
5088 BFD_ASSERT (! abfd->output_has_begun);
5089 if (! _bfd_elf_compute_section_file_positions (abfd, info))
5090 goto error_return;
5091
b037af20
MM
5092 /* Figure out how many relocations we will have in each section.
5093 Just using RELOC_COUNT isn't good enough since that doesn't
5094 maintain a separate value for REL vs. RELA relocations. */
9317eacc 5095 if (emit_relocs)
b037af20
MM
5096 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5097 for (o = sub->sections; o != NULL; o = o->next)
5098 {
814fe68a 5099 asection *output_section;
b037af20 5100
814fe68a
ILT
5101 if (! o->linker_mark)
5102 {
5103 /* This section was omitted from the link. */
5104 continue;
5105 }
5106
5107 output_section = o->output_section;
5108
5109 if (output_section != NULL
5110 && (o->flags & SEC_RELOC) != 0)
b037af20 5111 {
3e932841 5112 struct bfd_elf_section_data *esdi
b037af20 5113 = elf_section_data (o);
3e932841 5114 struct bfd_elf_section_data *esdo
b037af20 5115 = elf_section_data (output_section);
ce006217
MM
5116 unsigned int *rel_count;
5117 unsigned int *rel_count2;
c119f9b8
AM
5118 bfd_size_type entsize;
5119 bfd_size_type entsize2;
b037af20 5120
c119f9b8 5121 /* We must be careful to add the relocations from the
ce006217 5122 input section to the right output count. */
c119f9b8
AM
5123 entsize = esdi->rel_hdr.sh_entsize;
5124 entsize2 = esdi->rel_hdr2 ? esdi->rel_hdr2->sh_entsize : 0;
5125 BFD_ASSERT ((entsize == sizeof (Elf_External_Rel)
5126 || entsize == sizeof (Elf_External_Rela))
5127 && entsize2 != entsize
5128 && (entsize2 == 0
5129 || entsize2 == sizeof (Elf_External_Rel)
5130 || entsize2 == sizeof (Elf_External_Rela)));
5131 if (entsize == esdo->rel_hdr.sh_entsize)
ce006217
MM
5132 {
5133 rel_count = &esdo->rel_count;
5134 rel_count2 = &esdo->rel_count2;
5135 }
5136 else
5137 {
5138 rel_count = &esdo->rel_count2;
5139 rel_count2 = &esdo->rel_count;
5140 }
3e932841 5141
d9bc7a44 5142 *rel_count += NUM_SHDR_ENTRIES (& esdi->rel_hdr);
b037af20 5143 if (esdi->rel_hdr2)
d9bc7a44 5144 *rel_count2 += NUM_SHDR_ENTRIES (esdi->rel_hdr2);
9317eacc 5145 output_section->flags |= SEC_RELOC;
b037af20
MM
5146 }
5147 }
5148
252b5132
RH
5149 /* That created the reloc sections. Set their sizes, and assign
5150 them file positions, and allocate some buffers. */
5151 for (o = abfd->sections; o != NULL; o = o->next)
5152 {
5153 if ((o->flags & SEC_RELOC) != 0)
5154 {
23bc299b
MM
5155 if (!elf_link_size_reloc_section (abfd,
5156 &elf_section_data (o)->rel_hdr,
5157 o))
252b5132
RH
5158 goto error_return;
5159
23bc299b
MM
5160 if (elf_section_data (o)->rel_hdr2
5161 && !elf_link_size_reloc_section (abfd,
5162 elf_section_data (o)->rel_hdr2,
5163 o))
252b5132 5164 goto error_return;
252b5132 5165 }
b037af20
MM
5166
5167 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
3e932841 5168 to count upwards while actually outputting the relocations. */
b037af20
MM
5169 elf_section_data (o)->rel_count = 0;
5170 elf_section_data (o)->rel_count2 = 0;
252b5132
RH
5171 }
5172
5173 _bfd_elf_assign_file_positions_for_relocs (abfd);
5174
5175 /* We have now assigned file positions for all the sections except
5176 .symtab and .strtab. We start the .symtab section at the current
5177 file position, and write directly to it. We build the .strtab
5178 section in memory. */
5179 bfd_get_symcount (abfd) = 0;
5180 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5181 /* sh_name is set in prep_headers. */
5182 symtab_hdr->sh_type = SHT_SYMTAB;
5183 symtab_hdr->sh_flags = 0;
5184 symtab_hdr->sh_addr = 0;
5185 symtab_hdr->sh_size = 0;
5186 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
5187 /* sh_link is set in assign_section_numbers. */
5188 /* sh_info is set below. */
5189 /* sh_offset is set just below. */
f0e1d18a 5190 symtab_hdr->sh_addralign = bed->s->file_align;
252b5132
RH
5191
5192 off = elf_tdata (abfd)->next_file_pos;
5193 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
5194
5195 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5196 incorrect. We do not yet know the size of the .symtab section.
5197 We correct next_file_pos below, after we do know the size. */
5198
5199 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5200 continuously seeking to the right position in the file. */
5201 if (! info->keep_memory || max_sym_count < 20)
5202 finfo.symbuf_size = 20;
5203 else
5204 finfo.symbuf_size = max_sym_count;
dc810e39
AM
5205 amt = finfo.symbuf_size;
5206 amt *= sizeof (Elf_External_Sym);
5207 finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
252b5132
RH
5208 if (finfo.symbuf == NULL)
5209 goto error_return;
9ad5cbcf
AM
5210 if (elf_numsections (abfd) > SHN_LORESERVE)
5211 {
5212 amt = finfo.symbuf_size;
5213 amt *= sizeof (Elf_External_Sym_Shndx);
5214 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5215 if (finfo.symshndxbuf == NULL)
5216 goto error_return;
5217 }
252b5132
RH
5218
5219 /* Start writing out the symbol table. The first symbol is always a
5220 dummy symbol. */
9317eacc
CM
5221 if (info->strip != strip_all
5222 || emit_relocs)
252b5132
RH
5223 {
5224 elfsym.st_value = 0;
5225 elfsym.st_size = 0;
5226 elfsym.st_info = 0;
5227 elfsym.st_other = 0;
5228 elfsym.st_shndx = SHN_UNDEF;
5229 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5230 &elfsym, bfd_und_section_ptr))
5231 goto error_return;
5232 }
5233
5234#if 0
5235 /* Some standard ELF linkers do this, but we don't because it causes
5236 bootstrap comparison failures. */
5237 /* Output a file symbol for the output file as the second symbol.
5238 We output this even if we are discarding local symbols, although
5239 I'm not sure if this is correct. */
5240 elfsym.st_value = 0;
5241 elfsym.st_size = 0;
5242 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5243 elfsym.st_other = 0;
5244 elfsym.st_shndx = SHN_ABS;
5245 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
5246 &elfsym, bfd_abs_section_ptr))
5247 goto error_return;
5248#endif
5249
5250 /* Output a symbol for each section. We output these even if we are
5251 discarding local symbols, since they are used for relocs. These
5252 symbols have no names. We store the index of each one in the
5253 index field of the section, so that we can find it again when
5254 outputting relocs. */
9317eacc
CM
5255 if (info->strip != strip_all
5256 || emit_relocs)
252b5132
RH
5257 {
5258 elfsym.st_size = 0;
5259 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5260 elfsym.st_other = 0;
9ad5cbcf 5261 for (i = 1; i < elf_numsections (abfd); i++)
252b5132
RH
5262 {
5263 o = section_from_elf_index (abfd, i);
5264 if (o != NULL)
5265 o->target_index = bfd_get_symcount (abfd);
5266 elfsym.st_shndx = i;
7ad34365 5267 if (info->relocateable || o == NULL)
252b5132
RH
5268 elfsym.st_value = 0;
5269 else
5270 elfsym.st_value = o->vma;
5271 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5272 &elfsym, o))
5273 goto error_return;
9ad5cbcf
AM
5274 if (i == SHN_LORESERVE)
5275 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132
RH
5276 }
5277 }
5278
5279 /* Allocate some memory to hold information read in from the input
5280 files. */
9ad5cbcf
AM
5281 if (max_contents_size != 0)
5282 {
5283 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
5284 if (finfo.contents == NULL)
5285 goto error_return;
5286 }
5287
5288 if (max_external_reloc_size != 0)
5289 {
5290 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
5291 if (finfo.external_relocs == NULL)
5292 goto error_return;
5293 }
5294
5295 if (max_internal_reloc_count != 0)
5296 {
5297 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
5298 amt *= sizeof (Elf_Internal_Rela);
5299 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
5300 if (finfo.internal_relocs == NULL)
5301 goto error_return;
5302 }
5303
5304 if (max_sym_count != 0)
5305 {
5306 amt = max_sym_count * sizeof (Elf_External_Sym);
5307 finfo.external_syms = (Elf_External_Sym *) bfd_malloc (amt);
5308 if (finfo.external_syms == NULL)
5309 goto error_return;
5310
5311 amt = max_sym_count * sizeof (Elf_Internal_Sym);
5312 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
5313 if (finfo.internal_syms == NULL)
5314 goto error_return;
5315
5316 amt = max_sym_count * sizeof (long);
5317 finfo.indices = (long *) bfd_malloc (amt);
5318 if (finfo.indices == NULL)
5319 goto error_return;
5320
5321 amt = max_sym_count * sizeof (asection *);
5322 finfo.sections = (asection **) bfd_malloc (amt);
5323 if (finfo.sections == NULL)
5324 goto error_return;
5325 }
5326
5327 if (max_sym_shndx_count != 0)
5328 {
5329 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
5330 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5331 if (finfo.locsym_shndx == NULL)
5332 goto error_return;
5333 }
252b5132 5334
13ae64f3
JJ
5335 if (finfo.first_tls_sec)
5336 {
5337 unsigned int align = 0;
5338 bfd_vma base = finfo.first_tls_sec->vma, end = 0;
5339 asection *sec;
5340
5341 for (sec = finfo.first_tls_sec;
5342 sec && (sec->flags & SEC_THREAD_LOCAL);
5343 sec = sec->next)
5344 {
5345 bfd_vma size = sec->_raw_size;
5346
5347 if (bfd_get_section_alignment (abfd, sec) > align)
5348 align = bfd_get_section_alignment (abfd, sec);
5349 if (sec->_raw_size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
5350 {
5351 struct bfd_link_order *o;
5352
5353 size = 0;
5354 for (o = sec->link_order_head; o != NULL; o = o->next)
5355 if (size < o->offset + o->size)
5356 size = o->offset + o->size;
5357 }
5358 end = sec->vma + size;
5359 }
5360 elf_hash_table (info)->tls_segment
5361 = bfd_zalloc (abfd, sizeof (struct elf_link_tls_segment));
5362 if (elf_hash_table (info)->tls_segment == NULL)
5363 goto error_return;
5364 elf_hash_table (info)->tls_segment->start = base;
5365 elf_hash_table (info)->tls_segment->size = end - base;
5366 elf_hash_table (info)->tls_segment->align = align;
5367 }
5368
252b5132
RH
5369 /* Since ELF permits relocations to be against local symbols, we
5370 must have the local symbols available when we do the relocations.
5371 Since we would rather only read the local symbols once, and we
5372 would rather not keep them in memory, we handle all the
5373 relocations for a single input file at the same time.
5374
5375 Unfortunately, there is no way to know the total number of local
5376 symbols until we have seen all of them, and the local symbol
5377 indices precede the global symbol indices. This means that when
5378 we are generating relocateable output, and we see a reloc against
5379 a global symbol, we can not know the symbol index until we have
5380 finished examining all the local symbols to see which ones we are
5381 going to output. To deal with this, we keep the relocations in
5382 memory, and don't output them until the end of the link. This is
5383 an unfortunate waste of memory, but I don't see a good way around
5384 it. Fortunately, it only happens when performing a relocateable
5385 link, which is not the common case. FIXME: If keep_memory is set
5386 we could write the relocs out and then read them again; I don't
5387 know how bad the memory loss will be. */
5388
5389 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5390 sub->output_has_begun = false;
5391 for (o = abfd->sections; o != NULL; o = o->next)
5392 {
5393 for (p = o->link_order_head; p != NULL; p = p->next)
5394 {
5395 if (p->type == bfd_indirect_link_order
a50c1845 5396 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
4ddafab0 5397 == bfd_target_elf_flavour)
a50c1845 5398 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
252b5132 5399 {
252b5132
RH
5400 if (! sub->output_has_begun)
5401 {
5402 if (! elf_link_input_bfd (&finfo, sub))
5403 goto error_return;
5404 sub->output_has_begun = true;
5405 }
5406 }
5407 else if (p->type == bfd_section_reloc_link_order
5408 || p->type == bfd_symbol_reloc_link_order)
5409 {
5410 if (! elf_reloc_link_order (abfd, info, o, p))
5411 goto error_return;
5412 }
5413 else
5414 {
5415 if (! _bfd_default_link_order (abfd, info, o, p))
5416 goto error_return;
5417 }
5418 }
5419 }
5420
c44233aa
AM
5421 /* Output any global symbols that got converted to local in a
5422 version script or due to symbol visibility. We do this in a
5423 separate step since ELF requires all local symbols to appear
5424 prior to any global symbols. FIXME: We should only do this if
5425 some global symbols were, in fact, converted to become local.
5426 FIXME: Will this work correctly with the Irix 5 linker? */
5427 eoinfo.failed = false;
5428 eoinfo.finfo = &finfo;
5429 eoinfo.localsyms = true;
5430 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5431 (PTR) &eoinfo);
5432 if (eoinfo.failed)
5433 return false;
5434
252b5132 5435 /* That wrote out all the local symbols. Finish up the symbol table
5cc7c785
L
5436 with the global symbols. Even if we want to strip everything we
5437 can, we still need to deal with those global symbols that got
3e932841 5438 converted to local in a version script. */
252b5132 5439
30b30c21 5440 /* The sh_info field records the index of the first non local symbol. */
252b5132 5441 symtab_hdr->sh_info = bfd_get_symcount (abfd);
30b30c21 5442
fc8c40a0
AM
5443 if (dynamic
5444 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
30b30c21
RH
5445 {
5446 Elf_Internal_Sym sym;
5447 Elf_External_Sym *dynsym =
a7b97311 5448 (Elf_External_Sym *) finfo.dynsym_sec->contents;
71a40b32 5449 long last_local = 0;
30b30c21
RH
5450
5451 /* Write out the section symbols for the output sections. */
5452 if (info->shared)
5453 {
5454 asection *s;
5455
5456 sym.st_size = 0;
5457 sym.st_name = 0;
5458 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5459 sym.st_other = 0;
5460
5461 for (s = abfd->sections; s != NULL; s = s->next)
5462 {
5463 int indx;
9ad5cbcf
AM
5464 Elf_External_Sym *dest;
5465
30b30c21
RH
5466 indx = elf_section_data (s)->this_idx;
5467 BFD_ASSERT (indx > 0);
5468 sym.st_shndx = indx;
5469 sym.st_value = s->vma;
9ad5cbcf
AM
5470 dest = dynsym + elf_section_data (s)->dynindx;
5471 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
30b30c21
RH
5472 }
5473
5474 last_local = bfd_count_sections (abfd);
5475 }
5476
5477 /* Write out the local dynsyms. */
5478 if (elf_hash_table (info)->dynlocal)
5479 {
5480 struct elf_link_local_dynamic_entry *e;
5481 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
5482 {
318da145 5483 asection *s;
9ad5cbcf 5484 Elf_External_Sym *dest;
30b30c21 5485
b037af20
MM
5486 sym.st_size = e->isym.st_size;
5487 sym.st_other = e->isym.st_other;
5488
1fa0ddb3
RH
5489 /* Copy the internal symbol as is.
5490 Note that we saved a word of storage and overwrote
c44233aa
AM
5491 the original st_name with the dynstr_index. */
5492 sym = e->isym;
30b30c21 5493
c8e5ddc8
AM
5494 if (e->isym.st_shndx != SHN_UNDEF
5495 && (e->isym.st_shndx < SHN_LORESERVE
5496 || e->isym.st_shndx > SHN_HIRESERVE))
587ff49e
RH
5497 {
5498 s = bfd_section_from_elf_index (e->input_bfd,
5499 e->isym.st_shndx);
5500
5501 sym.st_shndx =
5502 elf_section_data (s->output_section)->this_idx;
5503 sym.st_value = (s->output_section->vma
5504 + s->output_offset
5505 + e->isym.st_value);
5506 }
30b30c21
RH
5507
5508 if (last_local < e->dynindx)
5509 last_local = e->dynindx;
5510
9ad5cbcf
AM
5511 dest = dynsym + e->dynindx;
5512 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
30b30c21
RH
5513 }
5514 }
5515
71a40b32
ILT
5516 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
5517 last_local + 1;
30b30c21 5518 }
252b5132
RH
5519
5520 /* We get the global symbols from the hash table. */
5521 eoinfo.failed = false;
5522 eoinfo.localsyms = false;
5523 eoinfo.finfo = &finfo;
5524 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5525 (PTR) &eoinfo);
5526 if (eoinfo.failed)
5527 return false;
5528
587ff49e
RH
5529 /* If backend needs to output some symbols not present in the hash
5530 table, do it now. */
5531 if (bed->elf_backend_output_arch_syms)
5532 {
dc810e39
AM
5533 typedef boolean (*out_sym_func) PARAMS ((PTR, const char *,
5534 Elf_Internal_Sym *,
5535 asection *));
5536
5537 if (! ((*bed->elf_backend_output_arch_syms)
5538 (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
587ff49e 5539 return false;
3e932841 5540 }
587ff49e 5541
252b5132
RH
5542 /* Flush all symbols to the file. */
5543 if (! elf_link_flush_output_syms (&finfo))
5544 return false;
5545
5546 /* Now we know the size of the symtab section. */
5547 off += symtab_hdr->sh_size;
5548
5549 /* Finish up and write out the symbol string table (.strtab)
5550 section. */
5551 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5552 /* sh_name was set in prep_headers. */
5553 symstrtab_hdr->sh_type = SHT_STRTAB;
5554 symstrtab_hdr->sh_flags = 0;
5555 symstrtab_hdr->sh_addr = 0;
5556 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
5557 symstrtab_hdr->sh_entsize = 0;
5558 symstrtab_hdr->sh_link = 0;
5559 symstrtab_hdr->sh_info = 0;
5560 /* sh_offset is set just below. */
5561 symstrtab_hdr->sh_addralign = 1;
5562
5563 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
5564 elf_tdata (abfd)->next_file_pos = off;
5565
5566 if (bfd_get_symcount (abfd) > 0)
5567 {
5568 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
5569 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
5570 return false;
5571 }
5572
5573 /* Adjust the relocs to have the correct symbol indices. */
5574 for (o = abfd->sections; o != NULL; o = o->next)
5575 {
252b5132
RH
5576 if ((o->flags & SEC_RELOC) == 0)
5577 continue;
5578
3e932841 5579 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
31367b81
MM
5580 elf_section_data (o)->rel_count,
5581 elf_section_data (o)->rel_hashes);
5582 if (elf_section_data (o)->rel_hdr2 != NULL)
5583 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
5584 elf_section_data (o)->rel_count2,
3e932841 5585 (elf_section_data (o)->rel_hashes
31367b81 5586 + elf_section_data (o)->rel_count));
252b5132
RH
5587
5588 /* Set the reloc_count field to 0 to prevent write_relocs from
5589 trying to swap the relocs out itself. */
5590 o->reloc_count = 0;
5591 }
5592
db6751f2
JJ
5593 if (dynamic && info->combreloc && dynobj != NULL)
5594 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
5595
252b5132
RH
5596 /* If we are linking against a dynamic object, or generating a
5597 shared library, finish up the dynamic linking information. */
5598 if (dynamic)
5599 {
5600 Elf_External_Dyn *dyncon, *dynconend;
5601
5602 /* Fix up .dynamic entries. */
5603 o = bfd_get_section_by_name (dynobj, ".dynamic");
5604 BFD_ASSERT (o != NULL);
5605
5606 dyncon = (Elf_External_Dyn *) o->contents;
5607 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5608 for (; dyncon < dynconend; dyncon++)
5609 {
5610 Elf_Internal_Dyn dyn;
5611 const char *name;
5612 unsigned int type;
5613
5614 elf_swap_dyn_in (dynobj, dyncon, &dyn);
5615
5616 switch (dyn.d_tag)
5617 {
5618 default:
5619 break;
db6751f2
JJ
5620 case DT_NULL:
5621 if (relativecount > 0 && dyncon + 1 < dynconend)
5622 {
5623 switch (elf_section_data (reldyn)->this_hdr.sh_type)
5624 {
5625 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
5626 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
5627 default: break;
5628 }
5629 if (dyn.d_tag != DT_NULL)
5630 {
5631 dyn.d_un.d_val = relativecount;
5632 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5633 relativecount = 0;
5634 }
5635 }
5636 break;
252b5132 5637 case DT_INIT:
f0c2e336 5638 name = info->init_function;
252b5132
RH
5639 goto get_sym;
5640 case DT_FINI:
f0c2e336 5641 name = info->fini_function;
252b5132
RH
5642 get_sym:
5643 {
5644 struct elf_link_hash_entry *h;
5645
5646 h = elf_link_hash_lookup (elf_hash_table (info), name,
5647 false, false, true);
5648 if (h != NULL
5649 && (h->root.type == bfd_link_hash_defined
5650 || h->root.type == bfd_link_hash_defweak))
5651 {
5652 dyn.d_un.d_val = h->root.u.def.value;
5653 o = h->root.u.def.section;
5654 if (o->output_section != NULL)
5655 dyn.d_un.d_val += (o->output_section->vma
5656 + o->output_offset);
5657 else
5658 {
5659 /* The symbol is imported from another shared
5660 library and does not apply to this one. */
5661 dyn.d_un.d_val = 0;
5662 }
5663
5664 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5665 }
5666 }
5667 break;
5668
30831527
RH
5669 case DT_PREINIT_ARRAYSZ:
5670 name = ".preinit_array";
5671 goto get_size;
5672 case DT_INIT_ARRAYSZ:
5673 name = ".init_array";
5674 goto get_size;
5675 case DT_FINI_ARRAYSZ:
5676 name = ".fini_array";
5677 get_size:
5678 o = bfd_get_section_by_name (abfd, name);
2cb69dd3
JL
5679 if (o == NULL)
5680 {
5681 (*_bfd_error_handler)
5682 (_("%s: could not find output section %s"),
5683 bfd_get_filename (abfd), name);
5684 goto error_return;
5685 }
25e27870
L
5686 if (o->_raw_size == 0)
5687 (*_bfd_error_handler)
5688 (_("warning: %s section has zero size"), name);
30831527
RH
5689 dyn.d_un.d_val = o->_raw_size;
5690 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5691 break;
5692
5693 case DT_PREINIT_ARRAY:
5694 name = ".preinit_array";
5695 goto get_vma;
5696 case DT_INIT_ARRAY:
5697 name = ".init_array";
5698 goto get_vma;
5699 case DT_FINI_ARRAY:
5700 name = ".fini_array";
5701 goto get_vma;
5702
252b5132
RH
5703 case DT_HASH:
5704 name = ".hash";
5705 goto get_vma;
5706 case DT_STRTAB:
5707 name = ".dynstr";
5708 goto get_vma;
5709 case DT_SYMTAB:
5710 name = ".dynsym";
5711 goto get_vma;
5712 case DT_VERDEF:
5713 name = ".gnu.version_d";
5714 goto get_vma;
5715 case DT_VERNEED:
5716 name = ".gnu.version_r";
5717 goto get_vma;
5718 case DT_VERSYM:
5719 name = ".gnu.version";
5720 get_vma:
5721 o = bfd_get_section_by_name (abfd, name);
2cb69dd3
JL
5722 if (o == NULL)
5723 {
5724 (*_bfd_error_handler)
5725 (_("%s: could not find output section %s"),
5726 bfd_get_filename (abfd), name);
5727 goto error_return;
5728 }
252b5132
RH
5729 dyn.d_un.d_ptr = o->vma;
5730 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5731 break;
5732
5733 case DT_REL:
5734 case DT_RELA:
5735 case DT_RELSZ:
5736 case DT_RELASZ:
5737 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5738 type = SHT_REL;
5739 else
5740 type = SHT_RELA;
5741 dyn.d_un.d_val = 0;
9ad5cbcf 5742 for (i = 1; i < elf_numsections (abfd); i++)
252b5132
RH
5743 {
5744 Elf_Internal_Shdr *hdr;
5745
5746 hdr = elf_elfsections (abfd)[i];
5747 if (hdr->sh_type == type
5748 && (hdr->sh_flags & SHF_ALLOC) != 0)
5749 {
5750 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5751 dyn.d_un.d_val += hdr->sh_size;
5752 else
5753 {
5754 if (dyn.d_un.d_val == 0
5755 || hdr->sh_addr < dyn.d_un.d_val)
5756 dyn.d_un.d_val = hdr->sh_addr;
5757 }
5758 }
5759 }
5760 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5761 break;
5762 }
5763 }
5764 }
5765
5766 /* If we have created any dynamic sections, then output them. */
5767 if (dynobj != NULL)
5768 {
5769 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5770 goto error_return;
5771
5772 for (o = dynobj->sections; o != NULL; o = o->next)
5773 {
5774 if ((o->flags & SEC_HAS_CONTENTS) == 0
fc8c40a0
AM
5775 || o->_raw_size == 0
5776 || o->output_section == bfd_abs_section_ptr)
252b5132
RH
5777 continue;
5778 if ((o->flags & SEC_LINKER_CREATED) == 0)
5779 {
5780 /* At this point, we are only interested in sections
c44233aa 5781 created by elf_link_create_dynamic_sections. */
252b5132
RH
5782 continue;
5783 }
5784 if ((elf_section_data (o->output_section)->this_hdr.sh_type
5785 != SHT_STRTAB)
5786 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
5787 {
5788 if (! bfd_set_section_contents (abfd, o->output_section,
dc810e39
AM
5789 o->contents,
5790 (file_ptr) o->output_offset,
252b5132
RH
5791 o->_raw_size))
5792 goto error_return;
5793 }
5794 else
5795 {
252b5132 5796 /* The contents of the .dynstr section are actually in a
c44233aa 5797 stringtab. */
252b5132
RH
5798 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
5799 if (bfd_seek (abfd, off, SEEK_SET) != 0
2b0f7ef9
JJ
5800 || ! _bfd_elf_strtab_emit (abfd,
5801 elf_hash_table (info)->dynstr))
252b5132
RH
5802 goto error_return;
5803 }
5804 }
5805 }
5806
1126897b
AM
5807 if (info->relocateable)
5808 {
5809 boolean failed = false;
5810
5811 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
5812 if (failed)
5813 goto error_return;
5814 }
5815
252b5132
RH
5816 /* If we have optimized stabs strings, output them. */
5817 if (elf_hash_table (info)->stab_info != NULL)
5818 {
5819 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
5820 goto error_return;
5821 }
5822
57a72197 5823 if (info->eh_frame_hdr && elf_hash_table (info)->dynobj)
65765700
JJ
5824 {
5825 o = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
5826 ".eh_frame_hdr");
5827 if (o
5828 && (elf_section_data (o)->sec_info_type
5829 == ELF_INFO_TYPE_EH_FRAME_HDR))
5830 {
5831 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, o))
5832 goto error_return;
5833 }
5834 }
5835
252b5132
RH
5836 if (finfo.symstrtab != NULL)
5837 _bfd_stringtab_free (finfo.symstrtab);
5838 if (finfo.contents != NULL)
5839 free (finfo.contents);
5840 if (finfo.external_relocs != NULL)
5841 free (finfo.external_relocs);
5842 if (finfo.internal_relocs != NULL)
5843 free (finfo.internal_relocs);
5844 if (finfo.external_syms != NULL)
5845 free (finfo.external_syms);
9ad5cbcf
AM
5846 if (finfo.locsym_shndx != NULL)
5847 free (finfo.locsym_shndx);
252b5132
RH
5848 if (finfo.internal_syms != NULL)
5849 free (finfo.internal_syms);
5850 if (finfo.indices != NULL)
5851 free (finfo.indices);
5852 if (finfo.sections != NULL)
5853 free (finfo.sections);
5854 if (finfo.symbuf != NULL)
5855 free (finfo.symbuf);
9ad5cbcf
AM
5856 if (finfo.symshndxbuf != NULL)
5857 free (finfo.symbuf);
252b5132
RH
5858 for (o = abfd->sections; o != NULL; o = o->next)
5859 {
5860 if ((o->flags & SEC_RELOC) != 0
5861 && elf_section_data (o)->rel_hashes != NULL)
c44233aa 5862 free (elf_section_data (o)->rel_hashes);
252b5132
RH
5863 }
5864
5865 elf_tdata (abfd)->linker = true;
5866
5867 return true;
5868
5869 error_return:
5870 if (finfo.symstrtab != NULL)
5871 _bfd_stringtab_free (finfo.symstrtab);
5872 if (finfo.contents != NULL)
5873 free (finfo.contents);
5874 if (finfo.external_relocs != NULL)
5875 free (finfo.external_relocs);
5876 if (finfo.internal_relocs != NULL)
5877 free (finfo.internal_relocs);
5878 if (finfo.external_syms != NULL)
5879 free (finfo.external_syms);
9ad5cbcf
AM
5880 if (finfo.locsym_shndx != NULL)
5881 free (finfo.locsym_shndx);
252b5132
RH
5882 if (finfo.internal_syms != NULL)
5883 free (finfo.internal_syms);
5884 if (finfo.indices != NULL)
5885 free (finfo.indices);
5886 if (finfo.sections != NULL)
5887 free (finfo.sections);
5888 if (finfo.symbuf != NULL)
5889 free (finfo.symbuf);
9ad5cbcf
AM
5890 if (finfo.symshndxbuf != NULL)
5891 free (finfo.symbuf);
252b5132
RH
5892 for (o = abfd->sections; o != NULL; o = o->next)
5893 {
5894 if ((o->flags & SEC_RELOC) != 0
5895 && elf_section_data (o)->rel_hashes != NULL)
5896 free (elf_section_data (o)->rel_hashes);
5897 }
5898
5899 return false;
5900}
5901
5902/* Add a symbol to the output symbol table. */
5903
5904static boolean
5905elf_link_output_sym (finfo, name, elfsym, input_sec)
5906 struct elf_final_link_info *finfo;
5907 const char *name;
5908 Elf_Internal_Sym *elfsym;
5909 asection *input_sec;
5910{
9ad5cbcf
AM
5911 Elf_External_Sym *dest;
5912 Elf_External_Sym_Shndx *destshndx;
c44233aa 5913
252b5132
RH
5914 boolean (*output_symbol_hook) PARAMS ((bfd *,
5915 struct bfd_link_info *info,
5916 const char *,
5917 Elf_Internal_Sym *,
5918 asection *));
5919
5920 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
5921 elf_backend_link_output_symbol_hook;
5922 if (output_symbol_hook != NULL)
5923 {
5924 if (! ((*output_symbol_hook)
5925 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
5926 return false;
5927 }
5928
5929 if (name == (const char *) NULL || *name == '\0')
5930 elfsym->st_name = 0;
5931 else if (input_sec->flags & SEC_EXCLUDE)
5932 elfsym->st_name = 0;
5933 else
5934 {
5935 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
a7b97311 5936 name, true, false);
252b5132
RH
5937 if (elfsym->st_name == (unsigned long) -1)
5938 return false;
5939 }
5940
5941 if (finfo->symbuf_count >= finfo->symbuf_size)
5942 {
5943 if (! elf_link_flush_output_syms (finfo))
5944 return false;
5945 }
5946
9ad5cbcf
AM
5947 dest = finfo->symbuf + finfo->symbuf_count;
5948 destshndx = finfo->symshndxbuf;
5949 if (destshndx != NULL)
5950 destshndx += finfo->symbuf_count;
5951 elf_swap_symbol_out (finfo->output_bfd, elfsym, (PTR) dest, (PTR) destshndx);
252b5132
RH
5952 ++finfo->symbuf_count;
5953
5954 ++ bfd_get_symcount (finfo->output_bfd);
5955
5956 return true;
5957}
5958
5959/* Flush the output symbols to the file. */
5960
5961static boolean
5962elf_link_flush_output_syms (finfo)
5963 struct elf_final_link_info *finfo;
5964{
5965 if (finfo->symbuf_count > 0)
5966 {
9ad5cbcf 5967 Elf_Internal_Shdr *hdr;
dc810e39
AM
5968 file_ptr pos;
5969 bfd_size_type amt;
252b5132 5970
9ad5cbcf
AM
5971 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5972 pos = hdr->sh_offset + hdr->sh_size;
dc810e39
AM
5973 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
5974 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5975 || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
252b5132
RH
5976 return false;
5977
9ad5cbcf
AM
5978 hdr->sh_size += amt;
5979
5980 if (finfo->symshndxbuf != NULL)
5981 {
5982 hdr = &elf_tdata (finfo->output_bfd)->symtab_shndx_hdr;
5983 pos = hdr->sh_offset + hdr->sh_size;
5984 amt = finfo->symbuf_count * sizeof (Elf_External_Sym_Shndx);
5985 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5986 || (bfd_bwrite ((PTR) finfo->symshndxbuf, amt, finfo->output_bfd)
5987 != amt))
5988 return false;
5989
5990 hdr->sh_size += amt;
5991 }
252b5132
RH
5992
5993 finfo->symbuf_count = 0;
5994 }
5995
5996 return true;
5997}
5998
f5fa8ca2
JJ
5999/* Adjust all external symbols pointing into SEC_MERGE sections
6000 to reflect the object merging within the sections. */
6001
6002static boolean
6003elf_link_sec_merge_syms (h, data)
6004 struct elf_link_hash_entry *h;
6005 PTR data;
6006{
6007 asection *sec;
6008
e92d460e
AM
6009 if (h->root.type == bfd_link_hash_warning)
6010 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6011
f5fa8ca2
JJ
6012 if ((h->root.type == bfd_link_hash_defined
6013 || h->root.type == bfd_link_hash_defweak)
6014 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
65765700 6015 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
f5fa8ca2
JJ
6016 {
6017 bfd *output_bfd = (bfd *) data;
6018
6019 h->root.u.def.value =
6020 _bfd_merged_section_offset (output_bfd,
6021 &h->root.u.def.section,
65765700 6022 elf_section_data (sec)->sec_info,
f5fa8ca2
JJ
6023 h->root.u.def.value, (bfd_vma) 0);
6024 }
6025
6026 return true;
6027}
6028
f5d44ba0
AM
6029/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6030 allowing an unsatisfied unversioned symbol in the DSO to match a
6031 versioned symbol that would normally require an explicit version. */
6032
6033static boolean
6034elf_link_check_versioned_symbol (info, h)
6035 struct bfd_link_info *info;
6036 struct elf_link_hash_entry *h;
6037{
6038 bfd *undef_bfd = h->root.u.undef.abfd;
6039 struct elf_link_loaded_list *loaded;
f5d44ba0
AM
6040
6041 if ((undef_bfd->flags & DYNAMIC) == 0
6042 || info->hash->creator->flavour != bfd_target_elf_flavour
6043 || elf_dt_soname (h->root.u.undef.abfd) == NULL)
6044 return false;
6045
6046 for (loaded = elf_hash_table (info)->loaded;
6047 loaded != NULL;
6048 loaded = loaded->next)
6049 {
6050 bfd *input;
6051 Elf_Internal_Shdr *hdr;
6052 bfd_size_type symcount;
6053 bfd_size_type extsymcount;
6054 bfd_size_type extsymoff;
6055 Elf_Internal_Shdr *versymhdr;
6cdc0ccc
AM
6056 Elf_Internal_Sym *isym;
6057 Elf_Internal_Sym *isymend;
6058 Elf_Internal_Sym *isymbuf;
f5d44ba0 6059 Elf_External_Versym *ever;
6cdc0ccc 6060 Elf_External_Versym *extversym;
f5d44ba0
AM
6061
6062 input = loaded->abfd;
6063
6064 /* We check each DSO for a possible hidden versioned definition. */
6065 if (input == undef_bfd
6066 || (input->flags & DYNAMIC) == 0
6067 || elf_dynversym (input) == 0)
6068 continue;
6069
6070 hdr = &elf_tdata (input)->dynsymtab_hdr;
6071
6072 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
6073 if (elf_bad_symtab (input))
6074 {
6075 extsymcount = symcount;
6076 extsymoff = 0;
6077 }
6078 else
6079 {
6080 extsymcount = symcount - hdr->sh_info;
6081 extsymoff = hdr->sh_info;
6082 }
6083
6084 if (extsymcount == 0)
6085 continue;
6086
6cdc0ccc
AM
6087 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6088 NULL, NULL, NULL);
6089 if (isymbuf == NULL)
f5d44ba0
AM
6090 return false;
6091
f5d44ba0
AM
6092 /* Read in any version definitions. */
6093 versymhdr = &elf_tdata (input)->dynversym_hdr;
6094 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
6095 if (extversym == NULL)
6096 goto error_ret;
6097
6098 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6099 || (bfd_bread ((PTR) extversym, versymhdr->sh_size, input)
6100 != versymhdr->sh_size))
6101 {
6102 free (extversym);
6103 error_ret:
6cdc0ccc 6104 free (isymbuf);
f5d44ba0
AM
6105 return false;
6106 }
6107
6108 ever = extversym + extsymoff;
6cdc0ccc
AM
6109 isymend = isymbuf + extsymcount;
6110 for (isym = isymbuf; isym < isymend; isym++, ever++)
f5d44ba0
AM
6111 {
6112 const char *name;
f5d44ba0
AM
6113 Elf_Internal_Versym iver;
6114
6cdc0ccc
AM
6115 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6116 || isym->st_shndx == SHN_UNDEF)
f5d44ba0
AM
6117 continue;
6118
6119 name = bfd_elf_string_from_elf_section (input,
6120 hdr->sh_link,
6cdc0ccc 6121 isym->st_name);
f5d44ba0
AM
6122 if (strcmp (name, h->root.root.string) != 0)
6123 continue;
6124
6125 _bfd_elf_swap_versym_in (input, ever, &iver);
6126
6127 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6128 {
6129 /* If we have a non-hidden versioned sym, then it should
6130 have provided a definition for the undefined sym. */
6131 abort ();
6132 }
6133
6134 if ((iver.vs_vers & VERSYM_VERSION) == 2)
6135 {
6136 /* This is the oldest (default) sym. We can use it. */
6137 free (extversym);
6cdc0ccc 6138 free (isymbuf);
f5d44ba0
AM
6139 return true;
6140 }
6141 }
6142
6143 free (extversym);
6cdc0ccc 6144 free (isymbuf);
f5d44ba0
AM
6145 }
6146
6147 return false;
6148}
6149
252b5132
RH
6150/* Add an external symbol to the symbol table. This is called from
6151 the hash table traversal routine. When generating a shared object,
6152 we go through the symbol table twice. The first time we output
6153 anything that might have been forced to local scope in a version
6154 script. The second time we output the symbols that are still
6155 global symbols. */
6156
6157static boolean
6158elf_link_output_extsym (h, data)
6159 struct elf_link_hash_entry *h;
6160 PTR data;
6161{
6162 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
6163 struct elf_final_link_info *finfo = eoinfo->finfo;
6164 boolean strip;
6165 Elf_Internal_Sym sym;
6166 asection *input_sec;
6167
e92d460e
AM
6168 if (h->root.type == bfd_link_hash_warning)
6169 {
6170 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6171 if (h->root.type == bfd_link_hash_new)
6172 return true;
6173 }
6174
252b5132
RH
6175 /* Decide whether to output this symbol in this pass. */
6176 if (eoinfo->localsyms)
6177 {
6178 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6179 return true;
6180 }
6181 else
6182 {
6183 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6184 return true;
6185 }
6186
6187 /* If we are not creating a shared library, and this symbol is
6188 referenced by a shared library but is not defined anywhere, then
6189 warn that it is undefined. If we do not do this, the runtime
6190 linker will complain that the symbol is undefined when the
6191 program is run. We don't have to worry about symbols that are
6192 referenced by regular files, because we will already have issued
6193 warnings for them. */
6194 if (! finfo->info->relocateable
b79e8c78 6195 && ! finfo->info->allow_shlib_undefined
e45bf863 6196 && ! finfo->info->shared
252b5132
RH
6197 && h->root.type == bfd_link_hash_undefined
6198 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
f5d44ba0
AM
6199 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
6200 && ! elf_link_check_versioned_symbol (finfo->info, h))
252b5132
RH
6201 {
6202 if (! ((*finfo->info->callbacks->undefined_symbol)
6203 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
dc810e39 6204 (asection *) NULL, (bfd_vma) 0, true)))
252b5132
RH
6205 {
6206 eoinfo->failed = true;
6207 return false;
6208 }
6209 }
6210
6211 /* We don't want to output symbols that have never been mentioned by
6212 a regular file, or that we have been told to strip. However, if
6213 h->indx is set to -2, the symbol is used by a reloc and we must
6214 output it. */
6215 if (h->indx == -2)
6216 strip = false;
6217 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6218 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
6219 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
6220 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6221 strip = true;
6222 else if (finfo->info->strip == strip_all
6223 || (finfo->info->strip == strip_some
6224 && bfd_hash_lookup (finfo->info->keep_hash,
6225 h->root.root.string,
6226 false, false) == NULL))
6227 strip = true;
6228 else
6229 strip = false;
6230
6231 /* If we're stripping it, and it's not a dynamic symbol, there's
2bd171e0
ILT
6232 nothing else to do unless it is a forced local symbol. */
6233 if (strip
6234 && h->dynindx == -1
6235 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
252b5132
RH
6236 return true;
6237
6238 sym.st_value = 0;
6239 sym.st_size = h->size;
6240 sym.st_other = h->other;
6241 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6242 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6243 else if (h->root.type == bfd_link_hash_undefweak
6244 || h->root.type == bfd_link_hash_defweak)
6245 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6246 else
6247 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6248
6249 switch (h->root.type)
6250 {
6251 default:
6252 case bfd_link_hash_new:
e92d460e 6253 case bfd_link_hash_warning:
252b5132
RH
6254 abort ();
6255 return false;
6256
6257 case bfd_link_hash_undefined:
252b5132
RH
6258 case bfd_link_hash_undefweak:
6259 input_sec = bfd_und_section_ptr;
6260 sym.st_shndx = SHN_UNDEF;
6261 break;
6262
6263 case bfd_link_hash_defined:
6264 case bfd_link_hash_defweak:
6265 {
6266 input_sec = h->root.u.def.section;
6267 if (input_sec->output_section != NULL)
6268 {
6269 sym.st_shndx =
6270 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6271 input_sec->output_section);
9ad5cbcf 6272 if (sym.st_shndx == SHN_BAD)
252b5132
RH
6273 {
6274 (*_bfd_error_handler)
6275 (_("%s: could not find output section %s for input section %s"),
6276 bfd_get_filename (finfo->output_bfd),
6277 input_sec->output_section->name,
6278 input_sec->name);
6279 eoinfo->failed = true;
6280 return false;
6281 }
6282
6283 /* ELF symbols in relocateable files are section relative,
6284 but in nonrelocateable files they are virtual
6285 addresses. */
6286 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6287 if (! finfo->info->relocateable)
13ae64f3
JJ
6288 {
6289 sym.st_value += input_sec->output_section->vma;
6290 if (h->type == STT_TLS)
6291 {
6292 /* STT_TLS symbols are relative to PT_TLS segment
6293 base. */
6294 BFD_ASSERT (finfo->first_tls_sec != NULL);
6295 sym.st_value -= finfo->first_tls_sec->vma;
6296 }
6297 }
252b5132
RH
6298 }
6299 else
6300 {
6301 BFD_ASSERT (input_sec->owner == NULL
6302 || (input_sec->owner->flags & DYNAMIC) != 0);
6303 sym.st_shndx = SHN_UNDEF;
6304 input_sec = bfd_und_section_ptr;
6305 }
6306 }
6307 break;
6308
6309 case bfd_link_hash_common:
6310 input_sec = h->root.u.c.p->section;
6311 sym.st_shndx = SHN_COMMON;
6312 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6313 break;
6314
6315 case bfd_link_hash_indirect:
6316 /* These symbols are created by symbol versioning. They point
c44233aa
AM
6317 to the decorated version of the name. For example, if the
6318 symbol foo@@GNU_1.2 is the default, which should be used when
6319 foo is used with no version, then we add an indirect symbol
6320 foo which points to foo@@GNU_1.2. We ignore these symbols,
6321 since the indirected symbol is already in the hash table. */
94b6c40a 6322 return true;
252b5132
RH
6323 }
6324
6325 /* Give the processor backend a chance to tweak the symbol value,
6326 and also to finish up anything that needs to be done for this
c44233aa
AM
6327 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6328 forced local syms when non-shared is due to a historical quirk. */
252b5132
RH
6329 if ((h->dynindx != -1
6330 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
c44233aa
AM
6331 && (finfo->info->shared
6332 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
252b5132
RH
6333 && elf_hash_table (finfo->info)->dynamic_sections_created)
6334 {
6335 struct elf_backend_data *bed;
6336
6337 bed = get_elf_backend_data (finfo->output_bfd);
6338 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6339 (finfo->output_bfd, finfo->info, h, &sym)))
6340 {
6341 eoinfo->failed = true;
6342 return false;
6343 }
6344 }
6345
6346 /* If we are marking the symbol as undefined, and there are no
6347 non-weak references to this symbol from a regular object, then
91d3970e
ILT
6348 mark the symbol as weak undefined; if there are non-weak
6349 references, mark the symbol as strong. We can't do this earlier,
252b5132
RH
6350 because it might not be marked as undefined until the
6351 finish_dynamic_symbol routine gets through with it. */
6352 if (sym.st_shndx == SHN_UNDEF
252b5132 6353 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
a7b97311
AM
6354 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6355 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
91d3970e
ILT
6356 {
6357 int bindtype;
6358
6359 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6360 bindtype = STB_GLOBAL;
6361 else
6362 bindtype = STB_WEAK;
6363 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6364 }
252b5132 6365
32c092c3 6366 /* If a symbol is not defined locally, we clear the visibility
3e932841 6367 field. */
2cd533b7
L
6368 if (! finfo->info->relocateable
6369 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
a7b97311 6370 sym.st_other ^= ELF_ST_VISIBILITY (sym.st_other);
32c092c3 6371
252b5132 6372 /* If this symbol should be put in the .dynsym section, then put it
f5d44ba0
AM
6373 there now. We already know the symbol index. We also fill in
6374 the entry in the .hash section. */
252b5132
RH
6375 if (h->dynindx != -1
6376 && elf_hash_table (finfo->info)->dynamic_sections_created)
6377 {
6378 size_t bucketcount;
6379 size_t bucket;
c7ac6ff8 6380 size_t hash_entry_size;
252b5132
RH
6381 bfd_byte *bucketpos;
6382 bfd_vma chain;
dc810e39 6383 Elf_External_Sym *esym;
252b5132
RH
6384
6385 sym.st_name = h->dynstr_index;
dc810e39 6386 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
9ad5cbcf 6387 elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym, (PTR) 0);
252b5132
RH
6388
6389 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6390 bucket = h->elf_hash_value % bucketcount;
3e932841 6391 hash_entry_size
c7ac6ff8 6392 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
252b5132 6393 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
c7ac6ff8
MM
6394 + (bucket + 2) * hash_entry_size);
6395 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
dc810e39
AM
6396 bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
6397 bucketpos);
c7ac6ff8
MM
6398 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6399 ((bfd_byte *) finfo->hash_sec->contents
6400 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
252b5132
RH
6401
6402 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6403 {
6404 Elf_Internal_Versym iversym;
dc810e39 6405 Elf_External_Versym *eversym;
252b5132
RH
6406
6407 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6408 {
6409 if (h->verinfo.verdef == NULL)
6410 iversym.vs_vers = 0;
6411 else
6412 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6413 }
6414 else
6415 {
6416 if (h->verinfo.vertree == NULL)
6417 iversym.vs_vers = 1;
6418 else
6419 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6420 }
6421
6422 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6423 iversym.vs_vers |= VERSYM_HIDDEN;
6424
dc810e39
AM
6425 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6426 eversym += h->dynindx;
6427 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
252b5132
RH
6428 }
6429 }
6430
6431 /* If we're stripping it, then it was just a dynamic symbol, and
6432 there's nothing else to do. */
6433 if (strip)
6434 return true;
6435
6436 h->indx = bfd_get_symcount (finfo->output_bfd);
6437
6438 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
6439 {
6440 eoinfo->failed = true;
6441 return false;
6442 }
6443
6444 return true;
6445}
6446
23bc299b
MM
6447/* Copy the relocations indicated by the INTERNAL_RELOCS (which
6448 originated from the section given by INPUT_REL_HDR) to the
6449 OUTPUT_BFD. */
6450
41241523 6451static boolean
3e932841 6452elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
23bc299b
MM
6453 internal_relocs)
6454 bfd *output_bfd;
6455 asection *input_section;
6456 Elf_Internal_Shdr *input_rel_hdr;
6457 Elf_Internal_Rela *internal_relocs;
6458{
6459 Elf_Internal_Rela *irela;
6460 Elf_Internal_Rela *irelaend;
6461 Elf_Internal_Shdr *output_rel_hdr;
6462 asection *output_section;
7442e600 6463 unsigned int *rel_countp = NULL;
32f0787a 6464 struct elf_backend_data *bed;
dc810e39 6465 bfd_size_type amt;
23bc299b
MM
6466
6467 output_section = input_section->output_section;
6468 output_rel_hdr = NULL;
6469
3e932841 6470 if (elf_section_data (output_section)->rel_hdr.sh_entsize
23bc299b
MM
6471 == input_rel_hdr->sh_entsize)
6472 {
6473 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
6474 rel_countp = &elf_section_data (output_section)->rel_count;
6475 }
6476 else if (elf_section_data (output_section)->rel_hdr2
6477 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
6478 == input_rel_hdr->sh_entsize))
6479 {
6480 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
6481 rel_countp = &elf_section_data (output_section)->rel_count2;
6482 }
41241523
TS
6483 else
6484 {
6485 (*_bfd_error_handler) (
6486 _("%s: relocation size mismatch in %s section %s"),
6487 bfd_get_filename (output_bfd),
6488 bfd_archive_filename (input_section->owner),
6489 input_section->name);
6490 bfd_set_error (bfd_error_wrong_object_format);
6491 return false;
6492 }
32f0787a
UC
6493
6494 bed = get_elf_backend_data (output_bfd);
23bc299b 6495 irela = internal_relocs;
209f668e
NC
6496 irelaend = irela + NUM_SHDR_ENTRIES (input_rel_hdr)
6497 * bed->s->int_rels_per_ext_rel;
6498
23bc299b
MM
6499 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
6500 {
6501 Elf_External_Rel *erel;
209f668e 6502 Elf_Internal_Rel *irel;
dc810e39
AM
6503
6504 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
6505 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
209f668e
NC
6506 if (irel == NULL)
6507 {
6508 (*_bfd_error_handler) (_("Error: out of memory"));
6509 abort ();
6510 }
23bc299b
MM
6511
6512 erel = ((Elf_External_Rel *) output_rel_hdr->contents + *rel_countp);
209f668e 6513 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erel++)
23bc299b 6514 {
4e8a9624 6515 unsigned int i;
dc810e39 6516
209f668e
NC
6517 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6518 {
6519 irel[i].r_offset = irela[i].r_offset;
6520 irel[i].r_info = irela[i].r_info;
6521 BFD_ASSERT (irela[i].r_addend == 0);
6522 }
23bc299b 6523
32f0787a 6524 if (bed->s->swap_reloc_out)
209f668e 6525 (*bed->s->swap_reloc_out) (output_bfd, irel, (PTR) erel);
32f0787a 6526 else
209f668e 6527 elf_swap_reloc_out (output_bfd, irel, erel);
23bc299b 6528 }
209f668e
NC
6529
6530 free (irel);
23bc299b
MM
6531 }
6532 else
6533 {
6534 Elf_External_Rela *erela;
6535
209f668e
NC
6536 BFD_ASSERT (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
6537
23bc299b 6538 erela = ((Elf_External_Rela *) output_rel_hdr->contents + *rel_countp);
209f668e 6539 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erela++)
32f0787a
UC
6540 if (bed->s->swap_reloca_out)
6541 (*bed->s->swap_reloca_out) (output_bfd, irela, (PTR) erela);
6542 else
6543 elf_swap_reloca_out (output_bfd, irela, erela);
23bc299b
MM
6544 }
6545
6546 /* Bump the counter, so that we know where to add the next set of
6547 relocations. */
d9bc7a44 6548 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
41241523
TS
6549
6550 return true;
23bc299b
MM
6551}
6552
252b5132
RH
6553/* Link an input file into the linker output file. This function
6554 handles all the sections and relocations of the input file at once.
6555 This is so that we only have to read the local symbols once, and
6556 don't have to keep them in memory. */
6557
6558static boolean
6559elf_link_input_bfd (finfo, input_bfd)
6560 struct elf_final_link_info *finfo;
6561 bfd *input_bfd;
6562{
6563 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
6564 bfd *, asection *, bfd_byte *,
6565 Elf_Internal_Rela *,
6566 Elf_Internal_Sym *, asection **));
6567 bfd *output_bfd;
6568 Elf_Internal_Shdr *symtab_hdr;
6569 size_t locsymcount;
6570 size_t extsymoff;
6cdc0ccc 6571 Elf_Internal_Sym *isymbuf;
252b5132 6572 Elf_Internal_Sym *isym;
6cdc0ccc 6573 Elf_Internal_Sym *isymend;
252b5132
RH
6574 long *pindex;
6575 asection **ppsection;
6576 asection *o;
c7ac6ff8 6577 struct elf_backend_data *bed;
9317eacc 6578 boolean emit_relocs;
f8deed93 6579 struct elf_link_hash_entry **sym_hashes;
252b5132
RH
6580
6581 output_bfd = finfo->output_bfd;
c7ac6ff8
MM
6582 bed = get_elf_backend_data (output_bfd);
6583 relocate_section = bed->elf_backend_relocate_section;
252b5132
RH
6584
6585 /* If this is a dynamic object, we don't want to do anything here:
6586 we don't want the local symbols, and we don't want the section
6587 contents. */
6588 if ((input_bfd->flags & DYNAMIC) != 0)
6589 return true;
6590
9317eacc 6591 emit_relocs = (finfo->info->relocateable
c44233aa
AM
6592 || finfo->info->emitrelocations
6593 || bed->elf_backend_emit_relocs);
9317eacc 6594
252b5132
RH
6595 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6596 if (elf_bad_symtab (input_bfd))
6597 {
6598 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6599 extsymoff = 0;
6600 }
6601 else
6602 {
6603 locsymcount = symtab_hdr->sh_info;
6604 extsymoff = symtab_hdr->sh_info;
6605 }
6606
6607 /* Read the local symbols. */
6cdc0ccc
AM
6608 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6609 if (isymbuf == NULL && locsymcount != 0)
6610 {
6611 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6612 finfo->internal_syms,
6613 finfo->external_syms,
6614 finfo->locsym_shndx);
6615 if (isymbuf == NULL)
252b5132
RH
6616 return false;
6617 }
6618
6cdc0ccc
AM
6619 /* Find local symbol sections and adjust values of symbols in
6620 SEC_MERGE sections. Write out those local symbols we know are
6621 going into the output file. */
6622 isymend = isymbuf + locsymcount;
6623 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6624 isym < isymend;
6625 isym++, pindex++, ppsection++)
252b5132
RH
6626 {
6627 asection *isec;
6628 const char *name;
6629 Elf_Internal_Sym osym;
6630
252b5132
RH
6631 *pindex = -1;
6632
6633 if (elf_bad_symtab (input_bfd))
6634 {
6635 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6636 {
6637 *ppsection = NULL;
6638 continue;
6639 }
6640 }
6641
6642 if (isym->st_shndx == SHN_UNDEF)
862517b6 6643 isec = bfd_und_section_ptr;
9ad5cbcf
AM
6644 else if (isym->st_shndx < SHN_LORESERVE
6645 || isym->st_shndx > SHN_HIRESERVE)
f5fa8ca2
JJ
6646 {
6647 isec = section_from_elf_index (input_bfd, isym->st_shndx);
65765700
JJ
6648 if (isec
6649 && elf_section_data (isec)->sec_info_type == ELF_INFO_TYPE_MERGE
f5fa8ca2
JJ
6650 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6651 isym->st_value =
6652 _bfd_merged_section_offset (output_bfd, &isec,
65765700 6653 elf_section_data (isec)->sec_info,
f5fa8ca2
JJ
6654 isym->st_value, (bfd_vma) 0);
6655 }
252b5132 6656 else if (isym->st_shndx == SHN_ABS)
862517b6 6657 isec = bfd_abs_section_ptr;
252b5132 6658 else if (isym->st_shndx == SHN_COMMON)
862517b6 6659 isec = bfd_com_section_ptr;
252b5132
RH
6660 else
6661 {
6662 /* Who knows? */
6663 isec = NULL;
6664 }
6665
6666 *ppsection = isec;
6667
6668 /* Don't output the first, undefined, symbol. */
6cdc0ccc 6669 if (ppsection == finfo->sections)
252b5132
RH
6670 continue;
6671
24376d1b
AM
6672 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6673 {
24376d1b
AM
6674 /* We never output section symbols. Instead, we use the
6675 section symbol of the corresponding section in the output
6676 file. */
6677 continue;
6678 }
6679
252b5132
RH
6680 /* If we are stripping all symbols, we don't want to output this
6681 one. */
6682 if (finfo->info->strip == strip_all)
6683 continue;
6684
252b5132
RH
6685 /* If we are discarding all local symbols, we don't want to
6686 output this one. If we are generating a relocateable output
6687 file, then some of the local symbols may be required by
6688 relocs; we output them below as we discover that they are
6689 needed. */
6690 if (finfo->info->discard == discard_all)
6691 continue;
6692
6693 /* If this symbol is defined in a section which we are
c44233aa
AM
6694 discarding, we don't need to keep it, but note that
6695 linker_mark is only reliable for sections that have contents.
6696 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6697 as well as linker_mark. */
9ad5cbcf 6698 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
252b5132
RH
6699 && isec != NULL
6700 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6701 || (! finfo->info->relocateable
6702 && (isec->flags & SEC_EXCLUDE) != 0)))
6703 continue;
6704
6705 /* Get the name of the symbol. */
6706 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6707 isym->st_name);
6708 if (name == NULL)
6709 return false;
6710
6711 /* See if we are discarding symbols with this name. */
6712 if ((finfo->info->strip == strip_some
6713 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
6714 == NULL))
f5fa8ca2
JJ
6715 || (((finfo->info->discard == discard_sec_merge
6716 && (isec->flags & SEC_MERGE) && ! finfo->info->relocateable)
6717 || finfo->info->discard == discard_l)
252b5132
RH
6718 && bfd_is_local_label_name (input_bfd, name)))
6719 continue;
6720
6721 /* If we get here, we are going to output this symbol. */
6722
6723 osym = *isym;
6724
6725 /* Adjust the section index for the output file. */
6726 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6727 isec->output_section);
9ad5cbcf 6728 if (osym.st_shndx == SHN_BAD)
252b5132
RH
6729 return false;
6730
6731 *pindex = bfd_get_symcount (output_bfd);
6732
6733 /* ELF symbols in relocateable files are section relative, but
6734 in executable files they are virtual addresses. Note that
6735 this code assumes that all ELF sections have an associated
6736 BFD section with a reasonable value for output_offset; below
6737 we assume that they also have a reasonable value for
6738 output_section. Any special sections must be set up to meet
6739 these requirements. */
6740 osym.st_value += isec->output_offset;
6741 if (! finfo->info->relocateable)
13ae64f3
JJ
6742 {
6743 osym.st_value += isec->output_section->vma;
6744 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6745 {
6746 /* STT_TLS symbols are relative to PT_TLS segment base. */
6747 BFD_ASSERT (finfo->first_tls_sec != NULL);
6748 osym.st_value -= finfo->first_tls_sec->vma;
6749 }
6750 }
252b5132
RH
6751
6752 if (! elf_link_output_sym (finfo, name, &osym, isec))
6753 return false;
6754 }
6755
6756 /* Relocate the contents of each section. */
f8deed93 6757 sym_hashes = elf_sym_hashes (input_bfd);
252b5132
RH
6758 for (o = input_bfd->sections; o != NULL; o = o->next)
6759 {
6760 bfd_byte *contents;
6761
6762 if (! o->linker_mark)
6763 {
6764 /* This section was omitted from the link. */
6765 continue;
6766 }
6767
6768 if ((o->flags & SEC_HAS_CONTENTS) == 0
6769 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
6770 continue;
6771
6772 if ((o->flags & SEC_LINKER_CREATED) != 0)
6773 {
6774 /* Section was created by elf_link_create_dynamic_sections
6775 or somesuch. */
6776 continue;
6777 }
6778
6779 /* Get the contents of the section. They have been cached by a
c44233aa
AM
6780 relaxation routine. Note that o is a section in an input
6781 file, so the contents field will not have been set by any of
6782 the routines which work on output files. */
252b5132
RH
6783 if (elf_section_data (o)->this_hdr.contents != NULL)
6784 contents = elf_section_data (o)->this_hdr.contents;
6785 else
6786 {
6787 contents = finfo->contents;
6788 if (! bfd_get_section_contents (input_bfd, o, contents,
6789 (file_ptr) 0, o->_raw_size))
6790 return false;
6791 }
6792
6793 if ((o->flags & SEC_RELOC) != 0)
6794 {
6795 Elf_Internal_Rela *internal_relocs;
6796
6797 /* Get the swapped relocs. */
6798 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
6799 (input_bfd, o, finfo->external_relocs,
6800 finfo->internal_relocs, false));
6801 if (internal_relocs == NULL
6802 && o->reloc_count > 0)
6803 return false;
6804
ec338859
AM
6805 /* Run through the relocs looking for any against symbols
6806 from discarded sections and section symbols from
6807 removed link-once sections. Complain about relocs
6808 against discarded sections. Zero relocs against removed
6809 link-once sections. We should really complain if
6810 anything in the final link tries to use it, but
6811 DWARF-based exception handling might have an entry in
6812 .eh_frame to describe a routine in the linkonce section,
6813 and it turns out to be hard to remove the .eh_frame
6814 entry too. FIXME. */
73d074b4
DJ
6815 if (!finfo->info->relocateable
6816 && !elf_section_ignore_discarded_relocs (o))
ec338859
AM
6817 {
6818 Elf_Internal_Rela *rel, *relend;
50b4d486 6819
ec338859
AM
6820 rel = internal_relocs;
6821 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6822 for ( ; rel < relend; rel++)
6823 {
6824 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6825
6826 if (r_symndx >= locsymcount
6827 || (elf_bad_symtab (input_bfd)
6828 && finfo->sections[r_symndx] == NULL))
6829 {
6830 struct elf_link_hash_entry *h;
6831
6832 h = sym_hashes[r_symndx - extsymoff];
6833 while (h->root.type == bfd_link_hash_indirect
6834 || h->root.type == bfd_link_hash_warning)
6835 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6836
6837 /* Complain if the definition comes from a
6838 discarded section. */
6839 if ((h->root.type == bfd_link_hash_defined
6840 || h->root.type == bfd_link_hash_defweak)
ed4de5e2 6841 && elf_discarded_section (h->root.u.def.section))
ec338859 6842 {
f8deed93 6843#if BFD_VERSION_DATE < 20031005
ec338859
AM
6844 if ((o->flags & SEC_DEBUGGING) != 0)
6845 {
f8deed93 6846#if BFD_VERSION_DATE > 20021005
ec338859
AM
6847 (*finfo->info->callbacks->warning)
6848 (finfo->info,
6849 _("warning: relocation against removed section; zeroing"),
6850 NULL, input_bfd, o, rel->r_offset);
f8deed93 6851#endif
45e9217a 6852 BFD_ASSERT (r_symndx != 0);
f8deed93 6853 memset (rel, 0, sizeof (*rel));
ec338859
AM
6854 }
6855 else
f8deed93 6856#endif
ec338859
AM
6857 {
6858 if (! ((*finfo->info->callbacks->undefined_symbol)
6859 (finfo->info, h->root.root.string,
6860 input_bfd, o, rel->r_offset,
6861 true)))
6862 return false;
6863 }
6864 }
6865 }
6866 else
6867 {
f9f32305 6868 asection *sec = finfo->sections[r_symndx];
50b4d486 6869
ed4de5e2 6870 if (sec != NULL && elf_discarded_section (sec))
f9f32305 6871 {
f8deed93 6872#if BFD_VERSION_DATE < 20031005
ad43ed4c
L
6873 if ((o->flags & SEC_DEBUGGING) != 0
6874 || (sec->flags & SEC_LINK_ONCE) != 0)
f9f32305 6875 {
50b4d486 6876#if BFD_VERSION_DATE > 20021005
f9f32305
AM
6877 (*finfo->info->callbacks->warning)
6878 (finfo->info,
6879 _("warning: relocation against removed section"),
6880 NULL, input_bfd, o, rel->r_offset);
50b4d486 6881#endif
45e9217a 6882 BFD_ASSERT (r_symndx != 0);
f9f32305
AM
6883 rel->r_info
6884 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
6885 rel->r_addend = 0;
6886 }
6887 else
f8deed93 6888#endif
f9f32305
AM
6889 {
6890 boolean ok;
6891 const char *msg
6892 = _("local symbols in discarded section %s");
6893 bfd_size_type amt
6894 = strlen (sec->name) + strlen (msg) - 1;
6895 char *buf = (char *) bfd_malloc (amt);
6896
6897 if (buf != NULL)
6898 sprintf (buf, msg, sec->name);
6899 else
6900 buf = (char *) sec->name;
6901 ok = (*finfo->info->callbacks
6902 ->undefined_symbol) (finfo->info, buf,
6903 input_bfd, o,
6904 rel->r_offset,
6905 true);
6906 if (buf != sec->name)
6907 free (buf);
6908 if (!ok)
6909 return false;
ec338859
AM
6910 }
6911 }
6912 }
6913 }
6914 }
50b4d486 6915
252b5132
RH
6916 /* Relocate the section by invoking a back end routine.
6917
6918 The back end routine is responsible for adjusting the
6919 section contents as necessary, and (if using Rela relocs
6920 and generating a relocateable output file) adjusting the
6921 reloc addend as necessary.
6922
6923 The back end routine does not have to worry about setting
6924 the reloc address or the reloc symbol index.
6925
6926 The back end routine is given a pointer to the swapped in
6927 internal symbols, and can access the hash table entries
6928 for the external symbols via elf_sym_hashes (input_bfd).
6929
6930 When generating relocateable output, the back end routine
6931 must handle STB_LOCAL/STT_SECTION symbols specially. The
6932 output symbol is going to be a section symbol
6933 corresponding to the output section, which will require
6934 the addend to be adjusted. */
6935
6936 if (! (*relocate_section) (output_bfd, finfo->info,
6937 input_bfd, o, contents,
6938 internal_relocs,
6cdc0ccc 6939 isymbuf,
252b5132
RH
6940 finfo->sections))
6941 return false;
6942
9317eacc 6943 if (emit_relocs)
252b5132
RH
6944 {
6945 Elf_Internal_Rela *irela;
6946 Elf_Internal_Rela *irelaend;
6947 struct elf_link_hash_entry **rel_hash;
6948 Elf_Internal_Shdr *input_rel_hdr;
4e8a9624 6949 unsigned int next_erel;
41241523
TS
6950 boolean (*reloc_emitter) PARAMS ((bfd *, asection *,
6951 Elf_Internal_Shdr *,
6952 Elf_Internal_Rela *));
b491616a
AM
6953 boolean rela_normal;
6954
6955 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6956 rela_normal = (bed->rela_normal
6957 && (input_rel_hdr->sh_entsize
6958 == sizeof (Elf_External_Rela)));
252b5132
RH
6959
6960 /* Adjust the reloc addresses and symbol indices. */
6961
6962 irela = internal_relocs;
dc810e39 6963 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132 6964 rel_hash = (elf_section_data (o->output_section)->rel_hashes
31367b81
MM
6965 + elf_section_data (o->output_section)->rel_count
6966 + elf_section_data (o->output_section)->rel_count2);
209f668e 6967 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
252b5132
RH
6968 {
6969 unsigned long r_symndx;
252b5132
RH
6970 asection *sec;
6971
209f668e
NC
6972 if (next_erel == bed->s->int_rels_per_ext_rel)
6973 {
6974 rel_hash++;
6975 next_erel = 0;
6976 }
6977
252b5132
RH
6978 irela->r_offset += o->output_offset;
6979
7ad34365 6980 /* Relocs in an executable have to be virtual addresses. */
fd984e46 6981 if (!finfo->info->relocateable)
7ad34365
NC
6982 irela->r_offset += o->output_section->vma;
6983
252b5132
RH
6984 r_symndx = ELF_R_SYM (irela->r_info);
6985
6986 if (r_symndx == 0)
6987 continue;
6988
6989 if (r_symndx >= locsymcount
6990 || (elf_bad_symtab (input_bfd)
6991 && finfo->sections[r_symndx] == NULL))
6992 {
6993 struct elf_link_hash_entry *rh;
209f668e 6994 unsigned long indx;
252b5132
RH
6995
6996 /* This is a reloc against a global symbol. We
6997 have not yet output all the local symbols, so
6998 we do not know the symbol index of any global
6999 symbol. We set the rel_hash entry for this
7000 reloc to point to the global hash table entry
7001 for this symbol. The symbol index is then
7002 set at the end of elf_bfd_final_link. */
7003 indx = r_symndx - extsymoff;
7004 rh = elf_sym_hashes (input_bfd)[indx];
7005 while (rh->root.type == bfd_link_hash_indirect
7006 || rh->root.type == bfd_link_hash_warning)
7007 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7008
7009 /* Setting the index to -2 tells
7010 elf_link_output_extsym that this symbol is
7011 used by a reloc. */
7012 BFD_ASSERT (rh->indx < 0);
7013 rh->indx = -2;
7014
7015 *rel_hash = rh;
7016
7017 continue;
7018 }
7019
3e932841 7020 /* This is a reloc against a local symbol. */
252b5132
RH
7021
7022 *rel_hash = NULL;
6cdc0ccc 7023 isym = isymbuf + r_symndx;
252b5132
RH
7024 sec = finfo->sections[r_symndx];
7025 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
7026 {
7027 /* I suppose the backend ought to fill in the
7028 section of any STT_SECTION symbol against a
7029 processor specific section. If we have
7030 discarded a section, the output_section will
7031 be the absolute section. */
b491616a
AM
7032 if (bfd_is_abs_section (sec)
7033 || (sec != NULL
7034 && bfd_is_abs_section (sec->output_section)))
252b5132
RH
7035 r_symndx = 0;
7036 else if (sec == NULL || sec->owner == NULL)
7037 {
7038 bfd_set_error (bfd_error_bad_value);
7039 return false;
7040 }
7041 else
7042 {
7043 r_symndx = sec->output_section->target_index;
7044 BFD_ASSERT (r_symndx != 0);
7045 }
b491616a
AM
7046
7047 /* Adjust the addend according to where the
f5d44ba0 7048 section winds up in the output section. */
b491616a
AM
7049 if (rela_normal)
7050 irela->r_addend += sec->output_offset;
252b5132
RH
7051 }
7052 else
7053 {
7054 if (finfo->indices[r_symndx] == -1)
7055 {
dc810e39 7056 unsigned long shlink;
252b5132
RH
7057 const char *name;
7058 asection *osec;
7059
7060 if (finfo->info->strip == strip_all)
7061 {
7062 /* You can't do ld -r -s. */
7063 bfd_set_error (bfd_error_invalid_operation);
7064 return false;
7065 }
7066
7067 /* This symbol was skipped earlier, but
7068 since it is needed by a reloc, we
7069 must output it now. */
dc810e39 7070 shlink = symtab_hdr->sh_link;
a7b97311 7071 name = (bfd_elf_string_from_elf_section
dc810e39 7072 (input_bfd, shlink, isym->st_name));
252b5132
RH
7073 if (name == NULL)
7074 return false;
7075
7076 osec = sec->output_section;
7077 isym->st_shndx =
7078 _bfd_elf_section_from_bfd_section (output_bfd,
7079 osec);
9ad5cbcf 7080 if (isym->st_shndx == SHN_BAD)
252b5132
RH
7081 return false;
7082
7083 isym->st_value += sec->output_offset;
7084 if (! finfo->info->relocateable)
13ae64f3
JJ
7085 {
7086 isym->st_value += osec->vma;
7087 if (ELF_ST_TYPE (isym->st_info) == STT_TLS)
7088 {
7089 /* STT_TLS symbols are relative to PT_TLS
7090 segment base. */
7091 BFD_ASSERT (finfo->first_tls_sec != NULL);
7092 isym->st_value -= finfo->first_tls_sec->vma;
7093 }
7094 }
252b5132 7095
a7b97311
AM
7096 finfo->indices[r_symndx]
7097 = bfd_get_symcount (output_bfd);
252b5132
RH
7098
7099 if (! elf_link_output_sym (finfo, name, isym, sec))
7100 return false;
7101 }
7102
7103 r_symndx = finfo->indices[r_symndx];
7104 }
7105
7106 irela->r_info = ELF_R_INFO (r_symndx,
7107 ELF_R_TYPE (irela->r_info));
7108 }
7109
7110 /* Swap out the relocs. */
c44233aa
AM
7111 if (bed->elf_backend_emit_relocs
7112 && !(finfo->info->relocateable
a7b97311 7113 || finfo->info->emitrelocations))
c44233aa
AM
7114 reloc_emitter = bed->elf_backend_emit_relocs;
7115 else
7116 reloc_emitter = elf_link_output_relocs;
9317eacc 7117
41241523
TS
7118 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7119 internal_relocs))
7120 return false;
9317eacc 7121
23bc299b 7122 input_rel_hdr = elf_section_data (o)->rel_hdr2;
c44233aa
AM
7123 if (input_rel_hdr)
7124 {
7125 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
dc810e39 7126 * bed->s->int_rels_per_ext_rel);
41241523
TS
7127 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7128 internal_relocs))
7129 return false;
c44233aa 7130 }
9317eacc 7131
252b5132
RH
7132 }
7133 }
7134
7135 /* Write out the modified section contents. */
73d074b4 7136 if (bed->elf_backend_write_section
f9f32305 7137 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
73d074b4
DJ
7138 {
7139 /* Section written out. */
7140 }
65765700 7141 else switch (elf_section_data (o)->sec_info_type)
f5fa8ca2 7142 {
65765700 7143 case ELF_INFO_TYPE_STABS:
f5fa8ca2 7144 if (! (_bfd_write_section_stabs
65765700
JJ
7145 (output_bfd,
7146 &elf_hash_table (finfo->info)->stab_info,
7147 o, &elf_section_data (o)->sec_info, contents)))
f5fa8ca2 7148 return false;
65765700
JJ
7149 break;
7150 case ELF_INFO_TYPE_MERGE:
f5fa8ca2 7151 if (! (_bfd_write_merged_section
65765700 7152 (output_bfd, o, elf_section_data (o)->sec_info)))
252b5132 7153 return false;
65765700
JJ
7154 break;
7155 case ELF_INFO_TYPE_EH_FRAME:
7156 {
7157 asection *ehdrsec;
7158
7159 ehdrsec
7160 = bfd_get_section_by_name (elf_hash_table (finfo->info)->dynobj,
7161 ".eh_frame_hdr");
7162 if (! (_bfd_elf_write_section_eh_frame (output_bfd, o, ehdrsec,
7163 contents)))
7164 return false;
7165 }
7166 break;
7167 default:
7168 {
7169 bfd_size_type sec_size;
7170
7171 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
7172 if (! (o->flags & SEC_EXCLUDE)
7173 && ! bfd_set_section_contents (output_bfd, o->output_section,
7174 contents,
7175 (file_ptr) o->output_offset,
7176 sec_size))
7177 return false;
7178 }
7179 break;
252b5132
RH
7180 }
7181 }
7182
7183 return true;
7184}
7185
7186/* Generate a reloc when linking an ELF file. This is a reloc
7187 requested by the linker, and does come from any input file. This
7188 is used to build constructor and destructor tables when linking
7189 with -Ur. */
7190
7191static boolean
7192elf_reloc_link_order (output_bfd, info, output_section, link_order)
7193 bfd *output_bfd;
7194 struct bfd_link_info *info;
7195 asection *output_section;
7196 struct bfd_link_order *link_order;
7197{
7198 reloc_howto_type *howto;
7199 long indx;
7200 bfd_vma offset;
7201 bfd_vma addend;
7202 struct elf_link_hash_entry **rel_hash_ptr;
7203 Elf_Internal_Shdr *rel_hdr;
32f0787a 7204 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
252b5132
RH
7205
7206 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7207 if (howto == NULL)
7208 {
7209 bfd_set_error (bfd_error_bad_value);
7210 return false;
7211 }
7212
7213 addend = link_order->u.reloc.p->addend;
7214
7215 /* Figure out the symbol index. */
7216 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
31367b81
MM
7217 + elf_section_data (output_section)->rel_count
7218 + elf_section_data (output_section)->rel_count2);
252b5132
RH
7219 if (link_order->type == bfd_section_reloc_link_order)
7220 {
7221 indx = link_order->u.reloc.p->u.section->target_index;
7222 BFD_ASSERT (indx != 0);
7223 *rel_hash_ptr = NULL;
7224 }
7225 else
7226 {
7227 struct elf_link_hash_entry *h;
7228
7229 /* Treat a reloc against a defined symbol as though it were
c44233aa 7230 actually against the section. */
252b5132
RH
7231 h = ((struct elf_link_hash_entry *)
7232 bfd_wrapped_link_hash_lookup (output_bfd, info,
7233 link_order->u.reloc.p->u.name,
7234 false, false, true));
7235 if (h != NULL
7236 && (h->root.type == bfd_link_hash_defined
7237 || h->root.type == bfd_link_hash_defweak))
7238 {
7239 asection *section;
7240
7241 section = h->root.u.def.section;
7242 indx = section->output_section->target_index;
7243 *rel_hash_ptr = NULL;
7244 /* It seems that we ought to add the symbol value to the
c44233aa
AM
7245 addend here, but in practice it has already been added
7246 because it was passed to constructor_callback. */
252b5132
RH
7247 addend += section->output_section->vma + section->output_offset;
7248 }
7249 else if (h != NULL)
7250 {
7251 /* Setting the index to -2 tells elf_link_output_extsym that
7252 this symbol is used by a reloc. */
7253 h->indx = -2;
7254 *rel_hash_ptr = h;
7255 indx = 0;
7256 }
7257 else
7258 {
7259 if (! ((*info->callbacks->unattached_reloc)
7260 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
7261 (asection *) NULL, (bfd_vma) 0)))
7262 return false;
7263 indx = 0;
7264 }
7265 }
7266
7267 /* If this is an inplace reloc, we must write the addend into the
7268 object file. */
7269 if (howto->partial_inplace && addend != 0)
7270 {
7271 bfd_size_type size;
7272 bfd_reloc_status_type rstat;
7273 bfd_byte *buf;
7274 boolean ok;
dc810e39 7275 const char *sym_name;
252b5132
RH
7276
7277 size = bfd_get_reloc_size (howto);
7278 buf = (bfd_byte *) bfd_zmalloc (size);
7279 if (buf == (bfd_byte *) NULL)
7280 return false;
dc810e39 7281 rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf);
252b5132
RH
7282 switch (rstat)
7283 {
7284 case bfd_reloc_ok:
7285 break;
dc810e39 7286
252b5132
RH
7287 default:
7288 case bfd_reloc_outofrange:
7289 abort ();
dc810e39 7290
252b5132 7291 case bfd_reloc_overflow:
dc810e39
AM
7292 if (link_order->type == bfd_section_reloc_link_order)
7293 sym_name = bfd_section_name (output_bfd,
7294 link_order->u.reloc.p->u.section);
7295 else
7296 sym_name = link_order->u.reloc.p->u.name;
252b5132 7297 if (! ((*info->callbacks->reloc_overflow)
dc810e39
AM
7298 (info, sym_name, howto->name, addend,
7299 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
252b5132
RH
7300 {
7301 free (buf);
7302 return false;
7303 }
7304 break;
7305 }
7306 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
7307 (file_ptr) link_order->offset, size);
7308 free (buf);
7309 if (! ok)
7310 return false;
7311 }
7312
7313 /* The address of a reloc is relative to the section in a
7314 relocateable file, and is a virtual address in an executable
7315 file. */
7316 offset = link_order->offset;
7317 if (! info->relocateable)
7318 offset += output_section->vma;
7319
7320 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7321
7322 if (rel_hdr->sh_type == SHT_REL)
7323 {
dc810e39 7324 bfd_size_type size;
209f668e 7325 Elf_Internal_Rel *irel;
252b5132 7326 Elf_External_Rel *erel;
4e8a9624 7327 unsigned int i;
dc810e39
AM
7328
7329 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
7330 irel = (Elf_Internal_Rel *) bfd_zmalloc (size);
209f668e
NC
7331 if (irel == NULL)
7332 return false;
dc810e39 7333
209f668e
NC
7334 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7335 irel[i].r_offset = offset;
7336 irel[0].r_info = ELF_R_INFO (indx, howto->type);
252b5132 7337
252b5132 7338 erel = ((Elf_External_Rel *) rel_hdr->contents
0525d26e 7339 + elf_section_data (output_section)->rel_count);
209f668e 7340
32f0787a 7341 if (bed->s->swap_reloc_out)
209f668e 7342 (*bed->s->swap_reloc_out) (output_bfd, irel, (bfd_byte *) erel);
32f0787a 7343 else
209f668e
NC
7344 elf_swap_reloc_out (output_bfd, irel, erel);
7345
7346 free (irel);
252b5132
RH
7347 }
7348 else
7349 {
dc810e39 7350 bfd_size_type size;
209f668e 7351 Elf_Internal_Rela *irela;
252b5132 7352 Elf_External_Rela *erela;
4e8a9624 7353 unsigned int i;
dc810e39
AM
7354
7355 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
7356 irela = (Elf_Internal_Rela *) bfd_zmalloc (size);
209f668e
NC
7357 if (irela == NULL)
7358 return false;
7359
7360 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7361 irela[i].r_offset = offset;
7362 irela[0].r_info = ELF_R_INFO (indx, howto->type);
7363 irela[0].r_addend = addend;
252b5132 7364
252b5132 7365 erela = ((Elf_External_Rela *) rel_hdr->contents
0525d26e 7366 + elf_section_data (output_section)->rel_count);
209f668e 7367
32f0787a 7368 if (bed->s->swap_reloca_out)
209f668e 7369 (*bed->s->swap_reloca_out) (output_bfd, irela, (bfd_byte *) erela);
32f0787a 7370 else
209f668e 7371 elf_swap_reloca_out (output_bfd, irela, erela);
252b5132
RH
7372 }
7373
0525d26e 7374 ++elf_section_data (output_section)->rel_count;
252b5132
RH
7375
7376 return true;
7377}
252b5132
RH
7378\f
7379/* Allocate a pointer to live in a linker created section. */
7380
7381boolean
7382elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
7383 bfd *abfd;
7384 struct bfd_link_info *info;
7385 elf_linker_section_t *lsect;
7386 struct elf_link_hash_entry *h;
7387 const Elf_Internal_Rela *rel;
7388{
7389 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
7390 elf_linker_section_pointers_t *linker_section_ptr;
dc810e39
AM
7391 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7392 bfd_size_type amt;
252b5132
RH
7393
7394 BFD_ASSERT (lsect != NULL);
7395
a7b97311 7396 /* Is this a global symbol? */
252b5132
RH
7397 if (h != NULL)
7398 {
a7b97311 7399 /* Has this symbol already been allocated? If so, our work is done. */
252b5132
RH
7400 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
7401 rel->r_addend,
7402 lsect->which))
7403 return true;
7404
7405 ptr_linker_section_ptr = &h->linker_section_pointer;
7406 /* Make sure this symbol is output as a dynamic symbol. */
7407 if (h->dynindx == -1)
7408 {
7409 if (! elf_link_record_dynamic_symbol (info, h))
7410 return false;
7411 }
7412
7413 if (lsect->rel_section)
7414 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7415 }
a7b97311 7416 else
252b5132 7417 {
a7b97311 7418 /* Allocation of a pointer to a local symbol. */
252b5132
RH
7419 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
7420
a7b97311 7421 /* Allocate a table to hold the local symbols if first time. */
252b5132
RH
7422 if (!ptr)
7423 {
7424 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
7425 register unsigned int i;
7426
dc810e39
AM
7427 amt = num_symbols;
7428 amt *= sizeof (elf_linker_section_pointers_t *);
7429 ptr = (elf_linker_section_pointers_t **) bfd_alloc (abfd, amt);
252b5132
RH
7430
7431 if (!ptr)
7432 return false;
7433
7434 elf_local_ptr_offsets (abfd) = ptr;
7435 for (i = 0; i < num_symbols; i++)
a7b97311 7436 ptr[i] = (elf_linker_section_pointers_t *) 0;
252b5132
RH
7437 }
7438
a7b97311 7439 /* Has this symbol already been allocated? If so, our work is done. */
252b5132
RH
7440 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
7441 rel->r_addend,
7442 lsect->which))
7443 return true;
7444
7445 ptr_linker_section_ptr = &ptr[r_symndx];
7446
7447 if (info->shared)
7448 {
7449 /* If we are generating a shared object, we need to
7450 output a R_<xxx>_RELATIVE reloc so that the
7451 dynamic linker can adjust this GOT entry. */
7452 BFD_ASSERT (lsect->rel_section != NULL);
7453 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7454 }
7455 }
7456
a7b97311
AM
7457 /* Allocate space for a pointer in the linker section, and allocate
7458 a new pointer record from internal memory. */
252b5132 7459 BFD_ASSERT (ptr_linker_section_ptr != NULL);
dc810e39
AM
7460 amt = sizeof (elf_linker_section_pointers_t);
7461 linker_section_ptr = (elf_linker_section_pointers_t *) bfd_alloc (abfd, amt);
252b5132
RH
7462
7463 if (!linker_section_ptr)
7464 return false;
7465
7466 linker_section_ptr->next = *ptr_linker_section_ptr;
7467 linker_section_ptr->addend = rel->r_addend;
7468 linker_section_ptr->which = lsect->which;
7469 linker_section_ptr->written_address_p = false;
7470 *ptr_linker_section_ptr = linker_section_ptr;
7471
7472#if 0
7473 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
7474 {
a7b97311
AM
7475 linker_section_ptr->offset = (lsect->section->_raw_size
7476 - lsect->hole_size + (ARCH_SIZE / 8));
252b5132
RH
7477 lsect->hole_offset += ARCH_SIZE / 8;
7478 lsect->sym_offset += ARCH_SIZE / 8;
a7b97311 7479 if (lsect->sym_hash)
252b5132 7480 {
a7b97311 7481 /* Bump up symbol value if needed. */
252b5132
RH
7482 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
7483#ifdef DEBUG
7484 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
7485 lsect->sym_hash->root.root.string,
a7b97311
AM
7486 (long) ARCH_SIZE / 8,
7487 (long) lsect->sym_hash->root.u.def.value);
252b5132
RH
7488#endif
7489 }
7490 }
7491 else
7492#endif
7493 linker_section_ptr->offset = lsect->section->_raw_size;
7494
7495 lsect->section->_raw_size += ARCH_SIZE / 8;
7496
7497#ifdef DEBUG
a7b97311
AM
7498 fprintf (stderr,
7499 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7500 lsect->name, (long) linker_section_ptr->offset,
7501 (long) lsect->section->_raw_size);
252b5132
RH
7502#endif
7503
7504 return true;
7505}
252b5132
RH
7506\f
7507#if ARCH_SIZE==64
7508#define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7509#endif
7510#if ARCH_SIZE==32
7511#define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7512#endif
7513
209f668e 7514/* Fill in the address for a pointer generated in a linker section. */
252b5132
RH
7515
7516bfd_vma
a7b97311
AM
7517elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h,
7518 relocation, rel, relative_reloc)
252b5132
RH
7519 bfd *output_bfd;
7520 bfd *input_bfd;
7521 struct bfd_link_info *info;
7522 elf_linker_section_t *lsect;
7523 struct elf_link_hash_entry *h;
7524 bfd_vma relocation;
7525 const Elf_Internal_Rela *rel;
7526 int relative_reloc;
7527{
7528 elf_linker_section_pointers_t *linker_section_ptr;
7529
7530 BFD_ASSERT (lsect != NULL);
7531
a7b97311 7532 if (h != NULL)
252b5132 7533 {
a7b97311
AM
7534 /* Handle global symbol. */
7535 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7536 (h->linker_section_pointer,
7537 rel->r_addend,
7538 lsect->which));
252b5132
RH
7539
7540 BFD_ASSERT (linker_section_ptr != NULL);
7541
7542 if (! elf_hash_table (info)->dynamic_sections_created
7543 || (info->shared
7544 && info->symbolic
7545 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
7546 {
7547 /* This is actually a static link, or it is a
7548 -Bsymbolic link and the symbol is defined
7549 locally. We must initialize this entry in the
7550 global section.
7551
7552 When doing a dynamic link, we create a .rela.<xxx>
7553 relocation entry to initialize the value. This
7554 is done in the finish_dynamic_symbol routine. */
7555 if (!linker_section_ptr->written_address_p)
7556 {
7557 linker_section_ptr->written_address_p = true;
a7b97311
AM
7558 bfd_put_ptr (output_bfd,
7559 relocation + linker_section_ptr->addend,
7560 (lsect->section->contents
7561 + linker_section_ptr->offset));
252b5132
RH
7562 }
7563 }
7564 }
a7b97311 7565 else
252b5132 7566 {
a7b97311 7567 /* Handle local symbol. */
252b5132
RH
7568 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7569 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
7570 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
a7b97311
AM
7571 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7572 (elf_local_ptr_offsets (input_bfd)[r_symndx],
7573 rel->r_addend,
7574 lsect->which));
252b5132
RH
7575
7576 BFD_ASSERT (linker_section_ptr != NULL);
7577
a7b97311 7578 /* Write out pointer if it hasn't been rewritten out before. */
252b5132
RH
7579 if (!linker_section_ptr->written_address_p)
7580 {
7581 linker_section_ptr->written_address_p = true;
7582 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
7583 lsect->section->contents + linker_section_ptr->offset);
7584
7585 if (info->shared)
7586 {
7587 asection *srel = lsect->rel_section;
209f668e 7588 Elf_Internal_Rela *outrel;
dc810e39 7589 Elf_External_Rela *erel;
209f668e
NC
7590 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7591 unsigned int i;
dc810e39 7592 bfd_size_type amt;
209f668e 7593
dc810e39
AM
7594 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
7595 outrel = (Elf_Internal_Rela *) bfd_zmalloc (amt);
209f668e
NC
7596 if (outrel == NULL)
7597 {
7598 (*_bfd_error_handler) (_("Error: out of memory"));
7599 return 0;
7600 }
252b5132 7601
a7b97311
AM
7602 /* We need to generate a relative reloc for the dynamic
7603 linker. */
252b5132 7604 if (!srel)
a7b97311
AM
7605 {
7606 srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
7607 lsect->rel_name);
7608 lsect->rel_section = srel;
7609 }
252b5132
RH
7610
7611 BFD_ASSERT (srel != NULL);
7612
209f668e
NC
7613 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7614 outrel[i].r_offset = (lsect->section->output_section->vma
7615 + lsect->section->output_offset
7616 + linker_section_ptr->offset);
7617 outrel[0].r_info = ELF_R_INFO (0, relative_reloc);
7618 outrel[0].r_addend = 0;
dc810e39
AM
7619 erel = (Elf_External_Rela *) lsect->section->contents;
7620 erel += elf_section_data (lsect->section)->rel_count;
7621 elf_swap_reloca_out (output_bfd, outrel, erel);
0525d26e 7622 ++elf_section_data (lsect->section)->rel_count;
dc810e39 7623
209f668e 7624 free (outrel);
252b5132
RH
7625 }
7626 }
7627 }
7628
7629 relocation = (lsect->section->output_offset
7630 + linker_section_ptr->offset
7631 - lsect->hole_offset
7632 - lsect->sym_offset);
7633
7634#ifdef DEBUG
a7b97311
AM
7635 fprintf (stderr,
7636 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7637 lsect->name, (long) relocation, (long) relocation);
252b5132
RH
7638#endif
7639
7640 /* Subtract out the addend, because it will get added back in by the normal
7641 processing. */
7642 return relocation - linker_section_ptr->addend;
7643}
7644\f
7645/* Garbage collect unused sections. */
7646
7647static boolean elf_gc_mark
7648 PARAMS ((struct bfd_link_info *info, asection *sec,
7649 asection * (*gc_mark_hook)
1e2f5b6e 7650 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
252b5132
RH
7651 struct elf_link_hash_entry *, Elf_Internal_Sym *))));
7652
7653static boolean elf_gc_sweep
7654 PARAMS ((struct bfd_link_info *info,
7655 boolean (*gc_sweep_hook)
7656 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7657 const Elf_Internal_Rela *relocs))));
7658
7659static boolean elf_gc_sweep_symbol
7660 PARAMS ((struct elf_link_hash_entry *h, PTR idxptr));
7661
7662static boolean elf_gc_allocate_got_offsets
7663 PARAMS ((struct elf_link_hash_entry *h, PTR offarg));
7664
7665static boolean elf_gc_propagate_vtable_entries_used
7666 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7667
7668static boolean elf_gc_smash_unused_vtentry_relocs
7669 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7670
7671/* The mark phase of garbage collection. For a given section, mark
dbb410c3
AM
7672 it and any sections in this section's group, and all the sections
7673 which define symbols to which it refers. */
252b5132
RH
7674
7675static boolean
7676elf_gc_mark (info, sec, gc_mark_hook)
7677 struct bfd_link_info *info;
7678 asection *sec;
7679 asection * (*gc_mark_hook)
1e2f5b6e 7680 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
252b5132
RH
7681 struct elf_link_hash_entry *, Elf_Internal_Sym *));
7682{
dbb410c3
AM
7683 boolean ret;
7684 asection *group_sec;
252b5132
RH
7685
7686 sec->gc_mark = 1;
7687
dbb410c3
AM
7688 /* Mark all the sections in the group. */
7689 group_sec = elf_section_data (sec)->next_in_group;
7690 if (group_sec && !group_sec->gc_mark)
7691 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
7692 return false;
252b5132 7693
dbb410c3
AM
7694 /* Look through the section relocs. */
7695 ret = true;
252b5132
RH
7696 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
7697 {
7698 Elf_Internal_Rela *relstart, *rel, *relend;
7699 Elf_Internal_Shdr *symtab_hdr;
7700 struct elf_link_hash_entry **sym_hashes;
7701 size_t nlocsyms;
7702 size_t extsymoff;
252b5132 7703 bfd *input_bfd = sec->owner;
c7ac6ff8 7704 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
6cdc0ccc 7705 Elf_Internal_Sym *isym = NULL;
252b5132
RH
7706
7707 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7708 sym_hashes = elf_sym_hashes (input_bfd);
7709
7710 /* Read the local symbols. */
7711 if (elf_bad_symtab (input_bfd))
7712 {
7713 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
c44233aa 7714 extsymoff = 0;
252b5132
RH
7715 }
7716 else
7717 extsymoff = nlocsyms = symtab_hdr->sh_info;
9ad5cbcf 7718
6cdc0ccc
AM
7719 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
7720 if (isym == NULL && nlocsyms != 0)
9ad5cbcf 7721 {
6cdc0ccc
AM
7722 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
7723 NULL, NULL, NULL);
7724 if (isym == NULL)
9ad5cbcf
AM
7725 return false;
7726 }
7727
252b5132
RH
7728 /* Read the relocations. */
7729 relstart = (NAME(_bfd_elf,link_read_relocs)
6cdc0ccc 7730 (input_bfd, sec, NULL, (Elf_Internal_Rela *) NULL,
252b5132
RH
7731 info->keep_memory));
7732 if (relstart == NULL)
7733 {
7734 ret = false;
7735 goto out1;
7736 }
c7ac6ff8 7737 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132
RH
7738
7739 for (rel = relstart; rel < relend; rel++)
7740 {
7741 unsigned long r_symndx;
7742 asection *rsec;
7743 struct elf_link_hash_entry *h;
252b5132
RH
7744
7745 r_symndx = ELF_R_SYM (rel->r_info);
7746 if (r_symndx == 0)
7747 continue;
7748
6cdc0ccc
AM
7749 if (r_symndx >= nlocsyms
7750 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
252b5132
RH
7751 {
7752 h = sym_hashes[r_symndx - extsymoff];
1e2f5b6e 7753 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
252b5132
RH
7754 }
7755 else
7756 {
6cdc0ccc 7757 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
252b5132
RH
7758 }
7759
7760 if (rsec && !rsec->gc_mark)
b91afed7
AM
7761 {
7762 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
7763 rsec->gc_mark = 1;
7764 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
7765 {
7766 ret = false;
7767 goto out2;
7768 }
7769 }
252b5132
RH
7770 }
7771
7772 out2:
6cdc0ccc 7773 if (elf_section_data (sec)->relocs != relstart)
252b5132
RH
7774 free (relstart);
7775 out1:
6cdc0ccc
AM
7776 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
7777 {
7778 if (! info->keep_memory)
7779 free (isym);
7780 else
7781 symtab_hdr->contents = (unsigned char *) isym;
7782 }
252b5132
RH
7783 }
7784
7785 return ret;
7786}
7787
7788/* The sweep phase of garbage collection. Remove all garbage sections. */
7789
7790static boolean
7791elf_gc_sweep (info, gc_sweep_hook)
7792 struct bfd_link_info *info;
7793 boolean (*gc_sweep_hook)
7794 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7795 const Elf_Internal_Rela *relocs));
7796{
7797 bfd *sub;
7798
7799 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7800 {
7801 asection *o;
7802
f6af82bd
AM
7803 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7804 continue;
7805
252b5132
RH
7806 for (o = sub->sections; o != NULL; o = o->next)
7807 {
7808 /* Keep special sections. Keep .debug sections. */
7809 if ((o->flags & SEC_LINKER_CREATED)
7810 || (o->flags & SEC_DEBUGGING))
7811 o->gc_mark = 1;
7812
7813 if (o->gc_mark)
7814 continue;
7815
7816 /* Skip sweeping sections already excluded. */
7817 if (o->flags & SEC_EXCLUDE)
7818 continue;
7819
7820 /* Since this is early in the link process, it is simple
7821 to remove a section from the output. */
7822 o->flags |= SEC_EXCLUDE;
7823
7824 /* But we also have to update some of the relocation
7825 info we collected before. */
7826 if (gc_sweep_hook
7827 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
7828 {
7829 Elf_Internal_Rela *internal_relocs;
7830 boolean r;
7831
7832 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
7833 (o->owner, o, NULL, NULL, info->keep_memory));
7834 if (internal_relocs == NULL)
7835 return false;
7836
3e932841 7837 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
252b5132 7838
6cdc0ccc 7839 if (elf_section_data (o)->relocs != internal_relocs)
252b5132
RH
7840 free (internal_relocs);
7841
7842 if (!r)
7843 return false;
7844 }
7845 }
7846 }
7847
7848 /* Remove the symbols that were in the swept sections from the dynamic
7849 symbol table. GCFIXME: Anyone know how to get them out of the
7850 static symbol table as well? */
7851 {
7852 int i = 0;
7853
7854 elf_link_hash_traverse (elf_hash_table (info),
7855 elf_gc_sweep_symbol,
7856 (PTR) &i);
7857
7858 elf_hash_table (info)->dynsymcount = i;
7859 }
7860
7861 return true;
7862}
7863
7864/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7865
7866static boolean
7867elf_gc_sweep_symbol (h, idxptr)
7868 struct elf_link_hash_entry *h;
7869 PTR idxptr;
7870{
7871 int *idx = (int *) idxptr;
7872
e92d460e
AM
7873 if (h->root.type == bfd_link_hash_warning)
7874 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7875
252b5132
RH
7876 if (h->dynindx != -1
7877 && ((h->root.type != bfd_link_hash_defined
7878 && h->root.type != bfd_link_hash_defweak)
7879 || h->root.u.def.section->gc_mark))
7880 h->dynindx = (*idx)++;
7881
7882 return true;
7883}
7884
7885/* Propogate collected vtable information. This is called through
7886 elf_link_hash_traverse. */
7887
7888static boolean
7889elf_gc_propagate_vtable_entries_used (h, okp)
7890 struct elf_link_hash_entry *h;
7891 PTR okp;
7892{
e92d460e
AM
7893 if (h->root.type == bfd_link_hash_warning)
7894 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7895
3e932841 7896 /* Those that are not vtables. */
252b5132
RH
7897 if (h->vtable_parent == NULL)
7898 return true;
7899
7900 /* Those vtables that do not have parents, we cannot merge. */
7901 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
7902 return true;
7903
7904 /* If we've already been done, exit. */
7905 if (h->vtable_entries_used && h->vtable_entries_used[-1])
7906 return true;
7907
7908 /* Make sure the parent's table is up to date. */
7909 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
7910
7911 if (h->vtable_entries_used == NULL)
7912 {
7913 /* None of this table's entries were referenced. Re-use the
7914 parent's table. */
7915 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
7916 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
7917 }
7918 else
7919 {
7920 size_t n;
7921 boolean *cu, *pu;
7922
7923 /* Or the parent's entries into ours. */
7924 cu = h->vtable_entries_used;
7925 cu[-1] = true;
7926 pu = h->vtable_parent->vtable_entries_used;
7927 if (pu != NULL)
7928 {
c44233aa
AM
7929 asection *sec = h->root.u.def.section;
7930 struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
7931 int file_align = bed->s->file_align;
0d1ea5c0
CM
7932
7933 n = h->vtable_parent->vtable_entries_size / file_align;
374b596d 7934 while (n--)
252b5132 7935 {
374b596d
NC
7936 if (*pu)
7937 *cu = true;
7938 pu++;
7939 cu++;
252b5132
RH
7940 }
7941 }
7942 }
7943
7944 return true;
7945}
7946
7947static boolean
7948elf_gc_smash_unused_vtentry_relocs (h, okp)
7949 struct elf_link_hash_entry *h;
7950 PTR okp;
7951{
7952 asection *sec;
7953 bfd_vma hstart, hend;
7954 Elf_Internal_Rela *relstart, *relend, *rel;
c7ac6ff8 7955 struct elf_backend_data *bed;
0d1ea5c0 7956 int file_align;
252b5132 7957
e92d460e
AM
7958 if (h->root.type == bfd_link_hash_warning)
7959 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7960
252b5132
RH
7961 /* Take care of both those symbols that do not describe vtables as
7962 well as those that are not loaded. */
7963 if (h->vtable_parent == NULL)
7964 return true;
7965
7966 BFD_ASSERT (h->root.type == bfd_link_hash_defined
7967 || h->root.type == bfd_link_hash_defweak);
7968
7969 sec = h->root.u.def.section;
7970 hstart = h->root.u.def.value;
7971 hend = hstart + h->size;
7972
7973 relstart = (NAME(_bfd_elf,link_read_relocs)
7974 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, true));
7975 if (!relstart)
a7b97311 7976 return *(boolean *) okp = false;
c7ac6ff8 7977 bed = get_elf_backend_data (sec->owner);
0d1ea5c0
CM
7978 file_align = bed->s->file_align;
7979
c7ac6ff8 7980 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132
RH
7981
7982 for (rel = relstart; rel < relend; ++rel)
7983 if (rel->r_offset >= hstart && rel->r_offset < hend)
7984 {
7985 /* If the entry is in use, do nothing. */
7986 if (h->vtable_entries_used
7987 && (rel->r_offset - hstart) < h->vtable_entries_size)
7988 {
0d1ea5c0 7989 bfd_vma entry = (rel->r_offset - hstart) / file_align;
252b5132
RH
7990 if (h->vtable_entries_used[entry])
7991 continue;
7992 }
7993 /* Otherwise, kill it. */
7994 rel->r_offset = rel->r_info = rel->r_addend = 0;
7995 }
7996
7997 return true;
7998}
7999
8000/* Do mark and sweep of unused sections. */
8001
8002boolean
8003elf_gc_sections (abfd, info)
8004 bfd *abfd;
8005 struct bfd_link_info *info;
8006{
8007 boolean ok = true;
8008 bfd *sub;
8009 asection * (*gc_mark_hook)
1e2f5b6e 8010 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
c44233aa 8011 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
252b5132
RH
8012
8013 if (!get_elf_backend_data (abfd)->can_gc_sections
6d3e950b 8014 || info->relocateable || info->emitrelocations
252b5132
RH
8015 || elf_hash_table (info)->dynamic_sections_created)
8016 return true;
8017
8018 /* Apply transitive closure to the vtable entry usage info. */
8019 elf_link_hash_traverse (elf_hash_table (info),
8020 elf_gc_propagate_vtable_entries_used,
8021 (PTR) &ok);
8022 if (!ok)
8023 return false;
8024
8025 /* Kill the vtable relocations that were not used. */
8026 elf_link_hash_traverse (elf_hash_table (info),
8027 elf_gc_smash_unused_vtentry_relocs,
8028 (PTR) &ok);
8029 if (!ok)
8030 return false;
8031
8032 /* Grovel through relocs to find out who stays ... */
8033
8034 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8035 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8036 {
8037 asection *o;
f6af82bd
AM
8038
8039 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8040 continue;
8041
252b5132
RH
8042 for (o = sub->sections; o != NULL; o = o->next)
8043 {
8044 if (o->flags & SEC_KEEP)
c44233aa 8045 if (!elf_gc_mark (info, o, gc_mark_hook))
252b5132
RH
8046 return false;
8047 }
8048 }
8049
8050 /* ... and mark SEC_EXCLUDE for those that go. */
a7b97311 8051 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
252b5132
RH
8052 return false;
8053
8054 return true;
8055}
8056\f
8057/* Called from check_relocs to record the existance of a VTINHERIT reloc. */
8058
8059boolean
8060elf_gc_record_vtinherit (abfd, sec, h, offset)
8061 bfd *abfd;
8062 asection *sec;
8063 struct elf_link_hash_entry *h;
8064 bfd_vma offset;
8065{
8066 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8067 struct elf_link_hash_entry **search, *child;
8068 bfd_size_type extsymcount;
8069
8070 /* The sh_info field of the symtab header tells us where the
8071 external symbols start. We don't care about the local symbols at
8072 this point. */
8073 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
8074 if (!elf_bad_symtab (abfd))
8075 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8076
8077 sym_hashes = elf_sym_hashes (abfd);
8078 sym_hashes_end = sym_hashes + extsymcount;
8079
8080 /* Hunt down the child symbol, which is in this section at the same
8081 offset as the relocation. */
8082 for (search = sym_hashes; search != sym_hashes_end; ++search)
8083 {
8084 if ((child = *search) != NULL
8085 && (child->root.type == bfd_link_hash_defined
8086 || child->root.type == bfd_link_hash_defweak)
8087 && child->root.u.def.section == sec
8088 && child->root.u.def.value == offset)
8089 goto win;
8090 }
8091
8092 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
8f615d07 8093 bfd_archive_filename (abfd), sec->name,
a7b97311 8094 (unsigned long) offset);
252b5132
RH
8095 bfd_set_error (bfd_error_invalid_operation);
8096 return false;
8097
dc810e39 8098 win:
252b5132
RH
8099 if (!h)
8100 {
8101 /* This *should* only be the absolute section. It could potentially
8102 be that someone has defined a non-global vtable though, which
8103 would be bad. It isn't worth paging in the local symbols to be
8104 sure though; that case should simply be handled by the assembler. */
8105
8106 child->vtable_parent = (struct elf_link_hash_entry *) -1;
8107 }
8108 else
8109 child->vtable_parent = h;
8110
8111 return true;
8112}
8113
8114/* Called from check_relocs to record the existance of a VTENTRY reloc. */
8115
8116boolean
8117elf_gc_record_vtentry (abfd, sec, h, addend)
7442e600
ILT
8118 bfd *abfd ATTRIBUTE_UNUSED;
8119 asection *sec ATTRIBUTE_UNUSED;
252b5132
RH
8120 struct elf_link_hash_entry *h;
8121 bfd_vma addend;
8122{
0d1ea5c0
CM
8123 struct elf_backend_data *bed = get_elf_backend_data (abfd);
8124 int file_align = bed->s->file_align;
8125
252b5132
RH
8126 if (addend >= h->vtable_entries_size)
8127 {
8128 size_t size, bytes;
8129 boolean *ptr = h->vtable_entries_used;
8130
8131 /* While the symbol is undefined, we have to be prepared to handle
8132 a zero size. */
8133 if (h->root.type == bfd_link_hash_undefined)
8134 size = addend;
8135 else
8136 {
8137 size = h->size;
8138 if (size < addend)
8139 {
8140 /* Oops! We've got a reference past the defined end of
8141 the table. This is probably a bug -- shall we warn? */
8142 size = addend;
8143 }
8144 }
8145
8146 /* Allocate one extra entry for use as a "done" flag for the
8147 consolidation pass. */
0d1ea5c0 8148 bytes = (size / file_align + 1) * sizeof (boolean);
252b5132
RH
8149
8150 if (ptr)
8151 {
dc810e39 8152 ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes);
3e932841 8153
fed79cc6
NC
8154 if (ptr != NULL)
8155 {
8156 size_t oldbytes;
252b5132 8157
a7b97311
AM
8158 oldbytes = ((h->vtable_entries_size / file_align + 1)
8159 * sizeof (boolean));
8160 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
fed79cc6 8161 }
252b5132
RH
8162 }
8163 else
dc810e39 8164 ptr = bfd_zmalloc ((bfd_size_type) bytes);
252b5132 8165
fed79cc6
NC
8166 if (ptr == NULL)
8167 return false;
3e932841 8168
252b5132 8169 /* And arrange for that done flag to be at index -1. */
fed79cc6 8170 h->vtable_entries_used = ptr + 1;
252b5132
RH
8171 h->vtable_entries_size = size;
8172 }
3e932841 8173
0d1ea5c0 8174 h->vtable_entries_used[addend / file_align] = true;
252b5132
RH
8175
8176 return true;
8177}
8178
8179/* And an accompanying bit to work out final got entry offsets once
8180 we're done. Should be called from final_link. */
8181
8182boolean
8183elf_gc_common_finalize_got_offsets (abfd, info)
8184 bfd *abfd;
8185 struct bfd_link_info *info;
8186{
8187 bfd *i;
8188 struct elf_backend_data *bed = get_elf_backend_data (abfd);
8189 bfd_vma gotoff;
8190
8191 /* The GOT offset is relative to the .got section, but the GOT header is
8192 put into the .got.plt section, if the backend uses it. */
8193 if (bed->want_got_plt)
8194 gotoff = 0;
8195 else
8196 gotoff = bed->got_header_size;
8197
8198 /* Do the local .got entries first. */
8199 for (i = info->input_bfds; i; i = i->link_next)
8200 {
f6af82bd 8201 bfd_signed_vma *local_got;
252b5132
RH
8202 bfd_size_type j, locsymcount;
8203 Elf_Internal_Shdr *symtab_hdr;
8204
f6af82bd
AM
8205 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
8206 continue;
8207
8208 local_got = elf_local_got_refcounts (i);
252b5132
RH
8209 if (!local_got)
8210 continue;
8211
8212 symtab_hdr = &elf_tdata (i)->symtab_hdr;
8213 if (elf_bad_symtab (i))
8214 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8215 else
8216 locsymcount = symtab_hdr->sh_info;
8217
8218 for (j = 0; j < locsymcount; ++j)
8219 {
8220 if (local_got[j] > 0)
8221 {
8222 local_got[j] = gotoff;
8223 gotoff += ARCH_SIZE / 8;
8224 }
8225 else
8226 local_got[j] = (bfd_vma) -1;
8227 }
8228 }
8229
dd5724d5
AM
8230 /* Then the global .got entries. .plt refcounts are handled by
8231 adjust_dynamic_symbol */
252b5132
RH
8232 elf_link_hash_traverse (elf_hash_table (info),
8233 elf_gc_allocate_got_offsets,
8234 (PTR) &gotoff);
8235 return true;
8236}
8237
8238/* We need a special top-level link routine to convert got reference counts
8239 to real got offsets. */
8240
8241static boolean
8242elf_gc_allocate_got_offsets (h, offarg)
8243 struct elf_link_hash_entry *h;
8244 PTR offarg;
8245{
8246 bfd_vma *off = (bfd_vma *) offarg;
8247
e92d460e
AM
8248 if (h->root.type == bfd_link_hash_warning)
8249 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8250
252b5132
RH
8251 if (h->got.refcount > 0)
8252 {
8253 h->got.offset = off[0];
8254 off[0] += ARCH_SIZE / 8;
8255 }
8256 else
8257 h->got.offset = (bfd_vma) -1;
8258
8259 return true;
8260}
8261
8262/* Many folk need no more in the way of final link than this, once
8263 got entry reference counting is enabled. */
8264
8265boolean
8266elf_gc_common_final_link (abfd, info)
8267 bfd *abfd;
8268 struct bfd_link_info *info;
8269{
8270 if (!elf_gc_common_finalize_got_offsets (abfd, info))
8271 return false;
8272
8273 /* Invoke the regular ELF backend linker to do all the work. */
8274 return elf_bfd_final_link (abfd, info);
8275}
8276
8277/* This function will be called though elf_link_hash_traverse to store
8278 all hash value of the exported symbols in an array. */
8279
8280static boolean
8281elf_collect_hash_codes (h, data)
8282 struct elf_link_hash_entry *h;
8283 PTR data;
8284{
8285 unsigned long **valuep = (unsigned long **) data;
8286 const char *name;
8287 char *p;
8288 unsigned long ha;
8289 char *alc = NULL;
8290
e92d460e
AM
8291 if (h->root.type == bfd_link_hash_warning)
8292 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8293
252b5132
RH
8294 /* Ignore indirect symbols. These are added by the versioning code. */
8295 if (h->dynindx == -1)
8296 return true;
8297
8298 name = h->root.root.string;
8299 p = strchr (name, ELF_VER_CHR);
8300 if (p != NULL)
8301 {
dc810e39
AM
8302 alc = bfd_malloc ((bfd_size_type) (p - name + 1));
8303 memcpy (alc, name, (size_t) (p - name));
252b5132
RH
8304 alc[p - name] = '\0';
8305 name = alc;
8306 }
8307
8308 /* Compute the hash value. */
8309 ha = bfd_elf_hash (name);
8310
8311 /* Store the found hash value in the array given as the argument. */
8312 *(*valuep)++ = ha;
8313
8314 /* And store it in the struct so that we can put it in the hash table
8315 later. */
8316 h->elf_hash_value = ha;
8317
8318 if (alc != NULL)
8319 free (alc);
8320
8321 return true;
8322}
73d074b4
DJ
8323
8324boolean
8325elf_reloc_symbol_deleted_p (offset, cookie)
8326 bfd_vma offset;
8327 PTR cookie;
8328{
9ad5cbcf 8329 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
73d074b4
DJ
8330
8331 if (rcookie->bad_symtab)
8332 rcookie->rel = rcookie->rels;
8333
8334 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
8335 {
8336 unsigned long r_symndx = ELF_R_SYM (rcookie->rel->r_info);
73d074b4
DJ
8337
8338 if (! rcookie->bad_symtab)
8339 if (rcookie->rel->r_offset > offset)
8340 return false;
8341 if (rcookie->rel->r_offset != offset)
8342 continue;
8343
73d074b4 8344 if (r_symndx >= rcookie->locsymcount
6cdc0ccc 8345 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
73d074b4
DJ
8346 {
8347 struct elf_link_hash_entry *h;
8348
8349 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
8350
8351 while (h->root.type == bfd_link_hash_indirect
8352 || h->root.type == bfd_link_hash_warning)
8353 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8354
8355 if ((h->root.type == bfd_link_hash_defined
8356 || h->root.type == bfd_link_hash_defweak)
ed4de5e2 8357 && elf_discarded_section (h->root.u.def.section))
73d074b4
DJ
8358 return true;
8359 else
8360 return false;
8361 }
6cdc0ccc 8362 else
73d074b4
DJ
8363 {
8364 /* It's not a relocation against a global symbol,
44421011 8365 but it could be a relocation against a local
73d074b4
DJ
8366 symbol for a discarded section. */
8367 asection *isec;
6cdc0ccc 8368 Elf_Internal_Sym *isym;
73d074b4
DJ
8369
8370 /* Need to: get the symbol; get the section. */
6cdc0ccc
AM
8371 isym = &rcookie->locsyms[r_symndx];
8372 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
73d074b4 8373 {
6cdc0ccc 8374 isec = section_from_elf_index (rcookie->abfd, isym->st_shndx);
ed4de5e2 8375 if (isec != NULL && elf_discarded_section (isec))
73d074b4
DJ
8376 return true;
8377 }
8378 }
8379 return false;
8380 }
8381 return false;
8382}
8383
8384/* Discard unneeded references to discarded sections.
8385 Returns true if any section's size was changed. */
8386/* This function assumes that the relocations are in sorted order,
8387 which is true for all known assemblers. */
8388
8389boolean
65765700
JJ
8390elf_bfd_discard_info (output_bfd, info)
8391 bfd *output_bfd;
73d074b4
DJ
8392 struct bfd_link_info *info;
8393{
8394 struct elf_reloc_cookie cookie;
65765700 8395 asection *stab, *eh, *ehdr;
73d074b4 8396 Elf_Internal_Shdr *symtab_hdr;
73d074b4
DJ
8397 struct elf_backend_data *bed;
8398 bfd *abfd;
8399 boolean ret = false;
65765700 8400 boolean strip = info->strip == strip_all || info->strip == strip_debugger;
73d074b4
DJ
8401
8402 if (info->relocateable
8403 || info->traditional_format
8404 || info->hash->creator->flavour != bfd_target_elf_flavour
65765700 8405 || ! is_elf_hash_table (info))
73d074b4 8406 return false;
65765700 8407
72dd6331
AM
8408 ehdr = NULL;
8409 if (elf_hash_table (info)->dynobj != NULL)
8410 ehdr = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
8411 ".eh_frame_hdr");
65765700 8412
73d074b4
DJ
8413 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
8414 {
163c1c30
L
8415 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
8416 continue;
8417
73d074b4
DJ
8418 bed = get_elf_backend_data (abfd);
8419
8420 if ((abfd->flags & DYNAMIC) != 0)
8421 continue;
8422
65765700
JJ
8423 eh = NULL;
8424 if (ehdr)
8425 {
8426 eh = bfd_get_section_by_name (abfd, ".eh_frame");
2d653fc7
AM
8427 if (eh && (eh->_raw_size == 0
8428 || bfd_is_abs_section (eh->output_section)))
65765700
JJ
8429 eh = NULL;
8430 }
8431
2d653fc7
AM
8432 stab = NULL;
8433 if (!strip)
8434 {
8435 stab = bfd_get_section_by_name (abfd, ".stab");
8436 if (stab && (stab->_raw_size == 0
8437 || bfd_is_abs_section (stab->output_section)))
8438 stab = NULL;
8439 }
40b829d4
AM
8440 if ((! stab
8441 || elf_section_data(stab)->sec_info_type != ELF_INFO_TYPE_STABS)
65765700
JJ
8442 && ! eh
8443 && (strip || ! bed->elf_backend_discard_info))
73d074b4
DJ
8444 continue;
8445
8446 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
73d074b4
DJ
8447 cookie.abfd = abfd;
8448 cookie.sym_hashes = elf_sym_hashes (abfd);
8449 cookie.bad_symtab = elf_bad_symtab (abfd);
8450 if (cookie.bad_symtab)
8451 {
8452 cookie.locsymcount =
8453 symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8454 cookie.extsymoff = 0;
8455 }
8456 else
8457 {
8458 cookie.locsymcount = symtab_hdr->sh_info;
8459 cookie.extsymoff = symtab_hdr->sh_info;
8460 }
8461
6cdc0ccc
AM
8462 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
8463 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
c44233aa 8464 {
6cdc0ccc
AM
8465 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8466 cookie.locsymcount, 0,
8467 NULL, NULL, NULL);
c44233aa 8468 if (cookie.locsyms == NULL)
9ad5cbcf 8469 return false;
9ad5cbcf 8470 }
73d074b4 8471
65765700 8472 if (stab)
73d074b4
DJ
8473 {
8474 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
6cdc0ccc 8475 (abfd, stab, (PTR) NULL, (Elf_Internal_Rela *) NULL,
73d074b4
DJ
8476 info->keep_memory));
8477 if (cookie.rels)
8478 {
8479 cookie.rel = cookie.rels;
8480 cookie.relend =
65765700
JJ
8481 cookie.rels + stab->reloc_count * bed->s->int_rels_per_ext_rel;
8482 if (_bfd_discard_section_stabs (abfd, stab,
8483 elf_section_data (stab)->sec_info,
73d074b4
DJ
8484 elf_reloc_symbol_deleted_p,
8485 &cookie))
8486 ret = true;
6cdc0ccc 8487 if (elf_section_data (stab)->relocs != cookie.rels)
73d074b4
DJ
8488 free (cookie.rels);
8489 }
8490 }
8491
65765700
JJ
8492 if (eh)
8493 {
8494 cookie.rels = NULL;
8495 cookie.rel = NULL;
8496 cookie.relend = NULL;
8497 if (eh->reloc_count)
8498 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
40b829d4 8499 (abfd, eh, (PTR) NULL, (Elf_Internal_Rela *) NULL,
65765700
JJ
8500 info->keep_memory));
8501 if (cookie.rels)
8502 {
8503 cookie.rel = cookie.rels;
8504 cookie.relend =
8505 cookie.rels + eh->reloc_count * bed->s->int_rels_per_ext_rel;
8506 }
8507 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, ehdr,
8508 elf_reloc_symbol_deleted_p,
8509 &cookie))
8510 ret = true;
6cdc0ccc 8511 if (cookie.rels && elf_section_data (eh)->relocs != cookie.rels)
65765700
JJ
8512 free (cookie.rels);
8513 }
8514
73d074b4
DJ
8515 if (bed->elf_backend_discard_info)
8516 {
8517 if (bed->elf_backend_discard_info (abfd, &cookie, info))
8518 ret = true;
8519 }
8520
6cdc0ccc
AM
8521 if (cookie.locsyms != NULL
8522 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
8523 {
8524 if (! info->keep_memory)
8525 free (cookie.locsyms);
8526 else
8527 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
8528 }
73d074b4 8529 }
65765700 8530
40b829d4 8531 if (ehdr && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info, ehdr))
65765700 8532 ret = true;
73d074b4
DJ
8533 return ret;
8534}
8535
8536static boolean
8537elf_section_ignore_discarded_relocs (sec)
8538 asection *sec;
8539{
40b829d4
AM
8540 struct elf_backend_data *bed;
8541
65765700
JJ
8542 switch (elf_section_data (sec)->sec_info_type)
8543 {
8544 case ELF_INFO_TYPE_STABS:
8545 case ELF_INFO_TYPE_EH_FRAME:
8546 return true;
8547 default:
8548 break;
8549 }
40b829d4
AM
8550
8551 bed = get_elf_backend_data (sec->owner);
8552 if (bed->elf_backend_ignore_discarded_relocs != NULL
8553 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
73d074b4 8554 return true;
65765700
JJ
8555
8556 return false;
73d074b4 8557}
This page took 0.634671 seconds and 4 git commands to generate.