bfd/
[deliverable/binutils-gdb.git] / bfd / elflink.h
CommitLineData
252b5132 1/* ELF linker support.
79349b09 2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
7898deda 3 Free Software Foundation, Inc.
252b5132 4
ae9a127f 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
ae9a127f
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
252b5132 11
ae9a127f
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
ae9a127f
NC
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
252b5132
RH
20
21/* ELF linker code. */
22
29ca8dc5
NS
23#include "safe-ctype.h"
24
268b6b39 25static bfd_boolean elf_link_add_object_symbols (bfd *, struct bfd_link_info *);
268b6b39
AM
26static bfd_boolean elf_finalize_dynstr (bfd *, struct bfd_link_info *);
27static bfd_boolean elf_collect_hash_codes (struct elf_link_hash_entry *,
28 void *);
29static bfd_boolean elf_section_ignore_discarded_relocs (asection *);
252b5132
RH
30
31/* Given an ELF BFD, add symbols to the global hash table as
32 appropriate. */
33
b34976b6 34bfd_boolean
268b6b39 35elf_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
36{
37 switch (bfd_get_format (abfd))
38 {
39 case bfd_object:
40 return elf_link_add_object_symbols (abfd, info);
41 case bfd_archive:
0ad989f9 42 return _bfd_elf_link_add_archive_symbols (abfd, info);
252b5132
RH
43 default:
44 bfd_set_error (bfd_error_wrong_format);
b34976b6 45 return FALSE;
252b5132
RH
46 }
47}
48\f
b60d7a1a
L
49/* Sort symbol by value and section. */
50static int
51sort_symbol (const void *arg1, const void *arg2)
52{
53 const struct elf_link_hash_entry *h1
54 = *(const struct elf_link_hash_entry **) arg1;
55 const struct elf_link_hash_entry *h2
56 = *(const struct elf_link_hash_entry **) arg2;
57 bfd_signed_vma vdiff = h1->root.u.def.value - h2->root.u.def.value;
58
59 if (vdiff)
60 return vdiff > 0 ? 1 : -1;
61 else
62 {
63 long sdiff = h1->root.u.def.section - h2->root.u.def.section;
64 if (sdiff)
65 return sdiff > 0 ? 1 : -1;
66 else
67 return 0;
68 }
69}
70
4a43e768
AM
71/* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
72 1 if a DT_NEEDED tag already exists, and 0 on success. */
73
74static int
75add_dt_needed_tag (struct bfd_link_info *info, const char *soname,
76 bfd_boolean do_it)
77{
78 struct elf_link_hash_table *hash_table;
79 bfd_size_type oldsize;
80 bfd_size_type strindex;
81
82 hash_table = elf_hash_table (info);
83 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
84 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
85 if (strindex == (bfd_size_type) -1)
86 return -1;
87
88 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
89 {
90 asection *sdyn;
91 Elf_External_Dyn *dyncon, *dynconend;
92
93 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
94 BFD_ASSERT (sdyn != NULL);
95
96 dyncon = (Elf_External_Dyn *) sdyn->contents;
97 dynconend = (Elf_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
98 for (; dyncon < dynconend; dyncon++)
99 {
100 Elf_Internal_Dyn dyn;
101
102 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
103 if (dyn.d_tag == DT_NEEDED
104 && dyn.d_un.d_val == strindex)
105 {
106 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
107 return 1;
108 }
109 }
110 }
111
112 if (do_it)
113 {
114 if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
115 return -1;
116 }
117 else
118 /* We were just checking for existence of the tag. */
119 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
120
121 return 0;
122}
123
45d6a902 124/* Add symbols from an ELF object file to the linker hash table. */
252b5132 125
b34976b6 126static bfd_boolean
268b6b39 127elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
252b5132 128{
45d6a902 129 bfd_boolean (*add_symbol_hook)
268b6b39
AM
130 (bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
131 const char **, flagword *, asection **, bfd_vma *);
45d6a902 132 bfd_boolean (*check_relocs)
268b6b39 133 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
45d6a902
AM
134 bfd_boolean collect;
135 Elf_Internal_Shdr *hdr;
136 bfd_size_type symcount;
137 bfd_size_type extsymcount;
138 bfd_size_type extsymoff;
139 struct elf_link_hash_entry **sym_hash;
140 bfd_boolean dynamic;
141 Elf_External_Versym *extversym = NULL;
142 Elf_External_Versym *ever;
143 struct elf_link_hash_entry *weaks;
144 struct elf_link_hash_entry **nondeflt_vers = NULL;
145 bfd_size_type nondeflt_vers_cnt = 0;
146 Elf_Internal_Sym *isymbuf = NULL;
147 Elf_Internal_Sym *isym;
148 Elf_Internal_Sym *isymend;
9c5bfbb7 149 const struct elf_backend_data *bed;
45d6a902 150 bfd_boolean dt_needed;
79349b09 151 bfd_boolean add_needed;
45d6a902
AM
152 struct elf_link_hash_table * hash_table;
153 bfd_size_type amt;
252b5132 154
45d6a902 155 hash_table = elf_hash_table (info);
252b5132 156
45d6a902
AM
157 bed = get_elf_backend_data (abfd);
158 add_symbol_hook = bed->elf_add_symbol_hook;
159 collect = bed->collect;
252b5132 160
45d6a902
AM
161 if ((abfd->flags & DYNAMIC) == 0)
162 dynamic = FALSE;
252b5132 163 else
252b5132 164 {
45d6a902 165 dynamic = TRUE;
252b5132 166
45d6a902
AM
167 /* You can't use -r against a dynamic object. Also, there's no
168 hope of using a dynamic object which does not exactly match
169 the format of the output file. */
0eddce27
AM
170 if (info->relocatable
171 || !is_elf_hash_table (hash_table)
172 || hash_table->root.creator != abfd->xvec)
45d6a902
AM
173 {
174 bfd_set_error (bfd_error_invalid_operation);
175 goto error_return;
176 }
252b5132
RH
177 }
178
45d6a902
AM
179 /* As a GNU extension, any input sections which are named
180 .gnu.warning.SYMBOL are treated as warning symbols for the given
181 symbol. This differs from .gnu.warning sections, which generate
182 warnings when they are included in an output file. */
36af4a4e 183 if (info->executable)
45d6a902
AM
184 {
185 asection *s;
b4536acd 186
45d6a902
AM
187 for (s = abfd->sections; s != NULL; s = s->next)
188 {
189 const char *name;
252b5132 190
45d6a902
AM
191 name = bfd_get_section_name (abfd, s);
192 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
193 {
194 char *msg;
195 bfd_size_type sz;
8c675694
NC
196 bfd_size_type prefix_len;
197 const char * gnu_warning_prefix = _("warning: ");
252b5132 198
45d6a902 199 name += sizeof ".gnu.warning." - 1;
0035bd7b 200
45d6a902
AM
201 /* If this is a shared object, then look up the symbol
202 in the hash table. If it is there, and it is already
203 been defined, then we will not be using the entry
204 from this shared object, so we don't need to warn.
205 FIXME: If we see the definition in a regular object
206 later on, we will warn, but we shouldn't. The only
207 fix is to keep track of what warnings we are supposed
208 to emit, and then handle them all at the end of the
209 link. */
0eddce27 210 if (dynamic)
45d6a902
AM
211 {
212 struct elf_link_hash_entry *h;
0035bd7b 213
45d6a902
AM
214 h = elf_link_hash_lookup (hash_table, name,
215 FALSE, FALSE, TRUE);
0035bd7b 216
45d6a902
AM
217 /* FIXME: What about bfd_link_hash_common? */
218 if (h != NULL
219 && (h->root.type == bfd_link_hash_defined
220 || h->root.type == bfd_link_hash_defweak))
221 {
222 /* We don't want to issue this warning. Clobber
223 the section size so that the warning does not
224 get copied into the output file. */
225 s->_raw_size = 0;
226 continue;
227 }
228 }
0035bd7b 229
45d6a902 230 sz = bfd_section_size (abfd, s);
8c675694
NC
231 prefix_len = strlen (gnu_warning_prefix);
232 msg = bfd_alloc (abfd, prefix_len + sz + 1);
45d6a902
AM
233 if (msg == NULL)
234 goto error_return;
252b5132 235
8c675694
NC
236 strcpy (msg, gnu_warning_prefix);
237 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
45d6a902 238 goto error_return;
252b5132 239
8c675694 240 msg[prefix_len + sz] = '\0';
252b5132 241
45d6a902 242 if (! (_bfd_generic_link_add_one_symbol
268b6b39
AM
243 (info, abfd, name, BSF_WARNING, s, 0, msg,
244 FALSE, collect, NULL)))
45d6a902 245 goto error_return;
252b5132 246
1049f94e 247 if (! info->relocatable)
45d6a902
AM
248 {
249 /* Clobber the section size so that the warning does
250 not get copied into the output file. */
251 s->_raw_size = 0;
252 }
1b1fe8fe 253 }
d31e3dfe 254 }
1b1fe8fe
L
255 }
256
45d6a902 257 dt_needed = FALSE;
4a43e768 258 add_needed = TRUE;
45d6a902 259 if (! dynamic)
c424e0e9 260 {
45d6a902
AM
261 /* If we are creating a shared library, create all the dynamic
262 sections immediately. We need to attach them to something,
263 so we attach them to this BFD, provided it is the right
264 format. FIXME: If there are no input BFD's of the same
265 format as the output, we can't make a shared library. */
266 if (info->shared
0eddce27
AM
267 && is_elf_hash_table (hash_table)
268 && hash_table->root.creator == abfd->xvec
269 && ! hash_table->dynamic_sections_created)
c424e0e9 270 {
45d6a902
AM
271 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
272 goto error_return;
c424e0e9
L
273 }
274 }
0eddce27 275 else if (!is_elf_hash_table (hash_table))
45d6a902 276 goto error_return;
c424e0e9 277 else
c424e0e9 278 {
45d6a902 279 asection *s;
4a43e768 280 const char *soname = NULL;
45d6a902 281 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4a43e768 282 int ret;
252b5132 283
45d6a902
AM
284 /* ld --just-symbols and dynamic objects don't mix very well.
285 Test for --just-symbols by looking at info set up by
286 _bfd_elf_link_just_syms. */
287 if ((s = abfd->sections) != NULL
288 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
289 goto error_return;
252b5132 290
4a43e768
AM
291 /* If this dynamic lib was specified on the command line with
292 --as-needed in effect, then we don't want to add a DT_NEEDED
293 tag unless the lib is actually used.
294 For libs brought in by another lib's DT_NEEDED we do the same,
295 and also modify handling of weak syms. */
296 switch elf_dyn_lib_class (abfd)
297 {
298 case DYN_NORMAL:
299 break;
300 case DYN_DT_NEEDED:
301 dt_needed = TRUE;
302 /* Fall thru */
303 case DYN_AS_NEEDED:
304 add_needed = FALSE;
45d6a902 305 }
4a43e768 306
45d6a902
AM
307 s = bfd_get_section_by_name (abfd, ".dynamic");
308 if (s != NULL)
309 {
310 Elf_External_Dyn *dynbuf = NULL;
311 Elf_External_Dyn *extdyn;
312 Elf_External_Dyn *extdynend;
313 int elfsec;
314 unsigned long shlink;
252b5132 315
268b6b39 316 dynbuf = bfd_malloc (s->_raw_size);
45d6a902
AM
317 if (dynbuf == NULL)
318 goto error_return;
252b5132 319
268b6b39 320 if (! bfd_get_section_contents (abfd, s, dynbuf, 0, s->_raw_size))
45d6a902 321 goto error_free_dyn;
252b5132 322
45d6a902
AM
323 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
324 if (elfsec == -1)
325 goto error_free_dyn;
326 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132 327
45d6a902
AM
328 extdyn = dynbuf;
329 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
330 for (; extdyn < extdynend; extdyn++)
331 {
332 Elf_Internal_Dyn dyn;
252b5132 333
45d6a902
AM
334 elf_swap_dyn_in (abfd, extdyn, &dyn);
335 if (dyn.d_tag == DT_SONAME)
336 {
337 unsigned int tagv = dyn.d_un.d_val;
4a43e768
AM
338 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
339 if (soname == NULL)
45d6a902
AM
340 goto error_free_dyn;
341 }
342 if (dyn.d_tag == DT_NEEDED)
343 {
344 struct bfd_link_needed_list *n, **pn;
345 char *fnm, *anm;
346 unsigned int tagv = dyn.d_un.d_val;
252b5132 347
45d6a902 348 amt = sizeof (struct bfd_link_needed_list);
268b6b39 349 n = bfd_alloc (abfd, amt);
45d6a902
AM
350 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
351 if (n == NULL || fnm == NULL)
352 goto error_free_dyn;
353 amt = strlen (fnm) + 1;
354 anm = bfd_alloc (abfd, amt);
355 if (anm == NULL)
356 goto error_free_dyn;
268b6b39 357 memcpy (anm, fnm, amt);
45d6a902
AM
358 n->name = anm;
359 n->by = abfd;
360 n->next = NULL;
361 for (pn = & hash_table->needed;
362 *pn != NULL;
363 pn = &(*pn)->next)
364 ;
365 *pn = n;
366 }
367 if (dyn.d_tag == DT_RUNPATH)
368 {
369 struct bfd_link_needed_list *n, **pn;
370 char *fnm, *anm;
371 unsigned int tagv = dyn.d_un.d_val;
252b5132 372
45d6a902 373 amt = sizeof (struct bfd_link_needed_list);
268b6b39 374 n = bfd_alloc (abfd, amt);
45d6a902
AM
375 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
376 if (n == NULL || fnm == NULL)
377 goto error_free_dyn;
378 amt = strlen (fnm) + 1;
379 anm = bfd_alloc (abfd, amt);
380 if (anm == NULL)
381 goto error_free_dyn;
268b6b39 382 memcpy (anm, fnm, amt);
45d6a902
AM
383 n->name = anm;
384 n->by = abfd;
385 n->next = NULL;
386 for (pn = & runpath;
387 *pn != NULL;
388 pn = &(*pn)->next)
389 ;
390 *pn = n;
391 }
392 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
393 if (!runpath && dyn.d_tag == DT_RPATH)
394 {
395 struct bfd_link_needed_list *n, **pn;
396 char *fnm, *anm;
397 unsigned int tagv = dyn.d_un.d_val;
252b5132 398
45d6a902 399 amt = sizeof (struct bfd_link_needed_list);
268b6b39 400 n = bfd_alloc (abfd, amt);
45d6a902
AM
401 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
402 if (n == NULL || fnm == NULL)
403 goto error_free_dyn;
404 amt = strlen (fnm) + 1;
405 anm = bfd_alloc (abfd, amt);
406 if (anm == NULL)
407 {
408 error_free_dyn:
409 free (dynbuf);
410 goto error_return;
411 }
268b6b39 412 memcpy (anm, fnm, amt);
45d6a902
AM
413 n->name = anm;
414 n->by = abfd;
415 n->next = NULL;
416 for (pn = & rpath;
417 *pn != NULL;
418 pn = &(*pn)->next)
419 ;
420 *pn = n;
421 }
422 }
252b5132 423
45d6a902
AM
424 free (dynbuf);
425 }
252b5132 426
45d6a902
AM
427 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
428 frees all more recently bfd_alloc'd blocks as well. */
429 if (runpath)
430 rpath = runpath;
3c0a515d 431
45d6a902 432 if (rpath)
3c0a515d 433 {
45d6a902
AM
434 struct bfd_link_needed_list **pn;
435 for (pn = & hash_table->runpath;
436 *pn != NULL;
437 pn = &(*pn)->next)
438 ;
439 *pn = rpath;
3c0a515d 440 }
252b5132 441
45d6a902
AM
442 /* We do not want to include any of the sections in a dynamic
443 object in the output file. We hack by simply clobbering the
444 list of sections in the BFD. This could be handled more
445 cleanly by, say, a new section flag; the existing
446 SEC_NEVER_LOAD flag is not the one we want, because that one
447 still implies that the section takes up space in the output
448 file. */
449 bfd_section_list_clear (abfd);
0525d26e 450
45d6a902
AM
451 /* If this is the first dynamic object found in the link, create
452 the special sections required for dynamic linking. */
0eddce27
AM
453 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
454 goto error_return;
0525d26e 455
4a43e768
AM
456 /* Find the name to use in a DT_NEEDED entry that refers to this
457 object. If the object has a DT_SONAME entry, we use it.
458 Otherwise, if the generic linker stuck something in
459 elf_dt_name, we use that. Otherwise, we just use the file
460 name. */
461 if (soname == NULL || *soname == '\0')
45d6a902 462 {
4a43e768
AM
463 soname = elf_dt_name (abfd);
464 if (soname == NULL || *soname == '\0')
465 soname = bfd_get_filename (abfd);
466 }
215007a6 467
4a43e768
AM
468 /* Save the SONAME because sometimes the linker emulation code
469 will need to know it. */
470 elf_dt_name (abfd) = soname;
45d6a902 471
4a43e768
AM
472 ret = add_dt_needed_tag (info, soname, add_needed);
473 if (ret < 0)
474 goto error_return;
45d6a902 475
4a43e768
AM
476 /* If we have already included this dynamic object in the
477 link, just ignore it. There is no reason to include a
478 particular dynamic object more than once. */
479 if (ret > 0)
480 return TRUE;
215007a6
L
481 }
482
45d6a902
AM
483 /* If this is a dynamic object, we always link against the .dynsym
484 symbol table, not the .symtab symbol table. The dynamic linker
485 will only see the .dynsym symbol table, so there is no reason to
486 look at .symtab for a dynamic object. */
215007a6 487
45d6a902
AM
488 if (! dynamic || elf_dynsymtab (abfd) == 0)
489 hdr = &elf_tdata (abfd)->symtab_hdr;
490 else
491 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
215007a6 492
45d6a902 493 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
d31e3dfe 494
45d6a902
AM
495 /* The sh_info field of the symtab header tells us where the
496 external symbols start. We don't care about the local symbols at
497 this point. */
498 if (elf_bad_symtab (abfd))
215007a6 499 {
45d6a902
AM
500 extsymcount = symcount;
501 extsymoff = 0;
215007a6
L
502 }
503 else
504 {
45d6a902
AM
505 extsymcount = symcount - hdr->sh_info;
506 extsymoff = hdr->sh_info;
215007a6
L
507 }
508
45d6a902
AM
509 sym_hash = NULL;
510 if (extsymcount != 0)
215007a6 511 {
45d6a902
AM
512 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
513 NULL, NULL, NULL);
514 if (isymbuf == NULL)
515 goto error_return;
215007a6 516
45d6a902
AM
517 /* We store a pointer to the hash table entry for each external
518 symbol. */
519 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
268b6b39 520 sym_hash = bfd_alloc (abfd, amt);
45d6a902
AM
521 if (sym_hash == NULL)
522 goto error_free_sym;
523 elf_sym_hashes (abfd) = sym_hash;
524 }
215007a6 525
45d6a902
AM
526 if (dynamic)
527 {
528 /* Read in any version definitions. */
529 if (! _bfd_elf_slurp_version_tables (abfd))
530 goto error_free_sym;
215007a6 531
45d6a902
AM
532 /* Read in the symbol versions, but don't bother to convert them
533 to internal format. */
534 if (elf_dynversym (abfd) != 0)
215007a6 535 {
45d6a902
AM
536 Elf_Internal_Shdr *versymhdr;
537
538 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
268b6b39 539 extversym = bfd_malloc (versymhdr->sh_size);
45d6a902
AM
540 if (extversym == NULL)
541 goto error_free_sym;
542 amt = versymhdr->sh_size;
543 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
268b6b39 544 || bfd_bread (extversym, amt, abfd) != amt)
45d6a902 545 goto error_free_vers;
215007a6
L
546 }
547 }
548
45d6a902 549 weaks = NULL;
215007a6 550
45d6a902
AM
551 ever = extversym != NULL ? extversym + extsymoff : NULL;
552 for (isym = isymbuf, isymend = isymbuf + extsymcount;
553 isym < isymend;
554 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
555 {
556 int bind;
557 bfd_vma value;
558 asection *sec;
559 flagword flags;
560 const char *name;
561 struct elf_link_hash_entry *h;
562 bfd_boolean definition;
563 bfd_boolean size_change_ok;
564 bfd_boolean type_change_ok;
565 bfd_boolean new_weakdef;
566 bfd_boolean override;
567 unsigned int old_alignment;
568 bfd *old_bfd;
215007a6 569
45d6a902 570 override = FALSE;
d31e3dfe 571
45d6a902
AM
572 flags = BSF_NO_FLAGS;
573 sec = NULL;
574 value = isym->st_value;
575 *sym_hash = NULL;
215007a6 576
45d6a902
AM
577 bind = ELF_ST_BIND (isym->st_info);
578 if (bind == STB_LOCAL)
579 {
580 /* This should be impossible, since ELF requires that all
581 global symbols follow all local symbols, and that sh_info
4cc11e76 582 point to the first global symbol. Unfortunately, Irix 5
45d6a902
AM
583 screws this up. */
584 continue;
585 }
586 else if (bind == STB_GLOBAL)
587 {
588 if (isym->st_shndx != SHN_UNDEF
589 && isym->st_shndx != SHN_COMMON)
590 flags = BSF_GLOBAL;
591 }
592 else if (bind == STB_WEAK)
593 flags = BSF_WEAK;
594 else
595 {
596 /* Leave it up to the processor backend. */
597 }
215007a6 598
45d6a902
AM
599 if (isym->st_shndx == SHN_UNDEF)
600 sec = bfd_und_section_ptr;
601 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
602 {
603 sec = section_from_elf_index (abfd, isym->st_shndx);
604 if (sec == NULL)
605 sec = bfd_abs_section_ptr;
606 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
607 value -= sec->vma;
608 }
609 else if (isym->st_shndx == SHN_ABS)
610 sec = bfd_abs_section_ptr;
611 else if (isym->st_shndx == SHN_COMMON)
612 {
613 sec = bfd_com_section_ptr;
614 /* What ELF calls the size we call the value. What ELF
615 calls the value we call the alignment. */
616 value = isym->st_size;
617 }
618 else
215007a6 619 {
45d6a902
AM
620 /* Leave it up to the processor backend. */
621 }
622
623 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
624 isym->st_name);
268b6b39 625 if (name == NULL)
45d6a902 626 goto error_free_vers;
215007a6 627
45d6a902
AM
628 if (isym->st_shndx == SHN_COMMON
629 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
630 {
631 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
215007a6 632
45d6a902 633 if (tcomm == NULL)
215007a6 634 {
45d6a902
AM
635 tcomm = bfd_make_section (abfd, ".tcommon");
636 if (tcomm == NULL
637 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
638 | SEC_IS_COMMON
639 | SEC_LINKER_CREATED
640 | SEC_THREAD_LOCAL)))
641 goto error_free_vers;
215007a6 642 }
45d6a902 643 sec = tcomm;
215007a6 644 }
45d6a902
AM
645 else if (add_symbol_hook)
646 {
647 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
648 &value))
649 goto error_free_vers;
252b5132 650
45d6a902
AM
651 /* The hook function sets the name to NULL if this symbol
652 should be skipped for some reason. */
268b6b39 653 if (name == NULL)
45d6a902
AM
654 continue;
655 }
252b5132 656
45d6a902 657 /* Sanity check that all possibilities were handled. */
268b6b39 658 if (sec == NULL)
252b5132 659 {
45d6a902
AM
660 bfd_set_error (bfd_error_bad_value);
661 goto error_free_vers;
252b5132 662 }
252b5132 663
45d6a902
AM
664 if (bfd_is_und_section (sec)
665 || bfd_is_com_section (sec))
666 definition = FALSE;
667 else
668 definition = TRUE;
252b5132 669
45d6a902
AM
670 size_change_ok = FALSE;
671 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
672 old_alignment = 0;
673 old_bfd = NULL;
674
0eddce27 675 if (is_elf_hash_table (hash_table))
252b5132 676 {
45d6a902
AM
677 Elf_Internal_Versym iver;
678 unsigned int vernum = 0;
679 bfd_boolean skip;
252b5132 680
45d6a902 681 if (ever != NULL)
252b5132 682 {
45d6a902
AM
683 _bfd_elf_swap_versym_in (abfd, ever, &iver);
684 vernum = iver.vs_vers & VERSYM_VERSION;
252b5132 685
45d6a902
AM
686 /* If this is a hidden symbol, or if it is not version
687 1, we append the version name to the symbol name.
688 However, we do not modify a non-hidden absolute
689 symbol, because it might be the version symbol
690 itself. FIXME: What if it isn't? */
691 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
692 || (vernum > 1 && ! bfd_is_abs_section (sec)))
252b5132 693 {
45d6a902
AM
694 const char *verstr;
695 size_t namelen, verlen, newlen;
696 char *newname, *p;
252b5132 697
45d6a902 698 if (isym->st_shndx != SHN_UNDEF)
252b5132 699 {
45d6a902
AM
700 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
701 {
702 (*_bfd_error_handler)
703 (_("%s: %s: invalid version %u (max %d)"),
704 bfd_archive_filename (abfd), name, vernum,
705 elf_tdata (abfd)->dynverdef_hdr.sh_info);
706 bfd_set_error (bfd_error_bad_value);
707 goto error_free_vers;
708 }
709 else if (vernum > 1)
710 verstr =
711 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
712 else
713 verstr = "";
6cdc0ccc 714 }
45d6a902
AM
715 else
716 {
717 /* We cannot simply test for the number of
718 entries in the VERNEED section since the
719 numbers for the needed versions do not start
720 at 0. */
721 Elf_Internal_Verneed *t;
252b5132 722
45d6a902
AM
723 verstr = NULL;
724 for (t = elf_tdata (abfd)->verref;
725 t != NULL;
726 t = t->vn_nextref)
727 {
728 Elf_Internal_Vernaux *a;
252b5132 729
45d6a902
AM
730 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
731 {
732 if (a->vna_other == vernum)
733 {
734 verstr = a->vna_nodename;
735 break;
736 }
737 }
738 if (a != NULL)
739 break;
740 }
741 if (verstr == NULL)
742 {
743 (*_bfd_error_handler)
744 (_("%s: %s: invalid needed version %d"),
745 bfd_archive_filename (abfd), name, vernum);
746 bfd_set_error (bfd_error_bad_value);
747 goto error_free_vers;
748 }
749 }
252b5132 750
45d6a902
AM
751 namelen = strlen (name);
752 verlen = strlen (verstr);
753 newlen = namelen + verlen + 2;
754 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
755 && isym->st_shndx != SHN_UNDEF)
756 ++newlen;
252b5132 757
268b6b39 758 newname = bfd_alloc (abfd, newlen);
45d6a902
AM
759 if (newname == NULL)
760 goto error_free_vers;
761 memcpy (newname, name, namelen);
762 p = newname + namelen;
763 *p++ = ELF_VER_CHR;
764 /* If this is a defined non-hidden version symbol,
765 we add another @ to the name. This indicates the
766 default version of the symbol. */
767 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
768 && isym->st_shndx != SHN_UNDEF)
769 *p++ = ELF_VER_CHR;
770 memcpy (p, verstr, verlen + 1);
252b5132 771
45d6a902
AM
772 name = newname;
773 }
774 }
252b5132 775
45d6a902
AM
776 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
777 sym_hash, &skip, &override,
778 &type_change_ok, &size_change_ok,
779 dt_needed))
780 goto error_free_vers;
252b5132 781
45d6a902
AM
782 if (skip)
783 continue;
252b5132 784
45d6a902
AM
785 if (override)
786 definition = FALSE;
252b5132 787
45d6a902
AM
788 h = *sym_hash;
789 while (h->root.type == bfd_link_hash_indirect
790 || h->root.type == bfd_link_hash_warning)
791 h = (struct elf_link_hash_entry *) h->root.u.i.link;
252b5132 792
45d6a902
AM
793 /* Remember the old alignment if this is a common symbol, so
794 that we don't reduce the alignment later on. We can't
795 check later, because _bfd_generic_link_add_one_symbol
796 will set a default for the alignment which we want to
797 override. We also remember the old bfd where the existing
798 definition comes from. */
799 switch (h->root.type)
800 {
801 default:
802 break;
252b5132 803
45d6a902
AM
804 case bfd_link_hash_defined:
805 case bfd_link_hash_defweak:
806 old_bfd = h->root.u.def.section->owner;
807 break;
79349b09 808
45d6a902
AM
809 case bfd_link_hash_common:
810 old_bfd = h->root.u.c.p->section->owner;
811 old_alignment = h->root.u.c.p->alignment_power;
812 break;
813 }
252b5132 814
45d6a902
AM
815 if (elf_tdata (abfd)->verdef != NULL
816 && ! override
817 && vernum > 1
818 && definition)
819 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
820 }
252b5132 821
45d6a902 822 if (! (_bfd_generic_link_add_one_symbol
268b6b39
AM
823 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
824 (struct bfd_link_hash_entry **) sym_hash)))
45d6a902 825 goto error_free_vers;
252b5132 826
45d6a902
AM
827 h = *sym_hash;
828 while (h->root.type == bfd_link_hash_indirect
829 || h->root.type == bfd_link_hash_warning)
830 h = (struct elf_link_hash_entry *) h->root.u.i.link;
831 *sym_hash = h;
252b5132 832
45d6a902
AM
833 new_weakdef = FALSE;
834 if (dynamic
835 && definition
836 && (flags & BSF_WEAK) != 0
837 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
0eddce27 838 && is_elf_hash_table (hash_table)
45d6a902
AM
839 && h->weakdef == NULL)
840 {
841 /* Keep a list of all weak defined non function symbols from
842 a dynamic object, using the weakdef field. Later in this
843 function we will set the weakdef field to the correct
844 value. We only put non-function symbols from dynamic
845 objects on this list, because that happens to be the only
846 time we need to know the normal symbol corresponding to a
847 weak symbol, and the information is time consuming to
848 figure out. If the weakdef field is not already NULL,
849 then this symbol was already defined by some previous
850 dynamic object, and we will be using that previous
851 definition anyhow. */
252b5132 852
45d6a902
AM
853 h->weakdef = weaks;
854 weaks = h;
855 new_weakdef = TRUE;
252b5132
RH
856 }
857
45d6a902
AM
858 /* Set the alignment of a common symbol. */
859 if (isym->st_shndx == SHN_COMMON
860 && h->root.type == bfd_link_hash_common)
252b5132 861 {
45d6a902
AM
862 unsigned int align;
863
864 align = bfd_log2 (isym->st_value);
865 if (align > old_alignment
866 /* Permit an alignment power of zero if an alignment of one
867 is specified and no other alignments have been specified. */
868 || (isym->st_value == 1 && old_alignment == 0))
869 h->root.u.c.p->alignment_power = align;
870 else
871 h->root.u.c.p->alignment_power = old_alignment;
252b5132 872 }
252b5132 873
0eddce27 874 if (is_elf_hash_table (hash_table))
45d6a902
AM
875 {
876 int old_flags;
877 bfd_boolean dynsym;
878 int new_flag;
879
880 /* Check the alignment when a common symbol is involved. This
4cc11e76 881 can change when a common symbol is overridden by a normal
45d6a902
AM
882 definition or a common symbol is ignored due to the old
883 normal definition. We need to make sure the maximum
884 alignment is maintained. */
885 if ((old_alignment || isym->st_shndx == SHN_COMMON)
886 && h->root.type != bfd_link_hash_common)
887 {
888 unsigned int common_align;
889 unsigned int normal_align;
890 unsigned int symbol_align;
891 bfd *normal_bfd;
892 bfd *common_bfd;
893
894 symbol_align = ffs (h->root.u.def.value) - 1;
40a0491a
AM
895 if (h->root.u.def.section->owner != NULL
896 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
45d6a902
AM
897 {
898 normal_align = h->root.u.def.section->alignment_power;
899 if (normal_align > symbol_align)
900 normal_align = symbol_align;
901 }
902 else
903 normal_align = symbol_align;
904
905 if (old_alignment)
906 {
907 common_align = old_alignment;
908 common_bfd = old_bfd;
909 normal_bfd = abfd;
910 }
911 else
912 {
913 common_align = bfd_log2 (isym->st_value);
914 common_bfd = abfd;
915 normal_bfd = old_bfd;
916 }
917
918 if (normal_align < common_align)
919 (*_bfd_error_handler)
920 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
921 1 << normal_align,
922 name,
923 bfd_archive_filename (normal_bfd),
924 1 << common_align,
925 bfd_archive_filename (common_bfd));
926 }
252b5132 927
45d6a902
AM
928 /* Remember the symbol size and type. */
929 if (isym->st_size != 0
930 && (definition || h->size == 0))
931 {
932 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
933 (*_bfd_error_handler)
934 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
935 name, (unsigned long) h->size,
936 bfd_archive_filename (old_bfd),
937 (unsigned long) isym->st_size,
938 bfd_archive_filename (abfd));
252b5132 939
45d6a902
AM
940 h->size = isym->st_size;
941 }
252b5132 942
45d6a902
AM
943 /* If this is a common symbol, then we always want H->SIZE
944 to be the size of the common symbol. The code just above
945 won't fix the size if a common symbol becomes larger. We
946 don't warn about a size change here, because that is
947 covered by --warn-common. */
948 if (h->root.type == bfd_link_hash_common)
949 h->size = h->root.u.c.size;
252b5132 950
45d6a902
AM
951 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
952 && (definition || h->type == STT_NOTYPE))
953 {
954 if (h->type != STT_NOTYPE
955 && h->type != ELF_ST_TYPE (isym->st_info)
956 && ! type_change_ok)
957 (*_bfd_error_handler)
958 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
959 name, h->type, ELF_ST_TYPE (isym->st_info),
960 bfd_archive_filename (abfd));
252b5132 961
45d6a902
AM
962 h->type = ELF_ST_TYPE (isym->st_info);
963 }
252b5132 964
45d6a902
AM
965 /* If st_other has a processor-specific meaning, specific
966 code might be needed here. We never merge the visibility
967 attribute with the one from a dynamic object. */
9bf7216d
KK
968 if (bed->elf_backend_merge_symbol_attribute)
969 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
970 dynamic);
971
45d6a902
AM
972 if (isym->st_other != 0 && !dynamic)
973 {
974 unsigned char hvis, symvis, other, nvis;
252b5132 975
45d6a902
AM
976 /* Take the balance of OTHER from the definition. */
977 other = (definition ? isym->st_other : h->other);
978 other &= ~ ELF_ST_VISIBILITY (-1);
8ea2e4bd 979
45d6a902
AM
980 /* Combine visibilities, using the most constraining one. */
981 hvis = ELF_ST_VISIBILITY (h->other);
982 symvis = ELF_ST_VISIBILITY (isym->st_other);
983 if (! hvis)
984 nvis = symvis;
985 else if (! symvis)
986 nvis = hvis;
987 else
988 nvis = hvis < symvis ? hvis : symvis;
51b64d56 989
45d6a902
AM
990 h->other = other | nvis;
991 }
252b5132 992
45d6a902
AM
993 /* Set a flag in the hash table entry indicating the type of
994 reference or definition we just found. Keep a count of
995 the number of dynamic symbols we find. A dynamic symbol
996 is one which is referenced or defined by both a regular
997 object and a shared object. */
998 old_flags = h->elf_link_hash_flags;
999 dynsym = FALSE;
1000 if (! dynamic)
1001 {
1002 if (! definition)
1003 {
1004 new_flag = ELF_LINK_HASH_REF_REGULAR;
1005 if (bind != STB_WEAK)
1006 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
1007 }
1008 else
1009 new_flag = ELF_LINK_HASH_DEF_REGULAR;
36af4a4e 1010 if (! info->executable
45d6a902
AM
1011 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
1012 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
1013 dynsym = TRUE;
1014 }
1015 else
1016 {
1017 if (! definition)
1018 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
1019 else
1020 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
1021 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
1022 | ELF_LINK_HASH_REF_REGULAR)) != 0
1023 || (h->weakdef != NULL
1024 && ! new_weakdef
1025 && h->weakdef->dynindx != -1))
1026 dynsym = TRUE;
1027 }
252b5132 1028
45d6a902 1029 h->elf_link_hash_flags |= new_flag;
252b5132 1030
45d6a902
AM
1031 /* Check to see if we need to add an indirect symbol for
1032 the default name. */
1033 if (definition || h->root.type == bfd_link_hash_common)
1034 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
1035 &sec, &value, &dynsym,
1036 override, dt_needed))
1037 goto error_free_vers;
68f69152 1038
45d6a902
AM
1039 if (definition && !dynamic)
1040 {
1041 char *p = strchr (name, ELF_VER_CHR);
1042 if (p != NULL && p[1] != ELF_VER_CHR)
1043 {
1044 /* Queue non-default versions so that .symver x, x@FOO
1045 aliases can be checked. */
1046 if (! nondeflt_vers)
1047 {
1048 amt = (isymend - isym + 1)
1049 * sizeof (struct elf_link_hash_entry *);
1050 nondeflt_vers = bfd_malloc (amt);
1051 }
1052 nondeflt_vers [nondeflt_vers_cnt++] = h;
1053 }
1054 }
252b5132 1055
45d6a902
AM
1056 if (dynsym && h->dynindx == -1)
1057 {
1058 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1059 goto error_free_vers;
1060 if (h->weakdef != NULL
1061 && ! new_weakdef
1062 && h->weakdef->dynindx == -1)
1063 {
1064 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
1065 goto error_free_vers;
1066 }
1067 }
1068 else if (dynsym && h->dynindx != -1)
1069 /* If the symbol already has a dynamic index, but
1070 visibility says it should not be visible, turn it into
1071 a local symbol. */
1072 switch (ELF_ST_VISIBILITY (h->other))
1073 {
1074 case STV_INTERNAL:
1075 case STV_HIDDEN:
1076 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1077 break;
1078 }
252b5132 1079
4a43e768 1080 if (!add_needed && definition
45d6a902
AM
1081 && (h->elf_link_hash_flags
1082 & ELF_LINK_HASH_REF_REGULAR) != 0)
1083 {
4a43e768 1084 int ret;
252b5132 1085
4a43e768
AM
1086 /* A symbol from a library loaded via DT_NEEDED of some
1087 other library is referenced by a regular object.
1088 Add a DT_NEEDED entry for it. */
79349b09 1089 add_needed = TRUE;
4a43e768
AM
1090 ret = add_dt_needed_tag (info, elf_dt_name (abfd), add_needed);
1091 if (ret < 0)
45d6a902 1092 goto error_free_vers;
252b5132 1093
4a43e768 1094 BFD_ASSERT (ret == 0);
252b5132
RH
1095 }
1096 }
45d6a902 1097 }
252b5132 1098
45d6a902
AM
1099 /* Now that all the symbols from this input file are created, handle
1100 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
1101 if (nondeflt_vers != NULL)
1102 {
1103 bfd_size_type cnt, symidx;
391a809a 1104
45d6a902 1105 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
ea44b734 1106 {
45d6a902
AM
1107 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
1108 char *shortname, *p;
ea44b734 1109
45d6a902
AM
1110 p = strchr (h->root.root.string, ELF_VER_CHR);
1111 if (p == NULL
1112 || (h->root.type != bfd_link_hash_defined
1113 && h->root.type != bfd_link_hash_defweak))
1114 continue;
31941635 1115
45d6a902
AM
1116 amt = p - h->root.root.string;
1117 shortname = bfd_malloc (amt + 1);
1118 memcpy (shortname, h->root.root.string, amt);
1119 shortname[amt] = '\0';
31941635 1120
45d6a902 1121 hi = (struct elf_link_hash_entry *)
0eddce27 1122 bfd_link_hash_lookup (&hash_table->root, shortname,
45d6a902
AM
1123 FALSE, FALSE, FALSE);
1124 if (hi != NULL
1125 && hi->root.type == h->root.type
1126 && hi->root.u.def.value == h->root.u.def.value
1127 && hi->root.u.def.section == h->root.u.def.section)
1128 {
1129 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1130 hi->root.type = bfd_link_hash_indirect;
1131 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
1132 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1133 sym_hash = elf_sym_hashes (abfd);
1134 if (sym_hash)
1135 for (symidx = 0; symidx < extsymcount; ++symidx)
1136 if (sym_hash[symidx] == hi)
1137 {
1138 sym_hash[symidx] = h;
1139 break;
1140 }
1141 }
1142 free (shortname);
1143 }
1144 free (nondeflt_vers);
1145 nondeflt_vers = NULL;
1146 }
31941635 1147
45d6a902
AM
1148 if (extversym != NULL)
1149 {
1150 free (extversym);
1151 extversym = NULL;
1152 }
31941635 1153
45d6a902
AM
1154 if (isymbuf != NULL)
1155 free (isymbuf);
1156 isymbuf = NULL;
31941635 1157
45d6a902
AM
1158 /* Now set the weakdefs field correctly for all the weak defined
1159 symbols we found. The only way to do this is to search all the
1160 symbols. Since we only need the information for non functions in
1161 dynamic objects, that's the only time we actually put anything on
1162 the list WEAKS. We need this information so that if a regular
1163 object refers to a symbol defined weakly in a dynamic object, the
1164 real symbol in the dynamic object is also put in the dynamic
1165 symbols; we also must arrange for both symbols to point to the
1166 same memory location. We could handle the general case of symbol
1167 aliasing, but a general symbol alias can only be generated in
1168 assembler code, handling it correctly would be very time
1169 consuming, and other ELF linkers don't handle general aliasing
1170 either. */
b60d7a1a 1171 if (weaks != NULL)
45d6a902 1172 {
45d6a902
AM
1173 struct elf_link_hash_entry **hpp;
1174 struct elf_link_hash_entry **hppend;
b60d7a1a
L
1175 struct elf_link_hash_entry **sorted_sym_hash;
1176 struct elf_link_hash_entry *h;
1177 size_t sym_count;
252b5132 1178
b60d7a1a
L
1179 /* Since we have to search the whole symbol list for each weak
1180 defined symbol, search time for N weak defined symbols will be
1181 O(N^2). Binary search will cut it down to O(NlogN). */
1182 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1183 sorted_sym_hash = bfd_malloc (amt);
1184 if (sorted_sym_hash == NULL)
1185 goto error_return;
1186 sym_hash = sorted_sym_hash;
45d6a902
AM
1187 hpp = elf_sym_hashes (abfd);
1188 hppend = hpp + extsymcount;
b60d7a1a 1189 sym_count = 0;
45d6a902 1190 for (; hpp < hppend; hpp++)
31941635 1191 {
45d6a902 1192 h = *hpp;
b60d7a1a 1193 if (h != NULL
45d6a902 1194 && h->root.type == bfd_link_hash_defined
b60d7a1a 1195 && h->type != STT_FUNC)
31941635 1196 {
b60d7a1a
L
1197 *sym_hash = h;
1198 sym_hash++;
1199 sym_count++;
1200 }
1201 }
1202
1203 qsort (sorted_sym_hash, sym_count,
1204 sizeof (struct elf_link_hash_entry *),
1205 sort_symbol);
252b5132 1206
b60d7a1a
L
1207 while (weaks != NULL)
1208 {
1209 struct elf_link_hash_entry *hlook;
1210 asection *slook;
1211 bfd_vma vlook;
1212 long ilook;
1213 size_t i, j, idx;
1214
1215 hlook = weaks;
1216 weaks = hlook->weakdef;
1217 hlook->weakdef = NULL;
1218
1219 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
1220 || hlook->root.type == bfd_link_hash_defweak
1221 || hlook->root.type == bfd_link_hash_common
1222 || hlook->root.type == bfd_link_hash_indirect);
1223 slook = hlook->root.u.def.section;
1224 vlook = hlook->root.u.def.value;
1225
1226 ilook = -1;
1227 i = 0;
1228 j = sym_count;
1229 while (i < j)
1230 {
1231 bfd_signed_vma vdiff;
1232 idx = (i + j) / 2;
1233 h = sorted_sym_hash [idx];
1234 vdiff = vlook - h->root.u.def.value;
1235 if (vdiff < 0)
1236 j = idx;
1237 else if (vdiff > 0)
1238 i = idx + 1;
1239 else
45d6a902 1240 {
b60d7a1a
L
1241 long sdiff = slook - h->root.u.def.section;
1242 if (sdiff < 0)
1243 j = idx;
1244 else if (sdiff > 0)
1245 i = idx + 1;
1246 else
1247 {
1248 ilook = idx;
1249 break;
1250 }
45d6a902 1251 }
b60d7a1a
L
1252 }
1253
1254 /* We didn't find a value/section match. */
1255 if (ilook == -1)
1256 continue;
1257
1258 for (i = ilook; i < sym_count; i++)
1259 {
1260 h = sorted_sym_hash [i];
f0c2e336 1261
b60d7a1a
L
1262 /* Stop if value or section doesn't match. */
1263 if (h->root.u.def.value != vlook
1264 || h->root.u.def.section != slook)
1265 break;
1266 else if (h != hlook)
45d6a902 1267 {
b60d7a1a
L
1268 hlook->weakdef = h;
1269
1270 /* If the weak definition is in the list of dynamic
1271 symbols, make sure the real definition is put
1272 there as well. */
1273 if (hlook->dynindx != -1 && h->dynindx == -1)
1274 {
1275 if (! _bfd_elf_link_record_dynamic_symbol (info,
1276 h))
1277 goto error_return;
1278 }
1279
1280 /* If the real definition is in the list of dynamic
1281 symbols, make sure the weak definition is put
1282 there as well. If we don't do this, then the
1283 dynamic loader might not merge the entries for the
1284 real definition and the weak definition. */
1285 if (h->dynindx != -1 && hlook->dynindx == -1)
1286 {
1287 if (! _bfd_elf_link_record_dynamic_symbol (info,
1288 hlook))
1289 goto error_return;
1290 }
1291 break;
45d6a902 1292 }
45d6a902 1293 }
252b5132 1294 }
b60d7a1a
L
1295
1296 free (sorted_sym_hash);
45d6a902 1297 }
f0c2e336 1298
45d6a902
AM
1299 /* If this object is the same format as the output object, and it is
1300 not a shared library, then let the backend look through the
1301 relocs.
fa7ea4d8 1302
45d6a902
AM
1303 This is required to build global offset table entries and to
1304 arrange for dynamic relocs. It is not required for the
1305 particular common case of linking non PIC code, even when linking
1306 against shared libraries, but unfortunately there is no way of
1307 knowing whether an object file has been compiled PIC or not.
1308 Looking through the relocs is not particularly time consuming.
1309 The problem is that we must either (1) keep the relocs in memory,
1310 which causes the linker to require additional runtime memory or
1311 (2) read the relocs twice from the input file, which wastes time.
1312 This would be a good case for using mmap.
60166579 1313
45d6a902
AM
1314 I have no idea how to handle linking PIC code into a file of a
1315 different format. It probably can't be done. */
1316 check_relocs = get_elf_backend_data (abfd)->check_relocs;
1317 if (! dynamic
0eddce27
AM
1318 && is_elf_hash_table (hash_table)
1319 && hash_table->root.creator == abfd->xvec
45d6a902
AM
1320 && check_relocs != NULL)
1321 {
1322 asection *o;
fa7ea4d8 1323
45d6a902 1324 for (o = abfd->sections; o != NULL; o = o->next)
fa7ea4d8 1325 {
45d6a902
AM
1326 Elf_Internal_Rela *internal_relocs;
1327 bfd_boolean ok;
1328
1329 if ((o->flags & SEC_RELOC) == 0
1330 || o->reloc_count == 0
1331 || ((info->strip == strip_all || info->strip == strip_debugger)
1332 && (o->flags & SEC_DEBUGGING) != 0)
1333 || bfd_is_abs_section (o->output_section))
1334 continue;
1335
268b6b39
AM
1336 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
1337 info->keep_memory);
45d6a902
AM
1338 if (internal_relocs == NULL)
1339 goto error_return;
1340
1341 ok = (*check_relocs) (abfd, info, o, internal_relocs);
1342
1343 if (elf_section_data (o)->relocs != internal_relocs)
1344 free (internal_relocs);
1345
1346 if (! ok)
1347 goto error_return;
fa7ea4d8 1348 }
45d6a902 1349 }
30831527 1350
45d6a902
AM
1351 /* If this is a non-traditional link, try to optimize the handling
1352 of the .stab/.stabstr sections. */
1353 if (! dynamic
1354 && ! info->traditional_format
0eddce27 1355 && is_elf_hash_table (hash_table)
45d6a902
AM
1356 && (info->strip != strip_all && info->strip != strip_debugger))
1357 {
29ca8dc5 1358 asection *stabstr;
79349b09 1359
29ca8dc5
NS
1360 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
1361 if (stabstr != NULL)
fc8c40a0 1362 {
29ca8dc5
NS
1363 bfd_size_type string_offset = 0;
1364 asection *stab;
1365
1366 for (stab = abfd->sections; stab; stab = stab->next)
1367 if (strncmp (".stab", stab->name, 5) == 0
1368 && (!stab->name[5] ||
1369 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
1370 && (stab->flags & SEC_MERGE) == 0
1371 && !bfd_is_abs_section (stab->output_section))
1372 {
1373 struct bfd_elf_section_data *secdata;
79349b09 1374
29ca8dc5
NS
1375 secdata = elf_section_data (stab);
1376 if (! _bfd_link_section_stabs (abfd,
1377 & hash_table->stab_info,
1378 stab, stabstr,
1379 &secdata->sec_info,
1380 &string_offset))
1381 goto error_return;
1382 if (secdata->sec_info)
1383 stab->sec_info_type = ELF_INFO_TYPE_STABS;
45d6a902 1384 }
fc8c40a0 1385 }
252b5132
RH
1386 }
1387
0eddce27
AM
1388 if (! info->relocatable
1389 && ! dynamic
1390 && is_elf_hash_table (hash_table))
252b5132 1391 {
252b5132 1392 asection *s;
252b5132 1393
45d6a902
AM
1394 for (s = abfd->sections; s != NULL; s = s->next)
1395 if ((s->flags & SEC_MERGE) != 0
1396 && !bfd_is_abs_section (s->output_section))
1397 {
1398 struct bfd_elf_section_data *secdata;
252b5132 1399
45d6a902
AM
1400 secdata = elf_section_data (s);
1401 if (! _bfd_merge_section (abfd,
1402 & hash_table->merge_info,
1403 s, &secdata->sec_info))
1404 goto error_return;
1405 else if (secdata->sec_info)
1406 s->sec_info_type = ELF_INFO_TYPE_MERGE;
1407 }
1408 }
252b5132 1409
0eddce27 1410 if (is_elf_hash_table (hash_table))
45d6a902
AM
1411 {
1412 /* Add this bfd to the loaded list. */
1413 struct elf_link_loaded_list *n;
6b9b879a 1414
268b6b39 1415 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
45d6a902
AM
1416 if (n == NULL)
1417 goto error_return;
1418 n->abfd = abfd;
1419 n->next = hash_table->loaded;
1420 hash_table->loaded = n;
1421 }
252b5132 1422
45d6a902 1423 return TRUE;
252b5132 1424
45d6a902
AM
1425 error_free_vers:
1426 if (nondeflt_vers != NULL)
1427 free (nondeflt_vers);
1428 if (extversym != NULL)
1429 free (extversym);
1430 error_free_sym:
1431 if (isymbuf != NULL)
1432 free (isymbuf);
1433 error_return:
1434 return FALSE;
1435}
252b5132 1436
45d6a902 1437/* Add an entry to the .dynamic table. */
252b5132 1438
45d6a902 1439bfd_boolean
268b6b39 1440elf_add_dynamic_entry (struct bfd_link_info *info, bfd_vma tag, bfd_vma val)
45d6a902
AM
1441{
1442 Elf_Internal_Dyn dyn;
1443 bfd *dynobj;
1444 asection *s;
1445 bfd_size_type newsize;
1446 bfd_byte *newcontents;
252b5132 1447
0eddce27 1448 if (! is_elf_hash_table (info->hash))
45d6a902 1449 return FALSE;
252b5132 1450
45d6a902
AM
1451 dynobj = elf_hash_table (info)->dynobj;
1452
1453 s = bfd_get_section_by_name (dynobj, ".dynamic");
1454 BFD_ASSERT (s != NULL);
1455
1456 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
268b6b39 1457 newcontents = bfd_realloc (s->contents, newsize);
45d6a902
AM
1458 if (newcontents == NULL)
1459 return FALSE;
1460
1461 dyn.d_tag = tag;
1462 dyn.d_un.d_val = val;
1463 elf_swap_dyn_out (dynobj, &dyn,
1464 (Elf_External_Dyn *) (newcontents + s->_raw_size));
1465
1466 s->_raw_size = newsize;
1467 s->contents = newcontents;
1468
1469 return TRUE;
1470}
1471\f
1472/* Array used to determine the number of hash table buckets to use
1473 based on the number of symbols there are. If there are fewer than
1474 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
1475 fewer than 37 we use 17 buckets, and so forth. We never use more
1476 than 32771 buckets. */
252b5132 1477
45d6a902
AM
1478static const size_t elf_buckets[] =
1479{
1480 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
1481 16411, 32771, 0
1482};
252b5132 1483
45d6a902
AM
1484/* Compute bucket count for hashing table. We do not use a static set
1485 of possible tables sizes anymore. Instead we determine for all
1486 possible reasonable sizes of the table the outcome (i.e., the
1487 number of collisions etc) and choose the best solution. The
1488 weighting functions are not too simple to allow the table to grow
1489 without bounds. Instead one of the weighting factors is the size.
1490 Therefore the result is always a good payoff between few collisions
1491 (= short chain lengths) and table size. */
1492static size_t
268b6b39 1493compute_bucket_count (struct bfd_link_info *info)
45d6a902
AM
1494{
1495 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
1496 size_t best_size = 0;
1497 unsigned long int *hashcodes;
1498 unsigned long int *hashcodesp;
1499 unsigned long int i;
1500 bfd_size_type amt;
252b5132 1501
45d6a902
AM
1502 /* Compute the hash values for all exported symbols. At the same
1503 time store the values in an array so that we could use them for
1504 optimizations. */
1505 amt = dynsymcount;
1506 amt *= sizeof (unsigned long int);
268b6b39 1507 hashcodes = bfd_malloc (amt);
45d6a902
AM
1508 if (hashcodes == NULL)
1509 return 0;
1510 hashcodesp = hashcodes;
252b5132 1511
45d6a902
AM
1512 /* Put all hash values in HASHCODES. */
1513 elf_link_hash_traverse (elf_hash_table (info),
1514 elf_collect_hash_codes, &hashcodesp);
252b5132 1515
45d6a902
AM
1516 /* We have a problem here. The following code to optimize the table
1517 size requires an integer type with more the 32 bits. If
1518 BFD_HOST_U_64_BIT is set we know about such a type. */
1519#ifdef BFD_HOST_U_64_BIT
1520 if (info->optimize)
1521 {
1522 unsigned long int nsyms = hashcodesp - hashcodes;
1523 size_t minsize;
1524 size_t maxsize;
1525 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
1526 unsigned long int *counts ;
252b5132 1527
45d6a902
AM
1528 /* Possible optimization parameters: if we have NSYMS symbols we say
1529 that the hashing table must at least have NSYMS/4 and at most
1530 2*NSYMS buckets. */
1531 minsize = nsyms / 4;
1532 if (minsize == 0)
1533 minsize = 1;
1534 best_size = maxsize = nsyms * 2;
252b5132 1535
45d6a902
AM
1536 /* Create array where we count the collisions in. We must use bfd_malloc
1537 since the size could be large. */
1538 amt = maxsize;
1539 amt *= sizeof (unsigned long int);
268b6b39 1540 counts = bfd_malloc (amt);
45d6a902
AM
1541 if (counts == NULL)
1542 {
1543 free (hashcodes);
1544 return 0;
1545 }
252b5132 1546
45d6a902
AM
1547 /* Compute the "optimal" size for the hash table. The criteria is a
1548 minimal chain length. The minor criteria is (of course) the size
1549 of the table. */
1550 for (i = minsize; i < maxsize; ++i)
1551 {
1552 /* Walk through the array of hashcodes and count the collisions. */
1553 BFD_HOST_U_64_BIT max;
1554 unsigned long int j;
1555 unsigned long int fact;
252b5132 1556
45d6a902 1557 memset (counts, '\0', i * sizeof (unsigned long int));
252b5132 1558
45d6a902
AM
1559 /* Determine how often each hash bucket is used. */
1560 for (j = 0; j < nsyms; ++j)
1561 ++counts[hashcodes[j] % i];
252b5132 1562
45d6a902
AM
1563 /* For the weight function we need some information about the
1564 pagesize on the target. This is information need not be 100%
1565 accurate. Since this information is not available (so far) we
1566 define it here to a reasonable default value. If it is crucial
1567 to have a better value some day simply define this value. */
1568# ifndef BFD_TARGET_PAGESIZE
1569# define BFD_TARGET_PAGESIZE (4096)
1570# endif
252b5132 1571
45d6a902
AM
1572 /* We in any case need 2 + NSYMS entries for the size values and
1573 the chains. */
1574 max = (2 + nsyms) * (ARCH_SIZE / 8);
252b5132 1575
45d6a902
AM
1576# if 1
1577 /* Variant 1: optimize for short chains. We add the squares
4cc11e76 1578 of all the chain lengths (which favors many small chain
45d6a902
AM
1579 over a few long chains). */
1580 for (j = 0; j < i; ++j)
1581 max += counts[j] * counts[j];
252b5132 1582
45d6a902
AM
1583 /* This adds penalties for the overall size of the table. */
1584 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
1585 max *= fact * fact;
1586# else
1587 /* Variant 2: Optimize a lot more for small table. Here we
1588 also add squares of the size but we also add penalties for
1589 empty slots (the +1 term). */
1590 for (j = 0; j < i; ++j)
1591 max += (1 + counts[j]) * (1 + counts[j]);
252b5132 1592
45d6a902
AM
1593 /* The overall size of the table is considered, but not as
1594 strong as in variant 1, where it is squared. */
1595 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
1596 max *= fact;
1597# endif
252b5132 1598
45d6a902
AM
1599 /* Compare with current best results. */
1600 if (max < best_chlen)
1601 {
1602 best_chlen = max;
1603 best_size = i;
252b5132 1604 }
d6cf2879
L
1605 }
1606
45d6a902
AM
1607 free (counts);
1608 }
1609 else
1610#endif /* defined (BFD_HOST_U_64_BIT) */
1611 {
1612 /* This is the fallback solution if no 64bit type is available or if we
1613 are not supposed to spend much time on optimizations. We select the
1614 bucket count using a fixed set of numbers. */
1615 for (i = 0; elf_buckets[i] != 0; i++)
d6cf2879 1616 {
45d6a902
AM
1617 best_size = elf_buckets[i];
1618 if (dynsymcount < elf_buckets[i + 1])
1619 break;
d6cf2879 1620 }
45d6a902 1621 }
d6cf2879 1622
45d6a902
AM
1623 /* Free the arrays we needed. */
1624 free (hashcodes);
252b5132 1625
45d6a902
AM
1626 return best_size;
1627}
252b5132 1628
45d6a902
AM
1629/* Set up the sizes and contents of the ELF dynamic sections. This is
1630 called by the ELF linker emulation before_allocation routine. We
1631 must set the sizes of the sections before the linker sets the
1632 addresses of the various sections. */
252b5132 1633
45d6a902 1634bfd_boolean
268b6b39
AM
1635NAME(bfd_elf,size_dynamic_sections) (bfd *output_bfd,
1636 const char *soname,
1637 const char *rpath,
1638 const char *filter_shlib,
1639 const char * const *auxiliary_filters,
1640 struct bfd_link_info *info,
1641 asection **sinterpptr,
1642 struct bfd_elf_version_tree *verdefs)
45d6a902
AM
1643{
1644 bfd_size_type soname_indx;
1645 bfd *dynobj;
9c5bfbb7 1646 const struct elf_backend_data *bed;
45d6a902 1647 struct elf_assign_sym_version_info asvinfo;
252b5132 1648
45d6a902 1649 *sinterpptr = NULL;
252b5132 1650
45d6a902 1651 soname_indx = (bfd_size_type) -1;
252b5132 1652
0eddce27 1653 if (!is_elf_hash_table (info->hash))
45d6a902 1654 return TRUE;
252b5132 1655
9ee5e499
JJ
1656 if (info->execstack)
1657 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
1658 else if (info->noexecstack)
1659 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
1660 else
1661 {
1662 bfd *inputobj;
1663 asection *notesec = NULL;
1664 int exec = 0;
1665
1666 for (inputobj = info->input_bfds;
1667 inputobj;
1668 inputobj = inputobj->link_next)
1669 {
1670 asection *s;
1671
1672 if (inputobj->flags & DYNAMIC)
1673 continue;
1674 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
1675 if (s)
1676 {
1677 if (s->flags & SEC_CODE)
1678 exec = PF_X;
1679 notesec = s;
1680 }
1681 else
1682 exec = PF_X;
1683 }
1684 if (notesec)
1685 {
1686 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
1049f94e 1687 if (exec && info->relocatable
9ee5e499
JJ
1688 && notesec->output_section != bfd_abs_section_ptr)
1689 notesec->output_section->flags |= SEC_CODE;
1690 }
1691 }
1692
45d6a902
AM
1693 /* Any syms created from now on start with -1 in
1694 got.refcount/offset and plt.refcount/offset. */
1695 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
252b5132 1696
45d6a902
AM
1697 /* The backend may have to create some sections regardless of whether
1698 we're dynamic or not. */
1699 bed = get_elf_backend_data (output_bfd);
1700 if (bed->elf_backend_always_size_sections
1701 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
1702 return FALSE;
252b5132 1703
45d6a902 1704 dynobj = elf_hash_table (info)->dynobj;
252b5132 1705
45d6a902
AM
1706 /* If there were no dynamic objects in the link, there is nothing to
1707 do here. */
1708 if (dynobj == NULL)
1709 return TRUE;
252b5132 1710
45d6a902
AM
1711 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
1712 return FALSE;
252b5132 1713
45d6a902
AM
1714 if (elf_hash_table (info)->dynamic_sections_created)
1715 {
1716 struct elf_info_failed eif;
1717 struct elf_link_hash_entry *h;
1718 asection *dynstr;
1719 struct bfd_elf_version_tree *t;
1720 struct bfd_elf_version_expr *d;
1721 bfd_boolean all_defined;
30b30c21 1722
45d6a902 1723 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
893c4fe2 1724 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
252b5132 1725
45d6a902 1726 if (soname != NULL)
252b5132 1727 {
45d6a902
AM
1728 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
1729 soname, TRUE);
1730 if (soname_indx == (bfd_size_type) -1
268b6b39 1731 || ! elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
45d6a902 1732 return FALSE;
252b5132 1733 }
45d6a902
AM
1734
1735 if (info->symbolic)
252b5132 1736 {
268b6b39 1737 if (! elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
b34976b6 1738 return FALSE;
45d6a902
AM
1739 info->flags |= DF_SYMBOLIC;
1740 }
252b5132 1741
45d6a902
AM
1742 if (rpath != NULL)
1743 {
1744 bfd_size_type indx;
1745
1746 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
1747 TRUE);
1748 if (info->new_dtags)
1749 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
1750 if (indx == (bfd_size_type) -1
268b6b39 1751 || ! elf_add_dynamic_entry (info, DT_RPATH, indx)
45d6a902 1752 || (info->new_dtags
268b6b39 1753 && ! elf_add_dynamic_entry (info, DT_RUNPATH, indx)))
b34976b6 1754 return FALSE;
252b5132
RH
1755 }
1756
45d6a902 1757 if (filter_shlib != NULL)
fc8c40a0 1758 {
45d6a902 1759 bfd_size_type indx;
fc8c40a0 1760
45d6a902
AM
1761 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
1762 filter_shlib, TRUE);
1763 if (indx == (bfd_size_type) -1
268b6b39 1764 || ! elf_add_dynamic_entry (info, DT_FILTER, indx))
45d6a902 1765 return FALSE;
fc8c40a0 1766 }
252b5132 1767
45d6a902
AM
1768 if (auxiliary_filters != NULL)
1769 {
1770 const char * const *p;
252b5132 1771
45d6a902
AM
1772 for (p = auxiliary_filters; *p != NULL; p++)
1773 {
1774 bfd_size_type indx;
1775
1776 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
1777 *p, TRUE);
1778 if (indx == (bfd_size_type) -1
268b6b39 1779 || ! elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
45d6a902
AM
1780 return FALSE;
1781 }
1782 }
1783
1784 eif.info = info;
1785 eif.verdefs = verdefs;
1786 eif.failed = FALSE;
252b5132 1787
45d6a902
AM
1788 /* If we are supposed to export all symbols into the dynamic symbol
1789 table (this is not the normal case), then do so. */
1790 if (info->export_dynamic)
1791 {
1792 elf_link_hash_traverse (elf_hash_table (info),
1793 _bfd_elf_export_symbol,
268b6b39 1794 &eif);
45d6a902
AM
1795 if (eif.failed)
1796 return FALSE;
1797 }
252b5132 1798
4cc11e76 1799 /* Make all global versions with definition. */
45d6a902 1800 for (t = verdefs; t != NULL; t = t->next)
108ba305 1801 for (d = t->globals.list; d != NULL; d = d->next)
5e35cbc2 1802 if (!d->symver && d->symbol)
45d6a902
AM
1803 {
1804 const char *verstr, *name;
1805 size_t namelen, verlen, newlen;
1806 char *newname, *p;
1807 struct elf_link_hash_entry *newh;
252b5132 1808
5e35cbc2 1809 name = d->symbol;
45d6a902
AM
1810 namelen = strlen (name);
1811 verstr = t->name;
1812 verlen = strlen (verstr);
1813 newlen = namelen + verlen + 3;
2b0f7ef9 1814
268b6b39 1815 newname = bfd_malloc (newlen);
45d6a902
AM
1816 if (newname == NULL)
1817 return FALSE;
1818 memcpy (newname, name, namelen);
2b0f7ef9 1819
45d6a902
AM
1820 /* Check the hidden versioned definition. */
1821 p = newname + namelen;
1822 *p++ = ELF_VER_CHR;
1823 memcpy (p, verstr, verlen + 1);
1824 newh = elf_link_hash_lookup (elf_hash_table (info),
1825 newname, FALSE, FALSE,
1826 FALSE);
1827 if (newh == NULL
1828 || (newh->root.type != bfd_link_hash_defined
1829 && newh->root.type != bfd_link_hash_defweak))
1830 {
1831 /* Check the default versioned definition. */
1832 *p++ = ELF_VER_CHR;
1833 memcpy (p, verstr, verlen + 1);
1834 newh = elf_link_hash_lookup (elf_hash_table (info),
1835 newname, FALSE, FALSE,
1836 FALSE);
1837 }
1838 free (newname);
252b5132 1839
45d6a902
AM
1840 /* Mark this version if there is a definition and it is
1841 not defined in a shared object. */
1842 if (newh != NULL
1843 && ((newh->elf_link_hash_flags
1844 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
1845 && (newh->root.type == bfd_link_hash_defined
1846 || newh->root.type == bfd_link_hash_defweak))
1847 d->symver = 1;
1848 }
252b5132 1849
45d6a902
AM
1850 /* Attach all the symbols to their version information. */
1851 asvinfo.output_bfd = output_bfd;
1852 asvinfo.info = info;
1853 asvinfo.verdefs = verdefs;
1854 asvinfo.failed = FALSE;
c44233aa 1855
45d6a902
AM
1856 elf_link_hash_traverse (elf_hash_table (info),
1857 _bfd_elf_link_assign_sym_version,
268b6b39 1858 &asvinfo);
45d6a902
AM
1859 if (asvinfo.failed)
1860 return FALSE;
c44233aa 1861
45d6a902
AM
1862 if (!info->allow_undefined_version)
1863 {
4cc11e76 1864 /* Check if all global versions have a definition. */
45d6a902
AM
1865 all_defined = TRUE;
1866 for (t = verdefs; t != NULL; t = t->next)
108ba305 1867 for (d = t->globals.list; d != NULL; d = d->next)
3e0e1ef1 1868 if (!d->symver && !d->script)
45d6a902
AM
1869 {
1870 (*_bfd_error_handler)
1871 (_("%s: undefined version: %s"),
1872 d->pattern, t->name);
1873 all_defined = FALSE;
1874 }
2b0f7ef9 1875
45d6a902
AM
1876 if (!all_defined)
1877 {
1878 bfd_set_error (bfd_error_bad_value);
1879 return FALSE;
1880 }
1881 }
e92d460e 1882
45d6a902
AM
1883 /* Find all symbols which were defined in a dynamic object and make
1884 the backend pick a reasonable value for them. */
1885 elf_link_hash_traverse (elf_hash_table (info),
1886 _bfd_elf_adjust_dynamic_symbol,
268b6b39 1887 &eif);
45d6a902
AM
1888 if (eif.failed)
1889 return FALSE;
2b0f7ef9 1890
45d6a902
AM
1891 /* Add some entries to the .dynamic section. We fill in some of the
1892 values later, in elf_bfd_final_link, but we must add the entries
1893 now so that we know the final size of the .dynamic section. */
2b0f7ef9 1894
45d6a902
AM
1895 /* If there are initialization and/or finalization functions to
1896 call then add the corresponding DT_INIT/DT_FINI entries. */
1897 h = (info->init_function
1898 ? elf_link_hash_lookup (elf_hash_table (info),
1899 info->init_function, FALSE,
1900 FALSE, FALSE)
1901 : NULL);
1902 if (h != NULL
1903 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
1904 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
1905 {
268b6b39 1906 if (! elf_add_dynamic_entry (info, DT_INIT, 0))
45d6a902
AM
1907 return FALSE;
1908 }
1909 h = (info->fini_function
1910 ? elf_link_hash_lookup (elf_hash_table (info),
1911 info->fini_function, FALSE,
1912 FALSE, FALSE)
1913 : NULL);
1914 if (h != NULL
1915 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
1916 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
1917 {
268b6b39 1918 if (! elf_add_dynamic_entry (info, DT_FINI, 0))
45d6a902
AM
1919 return FALSE;
1920 }
2b0f7ef9 1921
45d6a902
AM
1922 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
1923 {
1924 /* DT_PREINIT_ARRAY is not allowed in shared library. */
36af4a4e 1925 if (! info->executable)
45d6a902
AM
1926 {
1927 bfd *sub;
1928 asection *o;
2b0f7ef9 1929
45d6a902
AM
1930 for (sub = info->input_bfds; sub != NULL;
1931 sub = sub->link_next)
1932 for (o = sub->sections; o != NULL; o = o->next)
1933 if (elf_section_data (o)->this_hdr.sh_type
1934 == SHT_PREINIT_ARRAY)
1935 {
1936 (*_bfd_error_handler)
1937 (_("%s: .preinit_array section is not allowed in DSO"),
1938 bfd_archive_filename (sub));
1939 break;
1940 }
2b0f7ef9 1941
45d6a902
AM
1942 bfd_set_error (bfd_error_nonrepresentable_section);
1943 return FALSE;
1944 }
2b0f7ef9 1945
268b6b39
AM
1946 if (!elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
1947 || !elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
45d6a902
AM
1948 return FALSE;
1949 }
1950 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
2b0f7ef9 1951 {
268b6b39
AM
1952 if (!elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
1953 || !elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
45d6a902
AM
1954 return FALSE;
1955 }
1956 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
1957 {
268b6b39
AM
1958 if (!elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
1959 || !elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
45d6a902 1960 return FALSE;
2b0f7ef9 1961 }
2b0f7ef9 1962
45d6a902
AM
1963 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
1964 /* If .dynstr is excluded from the link, we don't want any of
1965 these tags. Strictly, we should be checking each section
1966 individually; This quick check covers for the case where
1967 someone does a /DISCARD/ : { *(*) }. */
1968 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
1969 {
1970 bfd_size_type strsize;
1971
1972 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
268b6b39
AM
1973 if (! elf_add_dynamic_entry (info, DT_HASH, 0)
1974 || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
1975 || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0)
1976 || ! elf_add_dynamic_entry (info, DT_STRSZ, strsize)
1977 || ! elf_add_dynamic_entry (info, DT_SYMENT,
1978 sizeof (Elf_External_Sym)))
45d6a902
AM
1979 return FALSE;
1980 }
1981 }
2b0f7ef9 1982
45d6a902
AM
1983 /* The backend must work out the sizes of all the other dynamic
1984 sections. */
1985 if (bed->elf_backend_size_dynamic_sections
1986 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
1987 return FALSE;
2b0f7ef9 1988
45d6a902 1989 if (elf_hash_table (info)->dynamic_sections_created)
2b0f7ef9 1990 {
45d6a902 1991 bfd_size_type dynsymcount;
2b0f7ef9 1992 asection *s;
45d6a902
AM
1993 size_t bucketcount = 0;
1994 size_t hash_entry_size;
1995 unsigned int dtagcount;
c44233aa 1996
45d6a902 1997 /* Set up the version definition section. */
2b0f7ef9 1998 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
45d6a902 1999 BFD_ASSERT (s != NULL);
2b0f7ef9 2000
45d6a902
AM
2001 /* We may have created additional version definitions if we are
2002 just linking a regular application. */
2003 verdefs = asvinfo.verdefs;
c44233aa 2004
45d6a902
AM
2005 /* Skip anonymous version tag. */
2006 if (verdefs != NULL && verdefs->vernum == 0)
2007 verdefs = verdefs->next;
2008
2009 if (verdefs == NULL)
2010 _bfd_strip_section_from_output (info, s);
2011 else
2b0f7ef9 2012 {
45d6a902
AM
2013 unsigned int cdefs;
2014 bfd_size_type size;
2015 struct bfd_elf_version_tree *t;
2016 bfd_byte *p;
2017 Elf_Internal_Verdef def;
2018 Elf_Internal_Verdaux defaux;
2019
2020 cdefs = 0;
2021 size = 0;
2022
2023 /* Make space for the base version. */
2024 size += sizeof (Elf_External_Verdef);
2025 size += sizeof (Elf_External_Verdaux);
2026 ++cdefs;
2027
2028 for (t = verdefs; t != NULL; t = t->next)
2b0f7ef9 2029 {
45d6a902
AM
2030 struct bfd_elf_version_deps *n;
2031
2032 size += sizeof (Elf_External_Verdef);
2033 size += sizeof (Elf_External_Verdaux);
2034 ++cdefs;
2035
2036 for (n = t->deps; n != NULL; n = n->next)
2037 size += sizeof (Elf_External_Verdaux);
2b0f7ef9 2038 }
2b0f7ef9 2039
45d6a902 2040 s->_raw_size = size;
268b6b39 2041 s->contents = bfd_alloc (output_bfd, s->_raw_size);
45d6a902
AM
2042 if (s->contents == NULL && s->_raw_size != 0)
2043 return FALSE;
2b0f7ef9 2044
45d6a902 2045 /* Fill in the version definition section. */
252b5132 2046
45d6a902 2047 p = s->contents;
94b6c40a 2048
45d6a902
AM
2049 def.vd_version = VER_DEF_CURRENT;
2050 def.vd_flags = VER_FLG_BASE;
2051 def.vd_ndx = 1;
2052 def.vd_cnt = 1;
2053 def.vd_aux = sizeof (Elf_External_Verdef);
2054 def.vd_next = (sizeof (Elf_External_Verdef)
2055 + sizeof (Elf_External_Verdaux));
252b5132 2056
45d6a902 2057 if (soname_indx != (bfd_size_type) -1)
252b5132 2058 {
45d6a902
AM
2059 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2060 soname_indx);
2061 def.vd_hash = bfd_elf_hash (soname);
2062 defaux.vda_name = soname_indx;
252b5132 2063 }
45d6a902
AM
2064 else
2065 {
2066 const char *name;
2067 bfd_size_type indx;
252b5132 2068
45d6a902
AM
2069 name = basename (output_bfd->filename);
2070 def.vd_hash = bfd_elf_hash (name);
2071 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2072 name, FALSE);
2073 if (indx == (bfd_size_type) -1)
2074 return FALSE;
2075 defaux.vda_name = indx;
2076 }
2077 defaux.vda_next = 0;
8ea2e4bd 2078
45d6a902
AM
2079 _bfd_elf_swap_verdef_out (output_bfd, &def,
2080 (Elf_External_Verdef *) p);
2081 p += sizeof (Elf_External_Verdef);
2082 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2083 (Elf_External_Verdaux *) p);
2084 p += sizeof (Elf_External_Verdaux);
e5094212 2085
45d6a902
AM
2086 for (t = verdefs; t != NULL; t = t->next)
2087 {
2088 unsigned int cdeps;
2089 struct bfd_elf_version_deps *n;
2090 struct elf_link_hash_entry *h;
2091 struct bfd_link_hash_entry *bh;
252b5132 2092
45d6a902
AM
2093 cdeps = 0;
2094 for (n = t->deps; n != NULL; n = n->next)
2095 ++cdeps;
ef5aade5 2096
45d6a902
AM
2097 /* Add a symbol representing this version. */
2098 bh = NULL;
2099 if (! (_bfd_generic_link_add_one_symbol
2100 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
268b6b39 2101 0, NULL, FALSE,
45d6a902
AM
2102 get_elf_backend_data (dynobj)->collect, &bh)))
2103 return FALSE;
2104 h = (struct elf_link_hash_entry *) bh;
2105 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
2106 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2107 h->type = STT_OBJECT;
2108 h->verinfo.vertree = t;
fc4cc5bb 2109
45d6a902
AM
2110 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2111 return FALSE;
2112
2113 def.vd_version = VER_DEF_CURRENT;
2114 def.vd_flags = 0;
108ba305 2115 if (t->globals.list == NULL && t->locals.list == NULL && ! t->used)
45d6a902
AM
2116 def.vd_flags |= VER_FLG_WEAK;
2117 def.vd_ndx = t->vernum + 1;
2118 def.vd_cnt = cdeps + 1;
2119 def.vd_hash = bfd_elf_hash (t->name);
2120 def.vd_aux = sizeof (Elf_External_Verdef);
2121 if (t->next != NULL)
2122 def.vd_next = (sizeof (Elf_External_Verdef)
2123 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
2124 else
2125 def.vd_next = 0;
2126
2127 _bfd_elf_swap_verdef_out (output_bfd, &def,
2128 (Elf_External_Verdef *) p);
2129 p += sizeof (Elf_External_Verdef);
2130
2131 defaux.vda_name = h->dynstr_index;
2132 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2133 h->dynstr_index);
2134 if (t->deps == NULL)
2135 defaux.vda_next = 0;
2136 else
2137 defaux.vda_next = sizeof (Elf_External_Verdaux);
2138 t->name_indx = defaux.vda_name;
2139
2140 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2141 (Elf_External_Verdaux *) p);
2142 p += sizeof (Elf_External_Verdaux);
2143
2144 for (n = t->deps; n != NULL; n = n->next)
2145 {
2146 if (n->version_needed == NULL)
2147 {
2148 /* This can happen if there was an error in the
2149 version script. */
2150 defaux.vda_name = 0;
2151 }
2152 else
2153 {
2154 defaux.vda_name = n->version_needed->name_indx;
2155 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2156 defaux.vda_name);
2157 }
2158 if (n->next == NULL)
2159 defaux.vda_next = 0;
2160 else
2161 defaux.vda_next = sizeof (Elf_External_Verdaux);
2162
2163 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2164 (Elf_External_Verdaux *) p);
2165 p += sizeof (Elf_External_Verdaux);
2166 }
2167 }
2168
268b6b39
AM
2169 if (! elf_add_dynamic_entry (info, DT_VERDEF, 0)
2170 || ! elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
45d6a902
AM
2171 return FALSE;
2172
2173 elf_tdata (output_bfd)->cverdefs = cdefs;
2174 }
8e67855b 2175
45d6a902 2176 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
0a991dfe 2177 {
268b6b39 2178 if (! elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
45d6a902
AM
2179 return FALSE;
2180 }
646411fb
JJ
2181 else if (info->flags & DF_BIND_NOW)
2182 {
2183 if (! elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
2184 return FALSE;
2185 }
0a991dfe 2186
45d6a902
AM
2187 if (info->flags_1)
2188 {
36af4a4e 2189 if (info->executable)
45d6a902
AM
2190 info->flags_1 &= ~ (DF_1_INITFIRST
2191 | DF_1_NODELETE
2192 | DF_1_NOOPEN);
268b6b39 2193 if (! elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
45d6a902 2194 return FALSE;
0a991dfe 2195 }
fc4cc5bb 2196
45d6a902 2197 /* Work out the size of the version reference section. */
252b5132 2198
45d6a902
AM
2199 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2200 BFD_ASSERT (s != NULL);
2201 {
2202 struct elf_find_verdep_info sinfo;
252b5132 2203
45d6a902
AM
2204 sinfo.output_bfd = output_bfd;
2205 sinfo.info = info;
2206 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
2207 if (sinfo.vers == 0)
2208 sinfo.vers = 1;
2209 sinfo.failed = FALSE;
252b5132 2210
45d6a902
AM
2211 elf_link_hash_traverse (elf_hash_table (info),
2212 _bfd_elf_link_find_version_dependencies,
268b6b39 2213 &sinfo);
5cab59f6 2214
45d6a902
AM
2215 if (elf_tdata (output_bfd)->verref == NULL)
2216 _bfd_strip_section_from_output (info, s);
2217 else
2218 {
2219 Elf_Internal_Verneed *t;
2220 unsigned int size;
2221 unsigned int crefs;
2222 bfd_byte *p;
e92d460e 2223
45d6a902
AM
2224 /* Build the version definition section. */
2225 size = 0;
2226 crefs = 0;
2227 for (t = elf_tdata (output_bfd)->verref;
2228 t != NULL;
2229 t = t->vn_nextref)
2230 {
2231 Elf_Internal_Vernaux *a;
e92d460e 2232
45d6a902
AM
2233 size += sizeof (Elf_External_Verneed);
2234 ++crefs;
2235 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2236 size += sizeof (Elf_External_Vernaux);
2237 }
252b5132 2238
45d6a902 2239 s->_raw_size = size;
268b6b39 2240 s->contents = bfd_alloc (output_bfd, s->_raw_size);
45d6a902
AM
2241 if (s->contents == NULL)
2242 return FALSE;
252b5132 2243
45d6a902
AM
2244 p = s->contents;
2245 for (t = elf_tdata (output_bfd)->verref;
2246 t != NULL;
2247 t = t->vn_nextref)
2248 {
2249 unsigned int caux;
2250 Elf_Internal_Vernaux *a;
2251 bfd_size_type indx;
252b5132 2252
45d6a902
AM
2253 caux = 0;
2254 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2255 ++caux;
252b5132 2256
45d6a902
AM
2257 t->vn_version = VER_NEED_CURRENT;
2258 t->vn_cnt = caux;
2259 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2260 elf_dt_name (t->vn_bfd) != NULL
2261 ? elf_dt_name (t->vn_bfd)
2262 : basename (t->vn_bfd->filename),
2263 FALSE);
2264 if (indx == (bfd_size_type) -1)
2265 return FALSE;
2266 t->vn_file = indx;
2267 t->vn_aux = sizeof (Elf_External_Verneed);
2268 if (t->vn_nextref == NULL)
2269 t->vn_next = 0;
2270 else
2271 t->vn_next = (sizeof (Elf_External_Verneed)
2272 + caux * sizeof (Elf_External_Vernaux));
252b5132 2273
45d6a902
AM
2274 _bfd_elf_swap_verneed_out (output_bfd, t,
2275 (Elf_External_Verneed *) p);
2276 p += sizeof (Elf_External_Verneed);
252b5132 2277
45d6a902
AM
2278 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2279 {
2280 a->vna_hash = bfd_elf_hash (a->vna_nodename);
2281 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2282 a->vna_nodename, FALSE);
2283 if (indx == (bfd_size_type) -1)
2284 return FALSE;
2285 a->vna_name = indx;
2286 if (a->vna_nextptr == NULL)
2287 a->vna_next = 0;
2288 else
2289 a->vna_next = sizeof (Elf_External_Vernaux);
252b5132 2290
45d6a902
AM
2291 _bfd_elf_swap_vernaux_out (output_bfd, a,
2292 (Elf_External_Vernaux *) p);
2293 p += sizeof (Elf_External_Vernaux);
2294 }
2295 }
252b5132 2296
268b6b39
AM
2297 if (! elf_add_dynamic_entry (info, DT_VERNEED, 0)
2298 || ! elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
45d6a902 2299 return FALSE;
252b5132 2300
45d6a902
AM
2301 elf_tdata (output_bfd)->cverrefs = crefs;
2302 }
2303 }
252b5132 2304
45d6a902
AM
2305 /* Assign dynsym indicies. In a shared library we generate a
2306 section symbol for each output section, which come first.
2307 Next come all of the back-end allocated local dynamic syms,
2308 followed by the rest of the global symbols. */
e92d460e 2309
45d6a902 2310 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
bc2b6df7 2311
45d6a902
AM
2312 /* Work out the size of the symbol version section. */
2313 s = bfd_get_section_by_name (dynobj, ".gnu.version");
2314 BFD_ASSERT (s != NULL);
2315 if (dynsymcount == 0
2316 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
252b5132 2317 {
45d6a902
AM
2318 _bfd_strip_section_from_output (info, s);
2319 /* The DYNSYMCOUNT might have changed if we were going to
2320 output a dynamic symbol table entry for S. */
2321 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
252b5132 2322 }
45d6a902 2323 else
c44233aa 2324 {
45d6a902 2325 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
268b6b39 2326 s->contents = bfd_zalloc (output_bfd, s->_raw_size);
45d6a902
AM
2327 if (s->contents == NULL)
2328 return FALSE;
252b5132 2329
268b6b39 2330 if (! elf_add_dynamic_entry (info, DT_VERSYM, 0))
45d6a902
AM
2331 return FALSE;
2332 }
252b5132 2333
45d6a902
AM
2334 /* Set the size of the .dynsym and .hash sections. We counted
2335 the number of dynamic symbols in elf_link_add_object_symbols.
2336 We will build the contents of .dynsym and .hash when we build
2337 the final symbol table, because until then we do not know the
2338 correct value to give the symbols. We built the .dynstr
2339 section as we went along in elf_link_add_object_symbols. */
2340 s = bfd_get_section_by_name (dynobj, ".dynsym");
2341 BFD_ASSERT (s != NULL);
2342 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
268b6b39 2343 s->contents = bfd_alloc (output_bfd, s->_raw_size);
45d6a902
AM
2344 if (s->contents == NULL && s->_raw_size != 0)
2345 return FALSE;
252b5132 2346
45d6a902 2347 if (dynsymcount != 0)
252b5132 2348 {
45d6a902
AM
2349 Elf_Internal_Sym isym;
2350
2351 /* The first entry in .dynsym is a dummy symbol. */
2352 isym.st_value = 0;
2353 isym.st_size = 0;
2354 isym.st_name = 0;
2355 isym.st_info = 0;
2356 isym.st_other = 0;
2357 isym.st_shndx = 0;
268b6b39 2358 elf_swap_symbol_out (output_bfd, &isym, s->contents, 0);
252b5132
RH
2359 }
2360
45d6a902
AM
2361 /* Compute the size of the hashing table. As a side effect this
2362 computes the hash values for all the names we export. */
2363 bucketcount = compute_bucket_count (info);
2364
2365 s = bfd_get_section_by_name (dynobj, ".hash");
2366 BFD_ASSERT (s != NULL);
2367 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
2368 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
268b6b39 2369 s->contents = bfd_zalloc (output_bfd, s->_raw_size);
45d6a902
AM
2370 if (s->contents == NULL)
2371 return FALSE;
252b5132 2372
268b6b39
AM
2373 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
2374 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
45d6a902 2375 s->contents + hash_entry_size);
252b5132 2376
45d6a902 2377 elf_hash_table (info)->bucketcount = bucketcount;
252b5132 2378
45d6a902
AM
2379 s = bfd_get_section_by_name (dynobj, ".dynstr");
2380 BFD_ASSERT (s != NULL);
252b5132 2381
45d6a902 2382 elf_finalize_dynstr (output_bfd, info);
252b5132 2383
45d6a902 2384 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
252b5132 2385
45d6a902 2386 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
268b6b39 2387 if (! elf_add_dynamic_entry (info, DT_NULL, 0))
45d6a902
AM
2388 return FALSE;
2389 }
252b5132 2390
b34976b6 2391 return TRUE;
252b5132 2392}
45d6a902
AM
2393\f
2394/* This function is used to adjust offsets into .dynstr for
2395 dynamic symbols. This is called via elf_link_hash_traverse. */
252b5132 2396
b34976b6 2397static bfd_boolean
268b6b39 2398elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
252b5132 2399{
268b6b39 2400 struct elf_strtab_hash *dynstr = data;
252b5132 2401
e92d460e
AM
2402 if (h->root.type == bfd_link_hash_warning)
2403 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2404
45d6a902
AM
2405 if (h->dynindx != -1)
2406 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2407 return TRUE;
2408}
252b5132 2409
45d6a902
AM
2410/* Assign string offsets in .dynstr, update all structures referencing
2411 them. */
252b5132 2412
45d6a902 2413static bfd_boolean
268b6b39 2414elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
45d6a902
AM
2415{
2416 struct elf_link_local_dynamic_entry *entry;
2417 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
2418 bfd *dynobj = elf_hash_table (info)->dynobj;
2419 asection *sdyn;
2420 bfd_size_type size;
2421 Elf_External_Dyn *dyncon, *dynconend;
252b5132 2422
45d6a902
AM
2423 _bfd_elf_strtab_finalize (dynstr);
2424 size = _bfd_elf_strtab_size (dynstr);
252b5132 2425
45d6a902
AM
2426 /* Update all .dynamic entries referencing .dynstr strings. */
2427 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2428 BFD_ASSERT (sdyn != NULL);
252b5132 2429
45d6a902
AM
2430 dyncon = (Elf_External_Dyn *) sdyn->contents;
2431 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2432 sdyn->_raw_size);
2433 for (; dyncon < dynconend; dyncon++)
2434 {
2435 Elf_Internal_Dyn dyn;
252b5132 2436
45d6a902
AM
2437 elf_swap_dyn_in (dynobj, dyncon, & dyn);
2438 switch (dyn.d_tag)
252b5132 2439 {
45d6a902
AM
2440 case DT_STRSZ:
2441 dyn.d_un.d_val = size;
2442 elf_swap_dyn_out (dynobj, & dyn, dyncon);
2443 break;
2444 case DT_NEEDED:
2445 case DT_SONAME:
2446 case DT_RPATH:
2447 case DT_RUNPATH:
2448 case DT_FILTER:
2449 case DT_AUXILIARY:
2450 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2451 elf_swap_dyn_out (dynobj, & dyn, dyncon);
2452 break;
2453 default:
2454 break;
2455 }
2456 }
252b5132 2457
45d6a902
AM
2458 /* Now update local dynamic symbols. */
2459 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2460 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2461 entry->isym.st_name);
252b5132 2462
45d6a902
AM
2463 /* And the rest of dynamic symbols. */
2464 elf_link_hash_traverse (elf_hash_table (info),
2465 elf_adjust_dynstr_offsets, dynstr);
252b5132 2466
45d6a902
AM
2467 /* Adjust version definitions. */
2468 if (elf_tdata (output_bfd)->cverdefs)
2469 {
2470 asection *s;
2471 bfd_byte *p;
2472 bfd_size_type i;
2473 Elf_Internal_Verdef def;
2474 Elf_Internal_Verdaux defaux;
252b5132 2475
45d6a902
AM
2476 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2477 p = (bfd_byte *) s->contents;
2478 do
252b5132 2479 {
45d6a902
AM
2480 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2481 &def);
2482 p += sizeof (Elf_External_Verdef);
2483 for (i = 0; i < def.vd_cnt; ++i)
252b5132 2484 {
45d6a902
AM
2485 _bfd_elf_swap_verdaux_in (output_bfd,
2486 (Elf_External_Verdaux *) p, &defaux);
2487 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2488 defaux.vda_name);
2489 _bfd_elf_swap_verdaux_out (output_bfd,
2490 &defaux, (Elf_External_Verdaux *) p);
2491 p += sizeof (Elf_External_Verdaux);
252b5132 2492 }
252b5132 2493 }
45d6a902 2494 while (def.vd_next);
252b5132
RH
2495 }
2496
45d6a902
AM
2497 /* Adjust version references. */
2498 if (elf_tdata (output_bfd)->verref)
252b5132 2499 {
45d6a902
AM
2500 asection *s;
2501 bfd_byte *p;
2502 bfd_size_type i;
2503 Elf_Internal_Verneed need;
2504 Elf_Internal_Vernaux needaux;
252b5132 2505
45d6a902
AM
2506 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2507 p = (bfd_byte *) s->contents;
2508 do
252b5132 2509 {
45d6a902
AM
2510 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2511 &need);
2512 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2513 _bfd_elf_swap_verneed_out (output_bfd, &need,
2514 (Elf_External_Verneed *) p);
2515 p += sizeof (Elf_External_Verneed);
2516 for (i = 0; i < need.vn_cnt; ++i)
252b5132 2517 {
45d6a902
AM
2518 _bfd_elf_swap_vernaux_in (output_bfd,
2519 (Elf_External_Vernaux *) p, &needaux);
2520 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2521 needaux.vna_name);
2522 _bfd_elf_swap_vernaux_out (output_bfd,
2523 &needaux,
2524 (Elf_External_Vernaux *) p);
2525 p += sizeof (Elf_External_Vernaux);
252b5132
RH
2526 }
2527 }
45d6a902 2528 while (need.vn_next);
252b5132
RH
2529 }
2530
b34976b6 2531 return TRUE;
252b5132 2532}
252b5132
RH
2533\f
2534/* Final phase of ELF linker. */
2535
2536/* A structure we use to avoid passing large numbers of arguments. */
2537
2538struct elf_final_link_info
2539{
2540 /* General link information. */
2541 struct bfd_link_info *info;
2542 /* Output BFD. */
2543 bfd *output_bfd;
2544 /* Symbol string table. */
2545 struct bfd_strtab_hash *symstrtab;
2546 /* .dynsym section. */
2547 asection *dynsym_sec;
2548 /* .hash section. */
2549 asection *hash_sec;
2550 /* symbol version section (.gnu.version). */
2551 asection *symver_sec;
2552 /* Buffer large enough to hold contents of any section. */
2553 bfd_byte *contents;
2554 /* Buffer large enough to hold external relocs of any section. */
268b6b39 2555 void *external_relocs;
252b5132
RH
2556 /* Buffer large enough to hold internal relocs of any section. */
2557 Elf_Internal_Rela *internal_relocs;
2558 /* Buffer large enough to hold external local symbols of any input
2559 BFD. */
2560 Elf_External_Sym *external_syms;
9ad5cbcf
AM
2561 /* And a buffer for symbol section indices. */
2562 Elf_External_Sym_Shndx *locsym_shndx;
252b5132
RH
2563 /* Buffer large enough to hold internal local symbols of any input
2564 BFD. */
2565 Elf_Internal_Sym *internal_syms;
2566 /* Array large enough to hold a symbol index for each local symbol
2567 of any input BFD. */
2568 long *indices;
2569 /* Array large enough to hold a section pointer for each local
2570 symbol of any input BFD. */
2571 asection **sections;
2572 /* Buffer to hold swapped out symbols. */
2573 Elf_External_Sym *symbuf;
9ad5cbcf
AM
2574 /* And one for symbol section indices. */
2575 Elf_External_Sym_Shndx *symshndxbuf;
252b5132
RH
2576 /* Number of swapped out symbols in buffer. */
2577 size_t symbuf_count;
2578 /* Number of symbols which fit in symbuf. */
2579 size_t symbuf_size;
c97e73dd
AM
2580 /* And same for symshndxbuf. */
2581 size_t shndxbuf_size;
252b5132
RH
2582};
2583
b34976b6 2584static bfd_boolean elf_link_output_sym
754021d0
AM
2585 (struct elf_final_link_info *, const char *, Elf_Internal_Sym *, asection *,
2586 struct elf_link_hash_entry *);
b34976b6 2587static bfd_boolean elf_link_flush_output_syms
268b6b39 2588 (struct elf_final_link_info *);
b34976b6 2589static bfd_boolean elf_link_output_extsym
268b6b39 2590 (struct elf_link_hash_entry *, void *);
b34976b6 2591static bfd_boolean elf_link_input_bfd
268b6b39 2592 (struct elf_final_link_info *, bfd *);
b34976b6 2593static bfd_boolean elf_reloc_link_order
268b6b39 2594 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
252b5132
RH
2595
2596/* This struct is used to pass information to elf_link_output_extsym. */
2597
2598struct elf_outext_info
2599{
b34976b6
AM
2600 bfd_boolean failed;
2601 bfd_boolean localsyms;
252b5132
RH
2602 struct elf_final_link_info *finfo;
2603};
2604
1049f94e 2605/* When performing a relocatable link, the input relocations are
31367b81
MM
2606 preserved. But, if they reference global symbols, the indices
2607 referenced must be updated. Update all the relocations in
2608 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
2609
2610static void
268b6b39
AM
2611elf_link_adjust_relocs (bfd *abfd,
2612 Elf_Internal_Shdr *rel_hdr,
2613 unsigned int count,
2614 struct elf_link_hash_entry **rel_hash)
31367b81
MM
2615{
2616 unsigned int i;
9c5bfbb7 2617 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
947216bf 2618 bfd_byte *erela;
268b6b39
AM
2619 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2620 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
209f668e 2621
947216bf 2622 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
209f668e 2623 {
947216bf
AM
2624 swap_in = bed->s->swap_reloc_in;
2625 swap_out = bed->s->swap_reloc_out;
209f668e 2626 }
947216bf 2627 else if (rel_hdr->sh_entsize == sizeof (Elf_External_Rela))
209f668e 2628 {
947216bf
AM
2629 swap_in = bed->s->swap_reloca_in;
2630 swap_out = bed->s->swap_reloca_out;
209f668e 2631 }
947216bf
AM
2632 else
2633 abort ();
2634
2635 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
2636 abort ();
31367b81 2637
947216bf
AM
2638 erela = rel_hdr->contents;
2639 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
31367b81 2640 {
947216bf
AM
2641 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
2642 unsigned int j;
2643
31367b81
MM
2644 if (*rel_hash == NULL)
2645 continue;
2646
2647 BFD_ASSERT ((*rel_hash)->indx >= 0);
2648
947216bf
AM
2649 (*swap_in) (abfd, erela, irela);
2650 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
2651 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
2652 ELF_R_TYPE (irela[j].r_info));
2653 (*swap_out) (abfd, irela, erela);
31367b81
MM
2654 }
2655}
2656
58821868
AM
2657struct elf_link_sort_rela
2658{
db6751f2
JJ
2659 bfd_vma offset;
2660 enum elf_reloc_type_class type;
947216bf
AM
2661 /* We use this as an array of size int_rels_per_ext_rel. */
2662 Elf_Internal_Rela rela[1];
db6751f2
JJ
2663};
2664
2665static int
268b6b39 2666elf_link_sort_cmp1 (const void *A, const void *B)
db6751f2 2667{
268b6b39
AM
2668 const struct elf_link_sort_rela *a = A;
2669 const struct elf_link_sort_rela *b = B;
db6751f2
JJ
2670 int relativea, relativeb;
2671
2672 relativea = a->type == reloc_class_relative;
2673 relativeb = b->type == reloc_class_relative;
2674
2675 if (relativea < relativeb)
db6751f2 2676 return 1;
fcfbdf31
JJ
2677 if (relativea > relativeb)
2678 return -1;
947216bf 2679 if (ELF_R_SYM (a->rela->r_info) < ELF_R_SYM (b->rela->r_info))
db6751f2 2680 return -1;
947216bf 2681 if (ELF_R_SYM (a->rela->r_info) > ELF_R_SYM (b->rela->r_info))
db6751f2 2682 return 1;
947216bf 2683 if (a->rela->r_offset < b->rela->r_offset)
db6751f2 2684 return -1;
947216bf 2685 if (a->rela->r_offset > b->rela->r_offset)
db6751f2
JJ
2686 return 1;
2687 return 0;
2688}
2689
2690static int
268b6b39 2691elf_link_sort_cmp2 (const void *A, const void *B)
db6751f2 2692{
268b6b39
AM
2693 const struct elf_link_sort_rela *a = A;
2694 const struct elf_link_sort_rela *b = B;
db6751f2
JJ
2695 int copya, copyb;
2696
2697 if (a->offset < b->offset)
2698 return -1;
2699 if (a->offset > b->offset)
2700 return 1;
290394d6
JJ
2701 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
2702 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
db6751f2
JJ
2703 if (copya < copyb)
2704 return -1;
2705 if (copya > copyb)
2706 return 1;
947216bf 2707 if (a->rela->r_offset < b->rela->r_offset)
db6751f2 2708 return -1;
947216bf 2709 if (a->rela->r_offset > b->rela->r_offset)
db6751f2
JJ
2710 return 1;
2711 return 0;
2712}
2713
2714static size_t
268b6b39 2715elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
db6751f2 2716{
e717da7e 2717 asection *reldyn;
f51e552e 2718 bfd_size_type count, size;
947216bf
AM
2719 size_t i, ret, sort_elt, ext_size;
2720 bfd_byte *sort, *s_non_relative, *p;
2721 struct elf_link_sort_rela *sq;
9c5bfbb7 2722 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
033fd5f9 2723 int i2e = bed->s->int_rels_per_ext_rel;
268b6b39
AM
2724 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2725 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
e717da7e 2726 struct bfd_link_order *lo;
db6751f2
JJ
2727
2728 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
2729 if (reldyn == NULL || reldyn->_raw_size == 0)
2730 {
2731 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
2732 if (reldyn == NULL || reldyn->_raw_size == 0)
2733 return 0;
947216bf
AM
2734 ext_size = sizeof (Elf_External_Rel);
2735 swap_in = bed->s->swap_reloc_in;
2736 swap_out = bed->s->swap_reloc_out;
db6751f2
JJ
2737 }
2738 else
947216bf
AM
2739 {
2740 ext_size = sizeof (Elf_External_Rela);
2741 swap_in = bed->s->swap_reloca_in;
2742 swap_out = bed->s->swap_reloca_out;
2743 }
2744 count = reldyn->_raw_size / ext_size;
db6751f2
JJ
2745
2746 size = 0;
e717da7e
AM
2747 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
2748 if (lo->type == bfd_indirect_link_order)
2749 {
2750 asection *o = lo->u.indirect.section;
2751 size += o->_raw_size;
2752 }
db6751f2
JJ
2753
2754 if (size != reldyn->_raw_size)
2755 return 0;
2756
947216bf
AM
2757 sort_elt = (sizeof (struct elf_link_sort_rela)
2758 + (i2e - 1) * sizeof (Elf_Internal_Rela));
2759 sort = bfd_zmalloc (sort_elt * count);
2760 if (sort == NULL)
db6751f2
JJ
2761 {
2762 (*info->callbacks->warning)
268b6b39 2763 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
db6751f2
JJ
2764 return 0;
2765 }
2766
e717da7e
AM
2767 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
2768 if (lo->type == bfd_indirect_link_order)
db6751f2 2769 {
947216bf 2770 bfd_byte *erel, *erelend;
e717da7e 2771 asection *o = lo->u.indirect.section;
db6751f2 2772
947216bf
AM
2773 erel = o->contents;
2774 erelend = o->contents + o->_raw_size;
2775 p = sort + o->output_offset / ext_size * sort_elt;
2776 while (erel < erelend)
db6751f2 2777 {
947216bf
AM
2778 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
2779 (*swap_in) (abfd, erel, s->rela);
2780 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
2781 p += sort_elt;
2782 erel += ext_size;
db6751f2
JJ
2783 }
2784 }
2785
268b6b39 2786 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
947216bf
AM
2787
2788 for (i = 0, p = sort; i < count; i++, p += sort_elt)
db6751f2 2789 {
947216bf
AM
2790 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
2791 if (s->type != reloc_class_relative)
2792 break;
db6751f2 2793 }
947216bf
AM
2794 ret = i;
2795 s_non_relative = p;
2796
2797 sq = (struct elf_link_sort_rela *) s_non_relative;
2798 for (; i < count; i++, p += sort_elt)
2799 {
2800 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
2801 if (ELF_R_SYM (sp->rela->r_info) != ELF_R_SYM (sq->rela->r_info))
2802 sq = sp;
2803 sp->offset = sq->rela->r_offset;
2804 }
2805
268b6b39 2806 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
dc810e39 2807
e717da7e
AM
2808 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
2809 if (lo->type == bfd_indirect_link_order)
db6751f2 2810 {
947216bf 2811 bfd_byte *erel, *erelend;
e717da7e 2812 asection *o = lo->u.indirect.section;
db6751f2 2813
947216bf
AM
2814 erel = o->contents;
2815 erelend = o->contents + o->_raw_size;
2816 p = sort + o->output_offset / ext_size * sort_elt;
2817 while (erel < erelend)
db6751f2 2818 {
947216bf
AM
2819 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
2820 (*swap_out) (abfd, s->rela, erel);
2821 p += sort_elt;
2822 erel += ext_size;
db6751f2
JJ
2823 }
2824 }
2825
5ed6aba4 2826 free (sort);
db6751f2
JJ
2827 *psec = reldyn;
2828 return ret;
2829}
2830
252b5132
RH
2831/* Do the final step of an ELF link. */
2832
b34976b6 2833bfd_boolean
268b6b39 2834elf_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
252b5132 2835{
b34976b6
AM
2836 bfd_boolean dynamic;
2837 bfd_boolean emit_relocs;
252b5132
RH
2838 bfd *dynobj;
2839 struct elf_final_link_info finfo;
2840 register asection *o;
2841 register struct bfd_link_order *p;
2842 register bfd *sub;
dc810e39
AM
2843 bfd_size_type max_contents_size;
2844 bfd_size_type max_external_reloc_size;
2845 bfd_size_type max_internal_reloc_count;
2846 bfd_size_type max_sym_count;
9ad5cbcf 2847 bfd_size_type max_sym_shndx_count;
252b5132
RH
2848 file_ptr off;
2849 Elf_Internal_Sym elfsym;
2850 unsigned int i;
2851 Elf_Internal_Shdr *symtab_hdr;
c97e73dd 2852 Elf_Internal_Shdr *symtab_shndx_hdr;
252b5132 2853 Elf_Internal_Shdr *symstrtab_hdr;
9c5bfbb7 2854 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 2855 struct elf_outext_info eoinfo;
b34976b6 2856 bfd_boolean merged;
db6751f2
JJ
2857 size_t relativecount = 0;
2858 asection *reldyn = 0;
dc810e39 2859 bfd_size_type amt;
252b5132 2860
0eddce27 2861 if (! is_elf_hash_table (info->hash))
b34976b6 2862 return FALSE;
8ea2e4bd 2863
252b5132
RH
2864 if (info->shared)
2865 abfd->flags |= DYNAMIC;
2866
2867 dynamic = elf_hash_table (info)->dynamic_sections_created;
2868 dynobj = elf_hash_table (info)->dynobj;
2869
1049f94e 2870 emit_relocs = (info->relocatable
c44233aa
AM
2871 || info->emitrelocations
2872 || bed->elf_backend_emit_relocs);
9317eacc 2873
252b5132
RH
2874 finfo.info = info;
2875 finfo.output_bfd = abfd;
2876 finfo.symstrtab = elf_stringtab_init ();
2877 if (finfo.symstrtab == NULL)
b34976b6 2878 return FALSE;
252b5132
RH
2879
2880 if (! dynamic)
2881 {
2882 finfo.dynsym_sec = NULL;
2883 finfo.hash_sec = NULL;
2884 finfo.symver_sec = NULL;
2885 }
2886 else
2887 {
2888 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
2889 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
2890 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
2891 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
2892 /* Note that it is OK if symver_sec is NULL. */
2893 }
2894
2895 finfo.contents = NULL;
2896 finfo.external_relocs = NULL;
2897 finfo.internal_relocs = NULL;
2898 finfo.external_syms = NULL;
9ad5cbcf 2899 finfo.locsym_shndx = NULL;
252b5132
RH
2900 finfo.internal_syms = NULL;
2901 finfo.indices = NULL;
2902 finfo.sections = NULL;
2903 finfo.symbuf = NULL;
9ad5cbcf 2904 finfo.symshndxbuf = NULL;
252b5132 2905 finfo.symbuf_count = 0;
c97e73dd 2906 finfo.shndxbuf_size = 0;
252b5132
RH
2907
2908 /* Count up the number of relocations we will output for each output
2909 section, so that we know the sizes of the reloc sections. We
2910 also figure out some maximum sizes. */
2911 max_contents_size = 0;
2912 max_external_reloc_size = 0;
2913 max_internal_reloc_count = 0;
2914 max_sym_count = 0;
9ad5cbcf 2915 max_sym_shndx_count = 0;
b34976b6 2916 merged = FALSE;
268b6b39 2917 for (o = abfd->sections; o != NULL; o = o->next)
252b5132 2918 {
3f9a32bd 2919 struct bfd_elf_section_data *esdo = elf_section_data (o);
252b5132
RH
2920 o->reloc_count = 0;
2921
2922 for (p = o->link_order_head; p != NULL; p = p->next)
2923 {
3f9a32bd
AM
2924 unsigned int reloc_count = 0;
2925 struct bfd_elf_section_data *esdi = NULL;
2926 unsigned int *rel_count1;
2927
252b5132
RH
2928 if (p->type == bfd_section_reloc_link_order
2929 || p->type == bfd_symbol_reloc_link_order)
3f9a32bd 2930 reloc_count = 1;
252b5132
RH
2931 else if (p->type == bfd_indirect_link_order)
2932 {
2933 asection *sec;
2934
2935 sec = p->u.indirect.section;
3f9a32bd 2936 esdi = elf_section_data (sec);
252b5132
RH
2937
2938 /* Mark all sections which are to be included in the
2939 link. This will normally be every section. We need
2940 to do this so that we can identify any sections which
2941 the linker has decided to not include. */
b34976b6 2942 sec->linker_mark = TRUE;
252b5132 2943
f5fa8ca2 2944 if (sec->flags & SEC_MERGE)
b34976b6 2945 merged = TRUE;
f5fa8ca2 2946
1049f94e 2947 if (info->relocatable || info->emitrelocations)
3f9a32bd 2948 reloc_count = sec->reloc_count;
c44233aa 2949 else if (bed->elf_backend_count_relocs)
9317eacc
CM
2950 {
2951 Elf_Internal_Rela * relocs;
2952
268b6b39
AM
2953 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
2954 info->keep_memory);
9317eacc 2955
3f9a32bd 2956 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
9317eacc 2957
6cdc0ccc 2958 if (elf_section_data (o)->relocs != relocs)
9317eacc
CM
2959 free (relocs);
2960 }
252b5132
RH
2961
2962 if (sec->_raw_size > max_contents_size)
2963 max_contents_size = sec->_raw_size;
2964 if (sec->_cooked_size > max_contents_size)
2965 max_contents_size = sec->_cooked_size;
2966
2967 /* We are interested in just local symbols, not all
2968 symbols. */
2969 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
2970 && (sec->owner->flags & DYNAMIC) == 0)
2971 {
2972 size_t sym_count;
2973
2974 if (elf_bad_symtab (sec->owner))
2975 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
2976 / sizeof (Elf_External_Sym));
2977 else
2978 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
2979
2980 if (sym_count > max_sym_count)
2981 max_sym_count = sym_count;
2982
9ad5cbcf
AM
2983 if (sym_count > max_sym_shndx_count
2984 && elf_symtab_shndx (sec->owner) != 0)
2985 max_sym_shndx_count = sym_count;
2986
252b5132
RH
2987 if ((sec->flags & SEC_RELOC) != 0)
2988 {
2989 size_t ext_size;
2990
2991 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
2992 if (ext_size > max_external_reloc_size)
2993 max_external_reloc_size = ext_size;
2994 if (sec->reloc_count > max_internal_reloc_count)
2995 max_internal_reloc_count = sec->reloc_count;
2996 }
2997 }
2998 }
3f9a32bd
AM
2999
3000 if (reloc_count == 0)
3001 continue;
3002
3003 o->reloc_count += reloc_count;
3004
3005 /* MIPS may have a mix of REL and RELA relocs on sections.
3006 To support this curious ABI we keep reloc counts in
3007 elf_section_data too. We must be careful to add the
3008 relocations from the input section to the right output
3009 count. FIXME: Get rid of one count. We have
3010 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
3011 rel_count1 = &esdo->rel_count;
3012 if (esdi != NULL)
3013 {
3014 bfd_boolean same_size;
3015 bfd_size_type entsize1;
3016
3017 entsize1 = esdi->rel_hdr.sh_entsize;
3018 BFD_ASSERT (entsize1 == sizeof (Elf_External_Rel)
3019 || entsize1 == sizeof (Elf_External_Rela));
3020 same_size = (!o->use_rela_p
3021 == (entsize1 == sizeof (Elf_External_Rel)));
3022
3023 if (!same_size)
3024 rel_count1 = &esdo->rel_count2;
3025
3026 if (esdi->rel_hdr2 != NULL)
3027 {
3028 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
3029 unsigned int alt_count;
3030 unsigned int *rel_count2;
3031
3032 BFD_ASSERT (entsize2 != entsize1
3033 && (entsize2 == sizeof (Elf_External_Rel)
3034 || entsize2 == sizeof (Elf_External_Rela)));
3035
3036 rel_count2 = &esdo->rel_count2;
3037 if (!same_size)
3038 rel_count2 = &esdo->rel_count;
3039
3040 /* The following is probably too simplistic if the
3041 backend counts output relocs unusually. */
3042 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
3043 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
3044 *rel_count2 += alt_count;
3045 reloc_count -= alt_count;
3046 }
3047 }
3048 *rel_count1 += reloc_count;
252b5132
RH
3049 }
3050
3051 if (o->reloc_count > 0)
3052 o->flags |= SEC_RELOC;
3053 else
3054 {
3055 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3056 set it (this is probably a bug) and if it is set
3057 assign_section_numbers will create a reloc section. */
3058 o->flags &=~ SEC_RELOC;
3059 }
3060
3061 /* If the SEC_ALLOC flag is not set, force the section VMA to
3062 zero. This is done in elf_fake_sections as well, but forcing
3063 the VMA to 0 here will ensure that relocs against these
3064 sections are handled correctly. */
3065 if ((o->flags & SEC_ALLOC) == 0
3066 && ! o->user_set_vma)
3067 o->vma = 0;
3068 }
3069
1049f94e 3070 if (! info->relocatable && merged)
f5fa8ca2 3071 elf_link_hash_traverse (elf_hash_table (info),
268b6b39 3072 _bfd_elf_link_sec_merge_syms, abfd);
f5fa8ca2 3073
252b5132
RH
3074 /* Figure out the file positions for everything but the symbol table
3075 and the relocs. We set symcount to force assign_section_numbers
3076 to create a symbol table. */
3077 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
3078 BFD_ASSERT (! abfd->output_has_begun);
3079 if (! _bfd_elf_compute_section_file_positions (abfd, info))
3080 goto error_return;
3081
3082 /* That created the reloc sections. Set their sizes, and assign
3083 them file positions, and allocate some buffers. */
3084 for (o = abfd->sections; o != NULL; o = o->next)
3085 {
3086 if ((o->flags & SEC_RELOC) != 0)
3087 {
45d6a902
AM
3088 if (!(_bfd_elf_link_size_reloc_section
3089 (abfd, &elf_section_data (o)->rel_hdr, o)))
252b5132
RH
3090 goto error_return;
3091
23bc299b 3092 if (elf_section_data (o)->rel_hdr2
45d6a902
AM
3093 && !(_bfd_elf_link_size_reloc_section
3094 (abfd, elf_section_data (o)->rel_hdr2, o)))
252b5132 3095 goto error_return;
252b5132 3096 }
b037af20
MM
3097
3098 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
3e932841 3099 to count upwards while actually outputting the relocations. */
b037af20
MM
3100 elf_section_data (o)->rel_count = 0;
3101 elf_section_data (o)->rel_count2 = 0;
252b5132
RH
3102 }
3103
3104 _bfd_elf_assign_file_positions_for_relocs (abfd);
3105
3106 /* We have now assigned file positions for all the sections except
3107 .symtab and .strtab. We start the .symtab section at the current
3108 file position, and write directly to it. We build the .strtab
3109 section in memory. */
3110 bfd_get_symcount (abfd) = 0;
3111 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3112 /* sh_name is set in prep_headers. */
3113 symtab_hdr->sh_type = SHT_SYMTAB;
c97e73dd 3114 /* sh_flags, sh_addr and sh_size all start off zero. */
252b5132
RH
3115 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
3116 /* sh_link is set in assign_section_numbers. */
3117 /* sh_info is set below. */
3118 /* sh_offset is set just below. */
45d6a902 3119 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
252b5132
RH
3120
3121 off = elf_tdata (abfd)->next_file_pos;
b34976b6 3122 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
252b5132
RH
3123
3124 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3125 incorrect. We do not yet know the size of the .symtab section.
3126 We correct next_file_pos below, after we do know the size. */
3127
3128 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3129 continuously seeking to the right position in the file. */
3130 if (! info->keep_memory || max_sym_count < 20)
3131 finfo.symbuf_size = 20;
3132 else
3133 finfo.symbuf_size = max_sym_count;
dc810e39
AM
3134 amt = finfo.symbuf_size;
3135 amt *= sizeof (Elf_External_Sym);
268b6b39 3136 finfo.symbuf = bfd_malloc (amt);
252b5132
RH
3137 if (finfo.symbuf == NULL)
3138 goto error_return;
9ad5cbcf
AM
3139 if (elf_numsections (abfd) > SHN_LORESERVE)
3140 {
c97e73dd
AM
3141 /* Wild guess at number of output symbols. realloc'd as needed. */
3142 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
3143 finfo.shndxbuf_size = amt;
9ad5cbcf 3144 amt *= sizeof (Elf_External_Sym_Shndx);
268b6b39 3145 finfo.symshndxbuf = bfd_zmalloc (amt);
9ad5cbcf
AM
3146 if (finfo.symshndxbuf == NULL)
3147 goto error_return;
3148 }
252b5132
RH
3149
3150 /* Start writing out the symbol table. The first symbol is always a
3151 dummy symbol. */
9317eacc
CM
3152 if (info->strip != strip_all
3153 || emit_relocs)
252b5132
RH
3154 {
3155 elfsym.st_value = 0;
3156 elfsym.st_size = 0;
3157 elfsym.st_info = 0;
3158 elfsym.st_other = 0;
3159 elfsym.st_shndx = SHN_UNDEF;
754021d0
AM
3160 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
3161 NULL))
252b5132
RH
3162 goto error_return;
3163 }
3164
3165#if 0
3166 /* Some standard ELF linkers do this, but we don't because it causes
3167 bootstrap comparison failures. */
3168 /* Output a file symbol for the output file as the second symbol.
3169 We output this even if we are discarding local symbols, although
3170 I'm not sure if this is correct. */
3171 elfsym.st_value = 0;
3172 elfsym.st_size = 0;
3173 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3174 elfsym.st_other = 0;
3175 elfsym.st_shndx = SHN_ABS;
3176 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
754021d0 3177 &elfsym, bfd_abs_section_ptr, NULL))
252b5132
RH
3178 goto error_return;
3179#endif
3180
3181 /* Output a symbol for each section. We output these even if we are
3182 discarding local symbols, since they are used for relocs. These
3183 symbols have no names. We store the index of each one in the
3184 index field of the section, so that we can find it again when
3185 outputting relocs. */
9317eacc
CM
3186 if (info->strip != strip_all
3187 || emit_relocs)
252b5132
RH
3188 {
3189 elfsym.st_size = 0;
3190 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3191 elfsym.st_other = 0;
9ad5cbcf 3192 for (i = 1; i < elf_numsections (abfd); i++)
252b5132
RH
3193 {
3194 o = section_from_elf_index (abfd, i);
3195 if (o != NULL)
3196 o->target_index = bfd_get_symcount (abfd);
3197 elfsym.st_shndx = i;
1049f94e 3198 if (info->relocatable || o == NULL)
252b5132
RH
3199 elfsym.st_value = 0;
3200 else
3201 elfsym.st_value = o->vma;
754021d0 3202 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
252b5132 3203 goto error_return;
c97e73dd 3204 if (i == SHN_LORESERVE - 1)
9ad5cbcf 3205 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132
RH
3206 }
3207 }
3208
3209 /* Allocate some memory to hold information read in from the input
3210 files. */
9ad5cbcf
AM
3211 if (max_contents_size != 0)
3212 {
268b6b39 3213 finfo.contents = bfd_malloc (max_contents_size);
9ad5cbcf
AM
3214 if (finfo.contents == NULL)
3215 goto error_return;
3216 }
3217
3218 if (max_external_reloc_size != 0)
3219 {
268b6b39 3220 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
9ad5cbcf
AM
3221 if (finfo.external_relocs == NULL)
3222 goto error_return;
3223 }
3224
3225 if (max_internal_reloc_count != 0)
3226 {
3227 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
3228 amt *= sizeof (Elf_Internal_Rela);
268b6b39 3229 finfo.internal_relocs = bfd_malloc (amt);
9ad5cbcf
AM
3230 if (finfo.internal_relocs == NULL)
3231 goto error_return;
3232 }
3233
3234 if (max_sym_count != 0)
3235 {
3236 amt = max_sym_count * sizeof (Elf_External_Sym);
268b6b39 3237 finfo.external_syms = bfd_malloc (amt);
9ad5cbcf
AM
3238 if (finfo.external_syms == NULL)
3239 goto error_return;
3240
3241 amt = max_sym_count * sizeof (Elf_Internal_Sym);
268b6b39 3242 finfo.internal_syms = bfd_malloc (amt);
9ad5cbcf
AM
3243 if (finfo.internal_syms == NULL)
3244 goto error_return;
3245
3246 amt = max_sym_count * sizeof (long);
268b6b39 3247 finfo.indices = bfd_malloc (amt);
9ad5cbcf
AM
3248 if (finfo.indices == NULL)
3249 goto error_return;
3250
3251 amt = max_sym_count * sizeof (asection *);
268b6b39 3252 finfo.sections = bfd_malloc (amt);
9ad5cbcf
AM
3253 if (finfo.sections == NULL)
3254 goto error_return;
3255 }
3256
3257 if (max_sym_shndx_count != 0)
3258 {
3259 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
268b6b39 3260 finfo.locsym_shndx = bfd_malloc (amt);
9ad5cbcf
AM
3261 if (finfo.locsym_shndx == NULL)
3262 goto error_return;
3263 }
252b5132 3264
e1918d23 3265 if (elf_hash_table (info)->tls_sec)
13ae64f3 3266 {
e1918d23 3267 bfd_vma base, end = 0;
13ae64f3
JJ
3268 asection *sec;
3269
e1918d23 3270 for (sec = elf_hash_table (info)->tls_sec;
13ae64f3
JJ
3271 sec && (sec->flags & SEC_THREAD_LOCAL);
3272 sec = sec->next)
3273 {
3274 bfd_vma size = sec->_raw_size;
3275
e1918d23 3276 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
13ae64f3
JJ
3277 {
3278 struct bfd_link_order *o;
3279
13ae64f3
JJ
3280 for (o = sec->link_order_head; o != NULL; o = o->next)
3281 if (size < o->offset + o->size)
58821868 3282 size = o->offset + o->size;
13ae64f3
JJ
3283 }
3284 end = sec->vma + size;
3285 }
e1918d23
AM
3286 base = elf_hash_table (info)->tls_sec->vma;
3287 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
3288 elf_hash_table (info)->tls_size = end - base;
13ae64f3
JJ
3289 }
3290
252b5132
RH
3291 /* Since ELF permits relocations to be against local symbols, we
3292 must have the local symbols available when we do the relocations.
3293 Since we would rather only read the local symbols once, and we
3294 would rather not keep them in memory, we handle all the
3295 relocations for a single input file at the same time.
3296
3297 Unfortunately, there is no way to know the total number of local
3298 symbols until we have seen all of them, and the local symbol
3299 indices precede the global symbol indices. This means that when
1049f94e 3300 we are generating relocatable output, and we see a reloc against
252b5132
RH
3301 a global symbol, we can not know the symbol index until we have
3302 finished examining all the local symbols to see which ones we are
3303 going to output. To deal with this, we keep the relocations in
3304 memory, and don't output them until the end of the link. This is
3305 an unfortunate waste of memory, but I don't see a good way around
1049f94e 3306 it. Fortunately, it only happens when performing a relocatable
252b5132
RH
3307 link, which is not the common case. FIXME: If keep_memory is set
3308 we could write the relocs out and then read them again; I don't
3309 know how bad the memory loss will be. */
3310
3311 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
b34976b6 3312 sub->output_has_begun = FALSE;
252b5132
RH
3313 for (o = abfd->sections; o != NULL; o = o->next)
3314 {
3315 for (p = o->link_order_head; p != NULL; p = p->next)
3316 {
3317 if (p->type == bfd_indirect_link_order
a50c1845 3318 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
4ddafab0 3319 == bfd_target_elf_flavour)
a50c1845 3320 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
252b5132 3321 {
252b5132
RH
3322 if (! sub->output_has_begun)
3323 {
3324 if (! elf_link_input_bfd (&finfo, sub))
3325 goto error_return;
b34976b6 3326 sub->output_has_begun = TRUE;
252b5132
RH
3327 }
3328 }
3329 else if (p->type == bfd_section_reloc_link_order
3330 || p->type == bfd_symbol_reloc_link_order)
3331 {
3332 if (! elf_reloc_link_order (abfd, info, o, p))
3333 goto error_return;
3334 }
3335 else
3336 {
3337 if (! _bfd_default_link_order (abfd, info, o, p))
3338 goto error_return;
3339 }
3340 }
3341 }
3342
c44233aa
AM
3343 /* Output any global symbols that got converted to local in a
3344 version script or due to symbol visibility. We do this in a
3345 separate step since ELF requires all local symbols to appear
3346 prior to any global symbols. FIXME: We should only do this if
3347 some global symbols were, in fact, converted to become local.
3348 FIXME: Will this work correctly with the Irix 5 linker? */
b34976b6 3349 eoinfo.failed = FALSE;
c44233aa 3350 eoinfo.finfo = &finfo;
b34976b6 3351 eoinfo.localsyms = TRUE;
c44233aa 3352 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
268b6b39 3353 &eoinfo);
c44233aa 3354 if (eoinfo.failed)
b34976b6 3355 return FALSE;
c44233aa 3356
252b5132 3357 /* That wrote out all the local symbols. Finish up the symbol table
5cc7c785
L
3358 with the global symbols. Even if we want to strip everything we
3359 can, we still need to deal with those global symbols that got
3e932841 3360 converted to local in a version script. */
252b5132 3361
30b30c21 3362 /* The sh_info field records the index of the first non local symbol. */
252b5132 3363 symtab_hdr->sh_info = bfd_get_symcount (abfd);
30b30c21 3364
fc8c40a0
AM
3365 if (dynamic
3366 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
30b30c21
RH
3367 {
3368 Elf_Internal_Sym sym;
3369 Elf_External_Sym *dynsym =
a7b97311 3370 (Elf_External_Sym *) finfo.dynsym_sec->contents;
71a40b32 3371 long last_local = 0;
30b30c21
RH
3372
3373 /* Write out the section symbols for the output sections. */
3374 if (info->shared)
3375 {
3376 asection *s;
3377
3378 sym.st_size = 0;
3379 sym.st_name = 0;
3380 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3381 sym.st_other = 0;
3382
3383 for (s = abfd->sections; s != NULL; s = s->next)
3384 {
3385 int indx;
9ad5cbcf
AM
3386 Elf_External_Sym *dest;
3387
30b30c21
RH
3388 indx = elf_section_data (s)->this_idx;
3389 BFD_ASSERT (indx > 0);
3390 sym.st_shndx = indx;
3391 sym.st_value = s->vma;
9ad5cbcf 3392 dest = dynsym + elf_section_data (s)->dynindx;
268b6b39 3393 elf_swap_symbol_out (abfd, &sym, dest, 0);
30b30c21
RH
3394 }
3395
3396 last_local = bfd_count_sections (abfd);
3397 }
3398
3399 /* Write out the local dynsyms. */
3400 if (elf_hash_table (info)->dynlocal)
3401 {
3402 struct elf_link_local_dynamic_entry *e;
3403 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
3404 {
318da145 3405 asection *s;
9ad5cbcf 3406 Elf_External_Sym *dest;
30b30c21 3407
b037af20
MM
3408 sym.st_size = e->isym.st_size;
3409 sym.st_other = e->isym.st_other;
3410
1fa0ddb3
RH
3411 /* Copy the internal symbol as is.
3412 Note that we saved a word of storage and overwrote
c44233aa
AM
3413 the original st_name with the dynstr_index. */
3414 sym = e->isym;
30b30c21 3415
c8e5ddc8 3416 if (e->isym.st_shndx != SHN_UNDEF
58821868
AM
3417 && (e->isym.st_shndx < SHN_LORESERVE
3418 || e->isym.st_shndx > SHN_HIRESERVE))
587ff49e
RH
3419 {
3420 s = bfd_section_from_elf_index (e->input_bfd,
3421 e->isym.st_shndx);
3422
3423 sym.st_shndx =
3424 elf_section_data (s->output_section)->this_idx;
3425 sym.st_value = (s->output_section->vma
3426 + s->output_offset
3427 + e->isym.st_value);
3428 }
30b30c21
RH
3429
3430 if (last_local < e->dynindx)
3431 last_local = e->dynindx;
3432
9ad5cbcf 3433 dest = dynsym + e->dynindx;
268b6b39 3434 elf_swap_symbol_out (abfd, &sym, dest, 0);
30b30c21
RH
3435 }
3436 }
3437
71a40b32
ILT
3438 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
3439 last_local + 1;
30b30c21 3440 }
252b5132
RH
3441
3442 /* We get the global symbols from the hash table. */
b34976b6
AM
3443 eoinfo.failed = FALSE;
3444 eoinfo.localsyms = FALSE;
252b5132
RH
3445 eoinfo.finfo = &finfo;
3446 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
268b6b39 3447 &eoinfo);
252b5132 3448 if (eoinfo.failed)
b34976b6 3449 return FALSE;
252b5132 3450
587ff49e
RH
3451 /* If backend needs to output some symbols not present in the hash
3452 table, do it now. */
3453 if (bed->elf_backend_output_arch_syms)
3454 {
b34976b6 3455 typedef bfd_boolean (*out_sym_func)
754021d0
AM
3456 (void *, const char *, Elf_Internal_Sym *, asection *,
3457 struct elf_link_hash_entry *);
dc810e39
AM
3458
3459 if (! ((*bed->elf_backend_output_arch_syms)
268b6b39 3460 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
b34976b6 3461 return FALSE;
3e932841 3462 }
587ff49e 3463
252b5132
RH
3464 /* Flush all symbols to the file. */
3465 if (! elf_link_flush_output_syms (&finfo))
b34976b6 3466 return FALSE;
252b5132
RH
3467
3468 /* Now we know the size of the symtab section. */
3469 off += symtab_hdr->sh_size;
3470
c97e73dd
AM
3471 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3472 if (symtab_shndx_hdr->sh_name != 0)
3473 {
3474 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
3475 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
3476 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
3477 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
3478 symtab_shndx_hdr->sh_size = amt;
3479
3480 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
b34976b6 3481 off, TRUE);
c97e73dd
AM
3482
3483 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
268b6b39 3484 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
b34976b6 3485 return FALSE;
c97e73dd
AM
3486 }
3487
3488
252b5132
RH
3489 /* Finish up and write out the symbol string table (.strtab)
3490 section. */
3491 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3492 /* sh_name was set in prep_headers. */
3493 symstrtab_hdr->sh_type = SHT_STRTAB;
3494 symstrtab_hdr->sh_flags = 0;
3495 symstrtab_hdr->sh_addr = 0;
3496 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
3497 symstrtab_hdr->sh_entsize = 0;
3498 symstrtab_hdr->sh_link = 0;
3499 symstrtab_hdr->sh_info = 0;
3500 /* sh_offset is set just below. */
3501 symstrtab_hdr->sh_addralign = 1;
3502
b34976b6 3503 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
252b5132
RH
3504 elf_tdata (abfd)->next_file_pos = off;
3505
3506 if (bfd_get_symcount (abfd) > 0)
3507 {
3508 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
3509 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
b34976b6 3510 return FALSE;
252b5132
RH
3511 }
3512
3513 /* Adjust the relocs to have the correct symbol indices. */
3514 for (o = abfd->sections; o != NULL; o = o->next)
3515 {
252b5132
RH
3516 if ((o->flags & SEC_RELOC) == 0)
3517 continue;
3518
3e932841 3519 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
31367b81
MM
3520 elf_section_data (o)->rel_count,
3521 elf_section_data (o)->rel_hashes);
3522 if (elf_section_data (o)->rel_hdr2 != NULL)
3523 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
3524 elf_section_data (o)->rel_count2,
3e932841 3525 (elf_section_data (o)->rel_hashes
31367b81 3526 + elf_section_data (o)->rel_count));
252b5132
RH
3527
3528 /* Set the reloc_count field to 0 to prevent write_relocs from
3529 trying to swap the relocs out itself. */
3530 o->reloc_count = 0;
3531 }
3532
db6751f2
JJ
3533 if (dynamic && info->combreloc && dynobj != NULL)
3534 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
3535
252b5132
RH
3536 /* If we are linking against a dynamic object, or generating a
3537 shared library, finish up the dynamic linking information. */
3538 if (dynamic)
3539 {
3540 Elf_External_Dyn *dyncon, *dynconend;
3541
3542 /* Fix up .dynamic entries. */
3543 o = bfd_get_section_by_name (dynobj, ".dynamic");
3544 BFD_ASSERT (o != NULL);
3545
3546 dyncon = (Elf_External_Dyn *) o->contents;
3547 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
3548 for (; dyncon < dynconend; dyncon++)
3549 {
3550 Elf_Internal_Dyn dyn;
3551 const char *name;
3552 unsigned int type;
3553
3554 elf_swap_dyn_in (dynobj, dyncon, &dyn);
3555
3556 switch (dyn.d_tag)
3557 {
3558 default:
3559 break;
db6751f2
JJ
3560 case DT_NULL:
3561 if (relativecount > 0 && dyncon + 1 < dynconend)
3562 {
3563 switch (elf_section_data (reldyn)->this_hdr.sh_type)
3564 {
3565 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
3566 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
3567 default: break;
3568 }
3569 if (dyn.d_tag != DT_NULL)
3570 {
3571 dyn.d_un.d_val = relativecount;
3572 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3573 relativecount = 0;
3574 }
3575 }
3576 break;
252b5132 3577 case DT_INIT:
f0c2e336 3578 name = info->init_function;
252b5132
RH
3579 goto get_sym;
3580 case DT_FINI:
f0c2e336 3581 name = info->fini_function;
252b5132
RH
3582 get_sym:
3583 {
3584 struct elf_link_hash_entry *h;
3585
3586 h = elf_link_hash_lookup (elf_hash_table (info), name,
b34976b6 3587 FALSE, FALSE, TRUE);
252b5132
RH
3588 if (h != NULL
3589 && (h->root.type == bfd_link_hash_defined
3590 || h->root.type == bfd_link_hash_defweak))
3591 {
3592 dyn.d_un.d_val = h->root.u.def.value;
3593 o = h->root.u.def.section;
3594 if (o->output_section != NULL)
3595 dyn.d_un.d_val += (o->output_section->vma
3596 + o->output_offset);
3597 else
3598 {
3599 /* The symbol is imported from another shared
3600 library and does not apply to this one. */
3601 dyn.d_un.d_val = 0;
3602 }
3603
3604 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3605 }
3606 }
3607 break;
3608
30831527
RH
3609 case DT_PREINIT_ARRAYSZ:
3610 name = ".preinit_array";
3611 goto get_size;
3612 case DT_INIT_ARRAYSZ:
3613 name = ".init_array";
3614 goto get_size;
3615 case DT_FINI_ARRAYSZ:
3616 name = ".fini_array";
3617 get_size:
3618 o = bfd_get_section_by_name (abfd, name);
2cb69dd3
JL
3619 if (o == NULL)
3620 {
3621 (*_bfd_error_handler)
3622 (_("%s: could not find output section %s"),
3623 bfd_get_filename (abfd), name);
3624 goto error_return;
3625 }
25e27870
L
3626 if (o->_raw_size == 0)
3627 (*_bfd_error_handler)
3628 (_("warning: %s section has zero size"), name);
30831527
RH
3629 dyn.d_un.d_val = o->_raw_size;
3630 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3631 break;
3632
3633 case DT_PREINIT_ARRAY:
3634 name = ".preinit_array";
3635 goto get_vma;
3636 case DT_INIT_ARRAY:
3637 name = ".init_array";
3638 goto get_vma;
3639 case DT_FINI_ARRAY:
3640 name = ".fini_array";
3641 goto get_vma;
3642
252b5132
RH
3643 case DT_HASH:
3644 name = ".hash";
3645 goto get_vma;
3646 case DT_STRTAB:
3647 name = ".dynstr";
3648 goto get_vma;
3649 case DT_SYMTAB:
3650 name = ".dynsym";
3651 goto get_vma;
3652 case DT_VERDEF:
3653 name = ".gnu.version_d";
3654 goto get_vma;
3655 case DT_VERNEED:
3656 name = ".gnu.version_r";
3657 goto get_vma;
3658 case DT_VERSYM:
3659 name = ".gnu.version";
3660 get_vma:
3661 o = bfd_get_section_by_name (abfd, name);
2cb69dd3
JL
3662 if (o == NULL)
3663 {
3664 (*_bfd_error_handler)
3665 (_("%s: could not find output section %s"),
3666 bfd_get_filename (abfd), name);
3667 goto error_return;
3668 }
252b5132
RH
3669 dyn.d_un.d_ptr = o->vma;
3670 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3671 break;
3672
3673 case DT_REL:
3674 case DT_RELA:
3675 case DT_RELSZ:
3676 case DT_RELASZ:
3677 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
3678 type = SHT_REL;
3679 else
3680 type = SHT_RELA;
3681 dyn.d_un.d_val = 0;
9ad5cbcf 3682 for (i = 1; i < elf_numsections (abfd); i++)
252b5132
RH
3683 {
3684 Elf_Internal_Shdr *hdr;
3685
3686 hdr = elf_elfsections (abfd)[i];
3687 if (hdr->sh_type == type
3688 && (hdr->sh_flags & SHF_ALLOC) != 0)
3689 {
3690 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
3691 dyn.d_un.d_val += hdr->sh_size;
3692 else
3693 {
3694 if (dyn.d_un.d_val == 0
3695 || hdr->sh_addr < dyn.d_un.d_val)
3696 dyn.d_un.d_val = hdr->sh_addr;
3697 }
3698 }
3699 }
3700 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3701 break;
3702 }
3703 }
3704 }
3705
3706 /* If we have created any dynamic sections, then output them. */
3707 if (dynobj != NULL)
3708 {
3709 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
3710 goto error_return;
3711
3712 for (o = dynobj->sections; o != NULL; o = o->next)
3713 {
3714 if ((o->flags & SEC_HAS_CONTENTS) == 0
fc8c40a0
AM
3715 || o->_raw_size == 0
3716 || o->output_section == bfd_abs_section_ptr)
252b5132
RH
3717 continue;
3718 if ((o->flags & SEC_LINKER_CREATED) == 0)
3719 {
3720 /* At this point, we are only interested in sections
45d6a902 3721 created by _bfd_elf_link_create_dynamic_sections. */
252b5132
RH
3722 continue;
3723 }
3724 if ((elf_section_data (o->output_section)->this_hdr.sh_type
3725 != SHT_STRTAB)
3726 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
3727 {
3728 if (! bfd_set_section_contents (abfd, o->output_section,
dc810e39
AM
3729 o->contents,
3730 (file_ptr) o->output_offset,
252b5132
RH
3731 o->_raw_size))
3732 goto error_return;
3733 }
3734 else
3735 {
252b5132 3736 /* The contents of the .dynstr section are actually in a
c44233aa 3737 stringtab. */
252b5132
RH
3738 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
3739 if (bfd_seek (abfd, off, SEEK_SET) != 0
2b0f7ef9
JJ
3740 || ! _bfd_elf_strtab_emit (abfd,
3741 elf_hash_table (info)->dynstr))
252b5132
RH
3742 goto error_return;
3743 }
3744 }
3745 }
3746
1049f94e 3747 if (info->relocatable)
1126897b 3748 {
b34976b6 3749 bfd_boolean failed = FALSE;
1126897b
AM
3750
3751 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3752 if (failed)
3753 goto error_return;
3754 }
3755
252b5132
RH
3756 /* If we have optimized stabs strings, output them. */
3757 if (elf_hash_table (info)->stab_info != NULL)
3758 {
3759 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
3760 goto error_return;
3761 }
3762
126495ed 3763 if (info->eh_frame_hdr)
65765700 3764 {
126495ed
AM
3765 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
3766 goto error_return;
65765700
JJ
3767 }
3768
252b5132
RH
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);
9ad5cbcf
AM
3779 if (finfo.locsym_shndx != NULL)
3780 free (finfo.locsym_shndx);
252b5132
RH
3781 if (finfo.internal_syms != NULL)
3782 free (finfo.internal_syms);
3783 if (finfo.indices != NULL)
3784 free (finfo.indices);
3785 if (finfo.sections != NULL)
3786 free (finfo.sections);
3787 if (finfo.symbuf != NULL)
3788 free (finfo.symbuf);
9ad5cbcf 3789 if (finfo.symshndxbuf != NULL)
c97e73dd 3790 free (finfo.symshndxbuf);
252b5132
RH
3791 for (o = abfd->sections; o != NULL; o = o->next)
3792 {
3793 if ((o->flags & SEC_RELOC) != 0
3794 && elf_section_data (o)->rel_hashes != NULL)
c44233aa 3795 free (elf_section_data (o)->rel_hashes);
252b5132
RH
3796 }
3797
b34976b6 3798 elf_tdata (abfd)->linker = TRUE;
252b5132 3799
b34976b6 3800 return TRUE;
252b5132
RH
3801
3802 error_return:
3803 if (finfo.symstrtab != NULL)
3804 _bfd_stringtab_free (finfo.symstrtab);
3805 if (finfo.contents != NULL)
3806 free (finfo.contents);
3807 if (finfo.external_relocs != NULL)
3808 free (finfo.external_relocs);
3809 if (finfo.internal_relocs != NULL)
3810 free (finfo.internal_relocs);
3811 if (finfo.external_syms != NULL)
3812 free (finfo.external_syms);
9ad5cbcf
AM
3813 if (finfo.locsym_shndx != NULL)
3814 free (finfo.locsym_shndx);
252b5132
RH
3815 if (finfo.internal_syms != NULL)
3816 free (finfo.internal_syms);
3817 if (finfo.indices != NULL)
3818 free (finfo.indices);
3819 if (finfo.sections != NULL)
3820 free (finfo.sections);
3821 if (finfo.symbuf != NULL)
3822 free (finfo.symbuf);
9ad5cbcf 3823 if (finfo.symshndxbuf != NULL)
c97e73dd 3824 free (finfo.symshndxbuf);
252b5132
RH
3825 for (o = abfd->sections; o != NULL; o = o->next)
3826 {
3827 if ((o->flags & SEC_RELOC) != 0
3828 && elf_section_data (o)->rel_hashes != NULL)
3829 free (elf_section_data (o)->rel_hashes);
3830 }
3831
b34976b6 3832 return FALSE;
252b5132
RH
3833}
3834
3835/* Add a symbol to the output symbol table. */
3836
b34976b6 3837static bfd_boolean
268b6b39
AM
3838elf_link_output_sym (struct elf_final_link_info *finfo,
3839 const char *name,
3840 Elf_Internal_Sym *elfsym,
754021d0
AM
3841 asection *input_sec,
3842 struct elf_link_hash_entry *h)
252b5132 3843{
9ad5cbcf
AM
3844 Elf_External_Sym *dest;
3845 Elf_External_Sym_Shndx *destshndx;
b34976b6 3846 bfd_boolean (*output_symbol_hook)
754021d0
AM
3847 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
3848 struct elf_link_hash_entry *);
252b5132
RH
3849
3850 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
3851 elf_backend_link_output_symbol_hook;
3852 if (output_symbol_hook != NULL)
3853 {
754021d0 3854 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
b34976b6 3855 return FALSE;
252b5132
RH
3856 }
3857
268b6b39 3858 if (name == NULL || *name == '\0')
252b5132
RH
3859 elfsym->st_name = 0;
3860 else if (input_sec->flags & SEC_EXCLUDE)
3861 elfsym->st_name = 0;
3862 else
3863 {
3864 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
b34976b6 3865 name, TRUE, FALSE);
252b5132 3866 if (elfsym->st_name == (unsigned long) -1)
b34976b6 3867 return FALSE;
252b5132
RH
3868 }
3869
3870 if (finfo->symbuf_count >= finfo->symbuf_size)
3871 {
3872 if (! elf_link_flush_output_syms (finfo))
b34976b6 3873 return FALSE;
252b5132
RH
3874 }
3875
9ad5cbcf
AM
3876 dest = finfo->symbuf + finfo->symbuf_count;
3877 destshndx = finfo->symshndxbuf;
3878 if (destshndx != NULL)
c97e73dd
AM
3879 {
3880 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
3881 {
3882 bfd_size_type amt;
252b5132 3883
c97e73dd
AM
3884 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
3885 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
3886 if (destshndx == NULL)
b34976b6 3887 return FALSE;
c97e73dd
AM
3888 memset ((char *) destshndx + amt, 0, amt);
3889 finfo->shndxbuf_size *= 2;
3890 }
3891 destshndx += bfd_get_symcount (finfo->output_bfd);
3892 }
3893
268b6b39 3894 elf_swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
c97e73dd
AM
3895 finfo->symbuf_count += 1;
3896 bfd_get_symcount (finfo->output_bfd) += 1;
252b5132 3897
b34976b6 3898 return TRUE;
252b5132
RH
3899}
3900
3901/* Flush the output symbols to the file. */
3902
b34976b6 3903static bfd_boolean
268b6b39 3904elf_link_flush_output_syms (struct elf_final_link_info *finfo)
252b5132
RH
3905{
3906 if (finfo->symbuf_count > 0)
3907 {
9ad5cbcf 3908 Elf_Internal_Shdr *hdr;
dc810e39
AM
3909 file_ptr pos;
3910 bfd_size_type amt;
252b5132 3911
9ad5cbcf
AM
3912 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
3913 pos = hdr->sh_offset + hdr->sh_size;
dc810e39
AM
3914 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
3915 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
268b6b39 3916 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
b34976b6 3917 return FALSE;
252b5132 3918
9ad5cbcf 3919 hdr->sh_size += amt;
252b5132
RH
3920 finfo->symbuf_count = 0;
3921 }
3922
b34976b6 3923 return TRUE;
252b5132
RH
3924}
3925
f5d44ba0
AM
3926/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
3927 allowing an unsatisfied unversioned symbol in the DSO to match a
71552942
L
3928 versioned symbol that would normally require an explicit version.
3929 We also handle the case that a DSO references a hidden symbol
3930 which may be satisfied by a versioned symbol in another DSO. */
f5d44ba0 3931
b34976b6 3932static bfd_boolean
268b6b39
AM
3933elf_link_check_versioned_symbol (struct bfd_link_info *info,
3934 struct elf_link_hash_entry *h)
f5d44ba0 3935{
71552942 3936 bfd *abfd;
f5d44ba0 3937 struct elf_link_loaded_list *loaded;
f5d44ba0 3938
0eddce27 3939 if (!is_elf_hash_table (info->hash))
b34976b6 3940 return FALSE;
f5d44ba0 3941
71552942
L
3942 switch (h->root.type)
3943 {
3944 default:
3945 abfd = NULL;
3946 break;
3947
3948 case bfd_link_hash_undefined:
3949 case bfd_link_hash_undefweak:
3950 abfd = h->root.u.undef.abfd;
4a43e768
AM
3951 if ((abfd->flags & DYNAMIC) == 0
3952 || elf_dyn_lib_class (abfd) != DYN_DT_NEEDED)
71552942
L
3953 return FALSE;
3954 break;
3955
3956 case bfd_link_hash_defined:
3957 case bfd_link_hash_defweak:
3958 abfd = h->root.u.def.section->owner;
3959 break;
3960
3961 case bfd_link_hash_common:
3962 abfd = h->root.u.c.p->section->owner;
3963 break;
3964 }
3965 BFD_ASSERT (abfd != NULL);
3966
f5d44ba0
AM
3967 for (loaded = elf_hash_table (info)->loaded;
3968 loaded != NULL;
3969 loaded = loaded->next)
3970 {
3971 bfd *input;
3972 Elf_Internal_Shdr *hdr;
3973 bfd_size_type symcount;
3974 bfd_size_type extsymcount;
3975 bfd_size_type extsymoff;
3976 Elf_Internal_Shdr *versymhdr;
6cdc0ccc
AM
3977 Elf_Internal_Sym *isym;
3978 Elf_Internal_Sym *isymend;
3979 Elf_Internal_Sym *isymbuf;
f5d44ba0 3980 Elf_External_Versym *ever;
6cdc0ccc 3981 Elf_External_Versym *extversym;
f5d44ba0
AM
3982
3983 input = loaded->abfd;
3984
3985 /* We check each DSO for a possible hidden versioned definition. */
71552942 3986 if (input == abfd
f5d44ba0
AM
3987 || (input->flags & DYNAMIC) == 0
3988 || elf_dynversym (input) == 0)
3989 continue;
3990
3991 hdr = &elf_tdata (input)->dynsymtab_hdr;
3992
3993 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
3994 if (elf_bad_symtab (input))
3995 {
3996 extsymcount = symcount;
3997 extsymoff = 0;
3998 }
3999 else
4000 {
4001 extsymcount = symcount - hdr->sh_info;
4002 extsymoff = hdr->sh_info;
4003 }
4004
4005 if (extsymcount == 0)
4006 continue;
4007
6cdc0ccc
AM
4008 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
4009 NULL, NULL, NULL);
4010 if (isymbuf == NULL)
b34976b6 4011 return FALSE;
f5d44ba0 4012
f5d44ba0
AM
4013 /* Read in any version definitions. */
4014 versymhdr = &elf_tdata (input)->dynversym_hdr;
268b6b39 4015 extversym = bfd_malloc (versymhdr->sh_size);
f5d44ba0
AM
4016 if (extversym == NULL)
4017 goto error_ret;
4018
4019 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
268b6b39 4020 || (bfd_bread (extversym, versymhdr->sh_size, input)
f5d44ba0
AM
4021 != versymhdr->sh_size))
4022 {
4023 free (extversym);
4024 error_ret:
6cdc0ccc 4025 free (isymbuf);
b34976b6 4026 return FALSE;
f5d44ba0
AM
4027 }
4028
4029 ever = extversym + extsymoff;
6cdc0ccc
AM
4030 isymend = isymbuf + extsymcount;
4031 for (isym = isymbuf; isym < isymend; isym++, ever++)
f5d44ba0
AM
4032 {
4033 const char *name;
f5d44ba0 4034 Elf_Internal_Versym iver;
a15d9d3a 4035 unsigned short version_index;
f5d44ba0 4036
6cdc0ccc
AM
4037 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
4038 || isym->st_shndx == SHN_UNDEF)
f5d44ba0
AM
4039 continue;
4040
4041 name = bfd_elf_string_from_elf_section (input,
4042 hdr->sh_link,
6cdc0ccc 4043 isym->st_name);
f5d44ba0
AM
4044 if (strcmp (name, h->root.root.string) != 0)
4045 continue;
4046
4047 _bfd_elf_swap_versym_in (input, ever, &iver);
4048
4049 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
4050 {
4051 /* If we have a non-hidden versioned sym, then it should
4052 have provided a definition for the undefined sym. */
4053 abort ();
4054 }
4055
a15d9d3a
L
4056 version_index = iver.vs_vers & VERSYM_VERSION;
4057 if (version_index == 1 || version_index == 2)
f5d44ba0 4058 {
a15d9d3a 4059 /* This is the base or first version. We can use it. */
f5d44ba0 4060 free (extversym);
6cdc0ccc 4061 free (isymbuf);
b34976b6 4062 return TRUE;
f5d44ba0
AM
4063 }
4064 }
4065
4066 free (extversym);
6cdc0ccc 4067 free (isymbuf);
f5d44ba0
AM
4068 }
4069
b34976b6 4070 return FALSE;
f5d44ba0
AM
4071}
4072
252b5132
RH
4073/* Add an external symbol to the symbol table. This is called from
4074 the hash table traversal routine. When generating a shared object,
4075 we go through the symbol table twice. The first time we output
4076 anything that might have been forced to local scope in a version
4077 script. The second time we output the symbols that are still
4078 global symbols. */
4079
b34976b6 4080static bfd_boolean
268b6b39 4081elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
252b5132 4082{
268b6b39 4083 struct elf_outext_info *eoinfo = data;
252b5132 4084 struct elf_final_link_info *finfo = eoinfo->finfo;
b34976b6 4085 bfd_boolean strip;
252b5132
RH
4086 Elf_Internal_Sym sym;
4087 asection *input_sec;
4088
e92d460e
AM
4089 if (h->root.type == bfd_link_hash_warning)
4090 {
4091 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4092 if (h->root.type == bfd_link_hash_new)
b34976b6 4093 return TRUE;
e92d460e
AM
4094 }
4095
252b5132
RH
4096 /* Decide whether to output this symbol in this pass. */
4097 if (eoinfo->localsyms)
4098 {
4099 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
b34976b6 4100 return TRUE;
252b5132
RH
4101 }
4102 else
4103 {
4104 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
b34976b6 4105 return TRUE;
252b5132
RH
4106 }
4107
560e09e9
NC
4108 /* If we have an undefined symbol reference here then it must have
4109 come from a shared library that is being linked in. (Undefined
4110 references in regular files have already been handled). If we
4111 are reporting errors for this situation then do so now. */
4112 if (h->root.type == bfd_link_hash_undefined
252b5132 4113 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
f5d44ba0 4114 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
560e09e9
NC
4115 && ! elf_link_check_versioned_symbol (finfo->info, h)
4116 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
252b5132
RH
4117 {
4118 if (! ((*finfo->info->callbacks->undefined_symbol)
4119 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
560e09e9 4120 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
252b5132 4121 {
b34976b6
AM
4122 eoinfo->failed = TRUE;
4123 return FALSE;
252b5132
RH
4124 }
4125 }
4126
1b1fe8fe
L
4127 /* We should also warn if a forced local symbol is referenced from
4128 shared libraries. */
1049f94e 4129 if (! finfo->info->relocatable
560e09e9 4130 && (! finfo->info->shared)
1b1fe8fe 4131 && (h->elf_link_hash_flags
560e09e9 4132 & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK))
71552942
L
4133 == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)
4134 && ! elf_link_check_versioned_symbol (finfo->info, h))
1b1fe8fe
L
4135 {
4136 (*_bfd_error_handler)
4137 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
4138 bfd_get_filename (finfo->output_bfd),
4139 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
4140 ? "internal"
4141 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
4142 ? "hidden" : "local",
4143 h->root.root.string,
4144 bfd_archive_filename (h->root.u.def.section->owner));
4145 eoinfo->failed = TRUE;
4146 return FALSE;
4147 }
4148
252b5132
RH
4149 /* We don't want to output symbols that have never been mentioned by
4150 a regular file, or that we have been told to strip. However, if
4151 h->indx is set to -2, the symbol is used by a reloc and we must
4152 output it. */
4153 if (h->indx == -2)
b34976b6 4154 strip = FALSE;
252b5132
RH
4155 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4156 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
4157 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4158 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
b34976b6 4159 strip = TRUE;
75828352
AM
4160 else if (finfo->info->strip == strip_all)
4161 strip = TRUE;
4162 else if (finfo->info->strip == strip_some
4163 && bfd_hash_lookup (finfo->info->keep_hash,
4164 h->root.root.string, FALSE, FALSE) == NULL)
4165 strip = TRUE;
4166 else if (finfo->info->strip_discarded
4167 && (h->root.type == bfd_link_hash_defined
4168 || h->root.type == bfd_link_hash_defweak)
4169 && elf_discarded_section (h->root.u.def.section))
b34976b6 4170 strip = TRUE;
252b5132 4171 else
b34976b6 4172 strip = FALSE;
252b5132
RH
4173
4174 /* If we're stripping it, and it's not a dynamic symbol, there's
2bd171e0
ILT
4175 nothing else to do unless it is a forced local symbol. */
4176 if (strip
4177 && h->dynindx == -1
4178 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
b34976b6 4179 return TRUE;
252b5132
RH
4180
4181 sym.st_value = 0;
4182 sym.st_size = h->size;
4183 sym.st_other = h->other;
4184 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4185 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
4186 else if (h->root.type == bfd_link_hash_undefweak
4187 || h->root.type == bfd_link_hash_defweak)
4188 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
4189 else
4190 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
4191
4192 switch (h->root.type)
4193 {
4194 default:
4195 case bfd_link_hash_new:
e92d460e 4196 case bfd_link_hash_warning:
252b5132 4197 abort ();
b34976b6 4198 return FALSE;
252b5132
RH
4199
4200 case bfd_link_hash_undefined:
252b5132
RH
4201 case bfd_link_hash_undefweak:
4202 input_sec = bfd_und_section_ptr;
4203 sym.st_shndx = SHN_UNDEF;
4204 break;
4205
4206 case bfd_link_hash_defined:
4207 case bfd_link_hash_defweak:
4208 {
4209 input_sec = h->root.u.def.section;
4210 if (input_sec->output_section != NULL)
4211 {
4212 sym.st_shndx =
4213 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
4214 input_sec->output_section);
9ad5cbcf 4215 if (sym.st_shndx == SHN_BAD)
252b5132
RH
4216 {
4217 (*_bfd_error_handler)
4218 (_("%s: could not find output section %s for input section %s"),
4219 bfd_get_filename (finfo->output_bfd),
4220 input_sec->output_section->name,
4221 input_sec->name);
b34976b6
AM
4222 eoinfo->failed = TRUE;
4223 return FALSE;
252b5132
RH
4224 }
4225
1049f94e
AM
4226 /* ELF symbols in relocatable files are section relative,
4227 but in nonrelocatable files they are virtual
252b5132
RH
4228 addresses. */
4229 sym.st_value = h->root.u.def.value + input_sec->output_offset;
1049f94e 4230 if (! finfo->info->relocatable)
13ae64f3
JJ
4231 {
4232 sym.st_value += input_sec->output_section->vma;
4233 if (h->type == STT_TLS)
4234 {
4235 /* STT_TLS symbols are relative to PT_TLS segment
4236 base. */
e1918d23
AM
4237 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
4238 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
13ae64f3
JJ
4239 }
4240 }
252b5132
RH
4241 }
4242 else
4243 {
4244 BFD_ASSERT (input_sec->owner == NULL
4245 || (input_sec->owner->flags & DYNAMIC) != 0);
4246 sym.st_shndx = SHN_UNDEF;
4247 input_sec = bfd_und_section_ptr;
4248 }
4249 }
4250 break;
4251
4252 case bfd_link_hash_common:
4253 input_sec = h->root.u.c.p->section;
4254 sym.st_shndx = SHN_COMMON;
4255 sym.st_value = 1 << h->root.u.c.p->alignment_power;
4256 break;
4257
4258 case bfd_link_hash_indirect:
4259 /* These symbols are created by symbol versioning. They point
c44233aa
AM
4260 to the decorated version of the name. For example, if the
4261 symbol foo@@GNU_1.2 is the default, which should be used when
4262 foo is used with no version, then we add an indirect symbol
4263 foo which points to foo@@GNU_1.2. We ignore these symbols,
4264 since the indirected symbol is already in the hash table. */
b34976b6 4265 return TRUE;
252b5132
RH
4266 }
4267
4268 /* Give the processor backend a chance to tweak the symbol value,
4269 and also to finish up anything that needs to be done for this
c44233aa
AM
4270 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
4271 forced local syms when non-shared is due to a historical quirk. */
252b5132
RH
4272 if ((h->dynindx != -1
4273 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
ef5aade5
L
4274 && ((finfo->info->shared
4275 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4276 || h->root.type != bfd_link_hash_undefweak))
c44233aa 4277 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
252b5132
RH
4278 && elf_hash_table (finfo->info)->dynamic_sections_created)
4279 {
9c5bfbb7 4280 const struct elf_backend_data *bed;
252b5132
RH
4281
4282 bed = get_elf_backend_data (finfo->output_bfd);
4283 if (! ((*bed->elf_backend_finish_dynamic_symbol)
4284 (finfo->output_bfd, finfo->info, h, &sym)))
4285 {
b34976b6
AM
4286 eoinfo->failed = TRUE;
4287 return FALSE;
252b5132
RH
4288 }
4289 }
4290
4291 /* If we are marking the symbol as undefined, and there are no
4292 non-weak references to this symbol from a regular object, then
91d3970e
ILT
4293 mark the symbol as weak undefined; if there are non-weak
4294 references, mark the symbol as strong. We can't do this earlier,
252b5132
RH
4295 because it might not be marked as undefined until the
4296 finish_dynamic_symbol routine gets through with it. */
4297 if (sym.st_shndx == SHN_UNDEF
252b5132 4298 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
a7b97311
AM
4299 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
4300 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
91d3970e
ILT
4301 {
4302 int bindtype;
4303
4304 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
4305 bindtype = STB_GLOBAL;
4306 else
4307 bindtype = STB_WEAK;
4308 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
4309 }
252b5132 4310
1b1fe8fe
L
4311 /* If a non-weak symbol with non-default visibility is not defined
4312 locally, it is a fatal error. */
1049f94e 4313 if (! finfo->info->relocatable
9c7a29a3 4314 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
1b1fe8fe 4315 && ELF_ST_BIND (sym.st_info) != STB_WEAK
22d5e339 4316 && h->root.type == bfd_link_hash_undefined
2cd533b7 4317 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1b1fe8fe
L
4318 {
4319 (*_bfd_error_handler)
4320 (_("%s: %s symbol `%s' isn't defined"),
4321 bfd_get_filename (finfo->output_bfd),
4322 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
4323 ? "protected"
4324 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
4325 ? "internal" : "hidden",
4326 h->root.root.string);
4327 eoinfo->failed = TRUE;
4328 return FALSE;
4329 }
32c092c3 4330
252b5132 4331 /* If this symbol should be put in the .dynsym section, then put it
f5d44ba0
AM
4332 there now. We already know the symbol index. We also fill in
4333 the entry in the .hash section. */
252b5132
RH
4334 if (h->dynindx != -1
4335 && elf_hash_table (finfo->info)->dynamic_sections_created)
4336 {
4337 size_t bucketcount;
4338 size_t bucket;
c7ac6ff8 4339 size_t hash_entry_size;
252b5132
RH
4340 bfd_byte *bucketpos;
4341 bfd_vma chain;
dc810e39 4342 Elf_External_Sym *esym;
252b5132
RH
4343
4344 sym.st_name = h->dynstr_index;
dc810e39 4345 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
268b6b39 4346 elf_swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
252b5132
RH
4347
4348 bucketcount = elf_hash_table (finfo->info)->bucketcount;
4349 bucket = h->elf_hash_value % bucketcount;
3e932841 4350 hash_entry_size
c7ac6ff8 4351 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
252b5132 4352 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
c7ac6ff8
MM
4353 + (bucket + 2) * hash_entry_size);
4354 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
268b6b39 4355 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
c7ac6ff8
MM
4356 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
4357 ((bfd_byte *) finfo->hash_sec->contents
4358 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
252b5132
RH
4359
4360 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
4361 {
4362 Elf_Internal_Versym iversym;
dc810e39 4363 Elf_External_Versym *eversym;
252b5132
RH
4364
4365 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4366 {
4367 if (h->verinfo.verdef == NULL)
4368 iversym.vs_vers = 0;
4369 else
4370 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
4371 }
4372 else
4373 {
4374 if (h->verinfo.vertree == NULL)
4375 iversym.vs_vers = 1;
4376 else
4377 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
4378 }
4379
4380 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
4381 iversym.vs_vers |= VERSYM_HIDDEN;
4382
dc810e39
AM
4383 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
4384 eversym += h->dynindx;
4385 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
252b5132
RH
4386 }
4387 }
4388
4389 /* If we're stripping it, then it was just a dynamic symbol, and
4390 there's nothing else to do. */
7330fb86 4391 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
b34976b6 4392 return TRUE;
252b5132
RH
4393
4394 h->indx = bfd_get_symcount (finfo->output_bfd);
4395
754021d0 4396 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
252b5132 4397 {
b34976b6
AM
4398 eoinfo->failed = TRUE;
4399 return FALSE;
252b5132
RH
4400 }
4401
b34976b6 4402 return TRUE;
252b5132
RH
4403}
4404
4405/* Link an input file into the linker output file. This function
4406 handles all the sections and relocations of the input file at once.
4407 This is so that we only have to read the local symbols once, and
4408 don't have to keep them in memory. */
4409
b34976b6 4410static bfd_boolean
268b6b39 4411elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
252b5132 4412{
b34976b6 4413 bfd_boolean (*relocate_section)
268b6b39
AM
4414 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
4415 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
252b5132
RH
4416 bfd *output_bfd;
4417 Elf_Internal_Shdr *symtab_hdr;
4418 size_t locsymcount;
4419 size_t extsymoff;
6cdc0ccc 4420 Elf_Internal_Sym *isymbuf;
252b5132 4421 Elf_Internal_Sym *isym;
6cdc0ccc 4422 Elf_Internal_Sym *isymend;
252b5132
RH
4423 long *pindex;
4424 asection **ppsection;
4425 asection *o;
9c5bfbb7 4426 const struct elf_backend_data *bed;
b34976b6 4427 bfd_boolean emit_relocs;
f8deed93 4428 struct elf_link_hash_entry **sym_hashes;
252b5132
RH
4429
4430 output_bfd = finfo->output_bfd;
c7ac6ff8
MM
4431 bed = get_elf_backend_data (output_bfd);
4432 relocate_section = bed->elf_backend_relocate_section;
252b5132
RH
4433
4434 /* If this is a dynamic object, we don't want to do anything here:
4435 we don't want the local symbols, and we don't want the section
4436 contents. */
4437 if ((input_bfd->flags & DYNAMIC) != 0)
b34976b6 4438 return TRUE;
252b5132 4439
1049f94e 4440 emit_relocs = (finfo->info->relocatable
c44233aa
AM
4441 || finfo->info->emitrelocations
4442 || bed->elf_backend_emit_relocs);
9317eacc 4443
252b5132
RH
4444 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4445 if (elf_bad_symtab (input_bfd))
4446 {
4447 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
4448 extsymoff = 0;
4449 }
4450 else
4451 {
4452 locsymcount = symtab_hdr->sh_info;
4453 extsymoff = symtab_hdr->sh_info;
4454 }
4455
4456 /* Read the local symbols. */
6cdc0ccc
AM
4457 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4458 if (isymbuf == NULL && locsymcount != 0)
4459 {
4460 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
4461 finfo->internal_syms,
4462 finfo->external_syms,
4463 finfo->locsym_shndx);
4464 if (isymbuf == NULL)
b34976b6 4465 return FALSE;
252b5132
RH
4466 }
4467
6cdc0ccc
AM
4468 /* Find local symbol sections and adjust values of symbols in
4469 SEC_MERGE sections. Write out those local symbols we know are
4470 going into the output file. */
4471 isymend = isymbuf + locsymcount;
4472 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
4473 isym < isymend;
4474 isym++, pindex++, ppsection++)
252b5132
RH
4475 {
4476 asection *isec;
4477 const char *name;
4478 Elf_Internal_Sym osym;
4479
252b5132
RH
4480 *pindex = -1;
4481
4482 if (elf_bad_symtab (input_bfd))
4483 {
4484 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
4485 {
4486 *ppsection = NULL;
4487 continue;
4488 }
4489 }
4490
4491 if (isym->st_shndx == SHN_UNDEF)
862517b6 4492 isec = bfd_und_section_ptr;
9ad5cbcf
AM
4493 else if (isym->st_shndx < SHN_LORESERVE
4494 || isym->st_shndx > SHN_HIRESERVE)
f5fa8ca2
JJ
4495 {
4496 isec = section_from_elf_index (input_bfd, isym->st_shndx);
65765700 4497 if (isec
68bfbfcc 4498 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
f5fa8ca2
JJ
4499 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
4500 isym->st_value =
4501 _bfd_merged_section_offset (output_bfd, &isec,
65765700 4502 elf_section_data (isec)->sec_info,
268b6b39 4503 isym->st_value, 0);
f5fa8ca2 4504 }
252b5132 4505 else if (isym->st_shndx == SHN_ABS)
862517b6 4506 isec = bfd_abs_section_ptr;
252b5132 4507 else if (isym->st_shndx == SHN_COMMON)
862517b6 4508 isec = bfd_com_section_ptr;
252b5132
RH
4509 else
4510 {
4511 /* Who knows? */
4512 isec = NULL;
4513 }
4514
4515 *ppsection = isec;
4516
4517 /* Don't output the first, undefined, symbol. */
6cdc0ccc 4518 if (ppsection == finfo->sections)
252b5132
RH
4519 continue;
4520
24376d1b
AM
4521 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4522 {
24376d1b
AM
4523 /* We never output section symbols. Instead, we use the
4524 section symbol of the corresponding section in the output
4525 file. */
4526 continue;
4527 }
4528
252b5132
RH
4529 /* If we are stripping all symbols, we don't want to output this
4530 one. */
4531 if (finfo->info->strip == strip_all)
4532 continue;
4533
252b5132 4534 /* If we are discarding all local symbols, we don't want to
1049f94e 4535 output this one. If we are generating a relocatable output
252b5132
RH
4536 file, then some of the local symbols may be required by
4537 relocs; we output them below as we discover that they are
4538 needed. */
4539 if (finfo->info->discard == discard_all)
4540 continue;
4541
4542 /* If this symbol is defined in a section which we are
c44233aa
AM
4543 discarding, we don't need to keep it, but note that
4544 linker_mark is only reliable for sections that have contents.
4545 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4546 as well as linker_mark. */
9ad5cbcf 4547 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
252b5132
RH
4548 && isec != NULL
4549 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
1049f94e 4550 || (! finfo->info->relocatable
252b5132
RH
4551 && (isec->flags & SEC_EXCLUDE) != 0)))
4552 continue;
4553
4554 /* Get the name of the symbol. */
4555 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
4556 isym->st_name);
4557 if (name == NULL)
b34976b6 4558 return FALSE;
252b5132
RH
4559
4560 /* See if we are discarding symbols with this name. */
4561 if ((finfo->info->strip == strip_some
b34976b6 4562 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
252b5132 4563 == NULL))
f5fa8ca2 4564 || (((finfo->info->discard == discard_sec_merge
1049f94e 4565 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
f5fa8ca2 4566 || finfo->info->discard == discard_l)
252b5132
RH
4567 && bfd_is_local_label_name (input_bfd, name)))
4568 continue;
4569
4570 /* If we get here, we are going to output this symbol. */
4571
4572 osym = *isym;
4573
4574 /* Adjust the section index for the output file. */
4575 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
4576 isec->output_section);
9ad5cbcf 4577 if (osym.st_shndx == SHN_BAD)
b34976b6 4578 return FALSE;
252b5132
RH
4579
4580 *pindex = bfd_get_symcount (output_bfd);
4581
1049f94e 4582 /* ELF symbols in relocatable files are section relative, but
252b5132
RH
4583 in executable files they are virtual addresses. Note that
4584 this code assumes that all ELF sections have an associated
4585 BFD section with a reasonable value for output_offset; below
4586 we assume that they also have a reasonable value for
4587 output_section. Any special sections must be set up to meet
4588 these requirements. */
4589 osym.st_value += isec->output_offset;
1049f94e 4590 if (! finfo->info->relocatable)
13ae64f3
JJ
4591 {
4592 osym.st_value += isec->output_section->vma;
4593 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
4594 {
4595 /* STT_TLS symbols are relative to PT_TLS segment base. */
e1918d23
AM
4596 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
4597 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
13ae64f3
JJ
4598 }
4599 }
252b5132 4600
754021d0 4601 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
b34976b6 4602 return FALSE;
252b5132
RH
4603 }
4604
4605 /* Relocate the contents of each section. */
f8deed93 4606 sym_hashes = elf_sym_hashes (input_bfd);
252b5132
RH
4607 for (o = input_bfd->sections; o != NULL; o = o->next)
4608 {
4609 bfd_byte *contents;
4610
4611 if (! o->linker_mark)
4612 {
4613 /* This section was omitted from the link. */
4614 continue;
4615 }
4616
4617 if ((o->flags & SEC_HAS_CONTENTS) == 0
4618 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
4619 continue;
4620
4621 if ((o->flags & SEC_LINKER_CREATED) != 0)
4622 {
45d6a902 4623 /* Section was created by _bfd_elf_link_create_dynamic_sections
252b5132
RH
4624 or somesuch. */
4625 continue;
4626 }
4627
4628 /* Get the contents of the section. They have been cached by a
c44233aa
AM
4629 relaxation routine. Note that o is a section in an input
4630 file, so the contents field will not have been set by any of
4631 the routines which work on output files. */
252b5132
RH
4632 if (elf_section_data (o)->this_hdr.contents != NULL)
4633 contents = elf_section_data (o)->this_hdr.contents;
4634 else
4635 {
4636 contents = finfo->contents;
268b6b39
AM
4637 if (! bfd_get_section_contents (input_bfd, o, contents, 0,
4638 o->_raw_size))
b34976b6 4639 return FALSE;
252b5132
RH
4640 }
4641
4642 if ((o->flags & SEC_RELOC) != 0)
4643 {
4644 Elf_Internal_Rela *internal_relocs;
4645
4646 /* Get the swapped relocs. */
45d6a902
AM
4647 internal_relocs
4648 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
4649 finfo->internal_relocs, FALSE);
252b5132
RH
4650 if (internal_relocs == NULL
4651 && o->reloc_count > 0)
b34976b6 4652 return FALSE;
252b5132 4653
ec338859
AM
4654 /* Run through the relocs looking for any against symbols
4655 from discarded sections and section symbols from
4656 removed link-once sections. Complain about relocs
4657 against discarded sections. Zero relocs against removed
f97b9cb8
L
4658 link-once sections. Preserve debug information as much
4659 as we can. */
4660 if (!elf_section_ignore_discarded_relocs (o))
ec338859
AM
4661 {
4662 Elf_Internal_Rela *rel, *relend;
50b4d486 4663
ec338859
AM
4664 rel = internal_relocs;
4665 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
4666 for ( ; rel < relend; rel++)
4667 {
4668 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
f97b9cb8 4669 asection *sec;
ec338859
AM
4670
4671 if (r_symndx >= locsymcount
4672 || (elf_bad_symtab (input_bfd)
4673 && finfo->sections[r_symndx] == NULL))
4674 {
4675 struct elf_link_hash_entry *h;
4676
4677 h = sym_hashes[r_symndx - extsymoff];
4678 while (h->root.type == bfd_link_hash_indirect
4679 || h->root.type == bfd_link_hash_warning)
4680 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4681
4682 /* Complain if the definition comes from a
4683 discarded section. */
f97b9cb8 4684 sec = h->root.u.def.section;
ec338859
AM
4685 if ((h->root.type == bfd_link_hash_defined
4686 || h->root.type == bfd_link_hash_defweak)
f97b9cb8 4687 && elf_discarded_section (sec))
ec338859 4688 {
ec338859
AM
4689 if ((o->flags & SEC_DEBUGGING) != 0)
4690 {
45e9217a 4691 BFD_ASSERT (r_symndx != 0);
f97b9cb8
L
4692 /* Try to preserve debug information. */
4693 if ((o->flags & SEC_DEBUGGING) != 0
4694 && sec->kept_section != NULL
6e35c4da 4695 && sec->_raw_size == sec->kept_section->_raw_size)
f97b9cb8
L
4696 h->root.u.def.section
4697 = sec->kept_section;
4698 else
4699 memset (rel, 0, sizeof (*rel));
ec338859
AM
4700 }
4701 else
6f6f27f8
L
4702 finfo->info->callbacks->error_handler
4703 (LD_DEFINITION_IN_DISCARDED_SECTION,
4704 _("%T: discarded in section `%s' from %s\n"),
4705 h->root.root.string,
4706 h->root.root.string,
4707 h->root.u.def.section->name,
4708 bfd_archive_filename (h->root.u.def.section->owner));
ec338859
AM
4709 }
4710 }
4711 else
4712 {
f97b9cb8 4713 sec = finfo->sections[r_symndx];
50b4d486 4714
ed4de5e2 4715 if (sec != NULL && elf_discarded_section (sec))
f9f32305 4716 {
ad43ed4c
L
4717 if ((o->flags & SEC_DEBUGGING) != 0
4718 || (sec->flags & SEC_LINK_ONCE) != 0)
f9f32305 4719 {
45e9217a 4720 BFD_ASSERT (r_symndx != 0);
f97b9cb8
L
4721 /* Try to preserve debug information. */
4722 if ((o->flags & SEC_DEBUGGING) != 0
4723 && sec->kept_section != NULL
6e35c4da 4724 && sec->_raw_size == sec->kept_section->_raw_size)
f97b9cb8
L
4725 finfo->sections[r_symndx]
4726 = sec->kept_section;
4727 else
4728 {
4729 rel->r_info
4730 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
4731 rel->r_addend = 0;
4732 }
f9f32305
AM
4733 }
4734 else
f9f32305 4735 {
6f6f27f8
L
4736 static int count;
4737 int ok;
4738 char *buf;
4739
4740 ok = asprintf (&buf, "local symbol %d",
4741 count++);
4742 if (ok <= 0)
4743 buf = (char *) "local symbol";
4744 finfo->info->callbacks->error_handler
4745 (LD_DEFINITION_IN_DISCARDED_SECTION,
4746 _("%T: discarded in section `%s' from %s\n"),
4747 buf, buf, sec->name,
4748 bfd_archive_filename (input_bfd));
4749 if (ok != -1)
f9f32305 4750 free (buf);
ec338859
AM
4751 }
4752 }
4753 }
4754 }
4755 }
50b4d486 4756
252b5132
RH
4757 /* Relocate the section by invoking a back end routine.
4758
4759 The back end routine is responsible for adjusting the
4760 section contents as necessary, and (if using Rela relocs
1049f94e 4761 and generating a relocatable output file) adjusting the
252b5132
RH
4762 reloc addend as necessary.
4763
4764 The back end routine does not have to worry about setting
4765 the reloc address or the reloc symbol index.
4766
4767 The back end routine is given a pointer to the swapped in
4768 internal symbols, and can access the hash table entries
4769 for the external symbols via elf_sym_hashes (input_bfd).
4770
1049f94e 4771 When generating relocatable output, the back end routine
252b5132
RH
4772 must handle STB_LOCAL/STT_SECTION symbols specially. The
4773 output symbol is going to be a section symbol
4774 corresponding to the output section, which will require
4775 the addend to be adjusted. */
4776
4777 if (! (*relocate_section) (output_bfd, finfo->info,
4778 input_bfd, o, contents,
4779 internal_relocs,
6cdc0ccc 4780 isymbuf,
252b5132 4781 finfo->sections))
b34976b6 4782 return FALSE;
252b5132 4783
9317eacc 4784 if (emit_relocs)
252b5132
RH
4785 {
4786 Elf_Internal_Rela *irela;
4787 Elf_Internal_Rela *irelaend;
73722af0 4788 bfd_vma last_offset;
252b5132 4789 struct elf_link_hash_entry **rel_hash;
c89583f8 4790 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
4e8a9624 4791 unsigned int next_erel;
b34976b6 4792 bfd_boolean (*reloc_emitter)
268b6b39 4793 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
b34976b6 4794 bfd_boolean rela_normal;
b491616a
AM
4795
4796 input_rel_hdr = &elf_section_data (o)->rel_hdr;
4797 rela_normal = (bed->rela_normal
4798 && (input_rel_hdr->sh_entsize
4799 == sizeof (Elf_External_Rela)));
252b5132
RH
4800
4801 /* Adjust the reloc addresses and symbol indices. */
4802
4803 irela = internal_relocs;
dc810e39 4804 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132 4805 rel_hash = (elf_section_data (o->output_section)->rel_hashes
31367b81
MM
4806 + elf_section_data (o->output_section)->rel_count
4807 + elf_section_data (o->output_section)->rel_count2);
73722af0 4808 last_offset = o->output_offset;
1049f94e 4809 if (!finfo->info->relocatable)
73722af0 4810 last_offset += o->output_section->vma;
209f668e 4811 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
252b5132
RH
4812 {
4813 unsigned long r_symndx;
252b5132 4814 asection *sec;
fad2542d 4815 Elf_Internal_Sym sym;
252b5132 4816
209f668e
NC
4817 if (next_erel == bed->s->int_rels_per_ext_rel)
4818 {
4819 rel_hash++;
4820 next_erel = 0;
4821 }
4822
d6fe2dc1
AM
4823 irela->r_offset = _bfd_elf_section_offset (output_bfd,
4824 finfo->info, o,
4825 irela->r_offset);
4826 if (irela->r_offset >= (bfd_vma) -2)
4827 {
73722af0
AM
4828 /* This is a reloc for a deleted entry or somesuch.
4829 Turn it into an R_*_NONE reloc, at the same
4830 offset as the last reloc. elf_eh_frame.c and
4831 elf_bfd_discard_info rely on reloc offsets
b34976b6 4832 being ordered. */
73722af0
AM
4833 irela->r_offset = last_offset;
4834 irela->r_info = 0;
4835 irela->r_addend = 0;
d6fe2dc1
AM
4836 continue;
4837 }
4838
252b5132
RH
4839 irela->r_offset += o->output_offset;
4840
7ad34365 4841 /* Relocs in an executable have to be virtual addresses. */
1049f94e 4842 if (!finfo->info->relocatable)
7ad34365
NC
4843 irela->r_offset += o->output_section->vma;
4844
73722af0 4845 last_offset = irela->r_offset;
252b5132 4846
73722af0
AM
4847 r_symndx = ELF_R_SYM (irela->r_info);
4848 if (r_symndx == STN_UNDEF)
252b5132
RH
4849 continue;
4850
4851 if (r_symndx >= locsymcount
4852 || (elf_bad_symtab (input_bfd)
4853 && finfo->sections[r_symndx] == NULL))
4854 {
4855 struct elf_link_hash_entry *rh;
209f668e 4856 unsigned long indx;
252b5132
RH
4857
4858 /* This is a reloc against a global symbol. We
4859 have not yet output all the local symbols, so
4860 we do not know the symbol index of any global
4861 symbol. We set the rel_hash entry for this
4862 reloc to point to the global hash table entry
4863 for this symbol. The symbol index is then
4864 set at the end of elf_bfd_final_link. */
4865 indx = r_symndx - extsymoff;
4866 rh = elf_sym_hashes (input_bfd)[indx];
4867 while (rh->root.type == bfd_link_hash_indirect
4868 || rh->root.type == bfd_link_hash_warning)
4869 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
4870
4871 /* Setting the index to -2 tells
4872 elf_link_output_extsym that this symbol is
4873 used by a reloc. */
4874 BFD_ASSERT (rh->indx < 0);
4875 rh->indx = -2;
4876
4877 *rel_hash = rh;
4878
4879 continue;
4880 }
4881
3e932841 4882 /* This is a reloc against a local symbol. */
252b5132
RH
4883
4884 *rel_hash = NULL;
fad2542d 4885 sym = isymbuf[r_symndx];
252b5132 4886 sec = finfo->sections[r_symndx];
fad2542d 4887 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
252b5132
RH
4888 {
4889 /* I suppose the backend ought to fill in the
4890 section of any STT_SECTION symbol against a
4891 processor specific section. If we have
4892 discarded a section, the output_section will
4893 be the absolute section. */
b491616a
AM
4894 if (bfd_is_abs_section (sec)
4895 || (sec != NULL
4896 && bfd_is_abs_section (sec->output_section)))
252b5132
RH
4897 r_symndx = 0;
4898 else if (sec == NULL || sec->owner == NULL)
4899 {
4900 bfd_set_error (bfd_error_bad_value);
b34976b6 4901 return FALSE;
252b5132
RH
4902 }
4903 else
4904 {
4905 r_symndx = sec->output_section->target_index;
4906 BFD_ASSERT (r_symndx != 0);
4907 }
b491616a
AM
4908
4909 /* Adjust the addend according to where the
f5d44ba0 4910 section winds up in the output section. */
b491616a
AM
4911 if (rela_normal)
4912 irela->r_addend += sec->output_offset;
252b5132
RH
4913 }
4914 else
4915 {
4916 if (finfo->indices[r_symndx] == -1)
4917 {
dc810e39 4918 unsigned long shlink;
252b5132
RH
4919 const char *name;
4920 asection *osec;
4921
4922 if (finfo->info->strip == strip_all)
4923 {
4924 /* You can't do ld -r -s. */
4925 bfd_set_error (bfd_error_invalid_operation);
b34976b6 4926 return FALSE;
252b5132
RH
4927 }
4928
4929 /* This symbol was skipped earlier, but
4930 since it is needed by a reloc, we
4931 must output it now. */
dc810e39 4932 shlink = symtab_hdr->sh_link;
a7b97311 4933 name = (bfd_elf_string_from_elf_section
fad2542d 4934 (input_bfd, shlink, sym.st_name));
252b5132 4935 if (name == NULL)
b34976b6 4936 return FALSE;
252b5132
RH
4937
4938 osec = sec->output_section;
fad2542d 4939 sym.st_shndx =
252b5132
RH
4940 _bfd_elf_section_from_bfd_section (output_bfd,
4941 osec);
fad2542d 4942 if (sym.st_shndx == SHN_BAD)
b34976b6 4943 return FALSE;
252b5132 4944
fad2542d 4945 sym.st_value += sec->output_offset;
1049f94e 4946 if (! finfo->info->relocatable)
13ae64f3 4947 {
fad2542d
AM
4948 sym.st_value += osec->vma;
4949 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
13ae64f3
JJ
4950 {
4951 /* STT_TLS symbols are relative to PT_TLS
4952 segment base. */
e1918d23
AM
4953 BFD_ASSERT (elf_hash_table (finfo->info)
4954 ->tls_sec != NULL);
4955 sym.st_value -= (elf_hash_table (finfo->info)
4956 ->tls_sec->vma);
13ae64f3
JJ
4957 }
4958 }
252b5132 4959
a7b97311
AM
4960 finfo->indices[r_symndx]
4961 = bfd_get_symcount (output_bfd);
252b5132 4962
754021d0
AM
4963 if (! elf_link_output_sym (finfo, name, &sym, sec,
4964 NULL))
b34976b6 4965 return FALSE;
252b5132
RH
4966 }
4967
4968 r_symndx = finfo->indices[r_symndx];
4969 }
4970
4971 irela->r_info = ELF_R_INFO (r_symndx,
4972 ELF_R_TYPE (irela->r_info));
4973 }
4974
4975 /* Swap out the relocs. */
c44233aa 4976 if (bed->elf_backend_emit_relocs
1049f94e 4977 && !(finfo->info->relocatable
a7b97311 4978 || finfo->info->emitrelocations))
c44233aa
AM
4979 reloc_emitter = bed->elf_backend_emit_relocs;
4980 else
45d6a902 4981 reloc_emitter = _bfd_elf_link_output_relocs;
9317eacc 4982
c89583f8
AM
4983 if (input_rel_hdr->sh_size != 0
4984 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
4985 internal_relocs))
b34976b6 4986 return FALSE;
9317eacc 4987
c89583f8
AM
4988 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
4989 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
c44233aa
AM
4990 {
4991 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
dc810e39 4992 * bed->s->int_rels_per_ext_rel);
c89583f8 4993 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
41241523 4994 internal_relocs))
b34976b6 4995 return FALSE;
c44233aa 4996 }
252b5132
RH
4997 }
4998 }
4999
5000 /* Write out the modified section contents. */
73d074b4 5001 if (bed->elf_backend_write_section
f9f32305 5002 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
73d074b4
DJ
5003 {
5004 /* Section written out. */
5005 }
68bfbfcc 5006 else switch (o->sec_info_type)
f5fa8ca2 5007 {
65765700 5008 case ELF_INFO_TYPE_STABS:
f5fa8ca2 5009 if (! (_bfd_write_section_stabs
65765700
JJ
5010 (output_bfd,
5011 &elf_hash_table (finfo->info)->stab_info,
5012 o, &elf_section_data (o)->sec_info, contents)))
b34976b6 5013 return FALSE;
65765700
JJ
5014 break;
5015 case ELF_INFO_TYPE_MERGE:
126495ed
AM
5016 if (! _bfd_write_merged_section (output_bfd, o,
5017 elf_section_data (o)->sec_info))
b34976b6 5018 return FALSE;
65765700
JJ
5019 break;
5020 case ELF_INFO_TYPE_EH_FRAME:
5021 {
126495ed
AM
5022 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
5023 o, contents))
b34976b6 5024 return FALSE;
65765700
JJ
5025 }
5026 break;
5027 default:
5028 {
5029 bfd_size_type sec_size;
5030
5031 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
5032 if (! (o->flags & SEC_EXCLUDE)
5033 && ! bfd_set_section_contents (output_bfd, o->output_section,
5034 contents,
5035 (file_ptr) o->output_offset,
5036 sec_size))
b34976b6 5037 return FALSE;
65765700
JJ
5038 }
5039 break;
252b5132
RH
5040 }
5041 }
5042
b34976b6 5043 return TRUE;
252b5132
RH
5044}
5045
5046/* Generate a reloc when linking an ELF file. This is a reloc
5047 requested by the linker, and does come from any input file. This
5048 is used to build constructor and destructor tables when linking
5049 with -Ur. */
5050
b34976b6 5051static bfd_boolean
268b6b39
AM
5052elf_reloc_link_order (bfd *output_bfd,
5053 struct bfd_link_info *info,
5054 asection *output_section,
5055 struct bfd_link_order *link_order)
252b5132
RH
5056{
5057 reloc_howto_type *howto;
5058 long indx;
5059 bfd_vma offset;
5060 bfd_vma addend;
5061 struct elf_link_hash_entry **rel_hash_ptr;
5062 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 5063 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
947216bf
AM
5064 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
5065 bfd_byte *erel;
5066 unsigned int i;
252b5132
RH
5067
5068 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5069 if (howto == NULL)
5070 {
5071 bfd_set_error (bfd_error_bad_value);
b34976b6 5072 return FALSE;
252b5132
RH
5073 }
5074
5075 addend = link_order->u.reloc.p->addend;
5076
5077 /* Figure out the symbol index. */
5078 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
31367b81
MM
5079 + elf_section_data (output_section)->rel_count
5080 + elf_section_data (output_section)->rel_count2);
252b5132
RH
5081 if (link_order->type == bfd_section_reloc_link_order)
5082 {
5083 indx = link_order->u.reloc.p->u.section->target_index;
5084 BFD_ASSERT (indx != 0);
5085 *rel_hash_ptr = NULL;
5086 }
5087 else
5088 {
5089 struct elf_link_hash_entry *h;
5090
5091 /* Treat a reloc against a defined symbol as though it were
c44233aa 5092 actually against the section. */
252b5132
RH
5093 h = ((struct elf_link_hash_entry *)
5094 bfd_wrapped_link_hash_lookup (output_bfd, info,
5095 link_order->u.reloc.p->u.name,
b34976b6 5096 FALSE, FALSE, TRUE));
252b5132
RH
5097 if (h != NULL
5098 && (h->root.type == bfd_link_hash_defined
5099 || h->root.type == bfd_link_hash_defweak))
5100 {
5101 asection *section;
5102
5103 section = h->root.u.def.section;
5104 indx = section->output_section->target_index;
5105 *rel_hash_ptr = NULL;
5106 /* It seems that we ought to add the symbol value to the
c44233aa
AM
5107 addend here, but in practice it has already been added
5108 because it was passed to constructor_callback. */
252b5132
RH
5109 addend += section->output_section->vma + section->output_offset;
5110 }
5111 else if (h != NULL)
5112 {
5113 /* Setting the index to -2 tells elf_link_output_extsym that
5114 this symbol is used by a reloc. */
5115 h->indx = -2;
5116 *rel_hash_ptr = h;
5117 indx = 0;
5118 }
5119 else
5120 {
5121 if (! ((*info->callbacks->unattached_reloc)
268b6b39 5122 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
b34976b6 5123 return FALSE;
252b5132
RH
5124 indx = 0;
5125 }
5126 }
5127
5128 /* If this is an inplace reloc, we must write the addend into the
5129 object file. */
5130 if (howto->partial_inplace && addend != 0)
5131 {
5132 bfd_size_type size;
5133 bfd_reloc_status_type rstat;
5134 bfd_byte *buf;
b34976b6 5135 bfd_boolean ok;
dc810e39 5136 const char *sym_name;
252b5132
RH
5137
5138 size = bfd_get_reloc_size (howto);
268b6b39
AM
5139 buf = bfd_zmalloc (size);
5140 if (buf == NULL)
b34976b6 5141 return FALSE;
268b6b39 5142 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
252b5132
RH
5143 switch (rstat)
5144 {
5145 case bfd_reloc_ok:
5146 break;
dc810e39 5147
252b5132
RH
5148 default:
5149 case bfd_reloc_outofrange:
5150 abort ();
dc810e39 5151
252b5132 5152 case bfd_reloc_overflow:
dc810e39
AM
5153 if (link_order->type == bfd_section_reloc_link_order)
5154 sym_name = bfd_section_name (output_bfd,
5155 link_order->u.reloc.p->u.section);
5156 else
5157 sym_name = link_order->u.reloc.p->u.name;
252b5132 5158 if (! ((*info->callbacks->reloc_overflow)
268b6b39 5159 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
252b5132
RH
5160 {
5161 free (buf);
b34976b6 5162 return FALSE;
252b5132
RH
5163 }
5164 break;
5165 }
268b6b39
AM
5166 ok = bfd_set_section_contents (output_bfd, output_section, buf,
5167 link_order->offset, size);
252b5132
RH
5168 free (buf);
5169 if (! ok)
b34976b6 5170 return FALSE;
252b5132
RH
5171 }
5172
5173 /* The address of a reloc is relative to the section in a
1049f94e 5174 relocatable file, and is a virtual address in an executable
252b5132
RH
5175 file. */
5176 offset = link_order->offset;
1049f94e 5177 if (! info->relocatable)
252b5132
RH
5178 offset += output_section->vma;
5179
947216bf
AM
5180 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
5181 {
5182 irel[i].r_offset = offset;
5183 irel[i].r_info = 0;
5184 irel[i].r_addend = 0;
5185 }
5186 irel[0].r_info = ELF_R_INFO (indx, howto->type);
252b5132 5187
947216bf
AM
5188 rel_hdr = &elf_section_data (output_section)->rel_hdr;
5189 erel = rel_hdr->contents;
252b5132
RH
5190 if (rel_hdr->sh_type == SHT_REL)
5191 {
947216bf
AM
5192 erel += (elf_section_data (output_section)->rel_count
5193 * sizeof (Elf_External_Rel));
5194 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
252b5132
RH
5195 }
5196 else
5197 {
947216bf
AM
5198 irel[0].r_addend = addend;
5199 erel += (elf_section_data (output_section)->rel_count
5200 * sizeof (Elf_External_Rela));
5201 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
252b5132
RH
5202 }
5203
0525d26e 5204 ++elf_section_data (output_section)->rel_count;
252b5132 5205
b34976b6 5206 return TRUE;
252b5132 5207}
252b5132 5208\f
252b5132
RH
5209/* Garbage collect unused sections. */
5210
b34976b6 5211static bfd_boolean elf_gc_sweep_symbol
268b6b39 5212 (struct elf_link_hash_entry *, void *);
252b5132 5213
b34976b6 5214static bfd_boolean elf_gc_allocate_got_offsets
268b6b39 5215 (struct elf_link_hash_entry *, void *);
252b5132
RH
5216
5217/* The mark phase of garbage collection. For a given section, mark
dbb410c3
AM
5218 it and any sections in this section's group, and all the sections
5219 which define symbols to which it refers. */
252b5132 5220
b34976b6 5221typedef asection * (*gc_mark_hook_fn)
268b6b39
AM
5222 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5223 struct elf_link_hash_entry *, Elf_Internal_Sym *);
b34976b6
AM
5224
5225static bfd_boolean
268b6b39
AM
5226elf_gc_mark (struct bfd_link_info *info,
5227 asection *sec,
5228 gc_mark_hook_fn gc_mark_hook)
252b5132 5229{
b34976b6 5230 bfd_boolean ret;
dbb410c3 5231 asection *group_sec;
252b5132
RH
5232
5233 sec->gc_mark = 1;
5234
dbb410c3
AM
5235 /* Mark all the sections in the group. */
5236 group_sec = elf_section_data (sec)->next_in_group;
5237 if (group_sec && !group_sec->gc_mark)
5238 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
b34976b6 5239 return FALSE;
252b5132 5240
dbb410c3 5241 /* Look through the section relocs. */
b34976b6 5242 ret = TRUE;
252b5132
RH
5243 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
5244 {
5245 Elf_Internal_Rela *relstart, *rel, *relend;
5246 Elf_Internal_Shdr *symtab_hdr;
5247 struct elf_link_hash_entry **sym_hashes;
5248 size_t nlocsyms;
5249 size_t extsymoff;
252b5132 5250 bfd *input_bfd = sec->owner;
9c5bfbb7 5251 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
6cdc0ccc 5252 Elf_Internal_Sym *isym = NULL;
252b5132
RH
5253
5254 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5255 sym_hashes = elf_sym_hashes (input_bfd);
5256
5257 /* Read the local symbols. */
5258 if (elf_bad_symtab (input_bfd))
5259 {
5260 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
c44233aa 5261 extsymoff = 0;
252b5132
RH
5262 }
5263 else
5264 extsymoff = nlocsyms = symtab_hdr->sh_info;
9ad5cbcf 5265
6cdc0ccc
AM
5266 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
5267 if (isym == NULL && nlocsyms != 0)
9ad5cbcf 5268 {
6cdc0ccc
AM
5269 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
5270 NULL, NULL, NULL);
5271 if (isym == NULL)
b34976b6 5272 return FALSE;
9ad5cbcf
AM
5273 }
5274
252b5132 5275 /* Read the relocations. */
268b6b39 5276 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
45d6a902 5277 info->keep_memory);
252b5132
RH
5278 if (relstart == NULL)
5279 {
b34976b6 5280 ret = FALSE;
252b5132
RH
5281 goto out1;
5282 }
c7ac6ff8 5283 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132
RH
5284
5285 for (rel = relstart; rel < relend; rel++)
5286 {
5287 unsigned long r_symndx;
5288 asection *rsec;
5289 struct elf_link_hash_entry *h;
252b5132
RH
5290
5291 r_symndx = ELF_R_SYM (rel->r_info);
5292 if (r_symndx == 0)
5293 continue;
5294
6cdc0ccc
AM
5295 if (r_symndx >= nlocsyms
5296 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
252b5132
RH
5297 {
5298 h = sym_hashes[r_symndx - extsymoff];
1e2f5b6e 5299 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
252b5132
RH
5300 }
5301 else
5302 {
6cdc0ccc 5303 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
252b5132
RH
5304 }
5305
5306 if (rsec && !rsec->gc_mark)
b91afed7
AM
5307 {
5308 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
5309 rsec->gc_mark = 1;
5310 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
5311 {
b34976b6 5312 ret = FALSE;
b91afed7
AM
5313 goto out2;
5314 }
5315 }
252b5132
RH
5316 }
5317
5318 out2:
6cdc0ccc 5319 if (elf_section_data (sec)->relocs != relstart)
252b5132
RH
5320 free (relstart);
5321 out1:
6cdc0ccc
AM
5322 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
5323 {
5324 if (! info->keep_memory)
5325 free (isym);
5326 else
5327 symtab_hdr->contents = (unsigned char *) isym;
5328 }
252b5132
RH
5329 }
5330
5331 return ret;
5332}
5333
5334/* The sweep phase of garbage collection. Remove all garbage sections. */
5335
b34976b6 5336typedef bfd_boolean (*gc_sweep_hook_fn)
268b6b39 5337 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
b34976b6
AM
5338
5339static bfd_boolean
268b6b39 5340elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
252b5132
RH
5341{
5342 bfd *sub;
5343
5344 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5345 {
5346 asection *o;
5347
f6af82bd
AM
5348 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5349 continue;
5350
252b5132
RH
5351 for (o = sub->sections; o != NULL; o = o->next)
5352 {
5353 /* Keep special sections. Keep .debug sections. */
5354 if ((o->flags & SEC_LINKER_CREATED)
5355 || (o->flags & SEC_DEBUGGING))
5356 o->gc_mark = 1;
5357
5358 if (o->gc_mark)
5359 continue;
5360
5361 /* Skip sweeping sections already excluded. */
5362 if (o->flags & SEC_EXCLUDE)
5363 continue;
5364
5365 /* Since this is early in the link process, it is simple
5366 to remove a section from the output. */
5367 o->flags |= SEC_EXCLUDE;
5368
5369 /* But we also have to update some of the relocation
5370 info we collected before. */
5371 if (gc_sweep_hook
5372 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
5373 {
5374 Elf_Internal_Rela *internal_relocs;
b34976b6 5375 bfd_boolean r;
252b5132 5376
45d6a902 5377 internal_relocs
268b6b39 5378 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
45d6a902 5379 info->keep_memory);
252b5132 5380 if (internal_relocs == NULL)
b34976b6 5381 return FALSE;
252b5132 5382
3e932841 5383 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
252b5132 5384
6cdc0ccc 5385 if (elf_section_data (o)->relocs != internal_relocs)
252b5132
RH
5386 free (internal_relocs);
5387
5388 if (!r)
b34976b6 5389 return FALSE;
252b5132
RH
5390 }
5391 }
5392 }
5393
5394 /* Remove the symbols that were in the swept sections from the dynamic
5395 symbol table. GCFIXME: Anyone know how to get them out of the
5396 static symbol table as well? */
5397 {
5398 int i = 0;
5399
268b6b39 5400 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
252b5132
RH
5401
5402 elf_hash_table (info)->dynsymcount = i;
5403 }
5404
b34976b6 5405 return TRUE;
252b5132
RH
5406}
5407
5408/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5409
b34976b6 5410static bfd_boolean
268b6b39 5411elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
252b5132 5412{
268b6b39 5413 int *idx = idxptr;
252b5132 5414
e92d460e
AM
5415 if (h->root.type == bfd_link_hash_warning)
5416 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5417
252b5132
RH
5418 if (h->dynindx != -1
5419 && ((h->root.type != bfd_link_hash_defined
5420 && h->root.type != bfd_link_hash_defweak)
5421 || h->root.u.def.section->gc_mark))
5422 h->dynindx = (*idx)++;
5423
b34976b6 5424 return TRUE;
252b5132
RH
5425}
5426
4cc11e76 5427/* Propagate collected vtable information. This is called through
252b5132
RH
5428 elf_link_hash_traverse. */
5429
b34976b6 5430static bfd_boolean
268b6b39 5431elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
252b5132 5432{
e92d460e
AM
5433 if (h->root.type == bfd_link_hash_warning)
5434 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5435
3e932841 5436 /* Those that are not vtables. */
252b5132 5437 if (h->vtable_parent == NULL)
b34976b6 5438 return TRUE;
252b5132
RH
5439
5440 /* Those vtables that do not have parents, we cannot merge. */
5441 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
b34976b6 5442 return TRUE;
252b5132
RH
5443
5444 /* If we've already been done, exit. */
5445 if (h->vtable_entries_used && h->vtable_entries_used[-1])
b34976b6 5446 return TRUE;
252b5132
RH
5447
5448 /* Make sure the parent's table is up to date. */
5449 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
5450
5451 if (h->vtable_entries_used == NULL)
5452 {
5453 /* None of this table's entries were referenced. Re-use the
5454 parent's table. */
5455 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
5456 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
5457 }
5458 else
5459 {
5460 size_t n;
b34976b6 5461 bfd_boolean *cu, *pu;
252b5132
RH
5462
5463 /* Or the parent's entries into ours. */
5464 cu = h->vtable_entries_used;
b34976b6 5465 cu[-1] = TRUE;
252b5132
RH
5466 pu = h->vtable_parent->vtable_entries_used;
5467 if (pu != NULL)
5468 {
9c5bfbb7
AM
5469 const struct elf_backend_data *bed;
5470 unsigned int log_file_align;
0d1ea5c0 5471
9c5bfbb7
AM
5472 bed = get_elf_backend_data (h->root.u.def.section->owner);
5473 log_file_align = bed->s->log_file_align;
45d6a902 5474 n = h->vtable_parent->vtable_entries_size >> log_file_align;
374b596d 5475 while (n--)
252b5132 5476 {
374b596d 5477 if (*pu)
b34976b6 5478 *cu = TRUE;
374b596d
NC
5479 pu++;
5480 cu++;
252b5132
RH
5481 }
5482 }
5483 }
5484
b34976b6 5485 return TRUE;
252b5132
RH
5486}
5487
b34976b6 5488static bfd_boolean
268b6b39 5489elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
252b5132
RH
5490{
5491 asection *sec;
5492 bfd_vma hstart, hend;
5493 Elf_Internal_Rela *relstart, *relend, *rel;
9c5bfbb7 5494 const struct elf_backend_data *bed;
45d6a902 5495 unsigned int log_file_align;
252b5132 5496
e92d460e
AM
5497 if (h->root.type == bfd_link_hash_warning)
5498 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5499
252b5132
RH
5500 /* Take care of both those symbols that do not describe vtables as
5501 well as those that are not loaded. */
5502 if (h->vtable_parent == NULL)
b34976b6 5503 return TRUE;
252b5132
RH
5504
5505 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5506 || h->root.type == bfd_link_hash_defweak);
5507
5508 sec = h->root.u.def.section;
5509 hstart = h->root.u.def.value;
5510 hend = hstart + h->size;
5511
268b6b39 5512 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
252b5132 5513 if (!relstart)
b34976b6 5514 return *(bfd_boolean *) okp = FALSE;
c7ac6ff8 5515 bed = get_elf_backend_data (sec->owner);
45d6a902 5516 log_file_align = bed->s->log_file_align;
0d1ea5c0 5517
c7ac6ff8 5518 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132
RH
5519
5520 for (rel = relstart; rel < relend; ++rel)
5521 if (rel->r_offset >= hstart && rel->r_offset < hend)
5522 {
5523 /* If the entry is in use, do nothing. */
5524 if (h->vtable_entries_used
5525 && (rel->r_offset - hstart) < h->vtable_entries_size)
5526 {
45d6a902 5527 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
252b5132
RH
5528 if (h->vtable_entries_used[entry])
5529 continue;
5530 }
5531 /* Otherwise, kill it. */
5532 rel->r_offset = rel->r_info = rel->r_addend = 0;
5533 }
5534
b34976b6 5535 return TRUE;
252b5132
RH
5536}
5537
5538/* Do mark and sweep of unused sections. */
5539
b34976b6 5540bfd_boolean
268b6b39 5541elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 5542{
b34976b6 5543 bfd_boolean ok = TRUE;
252b5132
RH
5544 bfd *sub;
5545 asection * (*gc_mark_hook)
268b6b39
AM
5546 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5547 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
252b5132
RH
5548
5549 if (!get_elf_backend_data (abfd)->can_gc_sections
0eddce27
AM
5550 || info->relocatable
5551 || info->emitrelocations
5552 || !is_elf_hash_table (info->hash)
252b5132 5553 || elf_hash_table (info)->dynamic_sections_created)
2627de83
AM
5554 {
5555 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
5556 return TRUE;
5557 }
252b5132
RH
5558
5559 /* Apply transitive closure to the vtable entry usage info. */
5560 elf_link_hash_traverse (elf_hash_table (info),
5561 elf_gc_propagate_vtable_entries_used,
268b6b39 5562 &ok);
252b5132 5563 if (!ok)
b34976b6 5564 return FALSE;
252b5132
RH
5565
5566 /* Kill the vtable relocations that were not used. */
5567 elf_link_hash_traverse (elf_hash_table (info),
5568 elf_gc_smash_unused_vtentry_relocs,
268b6b39 5569 &ok);
252b5132 5570 if (!ok)
b34976b6 5571 return FALSE;
252b5132
RH
5572
5573 /* Grovel through relocs to find out who stays ... */
5574
5575 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
5576 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5577 {
5578 asection *o;
f6af82bd
AM
5579
5580 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5581 continue;
5582
252b5132
RH
5583 for (o = sub->sections; o != NULL; o = o->next)
5584 {
5585 if (o->flags & SEC_KEEP)
c44233aa 5586 if (!elf_gc_mark (info, o, gc_mark_hook))
b34976b6 5587 return FALSE;
252b5132
RH
5588 }
5589 }
5590
5591 /* ... and mark SEC_EXCLUDE for those that go. */
a7b97311 5592 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
b34976b6 5593 return FALSE;
252b5132 5594
b34976b6 5595 return TRUE;
252b5132
RH
5596}
5597\f
4cc11e76 5598/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
252b5132 5599
b34976b6 5600bfd_boolean
268b6b39
AM
5601elf_gc_record_vtinherit (bfd *abfd,
5602 asection *sec,
5603 struct elf_link_hash_entry *h,
5604 bfd_vma offset)
252b5132
RH
5605{
5606 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
5607 struct elf_link_hash_entry **search, *child;
5608 bfd_size_type extsymcount;
5609
5610 /* The sh_info field of the symtab header tells us where the
5611 external symbols start. We don't care about the local symbols at
5612 this point. */
5613 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
5614 if (!elf_bad_symtab (abfd))
5615 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
5616
5617 sym_hashes = elf_sym_hashes (abfd);
5618 sym_hashes_end = sym_hashes + extsymcount;
5619
5620 /* Hunt down the child symbol, which is in this section at the same
5621 offset as the relocation. */
5622 for (search = sym_hashes; search != sym_hashes_end; ++search)
5623 {
5624 if ((child = *search) != NULL
5625 && (child->root.type == bfd_link_hash_defined
5626 || child->root.type == bfd_link_hash_defweak)
5627 && child->root.u.def.section == sec
5628 && child->root.u.def.value == offset)
5629 goto win;
5630 }
5631
5632 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
8f615d07 5633 bfd_archive_filename (abfd), sec->name,
a7b97311 5634 (unsigned long) offset);
252b5132 5635 bfd_set_error (bfd_error_invalid_operation);
b34976b6 5636 return FALSE;
252b5132 5637
dc810e39 5638 win:
252b5132
RH
5639 if (!h)
5640 {
5641 /* This *should* only be the absolute section. It could potentially
5642 be that someone has defined a non-global vtable though, which
5643 would be bad. It isn't worth paging in the local symbols to be
5644 sure though; that case should simply be handled by the assembler. */
5645
5646 child->vtable_parent = (struct elf_link_hash_entry *) -1;
5647 }
5648 else
5649 child->vtable_parent = h;
5650
b34976b6 5651 return TRUE;
252b5132
RH
5652}
5653
4cc11e76 5654/* Called from check_relocs to record the existence of a VTENTRY reloc. */
252b5132 5655
b34976b6 5656bfd_boolean
268b6b39
AM
5657elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
5658 asection *sec ATTRIBUTE_UNUSED,
5659 struct elf_link_hash_entry *h,
5660 bfd_vma addend)
252b5132 5661{
9c5bfbb7 5662 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
45d6a902 5663 unsigned int log_file_align = bed->s->log_file_align;
0d1ea5c0 5664
64c456e1 5665 if (addend >= h->vtable_entries_size)
252b5132 5666 {
64c456e1 5667 size_t size, bytes, file_align;
b34976b6 5668 bfd_boolean *ptr = h->vtable_entries_used;
252b5132
RH
5669
5670 /* While the symbol is undefined, we have to be prepared to handle
5671 a zero size. */
64c456e1 5672 file_align = 1 << log_file_align;
252b5132 5673 if (h->root.type == bfd_link_hash_undefined)
64c456e1 5674 size = addend + file_align;
252b5132
RH
5675 else
5676 {
5677 size = h->size;
64c456e1 5678 if (addend >= size)
252b5132
RH
5679 {
5680 /* Oops! We've got a reference past the defined end of
5681 the table. This is probably a bug -- shall we warn? */
64c456e1 5682 size = addend + file_align;
252b5132
RH
5683 }
5684 }
64c456e1 5685 size = (size + file_align - 1) & -file_align;
252b5132
RH
5686
5687 /* Allocate one extra entry for use as a "done" flag for the
64c456e1
AM
5688 consolidation pass. */
5689 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
252b5132
RH
5690
5691 if (ptr)
5692 {
268b6b39 5693 ptr = bfd_realloc (ptr - 1, bytes);
3e932841 5694
fed79cc6
NC
5695 if (ptr != NULL)
5696 {
5697 size_t oldbytes;
252b5132 5698
45d6a902 5699 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
b34976b6 5700 * sizeof (bfd_boolean));
a7b97311 5701 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
fed79cc6 5702 }
252b5132
RH
5703 }
5704 else
268b6b39 5705 ptr = bfd_zmalloc (bytes);
252b5132 5706
fed79cc6 5707 if (ptr == NULL)
b34976b6 5708 return FALSE;
3e932841 5709
252b5132 5710 /* And arrange for that done flag to be at index -1. */
fed79cc6 5711 h->vtable_entries_used = ptr + 1;
252b5132
RH
5712 h->vtable_entries_size = size;
5713 }
3e932841 5714
45d6a902 5715 h->vtable_entries_used[addend >> log_file_align] = TRUE;
252b5132 5716
b34976b6 5717 return TRUE;
252b5132
RH
5718}
5719
5720/* And an accompanying bit to work out final got entry offsets once
5721 we're done. Should be called from final_link. */
5722
b34976b6 5723bfd_boolean
268b6b39
AM
5724elf_gc_common_finalize_got_offsets (bfd *abfd,
5725 struct bfd_link_info *info)
252b5132
RH
5726{
5727 bfd *i;
9c5bfbb7 5728 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
5729 bfd_vma gotoff;
5730
0eddce27
AM
5731 if (! is_elf_hash_table (info->hash))
5732 return FALSE;
5733
252b5132
RH
5734 /* The GOT offset is relative to the .got section, but the GOT header is
5735 put into the .got.plt section, if the backend uses it. */
5736 if (bed->want_got_plt)
5737 gotoff = 0;
5738 else
5739 gotoff = bed->got_header_size;
5740
5741 /* Do the local .got entries first. */
5742 for (i = info->input_bfds; i; i = i->link_next)
5743 {
f6af82bd 5744 bfd_signed_vma *local_got;
252b5132
RH
5745 bfd_size_type j, locsymcount;
5746 Elf_Internal_Shdr *symtab_hdr;
5747
f6af82bd
AM
5748 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
5749 continue;
5750
5751 local_got = elf_local_got_refcounts (i);
252b5132
RH
5752 if (!local_got)
5753 continue;
5754
5755 symtab_hdr = &elf_tdata (i)->symtab_hdr;
5756 if (elf_bad_symtab (i))
5757 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
5758 else
5759 locsymcount = symtab_hdr->sh_info;
5760
5761 for (j = 0; j < locsymcount; ++j)
5762 {
5763 if (local_got[j] > 0)
5764 {
5765 local_got[j] = gotoff;
5766 gotoff += ARCH_SIZE / 8;
5767 }
5768 else
5769 local_got[j] = (bfd_vma) -1;
5770 }
5771 }
5772
dd5724d5
AM
5773 /* Then the global .got entries. .plt refcounts are handled by
5774 adjust_dynamic_symbol */
252b5132
RH
5775 elf_link_hash_traverse (elf_hash_table (info),
5776 elf_gc_allocate_got_offsets,
268b6b39 5777 &gotoff);
b34976b6 5778 return TRUE;
252b5132
RH
5779}
5780
5781/* We need a special top-level link routine to convert got reference counts
5782 to real got offsets. */
5783
b34976b6 5784static bfd_boolean
268b6b39 5785elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *offarg)
252b5132 5786{
268b6b39 5787 bfd_vma *off = offarg;
252b5132 5788
e92d460e
AM
5789 if (h->root.type == bfd_link_hash_warning)
5790 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5791
252b5132
RH
5792 if (h->got.refcount > 0)
5793 {
5794 h->got.offset = off[0];
5795 off[0] += ARCH_SIZE / 8;
5796 }
5797 else
5798 h->got.offset = (bfd_vma) -1;
5799
b34976b6 5800 return TRUE;
252b5132
RH
5801}
5802
5803/* Many folk need no more in the way of final link than this, once
5804 got entry reference counting is enabled. */
5805
b34976b6 5806bfd_boolean
268b6b39 5807elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
5808{
5809 if (!elf_gc_common_finalize_got_offsets (abfd, info))
b34976b6 5810 return FALSE;
252b5132
RH
5811
5812 /* Invoke the regular ELF backend linker to do all the work. */
5813 return elf_bfd_final_link (abfd, info);
5814}
5815
5816/* This function will be called though elf_link_hash_traverse to store
5817 all hash value of the exported symbols in an array. */
5818
b34976b6 5819static bfd_boolean
268b6b39 5820elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
252b5132 5821{
268b6b39 5822 unsigned long **valuep = data;
252b5132
RH
5823 const char *name;
5824 char *p;
5825 unsigned long ha;
5826 char *alc = NULL;
5827
e92d460e
AM
5828 if (h->root.type == bfd_link_hash_warning)
5829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5830
252b5132
RH
5831 /* Ignore indirect symbols. These are added by the versioning code. */
5832 if (h->dynindx == -1)
b34976b6 5833 return TRUE;
252b5132
RH
5834
5835 name = h->root.root.string;
5836 p = strchr (name, ELF_VER_CHR);
5837 if (p != NULL)
5838 {
268b6b39
AM
5839 alc = bfd_malloc (p - name + 1);
5840 memcpy (alc, name, p - name);
252b5132
RH
5841 alc[p - name] = '\0';
5842 name = alc;
5843 }
5844
5845 /* Compute the hash value. */
5846 ha = bfd_elf_hash (name);
5847
5848 /* Store the found hash value in the array given as the argument. */
5849 *(*valuep)++ = ha;
5850
5851 /* And store it in the struct so that we can put it in the hash table
5852 later. */
5853 h->elf_hash_value = ha;
5854
5855 if (alc != NULL)
5856 free (alc);
5857
b34976b6 5858 return TRUE;
252b5132 5859}
73d074b4 5860
b34976b6 5861bfd_boolean
268b6b39 5862elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
73d074b4 5863{
268b6b39 5864 struct elf_reloc_cookie *rcookie = cookie;
73d074b4
DJ
5865
5866 if (rcookie->bad_symtab)
5867 rcookie->rel = rcookie->rels;
5868
5869 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
5870 {
d6fe2dc1 5871 unsigned long r_symndx;
73d074b4
DJ
5872
5873 if (! rcookie->bad_symtab)
5874 if (rcookie->rel->r_offset > offset)
b34976b6 5875 return FALSE;
73d074b4
DJ
5876 if (rcookie->rel->r_offset != offset)
5877 continue;
5878
d6fe2dc1
AM
5879 r_symndx = ELF_R_SYM (rcookie->rel->r_info);
5880 if (r_symndx == SHN_UNDEF)
b34976b6 5881 return TRUE;
d6fe2dc1 5882
73d074b4 5883 if (r_symndx >= rcookie->locsymcount
6cdc0ccc 5884 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
73d074b4
DJ
5885 {
5886 struct elf_link_hash_entry *h;
5887
5888 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
5889
5890 while (h->root.type == bfd_link_hash_indirect
5891 || h->root.type == bfd_link_hash_warning)
5892 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5893
5894 if ((h->root.type == bfd_link_hash_defined
5895 || h->root.type == bfd_link_hash_defweak)
ed4de5e2 5896 && elf_discarded_section (h->root.u.def.section))
b34976b6 5897 return TRUE;
73d074b4 5898 else
b34976b6 5899 return FALSE;
73d074b4 5900 }
6cdc0ccc 5901 else
73d074b4
DJ
5902 {
5903 /* It's not a relocation against a global symbol,
44421011 5904 but it could be a relocation against a local
73d074b4
DJ
5905 symbol for a discarded section. */
5906 asection *isec;
6cdc0ccc 5907 Elf_Internal_Sym *isym;
73d074b4
DJ
5908
5909 /* Need to: get the symbol; get the section. */
6cdc0ccc
AM
5910 isym = &rcookie->locsyms[r_symndx];
5911 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
73d074b4 5912 {
6cdc0ccc 5913 isec = section_from_elf_index (rcookie->abfd, isym->st_shndx);
ed4de5e2 5914 if (isec != NULL && elf_discarded_section (isec))
b34976b6 5915 return TRUE;
73d074b4
DJ
5916 }
5917 }
b34976b6 5918 return FALSE;
73d074b4 5919 }
b34976b6 5920 return FALSE;
73d074b4
DJ
5921}
5922
5923/* Discard unneeded references to discarded sections.
b34976b6 5924 Returns TRUE if any section's size was changed. */
73d074b4 5925/* This function assumes that the relocations are in sorted order,
ab3acfbe 5926 which is true for all known assemblers. */
73d074b4 5927
b34976b6 5928bfd_boolean
268b6b39 5929elf_bfd_discard_info (bfd *output_bfd, struct bfd_link_info *info)
73d074b4
DJ
5930{
5931 struct elf_reloc_cookie cookie;
126495ed 5932 asection *stab, *eh;
73d074b4 5933 Elf_Internal_Shdr *symtab_hdr;
9c5bfbb7 5934 const struct elf_backend_data *bed;
73d074b4 5935 bfd *abfd;
99eb2ac8 5936 unsigned int count;
b34976b6 5937 bfd_boolean ret = FALSE;
73d074b4 5938
d6fe2dc1 5939 if (info->traditional_format
0eddce27 5940 || !is_elf_hash_table (info->hash))
b34976b6 5941 return FALSE;
65765700 5942
73d074b4
DJ
5943 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
5944 {
163c1c30
L
5945 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
5946 continue;
5947
73d074b4
DJ
5948 bed = get_elf_backend_data (abfd);
5949
5950 if ((abfd->flags & DYNAMIC) != 0)
5951 continue;
5952
126495ed 5953 eh = bfd_get_section_by_name (abfd, ".eh_frame");
1049f94e 5954 if (info->relocatable
2d36fe5f
AH
5955 || (eh != NULL
5956 && (eh->_raw_size == 0
5957 || bfd_is_abs_section (eh->output_section))))
126495ed 5958 eh = NULL;
65765700 5959
99eb2ac8
AM
5960 stab = bfd_get_section_by_name (abfd, ".stab");
5961 if (stab != NULL
5962 && (stab->_raw_size == 0
5963 || bfd_is_abs_section (stab->output_section)
68bfbfcc 5964 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
99eb2ac8
AM
5965 stab = NULL;
5966
5967 if (stab == NULL
5968 && eh == NULL
5969 && bed->elf_backend_discard_info == NULL)
73d074b4
DJ
5970 continue;
5971
5972 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
73d074b4
DJ
5973 cookie.abfd = abfd;
5974 cookie.sym_hashes = elf_sym_hashes (abfd);
5975 cookie.bad_symtab = elf_bad_symtab (abfd);
5976 if (cookie.bad_symtab)
5977 {
99eb2ac8 5978 cookie.locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
73d074b4
DJ
5979 cookie.extsymoff = 0;
5980 }
5981 else
5982 {
5983 cookie.locsymcount = symtab_hdr->sh_info;
5984 cookie.extsymoff = symtab_hdr->sh_info;
5985 }
5986
6cdc0ccc
AM
5987 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
5988 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
c44233aa 5989 {
6cdc0ccc
AM
5990 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
5991 cookie.locsymcount, 0,
5992 NULL, NULL, NULL);
c44233aa 5993 if (cookie.locsyms == NULL)
b34976b6 5994 return FALSE;
9ad5cbcf 5995 }
73d074b4 5996
99eb2ac8 5997 if (stab != NULL)
73d074b4 5998 {
99eb2ac8
AM
5999 cookie.rels = NULL;
6000 count = stab->reloc_count;
6001 if (count != 0)
268b6b39
AM
6002 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
6003 info->keep_memory);
99eb2ac8 6004 if (cookie.rels != NULL)
73d074b4
DJ
6005 {
6006 cookie.rel = cookie.rels;
99eb2ac8
AM
6007 cookie.relend = cookie.rels;
6008 cookie.relend += count * bed->s->int_rels_per_ext_rel;
65765700
JJ
6009 if (_bfd_discard_section_stabs (abfd, stab,
6010 elf_section_data (stab)->sec_info,
73d074b4
DJ
6011 elf_reloc_symbol_deleted_p,
6012 &cookie))
b34976b6 6013 ret = TRUE;
6cdc0ccc 6014 if (elf_section_data (stab)->relocs != cookie.rels)
73d074b4
DJ
6015 free (cookie.rels);
6016 }
6017 }
6018
99eb2ac8 6019 if (eh != NULL)
65765700
JJ
6020 {
6021 cookie.rels = NULL;
99eb2ac8
AM
6022 count = eh->reloc_count;
6023 if (count != 0)
268b6b39
AM
6024 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
6025 info->keep_memory);
99eb2ac8
AM
6026 cookie.rel = cookie.rels;
6027 cookie.relend = cookie.rels;
6028 if (cookie.rels != NULL)
6029 cookie.relend += count * bed->s->int_rels_per_ext_rel;
6030
126495ed 6031 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
65765700
JJ
6032 elf_reloc_symbol_deleted_p,
6033 &cookie))
b34976b6 6034 ret = TRUE;
99eb2ac8
AM
6035
6036 if (cookie.rels != NULL
6037 && elf_section_data (eh)->relocs != cookie.rels)
65765700
JJ
6038 free (cookie.rels);
6039 }
6040
99eb2ac8
AM
6041 if (bed->elf_backend_discard_info != NULL
6042 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
b34976b6 6043 ret = TRUE;
73d074b4 6044
6cdc0ccc
AM
6045 if (cookie.locsyms != NULL
6046 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
6047 {
6048 if (! info->keep_memory)
6049 free (cookie.locsyms);
6050 else
6051 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
6052 }
73d074b4 6053 }
65765700 6054
126495ed 6055 if (info->eh_frame_hdr
1049f94e 6056 && !info->relocatable
126495ed 6057 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
b34976b6 6058 ret = TRUE;
99eb2ac8 6059
73d074b4
DJ
6060 return ret;
6061}
6062
b34976b6 6063static bfd_boolean
268b6b39 6064elf_section_ignore_discarded_relocs (asection *sec)
73d074b4 6065{
9c5bfbb7 6066 const struct elf_backend_data *bed;
40b829d4 6067
68bfbfcc 6068 switch (sec->sec_info_type)
65765700
JJ
6069 {
6070 case ELF_INFO_TYPE_STABS:
6071 case ELF_INFO_TYPE_EH_FRAME:
b34976b6 6072 return TRUE;
65765700
JJ
6073 default:
6074 break;
6075 }
40b829d4
AM
6076
6077 bed = get_elf_backend_data (sec->owner);
6078 if (bed->elf_backend_ignore_discarded_relocs != NULL
6079 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
b34976b6 6080 return TRUE;
65765700 6081
b34976b6 6082 return FALSE;
73d074b4 6083}
This page took 0.742751 seconds and 4 git commands to generate.