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