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