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