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