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