(elf_link_add_object_symbols): Also read verneed
[binutils-gdb.git] / bfd / elflink.h
1 /* ELF linker support.
2 Copyright 1995, 1996, 1997 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 /* ELF linker code. */
21
22 static boolean elf_link_add_object_symbols
23 PARAMS ((bfd *, struct bfd_link_info *));
24 static boolean elf_link_add_archive_symbols
25 PARAMS ((bfd *, struct bfd_link_info *));
26 static boolean elf_export_symbol
27 PARAMS ((struct elf_link_hash_entry *, PTR));
28 static boolean elf_adjust_dynamic_symbol
29 PARAMS ((struct elf_link_hash_entry *, PTR));
30 static boolean elf_link_find_version_dependencies
31 PARAMS ((struct elf_link_hash_entry *, PTR));
32 static boolean elf_link_find_version_dependencies
33 PARAMS ((struct elf_link_hash_entry *, PTR));
34 static boolean elf_link_assign_sym_version
35 PARAMS ((struct elf_link_hash_entry *, PTR));
36 static boolean elf_link_renumber_dynsyms
37 PARAMS ((struct elf_link_hash_entry *, PTR));
38
39 /* This struct is used to pass information to routines called via
40 elf_link_hash_traverse which must return failure. */
41
42 struct elf_info_failed
43 {
44 boolean failed;
45 struct bfd_link_info *info;
46 };
47
48 /* Given an ELF BFD, add symbols to the global hash table as
49 appropriate. */
50
51 boolean
52 elf_bfd_link_add_symbols (abfd, info)
53 bfd *abfd;
54 struct bfd_link_info *info;
55 {
56 switch (bfd_get_format (abfd))
57 {
58 case bfd_object:
59 return elf_link_add_object_symbols (abfd, info);
60 case bfd_archive:
61 return elf_link_add_archive_symbols (abfd, info);
62 default:
63 bfd_set_error (bfd_error_wrong_format);
64 return false;
65 }
66 }
67 \f
68
69 /* Add symbols from an ELF archive file to the linker hash table. We
70 don't use _bfd_generic_link_add_archive_symbols because of a
71 problem which arises on UnixWare. The UnixWare libc.so is an
72 archive which includes an entry libc.so.1 which defines a bunch of
73 symbols. The libc.so archive also includes a number of other
74 object files, which also define symbols, some of which are the same
75 as those defined in libc.so.1. Correct linking requires that we
76 consider each object file in turn, and include it if it defines any
77 symbols we need. _bfd_generic_link_add_archive_symbols does not do
78 this; it looks through the list of undefined symbols, and includes
79 any object file which defines them. When this algorithm is used on
80 UnixWare, it winds up pulling in libc.so.1 early and defining a
81 bunch of symbols. This means that some of the other objects in the
82 archive are not included in the link, which is incorrect since they
83 precede libc.so.1 in the archive.
84
85 Fortunately, ELF archive handling is simpler than that done by
86 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
87 oddities. In ELF, if we find a symbol in the archive map, and the
88 symbol is currently undefined, we know that we must pull in that
89 object file.
90
91 Unfortunately, we do have to make multiple passes over the symbol
92 table until nothing further is resolved. */
93
94 static boolean
95 elf_link_add_archive_symbols (abfd, info)
96 bfd *abfd;
97 struct bfd_link_info *info;
98 {
99 symindex c;
100 boolean *defined = NULL;
101 boolean *included = NULL;
102 carsym *symdefs;
103 boolean loop;
104
105 if (! bfd_has_map (abfd))
106 {
107 /* An empty archive is a special case. */
108 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
109 return true;
110 bfd_set_error (bfd_error_no_armap);
111 return false;
112 }
113
114 /* Keep track of all symbols we know to be already defined, and all
115 files we know to be already included. This is to speed up the
116 second and subsequent passes. */
117 c = bfd_ardata (abfd)->symdef_count;
118 if (c == 0)
119 return true;
120 defined = (boolean *) bfd_malloc (c * sizeof (boolean));
121 included = (boolean *) bfd_malloc (c * sizeof (boolean));
122 if (defined == (boolean *) NULL || included == (boolean *) NULL)
123 goto error_return;
124 memset (defined, 0, c * sizeof (boolean));
125 memset (included, 0, c * sizeof (boolean));
126
127 symdefs = bfd_ardata (abfd)->symdefs;
128
129 do
130 {
131 file_ptr last;
132 symindex i;
133 carsym *symdef;
134 carsym *symdefend;
135
136 loop = false;
137 last = -1;
138
139 symdef = symdefs;
140 symdefend = symdef + c;
141 for (i = 0; symdef < symdefend; symdef++, i++)
142 {
143 struct elf_link_hash_entry *h;
144 bfd *element;
145 struct bfd_link_hash_entry *undefs_tail;
146 symindex mark;
147
148 if (defined[i] || included[i])
149 continue;
150 if (symdef->file_offset == last)
151 {
152 included[i] = true;
153 continue;
154 }
155
156 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
157 false, false, false);
158
159 if (h == NULL)
160 {
161 char *p, *copy;
162
163 /* If this is a default version (the name contains @@),
164 look up the symbol again without the version. The
165 effect is that references to the symbol without the
166 version will be matched by the default symbol in the
167 archive. */
168
169 p = strchr (symdef->name, ELF_VER_CHR);
170 if (p == NULL || p[1] != ELF_VER_CHR)
171 continue;
172
173 copy = bfd_alloc (abfd, p - symdef->name + 1);
174 if (copy == NULL)
175 goto error_return;
176 memcpy (copy, symdef->name, p - symdef->name);
177 copy[p - symdef->name] = '\0';
178
179 h = elf_link_hash_lookup (elf_hash_table (info), copy,
180 false, false, false);
181
182 bfd_release (abfd, copy);
183 }
184
185 if (h == NULL)
186 continue;
187
188 if (h->root.type != bfd_link_hash_undefined)
189 {
190 if (h->root.type != bfd_link_hash_undefweak)
191 defined[i] = true;
192 continue;
193 }
194
195 /* We need to include this archive member. */
196
197 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
198 if (element == (bfd *) NULL)
199 goto error_return;
200
201 if (! bfd_check_format (element, bfd_object))
202 goto error_return;
203
204 /* Doublecheck that we have not included this object
205 already--it should be impossible, but there may be
206 something wrong with the archive. */
207 if (element->archive_pass != 0)
208 {
209 bfd_set_error (bfd_error_bad_value);
210 goto error_return;
211 }
212 element->archive_pass = 1;
213
214 undefs_tail = info->hash->undefs_tail;
215
216 if (! (*info->callbacks->add_archive_element) (info, element,
217 symdef->name))
218 goto error_return;
219 if (! elf_link_add_object_symbols (element, info))
220 goto error_return;
221
222 /* If there are any new undefined symbols, we need to make
223 another pass through the archive in order to see whether
224 they can be defined. FIXME: This isn't perfect, because
225 common symbols wind up on undefs_tail and because an
226 undefined symbol which is defined later on in this pass
227 does not require another pass. This isn't a bug, but it
228 does make the code less efficient than it could be. */
229 if (undefs_tail != info->hash->undefs_tail)
230 loop = true;
231
232 /* Look backward to mark all symbols from this object file
233 which we have already seen in this pass. */
234 mark = i;
235 do
236 {
237 included[mark] = true;
238 if (mark == 0)
239 break;
240 --mark;
241 }
242 while (symdefs[mark].file_offset == symdef->file_offset);
243
244 /* We mark subsequent symbols from this object file as we go
245 on through the loop. */
246 last = symdef->file_offset;
247 }
248 }
249 while (loop);
250
251 free (defined);
252 free (included);
253
254 return true;
255
256 error_return:
257 if (defined != (boolean *) NULL)
258 free (defined);
259 if (included != (boolean *) NULL)
260 free (included);
261 return false;
262 }
263
264 /* Add symbols from an ELF object file to the linker hash table. */
265
266 static boolean
267 elf_link_add_object_symbols (abfd, info)
268 bfd *abfd;
269 struct bfd_link_info *info;
270 {
271 boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
272 const Elf_Internal_Sym *,
273 const char **, flagword *,
274 asection **, bfd_vma *));
275 boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
276 asection *, const Elf_Internal_Rela *));
277 boolean collect;
278 Elf_Internal_Shdr *hdr;
279 size_t symcount;
280 size_t extsymcount;
281 size_t extsymoff;
282 Elf_External_Sym *buf = NULL;
283 struct elf_link_hash_entry **sym_hash;
284 boolean dynamic;
285 bfd_byte *dynver = NULL;
286 Elf_External_Versym *extversym = NULL;
287 Elf_External_Versym *ever;
288 Elf_External_Dyn *dynbuf = NULL;
289 struct elf_link_hash_entry *weaks;
290 Elf_External_Sym *esym;
291 Elf_External_Sym *esymend;
292
293 add_symbol_hook = get_elf_backend_data (abfd)->elf_add_symbol_hook;
294 collect = get_elf_backend_data (abfd)->collect;
295
296 if ((abfd->flags & DYNAMIC) == 0)
297 dynamic = false;
298 else
299 {
300 dynamic = true;
301
302 /* You can't use -r against a dynamic object. Also, there's no
303 hope of using a dynamic object which does not exactly match
304 the format of the output file. */
305 if (info->relocateable || info->hash->creator != abfd->xvec)
306 {
307 bfd_set_error (bfd_error_invalid_operation);
308 goto error_return;
309 }
310 }
311
312 /* As a GNU extension, any input sections which are named
313 .gnu.warning.SYMBOL are treated as warning symbols for the given
314 symbol. This differs from .gnu.warning sections, which generate
315 warnings when they are included in an output file. */
316 if (! info->shared)
317 {
318 asection *s;
319
320 for (s = abfd->sections; s != NULL; s = s->next)
321 {
322 const char *name;
323
324 name = bfd_get_section_name (abfd, s);
325 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
326 {
327 char *msg;
328 bfd_size_type sz;
329
330 name += sizeof ".gnu.warning." - 1;
331
332 /* If this is a shared object, then look up the symbol
333 in the hash table. If it is there, and it is already
334 been defined, then we will not be using the entry
335 from this shared object, so we don't need to warn.
336 FIXME: If we see the definition in a regular object
337 later on, we will warn, but we shouldn't. The only
338 fix is to keep track of what warnings we are supposed
339 to emit, and then handle them all at the end of the
340 link. */
341 if (dynamic && abfd->xvec == info->hash->creator)
342 {
343 struct elf_link_hash_entry *h;
344
345 h = elf_link_hash_lookup (elf_hash_table (info), name,
346 false, false, true);
347
348 /* FIXME: What about bfd_link_hash_common? */
349 if (h != NULL
350 && (h->root.type == bfd_link_hash_defined
351 || h->root.type == bfd_link_hash_defweak))
352 {
353 /* We don't want to issue this warning. Clobber
354 the section size so that the warning does not
355 get copied into the output file. */
356 s->_raw_size = 0;
357 continue;
358 }
359 }
360
361 sz = bfd_section_size (abfd, s);
362 msg = (char *) bfd_alloc (abfd, sz);
363 if (msg == NULL)
364 goto error_return;
365
366 if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
367 goto error_return;
368
369 if (! (_bfd_generic_link_add_one_symbol
370 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
371 false, collect, (struct bfd_link_hash_entry **) NULL)))
372 goto error_return;
373
374 if (! info->relocateable)
375 {
376 /* Clobber the section size so that the warning does
377 not get copied into the output file. */
378 s->_raw_size = 0;
379 }
380 }
381 }
382 }
383
384 /* If this is a dynamic object, we always link against the .dynsym
385 symbol table, not the .symtab symbol table. The dynamic linker
386 will only see the .dynsym symbol table, so there is no reason to
387 look at .symtab for a dynamic object. */
388
389 if (! dynamic || elf_dynsymtab (abfd) == 0)
390 hdr = &elf_tdata (abfd)->symtab_hdr;
391 else
392 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
393
394 if (dynamic)
395 {
396 /* Read in any version definitions. */
397
398 if (! _bfd_elf_slurp_version_tables (abfd))
399 goto error_return;
400
401 /* Read in the symbol versions, but don't bother to convert them
402 to internal format. */
403 if (elf_dynversym (abfd) != 0)
404 {
405 Elf_Internal_Shdr *versymhdr;
406
407 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
408 extversym = (Elf_External_Versym *) bfd_malloc (hdr->sh_size);
409 if (extversym == NULL)
410 goto error_return;
411 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
412 || (bfd_read ((PTR) extversym, 1, versymhdr->sh_size, abfd)
413 != versymhdr->sh_size))
414 goto error_return;
415 }
416 }
417
418 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
419
420 /* The sh_info field of the symtab header tells us where the
421 external symbols start. We don't care about the local symbols at
422 this point. */
423 if (elf_bad_symtab (abfd))
424 {
425 extsymcount = symcount;
426 extsymoff = 0;
427 }
428 else
429 {
430 extsymcount = symcount - hdr->sh_info;
431 extsymoff = hdr->sh_info;
432 }
433
434 buf = ((Elf_External_Sym *)
435 bfd_malloc (extsymcount * sizeof (Elf_External_Sym)));
436 if (buf == NULL && extsymcount != 0)
437 goto error_return;
438
439 /* We store a pointer to the hash table entry for each external
440 symbol. */
441 sym_hash = ((struct elf_link_hash_entry **)
442 bfd_alloc (abfd,
443 extsymcount * sizeof (struct elf_link_hash_entry *)));
444 if (sym_hash == NULL)
445 goto error_return;
446 elf_sym_hashes (abfd) = sym_hash;
447
448 if (! dynamic)
449 {
450 /* If we are creating a shared library, create all the dynamic
451 sections immediately. We need to attach them to something,
452 so we attach them to this BFD, provided it is the right
453 format. FIXME: If there are no input BFD's of the same
454 format as the output, we can't make a shared library. */
455 if (info->shared
456 && ! elf_hash_table (info)->dynamic_sections_created
457 && abfd->xvec == info->hash->creator)
458 {
459 if (! elf_link_create_dynamic_sections (abfd, info))
460 goto error_return;
461 }
462 }
463 else
464 {
465 asection *s;
466 boolean add_needed;
467 const char *name;
468 bfd_size_type oldsize;
469 bfd_size_type strindex;
470
471 /* Find the name to use in a DT_NEEDED entry that refers to this
472 object. If the object has a DT_SONAME entry, we use it.
473 Otherwise, if the generic linker stuck something in
474 elf_dt_name, we use that. Otherwise, we just use the file
475 name. If the generic linker put a null string into
476 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
477 there is a DT_SONAME entry. */
478 add_needed = true;
479 name = bfd_get_filename (abfd);
480 if (elf_dt_name (abfd) != NULL)
481 {
482 name = elf_dt_name (abfd);
483 if (*name == '\0')
484 add_needed = false;
485 }
486 s = bfd_get_section_by_name (abfd, ".dynamic");
487 if (s != NULL)
488 {
489 Elf_External_Dyn *extdyn;
490 Elf_External_Dyn *extdynend;
491 int elfsec;
492 unsigned long link;
493
494 dynbuf = (Elf_External_Dyn *) bfd_malloc ((size_t) s->_raw_size);
495 if (dynbuf == NULL)
496 goto error_return;
497
498 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
499 (file_ptr) 0, s->_raw_size))
500 goto error_return;
501
502 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
503 if (elfsec == -1)
504 goto error_return;
505 link = elf_elfsections (abfd)[elfsec]->sh_link;
506
507 extdyn = dynbuf;
508 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
509 for (; extdyn < extdynend; extdyn++)
510 {
511 Elf_Internal_Dyn dyn;
512
513 elf_swap_dyn_in (abfd, extdyn, &dyn);
514 if (dyn.d_tag == DT_SONAME)
515 {
516 name = bfd_elf_string_from_elf_section (abfd, link,
517 dyn.d_un.d_val);
518 if (name == NULL)
519 goto error_return;
520 }
521 if (dyn.d_tag == DT_NEEDED)
522 {
523 struct bfd_link_needed_list *n, **pn;
524 char *fnm, *anm;
525
526 n = ((struct bfd_link_needed_list *)
527 bfd_alloc (abfd, sizeof (struct bfd_link_needed_list)));
528 fnm = bfd_elf_string_from_elf_section (abfd, link,
529 dyn.d_un.d_val);
530 if (n == NULL || fnm == NULL)
531 goto error_return;
532 anm = bfd_alloc (abfd, strlen (fnm) + 1);
533 if (anm == NULL)
534 goto error_return;
535 strcpy (anm, fnm);
536 n->name = anm;
537 n->by = abfd;
538 n->next = NULL;
539 for (pn = &elf_hash_table (info)->needed;
540 *pn != NULL;
541 pn = &(*pn)->next)
542 ;
543 *pn = n;
544 }
545 }
546
547 free (dynbuf);
548 dynbuf = NULL;
549 }
550
551 /* We do not want to include any of the sections in a dynamic
552 object in the output file. We hack by simply clobbering the
553 list of sections in the BFD. This could be handled more
554 cleanly by, say, a new section flag; the existing
555 SEC_NEVER_LOAD flag is not the one we want, because that one
556 still implies that the section takes up space in the output
557 file. */
558 abfd->sections = NULL;
559 abfd->section_count = 0;
560
561 /* If this is the first dynamic object found in the link, create
562 the special sections required for dynamic linking. */
563 if (! elf_hash_table (info)->dynamic_sections_created)
564 {
565 if (! elf_link_create_dynamic_sections (abfd, info))
566 goto error_return;
567 }
568
569 if (add_needed)
570 {
571 /* Add a DT_NEEDED entry for this dynamic object. */
572 oldsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
573 strindex = _bfd_stringtab_add (elf_hash_table (info)->dynstr, name,
574 true, false);
575 if (strindex == (bfd_size_type) -1)
576 goto error_return;
577
578 if (oldsize == _bfd_stringtab_size (elf_hash_table (info)->dynstr))
579 {
580 asection *sdyn;
581 Elf_External_Dyn *dyncon, *dynconend;
582
583 /* The hash table size did not change, which means that
584 the dynamic object name was already entered. If we
585 have already included this dynamic object in the
586 link, just ignore it. There is no reason to include
587 a particular dynamic object more than once. */
588 sdyn = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
589 ".dynamic");
590 BFD_ASSERT (sdyn != NULL);
591
592 dyncon = (Elf_External_Dyn *) sdyn->contents;
593 dynconend = (Elf_External_Dyn *) (sdyn->contents +
594 sdyn->_raw_size);
595 for (; dyncon < dynconend; dyncon++)
596 {
597 Elf_Internal_Dyn dyn;
598
599 elf_swap_dyn_in (elf_hash_table (info)->dynobj, dyncon,
600 &dyn);
601 if (dyn.d_tag == DT_NEEDED
602 && dyn.d_un.d_val == strindex)
603 {
604 if (buf != NULL)
605 free (buf);
606 if (extversym != NULL)
607 free (extversym);
608 return true;
609 }
610 }
611 }
612
613 if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
614 goto error_return;
615 }
616
617 /* Save the SONAME, if there is one, because sometimes the
618 linker emulation code will need to know it. */
619 if (*name == '\0')
620 name = bfd_get_filename (abfd);
621 elf_dt_name (abfd) = name;
622 }
623
624 if (bfd_seek (abfd,
625 hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym),
626 SEEK_SET) != 0
627 || (bfd_read ((PTR) buf, sizeof (Elf_External_Sym), extsymcount, abfd)
628 != extsymcount * sizeof (Elf_External_Sym)))
629 goto error_return;
630
631 weaks = NULL;
632
633 ever = extversym != NULL ? extversym + extsymoff : NULL;
634 esymend = buf + extsymcount;
635 for (esym = buf;
636 esym < esymend;
637 esym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
638 {
639 Elf_Internal_Sym sym;
640 int bind;
641 bfd_vma value;
642 asection *sec;
643 flagword flags;
644 const char *name;
645 struct elf_link_hash_entry *h;
646 boolean definition;
647 boolean size_change_ok, type_change_ok;
648 boolean new_weakdef;
649 unsigned int old_alignment;
650
651 elf_swap_symbol_in (abfd, esym, &sym);
652
653 flags = BSF_NO_FLAGS;
654 sec = NULL;
655 value = sym.st_value;
656 *sym_hash = NULL;
657
658 bind = ELF_ST_BIND (sym.st_info);
659 if (bind == STB_LOCAL)
660 {
661 /* This should be impossible, since ELF requires that all
662 global symbols follow all local symbols, and that sh_info
663 point to the first global symbol. Unfortunatealy, Irix 5
664 screws this up. */
665 continue;
666 }
667 else if (bind == STB_GLOBAL)
668 {
669 if (sym.st_shndx != SHN_UNDEF
670 && sym.st_shndx != SHN_COMMON)
671 flags = BSF_GLOBAL;
672 else
673 flags = 0;
674 }
675 else if (bind == STB_WEAK)
676 flags = BSF_WEAK;
677 else
678 {
679 /* Leave it up to the processor backend. */
680 }
681
682 if (sym.st_shndx == SHN_UNDEF)
683 sec = bfd_und_section_ptr;
684 else if (sym.st_shndx > 0 && sym.st_shndx < SHN_LORESERVE)
685 {
686 sec = section_from_elf_index (abfd, sym.st_shndx);
687 if (sec == NULL)
688 sec = bfd_abs_section_ptr;
689 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
690 value -= sec->vma;
691 }
692 else if (sym.st_shndx == SHN_ABS)
693 sec = bfd_abs_section_ptr;
694 else if (sym.st_shndx == SHN_COMMON)
695 {
696 sec = bfd_com_section_ptr;
697 /* What ELF calls the size we call the value. What ELF
698 calls the value we call the alignment. */
699 value = sym.st_size;
700 }
701 else
702 {
703 /* Leave it up to the processor backend. */
704 }
705
706 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
707 if (name == (const char *) NULL)
708 goto error_return;
709
710 if (add_symbol_hook)
711 {
712 if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
713 &value))
714 goto error_return;
715
716 /* The hook function sets the name to NULL if this symbol
717 should be skipped for some reason. */
718 if (name == (const char *) NULL)
719 continue;
720 }
721
722 /* Sanity check that all possibilities were handled. */
723 if (sec == (asection *) NULL)
724 {
725 bfd_set_error (bfd_error_bad_value);
726 goto error_return;
727 }
728
729 if (bfd_is_und_section (sec)
730 || bfd_is_com_section (sec))
731 definition = false;
732 else
733 definition = true;
734
735 size_change_ok = false;
736 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
737 old_alignment = 0;
738 if (info->hash->creator->flavour == bfd_target_elf_flavour)
739 {
740 Elf_Internal_Versym iver;
741 int vernum;
742 boolean override;
743
744 if (ever != NULL)
745 {
746 _bfd_elf_swap_versym_in (abfd, ever, &iver);
747 vernum = iver.vs_vers & VERSYM_VERSION;
748
749 /* If this is a hidden symbol, or if it is not version
750 1, we append the version name to the symbol name.
751 However, we do not modify a non-hidden absolute
752 symbol, because it might be the version symbol
753 itself. FIXME: What if it isn't? */
754 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
755 || (vernum > 1 && ! bfd_is_abs_section (sec)))
756 {
757 const char *verstr;
758 int namelen, newlen;
759 char *newname, *p;
760
761 if (sym.st_shndx != SHN_UNDEF)
762 {
763 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
764 {
765 (*_bfd_error_handler)
766 ("%s: %s: invalid version %d (max %d)",
767 abfd->filename, name, vernum,
768 elf_tdata (abfd)->dynverdef_hdr.sh_info);
769 bfd_set_error (bfd_error_bad_value);
770 goto error_return;
771 }
772 else if (vernum > 1)
773 verstr =
774 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
775 else
776 verstr = "";
777 }
778 else
779 {
780 /* We cannot simply test for the number of
781 entries in the VERNEED section since the
782 numbers for the needed versions do not start
783 at 0. */
784 Elf_Internal_Verneed *t;
785
786 verstr = NULL;
787 for (t = elf_tdata (abfd)->verref;
788 t != NULL;
789 t = t->vn_nextref)
790 {
791 Elf_Internal_Vernaux *a;
792
793 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
794 {
795 if (a->vna_other == vernum)
796 {
797 verstr = a->vna_nodename;
798 break;
799 }
800 }
801 if (a != NULL)
802 break;
803 }
804 if (verstr == NULL)
805 {
806 (*_bfd_error_handler)
807 ("%s: %s: invalid needed version %d",
808 abfd->filename, name, vernum);
809 bfd_set_error (bfd_error_bad_value);
810 goto error_return;
811 }
812 }
813
814 namelen = strlen (name);
815 newlen = namelen + strlen (verstr) + 2;
816 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
817 ++newlen;
818
819 newname = (char *) bfd_alloc (abfd, newlen);
820 if (newname == NULL)
821 goto error_return;
822 strcpy (newname, name);
823 p = newname + namelen;
824 *p++ = ELF_VER_CHR;
825 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
826 *p++ = ELF_VER_CHR;
827 strcpy (p, verstr);
828
829 name = newname;
830 }
831 }
832
833 /* We need to look up the symbol now in order to get some of
834 the dynamic object handling right. We pass the hash
835 table entry in to _bfd_generic_link_add_one_symbol so
836 that it does not have to look it up again. */
837 if (! bfd_is_und_section (sec))
838 h = elf_link_hash_lookup (elf_hash_table (info), name,
839 true, false, false);
840 else
841 h = ((struct elf_link_hash_entry *)
842 bfd_wrapped_link_hash_lookup (abfd, info, name, true,
843 false, false));
844 if (h == NULL)
845 goto error_return;
846 *sym_hash = h;
847
848 if (h->root.type == bfd_link_hash_new)
849 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
850
851 while (h->root.type == bfd_link_hash_indirect
852 || h->root.type == bfd_link_hash_warning)
853 h = (struct elf_link_hash_entry *) h->root.u.i.link;
854
855 /* FIXME: There are too many cases here, and it's too
856 confusing. This code needs to be reorganized somehow. */
857
858 /* It's OK to change the type if it used to be a weak
859 definition, or if the current definition is weak (and
860 hence might be ignored). */
861 if (h->root.type == bfd_link_hash_defweak
862 || h->root.type == bfd_link_hash_undefweak
863 || bind == STB_WEAK)
864 type_change_ok = true;
865
866 /* It's OK to change the size if it used to be a weak
867 definition, or if it used to be undefined, or if we will
868 be overriding an old definition. */
869 if (type_change_ok
870 || h->root.type == bfd_link_hash_undefined)
871 size_change_ok = true;
872
873 if (h->root.type == bfd_link_hash_common)
874 old_alignment = h->root.u.c.p->alignment_power;
875
876 override = false;
877
878 /* If we are looking at a dynamic object, and this is a
879 definition, we need to see if it has already been defined
880 by some other object. If it has, we want to use the
881 existing definition, and we do not want to report a
882 multiple symbol definition error; we do this by
883 clobbering sec to be bfd_und_section_ptr. We treat a
884 common symbol as a definition if the symbol in the shared
885 library is a function, since common symbols always
886 represent variables; this can cause confusion in
887 principle, but any such confusion would seem to indicate
888 an erroneous program or shared library. */
889 if (dynamic && definition)
890 {
891 if (h->root.type == bfd_link_hash_defined
892 || h->root.type == bfd_link_hash_defweak
893 || (h->root.type == bfd_link_hash_common
894 && (bind == STB_WEAK
895 || ELF_ST_TYPE (sym.st_info) == STT_FUNC)))
896 {
897 /* In the special case of two symbols which look
898 like common symbols in a dynamic object, set the
899 size of the symbol to the larger of the two. */
900 if ((sec->flags & SEC_ALLOC) != 0
901 && (sec->flags & SEC_LOAD) == 0
902 && sym.st_size > 0
903 && bind != STB_WEAK
904 && ELF_ST_TYPE (sym.st_info) != STT_FUNC
905 && h->root.type == bfd_link_hash_defined
906 && (h->elf_link_hash_flags
907 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
908 && (h->root.u.def.section->owner->flags & DYNAMIC) != 0
909 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
910 && (h->root.u.def.section->flags & SEC_LOAD) == 0
911 && h->size > 0
912 && h->type != STT_FUNC
913 && sym.st_size != h->size)
914 {
915 /* Note that we only warn if the size is
916 different. If the size is the same, then we
917 simply let the first shared library override
918 the second. */
919 if (! ((*info->callbacks->multiple_common)
920 (info, h->root.root.string,
921 h->root.u.def.section->owner,
922 bfd_link_hash_common,
923 h->size, abfd, bfd_link_hash_common,
924 sym.st_size)))
925 goto error_return;
926 if (sym.st_size > h->size)
927 h->size = sym.st_size;
928 }
929
930 override = true;
931 sec = bfd_und_section_ptr;
932 definition = false;
933 size_change_ok = true;
934 if (h->root.type == bfd_link_hash_common)
935 type_change_ok = true;
936 }
937 }
938
939 /* If we already have a common symbol, and the symbol in the
940 shared library is in an uninitialized section, then treat
941 the shared library symbol as a common symbol. This will
942 not always be correct, but it should do little harm. */
943 if (dynamic
944 && definition
945 && h->root.type == bfd_link_hash_common
946 && (sec->flags & SEC_ALLOC) != 0
947 && (sec->flags & SEC_LOAD) == 0
948 && sym.st_size > 0
949 && bind != STB_WEAK
950 && ELF_ST_TYPE (sym.st_info) != STT_FUNC)
951 {
952 override = true;
953 sec = bfd_com_section_ptr;
954 definition = false;
955 value = sym.st_size;
956 size_change_ok = true;
957 }
958
959 /* If we are not looking at a dynamic object, and we have a
960 definition, we want to override any definition we may
961 have from a dynamic object. Symbols from regular files
962 always take precedence over symbols from dynamic objects,
963 even if they are defined after the dynamic object in the
964 link. */
965 if (! dynamic
966 && (definition
967 || (bfd_is_com_section (sec)
968 && (h->root.type == bfd_link_hash_defweak
969 || h->type == STT_FUNC)))
970 && (h->root.type == bfd_link_hash_defined
971 || h->root.type == bfd_link_hash_defweak)
972 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
973 && (h->root.u.def.section->owner->flags & DYNAMIC) != 0)
974 {
975 override = true;
976 /* Change the hash table entry to undefined, and let
977 _bfd_generic_link_add_one_symbol do the right thing
978 with the new definition. */
979 h->root.type = bfd_link_hash_undefined;
980 h->root.u.undef.abfd = h->root.u.def.section->owner;
981 size_change_ok = true;
982 if (bfd_is_com_section (sec))
983 type_change_ok = true;
984
985 /* This union may have been set to be non-NULL when this
986 symbol was seen in a dynamic object. We must force
987 the union to be NULL, so that it is correct for a
988 regular symbol. */
989 h->verinfo.vertree = NULL;
990 }
991
992 /* If we are not looking at a shared library and we have a
993 common symbol, and the symbol in the shared library is in
994 an uninitialized section, then treat the shared library
995 symbol as a common symbol. This will not always be
996 correct, but it should do little harm. Note that the
997 above condition already handled cases in which a common
998 symbol should simply override the definition in the
999 shared library. */
1000 if (! dynamic
1001 && ! override
1002 && bfd_is_com_section (sec)
1003 && h->root.type == bfd_link_hash_defined
1004 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1005 && (h->root.u.def.section->owner->flags & DYNAMIC) != 0
1006 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1007 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1008 && h->size > 0
1009 && h->type != STT_FUNC)
1010 {
1011 /* It would be best if we could set the hash table entry
1012 to a common symbol, but we don't know what to use for
1013 the section or the alignment. */
1014 if (! ((*info->callbacks->multiple_common)
1015 (info, h->root.root.string,
1016 h->root.u.def.section->owner, bfd_link_hash_common,
1017 h->size, abfd, bfd_link_hash_common, value)))
1018 goto error_return;
1019
1020 if (h->size > value)
1021 value = h->size;
1022
1023 /* FIXME: We no longer know the alignment required by
1024 the symbol in the shared library, so we just wind up
1025 using the one from the regular object. */
1026
1027 override = true;
1028 h->root.type = bfd_link_hash_undefined;
1029 h->root.u.undef.abfd = h->root.u.def.section->owner;
1030 size_change_ok = true;
1031 type_change_ok = true;
1032 h->verinfo.vertree = NULL;
1033 }
1034
1035 if (ever != NULL
1036 && ! override
1037 && vernum > 1
1038 && (h->verinfo.verdef == NULL || definition))
1039 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1040 }
1041
1042 if (! (_bfd_generic_link_add_one_symbol
1043 (info, abfd, name, flags, sec, value, (const char *) NULL,
1044 false, collect, (struct bfd_link_hash_entry **) sym_hash)))
1045 goto error_return;
1046
1047 h = *sym_hash;
1048 while (h->root.type == bfd_link_hash_indirect
1049 || h->root.type == bfd_link_hash_warning)
1050 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1051 *sym_hash = h;
1052
1053 new_weakdef = false;
1054 if (dynamic
1055 && definition
1056 && (flags & BSF_WEAK) != 0
1057 && ELF_ST_TYPE (sym.st_info) != STT_FUNC
1058 && info->hash->creator->flavour == bfd_target_elf_flavour
1059 && h->weakdef == NULL)
1060 {
1061 /* Keep a list of all weak defined non function symbols from
1062 a dynamic object, using the weakdef field. Later in this
1063 function we will set the weakdef field to the correct
1064 value. We only put non-function symbols from dynamic
1065 objects on this list, because that happens to be the only
1066 time we need to know the normal symbol corresponding to a
1067 weak symbol, and the information is time consuming to
1068 figure out. If the weakdef field is not already NULL,
1069 then this symbol was already defined by some previous
1070 dynamic object, and we will be using that previous
1071 definition anyhow. */
1072
1073 h->weakdef = weaks;
1074 weaks = h;
1075 new_weakdef = true;
1076 }
1077
1078 /* Set the alignment of a common symbol. */
1079 if (sym.st_shndx == SHN_COMMON
1080 && h->root.type == bfd_link_hash_common)
1081 {
1082 unsigned int align;
1083
1084 align = bfd_log2 (sym.st_value);
1085 if (align > old_alignment)
1086 h->root.u.c.p->alignment_power = align;
1087 }
1088
1089 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1090 {
1091 int old_flags;
1092 boolean dynsym;
1093 int new_flag;
1094
1095 /* Remember the symbol size and type. */
1096 if (sym.st_size != 0
1097 && (definition || h->size == 0))
1098 {
1099 if (h->size != 0 && h->size != sym.st_size && ! size_change_ok)
1100 (*_bfd_error_handler)
1101 ("Warning: size of symbol `%s' changed from %lu to %lu in %s",
1102 name, (unsigned long) h->size, (unsigned long) sym.st_size,
1103 bfd_get_filename (abfd));
1104
1105 h->size = sym.st_size;
1106 }
1107
1108 /* If this is a common symbol, then we always want H->SIZE
1109 to be the size of the common symbol. The code just above
1110 won't fix the size if a common symbol becomes larger. We
1111 don't warn about a size change here, because that is
1112 covered by --warn-common. */
1113 if (h->root.type == bfd_link_hash_common)
1114 h->size = h->root.u.c.size;
1115
1116 if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE
1117 && (definition || h->type == STT_NOTYPE))
1118 {
1119 if (h->type != STT_NOTYPE
1120 && h->type != ELF_ST_TYPE (sym.st_info)
1121 && ! type_change_ok)
1122 (*_bfd_error_handler)
1123 ("Warning: type of symbol `%s' changed from %d to %d in %s",
1124 name, h->type, ELF_ST_TYPE (sym.st_info),
1125 bfd_get_filename (abfd));
1126
1127 h->type = ELF_ST_TYPE (sym.st_info);
1128 }
1129
1130 if (sym.st_other != 0
1131 && (definition || h->other == 0))
1132 h->other = sym.st_other;
1133
1134 /* Set a flag in the hash table entry indicating the type of
1135 reference or definition we just found. Keep a count of
1136 the number of dynamic symbols we find. A dynamic symbol
1137 is one which is referenced or defined by both a regular
1138 object and a shared object. */
1139 old_flags = h->elf_link_hash_flags;
1140 dynsym = false;
1141 if (! dynamic)
1142 {
1143 if (! definition)
1144 new_flag = ELF_LINK_HASH_REF_REGULAR;
1145 else
1146 new_flag = ELF_LINK_HASH_DEF_REGULAR;
1147 if (info->shared
1148 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
1149 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
1150 dynsym = true;
1151 }
1152 else
1153 {
1154 if (! definition)
1155 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
1156 else
1157 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
1158 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
1159 | ELF_LINK_HASH_REF_REGULAR)) != 0
1160 || (h->weakdef != NULL
1161 && ! new_weakdef
1162 && h->weakdef->dynindx != -1))
1163 dynsym = true;
1164 }
1165
1166 h->elf_link_hash_flags |= new_flag;
1167
1168 /* If this symbol has a version, and it is the default
1169 version, we create an indirect symbol from the default
1170 name to the fully decorated name. This will cause
1171 external references which do not specify a version to be
1172 bound to this version of the symbol. */
1173 if (definition)
1174 {
1175 char *p;
1176
1177 p = strchr (name, ELF_VER_CHR);
1178 if (p != NULL && p[1] == ELF_VER_CHR)
1179 {
1180 char *shortname;
1181 struct elf_link_hash_entry *hold;
1182
1183 shortname = bfd_hash_allocate (&info->hash->table,
1184 p - name + 1);
1185 if (shortname == NULL)
1186 goto error_return;
1187 strncpy (shortname, name, p - name);
1188 shortname[p - name] = '\0';
1189
1190 /* First look to see if we have an existing symbol
1191 with this name. */
1192 hold = elf_link_hash_lookup (elf_hash_table (info),
1193 shortname, false, false,
1194 false);
1195
1196 /* If we are looking at a normal object, and the
1197 symbol was seen in a shared object, clobber the
1198 definition in the shared object. */
1199 if (hold != NULL
1200 && ! dynamic
1201 && (hold->root.type == bfd_link_hash_defined
1202 || hold->root.type == bfd_link_hash_defweak)
1203 && (hold->elf_link_hash_flags
1204 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1205 && ((hold->root.u.def.section->owner->flags & DYNAMIC)
1206 != 0))
1207 {
1208 /* Change the hash table entry to undefined, so
1209 that _bfd_generic_link_add_one_symbol will do
1210 the right thing. */
1211 hold->root.type = bfd_link_hash_undefined;
1212 hold->root.u.undef.abfd =
1213 hold->root.u.def.section->owner;
1214 hold->verinfo.vertree = NULL;
1215 hold = NULL;
1216 }
1217
1218 /* If we are looking at a shared object, and we have
1219 already seen this symbol defined elsewhere, then
1220 don't try to define it again. */
1221 if (hold != NULL
1222 && dynamic
1223 && (hold->root.type == bfd_link_hash_defined
1224 || hold->root.type == bfd_link_hash_defweak
1225 || hold->root.type == bfd_link_hash_indirect
1226 || (hold->root.type == bfd_link_hash_common
1227 && (bind == STB_WEAK
1228 || ELF_ST_TYPE (sym.st_info) == STT_FUNC))))
1229 {
1230 /* Don't add an indirect symbol. */
1231 }
1232 else
1233 {
1234 struct elf_link_hash_entry *hi;
1235
1236 hi = NULL;
1237 if (! (_bfd_generic_link_add_one_symbol
1238 (info, abfd, shortname, BSF_INDIRECT,
1239 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1240 collect, (struct bfd_link_hash_entry **) &hi)))
1241 goto error_return;
1242
1243 /* If there is a duplicate definition somewhere,
1244 then HI may not point to an indirect symbol.
1245 We will have reported an error to the user in
1246 that case. */
1247
1248 if (hi->root.type == bfd_link_hash_indirect)
1249 {
1250 hi->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
1251
1252 /* If the symbol became indirect, then we
1253 assume that we have not seen a definition
1254 before. */
1255 BFD_ASSERT ((hi->elf_link_hash_flags
1256 & (ELF_LINK_HASH_DEF_DYNAMIC
1257 | ELF_LINK_HASH_DEF_REGULAR))
1258 == 0);
1259
1260 /* Copy down any references that we may have
1261 already seen to the symbol which just
1262 became indirect. */
1263 h->elf_link_hash_flags |=
1264 (hi->elf_link_hash_flags
1265 & (ELF_LINK_HASH_REF_DYNAMIC
1266 | ELF_LINK_HASH_REF_REGULAR));
1267
1268 /* Copy over the global table offset entry.
1269 This may have been already set up by a
1270 check_relocs routine. */
1271 if (h->got_offset == (bfd_vma) -1)
1272 {
1273 h->got_offset = hi->got_offset;
1274 hi->got_offset = (bfd_vma) -1;
1275 }
1276 BFD_ASSERT (hi->got_offset == (bfd_vma) -1);
1277
1278 if (h->dynindx == -1)
1279 {
1280 h->dynindx = hi->dynindx;
1281 h->dynstr_index = hi->dynstr_index;
1282 hi->dynindx = -1;
1283 hi->dynstr_index = 0;
1284 }
1285 BFD_ASSERT (hi->dynindx == -1);
1286
1287 /* FIXME: There may be other information to
1288 copy over for particular targets. */
1289
1290 /* See if the new flags lead us to realize
1291 that the symbol must be dynamic. */
1292 if (! dynsym)
1293 {
1294 if (! dynamic)
1295 {
1296 if (info->shared
1297 || ((hi->elf_link_hash_flags
1298 & ELF_LINK_HASH_REF_DYNAMIC)
1299 != 0))
1300 dynsym = true;
1301 }
1302 else
1303 {
1304 if ((hi->elf_link_hash_flags
1305 & ELF_LINK_HASH_REF_REGULAR) != 0)
1306 dynsym = true;
1307 }
1308 }
1309 }
1310 }
1311
1312 /* We also need to define an indirection from the
1313 nondefault version of the symbol. */
1314
1315 shortname = bfd_hash_allocate (&info->hash->table,
1316 strlen (name));
1317 if (shortname == NULL)
1318 goto error_return;
1319 strncpy (shortname, name, p - name);
1320 strcpy (shortname + (p - name), p + 1);
1321
1322 /* First look to see if we have an existing symbol
1323 with this name. */
1324 hold = elf_link_hash_lookup (elf_hash_table (info),
1325 shortname, false, false,
1326 false);
1327
1328 /* If we are looking at a normal object, and the
1329 symbol was seen in a shared object, clobber the
1330 definition in the shared object. */
1331 if (hold != NULL
1332 && ! dynamic
1333 && (hold->root.type == bfd_link_hash_defined
1334 || hold->root.type == bfd_link_hash_defweak)
1335 && (hold->elf_link_hash_flags
1336 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1337 && ((hold->root.u.def.section->owner->flags & DYNAMIC)
1338 != 0))
1339 {
1340 /* Change the hash table entry to undefined, so
1341 that _bfd_generic_link_add_one_symbol will do
1342 the right thing. */
1343 hold->root.type = bfd_link_hash_undefined;
1344 hold->root.u.undef.abfd =
1345 hold->root.u.def.section->owner;
1346 hold->verinfo.vertree = NULL;
1347 hold = NULL;
1348 }
1349
1350 /* If we are looking at a shared object, and we have
1351 already seen this symbol defined elsewhere, then
1352 don't try to define it again. */
1353 if (hold != NULL
1354 && dynamic
1355 && (hold->root.type == bfd_link_hash_defined
1356 || hold->root.type == bfd_link_hash_defweak
1357 || hold->root.type == bfd_link_hash_indirect
1358 || (hold->root.type == bfd_link_hash_common
1359 && (bind == STB_WEAK
1360 || ELF_ST_TYPE (sym.st_info) == STT_FUNC))))
1361 {
1362 /* Don't add an indirect symbol. */
1363 }
1364 else
1365 {
1366 struct elf_link_hash_entry *hi;
1367
1368 hi = NULL;
1369 if (! (_bfd_generic_link_add_one_symbol
1370 (info, abfd, shortname, BSF_INDIRECT,
1371 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1372 collect, (struct bfd_link_hash_entry **) &hi)))
1373 goto error_return;
1374
1375 /* If there is a duplicate definition somewhere,
1376 then HI may not point to an indirect symbol.
1377 We will have reported an error to the user in
1378 that case. */
1379
1380 if (hi->root.type == bfd_link_hash_indirect)
1381 {
1382 hi->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
1383
1384 /* If the symbol became indirect, then we
1385 assume that we have not seen a definition
1386 before. */
1387 BFD_ASSERT ((hi->elf_link_hash_flags
1388 & (ELF_LINK_HASH_DEF_DYNAMIC
1389 | ELF_LINK_HASH_DEF_REGULAR))
1390 == 0);
1391
1392 /* Copy down any references that we may have
1393 already seen to the symbol which just
1394 became indirect. */
1395 h->elf_link_hash_flags |=
1396 (hi->elf_link_hash_flags
1397 & (ELF_LINK_HASH_REF_DYNAMIC
1398 | ELF_LINK_HASH_REF_REGULAR));
1399
1400 /* Copy over the global table offset entry.
1401 This may have been already set up by a
1402 check_relocs routine. */
1403 if (h->got_offset == (bfd_vma) -1)
1404 {
1405 h->got_offset = hi->got_offset;
1406 hi->got_offset = (bfd_vma) -1;
1407 }
1408 BFD_ASSERT (hi->got_offset == (bfd_vma) -1);
1409
1410 if (h->dynindx == -1)
1411 {
1412 h->dynindx = hi->dynindx;
1413 h->dynstr_index = hi->dynstr_index;
1414 hi->dynindx = -1;
1415 hi->dynstr_index = 0;
1416 }
1417 BFD_ASSERT (hi->dynindx == -1);
1418
1419 /* FIXME: There may be other information to
1420 copy over for particular targets. */
1421
1422 /* See if the new flags lead us to realize
1423 that the symbol must be dynamic. */
1424 if (! dynsym)
1425 {
1426 if (! dynamic)
1427 {
1428 if (info->shared
1429 || ((hi->elf_link_hash_flags
1430 & ELF_LINK_HASH_REF_DYNAMIC)
1431 != 0))
1432 dynsym = true;
1433 }
1434 else
1435 {
1436 if ((hi->elf_link_hash_flags
1437 & ELF_LINK_HASH_REF_REGULAR) != 0)
1438 dynsym = true;
1439 }
1440 }
1441 }
1442 }
1443 }
1444 }
1445
1446 if (dynsym && h->dynindx == -1)
1447 {
1448 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1449 goto error_return;
1450 if (h->weakdef != NULL
1451 && ! new_weakdef
1452 && h->weakdef->dynindx == -1)
1453 {
1454 if (! _bfd_elf_link_record_dynamic_symbol (info,
1455 h->weakdef))
1456 goto error_return;
1457 }
1458 }
1459 }
1460 }
1461
1462 /* Now set the weakdefs field correctly for all the weak defined
1463 symbols we found. The only way to do this is to search all the
1464 symbols. Since we only need the information for non functions in
1465 dynamic objects, that's the only time we actually put anything on
1466 the list WEAKS. We need this information so that if a regular
1467 object refers to a symbol defined weakly in a dynamic object, the
1468 real symbol in the dynamic object is also put in the dynamic
1469 symbols; we also must arrange for both symbols to point to the
1470 same memory location. We could handle the general case of symbol
1471 aliasing, but a general symbol alias can only be generated in
1472 assembler code, handling it correctly would be very time
1473 consuming, and other ELF linkers don't handle general aliasing
1474 either. */
1475 while (weaks != NULL)
1476 {
1477 struct elf_link_hash_entry *hlook;
1478 asection *slook;
1479 bfd_vma vlook;
1480 struct elf_link_hash_entry **hpp;
1481 struct elf_link_hash_entry **hppend;
1482
1483 hlook = weaks;
1484 weaks = hlook->weakdef;
1485 hlook->weakdef = NULL;
1486
1487 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
1488 || hlook->root.type == bfd_link_hash_defweak
1489 || hlook->root.type == bfd_link_hash_common
1490 || hlook->root.type == bfd_link_hash_indirect);
1491 slook = hlook->root.u.def.section;
1492 vlook = hlook->root.u.def.value;
1493
1494 hpp = elf_sym_hashes (abfd);
1495 hppend = hpp + extsymcount;
1496 for (; hpp < hppend; hpp++)
1497 {
1498 struct elf_link_hash_entry *h;
1499
1500 h = *hpp;
1501 if (h != NULL && h != hlook
1502 && h->root.type == bfd_link_hash_defined
1503 && h->root.u.def.section == slook
1504 && h->root.u.def.value == vlook)
1505 {
1506 hlook->weakdef = h;
1507
1508 /* If the weak definition is in the list of dynamic
1509 symbols, make sure the real definition is put there
1510 as well. */
1511 if (hlook->dynindx != -1
1512 && h->dynindx == -1)
1513 {
1514 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1515 goto error_return;
1516 }
1517
1518 /* If the real definition is in the list of dynamic
1519 symbols, make sure the weak definition is put there
1520 as well. If we don't do this, then the dynamic
1521 loader might not merge the entries for the real
1522 definition and the weak definition. */
1523 if (h->dynindx != -1
1524 && hlook->dynindx == -1)
1525 {
1526 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
1527 goto error_return;
1528 }
1529
1530 break;
1531 }
1532 }
1533 }
1534
1535 if (buf != NULL)
1536 {
1537 free (buf);
1538 buf = NULL;
1539 }
1540
1541 if (extversym != NULL)
1542 {
1543 free (extversym);
1544 extversym = NULL;
1545 }
1546
1547 /* If this object is the same format as the output object, and it is
1548 not a shared library, then let the backend look through the
1549 relocs.
1550
1551 This is required to build global offset table entries and to
1552 arrange for dynamic relocs. It is not required for the
1553 particular common case of linking non PIC code, even when linking
1554 against shared libraries, but unfortunately there is no way of
1555 knowing whether an object file has been compiled PIC or not.
1556 Looking through the relocs is not particularly time consuming.
1557 The problem is that we must either (1) keep the relocs in memory,
1558 which causes the linker to require additional runtime memory or
1559 (2) read the relocs twice from the input file, which wastes time.
1560 This would be a good case for using mmap.
1561
1562 I have no idea how to handle linking PIC code into a file of a
1563 different format. It probably can't be done. */
1564 check_relocs = get_elf_backend_data (abfd)->check_relocs;
1565 if (! dynamic
1566 && abfd->xvec == info->hash->creator
1567 && check_relocs != NULL)
1568 {
1569 asection *o;
1570
1571 for (o = abfd->sections; o != NULL; o = o->next)
1572 {
1573 Elf_Internal_Rela *internal_relocs;
1574 boolean ok;
1575
1576 if ((o->flags & SEC_RELOC) == 0
1577 || o->reloc_count == 0
1578 || ((info->strip == strip_all || info->strip == strip_debugger)
1579 && (o->flags & SEC_DEBUGGING) != 0)
1580 || bfd_is_abs_section (o->output_section))
1581 continue;
1582
1583 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
1584 (abfd, o, (PTR) NULL,
1585 (Elf_Internal_Rela *) NULL,
1586 info->keep_memory));
1587 if (internal_relocs == NULL)
1588 goto error_return;
1589
1590 ok = (*check_relocs) (abfd, info, o, internal_relocs);
1591
1592 if (! info->keep_memory)
1593 free (internal_relocs);
1594
1595 if (! ok)
1596 goto error_return;
1597 }
1598 }
1599
1600 /* If this is a non-traditional, non-relocateable link, try to
1601 optimize the handling of the .stab/.stabstr sections. */
1602 if (! dynamic
1603 && ! info->relocateable
1604 && ! info->traditional_format
1605 && info->hash->creator->flavour == bfd_target_elf_flavour
1606 && (info->strip != strip_all && info->strip != strip_debugger))
1607 {
1608 asection *stab, *stabstr;
1609
1610 stab = bfd_get_section_by_name (abfd, ".stab");
1611 if (stab != NULL)
1612 {
1613 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
1614
1615 if (stabstr != NULL)
1616 {
1617 struct bfd_elf_section_data *secdata;
1618
1619 secdata = elf_section_data (stab);
1620 if (! _bfd_link_section_stabs (abfd,
1621 &elf_hash_table (info)->stab_info,
1622 stab, stabstr,
1623 &secdata->stab_info))
1624 goto error_return;
1625 }
1626 }
1627 }
1628
1629 return true;
1630
1631 error_return:
1632 if (buf != NULL)
1633 free (buf);
1634 if (dynbuf != NULL)
1635 free (dynbuf);
1636 if (dynver != NULL)
1637 free (dynver);
1638 if (extversym != NULL)
1639 free (extversym);
1640 return false;
1641 }
1642
1643 /* Create some sections which will be filled in with dynamic linking
1644 information. ABFD is an input file which requires dynamic sections
1645 to be created. The dynamic sections take up virtual memory space
1646 when the final executable is run, so we need to create them before
1647 addresses are assigned to the output sections. We work out the
1648 actual contents and size of these sections later. */
1649
1650 boolean
1651 elf_link_create_dynamic_sections (abfd, info)
1652 bfd *abfd;
1653 struct bfd_link_info *info;
1654 {
1655 flagword flags;
1656 register asection *s;
1657 struct elf_link_hash_entry *h;
1658 struct elf_backend_data *bed;
1659
1660 if (elf_hash_table (info)->dynamic_sections_created)
1661 return true;
1662
1663 /* Make sure that all dynamic sections use the same input BFD. */
1664 if (elf_hash_table (info)->dynobj == NULL)
1665 elf_hash_table (info)->dynobj = abfd;
1666 else
1667 abfd = elf_hash_table (info)->dynobj;
1668
1669 /* Note that we set the SEC_IN_MEMORY flag for all of these
1670 sections. */
1671 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1672 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1673
1674 /* A dynamically linked executable has a .interp section, but a
1675 shared library does not. */
1676 if (! info->shared)
1677 {
1678 s = bfd_make_section (abfd, ".interp");
1679 if (s == NULL
1680 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
1681 return false;
1682 }
1683
1684 /* Create sections to hold version informations. These are removed
1685 if they are not needed. */
1686 s = bfd_make_section (abfd, ".gnu.version_d");
1687 if (s == NULL
1688 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1689 || ! bfd_set_section_alignment (abfd, s, 2))
1690 return false;
1691
1692 s = bfd_make_section (abfd, ".gnu.version");
1693 if (s == NULL
1694 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1695 || ! bfd_set_section_alignment (abfd, s, 1))
1696 return false;
1697
1698 s = bfd_make_section (abfd, ".gnu.version_r");
1699 if (s == NULL
1700 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1701 || ! bfd_set_section_alignment (abfd, s, 2))
1702 return false;
1703
1704 s = bfd_make_section (abfd, ".dynsym");
1705 if (s == NULL
1706 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1707 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
1708 return false;
1709
1710 s = bfd_make_section (abfd, ".dynstr");
1711 if (s == NULL
1712 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
1713 return false;
1714
1715 /* Create a strtab to hold the dynamic symbol names. */
1716 if (elf_hash_table (info)->dynstr == NULL)
1717 {
1718 elf_hash_table (info)->dynstr = elf_stringtab_init ();
1719 if (elf_hash_table (info)->dynstr == NULL)
1720 return false;
1721 }
1722
1723 s = bfd_make_section (abfd, ".dynamic");
1724 if (s == NULL
1725 || ! bfd_set_section_flags (abfd, s, flags)
1726 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
1727 return false;
1728
1729 /* The special symbol _DYNAMIC is always set to the start of the
1730 .dynamic section. This call occurs before we have processed the
1731 symbols for any dynamic object, so we don't have to worry about
1732 overriding a dynamic definition. We could set _DYNAMIC in a
1733 linker script, but we only want to define it if we are, in fact,
1734 creating a .dynamic section. We don't want to define it if there
1735 is no .dynamic section, since on some ELF platforms the start up
1736 code examines it to decide how to initialize the process. */
1737 h = NULL;
1738 if (! (_bfd_generic_link_add_one_symbol
1739 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
1740 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
1741 (struct bfd_link_hash_entry **) &h)))
1742 return false;
1743 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1744 h->type = STT_OBJECT;
1745
1746 if (info->shared
1747 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1748 return false;
1749
1750 s = bfd_make_section (abfd, ".hash");
1751 if (s == NULL
1752 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1753 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
1754 return false;
1755
1756 /* Let the backend create the rest of the sections. This lets the
1757 backend set the right flags. The backend will normally create
1758 the .got and .plt sections. */
1759 bed = get_elf_backend_data (abfd);
1760 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
1761 return false;
1762
1763 elf_hash_table (info)->dynamic_sections_created = true;
1764
1765 return true;
1766 }
1767
1768 /* Add an entry to the .dynamic table. */
1769
1770 boolean
1771 elf_add_dynamic_entry (info, tag, val)
1772 struct bfd_link_info *info;
1773 bfd_vma tag;
1774 bfd_vma val;
1775 {
1776 Elf_Internal_Dyn dyn;
1777 bfd *dynobj;
1778 asection *s;
1779 size_t newsize;
1780 bfd_byte *newcontents;
1781
1782 dynobj = elf_hash_table (info)->dynobj;
1783
1784 s = bfd_get_section_by_name (dynobj, ".dynamic");
1785 BFD_ASSERT (s != NULL);
1786
1787 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
1788 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
1789 if (newcontents == NULL)
1790 return false;
1791
1792 dyn.d_tag = tag;
1793 dyn.d_un.d_val = val;
1794 elf_swap_dyn_out (dynobj, &dyn,
1795 (Elf_External_Dyn *) (newcontents + s->_raw_size));
1796
1797 s->_raw_size = newsize;
1798 s->contents = newcontents;
1799
1800 return true;
1801 }
1802 \f
1803
1804 /* Read and swap the relocs for a section. They may have been cached.
1805 If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are not NULL,
1806 they are used as buffers to read into. They are known to be large
1807 enough. If the INTERNAL_RELOCS relocs argument is NULL, the return
1808 value is allocated using either malloc or bfd_alloc, according to
1809 the KEEP_MEMORY argument. */
1810
1811 Elf_Internal_Rela *
1812 NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
1813 keep_memory)
1814 bfd *abfd;
1815 asection *o;
1816 PTR external_relocs;
1817 Elf_Internal_Rela *internal_relocs;
1818 boolean keep_memory;
1819 {
1820 Elf_Internal_Shdr *rel_hdr;
1821 PTR alloc1 = NULL;
1822 Elf_Internal_Rela *alloc2 = NULL;
1823
1824 if (elf_section_data (o)->relocs != NULL)
1825 return elf_section_data (o)->relocs;
1826
1827 if (o->reloc_count == 0)
1828 return NULL;
1829
1830 rel_hdr = &elf_section_data (o)->rel_hdr;
1831
1832 if (internal_relocs == NULL)
1833 {
1834 size_t size;
1835
1836 size = o->reloc_count * sizeof (Elf_Internal_Rela);
1837 if (keep_memory)
1838 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
1839 else
1840 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
1841 if (internal_relocs == NULL)
1842 goto error_return;
1843 }
1844
1845 if (external_relocs == NULL)
1846 {
1847 alloc1 = (PTR) bfd_malloc ((size_t) rel_hdr->sh_size);
1848 if (alloc1 == NULL)
1849 goto error_return;
1850 external_relocs = alloc1;
1851 }
1852
1853 if ((bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
1854 || (bfd_read (external_relocs, 1, rel_hdr->sh_size, abfd)
1855 != rel_hdr->sh_size))
1856 goto error_return;
1857
1858 /* Swap in the relocs. For convenience, we always produce an
1859 Elf_Internal_Rela array; if the relocs are Rel, we set the addend
1860 to 0. */
1861 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
1862 {
1863 Elf_External_Rel *erel;
1864 Elf_External_Rel *erelend;
1865 Elf_Internal_Rela *irela;
1866
1867 erel = (Elf_External_Rel *) external_relocs;
1868 erelend = erel + o->reloc_count;
1869 irela = internal_relocs;
1870 for (; erel < erelend; erel++, irela++)
1871 {
1872 Elf_Internal_Rel irel;
1873
1874 elf_swap_reloc_in (abfd, erel, &irel);
1875 irela->r_offset = irel.r_offset;
1876 irela->r_info = irel.r_info;
1877 irela->r_addend = 0;
1878 }
1879 }
1880 else
1881 {
1882 Elf_External_Rela *erela;
1883 Elf_External_Rela *erelaend;
1884 Elf_Internal_Rela *irela;
1885
1886 BFD_ASSERT (rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
1887
1888 erela = (Elf_External_Rela *) external_relocs;
1889 erelaend = erela + o->reloc_count;
1890 irela = internal_relocs;
1891 for (; erela < erelaend; erela++, irela++)
1892 elf_swap_reloca_in (abfd, erela, irela);
1893 }
1894
1895 /* Cache the results for next time, if we can. */
1896 if (keep_memory)
1897 elf_section_data (o)->relocs = internal_relocs;
1898
1899 if (alloc1 != NULL)
1900 free (alloc1);
1901
1902 /* Don't free alloc2, since if it was allocated we are passing it
1903 back (under the name of internal_relocs). */
1904
1905 return internal_relocs;
1906
1907 error_return:
1908 if (alloc1 != NULL)
1909 free (alloc1);
1910 if (alloc2 != NULL)
1911 free (alloc2);
1912 return NULL;
1913 }
1914 \f
1915
1916 /* Record an assignment to a symbol made by a linker script. We need
1917 this in case some dynamic object refers to this symbol. */
1918
1919 /*ARGSUSED*/
1920 boolean
1921 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
1922 bfd *output_bfd;
1923 struct bfd_link_info *info;
1924 const char *name;
1925 boolean provide;
1926 {
1927 struct elf_link_hash_entry *h;
1928
1929 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1930 return true;
1931
1932 h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
1933 if (h == NULL)
1934 return false;
1935
1936 if (h->root.type == bfd_link_hash_new)
1937 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
1938
1939 /* If this symbol is being provided by the linker script, and it is
1940 currently defined by a dynamic object, but not by a regular
1941 object, then mark it as undefined so that the generic linker will
1942 force the correct value. */
1943 if (provide
1944 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1945 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1946 h->root.type = bfd_link_hash_undefined;
1947
1948 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1949 h->type = STT_OBJECT;
1950
1951 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
1952 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
1953 || info->shared)
1954 && h->dynindx == -1)
1955 {
1956 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1957 return false;
1958
1959 /* If this is a weak defined symbol, and we know a corresponding
1960 real symbol from the same dynamic object, make sure the real
1961 symbol is also made into a dynamic symbol. */
1962 if (h->weakdef != NULL
1963 && h->weakdef->dynindx == -1)
1964 {
1965 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
1966 return false;
1967 }
1968 }
1969
1970 return true;
1971 }
1972 \f
1973 /* This structure is used to pass information to
1974 elf_link_assign_sym_version. */
1975
1976 struct elf_assign_sym_version_info
1977 {
1978 /* Output BFD. */
1979 bfd *output_bfd;
1980 /* General link information. */
1981 struct bfd_link_info *info;
1982 /* Version tree. */
1983 struct bfd_elf_version_tree *verdefs;
1984 /* Whether we are exporting all dynamic symbols. */
1985 boolean export_dynamic;
1986 /* Whether we removed any symbols from the dynamic symbol table. */
1987 boolean removed_dynamic;
1988 /* Whether we had a failure. */
1989 boolean failed;
1990 };
1991
1992 /* This structure is used to pass information to
1993 elf_link_find_version_dependencies. */
1994
1995 struct elf_find_verdep_info
1996 {
1997 /* Output BFD. */
1998 bfd *output_bfd;
1999 /* General link information. */
2000 struct bfd_link_info *info;
2001 /* The number of dependencies. */
2002 unsigned int vers;
2003 /* Whether we had a failure. */
2004 boolean failed;
2005 };
2006
2007 /* Array used to determine the number of hash table buckets to use
2008 based on the number of symbols there are. If there are fewer than
2009 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2010 fewer than 37 we use 17 buckets, and so forth. We never use more
2011 than 32771 buckets. */
2012
2013 static const size_t elf_buckets[] =
2014 {
2015 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2016 16411, 32771, 0
2017 };
2018
2019 /* Set up the sizes and contents of the ELF dynamic sections. This is
2020 called by the ELF linker emulation before_allocation routine. We
2021 must set the sizes of the sections before the linker sets the
2022 addresses of the various sections. */
2023
2024 boolean
2025 NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
2026 export_dynamic, filter_shlib,
2027 auxiliary_filters, info, sinterpptr,
2028 verdefs)
2029 bfd *output_bfd;
2030 const char *soname;
2031 const char *rpath;
2032 boolean export_dynamic;
2033 const char *filter_shlib;
2034 const char * const *auxiliary_filters;
2035 struct bfd_link_info *info;
2036 asection **sinterpptr;
2037 struct bfd_elf_version_tree *verdefs;
2038 {
2039 bfd_size_type soname_indx;
2040 bfd *dynobj;
2041 struct elf_backend_data *bed;
2042 bfd_size_type old_dynsymcount;
2043
2044 *sinterpptr = NULL;
2045
2046 soname_indx = -1;
2047
2048 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2049 return true;
2050
2051 /* The backend may have to create some sections regardless of whether
2052 we're dynamic or not. */
2053 bed = get_elf_backend_data (output_bfd);
2054 if (bed->elf_backend_always_size_sections
2055 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
2056 return false;
2057
2058 dynobj = elf_hash_table (info)->dynobj;
2059
2060 /* If there were no dynamic objects in the link, there is nothing to
2061 do here. */
2062 if (dynobj == NULL)
2063 return true;
2064
2065 /* If we are supposed to export all symbols into the dynamic symbol
2066 table (this is not the normal case), then do so. */
2067 if (export_dynamic)
2068 {
2069 struct elf_info_failed eif;
2070
2071 eif.failed = false;
2072 eif.info = info;
2073 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
2074 (PTR) &eif);
2075 if (eif.failed)
2076 return false;
2077 }
2078
2079 if (elf_hash_table (info)->dynamic_sections_created)
2080 {
2081 struct elf_info_failed eif;
2082 struct elf_link_hash_entry *h;
2083 bfd_size_type strsize;
2084
2085 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
2086 BFD_ASSERT (*sinterpptr != NULL || info->shared);
2087
2088 if (soname != NULL)
2089 {
2090 soname_indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2091 soname, true, true);
2092 if (soname_indx == (bfd_size_type) -1
2093 || ! elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
2094 return false;
2095 }
2096
2097 if (info->symbolic)
2098 {
2099 if (! elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
2100 return false;
2101 }
2102
2103 if (rpath != NULL)
2104 {
2105 bfd_size_type indx;
2106
2107 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, rpath,
2108 true, true);
2109 if (indx == (bfd_size_type) -1
2110 || ! elf_add_dynamic_entry (info, DT_RPATH, indx))
2111 return false;
2112 }
2113
2114 if (filter_shlib != NULL)
2115 {
2116 bfd_size_type indx;
2117
2118 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2119 filter_shlib, true, true);
2120 if (indx == (bfd_size_type) -1
2121 || ! elf_add_dynamic_entry (info, DT_FILTER, indx))
2122 return false;
2123 }
2124
2125 if (auxiliary_filters != NULL)
2126 {
2127 const char * const *p;
2128
2129 for (p = auxiliary_filters; *p != NULL; p++)
2130 {
2131 bfd_size_type indx;
2132
2133 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2134 *p, true, true);
2135 if (indx == (bfd_size_type) -1
2136 || ! elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
2137 return false;
2138 }
2139 }
2140
2141 /* Find all symbols which were defined in a dynamic object and make
2142 the backend pick a reasonable value for them. */
2143 eif.failed = false;
2144 eif.info = info;
2145 elf_link_hash_traverse (elf_hash_table (info),
2146 elf_adjust_dynamic_symbol,
2147 (PTR) &eif);
2148 if (eif.failed)
2149 return false;
2150
2151 /* Add some entries to the .dynamic section. We fill in some of the
2152 values later, in elf_bfd_final_link, but we must add the entries
2153 now so that we know the final size of the .dynamic section. */
2154 h = elf_link_hash_lookup (elf_hash_table (info), "_init", false,
2155 false, false);
2156 if (h != NULL
2157 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
2158 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
2159 {
2160 if (! elf_add_dynamic_entry (info, DT_INIT, 0))
2161 return false;
2162 }
2163 h = elf_link_hash_lookup (elf_hash_table (info), "_fini", false,
2164 false, false);
2165 if (h != NULL
2166 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
2167 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
2168 {
2169 if (! elf_add_dynamic_entry (info, DT_FINI, 0))
2170 return false;
2171 }
2172 strsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
2173 if (! elf_add_dynamic_entry (info, DT_HASH, 0)
2174 || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
2175 || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0)
2176 || ! elf_add_dynamic_entry (info, DT_STRSZ, strsize)
2177 || ! elf_add_dynamic_entry (info, DT_SYMENT,
2178 sizeof (Elf_External_Sym)))
2179 return false;
2180 }
2181
2182 /* The backend must work out the sizes of all the other dynamic
2183 sections. */
2184 old_dynsymcount = elf_hash_table (info)->dynsymcount;
2185 if (! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
2186 return false;
2187
2188 if (elf_hash_table (info)->dynamic_sections_created)
2189 {
2190 size_t dynsymcount;
2191 asection *s;
2192 size_t i;
2193 size_t bucketcount = 0;
2194 Elf_Internal_Sym isym;
2195 struct elf_assign_sym_version_info sinfo;
2196
2197 /* Set up the version definition section. */
2198 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2199 BFD_ASSERT (s != NULL);
2200
2201 /* Attach all the symbols to their version information. This
2202 may cause some symbols to be unexported. */
2203 sinfo.output_bfd = output_bfd;
2204 sinfo.info = info;
2205 sinfo.verdefs = verdefs;
2206 sinfo.export_dynamic = export_dynamic;
2207 sinfo.removed_dynamic = false;
2208 sinfo.failed = false;
2209
2210 elf_link_hash_traverse (elf_hash_table (info),
2211 elf_link_assign_sym_version,
2212 (PTR) &sinfo);
2213 if (sinfo.failed)
2214 return false;
2215
2216 /* We may have created additional version definitions if we are
2217 just linking a regular application. */
2218 verdefs = sinfo.verdefs;
2219
2220 if (verdefs == NULL)
2221 {
2222 asection **spp;
2223
2224 /* Don't include this section in the output file. */
2225 for (spp = &output_bfd->sections;
2226 *spp != s->output_section;
2227 spp = &(*spp)->next)
2228 ;
2229 *spp = s->output_section->next;
2230 --output_bfd->section_count;
2231 }
2232 else
2233 {
2234 unsigned int cdefs;
2235 bfd_size_type size;
2236 struct bfd_elf_version_tree *t;
2237 bfd_byte *p;
2238 Elf_Internal_Verdef def;
2239 Elf_Internal_Verdaux defaux;
2240
2241 if (sinfo.removed_dynamic)
2242 {
2243 /* Some dynamic symbols were changed to be local
2244 symbols. In this case, we renumber all of the
2245 dynamic symbols, so that we don't have a hole. If
2246 the backend changed dynsymcount, then assume that the
2247 new symbols are at the start. This is the case on
2248 the MIPS. FIXME: The names of the removed symbols
2249 will still be in the dynamic string table, wasting
2250 space. */
2251 elf_hash_table (info)->dynsymcount =
2252 1 + (elf_hash_table (info)->dynsymcount - old_dynsymcount);
2253 elf_link_hash_traverse (elf_hash_table (info),
2254 elf_link_renumber_dynsyms,
2255 (PTR) info);
2256 }
2257
2258 cdefs = 0;
2259 size = 0;
2260
2261 /* Make space for the base version. */
2262 size += sizeof (Elf_External_Verdef);
2263 size += sizeof (Elf_External_Verdaux);
2264 ++cdefs;
2265
2266 for (t = verdefs; t != NULL; t = t->next)
2267 {
2268 struct bfd_elf_version_deps *n;
2269
2270 size += sizeof (Elf_External_Verdef);
2271 size += sizeof (Elf_External_Verdaux);
2272 ++cdefs;
2273
2274 for (n = t->deps; n != NULL; n = n->next)
2275 size += sizeof (Elf_External_Verdaux);
2276 }
2277
2278 s->_raw_size = size;
2279 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
2280 if (s->contents == NULL && s->_raw_size != 0)
2281 return false;
2282
2283 /* Fill in the version definition section. */
2284
2285 p = s->contents;
2286
2287 def.vd_version = VER_DEF_CURRENT;
2288 def.vd_flags = VER_FLG_BASE;
2289 def.vd_ndx = 1;
2290 def.vd_cnt = 1;
2291 def.vd_aux = sizeof (Elf_External_Verdef);
2292 def.vd_next = (sizeof (Elf_External_Verdef)
2293 + sizeof (Elf_External_Verdaux));
2294
2295 if (soname_indx != -1)
2296 {
2297 def.vd_hash = bfd_elf_hash ((const unsigned char *) soname);
2298 defaux.vda_name = soname_indx;
2299 }
2300 else
2301 {
2302 const char *name;
2303 bfd_size_type indx;
2304
2305 name = output_bfd->filename;
2306 def.vd_hash = bfd_elf_hash ((const unsigned char *) name);
2307 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2308 name, true, false);
2309 if (indx == (bfd_size_type) -1)
2310 return false;
2311 defaux.vda_name = indx;
2312 }
2313 defaux.vda_next = 0;
2314
2315 _bfd_elf_swap_verdef_out (output_bfd, &def,
2316 (Elf_External_Verdef *)p);
2317 p += sizeof (Elf_External_Verdef);
2318 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2319 (Elf_External_Verdaux *) p);
2320 p += sizeof (Elf_External_Verdaux);
2321
2322 for (t = verdefs; t != NULL; t = t->next)
2323 {
2324 unsigned int cdeps;
2325 struct bfd_elf_version_deps *n;
2326 struct elf_link_hash_entry *h;
2327
2328 cdeps = 0;
2329 for (n = t->deps; n != NULL; n = n->next)
2330 ++cdeps;
2331
2332 /* Add a symbol representing this version. */
2333 h = NULL;
2334 if (! (_bfd_generic_link_add_one_symbol
2335 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
2336 (bfd_vma) 0, (const char *) NULL, false,
2337 get_elf_backend_data (dynobj)->collect,
2338 (struct bfd_link_hash_entry **) &h)))
2339 return false;
2340 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
2341 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2342 h->type = STT_OBJECT;
2343 h->verinfo.vertree = t;
2344
2345 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2346 return false;
2347
2348 def.vd_version = VER_DEF_CURRENT;
2349 def.vd_flags = 0;
2350 if (t->globals == NULL && t->locals == NULL && ! t->used)
2351 def.vd_flags |= VER_FLG_WEAK;
2352 def.vd_ndx = t->vernum + 1;
2353 def.vd_cnt = cdeps + 1;
2354 def.vd_hash = bfd_elf_hash ((const unsigned char *) t->name);
2355 def.vd_aux = sizeof (Elf_External_Verdef);
2356 if (t->next != NULL)
2357 def.vd_next = (sizeof (Elf_External_Verdef)
2358 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
2359 else
2360 def.vd_next = 0;
2361
2362 _bfd_elf_swap_verdef_out (output_bfd, &def,
2363 (Elf_External_Verdef *) p);
2364 p += sizeof (Elf_External_Verdef);
2365
2366 defaux.vda_name = h->dynstr_index;
2367 if (t->deps == NULL)
2368 defaux.vda_next = 0;
2369 else
2370 defaux.vda_next = sizeof (Elf_External_Verdaux);
2371 t->name_indx = defaux.vda_name;
2372
2373 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2374 (Elf_External_Verdaux *) p);
2375 p += sizeof (Elf_External_Verdaux);
2376
2377 for (n = t->deps; n != NULL; n = n->next)
2378 {
2379 defaux.vda_name = n->version_needed->name_indx;
2380 if (n->next == NULL)
2381 defaux.vda_next = 0;
2382 else
2383 defaux.vda_next = sizeof (Elf_External_Verdaux);
2384
2385 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2386 (Elf_External_Verdaux *) p);
2387 p += sizeof (Elf_External_Verdaux);
2388 }
2389 }
2390
2391 if (! elf_add_dynamic_entry (info, DT_VERDEF, 0)
2392 || ! elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
2393 return false;
2394
2395 elf_tdata (output_bfd)->cverdefs = cdefs;
2396 }
2397
2398 /* Work out the size of the version reference section. */
2399
2400 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2401 BFD_ASSERT (s != NULL);
2402 {
2403 struct elf_find_verdep_info sinfo;
2404
2405 sinfo.output_bfd = output_bfd;
2406 sinfo.info = info;
2407 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
2408 if (sinfo.vers == 0)
2409 sinfo.vers = 1;
2410 sinfo.failed = false;
2411
2412 elf_link_hash_traverse (elf_hash_table (info),
2413 elf_link_find_version_dependencies,
2414 (PTR) &sinfo);
2415
2416 if (elf_tdata (output_bfd)->verref == NULL)
2417 {
2418 asection **spp;
2419
2420 /* We don't have any version definitions, so we can just
2421 remove the section. */
2422
2423 for (spp = &output_bfd->sections;
2424 *spp != s->output_section;
2425 spp = &(*spp)->next)
2426 ;
2427 *spp = s->output_section->next;
2428 --output_bfd->section_count;
2429 }
2430 else
2431 {
2432 Elf_Internal_Verneed *t;
2433 unsigned int size;
2434 unsigned int crefs;
2435 bfd_byte *p;
2436
2437 /* Build the version definition section. */
2438 size = 0;
2439 crefs = 0;
2440 for (t = elf_tdata (output_bfd)->verref;
2441 t != NULL;
2442 t = t->vn_nextref)
2443 {
2444 Elf_Internal_Vernaux *a;
2445
2446 size += sizeof (Elf_External_Verneed);
2447 ++crefs;
2448 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2449 size += sizeof (Elf_External_Vernaux);
2450 }
2451
2452 s->_raw_size = size;
2453 s->contents = (bfd_byte *) bfd_alloc (output_bfd, size);
2454 if (s->contents == NULL)
2455 return false;
2456
2457 p = s->contents;
2458 for (t = elf_tdata (output_bfd)->verref;
2459 t != NULL;
2460 t = t->vn_nextref)
2461 {
2462 unsigned int caux;
2463 Elf_Internal_Vernaux *a;
2464 bfd_size_type indx;
2465
2466 caux = 0;
2467 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2468 ++caux;
2469
2470 t->vn_version = VER_NEED_CURRENT;
2471 t->vn_cnt = caux;
2472 if (elf_dt_name (t->vn_bfd) != NULL)
2473 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2474 elf_dt_name (t->vn_bfd),
2475 true, false);
2476 else
2477 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2478 t->vn_bfd->filename, true, false);
2479 if (indx == (bfd_size_type) -1)
2480 return false;
2481 t->vn_file = indx;
2482 t->vn_aux = sizeof (Elf_External_Verneed);
2483 if (t->vn_nextref == NULL)
2484 t->vn_next = 0;
2485 else
2486 t->vn_next = (sizeof (Elf_External_Verneed)
2487 + caux * sizeof (Elf_External_Vernaux));
2488
2489 _bfd_elf_swap_verneed_out (output_bfd, t,
2490 (Elf_External_Verneed *) p);
2491 p += sizeof (Elf_External_Verneed);
2492
2493 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2494 {
2495 a->vna_hash = bfd_elf_hash ((const unsigned char *)
2496 a->vna_nodename);
2497 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2498 a->vna_nodename, true, false);
2499 if (indx == (bfd_size_type) -1)
2500 return false;
2501 a->vna_name = indx;
2502 if (a->vna_nextptr == NULL)
2503 a->vna_next = 0;
2504 else
2505 a->vna_next = sizeof (Elf_External_Vernaux);
2506
2507 _bfd_elf_swap_vernaux_out (output_bfd, a,
2508 (Elf_External_Vernaux *) p);
2509 p += sizeof (Elf_External_Vernaux);
2510 }
2511 }
2512
2513 if (! elf_add_dynamic_entry (info, DT_VERNEED, 0)
2514 || ! elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
2515 return false;
2516
2517 elf_tdata (output_bfd)->cverrefs = crefs;
2518 }
2519 }
2520
2521 dynsymcount = elf_hash_table (info)->dynsymcount;
2522
2523 /* Work out the size of the symbol version section. */
2524 s = bfd_get_section_by_name (dynobj, ".gnu.version");
2525 BFD_ASSERT (s != NULL);
2526 if (dynsymcount == 0
2527 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
2528 {
2529 asection **spp;
2530
2531 /* We don't need any symbol versions; just discard the
2532 section. */
2533 for (spp = &output_bfd->sections;
2534 *spp != s->output_section;
2535 spp = &(*spp)->next)
2536 ;
2537 *spp = s->output_section->next;
2538 --output_bfd->section_count;
2539 }
2540 else
2541 {
2542 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
2543 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
2544 if (s->contents == NULL)
2545 return false;
2546
2547 if (! elf_add_dynamic_entry (info, DT_VERSYM, 0))
2548 return false;
2549 }
2550
2551 /* Set the size of the .dynsym and .hash sections. We counted
2552 the number of dynamic symbols in elf_link_add_object_symbols.
2553 We will build the contents of .dynsym and .hash when we build
2554 the final symbol table, because until then we do not know the
2555 correct value to give the symbols. We built the .dynstr
2556 section as we went along in elf_link_add_object_symbols. */
2557 s = bfd_get_section_by_name (dynobj, ".dynsym");
2558 BFD_ASSERT (s != NULL);
2559 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
2560 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
2561 if (s->contents == NULL && s->_raw_size != 0)
2562 return false;
2563
2564 /* The first entry in .dynsym is a dummy symbol. */
2565 isym.st_value = 0;
2566 isym.st_size = 0;
2567 isym.st_name = 0;
2568 isym.st_info = 0;
2569 isym.st_other = 0;
2570 isym.st_shndx = 0;
2571 elf_swap_symbol_out (output_bfd, &isym,
2572 (PTR) (Elf_External_Sym *) s->contents);
2573
2574 for (i = 0; elf_buckets[i] != 0; i++)
2575 {
2576 bucketcount = elf_buckets[i];
2577 if (dynsymcount < elf_buckets[i + 1])
2578 break;
2579 }
2580
2581 s = bfd_get_section_by_name (dynobj, ".hash");
2582 BFD_ASSERT (s != NULL);
2583 s->_raw_size = (2 + bucketcount + dynsymcount) * (ARCH_SIZE / 8);
2584 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
2585 if (s->contents == NULL)
2586 return false;
2587 memset (s->contents, 0, (size_t) s->_raw_size);
2588
2589 put_word (output_bfd, bucketcount, s->contents);
2590 put_word (output_bfd, dynsymcount, s->contents + (ARCH_SIZE / 8));
2591
2592 elf_hash_table (info)->bucketcount = bucketcount;
2593
2594 s = bfd_get_section_by_name (dynobj, ".dynstr");
2595 BFD_ASSERT (s != NULL);
2596 s->_raw_size = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
2597
2598 if (! elf_add_dynamic_entry (info, DT_NULL, 0))
2599 return false;
2600 }
2601
2602 return true;
2603 }
2604 \f
2605 /* Make the backend pick a good value for a dynamic symbol. This is
2606 called via elf_link_hash_traverse, and also calls itself
2607 recursively. */
2608
2609 static boolean
2610 elf_adjust_dynamic_symbol (h, data)
2611 struct elf_link_hash_entry *h;
2612 PTR data;
2613 {
2614 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2615 bfd *dynobj;
2616 struct elf_backend_data *bed;
2617
2618 /* Ignore indirect symbols. These are added by the versioning code. */
2619 if (h->root.type == bfd_link_hash_indirect)
2620 return true;
2621
2622 /* If this symbol was mentioned in a non-ELF file, try to set
2623 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2624 permit a non-ELF file to correctly refer to a symbol defined in
2625 an ELF dynamic object. */
2626 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
2627 {
2628 if (h->root.type != bfd_link_hash_defined
2629 && h->root.type != bfd_link_hash_defweak)
2630 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2631 else
2632 {
2633 if (h->root.u.def.section->owner != NULL
2634 && (bfd_get_flavour (h->root.u.def.section->owner)
2635 == bfd_target_elf_flavour))
2636 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2637 else
2638 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2639 }
2640
2641 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2642 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
2643 {
2644 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
2645 {
2646 eif->failed = true;
2647 return false;
2648 }
2649 }
2650 }
2651
2652 /* If this is a final link, and the symbol was defined as a common
2653 symbol in a regular object file, and there was no definition in
2654 any dynamic object, then the linker will have allocated space for
2655 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2656 flag will not have been set. */
2657 if (h->root.type == bfd_link_hash_defined
2658 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2659 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
2660 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2661 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2662 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2663
2664 /* If -Bsymbolic was used (which means to bind references to global
2665 symbols to the definition within the shared object), and this
2666 symbol was defined in a regular object, then it actually doesn't
2667 need a PLT entry. */
2668 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2669 && eif->info->shared
2670 && eif->info->symbolic
2671 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2672 h->elf_link_hash_flags &=~ ELF_LINK_HASH_NEEDS_PLT;
2673
2674 /* If this symbol does not require a PLT entry, and it is not
2675 defined by a dynamic object, or is not referenced by a regular
2676 object, ignore it. We do have to handle a weak defined symbol,
2677 even if no regular object refers to it, if we decided to add it
2678 to the dynamic symbol table. FIXME: Do we normally need to worry
2679 about symbols which are defined by one dynamic object and
2680 referenced by another one? */
2681 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
2682 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2683 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2684 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
2685 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
2686 return true;
2687
2688 /* If we've already adjusted this symbol, don't do it again. This
2689 can happen via a recursive call. */
2690 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
2691 return true;
2692
2693 /* Don't look at this symbol again. Note that we must set this
2694 after checking the above conditions, because we may look at a
2695 symbol once, decide not to do anything, and then get called
2696 recursively later after REF_REGULAR is set below. */
2697 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
2698
2699 /* If this is a weak definition, and we know a real definition, and
2700 the real symbol is not itself defined by a regular object file,
2701 then get a good value for the real definition. We handle the
2702 real symbol first, for the convenience of the backend routine.
2703
2704 Note that there is a confusing case here. If the real definition
2705 is defined by a regular object file, we don't get the real symbol
2706 from the dynamic object, but we do get the weak symbol. If the
2707 processor backend uses a COPY reloc, then if some routine in the
2708 dynamic object changes the real symbol, we will not see that
2709 change in the corresponding weak symbol. This is the way other
2710 ELF linkers work as well, and seems to be a result of the shared
2711 library model.
2712
2713 I will clarify this issue. Most SVR4 shared libraries define the
2714 variable _timezone and define timezone as a weak synonym. The
2715 tzset call changes _timezone. If you write
2716 extern int timezone;
2717 int _timezone = 5;
2718 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2719 you might expect that, since timezone is a synonym for _timezone,
2720 the same number will print both times. However, if the processor
2721 backend uses a COPY reloc, then actually timezone will be copied
2722 into your process image, and, since you define _timezone
2723 yourself, _timezone will not. Thus timezone and _timezone will
2724 wind up at different memory locations. The tzset call will set
2725 _timezone, leaving timezone unchanged. */
2726
2727 if (h->weakdef != NULL)
2728 {
2729 struct elf_link_hash_entry *weakdef;
2730
2731 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2732 || h->root.type == bfd_link_hash_defweak);
2733 weakdef = h->weakdef;
2734 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2735 || weakdef->root.type == bfd_link_hash_defweak);
2736 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
2737 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2738 {
2739 /* This symbol is defined by a regular object file, so we
2740 will not do anything special. Clear weakdef for the
2741 convenience of the processor backend. */
2742 h->weakdef = NULL;
2743 }
2744 else
2745 {
2746 /* There is an implicit reference by a regular object file
2747 via the weak symbol. */
2748 weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2749 if (! elf_adjust_dynamic_symbol (weakdef, (PTR) eif))
2750 return false;
2751 }
2752 }
2753
2754 dynobj = elf_hash_table (eif->info)->dynobj;
2755 bed = get_elf_backend_data (dynobj);
2756 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2757 {
2758 eif->failed = true;
2759 return false;
2760 }
2761
2762 return true;
2763 }
2764 \f
2765 /* This routine is used to export all defined symbols into the dynamic
2766 symbol table. It is called via elf_link_hash_traverse. */
2767
2768 static boolean
2769 elf_export_symbol (h, data)
2770 struct elf_link_hash_entry *h;
2771 PTR data;
2772 {
2773 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2774
2775 /* Ignore indirect symbols. These are added by the versioning code. */
2776 if (h->root.type == bfd_link_hash_indirect)
2777 return true;
2778
2779 if (h->dynindx == -1
2780 && (h->elf_link_hash_flags
2781 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
2782 {
2783 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
2784 {
2785 eif->failed = true;
2786 return false;
2787 }
2788 }
2789
2790 return true;
2791 }
2792 \f
2793 /* Look through the symbols which are defined in other shared
2794 libraries and referenced here. Update the list of version
2795 dependencies. This will be put into the .gnu.version_r section.
2796 This function is called via elf_link_hash_traverse. */
2797
2798 static boolean
2799 elf_link_find_version_dependencies (h, data)
2800 struct elf_link_hash_entry *h;
2801 PTR data;
2802 {
2803 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2804 Elf_Internal_Verneed *t;
2805 Elf_Internal_Vernaux *a;
2806
2807 /* We only care about symbols defined in shared objects with version
2808 information. */
2809 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2810 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2811 || h->dynindx == -1
2812 || h->verinfo.verdef == NULL)
2813 return true;
2814
2815 /* See if we already know about this version. */
2816 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
2817 {
2818 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2819 continue;
2820
2821 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2822 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2823 return true;
2824
2825 break;
2826 }
2827
2828 /* This is a new version. Add it to tree we are building. */
2829
2830 if (t == NULL)
2831 {
2832 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, sizeof *t);
2833 if (t == NULL)
2834 {
2835 rinfo->failed = true;
2836 return false;
2837 }
2838
2839 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2840 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
2841 elf_tdata (rinfo->output_bfd)->verref = t;
2842 }
2843
2844 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, sizeof *a);
2845
2846 /* Note that we are copying a string pointer here, and testing it
2847 above. If bfd_elf_string_from_elf_section is ever changed to
2848 discard the string data when low in memory, this will have to be
2849 fixed. */
2850 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2851
2852 a->vna_flags = h->verinfo.verdef->vd_flags;
2853 a->vna_nextptr = t->vn_auxptr;
2854
2855 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2856 ++rinfo->vers;
2857
2858 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2859
2860 t->vn_auxptr = a;
2861
2862 return true;
2863 }
2864
2865 /* Figure out appropriate versions for all the symbols. We may not
2866 have the version number script until we have read all of the input
2867 files, so until that point we don't know which symbols should be
2868 local. This function is called via elf_link_hash_traverse. */
2869
2870 static boolean
2871 elf_link_assign_sym_version (h, data)
2872 struct elf_link_hash_entry *h;
2873 PTR data;
2874 {
2875 struct elf_assign_sym_version_info *sinfo =
2876 (struct elf_assign_sym_version_info *) data;
2877 struct bfd_link_info *info = sinfo->info;
2878 char *p;
2879
2880 /* We only need version numbers for symbols defined in regular
2881 objects. */
2882 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2883 return true;
2884
2885 p = strchr (h->root.root.string, ELF_VER_CHR);
2886 if (p != NULL && h->verinfo.vertree == NULL)
2887 {
2888 struct bfd_elf_version_tree *t;
2889 boolean hidden;
2890
2891 hidden = true;
2892
2893 /* There are two consecutive ELF_VER_CHR characters if this is
2894 not a hidden symbol. */
2895 ++p;
2896 if (*p == ELF_VER_CHR)
2897 {
2898 hidden = false;
2899 ++p;
2900 }
2901
2902 /* If there is no version string, we can just return out. */
2903 if (*p == '\0')
2904 {
2905 if (hidden)
2906 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
2907 return true;
2908 }
2909
2910 /* Look for the version. If we find it, it is no longer weak. */
2911 for (t = sinfo->verdefs; t != NULL; t = t->next)
2912 {
2913 if (strcmp (t->name, p) == 0)
2914 {
2915 h->verinfo.vertree = t;
2916 t->used = true;
2917
2918 /* See if there is anything to force this symbol to
2919 local scope. */
2920 if (t->locals != NULL)
2921 {
2922 int len;
2923 char *alc;
2924 struct bfd_elf_version_expr *d;
2925
2926 len = p - h->root.root.string;
2927 alc = bfd_alloc (sinfo->output_bfd, len);
2928 if (alc == NULL)
2929 return false;
2930 strncpy (alc, h->root.root.string, len - 1);
2931 alc[len - 1] = '\0';
2932 if (alc[len - 2] == ELF_VER_CHR)
2933 alc[len - 2] = '\0';
2934
2935 for (d = t->locals; d != NULL; d = d->next)
2936 {
2937 if ((d->match[0] == '*' && d->match[1] == '\0')
2938 || fnmatch (d->match, alc, 0) == 0)
2939 {
2940 if (h->dynindx != -1
2941 && info->shared
2942 && ! sinfo->export_dynamic
2943 && (h->elf_link_hash_flags
2944 & ELF_LINK_HASH_NEEDS_PLT) == 0)
2945 {
2946 sinfo->removed_dynamic = true;
2947 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
2948 h->dynindx = -1;
2949 /* FIXME: The name of the symbol has
2950 already been recorded in the dynamic
2951 string table section. */
2952 }
2953
2954 break;
2955 }
2956 }
2957
2958 bfd_release (sinfo->output_bfd, alc);
2959 }
2960
2961 break;
2962 }
2963 }
2964
2965 /* If we are building an application, we need to create a
2966 version node for this version. */
2967 if (t == NULL && ! info->shared)
2968 {
2969 struct bfd_elf_version_tree **pp;
2970 int version_index;
2971
2972 /* If we aren't going to export this symbol, we don't need
2973 to worry about it. */
2974 if (h->dynindx == -1)
2975 return true;
2976
2977 t = ((struct bfd_elf_version_tree *)
2978 bfd_alloc (sinfo->output_bfd, sizeof *t));
2979 if (t == NULL)
2980 {
2981 sinfo->failed = true;
2982 return false;
2983 }
2984
2985 t->next = NULL;
2986 t->name = p;
2987 t->globals = NULL;
2988 t->locals = NULL;
2989 t->deps = NULL;
2990 t->name_indx = (unsigned int) -1;
2991 t->used = true;
2992
2993 version_index = 1;
2994 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
2995 ++version_index;
2996 t->vernum = version_index;
2997
2998 *pp = t;
2999
3000 h->verinfo.vertree = t;
3001 }
3002 else if (t == NULL)
3003 {
3004 /* We could not find the version for a symbol when
3005 generating a shared archive. Return an error. */
3006 (*_bfd_error_handler)
3007 ("%s: undefined version name %s",
3008 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
3009 bfd_set_error (bfd_error_bad_value);
3010 sinfo->failed = true;
3011 return false;
3012 }
3013
3014 if (hidden)
3015 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
3016 }
3017
3018 /* If we don't have a version for this symbol, see if we can find
3019 something. */
3020 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
3021 {
3022 struct bfd_elf_version_tree *t;
3023 struct bfd_elf_version_tree *deflt;
3024 struct bfd_elf_version_expr *d;
3025
3026 /* See if can find what version this symbol is in. If the
3027 symbol is supposed to eb local, then don't actually register
3028 it. */
3029 deflt = NULL;
3030 for (t = sinfo->verdefs; t != NULL; t = t->next)
3031 {
3032 if (t->globals != NULL)
3033 {
3034 for (d = t->globals; d != NULL; d = d->next)
3035 {
3036 if (fnmatch (d->match, h->root.root.string, 0) == 0)
3037 {
3038 h->verinfo.vertree = t;
3039 break;
3040 }
3041 }
3042
3043 if (d != NULL)
3044 break;
3045 }
3046
3047 if (t->locals != NULL)
3048 {
3049 for (d = t->locals; d != NULL; d = d->next)
3050 {
3051 if (d->match[0] == '*' && d->match[1] == '\0')
3052 deflt = t;
3053 else if (fnmatch (d->match, h->root.root.string, 0) == 0)
3054 {
3055 h->verinfo.vertree = t;
3056 if (h->dynindx != -1
3057 && info->shared
3058 && ! sinfo->export_dynamic
3059 && (h->elf_link_hash_flags
3060 & ELF_LINK_HASH_NEEDS_PLT) == 0)
3061 {
3062 sinfo->removed_dynamic = true;
3063 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
3064 h->dynindx = -1;
3065 /* FIXME: The name of the symbol has already
3066 been recorded in the dynamic string table
3067 section. */
3068 }
3069 break;
3070 }
3071 }
3072
3073 if (d != NULL)
3074 break;
3075 }
3076 }
3077
3078 if (deflt != NULL && h->verinfo.vertree == NULL)
3079 {
3080 h->verinfo.vertree = deflt;
3081 if (h->dynindx != -1
3082 && info->shared
3083 && ! sinfo->export_dynamic
3084 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
3085 {
3086 sinfo->removed_dynamic = true;
3087 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
3088 h->dynindx = -1;
3089 /* FIXME: The name of the symbol has already been
3090 recorded in the dynamic string table section. */
3091 }
3092 }
3093 }
3094
3095 return true;
3096 }
3097
3098 /* This function is used to renumber the dynamic symbols, if some of
3099 them are removed because they are marked as local. This is called
3100 via elf_link_hash_traverse. */
3101
3102 static boolean
3103 elf_link_renumber_dynsyms (h, data)
3104 struct elf_link_hash_entry *h;
3105 PTR data;
3106 {
3107 struct bfd_link_info *info = (struct bfd_link_info *) data;
3108
3109 if (h->dynindx != -1)
3110 {
3111 h->dynindx = elf_hash_table (info)->dynsymcount;
3112 ++elf_hash_table (info)->dynsymcount;
3113 }
3114
3115 return true;
3116 }
3117 \f
3118 /* Final phase of ELF linker. */
3119
3120 /* A structure we use to avoid passing large numbers of arguments. */
3121
3122 struct elf_final_link_info
3123 {
3124 /* General link information. */
3125 struct bfd_link_info *info;
3126 /* Output BFD. */
3127 bfd *output_bfd;
3128 /* Symbol string table. */
3129 struct bfd_strtab_hash *symstrtab;
3130 /* .dynsym section. */
3131 asection *dynsym_sec;
3132 /* .hash section. */
3133 asection *hash_sec;
3134 /* symbol version section (.gnu.version). */
3135 asection *symver_sec;
3136 /* Buffer large enough to hold contents of any section. */
3137 bfd_byte *contents;
3138 /* Buffer large enough to hold external relocs of any section. */
3139 PTR external_relocs;
3140 /* Buffer large enough to hold internal relocs of any section. */
3141 Elf_Internal_Rela *internal_relocs;
3142 /* Buffer large enough to hold external local symbols of any input
3143 BFD. */
3144 Elf_External_Sym *external_syms;
3145 /* Buffer large enough to hold internal local symbols of any input
3146 BFD. */
3147 Elf_Internal_Sym *internal_syms;
3148 /* Array large enough to hold a symbol index for each local symbol
3149 of any input BFD. */
3150 long *indices;
3151 /* Array large enough to hold a section pointer for each local
3152 symbol of any input BFD. */
3153 asection **sections;
3154 /* Buffer to hold swapped out symbols. */
3155 Elf_External_Sym *symbuf;
3156 /* Number of swapped out symbols in buffer. */
3157 size_t symbuf_count;
3158 /* Number of symbols which fit in symbuf. */
3159 size_t symbuf_size;
3160 };
3161
3162 static boolean elf_link_output_sym
3163 PARAMS ((struct elf_final_link_info *, const char *,
3164 Elf_Internal_Sym *, asection *));
3165 static boolean elf_link_flush_output_syms
3166 PARAMS ((struct elf_final_link_info *));
3167 static boolean elf_link_output_extsym
3168 PARAMS ((struct elf_link_hash_entry *, PTR));
3169 static boolean elf_link_input_bfd
3170 PARAMS ((struct elf_final_link_info *, bfd *));
3171 static boolean elf_reloc_link_order
3172 PARAMS ((bfd *, struct bfd_link_info *, asection *,
3173 struct bfd_link_order *));
3174
3175 /* This struct is used to pass information to elf_link_output_extsym. */
3176
3177 struct elf_outext_info
3178 {
3179 boolean failed;
3180 boolean localsyms;
3181 struct elf_final_link_info *finfo;
3182 };
3183
3184 /* Do the final step of an ELF link. */
3185
3186 boolean
3187 elf_bfd_final_link (abfd, info)
3188 bfd *abfd;
3189 struct bfd_link_info *info;
3190 {
3191 boolean dynamic;
3192 bfd *dynobj;
3193 struct elf_final_link_info finfo;
3194 register asection *o;
3195 register struct bfd_link_order *p;
3196 register bfd *sub;
3197 size_t max_contents_size;
3198 size_t max_external_reloc_size;
3199 size_t max_internal_reloc_count;
3200 size_t max_sym_count;
3201 file_ptr off;
3202 Elf_Internal_Sym elfsym;
3203 unsigned int i;
3204 Elf_Internal_Shdr *symtab_hdr;
3205 Elf_Internal_Shdr *symstrtab_hdr;
3206 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3207 struct elf_outext_info eoinfo;
3208
3209 if (info->shared)
3210 abfd->flags |= DYNAMIC;
3211
3212 dynamic = elf_hash_table (info)->dynamic_sections_created;
3213 dynobj = elf_hash_table (info)->dynobj;
3214
3215 finfo.info = info;
3216 finfo.output_bfd = abfd;
3217 finfo.symstrtab = elf_stringtab_init ();
3218 if (finfo.symstrtab == NULL)
3219 return false;
3220
3221 if (! dynamic)
3222 {
3223 finfo.dynsym_sec = NULL;
3224 finfo.hash_sec = NULL;
3225 finfo.symver_sec = NULL;
3226 }
3227 else
3228 {
3229 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
3230 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
3231 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
3232 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
3233 /* Note that it is OK if symver_sec is NULL. */
3234 }
3235
3236 finfo.contents = NULL;
3237 finfo.external_relocs = NULL;
3238 finfo.internal_relocs = NULL;
3239 finfo.external_syms = NULL;
3240 finfo.internal_syms = NULL;
3241 finfo.indices = NULL;
3242 finfo.sections = NULL;
3243 finfo.symbuf = NULL;
3244 finfo.symbuf_count = 0;
3245
3246 /* Count up the number of relocations we will output for each output
3247 section, so that we know the sizes of the reloc sections. We
3248 also figure out some maximum sizes. */
3249 max_contents_size = 0;
3250 max_external_reloc_size = 0;
3251 max_internal_reloc_count = 0;
3252 max_sym_count = 0;
3253 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3254 {
3255 o->reloc_count = 0;
3256
3257 for (p = o->link_order_head; p != NULL; p = p->next)
3258 {
3259 if (p->type == bfd_section_reloc_link_order
3260 || p->type == bfd_symbol_reloc_link_order)
3261 ++o->reloc_count;
3262 else if (p->type == bfd_indirect_link_order)
3263 {
3264 asection *sec;
3265
3266 sec = p->u.indirect.section;
3267
3268 /* Mark all sections which are to be included in the
3269 link. This will normally be every section. We need
3270 to do this so that we can identify any sections which
3271 the linker has decided to not include. */
3272 sec->linker_mark = true;
3273
3274 if (info->relocateable)
3275 o->reloc_count += sec->reloc_count;
3276
3277 if (sec->_raw_size > max_contents_size)
3278 max_contents_size = sec->_raw_size;
3279 if (sec->_cooked_size > max_contents_size)
3280 max_contents_size = sec->_cooked_size;
3281
3282 /* We are interested in just local symbols, not all
3283 symbols. */
3284 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
3285 && (sec->owner->flags & DYNAMIC) == 0)
3286 {
3287 size_t sym_count;
3288
3289 if (elf_bad_symtab (sec->owner))
3290 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
3291 / sizeof (Elf_External_Sym));
3292 else
3293 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
3294
3295 if (sym_count > max_sym_count)
3296 max_sym_count = sym_count;
3297
3298 if ((sec->flags & SEC_RELOC) != 0)
3299 {
3300 size_t ext_size;
3301
3302 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
3303 if (ext_size > max_external_reloc_size)
3304 max_external_reloc_size = ext_size;
3305 if (sec->reloc_count > max_internal_reloc_count)
3306 max_internal_reloc_count = sec->reloc_count;
3307 }
3308 }
3309 }
3310 }
3311
3312 if (o->reloc_count > 0)
3313 o->flags |= SEC_RELOC;
3314 else
3315 {
3316 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3317 set it (this is probably a bug) and if it is set
3318 assign_section_numbers will create a reloc section. */
3319 o->flags &=~ SEC_RELOC;
3320 }
3321
3322 /* If the SEC_ALLOC flag is not set, force the section VMA to
3323 zero. This is done in elf_fake_sections as well, but forcing
3324 the VMA to 0 here will ensure that relocs against these
3325 sections are handled correctly. */
3326 if ((o->flags & SEC_ALLOC) == 0
3327 && ! o->user_set_vma)
3328 o->vma = 0;
3329 }
3330
3331 /* Figure out the file positions for everything but the symbol table
3332 and the relocs. We set symcount to force assign_section_numbers
3333 to create a symbol table. */
3334 abfd->symcount = info->strip == strip_all ? 0 : 1;
3335 BFD_ASSERT (! abfd->output_has_begun);
3336 if (! _bfd_elf_compute_section_file_positions (abfd, info))
3337 goto error_return;
3338
3339 /* That created the reloc sections. Set their sizes, and assign
3340 them file positions, and allocate some buffers. */
3341 for (o = abfd->sections; o != NULL; o = o->next)
3342 {
3343 if ((o->flags & SEC_RELOC) != 0)
3344 {
3345 Elf_Internal_Shdr *rel_hdr;
3346 register struct elf_link_hash_entry **p, **pend;
3347
3348 rel_hdr = &elf_section_data (o)->rel_hdr;
3349
3350 rel_hdr->sh_size = rel_hdr->sh_entsize * o->reloc_count;
3351
3352 /* The contents field must last into write_object_contents,
3353 so we allocate it with bfd_alloc rather than malloc. */
3354 rel_hdr->contents = (PTR) bfd_alloc (abfd, rel_hdr->sh_size);
3355 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
3356 goto error_return;
3357
3358 p = ((struct elf_link_hash_entry **)
3359 bfd_malloc (o->reloc_count
3360 * sizeof (struct elf_link_hash_entry *)));
3361 if (p == NULL && o->reloc_count != 0)
3362 goto error_return;
3363 elf_section_data (o)->rel_hashes = p;
3364 pend = p + o->reloc_count;
3365 for (; p < pend; p++)
3366 *p = NULL;
3367
3368 /* Use the reloc_count field as an index when outputting the
3369 relocs. */
3370 o->reloc_count = 0;
3371 }
3372 }
3373
3374 _bfd_elf_assign_file_positions_for_relocs (abfd);
3375
3376 /* We have now assigned file positions for all the sections except
3377 .symtab and .strtab. We start the .symtab section at the current
3378 file position, and write directly to it. We build the .strtab
3379 section in memory. */
3380 abfd->symcount = 0;
3381 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3382 /* sh_name is set in prep_headers. */
3383 symtab_hdr->sh_type = SHT_SYMTAB;
3384 symtab_hdr->sh_flags = 0;
3385 symtab_hdr->sh_addr = 0;
3386 symtab_hdr->sh_size = 0;
3387 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
3388 /* sh_link is set in assign_section_numbers. */
3389 /* sh_info is set below. */
3390 /* sh_offset is set just below. */
3391 symtab_hdr->sh_addralign = 4; /* FIXME: system dependent? */
3392
3393 off = elf_tdata (abfd)->next_file_pos;
3394 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
3395
3396 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3397 incorrect. We do not yet know the size of the .symtab section.
3398 We correct next_file_pos below, after we do know the size. */
3399
3400 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3401 continuously seeking to the right position in the file. */
3402 if (! info->keep_memory || max_sym_count < 20)
3403 finfo.symbuf_size = 20;
3404 else
3405 finfo.symbuf_size = max_sym_count;
3406 finfo.symbuf = ((Elf_External_Sym *)
3407 bfd_malloc (finfo.symbuf_size * sizeof (Elf_External_Sym)));
3408 if (finfo.symbuf == NULL)
3409 goto error_return;
3410
3411 /* Start writing out the symbol table. The first symbol is always a
3412 dummy symbol. */
3413 if (info->strip != strip_all || info->relocateable)
3414 {
3415 elfsym.st_value = 0;
3416 elfsym.st_size = 0;
3417 elfsym.st_info = 0;
3418 elfsym.st_other = 0;
3419 elfsym.st_shndx = SHN_UNDEF;
3420 if (! elf_link_output_sym (&finfo, (const char *) NULL,
3421 &elfsym, bfd_und_section_ptr))
3422 goto error_return;
3423 }
3424
3425 #if 0
3426 /* Some standard ELF linkers do this, but we don't because it causes
3427 bootstrap comparison failures. */
3428 /* Output a file symbol for the output file as the second symbol.
3429 We output this even if we are discarding local symbols, although
3430 I'm not sure if this is correct. */
3431 elfsym.st_value = 0;
3432 elfsym.st_size = 0;
3433 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3434 elfsym.st_other = 0;
3435 elfsym.st_shndx = SHN_ABS;
3436 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
3437 &elfsym, bfd_abs_section_ptr))
3438 goto error_return;
3439 #endif
3440
3441 /* Output a symbol for each section. We output these even if we are
3442 discarding local symbols, since they are used for relocs. These
3443 symbols have no names. We store the index of each one in the
3444 index field of the section, so that we can find it again when
3445 outputting relocs. */
3446 if (info->strip != strip_all || info->relocateable)
3447 {
3448 elfsym.st_size = 0;
3449 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3450 elfsym.st_other = 0;
3451 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
3452 {
3453 o = section_from_elf_index (abfd, i);
3454 if (o != NULL)
3455 o->target_index = abfd->symcount;
3456 elfsym.st_shndx = i;
3457 if (info->relocateable || o == NULL)
3458 elfsym.st_value = 0;
3459 else
3460 elfsym.st_value = o->vma;
3461 if (! elf_link_output_sym (&finfo, (const char *) NULL,
3462 &elfsym, o))
3463 goto error_return;
3464 }
3465 }
3466
3467 /* Allocate some memory to hold information read in from the input
3468 files. */
3469 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
3470 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
3471 finfo.internal_relocs = ((Elf_Internal_Rela *)
3472 bfd_malloc (max_internal_reloc_count
3473 * sizeof (Elf_Internal_Rela)));
3474 finfo.external_syms = ((Elf_External_Sym *)
3475 bfd_malloc (max_sym_count
3476 * sizeof (Elf_External_Sym)));
3477 finfo.internal_syms = ((Elf_Internal_Sym *)
3478 bfd_malloc (max_sym_count
3479 * sizeof (Elf_Internal_Sym)));
3480 finfo.indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
3481 finfo.sections = ((asection **)
3482 bfd_malloc (max_sym_count * sizeof (asection *)));
3483 if ((finfo.contents == NULL && max_contents_size != 0)
3484 || (finfo.external_relocs == NULL && max_external_reloc_size != 0)
3485 || (finfo.internal_relocs == NULL && max_internal_reloc_count != 0)
3486 || (finfo.external_syms == NULL && max_sym_count != 0)
3487 || (finfo.internal_syms == NULL && max_sym_count != 0)
3488 || (finfo.indices == NULL && max_sym_count != 0)
3489 || (finfo.sections == NULL && max_sym_count != 0))
3490 goto error_return;
3491
3492 /* Since ELF permits relocations to be against local symbols, we
3493 must have the local symbols available when we do the relocations.
3494 Since we would rather only read the local symbols once, and we
3495 would rather not keep them in memory, we handle all the
3496 relocations for a single input file at the same time.
3497
3498 Unfortunately, there is no way to know the total number of local
3499 symbols until we have seen all of them, and the local symbol
3500 indices precede the global symbol indices. This means that when
3501 we are generating relocateable output, and we see a reloc against
3502 a global symbol, we can not know the symbol index until we have
3503 finished examining all the local symbols to see which ones we are
3504 going to output. To deal with this, we keep the relocations in
3505 memory, and don't output them until the end of the link. This is
3506 an unfortunate waste of memory, but I don't see a good way around
3507 it. Fortunately, it only happens when performing a relocateable
3508 link, which is not the common case. FIXME: If keep_memory is set
3509 we could write the relocs out and then read them again; I don't
3510 know how bad the memory loss will be. */
3511
3512 for (sub = info->input_bfds; sub != NULL; sub = sub->next)
3513 sub->output_has_begun = false;
3514 for (o = abfd->sections; o != NULL; o = o->next)
3515 {
3516 for (p = o->link_order_head; p != NULL; p = p->next)
3517 {
3518 if (p->type == bfd_indirect_link_order
3519 && (bfd_get_flavour (p->u.indirect.section->owner)
3520 == bfd_target_elf_flavour))
3521 {
3522 sub = p->u.indirect.section->owner;
3523 if (! sub->output_has_begun)
3524 {
3525 if (! elf_link_input_bfd (&finfo, sub))
3526 goto error_return;
3527 sub->output_has_begun = true;
3528 }
3529 }
3530 else if (p->type == bfd_section_reloc_link_order
3531 || p->type == bfd_symbol_reloc_link_order)
3532 {
3533 if (! elf_reloc_link_order (abfd, info, o, p))
3534 goto error_return;
3535 }
3536 else
3537 {
3538 if (! _bfd_default_link_order (abfd, info, o, p))
3539 goto error_return;
3540 }
3541 }
3542 }
3543
3544 /* That wrote out all the local symbols. Finish up the symbol table
3545 with the global symbols. */
3546
3547 if (info->strip != strip_all && info->shared)
3548 {
3549 /* Output any global symbols that got converted to local in a
3550 version script. We do this in a separate step since ELF
3551 requires all local symbols to appear prior to any global
3552 symbols. FIXME: We should only do this if some global
3553 symbols were, in fact, converted to become local. FIXME:
3554 Will this work correctly with the Irix 5 linker? */
3555 eoinfo.failed = false;
3556 eoinfo.finfo = &finfo;
3557 eoinfo.localsyms = true;
3558 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
3559 (PTR) &eoinfo);
3560 if (eoinfo.failed)
3561 return false;
3562 }
3563
3564 /* The sh_info field records the index of the first non local
3565 symbol. */
3566 symtab_hdr->sh_info = abfd->symcount;
3567 if (dynamic)
3568 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = 1;
3569
3570 /* We get the global symbols from the hash table. */
3571 eoinfo.failed = false;
3572 eoinfo.localsyms = false;
3573 eoinfo.finfo = &finfo;
3574 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
3575 (PTR) &eoinfo);
3576 if (eoinfo.failed)
3577 return false;
3578
3579 /* Flush all symbols to the file. */
3580 if (! elf_link_flush_output_syms (&finfo))
3581 return false;
3582
3583 /* Now we know the size of the symtab section. */
3584 off += symtab_hdr->sh_size;
3585
3586 /* Finish up and write out the symbol string table (.strtab)
3587 section. */
3588 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3589 /* sh_name was set in prep_headers. */
3590 symstrtab_hdr->sh_type = SHT_STRTAB;
3591 symstrtab_hdr->sh_flags = 0;
3592 symstrtab_hdr->sh_addr = 0;
3593 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
3594 symstrtab_hdr->sh_entsize = 0;
3595 symstrtab_hdr->sh_link = 0;
3596 symstrtab_hdr->sh_info = 0;
3597 /* sh_offset is set just below. */
3598 symstrtab_hdr->sh_addralign = 1;
3599
3600 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
3601 elf_tdata (abfd)->next_file_pos = off;
3602
3603 if (abfd->symcount > 0)
3604 {
3605 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
3606 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
3607 return false;
3608 }
3609
3610 /* Adjust the relocs to have the correct symbol indices. */
3611 for (o = abfd->sections; o != NULL; o = o->next)
3612 {
3613 struct elf_link_hash_entry **rel_hash;
3614 Elf_Internal_Shdr *rel_hdr;
3615
3616 if ((o->flags & SEC_RELOC) == 0)
3617 continue;
3618
3619 rel_hash = elf_section_data (o)->rel_hashes;
3620 rel_hdr = &elf_section_data (o)->rel_hdr;
3621 for (i = 0; i < o->reloc_count; i++, rel_hash++)
3622 {
3623 if (*rel_hash == NULL)
3624 continue;
3625
3626 BFD_ASSERT ((*rel_hash)->indx >= 0);
3627
3628 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
3629 {
3630 Elf_External_Rel *erel;
3631 Elf_Internal_Rel irel;
3632
3633 erel = (Elf_External_Rel *) rel_hdr->contents + i;
3634 elf_swap_reloc_in (abfd, erel, &irel);
3635 irel.r_info = ELF_R_INFO ((*rel_hash)->indx,
3636 ELF_R_TYPE (irel.r_info));
3637 elf_swap_reloc_out (abfd, &irel, erel);
3638 }
3639 else
3640 {
3641 Elf_External_Rela *erela;
3642 Elf_Internal_Rela irela;
3643
3644 BFD_ASSERT (rel_hdr->sh_entsize
3645 == sizeof (Elf_External_Rela));
3646
3647 erela = (Elf_External_Rela *) rel_hdr->contents + i;
3648 elf_swap_reloca_in (abfd, erela, &irela);
3649 irela.r_info = ELF_R_INFO ((*rel_hash)->indx,
3650 ELF_R_TYPE (irela.r_info));
3651 elf_swap_reloca_out (abfd, &irela, erela);
3652 }
3653 }
3654
3655 /* Set the reloc_count field to 0 to prevent write_relocs from
3656 trying to swap the relocs out itself. */
3657 o->reloc_count = 0;
3658 }
3659
3660 /* If we are linking against a dynamic object, or generating a
3661 shared library, finish up the dynamic linking information. */
3662 if (dynamic)
3663 {
3664 Elf_External_Dyn *dyncon, *dynconend;
3665
3666 /* Fix up .dynamic entries. */
3667 o = bfd_get_section_by_name (dynobj, ".dynamic");
3668 BFD_ASSERT (o != NULL);
3669
3670 dyncon = (Elf_External_Dyn *) o->contents;
3671 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
3672 for (; dyncon < dynconend; dyncon++)
3673 {
3674 Elf_Internal_Dyn dyn;
3675 const char *name;
3676 unsigned int type;
3677
3678 elf_swap_dyn_in (dynobj, dyncon, &dyn);
3679
3680 switch (dyn.d_tag)
3681 {
3682 default:
3683 break;
3684
3685 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on
3686 magic _init and _fini symbols. This is pretty ugly,
3687 but we are compatible. */
3688 case DT_INIT:
3689 name = "_init";
3690 goto get_sym;
3691 case DT_FINI:
3692 name = "_fini";
3693 get_sym:
3694 {
3695 struct elf_link_hash_entry *h;
3696
3697 h = elf_link_hash_lookup (elf_hash_table (info), name,
3698 false, false, true);
3699 if (h != NULL
3700 && (h->root.type == bfd_link_hash_defined
3701 || h->root.type == bfd_link_hash_defweak))
3702 {
3703 dyn.d_un.d_val = h->root.u.def.value;
3704 o = h->root.u.def.section;
3705 if (o->output_section != NULL)
3706 dyn.d_un.d_val += (o->output_section->vma
3707 + o->output_offset);
3708 else
3709 {
3710 /* The symbol is imported from another shared
3711 library and does not apply to this one. */
3712 dyn.d_un.d_val = 0;
3713 }
3714
3715 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3716 }
3717 }
3718 break;
3719
3720 case DT_HASH:
3721 name = ".hash";
3722 goto get_vma;
3723 case DT_STRTAB:
3724 name = ".dynstr";
3725 goto get_vma;
3726 case DT_SYMTAB:
3727 name = ".dynsym";
3728 goto get_vma;
3729 case DT_VERDEF:
3730 name = ".gnu.version_d";
3731 goto get_vma;
3732 case DT_VERNEED:
3733 name = ".gnu.version_r";
3734 goto get_vma;
3735 case DT_VERSYM:
3736 name = ".gnu.version";
3737 get_vma:
3738 o = bfd_get_section_by_name (abfd, name);
3739 BFD_ASSERT (o != NULL);
3740 dyn.d_un.d_ptr = o->vma;
3741 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3742 break;
3743
3744 case DT_REL:
3745 case DT_RELA:
3746 case DT_RELSZ:
3747 case DT_RELASZ:
3748 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
3749 type = SHT_REL;
3750 else
3751 type = SHT_RELA;
3752 dyn.d_un.d_val = 0;
3753 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
3754 {
3755 Elf_Internal_Shdr *hdr;
3756
3757 hdr = elf_elfsections (abfd)[i];
3758 if (hdr->sh_type == type
3759 && (hdr->sh_flags & SHF_ALLOC) != 0)
3760 {
3761 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
3762 dyn.d_un.d_val += hdr->sh_size;
3763 else
3764 {
3765 if (dyn.d_un.d_val == 0
3766 || hdr->sh_addr < dyn.d_un.d_val)
3767 dyn.d_un.d_val = hdr->sh_addr;
3768 }
3769 }
3770 }
3771 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3772 break;
3773 }
3774 }
3775 }
3776
3777 /* If we have created any dynamic sections, then output them. */
3778 if (dynobj != NULL)
3779 {
3780 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
3781 goto error_return;
3782
3783 for (o = dynobj->sections; o != NULL; o = o->next)
3784 {
3785 if ((o->flags & SEC_HAS_CONTENTS) == 0
3786 || o->_raw_size == 0)
3787 continue;
3788 if ((o->flags & SEC_LINKER_CREATED) == 0)
3789 {
3790 /* At this point, we are only interested in sections
3791 created by elf_link_create_dynamic_sections. */
3792 continue;
3793 }
3794 if ((elf_section_data (o->output_section)->this_hdr.sh_type
3795 != SHT_STRTAB)
3796 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
3797 {
3798 if (! bfd_set_section_contents (abfd, o->output_section,
3799 o->contents, o->output_offset,
3800 o->_raw_size))
3801 goto error_return;
3802 }
3803 else
3804 {
3805 file_ptr off;
3806
3807 /* The contents of the .dynstr section are actually in a
3808 stringtab. */
3809 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
3810 if (bfd_seek (abfd, off, SEEK_SET) != 0
3811 || ! _bfd_stringtab_emit (abfd,
3812 elf_hash_table (info)->dynstr))
3813 goto error_return;
3814 }
3815 }
3816 }
3817
3818 /* If we have optimized stabs strings, output them. */
3819 if (elf_hash_table (info)->stab_info != NULL)
3820 {
3821 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
3822 goto error_return;
3823 }
3824
3825 if (finfo.symstrtab != NULL)
3826 _bfd_stringtab_free (finfo.symstrtab);
3827 if (finfo.contents != NULL)
3828 free (finfo.contents);
3829 if (finfo.external_relocs != NULL)
3830 free (finfo.external_relocs);
3831 if (finfo.internal_relocs != NULL)
3832 free (finfo.internal_relocs);
3833 if (finfo.external_syms != NULL)
3834 free (finfo.external_syms);
3835 if (finfo.internal_syms != NULL)
3836 free (finfo.internal_syms);
3837 if (finfo.indices != NULL)
3838 free (finfo.indices);
3839 if (finfo.sections != NULL)
3840 free (finfo.sections);
3841 if (finfo.symbuf != NULL)
3842 free (finfo.symbuf);
3843 for (o = abfd->sections; o != NULL; o = o->next)
3844 {
3845 if ((o->flags & SEC_RELOC) != 0
3846 && elf_section_data (o)->rel_hashes != NULL)
3847 free (elf_section_data (o)->rel_hashes);
3848 }
3849
3850 elf_tdata (abfd)->linker = true;
3851
3852 return true;
3853
3854 error_return:
3855 if (finfo.symstrtab != NULL)
3856 _bfd_stringtab_free (finfo.symstrtab);
3857 if (finfo.contents != NULL)
3858 free (finfo.contents);
3859 if (finfo.external_relocs != NULL)
3860 free (finfo.external_relocs);
3861 if (finfo.internal_relocs != NULL)
3862 free (finfo.internal_relocs);
3863 if (finfo.external_syms != NULL)
3864 free (finfo.external_syms);
3865 if (finfo.internal_syms != NULL)
3866 free (finfo.internal_syms);
3867 if (finfo.indices != NULL)
3868 free (finfo.indices);
3869 if (finfo.sections != NULL)
3870 free (finfo.sections);
3871 if (finfo.symbuf != NULL)
3872 free (finfo.symbuf);
3873 for (o = abfd->sections; o != NULL; o = o->next)
3874 {
3875 if ((o->flags & SEC_RELOC) != 0
3876 && elf_section_data (o)->rel_hashes != NULL)
3877 free (elf_section_data (o)->rel_hashes);
3878 }
3879
3880 return false;
3881 }
3882
3883 /* Add a symbol to the output symbol table. */
3884
3885 static boolean
3886 elf_link_output_sym (finfo, name, elfsym, input_sec)
3887 struct elf_final_link_info *finfo;
3888 const char *name;
3889 Elf_Internal_Sym *elfsym;
3890 asection *input_sec;
3891 {
3892 boolean (*output_symbol_hook) PARAMS ((bfd *,
3893 struct bfd_link_info *info,
3894 const char *,
3895 Elf_Internal_Sym *,
3896 asection *));
3897
3898 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
3899 elf_backend_link_output_symbol_hook;
3900 if (output_symbol_hook != NULL)
3901 {
3902 if (! ((*output_symbol_hook)
3903 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
3904 return false;
3905 }
3906
3907 if (name == (const char *) NULL || *name == '\0')
3908 elfsym->st_name = 0;
3909 else
3910 {
3911 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
3912 name, true,
3913 false);
3914 if (elfsym->st_name == (unsigned long) -1)
3915 return false;
3916 }
3917
3918 if (finfo->symbuf_count >= finfo->symbuf_size)
3919 {
3920 if (! elf_link_flush_output_syms (finfo))
3921 return false;
3922 }
3923
3924 elf_swap_symbol_out (finfo->output_bfd, elfsym,
3925 (PTR) (finfo->symbuf + finfo->symbuf_count));
3926 ++finfo->symbuf_count;
3927
3928 ++finfo->output_bfd->symcount;
3929
3930 return true;
3931 }
3932
3933 /* Flush the output symbols to the file. */
3934
3935 static boolean
3936 elf_link_flush_output_syms (finfo)
3937 struct elf_final_link_info *finfo;
3938 {
3939 if (finfo->symbuf_count > 0)
3940 {
3941 Elf_Internal_Shdr *symtab;
3942
3943 symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr;
3944
3945 if (bfd_seek (finfo->output_bfd, symtab->sh_offset + symtab->sh_size,
3946 SEEK_SET) != 0
3947 || (bfd_write ((PTR) finfo->symbuf, finfo->symbuf_count,
3948 sizeof (Elf_External_Sym), finfo->output_bfd)
3949 != finfo->symbuf_count * sizeof (Elf_External_Sym)))
3950 return false;
3951
3952 symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym);
3953
3954 finfo->symbuf_count = 0;
3955 }
3956
3957 return true;
3958 }
3959
3960 /* Add an external symbol to the symbol table. This is called from
3961 the hash table traversal routine. When generating a shared object,
3962 we go through the symbol table twice. The first time we output
3963 anything that might have been forced to local scope in a version
3964 script. The second time we output the symbols that are still
3965 global symbols. */
3966
3967 static boolean
3968 elf_link_output_extsym (h, data)
3969 struct elf_link_hash_entry *h;
3970 PTR data;
3971 {
3972 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
3973 struct elf_final_link_info *finfo = eoinfo->finfo;
3974 boolean strip;
3975 Elf_Internal_Sym sym;
3976 asection *input_sec;
3977
3978 /* Decide whether to output this symbol in this pass. */
3979 if (eoinfo->localsyms)
3980 {
3981 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3982 return true;
3983 }
3984 else
3985 {
3986 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
3987 return true;
3988 }
3989
3990 /* If we are not creating a shared library, and this symbol is
3991 referenced by a shared library but is not defined anywhere, then
3992 warn that it is undefined. If we do not do this, the runtime
3993 linker will complain that the symbol is undefined when the
3994 program is run. We don't have to worry about symbols that are
3995 referenced by regular files, because we will already have issued
3996 warnings for them. */
3997 if (! finfo->info->relocateable
3998 && ! finfo->info->shared
3999 && h->root.type == bfd_link_hash_undefined
4000 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
4001 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4002 {
4003 if (! ((*finfo->info->callbacks->undefined_symbol)
4004 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
4005 (asection *) NULL, 0)))
4006 {
4007 eoinfo->failed = true;
4008 return false;
4009 }
4010 }
4011
4012 /* We don't want to output symbols that have never been mentioned by
4013 a regular file, or that we have been told to strip. However, if
4014 h->indx is set to -2, the symbol is used by a reloc and we must
4015 output it. */
4016 if (h->indx == -2)
4017 strip = false;
4018 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4019 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
4020 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4021 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4022 strip = true;
4023 else if (finfo->info->strip == strip_all
4024 || (finfo->info->strip == strip_some
4025 && bfd_hash_lookup (finfo->info->keep_hash,
4026 h->root.root.string,
4027 false, false) == NULL))
4028 strip = true;
4029 else
4030 strip = false;
4031
4032 /* If we're stripping it, and it's not a dynamic symbol, there's
4033 nothing else to do. */
4034 if (strip && h->dynindx == -1)
4035 return true;
4036
4037 sym.st_value = 0;
4038 sym.st_size = h->size;
4039 sym.st_other = h->other;
4040 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4041 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
4042 else if (h->root.type == bfd_link_hash_undefweak
4043 || h->root.type == bfd_link_hash_defweak)
4044 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
4045 else
4046 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
4047
4048 switch (h->root.type)
4049 {
4050 default:
4051 case bfd_link_hash_new:
4052 abort ();
4053 return false;
4054
4055 case bfd_link_hash_undefined:
4056 input_sec = bfd_und_section_ptr;
4057 sym.st_shndx = SHN_UNDEF;
4058 break;
4059
4060 case bfd_link_hash_undefweak:
4061 input_sec = bfd_und_section_ptr;
4062 sym.st_shndx = SHN_UNDEF;
4063 break;
4064
4065 case bfd_link_hash_defined:
4066 case bfd_link_hash_defweak:
4067 {
4068 input_sec = h->root.u.def.section;
4069 if (input_sec->output_section != NULL)
4070 {
4071 sym.st_shndx =
4072 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
4073 input_sec->output_section);
4074 if (sym.st_shndx == (unsigned short) -1)
4075 {
4076 eoinfo->failed = true;
4077 return false;
4078 }
4079
4080 /* ELF symbols in relocateable files are section relative,
4081 but in nonrelocateable files they are virtual
4082 addresses. */
4083 sym.st_value = h->root.u.def.value + input_sec->output_offset;
4084 if (! finfo->info->relocateable)
4085 sym.st_value += input_sec->output_section->vma;
4086 }
4087 else
4088 {
4089 BFD_ASSERT (input_sec->owner == NULL
4090 || (input_sec->owner->flags & DYNAMIC) != 0);
4091 sym.st_shndx = SHN_UNDEF;
4092 input_sec = bfd_und_section_ptr;
4093 }
4094 }
4095 break;
4096
4097 case bfd_link_hash_common:
4098 input_sec = bfd_com_section_ptr;
4099 sym.st_shndx = SHN_COMMON;
4100 sym.st_value = 1 << h->root.u.c.p->alignment_power;
4101 break;
4102
4103 case bfd_link_hash_indirect:
4104 /* These symbols are created by symbol versioning. They point
4105 to the decorated version of the name. For example, if the
4106 symbol foo@@GNU_1.2 is the default, which should be used when
4107 foo is used with no version, then we add an indirect symbol
4108 foo which points to foo@@GNU_1.2. We ignore these symbols,
4109 since the indirected symbol is already in the hash table. If
4110 the indirect symbol is non-ELF, fall through and output it. */
4111 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) == 0)
4112 return true;
4113
4114 /* Fall through. */
4115 case bfd_link_hash_warning:
4116 /* We can't represent these symbols in ELF, although a warning
4117 symbol may have come from a .gnu.warning.SYMBOL section. We
4118 just put the target symbol in the hash table. If the target
4119 symbol does not really exist, don't do anything. */
4120 if (h->root.u.i.link->type == bfd_link_hash_new)
4121 return true;
4122 return (elf_link_output_extsym
4123 ((struct elf_link_hash_entry *) h->root.u.i.link, data));
4124 }
4125
4126 /* If this symbol should be put in the .dynsym section, then put it
4127 there now. We have already know the symbol index. We also fill
4128 in the entry in the .hash section. */
4129 if (h->dynindx != -1
4130 && elf_hash_table (finfo->info)->dynamic_sections_created)
4131 {
4132 struct elf_backend_data *bed;
4133 char *p, *copy;
4134 const char *name;
4135 size_t bucketcount;
4136 size_t bucket;
4137 bfd_byte *bucketpos;
4138 bfd_vma chain;
4139
4140 sym.st_name = h->dynstr_index;
4141
4142 /* Give the processor backend a chance to tweak the symbol
4143 value, and also to finish up anything that needs to be done
4144 for this symbol. */
4145 bed = get_elf_backend_data (finfo->output_bfd);
4146 if (! ((*bed->elf_backend_finish_dynamic_symbol)
4147 (finfo->output_bfd, finfo->info, h, &sym)))
4148 {
4149 eoinfo->failed = true;
4150 return false;
4151 }
4152
4153 elf_swap_symbol_out (finfo->output_bfd, &sym,
4154 (PTR) (((Elf_External_Sym *)
4155 finfo->dynsym_sec->contents)
4156 + h->dynindx));
4157
4158 /* We didn't include the version string in the dynamic string
4159 table, so we must not consider it in the hash table. */
4160 name = h->root.root.string;
4161 p = strchr (name, ELF_VER_CHR);
4162 if (p == NULL)
4163 copy = NULL;
4164 else
4165 {
4166 copy = bfd_alloc (finfo->output_bfd, p - name + 1);
4167 strncpy (copy, name, p - name);
4168 copy[p - name] = '\0';
4169 name = copy;
4170 }
4171
4172 bucketcount = elf_hash_table (finfo->info)->bucketcount;
4173 bucket = bfd_elf_hash ((const unsigned char *) name) % bucketcount;
4174 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
4175 + (bucket + 2) * (ARCH_SIZE / 8));
4176 chain = get_word (finfo->output_bfd, bucketpos);
4177 put_word (finfo->output_bfd, h->dynindx, bucketpos);
4178 put_word (finfo->output_bfd, chain,
4179 ((bfd_byte *) finfo->hash_sec->contents
4180 + (bucketcount + 2 + h->dynindx) * (ARCH_SIZE / 8)));
4181
4182 if (copy != NULL)
4183 bfd_release (finfo->output_bfd, copy);
4184
4185 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
4186 {
4187 Elf_Internal_Versym iversym;
4188
4189 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4190 {
4191 if (h->verinfo.verdef == NULL)
4192 iversym.vs_vers = 0;
4193 else
4194 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
4195 }
4196 else
4197 {
4198 if (h->verinfo.vertree == NULL)
4199 iversym.vs_vers = 1;
4200 else
4201 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
4202 }
4203
4204 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
4205 iversym.vs_vers |= VERSYM_HIDDEN;
4206
4207 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym,
4208 (((Elf_External_Versym *)
4209 finfo->symver_sec->contents)
4210 + h->dynindx));
4211 }
4212 }
4213
4214 /* If we're stripping it, then it was just a dynamic symbol, and
4215 there's nothing else to do. */
4216 if (strip)
4217 return true;
4218
4219 h->indx = finfo->output_bfd->symcount;
4220
4221 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
4222 {
4223 eoinfo->failed = true;
4224 return false;
4225 }
4226
4227 return true;
4228 }
4229
4230 /* Link an input file into the linker output file. This function
4231 handles all the sections and relocations of the input file at once.
4232 This is so that we only have to read the local symbols once, and
4233 don't have to keep them in memory. */
4234
4235 static boolean
4236 elf_link_input_bfd (finfo, input_bfd)
4237 struct elf_final_link_info *finfo;
4238 bfd *input_bfd;
4239 {
4240 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
4241 bfd *, asection *, bfd_byte *,
4242 Elf_Internal_Rela *,
4243 Elf_Internal_Sym *, asection **));
4244 bfd *output_bfd;
4245 Elf_Internal_Shdr *symtab_hdr;
4246 size_t locsymcount;
4247 size_t extsymoff;
4248 Elf_External_Sym *external_syms;
4249 Elf_External_Sym *esym;
4250 Elf_External_Sym *esymend;
4251 Elf_Internal_Sym *isym;
4252 long *pindex;
4253 asection **ppsection;
4254 asection *o;
4255
4256 output_bfd = finfo->output_bfd;
4257 relocate_section =
4258 get_elf_backend_data (output_bfd)->elf_backend_relocate_section;
4259
4260 /* If this is a dynamic object, we don't want to do anything here:
4261 we don't want the local symbols, and we don't want the section
4262 contents. */
4263 if ((input_bfd->flags & DYNAMIC) != 0)
4264 return true;
4265
4266 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4267 if (elf_bad_symtab (input_bfd))
4268 {
4269 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
4270 extsymoff = 0;
4271 }
4272 else
4273 {
4274 locsymcount = symtab_hdr->sh_info;
4275 extsymoff = symtab_hdr->sh_info;
4276 }
4277
4278 /* Read the local symbols. */
4279 if (symtab_hdr->contents != NULL)
4280 external_syms = (Elf_External_Sym *) symtab_hdr->contents;
4281 else if (locsymcount == 0)
4282 external_syms = NULL;
4283 else
4284 {
4285 external_syms = finfo->external_syms;
4286 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
4287 || (bfd_read (external_syms, sizeof (Elf_External_Sym),
4288 locsymcount, input_bfd)
4289 != locsymcount * sizeof (Elf_External_Sym)))
4290 return false;
4291 }
4292
4293 /* Swap in the local symbols and write out the ones which we know
4294 are going into the output file. */
4295 esym = external_syms;
4296 esymend = esym + locsymcount;
4297 isym = finfo->internal_syms;
4298 pindex = finfo->indices;
4299 ppsection = finfo->sections;
4300 for (; esym < esymend; esym++, isym++, pindex++, ppsection++)
4301 {
4302 asection *isec;
4303 const char *name;
4304 Elf_Internal_Sym osym;
4305
4306 elf_swap_symbol_in (input_bfd, esym, isym);
4307 *pindex = -1;
4308
4309 if (elf_bad_symtab (input_bfd))
4310 {
4311 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
4312 {
4313 *ppsection = NULL;
4314 continue;
4315 }
4316 }
4317
4318 if (isym->st_shndx == SHN_UNDEF)
4319 isec = bfd_und_section_ptr;
4320 else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE)
4321 isec = section_from_elf_index (input_bfd, isym->st_shndx);
4322 else if (isym->st_shndx == SHN_ABS)
4323 isec = bfd_abs_section_ptr;
4324 else if (isym->st_shndx == SHN_COMMON)
4325 isec = bfd_com_section_ptr;
4326 else
4327 {
4328 /* Who knows? */
4329 isec = NULL;
4330 }
4331
4332 *ppsection = isec;
4333
4334 /* Don't output the first, undefined, symbol. */
4335 if (esym == external_syms)
4336 continue;
4337
4338 /* If we are stripping all symbols, we don't want to output this
4339 one. */
4340 if (finfo->info->strip == strip_all)
4341 continue;
4342
4343 /* We never output section symbols. Instead, we use the section
4344 symbol of the corresponding section in the output file. */
4345 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4346 continue;
4347
4348 /* If we are discarding all local symbols, we don't want to
4349 output this one. If we are generating a relocateable output
4350 file, then some of the local symbols may be required by
4351 relocs; we output them below as we discover that they are
4352 needed. */
4353 if (finfo->info->discard == discard_all)
4354 continue;
4355
4356 /* If this symbol is defined in a section which we are
4357 discarding, we don't need to keep it, but note that
4358 linker_mark is only reliable for sections that have contents.
4359 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4360 as well as linker_mark. */
4361 if (isym->st_shndx > 0
4362 && isym->st_shndx < SHN_LORESERVE
4363 && isec != NULL
4364 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
4365 || (! finfo->info->relocateable
4366 && (isec->flags & SEC_EXCLUDE) != 0)))
4367 continue;
4368
4369 /* Get the name of the symbol. */
4370 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
4371 isym->st_name);
4372 if (name == NULL)
4373 return false;
4374
4375 /* See if we are discarding symbols with this name. */
4376 if ((finfo->info->strip == strip_some
4377 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
4378 == NULL))
4379 || (finfo->info->discard == discard_l
4380 && bfd_is_local_label_name (input_bfd, name)))
4381 continue;
4382
4383 /* If we get here, we are going to output this symbol. */
4384
4385 osym = *isym;
4386
4387 /* Adjust the section index for the output file. */
4388 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
4389 isec->output_section);
4390 if (osym.st_shndx == (unsigned short) -1)
4391 return false;
4392
4393 *pindex = output_bfd->symcount;
4394
4395 /* ELF symbols in relocateable files are section relative, but
4396 in executable files they are virtual addresses. Note that
4397 this code assumes that all ELF sections have an associated
4398 BFD section with a reasonable value for output_offset; below
4399 we assume that they also have a reasonable value for
4400 output_section. Any special sections must be set up to meet
4401 these requirements. */
4402 osym.st_value += isec->output_offset;
4403 if (! finfo->info->relocateable)
4404 osym.st_value += isec->output_section->vma;
4405
4406 if (! elf_link_output_sym (finfo, name, &osym, isec))
4407 return false;
4408 }
4409
4410 /* Relocate the contents of each section. */
4411 for (o = input_bfd->sections; o != NULL; o = o->next)
4412 {
4413 bfd_byte *contents;
4414
4415 if (! o->linker_mark)
4416 {
4417 /* This section was omitted from the link. */
4418 continue;
4419 }
4420
4421 if ((o->flags & SEC_HAS_CONTENTS) == 0
4422 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
4423 continue;
4424
4425 if ((o->flags & SEC_LINKER_CREATED) != 0)
4426 {
4427 /* Section was created by elf_link_create_dynamic_sections
4428 or somesuch. */
4429 continue;
4430 }
4431
4432 /* Get the contents of the section. They have been cached by a
4433 relaxation routine. Note that o is a section in an input
4434 file, so the contents field will not have been set by any of
4435 the routines which work on output files. */
4436 if (elf_section_data (o)->this_hdr.contents != NULL)
4437 contents = elf_section_data (o)->this_hdr.contents;
4438 else
4439 {
4440 contents = finfo->contents;
4441 if (! bfd_get_section_contents (input_bfd, o, contents,
4442 (file_ptr) 0, o->_raw_size))
4443 return false;
4444 }
4445
4446 if ((o->flags & SEC_RELOC) != 0)
4447 {
4448 Elf_Internal_Rela *internal_relocs;
4449
4450 /* Get the swapped relocs. */
4451 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
4452 (input_bfd, o, finfo->external_relocs,
4453 finfo->internal_relocs, false));
4454 if (internal_relocs == NULL
4455 && o->reloc_count > 0)
4456 return false;
4457
4458 /* Relocate the section by invoking a back end routine.
4459
4460 The back end routine is responsible for adjusting the
4461 section contents as necessary, and (if using Rela relocs
4462 and generating a relocateable output file) adjusting the
4463 reloc addend as necessary.
4464
4465 The back end routine does not have to worry about setting
4466 the reloc address or the reloc symbol index.
4467
4468 The back end routine is given a pointer to the swapped in
4469 internal symbols, and can access the hash table entries
4470 for the external symbols via elf_sym_hashes (input_bfd).
4471
4472 When generating relocateable output, the back end routine
4473 must handle STB_LOCAL/STT_SECTION symbols specially. The
4474 output symbol is going to be a section symbol
4475 corresponding to the output section, which will require
4476 the addend to be adjusted. */
4477
4478 if (! (*relocate_section) (output_bfd, finfo->info,
4479 input_bfd, o, contents,
4480 internal_relocs,
4481 finfo->internal_syms,
4482 finfo->sections))
4483 return false;
4484
4485 if (finfo->info->relocateable)
4486 {
4487 Elf_Internal_Rela *irela;
4488 Elf_Internal_Rela *irelaend;
4489 struct elf_link_hash_entry **rel_hash;
4490 Elf_Internal_Shdr *input_rel_hdr;
4491 Elf_Internal_Shdr *output_rel_hdr;
4492
4493 /* Adjust the reloc addresses and symbol indices. */
4494
4495 irela = internal_relocs;
4496 irelaend = irela + o->reloc_count;
4497 rel_hash = (elf_section_data (o->output_section)->rel_hashes
4498 + o->output_section->reloc_count);
4499 for (; irela < irelaend; irela++, rel_hash++)
4500 {
4501 unsigned long r_symndx;
4502 Elf_Internal_Sym *isym;
4503 asection *sec;
4504
4505 irela->r_offset += o->output_offset;
4506
4507 r_symndx = ELF_R_SYM (irela->r_info);
4508
4509 if (r_symndx == 0)
4510 continue;
4511
4512 if (r_symndx >= locsymcount
4513 || (elf_bad_symtab (input_bfd)
4514 && finfo->sections[r_symndx] == NULL))
4515 {
4516 long indx;
4517
4518 /* This is a reloc against a global symbol. We
4519 have not yet output all the local symbols, so
4520 we do not know the symbol index of any global
4521 symbol. We set the rel_hash entry for this
4522 reloc to point to the global hash table entry
4523 for this symbol. The symbol index is then
4524 set at the end of elf_bfd_final_link. */
4525 indx = r_symndx - extsymoff;
4526 *rel_hash = elf_sym_hashes (input_bfd)[indx];
4527
4528 /* Setting the index to -2 tells
4529 elf_link_output_extsym that this symbol is
4530 used by a reloc. */
4531 BFD_ASSERT ((*rel_hash)->indx < 0);
4532 (*rel_hash)->indx = -2;
4533
4534 continue;
4535 }
4536
4537 /* This is a reloc against a local symbol. */
4538
4539 *rel_hash = NULL;
4540 isym = finfo->internal_syms + r_symndx;
4541 sec = finfo->sections[r_symndx];
4542 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4543 {
4544 /* I suppose the backend ought to fill in the
4545 section of any STT_SECTION symbol against a
4546 processor specific section. If we have
4547 discarded a section, the output_section will
4548 be the absolute section. */
4549 if (sec != NULL
4550 && (bfd_is_abs_section (sec)
4551 || (sec->output_section != NULL
4552 && bfd_is_abs_section (sec->output_section))))
4553 r_symndx = 0;
4554 else if (sec == NULL || sec->owner == NULL)
4555 {
4556 bfd_set_error (bfd_error_bad_value);
4557 return false;
4558 }
4559 else
4560 {
4561 r_symndx = sec->output_section->target_index;
4562 BFD_ASSERT (r_symndx != 0);
4563 }
4564 }
4565 else
4566 {
4567 if (finfo->indices[r_symndx] == -1)
4568 {
4569 unsigned long link;
4570 const char *name;
4571 asection *osec;
4572
4573 if (finfo->info->strip == strip_all)
4574 {
4575 /* You can't do ld -r -s. */
4576 bfd_set_error (bfd_error_invalid_operation);
4577 return false;
4578 }
4579
4580 /* This symbol was skipped earlier, but
4581 since it is needed by a reloc, we
4582 must output it now. */
4583 link = symtab_hdr->sh_link;
4584 name = bfd_elf_string_from_elf_section (input_bfd,
4585 link,
4586 isym->st_name);
4587 if (name == NULL)
4588 return false;
4589
4590 osec = sec->output_section;
4591 isym->st_shndx =
4592 _bfd_elf_section_from_bfd_section (output_bfd,
4593 osec);
4594 if (isym->st_shndx == (unsigned short) -1)
4595 return false;
4596
4597 isym->st_value += sec->output_offset;
4598 if (! finfo->info->relocateable)
4599 isym->st_value += osec->vma;
4600
4601 finfo->indices[r_symndx] = output_bfd->symcount;
4602
4603 if (! elf_link_output_sym (finfo, name, isym, sec))
4604 return false;
4605 }
4606
4607 r_symndx = finfo->indices[r_symndx];
4608 }
4609
4610 irela->r_info = ELF_R_INFO (r_symndx,
4611 ELF_R_TYPE (irela->r_info));
4612 }
4613
4614 /* Swap out the relocs. */
4615 input_rel_hdr = &elf_section_data (o)->rel_hdr;
4616 output_rel_hdr = &elf_section_data (o->output_section)->rel_hdr;
4617 BFD_ASSERT (output_rel_hdr->sh_entsize
4618 == input_rel_hdr->sh_entsize);
4619 irela = internal_relocs;
4620 irelaend = irela + o->reloc_count;
4621 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4622 {
4623 Elf_External_Rel *erel;
4624
4625 erel = ((Elf_External_Rel *) output_rel_hdr->contents
4626 + o->output_section->reloc_count);
4627 for (; irela < irelaend; irela++, erel++)
4628 {
4629 Elf_Internal_Rel irel;
4630
4631 irel.r_offset = irela->r_offset;
4632 irel.r_info = irela->r_info;
4633 BFD_ASSERT (irela->r_addend == 0);
4634 elf_swap_reloc_out (output_bfd, &irel, erel);
4635 }
4636 }
4637 else
4638 {
4639 Elf_External_Rela *erela;
4640
4641 BFD_ASSERT (input_rel_hdr->sh_entsize
4642 == sizeof (Elf_External_Rela));
4643 erela = ((Elf_External_Rela *) output_rel_hdr->contents
4644 + o->output_section->reloc_count);
4645 for (; irela < irelaend; irela++, erela++)
4646 elf_swap_reloca_out (output_bfd, irela, erela);
4647 }
4648
4649 o->output_section->reloc_count += o->reloc_count;
4650 }
4651 }
4652
4653 /* Write out the modified section contents. */
4654 if (elf_section_data (o)->stab_info == NULL)
4655 {
4656 if (! bfd_set_section_contents (output_bfd, o->output_section,
4657 contents, o->output_offset,
4658 (o->_cooked_size != 0
4659 ? o->_cooked_size
4660 : o->_raw_size)))
4661 return false;
4662 }
4663 else
4664 {
4665 if (! (_bfd_write_section_stabs
4666 (output_bfd, &elf_hash_table (finfo->info)->stab_info,
4667 o, &elf_section_data (o)->stab_info, contents)))
4668 return false;
4669 }
4670 }
4671
4672 return true;
4673 }
4674
4675 /* Generate a reloc when linking an ELF file. This is a reloc
4676 requested by the linker, and does come from any input file. This
4677 is used to build constructor and destructor tables when linking
4678 with -Ur. */
4679
4680 static boolean
4681 elf_reloc_link_order (output_bfd, info, output_section, link_order)
4682 bfd *output_bfd;
4683 struct bfd_link_info *info;
4684 asection *output_section;
4685 struct bfd_link_order *link_order;
4686 {
4687 reloc_howto_type *howto;
4688 long indx;
4689 bfd_vma offset;
4690 bfd_vma addend;
4691 struct elf_link_hash_entry **rel_hash_ptr;
4692 Elf_Internal_Shdr *rel_hdr;
4693
4694 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4695 if (howto == NULL)
4696 {
4697 bfd_set_error (bfd_error_bad_value);
4698 return false;
4699 }
4700
4701 addend = link_order->u.reloc.p->addend;
4702
4703 /* Figure out the symbol index. */
4704 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
4705 + output_section->reloc_count);
4706 if (link_order->type == bfd_section_reloc_link_order)
4707 {
4708 indx = link_order->u.reloc.p->u.section->target_index;
4709 BFD_ASSERT (indx != 0);
4710 *rel_hash_ptr = NULL;
4711 }
4712 else
4713 {
4714 struct elf_link_hash_entry *h;
4715
4716 /* Treat a reloc against a defined symbol as though it were
4717 actually against the section. */
4718 h = ((struct elf_link_hash_entry *)
4719 bfd_wrapped_link_hash_lookup (output_bfd, info,
4720 link_order->u.reloc.p->u.name,
4721 false, false, true));
4722 if (h != NULL
4723 && (h->root.type == bfd_link_hash_defined
4724 || h->root.type == bfd_link_hash_defweak))
4725 {
4726 asection *section;
4727
4728 section = h->root.u.def.section;
4729 indx = section->output_section->target_index;
4730 *rel_hash_ptr = NULL;
4731 /* It seems that we ought to add the symbol value to the
4732 addend here, but in practice it has already been added
4733 because it was passed to constructor_callback. */
4734 addend += section->output_section->vma + section->output_offset;
4735 }
4736 else if (h != NULL)
4737 {
4738 /* Setting the index to -2 tells elf_link_output_extsym that
4739 this symbol is used by a reloc. */
4740 h->indx = -2;
4741 *rel_hash_ptr = h;
4742 indx = 0;
4743 }
4744 else
4745 {
4746 if (! ((*info->callbacks->unattached_reloc)
4747 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4748 (asection *) NULL, (bfd_vma) 0)))
4749 return false;
4750 indx = 0;
4751 }
4752 }
4753
4754 /* If this is an inplace reloc, we must write the addend into the
4755 object file. */
4756 if (howto->partial_inplace && addend != 0)
4757 {
4758 bfd_size_type size;
4759 bfd_reloc_status_type rstat;
4760 bfd_byte *buf;
4761 boolean ok;
4762
4763 size = bfd_get_reloc_size (howto);
4764 buf = (bfd_byte *) bfd_zmalloc (size);
4765 if (buf == (bfd_byte *) NULL)
4766 return false;
4767 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
4768 switch (rstat)
4769 {
4770 case bfd_reloc_ok:
4771 break;
4772 default:
4773 case bfd_reloc_outofrange:
4774 abort ();
4775 case bfd_reloc_overflow:
4776 if (! ((*info->callbacks->reloc_overflow)
4777 (info,
4778 (link_order->type == bfd_section_reloc_link_order
4779 ? bfd_section_name (output_bfd,
4780 link_order->u.reloc.p->u.section)
4781 : link_order->u.reloc.p->u.name),
4782 howto->name, addend, (bfd *) NULL, (asection *) NULL,
4783 (bfd_vma) 0)))
4784 {
4785 free (buf);
4786 return false;
4787 }
4788 break;
4789 }
4790 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4791 (file_ptr) link_order->offset, size);
4792 free (buf);
4793 if (! ok)
4794 return false;
4795 }
4796
4797 /* The address of a reloc is relative to the section in a
4798 relocateable file, and is a virtual address in an executable
4799 file. */
4800 offset = link_order->offset;
4801 if (! info->relocateable)
4802 offset += output_section->vma;
4803
4804 rel_hdr = &elf_section_data (output_section)->rel_hdr;
4805
4806 if (rel_hdr->sh_type == SHT_REL)
4807 {
4808 Elf_Internal_Rel irel;
4809 Elf_External_Rel *erel;
4810
4811 irel.r_offset = offset;
4812 irel.r_info = ELF_R_INFO (indx, howto->type);
4813 erel = ((Elf_External_Rel *) rel_hdr->contents
4814 + output_section->reloc_count);
4815 elf_swap_reloc_out (output_bfd, &irel, erel);
4816 }
4817 else
4818 {
4819 Elf_Internal_Rela irela;
4820 Elf_External_Rela *erela;
4821
4822 irela.r_offset = offset;
4823 irela.r_info = ELF_R_INFO (indx, howto->type);
4824 irela.r_addend = addend;
4825 erela = ((Elf_External_Rela *) rel_hdr->contents
4826 + output_section->reloc_count);
4827 elf_swap_reloca_out (output_bfd, &irela, erela);
4828 }
4829
4830 ++output_section->reloc_count;
4831
4832 return true;
4833 }
4834
4835 \f
4836 /* Allocate a pointer to live in a linker created section. */
4837
4838 boolean
4839 elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
4840 bfd *abfd;
4841 struct bfd_link_info *info;
4842 elf_linker_section_t *lsect;
4843 struct elf_link_hash_entry *h;
4844 const Elf_Internal_Rela *rel;
4845 {
4846 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
4847 elf_linker_section_pointers_t *linker_section_ptr;
4848 unsigned long r_symndx = ELF_R_SYM (rel->r_info);;
4849
4850 BFD_ASSERT (lsect != NULL);
4851
4852 /* Is this a global symbol? */
4853 if (h != NULL)
4854 {
4855 /* Has this symbol already been allocated, if so, our work is done */
4856 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
4857 rel->r_addend,
4858 lsect->which))
4859 return true;
4860
4861 ptr_linker_section_ptr = &h->linker_section_pointer;
4862 /* Make sure this symbol is output as a dynamic symbol. */
4863 if (h->dynindx == -1)
4864 {
4865 if (! elf_link_record_dynamic_symbol (info, h))
4866 return false;
4867 }
4868
4869 if (lsect->rel_section)
4870 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
4871 }
4872
4873 else /* Allocation of a pointer to a local symbol */
4874 {
4875 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
4876
4877 /* Allocate a table to hold the local symbols if first time */
4878 if (!ptr)
4879 {
4880 int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
4881 register unsigned int i;
4882
4883 ptr = (elf_linker_section_pointers_t **)
4884 bfd_alloc (abfd, num_symbols * sizeof (elf_linker_section_pointers_t *));
4885
4886 if (!ptr)
4887 return false;
4888
4889 elf_local_ptr_offsets (abfd) = ptr;
4890 for (i = 0; i < num_symbols; i++)
4891 ptr[i] = (elf_linker_section_pointers_t *)0;
4892 }
4893
4894 /* Has this symbol already been allocated, if so, our work is done */
4895 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
4896 rel->r_addend,
4897 lsect->which))
4898 return true;
4899
4900 ptr_linker_section_ptr = &ptr[r_symndx];
4901
4902 if (info->shared)
4903 {
4904 /* If we are generating a shared object, we need to
4905 output a R_<xxx>_RELATIVE reloc so that the
4906 dynamic linker can adjust this GOT entry. */
4907 BFD_ASSERT (lsect->rel_section != NULL);
4908 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
4909 }
4910 }
4911
4912 /* Allocate space for a pointer in the linker section, and allocate a new pointer record
4913 from internal memory. */
4914 BFD_ASSERT (ptr_linker_section_ptr != NULL);
4915 linker_section_ptr = (elf_linker_section_pointers_t *)
4916 bfd_alloc (abfd, sizeof (elf_linker_section_pointers_t));
4917
4918 if (!linker_section_ptr)
4919 return false;
4920
4921 linker_section_ptr->next = *ptr_linker_section_ptr;
4922 linker_section_ptr->addend = rel->r_addend;
4923 linker_section_ptr->which = lsect->which;
4924 linker_section_ptr->written_address_p = false;
4925 *ptr_linker_section_ptr = linker_section_ptr;
4926
4927 #if 0
4928 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
4929 {
4930 linker_section_ptr->offset = lsect->section->_raw_size - lsect->hole_size + (ARCH_SIZE / 8);
4931 lsect->hole_offset += ARCH_SIZE / 8;
4932 lsect->sym_offset += ARCH_SIZE / 8;
4933 if (lsect->sym_hash) /* Bump up symbol value if needed */
4934 {
4935 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
4936 #ifdef DEBUG
4937 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
4938 lsect->sym_hash->root.root.string,
4939 (long)ARCH_SIZE / 8,
4940 (long)lsect->sym_hash->root.u.def.value);
4941 #endif
4942 }
4943 }
4944 else
4945 #endif
4946 linker_section_ptr->offset = lsect->section->_raw_size;
4947
4948 lsect->section->_raw_size += ARCH_SIZE / 8;
4949
4950 #ifdef DEBUG
4951 fprintf (stderr, "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
4952 lsect->name, (long)linker_section_ptr->offset, (long)lsect->section->_raw_size);
4953 #endif
4954
4955 return true;
4956 }
4957
4958 \f
4959 #if ARCH_SIZE==64
4960 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
4961 #endif
4962 #if ARCH_SIZE==32
4963 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
4964 #endif
4965
4966 /* Fill in the address for a pointer generated in alinker section. */
4967
4968 bfd_vma
4969 elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h, relocation, rel, relative_reloc)
4970 bfd *output_bfd;
4971 bfd *input_bfd;
4972 struct bfd_link_info *info;
4973 elf_linker_section_t *lsect;
4974 struct elf_link_hash_entry *h;
4975 bfd_vma relocation;
4976 const Elf_Internal_Rela *rel;
4977 int relative_reloc;
4978 {
4979 elf_linker_section_pointers_t *linker_section_ptr;
4980
4981 BFD_ASSERT (lsect != NULL);
4982
4983 if (h != NULL) /* global symbol */
4984 {
4985 linker_section_ptr = _bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
4986 rel->r_addend,
4987 lsect->which);
4988
4989 BFD_ASSERT (linker_section_ptr != NULL);
4990
4991 if (! elf_hash_table (info)->dynamic_sections_created
4992 || (info->shared
4993 && info->symbolic
4994 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
4995 {
4996 /* This is actually a static link, or it is a
4997 -Bsymbolic link and the symbol is defined
4998 locally. We must initialize this entry in the
4999 global section.
5000
5001 When doing a dynamic link, we create a .rela.<xxx>
5002 relocation entry to initialize the value. This
5003 is done in the finish_dynamic_symbol routine. */
5004 if (!linker_section_ptr->written_address_p)
5005 {
5006 linker_section_ptr->written_address_p = true;
5007 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
5008 lsect->section->contents + linker_section_ptr->offset);
5009 }
5010 }
5011 }
5012 else /* local symbol */
5013 {
5014 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
5015 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
5016 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
5017 linker_section_ptr = _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd)[r_symndx],
5018 rel->r_addend,
5019 lsect->which);
5020
5021 BFD_ASSERT (linker_section_ptr != NULL);
5022
5023 /* Write out pointer if it hasn't been rewritten out before */
5024 if (!linker_section_ptr->written_address_p)
5025 {
5026 linker_section_ptr->written_address_p = true;
5027 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
5028 lsect->section->contents + linker_section_ptr->offset);
5029
5030 if (info->shared)
5031 {
5032 asection *srel = lsect->rel_section;
5033 Elf_Internal_Rela outrel;
5034
5035 /* We need to generate a relative reloc for the dynamic linker. */
5036 if (!srel)
5037 lsect->rel_section = srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
5038 lsect->rel_name);
5039
5040 BFD_ASSERT (srel != NULL);
5041
5042 outrel.r_offset = (lsect->section->output_section->vma
5043 + lsect->section->output_offset
5044 + linker_section_ptr->offset);
5045 outrel.r_info = ELF_R_INFO (0, relative_reloc);
5046 outrel.r_addend = 0;
5047 elf_swap_reloca_out (output_bfd, &outrel,
5048 (((Elf_External_Rela *)
5049 lsect->section->contents)
5050 + lsect->section->reloc_count));
5051 ++lsect->section->reloc_count;
5052 }
5053 }
5054 }
5055
5056 relocation = (lsect->section->output_offset
5057 + linker_section_ptr->offset
5058 - lsect->hole_offset
5059 - lsect->sym_offset);
5060
5061 #ifdef DEBUG
5062 fprintf (stderr, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
5063 lsect->name, (long)relocation, (long)relocation);
5064 #endif
5065
5066 /* Subtract out the addend, because it will get added back in by the normal
5067 processing. */
5068 return relocation - linker_section_ptr->addend;
5069 }