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