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