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