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