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