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