* elf32-arm.h (elf32_arm_size_dynamic_sections): When removing
[deliverable/binutils-gdb.git] / bfd / elflink.h
1 /* ELF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 /* ELF linker code. */
22
23 /* This struct is used to pass information to routines called via
24 elf_link_hash_traverse which must return failure. */
25
26 struct elf_info_failed
27 {
28 boolean failed;
29 struct bfd_link_info *info;
30 struct bfd_elf_version_tree *verdefs;
31 };
32
33 static boolean is_global_data_symbol_definition
34 PARAMS ((bfd *, Elf_Internal_Sym *));
35 static boolean elf_link_is_defined_archive_symbol
36 PARAMS ((bfd *, carsym *));
37 static boolean elf_link_add_object_symbols
38 PARAMS ((bfd *, struct bfd_link_info *));
39 static boolean elf_link_add_archive_symbols
40 PARAMS ((bfd *, struct bfd_link_info *));
41 static boolean elf_merge_symbol
42 PARAMS ((bfd *, struct bfd_link_info *, const char *,
43 Elf_Internal_Sym *, asection **, bfd_vma *,
44 struct elf_link_hash_entry **, boolean *, boolean *,
45 boolean *, boolean));
46 static boolean elf_add_default_symbol
47 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
48 const char *, Elf_Internal_Sym *, asection **, bfd_vma *,
49 boolean *, boolean, boolean));
50 static boolean elf_export_symbol
51 PARAMS ((struct elf_link_hash_entry *, PTR));
52 static boolean elf_finalize_dynstr
53 PARAMS ((bfd *, struct bfd_link_info *));
54 static boolean elf_fix_symbol_flags
55 PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
56 static boolean elf_adjust_dynamic_symbol
57 PARAMS ((struct elf_link_hash_entry *, PTR));
58 static boolean elf_link_find_version_dependencies
59 PARAMS ((struct elf_link_hash_entry *, PTR));
60 static boolean elf_link_find_version_dependencies
61 PARAMS ((struct elf_link_hash_entry *, PTR));
62 static boolean elf_link_assign_sym_version
63 PARAMS ((struct elf_link_hash_entry *, PTR));
64 static boolean elf_collect_hash_codes
65 PARAMS ((struct elf_link_hash_entry *, PTR));
66 static boolean elf_link_read_relocs_from_section
67 PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
68 static size_t compute_bucket_count
69 PARAMS ((struct bfd_link_info *));
70 static void elf_link_output_relocs
71 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
72 static boolean elf_link_size_reloc_section
73 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
74 static void elf_link_adjust_relocs
75 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
76 struct elf_link_hash_entry **));
77 static int elf_link_sort_cmp1
78 PARAMS ((const void *, const void *));
79 static int elf_link_sort_cmp2
80 PARAMS ((const void *, const void *));
81 static size_t elf_link_sort_relocs
82 PARAMS ((bfd *, struct bfd_link_info *, asection **));
83 static boolean elf_section_ignore_discarded_relocs
84 PARAMS ((asection *));
85
86 /* Given an ELF BFD, add symbols to the global hash table as
87 appropriate. */
88
89 boolean
90 elf_bfd_link_add_symbols (abfd, info)
91 bfd *abfd;
92 struct bfd_link_info *info;
93 {
94 switch (bfd_get_format (abfd))
95 {
96 case bfd_object:
97 return elf_link_add_object_symbols (abfd, info);
98 case bfd_archive:
99 return elf_link_add_archive_symbols (abfd, info);
100 default:
101 bfd_set_error (bfd_error_wrong_format);
102 return false;
103 }
104 }
105 \f
106 /* Return true iff this is a non-common, definition of a non-function symbol. */
107 static boolean
108 is_global_data_symbol_definition (abfd, sym)
109 bfd * abfd ATTRIBUTE_UNUSED;
110 Elf_Internal_Sym * sym;
111 {
112 /* Local symbols do not count, but target specific ones might. */
113 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
114 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
115 return false;
116
117 /* Function symbols do not count. */
118 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
119 return false;
120
121 /* If the section is undefined, then so is the symbol. */
122 if (sym->st_shndx == SHN_UNDEF)
123 return false;
124
125 /* If the symbol is defined in the common section, then
126 it is a common definition and so does not count. */
127 if (sym->st_shndx == SHN_COMMON)
128 return false;
129
130 /* If the symbol is in a target specific section then we
131 must rely upon the backend to tell us what it is. */
132 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
133 /* FIXME - this function is not coded yet:
134
135 return _bfd_is_global_symbol_definition (abfd, sym);
136
137 Instead for now assume that the definition is not global,
138 Even if this is wrong, at least the linker will behave
139 in the same way that it used to do. */
140 return false;
141
142 return true;
143 }
144
145 /* Search the symbol table of the archive element of the archive ABFD
146 whose archive map contains a mention of SYMDEF, and determine if
147 the symbol is defined in this element. */
148 static boolean
149 elf_link_is_defined_archive_symbol (abfd, symdef)
150 bfd * abfd;
151 carsym * symdef;
152 {
153 Elf_Internal_Shdr * hdr;
154 Elf_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 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
2060 (*bed->elf_backend_hide_symbol) (info, h);
2061 _bfd_elf_strtab_delref (hash_table->dynstr,
2062 h->dynstr_index);
2063 break;
2064 }
2065
2066 if (dt_needed && definition
2067 && (h->elf_link_hash_flags
2068 & ELF_LINK_HASH_REF_REGULAR) != 0)
2069 {
2070 bfd_size_type oldsize;
2071 bfd_size_type strindex;
2072
2073 if (! is_elf_hash_table (info))
2074 goto error_return;
2075
2076 /* The symbol from a DT_NEEDED object is referenced from
2077 the regular object to create a dynamic executable. We
2078 have to make sure there is a DT_NEEDED entry for it. */
2079
2080 dt_needed = false;
2081 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2082 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
2083 elf_dt_soname (abfd), false);
2084 if (strindex == (bfd_size_type) -1)
2085 goto error_return;
2086
2087 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2088 {
2089 asection *sdyn;
2090 Elf_External_Dyn *dyncon, *dynconend;
2091
2092 sdyn = bfd_get_section_by_name (hash_table->dynobj,
2093 ".dynamic");
2094 BFD_ASSERT (sdyn != NULL);
2095
2096 dyncon = (Elf_External_Dyn *) sdyn->contents;
2097 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2098 sdyn->_raw_size);
2099 for (; dyncon < dynconend; dyncon++)
2100 {
2101 Elf_Internal_Dyn dyn;
2102
2103 elf_swap_dyn_in (hash_table->dynobj,
2104 dyncon, &dyn);
2105 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
2106 dyn.d_un.d_val != strindex);
2107 }
2108 }
2109
2110 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
2111 goto error_return;
2112 }
2113 }
2114 }
2115
2116 /* Now set the weakdefs field correctly for all the weak defined
2117 symbols we found. The only way to do this is to search all the
2118 symbols. Since we only need the information for non functions in
2119 dynamic objects, that's the only time we actually put anything on
2120 the list WEAKS. We need this information so that if a regular
2121 object refers to a symbol defined weakly in a dynamic object, the
2122 real symbol in the dynamic object is also put in the dynamic
2123 symbols; we also must arrange for both symbols to point to the
2124 same memory location. We could handle the general case of symbol
2125 aliasing, but a general symbol alias can only be generated in
2126 assembler code, handling it correctly would be very time
2127 consuming, and other ELF linkers don't handle general aliasing
2128 either. */
2129 while (weaks != NULL)
2130 {
2131 struct elf_link_hash_entry *hlook;
2132 asection *slook;
2133 bfd_vma vlook;
2134 struct elf_link_hash_entry **hpp;
2135 struct elf_link_hash_entry **hppend;
2136
2137 hlook = weaks;
2138 weaks = hlook->weakdef;
2139 hlook->weakdef = NULL;
2140
2141 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
2142 || hlook->root.type == bfd_link_hash_defweak
2143 || hlook->root.type == bfd_link_hash_common
2144 || hlook->root.type == bfd_link_hash_indirect);
2145 slook = hlook->root.u.def.section;
2146 vlook = hlook->root.u.def.value;
2147
2148 hpp = elf_sym_hashes (abfd);
2149 hppend = hpp + extsymcount;
2150 for (; hpp < hppend; hpp++)
2151 {
2152 struct elf_link_hash_entry *h;
2153
2154 h = *hpp;
2155 if (h != NULL && h != hlook
2156 && h->root.type == bfd_link_hash_defined
2157 && h->root.u.def.section == slook
2158 && h->root.u.def.value == vlook)
2159 {
2160 hlook->weakdef = h;
2161
2162 /* If the weak definition is in the list of dynamic
2163 symbols, make sure the real definition is put there
2164 as well. */
2165 if (hlook->dynindx != -1
2166 && h->dynindx == -1)
2167 {
2168 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2169 goto error_return;
2170 }
2171
2172 /* If the real definition is in the list of dynamic
2173 symbols, make sure the weak definition is put there
2174 as well. If we don't do this, then the dynamic
2175 loader might not merge the entries for the real
2176 definition and the weak definition. */
2177 if (h->dynindx != -1
2178 && hlook->dynindx == -1)
2179 {
2180 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
2181 goto error_return;
2182 }
2183
2184 break;
2185 }
2186 }
2187 }
2188
2189 if (buf != NULL)
2190 {
2191 free (buf);
2192 buf = NULL;
2193 }
2194
2195 if (extversym != NULL)
2196 {
2197 free (extversym);
2198 extversym = NULL;
2199 }
2200
2201 /* If this object is the same format as the output object, and it is
2202 not a shared library, then let the backend look through the
2203 relocs.
2204
2205 This is required to build global offset table entries and to
2206 arrange for dynamic relocs. It is not required for the
2207 particular common case of linking non PIC code, even when linking
2208 against shared libraries, but unfortunately there is no way of
2209 knowing whether an object file has been compiled PIC or not.
2210 Looking through the relocs is not particularly time consuming.
2211 The problem is that we must either (1) keep the relocs in memory,
2212 which causes the linker to require additional runtime memory or
2213 (2) read the relocs twice from the input file, which wastes time.
2214 This would be a good case for using mmap.
2215
2216 I have no idea how to handle linking PIC code into a file of a
2217 different format. It probably can't be done. */
2218 check_relocs = get_elf_backend_data (abfd)->check_relocs;
2219 if (! dynamic
2220 && abfd->xvec == info->hash->creator
2221 && check_relocs != NULL)
2222 {
2223 asection *o;
2224
2225 for (o = abfd->sections; o != NULL; o = o->next)
2226 {
2227 Elf_Internal_Rela *internal_relocs;
2228 boolean ok;
2229
2230 if ((o->flags & SEC_RELOC) == 0
2231 || o->reloc_count == 0
2232 || ((info->strip == strip_all || info->strip == strip_debugger)
2233 && (o->flags & SEC_DEBUGGING) != 0)
2234 || bfd_is_abs_section (o->output_section))
2235 continue;
2236
2237 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
2238 (abfd, o, (PTR) NULL,
2239 (Elf_Internal_Rela *) NULL,
2240 info->keep_memory));
2241 if (internal_relocs == NULL)
2242 goto error_return;
2243
2244 ok = (*check_relocs) (abfd, info, o, internal_relocs);
2245
2246 if (! info->keep_memory)
2247 free (internal_relocs);
2248
2249 if (! ok)
2250 goto error_return;
2251 }
2252 }
2253
2254 /* If this is a non-traditional, non-relocateable link, try to
2255 optimize the handling of the .stab/.stabstr sections. */
2256 if (! dynamic
2257 && ! info->relocateable
2258 && ! info->traditional_format
2259 && info->hash->creator->flavour == bfd_target_elf_flavour
2260 && is_elf_hash_table (info)
2261 && (info->strip != strip_all && info->strip != strip_debugger))
2262 {
2263 asection *stab, *stabstr;
2264
2265 stab = bfd_get_section_by_name (abfd, ".stab");
2266 if (stab != NULL && !(stab->flags & SEC_MERGE))
2267 {
2268 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
2269
2270 if (stabstr != NULL)
2271 {
2272 struct bfd_elf_section_data *secdata;
2273
2274 secdata = elf_section_data (stab);
2275 if (! _bfd_link_section_stabs (abfd,
2276 & hash_table->stab_info,
2277 stab, stabstr,
2278 &secdata->sec_info))
2279 goto error_return;
2280 if (secdata->sec_info)
2281 secdata->sec_info_type = ELF_INFO_TYPE_STABS;
2282 }
2283 }
2284 }
2285
2286 if (! info->relocateable && ! dynamic
2287 && is_elf_hash_table (info))
2288 {
2289 asection *s;
2290
2291 for (s = abfd->sections; s != NULL; s = s->next)
2292 if (s->flags & SEC_MERGE)
2293 {
2294 struct bfd_elf_section_data *secdata;
2295
2296 secdata = elf_section_data (s);
2297 if (! _bfd_merge_section (abfd,
2298 & hash_table->merge_info,
2299 s, &secdata->sec_info))
2300 goto error_return;
2301 else if (secdata->sec_info)
2302 secdata->sec_info_type = ELF_INFO_TYPE_MERGE;
2303 }
2304 }
2305
2306 return true;
2307
2308 error_return:
2309 if (buf != NULL)
2310 free (buf);
2311 if (dynbuf != NULL)
2312 free (dynbuf);
2313 if (extversym != NULL)
2314 free (extversym);
2315 return false;
2316 }
2317
2318 /* Create some sections which will be filled in with dynamic linking
2319 information. ABFD is an input file which requires dynamic sections
2320 to be created. The dynamic sections take up virtual memory space
2321 when the final executable is run, so we need to create them before
2322 addresses are assigned to the output sections. We work out the
2323 actual contents and size of these sections later. */
2324
2325 boolean
2326 elf_link_create_dynamic_sections (abfd, info)
2327 bfd *abfd;
2328 struct bfd_link_info *info;
2329 {
2330 flagword flags;
2331 register asection *s;
2332 struct elf_link_hash_entry *h;
2333 struct elf_backend_data *bed;
2334
2335 if (! is_elf_hash_table (info))
2336 return false;
2337
2338 if (elf_hash_table (info)->dynamic_sections_created)
2339 return true;
2340
2341 /* Make sure that all dynamic sections use the same input BFD. */
2342 if (elf_hash_table (info)->dynobj == NULL)
2343 elf_hash_table (info)->dynobj = abfd;
2344 else
2345 abfd = elf_hash_table (info)->dynobj;
2346
2347 /* Note that we set the SEC_IN_MEMORY flag for all of these
2348 sections. */
2349 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2350 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2351
2352 /* A dynamically linked executable has a .interp section, but a
2353 shared library does not. */
2354 if (! info->shared)
2355 {
2356 s = bfd_make_section (abfd, ".interp");
2357 if (s == NULL
2358 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2359 return false;
2360 }
2361
2362 if (! info->traditional_format
2363 && info->hash->creator->flavour == bfd_target_elf_flavour)
2364 {
2365 s = bfd_make_section (abfd, ".eh_frame_hdr");
2366 if (s == NULL
2367 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2368 || ! bfd_set_section_alignment (abfd, s, 2))
2369 return false;
2370 }
2371
2372 /* Create sections to hold version informations. These are removed
2373 if they are not needed. */
2374 s = bfd_make_section (abfd, ".gnu.version_d");
2375 if (s == NULL
2376 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2377 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2378 return false;
2379
2380 s = bfd_make_section (abfd, ".gnu.version");
2381 if (s == NULL
2382 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2383 || ! bfd_set_section_alignment (abfd, s, 1))
2384 return false;
2385
2386 s = bfd_make_section (abfd, ".gnu.version_r");
2387 if (s == NULL
2388 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2389 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2390 return false;
2391
2392 s = bfd_make_section (abfd, ".dynsym");
2393 if (s == NULL
2394 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2395 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2396 return false;
2397
2398 s = bfd_make_section (abfd, ".dynstr");
2399 if (s == NULL
2400 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2401 return false;
2402
2403 /* Create a strtab to hold the dynamic symbol names. */
2404 if (elf_hash_table (info)->dynstr == NULL)
2405 {
2406 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
2407 if (elf_hash_table (info)->dynstr == NULL)
2408 return false;
2409 }
2410
2411 s = bfd_make_section (abfd, ".dynamic");
2412 if (s == NULL
2413 || ! bfd_set_section_flags (abfd, s, flags)
2414 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2415 return false;
2416
2417 /* The special symbol _DYNAMIC is always set to the start of the
2418 .dynamic section. This call occurs before we have processed the
2419 symbols for any dynamic object, so we don't have to worry about
2420 overriding a dynamic definition. We could set _DYNAMIC in a
2421 linker script, but we only want to define it if we are, in fact,
2422 creating a .dynamic section. We don't want to define it if there
2423 is no .dynamic section, since on some ELF platforms the start up
2424 code examines it to decide how to initialize the process. */
2425 h = NULL;
2426 if (! (_bfd_generic_link_add_one_symbol
2427 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
2428 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
2429 (struct bfd_link_hash_entry **) &h)))
2430 return false;
2431 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2432 h->type = STT_OBJECT;
2433
2434 if (info->shared
2435 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2436 return false;
2437
2438 bed = get_elf_backend_data (abfd);
2439
2440 s = bfd_make_section (abfd, ".hash");
2441 if (s == NULL
2442 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2443 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2444 return false;
2445 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
2446
2447 /* Let the backend create the rest of the sections. This lets the
2448 backend set the right flags. The backend will normally create
2449 the .got and .plt sections. */
2450 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
2451 return false;
2452
2453 elf_hash_table (info)->dynamic_sections_created = true;
2454
2455 return true;
2456 }
2457
2458 /* Add an entry to the .dynamic table. */
2459
2460 boolean
2461 elf_add_dynamic_entry (info, tag, val)
2462 struct bfd_link_info *info;
2463 bfd_vma tag;
2464 bfd_vma val;
2465 {
2466 Elf_Internal_Dyn dyn;
2467 bfd *dynobj;
2468 asection *s;
2469 bfd_size_type newsize;
2470 bfd_byte *newcontents;
2471
2472 if (! is_elf_hash_table (info))
2473 return false;
2474
2475 dynobj = elf_hash_table (info)->dynobj;
2476
2477 s = bfd_get_section_by_name (dynobj, ".dynamic");
2478 BFD_ASSERT (s != NULL);
2479
2480 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2481 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2482 if (newcontents == NULL)
2483 return false;
2484
2485 dyn.d_tag = tag;
2486 dyn.d_un.d_val = val;
2487 elf_swap_dyn_out (dynobj, &dyn,
2488 (Elf_External_Dyn *) (newcontents + s->_raw_size));
2489
2490 s->_raw_size = newsize;
2491 s->contents = newcontents;
2492
2493 return true;
2494 }
2495
2496 /* Record a new local dynamic symbol. */
2497
2498 boolean
2499 elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
2500 struct bfd_link_info *info;
2501 bfd *input_bfd;
2502 long input_indx;
2503 {
2504 struct elf_link_local_dynamic_entry *entry;
2505 struct elf_link_hash_table *eht;
2506 struct elf_strtab_hash *dynstr;
2507 Elf_External_Sym esym;
2508 Elf_External_Sym_Shndx eshndx;
2509 Elf_External_Sym_Shndx *shndx;
2510 unsigned long dynstr_index;
2511 char *name;
2512 file_ptr pos;
2513 bfd_size_type amt;
2514
2515 if (! is_elf_hash_table (info))
2516 return false;
2517
2518 /* See if the entry exists already. */
2519 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2520 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
2521 return true;
2522
2523 entry = (struct elf_link_local_dynamic_entry *)
2524 bfd_alloc (input_bfd, (bfd_size_type) sizeof (*entry));
2525 if (entry == NULL)
2526 return false;
2527
2528 /* Go find the symbol, so that we can find it's name. */
2529 amt = sizeof (Elf_External_Sym);
2530 pos = elf_tdata (input_bfd)->symtab_hdr.sh_offset + input_indx * amt;
2531 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2532 || bfd_bread ((PTR) &esym, amt, input_bfd) != amt)
2533 return false;
2534 shndx = NULL;
2535 if (elf_tdata (input_bfd)->symtab_shndx_hdr.sh_size != 0)
2536 {
2537 amt = sizeof (Elf_External_Sym_Shndx);
2538 pos = elf_tdata (input_bfd)->symtab_shndx_hdr.sh_offset;
2539 pos += input_indx * amt;
2540 shndx = &eshndx;
2541 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2542 || bfd_bread ((PTR) shndx, amt, input_bfd) != amt)
2543 return false;
2544 }
2545 elf_swap_symbol_in (input_bfd, &esym, shndx, &entry->isym);
2546
2547 name = (bfd_elf_string_from_elf_section
2548 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
2549 entry->isym.st_name));
2550
2551 dynstr = elf_hash_table (info)->dynstr;
2552 if (dynstr == NULL)
2553 {
2554 /* Create a strtab to hold the dynamic symbol names. */
2555 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
2556 if (dynstr == NULL)
2557 return false;
2558 }
2559
2560 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
2561 if (dynstr_index == (unsigned long) -1)
2562 return false;
2563 entry->isym.st_name = dynstr_index;
2564
2565 eht = elf_hash_table (info);
2566
2567 entry->next = eht->dynlocal;
2568 eht->dynlocal = entry;
2569 entry->input_bfd = input_bfd;
2570 entry->input_indx = input_indx;
2571 eht->dynsymcount++;
2572
2573 /* Whatever binding the symbol had before, it's now local. */
2574 entry->isym.st_info
2575 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
2576
2577 /* The dynindx will be set at the end of size_dynamic_sections. */
2578
2579 return true;
2580 }
2581 \f
2582 /* Read and swap the relocs from the section indicated by SHDR. This
2583 may be either a REL or a RELA section. The relocations are
2584 translated into RELA relocations and stored in INTERNAL_RELOCS,
2585 which should have already been allocated to contain enough space.
2586 The EXTERNAL_RELOCS are a buffer where the external form of the
2587 relocations should be stored.
2588
2589 Returns false if something goes wrong. */
2590
2591 static boolean
2592 elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2593 internal_relocs)
2594 bfd *abfd;
2595 Elf_Internal_Shdr *shdr;
2596 PTR external_relocs;
2597 Elf_Internal_Rela *internal_relocs;
2598 {
2599 struct elf_backend_data *bed;
2600 bfd_size_type amt;
2601
2602 /* If there aren't any relocations, that's OK. */
2603 if (!shdr)
2604 return true;
2605
2606 /* Position ourselves at the start of the section. */
2607 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2608 return false;
2609
2610 /* Read the relocations. */
2611 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2612 return false;
2613
2614 bed = get_elf_backend_data (abfd);
2615
2616 /* Convert the external relocations to the internal format. */
2617 if (shdr->sh_entsize == sizeof (Elf_External_Rel))
2618 {
2619 Elf_External_Rel *erel;
2620 Elf_External_Rel *erelend;
2621 Elf_Internal_Rela *irela;
2622 Elf_Internal_Rel *irel;
2623
2624 erel = (Elf_External_Rel *) external_relocs;
2625 erelend = erel + NUM_SHDR_ENTRIES (shdr);
2626 irela = internal_relocs;
2627 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
2628 irel = bfd_alloc (abfd, amt);
2629 for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
2630 {
2631 unsigned int i;
2632
2633 if (bed->s->swap_reloc_in)
2634 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
2635 else
2636 elf_swap_reloc_in (abfd, erel, irel);
2637
2638 for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i)
2639 {
2640 irela[i].r_offset = irel[i].r_offset;
2641 irela[i].r_info = irel[i].r_info;
2642 irela[i].r_addend = 0;
2643 }
2644 }
2645 }
2646 else
2647 {
2648 Elf_External_Rela *erela;
2649 Elf_External_Rela *erelaend;
2650 Elf_Internal_Rela *irela;
2651
2652 BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
2653
2654 erela = (Elf_External_Rela *) external_relocs;
2655 erelaend = erela + NUM_SHDR_ENTRIES (shdr);
2656 irela = internal_relocs;
2657 for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
2658 {
2659 if (bed->s->swap_reloca_in)
2660 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
2661 else
2662 elf_swap_reloca_in (abfd, erela, irela);
2663 }
2664 }
2665
2666 return true;
2667 }
2668
2669 /* Read and swap the relocs for a section O. They may have been
2670 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2671 not NULL, they are used as buffers to read into. They are known to
2672 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2673 the return value is allocated using either malloc or bfd_alloc,
2674 according to the KEEP_MEMORY argument. If O has two relocation
2675 sections (both REL and RELA relocations), then the REL_HDR
2676 relocations will appear first in INTERNAL_RELOCS, followed by the
2677 REL_HDR2 relocations. */
2678
2679 Elf_Internal_Rela *
2680 NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2681 keep_memory)
2682 bfd *abfd;
2683 asection *o;
2684 PTR external_relocs;
2685 Elf_Internal_Rela *internal_relocs;
2686 boolean keep_memory;
2687 {
2688 Elf_Internal_Shdr *rel_hdr;
2689 PTR alloc1 = NULL;
2690 Elf_Internal_Rela *alloc2 = NULL;
2691 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2692
2693 if (elf_section_data (o)->relocs != NULL)
2694 return elf_section_data (o)->relocs;
2695
2696 if (o->reloc_count == 0)
2697 return NULL;
2698
2699 rel_hdr = &elf_section_data (o)->rel_hdr;
2700
2701 if (internal_relocs == NULL)
2702 {
2703 bfd_size_type size;
2704
2705 size = o->reloc_count;
2706 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2707 if (keep_memory)
2708 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2709 else
2710 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2711 if (internal_relocs == NULL)
2712 goto error_return;
2713 }
2714
2715 if (external_relocs == NULL)
2716 {
2717 bfd_size_type size = rel_hdr->sh_size;
2718
2719 if (elf_section_data (o)->rel_hdr2)
2720 size += elf_section_data (o)->rel_hdr2->sh_size;
2721 alloc1 = (PTR) bfd_malloc (size);
2722 if (alloc1 == NULL)
2723 goto error_return;
2724 external_relocs = alloc1;
2725 }
2726
2727 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2728 external_relocs,
2729 internal_relocs))
2730 goto error_return;
2731 if (!elf_link_read_relocs_from_section
2732 (abfd,
2733 elf_section_data (o)->rel_hdr2,
2734 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2735 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2736 * bed->s->int_rels_per_ext_rel)))
2737 goto error_return;
2738
2739 /* Cache the results for next time, if we can. */
2740 if (keep_memory)
2741 elf_section_data (o)->relocs = internal_relocs;
2742
2743 if (alloc1 != NULL)
2744 free (alloc1);
2745
2746 /* Don't free alloc2, since if it was allocated we are passing it
2747 back (under the name of internal_relocs). */
2748
2749 return internal_relocs;
2750
2751 error_return:
2752 if (alloc1 != NULL)
2753 free (alloc1);
2754 if (alloc2 != NULL)
2755 free (alloc2);
2756 return NULL;
2757 }
2758 \f
2759 /* Record an assignment to a symbol made by a linker script. We need
2760 this in case some dynamic object refers to this symbol. */
2761
2762 boolean
2763 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
2764 bfd *output_bfd ATTRIBUTE_UNUSED;
2765 struct bfd_link_info *info;
2766 const char *name;
2767 boolean provide;
2768 {
2769 struct elf_link_hash_entry *h;
2770
2771 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2772 return true;
2773
2774 h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
2775 if (h == NULL)
2776 return false;
2777
2778 if (h->root.type == bfd_link_hash_new)
2779 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
2780
2781 /* If this symbol is being provided by the linker script, and it is
2782 currently defined by a dynamic object, but not by a regular
2783 object, then mark it as undefined so that the generic linker will
2784 force the correct value. */
2785 if (provide
2786 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2787 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2788 h->root.type = bfd_link_hash_undefined;
2789
2790 /* If this symbol is not being provided by the linker script, and it is
2791 currently defined by a dynamic object, but not by a regular object,
2792 then clear out any version information because the symbol will not be
2793 associated with the dynamic object any more. */
2794 if (!provide
2795 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2796 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2797 h->verinfo.verdef = NULL;
2798
2799 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2800
2801 /* When possible, keep the original type of the symbol. */
2802 if (h->type == STT_NOTYPE)
2803 h->type = STT_OBJECT;
2804
2805 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2806 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2807 || info->shared)
2808 && h->dynindx == -1)
2809 {
2810 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2811 return false;
2812
2813 /* If this is a weak defined symbol, and we know a corresponding
2814 real symbol from the same dynamic object, make sure the real
2815 symbol is also made into a dynamic symbol. */
2816 if (h->weakdef != NULL
2817 && h->weakdef->dynindx == -1)
2818 {
2819 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2820 return false;
2821 }
2822 }
2823
2824 return true;
2825 }
2826 \f
2827 /* This structure is used to pass information to
2828 elf_link_assign_sym_version. */
2829
2830 struct elf_assign_sym_version_info
2831 {
2832 /* Output BFD. */
2833 bfd *output_bfd;
2834 /* General link information. */
2835 struct bfd_link_info *info;
2836 /* Version tree. */
2837 struct bfd_elf_version_tree *verdefs;
2838 /* Whether we had a failure. */
2839 boolean failed;
2840 };
2841
2842 /* This structure is used to pass information to
2843 elf_link_find_version_dependencies. */
2844
2845 struct elf_find_verdep_info
2846 {
2847 /* Output BFD. */
2848 bfd *output_bfd;
2849 /* General link information. */
2850 struct bfd_link_info *info;
2851 /* The number of dependencies. */
2852 unsigned int vers;
2853 /* Whether we had a failure. */
2854 boolean failed;
2855 };
2856
2857 /* Array used to determine the number of hash table buckets to use
2858 based on the number of symbols there are. If there are fewer than
2859 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2860 fewer than 37 we use 17 buckets, and so forth. We never use more
2861 than 32771 buckets. */
2862
2863 static const size_t elf_buckets[] =
2864 {
2865 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2866 16411, 32771, 0
2867 };
2868
2869 /* Compute bucket count for hashing table. We do not use a static set
2870 of possible tables sizes anymore. Instead we determine for all
2871 possible reasonable sizes of the table the outcome (i.e., the
2872 number of collisions etc) and choose the best solution. The
2873 weighting functions are not too simple to allow the table to grow
2874 without bounds. Instead one of the weighting factors is the size.
2875 Therefore the result is always a good payoff between few collisions
2876 (= short chain lengths) and table size. */
2877 static size_t
2878 compute_bucket_count (info)
2879 struct bfd_link_info *info;
2880 {
2881 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
2882 size_t best_size = 0;
2883 unsigned long int *hashcodes;
2884 unsigned long int *hashcodesp;
2885 unsigned long int i;
2886 bfd_size_type amt;
2887
2888 /* Compute the hash values for all exported symbols. At the same
2889 time store the values in an array so that we could use them for
2890 optimizations. */
2891 amt = dynsymcount;
2892 amt *= sizeof (unsigned long int);
2893 hashcodes = (unsigned long int *) bfd_malloc (amt);
2894 if (hashcodes == NULL)
2895 return 0;
2896 hashcodesp = hashcodes;
2897
2898 /* Put all hash values in HASHCODES. */
2899 elf_link_hash_traverse (elf_hash_table (info),
2900 elf_collect_hash_codes, &hashcodesp);
2901
2902 /* We have a problem here. The following code to optimize the table
2903 size requires an integer type with more the 32 bits. If
2904 BFD_HOST_U_64_BIT is set we know about such a type. */
2905 #ifdef BFD_HOST_U_64_BIT
2906 if (info->optimize == true)
2907 {
2908 unsigned long int nsyms = hashcodesp - hashcodes;
2909 size_t minsize;
2910 size_t maxsize;
2911 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2912 unsigned long int *counts ;
2913
2914 /* Possible optimization parameters: if we have NSYMS symbols we say
2915 that the hashing table must at least have NSYMS/4 and at most
2916 2*NSYMS buckets. */
2917 minsize = nsyms / 4;
2918 if (minsize == 0)
2919 minsize = 1;
2920 best_size = maxsize = nsyms * 2;
2921
2922 /* Create array where we count the collisions in. We must use bfd_malloc
2923 since the size could be large. */
2924 amt = maxsize;
2925 amt *= sizeof (unsigned long int);
2926 counts = (unsigned long int *) bfd_malloc (amt);
2927 if (counts == NULL)
2928 {
2929 free (hashcodes);
2930 return 0;
2931 }
2932
2933 /* Compute the "optimal" size for the hash table. The criteria is a
2934 minimal chain length. The minor criteria is (of course) the size
2935 of the table. */
2936 for (i = minsize; i < maxsize; ++i)
2937 {
2938 /* Walk through the array of hashcodes and count the collisions. */
2939 BFD_HOST_U_64_BIT max;
2940 unsigned long int j;
2941 unsigned long int fact;
2942
2943 memset (counts, '\0', i * sizeof (unsigned long int));
2944
2945 /* Determine how often each hash bucket is used. */
2946 for (j = 0; j < nsyms; ++j)
2947 ++counts[hashcodes[j] % i];
2948
2949 /* For the weight function we need some information about the
2950 pagesize on the target. This is information need not be 100%
2951 accurate. Since this information is not available (so far) we
2952 define it here to a reasonable default value. If it is crucial
2953 to have a better value some day simply define this value. */
2954 # ifndef BFD_TARGET_PAGESIZE
2955 # define BFD_TARGET_PAGESIZE (4096)
2956 # endif
2957
2958 /* We in any case need 2 + NSYMS entries for the size values and
2959 the chains. */
2960 max = (2 + nsyms) * (ARCH_SIZE / 8);
2961
2962 # if 1
2963 /* Variant 1: optimize for short chains. We add the squares
2964 of all the chain lengths (which favous many small chain
2965 over a few long chains). */
2966 for (j = 0; j < i; ++j)
2967 max += counts[j] * counts[j];
2968
2969 /* This adds penalties for the overall size of the table. */
2970 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2971 max *= fact * fact;
2972 # else
2973 /* Variant 2: Optimize a lot more for small table. Here we
2974 also add squares of the size but we also add penalties for
2975 empty slots (the +1 term). */
2976 for (j = 0; j < i; ++j)
2977 max += (1 + counts[j]) * (1 + counts[j]);
2978
2979 /* The overall size of the table is considered, but not as
2980 strong as in variant 1, where it is squared. */
2981 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2982 max *= fact;
2983 # endif
2984
2985 /* Compare with current best results. */
2986 if (max < best_chlen)
2987 {
2988 best_chlen = max;
2989 best_size = i;
2990 }
2991 }
2992
2993 free (counts);
2994 }
2995 else
2996 #endif /* defined (BFD_HOST_U_64_BIT) */
2997 {
2998 /* This is the fallback solution if no 64bit type is available or if we
2999 are not supposed to spend much time on optimizations. We select the
3000 bucket count using a fixed set of numbers. */
3001 for (i = 0; elf_buckets[i] != 0; i++)
3002 {
3003 best_size = elf_buckets[i];
3004 if (dynsymcount < elf_buckets[i + 1])
3005 break;
3006 }
3007 }
3008
3009 /* Free the arrays we needed. */
3010 free (hashcodes);
3011
3012 return best_size;
3013 }
3014
3015 /* Set up the sizes and contents of the ELF dynamic sections. This is
3016 called by the ELF linker emulation before_allocation routine. We
3017 must set the sizes of the sections before the linker sets the
3018 addresses of the various sections. */
3019
3020 boolean
3021 NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
3022 filter_shlib,
3023 auxiliary_filters, info, sinterpptr,
3024 verdefs)
3025 bfd *output_bfd;
3026 const char *soname;
3027 const char *rpath;
3028 const char *filter_shlib;
3029 const char * const *auxiliary_filters;
3030 struct bfd_link_info *info;
3031 asection **sinterpptr;
3032 struct bfd_elf_version_tree *verdefs;
3033 {
3034 bfd_size_type soname_indx;
3035 bfd *dynobj;
3036 struct elf_backend_data *bed;
3037 struct elf_assign_sym_version_info asvinfo;
3038
3039 *sinterpptr = NULL;
3040
3041 soname_indx = (bfd_size_type) -1;
3042
3043 if (info->hash->creator->flavour != bfd_target_elf_flavour)
3044 return true;
3045
3046 if (! is_elf_hash_table (info))
3047 return false;
3048
3049 /* Any syms created from now on start with -1 in
3050 got.refcount/offset and plt.refcount/offset. */
3051 elf_hash_table (info)->init_refcount = -1;
3052
3053 /* The backend may have to create some sections regardless of whether
3054 we're dynamic or not. */
3055 bed = get_elf_backend_data (output_bfd);
3056 if (bed->elf_backend_always_size_sections
3057 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
3058 return false;
3059
3060 dynobj = elf_hash_table (info)->dynobj;
3061
3062 /* If there were no dynamic objects in the link, there is nothing to
3063 do here. */
3064 if (dynobj == NULL)
3065 return true;
3066
3067 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
3068 return false;
3069
3070 if (elf_hash_table (info)->dynamic_sections_created)
3071 {
3072 struct elf_info_failed eif;
3073 struct elf_link_hash_entry *h;
3074 asection *dynstr;
3075
3076 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
3077 BFD_ASSERT (*sinterpptr != NULL || info->shared);
3078
3079 if (soname != NULL)
3080 {
3081 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3082 soname, true);
3083 if (soname_indx == (bfd_size_type) -1
3084 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
3085 soname_indx))
3086 return false;
3087 }
3088
3089 if (info->symbolic)
3090 {
3091 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
3092 (bfd_vma) 0))
3093 return false;
3094 info->flags |= DF_SYMBOLIC;
3095 }
3096
3097 if (rpath != NULL)
3098 {
3099 bfd_size_type indx;
3100
3101 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
3102 true);
3103 if (info->new_dtags)
3104 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
3105 if (indx == (bfd_size_type) -1
3106 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
3107 || (info->new_dtags
3108 && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
3109 indx)))
3110 return false;
3111 }
3112
3113 if (filter_shlib != NULL)
3114 {
3115 bfd_size_type indx;
3116
3117 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3118 filter_shlib, true);
3119 if (indx == (bfd_size_type) -1
3120 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
3121 return false;
3122 }
3123
3124 if (auxiliary_filters != NULL)
3125 {
3126 const char * const *p;
3127
3128 for (p = auxiliary_filters; *p != NULL; p++)
3129 {
3130 bfd_size_type indx;
3131
3132 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3133 *p, true);
3134 if (indx == (bfd_size_type) -1
3135 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
3136 indx))
3137 return false;
3138 }
3139 }
3140
3141 eif.info = info;
3142 eif.verdefs = verdefs;
3143 eif.failed = false;
3144
3145 /* If we are supposed to export all symbols into the dynamic symbol
3146 table (this is not the normal case), then do so. */
3147 if (info->export_dynamic)
3148 {
3149 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
3150 (PTR) &eif);
3151 if (eif.failed)
3152 return false;
3153 }
3154
3155 /* Attach all the symbols to their version information. */
3156 asvinfo.output_bfd = output_bfd;
3157 asvinfo.info = info;
3158 asvinfo.verdefs = verdefs;
3159 asvinfo.failed = false;
3160
3161 elf_link_hash_traverse (elf_hash_table (info),
3162 elf_link_assign_sym_version,
3163 (PTR) &asvinfo);
3164 if (asvinfo.failed)
3165 return false;
3166
3167 /* Find all symbols which were defined in a dynamic object and make
3168 the backend pick a reasonable value for them. */
3169 elf_link_hash_traverse (elf_hash_table (info),
3170 elf_adjust_dynamic_symbol,
3171 (PTR) &eif);
3172 if (eif.failed)
3173 return false;
3174
3175 /* Add some entries to the .dynamic section. We fill in some of the
3176 values later, in elf_bfd_final_link, but we must add the entries
3177 now so that we know the final size of the .dynamic section. */
3178
3179 /* If there are initialization and/or finalization functions to
3180 call then add the corresponding DT_INIT/DT_FINI entries. */
3181 h = (info->init_function
3182 ? elf_link_hash_lookup (elf_hash_table (info),
3183 info->init_function, false,
3184 false, false)
3185 : NULL);
3186 if (h != NULL
3187 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3188 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3189 {
3190 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
3191 return false;
3192 }
3193 h = (info->fini_function
3194 ? elf_link_hash_lookup (elf_hash_table (info),
3195 info->fini_function, false,
3196 false, false)
3197 : NULL);
3198 if (h != NULL
3199 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3200 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3201 {
3202 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
3203 return false;
3204 }
3205
3206 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
3207 /* If .dynstr is excluded from the link, we don't want any of
3208 these tags. Strictly, we should be checking each section
3209 individually; This quick check covers for the case where
3210 someone does a /DISCARD/ : { *(*) }. */
3211 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
3212 {
3213 bfd_size_type strsize;
3214
3215 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3216 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
3217 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
3218 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
3219 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
3220 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
3221 (bfd_vma) sizeof (Elf_External_Sym)))
3222 return false;
3223 }
3224 }
3225
3226 /* The backend must work out the sizes of all the other dynamic
3227 sections. */
3228 if (bed->elf_backend_size_dynamic_sections
3229 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
3230 return false;
3231
3232 if (elf_hash_table (info)->dynamic_sections_created)
3233 {
3234 bfd_size_type dynsymcount;
3235 asection *s;
3236 size_t bucketcount = 0;
3237 size_t hash_entry_size;
3238 unsigned int dtagcount;
3239
3240 /* Set up the version definition section. */
3241 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3242 BFD_ASSERT (s != NULL);
3243
3244 /* We may have created additional version definitions if we are
3245 just linking a regular application. */
3246 verdefs = asvinfo.verdefs;
3247
3248 /* Skip anonymous version tag. */
3249 if (verdefs != NULL && verdefs->vernum == 0)
3250 verdefs = verdefs->next;
3251
3252 if (verdefs == NULL)
3253 _bfd_strip_section_from_output (info, s);
3254 else
3255 {
3256 unsigned int cdefs;
3257 bfd_size_type size;
3258 struct bfd_elf_version_tree *t;
3259 bfd_byte *p;
3260 Elf_Internal_Verdef def;
3261 Elf_Internal_Verdaux defaux;
3262
3263 cdefs = 0;
3264 size = 0;
3265
3266 /* Make space for the base version. */
3267 size += sizeof (Elf_External_Verdef);
3268 size += sizeof (Elf_External_Verdaux);
3269 ++cdefs;
3270
3271 for (t = verdefs; t != NULL; t = t->next)
3272 {
3273 struct bfd_elf_version_deps *n;
3274
3275 size += sizeof (Elf_External_Verdef);
3276 size += sizeof (Elf_External_Verdaux);
3277 ++cdefs;
3278
3279 for (n = t->deps; n != NULL; n = n->next)
3280 size += sizeof (Elf_External_Verdaux);
3281 }
3282
3283 s->_raw_size = size;
3284 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3285 if (s->contents == NULL && s->_raw_size != 0)
3286 return false;
3287
3288 /* Fill in the version definition section. */
3289
3290 p = s->contents;
3291
3292 def.vd_version = VER_DEF_CURRENT;
3293 def.vd_flags = VER_FLG_BASE;
3294 def.vd_ndx = 1;
3295 def.vd_cnt = 1;
3296 def.vd_aux = sizeof (Elf_External_Verdef);
3297 def.vd_next = (sizeof (Elf_External_Verdef)
3298 + sizeof (Elf_External_Verdaux));
3299
3300 if (soname_indx != (bfd_size_type) -1)
3301 {
3302 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3303 soname_indx);
3304 def.vd_hash = bfd_elf_hash (soname);
3305 defaux.vda_name = soname_indx;
3306 }
3307 else
3308 {
3309 const char *name;
3310 bfd_size_type indx;
3311
3312 name = basename (output_bfd->filename);
3313 def.vd_hash = bfd_elf_hash (name);
3314 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3315 name, false);
3316 if (indx == (bfd_size_type) -1)
3317 return false;
3318 defaux.vda_name = indx;
3319 }
3320 defaux.vda_next = 0;
3321
3322 _bfd_elf_swap_verdef_out (output_bfd, &def,
3323 (Elf_External_Verdef *) p);
3324 p += sizeof (Elf_External_Verdef);
3325 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3326 (Elf_External_Verdaux *) p);
3327 p += sizeof (Elf_External_Verdaux);
3328
3329 for (t = verdefs; t != NULL; t = t->next)
3330 {
3331 unsigned int cdeps;
3332 struct bfd_elf_version_deps *n;
3333 struct elf_link_hash_entry *h;
3334
3335 cdeps = 0;
3336 for (n = t->deps; n != NULL; n = n->next)
3337 ++cdeps;
3338
3339 /* Add a symbol representing this version. */
3340 h = NULL;
3341 if (! (_bfd_generic_link_add_one_symbol
3342 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
3343 (bfd_vma) 0, (const char *) NULL, false,
3344 get_elf_backend_data (dynobj)->collect,
3345 (struct bfd_link_hash_entry **) &h)))
3346 return false;
3347 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
3348 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3349 h->type = STT_OBJECT;
3350 h->verinfo.vertree = t;
3351
3352 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
3353 return false;
3354
3355 def.vd_version = VER_DEF_CURRENT;
3356 def.vd_flags = 0;
3357 if (t->globals == NULL && t->locals == NULL && ! t->used)
3358 def.vd_flags |= VER_FLG_WEAK;
3359 def.vd_ndx = t->vernum + 1;
3360 def.vd_cnt = cdeps + 1;
3361 def.vd_hash = bfd_elf_hash (t->name);
3362 def.vd_aux = sizeof (Elf_External_Verdef);
3363 if (t->next != NULL)
3364 def.vd_next = (sizeof (Elf_External_Verdef)
3365 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
3366 else
3367 def.vd_next = 0;
3368
3369 _bfd_elf_swap_verdef_out (output_bfd, &def,
3370 (Elf_External_Verdef *) p);
3371 p += sizeof (Elf_External_Verdef);
3372
3373 defaux.vda_name = h->dynstr_index;
3374 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3375 h->dynstr_index);
3376 if (t->deps == NULL)
3377 defaux.vda_next = 0;
3378 else
3379 defaux.vda_next = sizeof (Elf_External_Verdaux);
3380 t->name_indx = defaux.vda_name;
3381
3382 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3383 (Elf_External_Verdaux *) p);
3384 p += sizeof (Elf_External_Verdaux);
3385
3386 for (n = t->deps; n != NULL; n = n->next)
3387 {
3388 if (n->version_needed == NULL)
3389 {
3390 /* This can happen if there was an error in the
3391 version script. */
3392 defaux.vda_name = 0;
3393 }
3394 else
3395 {
3396 defaux.vda_name = n->version_needed->name_indx;
3397 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3398 defaux.vda_name);
3399 }
3400 if (n->next == NULL)
3401 defaux.vda_next = 0;
3402 else
3403 defaux.vda_next = sizeof (Elf_External_Verdaux);
3404
3405 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3406 (Elf_External_Verdaux *) p);
3407 p += sizeof (Elf_External_Verdaux);
3408 }
3409 }
3410
3411 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
3412 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
3413 (bfd_vma) cdefs))
3414 return false;
3415
3416 elf_tdata (output_bfd)->cverdefs = cdefs;
3417 }
3418
3419 if (info->new_dtags && info->flags)
3420 {
3421 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
3422 return false;
3423 }
3424
3425 if (info->flags_1)
3426 {
3427 if (! info->shared)
3428 info->flags_1 &= ~ (DF_1_INITFIRST
3429 | DF_1_NODELETE
3430 | DF_1_NOOPEN);
3431 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
3432 info->flags_1))
3433 return false;
3434 }
3435
3436 /* Work out the size of the version reference section. */
3437
3438 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3439 BFD_ASSERT (s != NULL);
3440 {
3441 struct elf_find_verdep_info sinfo;
3442
3443 sinfo.output_bfd = output_bfd;
3444 sinfo.info = info;
3445 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
3446 if (sinfo.vers == 0)
3447 sinfo.vers = 1;
3448 sinfo.failed = false;
3449
3450 elf_link_hash_traverse (elf_hash_table (info),
3451 elf_link_find_version_dependencies,
3452 (PTR) &sinfo);
3453
3454 if (elf_tdata (output_bfd)->verref == NULL)
3455 _bfd_strip_section_from_output (info, s);
3456 else
3457 {
3458 Elf_Internal_Verneed *t;
3459 unsigned int size;
3460 unsigned int crefs;
3461 bfd_byte *p;
3462
3463 /* Build the version definition section. */
3464 size = 0;
3465 crefs = 0;
3466 for (t = elf_tdata (output_bfd)->verref;
3467 t != NULL;
3468 t = t->vn_nextref)
3469 {
3470 Elf_Internal_Vernaux *a;
3471
3472 size += sizeof (Elf_External_Verneed);
3473 ++crefs;
3474 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3475 size += sizeof (Elf_External_Vernaux);
3476 }
3477
3478 s->_raw_size = size;
3479 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3480 if (s->contents == NULL)
3481 return false;
3482
3483 p = s->contents;
3484 for (t = elf_tdata (output_bfd)->verref;
3485 t != NULL;
3486 t = t->vn_nextref)
3487 {
3488 unsigned int caux;
3489 Elf_Internal_Vernaux *a;
3490 bfd_size_type indx;
3491
3492 caux = 0;
3493 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3494 ++caux;
3495
3496 t->vn_version = VER_NEED_CURRENT;
3497 t->vn_cnt = caux;
3498 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3499 elf_dt_name (t->vn_bfd) != NULL
3500 ? elf_dt_name (t->vn_bfd)
3501 : basename (t->vn_bfd->filename),
3502 false);
3503 if (indx == (bfd_size_type) -1)
3504 return false;
3505 t->vn_file = indx;
3506 t->vn_aux = sizeof (Elf_External_Verneed);
3507 if (t->vn_nextref == NULL)
3508 t->vn_next = 0;
3509 else
3510 t->vn_next = (sizeof (Elf_External_Verneed)
3511 + caux * sizeof (Elf_External_Vernaux));
3512
3513 _bfd_elf_swap_verneed_out (output_bfd, t,
3514 (Elf_External_Verneed *) p);
3515 p += sizeof (Elf_External_Verneed);
3516
3517 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3518 {
3519 a->vna_hash = bfd_elf_hash (a->vna_nodename);
3520 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3521 a->vna_nodename, false);
3522 if (indx == (bfd_size_type) -1)
3523 return false;
3524 a->vna_name = indx;
3525 if (a->vna_nextptr == NULL)
3526 a->vna_next = 0;
3527 else
3528 a->vna_next = sizeof (Elf_External_Vernaux);
3529
3530 _bfd_elf_swap_vernaux_out (output_bfd, a,
3531 (Elf_External_Vernaux *) p);
3532 p += sizeof (Elf_External_Vernaux);
3533 }
3534 }
3535
3536 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
3537 (bfd_vma) 0)
3538 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
3539 (bfd_vma) crefs))
3540 return false;
3541
3542 elf_tdata (output_bfd)->cverrefs = crefs;
3543 }
3544 }
3545
3546 /* Assign dynsym indicies. In a shared library we generate a
3547 section symbol for each output section, which come first.
3548 Next come all of the back-end allocated local dynamic syms,
3549 followed by the rest of the global symbols. */
3550
3551 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3552
3553 /* Work out the size of the symbol version section. */
3554 s = bfd_get_section_by_name (dynobj, ".gnu.version");
3555 BFD_ASSERT (s != NULL);
3556 if (dynsymcount == 0
3557 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3558 {
3559 _bfd_strip_section_from_output (info, s);
3560 /* The DYNSYMCOUNT might have changed if we were going to
3561 output a dynamic symbol table entry for S. */
3562 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3563 }
3564 else
3565 {
3566 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3567 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3568 if (s->contents == NULL)
3569 return false;
3570
3571 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
3572 return false;
3573 }
3574
3575 /* Set the size of the .dynsym and .hash sections. We counted
3576 the number of dynamic symbols in elf_link_add_object_symbols.
3577 We will build the contents of .dynsym and .hash when we build
3578 the final symbol table, because until then we do not know the
3579 correct value to give the symbols. We built the .dynstr
3580 section as we went along in elf_link_add_object_symbols. */
3581 s = bfd_get_section_by_name (dynobj, ".dynsym");
3582 BFD_ASSERT (s != NULL);
3583 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3584 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3585 if (s->contents == NULL && s->_raw_size != 0)
3586 return false;
3587
3588 if (dynsymcount != 0)
3589 {
3590 Elf_Internal_Sym isym;
3591
3592 /* The first entry in .dynsym is a dummy symbol. */
3593 isym.st_value = 0;
3594 isym.st_size = 0;
3595 isym.st_name = 0;
3596 isym.st_info = 0;
3597 isym.st_other = 0;
3598 isym.st_shndx = 0;
3599 elf_swap_symbol_out (output_bfd, &isym, (PTR) s->contents, (PTR) 0);
3600 }
3601
3602 /* Compute the size of the hashing table. As a side effect this
3603 computes the hash values for all the names we export. */
3604 bucketcount = compute_bucket_count (info);
3605
3606 s = bfd_get_section_by_name (dynobj, ".hash");
3607 BFD_ASSERT (s != NULL);
3608 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3609 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
3610 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3611 if (s->contents == NULL)
3612 return false;
3613 memset (s->contents, 0, (size_t) s->_raw_size);
3614
3615 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
3616 s->contents);
3617 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
3618 s->contents + hash_entry_size);
3619
3620 elf_hash_table (info)->bucketcount = bucketcount;
3621
3622 s = bfd_get_section_by_name (dynobj, ".dynstr");
3623 BFD_ASSERT (s != NULL);
3624
3625 elf_finalize_dynstr (output_bfd, info);
3626
3627 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3628
3629 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
3630 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
3631 return false;
3632 }
3633
3634 return true;
3635 }
3636 \f
3637 /* This function is used to adjust offsets into .dynstr for
3638 dynamic symbols. This is called via elf_link_hash_traverse. */
3639
3640 static boolean elf_adjust_dynstr_offsets
3641 PARAMS ((struct elf_link_hash_entry *, PTR));
3642
3643 static boolean
3644 elf_adjust_dynstr_offsets (h, data)
3645 struct elf_link_hash_entry *h;
3646 PTR data;
3647 {
3648 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3649
3650 if (h->dynindx != -1)
3651 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3652 return true;
3653 }
3654
3655 /* Assign string offsets in .dynstr, update all structures referencing
3656 them. */
3657
3658 static boolean
3659 elf_finalize_dynstr (output_bfd, info)
3660 bfd *output_bfd;
3661 struct bfd_link_info *info;
3662 {
3663 struct elf_link_local_dynamic_entry *entry;
3664 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
3665 bfd *dynobj = elf_hash_table (info)->dynobj;
3666 asection *sdyn;
3667 bfd_size_type size;
3668 Elf_External_Dyn *dyncon, *dynconend;
3669
3670 _bfd_elf_strtab_finalize (dynstr);
3671 size = _bfd_elf_strtab_size (dynstr);
3672
3673 /* Update all .dynamic entries referencing .dynstr strings. */
3674 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3675 BFD_ASSERT (sdyn != NULL);
3676
3677 dyncon = (Elf_External_Dyn *) sdyn->contents;
3678 dynconend = (Elf_External_Dyn *) (sdyn->contents +
3679 sdyn->_raw_size);
3680 for (; dyncon < dynconend; dyncon++)
3681 {
3682 Elf_Internal_Dyn dyn;
3683
3684 elf_swap_dyn_in (dynobj, dyncon, & dyn);
3685 switch (dyn.d_tag)
3686 {
3687 case DT_STRSZ:
3688 dyn.d_un.d_val = size;
3689 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3690 break;
3691 case DT_NEEDED:
3692 case DT_SONAME:
3693 case DT_RPATH:
3694 case DT_RUNPATH:
3695 case DT_FILTER:
3696 case DT_AUXILIARY:
3697 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3698 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3699 break;
3700 default:
3701 break;
3702 }
3703 }
3704
3705 /* Now update local dynamic symbols. */
3706 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
3707 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3708 entry->isym.st_name);
3709
3710 /* And the rest of dynamic symbols. */
3711 elf_link_hash_traverse (elf_hash_table (info),
3712 elf_adjust_dynstr_offsets, dynstr);
3713
3714 /* Adjust version definitions. */
3715 if (elf_tdata (output_bfd)->cverdefs)
3716 {
3717 asection *s;
3718 bfd_byte *p;
3719 bfd_size_type i;
3720 Elf_Internal_Verdef def;
3721 Elf_Internal_Verdaux defaux;
3722
3723 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3724 p = (bfd_byte *) s->contents;
3725 do
3726 {
3727 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3728 &def);
3729 p += sizeof (Elf_External_Verdef);
3730 for (i = 0; i < def.vd_cnt; ++i)
3731 {
3732 _bfd_elf_swap_verdaux_in (output_bfd,
3733 (Elf_External_Verdaux *) p, &defaux);
3734 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3735 defaux.vda_name);
3736 _bfd_elf_swap_verdaux_out (output_bfd,
3737 &defaux, (Elf_External_Verdaux *) p);
3738 p += sizeof (Elf_External_Verdaux);
3739 }
3740 }
3741 while (def.vd_next);
3742 }
3743
3744 /* Adjust version references. */
3745 if (elf_tdata (output_bfd)->verref)
3746 {
3747 asection *s;
3748 bfd_byte *p;
3749 bfd_size_type i;
3750 Elf_Internal_Verneed need;
3751 Elf_Internal_Vernaux needaux;
3752
3753 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3754 p = (bfd_byte *) s->contents;
3755 do
3756 {
3757 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3758 &need);
3759 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3760 _bfd_elf_swap_verneed_out (output_bfd, &need,
3761 (Elf_External_Verneed *) p);
3762 p += sizeof (Elf_External_Verneed);
3763 for (i = 0; i < need.vn_cnt; ++i)
3764 {
3765 _bfd_elf_swap_vernaux_in (output_bfd,
3766 (Elf_External_Vernaux *) p, &needaux);
3767 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3768 needaux.vna_name);
3769 _bfd_elf_swap_vernaux_out (output_bfd,
3770 &needaux,
3771 (Elf_External_Vernaux *) p);
3772 p += sizeof (Elf_External_Vernaux);
3773 }
3774 }
3775 while (need.vn_next);
3776 }
3777
3778 return true;
3779 }
3780
3781 /* Fix up the flags for a symbol. This handles various cases which
3782 can only be fixed after all the input files are seen. This is
3783 currently called by both adjust_dynamic_symbol and
3784 assign_sym_version, which is unnecessary but perhaps more robust in
3785 the face of future changes. */
3786
3787 static boolean
3788 elf_fix_symbol_flags (h, eif)
3789 struct elf_link_hash_entry *h;
3790 struct elf_info_failed *eif;
3791 {
3792 /* If this symbol was mentioned in a non-ELF file, try to set
3793 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3794 permit a non-ELF file to correctly refer to a symbol defined in
3795 an ELF dynamic object. */
3796 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3797 {
3798 while (h->root.type == bfd_link_hash_indirect)
3799 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3800
3801 if (h->root.type != bfd_link_hash_defined
3802 && h->root.type != bfd_link_hash_defweak)
3803 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3804 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3805 else
3806 {
3807 if (h->root.u.def.section->owner != NULL
3808 && (bfd_get_flavour (h->root.u.def.section->owner)
3809 == bfd_target_elf_flavour))
3810 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3811 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3812 else
3813 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3814 }
3815
3816 if (h->dynindx == -1
3817 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3818 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3819 {
3820 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3821 {
3822 eif->failed = true;
3823 return false;
3824 }
3825 }
3826 }
3827 else
3828 {
3829 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3830 was first seen in a non-ELF file. Fortunately, if the symbol
3831 was first seen in an ELF file, we're probably OK unless the
3832 symbol was defined in a non-ELF file. Catch that case here.
3833 FIXME: We're still in trouble if the symbol was first seen in
3834 a dynamic object, and then later in a non-ELF regular object. */
3835 if ((h->root.type == bfd_link_hash_defined
3836 || h->root.type == bfd_link_hash_defweak)
3837 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3838 && (h->root.u.def.section->owner != NULL
3839 ? (bfd_get_flavour (h->root.u.def.section->owner)
3840 != bfd_target_elf_flavour)
3841 : (bfd_is_abs_section (h->root.u.def.section)
3842 && (h->elf_link_hash_flags
3843 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3844 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3845 }
3846
3847 /* If this is a final link, and the symbol was defined as a common
3848 symbol in a regular object file, and there was no definition in
3849 any dynamic object, then the linker will have allocated space for
3850 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3851 flag will not have been set. */
3852 if (h->root.type == bfd_link_hash_defined
3853 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3854 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3855 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3856 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3857 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3858
3859 /* If -Bsymbolic was used (which means to bind references to global
3860 symbols to the definition within the shared object), and this
3861 symbol was defined in a regular object, then it actually doesn't
3862 need a PLT entry, and we can accomplish that by forcing it local.
3863 Likewise, if the symbol has hidden or internal visibility.
3864 FIXME: It might be that we also do not need a PLT for other
3865 non-hidden visibilities, but we would have to tell that to the
3866 backend specifically; we can't just clear PLT-related data here. */
3867 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3868 && eif->info->shared
3869 && is_elf_hash_table (eif->info)
3870 && (eif->info->symbolic
3871 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3872 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3873 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3874 {
3875 struct elf_backend_data *bed;
3876
3877 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3878 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3879 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3880 {
3881 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
3882 _bfd_elf_strtab_delref (elf_hash_table (eif->info)->dynstr,
3883 h->dynstr_index);
3884 }
3885 (*bed->elf_backend_hide_symbol) (eif->info, h);
3886 }
3887
3888 /* If this is a weak defined symbol in a dynamic object, and we know
3889 the real definition in the dynamic object, copy interesting flags
3890 over to the real definition. */
3891 if (h->weakdef != NULL)
3892 {
3893 struct elf_link_hash_entry *weakdef;
3894
3895 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3896 || h->root.type == bfd_link_hash_defweak);
3897 weakdef = h->weakdef;
3898 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
3899 || weakdef->root.type == bfd_link_hash_defweak);
3900 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
3901
3902 /* If the real definition is defined by a regular object file,
3903 don't do anything special. See the longer description in
3904 elf_adjust_dynamic_symbol, below. */
3905 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3906 h->weakdef = NULL;
3907 else
3908 {
3909 struct elf_backend_data *bed;
3910
3911 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3912 (*bed->elf_backend_copy_indirect_symbol) (weakdef, h);
3913 }
3914 }
3915
3916 return true;
3917 }
3918
3919 /* Make the backend pick a good value for a dynamic symbol. This is
3920 called via elf_link_hash_traverse, and also calls itself
3921 recursively. */
3922
3923 static boolean
3924 elf_adjust_dynamic_symbol (h, data)
3925 struct elf_link_hash_entry *h;
3926 PTR data;
3927 {
3928 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3929 bfd *dynobj;
3930 struct elf_backend_data *bed;
3931
3932 /* Ignore indirect symbols. These are added by the versioning code. */
3933 if (h->root.type == bfd_link_hash_indirect)
3934 return true;
3935
3936 if (! is_elf_hash_table (eif->info))
3937 return false;
3938
3939 /* Fix the symbol flags. */
3940 if (! elf_fix_symbol_flags (h, eif))
3941 return false;
3942
3943 /* If this symbol does not require a PLT entry, and it is not
3944 defined by a dynamic object, or is not referenced by a regular
3945 object, ignore it. We do have to handle a weak defined symbol,
3946 even if no regular object refers to it, if we decided to add it
3947 to the dynamic symbol table. FIXME: Do we normally need to worry
3948 about symbols which are defined by one dynamic object and
3949 referenced by another one? */
3950 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
3951 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3952 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3953 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
3954 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
3955 {
3956 h->plt.offset = (bfd_vma) -1;
3957 return true;
3958 }
3959
3960 /* If we've already adjusted this symbol, don't do it again. This
3961 can happen via a recursive call. */
3962 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
3963 return true;
3964
3965 /* Don't look at this symbol again. Note that we must set this
3966 after checking the above conditions, because we may look at a
3967 symbol once, decide not to do anything, and then get called
3968 recursively later after REF_REGULAR is set below. */
3969 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
3970
3971 /* If this is a weak definition, and we know a real definition, and
3972 the real symbol is not itself defined by a regular object file,
3973 then get a good value for the real definition. We handle the
3974 real symbol first, for the convenience of the backend routine.
3975
3976 Note that there is a confusing case here. If the real definition
3977 is defined by a regular object file, we don't get the real symbol
3978 from the dynamic object, but we do get the weak symbol. If the
3979 processor backend uses a COPY reloc, then if some routine in the
3980 dynamic object changes the real symbol, we will not see that
3981 change in the corresponding weak symbol. This is the way other
3982 ELF linkers work as well, and seems to be a result of the shared
3983 library model.
3984
3985 I will clarify this issue. Most SVR4 shared libraries define the
3986 variable _timezone and define timezone as a weak synonym. The
3987 tzset call changes _timezone. If you write
3988 extern int timezone;
3989 int _timezone = 5;
3990 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3991 you might expect that, since timezone is a synonym for _timezone,
3992 the same number will print both times. However, if the processor
3993 backend uses a COPY reloc, then actually timezone will be copied
3994 into your process image, and, since you define _timezone
3995 yourself, _timezone will not. Thus timezone and _timezone will
3996 wind up at different memory locations. The tzset call will set
3997 _timezone, leaving timezone unchanged. */
3998
3999 if (h->weakdef != NULL)
4000 {
4001 /* If we get to this point, we know there is an implicit
4002 reference by a regular object file via the weak symbol H.
4003 FIXME: Is this really true? What if the traversal finds
4004 H->WEAKDEF before it finds H? */
4005 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
4006
4007 if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
4008 return false;
4009 }
4010
4011 /* If a symbol has no type and no size and does not require a PLT
4012 entry, then we are probably about to do the wrong thing here: we
4013 are probably going to create a COPY reloc for an empty object.
4014 This case can arise when a shared object is built with assembly
4015 code, and the assembly code fails to set the symbol type. */
4016 if (h->size == 0
4017 && h->type == STT_NOTYPE
4018 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
4019 (*_bfd_error_handler)
4020 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4021 h->root.root.string);
4022
4023 dynobj = elf_hash_table (eif->info)->dynobj;
4024 bed = get_elf_backend_data (dynobj);
4025 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
4026 {
4027 eif->failed = true;
4028 return false;
4029 }
4030
4031 return true;
4032 }
4033 \f
4034 /* This routine is used to export all defined symbols into the dynamic
4035 symbol table. It is called via elf_link_hash_traverse. */
4036
4037 static boolean
4038 elf_export_symbol (h, data)
4039 struct elf_link_hash_entry *h;
4040 PTR data;
4041 {
4042 struct elf_info_failed *eif = (struct elf_info_failed *) data;
4043
4044 /* Ignore indirect symbols. These are added by the versioning code. */
4045 if (h->root.type == bfd_link_hash_indirect)
4046 return true;
4047
4048 if (h->dynindx == -1
4049 && (h->elf_link_hash_flags
4050 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
4051 {
4052 struct bfd_elf_version_tree *t;
4053 struct bfd_elf_version_expr *d;
4054
4055 for (t = eif->verdefs; t != NULL; t = t->next)
4056 {
4057 if (t->globals != NULL)
4058 {
4059 for (d = t->globals; d != NULL; d = d->next)
4060 {
4061 if ((*d->match) (d, h->root.root.string))
4062 goto doit;
4063 }
4064 }
4065
4066 if (t->locals != NULL)
4067 {
4068 for (d = t->locals ; d != NULL; d = d->next)
4069 {
4070 if ((*d->match) (d, h->root.root.string))
4071 return true;
4072 }
4073 }
4074 }
4075
4076 if (!eif->verdefs)
4077 {
4078 doit:
4079 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
4080 {
4081 eif->failed = true;
4082 return false;
4083 }
4084 }
4085 }
4086
4087 return true;
4088 }
4089 \f
4090 /* Look through the symbols which are defined in other shared
4091 libraries and referenced here. Update the list of version
4092 dependencies. This will be put into the .gnu.version_r section.
4093 This function is called via elf_link_hash_traverse. */
4094
4095 static boolean
4096 elf_link_find_version_dependencies (h, data)
4097 struct elf_link_hash_entry *h;
4098 PTR data;
4099 {
4100 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
4101 Elf_Internal_Verneed *t;
4102 Elf_Internal_Vernaux *a;
4103 bfd_size_type amt;
4104
4105 /* We only care about symbols defined in shared objects with version
4106 information. */
4107 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4108 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4109 || h->dynindx == -1
4110 || h->verinfo.verdef == NULL)
4111 return true;
4112
4113 /* See if we already know about this version. */
4114 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
4115 {
4116 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
4117 continue;
4118
4119 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4120 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
4121 return true;
4122
4123 break;
4124 }
4125
4126 /* This is a new version. Add it to tree we are building. */
4127
4128 if (t == NULL)
4129 {
4130 amt = sizeof *t;
4131 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
4132 if (t == NULL)
4133 {
4134 rinfo->failed = true;
4135 return false;
4136 }
4137
4138 t->vn_bfd = h->verinfo.verdef->vd_bfd;
4139 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
4140 elf_tdata (rinfo->output_bfd)->verref = t;
4141 }
4142
4143 amt = sizeof *a;
4144 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
4145
4146 /* Note that we are copying a string pointer here, and testing it
4147 above. If bfd_elf_string_from_elf_section is ever changed to
4148 discard the string data when low in memory, this will have to be
4149 fixed. */
4150 a->vna_nodename = h->verinfo.verdef->vd_nodename;
4151
4152 a->vna_flags = h->verinfo.verdef->vd_flags;
4153 a->vna_nextptr = t->vn_auxptr;
4154
4155 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
4156 ++rinfo->vers;
4157
4158 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
4159
4160 t->vn_auxptr = a;
4161
4162 return true;
4163 }
4164
4165 /* Figure out appropriate versions for all the symbols. We may not
4166 have the version number script until we have read all of the input
4167 files, so until that point we don't know which symbols should be
4168 local. This function is called via elf_link_hash_traverse. */
4169
4170 static boolean
4171 elf_link_assign_sym_version (h, data)
4172 struct elf_link_hash_entry *h;
4173 PTR data;
4174 {
4175 struct elf_assign_sym_version_info *sinfo;
4176 struct bfd_link_info *info;
4177 struct elf_backend_data *bed;
4178 struct elf_info_failed eif;
4179 char *p;
4180 bfd_size_type amt;
4181
4182 sinfo = (struct elf_assign_sym_version_info *) data;
4183 info = sinfo->info;
4184
4185 /* Fix the symbol flags. */
4186 eif.failed = false;
4187 eif.info = info;
4188 if (! elf_fix_symbol_flags (h, &eif))
4189 {
4190 if (eif.failed)
4191 sinfo->failed = true;
4192 return false;
4193 }
4194
4195 /* We only need version numbers for symbols defined in regular
4196 objects. */
4197 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4198 return true;
4199
4200 bed = get_elf_backend_data (sinfo->output_bfd);
4201 p = strchr (h->root.root.string, ELF_VER_CHR);
4202 if (p != NULL && h->verinfo.vertree == NULL)
4203 {
4204 struct bfd_elf_version_tree *t;
4205 boolean hidden;
4206
4207 hidden = true;
4208
4209 /* There are two consecutive ELF_VER_CHR characters if this is
4210 not a hidden symbol. */
4211 ++p;
4212 if (*p == ELF_VER_CHR)
4213 {
4214 hidden = false;
4215 ++p;
4216 }
4217
4218 /* If there is no version string, we can just return out. */
4219 if (*p == '\0')
4220 {
4221 if (hidden)
4222 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4223 return true;
4224 }
4225
4226 /* Look for the version. If we find it, it is no longer weak. */
4227 for (t = sinfo->verdefs; t != NULL; t = t->next)
4228 {
4229 if (strcmp (t->name, p) == 0)
4230 {
4231 size_t len;
4232 char *alc;
4233 struct bfd_elf_version_expr *d;
4234
4235 len = p - h->root.root.string;
4236 alc = bfd_alloc (sinfo->output_bfd, (bfd_size_type) len);
4237 if (alc == NULL)
4238 return false;
4239 strncpy (alc, h->root.root.string, len - 1);
4240 alc[len - 1] = '\0';
4241 if (alc[len - 2] == ELF_VER_CHR)
4242 alc[len - 2] = '\0';
4243
4244 h->verinfo.vertree = t;
4245 t->used = true;
4246 d = NULL;
4247
4248 if (t->globals != NULL)
4249 {
4250 for (d = t->globals; d != NULL; d = d->next)
4251 if ((*d->match) (d, alc))
4252 break;
4253 }
4254
4255 /* See if there is anything to force this symbol to
4256 local scope. */
4257 if (d == NULL && t->locals != NULL)
4258 {
4259 for (d = t->locals; d != NULL; d = d->next)
4260 {
4261 if ((*d->match) (d, alc))
4262 {
4263 if (h->dynindx != -1
4264 && info->shared
4265 && ! info->export_dynamic)
4266 {
4267 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
4268 (*bed->elf_backend_hide_symbol) (info, h);
4269 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4270 h->dynstr_index);
4271 }
4272
4273 break;
4274 }
4275 }
4276 }
4277
4278 bfd_release (sinfo->output_bfd, alc);
4279 break;
4280 }
4281 }
4282
4283 /* If we are building an application, we need to create a
4284 version node for this version. */
4285 if (t == NULL && ! info->shared)
4286 {
4287 struct bfd_elf_version_tree **pp;
4288 int version_index;
4289
4290 /* If we aren't going to export this symbol, we don't need
4291 to worry about it. */
4292 if (h->dynindx == -1)
4293 return true;
4294
4295 amt = sizeof *t;
4296 t = ((struct bfd_elf_version_tree *)
4297 bfd_alloc (sinfo->output_bfd, amt));
4298 if (t == NULL)
4299 {
4300 sinfo->failed = true;
4301 return false;
4302 }
4303
4304 t->next = NULL;
4305 t->name = p;
4306 t->globals = NULL;
4307 t->locals = NULL;
4308 t->deps = NULL;
4309 t->name_indx = (unsigned int) -1;
4310 t->used = true;
4311
4312 version_index = 1;
4313 /* Don't count anonymous version tag. */
4314 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
4315 version_index = 0;
4316 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
4317 ++version_index;
4318 t->vernum = version_index;
4319
4320 *pp = t;
4321
4322 h->verinfo.vertree = t;
4323 }
4324 else if (t == NULL)
4325 {
4326 /* We could not find the version for a symbol when
4327 generating a shared archive. Return an error. */
4328 (*_bfd_error_handler)
4329 (_("%s: undefined versioned symbol name %s"),
4330 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
4331 bfd_set_error (bfd_error_bad_value);
4332 sinfo->failed = true;
4333 return false;
4334 }
4335
4336 if (hidden)
4337 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4338 }
4339
4340 /* If we don't have a version for this symbol, see if we can find
4341 something. */
4342 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
4343 {
4344 struct bfd_elf_version_tree *t;
4345 struct bfd_elf_version_tree *deflt;
4346 struct bfd_elf_version_expr *d;
4347
4348 /* See if can find what version this symbol is in. If the
4349 symbol is supposed to be local, then don't actually register
4350 it. */
4351 deflt = NULL;
4352 for (t = sinfo->verdefs; t != NULL; t = t->next)
4353 {
4354 if (t->globals != NULL)
4355 {
4356 for (d = t->globals; d != NULL; d = d->next)
4357 {
4358 if ((*d->match) (d, h->root.root.string))
4359 {
4360 h->verinfo.vertree = t;
4361 break;
4362 }
4363 }
4364
4365 if (d != NULL)
4366 break;
4367 }
4368
4369 if (t->locals != NULL)
4370 {
4371 for (d = t->locals; d != NULL; d = d->next)
4372 {
4373 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
4374 deflt = t;
4375 else if ((*d->match) (d, h->root.root.string))
4376 {
4377 h->verinfo.vertree = t;
4378 if (h->dynindx != -1
4379 && info->shared
4380 && ! info->export_dynamic)
4381 {
4382 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
4383 (*bed->elf_backend_hide_symbol) (info, h);
4384 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4385 h->dynstr_index);
4386 }
4387 break;
4388 }
4389 }
4390
4391 if (d != NULL)
4392 break;
4393 }
4394 }
4395
4396 if (deflt != NULL && h->verinfo.vertree == NULL)
4397 {
4398 h->verinfo.vertree = deflt;
4399 if (h->dynindx != -1
4400 && info->shared
4401 && ! info->export_dynamic)
4402 {
4403 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
4404 (*bed->elf_backend_hide_symbol) (info, h);
4405 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4406 h->dynstr_index);
4407 }
4408 }
4409 }
4410
4411 return true;
4412 }
4413 \f
4414 /* Final phase of ELF linker. */
4415
4416 /* A structure we use to avoid passing large numbers of arguments. */
4417
4418 struct elf_final_link_info
4419 {
4420 /* General link information. */
4421 struct bfd_link_info *info;
4422 /* Output BFD. */
4423 bfd *output_bfd;
4424 /* Symbol string table. */
4425 struct bfd_strtab_hash *symstrtab;
4426 /* .dynsym section. */
4427 asection *dynsym_sec;
4428 /* .hash section. */
4429 asection *hash_sec;
4430 /* symbol version section (.gnu.version). */
4431 asection *symver_sec;
4432 /* Buffer large enough to hold contents of any section. */
4433 bfd_byte *contents;
4434 /* Buffer large enough to hold external relocs of any section. */
4435 PTR external_relocs;
4436 /* Buffer large enough to hold internal relocs of any section. */
4437 Elf_Internal_Rela *internal_relocs;
4438 /* Buffer large enough to hold external local symbols of any input
4439 BFD. */
4440 Elf_External_Sym *external_syms;
4441 /* And a buffer for symbol section indices. */
4442 Elf_External_Sym_Shndx *locsym_shndx;
4443 /* Buffer large enough to hold internal local symbols of any input
4444 BFD. */
4445 Elf_Internal_Sym *internal_syms;
4446 /* Array large enough to hold a symbol index for each local symbol
4447 of any input BFD. */
4448 long *indices;
4449 /* Array large enough to hold a section pointer for each local
4450 symbol of any input BFD. */
4451 asection **sections;
4452 /* Buffer to hold swapped out symbols. */
4453 Elf_External_Sym *symbuf;
4454 /* And one for symbol section indices. */
4455 Elf_External_Sym_Shndx *symshndxbuf;
4456 /* Number of swapped out symbols in buffer. */
4457 size_t symbuf_count;
4458 /* Number of symbols which fit in symbuf. */
4459 size_t symbuf_size;
4460 };
4461
4462 static boolean elf_link_output_sym
4463 PARAMS ((struct elf_final_link_info *, const char *,
4464 Elf_Internal_Sym *, asection *));
4465 static boolean elf_link_flush_output_syms
4466 PARAMS ((struct elf_final_link_info *));
4467 static boolean elf_link_output_extsym
4468 PARAMS ((struct elf_link_hash_entry *, PTR));
4469 static boolean elf_link_sec_merge_syms
4470 PARAMS ((struct elf_link_hash_entry *, PTR));
4471 static boolean elf_link_input_bfd
4472 PARAMS ((struct elf_final_link_info *, bfd *));
4473 static boolean elf_reloc_link_order
4474 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4475 struct bfd_link_order *));
4476
4477 /* This struct is used to pass information to elf_link_output_extsym. */
4478
4479 struct elf_outext_info
4480 {
4481 boolean failed;
4482 boolean localsyms;
4483 struct elf_final_link_info *finfo;
4484 };
4485
4486 /* Compute the size of, and allocate space for, REL_HDR which is the
4487 section header for a section containing relocations for O. */
4488
4489 static boolean
4490 elf_link_size_reloc_section (abfd, rel_hdr, o)
4491 bfd *abfd;
4492 Elf_Internal_Shdr *rel_hdr;
4493 asection *o;
4494 {
4495 bfd_size_type reloc_count;
4496 bfd_size_type num_rel_hashes;
4497
4498 /* Figure out how many relocations there will be. */
4499 if (rel_hdr == &elf_section_data (o)->rel_hdr)
4500 reloc_count = elf_section_data (o)->rel_count;
4501 else
4502 reloc_count = elf_section_data (o)->rel_count2;
4503
4504 num_rel_hashes = o->reloc_count;
4505 if (num_rel_hashes < reloc_count)
4506 num_rel_hashes = reloc_count;
4507
4508 /* That allows us to calculate the size of the section. */
4509 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
4510
4511 /* The contents field must last into write_object_contents, so we
4512 allocate it with bfd_alloc rather than malloc. Also since we
4513 cannot be sure that the contents will actually be filled in,
4514 we zero the allocated space. */
4515 rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
4516 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
4517 return false;
4518
4519 /* We only allocate one set of hash entries, so we only do it the
4520 first time we are called. */
4521 if (elf_section_data (o)->rel_hashes == NULL
4522 && num_rel_hashes)
4523 {
4524 struct elf_link_hash_entry **p;
4525
4526 p = ((struct elf_link_hash_entry **)
4527 bfd_zmalloc (num_rel_hashes
4528 * sizeof (struct elf_link_hash_entry *)));
4529 if (p == NULL)
4530 return false;
4531
4532 elf_section_data (o)->rel_hashes = p;
4533 }
4534
4535 return true;
4536 }
4537
4538 /* When performing a relocateable link, the input relocations are
4539 preserved. But, if they reference global symbols, the indices
4540 referenced must be updated. Update all the relocations in
4541 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4542
4543 static void
4544 elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
4545 bfd *abfd;
4546 Elf_Internal_Shdr *rel_hdr;
4547 unsigned int count;
4548 struct elf_link_hash_entry **rel_hash;
4549 {
4550 unsigned int i;
4551 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4552 Elf_Internal_Rel *irel;
4553 Elf_Internal_Rela *irela;
4554 bfd_size_type amt = sizeof (Elf_Internal_Rel) * bed->s->int_rels_per_ext_rel;
4555
4556 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
4557 if (irel == NULL)
4558 {
4559 (*_bfd_error_handler) (_("Error: out of memory"));
4560 abort ();
4561 }
4562
4563 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
4564 irela = (Elf_Internal_Rela *) bfd_zmalloc (amt);
4565 if (irela == NULL)
4566 {
4567 (*_bfd_error_handler) (_("Error: out of memory"));
4568 abort ();
4569 }
4570
4571 for (i = 0; i < count; i++, rel_hash++)
4572 {
4573 if (*rel_hash == NULL)
4574 continue;
4575
4576 BFD_ASSERT ((*rel_hash)->indx >= 0);
4577
4578 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4579 {
4580 Elf_External_Rel *erel;
4581 unsigned int j;
4582
4583 erel = (Elf_External_Rel *) rel_hdr->contents + i;
4584 if (bed->s->swap_reloc_in)
4585 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
4586 else
4587 elf_swap_reloc_in (abfd, erel, irel);
4588
4589 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4590 irel[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4591 ELF_R_TYPE (irel[j].r_info));
4592
4593 if (bed->s->swap_reloc_out)
4594 (*bed->s->swap_reloc_out) (abfd, irel, (bfd_byte *) erel);
4595 else
4596 elf_swap_reloc_out (abfd, irel, erel);
4597 }
4598 else
4599 {
4600 Elf_External_Rela *erela;
4601 unsigned int j;
4602
4603 BFD_ASSERT (rel_hdr->sh_entsize
4604 == sizeof (Elf_External_Rela));
4605
4606 erela = (Elf_External_Rela *) rel_hdr->contents + i;
4607 if (bed->s->swap_reloca_in)
4608 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
4609 else
4610 elf_swap_reloca_in (abfd, erela, irela);
4611
4612 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4613 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4614 ELF_R_TYPE (irela[j].r_info));
4615
4616 if (bed->s->swap_reloca_out)
4617 (*bed->s->swap_reloca_out) (abfd, irela, (bfd_byte *) erela);
4618 else
4619 elf_swap_reloca_out (abfd, irela, erela);
4620 }
4621 }
4622
4623 free (irel);
4624 free (irela);
4625 }
4626
4627 struct elf_link_sort_rela {
4628 bfd_vma offset;
4629 enum elf_reloc_type_class type;
4630 union {
4631 Elf_Internal_Rel rel;
4632 Elf_Internal_Rela rela;
4633 } u;
4634 };
4635
4636 static int
4637 elf_link_sort_cmp1 (A, B)
4638 const PTR A;
4639 const PTR B;
4640 {
4641 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4642 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4643 int relativea, relativeb;
4644
4645 relativea = a->type == reloc_class_relative;
4646 relativeb = b->type == reloc_class_relative;
4647
4648 if (relativea < relativeb)
4649 return 1;
4650 if (relativea > relativeb)
4651 return -1;
4652 if (ELF_R_SYM (a->u.rel.r_info) < ELF_R_SYM (b->u.rel.r_info))
4653 return -1;
4654 if (ELF_R_SYM (a->u.rel.r_info) > ELF_R_SYM (b->u.rel.r_info))
4655 return 1;
4656 if (a->u.rel.r_offset < b->u.rel.r_offset)
4657 return -1;
4658 if (a->u.rel.r_offset > b->u.rel.r_offset)
4659 return 1;
4660 return 0;
4661 }
4662
4663 static int
4664 elf_link_sort_cmp2 (A, B)
4665 const PTR A;
4666 const PTR B;
4667 {
4668 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4669 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4670 int copya, copyb;
4671
4672 if (a->offset < b->offset)
4673 return -1;
4674 if (a->offset > b->offset)
4675 return 1;
4676 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
4677 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
4678 if (copya < copyb)
4679 return -1;
4680 if (copya > copyb)
4681 return 1;
4682 if (a->u.rel.r_offset < b->u.rel.r_offset)
4683 return -1;
4684 if (a->u.rel.r_offset > b->u.rel.r_offset)
4685 return 1;
4686 return 0;
4687 }
4688
4689 static size_t
4690 elf_link_sort_relocs (abfd, info, psec)
4691 bfd *abfd;
4692 struct bfd_link_info *info;
4693 asection **psec;
4694 {
4695 bfd *dynobj = elf_hash_table (info)->dynobj;
4696 asection *reldyn, *o;
4697 boolean rel = false;
4698 bfd_size_type count, size;
4699 size_t i, j, ret;
4700 struct elf_link_sort_rela *rela;
4701 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4702
4703 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
4704 if (reldyn == NULL || reldyn->_raw_size == 0)
4705 {
4706 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
4707 if (reldyn == NULL || reldyn->_raw_size == 0)
4708 return 0;
4709 rel = true;
4710 count = reldyn->_raw_size / sizeof (Elf_External_Rel);
4711 }
4712 else
4713 count = reldyn->_raw_size / sizeof (Elf_External_Rela);
4714
4715 size = 0;
4716 for (o = dynobj->sections; o != NULL; o = o->next)
4717 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4718 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4719 && o->output_section == reldyn)
4720 size += o->_raw_size;
4721
4722 if (size != reldyn->_raw_size)
4723 return 0;
4724
4725 rela = (struct elf_link_sort_rela *) bfd_zmalloc (sizeof (*rela) * count);
4726 if (rela == NULL)
4727 {
4728 (*info->callbacks->warning)
4729 (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
4730 (bfd_vma) 0);
4731 return 0;
4732 }
4733
4734 for (o = dynobj->sections; o != NULL; o = o->next)
4735 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4736 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4737 && o->output_section == reldyn)
4738 {
4739 if (rel)
4740 {
4741 Elf_External_Rel *erel, *erelend;
4742 struct elf_link_sort_rela *s;
4743
4744 erel = (Elf_External_Rel *) o->contents;
4745 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4746 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4747 for (; erel < erelend; erel++, s++)
4748 {
4749 if (bed->s->swap_reloc_in)
4750 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, &s->u.rel);
4751 else
4752 elf_swap_reloc_in (abfd, erel, &s->u.rel);
4753
4754 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4755 }
4756 }
4757 else
4758 {
4759 Elf_External_Rela *erela, *erelaend;
4760 struct elf_link_sort_rela *s;
4761
4762 erela = (Elf_External_Rela *) o->contents;
4763 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4764 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4765 for (; erela < erelaend; erela++, s++)
4766 {
4767 if (bed->s->swap_reloca_in)
4768 (*bed->s->swap_reloca_in) (dynobj, (bfd_byte *) erela,
4769 &s->u.rela);
4770 else
4771 elf_swap_reloca_in (dynobj, erela, &s->u.rela);
4772
4773 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4774 }
4775 }
4776 }
4777
4778 qsort (rela, (size_t) count, sizeof (*rela), elf_link_sort_cmp1);
4779 for (ret = 0; ret < count && rela[ret].type == reloc_class_relative; ret++)
4780 ;
4781 for (i = ret, j = ret; i < count; i++)
4782 {
4783 if (ELF_R_SYM (rela[i].u.rel.r_info) != ELF_R_SYM (rela[j].u.rel.r_info))
4784 j = i;
4785 rela[i].offset = rela[j].u.rel.r_offset;
4786 }
4787 qsort (rela + ret, (size_t) count - ret, sizeof (*rela), elf_link_sort_cmp2);
4788
4789 for (o = dynobj->sections; o != NULL; o = o->next)
4790 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4791 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4792 && o->output_section == reldyn)
4793 {
4794 if (rel)
4795 {
4796 Elf_External_Rel *erel, *erelend;
4797 struct elf_link_sort_rela *s;
4798
4799 erel = (Elf_External_Rel *) o->contents;
4800 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4801 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4802 for (; erel < erelend; erel++, s++)
4803 {
4804 if (bed->s->swap_reloc_out)
4805 (*bed->s->swap_reloc_out) (abfd, &s->u.rel,
4806 (bfd_byte *) erel);
4807 else
4808 elf_swap_reloc_out (abfd, &s->u.rel, erel);
4809 }
4810 }
4811 else
4812 {
4813 Elf_External_Rela *erela, *erelaend;
4814 struct elf_link_sort_rela *s;
4815
4816 erela = (Elf_External_Rela *) o->contents;
4817 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4818 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4819 for (; erela < erelaend; erela++, s++)
4820 {
4821 if (bed->s->swap_reloca_out)
4822 (*bed->s->swap_reloca_out) (dynobj, &s->u.rela,
4823 (bfd_byte *) erela);
4824 else
4825 elf_swap_reloca_out (dynobj, &s->u.rela, erela);
4826 }
4827 }
4828 }
4829
4830 free (rela);
4831 *psec = reldyn;
4832 return ret;
4833 }
4834
4835 /* Do the final step of an ELF link. */
4836
4837 boolean
4838 elf_bfd_final_link (abfd, info)
4839 bfd *abfd;
4840 struct bfd_link_info *info;
4841 {
4842 boolean dynamic;
4843 boolean emit_relocs;
4844 bfd *dynobj;
4845 struct elf_final_link_info finfo;
4846 register asection *o;
4847 register struct bfd_link_order *p;
4848 register bfd *sub;
4849 bfd_size_type max_contents_size;
4850 bfd_size_type max_external_reloc_size;
4851 bfd_size_type max_internal_reloc_count;
4852 bfd_size_type max_sym_count;
4853 bfd_size_type max_sym_shndx_count;
4854 file_ptr off;
4855 Elf_Internal_Sym elfsym;
4856 unsigned int i;
4857 Elf_Internal_Shdr *symtab_hdr;
4858 Elf_Internal_Shdr *symstrtab_hdr;
4859 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4860 struct elf_outext_info eoinfo;
4861 boolean merged;
4862 size_t relativecount = 0;
4863 asection *reldyn = 0;
4864 bfd_size_type amt;
4865
4866 if (! is_elf_hash_table (info))
4867 return false;
4868
4869 if (info->shared)
4870 abfd->flags |= DYNAMIC;
4871
4872 dynamic = elf_hash_table (info)->dynamic_sections_created;
4873 dynobj = elf_hash_table (info)->dynobj;
4874
4875 emit_relocs = (info->relocateable
4876 || info->emitrelocations
4877 || bed->elf_backend_emit_relocs);
4878
4879 finfo.info = info;
4880 finfo.output_bfd = abfd;
4881 finfo.symstrtab = elf_stringtab_init ();
4882 if (finfo.symstrtab == NULL)
4883 return false;
4884
4885 if (! dynamic)
4886 {
4887 finfo.dynsym_sec = NULL;
4888 finfo.hash_sec = NULL;
4889 finfo.symver_sec = NULL;
4890 }
4891 else
4892 {
4893 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
4894 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
4895 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
4896 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
4897 /* Note that it is OK if symver_sec is NULL. */
4898 }
4899
4900 finfo.contents = NULL;
4901 finfo.external_relocs = NULL;
4902 finfo.internal_relocs = NULL;
4903 finfo.external_syms = NULL;
4904 finfo.locsym_shndx = NULL;
4905 finfo.internal_syms = NULL;
4906 finfo.indices = NULL;
4907 finfo.sections = NULL;
4908 finfo.symbuf = NULL;
4909 finfo.symshndxbuf = NULL;
4910 finfo.symbuf_count = 0;
4911
4912 /* Count up the number of relocations we will output for each output
4913 section, so that we know the sizes of the reloc sections. We
4914 also figure out some maximum sizes. */
4915 max_contents_size = 0;
4916 max_external_reloc_size = 0;
4917 max_internal_reloc_count = 0;
4918 max_sym_count = 0;
4919 max_sym_shndx_count = 0;
4920 merged = false;
4921 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4922 {
4923 o->reloc_count = 0;
4924
4925 for (p = o->link_order_head; p != NULL; p = p->next)
4926 {
4927 if (p->type == bfd_section_reloc_link_order
4928 || p->type == bfd_symbol_reloc_link_order)
4929 ++o->reloc_count;
4930 else if (p->type == bfd_indirect_link_order)
4931 {
4932 asection *sec;
4933
4934 sec = p->u.indirect.section;
4935
4936 /* Mark all sections which are to be included in the
4937 link. This will normally be every section. We need
4938 to do this so that we can identify any sections which
4939 the linker has decided to not include. */
4940 sec->linker_mark = true;
4941
4942 if (sec->flags & SEC_MERGE)
4943 merged = true;
4944
4945 if (info->relocateable || info->emitrelocations)
4946 o->reloc_count += sec->reloc_count;
4947 else if (bed->elf_backend_count_relocs)
4948 {
4949 Elf_Internal_Rela * relocs;
4950
4951 relocs = (NAME(_bfd_elf,link_read_relocs)
4952 (abfd, sec, (PTR) NULL,
4953 (Elf_Internal_Rela *) NULL, info->keep_memory));
4954
4955 o->reloc_count += (*bed->elf_backend_count_relocs)
4956 (sec, relocs);
4957
4958 if (!info->keep_memory)
4959 free (relocs);
4960 }
4961
4962 if (sec->_raw_size > max_contents_size)
4963 max_contents_size = sec->_raw_size;
4964 if (sec->_cooked_size > max_contents_size)
4965 max_contents_size = sec->_cooked_size;
4966
4967 /* We are interested in just local symbols, not all
4968 symbols. */
4969 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
4970 && (sec->owner->flags & DYNAMIC) == 0)
4971 {
4972 size_t sym_count;
4973
4974 if (elf_bad_symtab (sec->owner))
4975 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
4976 / sizeof (Elf_External_Sym));
4977 else
4978 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
4979
4980 if (sym_count > max_sym_count)
4981 max_sym_count = sym_count;
4982
4983 if (sym_count > max_sym_shndx_count
4984 && elf_symtab_shndx (sec->owner) != 0)
4985 max_sym_shndx_count = sym_count;
4986
4987 if ((sec->flags & SEC_RELOC) != 0)
4988 {
4989 size_t ext_size;
4990
4991 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
4992 if (ext_size > max_external_reloc_size)
4993 max_external_reloc_size = ext_size;
4994 if (sec->reloc_count > max_internal_reloc_count)
4995 max_internal_reloc_count = sec->reloc_count;
4996 }
4997 }
4998 }
4999 }
5000
5001 if (o->reloc_count > 0)
5002 o->flags |= SEC_RELOC;
5003 else
5004 {
5005 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5006 set it (this is probably a bug) and if it is set
5007 assign_section_numbers will create a reloc section. */
5008 o->flags &=~ SEC_RELOC;
5009 }
5010
5011 /* If the SEC_ALLOC flag is not set, force the section VMA to
5012 zero. This is done in elf_fake_sections as well, but forcing
5013 the VMA to 0 here will ensure that relocs against these
5014 sections are handled correctly. */
5015 if ((o->flags & SEC_ALLOC) == 0
5016 && ! o->user_set_vma)
5017 o->vma = 0;
5018 }
5019
5020 if (! info->relocateable && merged)
5021 elf_link_hash_traverse (elf_hash_table (info),
5022 elf_link_sec_merge_syms, (PTR) abfd);
5023
5024 /* Figure out the file positions for everything but the symbol table
5025 and the relocs. We set symcount to force assign_section_numbers
5026 to create a symbol table. */
5027 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
5028 BFD_ASSERT (! abfd->output_has_begun);
5029 if (! _bfd_elf_compute_section_file_positions (abfd, info))
5030 goto error_return;
5031
5032 /* Figure out how many relocations we will have in each section.
5033 Just using RELOC_COUNT isn't good enough since that doesn't
5034 maintain a separate value for REL vs. RELA relocations. */
5035 if (emit_relocs)
5036 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5037 for (o = sub->sections; o != NULL; o = o->next)
5038 {
5039 asection *output_section;
5040
5041 if (! o->linker_mark)
5042 {
5043 /* This section was omitted from the link. */
5044 continue;
5045 }
5046
5047 output_section = o->output_section;
5048
5049 if (output_section != NULL
5050 && (o->flags & SEC_RELOC) != 0)
5051 {
5052 struct bfd_elf_section_data *esdi
5053 = elf_section_data (o);
5054 struct bfd_elf_section_data *esdo
5055 = elf_section_data (output_section);
5056 unsigned int *rel_count;
5057 unsigned int *rel_count2;
5058
5059 /* We must be careful to add the relocation froms the
5060 input section to the right output count. */
5061 if (esdi->rel_hdr.sh_entsize == esdo->rel_hdr.sh_entsize)
5062 {
5063 rel_count = &esdo->rel_count;
5064 rel_count2 = &esdo->rel_count2;
5065 }
5066 else
5067 {
5068 rel_count = &esdo->rel_count2;
5069 rel_count2 = &esdo->rel_count;
5070 }
5071
5072 *rel_count += NUM_SHDR_ENTRIES (& esdi->rel_hdr);
5073 if (esdi->rel_hdr2)
5074 *rel_count2 += NUM_SHDR_ENTRIES (esdi->rel_hdr2);
5075 output_section->flags |= SEC_RELOC;
5076 }
5077 }
5078
5079 /* That created the reloc sections. Set their sizes, and assign
5080 them file positions, and allocate some buffers. */
5081 for (o = abfd->sections; o != NULL; o = o->next)
5082 {
5083 if ((o->flags & SEC_RELOC) != 0)
5084 {
5085 if (!elf_link_size_reloc_section (abfd,
5086 &elf_section_data (o)->rel_hdr,
5087 o))
5088 goto error_return;
5089
5090 if (elf_section_data (o)->rel_hdr2
5091 && !elf_link_size_reloc_section (abfd,
5092 elf_section_data (o)->rel_hdr2,
5093 o))
5094 goto error_return;
5095 }
5096
5097 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5098 to count upwards while actually outputting the relocations. */
5099 elf_section_data (o)->rel_count = 0;
5100 elf_section_data (o)->rel_count2 = 0;
5101 }
5102
5103 _bfd_elf_assign_file_positions_for_relocs (abfd);
5104
5105 /* We have now assigned file positions for all the sections except
5106 .symtab and .strtab. We start the .symtab section at the current
5107 file position, and write directly to it. We build the .strtab
5108 section in memory. */
5109 bfd_get_symcount (abfd) = 0;
5110 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5111 /* sh_name is set in prep_headers. */
5112 symtab_hdr->sh_type = SHT_SYMTAB;
5113 symtab_hdr->sh_flags = 0;
5114 symtab_hdr->sh_addr = 0;
5115 symtab_hdr->sh_size = 0;
5116 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
5117 /* sh_link is set in assign_section_numbers. */
5118 /* sh_info is set below. */
5119 /* sh_offset is set just below. */
5120 symtab_hdr->sh_addralign = bed->s->file_align;
5121
5122 off = elf_tdata (abfd)->next_file_pos;
5123 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
5124
5125 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5126 incorrect. We do not yet know the size of the .symtab section.
5127 We correct next_file_pos below, after we do know the size. */
5128
5129 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5130 continuously seeking to the right position in the file. */
5131 if (! info->keep_memory || max_sym_count < 20)
5132 finfo.symbuf_size = 20;
5133 else
5134 finfo.symbuf_size = max_sym_count;
5135 amt = finfo.symbuf_size;
5136 amt *= sizeof (Elf_External_Sym);
5137 finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
5138 if (finfo.symbuf == NULL)
5139 goto error_return;
5140 if (elf_numsections (abfd) > SHN_LORESERVE)
5141 {
5142 amt = finfo.symbuf_size;
5143 amt *= sizeof (Elf_External_Sym_Shndx);
5144 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5145 if (finfo.symshndxbuf == NULL)
5146 goto error_return;
5147 }
5148
5149 /* Start writing out the symbol table. The first symbol is always a
5150 dummy symbol. */
5151 if (info->strip != strip_all
5152 || emit_relocs)
5153 {
5154 elfsym.st_value = 0;
5155 elfsym.st_size = 0;
5156 elfsym.st_info = 0;
5157 elfsym.st_other = 0;
5158 elfsym.st_shndx = SHN_UNDEF;
5159 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5160 &elfsym, bfd_und_section_ptr))
5161 goto error_return;
5162 }
5163
5164 #if 0
5165 /* Some standard ELF linkers do this, but we don't because it causes
5166 bootstrap comparison failures. */
5167 /* Output a file symbol for the output file as the second symbol.
5168 We output this even if we are discarding local symbols, although
5169 I'm not sure if this is correct. */
5170 elfsym.st_value = 0;
5171 elfsym.st_size = 0;
5172 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5173 elfsym.st_other = 0;
5174 elfsym.st_shndx = SHN_ABS;
5175 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
5176 &elfsym, bfd_abs_section_ptr))
5177 goto error_return;
5178 #endif
5179
5180 /* Output a symbol for each section. We output these even if we are
5181 discarding local symbols, since they are used for relocs. These
5182 symbols have no names. We store the index of each one in the
5183 index field of the section, so that we can find it again when
5184 outputting relocs. */
5185 if (info->strip != strip_all
5186 || emit_relocs)
5187 {
5188 elfsym.st_size = 0;
5189 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5190 elfsym.st_other = 0;
5191 for (i = 1; i < elf_numsections (abfd); i++)
5192 {
5193 o = section_from_elf_index (abfd, i);
5194 if (o != NULL)
5195 o->target_index = bfd_get_symcount (abfd);
5196 elfsym.st_shndx = i;
5197 if (info->relocateable || o == NULL)
5198 elfsym.st_value = 0;
5199 else
5200 elfsym.st_value = o->vma;
5201 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5202 &elfsym, o))
5203 goto error_return;
5204 if (i == SHN_LORESERVE)
5205 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
5206 }
5207 }
5208
5209 /* Allocate some memory to hold information read in from the input
5210 files. */
5211 if (max_contents_size != 0)
5212 {
5213 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
5214 if (finfo.contents == NULL)
5215 goto error_return;
5216 }
5217
5218 if (max_external_reloc_size != 0)
5219 {
5220 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
5221 if (finfo.external_relocs == NULL)
5222 goto error_return;
5223 }
5224
5225 if (max_internal_reloc_count != 0)
5226 {
5227 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
5228 amt *= sizeof (Elf_Internal_Rela);
5229 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
5230 if (finfo.internal_relocs == NULL)
5231 goto error_return;
5232 }
5233
5234 if (max_sym_count != 0)
5235 {
5236 amt = max_sym_count * sizeof (Elf_External_Sym);
5237 finfo.external_syms = (Elf_External_Sym *) bfd_malloc (amt);
5238 if (finfo.external_syms == NULL)
5239 goto error_return;
5240
5241 amt = max_sym_count * sizeof (Elf_Internal_Sym);
5242 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
5243 if (finfo.internal_syms == NULL)
5244 goto error_return;
5245
5246 amt = max_sym_count * sizeof (long);
5247 finfo.indices = (long *) bfd_malloc (amt);
5248 if (finfo.indices == NULL)
5249 goto error_return;
5250
5251 amt = max_sym_count * sizeof (asection *);
5252 finfo.sections = (asection **) bfd_malloc (amt);
5253 if (finfo.sections == NULL)
5254 goto error_return;
5255 }
5256
5257 if (max_sym_shndx_count != 0)
5258 {
5259 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
5260 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5261 if (finfo.locsym_shndx == NULL)
5262 goto error_return;
5263 }
5264
5265 /* Since ELF permits relocations to be against local symbols, we
5266 must have the local symbols available when we do the relocations.
5267 Since we would rather only read the local symbols once, and we
5268 would rather not keep them in memory, we handle all the
5269 relocations for a single input file at the same time.
5270
5271 Unfortunately, there is no way to know the total number of local
5272 symbols until we have seen all of them, and the local symbol
5273 indices precede the global symbol indices. This means that when
5274 we are generating relocateable output, and we see a reloc against
5275 a global symbol, we can not know the symbol index until we have
5276 finished examining all the local symbols to see which ones we are
5277 going to output. To deal with this, we keep the relocations in
5278 memory, and don't output them until the end of the link. This is
5279 an unfortunate waste of memory, but I don't see a good way around
5280 it. Fortunately, it only happens when performing a relocateable
5281 link, which is not the common case. FIXME: If keep_memory is set
5282 we could write the relocs out and then read them again; I don't
5283 know how bad the memory loss will be. */
5284
5285 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5286 sub->output_has_begun = false;
5287 for (o = abfd->sections; o != NULL; o = o->next)
5288 {
5289 for (p = o->link_order_head; p != NULL; p = p->next)
5290 {
5291 if (p->type == bfd_indirect_link_order
5292 && (bfd_get_flavour (p->u.indirect.section->owner)
5293 == bfd_target_elf_flavour))
5294 {
5295 sub = p->u.indirect.section->owner;
5296 if (! sub->output_has_begun)
5297 {
5298 if (! elf_link_input_bfd (&finfo, sub))
5299 goto error_return;
5300 sub->output_has_begun = true;
5301 }
5302 }
5303 else if (p->type == bfd_section_reloc_link_order
5304 || p->type == bfd_symbol_reloc_link_order)
5305 {
5306 if (! elf_reloc_link_order (abfd, info, o, p))
5307 goto error_return;
5308 }
5309 else
5310 {
5311 if (! _bfd_default_link_order (abfd, info, o, p))
5312 goto error_return;
5313 }
5314 }
5315 }
5316
5317 /* That wrote out all the local symbols. Finish up the symbol table
5318 with the global symbols. Even if we want to strip everything we
5319 can, we still need to deal with those global symbols that got
5320 converted to local in a version script. */
5321
5322 if (info->shared)
5323 {
5324 /* Output any global symbols that got converted to local in a
5325 version script. We do this in a separate step since ELF
5326 requires all local symbols to appear prior to any global
5327 symbols. FIXME: We should only do this if some global
5328 symbols were, in fact, converted to become local. FIXME:
5329 Will this work correctly with the Irix 5 linker? */
5330 eoinfo.failed = false;
5331 eoinfo.finfo = &finfo;
5332 eoinfo.localsyms = true;
5333 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5334 (PTR) &eoinfo);
5335 if (eoinfo.failed)
5336 return false;
5337 }
5338
5339 /* The sh_info field records the index of the first non local symbol. */
5340 symtab_hdr->sh_info = bfd_get_symcount (abfd);
5341
5342 if (dynamic
5343 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
5344 {
5345 Elf_Internal_Sym sym;
5346 Elf_External_Sym *dynsym =
5347 (Elf_External_Sym *) finfo.dynsym_sec->contents;
5348 long last_local = 0;
5349
5350 /* Write out the section symbols for the output sections. */
5351 if (info->shared)
5352 {
5353 asection *s;
5354
5355 sym.st_size = 0;
5356 sym.st_name = 0;
5357 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5358 sym.st_other = 0;
5359
5360 for (s = abfd->sections; s != NULL; s = s->next)
5361 {
5362 int indx;
5363 Elf_External_Sym *dest;
5364
5365 indx = elf_section_data (s)->this_idx;
5366 BFD_ASSERT (indx > 0);
5367 sym.st_shndx = indx;
5368 sym.st_value = s->vma;
5369 dest = dynsym + elf_section_data (s)->dynindx;
5370 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5371 }
5372
5373 last_local = bfd_count_sections (abfd);
5374 }
5375
5376 /* Write out the local dynsyms. */
5377 if (elf_hash_table (info)->dynlocal)
5378 {
5379 struct elf_link_local_dynamic_entry *e;
5380 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
5381 {
5382 asection *s;
5383 Elf_External_Sym *dest;
5384
5385 sym.st_size = e->isym.st_size;
5386 sym.st_other = e->isym.st_other;
5387
5388 /* Copy the internal symbol as is.
5389 Note that we saved a word of storage and overwrote
5390 the original st_name with the dynstr_index. */
5391 sym = e->isym;
5392
5393 if (e->isym.st_shndx < SHN_LORESERVE
5394 || e->isym.st_shndx > SHN_HIRESERVE)
5395 {
5396 s = bfd_section_from_elf_index (e->input_bfd,
5397 e->isym.st_shndx);
5398
5399 sym.st_shndx =
5400 elf_section_data (s->output_section)->this_idx;
5401 sym.st_value = (s->output_section->vma
5402 + s->output_offset
5403 + e->isym.st_value);
5404 }
5405
5406 if (last_local < e->dynindx)
5407 last_local = e->dynindx;
5408
5409 dest = dynsym + e->dynindx;
5410 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5411 }
5412 }
5413
5414 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
5415 last_local + 1;
5416 }
5417
5418 /* We get the global symbols from the hash table. */
5419 eoinfo.failed = false;
5420 eoinfo.localsyms = false;
5421 eoinfo.finfo = &finfo;
5422 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5423 (PTR) &eoinfo);
5424 if (eoinfo.failed)
5425 return false;
5426
5427 /* If backend needs to output some symbols not present in the hash
5428 table, do it now. */
5429 if (bed->elf_backend_output_arch_syms)
5430 {
5431 typedef boolean (*out_sym_func) PARAMS ((PTR, const char *,
5432 Elf_Internal_Sym *,
5433 asection *));
5434
5435 if (! ((*bed->elf_backend_output_arch_syms)
5436 (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
5437 return false;
5438 }
5439
5440 /* Flush all symbols to the file. */
5441 if (! elf_link_flush_output_syms (&finfo))
5442 return false;
5443
5444 /* Now we know the size of the symtab section. */
5445 off += symtab_hdr->sh_size;
5446
5447 /* Finish up and write out the symbol string table (.strtab)
5448 section. */
5449 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5450 /* sh_name was set in prep_headers. */
5451 symstrtab_hdr->sh_type = SHT_STRTAB;
5452 symstrtab_hdr->sh_flags = 0;
5453 symstrtab_hdr->sh_addr = 0;
5454 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
5455 symstrtab_hdr->sh_entsize = 0;
5456 symstrtab_hdr->sh_link = 0;
5457 symstrtab_hdr->sh_info = 0;
5458 /* sh_offset is set just below. */
5459 symstrtab_hdr->sh_addralign = 1;
5460
5461 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
5462 elf_tdata (abfd)->next_file_pos = off;
5463
5464 if (bfd_get_symcount (abfd) > 0)
5465 {
5466 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
5467 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
5468 return false;
5469 }
5470
5471 /* Adjust the relocs to have the correct symbol indices. */
5472 for (o = abfd->sections; o != NULL; o = o->next)
5473 {
5474 if ((o->flags & SEC_RELOC) == 0)
5475 continue;
5476
5477 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
5478 elf_section_data (o)->rel_count,
5479 elf_section_data (o)->rel_hashes);
5480 if (elf_section_data (o)->rel_hdr2 != NULL)
5481 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
5482 elf_section_data (o)->rel_count2,
5483 (elf_section_data (o)->rel_hashes
5484 + elf_section_data (o)->rel_count));
5485
5486 /* Set the reloc_count field to 0 to prevent write_relocs from
5487 trying to swap the relocs out itself. */
5488 o->reloc_count = 0;
5489 }
5490
5491 if (dynamic && info->combreloc && dynobj != NULL)
5492 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
5493
5494 /* If we are linking against a dynamic object, or generating a
5495 shared library, finish up the dynamic linking information. */
5496 if (dynamic)
5497 {
5498 Elf_External_Dyn *dyncon, *dynconend;
5499
5500 /* Fix up .dynamic entries. */
5501 o = bfd_get_section_by_name (dynobj, ".dynamic");
5502 BFD_ASSERT (o != NULL);
5503
5504 dyncon = (Elf_External_Dyn *) o->contents;
5505 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5506 for (; dyncon < dynconend; dyncon++)
5507 {
5508 Elf_Internal_Dyn dyn;
5509 const char *name;
5510 unsigned int type;
5511
5512 elf_swap_dyn_in (dynobj, dyncon, &dyn);
5513
5514 switch (dyn.d_tag)
5515 {
5516 default:
5517 break;
5518 case DT_NULL:
5519 if (relativecount > 0 && dyncon + 1 < dynconend)
5520 {
5521 switch (elf_section_data (reldyn)->this_hdr.sh_type)
5522 {
5523 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
5524 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
5525 default: break;
5526 }
5527 if (dyn.d_tag != DT_NULL)
5528 {
5529 dyn.d_un.d_val = relativecount;
5530 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5531 relativecount = 0;
5532 }
5533 }
5534 break;
5535 case DT_INIT:
5536 name = info->init_function;
5537 goto get_sym;
5538 case DT_FINI:
5539 name = info->fini_function;
5540 get_sym:
5541 {
5542 struct elf_link_hash_entry *h;
5543
5544 h = elf_link_hash_lookup (elf_hash_table (info), name,
5545 false, false, true);
5546 if (h != NULL
5547 && (h->root.type == bfd_link_hash_defined
5548 || h->root.type == bfd_link_hash_defweak))
5549 {
5550 dyn.d_un.d_val = h->root.u.def.value;
5551 o = h->root.u.def.section;
5552 if (o->output_section != NULL)
5553 dyn.d_un.d_val += (o->output_section->vma
5554 + o->output_offset);
5555 else
5556 {
5557 /* The symbol is imported from another shared
5558 library and does not apply to this one. */
5559 dyn.d_un.d_val = 0;
5560 }
5561
5562 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5563 }
5564 }
5565 break;
5566
5567 case DT_HASH:
5568 name = ".hash";
5569 goto get_vma;
5570 case DT_STRTAB:
5571 name = ".dynstr";
5572 goto get_vma;
5573 case DT_SYMTAB:
5574 name = ".dynsym";
5575 goto get_vma;
5576 case DT_VERDEF:
5577 name = ".gnu.version_d";
5578 goto get_vma;
5579 case DT_VERNEED:
5580 name = ".gnu.version_r";
5581 goto get_vma;
5582 case DT_VERSYM:
5583 name = ".gnu.version";
5584 get_vma:
5585 o = bfd_get_section_by_name (abfd, name);
5586 BFD_ASSERT (o != NULL);
5587 dyn.d_un.d_ptr = o->vma;
5588 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5589 break;
5590
5591 case DT_REL:
5592 case DT_RELA:
5593 case DT_RELSZ:
5594 case DT_RELASZ:
5595 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5596 type = SHT_REL;
5597 else
5598 type = SHT_RELA;
5599 dyn.d_un.d_val = 0;
5600 for (i = 1; i < elf_numsections (abfd); i++)
5601 {
5602 Elf_Internal_Shdr *hdr;
5603
5604 hdr = elf_elfsections (abfd)[i];
5605 if (hdr->sh_type == type
5606 && (hdr->sh_flags & SHF_ALLOC) != 0)
5607 {
5608 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5609 dyn.d_un.d_val += hdr->sh_size;
5610 else
5611 {
5612 if (dyn.d_un.d_val == 0
5613 || hdr->sh_addr < dyn.d_un.d_val)
5614 dyn.d_un.d_val = hdr->sh_addr;
5615 }
5616 }
5617 }
5618 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5619 break;
5620 }
5621 }
5622 }
5623
5624 /* If we have created any dynamic sections, then output them. */
5625 if (dynobj != NULL)
5626 {
5627 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5628 goto error_return;
5629
5630 for (o = dynobj->sections; o != NULL; o = o->next)
5631 {
5632 if ((o->flags & SEC_HAS_CONTENTS) == 0
5633 || o->_raw_size == 0
5634 || o->output_section == bfd_abs_section_ptr)
5635 continue;
5636 if ((o->flags & SEC_LINKER_CREATED) == 0)
5637 {
5638 /* At this point, we are only interested in sections
5639 created by elf_link_create_dynamic_sections. */
5640 continue;
5641 }
5642 if ((elf_section_data (o->output_section)->this_hdr.sh_type
5643 != SHT_STRTAB)
5644 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
5645 {
5646 if (! bfd_set_section_contents (abfd, o->output_section,
5647 o->contents,
5648 (file_ptr) o->output_offset,
5649 o->_raw_size))
5650 goto error_return;
5651 }
5652 else
5653 {
5654 /* The contents of the .dynstr section are actually in a
5655 stringtab. */
5656 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
5657 if (bfd_seek (abfd, off, SEEK_SET) != 0
5658 || ! _bfd_elf_strtab_emit (abfd,
5659 elf_hash_table (info)->dynstr))
5660 goto error_return;
5661 }
5662 }
5663 }
5664
5665 /* If we have optimized stabs strings, output them. */
5666 if (elf_hash_table (info)->stab_info != NULL)
5667 {
5668 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
5669 goto error_return;
5670 }
5671
5672 if (info->eh_frame_hdr && elf_hash_table (info)->dynobj)
5673 {
5674 o = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
5675 ".eh_frame_hdr");
5676 if (o
5677 && (elf_section_data (o)->sec_info_type
5678 == ELF_INFO_TYPE_EH_FRAME_HDR))
5679 {
5680 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, o))
5681 goto error_return;
5682 }
5683 }
5684
5685 if (finfo.symstrtab != NULL)
5686 _bfd_stringtab_free (finfo.symstrtab);
5687 if (finfo.contents != NULL)
5688 free (finfo.contents);
5689 if (finfo.external_relocs != NULL)
5690 free (finfo.external_relocs);
5691 if (finfo.internal_relocs != NULL)
5692 free (finfo.internal_relocs);
5693 if (finfo.external_syms != NULL)
5694 free (finfo.external_syms);
5695 if (finfo.locsym_shndx != NULL)
5696 free (finfo.locsym_shndx);
5697 if (finfo.internal_syms != NULL)
5698 free (finfo.internal_syms);
5699 if (finfo.indices != NULL)
5700 free (finfo.indices);
5701 if (finfo.sections != NULL)
5702 free (finfo.sections);
5703 if (finfo.symbuf != NULL)
5704 free (finfo.symbuf);
5705 if (finfo.symshndxbuf != NULL)
5706 free (finfo.symbuf);
5707 for (o = abfd->sections; o != NULL; o = o->next)
5708 {
5709 if ((o->flags & SEC_RELOC) != 0
5710 && elf_section_data (o)->rel_hashes != NULL)
5711 free (elf_section_data (o)->rel_hashes);
5712 }
5713
5714 elf_tdata (abfd)->linker = true;
5715
5716 return true;
5717
5718 error_return:
5719 if (finfo.symstrtab != NULL)
5720 _bfd_stringtab_free (finfo.symstrtab);
5721 if (finfo.contents != NULL)
5722 free (finfo.contents);
5723 if (finfo.external_relocs != NULL)
5724 free (finfo.external_relocs);
5725 if (finfo.internal_relocs != NULL)
5726 free (finfo.internal_relocs);
5727 if (finfo.external_syms != NULL)
5728 free (finfo.external_syms);
5729 if (finfo.locsym_shndx != NULL)
5730 free (finfo.locsym_shndx);
5731 if (finfo.internal_syms != NULL)
5732 free (finfo.internal_syms);
5733 if (finfo.indices != NULL)
5734 free (finfo.indices);
5735 if (finfo.sections != NULL)
5736 free (finfo.sections);
5737 if (finfo.symbuf != NULL)
5738 free (finfo.symbuf);
5739 if (finfo.symshndxbuf != NULL)
5740 free (finfo.symbuf);
5741 for (o = abfd->sections; o != NULL; o = o->next)
5742 {
5743 if ((o->flags & SEC_RELOC) != 0
5744 && elf_section_data (o)->rel_hashes != NULL)
5745 free (elf_section_data (o)->rel_hashes);
5746 }
5747
5748 return false;
5749 }
5750
5751 /* Add a symbol to the output symbol table. */
5752
5753 static boolean
5754 elf_link_output_sym (finfo, name, elfsym, input_sec)
5755 struct elf_final_link_info *finfo;
5756 const char *name;
5757 Elf_Internal_Sym *elfsym;
5758 asection *input_sec;
5759 {
5760 Elf_External_Sym *dest;
5761 Elf_External_Sym_Shndx *destshndx;
5762
5763 boolean (*output_symbol_hook) PARAMS ((bfd *,
5764 struct bfd_link_info *info,
5765 const char *,
5766 Elf_Internal_Sym *,
5767 asection *));
5768
5769 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
5770 elf_backend_link_output_symbol_hook;
5771 if (output_symbol_hook != NULL)
5772 {
5773 if (! ((*output_symbol_hook)
5774 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
5775 return false;
5776 }
5777
5778 if (name == (const char *) NULL || *name == '\0')
5779 elfsym->st_name = 0;
5780 else if (input_sec->flags & SEC_EXCLUDE)
5781 elfsym->st_name = 0;
5782 else
5783 {
5784 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5785 name, true, false);
5786 if (elfsym->st_name == (unsigned long) -1)
5787 return false;
5788 }
5789
5790 if (finfo->symbuf_count >= finfo->symbuf_size)
5791 {
5792 if (! elf_link_flush_output_syms (finfo))
5793 return false;
5794 }
5795
5796 dest = finfo->symbuf + finfo->symbuf_count;
5797 destshndx = finfo->symshndxbuf;
5798 if (destshndx != NULL)
5799 destshndx += finfo->symbuf_count;
5800 elf_swap_symbol_out (finfo->output_bfd, elfsym, (PTR) dest, (PTR) destshndx);
5801 ++finfo->symbuf_count;
5802
5803 ++ bfd_get_symcount (finfo->output_bfd);
5804
5805 return true;
5806 }
5807
5808 /* Flush the output symbols to the file. */
5809
5810 static boolean
5811 elf_link_flush_output_syms (finfo)
5812 struct elf_final_link_info *finfo;
5813 {
5814 if (finfo->symbuf_count > 0)
5815 {
5816 Elf_Internal_Shdr *hdr;
5817 file_ptr pos;
5818 bfd_size_type amt;
5819
5820 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5821 pos = hdr->sh_offset + hdr->sh_size;
5822 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
5823 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5824 || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
5825 return false;
5826
5827 hdr->sh_size += amt;
5828
5829 if (finfo->symshndxbuf != NULL)
5830 {
5831 hdr = &elf_tdata (finfo->output_bfd)->symtab_shndx_hdr;
5832 pos = hdr->sh_offset + hdr->sh_size;
5833 amt = finfo->symbuf_count * sizeof (Elf_External_Sym_Shndx);
5834 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5835 || (bfd_bwrite ((PTR) finfo->symshndxbuf, amt, finfo->output_bfd)
5836 != amt))
5837 return false;
5838
5839 hdr->sh_size += amt;
5840 }
5841
5842 finfo->symbuf_count = 0;
5843 }
5844
5845 return true;
5846 }
5847
5848 /* Adjust all external symbols pointing into SEC_MERGE sections
5849 to reflect the object merging within the sections. */
5850
5851 static boolean
5852 elf_link_sec_merge_syms (h, data)
5853 struct elf_link_hash_entry *h;
5854 PTR data;
5855 {
5856 asection *sec;
5857
5858 if ((h->root.type == bfd_link_hash_defined
5859 || h->root.type == bfd_link_hash_defweak)
5860 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
5861 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
5862 {
5863 bfd *output_bfd = (bfd *) data;
5864
5865 h->root.u.def.value =
5866 _bfd_merged_section_offset (output_bfd,
5867 &h->root.u.def.section,
5868 elf_section_data (sec)->sec_info,
5869 h->root.u.def.value, (bfd_vma) 0);
5870 }
5871
5872 return true;
5873 }
5874
5875 /* Add an external symbol to the symbol table. This is called from
5876 the hash table traversal routine. When generating a shared object,
5877 we go through the symbol table twice. The first time we output
5878 anything that might have been forced to local scope in a version
5879 script. The second time we output the symbols that are still
5880 global symbols. */
5881
5882 static boolean
5883 elf_link_output_extsym (h, data)
5884 struct elf_link_hash_entry *h;
5885 PTR data;
5886 {
5887 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
5888 struct elf_final_link_info *finfo = eoinfo->finfo;
5889 boolean strip;
5890 Elf_Internal_Sym sym;
5891 asection *input_sec;
5892
5893 /* Decide whether to output this symbol in this pass. */
5894 if (eoinfo->localsyms)
5895 {
5896 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5897 return true;
5898 }
5899 else
5900 {
5901 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5902 return true;
5903 }
5904
5905 /* If we are not creating a shared library, and this symbol is
5906 referenced by a shared library but is not defined anywhere, then
5907 warn that it is undefined. If we do not do this, the runtime
5908 linker will complain that the symbol is undefined when the
5909 program is run. We don't have to worry about symbols that are
5910 referenced by regular files, because we will already have issued
5911 warnings for them. */
5912 if (! finfo->info->relocateable
5913 && ! finfo->info->allow_shlib_undefined
5914 && ! finfo->info->shared
5915 && h->root.type == bfd_link_hash_undefined
5916 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
5917 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5918 {
5919 if (! ((*finfo->info->callbacks->undefined_symbol)
5920 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
5921 (asection *) NULL, (bfd_vma) 0, true)))
5922 {
5923 eoinfo->failed = true;
5924 return false;
5925 }
5926 }
5927
5928 /* We don't want to output symbols that have never been mentioned by
5929 a regular file, or that we have been told to strip. However, if
5930 h->indx is set to -2, the symbol is used by a reloc and we must
5931 output it. */
5932 if (h->indx == -2)
5933 strip = false;
5934 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5935 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
5936 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
5937 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5938 strip = true;
5939 else if (finfo->info->strip == strip_all
5940 || (finfo->info->strip == strip_some
5941 && bfd_hash_lookup (finfo->info->keep_hash,
5942 h->root.root.string,
5943 false, false) == NULL))
5944 strip = true;
5945 else
5946 strip = false;
5947
5948 /* If we're stripping it, and it's not a dynamic symbol, there's
5949 nothing else to do unless it is a forced local symbol. */
5950 if (strip
5951 && h->dynindx == -1
5952 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5953 return true;
5954
5955 sym.st_value = 0;
5956 sym.st_size = h->size;
5957 sym.st_other = h->other;
5958 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5959 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
5960 else if (h->root.type == bfd_link_hash_undefweak
5961 || h->root.type == bfd_link_hash_defweak)
5962 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
5963 else
5964 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
5965
5966 switch (h->root.type)
5967 {
5968 default:
5969 case bfd_link_hash_new:
5970 abort ();
5971 return false;
5972
5973 case bfd_link_hash_undefined:
5974 input_sec = bfd_und_section_ptr;
5975 sym.st_shndx = SHN_UNDEF;
5976 break;
5977
5978 case bfd_link_hash_undefweak:
5979 input_sec = bfd_und_section_ptr;
5980 sym.st_shndx = SHN_UNDEF;
5981 break;
5982
5983 case bfd_link_hash_defined:
5984 case bfd_link_hash_defweak:
5985 {
5986 input_sec = h->root.u.def.section;
5987 if (input_sec->output_section != NULL)
5988 {
5989 sym.st_shndx =
5990 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
5991 input_sec->output_section);
5992 if (sym.st_shndx == SHN_BAD)
5993 {
5994 (*_bfd_error_handler)
5995 (_("%s: could not find output section %s for input section %s"),
5996 bfd_get_filename (finfo->output_bfd),
5997 input_sec->output_section->name,
5998 input_sec->name);
5999 eoinfo->failed = true;
6000 return false;
6001 }
6002
6003 /* ELF symbols in relocateable files are section relative,
6004 but in nonrelocateable files they are virtual
6005 addresses. */
6006 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6007 if (! finfo->info->relocateable)
6008 sym.st_value += input_sec->output_section->vma;
6009 }
6010 else
6011 {
6012 BFD_ASSERT (input_sec->owner == NULL
6013 || (input_sec->owner->flags & DYNAMIC) != 0);
6014 sym.st_shndx = SHN_UNDEF;
6015 input_sec = bfd_und_section_ptr;
6016 }
6017 }
6018 break;
6019
6020 case bfd_link_hash_common:
6021 input_sec = h->root.u.c.p->section;
6022 sym.st_shndx = SHN_COMMON;
6023 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6024 break;
6025
6026 case bfd_link_hash_indirect:
6027 /* These symbols are created by symbol versioning. They point
6028 to the decorated version of the name. For example, if the
6029 symbol foo@@GNU_1.2 is the default, which should be used when
6030 foo is used with no version, then we add an indirect symbol
6031 foo which points to foo@@GNU_1.2. We ignore these symbols,
6032 since the indirected symbol is already in the hash table. */
6033 return true;
6034
6035 case bfd_link_hash_warning:
6036 /* We can't represent these symbols in ELF, although a warning
6037 symbol may have come from a .gnu.warning.SYMBOL section. We
6038 just put the target symbol in the hash table. If the target
6039 symbol does not really exist, don't do anything. */
6040 if (h->root.u.i.link->type == bfd_link_hash_new)
6041 return true;
6042 return (elf_link_output_extsym
6043 ((struct elf_link_hash_entry *) h->root.u.i.link, data));
6044 }
6045
6046 /* Give the processor backend a chance to tweak the symbol value,
6047 and also to finish up anything that needs to be done for this
6048 symbol. */
6049 if ((h->dynindx != -1
6050 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6051 && elf_hash_table (finfo->info)->dynamic_sections_created)
6052 {
6053 struct elf_backend_data *bed;
6054
6055 bed = get_elf_backend_data (finfo->output_bfd);
6056 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6057 (finfo->output_bfd, finfo->info, h, &sym)))
6058 {
6059 eoinfo->failed = true;
6060 return false;
6061 }
6062 }
6063
6064 /* If we are marking the symbol as undefined, and there are no
6065 non-weak references to this symbol from a regular object, then
6066 mark the symbol as weak undefined; if there are non-weak
6067 references, mark the symbol as strong. We can't do this earlier,
6068 because it might not be marked as undefined until the
6069 finish_dynamic_symbol routine gets through with it. */
6070 if (sym.st_shndx == SHN_UNDEF
6071 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
6072 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6073 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6074 {
6075 int bindtype;
6076
6077 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6078 bindtype = STB_GLOBAL;
6079 else
6080 bindtype = STB_WEAK;
6081 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6082 }
6083
6084 /* If a symbol is not defined locally, we clear the visibility
6085 field. */
6086 if (! finfo->info->relocateable
6087 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6088 sym.st_other ^= ELF_ST_VISIBILITY (sym.st_other);
6089
6090 /* If this symbol should be put in the .dynsym section, then put it
6091 there now. We have already know the symbol index. We also fill
6092 in the entry in the .hash section. */
6093 if (h->dynindx != -1
6094 && elf_hash_table (finfo->info)->dynamic_sections_created)
6095 {
6096 size_t bucketcount;
6097 size_t bucket;
6098 size_t hash_entry_size;
6099 bfd_byte *bucketpos;
6100 bfd_vma chain;
6101 Elf_External_Sym *esym;
6102
6103 sym.st_name = h->dynstr_index;
6104 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
6105 elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym, (PTR) 0);
6106
6107 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6108 bucket = h->elf_hash_value % bucketcount;
6109 hash_entry_size
6110 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6111 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6112 + (bucket + 2) * hash_entry_size);
6113 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6114 bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
6115 bucketpos);
6116 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6117 ((bfd_byte *) finfo->hash_sec->contents
6118 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6119
6120 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6121 {
6122 Elf_Internal_Versym iversym;
6123 Elf_External_Versym *eversym;
6124
6125 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6126 {
6127 if (h->verinfo.verdef == NULL)
6128 iversym.vs_vers = 0;
6129 else
6130 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6131 }
6132 else
6133 {
6134 if (h->verinfo.vertree == NULL)
6135 iversym.vs_vers = 1;
6136 else
6137 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6138 }
6139
6140 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6141 iversym.vs_vers |= VERSYM_HIDDEN;
6142
6143 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6144 eversym += h->dynindx;
6145 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6146 }
6147 }
6148
6149 /* If we're stripping it, then it was just a dynamic symbol, and
6150 there's nothing else to do. */
6151 if (strip)
6152 return true;
6153
6154 h->indx = bfd_get_symcount (finfo->output_bfd);
6155
6156 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
6157 {
6158 eoinfo->failed = true;
6159 return false;
6160 }
6161
6162 return true;
6163 }
6164
6165 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6166 originated from the section given by INPUT_REL_HDR) to the
6167 OUTPUT_BFD. */
6168
6169 static void
6170 elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
6171 internal_relocs)
6172 bfd *output_bfd;
6173 asection *input_section;
6174 Elf_Internal_Shdr *input_rel_hdr;
6175 Elf_Internal_Rela *internal_relocs;
6176 {
6177 Elf_Internal_Rela *irela;
6178 Elf_Internal_Rela *irelaend;
6179 Elf_Internal_Shdr *output_rel_hdr;
6180 asection *output_section;
6181 unsigned int *rel_countp = NULL;
6182 struct elf_backend_data *bed;
6183 bfd_size_type amt;
6184
6185 output_section = input_section->output_section;
6186 output_rel_hdr = NULL;
6187
6188 if (elf_section_data (output_section)->rel_hdr.sh_entsize
6189 == input_rel_hdr->sh_entsize)
6190 {
6191 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
6192 rel_countp = &elf_section_data (output_section)->rel_count;
6193 }
6194 else if (elf_section_data (output_section)->rel_hdr2
6195 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
6196 == input_rel_hdr->sh_entsize))
6197 {
6198 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
6199 rel_countp = &elf_section_data (output_section)->rel_count2;
6200 }
6201
6202 BFD_ASSERT (output_rel_hdr != NULL);
6203
6204 bed = get_elf_backend_data (output_bfd);
6205 irela = internal_relocs;
6206 irelaend = irela + NUM_SHDR_ENTRIES (input_rel_hdr)
6207 * bed->s->int_rels_per_ext_rel;
6208
6209 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
6210 {
6211 Elf_External_Rel *erel;
6212 Elf_Internal_Rel *irel;
6213
6214 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
6215 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
6216 if (irel == NULL)
6217 {
6218 (*_bfd_error_handler) (_("Error: out of memory"));
6219 abort ();
6220 }
6221
6222 erel = ((Elf_External_Rel *) output_rel_hdr->contents + *rel_countp);
6223 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erel++)
6224 {
6225 unsigned int i;
6226
6227 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6228 {
6229 irel[i].r_offset = irela[i].r_offset;
6230 irel[i].r_info = irela[i].r_info;
6231 BFD_ASSERT (irela[i].r_addend == 0);
6232 }
6233
6234 if (bed->s->swap_reloc_out)
6235 (*bed->s->swap_reloc_out) (output_bfd, irel, (PTR) erel);
6236 else
6237 elf_swap_reloc_out (output_bfd, irel, erel);
6238 }
6239
6240 free (irel);
6241 }
6242 else
6243 {
6244 Elf_External_Rela *erela;
6245
6246 BFD_ASSERT (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
6247
6248 erela = ((Elf_External_Rela *) output_rel_hdr->contents + *rel_countp);
6249 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erela++)
6250 if (bed->s->swap_reloca_out)
6251 (*bed->s->swap_reloca_out) (output_bfd, irela, (PTR) erela);
6252 else
6253 elf_swap_reloca_out (output_bfd, irela, erela);
6254 }
6255
6256 /* Bump the counter, so that we know where to add the next set of
6257 relocations. */
6258 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
6259 }
6260
6261 /* Link an input file into the linker output file. This function
6262 handles all the sections and relocations of the input file at once.
6263 This is so that we only have to read the local symbols once, and
6264 don't have to keep them in memory. */
6265
6266 static boolean
6267 elf_link_input_bfd (finfo, input_bfd)
6268 struct elf_final_link_info *finfo;
6269 bfd *input_bfd;
6270 {
6271 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
6272 bfd *, asection *, bfd_byte *,
6273 Elf_Internal_Rela *,
6274 Elf_Internal_Sym *, asection **));
6275 bfd *output_bfd;
6276 Elf_Internal_Shdr *symtab_hdr;
6277 Elf_Internal_Shdr *shndx_hdr;
6278 size_t locsymcount;
6279 size_t extsymoff;
6280 Elf_External_Sym *external_syms;
6281 Elf_External_Sym *esym;
6282 Elf_External_Sym *esymend;
6283 Elf_External_Sym_Shndx *shndx_buf;
6284 Elf_External_Sym_Shndx *shndx;
6285 Elf_Internal_Sym *isym;
6286 long *pindex;
6287 asection **ppsection;
6288 asection *o;
6289 struct elf_backend_data *bed;
6290 boolean emit_relocs;
6291 struct elf_link_hash_entry **sym_hashes;
6292
6293 output_bfd = finfo->output_bfd;
6294 bed = get_elf_backend_data (output_bfd);
6295 relocate_section = bed->elf_backend_relocate_section;
6296
6297 /* If this is a dynamic object, we don't want to do anything here:
6298 we don't want the local symbols, and we don't want the section
6299 contents. */
6300 if ((input_bfd->flags & DYNAMIC) != 0)
6301 return true;
6302
6303 emit_relocs = (finfo->info->relocateable
6304 || finfo->info->emitrelocations
6305 || bed->elf_backend_emit_relocs);
6306
6307 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6308 if (elf_bad_symtab (input_bfd))
6309 {
6310 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6311 extsymoff = 0;
6312 }
6313 else
6314 {
6315 locsymcount = symtab_hdr->sh_info;
6316 extsymoff = symtab_hdr->sh_info;
6317 }
6318
6319 /* Read the local symbols. */
6320 if (symtab_hdr->contents != NULL)
6321 external_syms = (Elf_External_Sym *) symtab_hdr->contents;
6322 else if (locsymcount == 0)
6323 external_syms = NULL;
6324 else
6325 {
6326 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym);
6327 external_syms = finfo->external_syms;
6328 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
6329 || bfd_bread (external_syms, amt, input_bfd) != amt)
6330 return false;
6331 }
6332
6333 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
6334 shndx_buf = NULL;
6335 if (shndx_hdr->sh_size != 0 && locsymcount != 0)
6336 {
6337 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym_Shndx);
6338 shndx_buf = finfo->locsym_shndx;
6339 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
6340 || bfd_bread (shndx_buf, amt, input_bfd) != amt)
6341 return false;
6342 }
6343
6344 /* Swap in the local symbols and write out the ones which we know
6345 are going into the output file. */
6346 for (esym = external_syms, esymend = esym + locsymcount,
6347 isym = finfo->internal_syms, pindex = finfo->indices,
6348 ppsection = finfo->sections, shndx = shndx_buf;
6349 esym < esymend;
6350 esym++, isym++, pindex++, ppsection++,
6351 shndx = (shndx != NULL ? shndx + 1 : NULL))
6352 {
6353 asection *isec;
6354 const char *name;
6355 Elf_Internal_Sym osym;
6356
6357 elf_swap_symbol_in (input_bfd, esym, shndx, isym);
6358 *pindex = -1;
6359
6360 if (elf_bad_symtab (input_bfd))
6361 {
6362 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6363 {
6364 *ppsection = NULL;
6365 continue;
6366 }
6367 }
6368
6369 if (isym->st_shndx == SHN_UNDEF)
6370 isec = bfd_und_section_ptr;
6371 else if (isym->st_shndx < SHN_LORESERVE
6372 || isym->st_shndx > SHN_HIRESERVE)
6373 {
6374 isec = section_from_elf_index (input_bfd, isym->st_shndx);
6375 if (isec
6376 && elf_section_data (isec)->sec_info_type == ELF_INFO_TYPE_MERGE
6377 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6378 isym->st_value =
6379 _bfd_merged_section_offset (output_bfd, &isec,
6380 elf_section_data (isec)->sec_info,
6381 isym->st_value, (bfd_vma) 0);
6382 }
6383 else if (isym->st_shndx == SHN_ABS)
6384 isec = bfd_abs_section_ptr;
6385 else if (isym->st_shndx == SHN_COMMON)
6386 isec = bfd_com_section_ptr;
6387 else
6388 {
6389 /* Who knows? */
6390 isec = NULL;
6391 }
6392
6393 *ppsection = isec;
6394
6395 /* Don't output the first, undefined, symbol. */
6396 if (esym == external_syms)
6397 continue;
6398
6399 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6400 {
6401 /* We never output section symbols. Instead, we use the
6402 section symbol of the corresponding section in the output
6403 file. */
6404 continue;
6405 }
6406
6407 /* If we are stripping all symbols, we don't want to output this
6408 one. */
6409 if (finfo->info->strip == strip_all)
6410 continue;
6411
6412 /* If we are discarding all local symbols, we don't want to
6413 output this one. If we are generating a relocateable output
6414 file, then some of the local symbols may be required by
6415 relocs; we output them below as we discover that they are
6416 needed. */
6417 if (finfo->info->discard == discard_all)
6418 continue;
6419
6420 /* If this symbol is defined in a section which we are
6421 discarding, we don't need to keep it, but note that
6422 linker_mark is only reliable for sections that have contents.
6423 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6424 as well as linker_mark. */
6425 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6426 && isec != NULL
6427 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6428 || (! finfo->info->relocateable
6429 && (isec->flags & SEC_EXCLUDE) != 0)))
6430 continue;
6431
6432 /* Get the name of the symbol. */
6433 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6434 isym->st_name);
6435 if (name == NULL)
6436 return false;
6437
6438 /* See if we are discarding symbols with this name. */
6439 if ((finfo->info->strip == strip_some
6440 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
6441 == NULL))
6442 || (((finfo->info->discard == discard_sec_merge
6443 && (isec->flags & SEC_MERGE) && ! finfo->info->relocateable)
6444 || finfo->info->discard == discard_l)
6445 && bfd_is_local_label_name (input_bfd, name)))
6446 continue;
6447
6448 /* If we get here, we are going to output this symbol. */
6449
6450 osym = *isym;
6451
6452 /* Adjust the section index for the output file. */
6453 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6454 isec->output_section);
6455 if (osym.st_shndx == SHN_BAD)
6456 return false;
6457
6458 *pindex = bfd_get_symcount (output_bfd);
6459
6460 /* ELF symbols in relocateable files are section relative, but
6461 in executable files they are virtual addresses. Note that
6462 this code assumes that all ELF sections have an associated
6463 BFD section with a reasonable value for output_offset; below
6464 we assume that they also have a reasonable value for
6465 output_section. Any special sections must be set up to meet
6466 these requirements. */
6467 osym.st_value += isec->output_offset;
6468 if (! finfo->info->relocateable)
6469 osym.st_value += isec->output_section->vma;
6470
6471 if (! elf_link_output_sym (finfo, name, &osym, isec))
6472 return false;
6473 }
6474
6475 /* Relocate the contents of each section. */
6476 sym_hashes = elf_sym_hashes (input_bfd);
6477 for (o = input_bfd->sections; o != NULL; o = o->next)
6478 {
6479 bfd_byte *contents;
6480
6481 if (! o->linker_mark)
6482 {
6483 /* This section was omitted from the link. */
6484 continue;
6485 }
6486
6487 if ((o->flags & SEC_HAS_CONTENTS) == 0
6488 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
6489 continue;
6490
6491 if ((o->flags & SEC_LINKER_CREATED) != 0)
6492 {
6493 /* Section was created by elf_link_create_dynamic_sections
6494 or somesuch. */
6495 continue;
6496 }
6497
6498 /* Get the contents of the section. They have been cached by a
6499 relaxation routine. Note that o is a section in an input
6500 file, so the contents field will not have been set by any of
6501 the routines which work on output files. */
6502 if (elf_section_data (o)->this_hdr.contents != NULL)
6503 contents = elf_section_data (o)->this_hdr.contents;
6504 else
6505 {
6506 contents = finfo->contents;
6507 if (! bfd_get_section_contents (input_bfd, o, contents,
6508 (file_ptr) 0, o->_raw_size))
6509 return false;
6510 }
6511
6512 if ((o->flags & SEC_RELOC) != 0)
6513 {
6514 Elf_Internal_Rela *internal_relocs;
6515
6516 /* Get the swapped relocs. */
6517 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
6518 (input_bfd, o, finfo->external_relocs,
6519 finfo->internal_relocs, false));
6520 if (internal_relocs == NULL
6521 && o->reloc_count > 0)
6522 return false;
6523
6524 /* Run through the relocs looking for any against symbols
6525 from discarded sections and section symbols from
6526 removed link-once sections. Complain about relocs
6527 against discarded sections. Zero relocs against removed
6528 link-once sections. We should really complain if
6529 anything in the final link tries to use it, but
6530 DWARF-based exception handling might have an entry in
6531 .eh_frame to describe a routine in the linkonce section,
6532 and it turns out to be hard to remove the .eh_frame
6533 entry too. FIXME. */
6534 if (!finfo->info->relocateable
6535 && !elf_section_ignore_discarded_relocs (o))
6536 {
6537 Elf_Internal_Rela *rel, *relend;
6538
6539 rel = internal_relocs;
6540 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6541 for ( ; rel < relend; rel++)
6542 {
6543 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6544
6545 if (r_symndx >= locsymcount
6546 || (elf_bad_symtab (input_bfd)
6547 && finfo->sections[r_symndx] == NULL))
6548 {
6549 struct elf_link_hash_entry *h;
6550
6551 h = sym_hashes[r_symndx - extsymoff];
6552 while (h->root.type == bfd_link_hash_indirect
6553 || h->root.type == bfd_link_hash_warning)
6554 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6555
6556 /* Complain if the definition comes from a
6557 discarded section. */
6558 if ((h->root.type == bfd_link_hash_defined
6559 || h->root.type == bfd_link_hash_defweak)
6560 && elf_discarded_section (h->root.u.def.section))
6561 {
6562 #if BFD_VERSION_DATE < 20031005
6563 if ((o->flags & SEC_DEBUGGING) != 0)
6564 {
6565 #if BFD_VERSION_DATE > 20021005
6566 (*finfo->info->callbacks->warning)
6567 (finfo->info,
6568 _("warning: relocation against removed section; zeroing"),
6569 NULL, input_bfd, o, rel->r_offset);
6570 #endif
6571 BFD_ASSERT (r_symndx != 0);
6572 memset (rel, 0, sizeof (*rel));
6573 }
6574 else
6575 #endif
6576 {
6577 if (! ((*finfo->info->callbacks->undefined_symbol)
6578 (finfo->info, h->root.root.string,
6579 input_bfd, o, rel->r_offset,
6580 true)))
6581 return false;
6582 }
6583 }
6584 }
6585 else
6586 {
6587 asection *sec = finfo->sections[r_symndx];
6588
6589 if (sec != NULL && elf_discarded_section (sec))
6590 {
6591 #if BFD_VERSION_DATE < 20031005
6592 if ((o->flags & SEC_DEBUGGING) != 0
6593 || (sec->flags & SEC_LINK_ONCE) != 0)
6594 {
6595 #if BFD_VERSION_DATE > 20021005
6596 (*finfo->info->callbacks->warning)
6597 (finfo->info,
6598 _("warning: relocation against removed section"),
6599 NULL, input_bfd, o, rel->r_offset);
6600 #endif
6601 BFD_ASSERT (r_symndx != 0);
6602 rel->r_info
6603 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
6604 rel->r_addend = 0;
6605 }
6606 else
6607 #endif
6608 {
6609 boolean ok;
6610 const char *msg
6611 = _("local symbols in discarded section %s");
6612 bfd_size_type amt
6613 = strlen (sec->name) + strlen (msg) - 1;
6614 char *buf = (char *) bfd_malloc (amt);
6615
6616 if (buf != NULL)
6617 sprintf (buf, msg, sec->name);
6618 else
6619 buf = (char *) sec->name;
6620 ok = (*finfo->info->callbacks
6621 ->undefined_symbol) (finfo->info, buf,
6622 input_bfd, o,
6623 rel->r_offset,
6624 true);
6625 if (buf != sec->name)
6626 free (buf);
6627 if (!ok)
6628 return false;
6629 }
6630 }
6631 }
6632 }
6633 }
6634
6635 /* Relocate the section by invoking a back end routine.
6636
6637 The back end routine is responsible for adjusting the
6638 section contents as necessary, and (if using Rela relocs
6639 and generating a relocateable output file) adjusting the
6640 reloc addend as necessary.
6641
6642 The back end routine does not have to worry about setting
6643 the reloc address or the reloc symbol index.
6644
6645 The back end routine is given a pointer to the swapped in
6646 internal symbols, and can access the hash table entries
6647 for the external symbols via elf_sym_hashes (input_bfd).
6648
6649 When generating relocateable output, the back end routine
6650 must handle STB_LOCAL/STT_SECTION symbols specially. The
6651 output symbol is going to be a section symbol
6652 corresponding to the output section, which will require
6653 the addend to be adjusted. */
6654
6655 if (! (*relocate_section) (output_bfd, finfo->info,
6656 input_bfd, o, contents,
6657 internal_relocs,
6658 finfo->internal_syms,
6659 finfo->sections))
6660 return false;
6661
6662 if (emit_relocs)
6663 {
6664 Elf_Internal_Rela *irela;
6665 Elf_Internal_Rela *irelaend;
6666 struct elf_link_hash_entry **rel_hash;
6667 Elf_Internal_Shdr *input_rel_hdr;
6668 unsigned int next_erel;
6669 void (*reloc_emitter) PARAMS ((bfd *, asection *,
6670 Elf_Internal_Shdr *,
6671 Elf_Internal_Rela *));
6672
6673 /* Adjust the reloc addresses and symbol indices. */
6674
6675 irela = internal_relocs;
6676 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6677 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6678 + elf_section_data (o->output_section)->rel_count
6679 + elf_section_data (o->output_section)->rel_count2);
6680 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6681 {
6682 unsigned long r_symndx;
6683 asection *sec;
6684
6685 if (next_erel == bed->s->int_rels_per_ext_rel)
6686 {
6687 rel_hash++;
6688 next_erel = 0;
6689 }
6690
6691 irela->r_offset += o->output_offset;
6692
6693 /* Relocs in an executable have to be virtual addresses. */
6694 if (finfo->info->emitrelocations)
6695 irela->r_offset += o->output_section->vma;
6696
6697 r_symndx = ELF_R_SYM (irela->r_info);
6698
6699 if (r_symndx == 0)
6700 continue;
6701
6702 if (r_symndx >= locsymcount
6703 || (elf_bad_symtab (input_bfd)
6704 && finfo->sections[r_symndx] == NULL))
6705 {
6706 struct elf_link_hash_entry *rh;
6707 unsigned long indx;
6708
6709 /* This is a reloc against a global symbol. We
6710 have not yet output all the local symbols, so
6711 we do not know the symbol index of any global
6712 symbol. We set the rel_hash entry for this
6713 reloc to point to the global hash table entry
6714 for this symbol. The symbol index is then
6715 set at the end of elf_bfd_final_link. */
6716 indx = r_symndx - extsymoff;
6717 rh = elf_sym_hashes (input_bfd)[indx];
6718 while (rh->root.type == bfd_link_hash_indirect
6719 || rh->root.type == bfd_link_hash_warning)
6720 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6721
6722 /* Setting the index to -2 tells
6723 elf_link_output_extsym that this symbol is
6724 used by a reloc. */
6725 BFD_ASSERT (rh->indx < 0);
6726 rh->indx = -2;
6727
6728 *rel_hash = rh;
6729
6730 continue;
6731 }
6732
6733 /* This is a reloc against a local symbol. */
6734
6735 *rel_hash = NULL;
6736 isym = finfo->internal_syms + r_symndx;
6737 sec = finfo->sections[r_symndx];
6738 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6739 {
6740 /* I suppose the backend ought to fill in the
6741 section of any STT_SECTION symbol against a
6742 processor specific section. If we have
6743 discarded a section, the output_section will
6744 be the absolute section. */
6745 if (sec != NULL
6746 && (bfd_is_abs_section (sec)
6747 || (sec->output_section != NULL
6748 && bfd_is_abs_section (sec->output_section))))
6749 r_symndx = 0;
6750 else if (sec == NULL || sec->owner == NULL)
6751 {
6752 bfd_set_error (bfd_error_bad_value);
6753 return false;
6754 }
6755 else
6756 {
6757 r_symndx = sec->output_section->target_index;
6758 BFD_ASSERT (r_symndx != 0);
6759 }
6760 }
6761 else
6762 {
6763 if (finfo->indices[r_symndx] == -1)
6764 {
6765 unsigned long shlink;
6766 const char *name;
6767 asection *osec;
6768
6769 if (finfo->info->strip == strip_all)
6770 {
6771 /* You can't do ld -r -s. */
6772 bfd_set_error (bfd_error_invalid_operation);
6773 return false;
6774 }
6775
6776 /* This symbol was skipped earlier, but
6777 since it is needed by a reloc, we
6778 must output it now. */
6779 shlink = symtab_hdr->sh_link;
6780 name = (bfd_elf_string_from_elf_section
6781 (input_bfd, shlink, isym->st_name));
6782 if (name == NULL)
6783 return false;
6784
6785 osec = sec->output_section;
6786 isym->st_shndx =
6787 _bfd_elf_section_from_bfd_section (output_bfd,
6788 osec);
6789 if (isym->st_shndx == SHN_BAD)
6790 return false;
6791
6792 isym->st_value += sec->output_offset;
6793 if (! finfo->info->relocateable)
6794 isym->st_value += osec->vma;
6795
6796 finfo->indices[r_symndx]
6797 = bfd_get_symcount (output_bfd);
6798
6799 if (! elf_link_output_sym (finfo, name, isym, sec))
6800 return false;
6801 }
6802
6803 r_symndx = finfo->indices[r_symndx];
6804 }
6805
6806 irela->r_info = ELF_R_INFO (r_symndx,
6807 ELF_R_TYPE (irela->r_info));
6808 }
6809
6810 /* Swap out the relocs. */
6811 if (bed->elf_backend_emit_relocs
6812 && !(finfo->info->relocateable
6813 || finfo->info->emitrelocations))
6814 reloc_emitter = bed->elf_backend_emit_relocs;
6815 else
6816 reloc_emitter = elf_link_output_relocs;
6817
6818 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6819 (*reloc_emitter) (output_bfd, o, input_rel_hdr, internal_relocs);
6820
6821 input_rel_hdr = elf_section_data (o)->rel_hdr2;
6822 if (input_rel_hdr)
6823 {
6824 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
6825 * bed->s->int_rels_per_ext_rel);
6826 reloc_emitter (output_bfd, o, input_rel_hdr, internal_relocs);
6827 }
6828
6829 }
6830 }
6831
6832 /* Write out the modified section contents. */
6833 if (bed->elf_backend_write_section
6834 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
6835 {
6836 /* Section written out. */
6837 }
6838 else switch (elf_section_data (o)->sec_info_type)
6839 {
6840 case ELF_INFO_TYPE_STABS:
6841 if (! (_bfd_write_section_stabs
6842 (output_bfd,
6843 &elf_hash_table (finfo->info)->stab_info,
6844 o, &elf_section_data (o)->sec_info, contents)))
6845 return false;
6846 break;
6847 case ELF_INFO_TYPE_MERGE:
6848 if (! (_bfd_write_merged_section
6849 (output_bfd, o, elf_section_data (o)->sec_info)))
6850 return false;
6851 break;
6852 case ELF_INFO_TYPE_EH_FRAME:
6853 {
6854 asection *ehdrsec;
6855
6856 ehdrsec
6857 = bfd_get_section_by_name (elf_hash_table (finfo->info)->dynobj,
6858 ".eh_frame_hdr");
6859 if (! (_bfd_elf_write_section_eh_frame (output_bfd, o, ehdrsec,
6860 contents)))
6861 return false;
6862 }
6863 break;
6864 default:
6865 {
6866 bfd_size_type sec_size;
6867
6868 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
6869 if (! (o->flags & SEC_EXCLUDE)
6870 && ! bfd_set_section_contents (output_bfd, o->output_section,
6871 contents,
6872 (file_ptr) o->output_offset,
6873 sec_size))
6874 return false;
6875 }
6876 break;
6877 }
6878 }
6879
6880 return true;
6881 }
6882
6883 /* Generate a reloc when linking an ELF file. This is a reloc
6884 requested by the linker, and does come from any input file. This
6885 is used to build constructor and destructor tables when linking
6886 with -Ur. */
6887
6888 static boolean
6889 elf_reloc_link_order (output_bfd, info, output_section, link_order)
6890 bfd *output_bfd;
6891 struct bfd_link_info *info;
6892 asection *output_section;
6893 struct bfd_link_order *link_order;
6894 {
6895 reloc_howto_type *howto;
6896 long indx;
6897 bfd_vma offset;
6898 bfd_vma addend;
6899 struct elf_link_hash_entry **rel_hash_ptr;
6900 Elf_Internal_Shdr *rel_hdr;
6901 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
6902
6903 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6904 if (howto == NULL)
6905 {
6906 bfd_set_error (bfd_error_bad_value);
6907 return false;
6908 }
6909
6910 addend = link_order->u.reloc.p->addend;
6911
6912 /* Figure out the symbol index. */
6913 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
6914 + elf_section_data (output_section)->rel_count
6915 + elf_section_data (output_section)->rel_count2);
6916 if (link_order->type == bfd_section_reloc_link_order)
6917 {
6918 indx = link_order->u.reloc.p->u.section->target_index;
6919 BFD_ASSERT (indx != 0);
6920 *rel_hash_ptr = NULL;
6921 }
6922 else
6923 {
6924 struct elf_link_hash_entry *h;
6925
6926 /* Treat a reloc against a defined symbol as though it were
6927 actually against the section. */
6928 h = ((struct elf_link_hash_entry *)
6929 bfd_wrapped_link_hash_lookup (output_bfd, info,
6930 link_order->u.reloc.p->u.name,
6931 false, false, true));
6932 if (h != NULL
6933 && (h->root.type == bfd_link_hash_defined
6934 || h->root.type == bfd_link_hash_defweak))
6935 {
6936 asection *section;
6937
6938 section = h->root.u.def.section;
6939 indx = section->output_section->target_index;
6940 *rel_hash_ptr = NULL;
6941 /* It seems that we ought to add the symbol value to the
6942 addend here, but in practice it has already been added
6943 because it was passed to constructor_callback. */
6944 addend += section->output_section->vma + section->output_offset;
6945 }
6946 else if (h != NULL)
6947 {
6948 /* Setting the index to -2 tells elf_link_output_extsym that
6949 this symbol is used by a reloc. */
6950 h->indx = -2;
6951 *rel_hash_ptr = h;
6952 indx = 0;
6953 }
6954 else
6955 {
6956 if (! ((*info->callbacks->unattached_reloc)
6957 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
6958 (asection *) NULL, (bfd_vma) 0)))
6959 return false;
6960 indx = 0;
6961 }
6962 }
6963
6964 /* If this is an inplace reloc, we must write the addend into the
6965 object file. */
6966 if (howto->partial_inplace && addend != 0)
6967 {
6968 bfd_size_type size;
6969 bfd_reloc_status_type rstat;
6970 bfd_byte *buf;
6971 boolean ok;
6972 const char *sym_name;
6973
6974 size = bfd_get_reloc_size (howto);
6975 buf = (bfd_byte *) bfd_zmalloc (size);
6976 if (buf == (bfd_byte *) NULL)
6977 return false;
6978 rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf);
6979 switch (rstat)
6980 {
6981 case bfd_reloc_ok:
6982 break;
6983
6984 default:
6985 case bfd_reloc_outofrange:
6986 abort ();
6987
6988 case bfd_reloc_overflow:
6989 if (link_order->type == bfd_section_reloc_link_order)
6990 sym_name = bfd_section_name (output_bfd,
6991 link_order->u.reloc.p->u.section);
6992 else
6993 sym_name = link_order->u.reloc.p->u.name;
6994 if (! ((*info->callbacks->reloc_overflow)
6995 (info, sym_name, howto->name, addend,
6996 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
6997 {
6998 free (buf);
6999 return false;
7000 }
7001 break;
7002 }
7003 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
7004 (file_ptr) link_order->offset, size);
7005 free (buf);
7006 if (! ok)
7007 return false;
7008 }
7009
7010 /* The address of a reloc is relative to the section in a
7011 relocateable file, and is a virtual address in an executable
7012 file. */
7013 offset = link_order->offset;
7014 if (! info->relocateable)
7015 offset += output_section->vma;
7016
7017 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7018
7019 if (rel_hdr->sh_type == SHT_REL)
7020 {
7021 bfd_size_type size;
7022 Elf_Internal_Rel *irel;
7023 Elf_External_Rel *erel;
7024 unsigned int i;
7025
7026 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
7027 irel = (Elf_Internal_Rel *) bfd_zmalloc (size);
7028 if (irel == NULL)
7029 return false;
7030
7031 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7032 irel[i].r_offset = offset;
7033 irel[0].r_info = ELF_R_INFO (indx, howto->type);
7034
7035 erel = ((Elf_External_Rel *) rel_hdr->contents
7036 + elf_section_data (output_section)->rel_count);
7037
7038 if (bed->s->swap_reloc_out)
7039 (*bed->s->swap_reloc_out) (output_bfd, irel, (bfd_byte *) erel);
7040 else
7041 elf_swap_reloc_out (output_bfd, irel, erel);
7042
7043 free (irel);
7044 }
7045 else
7046 {
7047 bfd_size_type size;
7048 Elf_Internal_Rela *irela;
7049 Elf_External_Rela *erela;
7050 unsigned int i;
7051
7052 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
7053 irela = (Elf_Internal_Rela *) bfd_zmalloc (size);
7054 if (irela == NULL)
7055 return false;
7056
7057 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7058 irela[i].r_offset = offset;
7059 irela[0].r_info = ELF_R_INFO (indx, howto->type);
7060 irela[0].r_addend = addend;
7061
7062 erela = ((Elf_External_Rela *) rel_hdr->contents
7063 + elf_section_data (output_section)->rel_count);
7064
7065 if (bed->s->swap_reloca_out)
7066 (*bed->s->swap_reloca_out) (output_bfd, irela, (bfd_byte *) erela);
7067 else
7068 elf_swap_reloca_out (output_bfd, irela, erela);
7069 }
7070
7071 ++elf_section_data (output_section)->rel_count;
7072
7073 return true;
7074 }
7075 \f
7076 /* Allocate a pointer to live in a linker created section. */
7077
7078 boolean
7079 elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
7080 bfd *abfd;
7081 struct bfd_link_info *info;
7082 elf_linker_section_t *lsect;
7083 struct elf_link_hash_entry *h;
7084 const Elf_Internal_Rela *rel;
7085 {
7086 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
7087 elf_linker_section_pointers_t *linker_section_ptr;
7088 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7089 bfd_size_type amt;
7090
7091 BFD_ASSERT (lsect != NULL);
7092
7093 /* Is this a global symbol? */
7094 if (h != NULL)
7095 {
7096 /* Has this symbol already been allocated? If so, our work is done. */
7097 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
7098 rel->r_addend,
7099 lsect->which))
7100 return true;
7101
7102 ptr_linker_section_ptr = &h->linker_section_pointer;
7103 /* Make sure this symbol is output as a dynamic symbol. */
7104 if (h->dynindx == -1)
7105 {
7106 if (! elf_link_record_dynamic_symbol (info, h))
7107 return false;
7108 }
7109
7110 if (lsect->rel_section)
7111 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7112 }
7113 else
7114 {
7115 /* Allocation of a pointer to a local symbol. */
7116 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
7117
7118 /* Allocate a table to hold the local symbols if first time. */
7119 if (!ptr)
7120 {
7121 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
7122 register unsigned int i;
7123
7124 amt = num_symbols;
7125 amt *= sizeof (elf_linker_section_pointers_t *);
7126 ptr = (elf_linker_section_pointers_t **) bfd_alloc (abfd, amt);
7127
7128 if (!ptr)
7129 return false;
7130
7131 elf_local_ptr_offsets (abfd) = ptr;
7132 for (i = 0; i < num_symbols; i++)
7133 ptr[i] = (elf_linker_section_pointers_t *) 0;
7134 }
7135
7136 /* Has this symbol already been allocated? If so, our work is done. */
7137 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
7138 rel->r_addend,
7139 lsect->which))
7140 return true;
7141
7142 ptr_linker_section_ptr = &ptr[r_symndx];
7143
7144 if (info->shared)
7145 {
7146 /* If we are generating a shared object, we need to
7147 output a R_<xxx>_RELATIVE reloc so that the
7148 dynamic linker can adjust this GOT entry. */
7149 BFD_ASSERT (lsect->rel_section != NULL);
7150 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7151 }
7152 }
7153
7154 /* Allocate space for a pointer in the linker section, and allocate
7155 a new pointer record from internal memory. */
7156 BFD_ASSERT (ptr_linker_section_ptr != NULL);
7157 amt = sizeof (elf_linker_section_pointers_t);
7158 linker_section_ptr = (elf_linker_section_pointers_t *) bfd_alloc (abfd, amt);
7159
7160 if (!linker_section_ptr)
7161 return false;
7162
7163 linker_section_ptr->next = *ptr_linker_section_ptr;
7164 linker_section_ptr->addend = rel->r_addend;
7165 linker_section_ptr->which = lsect->which;
7166 linker_section_ptr->written_address_p = false;
7167 *ptr_linker_section_ptr = linker_section_ptr;
7168
7169 #if 0
7170 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
7171 {
7172 linker_section_ptr->offset = (lsect->section->_raw_size
7173 - lsect->hole_size + (ARCH_SIZE / 8));
7174 lsect->hole_offset += ARCH_SIZE / 8;
7175 lsect->sym_offset += ARCH_SIZE / 8;
7176 if (lsect->sym_hash)
7177 {
7178 /* Bump up symbol value if needed. */
7179 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
7180 #ifdef DEBUG
7181 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
7182 lsect->sym_hash->root.root.string,
7183 (long) ARCH_SIZE / 8,
7184 (long) lsect->sym_hash->root.u.def.value);
7185 #endif
7186 }
7187 }
7188 else
7189 #endif
7190 linker_section_ptr->offset = lsect->section->_raw_size;
7191
7192 lsect->section->_raw_size += ARCH_SIZE / 8;
7193
7194 #ifdef DEBUG
7195 fprintf (stderr,
7196 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7197 lsect->name, (long) linker_section_ptr->offset,
7198 (long) lsect->section->_raw_size);
7199 #endif
7200
7201 return true;
7202 }
7203 \f
7204 #if ARCH_SIZE==64
7205 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7206 #endif
7207 #if ARCH_SIZE==32
7208 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7209 #endif
7210
7211 /* Fill in the address for a pointer generated in a linker section. */
7212
7213 bfd_vma
7214 elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h,
7215 relocation, rel, relative_reloc)
7216 bfd *output_bfd;
7217 bfd *input_bfd;
7218 struct bfd_link_info *info;
7219 elf_linker_section_t *lsect;
7220 struct elf_link_hash_entry *h;
7221 bfd_vma relocation;
7222 const Elf_Internal_Rela *rel;
7223 int relative_reloc;
7224 {
7225 elf_linker_section_pointers_t *linker_section_ptr;
7226
7227 BFD_ASSERT (lsect != NULL);
7228
7229 if (h != NULL)
7230 {
7231 /* Handle global symbol. */
7232 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7233 (h->linker_section_pointer,
7234 rel->r_addend,
7235 lsect->which));
7236
7237 BFD_ASSERT (linker_section_ptr != NULL);
7238
7239 if (! elf_hash_table (info)->dynamic_sections_created
7240 || (info->shared
7241 && info->symbolic
7242 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
7243 {
7244 /* This is actually a static link, or it is a
7245 -Bsymbolic link and the symbol is defined
7246 locally. We must initialize this entry in the
7247 global section.
7248
7249 When doing a dynamic link, we create a .rela.<xxx>
7250 relocation entry to initialize the value. This
7251 is done in the finish_dynamic_symbol routine. */
7252 if (!linker_section_ptr->written_address_p)
7253 {
7254 linker_section_ptr->written_address_p = true;
7255 bfd_put_ptr (output_bfd,
7256 relocation + linker_section_ptr->addend,
7257 (lsect->section->contents
7258 + linker_section_ptr->offset));
7259 }
7260 }
7261 }
7262 else
7263 {
7264 /* Handle local symbol. */
7265 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7266 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
7267 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
7268 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7269 (elf_local_ptr_offsets (input_bfd)[r_symndx],
7270 rel->r_addend,
7271 lsect->which));
7272
7273 BFD_ASSERT (linker_section_ptr != NULL);
7274
7275 /* Write out pointer if it hasn't been rewritten out before. */
7276 if (!linker_section_ptr->written_address_p)
7277 {
7278 linker_section_ptr->written_address_p = true;
7279 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
7280 lsect->section->contents + linker_section_ptr->offset);
7281
7282 if (info->shared)
7283 {
7284 asection *srel = lsect->rel_section;
7285 Elf_Internal_Rela *outrel;
7286 Elf_External_Rela *erel;
7287 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7288 unsigned int i;
7289 bfd_size_type amt;
7290
7291 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
7292 outrel = (Elf_Internal_Rela *) bfd_zmalloc (amt);
7293 if (outrel == NULL)
7294 {
7295 (*_bfd_error_handler) (_("Error: out of memory"));
7296 return 0;
7297 }
7298
7299 /* We need to generate a relative reloc for the dynamic
7300 linker. */
7301 if (!srel)
7302 {
7303 srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
7304 lsect->rel_name);
7305 lsect->rel_section = srel;
7306 }
7307
7308 BFD_ASSERT (srel != NULL);
7309
7310 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7311 outrel[i].r_offset = (lsect->section->output_section->vma
7312 + lsect->section->output_offset
7313 + linker_section_ptr->offset);
7314 outrel[0].r_info = ELF_R_INFO (0, relative_reloc);
7315 outrel[0].r_addend = 0;
7316 erel = (Elf_External_Rela *) lsect->section->contents;
7317 erel += elf_section_data (lsect->section)->rel_count;
7318 elf_swap_reloca_out (output_bfd, outrel, erel);
7319 ++elf_section_data (lsect->section)->rel_count;
7320
7321 free (outrel);
7322 }
7323 }
7324 }
7325
7326 relocation = (lsect->section->output_offset
7327 + linker_section_ptr->offset
7328 - lsect->hole_offset
7329 - lsect->sym_offset);
7330
7331 #ifdef DEBUG
7332 fprintf (stderr,
7333 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7334 lsect->name, (long) relocation, (long) relocation);
7335 #endif
7336
7337 /* Subtract out the addend, because it will get added back in by the normal
7338 processing. */
7339 return relocation - linker_section_ptr->addend;
7340 }
7341 \f
7342 /* Garbage collect unused sections. */
7343
7344 static boolean elf_gc_mark
7345 PARAMS ((struct bfd_link_info *info, asection *sec,
7346 asection * (*gc_mark_hook)
7347 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7348 struct elf_link_hash_entry *, Elf_Internal_Sym *))));
7349
7350 static boolean elf_gc_sweep
7351 PARAMS ((struct bfd_link_info *info,
7352 boolean (*gc_sweep_hook)
7353 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7354 const Elf_Internal_Rela *relocs))));
7355
7356 static boolean elf_gc_sweep_symbol
7357 PARAMS ((struct elf_link_hash_entry *h, PTR idxptr));
7358
7359 static boolean elf_gc_allocate_got_offsets
7360 PARAMS ((struct elf_link_hash_entry *h, PTR offarg));
7361
7362 static boolean elf_gc_propagate_vtable_entries_used
7363 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7364
7365 static boolean elf_gc_smash_unused_vtentry_relocs
7366 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7367
7368 /* The mark phase of garbage collection. For a given section, mark
7369 it and any sections in this section's group, and all the sections
7370 which define symbols to which it refers. */
7371
7372 static boolean
7373 elf_gc_mark (info, sec, gc_mark_hook)
7374 struct bfd_link_info *info;
7375 asection *sec;
7376 asection * (*gc_mark_hook)
7377 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7378 struct elf_link_hash_entry *, Elf_Internal_Sym *));
7379 {
7380 boolean ret;
7381 asection *group_sec;
7382
7383 sec->gc_mark = 1;
7384
7385 /* Mark all the sections in the group. */
7386 group_sec = elf_section_data (sec)->next_in_group;
7387 if (group_sec && !group_sec->gc_mark)
7388 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
7389 return false;
7390
7391 /* Look through the section relocs. */
7392 ret = true;
7393 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
7394 {
7395 Elf_Internal_Rela *relstart, *rel, *relend;
7396 Elf_Internal_Shdr *symtab_hdr;
7397 Elf_Internal_Shdr *shndx_hdr;
7398 struct elf_link_hash_entry **sym_hashes;
7399 size_t nlocsyms;
7400 size_t extsymoff;
7401 Elf_External_Sym *locsyms, *freesyms = NULL;
7402 Elf_External_Sym_Shndx *locsym_shndx;
7403 bfd *input_bfd = sec->owner;
7404 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
7405
7406 /* GCFIXME: how to arrange so that relocs and symbols are not
7407 reread continually? */
7408
7409 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7410 sym_hashes = elf_sym_hashes (input_bfd);
7411
7412 /* Read the local symbols. */
7413 if (elf_bad_symtab (input_bfd))
7414 {
7415 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7416 extsymoff = 0;
7417 }
7418 else
7419 extsymoff = nlocsyms = symtab_hdr->sh_info;
7420
7421 if (symtab_hdr->contents)
7422 locsyms = (Elf_External_Sym *) symtab_hdr->contents;
7423 else if (nlocsyms == 0)
7424 locsyms = NULL;
7425 else
7426 {
7427 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym);
7428 locsyms = freesyms = bfd_malloc (amt);
7429 if (freesyms == NULL
7430 || bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
7431 || bfd_bread (locsyms, amt, input_bfd) != amt)
7432 {
7433 ret = false;
7434 goto out1;
7435 }
7436 }
7437
7438 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
7439 locsym_shndx = NULL;
7440 if (shndx_hdr->sh_size != 0 && nlocsyms != 0)
7441 {
7442 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym_Shndx);
7443 locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
7444 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
7445 || bfd_bread (locsym_shndx, amt, input_bfd) != amt)
7446 return false;
7447 }
7448
7449 /* Read the relocations. */
7450 relstart = (NAME(_bfd_elf,link_read_relocs)
7451 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL,
7452 info->keep_memory));
7453 if (relstart == NULL)
7454 {
7455 ret = false;
7456 goto out1;
7457 }
7458 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7459
7460 for (rel = relstart; rel < relend; rel++)
7461 {
7462 unsigned long r_symndx;
7463 asection *rsec;
7464 struct elf_link_hash_entry *h;
7465 Elf_Internal_Sym s;
7466
7467 r_symndx = ELF_R_SYM (rel->r_info);
7468 if (r_symndx == 0)
7469 continue;
7470
7471 if (elf_bad_symtab (sec->owner))
7472 {
7473 elf_swap_symbol_in (input_bfd,
7474 locsyms + r_symndx,
7475 locsym_shndx + (locsym_shndx ? r_symndx : 0),
7476 &s);
7477 if (ELF_ST_BIND (s.st_info) == STB_LOCAL)
7478 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
7479 else
7480 {
7481 h = sym_hashes[r_symndx - extsymoff];
7482 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
7483 }
7484 }
7485 else if (r_symndx >= nlocsyms)
7486 {
7487 h = sym_hashes[r_symndx - extsymoff];
7488 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
7489 }
7490 else
7491 {
7492 elf_swap_symbol_in (input_bfd,
7493 locsyms + r_symndx,
7494 locsym_shndx + (locsym_shndx ? r_symndx : 0),
7495 &s);
7496 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
7497 }
7498
7499 if (rsec && !rsec->gc_mark)
7500 if (!elf_gc_mark (info, rsec, gc_mark_hook))
7501 {
7502 ret = false;
7503 goto out2;
7504 }
7505 }
7506
7507 out2:
7508 if (!info->keep_memory)
7509 free (relstart);
7510 out1:
7511 if (freesyms)
7512 free (freesyms);
7513 }
7514
7515 return ret;
7516 }
7517
7518 /* The sweep phase of garbage collection. Remove all garbage sections. */
7519
7520 static boolean
7521 elf_gc_sweep (info, gc_sweep_hook)
7522 struct bfd_link_info *info;
7523 boolean (*gc_sweep_hook)
7524 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7525 const Elf_Internal_Rela *relocs));
7526 {
7527 bfd *sub;
7528
7529 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7530 {
7531 asection *o;
7532
7533 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7534 continue;
7535
7536 for (o = sub->sections; o != NULL; o = o->next)
7537 {
7538 /* Keep special sections. Keep .debug sections. */
7539 if ((o->flags & SEC_LINKER_CREATED)
7540 || (o->flags & SEC_DEBUGGING))
7541 o->gc_mark = 1;
7542
7543 if (o->gc_mark)
7544 continue;
7545
7546 /* Skip sweeping sections already excluded. */
7547 if (o->flags & SEC_EXCLUDE)
7548 continue;
7549
7550 /* Since this is early in the link process, it is simple
7551 to remove a section from the output. */
7552 o->flags |= SEC_EXCLUDE;
7553
7554 /* But we also have to update some of the relocation
7555 info we collected before. */
7556 if (gc_sweep_hook
7557 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
7558 {
7559 Elf_Internal_Rela *internal_relocs;
7560 boolean r;
7561
7562 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
7563 (o->owner, o, NULL, NULL, info->keep_memory));
7564 if (internal_relocs == NULL)
7565 return false;
7566
7567 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
7568
7569 if (!info->keep_memory)
7570 free (internal_relocs);
7571
7572 if (!r)
7573 return false;
7574 }
7575 }
7576 }
7577
7578 /* Remove the symbols that were in the swept sections from the dynamic
7579 symbol table. GCFIXME: Anyone know how to get them out of the
7580 static symbol table as well? */
7581 {
7582 int i = 0;
7583
7584 elf_link_hash_traverse (elf_hash_table (info),
7585 elf_gc_sweep_symbol,
7586 (PTR) &i);
7587
7588 elf_hash_table (info)->dynsymcount = i;
7589 }
7590
7591 return true;
7592 }
7593
7594 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7595
7596 static boolean
7597 elf_gc_sweep_symbol (h, idxptr)
7598 struct elf_link_hash_entry *h;
7599 PTR idxptr;
7600 {
7601 int *idx = (int *) idxptr;
7602
7603 if (h->dynindx != -1
7604 && ((h->root.type != bfd_link_hash_defined
7605 && h->root.type != bfd_link_hash_defweak)
7606 || h->root.u.def.section->gc_mark))
7607 h->dynindx = (*idx)++;
7608
7609 return true;
7610 }
7611
7612 /* Propogate collected vtable information. This is called through
7613 elf_link_hash_traverse. */
7614
7615 static boolean
7616 elf_gc_propagate_vtable_entries_used (h, okp)
7617 struct elf_link_hash_entry *h;
7618 PTR okp;
7619 {
7620 /* Those that are not vtables. */
7621 if (h->vtable_parent == NULL)
7622 return true;
7623
7624 /* Those vtables that do not have parents, we cannot merge. */
7625 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
7626 return true;
7627
7628 /* If we've already been done, exit. */
7629 if (h->vtable_entries_used && h->vtable_entries_used[-1])
7630 return true;
7631
7632 /* Make sure the parent's table is up to date. */
7633 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
7634
7635 if (h->vtable_entries_used == NULL)
7636 {
7637 /* None of this table's entries were referenced. Re-use the
7638 parent's table. */
7639 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
7640 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
7641 }
7642 else
7643 {
7644 size_t n;
7645 boolean *cu, *pu;
7646
7647 /* Or the parent's entries into ours. */
7648 cu = h->vtable_entries_used;
7649 cu[-1] = true;
7650 pu = h->vtable_parent->vtable_entries_used;
7651 if (pu != NULL)
7652 {
7653 asection *sec = h->root.u.def.section;
7654 struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
7655 int file_align = bed->s->file_align;
7656
7657 n = h->vtable_parent->vtable_entries_size / file_align;
7658 while (n--)
7659 {
7660 if (*pu)
7661 *cu = true;
7662 pu++;
7663 cu++;
7664 }
7665 }
7666 }
7667
7668 return true;
7669 }
7670
7671 static boolean
7672 elf_gc_smash_unused_vtentry_relocs (h, okp)
7673 struct elf_link_hash_entry *h;
7674 PTR okp;
7675 {
7676 asection *sec;
7677 bfd_vma hstart, hend;
7678 Elf_Internal_Rela *relstart, *relend, *rel;
7679 struct elf_backend_data *bed;
7680 int file_align;
7681
7682 /* Take care of both those symbols that do not describe vtables as
7683 well as those that are not loaded. */
7684 if (h->vtable_parent == NULL)
7685 return true;
7686
7687 BFD_ASSERT (h->root.type == bfd_link_hash_defined
7688 || h->root.type == bfd_link_hash_defweak);
7689
7690 sec = h->root.u.def.section;
7691 hstart = h->root.u.def.value;
7692 hend = hstart + h->size;
7693
7694 relstart = (NAME(_bfd_elf,link_read_relocs)
7695 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, true));
7696 if (!relstart)
7697 return *(boolean *) okp = false;
7698 bed = get_elf_backend_data (sec->owner);
7699 file_align = bed->s->file_align;
7700
7701 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7702
7703 for (rel = relstart; rel < relend; ++rel)
7704 if (rel->r_offset >= hstart && rel->r_offset < hend)
7705 {
7706 /* If the entry is in use, do nothing. */
7707 if (h->vtable_entries_used
7708 && (rel->r_offset - hstart) < h->vtable_entries_size)
7709 {
7710 bfd_vma entry = (rel->r_offset - hstart) / file_align;
7711 if (h->vtable_entries_used[entry])
7712 continue;
7713 }
7714 /* Otherwise, kill it. */
7715 rel->r_offset = rel->r_info = rel->r_addend = 0;
7716 }
7717
7718 return true;
7719 }
7720
7721 /* Do mark and sweep of unused sections. */
7722
7723 boolean
7724 elf_gc_sections (abfd, info)
7725 bfd *abfd;
7726 struct bfd_link_info *info;
7727 {
7728 boolean ok = true;
7729 bfd *sub;
7730 asection * (*gc_mark_hook)
7731 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7732 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
7733
7734 if (!get_elf_backend_data (abfd)->can_gc_sections
7735 || info->relocateable || info->emitrelocations
7736 || elf_hash_table (info)->dynamic_sections_created)
7737 return true;
7738
7739 /* Apply transitive closure to the vtable entry usage info. */
7740 elf_link_hash_traverse (elf_hash_table (info),
7741 elf_gc_propagate_vtable_entries_used,
7742 (PTR) &ok);
7743 if (!ok)
7744 return false;
7745
7746 /* Kill the vtable relocations that were not used. */
7747 elf_link_hash_traverse (elf_hash_table (info),
7748 elf_gc_smash_unused_vtentry_relocs,
7749 (PTR) &ok);
7750 if (!ok)
7751 return false;
7752
7753 /* Grovel through relocs to find out who stays ... */
7754
7755 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
7756 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7757 {
7758 asection *o;
7759
7760 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7761 continue;
7762
7763 for (o = sub->sections; o != NULL; o = o->next)
7764 {
7765 if (o->flags & SEC_KEEP)
7766 if (!elf_gc_mark (info, o, gc_mark_hook))
7767 return false;
7768 }
7769 }
7770
7771 /* ... and mark SEC_EXCLUDE for those that go. */
7772 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
7773 return false;
7774
7775 return true;
7776 }
7777 \f
7778 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
7779
7780 boolean
7781 elf_gc_record_vtinherit (abfd, sec, h, offset)
7782 bfd *abfd;
7783 asection *sec;
7784 struct elf_link_hash_entry *h;
7785 bfd_vma offset;
7786 {
7787 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7788 struct elf_link_hash_entry **search, *child;
7789 bfd_size_type extsymcount;
7790
7791 /* The sh_info field of the symtab header tells us where the
7792 external symbols start. We don't care about the local symbols at
7793 this point. */
7794 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
7795 if (!elf_bad_symtab (abfd))
7796 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
7797
7798 sym_hashes = elf_sym_hashes (abfd);
7799 sym_hashes_end = sym_hashes + extsymcount;
7800
7801 /* Hunt down the child symbol, which is in this section at the same
7802 offset as the relocation. */
7803 for (search = sym_hashes; search != sym_hashes_end; ++search)
7804 {
7805 if ((child = *search) != NULL
7806 && (child->root.type == bfd_link_hash_defined
7807 || child->root.type == bfd_link_hash_defweak)
7808 && child->root.u.def.section == sec
7809 && child->root.u.def.value == offset)
7810 goto win;
7811 }
7812
7813 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
7814 bfd_archive_filename (abfd), sec->name,
7815 (unsigned long) offset);
7816 bfd_set_error (bfd_error_invalid_operation);
7817 return false;
7818
7819 win:
7820 if (!h)
7821 {
7822 /* This *should* only be the absolute section. It could potentially
7823 be that someone has defined a non-global vtable though, which
7824 would be bad. It isn't worth paging in the local symbols to be
7825 sure though; that case should simply be handled by the assembler. */
7826
7827 child->vtable_parent = (struct elf_link_hash_entry *) -1;
7828 }
7829 else
7830 child->vtable_parent = h;
7831
7832 return true;
7833 }
7834
7835 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
7836
7837 boolean
7838 elf_gc_record_vtentry (abfd, sec, h, addend)
7839 bfd *abfd ATTRIBUTE_UNUSED;
7840 asection *sec ATTRIBUTE_UNUSED;
7841 struct elf_link_hash_entry *h;
7842 bfd_vma addend;
7843 {
7844 struct elf_backend_data *bed = get_elf_backend_data (abfd);
7845 int file_align = bed->s->file_align;
7846
7847 if (addend >= h->vtable_entries_size)
7848 {
7849 size_t size, bytes;
7850 boolean *ptr = h->vtable_entries_used;
7851
7852 /* While the symbol is undefined, we have to be prepared to handle
7853 a zero size. */
7854 if (h->root.type == bfd_link_hash_undefined)
7855 size = addend;
7856 else
7857 {
7858 size = h->size;
7859 if (size < addend)
7860 {
7861 /* Oops! We've got a reference past the defined end of
7862 the table. This is probably a bug -- shall we warn? */
7863 size = addend;
7864 }
7865 }
7866
7867 /* Allocate one extra entry for use as a "done" flag for the
7868 consolidation pass. */
7869 bytes = (size / file_align + 1) * sizeof (boolean);
7870
7871 if (ptr)
7872 {
7873 ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes);
7874
7875 if (ptr != NULL)
7876 {
7877 size_t oldbytes;
7878
7879 oldbytes = ((h->vtable_entries_size / file_align + 1)
7880 * sizeof (boolean));
7881 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
7882 }
7883 }
7884 else
7885 ptr = bfd_zmalloc ((bfd_size_type) bytes);
7886
7887 if (ptr == NULL)
7888 return false;
7889
7890 /* And arrange for that done flag to be at index -1. */
7891 h->vtable_entries_used = ptr + 1;
7892 h->vtable_entries_size = size;
7893 }
7894
7895 h->vtable_entries_used[addend / file_align] = true;
7896
7897 return true;
7898 }
7899
7900 /* And an accompanying bit to work out final got entry offsets once
7901 we're done. Should be called from final_link. */
7902
7903 boolean
7904 elf_gc_common_finalize_got_offsets (abfd, info)
7905 bfd *abfd;
7906 struct bfd_link_info *info;
7907 {
7908 bfd *i;
7909 struct elf_backend_data *bed = get_elf_backend_data (abfd);
7910 bfd_vma gotoff;
7911
7912 /* The GOT offset is relative to the .got section, but the GOT header is
7913 put into the .got.plt section, if the backend uses it. */
7914 if (bed->want_got_plt)
7915 gotoff = 0;
7916 else
7917 gotoff = bed->got_header_size;
7918
7919 /* Do the local .got entries first. */
7920 for (i = info->input_bfds; i; i = i->link_next)
7921 {
7922 bfd_signed_vma *local_got;
7923 bfd_size_type j, locsymcount;
7924 Elf_Internal_Shdr *symtab_hdr;
7925
7926 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
7927 continue;
7928
7929 local_got = elf_local_got_refcounts (i);
7930 if (!local_got)
7931 continue;
7932
7933 symtab_hdr = &elf_tdata (i)->symtab_hdr;
7934 if (elf_bad_symtab (i))
7935 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7936 else
7937 locsymcount = symtab_hdr->sh_info;
7938
7939 for (j = 0; j < locsymcount; ++j)
7940 {
7941 if (local_got[j] > 0)
7942 {
7943 local_got[j] = gotoff;
7944 gotoff += ARCH_SIZE / 8;
7945 }
7946 else
7947 local_got[j] = (bfd_vma) -1;
7948 }
7949 }
7950
7951 /* Then the global .got entries. .plt refcounts are handled by
7952 adjust_dynamic_symbol */
7953 elf_link_hash_traverse (elf_hash_table (info),
7954 elf_gc_allocate_got_offsets,
7955 (PTR) &gotoff);
7956 return true;
7957 }
7958
7959 /* We need a special top-level link routine to convert got reference counts
7960 to real got offsets. */
7961
7962 static boolean
7963 elf_gc_allocate_got_offsets (h, offarg)
7964 struct elf_link_hash_entry *h;
7965 PTR offarg;
7966 {
7967 bfd_vma *off = (bfd_vma *) offarg;
7968
7969 if (h->got.refcount > 0)
7970 {
7971 h->got.offset = off[0];
7972 off[0] += ARCH_SIZE / 8;
7973 }
7974 else
7975 h->got.offset = (bfd_vma) -1;
7976
7977 return true;
7978 }
7979
7980 /* Many folk need no more in the way of final link than this, once
7981 got entry reference counting is enabled. */
7982
7983 boolean
7984 elf_gc_common_final_link (abfd, info)
7985 bfd *abfd;
7986 struct bfd_link_info *info;
7987 {
7988 if (!elf_gc_common_finalize_got_offsets (abfd, info))
7989 return false;
7990
7991 /* Invoke the regular ELF backend linker to do all the work. */
7992 return elf_bfd_final_link (abfd, info);
7993 }
7994
7995 /* This function will be called though elf_link_hash_traverse to store
7996 all hash value of the exported symbols in an array. */
7997
7998 static boolean
7999 elf_collect_hash_codes (h, data)
8000 struct elf_link_hash_entry *h;
8001 PTR data;
8002 {
8003 unsigned long **valuep = (unsigned long **) data;
8004 const char *name;
8005 char *p;
8006 unsigned long ha;
8007 char *alc = NULL;
8008
8009 /* Ignore indirect symbols. These are added by the versioning code. */
8010 if (h->dynindx == -1)
8011 return true;
8012
8013 name = h->root.root.string;
8014 p = strchr (name, ELF_VER_CHR);
8015 if (p != NULL)
8016 {
8017 alc = bfd_malloc ((bfd_size_type) (p - name + 1));
8018 memcpy (alc, name, (size_t) (p - name));
8019 alc[p - name] = '\0';
8020 name = alc;
8021 }
8022
8023 /* Compute the hash value. */
8024 ha = bfd_elf_hash (name);
8025
8026 /* Store the found hash value in the array given as the argument. */
8027 *(*valuep)++ = ha;
8028
8029 /* And store it in the struct so that we can put it in the hash table
8030 later. */
8031 h->elf_hash_value = ha;
8032
8033 if (alc != NULL)
8034 free (alc);
8035
8036 return true;
8037 }
8038
8039 boolean
8040 elf_reloc_symbol_deleted_p (offset, cookie)
8041 bfd_vma offset;
8042 PTR cookie;
8043 {
8044 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
8045
8046 if (rcookie->bad_symtab)
8047 rcookie->rel = rcookie->rels;
8048
8049 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
8050 {
8051 unsigned long r_symndx = ELF_R_SYM (rcookie->rel->r_info);
8052 Elf_Internal_Sym isym;
8053
8054 if (! rcookie->bad_symtab)
8055 if (rcookie->rel->r_offset > offset)
8056 return false;
8057 if (rcookie->rel->r_offset != offset)
8058 continue;
8059
8060 if (rcookie->locsyms && r_symndx < rcookie->locsymcount)
8061 {
8062 Elf_External_Sym *lsym;
8063 Elf_External_Sym_Shndx *lshndx;
8064
8065 lsym = (Elf_External_Sym *) rcookie->locsyms + r_symndx;
8066 lshndx = (Elf_External_Sym_Shndx *) rcookie->locsym_shndx;
8067 if (lshndx != NULL)
8068 lshndx += r_symndx;
8069 elf_swap_symbol_in (rcookie->abfd, lsym, lshndx, &isym);
8070 }
8071
8072 if (r_symndx >= rcookie->locsymcount
8073 || (rcookie->locsyms
8074 && ELF_ST_BIND (isym.st_info) != STB_LOCAL))
8075 {
8076 struct elf_link_hash_entry *h;
8077
8078 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
8079
8080 while (h->root.type == bfd_link_hash_indirect
8081 || h->root.type == bfd_link_hash_warning)
8082 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8083
8084 if ((h->root.type == bfd_link_hash_defined
8085 || h->root.type == bfd_link_hash_defweak)
8086 && elf_discarded_section (h->root.u.def.section))
8087 return true;
8088 else
8089 return false;
8090 }
8091 else if (rcookie->locsyms)
8092 {
8093 /* It's not a relocation against a global symbol,
8094 but it could be a relocation against a local
8095 symbol for a discarded section. */
8096 asection *isec;
8097
8098 /* Need to: get the symbol; get the section. */
8099 if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE)
8100 {
8101 isec = section_from_elf_index (rcookie->abfd, isym.st_shndx);
8102 if (isec != NULL && elf_discarded_section (isec))
8103 return true;
8104 }
8105 }
8106 return false;
8107 }
8108 return false;
8109 }
8110
8111 /* Discard unneeded references to discarded sections.
8112 Returns true if any section's size was changed. */
8113 /* This function assumes that the relocations are in sorted order,
8114 which is true for all known assemblers. */
8115
8116 boolean
8117 elf_bfd_discard_info (output_bfd, info)
8118 bfd *output_bfd;
8119 struct bfd_link_info *info;
8120 {
8121 struct elf_reloc_cookie cookie;
8122 asection *stab, *eh, *ehdr;
8123 Elf_Internal_Shdr *symtab_hdr;
8124 Elf_Internal_Shdr *shndx_hdr;
8125 Elf_External_Sym *freesyms;
8126 struct elf_backend_data *bed;
8127 bfd *abfd;
8128 boolean ret = false;
8129 boolean strip = info->strip == strip_all || info->strip == strip_debugger;
8130
8131 if (info->relocateable
8132 || info->traditional_format
8133 || info->hash->creator->flavour != bfd_target_elf_flavour
8134 || ! is_elf_hash_table (info))
8135 return false;
8136
8137 ehdr = NULL;
8138 if (elf_hash_table (info)->dynobj != NULL)
8139 ehdr = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
8140 ".eh_frame_hdr");
8141
8142 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
8143 {
8144 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
8145 continue;
8146
8147 bed = get_elf_backend_data (abfd);
8148
8149 if ((abfd->flags & DYNAMIC) != 0)
8150 continue;
8151
8152 eh = NULL;
8153 if (ehdr)
8154 {
8155 eh = bfd_get_section_by_name (abfd, ".eh_frame");
8156 if (eh && eh->_raw_size == 0)
8157 eh = NULL;
8158 }
8159
8160 stab = strip ? NULL : bfd_get_section_by_name (abfd, ".stab");
8161 if ((! stab || elf_section_data(stab)->sec_info_type != ELF_INFO_TYPE_STABS)
8162 && ! eh
8163 && (strip || ! bed->elf_backend_discard_info))
8164 continue;
8165
8166 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8167 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8168
8169 cookie.abfd = abfd;
8170 cookie.sym_hashes = elf_sym_hashes (abfd);
8171 cookie.bad_symtab = elf_bad_symtab (abfd);
8172 if (cookie.bad_symtab)
8173 {
8174 cookie.locsymcount =
8175 symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8176 cookie.extsymoff = 0;
8177 }
8178 else
8179 {
8180 cookie.locsymcount = symtab_hdr->sh_info;
8181 cookie.extsymoff = symtab_hdr->sh_info;
8182 }
8183
8184 freesyms = NULL;
8185 if (symtab_hdr->contents)
8186 cookie.locsyms = (void *) symtab_hdr->contents;
8187 else if (cookie.locsymcount == 0)
8188 cookie.locsyms = NULL;
8189 else
8190 {
8191 bfd_size_type amt = cookie.locsymcount * sizeof (Elf_External_Sym);
8192 cookie.locsyms = bfd_malloc (amt);
8193 if (cookie.locsyms == NULL)
8194 return false;
8195 freesyms = cookie.locsyms;
8196 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
8197 || bfd_bread (cookie.locsyms, amt, abfd) != amt)
8198 {
8199 error_ret_free_loc:
8200 free (cookie.locsyms);
8201 return false;
8202 }
8203 }
8204
8205 cookie.locsym_shndx = NULL;
8206 if (shndx_hdr->sh_size != 0 && cookie.locsymcount != 0)
8207 {
8208 bfd_size_type amt;
8209 amt = cookie.locsymcount * sizeof (Elf_External_Sym_Shndx);
8210 cookie.locsym_shndx = bfd_malloc (amt);
8211 if (cookie.locsym_shndx == NULL)
8212 goto error_ret_free_loc;
8213 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
8214 || bfd_bread (cookie.locsym_shndx, amt, abfd) != amt)
8215 {
8216 free (cookie.locsym_shndx);
8217 goto error_ret_free_loc;
8218 }
8219 }
8220
8221 if (stab)
8222 {
8223 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8224 (abfd, stab, (PTR) NULL,
8225 (Elf_Internal_Rela *) NULL,
8226 info->keep_memory));
8227 if (cookie.rels)
8228 {
8229 cookie.rel = cookie.rels;
8230 cookie.relend =
8231 cookie.rels + stab->reloc_count * bed->s->int_rels_per_ext_rel;
8232 if (_bfd_discard_section_stabs (abfd, stab,
8233 elf_section_data (stab)->sec_info,
8234 elf_reloc_symbol_deleted_p,
8235 &cookie))
8236 ret = true;
8237 if (! info->keep_memory)
8238 free (cookie.rels);
8239 }
8240 }
8241
8242 if (eh)
8243 {
8244 cookie.rels = NULL;
8245 cookie.rel = NULL;
8246 cookie.relend = NULL;
8247 if (eh->reloc_count)
8248 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8249 (abfd, eh, (PTR) NULL,
8250 (Elf_Internal_Rela *) NULL,
8251 info->keep_memory));
8252 if (cookie.rels)
8253 {
8254 cookie.rel = cookie.rels;
8255 cookie.relend =
8256 cookie.rels + eh->reloc_count * bed->s->int_rels_per_ext_rel;
8257 }
8258 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, ehdr,
8259 elf_reloc_symbol_deleted_p,
8260 &cookie))
8261 ret = true;
8262 if (! info->keep_memory)
8263 free (cookie.rels);
8264 }
8265
8266 if (bed->elf_backend_discard_info)
8267 {
8268 if (bed->elf_backend_discard_info (abfd, &cookie, info))
8269 ret = true;
8270 }
8271
8272 if (cookie.locsym_shndx != NULL)
8273 free (cookie.locsym_shndx);
8274
8275 if (freesyms != NULL)
8276 free (freesyms);
8277 }
8278
8279 if (ehdr
8280 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd,
8281 info, ehdr))
8282 ret = true;
8283 return ret;
8284 }
8285
8286 static boolean
8287 elf_section_ignore_discarded_relocs (sec)
8288 asection *sec;
8289 {
8290 switch (elf_section_data (sec)->sec_info_type)
8291 {
8292 case ELF_INFO_TYPE_STABS:
8293 case ELF_INFO_TYPE_EH_FRAME:
8294 return true;
8295 default:
8296 break;
8297 }
8298 if ((get_elf_backend_data (sec->owner)->elf_backend_ignore_discarded_relocs
8299 != NULL)
8300 && (*get_elf_backend_data (sec->owner)
8301 ->elf_backend_ignore_discarded_relocs) (sec))
8302 return true;
8303
8304 return false;
8305 }
This page took 0.228411 seconds and 5 git commands to generate.