]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.h
* Makefile.tpl: Clean $(BUILD_SUBDIR).
[thirdparty/binutils-gdb.git] / bfd / elflink.h
CommitLineData
252b5132 1/* ELF linker support.
68bfbfcc 2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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
23/* This struct is used to pass information to routines called via
24 elf_link_hash_traverse which must return failure. */
25
26struct elf_info_failed
27{
b34976b6 28 bfd_boolean failed;
252b5132 29 struct bfd_link_info *info;
bc2b6df7 30 struct bfd_elf_version_tree *verdefs;
252b5132
RH
31};
32
b34976b6 33static bfd_boolean is_global_data_symbol_definition
a7b97311 34 PARAMS ((bfd *, Elf_Internal_Sym *));
b34976b6 35static bfd_boolean elf_link_is_defined_archive_symbol
a7b97311 36 PARAMS ((bfd *, carsym *));
b34976b6 37static bfd_boolean elf_link_add_object_symbols
252b5132 38 PARAMS ((bfd *, struct bfd_link_info *));
b34976b6 39static bfd_boolean elf_link_add_archive_symbols
252b5132 40 PARAMS ((bfd *, struct bfd_link_info *));
b34976b6 41static bfd_boolean elf_merge_symbol
215007a6
L
42 PARAMS ((bfd *, struct bfd_link_info *, const char *,
43 Elf_Internal_Sym *, asection **, bfd_vma *,
b34976b6
AM
44 struct elf_link_hash_entry **, bfd_boolean *, bfd_boolean *,
45 bfd_boolean *, bfd_boolean));
46static bfd_boolean elf_add_default_symbol
215007a6
L
47 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
48 const char *, Elf_Internal_Sym *, asection **, bfd_vma *,
b34976b6
AM
49 bfd_boolean *, bfd_boolean, bfd_boolean));
50static bfd_boolean elf_export_symbol
252b5132 51 PARAMS ((struct elf_link_hash_entry *, PTR));
b34976b6 52static bfd_boolean elf_finalize_dynstr
2b0f7ef9 53 PARAMS ((bfd *, struct bfd_link_info *));
b34976b6 54static bfd_boolean elf_fix_symbol_flags
252b5132 55 PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
b34976b6 56static bfd_boolean elf_adjust_dynamic_symbol
252b5132 57 PARAMS ((struct elf_link_hash_entry *, PTR));
b34976b6 58static bfd_boolean elf_link_find_version_dependencies
252b5132 59 PARAMS ((struct elf_link_hash_entry *, PTR));
b34976b6 60static bfd_boolean elf_link_assign_sym_version
252b5132 61 PARAMS ((struct elf_link_hash_entry *, PTR));
b34976b6 62static bfd_boolean elf_collect_hash_codes
252b5132 63 PARAMS ((struct elf_link_hash_entry *, PTR));
b34976b6 64static bfd_boolean elf_link_read_relocs_from_section
6b5bd373 65 PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
a7b97311
AM
66static size_t compute_bucket_count
67 PARAMS ((struct bfd_link_info *));
b34976b6 68static bfd_boolean elf_link_output_relocs
23bc299b 69 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
b34976b6 70static bfd_boolean elf_link_size_reloc_section
23bc299b 71 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
3e932841
KH
72static void elf_link_adjust_relocs
73 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
31367b81 74 struct elf_link_hash_entry **));
db6751f2
JJ
75static int elf_link_sort_cmp1
76 PARAMS ((const void *, const void *));
77static int elf_link_sort_cmp2
78 PARAMS ((const void *, const void *));
79static size_t elf_link_sort_relocs
80 PARAMS ((bfd *, struct bfd_link_info *, asection **));
b34976b6 81static bfd_boolean elf_section_ignore_discarded_relocs
73d074b4 82 PARAMS ((asection *));
252b5132
RH
83
84/* Given an ELF BFD, add symbols to the global hash table as
85 appropriate. */
86
b34976b6 87bfd_boolean
252b5132
RH
88elf_bfd_link_add_symbols (abfd, info)
89 bfd *abfd;
90 struct bfd_link_info *info;
91{
92 switch (bfd_get_format (abfd))
93 {
94 case bfd_object:
95 return elf_link_add_object_symbols (abfd, info);
96 case bfd_archive:
97 return elf_link_add_archive_symbols (abfd, info);
98 default:
99 bfd_set_error (bfd_error_wrong_format);
b34976b6 100 return FALSE;
252b5132
RH
101 }
102}
103\f
b34976b6
AM
104/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
105static bfd_boolean
7da9d88f 106is_global_data_symbol_definition (abfd, sym)
86033394 107 bfd * abfd ATTRIBUTE_UNUSED;
48dfb430
NC
108 Elf_Internal_Sym * sym;
109{
110 /* Local symbols do not count, but target specific ones might. */
111 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
112 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
b34976b6 113 return FALSE;
48dfb430 114
7da9d88f
NC
115 /* Function symbols do not count. */
116 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
b34976b6 117 return FALSE;
7da9d88f 118
48dfb430
NC
119 /* If the section is undefined, then so is the symbol. */
120 if (sym->st_shndx == SHN_UNDEF)
b34976b6 121 return FALSE;
3e932841 122
48dfb430
NC
123 /* If the symbol is defined in the common section, then
124 it is a common definition and so does not count. */
125 if (sym->st_shndx == SHN_COMMON)
b34976b6 126 return FALSE;
48dfb430
NC
127
128 /* If the symbol is in a target specific section then we
129 must rely upon the backend to tell us what it is. */
130 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
131 /* FIXME - this function is not coded yet:
3e932841 132
48dfb430 133 return _bfd_is_global_symbol_definition (abfd, sym);
3e932841 134
48dfb430
NC
135 Instead for now assume that the definition is not global,
136 Even if this is wrong, at least the linker will behave
137 in the same way that it used to do. */
b34976b6 138 return FALSE;
3e932841 139
b34976b6 140 return TRUE;
48dfb430
NC
141}
142
a3a8c91d 143/* Search the symbol table of the archive element of the archive ABFD
4e8a9624 144 whose archive map contains a mention of SYMDEF, and determine if
a3a8c91d 145 the symbol is defined in this element. */
b34976b6 146static bfd_boolean
a3a8c91d
NC
147elf_link_is_defined_archive_symbol (abfd, symdef)
148 bfd * abfd;
149 carsym * symdef;
150{
151 Elf_Internal_Shdr * hdr;
dc810e39
AM
152 bfd_size_type symcount;
153 bfd_size_type extsymcount;
154 bfd_size_type extsymoff;
6cdc0ccc
AM
155 Elf_Internal_Sym *isymbuf;
156 Elf_Internal_Sym *isym;
157 Elf_Internal_Sym *isymend;
b34976b6 158 bfd_boolean result;
3e932841 159
a3a8c91d
NC
160 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
161 if (abfd == (bfd *) NULL)
b34976b6 162 return FALSE;
a3a8c91d
NC
163
164 if (! bfd_check_format (abfd, bfd_object))
b34976b6 165 return FALSE;
a3a8c91d 166
48dfb430
NC
167 /* If we have already included the element containing this symbol in the
168 link then we do not need to include it again. Just claim that any symbol
169 it contains is not a definition, so that our caller will not decide to
170 (re)include this element. */
171 if (abfd->archive_pass)
b34976b6 172 return FALSE;
3e932841 173
a3a8c91d
NC
174 /* Select the appropriate symbol table. */
175 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
6cdc0ccc 176 hdr = &elf_tdata (abfd)->symtab_hdr;
a3a8c91d 177 else
6cdc0ccc 178 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
a3a8c91d
NC
179
180 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
181
182 /* The sh_info field of the symtab header tells us where the
183 external symbols start. We don't care about the local symbols. */
184 if (elf_bad_symtab (abfd))
185 {
186 extsymcount = symcount;
187 extsymoff = 0;
188 }
189 else
190 {
191 extsymcount = symcount - hdr->sh_info;
192 extsymoff = hdr->sh_info;
193 }
194
6cdc0ccc 195 if (extsymcount == 0)
b34976b6 196 return FALSE;
a3a8c91d 197
6cdc0ccc
AM
198 /* Read in the symbol table. */
199 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
200 NULL, NULL, NULL);
201 if (isymbuf == NULL)
b34976b6 202 return FALSE;
a3a8c91d
NC
203
204 /* Scan the symbol table looking for SYMDEF. */
b34976b6 205 result = FALSE;
6cdc0ccc 206 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
a3a8c91d 207 {
6cdc0ccc 208 const char *name;
a3a8c91d 209
6cdc0ccc
AM
210 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
211 isym->st_name);
a3a8c91d
NC
212 if (name == (const char *) NULL)
213 break;
214
215 if (strcmp (name, symdef->name) == 0)
216 {
6cdc0ccc 217 result = is_global_data_symbol_definition (abfd, isym);
a3a8c91d
NC
218 break;
219 }
220 }
221
6cdc0ccc 222 free (isymbuf);
3e932841 223
a3a8c91d
NC
224 return result;
225}
226\f
252b5132
RH
227/* Add symbols from an ELF archive file to the linker hash table. We
228 don't use _bfd_generic_link_add_archive_symbols because of a
229 problem which arises on UnixWare. The UnixWare libc.so is an
230 archive which includes an entry libc.so.1 which defines a bunch of
231 symbols. The libc.so archive also includes a number of other
232 object files, which also define symbols, some of which are the same
233 as those defined in libc.so.1. Correct linking requires that we
234 consider each object file in turn, and include it if it defines any
235 symbols we need. _bfd_generic_link_add_archive_symbols does not do
236 this; it looks through the list of undefined symbols, and includes
237 any object file which defines them. When this algorithm is used on
238 UnixWare, it winds up pulling in libc.so.1 early and defining a
239 bunch of symbols. This means that some of the other objects in the
240 archive are not included in the link, which is incorrect since they
241 precede libc.so.1 in the archive.
242
243 Fortunately, ELF archive handling is simpler than that done by
244 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
245 oddities. In ELF, if we find a symbol in the archive map, and the
246 symbol is currently undefined, we know that we must pull in that
247 object file.
248
249 Unfortunately, we do have to make multiple passes over the symbol
250 table until nothing further is resolved. */
251
b34976b6 252static bfd_boolean
252b5132
RH
253elf_link_add_archive_symbols (abfd, info)
254 bfd *abfd;
255 struct bfd_link_info *info;
256{
257 symindex c;
b34976b6
AM
258 bfd_boolean *defined = NULL;
259 bfd_boolean *included = NULL;
252b5132 260 carsym *symdefs;
b34976b6 261 bfd_boolean loop;
dc810e39 262 bfd_size_type amt;
252b5132
RH
263
264 if (! bfd_has_map (abfd))
265 {
266 /* An empty archive is a special case. */
267 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
b34976b6 268 return TRUE;
252b5132 269 bfd_set_error (bfd_error_no_armap);
b34976b6 270 return FALSE;
252b5132
RH
271 }
272
273 /* Keep track of all symbols we know to be already defined, and all
274 files we know to be already included. This is to speed up the
275 second and subsequent passes. */
276 c = bfd_ardata (abfd)->symdef_count;
277 if (c == 0)
b34976b6 278 return TRUE;
dc810e39 279 amt = c;
b34976b6
AM
280 amt *= sizeof (bfd_boolean);
281 defined = (bfd_boolean *) bfd_zmalloc (amt);
282 included = (bfd_boolean *) bfd_zmalloc (amt);
283 if (defined == (bfd_boolean *) NULL || included == (bfd_boolean *) NULL)
252b5132 284 goto error_return;
252b5132
RH
285
286 symdefs = bfd_ardata (abfd)->symdefs;
287
288 do
289 {
290 file_ptr last;
291 symindex i;
292 carsym *symdef;
293 carsym *symdefend;
294
b34976b6 295 loop = FALSE;
252b5132
RH
296 last = -1;
297
298 symdef = symdefs;
299 symdefend = symdef + c;
300 for (i = 0; symdef < symdefend; symdef++, i++)
301 {
302 struct elf_link_hash_entry *h;
303 bfd *element;
304 struct bfd_link_hash_entry *undefs_tail;
305 symindex mark;
306
307 if (defined[i] || included[i])
308 continue;
309 if (symdef->file_offset == last)
310 {
b34976b6 311 included[i] = TRUE;
252b5132
RH
312 continue;
313 }
314
315 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
b34976b6 316 FALSE, FALSE, FALSE);
252b5132
RH
317
318 if (h == NULL)
319 {
320 char *p, *copy;
48fc70a2 321 size_t len, first;
252b5132
RH
322
323 /* If this is a default version (the name contains @@),
48fc70a2
AM
324 look up the symbol again with only one `@' as well
325 as without the version. The effect is that references
326 to the symbol with and without the version will be
327 matched by the default symbol in the archive. */
252b5132
RH
328
329 p = strchr (symdef->name, ELF_VER_CHR);
330 if (p == NULL || p[1] != ELF_VER_CHR)
331 continue;
332
48fc70a2
AM
333 /* First check with only one `@'. */
334 len = strlen (symdef->name);
335 copy = bfd_alloc (abfd, (bfd_size_type) len);
252b5132
RH
336 if (copy == NULL)
337 goto error_return;
48fc70a2
AM
338 first = p - symdef->name + 1;
339 memcpy (copy, symdef->name, first);
340 memcpy (copy + first, symdef->name + first + 1, len - first);
252b5132
RH
341
342 h = elf_link_hash_lookup (elf_hash_table (info), copy,
b34976b6 343 FALSE, FALSE, FALSE);
252b5132 344
48fc70a2 345 if (h == NULL)
58821868 346 {
48fc70a2
AM
347 /* We also need to check references to the symbol
348 without the version. */
349
350 copy[first - 1] = '\0';
351 h = elf_link_hash_lookup (elf_hash_table (info),
b34976b6 352 copy, FALSE, FALSE, FALSE);
48fc70a2
AM
353 }
354
252b5132
RH
355 bfd_release (abfd, copy);
356 }
357
358 if (h == NULL)
359 continue;
360
a3a8c91d
NC
361 if (h->root.type == bfd_link_hash_common)
362 {
363 /* We currently have a common symbol. The archive map contains
364 a reference to this symbol, so we may want to include it. We
365 only want to include it however, if this archive element
366 contains a definition of the symbol, not just another common
367 declaration of it.
368
369 Unfortunately some archivers (including GNU ar) will put
370 declarations of common symbols into their archive maps, as
371 well as real definitions, so we cannot just go by the archive
372 map alone. Instead we must read in the element's symbol
373 table and check that to see what kind of symbol definition
374 this is. */
375 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
376 continue;
377 }
378 else if (h->root.type != bfd_link_hash_undefined)
252b5132
RH
379 {
380 if (h->root.type != bfd_link_hash_undefweak)
b34976b6 381 defined[i] = TRUE;
252b5132
RH
382 continue;
383 }
384
385 /* We need to include this archive member. */
252b5132
RH
386 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
387 if (element == (bfd *) NULL)
388 goto error_return;
389
390 if (! bfd_check_format (element, bfd_object))
391 goto error_return;
392
393 /* Doublecheck that we have not included this object
394 already--it should be impossible, but there may be
395 something wrong with the archive. */
396 if (element->archive_pass != 0)
397 {
398 bfd_set_error (bfd_error_bad_value);
399 goto error_return;
400 }
401 element->archive_pass = 1;
402
403 undefs_tail = info->hash->undefs_tail;
404
405 if (! (*info->callbacks->add_archive_element) (info, element,
406 symdef->name))
407 goto error_return;
408 if (! elf_link_add_object_symbols (element, info))
409 goto error_return;
410
411 /* If there are any new undefined symbols, we need to make
412 another pass through the archive in order to see whether
413 they can be defined. FIXME: This isn't perfect, because
414 common symbols wind up on undefs_tail and because an
415 undefined symbol which is defined later on in this pass
416 does not require another pass. This isn't a bug, but it
417 does make the code less efficient than it could be. */
418 if (undefs_tail != info->hash->undefs_tail)
b34976b6 419 loop = TRUE;
252b5132
RH
420
421 /* Look backward to mark all symbols from this object file
422 which we have already seen in this pass. */
423 mark = i;
424 do
425 {
b34976b6 426 included[mark] = TRUE;
252b5132
RH
427 if (mark == 0)
428 break;
429 --mark;
430 }
431 while (symdefs[mark].file_offset == symdef->file_offset);
432
433 /* We mark subsequent symbols from this object file as we go
434 on through the loop. */
435 last = symdef->file_offset;
436 }
437 }
438 while (loop);
439
440 free (defined);
441 free (included);
442
b34976b6 443 return TRUE;
252b5132
RH
444
445 error_return:
b34976b6 446 if (defined != (bfd_boolean *) NULL)
252b5132 447 free (defined);
b34976b6 448 if (included != (bfd_boolean *) NULL)
252b5132 449 free (included);
b34976b6 450 return FALSE;
252b5132
RH
451}
452
453/* This function is called when we want to define a new symbol. It
454 handles the various cases which arise when we find a definition in
455 a dynamic object, or when there is already a definition in a
456 dynamic object. The new symbol is described by NAME, SYM, PSEC,
457 and PVALUE. We set SYM_HASH to the hash table entry. We set
458 OVERRIDE if the old symbol is overriding a new definition. We set
459 TYPE_CHANGE_OK if it is OK for the type to change. We set
460 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
461 change, we mean that we shouldn't warn if the type or size does
456981d7
L
462 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
463 a shared object. */
252b5132 464
b34976b6 465static bfd_boolean
252b5132 466elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
456981d7 467 override, type_change_ok, size_change_ok, dt_needed)
252b5132
RH
468 bfd *abfd;
469 struct bfd_link_info *info;
470 const char *name;
471 Elf_Internal_Sym *sym;
472 asection **psec;
473 bfd_vma *pvalue;
474 struct elf_link_hash_entry **sym_hash;
b34976b6
AM
475 bfd_boolean *override;
476 bfd_boolean *type_change_ok;
477 bfd_boolean *size_change_ok;
478 bfd_boolean dt_needed;
252b5132
RH
479{
480 asection *sec;
481 struct elf_link_hash_entry *h;
3c0a515d 482 struct elf_link_hash_entry *flip;
252b5132
RH
483 int bind;
484 bfd *oldbfd;
b34976b6 485 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
252b5132 486
b34976b6 487 *override = FALSE;
252b5132
RH
488
489 sec = *psec;
490 bind = ELF_ST_BIND (sym->st_info);
491
492 if (! bfd_is_und_section (sec))
b34976b6 493 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
252b5132
RH
494 else
495 h = ((struct elf_link_hash_entry *)
b34976b6 496 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
252b5132 497 if (h == NULL)
b34976b6 498 return FALSE;
252b5132
RH
499 *sym_hash = h;
500
501 /* This code is for coping with dynamic objects, and is only useful
502 if we are doing an ELF link. */
503 if (info->hash->creator != abfd->xvec)
b34976b6 504 return TRUE;
252b5132
RH
505
506 /* For merging, we only care about real symbols. */
507
508 while (h->root.type == bfd_link_hash_indirect
509 || h->root.type == bfd_link_hash_warning)
510 h = (struct elf_link_hash_entry *) h->root.u.i.link;
511
512 /* If we just created the symbol, mark it as being an ELF symbol.
513 Other than that, there is nothing to do--there is no merge issue
514 with a newly defined symbol--so we just return. */
515
516 if (h->root.type == bfd_link_hash_new)
517 {
518 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
b34976b6 519 return TRUE;
252b5132
RH
520 }
521
522 /* OLDBFD is a BFD associated with the existing symbol. */
523
524 switch (h->root.type)
525 {
526 default:
527 oldbfd = NULL;
528 break;
529
530 case bfd_link_hash_undefined:
531 case bfd_link_hash_undefweak:
532 oldbfd = h->root.u.undef.abfd;
533 break;
534
535 case bfd_link_hash_defined:
536 case bfd_link_hash_defweak:
537 oldbfd = h->root.u.def.section->owner;
538 break;
539
540 case bfd_link_hash_common:
541 oldbfd = h->root.u.c.p->section->owner;
542 break;
543 }
544
b4536acd
ILT
545 /* In cases involving weak versioned symbols, we may wind up trying
546 to merge a symbol with itself. Catch that here, to avoid the
547 confusion that results if we try to override a symbol with
accc7f69
ILT
548 itself. The additional tests catch cases like
549 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
550 dynamic object, which we do want to handle here. */
551 if (abfd == oldbfd
552 && ((abfd->flags & DYNAMIC) == 0
553 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
b34976b6 554 return TRUE;
b4536acd 555
252b5132
RH
556 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
557 respectively, is from a dynamic object. */
558
559 if ((abfd->flags & DYNAMIC) != 0)
b34976b6 560 newdyn = TRUE;
252b5132 561 else
b34976b6 562 newdyn = FALSE;
252b5132 563
0035bd7b
ILT
564 if (oldbfd != NULL)
565 olddyn = (oldbfd->flags & DYNAMIC) != 0;
252b5132 566 else
0035bd7b
ILT
567 {
568 asection *hsec;
569
570 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
c44233aa 571 indices used by MIPS ELF. */
0035bd7b
ILT
572 switch (h->root.type)
573 {
574 default:
575 hsec = NULL;
576 break;
577
578 case bfd_link_hash_defined:
579 case bfd_link_hash_defweak:
580 hsec = h->root.u.def.section;
581 break;
582
583 case bfd_link_hash_common:
584 hsec = h->root.u.c.p->section;
585 break;
586 }
587
588 if (hsec == NULL)
b34976b6 589 olddyn = FALSE;
0035bd7b
ILT
590 else
591 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
592 }
252b5132
RH
593
594 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
595 respectively, appear to be a definition rather than reference. */
596
597 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
b34976b6 598 newdef = FALSE;
252b5132 599 else
b34976b6 600 newdef = TRUE;
252b5132
RH
601
602 if (h->root.type == bfd_link_hash_undefined
603 || h->root.type == bfd_link_hash_undefweak
604 || h->root.type == bfd_link_hash_common)
b34976b6 605 olddef = FALSE;
252b5132 606 else
b34976b6 607 olddef = TRUE;
252b5132
RH
608
609 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
610 symbol, respectively, appears to be a common symbol in a dynamic
611 object. If a symbol appears in an uninitialized section, and is
612 not weak, and is not a function, then it may be a common symbol
613 which was resolved when the dynamic object was created. We want
614 to treat such symbols specially, because they raise special
615 considerations when setting the symbol size: if the symbol
616 appears as a common symbol in a regular object, and the size in
617 the regular object is larger, we must make sure that we use the
618 larger size. This problematic case can always be avoided in C,
619 but it must be handled correctly when using Fortran shared
620 libraries.
621
622 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
623 likewise for OLDDYNCOMMON and OLDDEF.
624
625 Note that this test is just a heuristic, and that it is quite
626 possible to have an uninitialized symbol in a shared object which
627 is really a definition, rather than a common symbol. This could
628 lead to some minor confusion when the symbol really is a common
629 symbol in some regular object. However, I think it will be
630 harmless. */
631
632 if (newdyn
633 && newdef
634 && (sec->flags & SEC_ALLOC) != 0
635 && (sec->flags & SEC_LOAD) == 0
636 && sym->st_size > 0
637 && bind != STB_WEAK
638 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
b34976b6 639 newdyncommon = TRUE;
252b5132 640 else
b34976b6 641 newdyncommon = FALSE;
252b5132
RH
642
643 if (olddyn
644 && olddef
645 && h->root.type == bfd_link_hash_defined
646 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
647 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
648 && (h->root.u.def.section->flags & SEC_LOAD) == 0
649 && h->size > 0
650 && h->type != STT_FUNC)
b34976b6 651 olddyncommon = TRUE;
252b5132 652 else
b34976b6 653 olddyncommon = FALSE;
252b5132
RH
654
655 /* It's OK to change the type if either the existing symbol or the
456981d7
L
656 new symbol is weak unless it comes from a DT_NEEDED entry of
657 a shared object, in which case, the DT_NEEDED entry may not be
3e932841 658 required at the run time. */
252b5132 659
456981d7 660 if ((! dt_needed && h->root.type == bfd_link_hash_defweak)
252b5132
RH
661 || h->root.type == bfd_link_hash_undefweak
662 || bind == STB_WEAK)
b34976b6 663 *type_change_ok = TRUE;
252b5132
RH
664
665 /* It's OK to change the size if either the existing symbol or the
666 new symbol is weak, or if the old symbol is undefined. */
667
668 if (*type_change_ok
669 || h->root.type == bfd_link_hash_undefined)
b34976b6 670 *size_change_ok = TRUE;
252b5132
RH
671
672 /* If both the old and the new symbols look like common symbols in a
673 dynamic object, set the size of the symbol to the larger of the
674 two. */
675
676 if (olddyncommon
677 && newdyncommon
678 && sym->st_size != h->size)
679 {
680 /* Since we think we have two common symbols, issue a multiple
c44233aa
AM
681 common warning if desired. Note that we only warn if the
682 size is different. If the size is the same, we simply let
683 the old symbol override the new one as normally happens with
684 symbols defined in dynamic objects. */
252b5132
RH
685
686 if (! ((*info->callbacks->multiple_common)
687 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
688 h->size, abfd, bfd_link_hash_common, sym->st_size)))
b34976b6 689 return FALSE;
252b5132
RH
690
691 if (sym->st_size > h->size)
692 h->size = sym->st_size;
693
b34976b6 694 *size_change_ok = TRUE;
252b5132
RH
695 }
696
697 /* If we are looking at a dynamic object, and we have found a
698 definition, we need to see if the symbol was already defined by
699 some other object. If so, we want to use the existing
700 definition, and we do not want to report a multiple symbol
701 definition error; we do this by clobbering *PSEC to be
702 bfd_und_section_ptr.
703
704 We treat a common symbol as a definition if the symbol in the
705 shared library is a function, since common symbols always
706 represent variables; this can cause confusion in principle, but
707 any such confusion would seem to indicate an erroneous program or
708 shared library. We also permit a common symbol in a regular
0525d26e
ILT
709 object to override a weak symbol in a shared object.
710
711 We prefer a non-weak definition in a shared library to a weak
456981d7
L
712 definition in the executable unless it comes from a DT_NEEDED
713 entry of a shared object, in which case, the DT_NEEDED entry
3e932841 714 may not be required at the run time. */
252b5132
RH
715
716 if (newdyn
717 && newdef
718 && (olddef
719 || (h->root.type == bfd_link_hash_common
720 && (bind == STB_WEAK
0525d26e 721 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
3e932841 722 && (h->root.type != bfd_link_hash_defweak
456981d7 723 || dt_needed
0525d26e 724 || bind == STB_WEAK))
252b5132 725 {
b34976b6
AM
726 *override = TRUE;
727 newdef = FALSE;
728 newdyncommon = FALSE;
252b5132
RH
729
730 *psec = sec = bfd_und_section_ptr;
b34976b6 731 *size_change_ok = TRUE;
252b5132
RH
732
733 /* If we get here when the old symbol is a common symbol, then
c44233aa
AM
734 we are explicitly letting it override a weak symbol or
735 function in a dynamic object, and we don't want to warn about
736 a type change. If the old symbol is a defined symbol, a type
737 change warning may still be appropriate. */
252b5132
RH
738
739 if (h->root.type == bfd_link_hash_common)
b34976b6 740 *type_change_ok = TRUE;
252b5132
RH
741 }
742
743 /* Handle the special case of an old common symbol merging with a
744 new symbol which looks like a common symbol in a shared object.
745 We change *PSEC and *PVALUE to make the new symbol look like a
746 common symbol, and let _bfd_generic_link_add_one_symbol will do
747 the right thing. */
748
749 if (newdyncommon
750 && h->root.type == bfd_link_hash_common)
751 {
b34976b6
AM
752 *override = TRUE;
753 newdef = FALSE;
754 newdyncommon = FALSE;
252b5132
RH
755 *pvalue = sym->st_size;
756 *psec = sec = bfd_com_section_ptr;
b34976b6 757 *size_change_ok = TRUE;
252b5132
RH
758 }
759
760 /* If the old symbol is from a dynamic object, and the new symbol is
761 a definition which is not from a dynamic object, then the new
762 symbol overrides the old symbol. Symbols from regular files
763 always take precedence over symbols from dynamic objects, even if
764 they are defined after the dynamic object in the link.
765
766 As above, we again permit a common symbol in a regular object to
767 override a definition in a shared object if the shared object
0525d26e
ILT
768 symbol is a function or is weak.
769
770 As above, we permit a non-weak definition in a shared object to
771 override a weak definition in a regular object. */
252b5132 772
3c0a515d 773 flip = NULL;
252b5132
RH
774 if (! newdyn
775 && (newdef
776 || (bfd_is_com_section (sec)
777 && (h->root.type == bfd_link_hash_defweak
778 || h->type == STT_FUNC)))
779 && olddyn
780 && olddef
0525d26e
ILT
781 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
782 && (bind != STB_WEAK
783 || h->root.type == bfd_link_hash_defweak))
252b5132
RH
784 {
785 /* Change the hash table entry to undefined, and let
786 _bfd_generic_link_add_one_symbol do the right thing with the
787 new definition. */
788
789 h->root.type = bfd_link_hash_undefined;
790 h->root.u.undef.abfd = h->root.u.def.section->owner;
b34976b6 791 *size_change_ok = TRUE;
252b5132 792
b34976b6
AM
793 olddef = FALSE;
794 olddyncommon = FALSE;
252b5132
RH
795
796 /* We again permit a type change when a common symbol may be
c44233aa 797 overriding a function. */
252b5132
RH
798
799 if (bfd_is_com_section (sec))
b34976b6 800 *type_change_ok = TRUE;
252b5132 801
3c0a515d
L
802 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
803 flip = *sym_hash;
804 else
805 /* This union may have been set to be non-NULL when this symbol
806 was seen in a dynamic object. We must force the union to be
807 NULL, so that it is correct for a regular symbol. */
808 h->verinfo.vertree = NULL;
252b5132
RH
809 }
810
811 /* Handle the special case of a new common symbol merging with an
812 old symbol that looks like it might be a common symbol defined in
813 a shared object. Note that we have already handled the case in
814 which a new common symbol should simply override the definition
815 in the shared library. */
816
817 if (! newdyn
818 && bfd_is_com_section (sec)
819 && olddyncommon)
820 {
821 /* It would be best if we could set the hash table entry to a
822 common symbol, but we don't know what to use for the section
823 or the alignment. */
824 if (! ((*info->callbacks->multiple_common)
825 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
826 h->size, abfd, bfd_link_hash_common, sym->st_size)))
b34976b6 827 return FALSE;
252b5132
RH
828
829 /* If the predumed common symbol in the dynamic object is
c44233aa 830 larger, pretend that the new symbol has its size. */
252b5132
RH
831
832 if (h->size > *pvalue)
833 *pvalue = h->size;
834
835 /* FIXME: We no longer know the alignment required by the symbol
836 in the dynamic object, so we just wind up using the one from
837 the regular object. */
838
b34976b6
AM
839 olddef = FALSE;
840 olddyncommon = FALSE;
252b5132
RH
841
842 h->root.type = bfd_link_hash_undefined;
843 h->root.u.undef.abfd = h->root.u.def.section->owner;
844
b34976b6
AM
845 *size_change_ok = TRUE;
846 *type_change_ok = TRUE;
252b5132 847
3c0a515d
L
848 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
849 flip = *sym_hash;
850 else
851 h->verinfo.vertree = NULL;
852 }
853
854 if (flip != NULL)
855 {
856 /* Handle the case where we had a versioned symbol in a dynamic
857 library and now find a definition in a normal object. In this
858 case, we make the versioned symbol point to the normal one. */
859 flip->root.type = h->root.type;
860 flip->root.u.undef.abfd = h->root.u.undef.abfd;
861 h->root.type = bfd_link_hash_indirect;
862 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
863 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
864 {
865 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
866 flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
867 }
252b5132
RH
868 }
869
0525d26e
ILT
870 /* Handle the special case of a weak definition in a regular object
871 followed by a non-weak definition in a shared object. In this
456981d7
L
872 case, we prefer the definition in the shared object unless it
873 comes from a DT_NEEDED entry of a shared object, in which case,
3e932841 874 the DT_NEEDED entry may not be required at the run time. */
0525d26e 875 if (olddef
456981d7 876 && ! dt_needed
0525d26e
ILT
877 && h->root.type == bfd_link_hash_defweak
878 && newdef
879 && newdyn
880 && bind != STB_WEAK)
b4536acd
ILT
881 {
882 /* To make this work we have to frob the flags so that the rest
c44233aa
AM
883 of the code does not think we are using the regular
884 definition. */
64df8d0b
ILT
885 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
886 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
887 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
888 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
889 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
890 | ELF_LINK_HASH_DEF_DYNAMIC);
b4536acd
ILT
891
892 /* If H is the target of an indirection, we want the caller to
c44233aa
AM
893 use H rather than the indirect symbol. Otherwise if we are
894 defining a new indirect symbol we will wind up attaching it
895 to the entry we are overriding. */
b4536acd
ILT
896 *sym_hash = h;
897 }
0525d26e
ILT
898
899 /* Handle the special case of a non-weak definition in a shared
900 object followed by a weak definition in a regular object. In
3c0a515d 901 this case we prefer the definition in the shared object. To make
0525d26e
ILT
902 this work we have to tell the caller to not treat the new symbol
903 as a definition. */
904 if (olddef
905 && olddyn
906 && h->root.type != bfd_link_hash_defweak
907 && newdef
908 && ! newdyn
909 && bind == STB_WEAK)
b34976b6 910 *override = TRUE;
0525d26e 911
b34976b6 912 return TRUE;
252b5132
RH
913}
914
215007a6
L
915/* This function is called to create an indirect symbol from the
916 default for the symbol with the default version if needed. The
871da94f 917 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
215007a6
L
918 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
919 indicates if it comes from a DT_NEEDED entry of a shared object. */
920
b34976b6 921static bfd_boolean
871da94f 922elf_add_default_symbol (abfd, info, h, name, sym, psec, value,
215007a6
L
923 dynsym, override, dt_needed)
924 bfd *abfd;
925 struct bfd_link_info *info;
926 struct elf_link_hash_entry *h;
927 const char *name;
928 Elf_Internal_Sym *sym;
871da94f 929 asection **psec;
215007a6 930 bfd_vma *value;
b34976b6
AM
931 bfd_boolean *dynsym;
932 bfd_boolean override;
933 bfd_boolean dt_needed;
215007a6 934{
b34976b6
AM
935 bfd_boolean type_change_ok;
936 bfd_boolean size_change_ok;
215007a6
L
937 char *shortname;
938 struct elf_link_hash_entry *hi;
14a793b2 939 struct bfd_link_hash_entry *bh;
215007a6 940 struct elf_backend_data *bed;
b34976b6
AM
941 bfd_boolean collect;
942 bfd_boolean dynamic;
215007a6 943 char *p;
d4c88bbb 944 size_t len, shortlen;
871da94f 945 asection *sec;
215007a6
L
946
947 /* If this symbol has a version, and it is the default version, we
948 create an indirect symbol from the default name to the fully
949 decorated name. This will cause external references which do not
950 specify a version to be bound to this version of the symbol. */
951 p = strchr (name, ELF_VER_CHR);
952 if (p == NULL || p[1] != ELF_VER_CHR)
b34976b6 953 return TRUE;
215007a6
L
954
955 if (override)
956 {
957 /* We are overridden by an old defition. We need to check if we
d4c88bbb 958 need to create the indirect symbol from the default name. */
b34976b6
AM
959 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
960 FALSE, FALSE);
215007a6
L
961 BFD_ASSERT (hi != NULL);
962 if (hi == h)
b34976b6 963 return TRUE;
215007a6
L
964 while (hi->root.type == bfd_link_hash_indirect
965 || hi->root.type == bfd_link_hash_warning)
966 {
967 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
968 if (hi == h)
b34976b6 969 return TRUE;
215007a6
L
970 }
971 }
972
973 bed = get_elf_backend_data (abfd);
974 collect = bed->collect;
975 dynamic = (abfd->flags & DYNAMIC) != 0;
976
d4c88bbb
AM
977 shortlen = p - name;
978 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
215007a6 979 if (shortname == NULL)
b34976b6 980 return FALSE;
d4c88bbb
AM
981 memcpy (shortname, name, shortlen);
982 shortname[shortlen] = '\0';
215007a6
L
983
984 /* We are going to create a new symbol. Merge it with any existing
985 symbol with this name. For the purposes of the merge, act as
986 though we were defining the symbol we just defined, although we
987 actually going to define an indirect symbol. */
b34976b6
AM
988 type_change_ok = FALSE;
989 size_change_ok = FALSE;
871da94f
L
990 sec = *psec;
991 if (! elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
215007a6
L
992 &hi, &override, &type_change_ok,
993 &size_change_ok, dt_needed))
b34976b6 994 return FALSE;
215007a6
L
995
996 if (! override)
997 {
14a793b2 998 bh = &hi->root;
215007a6
L
999 if (! (_bfd_generic_link_add_one_symbol
1000 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
b34976b6
AM
1001 (bfd_vma) 0, name, FALSE, collect, &bh)))
1002 return FALSE;
14a793b2 1003 hi = (struct elf_link_hash_entry *) bh;
215007a6
L
1004 }
1005 else
1006 {
1007 /* In this case the symbol named SHORTNAME is overriding the
1008 indirect symbol we want to add. We were planning on making
1009 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1010 is the name without a version. NAME is the fully versioned
1011 name, and it is the default version.
1012
1013 Overriding means that we already saw a definition for the
1014 symbol SHORTNAME in a regular object, and it is overriding
1015 the symbol defined in the dynamic object.
1016
1017 When this happens, we actually want to change NAME, the
1018 symbol we just added, to refer to SHORTNAME. This will cause
1019 references to NAME in the shared object to become references
1020 to SHORTNAME in the regular object. This is what we expect
1021 when we override a function in a shared object: that the
1022 references in the shared object will be mapped to the
1023 definition in the regular object. */
1024
1025 while (hi->root.type == bfd_link_hash_indirect
1026 || hi->root.type == bfd_link_hash_warning)
1027 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1028
1029 h->root.type = bfd_link_hash_indirect;
1030 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1031 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1032 {
1033 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1034 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1035 if (hi->elf_link_hash_flags
1036 & (ELF_LINK_HASH_REF_REGULAR
1037 | ELF_LINK_HASH_DEF_REGULAR))
1038 {
1039 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
b34976b6 1040 return FALSE;
215007a6
L
1041 }
1042 }
1043
1044 /* Now set HI to H, so that the following code will set the
c44233aa 1045 other fields correctly. */
215007a6
L
1046 hi = h;
1047 }
1048
1049 /* If there is a duplicate definition somewhere, then HI may not
1050 point to an indirect symbol. We will have reported an error to
1051 the user in that case. */
1052
1053 if (hi->root.type == bfd_link_hash_indirect)
1054 {
1055 struct elf_link_hash_entry *ht;
1056
1057 /* If the symbol became indirect, then we assume that we have
1058 not seen a definition before. */
1059 BFD_ASSERT ((hi->elf_link_hash_flags
1060 & (ELF_LINK_HASH_DEF_DYNAMIC
1061 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1062
1063 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
b48fa14c 1064 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
215007a6
L
1065
1066 /* See if the new flags lead us to realize that the symbol must
1067 be dynamic. */
1068 if (! *dynsym)
1069 {
1070 if (! dynamic)
1071 {
1072 if (info->shared
1073 || ((hi->elf_link_hash_flags
1074 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
b34976b6 1075 *dynsym = TRUE;
215007a6
L
1076 }
1077 else
1078 {
1079 if ((hi->elf_link_hash_flags
1080 & ELF_LINK_HASH_REF_REGULAR) != 0)
b34976b6 1081 *dynsym = TRUE;
215007a6
L
1082 }
1083 }
1084 }
1085
1086 /* We also need to define an indirection from the nondefault version
1087 of the symbol. */
1088
d4c88bbb
AM
1089 len = strlen (name);
1090 shortname = bfd_hash_allocate (&info->hash->table, len);
215007a6 1091 if (shortname == NULL)
b34976b6 1092 return FALSE;
d4c88bbb
AM
1093 memcpy (shortname, name, shortlen);
1094 memcpy (shortname + shortlen, p + 1, len - shortlen);
215007a6
L
1095
1096 /* Once again, merge with any existing symbol. */
b34976b6
AM
1097 type_change_ok = FALSE;
1098 size_change_ok = FALSE;
871da94f
L
1099 sec = *psec;
1100 if (! elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
215007a6
L
1101 &hi, &override, &type_change_ok,
1102 &size_change_ok, dt_needed))
b34976b6 1103 return FALSE;
215007a6
L
1104
1105 if (override)
1106 {
1107 /* Here SHORTNAME is a versioned name, so we don't expect to see
d8bbe778
L
1108 the type of override we do in the case above unless it is
1109 overridden by a versioned definiton. */
1110 if (hi->root.type != bfd_link_hash_defined
1111 && hi->root.type != bfd_link_hash_defweak)
1112 (*_bfd_error_handler)
1113 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1114 bfd_archive_filename (abfd), shortname);
215007a6
L
1115 }
1116 else
1117 {
14a793b2 1118 bh = &hi->root;
215007a6
L
1119 if (! (_bfd_generic_link_add_one_symbol
1120 (info, abfd, shortname, BSF_INDIRECT,
b34976b6
AM
1121 bfd_ind_section_ptr, (bfd_vma) 0, name, FALSE, collect, &bh)))
1122 return FALSE;
14a793b2 1123 hi = (struct elf_link_hash_entry *) bh;
215007a6
L
1124
1125 /* If there is a duplicate definition somewhere, then HI may not
1126 point to an indirect symbol. We will have reported an error
1127 to the user in that case. */
1128
1129 if (hi->root.type == bfd_link_hash_indirect)
1130 {
1131 /* If the symbol became indirect, then we assume that we have
1132 not seen a definition before. */
1133 BFD_ASSERT ((hi->elf_link_hash_flags
1134 & (ELF_LINK_HASH_DEF_DYNAMIC
1135 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1136
b48fa14c 1137 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
215007a6
L
1138
1139 /* See if the new flags lead us to realize that the symbol
1140 must be dynamic. */
1141 if (! *dynsym)
1142 {
1143 if (! dynamic)
1144 {
1145 if (info->shared
1146 || ((hi->elf_link_hash_flags
1147 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
b34976b6 1148 *dynsym = TRUE;
215007a6
L
1149 }
1150 else
1151 {
1152 if ((hi->elf_link_hash_flags
1153 & ELF_LINK_HASH_REF_REGULAR) != 0)
b34976b6 1154 *dynsym = TRUE;
215007a6
L
1155 }
1156 }
1157 }
1158 }
1159
b34976b6 1160 return TRUE;
215007a6
L
1161}
1162
252b5132
RH
1163/* Add symbols from an ELF object file to the linker hash table. */
1164
b34976b6 1165static bfd_boolean
252b5132
RH
1166elf_link_add_object_symbols (abfd, info)
1167 bfd *abfd;
1168 struct bfd_link_info *info;
1169{
b34976b6
AM
1170 bfd_boolean (*add_symbol_hook)
1171 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
1172 const char **, flagword *, asection **, bfd_vma *));
1173 bfd_boolean (*check_relocs)
1174 PARAMS ((bfd *, struct bfd_link_info *, asection *,
1175 const Elf_Internal_Rela *));
1176 bfd_boolean collect;
252b5132 1177 Elf_Internal_Shdr *hdr;
dc810e39
AM
1178 bfd_size_type symcount;
1179 bfd_size_type extsymcount;
1180 bfd_size_type extsymoff;
252b5132 1181 struct elf_link_hash_entry **sym_hash;
b34976b6 1182 bfd_boolean dynamic;
252b5132
RH
1183 Elf_External_Versym *extversym = NULL;
1184 Elf_External_Versym *ever;
252b5132 1185 struct elf_link_hash_entry *weaks;
9147e853
JJ
1186 struct elf_link_hash_entry **nondeflt_vers = NULL;
1187 bfd_size_type nondeflt_vers_cnt = 0;
6cdc0ccc
AM
1188 Elf_Internal_Sym *isymbuf = NULL;
1189 Elf_Internal_Sym *isym;
1190 Elf_Internal_Sym *isymend;
c61b8717 1191 struct elf_backend_data *bed;
b34976b6 1192 bfd_boolean dt_needed;
8ea2e4bd 1193 struct elf_link_hash_table * hash_table;
dc810e39 1194 bfd_size_type amt;
8ea2e4bd
NC
1195
1196 hash_table = elf_hash_table (info);
252b5132 1197
c61b8717
RH
1198 bed = get_elf_backend_data (abfd);
1199 add_symbol_hook = bed->elf_add_symbol_hook;
1200 collect = bed->collect;
252b5132
RH
1201
1202 if ((abfd->flags & DYNAMIC) == 0)
b34976b6 1203 dynamic = FALSE;
252b5132
RH
1204 else
1205 {
b34976b6 1206 dynamic = TRUE;
252b5132
RH
1207
1208 /* You can't use -r against a dynamic object. Also, there's no
1209 hope of using a dynamic object which does not exactly match
1210 the format of the output file. */
1211 if (info->relocateable || info->hash->creator != abfd->xvec)
1212 {
1213 bfd_set_error (bfd_error_invalid_operation);
1214 goto error_return;
1215 }
1216 }
1217
1218 /* As a GNU extension, any input sections which are named
1219 .gnu.warning.SYMBOL are treated as warning symbols for the given
1220 symbol. This differs from .gnu.warning sections, which generate
1221 warnings when they are included in an output file. */
1222 if (! info->shared)
1223 {
1224 asection *s;
1225
1226 for (s = abfd->sections; s != NULL; s = s->next)
1227 {
1228 const char *name;
1229
1230 name = bfd_get_section_name (abfd, s);
1231 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1232 {
1233 char *msg;
1234 bfd_size_type sz;
1235
1236 name += sizeof ".gnu.warning." - 1;
1237
1238 /* If this is a shared object, then look up the symbol
1239 in the hash table. If it is there, and it is already
1240 been defined, then we will not be using the entry
1241 from this shared object, so we don't need to warn.
1242 FIXME: If we see the definition in a regular object
1243 later on, we will warn, but we shouldn't. The only
1244 fix is to keep track of what warnings we are supposed
1245 to emit, and then handle them all at the end of the
1246 link. */
1247 if (dynamic && abfd->xvec == info->hash->creator)
1248 {
1249 struct elf_link_hash_entry *h;
1250
8ea2e4bd 1251 h = elf_link_hash_lookup (hash_table, name,
b34976b6 1252 FALSE, FALSE, TRUE);
252b5132
RH
1253
1254 /* FIXME: What about bfd_link_hash_common? */
1255 if (h != NULL
1256 && (h->root.type == bfd_link_hash_defined
1257 || h->root.type == bfd_link_hash_defweak))
1258 {
1259 /* We don't want to issue this warning. Clobber
c44233aa
AM
1260 the section size so that the warning does not
1261 get copied into the output file. */
252b5132
RH
1262 s->_raw_size = 0;
1263 continue;
1264 }
1265 }
1266
1267 sz = bfd_section_size (abfd, s);
1268 msg = (char *) bfd_alloc (abfd, sz + 1);
1269 if (msg == NULL)
1270 goto error_return;
1271
1272 if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
1273 goto error_return;
1274
1275 msg[sz] = '\0';
1276
1277 if (! (_bfd_generic_link_add_one_symbol
1278 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
b34976b6 1279 FALSE, collect, (struct bfd_link_hash_entry **) NULL)))
252b5132
RH
1280 goto error_return;
1281
1282 if (! info->relocateable)
1283 {
1284 /* Clobber the section size so that the warning does
c44233aa 1285 not get copied into the output file. */
252b5132
RH
1286 s->_raw_size = 0;
1287 }
1288 }
1289 }
1290 }
1291
b34976b6 1292 dt_needed = FALSE;
252b5132
RH
1293 if (! dynamic)
1294 {
1295 /* If we are creating a shared library, create all the dynamic
c44233aa
AM
1296 sections immediately. We need to attach them to something,
1297 so we attach them to this BFD, provided it is the right
1298 format. FIXME: If there are no input BFD's of the same
1299 format as the output, we can't make a shared library. */
252b5132 1300 if (info->shared
8ea2e4bd
NC
1301 && is_elf_hash_table (info)
1302 && ! hash_table->dynamic_sections_created
252b5132
RH
1303 && abfd->xvec == info->hash->creator)
1304 {
1305 if (! elf_link_create_dynamic_sections (abfd, info))
1306 goto error_return;
1307 }
1308 }
8ea2e4bd
NC
1309 else if (! is_elf_hash_table (info))
1310 goto error_return;
252b5132
RH
1311 else
1312 {
1313 asection *s;
b34976b6 1314 bfd_boolean add_needed;
252b5132
RH
1315 const char *name;
1316 bfd_size_type oldsize;
1317 bfd_size_type strindex;
5d603aa9 1318 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
252b5132 1319
366f2964
AM
1320 /* ld --just-symbols and dynamic objects don't mix very well.
1321 Test for --just-symbols by looking at info set up by
1322 _bfd_elf_link_just_syms. */
1323 if ((s = abfd->sections) != NULL
68bfbfcc 1324 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
366f2964
AM
1325 goto error_return;
1326
252b5132
RH
1327 /* Find the name to use in a DT_NEEDED entry that refers to this
1328 object. If the object has a DT_SONAME entry, we use it.
1329 Otherwise, if the generic linker stuck something in
1330 elf_dt_name, we use that. Otherwise, we just use the file
1331 name. If the generic linker put a null string into
1332 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1333 there is a DT_SONAME entry. */
b34976b6 1334 add_needed = TRUE;
7913c838 1335 name = bfd_get_filename (abfd);
252b5132
RH
1336 if (elf_dt_name (abfd) != NULL)
1337 {
1338 name = elf_dt_name (abfd);
1339 if (*name == '\0')
74816898
L
1340 {
1341 if (elf_dt_soname (abfd) != NULL)
b34976b6 1342 dt_needed = TRUE;
74816898 1343
b34976b6 1344 add_needed = FALSE;
74816898 1345 }
252b5132
RH
1346 }
1347 s = bfd_get_section_by_name (abfd, ".dynamic");
1348 if (s != NULL)
1349 {
6cdc0ccc 1350 Elf_External_Dyn *dynbuf = NULL;
252b5132
RH
1351 Elf_External_Dyn *extdyn;
1352 Elf_External_Dyn *extdynend;
1353 int elfsec;
dc810e39 1354 unsigned long shlink;
252b5132 1355
dc810e39 1356 dynbuf = (Elf_External_Dyn *) bfd_malloc (s->_raw_size);
252b5132
RH
1357 if (dynbuf == NULL)
1358 goto error_return;
1359
1360 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
1361 (file_ptr) 0, s->_raw_size))
6cdc0ccc 1362 goto error_free_dyn;
252b5132
RH
1363
1364 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1365 if (elfsec == -1)
6cdc0ccc 1366 goto error_free_dyn;
dc810e39 1367 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1368
1369 extdyn = dynbuf;
1370 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
1371 for (; extdyn < extdynend; extdyn++)
1372 {
1373 Elf_Internal_Dyn dyn;
1374
1375 elf_swap_dyn_in (abfd, extdyn, &dyn);
1376 if (dyn.d_tag == DT_SONAME)
1377 {
dc810e39
AM
1378 unsigned int tagv = dyn.d_un.d_val;
1379 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132 1380 if (name == NULL)
6cdc0ccc 1381 goto error_free_dyn;
252b5132
RH
1382 }
1383 if (dyn.d_tag == DT_NEEDED)
1384 {
1385 struct bfd_link_needed_list *n, **pn;
1386 char *fnm, *anm;
dc810e39 1387 unsigned int tagv = dyn.d_un.d_val;
252b5132 1388
dc810e39
AM
1389 amt = sizeof (struct bfd_link_needed_list);
1390 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1391 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132 1392 if (n == NULL || fnm == NULL)
6cdc0ccc 1393 goto error_free_dyn;
d4c88bbb
AM
1394 amt = strlen (fnm) + 1;
1395 anm = bfd_alloc (abfd, amt);
252b5132 1396 if (anm == NULL)
6cdc0ccc 1397 goto error_free_dyn;
d4c88bbb 1398 memcpy (anm, fnm, (size_t) amt);
252b5132
RH
1399 n->name = anm;
1400 n->by = abfd;
1401 n->next = NULL;
8ea2e4bd 1402 for (pn = & hash_table->needed;
252b5132
RH
1403 *pn != NULL;
1404 pn = &(*pn)->next)
1405 ;
1406 *pn = n;
1407 }
a963dc6a
L
1408 if (dyn.d_tag == DT_RUNPATH)
1409 {
1410 struct bfd_link_needed_list *n, **pn;
1411 char *fnm, *anm;
dc810e39 1412 unsigned int tagv = dyn.d_un.d_val;
a963dc6a 1413
dc810e39
AM
1414 amt = sizeof (struct bfd_link_needed_list);
1415 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1416 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
a963dc6a 1417 if (n == NULL || fnm == NULL)
6cdc0ccc 1418 goto error_free_dyn;
d4c88bbb
AM
1419 amt = strlen (fnm) + 1;
1420 anm = bfd_alloc (abfd, amt);
a963dc6a 1421 if (anm == NULL)
6cdc0ccc 1422 goto error_free_dyn;
d4c88bbb 1423 memcpy (anm, fnm, (size_t) amt);
a963dc6a
L
1424 n->name = anm;
1425 n->by = abfd;
1426 n->next = NULL;
5d603aa9 1427 for (pn = & runpath;
a963dc6a
L
1428 *pn != NULL;
1429 pn = &(*pn)->next)
1430 ;
1431 *pn = n;
a963dc6a 1432 }
3e932841 1433 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
a963dc6a 1434 if (!runpath && dyn.d_tag == DT_RPATH)
c44233aa 1435 {
a963dc6a
L
1436 struct bfd_link_needed_list *n, **pn;
1437 char *fnm, *anm;
dc810e39 1438 unsigned int tagv = dyn.d_un.d_val;
a963dc6a 1439
dc810e39
AM
1440 amt = sizeof (struct bfd_link_needed_list);
1441 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1442 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
a963dc6a 1443 if (n == NULL || fnm == NULL)
6cdc0ccc 1444 goto error_free_dyn;
d4c88bbb
AM
1445 amt = strlen (fnm) + 1;
1446 anm = bfd_alloc (abfd, amt);
a963dc6a 1447 if (anm == NULL)
6cdc0ccc
AM
1448 {
1449 error_free_dyn:
1450 free (dynbuf);
1451 goto error_return;
1452 }
d4c88bbb 1453 memcpy (anm, fnm, (size_t) amt);
a963dc6a
L
1454 n->name = anm;
1455 n->by = abfd;
1456 n->next = NULL;
5d603aa9 1457 for (pn = & rpath;
a963dc6a
L
1458 *pn != NULL;
1459 pn = &(*pn)->next)
1460 ;
1461 *pn = n;
a963dc6a 1462 }
252b5132
RH
1463 }
1464
1465 free (dynbuf);
252b5132
RH
1466 }
1467
5d603aa9
L
1468 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
1469 frees all more recently bfd_alloc'd blocks as well. */
1470 if (runpath)
1471 rpath = runpath;
1472
1473 if (rpath)
1474 {
1475 struct bfd_link_needed_list **pn;
1476 for (pn = & hash_table->runpath;
1477 *pn != NULL;
1478 pn = &(*pn)->next)
1479 ;
1480 *pn = rpath;
1481 }
1482
252b5132
RH
1483 /* We do not want to include any of the sections in a dynamic
1484 object in the output file. We hack by simply clobbering the
1485 list of sections in the BFD. This could be handled more
1486 cleanly by, say, a new section flag; the existing
1487 SEC_NEVER_LOAD flag is not the one we want, because that one
1488 still implies that the section takes up space in the output
1489 file. */
c601ffdb 1490 bfd_section_list_clear (abfd);
252b5132
RH
1491
1492 /* If this is the first dynamic object found in the link, create
1493 the special sections required for dynamic linking. */
8ea2e4bd
NC
1494 if (! hash_table->dynamic_sections_created)
1495 if (! elf_link_create_dynamic_sections (abfd, info))
1496 goto error_return;
252b5132
RH
1497
1498 if (add_needed)
1499 {
1500 /* Add a DT_NEEDED entry for this dynamic object. */
2b0f7ef9 1501 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
b34976b6 1502 strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, FALSE);
252b5132
RH
1503 if (strindex == (bfd_size_type) -1)
1504 goto error_return;
1505
2b0f7ef9 1506 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
252b5132
RH
1507 {
1508 asection *sdyn;
1509 Elf_External_Dyn *dyncon, *dynconend;
1510
1511 /* The hash table size did not change, which means that
1512 the dynamic object name was already entered. If we
1513 have already included this dynamic object in the
1514 link, just ignore it. There is no reason to include
1515 a particular dynamic object more than once. */
8ea2e4bd 1516 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
252b5132
RH
1517 BFD_ASSERT (sdyn != NULL);
1518
1519 dyncon = (Elf_External_Dyn *) sdyn->contents;
1520 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1521 sdyn->_raw_size);
1522 for (; dyncon < dynconend; dyncon++)
1523 {
1524 Elf_Internal_Dyn dyn;
1525
8ea2e4bd 1526 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
252b5132
RH
1527 if (dyn.d_tag == DT_NEEDED
1528 && dyn.d_un.d_val == strindex)
1529 {
2b0f7ef9 1530 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
b34976b6 1531 return TRUE;
252b5132
RH
1532 }
1533 }
1534 }
1535
dc810e39 1536 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
252b5132
RH
1537 goto error_return;
1538 }
1539
1540 /* Save the SONAME, if there is one, because sometimes the
c44233aa 1541 linker emulation code will need to know it. */
252b5132 1542 if (*name == '\0')
210ba1e8 1543 name = basename (bfd_get_filename (abfd));
252b5132
RH
1544 elf_dt_name (abfd) = name;
1545 }
1546
6cdc0ccc
AM
1547 /* If this is a dynamic object, we always link against the .dynsym
1548 symbol table, not the .symtab symbol table. The dynamic linker
1549 will only see the .dynsym symbol table, so there is no reason to
1550 look at .symtab for a dynamic object. */
1551
1552 if (! dynamic || elf_dynsymtab (abfd) == 0)
1553 hdr = &elf_tdata (abfd)->symtab_hdr;
1554 else
1555 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1556
1557 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1558
1559 /* The sh_info field of the symtab header tells us where the
1560 external symbols start. We don't care about the local symbols at
1561 this point. */
1562 if (elf_bad_symtab (abfd))
1563 {
1564 extsymcount = symcount;
1565 extsymoff = 0;
1566 }
1567 else
1568 {
1569 extsymcount = symcount - hdr->sh_info;
1570 extsymoff = hdr->sh_info;
1571 }
252b5132 1572
6cdc0ccc
AM
1573 sym_hash = NULL;
1574 if (extsymcount != 0)
9ad5cbcf 1575 {
6cdc0ccc
AM
1576 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
1577 NULL, NULL, NULL);
1578 if (isymbuf == NULL)
9ad5cbcf 1579 goto error_return;
6cdc0ccc
AM
1580
1581 /* We store a pointer to the hash table entry for each external
1582 symbol. */
1583 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1584 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
1585 if (sym_hash == NULL)
1586 goto error_free_sym;
1587 elf_sym_hashes (abfd) = sym_hash;
1588 }
1589
1590 if (dynamic)
1591 {
1592 /* Read in any version definitions. */
1593 if (! _bfd_elf_slurp_version_tables (abfd))
1594 goto error_free_sym;
1595
1596 /* Read in the symbol versions, but don't bother to convert them
1597 to internal format. */
1598 if (elf_dynversym (abfd) != 0)
1599 {
1600 Elf_Internal_Shdr *versymhdr;
1601
1602 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
1603 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
1604 if (extversym == NULL)
1605 goto error_free_sym;
1606 amt = versymhdr->sh_size;
1607 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
1608 || bfd_bread ((PTR) extversym, amt, abfd) != amt)
1609 goto error_free_vers;
1610 }
9ad5cbcf
AM
1611 }
1612
252b5132
RH
1613 weaks = NULL;
1614
1615 ever = extversym != NULL ? extversym + extsymoff : NULL;
6cdc0ccc
AM
1616 for (isym = isymbuf, isymend = isymbuf + extsymcount;
1617 isym < isymend;
1618 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
252b5132 1619 {
252b5132
RH
1620 int bind;
1621 bfd_vma value;
1622 asection *sec;
1623 flagword flags;
1624 const char *name;
1625 struct elf_link_hash_entry *h;
b34976b6 1626 bfd_boolean definition;
f7661995
NC
1627 bfd_boolean size_change_ok;
1628 bfd_boolean type_change_ok;
b34976b6 1629 bfd_boolean new_weakdef;
b34976b6 1630 bfd_boolean override;
f7661995
NC
1631 unsigned int old_alignment;
1632 bfd *old_bfd;
215007a6 1633
b34976b6 1634 override = FALSE;
252b5132 1635
252b5132
RH
1636 flags = BSF_NO_FLAGS;
1637 sec = NULL;
6cdc0ccc 1638 value = isym->st_value;
252b5132
RH
1639 *sym_hash = NULL;
1640
6cdc0ccc 1641 bind = ELF_ST_BIND (isym->st_info);
252b5132
RH
1642 if (bind == STB_LOCAL)
1643 {
1644 /* This should be impossible, since ELF requires that all
1645 global symbols follow all local symbols, and that sh_info
1646 point to the first global symbol. Unfortunatealy, Irix 5
1647 screws this up. */
1648 continue;
1649 }
1650 else if (bind == STB_GLOBAL)
1651 {
6cdc0ccc
AM
1652 if (isym->st_shndx != SHN_UNDEF
1653 && isym->st_shndx != SHN_COMMON)
252b5132 1654 flags = BSF_GLOBAL;
252b5132
RH
1655 }
1656 else if (bind == STB_WEAK)
1657 flags = BSF_WEAK;
1658 else
1659 {
1660 /* Leave it up to the processor backend. */
1661 }
1662
6cdc0ccc 1663 if (isym->st_shndx == SHN_UNDEF)
252b5132 1664 sec = bfd_und_section_ptr;
6cdc0ccc 1665 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
252b5132 1666 {
6cdc0ccc 1667 sec = section_from_elf_index (abfd, isym->st_shndx);
252b5132
RH
1668 if (sec == NULL)
1669 sec = bfd_abs_section_ptr;
1670 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1671 value -= sec->vma;
1672 }
6cdc0ccc 1673 else if (isym->st_shndx == SHN_ABS)
252b5132 1674 sec = bfd_abs_section_ptr;
6cdc0ccc 1675 else if (isym->st_shndx == SHN_COMMON)
252b5132
RH
1676 {
1677 sec = bfd_com_section_ptr;
1678 /* What ELF calls the size we call the value. What ELF
1679 calls the value we call the alignment. */
6cdc0ccc 1680 value = isym->st_size;
252b5132
RH
1681 }
1682 else
1683 {
1684 /* Leave it up to the processor backend. */
1685 }
1686
6cdc0ccc
AM
1687 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
1688 isym->st_name);
252b5132 1689 if (name == (const char *) NULL)
6cdc0ccc 1690 goto error_free_vers;
252b5132 1691
6cdc0ccc
AM
1692 if (isym->st_shndx == SHN_COMMON
1693 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
13ae64f3
JJ
1694 {
1695 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
1696
1697 if (tcomm == NULL)
1698 {
1699 tcomm = bfd_make_section (abfd, ".tcommon");
1700 if (tcomm == NULL
1701 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
1702 | SEC_IS_COMMON
1703 | SEC_LINKER_CREATED
1704 | SEC_THREAD_LOCAL)))
6cdc0ccc 1705 goto error_free_vers;
13ae64f3
JJ
1706 }
1707 sec = tcomm;
1708 }
1709 else if (add_symbol_hook)
252b5132 1710 {
6cdc0ccc 1711 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
252b5132 1712 &value))
6cdc0ccc 1713 goto error_free_vers;
252b5132
RH
1714
1715 /* The hook function sets the name to NULL if this symbol
1716 should be skipped for some reason. */
1717 if (name == (const char *) NULL)
1718 continue;
1719 }
1720
1721 /* Sanity check that all possibilities were handled. */
1722 if (sec == (asection *) NULL)
1723 {
1724 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 1725 goto error_free_vers;
252b5132
RH
1726 }
1727
1728 if (bfd_is_und_section (sec)
1729 || bfd_is_com_section (sec))
b34976b6 1730 definition = FALSE;
252b5132 1731 else
b34976b6 1732 definition = TRUE;
252b5132 1733
b34976b6 1734 size_change_ok = FALSE;
252b5132
RH
1735 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
1736 old_alignment = 0;
f7661995
NC
1737 old_bfd = NULL;
1738
252b5132
RH
1739 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1740 {
1741 Elf_Internal_Versym iver;
1742 unsigned int vernum = 0;
252b5132
RH
1743
1744 if (ever != NULL)
1745 {
1746 _bfd_elf_swap_versym_in (abfd, ever, &iver);
1747 vernum = iver.vs_vers & VERSYM_VERSION;
1748
1749 /* If this is a hidden symbol, or if it is not version
c44233aa
AM
1750 1, we append the version name to the symbol name.
1751 However, we do not modify a non-hidden absolute
1752 symbol, because it might be the version symbol
1753 itself. FIXME: What if it isn't? */
252b5132
RH
1754 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1755 || (vernum > 1 && ! bfd_is_abs_section (sec)))
1756 {
1757 const char *verstr;
d4c88bbb 1758 size_t namelen, verlen, newlen;
252b5132
RH
1759 char *newname, *p;
1760
6cdc0ccc 1761 if (isym->st_shndx != SHN_UNDEF)
252b5132
RH
1762 {
1763 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1764 {
1765 (*_bfd_error_handler)
1766 (_("%s: %s: invalid version %u (max %d)"),
8f615d07 1767 bfd_archive_filename (abfd), name, vernum,
252b5132
RH
1768 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1769 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 1770 goto error_free_vers;
252b5132
RH
1771 }
1772 else if (vernum > 1)
1773 verstr =
1774 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1775 else
1776 verstr = "";
1777 }
1778 else
1779 {
1780 /* We cannot simply test for the number of
1781 entries in the VERNEED section since the
1782 numbers for the needed versions do not start
1783 at 0. */
1784 Elf_Internal_Verneed *t;
1785
1786 verstr = NULL;
1787 for (t = elf_tdata (abfd)->verref;
1788 t != NULL;
1789 t = t->vn_nextref)
1790 {
1791 Elf_Internal_Vernaux *a;
1792
1793 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1794 {
1795 if (a->vna_other == vernum)
1796 {
1797 verstr = a->vna_nodename;
1798 break;
1799 }
1800 }
1801 if (a != NULL)
1802 break;
1803 }
1804 if (verstr == NULL)
1805 {
1806 (*_bfd_error_handler)
1807 (_("%s: %s: invalid needed version %d"),
8f615d07 1808 bfd_archive_filename (abfd), name, vernum);
252b5132 1809 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 1810 goto error_free_vers;
252b5132
RH
1811 }
1812 }
1813
1814 namelen = strlen (name);
d4c88bbb
AM
1815 verlen = strlen (verstr);
1816 newlen = namelen + verlen + 2;
1817 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
6cdc0ccc 1818 && isym->st_shndx != SHN_UNDEF)
252b5132
RH
1819 ++newlen;
1820
d4c88bbb 1821 newname = (char *) bfd_alloc (abfd, (bfd_size_type) newlen);
252b5132 1822 if (newname == NULL)
6cdc0ccc 1823 goto error_free_vers;
d4c88bbb 1824 memcpy (newname, name, namelen);
252b5132
RH
1825 p = newname + namelen;
1826 *p++ = ELF_VER_CHR;
1287d1cc
ILT
1827 /* If this is a defined non-hidden version symbol,
1828 we add another @ to the name. This indicates the
1829 default version of the symbol. */
1830 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
6cdc0ccc 1831 && isym->st_shndx != SHN_UNDEF)
252b5132 1832 *p++ = ELF_VER_CHR;
d4c88bbb 1833 memcpy (p, verstr, verlen + 1);
252b5132
RH
1834
1835 name = newname;
1836 }
1837 }
1838
6cdc0ccc 1839 if (! elf_merge_symbol (abfd, info, name, isym, &sec, &value,
252b5132 1840 sym_hash, &override, &type_change_ok,
456981d7 1841 &size_change_ok, dt_needed))
6cdc0ccc 1842 goto error_free_vers;
252b5132
RH
1843
1844 if (override)
b34976b6 1845 definition = FALSE;
252b5132
RH
1846
1847 h = *sym_hash;
1848 while (h->root.type == bfd_link_hash_indirect
1849 || h->root.type == bfd_link_hash_warning)
1850 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1851
1852 /* Remember the old alignment if this is a common symbol, so
c44233aa
AM
1853 that we don't reduce the alignment later on. We can't
1854 check later, because _bfd_generic_link_add_one_symbol
1855 will set a default for the alignment which we want to
f7661995
NC
1856 override. We also remember the old bfd where the existing
1857 definition comes from. */
1858 switch (h->root.type)
1859 {
1860 default:
1861 break;
1862
1863 case bfd_link_hash_defined:
1864 case bfd_link_hash_defweak:
1865 old_bfd = h->root.u.def.section->owner;
1866 break;
1867
1868 case bfd_link_hash_common:
1869 old_bfd = h->root.u.c.p->section->owner;
1870 old_alignment = h->root.u.c.p->alignment_power;
1871 break;
1872 }
252b5132
RH
1873
1874 if (elf_tdata (abfd)->verdef != NULL
1875 && ! override
1876 && vernum > 1
1877 && definition)
1878 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1879 }
1880
1881 if (! (_bfd_generic_link_add_one_symbol
1882 (info, abfd, name, flags, sec, value, (const char *) NULL,
b34976b6 1883 FALSE, collect, (struct bfd_link_hash_entry **) sym_hash)))
6cdc0ccc 1884 goto error_free_vers;
252b5132
RH
1885
1886 h = *sym_hash;
1887 while (h->root.type == bfd_link_hash_indirect
1888 || h->root.type == bfd_link_hash_warning)
1889 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1890 *sym_hash = h;
1891
b34976b6 1892 new_weakdef = FALSE;
252b5132
RH
1893 if (dynamic
1894 && definition
1895 && (flags & BSF_WEAK) != 0
6cdc0ccc 1896 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
252b5132
RH
1897 && info->hash->creator->flavour == bfd_target_elf_flavour
1898 && h->weakdef == NULL)
1899 {
1900 /* Keep a list of all weak defined non function symbols from
1901 a dynamic object, using the weakdef field. Later in this
1902 function we will set the weakdef field to the correct
1903 value. We only put non-function symbols from dynamic
1904 objects on this list, because that happens to be the only
1905 time we need to know the normal symbol corresponding to a
1906 weak symbol, and the information is time consuming to
1907 figure out. If the weakdef field is not already NULL,
1908 then this symbol was already defined by some previous
1909 dynamic object, and we will be using that previous
1910 definition anyhow. */
1911
1912 h->weakdef = weaks;
1913 weaks = h;
b34976b6 1914 new_weakdef = TRUE;
252b5132
RH
1915 }
1916
1917 /* Set the alignment of a common symbol. */
6cdc0ccc 1918 if (isym->st_shndx == SHN_COMMON
252b5132
RH
1919 && h->root.type == bfd_link_hash_common)
1920 {
1921 unsigned int align;
1922
6cdc0ccc 1923 align = bfd_log2 (isym->st_value);
724982f6
NC
1924 if (align > old_alignment
1925 /* Permit an alignment power of zero if an alignment of one
1926 is specified and no other alignments have been specified. */
6cdc0ccc 1927 || (isym->st_value == 1 && old_alignment == 0))
252b5132 1928 h->root.u.c.p->alignment_power = align;
f7661995
NC
1929 else
1930 h->root.u.c.p->alignment_power = old_alignment;
252b5132
RH
1931 }
1932
1933 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1934 {
1935 int old_flags;
b34976b6 1936 bfd_boolean dynsym;
252b5132
RH
1937 int new_flag;
1938
f7661995
NC
1939 /* Check the alignment when a common symbol is involved. This
1940 can change when a common symbol is overriden by a normal
1941 definition or a common symbol is ignored due to the old
1942 normal definition. We need to make sure the maximum
1943 alignment is maintained. */
1944 if ((old_alignment || isym->st_shndx == SHN_COMMON)
1945 && h->root.type != bfd_link_hash_common)
1946 {
ca4fa240
NC
1947 unsigned int common_align;
1948 unsigned int normal_align;
1949 unsigned int symbol_align;
1950 bfd *normal_bfd;
1951 bfd *common_bfd;
f7661995
NC
1952
1953 symbol_align = ffs (h->root.u.def.value) - 1;
1954 if ((h->root.u.def.section->owner->flags & DYNAMIC) == 0)
1955 {
1956 normal_align = h->root.u.def.section->alignment_power;
1957 if (normal_align > symbol_align)
1958 normal_align = symbol_align;
1959 }
1960 else
1961 normal_align = symbol_align;
1962
1963 if (old_alignment)
ca4fa240
NC
1964 {
1965 common_align = old_alignment;
1966 common_bfd = old_bfd;
1967 normal_bfd = abfd;
1968 }
f7661995 1969 else
ca4fa240
NC
1970 {
1971 common_align = bfd_log2 (isym->st_value);
1972 common_bfd = abfd;
1973 normal_bfd = old_bfd;
1974 }
f7661995
NC
1975
1976 if (normal_align < common_align)
1977 (*_bfd_error_handler)
1978 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
ca4fa240
NC
1979 1 << normal_align,
1980 name,
1981 bfd_archive_filename (normal_bfd),
1982 1 << common_align,
1983 bfd_archive_filename (common_bfd));
f7661995
NC
1984 }
1985
252b5132 1986 /* Remember the symbol size and type. */
6cdc0ccc 1987 if (isym->st_size != 0
252b5132
RH
1988 && (definition || h->size == 0))
1989 {
6cdc0ccc 1990 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
252b5132 1991 (*_bfd_error_handler)
f7661995 1992 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
6cdc0ccc 1993 name, (unsigned long) h->size,
f7661995
NC
1994 bfd_archive_filename (old_bfd),
1995 (unsigned long) isym->st_size,
1996 bfd_archive_filename (abfd));
252b5132 1997
6cdc0ccc 1998 h->size = isym->st_size;
252b5132
RH
1999 }
2000
2001 /* If this is a common symbol, then we always want H->SIZE
c44233aa
AM
2002 to be the size of the common symbol. The code just above
2003 won't fix the size if a common symbol becomes larger. We
2004 don't warn about a size change here, because that is
2005 covered by --warn-common. */
252b5132
RH
2006 if (h->root.type == bfd_link_hash_common)
2007 h->size = h->root.u.c.size;
2008
6cdc0ccc 2009 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
252b5132
RH
2010 && (definition || h->type == STT_NOTYPE))
2011 {
2012 if (h->type != STT_NOTYPE
6cdc0ccc 2013 && h->type != ELF_ST_TYPE (isym->st_info)
252b5132
RH
2014 && ! type_change_ok)
2015 (*_bfd_error_handler)
2016 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
6cdc0ccc 2017 name, h->type, ELF_ST_TYPE (isym->st_info),
8f615d07 2018 bfd_archive_filename (abfd));
252b5132 2019
6cdc0ccc 2020 h->type = ELF_ST_TYPE (isym->st_info);
252b5132
RH
2021 }
2022
7a13edea
NC
2023 /* If st_other has a processor-specific meaning, specific code
2024 might be needed here. */
6cdc0ccc 2025 if (isym->st_other != 0)
7a13edea 2026 {
5941afc1 2027 unsigned char hvis, symvis, other, nvis;
7c25b72c
RH
2028
2029 /* Take the balance of OTHER from the definition. */
2030 other = (definition ? isym->st_other : h->other);
2031 other &= ~ ELF_ST_VISIBILITY (-1);
2032
7a13edea 2033 /* Combine visibilities, using the most constraining one. */
7c25b72c
RH
2034 hvis = ELF_ST_VISIBILITY (h->other);
2035 symvis = ELF_ST_VISIBILITY (isym->st_other);
5941afc1
L
2036 if (! hvis)
2037 nvis = symvis;
2038 else if (! symvis)
2039 nvis = hvis;
2040 else
2041 nvis = hvis < symvis ? hvis : symvis;
7c25b72c 2042
5941afc1 2043 h->other = other | nvis;
7a13edea 2044 }
252b5132
RH
2045
2046 /* Set a flag in the hash table entry indicating the type of
2047 reference or definition we just found. Keep a count of
2048 the number of dynamic symbols we find. A dynamic symbol
2049 is one which is referenced or defined by both a regular
2050 object and a shared object. */
2051 old_flags = h->elf_link_hash_flags;
b34976b6 2052 dynsym = FALSE;
252b5132
RH
2053 if (! dynamic)
2054 {
2055 if (! definition)
2056 {
2057 new_flag = ELF_LINK_HASH_REF_REGULAR;
2058 if (bind != STB_WEAK)
2059 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
2060 }
2061 else
2062 new_flag = ELF_LINK_HASH_DEF_REGULAR;
2063 if (info->shared
2064 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2065 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
b34976b6 2066 dynsym = TRUE;
252b5132
RH
2067 }
2068 else
2069 {
2070 if (! definition)
2071 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
2072 else
2073 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
2074 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
2075 | ELF_LINK_HASH_REF_REGULAR)) != 0
2076 || (h->weakdef != NULL
2077 && ! new_weakdef
2078 && h->weakdef->dynindx != -1))
b34976b6 2079 dynsym = TRUE;
252b5132
RH
2080 }
2081
2082 h->elf_link_hash_flags |= new_flag;
2083
215007a6
L
2084 /* Check to see if we need to add an indirect symbol for
2085 the default name. */
051b8577 2086 if (definition || h->root.type == bfd_link_hash_common)
6cdc0ccc 2087 if (! elf_add_default_symbol (abfd, info, h, name, isym,
215007a6
L
2088 &sec, &value, &dynsym,
2089 override, dt_needed))
6cdc0ccc 2090 goto error_free_vers;
252b5132 2091
9147e853
JJ
2092 if (definition && (abfd->flags & DYNAMIC) == 0)
2093 {
2094 char *p = strchr (name, ELF_VER_CHR);
2095 if (p != NULL && p[1] != ELF_VER_CHR)
2096 {
2097 /* Queue non-default versions so that .symver x, x@FOO
2098 aliases can be checked. */
2099 if (! nondeflt_vers)
2100 {
2101 amt = (isymend - isym + 1)
2102 * sizeof (struct elf_link_hash_entry *);
2103 nondeflt_vers = bfd_malloc (amt);
2104 }
2105 nondeflt_vers [nondeflt_vers_cnt++] = h;
2106 }
2107 }
2108
252b5132
RH
2109 if (dynsym && h->dynindx == -1)
2110 {
2111 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
6cdc0ccc 2112 goto error_free_vers;
252b5132
RH
2113 if (h->weakdef != NULL
2114 && ! new_weakdef
2115 && h->weakdef->dynindx == -1)
2116 {
a7b97311 2117 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
6cdc0ccc 2118 goto error_free_vers;
252b5132
RH
2119 }
2120 }
38048eb9 2121 else if (dynsym && h->dynindx != -1)
0444bdd4
L
2122 /* If the symbol already has a dynamic index, but
2123 visibility says it should not be visible, turn it into
2124 a local symbol. */
2125 switch (ELF_ST_VISIBILITY (h->other))
2126 {
2127 case STV_INTERNAL:
3e932841 2128 case STV_HIDDEN:
b34976b6 2129 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
0444bdd4
L
2130 break;
2131 }
74816898
L
2132
2133 if (dt_needed && definition
2134 && (h->elf_link_hash_flags
2135 & ELF_LINK_HASH_REF_REGULAR) != 0)
2136 {
2137 bfd_size_type oldsize;
2138 bfd_size_type strindex;
2139
8ea2e4bd 2140 if (! is_elf_hash_table (info))
6cdc0ccc 2141 goto error_free_vers;
8ea2e4bd 2142
74816898 2143 /* The symbol from a DT_NEEDED object is referenced from
c44233aa 2144 the regular object to create a dynamic executable. We
3e932841 2145 have to make sure there is a DT_NEEDED entry for it. */
74816898 2146
b34976b6 2147 dt_needed = FALSE;
2b0f7ef9
JJ
2148 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2149 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
b34976b6 2150 elf_dt_soname (abfd), FALSE);
74816898 2151 if (strindex == (bfd_size_type) -1)
6cdc0ccc 2152 goto error_free_vers;
74816898 2153
2b0f7ef9 2154 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
74816898
L
2155 {
2156 asection *sdyn;
2157 Elf_External_Dyn *dyncon, *dynconend;
2158
8ea2e4bd 2159 sdyn = bfd_get_section_by_name (hash_table->dynobj,
74816898
L
2160 ".dynamic");
2161 BFD_ASSERT (sdyn != NULL);
2162
2163 dyncon = (Elf_External_Dyn *) sdyn->contents;
2164 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2165 sdyn->_raw_size);
2166 for (; dyncon < dynconend; dyncon++)
2167 {
2168 Elf_Internal_Dyn dyn;
2169
8ea2e4bd 2170 elf_swap_dyn_in (hash_table->dynobj,
74816898
L
2171 dyncon, &dyn);
2172 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
2173 dyn.d_un.d_val != strindex);
2174 }
2175 }
2176
dc810e39 2177 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
6cdc0ccc 2178 goto error_free_vers;
74816898 2179 }
252b5132
RH
2180 }
2181 }
2182
9147e853
JJ
2183 /* Now that all the symbols from this input file are created, handle
2184 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
2185 if (nondeflt_vers != NULL)
2186 {
2187 bfd_size_type cnt, symidx;
2188
2189 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
2190 {
2191 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
2192 char *shortname, *p;
2193
2194 p = strchr (h->root.root.string, ELF_VER_CHR);
2195 if (p == NULL
2196 || (h->root.type != bfd_link_hash_defined
2197 && h->root.type != bfd_link_hash_defweak))
2198 continue;
2199
2200 amt = p - h->root.root.string;
2201 shortname = bfd_malloc (amt + 1);
2202 memcpy (shortname, h->root.root.string, amt);
2203 shortname[amt] = '\0';
2204
2205 hi = (struct elf_link_hash_entry *)
2206 bfd_link_hash_lookup (info->hash, shortname,
2207 FALSE, FALSE, FALSE);
2208 if (hi != NULL
2209 && hi->root.type == h->root.type
2210 && hi->root.u.def.value == h->root.u.def.value
2211 && hi->root.u.def.section == h->root.u.def.section)
2212 {
2213 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
2214 hi->root.type = bfd_link_hash_indirect;
2215 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
2216 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
2217 sym_hash = elf_sym_hashes (abfd);
2218 if (sym_hash)
2219 for (symidx = 0; symidx < extsymcount; ++symidx)
2220 if (sym_hash[symidx] == hi)
2221 {
2222 sym_hash[symidx] = h;
2223 break;
2224 }
2225 }
2226 free (shortname);
2227 }
2228 free (nondeflt_vers);
2229 nondeflt_vers = NULL;
2230 }
2231
6cdc0ccc
AM
2232 if (extversym != NULL)
2233 {
2234 free (extversym);
2235 extversym = NULL;
2236 }
2237
2238 if (isymbuf != NULL)
2239 free (isymbuf);
2240 isymbuf = NULL;
2241
252b5132
RH
2242 /* Now set the weakdefs field correctly for all the weak defined
2243 symbols we found. The only way to do this is to search all the
2244 symbols. Since we only need the information for non functions in
2245 dynamic objects, that's the only time we actually put anything on
2246 the list WEAKS. We need this information so that if a regular
2247 object refers to a symbol defined weakly in a dynamic object, the
2248 real symbol in the dynamic object is also put in the dynamic
2249 symbols; we also must arrange for both symbols to point to the
2250 same memory location. We could handle the general case of symbol
2251 aliasing, but a general symbol alias can only be generated in
2252 assembler code, handling it correctly would be very time
2253 consuming, and other ELF linkers don't handle general aliasing
2254 either. */
2255 while (weaks != NULL)
2256 {
2257 struct elf_link_hash_entry *hlook;
2258 asection *slook;
2259 bfd_vma vlook;
2260 struct elf_link_hash_entry **hpp;
2261 struct elf_link_hash_entry **hppend;
2262
2263 hlook = weaks;
2264 weaks = hlook->weakdef;
2265 hlook->weakdef = NULL;
2266
2267 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
2268 || hlook->root.type == bfd_link_hash_defweak
2269 || hlook->root.type == bfd_link_hash_common
2270 || hlook->root.type == bfd_link_hash_indirect);
2271 slook = hlook->root.u.def.section;
2272 vlook = hlook->root.u.def.value;
2273
2274 hpp = elf_sym_hashes (abfd);
2275 hppend = hpp + extsymcount;
2276 for (; hpp < hppend; hpp++)
2277 {
2278 struct elf_link_hash_entry *h;
2279
2280 h = *hpp;
2281 if (h != NULL && h != hlook
2282 && h->root.type == bfd_link_hash_defined
2283 && h->root.u.def.section == slook
2284 && h->root.u.def.value == vlook)
2285 {
2286 hlook->weakdef = h;
2287
2288 /* If the weak definition is in the list of dynamic
2289 symbols, make sure the real definition is put there
2290 as well. */
2291 if (hlook->dynindx != -1
2292 && h->dynindx == -1)
2293 {
2294 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2295 goto error_return;
2296 }
2297
2298 /* If the real definition is in the list of dynamic
c44233aa
AM
2299 symbols, make sure the weak definition is put there
2300 as well. If we don't do this, then the dynamic
2301 loader might not merge the entries for the real
2302 definition and the weak definition. */
252b5132
RH
2303 if (h->dynindx != -1
2304 && hlook->dynindx == -1)
2305 {
2306 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
2307 goto error_return;
2308 }
252b5132
RH
2309 break;
2310 }
2311 }
2312 }
2313
252b5132
RH
2314 /* If this object is the same format as the output object, and it is
2315 not a shared library, then let the backend look through the
2316 relocs.
2317
2318 This is required to build global offset table entries and to
2319 arrange for dynamic relocs. It is not required for the
2320 particular common case of linking non PIC code, even when linking
2321 against shared libraries, but unfortunately there is no way of
2322 knowing whether an object file has been compiled PIC or not.
2323 Looking through the relocs is not particularly time consuming.
2324 The problem is that we must either (1) keep the relocs in memory,
2325 which causes the linker to require additional runtime memory or
2326 (2) read the relocs twice from the input file, which wastes time.
2327 This would be a good case for using mmap.
2328
2329 I have no idea how to handle linking PIC code into a file of a
2330 different format. It probably can't be done. */
2331 check_relocs = get_elf_backend_data (abfd)->check_relocs;
2332 if (! dynamic
2333 && abfd->xvec == info->hash->creator
2334 && check_relocs != NULL)
2335 {
2336 asection *o;
2337
2338 for (o = abfd->sections; o != NULL; o = o->next)
2339 {
2340 Elf_Internal_Rela *internal_relocs;
b34976b6 2341 bfd_boolean ok;
252b5132
RH
2342
2343 if ((o->flags & SEC_RELOC) == 0
2344 || o->reloc_count == 0
2345 || ((info->strip == strip_all || info->strip == strip_debugger)
2346 && (o->flags & SEC_DEBUGGING) != 0)
2347 || bfd_is_abs_section (o->output_section))
2348 continue;
2349
2350 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
2351 (abfd, o, (PTR) NULL,
2352 (Elf_Internal_Rela *) NULL,
2353 info->keep_memory));
2354 if (internal_relocs == NULL)
2355 goto error_return;
2356
2357 ok = (*check_relocs) (abfd, info, o, internal_relocs);
2358
6cdc0ccc 2359 if (elf_section_data (o)->relocs != internal_relocs)
252b5132
RH
2360 free (internal_relocs);
2361
2362 if (! ok)
2363 goto error_return;
2364 }
2365 }
2366
d6fe2dc1
AM
2367 /* If this is a non-traditional link, try to optimize the handling
2368 of the .stab/.stabstr sections. */
252b5132 2369 if (! dynamic
252b5132
RH
2370 && ! info->traditional_format
2371 && info->hash->creator->flavour == bfd_target_elf_flavour
8ea2e4bd 2372 && is_elf_hash_table (info)
252b5132
RH
2373 && (info->strip != strip_all && info->strip != strip_debugger))
2374 {
2375 asection *stab, *stabstr;
2376
2377 stab = bfd_get_section_by_name (abfd, ".stab");
2d653fc7
AM
2378 if (stab != NULL
2379 && (stab->flags & SEC_MERGE) == 0
2380 && !bfd_is_abs_section (stab->output_section))
252b5132
RH
2381 {
2382 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
2383
2384 if (stabstr != NULL)
2385 {
2386 struct bfd_elf_section_data *secdata;
2387
2388 secdata = elf_section_data (stab);
2389 if (! _bfd_link_section_stabs (abfd,
8ea2e4bd 2390 & hash_table->stab_info,
252b5132 2391 stab, stabstr,
65765700 2392 &secdata->sec_info))
252b5132 2393 goto error_return;
65765700 2394 if (secdata->sec_info)
68bfbfcc 2395 stab->sec_info_type = ELF_INFO_TYPE_STABS;
252b5132
RH
2396 }
2397 }
2398 }
2399
8ea2e4bd
NC
2400 if (! info->relocateable && ! dynamic
2401 && is_elf_hash_table (info))
f5fa8ca2
JJ
2402 {
2403 asection *s;
2404
2405 for (s = abfd->sections; s != NULL; s = s->next)
2d653fc7
AM
2406 if ((s->flags & SEC_MERGE) != 0
2407 && !bfd_is_abs_section (s->output_section))
65765700
JJ
2408 {
2409 struct bfd_elf_section_data *secdata;
2410
2411 secdata = elf_section_data (s);
2412 if (! _bfd_merge_section (abfd,
2413 & hash_table->merge_info,
2414 s, &secdata->sec_info))
2415 goto error_return;
2416 else if (secdata->sec_info)
68bfbfcc 2417 s->sec_info_type = ELF_INFO_TYPE_MERGE;
65765700 2418 }
f5fa8ca2
JJ
2419 }
2420
f5d44ba0
AM
2421 if (is_elf_hash_table (info))
2422 {
2423 /* Add this bfd to the loaded list. */
2424 struct elf_link_loaded_list *n;
2425
2426 n = ((struct elf_link_loaded_list *)
2427 bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)));
2428 if (n == NULL)
2429 goto error_return;
2430 n->abfd = abfd;
2431 n->next = hash_table->loaded;
2432 hash_table->loaded = n;
2433 }
2434
b34976b6 2435 return TRUE;
252b5132 2436
6cdc0ccc 2437 error_free_vers:
9147e853
JJ
2438 if (nondeflt_vers != NULL)
2439 free (nondeflt_vers);
252b5132
RH
2440 if (extversym != NULL)
2441 free (extversym);
6cdc0ccc
AM
2442 error_free_sym:
2443 if (isymbuf != NULL)
2444 free (isymbuf);
2445 error_return:
b34976b6 2446 return FALSE;
252b5132
RH
2447}
2448
2449/* Create some sections which will be filled in with dynamic linking
2450 information. ABFD is an input file which requires dynamic sections
2451 to be created. The dynamic sections take up virtual memory space
2452 when the final executable is run, so we need to create them before
2453 addresses are assigned to the output sections. We work out the
2454 actual contents and size of these sections later. */
2455
b34976b6 2456bfd_boolean
252b5132
RH
2457elf_link_create_dynamic_sections (abfd, info)
2458 bfd *abfd;
2459 struct bfd_link_info *info;
2460{
2461 flagword flags;
2462 register asection *s;
2463 struct elf_link_hash_entry *h;
14a793b2 2464 struct bfd_link_hash_entry *bh;
252b5132
RH
2465 struct elf_backend_data *bed;
2466
8ea2e4bd 2467 if (! is_elf_hash_table (info))
b34976b6 2468 return FALSE;
8ea2e4bd 2469
252b5132 2470 if (elf_hash_table (info)->dynamic_sections_created)
b34976b6 2471 return TRUE;
252b5132
RH
2472
2473 /* Make sure that all dynamic sections use the same input BFD. */
2474 if (elf_hash_table (info)->dynobj == NULL)
2475 elf_hash_table (info)->dynobj = abfd;
2476 else
2477 abfd = elf_hash_table (info)->dynobj;
2478
2479 /* Note that we set the SEC_IN_MEMORY flag for all of these
2480 sections. */
2481 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2482 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2483
2484 /* A dynamically linked executable has a .interp section, but a
2485 shared library does not. */
2486 if (! info->shared)
2487 {
2488 s = bfd_make_section (abfd, ".interp");
2489 if (s == NULL
2490 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
b34976b6 2491 return FALSE;
252b5132
RH
2492 }
2493
65765700
JJ
2494 if (! info->traditional_format
2495 && info->hash->creator->flavour == bfd_target_elf_flavour)
2496 {
2497 s = bfd_make_section (abfd, ".eh_frame_hdr");
2498 if (s == NULL
2499 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2500 || ! bfd_set_section_alignment (abfd, s, 2))
b34976b6 2501 return FALSE;
126495ed 2502 elf_hash_table (info)->eh_info.hdr_sec = s;
65765700
JJ
2503 }
2504
252b5132
RH
2505 /* Create sections to hold version informations. These are removed
2506 if they are not needed. */
2507 s = bfd_make_section (abfd, ".gnu.version_d");
2508 if (s == NULL
2509 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2510 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
b34976b6 2511 return FALSE;
252b5132
RH
2512
2513 s = bfd_make_section (abfd, ".gnu.version");
2514 if (s == NULL
2515 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2516 || ! bfd_set_section_alignment (abfd, s, 1))
b34976b6 2517 return FALSE;
252b5132
RH
2518
2519 s = bfd_make_section (abfd, ".gnu.version_r");
2520 if (s == NULL
2521 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2522 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
b34976b6 2523 return FALSE;
252b5132
RH
2524
2525 s = bfd_make_section (abfd, ".dynsym");
2526 if (s == NULL
2527 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2528 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
b34976b6 2529 return FALSE;
252b5132
RH
2530
2531 s = bfd_make_section (abfd, ".dynstr");
2532 if (s == NULL
2533 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
b34976b6 2534 return FALSE;
252b5132
RH
2535
2536 /* Create a strtab to hold the dynamic symbol names. */
2537 if (elf_hash_table (info)->dynstr == NULL)
2538 {
2b0f7ef9 2539 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
252b5132 2540 if (elf_hash_table (info)->dynstr == NULL)
b34976b6 2541 return FALSE;
252b5132
RH
2542 }
2543
2544 s = bfd_make_section (abfd, ".dynamic");
2545 if (s == NULL
2546 || ! bfd_set_section_flags (abfd, s, flags)
2547 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
b34976b6 2548 return FALSE;
252b5132
RH
2549
2550 /* The special symbol _DYNAMIC is always set to the start of the
2551 .dynamic section. This call occurs before we have processed the
2552 symbols for any dynamic object, so we don't have to worry about
2553 overriding a dynamic definition. We could set _DYNAMIC in a
2554 linker script, but we only want to define it if we are, in fact,
2555 creating a .dynamic section. We don't want to define it if there
2556 is no .dynamic section, since on some ELF platforms the start up
2557 code examines it to decide how to initialize the process. */
14a793b2 2558 bh = NULL;
252b5132
RH
2559 if (! (_bfd_generic_link_add_one_symbol
2560 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
b34976b6
AM
2561 (const char *) 0, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
2562 return FALSE;
14a793b2 2563 h = (struct elf_link_hash_entry *) bh;
252b5132
RH
2564 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2565 h->type = STT_OBJECT;
2566
2567 if (info->shared
2568 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2569 return FALSE;
252b5132 2570
c7ac6ff8
MM
2571 bed = get_elf_backend_data (abfd);
2572
252b5132
RH
2573 s = bfd_make_section (abfd, ".hash");
2574 if (s == NULL
2575 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2576 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
b34976b6 2577 return FALSE;
c7ac6ff8 2578 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
252b5132
RH
2579
2580 /* Let the backend create the rest of the sections. This lets the
2581 backend set the right flags. The backend will normally create
2582 the .got and .plt sections. */
252b5132 2583 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
b34976b6 2584 return FALSE;
252b5132 2585
b34976b6 2586 elf_hash_table (info)->dynamic_sections_created = TRUE;
252b5132 2587
b34976b6 2588 return TRUE;
252b5132
RH
2589}
2590
2591/* Add an entry to the .dynamic table. */
2592
b34976b6 2593bfd_boolean
252b5132
RH
2594elf_add_dynamic_entry (info, tag, val)
2595 struct bfd_link_info *info;
2596 bfd_vma tag;
2597 bfd_vma val;
2598{
2599 Elf_Internal_Dyn dyn;
2600 bfd *dynobj;
2601 asection *s;
dc810e39 2602 bfd_size_type newsize;
252b5132
RH
2603 bfd_byte *newcontents;
2604
8ea2e4bd 2605 if (! is_elf_hash_table (info))
b34976b6 2606 return FALSE;
8ea2e4bd 2607
252b5132
RH
2608 dynobj = elf_hash_table (info)->dynobj;
2609
2610 s = bfd_get_section_by_name (dynobj, ".dynamic");
2611 BFD_ASSERT (s != NULL);
2612
2613 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2614 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2615 if (newcontents == NULL)
b34976b6 2616 return FALSE;
252b5132
RH
2617
2618 dyn.d_tag = tag;
2619 dyn.d_un.d_val = val;
2620 elf_swap_dyn_out (dynobj, &dyn,
2621 (Elf_External_Dyn *) (newcontents + s->_raw_size));
2622
2623 s->_raw_size = newsize;
2624 s->contents = newcontents;
2625
b34976b6 2626 return TRUE;
252b5132
RH
2627}
2628\f
6b5bd373
MM
2629/* Read and swap the relocs from the section indicated by SHDR. This
2630 may be either a REL or a RELA section. The relocations are
2631 translated into RELA relocations and stored in INTERNAL_RELOCS,
2632 which should have already been allocated to contain enough space.
2633 The EXTERNAL_RELOCS are a buffer where the external form of the
2634 relocations should be stored.
2635
b34976b6 2636 Returns FALSE if something goes wrong. */
6b5bd373 2637
b34976b6 2638static bfd_boolean
6b5bd373
MM
2639elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2640 internal_relocs)
2641 bfd *abfd;
2642 Elf_Internal_Shdr *shdr;
2643 PTR external_relocs;
2644 Elf_Internal_Rela *internal_relocs;
2645{
c7ac6ff8 2646 struct elf_backend_data *bed;
947216bf
AM
2647 void (*swap_in) PARAMS ((bfd *, const bfd_byte *, Elf_Internal_Rela *));
2648 const bfd_byte *erela;
2649 const bfd_byte *erelaend;
2650 Elf_Internal_Rela *irela;
c7ac6ff8 2651
6b5bd373
MM
2652 /* If there aren't any relocations, that's OK. */
2653 if (!shdr)
b34976b6 2654 return TRUE;
6b5bd373
MM
2655
2656 /* Position ourselves at the start of the section. */
2657 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
b34976b6 2658 return FALSE;
6b5bd373
MM
2659
2660 /* Read the relocations. */
dc810e39 2661 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
b34976b6 2662 return FALSE;
6b5bd373 2663
c7ac6ff8
MM
2664 bed = get_elf_backend_data (abfd);
2665
6b5bd373
MM
2666 /* Convert the external relocations to the internal format. */
2667 if (shdr->sh_entsize == sizeof (Elf_External_Rel))
947216bf
AM
2668 swap_in = bed->s->swap_reloc_in;
2669 else if (shdr->sh_entsize == sizeof (Elf_External_Rela))
2670 swap_in = bed->s->swap_reloca_in;
6b5bd373 2671 else
4fabe71e
AM
2672 {
2673 bfd_set_error (bfd_error_wrong_format);
2674 return FALSE;
2675 }
6b5bd373 2676
947216bf
AM
2677 erela = external_relocs;
2678 erelaend = erela + NUM_SHDR_ENTRIES (shdr) * shdr->sh_entsize;
2679 irela = internal_relocs;
2680 while (erela < erelaend)
2681 {
2682 (*swap_in) (abfd, erela, irela);
2683 irela += bed->s->int_rels_per_ext_rel;
2684 erela += shdr->sh_entsize;
6b5bd373
MM
2685 }
2686
b34976b6 2687 return TRUE;
6b5bd373
MM
2688}
2689
23bc299b
MM
2690/* Read and swap the relocs for a section O. They may have been
2691 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2692 not NULL, they are used as buffers to read into. They are known to
2693 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2694 the return value is allocated using either malloc or bfd_alloc,
2695 according to the KEEP_MEMORY argument. If O has two relocation
2696 sections (both REL and RELA relocations), then the REL_HDR
2697 relocations will appear first in INTERNAL_RELOCS, followed by the
2698 REL_HDR2 relocations. */
252b5132
RH
2699
2700Elf_Internal_Rela *
2701NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2702 keep_memory)
2703 bfd *abfd;
2704 asection *o;
2705 PTR external_relocs;
2706 Elf_Internal_Rela *internal_relocs;
b34976b6 2707 bfd_boolean keep_memory;
252b5132
RH
2708{
2709 Elf_Internal_Shdr *rel_hdr;
2710 PTR alloc1 = NULL;
2711 Elf_Internal_Rela *alloc2 = NULL;
c7ac6ff8 2712 struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
2713
2714 if (elf_section_data (o)->relocs != NULL)
2715 return elf_section_data (o)->relocs;
2716
2717 if (o->reloc_count == 0)
2718 return NULL;
2719
2720 rel_hdr = &elf_section_data (o)->rel_hdr;
2721
2722 if (internal_relocs == NULL)
2723 {
dc810e39 2724 bfd_size_type size;
252b5132 2725
dc810e39
AM
2726 size = o->reloc_count;
2727 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
252b5132
RH
2728 if (keep_memory)
2729 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2730 else
2731 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2732 if (internal_relocs == NULL)
2733 goto error_return;
2734 }
2735
2736 if (external_relocs == NULL)
2737 {
dc810e39 2738 bfd_size_type size = rel_hdr->sh_size;
6b5bd373
MM
2739
2740 if (elf_section_data (o)->rel_hdr2)
dc810e39 2741 size += elf_section_data (o)->rel_hdr2->sh_size;
6b5bd373 2742 alloc1 = (PTR) bfd_malloc (size);
252b5132
RH
2743 if (alloc1 == NULL)
2744 goto error_return;
2745 external_relocs = alloc1;
2746 }
2747
6b5bd373
MM
2748 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2749 external_relocs,
2750 internal_relocs))
2751 goto error_return;
3e932841
KH
2752 if (!elf_link_read_relocs_from_section
2753 (abfd,
6b5bd373 2754 elf_section_data (o)->rel_hdr2,
2f5116e2 2755 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
d9bc7a44 2756 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
c7ac6ff8 2757 * bed->s->int_rels_per_ext_rel)))
252b5132 2758 goto error_return;
252b5132
RH
2759
2760 /* Cache the results for next time, if we can. */
2761 if (keep_memory)
2762 elf_section_data (o)->relocs = internal_relocs;
2763
2764 if (alloc1 != NULL)
2765 free (alloc1);
2766
2767 /* Don't free alloc2, since if it was allocated we are passing it
2768 back (under the name of internal_relocs). */
2769
2770 return internal_relocs;
2771
2772 error_return:
2773 if (alloc1 != NULL)
2774 free (alloc1);
2775 if (alloc2 != NULL)
2776 free (alloc2);
2777 return NULL;
2778}
2779\f
252b5132
RH
2780/* Record an assignment to a symbol made by a linker script. We need
2781 this in case some dynamic object refers to this symbol. */
2782
b34976b6 2783bfd_boolean
252b5132 2784NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
7442e600 2785 bfd *output_bfd ATTRIBUTE_UNUSED;
252b5132
RH
2786 struct bfd_link_info *info;
2787 const char *name;
b34976b6 2788 bfd_boolean provide;
252b5132
RH
2789{
2790 struct elf_link_hash_entry *h;
2791
2792 if (info->hash->creator->flavour != bfd_target_elf_flavour)
b34976b6 2793 return TRUE;
252b5132 2794
b34976b6 2795 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
252b5132 2796 if (h == NULL)
b34976b6 2797 return FALSE;
252b5132
RH
2798
2799 if (h->root.type == bfd_link_hash_new)
a7b97311 2800 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
252b5132
RH
2801
2802 /* If this symbol is being provided by the linker script, and it is
2803 currently defined by a dynamic object, but not by a regular
2804 object, then mark it as undefined so that the generic linker will
2805 force the correct value. */
2806 if (provide
2807 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2808 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2809 h->root.type = bfd_link_hash_undefined;
2810
2811 /* If this symbol is not being provided by the linker script, and it is
2812 currently defined by a dynamic object, but not by a regular object,
2813 then clear out any version information because the symbol will not be
2814 associated with the dynamic object any more. */
2815 if (!provide
2816 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2817 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2818 h->verinfo.verdef = NULL;
2819
2820 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
994819d2 2821
252b5132
RH
2822 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2823 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2824 || info->shared)
2825 && h->dynindx == -1)
2826 {
2827 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2828 return FALSE;
252b5132
RH
2829
2830 /* If this is a weak defined symbol, and we know a corresponding
2831 real symbol from the same dynamic object, make sure the real
2832 symbol is also made into a dynamic symbol. */
2833 if (h->weakdef != NULL
2834 && h->weakdef->dynindx == -1)
2835 {
2836 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
b34976b6 2837 return FALSE;
252b5132
RH
2838 }
2839 }
2840
b34976b6 2841 return TRUE;
252b5132
RH
2842}
2843\f
2844/* This structure is used to pass information to
2845 elf_link_assign_sym_version. */
2846
2847struct elf_assign_sym_version_info
2848{
2849 /* Output BFD. */
2850 bfd *output_bfd;
2851 /* General link information. */
2852 struct bfd_link_info *info;
2853 /* Version tree. */
2854 struct bfd_elf_version_tree *verdefs;
252b5132 2855 /* Whether we had a failure. */
b34976b6 2856 bfd_boolean failed;
252b5132
RH
2857};
2858
2859/* This structure is used to pass information to
2860 elf_link_find_version_dependencies. */
2861
2862struct elf_find_verdep_info
2863{
2864 /* Output BFD. */
2865 bfd *output_bfd;
2866 /* General link information. */
2867 struct bfd_link_info *info;
2868 /* The number of dependencies. */
2869 unsigned int vers;
2870 /* Whether we had a failure. */
b34976b6 2871 bfd_boolean failed;
252b5132
RH
2872};
2873
2874/* Array used to determine the number of hash table buckets to use
2875 based on the number of symbols there are. If there are fewer than
2876 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2877 fewer than 37 we use 17 buckets, and so forth. We never use more
2878 than 32771 buckets. */
2879
2880static const size_t elf_buckets[] =
2881{
2882 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2883 16411, 32771, 0
2884};
2885
2886/* Compute bucket count for hashing table. We do not use a static set
2887 of possible tables sizes anymore. Instead we determine for all
2888 possible reasonable sizes of the table the outcome (i.e., the
2889 number of collisions etc) and choose the best solution. The
2890 weighting functions are not too simple to allow the table to grow
2891 without bounds. Instead one of the weighting factors is the size.
2892 Therefore the result is always a good payoff between few collisions
2893 (= short chain lengths) and table size. */
2894static size_t
2895compute_bucket_count (info)
2896 struct bfd_link_info *info;
2897{
2898 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
7442e600 2899 size_t best_size = 0;
252b5132
RH
2900 unsigned long int *hashcodes;
2901 unsigned long int *hashcodesp;
2902 unsigned long int i;
dc810e39 2903 bfd_size_type amt;
252b5132
RH
2904
2905 /* Compute the hash values for all exported symbols. At the same
2906 time store the values in an array so that we could use them for
2907 optimizations. */
dc810e39
AM
2908 amt = dynsymcount;
2909 amt *= sizeof (unsigned long int);
2910 hashcodes = (unsigned long int *) bfd_malloc (amt);
252b5132
RH
2911 if (hashcodes == NULL)
2912 return 0;
2913 hashcodesp = hashcodes;
2914
2915 /* Put all hash values in HASHCODES. */
2916 elf_link_hash_traverse (elf_hash_table (info),
2917 elf_collect_hash_codes, &hashcodesp);
2918
58821868
AM
2919 /* We have a problem here. The following code to optimize the table
2920 size requires an integer type with more the 32 bits. If
2921 BFD_HOST_U_64_BIT is set we know about such a type. */
252b5132 2922#ifdef BFD_HOST_U_64_BIT
82e51918 2923 if (info->optimize)
252b5132
RH
2924 {
2925 unsigned long int nsyms = hashcodesp - hashcodes;
2926 size_t minsize;
2927 size_t maxsize;
2928 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2929 unsigned long int *counts ;
2930
2931 /* Possible optimization parameters: if we have NSYMS symbols we say
2932 that the hashing table must at least have NSYMS/4 and at most
2933 2*NSYMS buckets. */
2934 minsize = nsyms / 4;
2935 if (minsize == 0)
2936 minsize = 1;
2937 best_size = maxsize = nsyms * 2;
2938
2939 /* Create array where we count the collisions in. We must use bfd_malloc
2940 since the size could be large. */
dc810e39
AM
2941 amt = maxsize;
2942 amt *= sizeof (unsigned long int);
2943 counts = (unsigned long int *) bfd_malloc (amt);
252b5132
RH
2944 if (counts == NULL)
2945 {
2946 free (hashcodes);
2947 return 0;
2948 }
2949
2950 /* Compute the "optimal" size for the hash table. The criteria is a
2951 minimal chain length. The minor criteria is (of course) the size
2952 of the table. */
2953 for (i = minsize; i < maxsize; ++i)
2954 {
2955 /* Walk through the array of hashcodes and count the collisions. */
2956 BFD_HOST_U_64_BIT max;
2957 unsigned long int j;
2958 unsigned long int fact;
2959
2960 memset (counts, '\0', i * sizeof (unsigned long int));
2961
2962 /* Determine how often each hash bucket is used. */
2963 for (j = 0; j < nsyms; ++j)
2964 ++counts[hashcodes[j] % i];
2965
2966 /* For the weight function we need some information about the
2967 pagesize on the target. This is information need not be 100%
2968 accurate. Since this information is not available (so far) we
2969 define it here to a reasonable default value. If it is crucial
2970 to have a better value some day simply define this value. */
2971# ifndef BFD_TARGET_PAGESIZE
2972# define BFD_TARGET_PAGESIZE (4096)
2973# endif
2974
2975 /* We in any case need 2 + NSYMS entries for the size values and
2976 the chains. */
2977 max = (2 + nsyms) * (ARCH_SIZE / 8);
2978
2979# if 1
2980 /* Variant 1: optimize for short chains. We add the squares
2981 of all the chain lengths (which favous many small chain
2982 over a few long chains). */
2983 for (j = 0; j < i; ++j)
2984 max += counts[j] * counts[j];
2985
2986 /* This adds penalties for the overall size of the table. */
2987 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2988 max *= fact * fact;
2989# else
2990 /* Variant 2: Optimize a lot more for small table. Here we
2991 also add squares of the size but we also add penalties for
2992 empty slots (the +1 term). */
2993 for (j = 0; j < i; ++j)
2994 max += (1 + counts[j]) * (1 + counts[j]);
2995
2996 /* The overall size of the table is considered, but not as
2997 strong as in variant 1, where it is squared. */
2998 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2999 max *= fact;
3000# endif
3001
3002 /* Compare with current best results. */
3003 if (max < best_chlen)
3004 {
3005 best_chlen = max;
3006 best_size = i;
3007 }
3008 }
3009
3010 free (counts);
3011 }
3012 else
3013#endif /* defined (BFD_HOST_U_64_BIT) */
3014 {
3015 /* This is the fallback solution if no 64bit type is available or if we
3016 are not supposed to spend much time on optimizations. We select the
3017 bucket count using a fixed set of numbers. */
3018 for (i = 0; elf_buckets[i] != 0; i++)
3019 {
3020 best_size = elf_buckets[i];
3021 if (dynsymcount < elf_buckets[i + 1])
3022 break;
3023 }
3024 }
3025
3026 /* Free the arrays we needed. */
3027 free (hashcodes);
3028
3029 return best_size;
3030}
3031
3032/* Set up the sizes and contents of the ELF dynamic sections. This is
3033 called by the ELF linker emulation before_allocation routine. We
3034 must set the sizes of the sections before the linker sets the
3035 addresses of the various sections. */
3036
b34976b6 3037bfd_boolean
252b5132 3038NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
99293407 3039 filter_shlib,
252b5132
RH
3040 auxiliary_filters, info, sinterpptr,
3041 verdefs)
3042 bfd *output_bfd;
3043 const char *soname;
3044 const char *rpath;
252b5132
RH
3045 const char *filter_shlib;
3046 const char * const *auxiliary_filters;
3047 struct bfd_link_info *info;
3048 asection **sinterpptr;
3049 struct bfd_elf_version_tree *verdefs;
3050{
3051 bfd_size_type soname_indx;
25e27870 3052 bfd *dynobj;
252b5132 3053 struct elf_backend_data *bed;
252b5132
RH
3054 struct elf_assign_sym_version_info asvinfo;
3055
3056 *sinterpptr = NULL;
3057
3058 soname_indx = (bfd_size_type) -1;
3059
3060 if (info->hash->creator->flavour != bfd_target_elf_flavour)
b34976b6 3061 return TRUE;
252b5132 3062
8ea2e4bd 3063 if (! is_elf_hash_table (info))
b34976b6 3064 return TRUE;
8ea2e4bd 3065
51b64d56
AM
3066 /* Any syms created from now on start with -1 in
3067 got.refcount/offset and plt.refcount/offset. */
5cab59f6 3068 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
51b64d56 3069
252b5132
RH
3070 /* The backend may have to create some sections regardless of whether
3071 we're dynamic or not. */
3072 bed = get_elf_backend_data (output_bfd);
3073 if (bed->elf_backend_always_size_sections
3074 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
b34976b6 3075 return FALSE;
252b5132
RH
3076
3077 dynobj = elf_hash_table (info)->dynobj;
3078
3079 /* If there were no dynamic objects in the link, there is nothing to
3080 do here. */
3081 if (dynobj == NULL)
b34976b6 3082 return TRUE;
252b5132 3083
68f69152 3084 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
b34976b6 3085 return FALSE;
68f69152 3086
252b5132
RH
3087 if (elf_hash_table (info)->dynamic_sections_created)
3088 {
3089 struct elf_info_failed eif;
3090 struct elf_link_hash_entry *h;
fc8c40a0 3091 asection *dynstr;
31941635
L
3092 struct bfd_elf_version_tree *t;
3093 struct bfd_elf_version_expr *d;
b34976b6 3094 bfd_boolean all_defined;
252b5132
RH
3095
3096 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
3097 BFD_ASSERT (*sinterpptr != NULL || info->shared);
3098
3099 if (soname != NULL)
3100 {
2b0f7ef9 3101 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
b34976b6 3102 soname, TRUE);
252b5132 3103 if (soname_indx == (bfd_size_type) -1
dc810e39
AM
3104 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
3105 soname_indx))
b34976b6 3106 return FALSE;
252b5132
RH
3107 }
3108
3109 if (info->symbolic)
3110 {
dc810e39
AM
3111 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
3112 (bfd_vma) 0))
b34976b6 3113 return FALSE;
d6cf2879 3114 info->flags |= DF_SYMBOLIC;
252b5132
RH
3115 }
3116
3117 if (rpath != NULL)
3118 {
3119 bfd_size_type indx;
3120
2b0f7ef9 3121 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
b34976b6 3122 TRUE);
2b0f7ef9
JJ
3123 if (info->new_dtags)
3124 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
252b5132 3125 if (indx == (bfd_size_type) -1
dc810e39 3126 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
c25373b7 3127 || (info->new_dtags
dc810e39
AM
3128 && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
3129 indx)))
b34976b6 3130 return FALSE;
252b5132
RH
3131 }
3132
3133 if (filter_shlib != NULL)
3134 {
3135 bfd_size_type indx;
3136
2b0f7ef9 3137 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
b34976b6 3138 filter_shlib, TRUE);
252b5132 3139 if (indx == (bfd_size_type) -1
dc810e39 3140 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
b34976b6 3141 return FALSE;
252b5132
RH
3142 }
3143
3144 if (auxiliary_filters != NULL)
3145 {
3146 const char * const *p;
3147
3148 for (p = auxiliary_filters; *p != NULL; p++)
3149 {
3150 bfd_size_type indx;
3151
2b0f7ef9 3152 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
b34976b6 3153 *p, TRUE);
252b5132 3154 if (indx == (bfd_size_type) -1
dc810e39
AM
3155 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
3156 indx))
b34976b6 3157 return FALSE;
252b5132
RH
3158 }
3159 }
3160
391a809a 3161 eif.info = info;
bc2b6df7 3162 eif.verdefs = verdefs;
b34976b6 3163 eif.failed = FALSE;
391a809a 3164
ea44b734 3165 /* If we are supposed to export all symbols into the dynamic symbol
c44233aa 3166 table (this is not the normal case), then do so. */
99293407 3167 if (info->export_dynamic)
ea44b734 3168 {
ea44b734 3169 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
c44233aa 3170 (PTR) &eif);
ea44b734 3171 if (eif.failed)
b34976b6 3172 return FALSE;
ea44b734
RH
3173 }
3174
31941635
L
3175 /* Make all global versions with definiton. */
3176 for (t = verdefs; t != NULL; t = t->next)
3177 for (d = t->globals; d != NULL; d = d->next)
3178 if (!d->symver && strchr (d->pattern, '*') == NULL)
3179 {
3180 const char *verstr, *name;
3181 size_t namelen, verlen, newlen;
3182 char *newname, *p;
3183 struct elf_link_hash_entry *newh;
3184
3185 name = d->pattern;
3186 namelen = strlen (name);
3187 verstr = t->name;
3188 verlen = strlen (verstr);
b34976b6 3189 newlen = namelen + verlen + 3;
31941635
L
3190
3191 newname = (char *) bfd_malloc ((bfd_size_type) newlen);
3192 if (newname == NULL)
b34976b6 3193 return FALSE;
31941635
L
3194 memcpy (newname, name, namelen);
3195
3196 /* Check the hidden versioned definition. */
3197 p = newname + namelen;
3198 *p++ = ELF_VER_CHR;
3199 memcpy (p, verstr, verlen + 1);
3200 newh = elf_link_hash_lookup (elf_hash_table (info),
b34976b6
AM
3201 newname, FALSE, FALSE,
3202 FALSE);
31941635
L
3203 if (newh == NULL
3204 || (newh->root.type != bfd_link_hash_defined
3205 && newh->root.type != bfd_link_hash_defweak))
3206 {
3207 /* Check the default versioned definition. */
3208 *p++ = ELF_VER_CHR;
3209 memcpy (p, verstr, verlen + 1);
3210 newh = elf_link_hash_lookup (elf_hash_table (info),
b34976b6
AM
3211 newname, FALSE, FALSE,
3212 FALSE);
31941635
L
3213 }
3214 free (newname);
3215
c828c771
L
3216 /* Mark this version if there is a definition and it is
3217 not defined in a shared object. */
31941635 3218 if (newh != NULL
c828c771
L
3219 && ((newh->elf_link_hash_flags
3220 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
31941635
L
3221 && (newh->root.type == bfd_link_hash_defined
3222 || newh->root.type == bfd_link_hash_defweak))
3223 d->symver = 1;
3224 }
3225
252b5132
RH
3226 /* Attach all the symbols to their version information. */
3227 asvinfo.output_bfd = output_bfd;
3228 asvinfo.info = info;
3229 asvinfo.verdefs = verdefs;
b34976b6 3230 asvinfo.failed = FALSE;
252b5132
RH
3231
3232 elf_link_hash_traverse (elf_hash_table (info),
3233 elf_link_assign_sym_version,
3234 (PTR) &asvinfo);
3235 if (asvinfo.failed)
b34976b6 3236 return FALSE;
252b5132 3237
31941635
L
3238 if (!info->allow_undefined_version)
3239 {
3240 /* Check if all global versions have a definiton. */
b34976b6 3241 all_defined = TRUE;
31941635
L
3242 for (t = verdefs; t != NULL; t = t->next)
3243 for (d = t->globals; d != NULL; d = d->next)
3244 if (!d->symver && !d->script
3245 && strchr (d->pattern, '*') == NULL)
3246 {
3247 (*_bfd_error_handler)
3248 (_("%s: undefined version: %s"),
3249 d->pattern, t->name);
b34976b6 3250 all_defined = FALSE;
31941635
L
3251 }
3252
3253 if (!all_defined)
3254 {
3255 bfd_set_error (bfd_error_bad_value);
b34976b6 3256 return FALSE;
31941635
L
3257 }
3258 }
3259
252b5132
RH
3260 /* Find all symbols which were defined in a dynamic object and make
3261 the backend pick a reasonable value for them. */
252b5132
RH
3262 elf_link_hash_traverse (elf_hash_table (info),
3263 elf_adjust_dynamic_symbol,
3264 (PTR) &eif);
3265 if (eif.failed)
b34976b6 3266 return FALSE;
252b5132
RH
3267
3268 /* Add some entries to the .dynamic section. We fill in some of the
3269 values later, in elf_bfd_final_link, but we must add the entries
3270 now so that we know the final size of the .dynamic section. */
f0c2e336
MM
3271
3272 /* If there are initialization and/or finalization functions to
3273 call then add the corresponding DT_INIT/DT_FINI entries. */
3274 h = (info->init_function
3e932841 3275 ? elf_link_hash_lookup (elf_hash_table (info),
b34976b6
AM
3276 info->init_function, FALSE,
3277 FALSE, FALSE)
f0c2e336 3278 : NULL);
252b5132
RH
3279 if (h != NULL
3280 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3281 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3282 {
dc810e39 3283 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
b34976b6 3284 return FALSE;
252b5132 3285 }
f0c2e336 3286 h = (info->fini_function
3e932841 3287 ? elf_link_hash_lookup (elf_hash_table (info),
b34976b6
AM
3288 info->fini_function, FALSE,
3289 FALSE, FALSE)
f0c2e336 3290 : NULL);
252b5132
RH
3291 if (h != NULL
3292 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3293 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3294 {
dc810e39 3295 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
b34976b6 3296 return FALSE;
252b5132 3297 }
f0c2e336 3298
fa7ea4d8
AM
3299 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
3300 {
3301 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3302 if (info->shared)
3303 {
3304 bfd *sub;
3305 asection *o;
3306
3307 for (sub = info->input_bfds; sub != NULL;
3308 sub = sub->link_next)
3309 for (o = sub->sections; o != NULL; o = o->next)
3310 if (elf_section_data (o)->this_hdr.sh_type
3311 == SHT_PREINIT_ARRAY)
3312 {
3313 (*_bfd_error_handler)
3314 (_("%s: .preinit_array section is not allowed in DSO"),
58821868 3315 bfd_archive_filename (sub));
fa7ea4d8
AM
3316 break;
3317 }
60166579 3318
36b4f6e7 3319 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 3320 return FALSE;
fa7ea4d8
AM
3321 }
3322
3323 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAY,
3324 (bfd_vma) 0)
3325 || !elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAYSZ,
3326 (bfd_vma) 0))
b34976b6 3327 return FALSE;
fa7ea4d8
AM
3328 }
3329 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
3330 {
3331 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAY,
3332 (bfd_vma) 0)
3333 || !elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAYSZ,
3334 (bfd_vma) 0))
b34976b6 3335 return FALSE;
fa7ea4d8
AM
3336 }
3337 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
3338 {
3339 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAY,
3340 (bfd_vma) 0)
3341 || !elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAYSZ,
3342 (bfd_vma) 0))
b34976b6 3343 return FALSE;
fa7ea4d8 3344 }
30831527 3345
fc8c40a0
AM
3346 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
3347 /* If .dynstr is excluded from the link, we don't want any of
3348 these tags. Strictly, we should be checking each section
3349 individually; This quick check covers for the case where
3350 someone does a /DISCARD/ : { *(*) }. */
3351 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
3352 {
3353 bfd_size_type strsize;
3354
2b0f7ef9 3355 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
dc810e39
AM
3356 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
3357 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
3358 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
3359 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
3360 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
3361 (bfd_vma) sizeof (Elf_External_Sym)))
b34976b6 3362 return FALSE;
fc8c40a0 3363 }
252b5132
RH
3364 }
3365
3366 /* The backend must work out the sizes of all the other dynamic
3367 sections. */
252b5132
RH
3368 if (bed->elf_backend_size_dynamic_sections
3369 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
b34976b6 3370 return FALSE;
252b5132
RH
3371
3372 if (elf_hash_table (info)->dynamic_sections_created)
3373 {
dc810e39 3374 bfd_size_type dynsymcount;
252b5132
RH
3375 asection *s;
3376 size_t bucketcount = 0;
c7ac6ff8 3377 size_t hash_entry_size;
db6751f2 3378 unsigned int dtagcount;
252b5132
RH
3379
3380 /* Set up the version definition section. */
3381 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3382 BFD_ASSERT (s != NULL);
3383
3384 /* We may have created additional version definitions if we are
c44233aa 3385 just linking a regular application. */
252b5132
RH
3386 verdefs = asvinfo.verdefs;
3387
6b9b879a
JJ
3388 /* Skip anonymous version tag. */
3389 if (verdefs != NULL && verdefs->vernum == 0)
3390 verdefs = verdefs->next;
3391
252b5132 3392 if (verdefs == NULL)
7f8d5fc9 3393 _bfd_strip_section_from_output (info, s);
252b5132
RH
3394 else
3395 {
3396 unsigned int cdefs;
3397 bfd_size_type size;
3398 struct bfd_elf_version_tree *t;
3399 bfd_byte *p;
3400 Elf_Internal_Verdef def;
3401 Elf_Internal_Verdaux defaux;
3402
252b5132
RH
3403 cdefs = 0;
3404 size = 0;
3405
3406 /* Make space for the base version. */
3407 size += sizeof (Elf_External_Verdef);
3408 size += sizeof (Elf_External_Verdaux);
3409 ++cdefs;
3410
3411 for (t = verdefs; t != NULL; t = t->next)
3412 {
3413 struct bfd_elf_version_deps *n;
3414
3415 size += sizeof (Elf_External_Verdef);
3416 size += sizeof (Elf_External_Verdaux);
3417 ++cdefs;
3418
3419 for (n = t->deps; n != NULL; n = n->next)
3420 size += sizeof (Elf_External_Verdaux);
3421 }
3422
3423 s->_raw_size = size;
3424 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3425 if (s->contents == NULL && s->_raw_size != 0)
b34976b6 3426 return FALSE;
252b5132
RH
3427
3428 /* Fill in the version definition section. */
3429
3430 p = s->contents;
3431
3432 def.vd_version = VER_DEF_CURRENT;
3433 def.vd_flags = VER_FLG_BASE;
3434 def.vd_ndx = 1;
3435 def.vd_cnt = 1;
3436 def.vd_aux = sizeof (Elf_External_Verdef);
3437 def.vd_next = (sizeof (Elf_External_Verdef)
3438 + sizeof (Elf_External_Verdaux));
3439
3440 if (soname_indx != (bfd_size_type) -1)
3441 {
2b0f7ef9
JJ
3442 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3443 soname_indx);
3a99b017 3444 def.vd_hash = bfd_elf_hash (soname);
252b5132
RH
3445 defaux.vda_name = soname_indx;
3446 }
3447 else
3448 {
3449 const char *name;
3450 bfd_size_type indx;
3451
96fd004e 3452 name = basename (output_bfd->filename);
3a99b017 3453 def.vd_hash = bfd_elf_hash (name);
2b0f7ef9 3454 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
b34976b6 3455 name, FALSE);
252b5132 3456 if (indx == (bfd_size_type) -1)
b34976b6 3457 return FALSE;
252b5132
RH
3458 defaux.vda_name = indx;
3459 }
3460 defaux.vda_next = 0;
3461
3462 _bfd_elf_swap_verdef_out (output_bfd, &def,
a7b97311 3463 (Elf_External_Verdef *) p);
252b5132
RH
3464 p += sizeof (Elf_External_Verdef);
3465 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3466 (Elf_External_Verdaux *) p);
3467 p += sizeof (Elf_External_Verdaux);
3468
3469 for (t = verdefs; t != NULL; t = t->next)
3470 {
3471 unsigned int cdeps;
3472 struct bfd_elf_version_deps *n;
3473 struct elf_link_hash_entry *h;
14a793b2 3474 struct bfd_link_hash_entry *bh;
252b5132
RH
3475
3476 cdeps = 0;
3477 for (n = t->deps; n != NULL; n = n->next)
3478 ++cdeps;
3479
3480 /* Add a symbol representing this version. */
14a793b2 3481 bh = NULL;
252b5132
RH
3482 if (! (_bfd_generic_link_add_one_symbol
3483 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
b34976b6 3484 (bfd_vma) 0, (const char *) NULL, FALSE,
14a793b2 3485 get_elf_backend_data (dynobj)->collect, &bh)))
b34976b6 3486 return FALSE;
14a793b2 3487 h = (struct elf_link_hash_entry *) bh;
252b5132
RH
3488 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
3489 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3490 h->type = STT_OBJECT;
3491 h->verinfo.vertree = t;
3492
3493 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 3494 return FALSE;
252b5132
RH
3495
3496 def.vd_version = VER_DEF_CURRENT;
3497 def.vd_flags = 0;
3498 if (t->globals == NULL && t->locals == NULL && ! t->used)
3499 def.vd_flags |= VER_FLG_WEAK;
3500 def.vd_ndx = t->vernum + 1;
3501 def.vd_cnt = cdeps + 1;
3a99b017 3502 def.vd_hash = bfd_elf_hash (t->name);
252b5132
RH
3503 def.vd_aux = sizeof (Elf_External_Verdef);
3504 if (t->next != NULL)
3505 def.vd_next = (sizeof (Elf_External_Verdef)
3506 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
3507 else
3508 def.vd_next = 0;
3509
3510 _bfd_elf_swap_verdef_out (output_bfd, &def,
3511 (Elf_External_Verdef *) p);
3512 p += sizeof (Elf_External_Verdef);
3513
3514 defaux.vda_name = h->dynstr_index;
2b0f7ef9
JJ
3515 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3516 h->dynstr_index);
252b5132
RH
3517 if (t->deps == NULL)
3518 defaux.vda_next = 0;
3519 else
3520 defaux.vda_next = sizeof (Elf_External_Verdaux);
3521 t->name_indx = defaux.vda_name;
3522
3523 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3524 (Elf_External_Verdaux *) p);
3525 p += sizeof (Elf_External_Verdaux);
3526
3527 for (n = t->deps; n != NULL; n = n->next)
3528 {
3529 if (n->version_needed == NULL)
3530 {
3531 /* This can happen if there was an error in the
3532 version script. */
3533 defaux.vda_name = 0;
3534 }
3535 else
2b0f7ef9
JJ
3536 {
3537 defaux.vda_name = n->version_needed->name_indx;
3538 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3539 defaux.vda_name);
3540 }
252b5132
RH
3541 if (n->next == NULL)
3542 defaux.vda_next = 0;
3543 else
3544 defaux.vda_next = sizeof (Elf_External_Verdaux);
3545
3546 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3547 (Elf_External_Verdaux *) p);
3548 p += sizeof (Elf_External_Verdaux);
3549 }
3550 }
3551
dc810e39
AM
3552 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
3553 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
3554 (bfd_vma) cdefs))
b34976b6 3555 return FALSE;
252b5132
RH
3556
3557 elf_tdata (output_bfd)->cverdefs = cdefs;
3558 }
3559
13ae64f3 3560 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
d6cf2879 3561 {
dc810e39 3562 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
b34976b6 3563 return FALSE;
d6cf2879
L
3564 }
3565
4d538889 3566 if (info->flags_1)
d6cf2879
L
3567 {
3568 if (! info->shared)
3569 info->flags_1 &= ~ (DF_1_INITFIRST
3570 | DF_1_NODELETE
3571 | DF_1_NOOPEN);
dc810e39
AM
3572 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
3573 info->flags_1))
b34976b6 3574 return FALSE;
d6cf2879
L
3575 }
3576
252b5132
RH
3577 /* Work out the size of the version reference section. */
3578
3579 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3580 BFD_ASSERT (s != NULL);
3581 {
3582 struct elf_find_verdep_info sinfo;
3583
3584 sinfo.output_bfd = output_bfd;
3585 sinfo.info = info;
3586 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
3587 if (sinfo.vers == 0)
3588 sinfo.vers = 1;
b34976b6 3589 sinfo.failed = FALSE;
252b5132
RH
3590
3591 elf_link_hash_traverse (elf_hash_table (info),
3592 elf_link_find_version_dependencies,
3593 (PTR) &sinfo);
3594
3595 if (elf_tdata (output_bfd)->verref == NULL)
7f8d5fc9 3596 _bfd_strip_section_from_output (info, s);
252b5132
RH
3597 else
3598 {
3599 Elf_Internal_Verneed *t;
3600 unsigned int size;
3601 unsigned int crefs;
3602 bfd_byte *p;
3603
3604 /* Build the version definition section. */
3605 size = 0;
3606 crefs = 0;
3607 for (t = elf_tdata (output_bfd)->verref;
3608 t != NULL;
3609 t = t->vn_nextref)
3610 {
3611 Elf_Internal_Vernaux *a;
3612
3613 size += sizeof (Elf_External_Verneed);
3614 ++crefs;
3615 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3616 size += sizeof (Elf_External_Vernaux);
3617 }
3618
3619 s->_raw_size = size;
dc810e39 3620 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
252b5132 3621 if (s->contents == NULL)
b34976b6 3622 return FALSE;
252b5132
RH
3623
3624 p = s->contents;
3625 for (t = elf_tdata (output_bfd)->verref;
3626 t != NULL;
3627 t = t->vn_nextref)
3628 {
3629 unsigned int caux;
3630 Elf_Internal_Vernaux *a;
3631 bfd_size_type indx;
3632
3633 caux = 0;
3634 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3635 ++caux;
3636
3637 t->vn_version = VER_NEED_CURRENT;
3638 t->vn_cnt = caux;
2b0f7ef9
JJ
3639 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3640 elf_dt_name (t->vn_bfd) != NULL
3641 ? elf_dt_name (t->vn_bfd)
3642 : basename (t->vn_bfd->filename),
b34976b6 3643 FALSE);
252b5132 3644 if (indx == (bfd_size_type) -1)
b34976b6 3645 return FALSE;
252b5132
RH
3646 t->vn_file = indx;
3647 t->vn_aux = sizeof (Elf_External_Verneed);
3648 if (t->vn_nextref == NULL)
3649 t->vn_next = 0;
3650 else
3651 t->vn_next = (sizeof (Elf_External_Verneed)
3652 + caux * sizeof (Elf_External_Vernaux));
3653
3654 _bfd_elf_swap_verneed_out (output_bfd, t,
3655 (Elf_External_Verneed *) p);
3656 p += sizeof (Elf_External_Verneed);
3657
3658 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3659 {
3a99b017 3660 a->vna_hash = bfd_elf_hash (a->vna_nodename);
2b0f7ef9 3661 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
b34976b6 3662 a->vna_nodename, FALSE);
252b5132 3663 if (indx == (bfd_size_type) -1)
b34976b6 3664 return FALSE;
252b5132
RH
3665 a->vna_name = indx;
3666 if (a->vna_nextptr == NULL)
3667 a->vna_next = 0;
3668 else
3669 a->vna_next = sizeof (Elf_External_Vernaux);
3670
3671 _bfd_elf_swap_vernaux_out (output_bfd, a,
3672 (Elf_External_Vernaux *) p);
3673 p += sizeof (Elf_External_Vernaux);
3674 }
3675 }
3676
dc810e39
AM
3677 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
3678 (bfd_vma) 0)
3679 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
3680 (bfd_vma) crefs))
b34976b6 3681 return FALSE;
252b5132
RH
3682
3683 elf_tdata (output_bfd)->cverrefs = crefs;
3684 }
3685 }
3686
3e932841 3687 /* Assign dynsym indicies. In a shared library we generate a
30b30c21
RH
3688 section symbol for each output section, which come first.
3689 Next come all of the back-end allocated local dynamic syms,
3690 followed by the rest of the global symbols. */
3691
3692 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
252b5132
RH
3693
3694 /* Work out the size of the symbol version section. */
3695 s = bfd_get_section_by_name (dynobj, ".gnu.version");
3696 BFD_ASSERT (s != NULL);
3697 if (dynsymcount == 0
3698 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3699 {
7f8d5fc9 3700 _bfd_strip_section_from_output (info, s);
42751cf3
MM
3701 /* The DYNSYMCOUNT might have changed if we were going to
3702 output a dynamic symbol table entry for S. */
30b30c21 3703 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
252b5132
RH
3704 }
3705 else
3706 {
3707 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3708 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3709 if (s->contents == NULL)
b34976b6 3710 return FALSE;
252b5132 3711
dc810e39 3712 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
b34976b6 3713 return FALSE;
252b5132
RH
3714 }
3715
3716 /* Set the size of the .dynsym and .hash sections. We counted
3717 the number of dynamic symbols in elf_link_add_object_symbols.
3718 We will build the contents of .dynsym and .hash when we build
3719 the final symbol table, because until then we do not know the
3720 correct value to give the symbols. We built the .dynstr
3721 section as we went along in elf_link_add_object_symbols. */
3722 s = bfd_get_section_by_name (dynobj, ".dynsym");
3723 BFD_ASSERT (s != NULL);
3724 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3725 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3726 if (s->contents == NULL && s->_raw_size != 0)
b34976b6 3727 return FALSE;
252b5132 3728
fc8c40a0
AM
3729 if (dynsymcount != 0)
3730 {
3731 Elf_Internal_Sym isym;
3732
3733 /* The first entry in .dynsym is a dummy symbol. */
3734 isym.st_value = 0;
3735 isym.st_size = 0;
3736 isym.st_name = 0;
3737 isym.st_info = 0;
3738 isym.st_other = 0;
3739 isym.st_shndx = 0;
9ad5cbcf 3740 elf_swap_symbol_out (output_bfd, &isym, (PTR) s->contents, (PTR) 0);
fc8c40a0 3741 }
252b5132
RH
3742
3743 /* Compute the size of the hashing table. As a side effect this
3744 computes the hash values for all the names we export. */
3745 bucketcount = compute_bucket_count (info);
3746
3747 s = bfd_get_section_by_name (dynobj, ".hash");
3748 BFD_ASSERT (s != NULL);
c7ac6ff8
MM
3749 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3750 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
1126897b 3751 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
252b5132 3752 if (s->contents == NULL)
b34976b6 3753 return FALSE;
252b5132 3754
dc810e39
AM
3755 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
3756 s->contents);
3757 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
c7ac6ff8 3758 s->contents + hash_entry_size);
252b5132
RH
3759
3760 elf_hash_table (info)->bucketcount = bucketcount;
3761
3762 s = bfd_get_section_by_name (dynobj, ".dynstr");
3763 BFD_ASSERT (s != NULL);
2b0f7ef9
JJ
3764
3765 elf_finalize_dynstr (output_bfd, info);
3766
3767 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
252b5132 3768
db6751f2 3769 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
dc810e39 3770 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
b34976b6 3771 return FALSE;
252b5132
RH
3772 }
3773
b34976b6 3774 return TRUE;
252b5132
RH
3775}
3776\f
2b0f7ef9
JJ
3777/* This function is used to adjust offsets into .dynstr for
3778 dynamic symbols. This is called via elf_link_hash_traverse. */
c44233aa 3779
b34976b6
AM
3780static bfd_boolean elf_adjust_dynstr_offsets
3781 PARAMS ((struct elf_link_hash_entry *, PTR));
c44233aa 3782
b34976b6 3783static bfd_boolean
2b0f7ef9
JJ
3784elf_adjust_dynstr_offsets (h, data)
3785 struct elf_link_hash_entry *h;
3786 PTR data;
3787{
3788 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3789
e92d460e
AM
3790 if (h->root.type == bfd_link_hash_warning)
3791 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3792
2b0f7ef9
JJ
3793 if (h->dynindx != -1)
3794 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
b34976b6 3795 return TRUE;
2b0f7ef9
JJ
3796}
3797
3798/* Assign string offsets in .dynstr, update all structures referencing
3799 them. */
3800
b34976b6 3801static bfd_boolean
2b0f7ef9
JJ
3802elf_finalize_dynstr (output_bfd, info)
3803 bfd *output_bfd;
3804 struct bfd_link_info *info;
3805{
3806 struct elf_link_local_dynamic_entry *entry;
3807 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
3808 bfd *dynobj = elf_hash_table (info)->dynobj;
3809 asection *sdyn;
3810 bfd_size_type size;
3811 Elf_External_Dyn *dyncon, *dynconend;
3812
3813 _bfd_elf_strtab_finalize (dynstr);
3814 size = _bfd_elf_strtab_size (dynstr);
3815
3816 /* Update all .dynamic entries referencing .dynstr strings. */
3817 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3818 BFD_ASSERT (sdyn != NULL);
3819
3820 dyncon = (Elf_External_Dyn *) sdyn->contents;
3821 dynconend = (Elf_External_Dyn *) (sdyn->contents +
3822 sdyn->_raw_size);
3823 for (; dyncon < dynconend; dyncon++)
3824 {
3825 Elf_Internal_Dyn dyn;
3826
3827 elf_swap_dyn_in (dynobj, dyncon, & dyn);
3828 switch (dyn.d_tag)
3829 {
3830 case DT_STRSZ:
3831 dyn.d_un.d_val = size;
3832 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3833 break;
3834 case DT_NEEDED:
3835 case DT_SONAME:
3836 case DT_RPATH:
3837 case DT_RUNPATH:
3838 case DT_FILTER:
3839 case DT_AUXILIARY:
3840 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3841 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3842 break;
3843 default:
3844 break;
3845 }
3846 }
3847
3848 /* Now update local dynamic symbols. */
3849 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
3850 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3851 entry->isym.st_name);
3852
3853 /* And the rest of dynamic symbols. */
3854 elf_link_hash_traverse (elf_hash_table (info),
3855 elf_adjust_dynstr_offsets, dynstr);
3856
3857 /* Adjust version definitions. */
3858 if (elf_tdata (output_bfd)->cverdefs)
3859 {
3860 asection *s;
3861 bfd_byte *p;
3862 bfd_size_type i;
3863 Elf_Internal_Verdef def;
3864 Elf_Internal_Verdaux defaux;
c44233aa 3865
2b0f7ef9
JJ
3866 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3867 p = (bfd_byte *) s->contents;
3868 do
3869 {
3870 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3871 &def);
3872 p += sizeof (Elf_External_Verdef);
3873 for (i = 0; i < def.vd_cnt; ++i)
3874 {
3875 _bfd_elf_swap_verdaux_in (output_bfd,
3876 (Elf_External_Verdaux *) p, &defaux);
3877 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3878 defaux.vda_name);
3879 _bfd_elf_swap_verdaux_out (output_bfd,
3880 &defaux, (Elf_External_Verdaux *) p);
3881 p += sizeof (Elf_External_Verdaux);
3882 }
3883 }
3884 while (def.vd_next);
3885 }
3886
3887 /* Adjust version references. */
3888 if (elf_tdata (output_bfd)->verref)
3889 {
3890 asection *s;
3891 bfd_byte *p;
3892 bfd_size_type i;
3893 Elf_Internal_Verneed need;
3894 Elf_Internal_Vernaux needaux;
c44233aa 3895
2b0f7ef9
JJ
3896 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3897 p = (bfd_byte *) s->contents;
3898 do
3899 {
3900 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3901 &need);
3902 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3903 _bfd_elf_swap_verneed_out (output_bfd, &need,
3904 (Elf_External_Verneed *) p);
3905 p += sizeof (Elf_External_Verneed);
3906 for (i = 0; i < need.vn_cnt; ++i)
3907 {
3908 _bfd_elf_swap_vernaux_in (output_bfd,
3909 (Elf_External_Vernaux *) p, &needaux);
3910 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3911 needaux.vna_name);
3912 _bfd_elf_swap_vernaux_out (output_bfd,
3913 &needaux,
3914 (Elf_External_Vernaux *) p);
3915 p += sizeof (Elf_External_Vernaux);
3916 }
3917 }
3918 while (need.vn_next);
3919 }
3920
b34976b6 3921 return TRUE;
2b0f7ef9
JJ
3922}
3923
252b5132
RH
3924/* Fix up the flags for a symbol. This handles various cases which
3925 can only be fixed after all the input files are seen. This is
3926 currently called by both adjust_dynamic_symbol and
3927 assign_sym_version, which is unnecessary but perhaps more robust in
3928 the face of future changes. */
3929
b34976b6 3930static bfd_boolean
252b5132
RH
3931elf_fix_symbol_flags (h, eif)
3932 struct elf_link_hash_entry *h;
3933 struct elf_info_failed *eif;
3934{
3935 /* If this symbol was mentioned in a non-ELF file, try to set
3936 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3937 permit a non-ELF file to correctly refer to a symbol defined in
3938 an ELF dynamic object. */
3939 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3940 {
94b6c40a
L
3941 while (h->root.type == bfd_link_hash_indirect)
3942 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3943
252b5132
RH
3944 if (h->root.type != bfd_link_hash_defined
3945 && h->root.type != bfd_link_hash_defweak)
3946 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3947 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3948 else
3949 {
3950 if (h->root.u.def.section->owner != NULL
3951 && (bfd_get_flavour (h->root.u.def.section->owner)
3952 == bfd_target_elf_flavour))
3953 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3954 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3955 else
3956 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3957 }
3958
3959 if (h->dynindx == -1
3960 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3961 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3962 {
3963 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3964 {
b34976b6
AM
3965 eif->failed = TRUE;
3966 return FALSE;
252b5132
RH
3967 }
3968 }
3969 }
3970 else
3971 {
3972 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
c44233aa
AM
3973 was first seen in a non-ELF file. Fortunately, if the symbol
3974 was first seen in an ELF file, we're probably OK unless the
3975 symbol was defined in a non-ELF file. Catch that case here.
3976 FIXME: We're still in trouble if the symbol was first seen in
3977 a dynamic object, and then later in a non-ELF regular object. */
252b5132
RH
3978 if ((h->root.type == bfd_link_hash_defined
3979 || h->root.type == bfd_link_hash_defweak)
3980 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3981 && (h->root.u.def.section->owner != NULL
3982 ? (bfd_get_flavour (h->root.u.def.section->owner)
3983 != bfd_target_elf_flavour)
3984 : (bfd_is_abs_section (h->root.u.def.section)
3985 && (h->elf_link_hash_flags
3986 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3987 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3988 }
3989
3990 /* If this is a final link, and the symbol was defined as a common
3991 symbol in a regular object file, and there was no definition in
3992 any dynamic object, then the linker will have allocated space for
3993 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3994 flag will not have been set. */
3995 if (h->root.type == bfd_link_hash_defined
3996 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3997 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3998 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3999 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4000 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4001
4002 /* If -Bsymbolic was used (which means to bind references to global
4003 symbols to the definition within the shared object), and this
4004 symbol was defined in a regular object, then it actually doesn't
d954b040
HPN
4005 need a PLT entry, and we can accomplish that by forcing it local.
4006 Likewise, if the symbol has hidden or internal visibility.
4007 FIXME: It might be that we also do not need a PLT for other
4008 non-hidden visibilities, but we would have to tell that to the
4009 backend specifically; we can't just clear PLT-related data here. */
252b5132
RH
4010 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
4011 && eif->info->shared
8ea2e4bd 4012 && is_elf_hash_table (eif->info)
d954b040
HPN
4013 && (eif->info->symbolic
4014 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
4015 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
252b5132
RH
4016 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
4017 {
391a809a 4018 struct elf_backend_data *bed;
b34976b6 4019 bfd_boolean force_local;
8ea2e4bd 4020
391a809a 4021 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
e5094212
AM
4022
4023 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
4024 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
4025 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
252b5132
RH
4026 }
4027
fc4cc5bb
ILT
4028 /* If this is a weak defined symbol in a dynamic object, and we know
4029 the real definition in the dynamic object, copy interesting flags
4030 over to the real definition. */
4031 if (h->weakdef != NULL)
4032 {
4033 struct elf_link_hash_entry *weakdef;
4034
8e67855b
NC
4035 weakdef = h->weakdef;
4036 if (h->root.type == bfd_link_hash_indirect)
4037 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4038
fc4cc5bb
ILT
4039 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4040 || h->root.type == bfd_link_hash_defweak);
fc4cc5bb
ILT
4041 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
4042 || weakdef->root.type == bfd_link_hash_defweak);
4043 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
4044
4045 /* If the real definition is defined by a regular object file,
4046 don't do anything special. See the longer description in
4047 elf_adjust_dynamic_symbol, below. */
4048 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
4049 h->weakdef = NULL;
4050 else
0a991dfe
AM
4051 {
4052 struct elf_backend_data *bed;
4053
4054 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
b48fa14c 4055 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
0a991dfe 4056 }
fc4cc5bb
ILT
4057 }
4058
b34976b6 4059 return TRUE;
252b5132
RH
4060}
4061
4062/* Make the backend pick a good value for a dynamic symbol. This is
4063 called via elf_link_hash_traverse, and also calls itself
4064 recursively. */
4065
b34976b6 4066static bfd_boolean
252b5132
RH
4067elf_adjust_dynamic_symbol (h, data)
4068 struct elf_link_hash_entry *h;
4069 PTR data;
4070{
4071 struct elf_info_failed *eif = (struct elf_info_failed *) data;
4072 bfd *dynobj;
4073 struct elf_backend_data *bed;
4074
5cab59f6
AM
4075 if (! is_elf_hash_table (eif->info))
4076 return FALSE;
4077
e92d460e
AM
4078 if (h->root.type == bfd_link_hash_warning)
4079 {
5cab59f6
AM
4080 h->plt = elf_hash_table (eif->info)->init_offset;
4081 h->got = elf_hash_table (eif->info)->init_offset;
e92d460e
AM
4082
4083 /* When warning symbols are created, they **replace** the "real"
4084 entry in the hash table, thus we never get to see the real
4085 symbol in a hash traversal. So look at it now. */
4086 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4087 }
4088
252b5132
RH
4089 /* Ignore indirect symbols. These are added by the versioning code. */
4090 if (h->root.type == bfd_link_hash_indirect)
b34976b6 4091 return TRUE;
252b5132
RH
4092
4093 /* Fix the symbol flags. */
4094 if (! elf_fix_symbol_flags (h, eif))
b34976b6 4095 return FALSE;
252b5132
RH
4096
4097 /* If this symbol does not require a PLT entry, and it is not
4098 defined by a dynamic object, or is not referenced by a regular
4099 object, ignore it. We do have to handle a weak defined symbol,
4100 even if no regular object refers to it, if we decided to add it
4101 to the dynamic symbol table. FIXME: Do we normally need to worry
4102 about symbols which are defined by one dynamic object and
4103 referenced by another one? */
4104 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
4105 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4106 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4107 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
4108 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
4109 {
5cab59f6 4110 h->plt = elf_hash_table (eif->info)->init_offset;
b34976b6 4111 return TRUE;
252b5132
RH
4112 }
4113
4114 /* If we've already adjusted this symbol, don't do it again. This
4115 can happen via a recursive call. */
4116 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
b34976b6 4117 return TRUE;
252b5132
RH
4118
4119 /* Don't look at this symbol again. Note that we must set this
4120 after checking the above conditions, because we may look at a
4121 symbol once, decide not to do anything, and then get called
4122 recursively later after REF_REGULAR is set below. */
4123 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
4124
4125 /* If this is a weak definition, and we know a real definition, and
4126 the real symbol is not itself defined by a regular object file,
4127 then get a good value for the real definition. We handle the
4128 real symbol first, for the convenience of the backend routine.
4129
4130 Note that there is a confusing case here. If the real definition
4131 is defined by a regular object file, we don't get the real symbol
4132 from the dynamic object, but we do get the weak symbol. If the
4133 processor backend uses a COPY reloc, then if some routine in the
4134 dynamic object changes the real symbol, we will not see that
4135 change in the corresponding weak symbol. This is the way other
4136 ELF linkers work as well, and seems to be a result of the shared
4137 library model.
4138
4139 I will clarify this issue. Most SVR4 shared libraries define the
4140 variable _timezone and define timezone as a weak synonym. The
4141 tzset call changes _timezone. If you write
4142 extern int timezone;
4143 int _timezone = 5;
4144 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4145 you might expect that, since timezone is a synonym for _timezone,
4146 the same number will print both times. However, if the processor
4147 backend uses a COPY reloc, then actually timezone will be copied
4148 into your process image, and, since you define _timezone
4149 yourself, _timezone will not. Thus timezone and _timezone will
4150 wind up at different memory locations. The tzset call will set
4151 _timezone, leaving timezone unchanged. */
4152
4153 if (h->weakdef != NULL)
4154 {
fc4cc5bb
ILT
4155 /* If we get to this point, we know there is an implicit
4156 reference by a regular object file via the weak symbol H.
4157 FIXME: Is this really true? What if the traversal finds
4158 H->WEAKDEF before it finds H? */
4159 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
252b5132 4160
fc4cc5bb 4161 if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
b34976b6 4162 return FALSE;
252b5132
RH
4163 }
4164
4165 /* If a symbol has no type and no size and does not require a PLT
4166 entry, then we are probably about to do the wrong thing here: we
4167 are probably going to create a COPY reloc for an empty object.
4168 This case can arise when a shared object is built with assembly
4169 code, and the assembly code fails to set the symbol type. */
4170 if (h->size == 0
4171 && h->type == STT_NOTYPE
4172 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
4173 (*_bfd_error_handler)
4174 (_("warning: type and size of dynamic symbol `%s' are not defined"),
58821868 4175 h->root.root.string);
252b5132
RH
4176
4177 dynobj = elf_hash_table (eif->info)->dynobj;
4178 bed = get_elf_backend_data (dynobj);
4179 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
4180 {
b34976b6
AM
4181 eif->failed = TRUE;
4182 return FALSE;
252b5132
RH
4183 }
4184
b34976b6 4185 return TRUE;
252b5132
RH
4186}
4187\f
4188/* This routine is used to export all defined symbols into the dynamic
4189 symbol table. It is called via elf_link_hash_traverse. */
4190
b34976b6 4191static bfd_boolean
252b5132
RH
4192elf_export_symbol (h, data)
4193 struct elf_link_hash_entry *h;
4194 PTR data;
4195{
4196 struct elf_info_failed *eif = (struct elf_info_failed *) data;
4197
4198 /* Ignore indirect symbols. These are added by the versioning code. */
4199 if (h->root.type == bfd_link_hash_indirect)
b34976b6 4200 return TRUE;
252b5132 4201
e92d460e
AM
4202 if (h->root.type == bfd_link_hash_warning)
4203 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4204
252b5132
RH
4205 if (h->dynindx == -1
4206 && (h->elf_link_hash_flags
4207 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
4208 {
bc2b6df7
L
4209 struct bfd_elf_version_tree *t;
4210 struct bfd_elf_version_expr *d;
4211
4212 for (t = eif->verdefs; t != NULL; t = t->next)
252b5132 4213 {
bc2b6df7
L
4214 if (t->globals != NULL)
4215 {
4216 for (d = t->globals; d != NULL; d = d->next)
4217 {
4218 if ((*d->match) (d, h->root.root.string))
4219 goto doit;
4220 }
4221 }
4222
4223 if (t->locals != NULL)
4224 {
4225 for (d = t->locals ; d != NULL; d = d->next)
4226 {
4227 if ((*d->match) (d, h->root.root.string))
b34976b6 4228 return TRUE;
bc2b6df7
L
4229 }
4230 }
252b5132 4231 }
bc2b6df7
L
4232
4233 if (!eif->verdefs)
c44233aa 4234 {
58821868 4235 doit:
bc2b6df7
L
4236 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
4237 {
b34976b6
AM
4238 eif->failed = TRUE;
4239 return FALSE;
bc2b6df7 4240 }
c44233aa 4241 }
252b5132
RH
4242 }
4243
b34976b6 4244 return TRUE;
252b5132
RH
4245}
4246\f
4247/* Look through the symbols which are defined in other shared
4248 libraries and referenced here. Update the list of version
4249 dependencies. This will be put into the .gnu.version_r section.
4250 This function is called via elf_link_hash_traverse. */
4251
b34976b6 4252static bfd_boolean
252b5132
RH
4253elf_link_find_version_dependencies (h, data)
4254 struct elf_link_hash_entry *h;
4255 PTR data;
4256{
4257 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
4258 Elf_Internal_Verneed *t;
4259 Elf_Internal_Vernaux *a;
dc810e39 4260 bfd_size_type amt;
252b5132 4261
e92d460e
AM
4262 if (h->root.type == bfd_link_hash_warning)
4263 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4264
252b5132
RH
4265 /* We only care about symbols defined in shared objects with version
4266 information. */
4267 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4268 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4269 || h->dynindx == -1
4270 || h->verinfo.verdef == NULL)
b34976b6 4271 return TRUE;
252b5132
RH
4272
4273 /* See if we already know about this version. */
4274 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
4275 {
4276 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
4277 continue;
4278
4279 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4280 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
b34976b6 4281 return TRUE;
252b5132
RH
4282
4283 break;
4284 }
4285
4286 /* This is a new version. Add it to tree we are building. */
4287
4288 if (t == NULL)
4289 {
dc810e39
AM
4290 amt = sizeof *t;
4291 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
252b5132
RH
4292 if (t == NULL)
4293 {
b34976b6
AM
4294 rinfo->failed = TRUE;
4295 return FALSE;
252b5132
RH
4296 }
4297
4298 t->vn_bfd = h->verinfo.verdef->vd_bfd;
4299 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
4300 elf_tdata (rinfo->output_bfd)->verref = t;
4301 }
4302
dc810e39
AM
4303 amt = sizeof *a;
4304 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
252b5132
RH
4305
4306 /* Note that we are copying a string pointer here, and testing it
4307 above. If bfd_elf_string_from_elf_section is ever changed to
4308 discard the string data when low in memory, this will have to be
4309 fixed. */
4310 a->vna_nodename = h->verinfo.verdef->vd_nodename;
4311
4312 a->vna_flags = h->verinfo.verdef->vd_flags;
4313 a->vna_nextptr = t->vn_auxptr;
4314
4315 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
4316 ++rinfo->vers;
4317
4318 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
4319
4320 t->vn_auxptr = a;
4321
b34976b6 4322 return TRUE;
252b5132
RH
4323}
4324
4325/* Figure out appropriate versions for all the symbols. We may not
4326 have the version number script until we have read all of the input
4327 files, so until that point we don't know which symbols should be
4328 local. This function is called via elf_link_hash_traverse. */
4329
b34976b6 4330static bfd_boolean
252b5132
RH
4331elf_link_assign_sym_version (h, data)
4332 struct elf_link_hash_entry *h;
4333 PTR data;
4334{
dc810e39
AM
4335 struct elf_assign_sym_version_info *sinfo;
4336 struct bfd_link_info *info;
c61b8717 4337 struct elf_backend_data *bed;
252b5132
RH
4338 struct elf_info_failed eif;
4339 char *p;
dc810e39
AM
4340 bfd_size_type amt;
4341
4342 sinfo = (struct elf_assign_sym_version_info *) data;
4343 info = sinfo->info;
252b5132 4344
e92d460e
AM
4345 if (h->root.type == bfd_link_hash_warning)
4346 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4347
252b5132 4348 /* Fix the symbol flags. */
b34976b6 4349 eif.failed = FALSE;
252b5132
RH
4350 eif.info = info;
4351 if (! elf_fix_symbol_flags (h, &eif))
4352 {
4353 if (eif.failed)
b34976b6
AM
4354 sinfo->failed = TRUE;
4355 return FALSE;
252b5132
RH
4356 }
4357
4358 /* We only need version numbers for symbols defined in regular
4359 objects. */
4360 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
b34976b6 4361 return TRUE;
252b5132 4362
c61b8717 4363 bed = get_elf_backend_data (sinfo->output_bfd);
252b5132
RH
4364 p = strchr (h->root.root.string, ELF_VER_CHR);
4365 if (p != NULL && h->verinfo.vertree == NULL)
4366 {
4367 struct bfd_elf_version_tree *t;
b34976b6 4368 bfd_boolean hidden;
252b5132 4369
b34976b6 4370 hidden = TRUE;
252b5132
RH
4371
4372 /* There are two consecutive ELF_VER_CHR characters if this is
c44233aa 4373 not a hidden symbol. */
252b5132
RH
4374 ++p;
4375 if (*p == ELF_VER_CHR)
4376 {
b34976b6 4377 hidden = FALSE;
252b5132
RH
4378 ++p;
4379 }
4380
4381 /* If there is no version string, we can just return out. */
4382 if (*p == '\0')
4383 {
4384 if (hidden)
4385 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
b34976b6 4386 return TRUE;
252b5132
RH
4387 }
4388
4389 /* Look for the version. If we find it, it is no longer weak. */
4390 for (t = sinfo->verdefs; t != NULL; t = t->next)
4391 {
4392 if (strcmp (t->name, p) == 0)
4393 {
dc810e39 4394 size_t len;
252b5132
RH
4395 char *alc;
4396 struct bfd_elf_version_expr *d;
4397
4398 len = p - h->root.root.string;
e5094212 4399 alc = bfd_malloc ((bfd_size_type) len);
252b5132 4400 if (alc == NULL)
b34976b6 4401 return FALSE;
d4c88bbb 4402 memcpy (alc, h->root.root.string, len - 1);
252b5132
RH
4403 alc[len - 1] = '\0';
4404 if (alc[len - 2] == ELF_VER_CHR)
c44233aa 4405 alc[len - 2] = '\0';
252b5132
RH
4406
4407 h->verinfo.vertree = t;
b34976b6 4408 t->used = TRUE;
252b5132
RH
4409 d = NULL;
4410
4411 if (t->globals != NULL)
4412 {
4413 for (d = t->globals; d != NULL; d = d->next)
4414 if ((*d->match) (d, alc))
4415 break;
4416 }
4417
4418 /* See if there is anything to force this symbol to
c44233aa 4419 local scope. */
252b5132
RH
4420 if (d == NULL && t->locals != NULL)
4421 {
4422 for (d = t->locals; d != NULL; d = d->next)
4423 {
4424 if ((*d->match) (d, alc))
4425 {
4426 if (h->dynindx != -1
4427 && info->shared
99293407 4428 && ! info->export_dynamic)
252b5132 4429 {
b34976b6 4430 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
252b5132
RH
4431 }
4432
4433 break;
4434 }
4435 }
4436 }
4437
e5094212 4438 free (alc);
252b5132
RH
4439 break;
4440 }
4441 }
4442
4443 /* If we are building an application, we need to create a
c44233aa 4444 version node for this version. */
252b5132
RH
4445 if (t == NULL && ! info->shared)
4446 {
4447 struct bfd_elf_version_tree **pp;
4448 int version_index;
4449
4450 /* If we aren't going to export this symbol, we don't need
c44233aa 4451 to worry about it. */
252b5132 4452 if (h->dynindx == -1)
b34976b6 4453 return TRUE;
252b5132 4454
dc810e39 4455 amt = sizeof *t;
252b5132 4456 t = ((struct bfd_elf_version_tree *)
dc810e39 4457 bfd_alloc (sinfo->output_bfd, amt));
252b5132
RH
4458 if (t == NULL)
4459 {
b34976b6
AM
4460 sinfo->failed = TRUE;
4461 return FALSE;
252b5132
RH
4462 }
4463
4464 t->next = NULL;
4465 t->name = p;
4466 t->globals = NULL;
4467 t->locals = NULL;
4468 t->deps = NULL;
4469 t->name_indx = (unsigned int) -1;
b34976b6 4470 t->used = TRUE;
252b5132
RH
4471
4472 version_index = 1;
6b9b879a
JJ
4473 /* Don't count anonymous version tag. */
4474 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
4475 version_index = 0;
252b5132
RH
4476 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
4477 ++version_index;
4478 t->vernum = version_index;
4479
4480 *pp = t;
4481
4482 h->verinfo.vertree = t;
4483 }
4484 else if (t == NULL)
4485 {
4486 /* We could not find the version for a symbol when
c44233aa 4487 generating a shared archive. Return an error. */
252b5132
RH
4488 (*_bfd_error_handler)
4489 (_("%s: undefined versioned symbol name %s"),
4490 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
4491 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
4492 sinfo->failed = TRUE;
4493 return FALSE;
252b5132
RH
4494 }
4495
4496 if (hidden)
4497 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4498 }
4499
4500 /* If we don't have a version for this symbol, see if we can find
4501 something. */
4502 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
4503 {
4504 struct bfd_elf_version_tree *t;
58821868 4505 struct bfd_elf_version_tree *local_ver;
252b5132
RH
4506 struct bfd_elf_version_expr *d;
4507
4508 /* See if can find what version this symbol is in. If the
c44233aa
AM
4509 symbol is supposed to be local, then don't actually register
4510 it. */
58821868 4511 local_ver = NULL;
252b5132
RH
4512 for (t = sinfo->verdefs; t != NULL; t = t->next)
4513 {
4514 if (t->globals != NULL)
4515 {
b34976b6 4516 bfd_boolean matched;
31941635 4517
b34976b6 4518 matched = FALSE;
252b5132
RH
4519 for (d = t->globals; d != NULL; d = d->next)
4520 {
4521 if ((*d->match) (d, h->root.root.string))
4522 {
31941635 4523 if (d->symver)
b34976b6 4524 matched = TRUE;
31941635
L
4525 else
4526 {
4527 /* There is a version without definition. Make
4528 the symbol the default definition for this
4529 version. */
4530 h->verinfo.vertree = t;
4531 local_ver = NULL;
4532 d->script = 1;
4533 break;
4534 }
252b5132
RH
4535 }
4536 }
4537
4538 if (d != NULL)
4539 break;
31941635
L
4540 else if (matched)
4541 /* There is no undefined version for this symbol. Hide the
4542 default one. */
b34976b6 4543 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
252b5132
RH
4544 }
4545
4546 if (t->locals != NULL)
4547 {
4548 for (d = t->locals; d != NULL; d = d->next)
4549 {
58821868
AM
4550 /* If the match is "*", keep looking for a more
4551 explicit, perhaps even global, match. */
252b5132 4552 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
58821868 4553 local_ver = t;
252b5132
RH
4554 else if ((*d->match) (d, h->root.root.string))
4555 {
58821868 4556 local_ver = t;
252b5132
RH
4557 break;
4558 }
4559 }
4560
4561 if (d != NULL)
4562 break;
4563 }
4564 }
4565
58821868 4566 if (local_ver != NULL)
252b5132 4567 {
58821868 4568 h->verinfo.vertree = local_ver;
252b5132
RH
4569 if (h->dynindx != -1
4570 && info->shared
99293407 4571 && ! info->export_dynamic)
252b5132 4572 {
b34976b6 4573 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
252b5132
RH
4574 }
4575 }
4576 }
4577
b34976b6 4578 return TRUE;
252b5132 4579}
252b5132
RH
4580\f
4581/* Final phase of ELF linker. */
4582
4583/* A structure we use to avoid passing large numbers of arguments. */
4584
4585struct elf_final_link_info
4586{
4587 /* General link information. */
4588 struct bfd_link_info *info;
4589 /* Output BFD. */
4590 bfd *output_bfd;
4591 /* Symbol string table. */
4592 struct bfd_strtab_hash *symstrtab;
4593 /* .dynsym section. */
4594 asection *dynsym_sec;
4595 /* .hash section. */
4596 asection *hash_sec;
4597 /* symbol version section (.gnu.version). */
4598 asection *symver_sec;
13ae64f3
JJ
4599 /* first SHF_TLS section (if any). */
4600 asection *first_tls_sec;
252b5132
RH
4601 /* Buffer large enough to hold contents of any section. */
4602 bfd_byte *contents;
4603 /* Buffer large enough to hold external relocs of any section. */
4604 PTR external_relocs;
4605 /* Buffer large enough to hold internal relocs of any section. */
4606 Elf_Internal_Rela *internal_relocs;
4607 /* Buffer large enough to hold external local symbols of any input
4608 BFD. */
4609 Elf_External_Sym *external_syms;
9ad5cbcf
AM
4610 /* And a buffer for symbol section indices. */
4611 Elf_External_Sym_Shndx *locsym_shndx;
252b5132
RH
4612 /* Buffer large enough to hold internal local symbols of any input
4613 BFD. */
4614 Elf_Internal_Sym *internal_syms;
4615 /* Array large enough to hold a symbol index for each local symbol
4616 of any input BFD. */
4617 long *indices;
4618 /* Array large enough to hold a section pointer for each local
4619 symbol of any input BFD. */
4620 asection **sections;
4621 /* Buffer to hold swapped out symbols. */
4622 Elf_External_Sym *symbuf;
9ad5cbcf
AM
4623 /* And one for symbol section indices. */
4624 Elf_External_Sym_Shndx *symshndxbuf;
252b5132
RH
4625 /* Number of swapped out symbols in buffer. */
4626 size_t symbuf_count;
4627 /* Number of symbols which fit in symbuf. */
4628 size_t symbuf_size;
c97e73dd
AM
4629 /* And same for symshndxbuf. */
4630 size_t shndxbuf_size;
252b5132
RH
4631};
4632
b34976b6 4633static bfd_boolean elf_link_output_sym
252b5132
RH
4634 PARAMS ((struct elf_final_link_info *, const char *,
4635 Elf_Internal_Sym *, asection *));
b34976b6 4636static bfd_boolean elf_link_flush_output_syms
252b5132 4637 PARAMS ((struct elf_final_link_info *));
b34976b6 4638static bfd_boolean elf_link_output_extsym
252b5132 4639 PARAMS ((struct elf_link_hash_entry *, PTR));
b34976b6 4640static bfd_boolean elf_link_sec_merge_syms
f5fa8ca2 4641 PARAMS ((struct elf_link_hash_entry *, PTR));
b34976b6 4642static bfd_boolean elf_link_check_versioned_symbol
f5d44ba0 4643 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
b34976b6 4644static bfd_boolean elf_link_input_bfd
252b5132 4645 PARAMS ((struct elf_final_link_info *, bfd *));
b34976b6 4646static bfd_boolean elf_reloc_link_order
252b5132
RH
4647 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4648 struct bfd_link_order *));
4649
4650/* This struct is used to pass information to elf_link_output_extsym. */
4651
4652struct elf_outext_info
4653{
b34976b6
AM
4654 bfd_boolean failed;
4655 bfd_boolean localsyms;
252b5132
RH
4656 struct elf_final_link_info *finfo;
4657};
4658
23bc299b
MM
4659/* Compute the size of, and allocate space for, REL_HDR which is the
4660 section header for a section containing relocations for O. */
4661
b34976b6 4662static bfd_boolean
23bc299b
MM
4663elf_link_size_reloc_section (abfd, rel_hdr, o)
4664 bfd *abfd;
4665 Elf_Internal_Shdr *rel_hdr;
4666 asection *o;
4667{
dc810e39
AM
4668 bfd_size_type reloc_count;
4669 bfd_size_type num_rel_hashes;
23bc299b 4670
b037af20
MM
4671 /* Figure out how many relocations there will be. */
4672 if (rel_hdr == &elf_section_data (o)->rel_hdr)
4673 reloc_count = elf_section_data (o)->rel_count;
4674 else
4675 reloc_count = elf_section_data (o)->rel_count2;
4676
9317eacc
CM
4677 num_rel_hashes = o->reloc_count;
4678 if (num_rel_hashes < reloc_count)
4679 num_rel_hashes = reloc_count;
dc810e39 4680
b037af20
MM
4681 /* That allows us to calculate the size of the section. */
4682 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
23bc299b
MM
4683
4684 /* The contents field must last into write_object_contents, so we
755cfd29
NC
4685 allocate it with bfd_alloc rather than malloc. Also since we
4686 cannot be sure that the contents will actually be filled in,
4687 we zero the allocated space. */
4688 rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
23bc299b 4689 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
b34976b6 4690 return FALSE;
3e932841 4691
b037af20
MM
4692 /* We only allocate one set of hash entries, so we only do it the
4693 first time we are called. */
9317eacc
CM
4694 if (elf_section_data (o)->rel_hashes == NULL
4695 && num_rel_hashes)
b037af20 4696 {
209f668e
NC
4697 struct elf_link_hash_entry **p;
4698
b037af20 4699 p = ((struct elf_link_hash_entry **)
9317eacc 4700 bfd_zmalloc (num_rel_hashes
209f668e 4701 * sizeof (struct elf_link_hash_entry *)));
9317eacc 4702 if (p == NULL)
b34976b6 4703 return FALSE;
23bc299b 4704
b037af20 4705 elf_section_data (o)->rel_hashes = p;
b037af20 4706 }
23bc299b 4707
b34976b6 4708 return TRUE;
23bc299b
MM
4709}
4710
31367b81
MM
4711/* When performing a relocateable link, the input relocations are
4712 preserved. But, if they reference global symbols, the indices
4713 referenced must be updated. Update all the relocations in
4714 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4715
4716static void
4717elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
4718 bfd *abfd;
4719 Elf_Internal_Shdr *rel_hdr;
4720 unsigned int count;
4721 struct elf_link_hash_entry **rel_hash;
4722{
4723 unsigned int i;
32f0787a 4724 struct elf_backend_data *bed = get_elf_backend_data (abfd);
947216bf
AM
4725 bfd_byte *erela;
4726 void (*swap_in) PARAMS ((bfd *, const bfd_byte *, Elf_Internal_Rela *));
4727 void (*swap_out) PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *));
209f668e 4728
947216bf 4729 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
209f668e 4730 {
947216bf
AM
4731 swap_in = bed->s->swap_reloc_in;
4732 swap_out = bed->s->swap_reloc_out;
209f668e 4733 }
947216bf 4734 else if (rel_hdr->sh_entsize == sizeof (Elf_External_Rela))
209f668e 4735 {
947216bf
AM
4736 swap_in = bed->s->swap_reloca_in;
4737 swap_out = bed->s->swap_reloca_out;
209f668e 4738 }
947216bf
AM
4739 else
4740 abort ();
4741
4742 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
4743 abort ();
31367b81 4744
947216bf
AM
4745 erela = rel_hdr->contents;
4746 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
31367b81 4747 {
947216bf
AM
4748 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
4749 unsigned int j;
4750
31367b81
MM
4751 if (*rel_hash == NULL)
4752 continue;
4753
4754 BFD_ASSERT ((*rel_hash)->indx >= 0);
4755
947216bf
AM
4756 (*swap_in) (abfd, erela, irela);
4757 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4758 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4759 ELF_R_TYPE (irela[j].r_info));
4760 (*swap_out) (abfd, irela, erela);
31367b81
MM
4761 }
4762}
4763
58821868
AM
4764struct elf_link_sort_rela
4765{
db6751f2
JJ
4766 bfd_vma offset;
4767 enum elf_reloc_type_class type;
947216bf
AM
4768 /* We use this as an array of size int_rels_per_ext_rel. */
4769 Elf_Internal_Rela rela[1];
db6751f2
JJ
4770};
4771
4772static int
4773elf_link_sort_cmp1 (A, B)
4774 const PTR A;
4775 const PTR B;
4776{
f51e552e
AM
4777 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4778 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
db6751f2
JJ
4779 int relativea, relativeb;
4780
4781 relativea = a->type == reloc_class_relative;
4782 relativeb = b->type == reloc_class_relative;
4783
4784 if (relativea < relativeb)
db6751f2 4785 return 1;
fcfbdf31
JJ
4786 if (relativea > relativeb)
4787 return -1;
947216bf 4788 if (ELF_R_SYM (a->rela->r_info) < ELF_R_SYM (b->rela->r_info))
db6751f2 4789 return -1;
947216bf 4790 if (ELF_R_SYM (a->rela->r_info) > ELF_R_SYM (b->rela->r_info))
db6751f2 4791 return 1;
947216bf 4792 if (a->rela->r_offset < b->rela->r_offset)
db6751f2 4793 return -1;
947216bf 4794 if (a->rela->r_offset > b->rela->r_offset)
db6751f2
JJ
4795 return 1;
4796 return 0;
4797}
4798
4799static int
4800elf_link_sort_cmp2 (A, B)
4801 const PTR A;
4802 const PTR B;
4803{
f51e552e
AM
4804 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4805 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
db6751f2
JJ
4806 int copya, copyb;
4807
4808 if (a->offset < b->offset)
4809 return -1;
4810 if (a->offset > b->offset)
4811 return 1;
290394d6
JJ
4812 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
4813 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
db6751f2
JJ
4814 if (copya < copyb)
4815 return -1;
4816 if (copya > copyb)
4817 return 1;
947216bf 4818 if (a->rela->r_offset < b->rela->r_offset)
db6751f2 4819 return -1;
947216bf 4820 if (a->rela->r_offset > b->rela->r_offset)
db6751f2
JJ
4821 return 1;
4822 return 0;
4823}
4824
4825static size_t
4826elf_link_sort_relocs (abfd, info, psec)
4827 bfd *abfd;
4828 struct bfd_link_info *info;
4829 asection **psec;
4830{
4831 bfd *dynobj = elf_hash_table (info)->dynobj;
4832 asection *reldyn, *o;
f51e552e 4833 bfd_size_type count, size;
947216bf
AM
4834 size_t i, ret, sort_elt, ext_size;
4835 bfd_byte *sort, *s_non_relative, *p;
4836 struct elf_link_sort_rela *sq;
db6751f2 4837 struct elf_backend_data *bed = get_elf_backend_data (abfd);
033fd5f9 4838 int i2e = bed->s->int_rels_per_ext_rel;
947216bf
AM
4839 void (*swap_in) PARAMS ((bfd *, const bfd_byte *, Elf_Internal_Rela *));
4840 void (*swap_out) PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *));
db6751f2
JJ
4841
4842 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
4843 if (reldyn == NULL || reldyn->_raw_size == 0)
4844 {
4845 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
4846 if (reldyn == NULL || reldyn->_raw_size == 0)
4847 return 0;
947216bf
AM
4848 ext_size = sizeof (Elf_External_Rel);
4849 swap_in = bed->s->swap_reloc_in;
4850 swap_out = bed->s->swap_reloc_out;
db6751f2
JJ
4851 }
4852 else
947216bf
AM
4853 {
4854 ext_size = sizeof (Elf_External_Rela);
4855 swap_in = bed->s->swap_reloca_in;
4856 swap_out = bed->s->swap_reloca_out;
4857 }
4858 count = reldyn->_raw_size / ext_size;
db6751f2
JJ
4859
4860 size = 0;
4861 for (o = dynobj->sections; o != NULL; o = o->next)
4862 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4863 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4864 && o->output_section == reldyn)
4865 size += o->_raw_size;
4866
4867 if (size != reldyn->_raw_size)
4868 return 0;
4869
947216bf
AM
4870 sort_elt = (sizeof (struct elf_link_sort_rela)
4871 + (i2e - 1) * sizeof (Elf_Internal_Rela));
4872 sort = bfd_zmalloc (sort_elt * count);
4873 if (sort == NULL)
db6751f2
JJ
4874 {
4875 (*info->callbacks->warning)
dc810e39
AM
4876 (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
4877 (bfd_vma) 0);
db6751f2
JJ
4878 return 0;
4879 }
4880
4881 for (o = dynobj->sections; o != NULL; o = o->next)
4882 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4883 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4884 && o->output_section == reldyn)
4885 {
947216bf 4886 bfd_byte *erel, *erelend;
db6751f2 4887
947216bf
AM
4888 erel = o->contents;
4889 erelend = o->contents + o->_raw_size;
4890 p = sort + o->output_offset / ext_size * sort_elt;
4891 while (erel < erelend)
db6751f2 4892 {
947216bf
AM
4893 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
4894 (*swap_in) (abfd, erel, s->rela);
4895 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
4896 p += sort_elt;
4897 erel += ext_size;
db6751f2
JJ
4898 }
4899 }
4900
947216bf
AM
4901 qsort (sort, (size_t) count, sort_elt, elf_link_sort_cmp1);
4902
4903 for (i = 0, p = sort; i < count; i++, p += sort_elt)
db6751f2 4904 {
947216bf
AM
4905 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
4906 if (s->type != reloc_class_relative)
4907 break;
db6751f2 4908 }
947216bf
AM
4909 ret = i;
4910 s_non_relative = p;
4911
4912 sq = (struct elf_link_sort_rela *) s_non_relative;
4913 for (; i < count; i++, p += sort_elt)
4914 {
4915 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
4916 if (ELF_R_SYM (sp->rela->r_info) != ELF_R_SYM (sq->rela->r_info))
4917 sq = sp;
4918 sp->offset = sq->rela->r_offset;
4919 }
4920
4921 qsort (s_non_relative, (size_t) count - ret, sort_elt, elf_link_sort_cmp2);
dc810e39 4922
db6751f2
JJ
4923 for (o = dynobj->sections; o != NULL; o = o->next)
4924 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4925 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4926 && o->output_section == reldyn)
4927 {
947216bf 4928 bfd_byte *erel, *erelend;
db6751f2 4929
947216bf
AM
4930 erel = o->contents;
4931 erelend = o->contents + o->_raw_size;
4932 p = sort + o->output_offset / ext_size * sort_elt;
4933 while (erel < erelend)
db6751f2 4934 {
947216bf
AM
4935 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
4936 (*swap_out) (abfd, s->rela, erel);
4937 p += sort_elt;
4938 erel += ext_size;
db6751f2
JJ
4939 }
4940 }
4941
5ed6aba4 4942 free (sort);
db6751f2
JJ
4943 *psec = reldyn;
4944 return ret;
4945}
4946
252b5132
RH
4947/* Do the final step of an ELF link. */
4948
b34976b6 4949bfd_boolean
252b5132
RH
4950elf_bfd_final_link (abfd, info)
4951 bfd *abfd;
4952 struct bfd_link_info *info;
4953{
b34976b6
AM
4954 bfd_boolean dynamic;
4955 bfd_boolean emit_relocs;
252b5132
RH
4956 bfd *dynobj;
4957 struct elf_final_link_info finfo;
4958 register asection *o;
4959 register struct bfd_link_order *p;
4960 register bfd *sub;
dc810e39
AM
4961 bfd_size_type max_contents_size;
4962 bfd_size_type max_external_reloc_size;
4963 bfd_size_type max_internal_reloc_count;
4964 bfd_size_type max_sym_count;
9ad5cbcf 4965 bfd_size_type max_sym_shndx_count;
252b5132
RH
4966 file_ptr off;
4967 Elf_Internal_Sym elfsym;
4968 unsigned int i;
4969 Elf_Internal_Shdr *symtab_hdr;
c97e73dd 4970 Elf_Internal_Shdr *symtab_shndx_hdr;
252b5132
RH
4971 Elf_Internal_Shdr *symstrtab_hdr;
4972 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4973 struct elf_outext_info eoinfo;
b34976b6 4974 bfd_boolean merged;
db6751f2
JJ
4975 size_t relativecount = 0;
4976 asection *reldyn = 0;
dc810e39 4977 bfd_size_type amt;
252b5132 4978
8ea2e4bd 4979 if (! is_elf_hash_table (info))
b34976b6 4980 return FALSE;
8ea2e4bd 4981
252b5132
RH
4982 if (info->shared)
4983 abfd->flags |= DYNAMIC;
4984
4985 dynamic = elf_hash_table (info)->dynamic_sections_created;
4986 dynobj = elf_hash_table (info)->dynobj;
4987
9317eacc 4988 emit_relocs = (info->relocateable
c44233aa
AM
4989 || info->emitrelocations
4990 || bed->elf_backend_emit_relocs);
9317eacc 4991
252b5132
RH
4992 finfo.info = info;
4993 finfo.output_bfd = abfd;
4994 finfo.symstrtab = elf_stringtab_init ();
4995 if (finfo.symstrtab == NULL)
b34976b6 4996 return FALSE;
252b5132
RH
4997
4998 if (! dynamic)
4999 {
5000 finfo.dynsym_sec = NULL;
5001 finfo.hash_sec = NULL;
5002 finfo.symver_sec = NULL;
5003 }
5004 else
5005 {
5006 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
5007 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
5008 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
5009 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
5010 /* Note that it is OK if symver_sec is NULL. */
5011 }
5012
5013 finfo.contents = NULL;
5014 finfo.external_relocs = NULL;
5015 finfo.internal_relocs = NULL;
5016 finfo.external_syms = NULL;
9ad5cbcf 5017 finfo.locsym_shndx = NULL;
252b5132
RH
5018 finfo.internal_syms = NULL;
5019 finfo.indices = NULL;
5020 finfo.sections = NULL;
5021 finfo.symbuf = NULL;
9ad5cbcf 5022 finfo.symshndxbuf = NULL;
252b5132 5023 finfo.symbuf_count = 0;
c97e73dd 5024 finfo.shndxbuf_size = 0;
13ae64f3
JJ
5025 finfo.first_tls_sec = NULL;
5026 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
5027 if ((o->flags & SEC_THREAD_LOCAL) != 0
5028 && (o->flags & SEC_LOAD) != 0)
5029 {
5030 finfo.first_tls_sec = o;
5031 break;
5032 }
252b5132
RH
5033
5034 /* Count up the number of relocations we will output for each output
5035 section, so that we know the sizes of the reloc sections. We
5036 also figure out some maximum sizes. */
5037 max_contents_size = 0;
5038 max_external_reloc_size = 0;
5039 max_internal_reloc_count = 0;
5040 max_sym_count = 0;
9ad5cbcf 5041 max_sym_shndx_count = 0;
b34976b6 5042 merged = FALSE;
252b5132
RH
5043 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
5044 {
3f9a32bd 5045 struct bfd_elf_section_data *esdo = elf_section_data (o);
252b5132
RH
5046 o->reloc_count = 0;
5047
5048 for (p = o->link_order_head; p != NULL; p = p->next)
5049 {
3f9a32bd
AM
5050 unsigned int reloc_count = 0;
5051 struct bfd_elf_section_data *esdi = NULL;
5052 unsigned int *rel_count1;
5053
252b5132
RH
5054 if (p->type == bfd_section_reloc_link_order
5055 || p->type == bfd_symbol_reloc_link_order)
3f9a32bd 5056 reloc_count = 1;
252b5132
RH
5057 else if (p->type == bfd_indirect_link_order)
5058 {
5059 asection *sec;
5060
5061 sec = p->u.indirect.section;
3f9a32bd 5062 esdi = elf_section_data (sec);
252b5132
RH
5063
5064 /* Mark all sections which are to be included in the
5065 link. This will normally be every section. We need
5066 to do this so that we can identify any sections which
5067 the linker has decided to not include. */
b34976b6 5068 sec->linker_mark = TRUE;
252b5132 5069
f5fa8ca2 5070 if (sec->flags & SEC_MERGE)
b34976b6 5071 merged = TRUE;
f5fa8ca2 5072
a712da20 5073 if (info->relocateable || info->emitrelocations)
3f9a32bd 5074 reloc_count = sec->reloc_count;
c44233aa 5075 else if (bed->elf_backend_count_relocs)
9317eacc
CM
5076 {
5077 Elf_Internal_Rela * relocs;
5078
5079 relocs = (NAME(_bfd_elf,link_read_relocs)
5080 (abfd, sec, (PTR) NULL,
5081 (Elf_Internal_Rela *) NULL, info->keep_memory));
5082
3f9a32bd 5083 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
9317eacc 5084
6cdc0ccc 5085 if (elf_section_data (o)->relocs != relocs)
9317eacc
CM
5086 free (relocs);
5087 }
252b5132
RH
5088
5089 if (sec->_raw_size > max_contents_size)
5090 max_contents_size = sec->_raw_size;
5091 if (sec->_cooked_size > max_contents_size)
5092 max_contents_size = sec->_cooked_size;
5093
5094 /* We are interested in just local symbols, not all
5095 symbols. */
5096 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
5097 && (sec->owner->flags & DYNAMIC) == 0)
5098 {
5099 size_t sym_count;
5100
5101 if (elf_bad_symtab (sec->owner))
5102 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
5103 / sizeof (Elf_External_Sym));
5104 else
5105 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
5106
5107 if (sym_count > max_sym_count)
5108 max_sym_count = sym_count;
5109
9ad5cbcf
AM
5110 if (sym_count > max_sym_shndx_count
5111 && elf_symtab_shndx (sec->owner) != 0)
5112 max_sym_shndx_count = sym_count;
5113
252b5132
RH
5114 if ((sec->flags & SEC_RELOC) != 0)
5115 {
5116 size_t ext_size;
5117
5118 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
5119 if (ext_size > max_external_reloc_size)
5120 max_external_reloc_size = ext_size;
5121 if (sec->reloc_count > max_internal_reloc_count)
5122 max_internal_reloc_count = sec->reloc_count;
5123 }
5124 }
5125 }
3f9a32bd
AM
5126
5127 if (reloc_count == 0)
5128 continue;
5129
5130 o->reloc_count += reloc_count;
5131
5132 /* MIPS may have a mix of REL and RELA relocs on sections.
5133 To support this curious ABI we keep reloc counts in
5134 elf_section_data too. We must be careful to add the
5135 relocations from the input section to the right output
5136 count. FIXME: Get rid of one count. We have
5137 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
5138 rel_count1 = &esdo->rel_count;
5139 if (esdi != NULL)
5140 {
5141 bfd_boolean same_size;
5142 bfd_size_type entsize1;
5143
5144 entsize1 = esdi->rel_hdr.sh_entsize;
5145 BFD_ASSERT (entsize1 == sizeof (Elf_External_Rel)
5146 || entsize1 == sizeof (Elf_External_Rela));
5147 same_size = (!o->use_rela_p
5148 == (entsize1 == sizeof (Elf_External_Rel)));
5149
5150 if (!same_size)
5151 rel_count1 = &esdo->rel_count2;
5152
5153 if (esdi->rel_hdr2 != NULL)
5154 {
5155 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
5156 unsigned int alt_count;
5157 unsigned int *rel_count2;
5158
5159 BFD_ASSERT (entsize2 != entsize1
5160 && (entsize2 == sizeof (Elf_External_Rel)
5161 || entsize2 == sizeof (Elf_External_Rela)));
5162
5163 rel_count2 = &esdo->rel_count2;
5164 if (!same_size)
5165 rel_count2 = &esdo->rel_count;
5166
5167 /* The following is probably too simplistic if the
5168 backend counts output relocs unusually. */
5169 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
5170 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
5171 *rel_count2 += alt_count;
5172 reloc_count -= alt_count;
5173 }
5174 }
5175 *rel_count1 += reloc_count;
252b5132
RH
5176 }
5177
5178 if (o->reloc_count > 0)
5179 o->flags |= SEC_RELOC;
5180 else
5181 {
5182 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5183 set it (this is probably a bug) and if it is set
5184 assign_section_numbers will create a reloc section. */
5185 o->flags &=~ SEC_RELOC;
5186 }
5187
5188 /* If the SEC_ALLOC flag is not set, force the section VMA to
5189 zero. This is done in elf_fake_sections as well, but forcing
5190 the VMA to 0 here will ensure that relocs against these
5191 sections are handled correctly. */
5192 if ((o->flags & SEC_ALLOC) == 0
5193 && ! o->user_set_vma)
5194 o->vma = 0;
5195 }
5196
f5fa8ca2
JJ
5197 if (! info->relocateable && merged)
5198 elf_link_hash_traverse (elf_hash_table (info),
5199 elf_link_sec_merge_syms, (PTR) abfd);
5200
252b5132
RH
5201 /* Figure out the file positions for everything but the symbol table
5202 and the relocs. We set symcount to force assign_section_numbers
5203 to create a symbol table. */
5204 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
5205 BFD_ASSERT (! abfd->output_has_begun);
5206 if (! _bfd_elf_compute_section_file_positions (abfd, info))
5207 goto error_return;
5208
5209 /* That created the reloc sections. Set their sizes, and assign
5210 them file positions, and allocate some buffers. */
5211 for (o = abfd->sections; o != NULL; o = o->next)
5212 {
5213 if ((o->flags & SEC_RELOC) != 0)
5214 {
23bc299b
MM
5215 if (!elf_link_size_reloc_section (abfd,
5216 &elf_section_data (o)->rel_hdr,
5217 o))
252b5132
RH
5218 goto error_return;
5219
23bc299b
MM
5220 if (elf_section_data (o)->rel_hdr2
5221 && !elf_link_size_reloc_section (abfd,
5222 elf_section_data (o)->rel_hdr2,
5223 o))
252b5132 5224 goto error_return;
252b5132 5225 }
b037af20
MM
5226
5227 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
3e932841 5228 to count upwards while actually outputting the relocations. */
b037af20
MM
5229 elf_section_data (o)->rel_count = 0;
5230 elf_section_data (o)->rel_count2 = 0;
252b5132
RH
5231 }
5232
5233 _bfd_elf_assign_file_positions_for_relocs (abfd);
5234
5235 /* We have now assigned file positions for all the sections except
5236 .symtab and .strtab. We start the .symtab section at the current
5237 file position, and write directly to it. We build the .strtab
5238 section in memory. */
5239 bfd_get_symcount (abfd) = 0;
5240 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5241 /* sh_name is set in prep_headers. */
5242 symtab_hdr->sh_type = SHT_SYMTAB;
c97e73dd 5243 /* sh_flags, sh_addr and sh_size all start off zero. */
252b5132
RH
5244 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
5245 /* sh_link is set in assign_section_numbers. */
5246 /* sh_info is set below. */
5247 /* sh_offset is set just below. */
f0e1d18a 5248 symtab_hdr->sh_addralign = bed->s->file_align;
252b5132
RH
5249
5250 off = elf_tdata (abfd)->next_file_pos;
b34976b6 5251 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
252b5132
RH
5252
5253 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5254 incorrect. We do not yet know the size of the .symtab section.
5255 We correct next_file_pos below, after we do know the size. */
5256
5257 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5258 continuously seeking to the right position in the file. */
5259 if (! info->keep_memory || max_sym_count < 20)
5260 finfo.symbuf_size = 20;
5261 else
5262 finfo.symbuf_size = max_sym_count;
dc810e39
AM
5263 amt = finfo.symbuf_size;
5264 amt *= sizeof (Elf_External_Sym);
5265 finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
252b5132
RH
5266 if (finfo.symbuf == NULL)
5267 goto error_return;
9ad5cbcf
AM
5268 if (elf_numsections (abfd) > SHN_LORESERVE)
5269 {
c97e73dd
AM
5270 /* Wild guess at number of output symbols. realloc'd as needed. */
5271 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
5272 finfo.shndxbuf_size = amt;
9ad5cbcf 5273 amt *= sizeof (Elf_External_Sym_Shndx);
c97e73dd 5274 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9ad5cbcf
AM
5275 if (finfo.symshndxbuf == NULL)
5276 goto error_return;
5277 }
252b5132
RH
5278
5279 /* Start writing out the symbol table. The first symbol is always a
5280 dummy symbol. */
9317eacc
CM
5281 if (info->strip != strip_all
5282 || emit_relocs)
252b5132
RH
5283 {
5284 elfsym.st_value = 0;
5285 elfsym.st_size = 0;
5286 elfsym.st_info = 0;
5287 elfsym.st_other = 0;
5288 elfsym.st_shndx = SHN_UNDEF;
5289 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5290 &elfsym, bfd_und_section_ptr))
5291 goto error_return;
5292 }
5293
5294#if 0
5295 /* Some standard ELF linkers do this, but we don't because it causes
5296 bootstrap comparison failures. */
5297 /* Output a file symbol for the output file as the second symbol.
5298 We output this even if we are discarding local symbols, although
5299 I'm not sure if this is correct. */
5300 elfsym.st_value = 0;
5301 elfsym.st_size = 0;
5302 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5303 elfsym.st_other = 0;
5304 elfsym.st_shndx = SHN_ABS;
5305 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
5306 &elfsym, bfd_abs_section_ptr))
5307 goto error_return;
5308#endif
5309
5310 /* Output a symbol for each section. We output these even if we are
5311 discarding local symbols, since they are used for relocs. These
5312 symbols have no names. We store the index of each one in the
5313 index field of the section, so that we can find it again when
5314 outputting relocs. */
9317eacc
CM
5315 if (info->strip != strip_all
5316 || emit_relocs)
252b5132
RH
5317 {
5318 elfsym.st_size = 0;
5319 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5320 elfsym.st_other = 0;
9ad5cbcf 5321 for (i = 1; i < elf_numsections (abfd); i++)
252b5132
RH
5322 {
5323 o = section_from_elf_index (abfd, i);
5324 if (o != NULL)
5325 o->target_index = bfd_get_symcount (abfd);
5326 elfsym.st_shndx = i;
7ad34365 5327 if (info->relocateable || o == NULL)
252b5132
RH
5328 elfsym.st_value = 0;
5329 else
5330 elfsym.st_value = o->vma;
5331 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5332 &elfsym, o))
5333 goto error_return;
c97e73dd 5334 if (i == SHN_LORESERVE - 1)
9ad5cbcf 5335 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132
RH
5336 }
5337 }
5338
5339 /* Allocate some memory to hold information read in from the input
5340 files. */
9ad5cbcf
AM
5341 if (max_contents_size != 0)
5342 {
5343 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
5344 if (finfo.contents == NULL)
5345 goto error_return;
5346 }
5347
5348 if (max_external_reloc_size != 0)
5349 {
5350 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
5351 if (finfo.external_relocs == NULL)
5352 goto error_return;
5353 }
5354
5355 if (max_internal_reloc_count != 0)
5356 {
5357 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
5358 amt *= sizeof (Elf_Internal_Rela);
5359 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
5360 if (finfo.internal_relocs == NULL)
5361 goto error_return;
5362 }
5363
5364 if (max_sym_count != 0)
5365 {
5366 amt = max_sym_count * sizeof (Elf_External_Sym);
5367 finfo.external_syms = (Elf_External_Sym *) bfd_malloc (amt);
5368 if (finfo.external_syms == NULL)
5369 goto error_return;
5370
5371 amt = max_sym_count * sizeof (Elf_Internal_Sym);
5372 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
5373 if (finfo.internal_syms == NULL)
5374 goto error_return;
5375
5376 amt = max_sym_count * sizeof (long);
5377 finfo.indices = (long *) bfd_malloc (amt);
5378 if (finfo.indices == NULL)
5379 goto error_return;
5380
5381 amt = max_sym_count * sizeof (asection *);
5382 finfo.sections = (asection **) bfd_malloc (amt);
5383 if (finfo.sections == NULL)
5384 goto error_return;
5385 }
5386
5387 if (max_sym_shndx_count != 0)
5388 {
5389 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
5390 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5391 if (finfo.locsym_shndx == NULL)
5392 goto error_return;
5393 }
252b5132 5394
13ae64f3
JJ
5395 if (finfo.first_tls_sec)
5396 {
5397 unsigned int align = 0;
5398 bfd_vma base = finfo.first_tls_sec->vma, end = 0;
5399 asection *sec;
5400
5401 for (sec = finfo.first_tls_sec;
5402 sec && (sec->flags & SEC_THREAD_LOCAL);
5403 sec = sec->next)
5404 {
5405 bfd_vma size = sec->_raw_size;
5406
5407 if (bfd_get_section_alignment (abfd, sec) > align)
5408 align = bfd_get_section_alignment (abfd, sec);
5409 if (sec->_raw_size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
5410 {
5411 struct bfd_link_order *o;
5412
5413 size = 0;
5414 for (o = sec->link_order_head; o != NULL; o = o->next)
5415 if (size < o->offset + o->size)
58821868 5416 size = o->offset + o->size;
13ae64f3
JJ
5417 }
5418 end = sec->vma + size;
5419 }
5420 elf_hash_table (info)->tls_segment
5421 = bfd_zalloc (abfd, sizeof (struct elf_link_tls_segment));
5422 if (elf_hash_table (info)->tls_segment == NULL)
5423 goto error_return;
5424 elf_hash_table (info)->tls_segment->start = base;
5425 elf_hash_table (info)->tls_segment->size = end - base;
5426 elf_hash_table (info)->tls_segment->align = align;
5427 }
5428
252b5132
RH
5429 /* Since ELF permits relocations to be against local symbols, we
5430 must have the local symbols available when we do the relocations.
5431 Since we would rather only read the local symbols once, and we
5432 would rather not keep them in memory, we handle all the
5433 relocations for a single input file at the same time.
5434
5435 Unfortunately, there is no way to know the total number of local
5436 symbols until we have seen all of them, and the local symbol
5437 indices precede the global symbol indices. This means that when
5438 we are generating relocateable output, and we see a reloc against
5439 a global symbol, we can not know the symbol index until we have
5440 finished examining all the local symbols to see which ones we are
5441 going to output. To deal with this, we keep the relocations in
5442 memory, and don't output them until the end of the link. This is
5443 an unfortunate waste of memory, but I don't see a good way around
5444 it. Fortunately, it only happens when performing a relocateable
5445 link, which is not the common case. FIXME: If keep_memory is set
5446 we could write the relocs out and then read them again; I don't
5447 know how bad the memory loss will be. */
5448
5449 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
b34976b6 5450 sub->output_has_begun = FALSE;
252b5132
RH
5451 for (o = abfd->sections; o != NULL; o = o->next)
5452 {
5453 for (p = o->link_order_head; p != NULL; p = p->next)
5454 {
5455 if (p->type == bfd_indirect_link_order
a50c1845 5456 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
4ddafab0 5457 == bfd_target_elf_flavour)
a50c1845 5458 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
252b5132 5459 {
252b5132
RH
5460 if (! sub->output_has_begun)
5461 {
5462 if (! elf_link_input_bfd (&finfo, sub))
5463 goto error_return;
b34976b6 5464 sub->output_has_begun = TRUE;
252b5132
RH
5465 }
5466 }
5467 else if (p->type == bfd_section_reloc_link_order
5468 || p->type == bfd_symbol_reloc_link_order)
5469 {
5470 if (! elf_reloc_link_order (abfd, info, o, p))
5471 goto error_return;
5472 }
5473 else
5474 {
5475 if (! _bfd_default_link_order (abfd, info, o, p))
5476 goto error_return;
5477 }
5478 }
5479 }
5480
c44233aa
AM
5481 /* Output any global symbols that got converted to local in a
5482 version script or due to symbol visibility. We do this in a
5483 separate step since ELF requires all local symbols to appear
5484 prior to any global symbols. FIXME: We should only do this if
5485 some global symbols were, in fact, converted to become local.
5486 FIXME: Will this work correctly with the Irix 5 linker? */
b34976b6 5487 eoinfo.failed = FALSE;
c44233aa 5488 eoinfo.finfo = &finfo;
b34976b6 5489 eoinfo.localsyms = TRUE;
c44233aa
AM
5490 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5491 (PTR) &eoinfo);
5492 if (eoinfo.failed)
b34976b6 5493 return FALSE;
c44233aa 5494
252b5132 5495 /* That wrote out all the local symbols. Finish up the symbol table
5cc7c785
L
5496 with the global symbols. Even if we want to strip everything we
5497 can, we still need to deal with those global symbols that got
3e932841 5498 converted to local in a version script. */
252b5132 5499
30b30c21 5500 /* The sh_info field records the index of the first non local symbol. */
252b5132 5501 symtab_hdr->sh_info = bfd_get_symcount (abfd);
30b30c21 5502
fc8c40a0
AM
5503 if (dynamic
5504 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
30b30c21
RH
5505 {
5506 Elf_Internal_Sym sym;
5507 Elf_External_Sym *dynsym =
a7b97311 5508 (Elf_External_Sym *) finfo.dynsym_sec->contents;
71a40b32 5509 long last_local = 0;
30b30c21
RH
5510
5511 /* Write out the section symbols for the output sections. */
5512 if (info->shared)
5513 {
5514 asection *s;
5515
5516 sym.st_size = 0;
5517 sym.st_name = 0;
5518 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5519 sym.st_other = 0;
5520
5521 for (s = abfd->sections; s != NULL; s = s->next)
5522 {
5523 int indx;
9ad5cbcf
AM
5524 Elf_External_Sym *dest;
5525
30b30c21
RH
5526 indx = elf_section_data (s)->this_idx;
5527 BFD_ASSERT (indx > 0);
5528 sym.st_shndx = indx;
5529 sym.st_value = s->vma;
9ad5cbcf
AM
5530 dest = dynsym + elf_section_data (s)->dynindx;
5531 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
30b30c21
RH
5532 }
5533
5534 last_local = bfd_count_sections (abfd);
5535 }
5536
5537 /* Write out the local dynsyms. */
5538 if (elf_hash_table (info)->dynlocal)
5539 {
5540 struct elf_link_local_dynamic_entry *e;
5541 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
5542 {
318da145 5543 asection *s;
9ad5cbcf 5544 Elf_External_Sym *dest;
30b30c21 5545
b037af20
MM
5546 sym.st_size = e->isym.st_size;
5547 sym.st_other = e->isym.st_other;
5548
1fa0ddb3
RH
5549 /* Copy the internal symbol as is.
5550 Note that we saved a word of storage and overwrote
c44233aa
AM
5551 the original st_name with the dynstr_index. */
5552 sym = e->isym;
30b30c21 5553
c8e5ddc8 5554 if (e->isym.st_shndx != SHN_UNDEF
58821868
AM
5555 && (e->isym.st_shndx < SHN_LORESERVE
5556 || e->isym.st_shndx > SHN_HIRESERVE))
587ff49e
RH
5557 {
5558 s = bfd_section_from_elf_index (e->input_bfd,
5559 e->isym.st_shndx);
5560
5561 sym.st_shndx =
5562 elf_section_data (s->output_section)->this_idx;
5563 sym.st_value = (s->output_section->vma
5564 + s->output_offset
5565 + e->isym.st_value);
5566 }
30b30c21
RH
5567
5568 if (last_local < e->dynindx)
5569 last_local = e->dynindx;
5570
9ad5cbcf
AM
5571 dest = dynsym + e->dynindx;
5572 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
30b30c21
RH
5573 }
5574 }
5575
71a40b32
ILT
5576 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
5577 last_local + 1;
30b30c21 5578 }
252b5132
RH
5579
5580 /* We get the global symbols from the hash table. */
b34976b6
AM
5581 eoinfo.failed = FALSE;
5582 eoinfo.localsyms = FALSE;
252b5132
RH
5583 eoinfo.finfo = &finfo;
5584 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5585 (PTR) &eoinfo);
5586 if (eoinfo.failed)
b34976b6 5587 return FALSE;
252b5132 5588
587ff49e
RH
5589 /* If backend needs to output some symbols not present in the hash
5590 table, do it now. */
5591 if (bed->elf_backend_output_arch_syms)
5592 {
b34976b6
AM
5593 typedef bfd_boolean (*out_sym_func)
5594 PARAMS ((PTR, const char *, Elf_Internal_Sym *, asection *));
dc810e39
AM
5595
5596 if (! ((*bed->elf_backend_output_arch_syms)
5597 (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
b34976b6 5598 return FALSE;
3e932841 5599 }
587ff49e 5600
252b5132
RH
5601 /* Flush all symbols to the file. */
5602 if (! elf_link_flush_output_syms (&finfo))
b34976b6 5603 return FALSE;
252b5132
RH
5604
5605 /* Now we know the size of the symtab section. */
5606 off += symtab_hdr->sh_size;
5607
c97e73dd
AM
5608 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5609 if (symtab_shndx_hdr->sh_name != 0)
5610 {
5611 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5612 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5613 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5614 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
5615 symtab_shndx_hdr->sh_size = amt;
5616
5617 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
b34976b6 5618 off, TRUE);
c97e73dd
AM
5619
5620 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
5621 || (bfd_bwrite ((PTR) finfo.symshndxbuf, amt, abfd) != amt))
b34976b6 5622 return FALSE;
c97e73dd
AM
5623 }
5624
5625
252b5132
RH
5626 /* Finish up and write out the symbol string table (.strtab)
5627 section. */
5628 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5629 /* sh_name was set in prep_headers. */
5630 symstrtab_hdr->sh_type = SHT_STRTAB;
5631 symstrtab_hdr->sh_flags = 0;
5632 symstrtab_hdr->sh_addr = 0;
5633 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
5634 symstrtab_hdr->sh_entsize = 0;
5635 symstrtab_hdr->sh_link = 0;
5636 symstrtab_hdr->sh_info = 0;
5637 /* sh_offset is set just below. */
5638 symstrtab_hdr->sh_addralign = 1;
5639
b34976b6 5640 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
252b5132
RH
5641 elf_tdata (abfd)->next_file_pos = off;
5642
5643 if (bfd_get_symcount (abfd) > 0)
5644 {
5645 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
5646 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
b34976b6 5647 return FALSE;
252b5132
RH
5648 }
5649
5650 /* Adjust the relocs to have the correct symbol indices. */
5651 for (o = abfd->sections; o != NULL; o = o->next)
5652 {
252b5132
RH
5653 if ((o->flags & SEC_RELOC) == 0)
5654 continue;
5655
3e932841 5656 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
31367b81
MM
5657 elf_section_data (o)->rel_count,
5658 elf_section_data (o)->rel_hashes);
5659 if (elf_section_data (o)->rel_hdr2 != NULL)
5660 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
5661 elf_section_data (o)->rel_count2,
3e932841 5662 (elf_section_data (o)->rel_hashes
31367b81 5663 + elf_section_data (o)->rel_count));
252b5132
RH
5664
5665 /* Set the reloc_count field to 0 to prevent write_relocs from
5666 trying to swap the relocs out itself. */
5667 o->reloc_count = 0;
5668 }
5669
db6751f2
JJ
5670 if (dynamic && info->combreloc && dynobj != NULL)
5671 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
5672
252b5132
RH
5673 /* If we are linking against a dynamic object, or generating a
5674 shared library, finish up the dynamic linking information. */
5675 if (dynamic)
5676 {
5677 Elf_External_Dyn *dyncon, *dynconend;
5678
5679 /* Fix up .dynamic entries. */
5680 o = bfd_get_section_by_name (dynobj, ".dynamic");
5681 BFD_ASSERT (o != NULL);
5682
5683 dyncon = (Elf_External_Dyn *) o->contents;
5684 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5685 for (; dyncon < dynconend; dyncon++)
5686 {
5687 Elf_Internal_Dyn dyn;
5688 const char *name;
5689 unsigned int type;
5690
5691 elf_swap_dyn_in (dynobj, dyncon, &dyn);
5692
5693 switch (dyn.d_tag)
5694 {
5695 default:
5696 break;
db6751f2
JJ
5697 case DT_NULL:
5698 if (relativecount > 0 && dyncon + 1 < dynconend)
5699 {
5700 switch (elf_section_data (reldyn)->this_hdr.sh_type)
5701 {
5702 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
5703 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
5704 default: break;
5705 }
5706 if (dyn.d_tag != DT_NULL)
5707 {
5708 dyn.d_un.d_val = relativecount;
5709 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5710 relativecount = 0;
5711 }
5712 }
5713 break;
252b5132 5714 case DT_INIT:
f0c2e336 5715 name = info->init_function;
252b5132
RH
5716 goto get_sym;
5717 case DT_FINI:
f0c2e336 5718 name = info->fini_function;
252b5132
RH
5719 get_sym:
5720 {
5721 struct elf_link_hash_entry *h;
5722
5723 h = elf_link_hash_lookup (elf_hash_table (info), name,
b34976b6 5724 FALSE, FALSE, TRUE);
252b5132
RH
5725 if (h != NULL
5726 && (h->root.type == bfd_link_hash_defined
5727 || h->root.type == bfd_link_hash_defweak))
5728 {
5729 dyn.d_un.d_val = h->root.u.def.value;
5730 o = h->root.u.def.section;
5731 if (o->output_section != NULL)
5732 dyn.d_un.d_val += (o->output_section->vma
5733 + o->output_offset);
5734 else
5735 {
5736 /* The symbol is imported from another shared
5737 library and does not apply to this one. */
5738 dyn.d_un.d_val = 0;
5739 }
5740
5741 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5742 }
5743 }
5744 break;
5745
30831527
RH
5746 case DT_PREINIT_ARRAYSZ:
5747 name = ".preinit_array";
5748 goto get_size;
5749 case DT_INIT_ARRAYSZ:
5750 name = ".init_array";
5751 goto get_size;
5752 case DT_FINI_ARRAYSZ:
5753 name = ".fini_array";
5754 get_size:
5755 o = bfd_get_section_by_name (abfd, name);
2cb69dd3
JL
5756 if (o == NULL)
5757 {
5758 (*_bfd_error_handler)
5759 (_("%s: could not find output section %s"),
5760 bfd_get_filename (abfd), name);
5761 goto error_return;
5762 }
25e27870
L
5763 if (o->_raw_size == 0)
5764 (*_bfd_error_handler)
5765 (_("warning: %s section has zero size"), name);
30831527
RH
5766 dyn.d_un.d_val = o->_raw_size;
5767 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5768 break;
5769
5770 case DT_PREINIT_ARRAY:
5771 name = ".preinit_array";
5772 goto get_vma;
5773 case DT_INIT_ARRAY:
5774 name = ".init_array";
5775 goto get_vma;
5776 case DT_FINI_ARRAY:
5777 name = ".fini_array";
5778 goto get_vma;
5779
252b5132
RH
5780 case DT_HASH:
5781 name = ".hash";
5782 goto get_vma;
5783 case DT_STRTAB:
5784 name = ".dynstr";
5785 goto get_vma;
5786 case DT_SYMTAB:
5787 name = ".dynsym";
5788 goto get_vma;
5789 case DT_VERDEF:
5790 name = ".gnu.version_d";
5791 goto get_vma;
5792 case DT_VERNEED:
5793 name = ".gnu.version_r";
5794 goto get_vma;
5795 case DT_VERSYM:
5796 name = ".gnu.version";
5797 get_vma:
5798 o = bfd_get_section_by_name (abfd, name);
2cb69dd3
JL
5799 if (o == NULL)
5800 {
5801 (*_bfd_error_handler)
5802 (_("%s: could not find output section %s"),
5803 bfd_get_filename (abfd), name);
5804 goto error_return;
5805 }
252b5132
RH
5806 dyn.d_un.d_ptr = o->vma;
5807 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5808 break;
5809
5810 case DT_REL:
5811 case DT_RELA:
5812 case DT_RELSZ:
5813 case DT_RELASZ:
5814 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5815 type = SHT_REL;
5816 else
5817 type = SHT_RELA;
5818 dyn.d_un.d_val = 0;
9ad5cbcf 5819 for (i = 1; i < elf_numsections (abfd); i++)
252b5132
RH
5820 {
5821 Elf_Internal_Shdr *hdr;
5822
5823 hdr = elf_elfsections (abfd)[i];
5824 if (hdr->sh_type == type
5825 && (hdr->sh_flags & SHF_ALLOC) != 0)
5826 {
5827 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5828 dyn.d_un.d_val += hdr->sh_size;
5829 else
5830 {
5831 if (dyn.d_un.d_val == 0
5832 || hdr->sh_addr < dyn.d_un.d_val)
5833 dyn.d_un.d_val = hdr->sh_addr;
5834 }
5835 }
5836 }
5837 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5838 break;
5839 }
5840 }
5841 }
5842
5843 /* If we have created any dynamic sections, then output them. */
5844 if (dynobj != NULL)
5845 {
5846 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5847 goto error_return;
5848
5849 for (o = dynobj->sections; o != NULL; o = o->next)
5850 {
5851 if ((o->flags & SEC_HAS_CONTENTS) == 0
fc8c40a0
AM
5852 || o->_raw_size == 0
5853 || o->output_section == bfd_abs_section_ptr)
252b5132
RH
5854 continue;
5855 if ((o->flags & SEC_LINKER_CREATED) == 0)
5856 {
5857 /* At this point, we are only interested in sections
c44233aa 5858 created by elf_link_create_dynamic_sections. */
252b5132
RH
5859 continue;
5860 }
5861 if ((elf_section_data (o->output_section)->this_hdr.sh_type
5862 != SHT_STRTAB)
5863 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
5864 {
5865 if (! bfd_set_section_contents (abfd, o->output_section,
dc810e39
AM
5866 o->contents,
5867 (file_ptr) o->output_offset,
252b5132
RH
5868 o->_raw_size))
5869 goto error_return;
5870 }
5871 else
5872 {
252b5132 5873 /* The contents of the .dynstr section are actually in a
c44233aa 5874 stringtab. */
252b5132
RH
5875 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
5876 if (bfd_seek (abfd, off, SEEK_SET) != 0
2b0f7ef9
JJ
5877 || ! _bfd_elf_strtab_emit (abfd,
5878 elf_hash_table (info)->dynstr))
252b5132
RH
5879 goto error_return;
5880 }
5881 }
5882 }
5883
1126897b
AM
5884 if (info->relocateable)
5885 {
b34976b6 5886 bfd_boolean failed = FALSE;
1126897b
AM
5887
5888 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
5889 if (failed)
5890 goto error_return;
5891 }
5892
252b5132
RH
5893 /* If we have optimized stabs strings, output them. */
5894 if (elf_hash_table (info)->stab_info != NULL)
5895 {
5896 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
5897 goto error_return;
5898 }
5899
126495ed 5900 if (info->eh_frame_hdr)
65765700 5901 {
126495ed
AM
5902 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
5903 goto error_return;
65765700
JJ
5904 }
5905
252b5132
RH
5906 if (finfo.symstrtab != NULL)
5907 _bfd_stringtab_free (finfo.symstrtab);
5908 if (finfo.contents != NULL)
5909 free (finfo.contents);
5910 if (finfo.external_relocs != NULL)
5911 free (finfo.external_relocs);
5912 if (finfo.internal_relocs != NULL)
5913 free (finfo.internal_relocs);
5914 if (finfo.external_syms != NULL)
5915 free (finfo.external_syms);
9ad5cbcf
AM
5916 if (finfo.locsym_shndx != NULL)
5917 free (finfo.locsym_shndx);
252b5132
RH
5918 if (finfo.internal_syms != NULL)
5919 free (finfo.internal_syms);
5920 if (finfo.indices != NULL)
5921 free (finfo.indices);
5922 if (finfo.sections != NULL)
5923 free (finfo.sections);
5924 if (finfo.symbuf != NULL)
5925 free (finfo.symbuf);
9ad5cbcf 5926 if (finfo.symshndxbuf != NULL)
c97e73dd 5927 free (finfo.symshndxbuf);
252b5132
RH
5928 for (o = abfd->sections; o != NULL; o = o->next)
5929 {
5930 if ((o->flags & SEC_RELOC) != 0
5931 && elf_section_data (o)->rel_hashes != NULL)
c44233aa 5932 free (elf_section_data (o)->rel_hashes);
252b5132
RH
5933 }
5934
b34976b6 5935 elf_tdata (abfd)->linker = TRUE;
252b5132 5936
b34976b6 5937 return TRUE;
252b5132
RH
5938
5939 error_return:
5940 if (finfo.symstrtab != NULL)
5941 _bfd_stringtab_free (finfo.symstrtab);
5942 if (finfo.contents != NULL)
5943 free (finfo.contents);
5944 if (finfo.external_relocs != NULL)
5945 free (finfo.external_relocs);
5946 if (finfo.internal_relocs != NULL)
5947 free (finfo.internal_relocs);
5948 if (finfo.external_syms != NULL)
5949 free (finfo.external_syms);
9ad5cbcf
AM
5950 if (finfo.locsym_shndx != NULL)
5951 free (finfo.locsym_shndx);
252b5132
RH
5952 if (finfo.internal_syms != NULL)
5953 free (finfo.internal_syms);
5954 if (finfo.indices != NULL)
5955 free (finfo.indices);
5956 if (finfo.sections != NULL)
5957 free (finfo.sections);
5958 if (finfo.symbuf != NULL)
5959 free (finfo.symbuf);
9ad5cbcf 5960 if (finfo.symshndxbuf != NULL)
c97e73dd 5961 free (finfo.symshndxbuf);
252b5132
RH
5962 for (o = abfd->sections; o != NULL; o = o->next)
5963 {
5964 if ((o->flags & SEC_RELOC) != 0
5965 && elf_section_data (o)->rel_hashes != NULL)
5966 free (elf_section_data (o)->rel_hashes);
5967 }
5968
b34976b6 5969 return FALSE;
252b5132
RH
5970}
5971
5972/* Add a symbol to the output symbol table. */
5973
b34976b6 5974static bfd_boolean
252b5132
RH
5975elf_link_output_sym (finfo, name, elfsym, input_sec)
5976 struct elf_final_link_info *finfo;
5977 const char *name;
5978 Elf_Internal_Sym *elfsym;
5979 asection *input_sec;
5980{
9ad5cbcf
AM
5981 Elf_External_Sym *dest;
5982 Elf_External_Sym_Shndx *destshndx;
b34976b6
AM
5983 bfd_boolean (*output_symbol_hook)
5984 PARAMS ((bfd *, struct bfd_link_info *info, const char *,
5985 Elf_Internal_Sym *, asection *));
252b5132
RH
5986
5987 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
5988 elf_backend_link_output_symbol_hook;
5989 if (output_symbol_hook != NULL)
5990 {
5991 if (! ((*output_symbol_hook)
5992 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
b34976b6 5993 return FALSE;
252b5132
RH
5994 }
5995
5996 if (name == (const char *) NULL || *name == '\0')
5997 elfsym->st_name = 0;
5998 else if (input_sec->flags & SEC_EXCLUDE)
5999 elfsym->st_name = 0;
6000 else
6001 {
6002 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
b34976b6 6003 name, TRUE, FALSE);
252b5132 6004 if (elfsym->st_name == (unsigned long) -1)
b34976b6 6005 return FALSE;
252b5132
RH
6006 }
6007
6008 if (finfo->symbuf_count >= finfo->symbuf_size)
6009 {
6010 if (! elf_link_flush_output_syms (finfo))
b34976b6 6011 return FALSE;
252b5132
RH
6012 }
6013
9ad5cbcf
AM
6014 dest = finfo->symbuf + finfo->symbuf_count;
6015 destshndx = finfo->symshndxbuf;
6016 if (destshndx != NULL)
c97e73dd
AM
6017 {
6018 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6019 {
6020 bfd_size_type amt;
252b5132 6021
c97e73dd
AM
6022 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6023 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6024 if (destshndx == NULL)
b34976b6 6025 return FALSE;
c97e73dd
AM
6026 memset ((char *) destshndx + amt, 0, amt);
6027 finfo->shndxbuf_size *= 2;
6028 }
6029 destshndx += bfd_get_symcount (finfo->output_bfd);
6030 }
6031
6032 elf_swap_symbol_out (finfo->output_bfd, elfsym, (PTR) dest, (PTR) destshndx);
6033 finfo->symbuf_count += 1;
6034 bfd_get_symcount (finfo->output_bfd) += 1;
252b5132 6035
b34976b6 6036 return TRUE;
252b5132
RH
6037}
6038
6039/* Flush the output symbols to the file. */
6040
b34976b6 6041static bfd_boolean
252b5132
RH
6042elf_link_flush_output_syms (finfo)
6043 struct elf_final_link_info *finfo;
6044{
6045 if (finfo->symbuf_count > 0)
6046 {
9ad5cbcf 6047 Elf_Internal_Shdr *hdr;
dc810e39
AM
6048 file_ptr pos;
6049 bfd_size_type amt;
252b5132 6050
9ad5cbcf
AM
6051 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6052 pos = hdr->sh_offset + hdr->sh_size;
dc810e39
AM
6053 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
6054 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6055 || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
b34976b6 6056 return FALSE;
252b5132 6057
9ad5cbcf 6058 hdr->sh_size += amt;
252b5132
RH
6059 finfo->symbuf_count = 0;
6060 }
6061
b34976b6 6062 return TRUE;
252b5132
RH
6063}
6064
f5fa8ca2
JJ
6065/* Adjust all external symbols pointing into SEC_MERGE sections
6066 to reflect the object merging within the sections. */
6067
b34976b6 6068static bfd_boolean
f5fa8ca2
JJ
6069elf_link_sec_merge_syms (h, data)
6070 struct elf_link_hash_entry *h;
6071 PTR data;
6072{
6073 asection *sec;
6074
e92d460e
AM
6075 if (h->root.type == bfd_link_hash_warning)
6076 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6077
f5fa8ca2
JJ
6078 if ((h->root.type == bfd_link_hash_defined
6079 || h->root.type == bfd_link_hash_defweak)
6080 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
68bfbfcc 6081 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
f5fa8ca2
JJ
6082 {
6083 bfd *output_bfd = (bfd *) data;
6084
6085 h->root.u.def.value =
6086 _bfd_merged_section_offset (output_bfd,
6087 &h->root.u.def.section,
65765700 6088 elf_section_data (sec)->sec_info,
f5fa8ca2
JJ
6089 h->root.u.def.value, (bfd_vma) 0);
6090 }
6091
b34976b6 6092 return TRUE;
f5fa8ca2
JJ
6093}
6094
f5d44ba0
AM
6095/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6096 allowing an unsatisfied unversioned symbol in the DSO to match a
6097 versioned symbol that would normally require an explicit version. */
6098
b34976b6 6099static bfd_boolean
f5d44ba0
AM
6100elf_link_check_versioned_symbol (info, h)
6101 struct bfd_link_info *info;
6102 struct elf_link_hash_entry *h;
6103{
6104 bfd *undef_bfd = h->root.u.undef.abfd;
6105 struct elf_link_loaded_list *loaded;
f5d44ba0
AM
6106
6107 if ((undef_bfd->flags & DYNAMIC) == 0
6108 || info->hash->creator->flavour != bfd_target_elf_flavour
6109 || elf_dt_soname (h->root.u.undef.abfd) == NULL)
b34976b6 6110 return FALSE;
f5d44ba0
AM
6111
6112 for (loaded = elf_hash_table (info)->loaded;
6113 loaded != NULL;
6114 loaded = loaded->next)
6115 {
6116 bfd *input;
6117 Elf_Internal_Shdr *hdr;
6118 bfd_size_type symcount;
6119 bfd_size_type extsymcount;
6120 bfd_size_type extsymoff;
6121 Elf_Internal_Shdr *versymhdr;
6cdc0ccc
AM
6122 Elf_Internal_Sym *isym;
6123 Elf_Internal_Sym *isymend;
6124 Elf_Internal_Sym *isymbuf;
f5d44ba0 6125 Elf_External_Versym *ever;
6cdc0ccc 6126 Elf_External_Versym *extversym;
f5d44ba0
AM
6127
6128 input = loaded->abfd;
6129
6130 /* We check each DSO for a possible hidden versioned definition. */
6131 if (input == undef_bfd
6132 || (input->flags & DYNAMIC) == 0
6133 || elf_dynversym (input) == 0)
6134 continue;
6135
6136 hdr = &elf_tdata (input)->dynsymtab_hdr;
6137
6138 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
6139 if (elf_bad_symtab (input))
6140 {
6141 extsymcount = symcount;
6142 extsymoff = 0;
6143 }
6144 else
6145 {
6146 extsymcount = symcount - hdr->sh_info;
6147 extsymoff = hdr->sh_info;
6148 }
6149
6150 if (extsymcount == 0)
6151 continue;
6152
6cdc0ccc
AM
6153 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6154 NULL, NULL, NULL);
6155 if (isymbuf == NULL)
b34976b6 6156 return FALSE;
f5d44ba0 6157
f5d44ba0
AM
6158 /* Read in any version definitions. */
6159 versymhdr = &elf_tdata (input)->dynversym_hdr;
6160 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
6161 if (extversym == NULL)
6162 goto error_ret;
6163
6164 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6165 || (bfd_bread ((PTR) extversym, versymhdr->sh_size, input)
6166 != versymhdr->sh_size))
6167 {
6168 free (extversym);
6169 error_ret:
6cdc0ccc 6170 free (isymbuf);
b34976b6 6171 return FALSE;
f5d44ba0
AM
6172 }
6173
6174 ever = extversym + extsymoff;
6cdc0ccc
AM
6175 isymend = isymbuf + extsymcount;
6176 for (isym = isymbuf; isym < isymend; isym++, ever++)
f5d44ba0
AM
6177 {
6178 const char *name;
f5d44ba0
AM
6179 Elf_Internal_Versym iver;
6180
6cdc0ccc
AM
6181 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6182 || isym->st_shndx == SHN_UNDEF)
f5d44ba0
AM
6183 continue;
6184
6185 name = bfd_elf_string_from_elf_section (input,
6186 hdr->sh_link,
6cdc0ccc 6187 isym->st_name);
f5d44ba0
AM
6188 if (strcmp (name, h->root.root.string) != 0)
6189 continue;
6190
6191 _bfd_elf_swap_versym_in (input, ever, &iver);
6192
6193 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6194 {
6195 /* If we have a non-hidden versioned sym, then it should
6196 have provided a definition for the undefined sym. */
6197 abort ();
6198 }
6199
6200 if ((iver.vs_vers & VERSYM_VERSION) == 2)
6201 {
6202 /* This is the oldest (default) sym. We can use it. */
6203 free (extversym);
6cdc0ccc 6204 free (isymbuf);
b34976b6 6205 return TRUE;
f5d44ba0
AM
6206 }
6207 }
6208
6209 free (extversym);
6cdc0ccc 6210 free (isymbuf);
f5d44ba0
AM
6211 }
6212
b34976b6 6213 return FALSE;
f5d44ba0
AM
6214}
6215
252b5132
RH
6216/* Add an external symbol to the symbol table. This is called from
6217 the hash table traversal routine. When generating a shared object,
6218 we go through the symbol table twice. The first time we output
6219 anything that might have been forced to local scope in a version
6220 script. The second time we output the symbols that are still
6221 global symbols. */
6222
b34976b6 6223static bfd_boolean
252b5132
RH
6224elf_link_output_extsym (h, data)
6225 struct elf_link_hash_entry *h;
6226 PTR data;
6227{
6228 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
6229 struct elf_final_link_info *finfo = eoinfo->finfo;
b34976b6 6230 bfd_boolean strip;
252b5132
RH
6231 Elf_Internal_Sym sym;
6232 asection *input_sec;
6233
e92d460e
AM
6234 if (h->root.type == bfd_link_hash_warning)
6235 {
6236 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6237 if (h->root.type == bfd_link_hash_new)
b34976b6 6238 return TRUE;
e92d460e
AM
6239 }
6240
252b5132
RH
6241 /* Decide whether to output this symbol in this pass. */
6242 if (eoinfo->localsyms)
6243 {
6244 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
b34976b6 6245 return TRUE;
252b5132
RH
6246 }
6247 else
6248 {
6249 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
b34976b6 6250 return TRUE;
252b5132
RH
6251 }
6252
6253 /* If we are not creating a shared library, and this symbol is
6254 referenced by a shared library but is not defined anywhere, then
6255 warn that it is undefined. If we do not do this, the runtime
6256 linker will complain that the symbol is undefined when the
6257 program is run. We don't have to worry about symbols that are
6258 referenced by regular files, because we will already have issued
6259 warnings for them. */
6260 if (! finfo->info->relocateable
ae9a127f 6261 && (! finfo->info->shared || ! finfo->info->allow_shlib_undefined)
252b5132
RH
6262 && h->root.type == bfd_link_hash_undefined
6263 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
f5d44ba0
AM
6264 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
6265 && ! elf_link_check_versioned_symbol (finfo->info, h))
252b5132
RH
6266 {
6267 if (! ((*finfo->info->callbacks->undefined_symbol)
6268 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
b34976b6 6269 (asection *) NULL, (bfd_vma) 0, TRUE)))
252b5132 6270 {
b34976b6
AM
6271 eoinfo->failed = TRUE;
6272 return FALSE;
252b5132
RH
6273 }
6274 }
6275
6276 /* We don't want to output symbols that have never been mentioned by
6277 a regular file, or that we have been told to strip. However, if
6278 h->indx is set to -2, the symbol is used by a reloc and we must
6279 output it. */
6280 if (h->indx == -2)
b34976b6 6281 strip = FALSE;
252b5132
RH
6282 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6283 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
6284 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
6285 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
b34976b6 6286 strip = TRUE;
75828352
AM
6287 else if (finfo->info->strip == strip_all)
6288 strip = TRUE;
6289 else if (finfo->info->strip == strip_some
6290 && bfd_hash_lookup (finfo->info->keep_hash,
6291 h->root.root.string, FALSE, FALSE) == NULL)
6292 strip = TRUE;
6293 else if (finfo->info->strip_discarded
6294 && (h->root.type == bfd_link_hash_defined
6295 || h->root.type == bfd_link_hash_defweak)
6296 && elf_discarded_section (h->root.u.def.section))
b34976b6 6297 strip = TRUE;
252b5132 6298 else
b34976b6 6299 strip = FALSE;
252b5132
RH
6300
6301 /* If we're stripping it, and it's not a dynamic symbol, there's
2bd171e0
ILT
6302 nothing else to do unless it is a forced local symbol. */
6303 if (strip
6304 && h->dynindx == -1
6305 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
b34976b6 6306 return TRUE;
252b5132
RH
6307
6308 sym.st_value = 0;
6309 sym.st_size = h->size;
6310 sym.st_other = h->other;
6311 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6312 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6313 else if (h->root.type == bfd_link_hash_undefweak
6314 || h->root.type == bfd_link_hash_defweak)
6315 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6316 else
6317 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6318
6319 switch (h->root.type)
6320 {
6321 default:
6322 case bfd_link_hash_new:
e92d460e 6323 case bfd_link_hash_warning:
252b5132 6324 abort ();
b34976b6 6325 return FALSE;
252b5132
RH
6326
6327 case bfd_link_hash_undefined:
252b5132
RH
6328 case bfd_link_hash_undefweak:
6329 input_sec = bfd_und_section_ptr;
6330 sym.st_shndx = SHN_UNDEF;
6331 break;
6332
6333 case bfd_link_hash_defined:
6334 case bfd_link_hash_defweak:
6335 {
6336 input_sec = h->root.u.def.section;
6337 if (input_sec->output_section != NULL)
6338 {
6339 sym.st_shndx =
6340 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6341 input_sec->output_section);
9ad5cbcf 6342 if (sym.st_shndx == SHN_BAD)
252b5132
RH
6343 {
6344 (*_bfd_error_handler)
6345 (_("%s: could not find output section %s for input section %s"),
6346 bfd_get_filename (finfo->output_bfd),
6347 input_sec->output_section->name,
6348 input_sec->name);
b34976b6
AM
6349 eoinfo->failed = TRUE;
6350 return FALSE;
252b5132
RH
6351 }
6352
6353 /* ELF symbols in relocateable files are section relative,
6354 but in nonrelocateable files they are virtual
6355 addresses. */
6356 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6357 if (! finfo->info->relocateable)
13ae64f3
JJ
6358 {
6359 sym.st_value += input_sec->output_section->vma;
6360 if (h->type == STT_TLS)
6361 {
6362 /* STT_TLS symbols are relative to PT_TLS segment
6363 base. */
6364 BFD_ASSERT (finfo->first_tls_sec != NULL);
6365 sym.st_value -= finfo->first_tls_sec->vma;
6366 }
6367 }
252b5132
RH
6368 }
6369 else
6370 {
6371 BFD_ASSERT (input_sec->owner == NULL
6372 || (input_sec->owner->flags & DYNAMIC) != 0);
6373 sym.st_shndx = SHN_UNDEF;
6374 input_sec = bfd_und_section_ptr;
6375 }
6376 }
6377 break;
6378
6379 case bfd_link_hash_common:
6380 input_sec = h->root.u.c.p->section;
6381 sym.st_shndx = SHN_COMMON;
6382 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6383 break;
6384
6385 case bfd_link_hash_indirect:
6386 /* These symbols are created by symbol versioning. They point
c44233aa
AM
6387 to the decorated version of the name. For example, if the
6388 symbol foo@@GNU_1.2 is the default, which should be used when
6389 foo is used with no version, then we add an indirect symbol
6390 foo which points to foo@@GNU_1.2. We ignore these symbols,
6391 since the indirected symbol is already in the hash table. */
b34976b6 6392 return TRUE;
252b5132
RH
6393 }
6394
6395 /* Give the processor backend a chance to tweak the symbol value,
6396 and also to finish up anything that needs to be done for this
c44233aa
AM
6397 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6398 forced local syms when non-shared is due to a historical quirk. */
252b5132
RH
6399 if ((h->dynindx != -1
6400 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
c44233aa
AM
6401 && (finfo->info->shared
6402 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
252b5132
RH
6403 && elf_hash_table (finfo->info)->dynamic_sections_created)
6404 {
6405 struct elf_backend_data *bed;
6406
6407 bed = get_elf_backend_data (finfo->output_bfd);
6408 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6409 (finfo->output_bfd, finfo->info, h, &sym)))
6410 {
b34976b6
AM
6411 eoinfo->failed = TRUE;
6412 return FALSE;
252b5132
RH
6413 }
6414 }
6415
6416 /* If we are marking the symbol as undefined, and there are no
6417 non-weak references to this symbol from a regular object, then
91d3970e
ILT
6418 mark the symbol as weak undefined; if there are non-weak
6419 references, mark the symbol as strong. We can't do this earlier,
252b5132
RH
6420 because it might not be marked as undefined until the
6421 finish_dynamic_symbol routine gets through with it. */
6422 if (sym.st_shndx == SHN_UNDEF
252b5132 6423 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
a7b97311
AM
6424 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6425 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
91d3970e
ILT
6426 {
6427 int bindtype;
6428
6429 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6430 bindtype = STB_GLOBAL;
6431 else
6432 bindtype = STB_WEAK;
6433 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6434 }
252b5132 6435
7c25b72c 6436 /* If a symbol is not defined locally, we clear the visibility field. */
2cd533b7
L
6437 if (! finfo->info->relocateable
6438 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
7c25b72c 6439 sym.st_other &= ~ ELF_ST_VISIBILITY (-1);
32c092c3 6440
252b5132 6441 /* If this symbol should be put in the .dynsym section, then put it
f5d44ba0
AM
6442 there now. We already know the symbol index. We also fill in
6443 the entry in the .hash section. */
252b5132
RH
6444 if (h->dynindx != -1
6445 && elf_hash_table (finfo->info)->dynamic_sections_created)
6446 {
6447 size_t bucketcount;
6448 size_t bucket;
c7ac6ff8 6449 size_t hash_entry_size;
252b5132
RH
6450 bfd_byte *bucketpos;
6451 bfd_vma chain;
dc810e39 6452 Elf_External_Sym *esym;
252b5132
RH
6453
6454 sym.st_name = h->dynstr_index;
dc810e39 6455 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
9ad5cbcf 6456 elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym, (PTR) 0);
252b5132
RH
6457
6458 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6459 bucket = h->elf_hash_value % bucketcount;
3e932841 6460 hash_entry_size
c7ac6ff8 6461 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
252b5132 6462 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
c7ac6ff8
MM
6463 + (bucket + 2) * hash_entry_size);
6464 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
dc810e39
AM
6465 bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
6466 bucketpos);
c7ac6ff8
MM
6467 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6468 ((bfd_byte *) finfo->hash_sec->contents
6469 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
252b5132
RH
6470
6471 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6472 {
6473 Elf_Internal_Versym iversym;
dc810e39 6474 Elf_External_Versym *eversym;
252b5132
RH
6475
6476 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6477 {
6478 if (h->verinfo.verdef == NULL)
6479 iversym.vs_vers = 0;
6480 else
6481 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6482 }
6483 else
6484 {
6485 if (h->verinfo.vertree == NULL)
6486 iversym.vs_vers = 1;
6487 else
6488 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6489 }
6490
6491 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6492 iversym.vs_vers |= VERSYM_HIDDEN;
6493
dc810e39
AM
6494 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6495 eversym += h->dynindx;
6496 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
252b5132
RH
6497 }
6498 }
6499
6500 /* If we're stripping it, then it was just a dynamic symbol, and
6501 there's nothing else to do. */
7330fb86 6502 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
b34976b6 6503 return TRUE;
252b5132
RH
6504
6505 h->indx = bfd_get_symcount (finfo->output_bfd);
6506
6507 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
6508 {
b34976b6
AM
6509 eoinfo->failed = TRUE;
6510 return FALSE;
252b5132
RH
6511 }
6512
b34976b6 6513 return TRUE;
252b5132
RH
6514}
6515
23bc299b
MM
6516/* Copy the relocations indicated by the INTERNAL_RELOCS (which
6517 originated from the section given by INPUT_REL_HDR) to the
6518 OUTPUT_BFD. */
6519
b34976b6 6520static bfd_boolean
3e932841 6521elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
23bc299b
MM
6522 internal_relocs)
6523 bfd *output_bfd;
6524 asection *input_section;
6525 Elf_Internal_Shdr *input_rel_hdr;
6526 Elf_Internal_Rela *internal_relocs;
6527{
6528 Elf_Internal_Rela *irela;
6529 Elf_Internal_Rela *irelaend;
947216bf 6530 bfd_byte *erel;
23bc299b
MM
6531 Elf_Internal_Shdr *output_rel_hdr;
6532 asection *output_section;
7442e600 6533 unsigned int *rel_countp = NULL;
32f0787a 6534 struct elf_backend_data *bed;
947216bf 6535 void (*swap_out) PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *));
23bc299b
MM
6536
6537 output_section = input_section->output_section;
6538 output_rel_hdr = NULL;
6539
3e932841 6540 if (elf_section_data (output_section)->rel_hdr.sh_entsize
23bc299b
MM
6541 == input_rel_hdr->sh_entsize)
6542 {
6543 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
6544 rel_countp = &elf_section_data (output_section)->rel_count;
6545 }
6546 else if (elf_section_data (output_section)->rel_hdr2
6547 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
6548 == input_rel_hdr->sh_entsize))
6549 {
6550 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
6551 rel_countp = &elf_section_data (output_section)->rel_count2;
6552 }
41241523
TS
6553 else
6554 {
58821868
AM
6555 (*_bfd_error_handler)
6556 (_("%s: relocation size mismatch in %s section %s"),
6557 bfd_get_filename (output_bfd),
6558 bfd_archive_filename (input_section->owner),
6559 input_section->name);
41241523 6560 bfd_set_error (bfd_error_wrong_object_format);
b34976b6 6561 return FALSE;
41241523 6562 }
32f0787a
UC
6563
6564 bed = get_elf_backend_data (output_bfd);
947216bf
AM
6565 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
6566 swap_out = bed->s->swap_reloc_out;
6567 else if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rela))
6568 swap_out = bed->s->swap_reloca_out;
6569 else
6570 abort ();
6571
6572 erel = output_rel_hdr->contents;
6573 erel += *rel_countp * input_rel_hdr->sh_entsize;
23bc299b 6574 irela = internal_relocs;
58821868
AM
6575 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
6576 * bed->s->int_rels_per_ext_rel);
947216bf 6577 while (irela < irelaend)
23bc299b 6578 {
947216bf
AM
6579 (*swap_out) (output_bfd, irela, erel);
6580 irela += bed->s->int_rels_per_ext_rel;
6581 erel += input_rel_hdr->sh_entsize;
23bc299b
MM
6582 }
6583
6584 /* Bump the counter, so that we know where to add the next set of
6585 relocations. */
d9bc7a44 6586 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
41241523 6587
b34976b6 6588 return TRUE;
23bc299b
MM
6589}
6590
252b5132
RH
6591/* Link an input file into the linker output file. This function
6592 handles all the sections and relocations of the input file at once.
6593 This is so that we only have to read the local symbols once, and
6594 don't have to keep them in memory. */
6595
b34976b6 6596static bfd_boolean
252b5132
RH
6597elf_link_input_bfd (finfo, input_bfd)
6598 struct elf_final_link_info *finfo;
6599 bfd *input_bfd;
6600{
b34976b6
AM
6601 bfd_boolean (*relocate_section)
6602 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6603 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
252b5132
RH
6604 bfd *output_bfd;
6605 Elf_Internal_Shdr *symtab_hdr;
6606 size_t locsymcount;
6607 size_t extsymoff;
6cdc0ccc 6608 Elf_Internal_Sym *isymbuf;
252b5132 6609 Elf_Internal_Sym *isym;
6cdc0ccc 6610 Elf_Internal_Sym *isymend;
252b5132
RH
6611 long *pindex;
6612 asection **ppsection;
6613 asection *o;
c7ac6ff8 6614 struct elf_backend_data *bed;
b34976b6 6615 bfd_boolean emit_relocs;
f8deed93 6616 struct elf_link_hash_entry **sym_hashes;
252b5132
RH
6617
6618 output_bfd = finfo->output_bfd;
c7ac6ff8
MM
6619 bed = get_elf_backend_data (output_bfd);
6620 relocate_section = bed->elf_backend_relocate_section;
252b5132
RH
6621
6622 /* If this is a dynamic object, we don't want to do anything here:
6623 we don't want the local symbols, and we don't want the section
6624 contents. */
6625 if ((input_bfd->flags & DYNAMIC) != 0)
b34976b6 6626 return TRUE;
252b5132 6627
9317eacc 6628 emit_relocs = (finfo->info->relocateable
c44233aa
AM
6629 || finfo->info->emitrelocations
6630 || bed->elf_backend_emit_relocs);
9317eacc 6631
252b5132
RH
6632 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6633 if (elf_bad_symtab (input_bfd))
6634 {
6635 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6636 extsymoff = 0;
6637 }
6638 else
6639 {
6640 locsymcount = symtab_hdr->sh_info;
6641 extsymoff = symtab_hdr->sh_info;
6642 }
6643
6644 /* Read the local symbols. */
6cdc0ccc
AM
6645 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6646 if (isymbuf == NULL && locsymcount != 0)
6647 {
6648 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6649 finfo->internal_syms,
6650 finfo->external_syms,
6651 finfo->locsym_shndx);
6652 if (isymbuf == NULL)
b34976b6 6653 return FALSE;
252b5132
RH
6654 }
6655
6cdc0ccc
AM
6656 /* Find local symbol sections and adjust values of symbols in
6657 SEC_MERGE sections. Write out those local symbols we know are
6658 going into the output file. */
6659 isymend = isymbuf + locsymcount;
6660 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6661 isym < isymend;
6662 isym++, pindex++, ppsection++)
252b5132
RH
6663 {
6664 asection *isec;
6665 const char *name;
6666 Elf_Internal_Sym osym;
6667
252b5132
RH
6668 *pindex = -1;
6669
6670 if (elf_bad_symtab (input_bfd))
6671 {
6672 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6673 {
6674 *ppsection = NULL;
6675 continue;
6676 }
6677 }
6678
6679 if (isym->st_shndx == SHN_UNDEF)
862517b6 6680 isec = bfd_und_section_ptr;
9ad5cbcf
AM
6681 else if (isym->st_shndx < SHN_LORESERVE
6682 || isym->st_shndx > SHN_HIRESERVE)
f5fa8ca2
JJ
6683 {
6684 isec = section_from_elf_index (input_bfd, isym->st_shndx);
65765700 6685 if (isec
68bfbfcc 6686 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
f5fa8ca2
JJ
6687 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6688 isym->st_value =
6689 _bfd_merged_section_offset (output_bfd, &isec,
65765700 6690 elf_section_data (isec)->sec_info,
f5fa8ca2
JJ
6691 isym->st_value, (bfd_vma) 0);
6692 }
252b5132 6693 else if (isym->st_shndx == SHN_ABS)
862517b6 6694 isec = bfd_abs_section_ptr;
252b5132 6695 else if (isym->st_shndx == SHN_COMMON)
862517b6 6696 isec = bfd_com_section_ptr;
252b5132
RH
6697 else
6698 {
6699 /* Who knows? */
6700 isec = NULL;
6701 }
6702
6703 *ppsection = isec;
6704
6705 /* Don't output the first, undefined, symbol. */
6cdc0ccc 6706 if (ppsection == finfo->sections)
252b5132
RH
6707 continue;
6708
24376d1b
AM
6709 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6710 {
24376d1b
AM
6711 /* We never output section symbols. Instead, we use the
6712 section symbol of the corresponding section in the output
6713 file. */
6714 continue;
6715 }
6716
252b5132
RH
6717 /* If we are stripping all symbols, we don't want to output this
6718 one. */
6719 if (finfo->info->strip == strip_all)
6720 continue;
6721
252b5132
RH
6722 /* If we are discarding all local symbols, we don't want to
6723 output this one. If we are generating a relocateable output
6724 file, then some of the local symbols may be required by
6725 relocs; we output them below as we discover that they are
6726 needed. */
6727 if (finfo->info->discard == discard_all)
6728 continue;
6729
6730 /* If this symbol is defined in a section which we are
c44233aa
AM
6731 discarding, we don't need to keep it, but note that
6732 linker_mark is only reliable for sections that have contents.
6733 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6734 as well as linker_mark. */
9ad5cbcf 6735 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
252b5132
RH
6736 && isec != NULL
6737 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6738 || (! finfo->info->relocateable
6739 && (isec->flags & SEC_EXCLUDE) != 0)))
6740 continue;
6741
6742 /* Get the name of the symbol. */
6743 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6744 isym->st_name);
6745 if (name == NULL)
b34976b6 6746 return FALSE;
252b5132
RH
6747
6748 /* See if we are discarding symbols with this name. */
6749 if ((finfo->info->strip == strip_some
b34976b6 6750 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
252b5132 6751 == NULL))
f5fa8ca2
JJ
6752 || (((finfo->info->discard == discard_sec_merge
6753 && (isec->flags & SEC_MERGE) && ! finfo->info->relocateable)
6754 || finfo->info->discard == discard_l)
252b5132
RH
6755 && bfd_is_local_label_name (input_bfd, name)))
6756 continue;
6757
6758 /* If we get here, we are going to output this symbol. */
6759
6760 osym = *isym;
6761
6762 /* Adjust the section index for the output file. */
6763 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6764 isec->output_section);
9ad5cbcf 6765 if (osym.st_shndx == SHN_BAD)
b34976b6 6766 return FALSE;
252b5132
RH
6767
6768 *pindex = bfd_get_symcount (output_bfd);
6769
6770 /* ELF symbols in relocateable files are section relative, but
6771 in executable files they are virtual addresses. Note that
6772 this code assumes that all ELF sections have an associated
6773 BFD section with a reasonable value for output_offset; below
6774 we assume that they also have a reasonable value for
6775 output_section. Any special sections must be set up to meet
6776 these requirements. */
6777 osym.st_value += isec->output_offset;
6778 if (! finfo->info->relocateable)
13ae64f3
JJ
6779 {
6780 osym.st_value += isec->output_section->vma;
6781 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6782 {
6783 /* STT_TLS symbols are relative to PT_TLS segment base. */
6784 BFD_ASSERT (finfo->first_tls_sec != NULL);
6785 osym.st_value -= finfo->first_tls_sec->vma;
6786 }
6787 }
252b5132
RH
6788
6789 if (! elf_link_output_sym (finfo, name, &osym, isec))
b34976b6 6790 return FALSE;
252b5132
RH
6791 }
6792
6793 /* Relocate the contents of each section. */
f8deed93 6794 sym_hashes = elf_sym_hashes (input_bfd);
252b5132
RH
6795 for (o = input_bfd->sections; o != NULL; o = o->next)
6796 {
6797 bfd_byte *contents;
6798
6799 if (! o->linker_mark)
6800 {
6801 /* This section was omitted from the link. */
6802 continue;
6803 }
6804
6805 if ((o->flags & SEC_HAS_CONTENTS) == 0
6806 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
6807 continue;
6808
6809 if ((o->flags & SEC_LINKER_CREATED) != 0)
6810 {
6811 /* Section was created by elf_link_create_dynamic_sections
6812 or somesuch. */
6813 continue;
6814 }
6815
6816 /* Get the contents of the section. They have been cached by a
c44233aa
AM
6817 relaxation routine. Note that o is a section in an input
6818 file, so the contents field will not have been set by any of
6819 the routines which work on output files. */
252b5132
RH
6820 if (elf_section_data (o)->this_hdr.contents != NULL)
6821 contents = elf_section_data (o)->this_hdr.contents;
6822 else
6823 {
6824 contents = finfo->contents;
6825 if (! bfd_get_section_contents (input_bfd, o, contents,
6826 (file_ptr) 0, o->_raw_size))
b34976b6 6827 return FALSE;
252b5132
RH
6828 }
6829
6830 if ((o->flags & SEC_RELOC) != 0)
6831 {
6832 Elf_Internal_Rela *internal_relocs;
6833
6834 /* Get the swapped relocs. */
6835 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
6836 (input_bfd, o, finfo->external_relocs,
b34976b6 6837 finfo->internal_relocs, FALSE));
252b5132
RH
6838 if (internal_relocs == NULL
6839 && o->reloc_count > 0)
b34976b6 6840 return FALSE;
252b5132 6841
ec338859
AM
6842 /* Run through the relocs looking for any against symbols
6843 from discarded sections and section symbols from
6844 removed link-once sections. Complain about relocs
6845 against discarded sections. Zero relocs against removed
126495ed 6846 link-once sections. */
73d074b4
DJ
6847 if (!finfo->info->relocateable
6848 && !elf_section_ignore_discarded_relocs (o))
ec338859
AM
6849 {
6850 Elf_Internal_Rela *rel, *relend;
50b4d486 6851
ec338859
AM
6852 rel = internal_relocs;
6853 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6854 for ( ; rel < relend; rel++)
6855 {
6856 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6857
6858 if (r_symndx >= locsymcount
6859 || (elf_bad_symtab (input_bfd)
6860 && finfo->sections[r_symndx] == NULL))
6861 {
6862 struct elf_link_hash_entry *h;
6863
6864 h = sym_hashes[r_symndx - extsymoff];
6865 while (h->root.type == bfd_link_hash_indirect
6866 || h->root.type == bfd_link_hash_warning)
6867 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6868
6869 /* Complain if the definition comes from a
6870 discarded section. */
6871 if ((h->root.type == bfd_link_hash_defined
6872 || h->root.type == bfd_link_hash_defweak)
ed4de5e2 6873 && elf_discarded_section (h->root.u.def.section))
ec338859 6874 {
ec338859
AM
6875 if ((o->flags & SEC_DEBUGGING) != 0)
6876 {
45e9217a 6877 BFD_ASSERT (r_symndx != 0);
f8deed93 6878 memset (rel, 0, sizeof (*rel));
ec338859
AM
6879 }
6880 else
ec338859
AM
6881 {
6882 if (! ((*finfo->info->callbacks->undefined_symbol)
6883 (finfo->info, h->root.root.string,
6884 input_bfd, o, rel->r_offset,
b34976b6
AM
6885 TRUE)))
6886 return FALSE;
ec338859
AM
6887 }
6888 }
6889 }
6890 else
6891 {
f9f32305 6892 asection *sec = finfo->sections[r_symndx];
50b4d486 6893
ed4de5e2 6894 if (sec != NULL && elf_discarded_section (sec))
f9f32305 6895 {
ad43ed4c
L
6896 if ((o->flags & SEC_DEBUGGING) != 0
6897 || (sec->flags & SEC_LINK_ONCE) != 0)
f9f32305 6898 {
45e9217a 6899 BFD_ASSERT (r_symndx != 0);
f9f32305
AM
6900 rel->r_info
6901 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
6902 rel->r_addend = 0;
6903 }
6904 else
f9f32305 6905 {
b34976b6 6906 bfd_boolean ok;
f9f32305
AM
6907 const char *msg
6908 = _("local symbols in discarded section %s");
6909 bfd_size_type amt
6910 = strlen (sec->name) + strlen (msg) - 1;
6911 char *buf = (char *) bfd_malloc (amt);
6912
6913 if (buf != NULL)
6914 sprintf (buf, msg, sec->name);
6915 else
6916 buf = (char *) sec->name;
6917 ok = (*finfo->info->callbacks
6918 ->undefined_symbol) (finfo->info, buf,
6919 input_bfd, o,
6920 rel->r_offset,
b34976b6 6921 TRUE);
f9f32305
AM
6922 if (buf != sec->name)
6923 free (buf);
6924 if (!ok)
b34976b6 6925 return FALSE;
ec338859
AM
6926 }
6927 }
6928 }
6929 }
6930 }
50b4d486 6931
252b5132
RH
6932 /* Relocate the section by invoking a back end routine.
6933
6934 The back end routine is responsible for adjusting the
6935 section contents as necessary, and (if using Rela relocs
6936 and generating a relocateable output file) adjusting the
6937 reloc addend as necessary.
6938
6939 The back end routine does not have to worry about setting
6940 the reloc address or the reloc symbol index.
6941
6942 The back end routine is given a pointer to the swapped in
6943 internal symbols, and can access the hash table entries
6944 for the external symbols via elf_sym_hashes (input_bfd).
6945
6946 When generating relocateable output, the back end routine
6947 must handle STB_LOCAL/STT_SECTION symbols specially. The
6948 output symbol is going to be a section symbol
6949 corresponding to the output section, which will require
6950 the addend to be adjusted. */
6951
6952 if (! (*relocate_section) (output_bfd, finfo->info,
6953 input_bfd, o, contents,
6954 internal_relocs,
6cdc0ccc 6955 isymbuf,
252b5132 6956 finfo->sections))
b34976b6 6957 return FALSE;
252b5132 6958
9317eacc 6959 if (emit_relocs)
252b5132
RH
6960 {
6961 Elf_Internal_Rela *irela;
6962 Elf_Internal_Rela *irelaend;
73722af0 6963 bfd_vma last_offset;
252b5132 6964 struct elf_link_hash_entry **rel_hash;
c89583f8 6965 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
4e8a9624 6966 unsigned int next_erel;
b34976b6
AM
6967 bfd_boolean (*reloc_emitter)
6968 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *,
6969 Elf_Internal_Rela *));
6970 bfd_boolean rela_normal;
b491616a
AM
6971
6972 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6973 rela_normal = (bed->rela_normal
6974 && (input_rel_hdr->sh_entsize
6975 == sizeof (Elf_External_Rela)));
252b5132
RH
6976
6977 /* Adjust the reloc addresses and symbol indices. */
6978
6979 irela = internal_relocs;
dc810e39 6980 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132 6981 rel_hash = (elf_section_data (o->output_section)->rel_hashes
31367b81
MM
6982 + elf_section_data (o->output_section)->rel_count
6983 + elf_section_data (o->output_section)->rel_count2);
73722af0
AM
6984 last_offset = o->output_offset;
6985 if (!finfo->info->relocateable)
6986 last_offset += o->output_section->vma;
209f668e 6987 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
252b5132
RH
6988 {
6989 unsigned long r_symndx;
252b5132 6990 asection *sec;
fad2542d 6991 Elf_Internal_Sym sym;
252b5132 6992
209f668e
NC
6993 if (next_erel == bed->s->int_rels_per_ext_rel)
6994 {
6995 rel_hash++;
6996 next_erel = 0;
6997 }
6998
d6fe2dc1
AM
6999 irela->r_offset = _bfd_elf_section_offset (output_bfd,
7000 finfo->info, o,
7001 irela->r_offset);
7002 if (irela->r_offset >= (bfd_vma) -2)
7003 {
73722af0
AM
7004 /* This is a reloc for a deleted entry or somesuch.
7005 Turn it into an R_*_NONE reloc, at the same
7006 offset as the last reloc. elf_eh_frame.c and
7007 elf_bfd_discard_info rely on reloc offsets
b34976b6 7008 being ordered. */
73722af0
AM
7009 irela->r_offset = last_offset;
7010 irela->r_info = 0;
7011 irela->r_addend = 0;
d6fe2dc1
AM
7012 continue;
7013 }
7014
252b5132
RH
7015 irela->r_offset += o->output_offset;
7016
7ad34365 7017 /* Relocs in an executable have to be virtual addresses. */
fd984e46 7018 if (!finfo->info->relocateable)
7ad34365
NC
7019 irela->r_offset += o->output_section->vma;
7020
73722af0 7021 last_offset = irela->r_offset;
252b5132 7022
73722af0
AM
7023 r_symndx = ELF_R_SYM (irela->r_info);
7024 if (r_symndx == STN_UNDEF)
252b5132
RH
7025 continue;
7026
7027 if (r_symndx >= locsymcount
7028 || (elf_bad_symtab (input_bfd)
7029 && finfo->sections[r_symndx] == NULL))
7030 {
7031 struct elf_link_hash_entry *rh;
209f668e 7032 unsigned long indx;
252b5132
RH
7033
7034 /* This is a reloc against a global symbol. We
7035 have not yet output all the local symbols, so
7036 we do not know the symbol index of any global
7037 symbol. We set the rel_hash entry for this
7038 reloc to point to the global hash table entry
7039 for this symbol. The symbol index is then
7040 set at the end of elf_bfd_final_link. */
7041 indx = r_symndx - extsymoff;
7042 rh = elf_sym_hashes (input_bfd)[indx];
7043 while (rh->root.type == bfd_link_hash_indirect
7044 || rh->root.type == bfd_link_hash_warning)
7045 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7046
7047 /* Setting the index to -2 tells
7048 elf_link_output_extsym that this symbol is
7049 used by a reloc. */
7050 BFD_ASSERT (rh->indx < 0);
7051 rh->indx = -2;
7052
7053 *rel_hash = rh;
7054
7055 continue;
7056 }
7057
3e932841 7058 /* This is a reloc against a local symbol. */
252b5132
RH
7059
7060 *rel_hash = NULL;
fad2542d 7061 sym = isymbuf[r_symndx];
252b5132 7062 sec = finfo->sections[r_symndx];
fad2542d 7063 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
252b5132
RH
7064 {
7065 /* I suppose the backend ought to fill in the
7066 section of any STT_SECTION symbol against a
7067 processor specific section. If we have
7068 discarded a section, the output_section will
7069 be the absolute section. */
b491616a
AM
7070 if (bfd_is_abs_section (sec)
7071 || (sec != NULL
7072 && bfd_is_abs_section (sec->output_section)))
252b5132
RH
7073 r_symndx = 0;
7074 else if (sec == NULL || sec->owner == NULL)
7075 {
7076 bfd_set_error (bfd_error_bad_value);
b34976b6 7077 return FALSE;
252b5132
RH
7078 }
7079 else
7080 {
7081 r_symndx = sec->output_section->target_index;
7082 BFD_ASSERT (r_symndx != 0);
7083 }
b491616a
AM
7084
7085 /* Adjust the addend according to where the
f5d44ba0 7086 section winds up in the output section. */
b491616a
AM
7087 if (rela_normal)
7088 irela->r_addend += sec->output_offset;
252b5132
RH
7089 }
7090 else
7091 {
7092 if (finfo->indices[r_symndx] == -1)
7093 {
dc810e39 7094 unsigned long shlink;
252b5132
RH
7095 const char *name;
7096 asection *osec;
7097
7098 if (finfo->info->strip == strip_all)
7099 {
7100 /* You can't do ld -r -s. */
7101 bfd_set_error (bfd_error_invalid_operation);
b34976b6 7102 return FALSE;
252b5132
RH
7103 }
7104
7105 /* This symbol was skipped earlier, but
7106 since it is needed by a reloc, we
7107 must output it now. */
dc810e39 7108 shlink = symtab_hdr->sh_link;
a7b97311 7109 name = (bfd_elf_string_from_elf_section
fad2542d 7110 (input_bfd, shlink, sym.st_name));
252b5132 7111 if (name == NULL)
b34976b6 7112 return FALSE;
252b5132
RH
7113
7114 osec = sec->output_section;
fad2542d 7115 sym.st_shndx =
252b5132
RH
7116 _bfd_elf_section_from_bfd_section (output_bfd,
7117 osec);
fad2542d 7118 if (sym.st_shndx == SHN_BAD)
b34976b6 7119 return FALSE;
252b5132 7120
fad2542d 7121 sym.st_value += sec->output_offset;
252b5132 7122 if (! finfo->info->relocateable)
13ae64f3 7123 {
fad2542d
AM
7124 sym.st_value += osec->vma;
7125 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
13ae64f3
JJ
7126 {
7127 /* STT_TLS symbols are relative to PT_TLS
7128 segment base. */
7129 BFD_ASSERT (finfo->first_tls_sec != NULL);
fad2542d 7130 sym.st_value -= finfo->first_tls_sec->vma;
13ae64f3
JJ
7131 }
7132 }
252b5132 7133
a7b97311
AM
7134 finfo->indices[r_symndx]
7135 = bfd_get_symcount (output_bfd);
252b5132 7136
fad2542d 7137 if (! elf_link_output_sym (finfo, name, &sym, sec))
b34976b6 7138 return FALSE;
252b5132
RH
7139 }
7140
7141 r_symndx = finfo->indices[r_symndx];
7142 }
7143
7144 irela->r_info = ELF_R_INFO (r_symndx,
7145 ELF_R_TYPE (irela->r_info));
7146 }
7147
7148 /* Swap out the relocs. */
c44233aa
AM
7149 if (bed->elf_backend_emit_relocs
7150 && !(finfo->info->relocateable
a7b97311 7151 || finfo->info->emitrelocations))
c44233aa
AM
7152 reloc_emitter = bed->elf_backend_emit_relocs;
7153 else
7154 reloc_emitter = elf_link_output_relocs;
9317eacc 7155
c89583f8
AM
7156 if (input_rel_hdr->sh_size != 0
7157 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7158 internal_relocs))
b34976b6 7159 return FALSE;
9317eacc 7160
c89583f8
AM
7161 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7162 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
c44233aa
AM
7163 {
7164 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
dc810e39 7165 * bed->s->int_rels_per_ext_rel);
c89583f8 7166 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
41241523 7167 internal_relocs))
b34976b6 7168 return FALSE;
c44233aa 7169 }
252b5132
RH
7170 }
7171 }
7172
7173 /* Write out the modified section contents. */
73d074b4 7174 if (bed->elf_backend_write_section
f9f32305 7175 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
73d074b4
DJ
7176 {
7177 /* Section written out. */
7178 }
68bfbfcc 7179 else switch (o->sec_info_type)
f5fa8ca2 7180 {
65765700 7181 case ELF_INFO_TYPE_STABS:
f5fa8ca2 7182 if (! (_bfd_write_section_stabs
65765700
JJ
7183 (output_bfd,
7184 &elf_hash_table (finfo->info)->stab_info,
7185 o, &elf_section_data (o)->sec_info, contents)))
b34976b6 7186 return FALSE;
65765700
JJ
7187 break;
7188 case ELF_INFO_TYPE_MERGE:
126495ed
AM
7189 if (! _bfd_write_merged_section (output_bfd, o,
7190 elf_section_data (o)->sec_info))
b34976b6 7191 return FALSE;
65765700
JJ
7192 break;
7193 case ELF_INFO_TYPE_EH_FRAME:
7194 {
126495ed
AM
7195 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7196 o, contents))
b34976b6 7197 return FALSE;
65765700
JJ
7198 }
7199 break;
7200 default:
7201 {
7202 bfd_size_type sec_size;
7203
7204 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
7205 if (! (o->flags & SEC_EXCLUDE)
7206 && ! bfd_set_section_contents (output_bfd, o->output_section,
7207 contents,
7208 (file_ptr) o->output_offset,
7209 sec_size))
b34976b6 7210 return FALSE;
65765700
JJ
7211 }
7212 break;
252b5132
RH
7213 }
7214 }
7215
b34976b6 7216 return TRUE;
252b5132
RH
7217}
7218
7219/* Generate a reloc when linking an ELF file. This is a reloc
7220 requested by the linker, and does come from any input file. This
7221 is used to build constructor and destructor tables when linking
7222 with -Ur. */
7223
b34976b6 7224static bfd_boolean
252b5132
RH
7225elf_reloc_link_order (output_bfd, info, output_section, link_order)
7226 bfd *output_bfd;
7227 struct bfd_link_info *info;
7228 asection *output_section;
7229 struct bfd_link_order *link_order;
7230{
7231 reloc_howto_type *howto;
7232 long indx;
7233 bfd_vma offset;
7234 bfd_vma addend;
7235 struct elf_link_hash_entry **rel_hash_ptr;
7236 Elf_Internal_Shdr *rel_hdr;
32f0787a 7237 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
947216bf
AM
7238 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7239 bfd_byte *erel;
7240 unsigned int i;
252b5132
RH
7241
7242 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7243 if (howto == NULL)
7244 {
7245 bfd_set_error (bfd_error_bad_value);
b34976b6 7246 return FALSE;
252b5132
RH
7247 }
7248
7249 addend = link_order->u.reloc.p->addend;
7250
7251 /* Figure out the symbol index. */
7252 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
31367b81
MM
7253 + elf_section_data (output_section)->rel_count
7254 + elf_section_data (output_section)->rel_count2);
252b5132
RH
7255 if (link_order->type == bfd_section_reloc_link_order)
7256 {
7257 indx = link_order->u.reloc.p->u.section->target_index;
7258 BFD_ASSERT (indx != 0);
7259 *rel_hash_ptr = NULL;
7260 }
7261 else
7262 {
7263 struct elf_link_hash_entry *h;
7264
7265 /* Treat a reloc against a defined symbol as though it were
c44233aa 7266 actually against the section. */
252b5132
RH
7267 h = ((struct elf_link_hash_entry *)
7268 bfd_wrapped_link_hash_lookup (output_bfd, info,
7269 link_order->u.reloc.p->u.name,
b34976b6 7270 FALSE, FALSE, TRUE));
252b5132
RH
7271 if (h != NULL
7272 && (h->root.type == bfd_link_hash_defined
7273 || h->root.type == bfd_link_hash_defweak))
7274 {
7275 asection *section;
7276
7277 section = h->root.u.def.section;
7278 indx = section->output_section->target_index;
7279 *rel_hash_ptr = NULL;
7280 /* It seems that we ought to add the symbol value to the
c44233aa
AM
7281 addend here, but in practice it has already been added
7282 because it was passed to constructor_callback. */
252b5132
RH
7283 addend += section->output_section->vma + section->output_offset;
7284 }
7285 else if (h != NULL)
7286 {
7287 /* Setting the index to -2 tells elf_link_output_extsym that
7288 this symbol is used by a reloc. */
7289 h->indx = -2;
7290 *rel_hash_ptr = h;
7291 indx = 0;
7292 }
7293 else
7294 {
7295 if (! ((*info->callbacks->unattached_reloc)
7296 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
7297 (asection *) NULL, (bfd_vma) 0)))
b34976b6 7298 return FALSE;
252b5132
RH
7299 indx = 0;
7300 }
7301 }
7302
7303 /* If this is an inplace reloc, we must write the addend into the
7304 object file. */
7305 if (howto->partial_inplace && addend != 0)
7306 {
7307 bfd_size_type size;
7308 bfd_reloc_status_type rstat;
7309 bfd_byte *buf;
b34976b6 7310 bfd_boolean ok;
dc810e39 7311 const char *sym_name;
252b5132
RH
7312
7313 size = bfd_get_reloc_size (howto);
7314 buf = (bfd_byte *) bfd_zmalloc (size);
7315 if (buf == (bfd_byte *) NULL)
b34976b6 7316 return FALSE;
dc810e39 7317 rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf);
252b5132
RH
7318 switch (rstat)
7319 {
7320 case bfd_reloc_ok:
7321 break;
dc810e39 7322
252b5132
RH
7323 default:
7324 case bfd_reloc_outofrange:
7325 abort ();
dc810e39 7326
252b5132 7327 case bfd_reloc_overflow:
dc810e39
AM
7328 if (link_order->type == bfd_section_reloc_link_order)
7329 sym_name = bfd_section_name (output_bfd,
7330 link_order->u.reloc.p->u.section);
7331 else
7332 sym_name = link_order->u.reloc.p->u.name;
252b5132 7333 if (! ((*info->callbacks->reloc_overflow)
dc810e39
AM
7334 (info, sym_name, howto->name, addend,
7335 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
252b5132
RH
7336 {
7337 free (buf);
b34976b6 7338 return FALSE;
252b5132
RH
7339 }
7340 break;
7341 }
7342 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
7343 (file_ptr) link_order->offset, size);
7344 free (buf);
7345 if (! ok)
b34976b6 7346 return FALSE;
252b5132
RH
7347 }
7348
7349 /* The address of a reloc is relative to the section in a
7350 relocateable file, and is a virtual address in an executable
7351 file. */
7352 offset = link_order->offset;
7353 if (! info->relocateable)
7354 offset += output_section->vma;
7355
947216bf
AM
7356 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7357 {
7358 irel[i].r_offset = offset;
7359 irel[i].r_info = 0;
7360 irel[i].r_addend = 0;
7361 }
7362 irel[0].r_info = ELF_R_INFO (indx, howto->type);
252b5132 7363
947216bf
AM
7364 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7365 erel = rel_hdr->contents;
252b5132
RH
7366 if (rel_hdr->sh_type == SHT_REL)
7367 {
947216bf
AM
7368 erel += (elf_section_data (output_section)->rel_count
7369 * sizeof (Elf_External_Rel));
7370 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
252b5132
RH
7371 }
7372 else
7373 {
947216bf
AM
7374 irel[0].r_addend = addend;
7375 erel += (elf_section_data (output_section)->rel_count
7376 * sizeof (Elf_External_Rela));
7377 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
252b5132
RH
7378 }
7379
0525d26e 7380 ++elf_section_data (output_section)->rel_count;
252b5132 7381
b34976b6 7382 return TRUE;
252b5132 7383}
252b5132
RH
7384\f
7385/* Allocate a pointer to live in a linker created section. */
7386
b34976b6 7387bfd_boolean
252b5132
RH
7388elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
7389 bfd *abfd;
7390 struct bfd_link_info *info;
7391 elf_linker_section_t *lsect;
7392 struct elf_link_hash_entry *h;
7393 const Elf_Internal_Rela *rel;
7394{
7395 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
7396 elf_linker_section_pointers_t *linker_section_ptr;
dc810e39
AM
7397 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7398 bfd_size_type amt;
252b5132
RH
7399
7400 BFD_ASSERT (lsect != NULL);
7401
a7b97311 7402 /* Is this a global symbol? */
252b5132
RH
7403 if (h != NULL)
7404 {
a7b97311 7405 /* Has this symbol already been allocated? If so, our work is done. */
252b5132
RH
7406 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
7407 rel->r_addend,
7408 lsect->which))
b34976b6 7409 return TRUE;
252b5132
RH
7410
7411 ptr_linker_section_ptr = &h->linker_section_pointer;
7412 /* Make sure this symbol is output as a dynamic symbol. */
7413 if (h->dynindx == -1)
7414 {
7415 if (! elf_link_record_dynamic_symbol (info, h))
b34976b6 7416 return FALSE;
252b5132
RH
7417 }
7418
7419 if (lsect->rel_section)
7420 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7421 }
a7b97311 7422 else
252b5132 7423 {
a7b97311 7424 /* Allocation of a pointer to a local symbol. */
252b5132
RH
7425 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
7426
a7b97311 7427 /* Allocate a table to hold the local symbols if first time. */
252b5132
RH
7428 if (!ptr)
7429 {
7430 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
7431 register unsigned int i;
7432
dc810e39
AM
7433 amt = num_symbols;
7434 amt *= sizeof (elf_linker_section_pointers_t *);
7435 ptr = (elf_linker_section_pointers_t **) bfd_alloc (abfd, amt);
252b5132
RH
7436
7437 if (!ptr)
b34976b6 7438 return FALSE;
252b5132
RH
7439
7440 elf_local_ptr_offsets (abfd) = ptr;
7441 for (i = 0; i < num_symbols; i++)
a7b97311 7442 ptr[i] = (elf_linker_section_pointers_t *) 0;
252b5132
RH
7443 }
7444
a7b97311 7445 /* Has this symbol already been allocated? If so, our work is done. */
252b5132
RH
7446 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
7447 rel->r_addend,
7448 lsect->which))
b34976b6 7449 return TRUE;
252b5132
RH
7450
7451 ptr_linker_section_ptr = &ptr[r_symndx];
7452
7453 if (info->shared)
7454 {
7455 /* If we are generating a shared object, we need to
7456 output a R_<xxx>_RELATIVE reloc so that the
7457 dynamic linker can adjust this GOT entry. */
7458 BFD_ASSERT (lsect->rel_section != NULL);
7459 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7460 }
7461 }
7462
a7b97311
AM
7463 /* Allocate space for a pointer in the linker section, and allocate
7464 a new pointer record from internal memory. */
252b5132 7465 BFD_ASSERT (ptr_linker_section_ptr != NULL);
dc810e39
AM
7466 amt = sizeof (elf_linker_section_pointers_t);
7467 linker_section_ptr = (elf_linker_section_pointers_t *) bfd_alloc (abfd, amt);
252b5132
RH
7468
7469 if (!linker_section_ptr)
b34976b6 7470 return FALSE;
252b5132
RH
7471
7472 linker_section_ptr->next = *ptr_linker_section_ptr;
7473 linker_section_ptr->addend = rel->r_addend;
7474 linker_section_ptr->which = lsect->which;
b34976b6 7475 linker_section_ptr->written_address_p = FALSE;
252b5132
RH
7476 *ptr_linker_section_ptr = linker_section_ptr;
7477
7478#if 0
7479 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
7480 {
a7b97311
AM
7481 linker_section_ptr->offset = (lsect->section->_raw_size
7482 - lsect->hole_size + (ARCH_SIZE / 8));
252b5132
RH
7483 lsect->hole_offset += ARCH_SIZE / 8;
7484 lsect->sym_offset += ARCH_SIZE / 8;
a7b97311 7485 if (lsect->sym_hash)
252b5132 7486 {
a7b97311 7487 /* Bump up symbol value if needed. */
252b5132
RH
7488 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
7489#ifdef DEBUG
7490 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
7491 lsect->sym_hash->root.root.string,
a7b97311
AM
7492 (long) ARCH_SIZE / 8,
7493 (long) lsect->sym_hash->root.u.def.value);
252b5132
RH
7494#endif
7495 }
7496 }
7497 else
7498#endif
7499 linker_section_ptr->offset = lsect->section->_raw_size;
7500
7501 lsect->section->_raw_size += ARCH_SIZE / 8;
7502
7503#ifdef DEBUG
a7b97311
AM
7504 fprintf (stderr,
7505 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7506 lsect->name, (long) linker_section_ptr->offset,
7507 (long) lsect->section->_raw_size);
252b5132
RH
7508#endif
7509
b34976b6 7510 return TRUE;
252b5132 7511}
252b5132
RH
7512\f
7513#if ARCH_SIZE==64
7514#define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7515#endif
7516#if ARCH_SIZE==32
7517#define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7518#endif
7519
209f668e 7520/* Fill in the address for a pointer generated in a linker section. */
252b5132
RH
7521
7522bfd_vma
a7b97311
AM
7523elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h,
7524 relocation, rel, relative_reloc)
252b5132
RH
7525 bfd *output_bfd;
7526 bfd *input_bfd;
7527 struct bfd_link_info *info;
7528 elf_linker_section_t *lsect;
7529 struct elf_link_hash_entry *h;
7530 bfd_vma relocation;
7531 const Elf_Internal_Rela *rel;
7532 int relative_reloc;
7533{
7534 elf_linker_section_pointers_t *linker_section_ptr;
7535
7536 BFD_ASSERT (lsect != NULL);
7537
a7b97311 7538 if (h != NULL)
252b5132 7539 {
a7b97311
AM
7540 /* Handle global symbol. */
7541 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7542 (h->linker_section_pointer,
7543 rel->r_addend,
7544 lsect->which));
252b5132
RH
7545
7546 BFD_ASSERT (linker_section_ptr != NULL);
7547
7548 if (! elf_hash_table (info)->dynamic_sections_created
7549 || (info->shared
7550 && info->symbolic
7551 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
7552 {
7553 /* This is actually a static link, or it is a
7554 -Bsymbolic link and the symbol is defined
7555 locally. We must initialize this entry in the
7556 global section.
7557
7558 When doing a dynamic link, we create a .rela.<xxx>
7559 relocation entry to initialize the value. This
7560 is done in the finish_dynamic_symbol routine. */
7561 if (!linker_section_ptr->written_address_p)
7562 {
b34976b6 7563 linker_section_ptr->written_address_p = TRUE;
a7b97311
AM
7564 bfd_put_ptr (output_bfd,
7565 relocation + linker_section_ptr->addend,
7566 (lsect->section->contents
7567 + linker_section_ptr->offset));
252b5132
RH
7568 }
7569 }
7570 }
a7b97311 7571 else
252b5132 7572 {
a7b97311 7573 /* Handle local symbol. */
252b5132
RH
7574 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7575 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
7576 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
a7b97311
AM
7577 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7578 (elf_local_ptr_offsets (input_bfd)[r_symndx],
7579 rel->r_addend,
7580 lsect->which));
252b5132
RH
7581
7582 BFD_ASSERT (linker_section_ptr != NULL);
7583
a7b97311 7584 /* Write out pointer if it hasn't been rewritten out before. */
252b5132
RH
7585 if (!linker_section_ptr->written_address_p)
7586 {
b34976b6 7587 linker_section_ptr->written_address_p = TRUE;
252b5132
RH
7588 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
7589 lsect->section->contents + linker_section_ptr->offset);
7590
7591 if (info->shared)
7592 {
7593 asection *srel = lsect->rel_section;
947216bf
AM
7594 Elf_Internal_Rela outrel[MAX_INT_RELS_PER_EXT_REL];
7595 bfd_byte *erel;
209f668e
NC
7596 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7597 unsigned int i;
252b5132 7598
a7b97311
AM
7599 /* We need to generate a relative reloc for the dynamic
7600 linker. */
252b5132 7601 if (!srel)
a7b97311
AM
7602 {
7603 srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
7604 lsect->rel_name);
7605 lsect->rel_section = srel;
7606 }
252b5132
RH
7607
7608 BFD_ASSERT (srel != NULL);
7609
209f668e 7610 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
947216bf
AM
7611 {
7612 outrel[i].r_offset = (lsect->section->output_section->vma
7613 + lsect->section->output_offset
7614 + linker_section_ptr->offset);
7615 outrel[i].r_info = 0;
7616 outrel[i].r_addend = 0;
7617 }
209f668e 7618 outrel[0].r_info = ELF_R_INFO (0, relative_reloc);
947216bf
AM
7619 erel = lsect->section->contents;
7620 erel += (elf_section_data (lsect->section)->rel_count++
7621 * sizeof (Elf_External_Rela));
dc810e39 7622 elf_swap_reloca_out (output_bfd, outrel, erel);
252b5132
RH
7623 }
7624 }
7625 }
7626
7627 relocation = (lsect->section->output_offset
7628 + linker_section_ptr->offset
7629 - lsect->hole_offset
7630 - lsect->sym_offset);
7631
7632#ifdef DEBUG
a7b97311
AM
7633 fprintf (stderr,
7634 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7635 lsect->name, (long) relocation, (long) relocation);
252b5132
RH
7636#endif
7637
7638 /* Subtract out the addend, because it will get added back in by the normal
7639 processing. */
7640 return relocation - linker_section_ptr->addend;
7641}
7642\f
7643/* Garbage collect unused sections. */
7644
b34976b6 7645static bfd_boolean elf_gc_mark
58821868
AM
7646 PARAMS ((struct bfd_link_info *, asection *,
7647 asection * (*) (asection *, struct bfd_link_info *,
7648 Elf_Internal_Rela *, struct elf_link_hash_entry *,
7649 Elf_Internal_Sym *)));
252b5132 7650
b34976b6 7651static bfd_boolean elf_gc_sweep
58821868 7652 PARAMS ((struct bfd_link_info *,
b34976b6
AM
7653 bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *,
7654 const Elf_Internal_Rela *)));
252b5132 7655
b34976b6 7656static bfd_boolean elf_gc_sweep_symbol
58821868 7657 PARAMS ((struct elf_link_hash_entry *, PTR));
252b5132 7658
b34976b6 7659static bfd_boolean elf_gc_allocate_got_offsets
58821868 7660 PARAMS ((struct elf_link_hash_entry *, PTR));
252b5132 7661
b34976b6 7662static bfd_boolean elf_gc_propagate_vtable_entries_used
58821868 7663 PARAMS ((struct elf_link_hash_entry *, PTR));
252b5132 7664
b34976b6 7665static bfd_boolean elf_gc_smash_unused_vtentry_relocs
58821868 7666 PARAMS ((struct elf_link_hash_entry *, PTR));
252b5132
RH
7667
7668/* The mark phase of garbage collection. For a given section, mark
dbb410c3
AM
7669 it and any sections in this section's group, and all the sections
7670 which define symbols to which it refers. */
252b5132 7671
b34976b6
AM
7672typedef asection * (*gc_mark_hook_fn)
7673 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
7674 struct elf_link_hash_entry *, Elf_Internal_Sym *));
7675
7676static bfd_boolean
252b5132
RH
7677elf_gc_mark (info, sec, gc_mark_hook)
7678 struct bfd_link_info *info;
7679 asection *sec;
b34976b6 7680 gc_mark_hook_fn gc_mark_hook;
252b5132 7681{
b34976b6 7682 bfd_boolean ret;
dbb410c3 7683 asection *group_sec;
252b5132
RH
7684
7685 sec->gc_mark = 1;
7686
dbb410c3
AM
7687 /* Mark all the sections in the group. */
7688 group_sec = elf_section_data (sec)->next_in_group;
7689 if (group_sec && !group_sec->gc_mark)
7690 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
b34976b6 7691 return FALSE;
252b5132 7692
dbb410c3 7693 /* Look through the section relocs. */
b34976b6 7694 ret = TRUE;
252b5132
RH
7695 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
7696 {
7697 Elf_Internal_Rela *relstart, *rel, *relend;
7698 Elf_Internal_Shdr *symtab_hdr;
7699 struct elf_link_hash_entry **sym_hashes;
7700 size_t nlocsyms;
7701 size_t extsymoff;
252b5132 7702 bfd *input_bfd = sec->owner;
c7ac6ff8 7703 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
6cdc0ccc 7704 Elf_Internal_Sym *isym = NULL;
252b5132
RH
7705
7706 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7707 sym_hashes = elf_sym_hashes (input_bfd);
7708
7709 /* Read the local symbols. */
7710 if (elf_bad_symtab (input_bfd))
7711 {
7712 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
c44233aa 7713 extsymoff = 0;
252b5132
RH
7714 }
7715 else
7716 extsymoff = nlocsyms = symtab_hdr->sh_info;
9ad5cbcf 7717
6cdc0ccc
AM
7718 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
7719 if (isym == NULL && nlocsyms != 0)
9ad5cbcf 7720 {
6cdc0ccc
AM
7721 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
7722 NULL, NULL, NULL);
7723 if (isym == NULL)
b34976b6 7724 return FALSE;
9ad5cbcf
AM
7725 }
7726
252b5132
RH
7727 /* Read the relocations. */
7728 relstart = (NAME(_bfd_elf,link_read_relocs)
6cdc0ccc 7729 (input_bfd, sec, NULL, (Elf_Internal_Rela *) NULL,
252b5132
RH
7730 info->keep_memory));
7731 if (relstart == NULL)
7732 {
b34976b6 7733 ret = FALSE;
252b5132
RH
7734 goto out1;
7735 }
c7ac6ff8 7736 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132
RH
7737
7738 for (rel = relstart; rel < relend; rel++)
7739 {
7740 unsigned long r_symndx;
7741 asection *rsec;
7742 struct elf_link_hash_entry *h;
252b5132
RH
7743
7744 r_symndx = ELF_R_SYM (rel->r_info);
7745 if (r_symndx == 0)
7746 continue;
7747
6cdc0ccc
AM
7748 if (r_symndx >= nlocsyms
7749 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
252b5132
RH
7750 {
7751 h = sym_hashes[r_symndx - extsymoff];
1e2f5b6e 7752 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
252b5132
RH
7753 }
7754 else
7755 {
6cdc0ccc 7756 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
252b5132
RH
7757 }
7758
7759 if (rsec && !rsec->gc_mark)
b91afed7
AM
7760 {
7761 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
7762 rsec->gc_mark = 1;
7763 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
7764 {
b34976b6 7765 ret = FALSE;
b91afed7
AM
7766 goto out2;
7767 }
7768 }
252b5132
RH
7769 }
7770
7771 out2:
6cdc0ccc 7772 if (elf_section_data (sec)->relocs != relstart)
252b5132
RH
7773 free (relstart);
7774 out1:
6cdc0ccc
AM
7775 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
7776 {
7777 if (! info->keep_memory)
7778 free (isym);
7779 else
7780 symtab_hdr->contents = (unsigned char *) isym;
7781 }
252b5132
RH
7782 }
7783
7784 return ret;
7785}
7786
7787/* The sweep phase of garbage collection. Remove all garbage sections. */
7788
b34976b6
AM
7789typedef bfd_boolean (*gc_sweep_hook_fn)
7790 PARAMS ((bfd *, struct bfd_link_info *, asection *,
7791 const Elf_Internal_Rela *));
7792
7793static bfd_boolean
252b5132
RH
7794elf_gc_sweep (info, gc_sweep_hook)
7795 struct bfd_link_info *info;
b34976b6 7796 gc_sweep_hook_fn gc_sweep_hook;
252b5132
RH
7797{
7798 bfd *sub;
7799
7800 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7801 {
7802 asection *o;
7803
f6af82bd
AM
7804 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7805 continue;
7806
252b5132
RH
7807 for (o = sub->sections; o != NULL; o = o->next)
7808 {
7809 /* Keep special sections. Keep .debug sections. */
7810 if ((o->flags & SEC_LINKER_CREATED)
7811 || (o->flags & SEC_DEBUGGING))
7812 o->gc_mark = 1;
7813
7814 if (o->gc_mark)
7815 continue;
7816
7817 /* Skip sweeping sections already excluded. */
7818 if (o->flags & SEC_EXCLUDE)
7819 continue;
7820
7821 /* Since this is early in the link process, it is simple
7822 to remove a section from the output. */
7823 o->flags |= SEC_EXCLUDE;
7824
7825 /* But we also have to update some of the relocation
7826 info we collected before. */
7827 if (gc_sweep_hook
7828 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
7829 {
7830 Elf_Internal_Rela *internal_relocs;
b34976b6 7831 bfd_boolean r;
252b5132
RH
7832
7833 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
7834 (o->owner, o, NULL, NULL, info->keep_memory));
7835 if (internal_relocs == NULL)
b34976b6 7836 return FALSE;
252b5132 7837
3e932841 7838 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
252b5132 7839
6cdc0ccc 7840 if (elf_section_data (o)->relocs != internal_relocs)
252b5132
RH
7841 free (internal_relocs);
7842
7843 if (!r)
b34976b6 7844 return FALSE;
252b5132
RH
7845 }
7846 }
7847 }
7848
7849 /* Remove the symbols that were in the swept sections from the dynamic
7850 symbol table. GCFIXME: Anyone know how to get them out of the
7851 static symbol table as well? */
7852 {
7853 int i = 0;
7854
7855 elf_link_hash_traverse (elf_hash_table (info),
7856 elf_gc_sweep_symbol,
7857 (PTR) &i);
7858
7859 elf_hash_table (info)->dynsymcount = i;
7860 }
7861
b34976b6 7862 return TRUE;
252b5132
RH
7863}
7864
7865/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7866
b34976b6 7867static bfd_boolean
252b5132
RH
7868elf_gc_sweep_symbol (h, idxptr)
7869 struct elf_link_hash_entry *h;
7870 PTR idxptr;
7871{
7872 int *idx = (int *) idxptr;
7873
e92d460e
AM
7874 if (h->root.type == bfd_link_hash_warning)
7875 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7876
252b5132
RH
7877 if (h->dynindx != -1
7878 && ((h->root.type != bfd_link_hash_defined
7879 && h->root.type != bfd_link_hash_defweak)
7880 || h->root.u.def.section->gc_mark))
7881 h->dynindx = (*idx)++;
7882
b34976b6 7883 return TRUE;
252b5132
RH
7884}
7885
7886/* Propogate collected vtable information. This is called through
7887 elf_link_hash_traverse. */
7888
b34976b6 7889static bfd_boolean
252b5132
RH
7890elf_gc_propagate_vtable_entries_used (h, okp)
7891 struct elf_link_hash_entry *h;
7892 PTR okp;
7893{
e92d460e
AM
7894 if (h->root.type == bfd_link_hash_warning)
7895 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7896
3e932841 7897 /* Those that are not vtables. */
252b5132 7898 if (h->vtable_parent == NULL)
b34976b6 7899 return TRUE;
252b5132
RH
7900
7901 /* Those vtables that do not have parents, we cannot merge. */
7902 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
b34976b6 7903 return TRUE;
252b5132
RH
7904
7905 /* If we've already been done, exit. */
7906 if (h->vtable_entries_used && h->vtable_entries_used[-1])
b34976b6 7907 return TRUE;
252b5132
RH
7908
7909 /* Make sure the parent's table is up to date. */
7910 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
7911
7912 if (h->vtable_entries_used == NULL)
7913 {
7914 /* None of this table's entries were referenced. Re-use the
7915 parent's table. */
7916 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
7917 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
7918 }
7919 else
7920 {
7921 size_t n;
b34976b6 7922 bfd_boolean *cu, *pu;
252b5132
RH
7923
7924 /* Or the parent's entries into ours. */
7925 cu = h->vtable_entries_used;
b34976b6 7926 cu[-1] = TRUE;
252b5132
RH
7927 pu = h->vtable_parent->vtable_entries_used;
7928 if (pu != NULL)
7929 {
c44233aa
AM
7930 asection *sec = h->root.u.def.section;
7931 struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
7932 int file_align = bed->s->file_align;
0d1ea5c0
CM
7933
7934 n = h->vtable_parent->vtable_entries_size / file_align;
374b596d 7935 while (n--)
252b5132 7936 {
374b596d 7937 if (*pu)
b34976b6 7938 *cu = TRUE;
374b596d
NC
7939 pu++;
7940 cu++;
252b5132
RH
7941 }
7942 }
7943 }
7944
b34976b6 7945 return TRUE;
252b5132
RH
7946}
7947
b34976b6 7948static bfd_boolean
252b5132
RH
7949elf_gc_smash_unused_vtentry_relocs (h, okp)
7950 struct elf_link_hash_entry *h;
7951 PTR okp;
7952{
7953 asection *sec;
7954 bfd_vma hstart, hend;
7955 Elf_Internal_Rela *relstart, *relend, *rel;
c7ac6ff8 7956 struct elf_backend_data *bed;
0d1ea5c0 7957 int file_align;
252b5132 7958
e92d460e
AM
7959 if (h->root.type == bfd_link_hash_warning)
7960 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7961
252b5132
RH
7962 /* Take care of both those symbols that do not describe vtables as
7963 well as those that are not loaded. */
7964 if (h->vtable_parent == NULL)
b34976b6 7965 return TRUE;
252b5132
RH
7966
7967 BFD_ASSERT (h->root.type == bfd_link_hash_defined
7968 || h->root.type == bfd_link_hash_defweak);
7969
7970 sec = h->root.u.def.section;
7971 hstart = h->root.u.def.value;
7972 hend = hstart + h->size;
7973
7974 relstart = (NAME(_bfd_elf,link_read_relocs)
b34976b6 7975 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, TRUE));
252b5132 7976 if (!relstart)
b34976b6 7977 return *(bfd_boolean *) okp = FALSE;
c7ac6ff8 7978 bed = get_elf_backend_data (sec->owner);
0d1ea5c0
CM
7979 file_align = bed->s->file_align;
7980
c7ac6ff8 7981 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132
RH
7982
7983 for (rel = relstart; rel < relend; ++rel)
7984 if (rel->r_offset >= hstart && rel->r_offset < hend)
7985 {
7986 /* If the entry is in use, do nothing. */
7987 if (h->vtable_entries_used
7988 && (rel->r_offset - hstart) < h->vtable_entries_size)
7989 {
0d1ea5c0 7990 bfd_vma entry = (rel->r_offset - hstart) / file_align;
252b5132
RH
7991 if (h->vtable_entries_used[entry])
7992 continue;
7993 }
7994 /* Otherwise, kill it. */
7995 rel->r_offset = rel->r_info = rel->r_addend = 0;
7996 }
7997
b34976b6 7998 return TRUE;
252b5132
RH
7999}
8000
8001/* Do mark and sweep of unused sections. */
8002
b34976b6 8003bfd_boolean
252b5132
RH
8004elf_gc_sections (abfd, info)
8005 bfd *abfd;
8006 struct bfd_link_info *info;
8007{
b34976b6 8008 bfd_boolean ok = TRUE;
252b5132
RH
8009 bfd *sub;
8010 asection * (*gc_mark_hook)
1e2f5b6e 8011 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
c44233aa 8012 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
252b5132
RH
8013
8014 if (!get_elf_backend_data (abfd)->can_gc_sections
6d3e950b 8015 || info->relocateable || info->emitrelocations
252b5132 8016 || elf_hash_table (info)->dynamic_sections_created)
b34976b6 8017 return TRUE;
252b5132
RH
8018
8019 /* Apply transitive closure to the vtable entry usage info. */
8020 elf_link_hash_traverse (elf_hash_table (info),
8021 elf_gc_propagate_vtable_entries_used,
8022 (PTR) &ok);
8023 if (!ok)
b34976b6 8024 return FALSE;
252b5132
RH
8025
8026 /* Kill the vtable relocations that were not used. */
8027 elf_link_hash_traverse (elf_hash_table (info),
8028 elf_gc_smash_unused_vtentry_relocs,
8029 (PTR) &ok);
8030 if (!ok)
b34976b6 8031 return FALSE;
252b5132
RH
8032
8033 /* Grovel through relocs to find out who stays ... */
8034
8035 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8036 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8037 {
8038 asection *o;
f6af82bd
AM
8039
8040 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8041 continue;
8042
252b5132
RH
8043 for (o = sub->sections; o != NULL; o = o->next)
8044 {
8045 if (o->flags & SEC_KEEP)
c44233aa 8046 if (!elf_gc_mark (info, o, gc_mark_hook))
b34976b6 8047 return FALSE;
252b5132
RH
8048 }
8049 }
8050
8051 /* ... and mark SEC_EXCLUDE for those that go. */
a7b97311 8052 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
b34976b6 8053 return FALSE;
252b5132 8054
b34976b6 8055 return TRUE;
252b5132
RH
8056}
8057\f
8058/* Called from check_relocs to record the existance of a VTINHERIT reloc. */
8059
b34976b6 8060bfd_boolean
252b5132
RH
8061elf_gc_record_vtinherit (abfd, sec, h, offset)
8062 bfd *abfd;
8063 asection *sec;
8064 struct elf_link_hash_entry *h;
8065 bfd_vma offset;
8066{
8067 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8068 struct elf_link_hash_entry **search, *child;
8069 bfd_size_type extsymcount;
8070
8071 /* The sh_info field of the symtab header tells us where the
8072 external symbols start. We don't care about the local symbols at
8073 this point. */
8074 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
8075 if (!elf_bad_symtab (abfd))
8076 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8077
8078 sym_hashes = elf_sym_hashes (abfd);
8079 sym_hashes_end = sym_hashes + extsymcount;
8080
8081 /* Hunt down the child symbol, which is in this section at the same
8082 offset as the relocation. */
8083 for (search = sym_hashes; search != sym_hashes_end; ++search)
8084 {
8085 if ((child = *search) != NULL
8086 && (child->root.type == bfd_link_hash_defined
8087 || child->root.type == bfd_link_hash_defweak)
8088 && child->root.u.def.section == sec
8089 && child->root.u.def.value == offset)
8090 goto win;
8091 }
8092
8093 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
8f615d07 8094 bfd_archive_filename (abfd), sec->name,
a7b97311 8095 (unsigned long) offset);
252b5132 8096 bfd_set_error (bfd_error_invalid_operation);
b34976b6 8097 return FALSE;
252b5132 8098
dc810e39 8099 win:
252b5132
RH
8100 if (!h)
8101 {
8102 /* This *should* only be the absolute section. It could potentially
8103 be that someone has defined a non-global vtable though, which
8104 would be bad. It isn't worth paging in the local symbols to be
8105 sure though; that case should simply be handled by the assembler. */
8106
8107 child->vtable_parent = (struct elf_link_hash_entry *) -1;
8108 }
8109 else
8110 child->vtable_parent = h;
8111
b34976b6 8112 return TRUE;
252b5132
RH
8113}
8114
8115/* Called from check_relocs to record the existance of a VTENTRY reloc. */
8116
b34976b6 8117bfd_boolean
252b5132 8118elf_gc_record_vtentry (abfd, sec, h, addend)
7442e600
ILT
8119 bfd *abfd ATTRIBUTE_UNUSED;
8120 asection *sec ATTRIBUTE_UNUSED;
252b5132
RH
8121 struct elf_link_hash_entry *h;
8122 bfd_vma addend;
8123{
0d1ea5c0
CM
8124 struct elf_backend_data *bed = get_elf_backend_data (abfd);
8125 int file_align = bed->s->file_align;
8126
252b5132
RH
8127 if (addend >= h->vtable_entries_size)
8128 {
8129 size_t size, bytes;
b34976b6 8130 bfd_boolean *ptr = h->vtable_entries_used;
252b5132
RH
8131
8132 /* While the symbol is undefined, we have to be prepared to handle
8133 a zero size. */
8134 if (h->root.type == bfd_link_hash_undefined)
8135 size = addend;
8136 else
8137 {
8138 size = h->size;
8139 if (size < addend)
8140 {
8141 /* Oops! We've got a reference past the defined end of
8142 the table. This is probably a bug -- shall we warn? */
8143 size = addend;
8144 }
8145 }
8146
8147 /* Allocate one extra entry for use as a "done" flag for the
8148 consolidation pass. */
b34976b6 8149 bytes = (size / file_align + 1) * sizeof (bfd_boolean);
252b5132
RH
8150
8151 if (ptr)
8152 {
dc810e39 8153 ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes);
3e932841 8154
fed79cc6
NC
8155 if (ptr != NULL)
8156 {
8157 size_t oldbytes;
252b5132 8158
a7b97311 8159 oldbytes = ((h->vtable_entries_size / file_align + 1)
b34976b6 8160 * sizeof (bfd_boolean));
a7b97311 8161 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
fed79cc6 8162 }
252b5132
RH
8163 }
8164 else
dc810e39 8165 ptr = bfd_zmalloc ((bfd_size_type) bytes);
252b5132 8166
fed79cc6 8167 if (ptr == NULL)
b34976b6 8168 return FALSE;
3e932841 8169
252b5132 8170 /* And arrange for that done flag to be at index -1. */
fed79cc6 8171 h->vtable_entries_used = ptr + 1;
252b5132
RH
8172 h->vtable_entries_size = size;
8173 }
3e932841 8174
b34976b6 8175 h->vtable_entries_used[addend / file_align] = TRUE;
252b5132 8176
b34976b6 8177 return TRUE;
252b5132
RH
8178}
8179
8180/* And an accompanying bit to work out final got entry offsets once
8181 we're done. Should be called from final_link. */
8182
b34976b6 8183bfd_boolean
252b5132
RH
8184elf_gc_common_finalize_got_offsets (abfd, info)
8185 bfd *abfd;
8186 struct bfd_link_info *info;
8187{
8188 bfd *i;
8189 struct elf_backend_data *bed = get_elf_backend_data (abfd);
8190 bfd_vma gotoff;
8191
8192 /* The GOT offset is relative to the .got section, but the GOT header is
8193 put into the .got.plt section, if the backend uses it. */
8194 if (bed->want_got_plt)
8195 gotoff = 0;
8196 else
8197 gotoff = bed->got_header_size;
8198
8199 /* Do the local .got entries first. */
8200 for (i = info->input_bfds; i; i = i->link_next)
8201 {
f6af82bd 8202 bfd_signed_vma *local_got;
252b5132
RH
8203 bfd_size_type j, locsymcount;
8204 Elf_Internal_Shdr *symtab_hdr;
8205
f6af82bd
AM
8206 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
8207 continue;
8208
8209 local_got = elf_local_got_refcounts (i);
252b5132
RH
8210 if (!local_got)
8211 continue;
8212
8213 symtab_hdr = &elf_tdata (i)->symtab_hdr;
8214 if (elf_bad_symtab (i))
8215 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8216 else
8217 locsymcount = symtab_hdr->sh_info;
8218
8219 for (j = 0; j < locsymcount; ++j)
8220 {
8221 if (local_got[j] > 0)
8222 {
8223 local_got[j] = gotoff;
8224 gotoff += ARCH_SIZE / 8;
8225 }
8226 else
8227 local_got[j] = (bfd_vma) -1;
8228 }
8229 }
8230
dd5724d5
AM
8231 /* Then the global .got entries. .plt refcounts are handled by
8232 adjust_dynamic_symbol */
252b5132
RH
8233 elf_link_hash_traverse (elf_hash_table (info),
8234 elf_gc_allocate_got_offsets,
8235 (PTR) &gotoff);
b34976b6 8236 return TRUE;
252b5132
RH
8237}
8238
8239/* We need a special top-level link routine to convert got reference counts
8240 to real got offsets. */
8241
b34976b6 8242static bfd_boolean
252b5132
RH
8243elf_gc_allocate_got_offsets (h, offarg)
8244 struct elf_link_hash_entry *h;
8245 PTR offarg;
8246{
8247 bfd_vma *off = (bfd_vma *) offarg;
8248
e92d460e
AM
8249 if (h->root.type == bfd_link_hash_warning)
8250 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8251
252b5132
RH
8252 if (h->got.refcount > 0)
8253 {
8254 h->got.offset = off[0];
8255 off[0] += ARCH_SIZE / 8;
8256 }
8257 else
8258 h->got.offset = (bfd_vma) -1;
8259
b34976b6 8260 return TRUE;
252b5132
RH
8261}
8262
8263/* Many folk need no more in the way of final link than this, once
8264 got entry reference counting is enabled. */
8265
b34976b6 8266bfd_boolean
252b5132
RH
8267elf_gc_common_final_link (abfd, info)
8268 bfd *abfd;
8269 struct bfd_link_info *info;
8270{
8271 if (!elf_gc_common_finalize_got_offsets (abfd, info))
b34976b6 8272 return FALSE;
252b5132
RH
8273
8274 /* Invoke the regular ELF backend linker to do all the work. */
8275 return elf_bfd_final_link (abfd, info);
8276}
8277
8278/* This function will be called though elf_link_hash_traverse to store
8279 all hash value of the exported symbols in an array. */
8280
b34976b6 8281static bfd_boolean
252b5132
RH
8282elf_collect_hash_codes (h, data)
8283 struct elf_link_hash_entry *h;
8284 PTR data;
8285{
8286 unsigned long **valuep = (unsigned long **) data;
8287 const char *name;
8288 char *p;
8289 unsigned long ha;
8290 char *alc = NULL;
8291
e92d460e
AM
8292 if (h->root.type == bfd_link_hash_warning)
8293 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8294
252b5132
RH
8295 /* Ignore indirect symbols. These are added by the versioning code. */
8296 if (h->dynindx == -1)
b34976b6 8297 return TRUE;
252b5132
RH
8298
8299 name = h->root.root.string;
8300 p = strchr (name, ELF_VER_CHR);
8301 if (p != NULL)
8302 {
dc810e39
AM
8303 alc = bfd_malloc ((bfd_size_type) (p - name + 1));
8304 memcpy (alc, name, (size_t) (p - name));
252b5132
RH
8305 alc[p - name] = '\0';
8306 name = alc;
8307 }
8308
8309 /* Compute the hash value. */
8310 ha = bfd_elf_hash (name);
8311
8312 /* Store the found hash value in the array given as the argument. */
8313 *(*valuep)++ = ha;
8314
8315 /* And store it in the struct so that we can put it in the hash table
8316 later. */
8317 h->elf_hash_value = ha;
8318
8319 if (alc != NULL)
8320 free (alc);
8321
b34976b6 8322 return TRUE;
252b5132 8323}
73d074b4 8324
b34976b6 8325bfd_boolean
73d074b4
DJ
8326elf_reloc_symbol_deleted_p (offset, cookie)
8327 bfd_vma offset;
8328 PTR cookie;
8329{
9ad5cbcf 8330 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
73d074b4
DJ
8331
8332 if (rcookie->bad_symtab)
8333 rcookie->rel = rcookie->rels;
8334
8335 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
8336 {
d6fe2dc1 8337 unsigned long r_symndx;
73d074b4
DJ
8338
8339 if (! rcookie->bad_symtab)
8340 if (rcookie->rel->r_offset > offset)
b34976b6 8341 return FALSE;
73d074b4
DJ
8342 if (rcookie->rel->r_offset != offset)
8343 continue;
8344
d6fe2dc1
AM
8345 r_symndx = ELF_R_SYM (rcookie->rel->r_info);
8346 if (r_symndx == SHN_UNDEF)
b34976b6 8347 return TRUE;
d6fe2dc1 8348
73d074b4 8349 if (r_symndx >= rcookie->locsymcount
6cdc0ccc 8350 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
73d074b4
DJ
8351 {
8352 struct elf_link_hash_entry *h;
8353
8354 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
8355
8356 while (h->root.type == bfd_link_hash_indirect
8357 || h->root.type == bfd_link_hash_warning)
8358 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8359
8360 if ((h->root.type == bfd_link_hash_defined
8361 || h->root.type == bfd_link_hash_defweak)
ed4de5e2 8362 && elf_discarded_section (h->root.u.def.section))
b34976b6 8363 return TRUE;
73d074b4 8364 else
b34976b6 8365 return FALSE;
73d074b4 8366 }
6cdc0ccc 8367 else
73d074b4
DJ
8368 {
8369 /* It's not a relocation against a global symbol,
44421011 8370 but it could be a relocation against a local
73d074b4
DJ
8371 symbol for a discarded section. */
8372 asection *isec;
6cdc0ccc 8373 Elf_Internal_Sym *isym;
73d074b4
DJ
8374
8375 /* Need to: get the symbol; get the section. */
6cdc0ccc
AM
8376 isym = &rcookie->locsyms[r_symndx];
8377 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
73d074b4 8378 {
6cdc0ccc 8379 isec = section_from_elf_index (rcookie->abfd, isym->st_shndx);
ed4de5e2 8380 if (isec != NULL && elf_discarded_section (isec))
b34976b6 8381 return TRUE;
73d074b4
DJ
8382 }
8383 }
b34976b6 8384 return FALSE;
73d074b4 8385 }
b34976b6 8386 return FALSE;
73d074b4
DJ
8387}
8388
8389/* Discard unneeded references to discarded sections.
b34976b6 8390 Returns TRUE if any section's size was changed. */
73d074b4 8391/* This function assumes that the relocations are in sorted order,
ab3acfbe 8392 which is true for all known assemblers. */
73d074b4 8393
b34976b6 8394bfd_boolean
65765700
JJ
8395elf_bfd_discard_info (output_bfd, info)
8396 bfd *output_bfd;
73d074b4
DJ
8397 struct bfd_link_info *info;
8398{
8399 struct elf_reloc_cookie cookie;
126495ed 8400 asection *stab, *eh;
73d074b4 8401 Elf_Internal_Shdr *symtab_hdr;
73d074b4
DJ
8402 struct elf_backend_data *bed;
8403 bfd *abfd;
99eb2ac8 8404 unsigned int count;
b34976b6 8405 bfd_boolean ret = FALSE;
73d074b4 8406
d6fe2dc1 8407 if (info->traditional_format
73d074b4 8408 || info->hash->creator->flavour != bfd_target_elf_flavour
65765700 8409 || ! is_elf_hash_table (info))
b34976b6 8410 return FALSE;
65765700 8411
73d074b4
DJ
8412 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
8413 {
163c1c30
L
8414 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
8415 continue;
8416
73d074b4
DJ
8417 bed = get_elf_backend_data (abfd);
8418
8419 if ((abfd->flags & DYNAMIC) != 0)
8420 continue;
8421
126495ed 8422 eh = bfd_get_section_by_name (abfd, ".eh_frame");
2d36fe5f
AH
8423 if (info->relocateable
8424 || (eh != NULL
8425 && (eh->_raw_size == 0
8426 || bfd_is_abs_section (eh->output_section))))
126495ed 8427 eh = NULL;
65765700 8428
99eb2ac8
AM
8429 stab = bfd_get_section_by_name (abfd, ".stab");
8430 if (stab != NULL
8431 && (stab->_raw_size == 0
8432 || bfd_is_abs_section (stab->output_section)
68bfbfcc 8433 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
99eb2ac8
AM
8434 stab = NULL;
8435
8436 if (stab == NULL
8437 && eh == NULL
8438 && bed->elf_backend_discard_info == NULL)
73d074b4
DJ
8439 continue;
8440
8441 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
73d074b4
DJ
8442 cookie.abfd = abfd;
8443 cookie.sym_hashes = elf_sym_hashes (abfd);
8444 cookie.bad_symtab = elf_bad_symtab (abfd);
8445 if (cookie.bad_symtab)
8446 {
99eb2ac8 8447 cookie.locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
73d074b4
DJ
8448 cookie.extsymoff = 0;
8449 }
8450 else
8451 {
8452 cookie.locsymcount = symtab_hdr->sh_info;
8453 cookie.extsymoff = symtab_hdr->sh_info;
8454 }
8455
6cdc0ccc
AM
8456 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
8457 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
c44233aa 8458 {
6cdc0ccc
AM
8459 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8460 cookie.locsymcount, 0,
8461 NULL, NULL, NULL);
c44233aa 8462 if (cookie.locsyms == NULL)
b34976b6 8463 return FALSE;
9ad5cbcf 8464 }
73d074b4 8465
99eb2ac8 8466 if (stab != NULL)
73d074b4 8467 {
99eb2ac8
AM
8468 cookie.rels = NULL;
8469 count = stab->reloc_count;
8470 if (count != 0)
8471 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8472 (abfd, stab, (PTR) NULL, (Elf_Internal_Rela *) NULL,
8473 info->keep_memory));
8474 if (cookie.rels != NULL)
73d074b4
DJ
8475 {
8476 cookie.rel = cookie.rels;
99eb2ac8
AM
8477 cookie.relend = cookie.rels;
8478 cookie.relend += count * bed->s->int_rels_per_ext_rel;
65765700
JJ
8479 if (_bfd_discard_section_stabs (abfd, stab,
8480 elf_section_data (stab)->sec_info,
73d074b4
DJ
8481 elf_reloc_symbol_deleted_p,
8482 &cookie))
b34976b6 8483 ret = TRUE;
6cdc0ccc 8484 if (elf_section_data (stab)->relocs != cookie.rels)
73d074b4
DJ
8485 free (cookie.rels);
8486 }
8487 }
8488
99eb2ac8 8489 if (eh != NULL)
65765700
JJ
8490 {
8491 cookie.rels = NULL;
99eb2ac8
AM
8492 count = eh->reloc_count;
8493 if (count != 0)
65765700 8494 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
40b829d4 8495 (abfd, eh, (PTR) NULL, (Elf_Internal_Rela *) NULL,
65765700 8496 info->keep_memory));
99eb2ac8
AM
8497 cookie.rel = cookie.rels;
8498 cookie.relend = cookie.rels;
8499 if (cookie.rels != NULL)
8500 cookie.relend += count * bed->s->int_rels_per_ext_rel;
8501
126495ed 8502 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
65765700
JJ
8503 elf_reloc_symbol_deleted_p,
8504 &cookie))
b34976b6 8505 ret = TRUE;
99eb2ac8
AM
8506
8507 if (cookie.rels != NULL
8508 && elf_section_data (eh)->relocs != cookie.rels)
65765700
JJ
8509 free (cookie.rels);
8510 }
8511
99eb2ac8
AM
8512 if (bed->elf_backend_discard_info != NULL
8513 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
b34976b6 8514 ret = TRUE;
73d074b4 8515
6cdc0ccc
AM
8516 if (cookie.locsyms != NULL
8517 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
8518 {
8519 if (! info->keep_memory)
8520 free (cookie.locsyms);
8521 else
8522 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
8523 }
73d074b4 8524 }
65765700 8525
126495ed 8526 if (info->eh_frame_hdr
2d36fe5f 8527 && !info->relocateable
126495ed 8528 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
b34976b6 8529 ret = TRUE;
99eb2ac8 8530
73d074b4
DJ
8531 return ret;
8532}
8533
b34976b6 8534static bfd_boolean
73d074b4
DJ
8535elf_section_ignore_discarded_relocs (sec)
8536 asection *sec;
8537{
40b829d4
AM
8538 struct elf_backend_data *bed;
8539
68bfbfcc 8540 switch (sec->sec_info_type)
65765700
JJ
8541 {
8542 case ELF_INFO_TYPE_STABS:
8543 case ELF_INFO_TYPE_EH_FRAME:
b34976b6 8544 return TRUE;
65765700
JJ
8545 default:
8546 break;
8547 }
40b829d4
AM
8548
8549 bed = get_elf_backend_data (sec->owner);
8550 if (bed->elf_backend_ignore_discarded_relocs != NULL
8551 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
b34976b6 8552 return TRUE;
65765700 8553
b34976b6 8554 return FALSE;
73d074b4 8555}