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