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