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