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