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