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