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