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