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